streaming
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineseassistant-ui Streaming
assistant-ui 流式传输
Always consult assistant-ui.com/llms.txt for latest API.
The package handles streaming from AI backends.
assistant-stream请始终查阅assistant-ui.com/llms.txt获取最新API。
assistant-streamReferences
参考文档
- ./references/data-stream.md -- AI SDK data stream format
- ./references/assistant-transport.md -- Native assistant-ui format
- ./references/encoders.md -- Encoders and decoders
- ./references/data-stream.md -- AI SDK 数据流格式
- ./references/assistant-transport.md -- 原生assistant-ui格式
- ./references/encoders.md -- 编码器与解码器
When to Use
使用场景
Using Vercel AI SDK?
├─ Yes → toUIMessageStreamResponse() (no assistant-stream needed)
└─ No → assistant-stream for custom backends是否使用Vercel AI SDK?
├─ 是 → 使用toUIMessageStreamResponse()(无需assistant-stream)
└─ 否 → 使用assistant-stream构建自定义后端Installation
安装
bash
npm install assistant-streambash
npm install assistant-streamCustom Streaming Response
自定义流式响应
ts
import { createAssistantStreamResponse } from "assistant-stream";
export async function POST(req: Request) {
return createAssistantStreamResponse(async (stream) => {
stream.appendText("Hello ");
stream.appendText("world!");
// Tool call example
const tool = stream.addToolCallPart({ toolCallId: "1", toolName: "get_weather" });
tool.argsText.append('{"city":"NYC"}');
tool.argsText.close();
tool.setResponse({ result: { temperature: 22 } });
stream.close();
});
}ts
import { createAssistantStreamResponse } from "assistant-stream";
export async function POST(req: Request) {
return createAssistantStreamResponse(async (stream) => {
stream.appendText("Hello ");
stream.appendText("world!");
// 工具调用示例
const tool = stream.addToolCallPart({ toolCallId: "1", toolName: "get_weather" });
tool.argsText.append('{"city":"NYC"}');
tool.argsText.close();
tool.setResponse({ result: { temperature: 22 } });
stream.close();
});
}With useLocalRuntime
结合useLocalRuntime使用
useLocalRuntimeChatModelRunResulttsx
import { useLocalRuntime } from "@assistant-ui/react";
const runtime = useLocalRuntime({
model: {
async *run({ messages, abortSignal }) {
const response = await fetch("/api/chat", {
method: "POST",
body: JSON.stringify({ messages }),
signal: abortSignal,
});
const reader = response.body?.getReader();
const decoder = new TextDecoder();
let buffer = "";
while (reader) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const parts = buffer.split("\n");
buffer = parts.pop() ?? "";
for (const chunk of parts.filter(Boolean)) {
yield { content: [{ type: "text", text: chunk }] };
}
}
},
},
});useLocalRuntimeChatModelRunResulttsx
import { useLocalRuntime } from "@assistant-ui/react";
const runtime = useLocalRuntime({
model: {
async *run({ messages, abortSignal }) {
const response = await fetch("/api/chat", {
method: "POST",
body: JSON.stringify({ messages }),
signal: abortSignal,
});
const reader = response.body?.getReader();
const decoder = new TextDecoder();
let buffer = "";
while (reader) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const parts = buffer.split("\n");
buffer = parts.pop() ?? "";
for (const chunk of parts.filter(Boolean)) {
yield { content: [{ type: "text", text: chunk }] };
}
}
},
},
});Debugging Streams
调试流式传输
ts
import { AssistantStream, DataStreamDecoder } from "assistant-stream";
const stream = AssistantStream.fromResponse(response, new DataStreamDecoder());
for await (const event of stream) {
console.log("Event:", JSON.stringify(event, null, 2));
}ts
import { AssistantStream, DataStreamDecoder } from "assistant-stream";
const stream = AssistantStream.fromResponse(response, new DataStreamDecoder());
for await (const event of stream) {
console.log("Event:", JSON.stringify(event, null, 2));
}Stream Event Types
流式事件类型
- with
part-start=part.type"text" | "reasoning" | "tool-call" | "source" | "file" - with streamed text
text-delta - with tool results
result - ,
step-start,step-finishmessage-finish - strings
error
- ,其中
part-start=part.type"text" | "reasoning" | "tool-call" | "source" | "file" - ,包含流式文本
text-delta - ,包含工具结果
result - ,
step-start,step-finishmessage-finish - ,错误字符串
error
Common Gotchas
常见问题
Stream not updating UI
- Check Content-Type is
text/event-stream - Check for CORS errors
Tool calls not rendering
- needs both
addToolCallPartandtoolCallIdtoolName - Register tool UI with
makeAssistantToolUI
Partial text not showing
- Use events for streaming
text-delta
流式传输未更新UI
- 检查Content-Type是否为
text/event-stream - 检查是否存在CORS错误
工具调用未渲染
- 需要同时传入
addToolCallPart和toolCallIdtoolName - 使用注册工具UI
makeAssistantToolUI
部分文本未显示
- 使用事件实现流式传输
text-delta