mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-20 21:04:45 +08:00
chore: merge with web UI project
This commit is contained in:
71
web/src/core/api/chat.ts
Normal file
71
web/src/core/api/chat.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { env } from "~/env";
|
||||
|
||||
import { fetchStream } from "../sse";
|
||||
import { sleep } from "../utils";
|
||||
|
||||
import type { ChatEvent } from "./types";
|
||||
|
||||
export function chatStream(
|
||||
userMessage: string,
|
||||
params: {
|
||||
thread_id: string;
|
||||
max_plan_iterations: number;
|
||||
max_step_num: number;
|
||||
interrupt_feedback?: string;
|
||||
},
|
||||
options: { abortSignal?: AbortSignal } = {},
|
||||
) {
|
||||
if (location.search.includes("mock")) {
|
||||
return chatStreamMock(userMessage, params, options);
|
||||
}
|
||||
return fetchStream<ChatEvent>(
|
||||
(env.NEXT_PUBLIC_API_URL ?? "http://localhost:8000/api") + "/chat/stream",
|
||||
{
|
||||
body: JSON.stringify({
|
||||
messages: [{ role: "user", content: userMessage }],
|
||||
auto_accepted_plan: false,
|
||||
...params,
|
||||
}),
|
||||
signal: options.abortSignal,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
async function* chatStreamMock(
|
||||
userMessage: string,
|
||||
_: {
|
||||
thread_id: string;
|
||||
max_plan_iterations: number;
|
||||
max_step_num: number;
|
||||
} = {
|
||||
thread_id: "__mock__",
|
||||
max_plan_iterations: 3,
|
||||
max_step_num: 1,
|
||||
},
|
||||
options: { abortSignal?: AbortSignal } = {},
|
||||
): AsyncIterable<ChatEvent> {
|
||||
const res = await fetch("/mock.txt", {
|
||||
signal: options.abortSignal,
|
||||
});
|
||||
await sleep(800);
|
||||
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];
|
||||
if (event === "message_chunk") {
|
||||
await sleep(0);
|
||||
} else if (event === "tool_call_result") {
|
||||
await sleep(1500);
|
||||
}
|
||||
try {
|
||||
yield {
|
||||
type: event,
|
||||
data: JSON.parse(data),
|
||||
} as ChatEvent;
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
2
web/src/core/api/index.ts
Normal file
2
web/src/core/api/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./chat";
|
||||
export * from "./types";
|
||||
81
web/src/core/api/types.ts
Normal file
81
web/src/core/api/types.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import type { Option } from "../messages";
|
||||
import type { StreamEvent } from "../sse";
|
||||
|
||||
// Tool Calls
|
||||
|
||||
export interface ToolCall {
|
||||
type: "tool_call";
|
||||
id: string;
|
||||
name: string;
|
||||
args: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface ToolCallChunk {
|
||||
type: "tool_call_chunk";
|
||||
index: number;
|
||||
id: string;
|
||||
name: string;
|
||||
args: string;
|
||||
}
|
||||
|
||||
// Events
|
||||
|
||||
interface GenericEvent<T extends string, D extends object> extends StreamEvent {
|
||||
type: T;
|
||||
data: {
|
||||
id: string;
|
||||
thread_id: string;
|
||||
agent: "coordinator" | "planner" | "researcher" | "coder" | "reporter";
|
||||
role: "user" | "assistant" | "tool";
|
||||
finish_reason?: "stop" | "tool_calls" | "interrupt";
|
||||
} & D;
|
||||
}
|
||||
|
||||
export interface MessageChunkEvent
|
||||
extends GenericEvent<
|
||||
"message_chunk",
|
||||
{
|
||||
content?: string;
|
||||
}
|
||||
> {}
|
||||
|
||||
export interface ToolCallsEvent
|
||||
extends GenericEvent<
|
||||
"tool_calls",
|
||||
{
|
||||
tool_calls: ToolCall[];
|
||||
tool_call_chunks: ToolCallChunk[];
|
||||
}
|
||||
> {}
|
||||
|
||||
export interface ToolCallChunksEvent
|
||||
extends GenericEvent<
|
||||
"tool_call_chunks",
|
||||
{
|
||||
tool_call_chunks: ToolCallChunk[];
|
||||
}
|
||||
> {}
|
||||
|
||||
export interface ToolCallResultEvent
|
||||
extends GenericEvent<
|
||||
"tool_call_result",
|
||||
{
|
||||
tool_call_id: string;
|
||||
content?: string;
|
||||
}
|
||||
> {}
|
||||
|
||||
export interface InterruptEvent
|
||||
extends GenericEvent<
|
||||
"interrupt",
|
||||
{
|
||||
options: Option[];
|
||||
}
|
||||
> {}
|
||||
|
||||
export type ChatEvent =
|
||||
| MessageChunkEvent
|
||||
| ToolCallsEvent
|
||||
| ToolCallChunksEvent
|
||||
| ToolCallResultEvent
|
||||
| InterruptEvent;
|
||||
Reference in New Issue
Block a user