Skip to main content
If your front end already uses the Vercel AI SDK’s UI hooks (useChat from @ai-sdk/react, and the Vue, Svelte and Angular equivalents), you can keep them and run a Lousho agent behind the route. Three functions, exported from the package root, do the translation. They import nothing from ai, use no node:* module, and work on Node, Workers and any Fetch-based runtime.

Route handler

The vendor/ model prefix chooses the provider; with OpenRouter use openrouter/<vendor>/<model> (e.g. openrouter/openai/gpt-4o-mini).
Without a session, pass every message instead: toUIMessageStreamResponse(agent.stream(fromUIMessages(messages))).

Client

This snippet targets ai 6 and 7 (useChat takes a transport, and sendMessage posts the message). On ai 4 — also a supported peer — useChat({ api: '/api/chat' }) takes the endpoint directly and returns input / handleInputChange / handleSubmit instead of sendMessage; ai 5 is not in the peer range.

Event mapping

A streaming tool’s snapshots arrive as preliminary outputs, which useChat shows as the tool part’s output with preliminary: true; the tool.done chunk after them has no preliminary and is the final output. Events with no UI counterpart (retries, compaction, permission decisions, guardrails, …) and the events of sub-agents are not forwarded. The finish chunk’s finishReason is the AI SDK’s (stop, length, content-filter, tool-calls, error; anything else, such as awaiting-approval or max-steps, is other).

Usage and cost

Usage goes in the finish chunk’s messageMetadata, so message.metadata on the finished assistant message has it:
usage is the run’s AgentEventUsage (absent when the run failed); loushoFinishReason is the run’s own finish reason, unmapped.

Approvals and ask_question

A tool that needs approval, or an ask_question call, pauses the run: the stream ends (finish with loushoFinishReason: "awaiting-approval") after a custom data part, which useChat shows as a part of type data-lousho-approval:
kind and question are present only for an ask_question; a tool waiting for an OAuth sign-in instead carries kind: 'sign-in' and signIn: { provider, displayName?, url } — show the link and, once the provider redirected back, approve it (“I’ve signed in”). Render the part with your own buttons and answer it with the session API’s approvals route (POST /chat/:sessionId/approvals/:approvalId, see Sessions and Approvals): { "approved": true } or { "approved": false, "note": "..." } for a tool, { "answer": "prod" } for a question. The response is the continuation as an event stream, which you can read with the UI bindings’ parser or ignore and call useChat’s sendMessage again; or, in your own route, call agent.approvals.streamResolve() / streamAnswer() and return toUIMessageStreamResponse() of the result, so the continuation renders in the same chat.

Inbound conversion

fromUIMessages() maps text parts to text, image/* file parts to image parts and other file parts to file parts (the part’s url, a data: or http(s) URL, becomes the data). Tool, reasoning, data, source and unknown parts are ignored, and messages that end up empty are dropped. The messages come from the client, so only user and assistant messages are kept: a system message (or any other role, or an entry that is not a message) is dropped, so a caller cannot add instructions to the agent’s prompt. Set instructions on the agent (createAgent({ instructions })), not in the request. A dedicated lousho/ai-sdk-ui subpath may follow; for now the functions come from the package root.