forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseChat.ts
More file actions
306 lines (277 loc) · 9.42 KB
/
Copy pathuseChat.ts
File metadata and controls
306 lines (277 loc) · 9.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
'use client';
import { useState, useCallback, useRef, useEffect } from 'react';
import {
getMessages,
sendMessageStream,
clearMessages as clearMessagesApi,
saveRealtimeMessage,
} from '@/lib/api';
import type { ClientMessage, MessageChunk, MessageFile } from '@/types/conversation';
import type { ChatContextInfo } from '@/components/chat/ChatContext';
import { useRequestOwner } from '@/hooks/useRequestOwner';
interface UseChatOptions {
appId?: string;
/**
* Which chat session to read. `null` is the default shared thread every
* client sees through `/v2/messages`; an id targets that one thread.
*/
chatSessionId?: string | null;
}
interface UseChatReturn {
messages: ClientMessage[];
isLoading: boolean;
isStreaming: boolean;
streamingText: string;
currentThinking: string;
error: string | null;
sendMessage: (
text: string,
fileIds?: string[],
context?: ChatContextInfo | null,
optimisticFiles?: MessageFile[],
) => Promise<void>;
clearHistory: () => Promise<void>;
loadHistory: () => Promise<void>;
appendRealtimeExchange: (humanText: string, aiText: string) => Promise<void>;
}
export function useChat(options: UseChatOptions = {}): UseChatReturn {
const { appId, chatSessionId = null } = options;
const [messages, setMessages] = useState<ClientMessage[]>([]);
const [isLoading, setIsLoading] = useState(false);
const [isStreaming, setIsStreaming] = useState(false);
const [streamingText, setStreamingText] = useState('');
const [currentThinking, setCurrentThinking] = useState('');
const [error, setError] = useState<string | null>(null);
// Track current app + session to detect changes
const currentAppIdRef = useRef(appId);
const currentSessionIdRef = useRef(chatSessionId);
// Track if we've loaded history for the current app + session
const historyLoadedRef = useRef<string | null>(null);
// A load or a stream belongs to the thread it was started for. Switching
// threads mid-flight must not let the previous thread's response land in the
// newly-selected one.
const claimRequest = useRequestOwner(`${appId ?? ''}||${chatSessionId ?? ''}`);
// Reset state when the app or the selected session changes. Switching threads
// must clear the transcript: leaving the previous session's messages on
// screen reads as though they belong to the newly-selected chat. The
// transient stream state goes with it, for the same reason.
useEffect(() => {
if (
currentAppIdRef.current !== appId ||
currentSessionIdRef.current !== chatSessionId
) {
currentAppIdRef.current = appId;
currentSessionIdRef.current = chatSessionId;
historyLoadedRef.current = null;
setMessages([]);
setError(null);
setStreamingText('');
setCurrentThinking('');
setIsStreaming(false);
setIsLoading(false);
}
}, [appId, chatSessionId]);
/**
* Load message history from server
*/
const loadHistory = useCallback(async () => {
const targetKey = `${appId ?? ''}||${chatSessionId ?? ''}`;
if (historyLoadedRef.current === targetKey) return;
const isCurrent = claimRequest();
setIsLoading(true);
setError(null);
try {
const history = await getMessages(appId, chatSessionId);
if (!isCurrent()) return;
setMessages([...history].reverse());
historyLoadedRef.current = targetKey;
} catch (err) {
if (!isCurrent()) return;
console.error('Failed to load message history:', err);
setError('Failed to load message history');
} finally {
if (isCurrent()) setIsLoading(false);
}
}, [appId, chatSessionId, claimRequest]);
/**
* Send a message and handle streaming response
*/
const sendMessage = useCallback(
async (
text: string,
fileIds?: string[],
context?: ChatContextInfo | null,
optimisticFiles?: MessageFile[],
) => {
if ((!text.trim() && !fileIds?.length) || isStreaming) return;
const isCurrent = claimRequest();
setError(null);
setIsStreaming(true);
setStreamingText('');
setCurrentThinking('');
// Add user message to the list immediately (optimistic update).
// Client-only fields (`ask_for_nps`) live on ClientMessage; backend REST
// authority for messages is the generated `Message` schema.
const createdAt = new Date().toISOString();
const uploadedFilesById = new Map(
optimisticFiles?.map((file) => [file.id, file]) ?? [],
);
const userMessage: ClientMessage = {
id: `temp-${Date.now()}`,
created_at: createdAt,
text: text.trim(),
sender: 'human',
type: 'text',
from_external_integration: false,
files: (fileIds ?? []).map(
(id) =>
uploadedFilesById.get(id) ?? {
id,
created_at: createdAt,
mime_type: 'application/octet-stream',
name: 'Attached file',
openai_file_id: '',
},
),
memories: [],
ask_for_nps: false,
};
setMessages((prev) => [...prev, userMessage]);
let accumulatedText = '';
try {
await sendMessageStream(
text.trim(),
(chunk: MessageChunk) => {
// Chunks that arrive after the reader moved to another thread
// belong to the thread they were requested for, not this one.
if (!isCurrent()) return;
switch (chunk.type) {
case 'think':
setCurrentThinking((prev) => prev + chunk.text);
break;
case 'data':
accumulatedText += chunk.text;
setStreamingText(accumulatedText);
break;
case 'done':
// Replace streaming text with final message
if (chunk.message) {
setMessages((prev) => [...prev, chunk.message!]);
}
setStreamingText('');
setCurrentThinking('');
break;
case 'message':
// Handle related memory messages if needed
// chunk.message contains related memory data
break;
case 'error':
setError(chunk.text);
break;
}
},
{ appId, chatSessionId, fileIds, context: context || null },
);
} catch (err) {
if (!isCurrent()) return;
console.error('Failed to send message:', err);
setError(err instanceof Error ? err.message : 'Failed to send message');
// If we have accumulated text, add it as a partial message
if (accumulatedText) {
const partialMessage: ClientMessage = {
id: `error-${Date.now()}`,
created_at: new Date().toISOString(),
text: accumulatedText + '\n\n[Message interrupted]',
sender: 'ai',
type: 'text',
from_external_integration: false,
files: [],
memories: [],
ask_for_nps: false,
};
setMessages((prev) => [...prev, partialMessage]);
}
} finally {
if (isCurrent()) {
setIsStreaming(false);
setStreamingText('');
}
}
},
[appId, chatSessionId, isStreaming, claimRequest],
);
/**
* Clear all message history
*/
const clearHistory = useCallback(async () => {
const isCurrent = claimRequest();
setIsLoading(true);
setError(null);
try {
await clearMessagesApi(appId, chatSessionId);
if (!isCurrent()) return;
setMessages([]);
historyLoadedRef.current = null;
} catch (err) {
if (!isCurrent()) return;
console.error('Failed to clear messages:', err);
setError('Failed to clear message history');
} finally {
if (isCurrent()) setIsLoading(false);
}
}, [appId, chatSessionId, claimRequest]);
const appendRealtimeExchange = useCallback(
async (humanText: string, aiText: string) => {
const isCurrent = claimRequest();
const entries = [
{ text: humanText.trim(), sender: 'human' as const },
{ text: aiText.trim(), sender: 'ai' as const },
].filter((entry) => entry.text);
if (entries.length === 0) return;
const optimistic = entries.map((entry) => ({
id: crypto.randomUUID(),
created_at: new Date().toISOString(),
text: entry.text,
sender: entry.sender,
type: 'text' as const,
from_external_integration: false,
files: [],
memories: [],
ask_for_nps: false,
message_source: 'realtime_voice',
chat_session_id: chatSessionId,
app_id: appId,
}));
setMessages((current) => [...current, ...optimistic]);
setError(null);
try {
for (const message of optimistic) {
await saveRealtimeMessage({
text: message.text,
sender: message.sender,
clientMessageId: message.id,
appId,
sessionId: chatSessionId,
});
}
} catch (err) {
console.error('Failed to save live conversation:', err);
if (isCurrent()) setError('Live conversation was not saved to chat history');
throw err;
}
},
[appId, chatSessionId, claimRequest],
);
return {
messages,
isLoading,
isStreaming,
streamingText,
currentThinking,
error,
sendMessage,
clearHistory,
loadHistory,
appendRealtimeExchange,
};
}