Skip to content

Chat client and state ​

ChatTransport ​

Everything the views need of a host goes through one object:

ts
interface ChatTransport {
    request<T extends AgentRequestType>(type: T, payload: ChatRequestMap[T]['payload']): Promise<ChatRequestMap[T]['result']>;
    on<E extends AgentEventType>(event: E, handler: (payload: ChatEventMap[E]) => void): () => void;
    readonly status: 'connecting' | 'open' | 'closed';
    subscribeStatus(handler: (status: ChatTransportStatus) => void): () => void;
}

The names and payloads are those of the agent protocol. A refused request rejects with an error with a string code. ChatTransportError(code, message) is such an error, errorCode(error) reads the code of any error, and isConnectionError(error) is true for not-connected and disconnected. A reconnecting socket keeps the same transport object and reports through subscribeStatus; when status turns open again, the views reload their state.

portTransport(port, options?) returns a PortTransport, a transport over a FramePort with a close(). It is open from the start, drops malformed frames with a warning, and reads results and events with readTolerant. A result that still fails its schema rejects with bad-reply. PortTransportOptions has one member, onDegraded(source, degraded), which runs for every reply or event that was read by giving something up, with the request or event and the WireDegradation list. The package keeps no telemetry, so your app reports these if it wants to. The transport cannot reconnect and has no timeout. The port's owner calls close(), which rejects waiting requests with disconnected, and closes the port itself.

Newer hosts ​

A host may run a newer version than the window. A transport of your own reads replies and events with readTolerant too, so its results match ChatRequestMap and ChatEventMap. Those maps type every result as Tolerant, and the state holds what they hand over:

TypeIs
ChatThreadItemAn item as the store holds it, ChatUnknownItem included
ChatThreadTurnA turn item as the store holds it
ChatThreadInfoA chat's info, where a state word of a newer host is kept as it came
ChatThreadEventAn event as the store applies it; ChatUnknownEvent changes nothing

An unknown item gets one muted row in its place. An approval or question among them that still waits says to answer it on the machine. The composer never offers it, and no fork, bookmark or other action is on the row. A goal in an unknown state offers only a clear. A runtime mode the window does not know reads as set on the machine. Usage of a provider it has no name for stays off the usage page. The words of the row come from useUnknownItemWords on the host.

ChatClient ​

new ChatClient(transport, sink, providers?) holds one connection's chats. It writes into a ChatSink: chatSink(keyOf) for the shared store, and providerSinkFor(scopeId) for the list of CLIs.

MethodWhat it does
open(chatId, options)Creates or loads the chat and attaches with the newest page of items. false while the transport is down; it attaches once it is up.
detach(chatId)Stops streaming the chat here. The agent goes on.
send(chatId, text, extras?)Returns { queued, turnId? }: whether it waits behind a running turn.
retarget(chatId, provider, selection)Moves a chat that has not spoken yet to another CLI.
loadEarlier(chatId, cursor)Reads the page before the oldest item held.
inspect(chatId)Reads the recent items once, without attaching, for a preview.
clear(chatId, force?)Starts the chat over; refused with chat-busy during a turn unless force.
restart(chatId)Ends the chat's CLI and keeps the conversation for the next message; refused with chat-busy while the chat works.
kill(chatId)Forgets the chat here and asks the host to remove it.
listSkills(chatId, alike?)The CLI's skills, for the $ picker. Calls with the same alike key share a request still on the wire.
addBookmark, renameBookmark, removeBookmarkReturn the chat's whole bookmark list.
removeVisual(chatId, visualId)Deletes a visual for good and returns the chat's visuals.
setPreferences(payload)Tells the host the defaults for chats it starts itself, also after every reconnect.
loadProviders(), loadStatuses()Reload the CLIs and every chat's status; open does this too.
isMounted(chatId), dispose()Whether a chat is open here, and releasing everything. dispose leaves the agents running.

open takes provider, account, cwd, resume, selection and runtimeMode for a new chat; an existing chat keeps its own. On a reconnect the client reattaches every open chat and catches up on what it missed. Open a chat id once per client, since a second open and a detach cancel each other.

A client of your own that keeps something per open node can follow the same reconnect rule with MountedRegistry, a Map of MountedEntry records with an attached flag. detachAll marks everything detached when the link goes, and reattachAll(reattach) attaches what is mounted but not attached again, a few at a time, once the link is back.

Close a scope in the reverse order of opening it:

ts
stopAccounts();
scope.chats.dispose();
transport.close();
useChats.getState().forgetWhere(scope.owns);
useProvidersStore.getState().forget(scope.id);
useProviderAccountsStore.getState().forget(scope.id);
useUsageStore.getState().forget(scope.id);

Reading the stores ​

The stores are Zustand stores shared by every scope.

  • useChats holds a ChatState per key: info, items by id, order, history when only the newest page is held, waitingBefore, subagentsBefore, workflowsBefore and outlineBefore for what lies before that page, bookmarks and visuals. statusByKey holds only the info and changes only with it.
  • useChatRow(chatId, select) reads one chat of the scope in context. Select only the field you draw, since a streamed word replaces items.
  • structure is items without the growth of streaming text. Derive rows from it, so a word does not redraw the whole thread.
  • waitingRequestsOf(chat) lists every waiting approval and question, also those before the loaded page.
  • useProviders(select) and useProviderAccounts(select) read the CLIs and accounts of the scope in context; providersOf(scopeId) and providerAccountsOf(scopeId) do the same outside React. knownAccounts(row) is undefined until the host answered and null for a host without accounts.
  • applyEvent(state, event) is the pure step the store takes for an event, for a store of your own.
  • ChatsById holds the rows keyed with the scope's keyOf, and ChatStatuses what each chat is doing without its thread. ChatHistoryPage is an earlier page of a thread than the newest one chat.attach hands over.
  • ProvidersRow is a scope's entry in the providers store, and the accounts store keeps one per scope too.
tsx
function Title({ chatId }: { chatId: string }) {
    const title = useChatRow(chatId, (row) => row?.info.suggestedTitle ?? 'New chat');

    return <span>{title}</span>;
}

Licenses and third-party notices are listed in each package.