mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 07:48:37 +01:00
fix chat streaming + add "deleted" field to chats
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { getUserProjectFromId } from "~/server/LIVE_DEMO_DATA";
|
||||
|
||||
import { AiChatModel } from "@schema/ai/AiChatSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
@@ -7,10 +7,10 @@ export default defineEventHandler(async event => {
|
||||
if (!data) return;
|
||||
|
||||
const { project_id } = data;
|
||||
|
||||
|
||||
if (!event.context.params) return;
|
||||
const chat_id = event.context.params['chat_id'];
|
||||
|
||||
const result = await AiChatModel.deleteOne({ _id: chat_id, project_id });
|
||||
return result.deletedCount > 0;
|
||||
const result = await AiChatModel.updateOne({ _id: chat_id, project_id }, { deleted: true });
|
||||
return result.modifiedCount > 0;
|
||||
});
|
||||
@@ -9,7 +9,7 @@ export default defineEventHandler(async event => {
|
||||
|
||||
const { project_id } = data;
|
||||
|
||||
const chatList = await AiChatModel.find({ project_id }, { _id: 1, title: 1 }, { sort: { updated_at: 1 } });
|
||||
const chatList = await AiChatModel.find({ project_id, deleted: false }, { _id: 1, title: 1 }, { sort: { updated_at: 1 } });
|
||||
|
||||
return chatList.map(e => e.toJSON());
|
||||
|
||||
|
||||
@@ -47,8 +47,8 @@ export default defineEventHandler(async event => {
|
||||
await updateChatStatus(targetChatId, currentStatus.join(''), false);
|
||||
},
|
||||
onFinish: async calls => {
|
||||
currentStatus.push('[data:FunctionFinish]');
|
||||
await updateChatStatus(targetChatId, currentStatus.join(''), false);
|
||||
// currentStatus.push('[data:FunctionFinish]');
|
||||
// await updateChatStatus(targetChatId, currentStatus.join(''), false);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -128,6 +128,15 @@ async function elaborateResponse(messages: OpenAI.Chat.Completions.ChatCompletio
|
||||
functionCall.result = functionResult;
|
||||
await callbacks?.onFunctionResult?.(functionCall.name, functionResult);
|
||||
|
||||
|
||||
|
||||
addMessageToChat({
|
||||
tool_call_id: functionCall.tool_call_id,
|
||||
role: 'tool',
|
||||
content: JSON.stringify(functionResult)
|
||||
}, chat_id);
|
||||
|
||||
|
||||
addMessageToChat({
|
||||
role: 'assistant',
|
||||
content: delta.content,
|
||||
@@ -141,14 +150,7 @@ async function elaborateResponse(messages: OpenAI.Chat.Completions.ChatCompletio
|
||||
arguments: functionCall.argsRaw.join('')
|
||||
}
|
||||
}
|
||||
],
|
||||
parsed: null
|
||||
}, chat_id);
|
||||
|
||||
addMessageToChat({
|
||||
tool_call_id: functionCall.tool_call_id,
|
||||
role: 'tool',
|
||||
content: JSON.stringify(functionResult)
|
||||
]
|
||||
}, chat_id);
|
||||
|
||||
|
||||
@@ -181,6 +183,7 @@ export async function sendMessageOnChat(text: string, pid: string, initial_chat_
|
||||
|
||||
if (chatMessages && chatMessages.length > 0) {
|
||||
messages.push(...chatMessages);
|
||||
await updateChatStatus(chat_id, '', false);
|
||||
} else {
|
||||
const roleMessage: OpenAI.Chat.Completions.ChatCompletionMessageParam = {
|
||||
role: 'system',
|
||||
@@ -198,8 +201,9 @@ export async function sendMessageOnChat(text: string, pid: string, initial_chat_
|
||||
|
||||
try {
|
||||
const streamResponse = await elaborateResponse(messages, pid, chat_id, callbacks);
|
||||
await addMessageToChat({ role: 'assistant', refusal: null, content: await streamResponse.finalContent() }, chat_id);
|
||||
return { content: '', charts: [] };
|
||||
const finalContent = await streamResponse.finalContent();
|
||||
await addMessageToChat({ role: 'assistant', refusal: null, content: finalContent }, chat_id);
|
||||
return { content: finalContent, charts: [] };
|
||||
} catch (ex: any) {
|
||||
console.error(ex);
|
||||
return { content: ex.message, charts: [] };
|
||||
|
||||
Reference in New Issue
Block a user