Custom Chat UI
Build a custom chat client against AgentBlit chat v2 (AI SDK useChat)
Build your own chat UI against AgentBlit chat v2. The server speaks the standard AI SDK UI message stream. Prefer useChat + DefaultChatTransport — do not invent a custom protocol.
Reference implementation
Clone and run the public examples first. Copy from them; do not re-derive the contract.
Repo: github.com/agentblit/agentblit-chat-examples
| Folder | Use when |
|---|---|
simplest/ | Minimal text chat + stop + tool approvals |
full/ | Threads, files, markdown, tool activity, elicitation, MCP Apps |
git clone https://github.com/agentblit/agentblit-chat-examples.git
cd agentblit-chat-examples/simplest
cp .env.example .env.local
# set NEXT_PUBLIC_AGENTBLIT_AGENT_ID
# optional: NEXT_PUBLIC_AGENTBLIT_API_BASE=http://localhost:3000
pnpm install && pnpm devStart with simplest/. Pull pieces from full/ only when you need them.
Contract
Endpoints
Base: {API_BASE}/api/v2/agents/{agentId}
| Method | Path | Purpose |
|---|---|---|
POST | /chat | Send / resume a turn (UI message stream) |
GET | /threads | List threads |
GET | /threads/{threadId}/messages | Load UI messages for a thread |
DELETE | /threads/{threadId} | Delete a thread |
POST | /files/upload | Presign uploads → then PUT to each uploadUrl |
POST | /threads/{threadId}/apps/bridge | MCP App iframe host (full/ only) |
Chat request
POST {API_BASE}/api/v2/agents/{agentId}/chat
Content-Type: application/json
{
"id": "<thread-id>",
"messages": [ /* AI SDK UIMessage[] */ ]
}Response: AI SDK UI message stream (same shape useChat expects).
Rules (do not break these)
- Server owns LLM history. Client sends
id+ messages for the UI / interrupt decisions. Do not maintain a parallel OpenAI-style history for the model. - Reuse one
threadIdfor the conversation (useChat({ id: threadId })). Generate withgenerateId()or use a URL segment. - Approvals and elicitation resume the same assistant message. After the user decides, let
useChatPOST again with updated tool parts — do not start a new user turn. - Enable auto-resume with:
sendAutomaticallyWhen: ({ messages }) =>
lastAssistantMessageIsCompleteWithApprovalResponses({ messages }) ||
lastAssistantMessageIsCompleteWithToolCalls({ messages }),- Optional header
X-Agentblit-User: <end-user-id>scopes threads per end user. CORS is open for custom UIs today.
Step 1 — Minimal chat (required)
Dependencies: ai, @ai-sdk/react.
const chatUrl = `${API_BASE}/api/v2/agents/${agentId}/chat`;"use client";
import { useChat } from "@ai-sdk/react";
import {
DefaultChatTransport,
generateId,
lastAssistantMessageIsCompleteWithApprovalResponses,
lastAssistantMessageIsCompleteWithToolCalls,
} from "ai";
import { useMemo } from "react";
export function Chat({ chatUrl }: { chatUrl: string }) {
const threadId = useMemo(() => generateId(), []);
const transport = useMemo(
() => new DefaultChatTransport({ api: chatUrl }),
[chatUrl],
);
const { messages, sendMessage, status, stop, addToolApprovalResponse, error } =
useChat({
id: threadId,
transport,
sendAutomaticallyWhen: ({ messages }) =>
lastAssistantMessageIsCompleteWithApprovalResponses({ messages }) ||
lastAssistantMessageIsCompleteWithToolCalls({ messages }),
});
// Render messages[].parts (text + tool parts).
// Send: await sendMessage({ text })
// Stop: stop()
// Approve: addToolApprovalResponse({ id: approvalId, approved: true|false })
}Exact UI wiring: simplest/src/components/chat.tsx.
Tool approvals
When a tool part has state === "approval-requested" and approval.isAutomatic === false, show Allow / Deny:
addToolApprovalResponse({ id: part.approval.id, approved: true });
// or approved: falsesendAutomaticallyWhen continues the run after the response.
Step 2 — Threads (optional)
Use when the UI needs history across reloads.
| Call | Client helper in examples |
|---|---|
GET .../threads | listThreads |
GET .../threads/{id}/messages | loadThreadMessages → pass as useChat({ messages: initial }) |
DELETE .../threads/{id} | deleteThread |
Put threadId in the URL (see full/ /c/[threadId]). Remount the chat panel with key={threadId} when switching threads.
Source: full/src/lib/api.ts.
Step 3 — File attachments (optional)
POST .../files/uploadwith{ files: [{ filename, mediaType, size }] }.PUTeach file to the returneduploadUrl.- Send AI SDK file parts:
await sendMessage({
text,
files: [
{
type: "file",
mediaType,
filename,
url, // download URL from the upload ticket
},
],
});Limits (server-enforced): up to 5 files, 10 MB each. Images, PDF, and text-like types are supported.
Source: full/src/lib/upload.ts.
Computer screenshots from tools arrive as normal assistant file parts when the tool sets share_with_user. Render part.type === "file" like any other attachment. Ignore markdown images that use a fake attachment: URL.
Step 4 — Elicitation (optional)
Some tools pause for structured user input. Detect elicitation on a tool part via toolMetadata.elicitation while state === "input-available". Submit with:
addToolOutput({
tool: toolName,
toolCallId,
output: {
action: "accept" | "decline" | "cancel",
content?: Record<string, string>, // field values when accept
},
});Source: full/src/components/message.tsx + submitElicitation in chat-panel.tsx.
Step 5 — MCP Apps (optional)
- Listen for stream data parts of type
data-mcp-app. - Host the app iframe through:
POST {API_BASE}/api/v2/agents/{agentId}/threads/{threadId}/apps/bridgeBridge methods used by the iframe: tools/call, resources/read, ui/update-model-context.
See the full/ README for the intended host shape. Skip this until you need interactive MCP App UIs.
Checklist for implementers / AI agents
Copy this order:
- Clone agentblit-chat-examples and run
simplest/. - Wire
DefaultChatTransport→POST /api/v2/agents/{agentId}/chat. - Set
useChatid+sendAutomaticallyWhenas above. - Render
message.parts(text, tool parts, laterfile). - Implement approval Allow / Deny via
addToolApprovalResponse. - Only then add threads, uploads, elicitation, or MCP Apps from
full/.
Do not:
- Call chat v1 (
/api/agents/.../chat) for new UIs. - Send OpenAI
messages/tool_callspayloads to v2. - Rebuild LLM memory on the client.
- Skip
sendAutomaticallyWhenif you support approvals or elicitation.