//#region src/react/hooks/use-agent-chat.d.ts /** * One Responses-API-shaped event yielded by the agents plugin SSE stream. * * The hook handles the two paths every chat UI needs — accumulating * `content` from `response.output_text.delta` and capturing `threadId` * from `appkit.metadata` — and surfaces everything else (tool calls, * approval gates, status events, etc.) through {@link UseAgentChatOptions.onEvent}. * * Fields beyond `type` are intentionally loose because the agents plugin * forwards adapter-specific shapes verbatim. Treat unknown fields as * opaque pass-through. */ interface AgentChatEvent { type: string; delta?: string; item_id?: string; item?: { type?: string; id?: string; call_id?: string; name?: string; arguments?: string; output?: string; status?: string; }; content?: string; data?: Record; error?: string; sequence_number?: number; output_index?: number; approval_id?: string; stream_id?: string; tool_name?: string; args?: unknown; annotations?: Record; } interface UseAgentChatOptions { /** * Agent name registered with the `agents()` plugin (e.g. `"assistant"`, * `"helper"`). Send-time payload includes this so the plugin routes the * turn to the right `AgentDefinition`. */ agent: string; /** * Override the chat endpoint. Default `"/api/agents/chat"` matches the * route the agents plugin mounts under its prefix. Useful when the * server mounts under a non-default base path or when proxying. */ endpoint?: string; /** * Called for every parsed SSE event before any state update. Use this * to drive tool-call rows, approval cards, inspectors, or anything * beyond the streaming text content. Errors thrown here are swallowed * so a buggy handler can't kill the stream. */ onEvent?: (event: AgentChatEvent) => void; } interface UseAgentChatResult { /** Accumulated assistant text from `response.output_text.delta` events. */ content: string; /** * Every parsed event, in order. Provided for components that need to * render historical tool calls or replay state after a remount — * lighter than re-deriving from message history. For one-off side * effects prefer {@link UseAgentChatOptions.onEvent}. */ events: AgentChatEvent[]; /** * Thread id captured from the first `appkit.metadata` event of the * stream. Subsequent `send()` calls automatically forward this so the * server reuses the same thread. */ threadId: string | null; /** True while an SSE stream is open. */ isStreaming: boolean; /** Last error message (cleared on next successful `send()`). */ error: string | null; /** * Send a user turn and stream the response. Aborts any in-flight * stream. Resolves when the stream completes (success or error). */ send: (message: string) => Promise; /** * Discard accumulated content, events, and threadId. Aborts any * in-flight stream. Use when switching agents or starting a fresh * conversation. */ reset: () => void; } /** * React hook for chatting with an agent registered via the `agents()` * plugin. Wraps {@link connectSSE} (which owns the buffer cap, abort * composition, retry/backoff, and frame parsing) with the small amount * of stateful glue every chat UI needs: accumulated assistant text, * thread id, streaming flag, and an event callback. * * The hook is intentionally lower-level than a full chat component — * it owns one stream at a time, not a multi-turn message history. The * caller composes its own messages array (typically a `useState`) and * appends to it via the `onEvent` callback for tool calls and via the * `content` field for assistant text. * * @example * ```tsx * function Chat({ agent }: { agent: string }) { * const [messages, setMessages] = useState([]); * const { content, threadId, isStreaming, send, reset } = useAgentChat({ * agent, * onEvent(ev) { * if (ev.type === "response.output_item.added" && ev.item?.type === "function_call") { * setMessages((m) => [...m, { role: "tool", name: ev.item?.name, args: ev.item?.arguments }]); * } * }, * }); * // `content` reflects the latest assistant turn; reset() between conversations. * // ... * } * ``` */ declare function useAgentChat({ agent, endpoint, onEvent }: UseAgentChatOptions): UseAgentChatResult; //#endregion export { AgentChatEvent, UseAgentChatOptions, UseAgentChatResult, useAgentChat }; //# sourceMappingURL=use-agent-chat.d.ts.map