Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

providers.md

92 lines4.9 KB

1# Provider architecture
2​
3> For maintainers. Using T3 Code? See [docs/user](../user/).
4​
5A provider is the agent runtime that does the actual work. T3 Code supports several, and the
6orchestration layer does not know which one is behind a thread.
7​
8## Built-in drivers
9​
10[`builtInDrivers.ts`][drivers] exports `BUILT_IN_DRIVERS` with five entries:
11​
12| Driver kind | Driver source |
13| ------------- | --------------------------------------- |
14| `codex` | [`Drivers/CodexDriver.ts`][codex] |
15| `claudeAgent` | [`Drivers/ClaudeDriver.ts`][claude] |
16| `cursor` | [`Drivers/CursorDriver.ts`][cursor] |
17| `grok` | [`Drivers/GrokDriver.ts`][grok] |
18| `opencode` | [`Drivers/OpenCodeDriver.ts`][opencode] |
19​
20Each driver declares its `driverKind`, a `configSchema`, and a `create` function that builds an
21adapter in a child scope. Adapter implementations live beside them in
22`apps/server/src/provider/Layers/` (`CodexAdapter.ts`, `ClaudeAdapter.ts`, and so on) and conform to
23[`ProviderAdapter.ts`][adapter]. Read the driver plus its adapter to see how a specific agent's
24transport, config, and event shapes are mapped.
25​
26## Registry and routing
27​
28Two registries separate configuration from live processes:
29​
30- [`ProviderInstanceRegistry`][instances] keys configured instances by `ProviderInstanceId`. Creating
31 one looks up the driver by `driverKind`, decodes `entry.config` with that driver's schema, opens a
32 child scope, and calls `driver.create`.
33- [`ProviderAdapterRegistry`][registry] resolves an instance ID to its live adapter via
34 `getByInstance`.
35​
36[`ProviderService`][service] sits on top. It combines the adapter registry with the provider session
37directory to route session and turn operations for a thread, so callers name a thread, not an agent.
38​
39Adding a driver means writing the driver plus adapter and adding it to `BUILT_IN_DRIVERS`. No
40orchestration, contract, or client change is required for the common case.
41​
42## How provider work is requested
43​
44Clients never call a provider directly. They dispatch orchestration commands over the RPC method
45`orchestration.dispatchCommand`, defined with the rest of the orchestration surface in
46[`orchestration.ts`][contracts]. The client-dispatchable provider-facing commands are
47`thread.turn.start`, `thread.turn.interrupt`, `thread.approval.respond`,
48`thread.user-input.respond`, `thread.checkpoint.revert`, and `thread.session.stop`, plus the mode
49setters `thread.runtime-mode.set` and `thread.interaction-mode.set`.
50​
51The engine persists an event for the command, and a server-side reactor performs the provider call.
52Provider output comes back as internal commands such as `thread.message.assistant.delta` and
53`thread.session.set`, which clients observe through `orchestration.subscribeThread`. See
54[overview.md](./overview.md) for the command/event loop.
55​
56## Server-side workers
57​
58Provider work flows through three queue-backed workers. All three are built with
59`makeDrainableWorker` from [`DrainableWorker.ts`][worker] and expose `drain` for deterministic test
60synchronization.
61​
621. [`ProviderRuntimeIngestion`][ingest] consumes provider runtime streams and emits orchestration
63 commands.
642. [`ProviderCommandReactor`][cmd] reacts to orchestration intent events and dispatches provider
65 calls.
663. [`CheckpointReactor`][checkpoint] captures workspace checkpoints on turn start and completion, and
67 performs reverts.
68​
69### Buffered assistant delivery
70​
71A thread in `buffered` assistant delivery mode accumulates assistant text instead of streaming each
72delta. The buffer is not held until turn completion. In [`ProviderRuntimeIngestion`][ingest],
73`MAX_BUFFERED_ASSISTANT_CHARS` is 24,000: the append that would exceed it invalidates the buffer and
74spills the whole accumulated text as one delta. The buffer also flushes at interaction boundaries,
75when a request opens (approval) or user input is requested, via
76`flushBufferedAssistantMessagesForTurn`.
77​
78[drivers]: ../../apps/server/src/provider/builtInDrivers.ts
79[codex]: ../../apps/server/src/provider/Drivers/CodexDriver.ts
80[claude]: ../../apps/server/src/provider/Drivers/ClaudeDriver.ts
81[cursor]: ../../apps/server/src/provider/Drivers/CursorDriver.ts
82[grok]: ../../apps/server/src/provider/Drivers/GrokDriver.ts
83[opencode]: ../../apps/server/src/provider/Drivers/OpenCodeDriver.ts
84[adapter]: ../../apps/server/src/provider/Services/ProviderAdapter.ts
85[instances]: ../../apps/server/src/provider/Services/ProviderInstanceRegistry.ts
86[registry]: ../../apps/server/src/provider/Services/ProviderAdapterRegistry.ts
87[service]: ../../apps/server/src/provider/Layers/ProviderService.ts
88[contracts]: ../../packages/contracts/src/orchestration.ts
89[worker]: ../../packages/shared/src/DrainableWorker.ts
90[ingest]: ../../apps/server/src/orchestration/Layers/ProviderRuntimeIngestion.ts
91[cmd]: ../../apps/server/src/orchestration/Layers/ProviderCommandReactor.ts
92[checkpoint]: ../../apps/server/src/orchestration/Layers/CheckpointReactor.ts