增加流式信息中断处理机制
This commit is contained in:
+177
-90
@@ -6,6 +6,13 @@ export type AgentModel =
|
||||
| "deepseek/deepseek-v4-pro";
|
||||
|
||||
export type StreamEvent =
|
||||
| {
|
||||
type: "state";
|
||||
sessionId: string;
|
||||
messages: unknown[];
|
||||
isStreaming: boolean;
|
||||
runStatus?: string;
|
||||
}
|
||||
| { type: "token"; sessionId: string; content: string }
|
||||
| { type: "done"; sessionId: string; totalDurationMs?: number }
|
||||
| { type: "session_title"; sessionId: string; title: string }
|
||||
@@ -44,6 +51,12 @@ type StreamOptions = {
|
||||
onEvent: (event: StreamEvent) => void;
|
||||
};
|
||||
|
||||
type ResumeStreamOptions = {
|
||||
sessionId: string;
|
||||
signal?: AbortSignal;
|
||||
onEvent: (event: StreamEvent) => void;
|
||||
};
|
||||
|
||||
const parseEventBlock = (block: string): { event?: string; data?: string } => {
|
||||
const lines = block.split("\n");
|
||||
let event: string | undefined;
|
||||
@@ -87,6 +100,126 @@ const resolveToolParams = (
|
||||
return isObjectRecord(params) ? params : {};
|
||||
};
|
||||
|
||||
const emitParsedStreamEvent = (
|
||||
event: string,
|
||||
data: string,
|
||||
onEvent: (event: StreamEvent) => void,
|
||||
) => {
|
||||
try {
|
||||
const parsed = JSON.parse(data) as {
|
||||
session_id?: string;
|
||||
content?: string;
|
||||
message?: string;
|
||||
detail?: string;
|
||||
tool?: string;
|
||||
params?: Record<string, unknown>;
|
||||
arguments?: unknown;
|
||||
id?: string;
|
||||
phase?: string;
|
||||
status?: "running" | "completed" | "error";
|
||||
title?: string;
|
||||
messages?: unknown[];
|
||||
is_streaming?: boolean;
|
||||
run_status?: string;
|
||||
started_at?: number;
|
||||
ended_at?: number;
|
||||
elapsed_ms?: number;
|
||||
duration_ms?: number;
|
||||
total_duration_ms?: number;
|
||||
};
|
||||
if (event === "state") {
|
||||
onEvent({
|
||||
type: "state",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
messages: Array.isArray(parsed.messages) ? parsed.messages : [],
|
||||
isStreaming: parsed.is_streaming ?? false,
|
||||
runStatus: parsed.run_status,
|
||||
});
|
||||
} else if (event === "token") {
|
||||
onEvent({
|
||||
type: "token",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
content: parsed.content ?? "",
|
||||
});
|
||||
} else if (event === "progress") {
|
||||
onEvent({
|
||||
type: "progress",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
id: parsed.id ?? `${parsed.phase ?? "progress"}-${Date.now()}`,
|
||||
phase: parsed.phase ?? "progress",
|
||||
status: parsed.status ?? "running",
|
||||
title: parsed.title ?? "正在处理",
|
||||
detail: parsed.detail,
|
||||
startedAt: parsed.started_at,
|
||||
endedAt: parsed.ended_at,
|
||||
elapsedMs: parsed.elapsed_ms,
|
||||
durationMs: parsed.duration_ms,
|
||||
});
|
||||
} else if (event === "done") {
|
||||
onEvent({
|
||||
type: "done",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
totalDurationMs: parsed.total_duration_ms,
|
||||
});
|
||||
} else if (event === "session_title") {
|
||||
onEvent({
|
||||
type: "session_title",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
title: typeof parsed.title === "string" ? parsed.title : "",
|
||||
});
|
||||
} else if (event === "error") {
|
||||
onEvent({
|
||||
type: "error",
|
||||
sessionId: parsed.session_id,
|
||||
message: parsed.message ?? "unknown error",
|
||||
detail: parsed.detail,
|
||||
totalDurationMs: parsed.total_duration_ms,
|
||||
});
|
||||
} else if (event === "tool_call") {
|
||||
onEvent({
|
||||
type: "tool_call",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
tool: parsed.tool ?? "",
|
||||
params: resolveToolParams(parsed.params, parsed.arguments),
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
onEvent({
|
||||
type: "error",
|
||||
message: "invalid SSE data payload",
|
||||
detail: data,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const readStreamEvents = async (
|
||||
response: Response,
|
||||
onEvent: (event: StreamEvent) => void,
|
||||
) => {
|
||||
if (!response.body) {
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
let buffer = "";
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const blocks = buffer.split("\n\n");
|
||||
buffer = blocks.pop() ?? "";
|
||||
|
||||
for (const block of blocks) {
|
||||
const { event, data } = parseEventBlock(block);
|
||||
if (!event || !data) continue;
|
||||
emitParsedStreamEvent(event, data, onEvent);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export const streamAgentChat = async ({
|
||||
message,
|
||||
sessionId,
|
||||
@@ -144,98 +277,52 @@ export const streamAgentChat = async ({
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = response.body.getReader();
|
||||
const decoder = new TextDecoder("utf-8");
|
||||
let buffer = "";
|
||||
await readStreamEvents(response, onEvent);
|
||||
};
|
||||
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
|
||||
buffer += decoder.decode(value, { stream: true });
|
||||
const blocks = buffer.split("\n\n");
|
||||
buffer = blocks.pop() ?? "";
|
||||
|
||||
for (const block of blocks) {
|
||||
const { event, data } = parseEventBlock(block);
|
||||
if (!event || !data) continue;
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(data) as {
|
||||
session_id?: string;
|
||||
content?: string;
|
||||
message?: string;
|
||||
detail?: string;
|
||||
tool?: string;
|
||||
params?: Record<string, unknown>;
|
||||
arguments?: unknown;
|
||||
id?: string;
|
||||
phase?: string;
|
||||
status?: "running" | "completed" | "error";
|
||||
title?: string;
|
||||
started_at?: number;
|
||||
ended_at?: number;
|
||||
elapsed_ms?: number;
|
||||
duration_ms?: number;
|
||||
total_duration_ms?: number;
|
||||
};
|
||||
if (event === "token") {
|
||||
onEvent({
|
||||
type: "token",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
content: parsed.content ?? "",
|
||||
});
|
||||
} else if (event === "progress") {
|
||||
onEvent({
|
||||
type: "progress",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
id: parsed.id ?? `${parsed.phase ?? "progress"}-${Date.now()}`,
|
||||
phase: parsed.phase ?? "progress",
|
||||
status: parsed.status ?? "running",
|
||||
title: parsed.title ?? "正在处理",
|
||||
detail: parsed.detail,
|
||||
startedAt: parsed.started_at,
|
||||
endedAt: parsed.ended_at,
|
||||
elapsedMs: parsed.elapsed_ms,
|
||||
durationMs: parsed.duration_ms,
|
||||
});
|
||||
} else if (event === "done") {
|
||||
onEvent({
|
||||
type: "done",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
totalDurationMs: parsed.total_duration_ms,
|
||||
});
|
||||
} else if (event === "session_title") {
|
||||
onEvent({
|
||||
type: "session_title",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
title: typeof parsed.title === "string" ? parsed.title : "",
|
||||
});
|
||||
} else if (event === "error") {
|
||||
onEvent({
|
||||
type: "error",
|
||||
sessionId: parsed.session_id,
|
||||
message: parsed.message ?? "unknown error",
|
||||
detail: parsed.detail,
|
||||
totalDurationMs: parsed.total_duration_ms,
|
||||
});
|
||||
} else if (event === "tool_call") {
|
||||
onEvent({
|
||||
type: "tool_call",
|
||||
sessionId: parsed.session_id ?? "",
|
||||
tool: parsed.tool ?? "",
|
||||
params: resolveToolParams(parsed.params, parsed.arguments),
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
onEvent({
|
||||
type: "error",
|
||||
message: "invalid SSE data payload",
|
||||
detail: data,
|
||||
});
|
||||
}
|
||||
}
|
||||
export const resumeAgentChatStream = async ({
|
||||
sessionId,
|
||||
signal,
|
||||
onEvent,
|
||||
}: ResumeStreamOptions) => {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await apiFetch(
|
||||
`${config.AGENT_URL}/api/v1/agent/chat/session/${encodeURIComponent(sessionId)}/stream`,
|
||||
{
|
||||
method: "GET",
|
||||
signal,
|
||||
headers: {
|
||||
Accept: "text/event-stream",
|
||||
},
|
||||
projectHeaderMode: "include",
|
||||
userHeaderMode: "include",
|
||||
skipAuthRedirect: true,
|
||||
},
|
||||
);
|
||||
} catch (error) {
|
||||
const detail = error instanceof Error ? error.message : String(error);
|
||||
onEvent({
|
||||
type: "error",
|
||||
sessionId,
|
||||
message: "network request failed",
|
||||
detail,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!response.ok || !response.body) {
|
||||
const detail = await response.text();
|
||||
onEvent({
|
||||
type: "error",
|
||||
sessionId,
|
||||
message: "stream request failed",
|
||||
detail,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await readStreamEvents(response, onEvent);
|
||||
};
|
||||
|
||||
export const abortAgentChat = async (sessionId?: string) => {
|
||||
|
||||
Reference in New Issue
Block a user