-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathChatStreamAPI.ts
72 lines (66 loc) · 1.97 KB
/
ChatStreamAPI.ts
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
import { ChatInputType } from '@/graphql/type';
import authenticatedFetch from '@/lib/authenticatedFetch';
export const startChatStream = async (
input: ChatInputType,
token: string,
stream: boolean = false // Default to non-streaming for better performance
): Promise<string> => {
if (!token) {
throw new Error('Not authenticated');
}
const { chatId, message, model } = input;
const response = await authenticatedFetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
chatId,
message,
model,
stream,
}),
});
if (!response.ok) {
throw new Error(
`Network response was not ok: ${response.status} ${response.statusText}`
);
}
// TODO: Handle streaming responses properly
// if (stream) {
// // For streaming responses, aggregate the streamed content
// let fullContent = '';
// const reader = response.body?.getReader();
// if (!reader) {
// throw new Error('No reader available');
// }
// while (true) {
// const { done, value } = await reader.read();
// if (done) break;
// const text = new TextDecoder().decode(value);
// const lines = text.split('\n\n');
// for (const line of lines) {
// if (line.startsWith('data: ')) {
// const data = line.slice(5);
// if (data === '[DONE]') break;
// try {
// const { content } = JSON.parse(data);
// if (content) {
// fullContent += content;
// }
// } catch (e) {
// console.error('Error parsing SSE data:', e);
// }
// }
// }
// }
// return fullContent;
// } else {
// // For non-streaming responses, return the content directly
// const data = await response.json();
// return data.content;
// }
const data = await response.json();
return data.content;
};