AI聊天更新历史记录

This commit is contained in:
14651
2025-08-14 00:36:04 +08:00
parent 3743c72a54
commit 362f286759
7 changed files with 1181 additions and 702 deletions

View File

@@ -1,4 +1,4 @@
// src/utils/ai_stream.js (H5 优化版)
// src/utils/ai_stream.js
import {
getToken
} from '@/utils/auth';
@@ -23,7 +23,6 @@ export function createChatStream(params) {
const fetchPromise = fetch(url, {
method: 'POST',
headers: {
'Accept': 'text/event-stream',
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'X-Request-ID': requestId
@@ -32,26 +31,27 @@ export function createChatStream(params) {
query: params.prompt,
user_id: params.userId,
user_name: params.userName,
user_token: params.user_token || '123',
user_token: params.user_token || '123',
user_role: 'student',
conversation_id: params.conversationId || null,
conversation_id: params.conversationId || null,
}),
signal: controller.signal
})
.then(resp => {
if (!resp.ok) throw new Error(`HTTP ${resp.status}`);
if (!resp.body) throw new Error('Response body is null');
return resp.body;
.then(response => {
if (!response.ok) throw new Error(`HTTP ${response.status}`);
if (!response.body) throw new Error('Response body is null');
return {
reader: response.body.getReader(),
decoder: new TextDecoder('utf-8')
};
});
return fetchPromise.then(body => ({
stream: Promise.resolve({
reader: body.getReader(),
decoder: new TextDecoder('utf-8')
}),
cancel: reason => !controller.signal.aborted && controller.abort(reason)
})).catch(err => ({
stream: Promise.reject(err),
cancel: () => {}
}));
return {
stream: fetchPromise,
cancel: (reason) => {
if (!controller.signal.aborted) {
controller.abort(reason);
}
}
};
}