mirror of
https://github.com/Litlyx/litlyx
synced 2025-12-10 15:58:38 +01:00
Add streaming to AI
This commit is contained in:
17
dashboard/server/api/ai/[chat_id]/status.ts
Normal file
17
dashboard/server/api/ai/[chat_id]/status.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
import { AiChatModel } from "@schema/ai/AiChatSchema";
|
||||
|
||||
export default defineEventHandler(async event => {
|
||||
const data = await getRequestData(event);
|
||||
if (!data) return;
|
||||
|
||||
const { project_id } = data;
|
||||
|
||||
if (!event.context.params) return;
|
||||
const chat_id = event.context.params['chat_id'];
|
||||
|
||||
const chat = await AiChatModel.findOne({ _id: chat_id, project_id }, { status: 1, completed: 1 });
|
||||
if (!chat) return;
|
||||
|
||||
return { status: chat.status, completed: chat.completed || false }
|
||||
});
|
||||
@@ -1,4 +1,4 @@
|
||||
import { sendMessageOnChat } from "~/server/services/AiService";
|
||||
import { sendMessageOnChat, updateChatStatus } from "~/server/services/AiService";
|
||||
import { getAiChatRemainings } from "./chats_remaining";
|
||||
|
||||
|
||||
@@ -15,7 +15,43 @@ export default defineEventHandler(async event => {
|
||||
const chatsRemaining = await getAiChatRemainings(pid);
|
||||
if (chatsRemaining <= 0) return setResponseStatus(event, 400, 'CHAT_LIMIT_REACHED');
|
||||
|
||||
const response = await sendMessageOnChat(text, pid, chat_id);
|
||||
const currentStatus: string[] = [];
|
||||
|
||||
let responseSent = false;
|
||||
|
||||
let targetChatId = '';
|
||||
|
||||
await sendMessageOnChat(text, pid, chat_id, {
|
||||
onChatId: async chat_id => {
|
||||
if (!responseSent) {
|
||||
event.node.res.setHeader('Content-Type', 'application/json');
|
||||
event.node.res.end(JSON.stringify({ chat_id }));
|
||||
targetChatId = chat_id;
|
||||
responseSent = true;
|
||||
}
|
||||
},
|
||||
onDelta: async text => {
|
||||
currentStatus.push(text);
|
||||
await updateChatStatus(targetChatId, currentStatus.join(''), false);
|
||||
},
|
||||
onFunctionName: async name => {
|
||||
currentStatus.push('[data:FunctionName]');
|
||||
await updateChatStatus(targetChatId, currentStatus.join(''), false);
|
||||
},
|
||||
onFunctionCall: async name => {
|
||||
currentStatus.push('[data:FunctionCall]');
|
||||
await updateChatStatus(targetChatId, currentStatus.join(''), false);
|
||||
},
|
||||
onFunctionResult: async (name, result) => {
|
||||
currentStatus.push('[data:FunctionResult]');
|
||||
await updateChatStatus(targetChatId, currentStatus.join(''), false);
|
||||
},
|
||||
onFinish: async calls => {
|
||||
currentStatus.push('[data:FunctionFinish]');
|
||||
await updateChatStatus(targetChatId, currentStatus.join(''), false);
|
||||
}
|
||||
});
|
||||
|
||||
await updateChatStatus(targetChatId, currentStatus.join(''), true);
|
||||
|
||||
return response;
|
||||
});
|
||||
Reference in New Issue
Block a user