src/agents/sources.ts exports the agents package's sources to the data module. This page covers the HTTP side: reads, commands and provider configuration. The three WebSocket sources are in Agents: WebSocket streams.
The request helper
request(store, base, account, action, now, kind) is the single HTTP path:
POST agentEndpoint(base, 'message') with headers Content-Type: application/cbor and Accept: application/cbor.
The body is encodeAgentEnvelope of the action, signed by signer(store, base, account).
The reply is decoded as CBOR. If decoding fails, the error is Agent server returned HTTP <status>., because "Proxies and the server's own 404 answer in text".
A non-2xx status or a reply with _ === 'Error' throws the reply's message.
signer returns the test signer when the account is the test identity, and throws if that identity is used with any base other than MOCK_AGENTS_URL ("The test identity can only use the local mock agents server."). Otherwise it calls signerFor(store, account) from identity and throws "Choose an identity in Me to use agents." when the key is absent.
Clock handling
Sources have no clock, so now is always an argument. The module distinguishes two kinds of request (comment, lines 38-45):
Commands sign with the caller's now exactly. The action computes it as time.epochAtZero + performanceNow() at the moment of the tap.
Reads sign with readTime(base, account, now) = max(now, last server time seen). A resource's now is a snapshot taken when the screen was visited and goes stale while the screen stays open. Live frames carry the server's own time (connected.connectedAt, an appended event's createdAt, a changed session's updatedAt), and noteServerTime keeps the maximum per server|account. It "only moves forward (replayed history carries old stamps)" and "is not capped: a capped value made reads fail on sessions open longer than the server's skew window".
In addition, the root's agentsHealth task bumps agentsReadTime every 60 seconds, so idle reads get a fresh caller clock without reopening any stream.
Read sources
Source | Arguments | Server actions | Returns |
|---|---|---|---|
|
|
| |
|
|
| |
|
|
| |
|
|
|
Notes:
When enabled is false or an id is missing, each source returns its empty value without a request. Every reply carries owner (base|account) so the root can discard a stale one.
agentsOverview drops sessions with a parentSessionId from the list and remembers each provider's type in providerTypes for model curation.
agentsSession caches the session's events, trigger context and plan in module maps keyed owner|sessionId. The stream source continues from them.
agentsLoadRuns takes agentsThread.revision only to be asked again when the transcript changes. It sorts runs by createdAt, loads each run's tree, and merges with runSnapshots: a stored record wins unless the incoming one has a newer updatedAt. The comment: "Never let its older snapshot put a completed child (or root) back into a running state".
Commands
agentsCommand takes base, account, command, id, text, provider, model, prompt, now and maps the command to one action:
Command | Action sent |
|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The client id is a 16 byte random hex nonce from crypto.getRandomValues. The reply is AgentReply {id, command, error} where id is continuedToSessionId, sessionId, agentId or the input id, in that order. A send whose reply names a different session makes the root replace the route with the continued session. Errors never throw: they come back in error.
Provider save flow
agentsSaveProvider takes base, account, type, name, apiKey, baseUrl, now:
Validate: a known provider type, a name, an API key when the type requires one, an HTTPS or loopback server when a key is present, a base URL for custom.
Resolve the endpoint: the typed base URL or the type's default, checked with normalizeAgentServerUrl.
If a key was given, send SetSecret {name: '<label>-api-key', value: <UTF-8 bytes>, metadata: {provider: type}}.
Send SetModelProvider {name: label, provider: {type, baseUrl?, secretRefs?: {apiKey: '<label>-api-key'}}}.
The key is sent as bytes inside the signed envelope and is never stored on the device.
Settings sources
agentsNormalizeServer([input]) returns {id: '', url, error} from normalizeAgentServerUrl.
agentsUseTestKey() creates a signer from 32 random bytes, keeps it in module memory only, and returns its account id with MOCK_AGENTS_URL (http://127.0.0.1:3068). "Only the unlinked /agents-test route invokes this source. It never reads or persists device keys."
The file also spreads an exactTime fallback into sources for the build-time render; see the data module.
Tests
parity.test.ts checks provider submissions for every non-custom type, the custom base URL error, idle reads using the caller clock, and signing through the real identity store. review.test.ts checks that commands ignore a hostile server time, that reads adopt server time only forward, and that a text 404 reports its status.
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime