Files
deer-flow/web/src/core/api/chat.ts

124 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-04-17 14:26:41 +08:00
// Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
// SPDX-License-Identifier: MIT
2025-04-24 15:41:33 +08:00
import type { MCPServerMetadata } from "../mcp";
import { extractReplayIdFromSearchParams } from "../replay/get-replay-id";
2025-04-17 12:02:23 +08:00
import { fetchStream } from "../sse";
import { sleep } from "../utils";
2025-04-24 15:41:33 +08:00
import { resolveServiceURL } from "./resolve-service-url";
2025-04-17 12:02:23 +08:00
import type { ChatEvent } from "./types";
export function chatStream(
userMessage: string,
params: {
thread_id: string;
auto_accepted_plan: boolean;
2025-04-17 12:02:23 +08:00
max_plan_iterations: number;
max_step_num: number;
interrupt_feedback?: string;
2025-04-24 15:41:33 +08:00
mcp_settings?: {
servers: Record<
string,
MCPServerMetadata & {
enabled_tools: string[];
add_to_agents: string[];
}
>;
};
2025-04-17 12:02:23 +08:00
},
options: { abortSignal?: AbortSignal } = {},
) {
2025-04-24 21:51:08 +08:00
if (location.search.includes("mock") || location.search.includes("replay=")) {
return chatReplayStream(userMessage, params, options);
2025-04-17 12:02:23 +08:00
}
2025-04-24 15:41:33 +08:00
return fetchStream<ChatEvent>(resolveServiceURL("chat/stream"), {
body: JSON.stringify({
messages: [{ role: "user", content: userMessage }],
...params,
}),
signal: options.abortSignal,
});
2025-04-17 12:02:23 +08:00
}
2025-04-24 21:51:08 +08:00
async function* chatReplayStream(
2025-04-17 12:02:23 +08:00
userMessage: string,
2025-04-19 11:03:33 +08:00
params: {
2025-04-17 12:02:23 +08:00
thread_id: string;
auto_accepted_plan: boolean;
2025-04-17 12:02:23 +08:00
max_plan_iterations: number;
max_step_num: number;
2025-04-19 11:03:33 +08:00
interrupt_feedback?: string;
2025-04-17 12:02:23 +08:00
} = {
thread_id: "__mock__",
auto_accepted_plan: false,
2025-04-17 12:02:23 +08:00
max_plan_iterations: 3,
max_step_num: 1,
2025-04-19 11:03:33 +08:00
interrupt_feedback: undefined,
2025-04-17 12:02:23 +08:00
},
options: { abortSignal?: AbortSignal } = {},
): AsyncIterable<ChatEvent> {
2025-04-24 21:51:08 +08:00
const urlParams = new URLSearchParams(window.location.search);
let replayFilePath = "";
if (urlParams.has("mock")) {
replayFilePath =
params.interrupt_feedback === "accepted"
? "/mock/before-interrupt.txt"
: "/mock/after-interrupt.txt";
} else {
const replayId = extractReplayIdFromSearchParams(window.location.search);
2025-04-24 21:51:08 +08:00
if (replayId) {
replayFilePath = `/replay/${replayId}.txt`;
} else {
replayFilePath = "/mock/before-interrupt.txt";
}
}
const res = await fetch(replayFilePath, {
2025-04-17 12:02:23 +08:00
signal: options.abortSignal,
});
const text = await res.text();
const chunks = text.split("\n\n");
for (const chunk of chunks) {
const [eventRaw, dataRaw] = chunk.split("\n") as [string, string];
const [, event] = eventRaw.split("event: ", 2) as [string, string];
const [, data] = dataRaw.split("data: ", 2) as [string, string];
2025-04-24 21:51:08 +08:00
2025-04-17 12:02:23 +08:00
try {
2025-04-24 21:51:08 +08:00
const chatEvent = {
2025-04-17 12:02:23 +08:00
type: event,
data: JSON.parse(data),
} as ChatEvent;
2025-04-24 21:51:08 +08:00
if (chatEvent.type === "message_chunk") {
if (!chatEvent.data.finish_reason) {
2025-04-25 13:36:21 +08:00
await sleepInReplay(100 + Math.random() * 250);
2025-04-24 21:51:08 +08:00
}
} else if (chatEvent.type === "tool_call_result") {
await sleepInReplay(1500);
2025-04-24 21:51:08 +08:00
}
yield chatEvent;
if (chatEvent.type === "tool_call_result") {
await sleepInReplay(800);
2025-04-24 21:51:08 +08:00
} else if (chatEvent.type === "message_chunk") {
if (chatEvent.data.role === "user") {
await sleepInReplay(1000);
2025-04-24 21:51:08 +08:00
}
}
2025-04-17 12:02:23 +08:00
} catch (e) {
console.error(e);
}
}
}
export async function sleepInReplay(ms: number) {
if (fastForwardReplaying) {
await sleep(0);
} else {
await sleep(ms);
}
}
let fastForwardReplaying = false;
export function fastForwardReplay(value: boolean) {
fastForwardReplaying = value;
}