Skip to content

artifactr.fastapi

The fastapi extra. See Serving over WebSocket and REST.

The thread protocol over WebSocket, and REST commands and reads, as a FastAPI router.

Include it in an application and give it the host's authentication::

async def resolve_actor(connection: HTTPConnection) -> tuple[str, Actor]:
    user = await authenticate(connection.headers)  # the application's own auth
    return user.tenant_id, UserActor(id=user.id, name=user.name)


app.include_router(
    artifactr_router(workspaces, runner, resolve_actor=resolve_actor), prefix="/v1"
)

Every command, over either transport, goes through artifactr.agent.Runner.execute, so it behaves identically (ADR-0048). See docs/protocol.md for the wire format.

The router

artifactr_router

artifactr_router(
    workspaces: Workspaces,
    runner: Runner[Any],
    *,
    resolve_actor: ResolveActor,
    hello_timeout: float = 10.0,
    outbox_size: int = 1000,
    presence: PresenceChannel | None = None,
    watch_recheck: float = RECHECK,
    tracer_provider: TracerProvider | None = None,
    meter_provider: MeterProvider | None = None,
) -> APIRouter

Build the router for the thread protocol, REST commands and reads.

Parameters:

Name Type Description Default
workspaces Workspaces

Opens tenant-scoped workspaces, and asks their authorize hook about each request, command and read.

required
runner Runner[Any]

Carries out commands, once per command_id, and runs the agent.

required
resolve_actor ResolveActor

Authenticates each request and connection.

required
hello_timeout float

Seconds a new connection has to send hello.

10.0
outbox_size int

Frames buffered for a slow connection before it is closed (4429).

1000
presence PresenceChannel | None

Passes presence between the connections that watch an artifact; by default, between this process's own, which serves an application that runs one.

None
watch_recheck float

Seconds between reads of a watcher's rights, which a lost grant or an integration turned off ends a watch within.

RECHECK
tracer_provider TracerProvider | None

Where artifactr.stream spans go. Defaults to the global one.

None
meter_provider MeterProvider | None

Where connection metrics go. Defaults to the global one.

None

The span of each REST request in a workspace (FastAPI's own, when it is instrumented) and each connection's artifactr.stream span are attributed to the tenant, workspace and actor.

ResolveActor module-attribute

ResolveActor = Callable[
    [HTTPConnection], Awaitable[tuple[TenantId, Actor]]
]

Authenticates a request or connection: returns its tenant and actor, or raises Unauthorized.

Unauthorized

Bases: Exception

Raise from resolve_actor to refuse a request (401) or connection (close 4401).

STATUS_CODES module-attribute

STATUS_CODES: dict[str, int] = {
    "version_conflict": 409,
    "invalid_state": 409,
    "not_enabled": 409,
    "validation_failed": 422,
    "patch_failed": 422,
    "not_found": 404,
    "forbidden": 403,
    "unsupported_protocol": 400,
}

The HTTP status for each rejection type.

Presence

PresenceChannel

Bases: Protocol

Passes presence between the peers in a room, in this process or across several.

join

join(
    room: Room, peer: str, deliver: Deliver
) -> list[PeerPresence]

Join a room, to be told of the others there; answer with what each last said.

Raises:

Type Description
InvalidState

If the room holds MOST_IN_A_ROOM peers already.

say

say(room: Room, frame: PeerPresence) -> None

Tell the others in a room what a peer says now.

Each peer's frames reach every other in the order the peer said them: a watcher keeps only the latest it was told, as the latest said.

leave

leave(room: Room, peer: str) -> None

Leave a room: the others are told, and the peer is told nothing more.

LocalPresence

LocalPresence(
    *,
    clock: Callable[[], float] = monotonic,
    most: int = MOST_IN_A_ROOM,
)

Rooms in this process: every connection to it shares them.

join

join(
    room: Room, peer: str, deliver: Deliver
) -> list[PeerPresence]

Join a room, to be told of the others there; answer with what each last said.

Raises:

Type Description
InvalidState

If the room holds as many peers as it may already.

say

say(room: Room, frame: PeerPresence) -> None

Tell the others in a room what a peer says now, and who has fallen silent.

leave

leave(room: Room, peer: str) -> None

Leave a room: the others are told, and the peer is told nothing more.

MOST_IN_A_ROOM module-attribute

MOST_IN_A_ROOM: Final = 50

The most peers in one room: each one's every frame goes to all the others.

SILENT module-attribute

SILENT: Final = 10.0

Seconds a peer may say nothing before it is announced as having left.

The OpenAPI document

The document committed as schemas/artifactr.openapi.json, from a bare application that mounts the router at its root.

openapi_app

openapi_app() -> FastAPI

A bare application that mounts only the router, and admits no one.

Its runner's agent never runs: no request gets past authentication to start a turn.

openapi_document

openapi_document() -> dict[str, Any]

The document openapi_app serves.