Merge branch 'main' into yxl
This commit is contained in:
@@ -107,102 +107,108 @@ const useChatGroupDBStore = defineStore("messageGroup", () => {
|
||||
return await baseDB.db.add(massageName.value, payload);
|
||||
}
|
||||
|
||||
async function getStearm(text, fileUrls = [], progress, options = {}) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
toggleTyping(true);
|
||||
const customDataID = 'message_' + UUID.generate()
|
||||
const params = {
|
||||
data: text,
|
||||
sessionId: chatSessionID.value,
|
||||
dataId: customDataID
|
||||
};
|
||||
if (fileUrls && fileUrls.length) {
|
||||
params['fileUrl'] = fileUrls.map((item) => item.url);
|
||||
}
|
||||
// ------>
|
||||
const MsgData = {
|
||||
text: text,
|
||||
self: true,
|
||||
displayText: text,
|
||||
files: fileUrls
|
||||
};
|
||||
addMessage(MsgData); // 添加message数据
|
||||
// <------
|
||||
const newMsg = {
|
||||
text: '', // 存储原始结构化内容
|
||||
self: false,
|
||||
displayText: '', // 用于流式渲染展示
|
||||
dataId: customDataID
|
||||
};
|
||||
const index = messages.value.length;
|
||||
messages.value.push(newMsg);
|
||||
|
||||
const rawParts = Array.isArray(text) ? text : [text]; // 统一处理
|
||||
|
||||
// 用于追加每个部分的流式数据
|
||||
let partIndex = 0;
|
||||
|
||||
function handleUnload() {
|
||||
newMsg.parentGroupId = chatSessionID.value;
|
||||
baseDB.db.add(massageName.value, newMsg).then((id) => {
|
||||
messages.value[index] = {
|
||||
...newMsg,
|
||||
id
|
||||
};
|
||||
});
|
||||
}
|
||||
// #ifdef H5
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener("unload", handleUnload);
|
||||
}
|
||||
// #endif
|
||||
|
||||
function onDataReceived(data) {
|
||||
// 支持追加多个部分
|
||||
newMsg.text += data;
|
||||
newMsg.displayText += data;
|
||||
messages.value[index] = {
|
||||
...newMsg
|
||||
};
|
||||
progress && progress();
|
||||
|
||||
// 调用外部传入的onDataReceived回调
|
||||
if (options.onDataReceived) {
|
||||
options.onDataReceived(data, newMsg, index);
|
||||
}
|
||||
}
|
||||
|
||||
function onError(error) {
|
||||
msg('服务响应异常');
|
||||
reject(error);
|
||||
}
|
||||
|
||||
function onComplete() {
|
||||
messages.value[index] = {
|
||||
...newMsg
|
||||
};
|
||||
toggleTyping(false);
|
||||
// #ifdef H5
|
||||
if (typeof window !== 'undefined') {
|
||||
window.removeEventListener("unload", handleUnload);
|
||||
}
|
||||
// #endif
|
||||
handleUnload();
|
||||
// 调用外部传入的onComplete回调
|
||||
if (options.onComplete) {
|
||||
options.onComplete();
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
|
||||
$api.streamRequest('/chat', params, onDataReceived, onError, onComplete);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
async function getStearm(text, fileUrls = [], progress, options = {}) {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
toggleTyping(true);
|
||||
const customDataID = 'message_' + UUID.generate()
|
||||
|
||||
// 对话历史管理:只保留最近的N条消息,防止token超限
|
||||
// 计算消息数量,只保留最近的10条消息(可根据实际情况调整)
|
||||
const MAX_HISTORY_MESSAGES = 10;
|
||||
const historyMessages = messages.value.slice(-MAX_HISTORY_MESSAGES);
|
||||
|
||||
const params = {
|
||||
data: text,
|
||||
sessionId: chatSessionID.value,
|
||||
dataId: customDataID
|
||||
};
|
||||
if (fileUrls && fileUrls.length) {
|
||||
params['fileUrl'] = fileUrls.map((item) => item.url);
|
||||
}
|
||||
// ------>
|
||||
const MsgData = {
|
||||
text: text,
|
||||
self: true,
|
||||
displayText: text,
|
||||
files: fileUrls
|
||||
};
|
||||
addMessage(MsgData); // 添加message数据
|
||||
// <------
|
||||
const newMsg = {
|
||||
text: '', // 存储原始结构化内容
|
||||
self: false,
|
||||
displayText: '', // 用于流式渲染展示
|
||||
dataId: customDataID
|
||||
};
|
||||
const index = messages.value.length;
|
||||
messages.value.push(newMsg);
|
||||
|
||||
const rawParts = Array.isArray(text) ? text : [text]; // 统一处理
|
||||
|
||||
// 用于追加每个部分的流式数据
|
||||
let partIndex = 0;
|
||||
|
||||
function handleUnload() {
|
||||
newMsg.parentGroupId = chatSessionID.value;
|
||||
baseDB.db.add(massageName.value, newMsg).then((id) => {
|
||||
messages.value[index] = {
|
||||
...newMsg,
|
||||
id
|
||||
};
|
||||
});
|
||||
}
|
||||
// #ifdef H5
|
||||
if (typeof window !== 'undefined') {
|
||||
window.addEventListener("unload", handleUnload);
|
||||
}
|
||||
// #endif
|
||||
|
||||
function onDataReceived(data) {
|
||||
// 支持追加多个部分
|
||||
newMsg.text += data;
|
||||
newMsg.displayText += data;
|
||||
messages.value[index] = {
|
||||
...newMsg
|
||||
};
|
||||
progress && progress();
|
||||
|
||||
// 调用外部传入的onDataReceived回调
|
||||
if (options.onDataReceived) {
|
||||
options.onDataReceived(data, newMsg, index);
|
||||
}
|
||||
}
|
||||
|
||||
function onError(error) {
|
||||
msg('服务响应异常');
|
||||
reject(error);
|
||||
}
|
||||
|
||||
function onComplete() {
|
||||
messages.value[index] = {
|
||||
...newMsg
|
||||
};
|
||||
toggleTyping(false);
|
||||
// #ifdef H5
|
||||
if (typeof window !== 'undefined') {
|
||||
window.removeEventListener("unload", handleUnload);
|
||||
}
|
||||
// #endif
|
||||
handleUnload();
|
||||
// 调用外部传入的onComplete回调
|
||||
if (options.onComplete) {
|
||||
options.onComplete();
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
|
||||
$api.streamRequest('/chat', params, onDataReceived, onError, onComplete);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 状态控制
|
||||
|
||||
Reference in New Issue
Block a user