Open History to see the last commit
1import { useState, useCallback, useRef, useId } from "react";
2import type { Message, StreamChunk } from "@tanstack/ai";
3import type {
4 UseChatOptions,
5 UseChatReturn,
6 ChatMessage,
7 ChatRequestBody,
8} from "./types";
9
10export function useChat(options: UseChatOptions = {}): UseChatReturn {
11 const {
12 api = "/api/chat",
13 initialMessages = [],
14 id: chatId,
15 onResponse,
16 onChunk,
17 onFinish,
18 onError,
19 headers,
20 body: extraBody,
21 credentials = "same-origin",
22 fetch: customFetch,
23 } = options;
24
25 const hookId = useId();
26 const uniqueId = chatId || hookId;
27
28 const [messages, setMessages] = useState<ChatMessage[]>(initialMessages);
29 const [isLoading, setIsLoading] = useState(false);
30 const [error, setError] = useState<Error | undefined>(undefined);
31
32 const abortControllerRef = useRef<AbortController | null>(null);
33 const fetchImpl = customFetch || fetch;
34
35 const generateId = useCallback(() => {
36 return `${uniqueId}-${Date.now()}-${Math.random()
37 .toString(36)
38 .substring(7)}`;
39 }, [uniqueId]);
40
41 const processStream = useCallback(
42 async (response: Response): Promise<ChatMessage> => {
43 const reader = response.body?.getReader();
44 if (!reader) {
45 throw new Error("Response body is not readable");
46 }
47
48 const decoder = new TextDecoder();
49 let accumulatedContent = "";
50 const assistantMessageId = generateId();
51 const assistantMessage: ChatMessage = {
52 id: assistantMessageId,
53 role: "assistant",
54 content: "",
55 createdAt: new Date(),
56 };
57
58 // Add the assistant message placeholder
59 setMessages((prev) => [...prev, assistantMessage]);
60
61 try {
62 while (true) {
63 const { done, value } = await reader.read();
64 if (done) break;
65
66 const chunk = decoder.decode(value, { stream: true });
67 const lines = chunk.split("\n").filter((line) => line.trim() !== "");
68
69 for (const line of lines) {
70 // Handle Server-Sent Events format
71 const data = line.startsWith("data: ") ? line.slice(6) : line;
72
73 if (data === "[DONE]") continue;
74
75 try {
76 const parsed: StreamChunk = JSON.parse(data);
77
78 // Call the onChunk callback
79 onChunk?.(parsed);
80
81 if (parsed.type === "content") {
82 accumulatedContent = parsed.content;
83
84 // Update the assistant message in place
85 setMessages((prev) =>
86 prev.map((msg) =>
87 msg.id === assistantMessageId
88 ? { ...msg, content: accumulatedContent }
89 : msg
90 )
91 );
92 } else if (parsed.type === "tool_call") {
93 // Handle tool calls if needed
94 // For now, just accumulate in message
95 setMessages((prev) =>
96 prev.map((msg) => {
97 if (msg.id === assistantMessageId) {
98 const existingToolCalls = msg.toolCalls || [];
99 const toolCallIndex = parsed.index;
100
101 // Update or add tool call at index
102 const updatedToolCalls = [...existingToolCalls];
103 if (!updatedToolCalls[toolCallIndex]) {
104 updatedToolCalls[toolCallIndex] = {
105 id: parsed.toolCall.id,
106 type: "function",
107 function: {
108 name: parsed.toolCall.function.name,
109 arguments: parsed.toolCall.function.arguments,
110 },
111 };
112 } else {
113 updatedToolCalls[toolCallIndex].function.arguments +=
114 parsed.toolCall.function.arguments;
115 }
116
117 return { ...msg, toolCalls: updatedToolCalls };
118 }
119 return msg;
120 })
121 );
122 } else if (parsed.type === "error") {
123 throw new Error(parsed.error.message);
124 }
125 } catch (parseError) {
126 // Skip non-JSON lines or malformed chunks
127 console.warn("Failed to parse chunk:", data, parseError);
128 }
129 }
130 }
131 } finally {
132 reader.releaseLock();
133 }
134
135 // Final message
136 const finalMessage: ChatMessage = {
137 id: assistantMessageId,
138 role: "assistant",
139 content: accumulatedContent,
140 createdAt: new Date(),
141 };
142
143 // Update with final message
144 setMessages((prev) =>
145 prev.map((msg) => (msg.id === assistantMessageId ? finalMessage : msg))
146 );
147
148 return finalMessage;
149 },
150 [generateId, onChunk]
151 );
152
153 const append = useCallback(
154 async (message: Message | ChatMessage) => {
155 const chatMessage: ChatMessage = {
156 ...(message as ChatMessage),
157 id: (message as ChatMessage).id || generateId(),
158 createdAt: (message as ChatMessage).createdAt || new Date(),
159 };
160
161 // Add user message immediately
162 setMessages((prev) => [...prev, chatMessage]);
163 setIsLoading(true);
164 setError(undefined);
165
166 // Create abort controller for this request
167 const abortController = new AbortController();
168 abortControllerRef.current = abortController;
169
170 try {
171 // Prepare request body
172 const requestBody: ChatRequestBody = {
173 messages: [...messages, chatMessage].map((msg) => ({
174 role: msg.role,
175 content: msg.content,
176 name: msg.name,
177 toolCalls: msg.toolCalls,
178 toolCallId: msg.toolCallId,
179 })),
180 data: extraBody,
181 };
182
183 // Make the request
184 const requestHeaders: Record<string, string> = {
185 "Content-Type": "application/json",
186 };
187
188 // Add custom headers
189 if (headers) {
190 if (headers instanceof Headers) {
191 headers.forEach((value, key) => {
192 requestHeaders[key] = value;
193 });
194 } else {
195 Object.assign(requestHeaders, headers);
196 }
197 }
198
199 const response = await fetchImpl(api, {
200 method: "POST",
201 headers: requestHeaders,
202 body: JSON.stringify(requestBody),
203 credentials,
204 signal: abortController.signal,
205 });
206
207 if (!response.ok) {
208 throw new Error(
209 `HTTP error! status: ${response.status} ${response.statusText}`
210 );
211 }
212
213 // Call onResponse callback
214 await onResponse?.(response);
215
216 // Process the streaming response
217 const assistantMessage = await processStream(response);
218
219 // Call onFinish callback
220 onFinish?.(assistantMessage);
221 } catch (err) {
222 if (err instanceof Error) {
223 if (err.name === "AbortError") {
224 // Request was aborted, ignore
225 return;
226 }
227
228 setError(err);
229 onError?.(err);
230 }
231 } finally {
232 setIsLoading(false);
233 abortControllerRef.current = null;
234 }
235 },
236 [
237 messages,
238 generateId,
239 api,
240 headers,
241 extraBody,
242 credentials,
243 fetchImpl,
244 onResponse,
245 onFinish,
246 onError,
247 processStream,
248 ]
249 );
250
251 const sendMessage = useCallback(
252 async (content: string) => {
253 if (!content.trim() || isLoading) {
254 return;
255 }
256
257 const userMessage: ChatMessage = {
258 id: generateId(),
259 role: "user",
260 content: content.trim(),
261 createdAt: new Date(),
262 };
263
264 await append(userMessage);
265 },
266 [isLoading, generateId, append]
267 );
268
269 const reload = useCallback(async () => {
270 if (messages.length === 0) return;
271
272 // Find the last user message
273 let lastUserMessageIndex = -1;
274 for (let i = messages.length - 1; i >= 0; i--) {
275 if (messages[i].role === "user") {
276 lastUserMessageIndex = i;
277 break;
278 }
279 }
280
281 if (lastUserMessageIndex === -1) return;
282
283 // Remove all messages after the last user message
284 const messagesToKeep = messages.slice(0, lastUserMessageIndex + 1);
285 setMessages(messagesToKeep);
286
287 // Resend the last user message
288 await append(messages[lastUserMessageIndex]);
289 }, [messages, append]);
290
291 const stop = useCallback(() => {
292 if (abortControllerRef.current) {
293 abortControllerRef.current.abort();
294 abortControllerRef.current = null;
295 setIsLoading(false);
296 }
297 }, []);
298
299 const clear = useCallback(() => {
300 setMessages([]);
301 setError(undefined);
302 }, []);
303
304 return {
305 messages,
306 sendMessage,
307 append,
308 reload,
309 stop,
310 isLoading,
311 error,
312 setMessages,
313 clear,
314 };
315}