通过 AG-UI 接入 Web 聊天前端
bub-ag-ui 插件(实验性)通过 AG-UI 协议将 Bub 接入各类 Web 聊天前端,不仅支持流式推送模型回复,还允许 Bub 调用运行在浏览器前端的工具函数。
在本指南中,我们将把 Bub 连接到一个 Web 聊天界面,并结合浏览器端的天气查询工具(get_weather)来规划旧金山的下午行程。如果你还没有选定具体的前端框架,可以直接从第一个 assistant-ui 标签页开始体验。
1. 安装并启动插件
Section titled “1. 安装并启动插件”在 Bub 运行环境中安装 bub-ag-ui 并启动本地服务:
bub install bub-ag-ui@mainbub ag-ui --helpbub ag-ui --host 127.0.0.1 --port 28201保持该终端运行。前端应用会向 http://127.0.0.1:28201/agent 发送请求并接收流式回复。
2. 连接前端
Section titled “2. 连接前端”从下方选择一个你喜欢的前端框架标签页,按步骤创建并启动项目。所有示例都会连接同一个 Bub 后端、在浏览器端注册 get_weather 工具(返回演示用的旧金山天气数据),并通过 sessionStorage 保存页面消息以便刷新后恢复。
各个前端示例会用到以下几个共用辅助文件;创建好对应前端项目后,按需把它们复制到项目中即可:
查看共用示例文件(scenario.json / vite 代理配置 / demo-session.ts)
CopilotKit、OpenUI 和 AI Elements 将天气工具及数据保存为 src/scenario.json。assistant-ui 示例已在 provider 中包含这些内容。
{ "name": "Weekend weather planner", "tool": { "name": "get_weather", "description": "Return a fixed demonstration weather forecast for a city. This is sample data for UI compatibility testing, not live weather.", "parameters": { "type": "object", "properties": {"city": {"type": "string", "description": "City to look up"}}, "required": ["city"], "additionalProperties": false } }, "result": { "city": "San Francisco", "temperature_c": 18, "condition": "Partly cloudy", "source": "Fixed demo forecast; not live weather" }, "prompt": "Call get_weather for San Francisco. Using that demo forecast, suggest a relaxed afternoon with a bookstore, coffee, and a sunset walk. Reply in English with the heading 'A slower afternoon', a three-row Markdown table (Time / Plan), and a short weather note. Keep it under 100 words. Use only get_weather; do not delegate or call other tools."}CopilotKit 和 OpenUI 在 vite.config.ts 的 defineConfig({...}) 中添加以下 server 属性。前端运行在 3000 端口,代理将 /api/* 去掉 /api 前缀后转发给 Bub。另外两个标签页包含各自的代理配置。
server: { host: "localhost", port: 3000, strictPort: true, proxy: { "/api": { target: "http://127.0.0.1:28201", changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ""), }, },},将存储辅助文件保存为 app/demo-session.ts(assistant-ui)或 src/demo-session.ts(其他前端)。它通过浏览器的 sessionStorage 保存会话 ID、消息和状态。
import { HttpAgent, type Message } from "@ag-ui/client";
type DemoSession = { threadId: string; messages: Message[]; state: HttpAgent["state"];};
// Demo storage for one browser tab. A deployed app supplies its own storage.export function readDemoSession(key: string): DemoSession { const saved = sessionStorage.getItem(key); if (saved) return JSON.parse(saved) as DemoSession; const session = { threadId: crypto.randomUUID(), messages: [], state: {} }; sessionStorage.setItem(key, JSON.stringify(session)); return session;}
export function persistDemoSession(key: string, agent: HttpAgent) { const save = () => sessionStorage.setItem( key, JSON.stringify({ threadId: agent.threadId, messages: agent.messages, state: agent.state, }), ); return agent.subscribe({ onRunFinalized: save, });}assistant-ui 提供 AG-UI runtime,负责连接和浏览器工具调用。
通过官方 Starter创建项目,选择默认模板,再安装 AG-UI runtime。保留 Starter 安装的 @assistant-ui/react 版本。
npx assistant-ui@latest create bub-assistant-uicd bub-assistant-uinpm install @assistant-ui/react-ag-ui@0.1.2 @ag-ui/client@1.0.4连接 Bub
Section titled “连接 Bub”将共用辅助文件保存为 app/demo-session.ts。在 next.config.ts 的配置对象中添加以下 rewrite,保留 Starter 原有配置和 withAui 包装:
async rewrites() { return [{ source: "/api/:path*", destination: "http://127.0.0.1:28201/:path*" }];},创建 app/BubRuntimeProvider.tsx,接入 runtime、恢复界面消息并注册天气工具:
已折叠 14 行代码
"use client";
import { useEffect, useState, type ReactNode } from "react";import { HttpAgent } from "@ag-ui/client";import { useAgUiRuntime, fromAgUiMessages } from "@assistant-ui/react-ag-ui";import { AssistantRuntimeProvider, ExportedMessageRepository, type ThreadHistoryAdapter,} from "@assistant-ui/react";import { readDemoSession, persistDemoSession } from "./demo-session";
type Session = { agent: HttpAgent; history: ThreadHistoryAdapter };
function ConnectedRuntime({ session, children }: { session: Session; children: ReactNode }) { const runtime = useAgUiRuntime({ agent: session.agent, adapters: { history: session.history } }); useEffect( () =>已折叠 21 行代码
runtime.registerModelContextProvider({ getModelContext: () => ({ tools: { get_weather: { description: "Return a fixed demo forecast for San Francisco, not live weather.", parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"], }, execute: async () => ({ city: "San Francisco", temperature_c: 18, condition: "Partly cloudy", source: "Fixed demo forecast; not live weather", }), }, }, }), }), [runtime], ); return <AssistantRuntimeProvider runtime={runtime}>{children}</AssistantRuntimeProvider>;}
export function BubRuntimeProvider({ children }: { children: ReactNode }) { const [session, setSession] = useState<Session | null>(null); const [error, setError] = useState(""); useEffect(() => { try { const key = "bub-assistant-ui-demo"; const saved = readDemoSession(key); const agent = new HttpAgent({ url: "/api/agent", threadId: saved.threadId, initialMessages: saved.messages, initialState: saved.state, }); const subscription = persistDemoSession(key, agent); setSession({ agent,已折叠 8 行代码
history: { load: async () => ({ ...ExportedMessageRepository.fromArray(fromAgUiMessages(saved.messages)), state: saved.state, }), // The SDK subscription saves the complete messages/state snapshot. append: async () => {}, }, }); return () => subscription.unsubscribe(); } catch (cause) { setError(String(cause)); } }, []); if (error) return <p role="alert">{error}</p>; if (!session) return <p role="status">Loading conversation...</p>; return <ConnectedRuntime session={session}>{children}</ConnectedRuntime>;}用以下内容替换 app/assistant.tsx,保留现有的 app/page.tsx 入口:
"use client";
import { BubRuntimeProvider } from "./BubRuntimeProvider";import { Thread } from "@/components/assistant-ui/elements/thread.aui";
export const Assistant = () => ( <BubRuntimeProvider> <div style={{ height: "100dvh" }}><Thread /></div> </BubRuntimeProvider>);npm run dev -- --hostname localhost --port 3000此示例使用 CopilotChat 和 useFrontendTool,通过仅供开发使用的 agents__unsafe_dev_only 连接 Bub。部署时使用 CopilotKit runtime,接法可参考 LangChain 集成指南。
创建 React 项目,并安装匹配的 CopilotKit 和 AG-UI 版本:
npm create vite@latest bub-copilotkit -- --template react-tscd bub-copilotkitnpm installnpm install @copilotkit/react-core@1.85.0 @ag-ui/client@1.0.2 @ag-ui/core@1.0.2 rxjs@7.8.1 zod@3.25.76连接 Bub
Section titled “连接 Bub”将共用文件保存为 src/demo-session.ts 和 src/scenario.json,添加共用的 Vite 代理。创建 src/BubHttpAgent.ts,从浏览器存储恢复界面消息,无需向 Bub 请求历史:
已折叠 6 行代码
import { HttpAgent } from "@ag-ui/client";import { EventType, type BaseEvent, type RunAgentInput } from "@ag-ui/core";import { Observable } from "rxjs";import { readDemoSession } from "./demo-session";
export const storageKey = "bub-copilotkit-demo";
export class BubHttpAgent extends HttpAgent { protected connect(input: RunAgentInput): Observable<BaseEvent> { return new Observable((subscriber) => { const saved = readDemoSession(storageKey); // Local UI hydration only; no request or model run is started here. subscriber.next({ type: EventType.RUN_STARTED, threadId: input.threadId, runId: input.runId, } as BaseEvent); subscriber.next({ type: EventType.MESSAGES_SNAPSHOT, messages: saved.messages } as BaseEvent); subscriber.next({ type: EventType.STATE_SNAPSHOT, snapshot: saved.state } as BaseEvent); subscriber.next({ type: EventType.RUN_FINISHED, threadId: input.threadId, runId: input.runId, } as BaseEvent); subscriber.complete(); }); }}替换 src/main.tsx。useFrontendTool 注册天气工具,并将执行结果返回给 Bub:
已折叠 10 行代码
import { createRoot } from "react-dom/client";import { BubHttpAgent, storageKey } from "./BubHttpAgent";import { readDemoSession, persistDemoSession } from "./demo-session";import { CopilotChat, CopilotKit, useFrontendTool } from "@copilotkit/react-core/v2";import "@copilotkit/react-core/v2/styles.css";import { z } from "zod";import scenario from "./scenario.json";
const saved = readDemoSession(storageKey);const threadId = saved.threadId;function WeatherChat() { useFrontendTool({ name: scenario.tool.name, description: scenario.tool.description, parameters: z.object({ city: z.string() }), handler: async ({ city }) => { console.info("COMPAT_TOOL_EXECUTED", "get_weather", city); return scenario.result; }, }); return ( <main style={{ height: "100dvh", minWidth: 0 }}> <CopilotChat agentId="bub" threadId={threadId} labels={{ welcomeMessageText: "Plan a slower afternoon with Bub.", chatInputPlaceholder: "Ask about the demo forecast...", }} /> </main> );}async function start() { const bub = new BubHttpAgent({ agentId: "bub", threadId, url: "/api/agent", initialMessages: saved.messages, initialState: saved.state, }); persistDemoSession(storageKey, bub); createRoot(document.getElementById("root")!).render( <CopilotKit agents__unsafe_dev_only={{ bub }} showDevConsole={false}> <WeatherChat /> </CopilotKit>, );}已折叠 4 行代码
start().catch((error) => { console.error(error); document.getElementById("root")!.textContent = String(error);});npm run devOpenUI 提供 AgentInterface 和 AG-UI 解析器。下方应用代码将它们接入 Bub,并处理浏览器工具调用。
创建 React 项目并安装 OpenUI:
npm create vite@latest bub-openui -- --template react-tscd bub-openuinpm installnpm install @openuidev/react-ui@0.18.5 @openuidev/react-headless@0.18.5 @openuidev/react-lang@1.1.0 @ag-ui/client@1.0.4 zod@4 zustand@4连接 Bub
Section titled “连接 Bub”将共用文件保存为 src/demo-session.ts 和 src/scenario.json,添加共用的 Vite 代理,再替换 src/main.tsx:
已折叠 28 行代码
import { createRoot } from "react-dom/client";import { AgentInterface } from "@openuidev/react-ui";import { agUIAdapter, type ChatLLM, type ChatStorage, type Message as OpenMessage,} from "@openuidev/react-headless";import { HttpAgent, type Message } from "@ag-ui/client";import "@openuidev/react-ui/styles/index.css";import scenario from "./scenario.json";import { readDemoSession, persistDemoSession } from "./demo-session";
const storageKey = "bub-openui-demo";const threadId = readDemoSession(storageKey).threadId;const baseThread = { id: threadId, title: "Weekend weather planner", createdAt: Date.now() };const storage: ChatStorage = { thread: { listThreads: async () => ({ threads: [baseThread] }), createThread: async () => baseThread, getMessages: async () => readDemoSession(storageKey).messages as OpenMessage[], updateThread: async (thread) => thread, deleteThread: async () => { throw Error("Deletion is not enabled in this compatibility demo"); }, },};
// OpenUI supplies the UI and AG-UI parser. This application bridge owns browser// tool execution and continuation; neither package is patched.const llm: ChatLLM = { streamProtocol: agUIAdapter(), send: async ({ messages, signal }) => { const agent = new HttpAgent({ url: "/api/agent", threadId, initialMessages: messages as Message[], initialState: readDemoSession(storageKey).state, }); const subscription = persistDemoSession(storageKey, agent);已折叠 66 行代码
const encoder = new TextEncoder(); const stream = new ReadableStream<Uint8Array>({ start(controller) { const emit = (event: unknown) => controller.enqueue(encoder.encode(`data: ${JSON.stringify(event)}\n\n`)); const abort = () => agent.abortRun(); signal.addEventListener("abort", abort, { once: true }); (async () => { try { const executed = new Set<string>(); for (let round = 0; round < 4; round++) { await agent.runAgent( { tools: [scenario.tool] }, { onEvent: ({ event }) => { emit(event); }, }, ); if (signal.aborted) break; const answered = new Set( agent.messages.filter((m) => m.role === "tool").map((m) => m.toolCallId), ); const pending = agent.messages .flatMap((m) => (m.role === "assistant" ? (m.toolCalls ?? []) : [])) .filter((t) => !answered.has(t.id)); if (!pending.length) break; for (const call of pending) { if (call.function.name !== "get_weather" || executed.has(call.id)) throw Error(`Unsupported or repeated browser tool: ${call.function.name}`); const args = JSON.parse(call.function.arguments); if (typeof args.city !== "string" || args.city.toLowerCase() !== "san francisco") throw Error("Demo fixture supports San Francisco only"); executed.add(call.id); console.info("COMPAT_TOOL_EXECUTED", "get_weather", args.city); const message = { id: crypto.randomUUID(), role: "tool" as const, toolCallId: call.id, content: JSON.stringify(scenario.result), }; agent.addMessage(message); emit({ type: "TOOL_CALL_RESULT", messageId: message.id, toolCallId: call.id, content: message.content, role: "tool", }); } if (round === 3) throw Error("Browser tool continuation limit exceeded"); } controller.close(); } catch (error) { console.error(error); controller.error(error); } finally { signal.removeEventListener("abort", abort); subscription.unsubscribe(); } })(); }, cancel() { agent.abortRun(); }, }); return new Response(stream, { headers: { "Content-Type": "text/event-stream" } }); },};createRoot(document.getElementById("root")!).render( <div style={{ height: "100dvh" }}> <AgentInterface llm={llm} storage={storage} agentName="Bub + OpenUI" theme={{ mode: "light" }} starterVariant="short" starters={[{ displayText: "Plan a slower afternoon", prompt: scenario.prompt }]} /> </div>,);此示例在侧栏保留一个会话,并禁用删除功能。默认渲染器会将 Markdown 表格显示为纯文本。调整界面时可参考 OpenUI Agent 示例。
npm run devAI Elements 提供 UI 组件,应用通过 HttpAgent 将它们接入 Bub,并处理浏览器工具调用。
创建 React 项目,安装 AG-UI 客户端和 Tailwind CSS:
npm create vite@latest bub-ai-elements -- --template react-tscd bub-ai-elementsnpm installnpm install @ag-ui/client@1.0.4 ai@7.0.153npm install -D tailwindcss @tailwindcss/vite @types/node连接 Bub
Section titled “连接 Bub”替换 vite.config.ts,配置 React、Tailwind、@ 别名和 Bub 代理:
import { defineConfig } from "vite";import react from "@vitejs/plugin-react";import tailwindcss from "@tailwindcss/vite";import { fileURLToPath } from "node:url";
export default defineConfig({ plugins: [react(), tailwindcss()], resolve: { alias: { "@": fileURLToPath(new URL("./src", import.meta.url)) } }, server: { host: "localhost", port: 3000, strictPort: true, proxy: { "/api": { target: "http://127.0.0.1:28201", changeOrigin: true, rewrite: (path) => path.replace(/^\/api/, ""), }, }, },});在 tsconfig.json 和 tsconfig.app.json 的 compilerOptions 中添加以下属性:
"paths": { "@/*": ["./src/*"] },"resolveJsonModule": true用以下内容替换 src/index.css,然后选择 Radix 安装 UI 组件:
@import "tailwindcss";npx shadcn@latest init --base radixnpx ai-elements@latest add conversation message prompt-input tool suggestion将共用文件保存为 src/demo-session.ts 和 src/scenario.json,再替换 src/main.tsx。组件用法可参考 AI Elements 指南。
已折叠 32 行代码
import { useEffect, useState } from "react";import { createRoot } from "react-dom/client";import { HttpAgent, type Message as AgMessage } from "@ag-ui/client";import { Conversation, ConversationContent, ConversationScrollButton,} from "@/components/ai-elements/conversation";import { Message, MessageContent, MessageResponse } from "@/components/ai-elements/message";import { PromptInput, PromptInputBody, PromptInputTextarea, PromptInputFooter, PromptInputSubmit, PromptInputTools, type PromptInputMessage,} from "@/components/ai-elements/prompt-input";import { Tool, ToolHeader, ToolContent, ToolInput, ToolOutput,} from "@/components/ai-elements/tool";import { Suggestion, Suggestions } from "@/components/ai-elements/suggestion";import { TooltipProvider } from "@/components/ui/tooltip";import type { ChatStatus } from "ai";import scenario from "./scenario.json";import { readDemoSession, persistDemoSession } from "./demo-session";import "./index.css";
const storageKey = "bub-ai-elements-demo";function Chat({ agent }: { agent: HttpAgent }) { const [messages, setMessages] = useState<AgMessage[]>(agent.messages); const [status, setStatus] = useState<ChatStatus>("ready"); const [text, setText] = useState(""); const [error, setError] = useState(""); useEffect( () => agent.subscribe({ onMessagesChanged: ({ messages }) => setMessages([...messages]), onTextMessageContentEvent: () => { setStatus("streaming"); }, }).unsubscribe, [agent], );已折叠 132 行代码
async function send(value: string) { if (!value.trim() || status === "submitted" || status === "streaming") return; setText(""); setError(""); setStatus("submitted"); agent.addMessage({ id: crypto.randomUUID(), role: "user", content: value }); try { const executed = new Set<string>(); for (let round = 0; round < 4; round++) { await agent.runAgent({ tools: [scenario.tool] }); const answered = new Set( agent.messages.filter((m) => m.role === "tool").map((m) => m.toolCallId), ); const pending = agent.messages .flatMap((m) => (m.role === "assistant" ? (m.toolCalls ?? []) : [])) .filter((t) => !answered.has(t.id)); if (!pending.length) break; for (const call of pending) { if (call.function.name !== "get_weather" || executed.has(call.id)) throw Error(`Unsupported or repeated browser tool: ${call.function.name}`); const args = JSON.parse(call.function.arguments); if (typeof args.city !== "string" || args.city.toLowerCase() !== "san francisco") throw Error("Demo fixture supports San Francisco only"); executed.add(call.id); console.info("COMPAT_TOOL_EXECUTED", "get_weather", args.city); agent.addMessage({ id: crypto.randomUUID(), role: "tool", toolCallId: call.id, content: JSON.stringify(scenario.result), }); } if (round === 3) throw Error("Browser tool continuation limit exceeded"); } setStatus("ready"); } catch (e) { setError(String(e)); setStatus("error"); console.error(e); } } const busy = status === "submitted" || status === "streaming"; // Conversation / message / composer layout adapted from the official chatbot // example. Mock streaming and unsupported model/search/attachment controls are // removed. AG-UI is an explicit application bridge, not an AI Elements runtime. return ( <div className="relative flex h-dvh w-full flex-col divide-y overflow-hidden"> <Conversation> <ConversationContent> {messages .filter((m) => m.role === "user" || m.role === "assistant") .map((message) => ( <Message from={message.role as "user" | "assistant"} key={message.id}> <div> {message.role === "assistant" && message.toolCalls?.map((call) => { const result = messages.find( (m) => m.role === "tool" && m.toolCallId === call.id, ); let input: unknown = {}; try { input = JSON.parse(call.function.arguments || "{}"); } catch { input = call.function.arguments; } return ( <Tool key={call.id}> <ToolHeader type="dynamic-tool" toolName={call.function.name} state={ result ? "output-available" : busy ? "input-streaming" : "input-available" } /> <ToolContent> <ToolInput input={input} /> {result && <ToolOutput output={result.content} errorText={undefined} />} </ToolContent> </Tool> ); })} {typeof message.content === "string" && message.content && ( <MessageContent> <MessageResponse>{message.content}</MessageResponse> </MessageContent> )} </div> </Message> ))} {error && ( <p role="alert" className="text-destructive"> {error} </p> )} </ConversationContent> <ConversationScrollButton /> </Conversation> <div className="grid shrink-0 gap-4 pt-4"> {!messages.length && ( <Suggestions className="px-4"> <Suggestion suggestion="Plan a slower afternoon" onClick={() => void send(scenario.prompt)} /> </Suggestions> )} <div className="w-full px-4 pb-4"> <PromptInput onSubmit={(message: PromptInputMessage) => send(message.text)}> <PromptInputBody> <PromptInputTextarea onChange={(e) => setText(e.target.value)} value={text} /> </PromptInputBody> <PromptInputFooter> <PromptInputTools> <span className="text-xs text-muted-foreground"> Bub · AG-UI bridge · Demo forecast </span> </PromptInputTools> <PromptInputSubmit status={status} disabled={!busy && !text.trim()} onStop={() => agent.abortRun()} /> </PromptInputFooter> </PromptInput> </div> </div> </div> );}async function start() { const saved = readDemoSession(storageKey); const agent = new HttpAgent({ url: "/api/agent", threadId: saved.threadId, initialMessages: saved.messages, initialState: saved.state, }); persistDemoSession(storageKey, agent); createRoot(document.getElementById("root")!).render( <TooltipProvider> <Chat agent={agent} /> </TooltipProvider>, );}start().catch((error) => { console.error(error); document.getElementById("root")!.textContent = String(error);});npm run dev3. 验证对话与工具调用
Section titled “3. 验证对话与工具调用”前端启动后,在浏览器打开 http://localhost:3000 并发送:
Call get_weather for San Francisco, then suggest an afternoon plan using the demo forecast.Bub 会调用浏览器端注册的 get_weather 工具(返回演示天气:18°C、局部多云),拿到结果后再为你生成一份旧金山下午的行程建议。
等回复生成完毕后,尝试刷新当前浏览器标签页,刚才的聊天记录依然会保留在界面上(OpenUI 可从左侧边栏选择已保存的会话)。你可以继续追问「我们刚才聊的是哪个城市?多少度?」,验证多轮上下文是否正常衔接。
会话与界面历史存储机制
Section titled “会话与界面历史存储机制”在上面的示例中,前端与后端各自分工保存了一部分状态:
- 前端界面消息(
sessionStorage):demo-session.ts将threadId、展示消息和前端状态保存在当前标签页的sessionStorage中,刷新同一标签页即可恢复界面。由于bub-ag-ui仅提供POST /agent接口、不提供历史消息拉取接口,新开的空白标签页无法从后端拉取旧界面记录。 - 后端模型上下文(Bub Tape):Bub 通过 Tape 保存模型上下文。只要请求携带相同的
threadId,就能继续之前的对话;配置本地文件或数据库存储可在后端重启后保留上下文。注意浏览器sessionStorage仅用于恢复前端界面,无法恢复后端已删除的 Tape 或中断的工具调用;因此在回复和工具调用完成前请保持后端运行(单轮默认超时 600 秒,含等待浏览器工具执行的时间)。
如需开启新会话,清除浏览器 sessionStorage 中的对应键并刷新页面即可。当前不支持「编辑历史消息」或「重新生成回复」;若 UI 模板自带这些按钮,建议将其隐藏或禁用。生产部署时请将 sessionStorage 替换为你自己的应用存储与多用户隔离方案。
在自定义前端中使用 HttpAgent
Section titled “在自定义前端中使用 HttpAgent”如果你打算从零编写自己的前端界面,可以直接使用官方的 @ag-ui/client SDK 管理连接与事件流。以下是不依赖任何 UI 框架的纯 TypeScript 最小示例(每次刷新都会开启新会话):
查看最小浏览器聊天示例(Vanilla TypeScript)
创建 Vite 项目:
npm create vite@latest bub-chat -- --template vanilla-tscd bub-chatnpm installnpm install @ag-ui/client@1.0.4替换 src/main.ts:
import { HttpAgent } from "@ag-ui/client";
const root = document.querySelector<HTMLDivElement>("#app")!;root.innerHTML = ` <h1>Bub chat</h1> <pre id="messages" aria-live="polite"></pre> <form> <label>Message <input name="message" required /></label> <button>Send</button> </form> <p id="status" role="status"></p>`;
const form = root.querySelector("form")!;const input = root.querySelector("input")!;const button = root.querySelector("button")!;const messages = root.querySelector<HTMLPreElement>("#messages")!;const status = root.querySelector<HTMLParagraphElement>("#status")!;const agent = new HttpAgent({ url: "http://127.0.0.1:28201/agent", threadId: crypto.randomUUID(),});
agent.subscribe({ onMessagesChanged: ({ messages: current }) => { messages.textContent = current .map((message) => `${message.role}: ${message.content ?? ""}`) .join("\n\n"); },});
form.addEventListener("submit", async (event) => { event.preventDefault(); const content = input.value.trim(); if (!content || button.disabled) return;
agent.addMessage({ id: crypto.randomUUID(), role: "user", content }); input.value = ""; button.disabled = true; status.textContent = "Running..."; try { await agent.runAgent({}, { onRunErrorEvent: ({ event }) => { status.textContent = event.message; }, }); if (status.textContent === "Running...") status.textContent = "Ready"; } catch (error) { status.textContent = String(error); } finally { button.disabled = false; input.focus(); }});启动 Vite,打开 http://localhost:3000:
npm run dev -- --host localhost --port 3000 --strictPort这个最小示例没有走本地代理,而是从浏览器直接跨域请求 Bub 后端。bub-ag-ui 默认仅放行 http://localhost:3000;如果你的前端运行在其他端口(如 Vite 默认的 5173),启动后端前请设置 BUB_AG_UI_ORIGINS:
export BUB_AG_UI_ORIGINS='["http://localhost:5173"]'注意 Origin 中的协议、域名和端口号必须完全一致(在浏览器跨域规则中,localhost 与 127.0.0.1 会被视为两个不同的来源)。
监听非本地回环地址(非 127.0.0.1)时,请设置 BUB_AG_UI_TOKEN 开启鉴权,并在发往 Bub 的请求头中携带 Authorization: Bearer <token>。建议由你自己的应用后端保存 Token 并代理转发请求;若由浏览器直连,还需通过 BUB_AG_UI_ORIGINS 显式放行前端域名。
单个共享 Token 对应单一用户身份;多用户鉴权与网关挂载方式见 bub-ag-ui 插件文档。
| 现象 | 排查与解决方法 |
|---|---|
提示找不到 ag-ui 子命令 |
确认当前终端的 Python 环境与安装插件的环境一致,再运行 bub ag-ui --help |
| 浏览器请求报 CORS 或网络错误 | 使用代理时检查前端代理配置;浏览器直连时检查 BUB_AG_UI_ORIGINS 是否与地址栏的协议、域名、端口完全一致 |
请求返回 401 或 403 |
检查请求头 Authorization: Bearer <token> 是否与后端 BUB_AG_UI_TOKEN 一致 |
| 发送消息后无回复 | 先在终端运行 bub run "Say hello" 验证模型配置,并查看 bub ag-ui 终端日志 |
| 浏览器工具执行后无后续回复 | 检查前端工具函数是否正常返回结果,并确认前端框架在拿到结果后继续推进了对话 |
前端收到 RUN_ERROR 事件 |
查看事件的 message 字段及后端终端日志 |