@chia1104/motoko-wire
Motoko's client contract: wire events, cursors, the session view reducer, the call protocol and the socket protocol's frames.
The client contract of Motoko: one domain-free event vocabulary (AgentWireEvent), its zod
schemas, the Cursor a client resumes a stream from, applyEvent, the pure reducer that folds
events into a session view, and the call protocol every transport carries to an instance. It
imports nothing but zod: no pi, no provider SDK, no Node, so a browser bundle can use it.
The server derives every event from committed conversation state. Live events, replayed history and a resumed stream fold through the same reducer, so a client that attaches late, reconnects, or follows a restarted process ends with the view a client connected all along has.
Usage
import { applyEvent, emptySessionView, streamItemSchema } from "@chia1104/motoko-wire";
import type { AgentSessionView, Cursor } from "@chia1104/motoko-wire";
/** A client folding a session's stream, whatever the transport (SSE, WebSocket, Electron IPC). */
export class SessionClient {
view: AgentSessionView = emptySessionView();
/** The last position folded; hand it back to resume, in this process or after a restart. */
cursor: Cursor | undefined;
/** Feed every message the transport delivers. */
receive(message: unknown): void {
const item = streamItemSchema.parse(message);
this.view = applyEvent(this.view, item.event);
this.cursor = item.cursor;
}
/** The calls an operator should decide now. */
pendingCalls() {
return this.view.items.filter(
(item) => item.kind === "tool" && this.view.pendingApprovals.includes(item.toolCallId),
);
}
}(examples/client.ts, typechecked with the package.)