@@ -0,0 +1,114 @@
1+import { getParseErrorMessage, handleError } from "@/src/lib/errors";
2+
3+type StreamEventCallbacks = {
4+ onContent?: (content: string) => void;
5+ onToolMessage?: (id: number, threadId: number, content: string, createdAt?: string) => void;
6+ onDone?: (data: {
7+ answer?: string;
8+ model?: string;
9+ maxPromptLength?: number | null;
10+ toolCallCounts?: string | null;
11+ }) => void;
12+ onError?: (error: string) => void;
13+ onStop?: () => void;
14+};
15+
16+/**
17+ * Parses a streaming message response from the chat API.
18+ * Handles Server-Sent Events (SSE) format with different message types.
19+ *
20+ * @param reader - ReadableStreamDefaultReader from the fetch response
21+ * @param callbacks - Callback functions for different event types
22+ * @param onError - Optional error handler callback
23+ */
24+export async function parseMessageStream(
25+ reader: ReadableStreamDefaultReader<Uint8Array>,
26+ callbacks: StreamEventCallbacks,
27+ onError?: (error: string) => void
28+): Promise<void> {
29+ const decoder = new TextDecoder();
30+ let buffer = "";
31+ let accumulatedContent = "";
32+
33+ while (true) {
34+ const { done, value } = await reader.read();
35+ if (done) break;
36+
37+ buffer += decoder.decode(value, { stream: true });
38+ const lines = buffer.split("\n");
39+ buffer = lines.pop() || "";
40+
41+ for (const line of lines) {
42+ if (line.trim() === "") continue;
43+ if (line.startsWith("data: ")) {
44+ try {
45+ const data = JSON.parse(line.slice(6));
46+ if (data.type === "content") {
47+ accumulatedContent += data.content || "";
48+ callbacks.onContent?.(accumulatedContent);
49+ } else if (data.type === "toolMessage") {
50+ callbacks.onToolMessage?.(data.id, data.threadId, data.content, data.createdAt);
51+ } else if (data.type === "done") {
52+ callbacks.onDone?.({
53+ answer: data.answer,
54+ model: data.model,
55+ maxPromptLength: data.maxPromptLength,
56+ toolCallCounts: data.toolCallCounts,
57+ });
58+ return;
59+ } else if (data.type === "error") {
60+ if (data.error === "Generation stopped") {
61+ callbacks.onStop?.();
62+ return;
63+ }
64+ const errorMsg = data.error || "Unknown error";
65+ callbacks.onError?.(errorMsg);
66+ if (onError) onError(errorMsg);
67+ throw new Error(errorMsg);
68+ }
69+ } catch (parseError) {
70+ handleError(parseError, onError);
71+ }
72+ }
73+ }
74+ }
75+}
76+
77+/**
78+ * Fetches and parses a streaming chat response.
79+ *
80+ * @param message - The message to send
81+ * @param threadId - The thread ID
82+ * @param callbacks - Callback functions for different event types
83+ * @param signal - Optional AbortSignal to cancel the request
84+ * @param onError - Optional error handler callback
85+ * @throws If the request fails or response is invalid
86+ */
87+export async function fetchAndParseMessageStream(
88+ message: string,
89+ threadId: number,
90+ callbacks: StreamEventCallbacks,
91+ signal?: AbortSignal,
92+ onError?: (error: string) => void
93+): Promise<void> {
94+ const res = await fetch("/api/chat", {
95+ method: "POST",
96+ headers: { "Content-Type": "application/json" },
97+ body: JSON.stringify({ message: message.trim(), threadId }),
98+ signal,
99+ });
100+
101+ if (!res.ok) {
102+ const errorData = await res.json().catch((parseError) => {
103+ throw new Error(getParseErrorMessage(parseError));
104+ });
105+ throw new Error(errorData.error || `HTTP error! status: ${res.status}`);
106+ }
107+
108+ if (!res.body) {
109+ throw new Error("Response body is null");
110+ }
111+
112+ const reader = res.body.getReader();
113+ await parseMessageStream(reader, callbacks, onError);
114+}