fix: 修改错误
This commit is contained in:
parent
8436b0462a
commit
1f81d3400c
@ -54,29 +54,46 @@ const onMessage = async ({ data, end, ws }) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const { uid, id, key } = client.data;
|
const { uid, id, key } = client.data;
|
||||||
const { inputs, message: sendMessage } = data;
|
const {
|
||||||
|
inputs,
|
||||||
|
message: sendMessage,
|
||||||
|
data: {},
|
||||||
|
} = data;
|
||||||
let root = data.root || false;
|
let root = data.root || false;
|
||||||
let chatSession = await ChatSession.findByPk(id);
|
let chatSession = await ChatSession.findByPk(id);
|
||||||
const config = await getConfigByKey(key);
|
const config = await getConfigByKey(key);
|
||||||
const { prompt, aiAgent, chatPrompt } = config;
|
const { prompt, aiAgent, chatPrompt } = config;
|
||||||
|
let userQuestion = sendMessage;
|
||||||
if (!chatSession) {
|
if (!chatSession) {
|
||||||
chatSession = await ChatSession.create({ key, id, data: {}, uid, chatPromptId: chatPrompt.id });
|
chatSession = await ChatSession.create({ key, id, data: data, uid, chatPromptId: chatPrompt.id });
|
||||||
root = true;
|
root = true;
|
||||||
} else {
|
} else {
|
||||||
|
// 更新session context的值
|
||||||
|
const newData = JSON.parse(data);
|
||||||
|
if (newData !== '{}' && JSON.stringify(chatSession.data) !== JSON.stringify(data)) {
|
||||||
|
await chatSession.update({ data: data });
|
||||||
|
}
|
||||||
|
if (root) {
|
||||||
|
const chatHistory = await ChatHistory.findAll({ where: { chatId: id }, logging: false });
|
||||||
|
chatHistory.forEach((item) => {
|
||||||
|
end({ code: 200, data: item, message: 'success', type: 'messages' });
|
||||||
|
});
|
||||||
|
// return;
|
||||||
|
}
|
||||||
root = false;
|
root = false;
|
||||||
const chatHistory = await ChatHistory.findAll({ where: { chatId: id }, logging: false });
|
|
||||||
chatHistory.forEach((item) => {
|
|
||||||
end({ code: 200, data: item, message: 'success', type: 'messages' });
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
if (!userQuestion) {
|
||||||
const template = await getTemplate({ data: prompt.presetData.data, inputs });
|
if (!prompt?.presetData) {
|
||||||
if (!template) {
|
end({ code: 404, data: {}, message: 'prompt not set, need presetData' });
|
||||||
end({ code: 404, data: {}, message: 'template not found' });
|
return;
|
||||||
return;
|
}
|
||||||
|
const template = await getTemplate({ data: prompt.presetData.data, inputs });
|
||||||
|
if (!template) {
|
||||||
|
end({ code: 404, data: {}, message: 'template not found' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
userQuestion = template;
|
||||||
}
|
}
|
||||||
const userQuestion = template || sendMessage;
|
|
||||||
// 保存到数据库
|
// 保存到数据库
|
||||||
const roleUser = await ChatHistory.create({
|
const roleUser = await ChatHistory.create({
|
||||||
data: {
|
data: {
|
||||||
@ -90,7 +107,7 @@ const onMessage = async ({ data, end, ws }) => {
|
|||||||
role: 'user',
|
role: 'user',
|
||||||
});
|
});
|
||||||
end({ code: 200, data: roleUser, message: 'success', type: 'messages' });
|
end({ code: 200, data: roleUser, message: 'success', type: 'messages' });
|
||||||
const result = await aiAgent.sendHumanMessage(template, { thread_id: id });
|
const result = await aiAgent.sendHumanMessage(userQuestion, { thread_id: id });
|
||||||
const lastMessage = result.messages[result.messages.length - 1];
|
const lastMessage = result.messages[result.messages.length - 1];
|
||||||
const message = result.messages[result.messages.length - 1].content;
|
const message = result.messages[result.messages.length - 1].content;
|
||||||
// 根据key找到对应的prompt
|
// 根据key找到对应的prompt
|
||||||
|
Loading…
x
Reference in New Issue
Block a user