API
Three hooks, a provider and the agent integration, as the design doc defines them. A draft; nothing is published yet.
Three kinds of people use CoUI, each with its own interface: app developers use the hooks (@coui-ai/react); whoever connects a UI framework uses CoUIIntegration; whoever connects an agent framework uses CoAgentIntegration (both in @coui-ai/core). Schemas are any Standard Schema: Zod, Valibot, ArkType.
useCoState
State both actors read and write. The same call shape as useState, plus a key and an optional description.
function useCoState<T>(
key: string | readonly (string | number)[],
initial: T,
options?: string | { describe?: string; schema?: Schema<T>; agent?: 'write' | 'propose' | 'read' },
): [value: T, set: (next: T | ((prev: T) => T)) => void, layers: CoStateLayers<T>];
interface CoStateLayers<T> {
by: 'human' | 'agent' | 'default';
/** The agent's value when it is stored but not shown. */
approval: CoApproval<T> | null;
/** Drop the human layer: the agent's value (or initial) shows again. */
release(): void;
}set writes the human layer; the agent writes through the set tool. agent: 'propose' makes every agent write wait; 'read' shows the key in context with no way to write it. An array key such as ['item', id, 'qty'] is joined with /.
const [title, setTitle, layers] = useCoState('title', '', { describe: 'Title', agent: 'propose' });
// …
{layers.approval && (
<p>
The agent suggests “{layers.approval.value}”
<button onClick={layers.approval.approve}>Accept</button>
<button onClick={layers.approval.reject}>Reject</button>
</p>
)}useCoRef
Makes an element something both actors can point at. The agent sees it as #id with its description.
function useCoRef<E extends Element = Element>(id: string, describe: string): RefCallback<E>;While mounted, the element carries data-co-pointed with the actors pointing at it; the app styles it:
[data-co-pointed~='human'] { outline: 2px solid #f2a100; }
[data-co-pointed~='agent'] { outline: 2px dotted #7a4fd6; }useCoCallback
A callback the person can trigger and the agent can call. Like useCallback with no deps array: it always runs the latest closure.
function useCoCallback<A = void, R = void>(
name: string,
fn: (args: A) => R | Promise<R>,
options?: string | { describe?: string; schema?: Schema<A>; needsApproval?: boolean; undo?: (args: A, result: R) => void },
): [run: (args: A) => Promise<R>, approvals: readonly CoApproval<A>[]];run is the person's: it runs at once. With needsApproval, each agent call waits in approvals until the person approves. Without undo, a callback's commits cannot be undone.
CoApproval
One type for everything that waits for the person, a state or a tool call.
interface CoApproval<T = unknown> {
id: string;
key: string;
/** The new value of a state, or the arguments of a tool call. */
value: T;
name?: string;
said?: string;
approve(): void;
reject(): void;
}CoUIProvider and useCoUI
function CoUIProvider(props: { children: ReactNode; store?: CoStore }): JSX.Element;
function useCoUI(): {
approvals: readonly CoApproval[];
history: History; // undo(by?), redo(by?), canUndo(by?), canRedo(by?), batch(fn)
pointing: { readonly human: readonly string[]; readonly agent: readonly string[] };
};The agent integration
interface CoAgentIntegration {
/** set, call, point: the same byte for byte, always. */
readonly tools: readonly ToolSchema[];
/** The context as text, in full or since a version, plus the version. */
context(since?: number): { text: string; version: number };
/** Run one tool call. Never throws. */
execute(call: ToolCall, seen?: { version: number; name?: string; said?: string }): Promise<ToolResult>;
}
type ToolResult =
| { status: 'applied'; value: unknown }
| { status: 'pending'; reason: 'human-set' | 'needs-approval'; current: unknown }
| { status: 'stale'; current: unknown }
| { status: 'rejected'; reason: string };| Tool | Input | For |
|---|---|---|
set | { key, value } | a useCoState |
call | { name, args } | a useCoCallback |
point | { id } | any useCoRef |
A context, sent with the person's message:
state:
filter = {"maxPrice":80} Product filter { maxPrice: number | null } (by: agent)
cart = [] Cart { id: string }[] (by: default)
on screen:
#p1 Runner X, $112 <- user is pointing here
#p2 Trail Lite, $79
callbacks:
addToCart({ id: string }) Add to cart (needs approval)The AI SDK
@coui-ai/ai-sdk has two things: coTools, the three tools with no execute, and withCoContext, which appends each user message's metadata.coui to its text.
// server
const result = streamText({
model,
messages: await convertToModelMessages(withCoContext(messages)),
tools: coTools,
});On the client, useChat's onToolCall passes each call to store.agent.execute() and addToolOutput sends the result back; send() attaches store.agent.context(seen) as metadata.coui.