Skip to content

Commit 7610987

Browse files
committed
feat(backend): refactor chat stream logic into useChatStream hook and remove unused chatStream utility
1 parent 9698cb8 commit 7610987

File tree

2 files changed

+73
-75
lines changed

2 files changed

+73
-75
lines changed

frontend/src/hooks/useChatStream.ts

+73-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { CREATE_CHAT } from '@/graphql/request';
44
import { Message } from '@/const/MessageType';
55
import { toast } from 'sonner';
66
import { logger } from '@/app/log/logger';
7-
import { startChatStream } from '@/utils/chatStream';
87
import { useAuthContext } from '@/providers/AuthProvider';
98

109
interface UseChatStreamProps {
@@ -63,14 +62,84 @@ export const useChatStream = ({
6362
},
6463
});
6564

65+
const startChatStream = async (
66+
targetChatId: string,
67+
message: string,
68+
model: string,
69+
stream: boolean = false // Default to non-streaming for better performance
70+
): Promise<string> => {
71+
if (!token) {
72+
throw new Error('Not authenticated');
73+
}
74+
75+
const response = await fetch('/api/chat', {
76+
method: 'POST',
77+
headers: {
78+
'Content-Type': 'application/json',
79+
Authorization: `Bearer ${token}`,
80+
},
81+
body: JSON.stringify({
82+
chatId: targetChatId,
83+
message,
84+
model,
85+
stream,
86+
}),
87+
});
88+
89+
if (!response.ok) {
90+
throw new Error(
91+
`Network response was not ok: ${response.status} ${response.statusText}`
92+
);
93+
}
94+
// TODO: Handle streaming responses properly
95+
// if (stream) {
96+
// // For streaming responses, aggregate the streamed content
97+
// let fullContent = '';
98+
// const reader = response.body?.getReader();
99+
// if (!reader) {
100+
// throw new Error('No reader available');
101+
// }
102+
103+
// while (true) {
104+
// const { done, value } = await reader.read();
105+
// if (done) break;
106+
107+
// const text = new TextDecoder().decode(value);
108+
// const lines = text.split('\n\n');
109+
110+
// for (const line of lines) {
111+
// if (line.startsWith('data: ')) {
112+
// const data = line.slice(5);
113+
// if (data === '[DONE]') break;
114+
// try {
115+
// const { content } = JSON.parse(data);
116+
// if (content) {
117+
// fullContent += content;
118+
// }
119+
// } catch (e) {
120+
// console.error('Error parsing SSE data:', e);
121+
// }
122+
// }
123+
// }
124+
// }
125+
// return fullContent;
126+
// } else {
127+
// // For non-streaming responses, return the content directly
128+
// const data = await response.json();
129+
// return data.content;
130+
// }
131+
132+
const data = await response.json();
133+
return data.content;
134+
};
135+
66136
const handleChatResponse = async (targetChatId: string, message: string) => {
67137
try {
68138
setInput('');
69139
const response = await startChatStream(
70140
targetChatId,
71141
message,
72-
selectedModel,
73-
token
142+
selectedModel
74143
);
75144

76145
setMessages((prev) => [
@@ -147,5 +216,6 @@ export const useChatStream = ({
147216
stop,
148217
isStreaming: loadingSubmit,
149218
currentChatId,
219+
startChatStream,
150220
};
151221
};

frontend/src/utils/chatStream.ts

-72
This file was deleted.

0 commit comments

Comments
 (0)