@hollis-labs/surface-chat
v0.28.7
Published
A conversation with an agent host, without any styling: the durable conversation wire, the chatstream/v1 event reducer, a store whose messages update one at a time, and scroll-follow. Visuals are the Surface registry's chat components.
Readme
@hollis-labs/surface-chat
A conversation with an agent host, without any styling. It is the behaviour half of Surface's chat; the visuals
are the registry's chat components (conversation, message, markdown, prompt-input, tool), and
web/src/agent/AgentPanel.tsx puts the two together.
Unpublished for now. Another app can take it as source (the page aliases @hollis-labs/surface-chat to src/index.ts); it
has no dependency but React, and React only for src/react.ts.
| File | What it is |
|---|---|
| src/stream.ts | Shared conversation event types and SSE frame decoding |
| src/reduce.ts | The conversation as data, and what each event does to it. Pure functions |
| src/conversation-store.ts | Durable conversation state, attachments, turns and approvals |
| src/react.ts | useChat(store, select) and its shorthands; useStickToBottom |
Why drawing stays cheap
- The shape. Items are held by id (
items) with their order beside them (ids). A streamed token replaces one item and nothing else:idskeeps its identity unless an item is added or removed, and so does every other item. A list drawn fromids, with each row subscribed to its own item, redraws only the row that changed. - The composer owns its draft. Typing changes component state in the composer and nothing in the store.
- One notification per frame. A burst of tokens between two frames is one redraw.
Measured on a live turn in the page (development build, window.__chatRenders, commits per component): typing 30
characters gave the composer 30 commits and the list and every row 0; a reply of 77 streamed tokens gave its own
row 73 commits, the list 2 (its two new items) and each of the four earlier messages 0.
History policy
The page attaches and reads the conversation stream from the beginning, then resumes from its last event. All turns arrive on that stream, including those sent by other clients. Resetting follows the successor conversation; there is no separate thread-history endpoint.
Not here
- Page tools. Surface tools run on the server.
- Cards. The panel draws each item through a table (
web/src/agent/items.tsx): a tool with a card of its own goes to that card, every other tool to the plain row.
npm test runs the reducer and store tests (node --test).
The conversation wire
conversation.ts, conversation-reduce.ts and conversation-store.ts bind the same reducer and
components to a durable conversation instead of one run at a time: the page attaches as a viewer or a
controller, reads one event stream from wherever it says, and submits turns when it holds the right to
type. createConversationStore is the panel's transport.
| What it handles | How |
|---|---|
| First paint and reload | Attach, read the stream from 0 (history arrives without deltas), then follow it. No separate history read |
| The sender's own message | Drawn at once under its request id; the stream's copy takes the same item, so it is never shown twice |
| A client that takes no deltas | A finished part carries its whole content, so stream: "committed" draws the same messages |
| A dropped connection | Resumes after the last event seen |
| The agent's side replaced (409 stale_attachment) | Attaches again and keeps its cursor |
| The conversation reset (410 conversation_closed) | Moves to the new conversation |
| Another window typing | A refused send says so; takeover() takes the input |
| Why a send failed | trySend() returns {ok, run} or {ok: false, reason, message}: a run going here, connecting, watching, another window has the input (403), the host still finishing a run (409), the host not answering (network, 502, 503, 504), or another refusal; send() answers the same as a boolean |
| A message into the running run | trySend(text, {now: true}) posts delivery "now" where state.steer (the conversation's capabilities.steer) is true; the 202 names the active run, which goes on, and the message is drawn from its chat.steer activity once (by request id). 409 steer_unsupported is the reason unsupported |
| Background work | state.background is the latest agent.background activity's tasks ([]: none; undefined: the host never said), from the stream or history |
| A turn the agent started by itself | state.agentRuns[run] holds run.start.meta's auto and cause, and first, the run's first text item |
| A second message right after a send | The page is busy from the turn's acceptance (the 202 names its run), not from the run's first event, so the second waits instead of being refused |
| A turn the host refuses (422) | The message stays, with the reason under it |
It assumes nothing about which agent runs the turns, only the wire (github.com/hollis-labs/libs/agent/chat),
at any base URL. The tests run it against an in-memory host (test/fake-host.ts) that follows the wire's
rules and event shapes. Surface's panel uses it for every configured agent.
sessionDraft(session) is a session's unsent text, one value shared by every composer of that session in
the page and followed in other tabs (kept in localStorage as chat.draft.<session>).
A window that only watches (role: "viewer") becomes a controller when it takes over. A page that is going
away should call dispose(): it detaches, which releases the input for the next page.
Joining message annotations
Text items produced by the reducer retain runId, partId, and messageId when the host supplied it.
Use lastMessageText(state, runId, messageId) to find the final text item for one message. A missing
identity yields no match; identical answer text or reused message IDs in another run do not match.
The identities survive terminal events and history replay. Separate asynchronous records, such as
pipeline decisions, belong in their own store and must not be injected into a completed chat run.
createConversationStore().onMessageEnd(fn) announces live assistant text completions once, with
the original wire timestamp as endedAt. History and reconnect do not reannounce them. Use that
clock for annotation visibility, even when a decision arrives later.
Output pipeline client
createDecisionStore({scope: {seat, session, conversation}, api}) keeps decisions beside chat state.
Its initial GET captures an event-log head; the independent SSE stream resumes after that head.
It deduplicates decisions and feedback, refetches after retention gaps or host replacement, and
joins ordinary messages by run and message identity. Recovered records without a run join by
message within the store's fixed session and conversation. Non-message subjects stay in the log.
Dispose and create a new store when the conversation or session changes.
The default pipelineApi() uses /api/m/chat/h, Surface's allowlisted proxy. After bounded SSE
reconnects fail, the store reads only in response to messageEnded(completion) or focus();
connect those to the conversation's onMessageEnd and the page's focus handler. There is no
decision polling timer. Call loadOlder() for older log entries and dispose() on unmount.
pipelineApi().feedback(id, request) writes log-only output feedback. Keep its
client_request_id unchanged across retries. It does not train the owner-input classifier or
apply a policy. The store consumes the resulting feedback event; detail and fallback GETs remain
authoritative when streaming is unavailable.
Wire types are generated from github.com/hollis-labs/libs/agent/pipeline v0.1.0, exported as
the type namespace Pipeline. Regenerate with npm run generate:pipeline --prefix packages/chat.
The package supplies transport and state; registry components supply the decision chip and log.
