feat:记录会话,按顺序记录AI和用户的会话
- 包含工具调用等
This commit is contained in:
@ -333,6 +333,24 @@ export class ChatHistoryManager {
|
||||
await this.saveConversation(messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加工具执行结果消息
|
||||
*/
|
||||
public async addToolExecutionResult(id: string, toolName: string, result: string): Promise<void> {
|
||||
await this.ensureCurrentTask();
|
||||
const messages = await this.loadConversation();
|
||||
|
||||
const toolResultMessage: ToolExecutionResultMessage = {
|
||||
type: MessageType.TOOL_EXECUTION_RESULT,
|
||||
id,
|
||||
toolName,
|
||||
text: result
|
||||
};
|
||||
|
||||
messages.push(toolResultMessage);
|
||||
await this.saveConversation(messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录对话轮次元数据
|
||||
*/
|
||||
|
||||
@ -170,6 +170,39 @@ async function handleUserMessageWithBackend(
|
||||
});
|
||||
console.log('[MessageHandler] postMessage 返回值:', result);
|
||||
|
||||
// 按照 segments 顺序保存AI响应到历史记录
|
||||
try {
|
||||
// 遍历 segments,按顺序保存每个段落
|
||||
for (const segment of segments) {
|
||||
if (segment.type === 'text' && segment.content) {
|
||||
// 保存文本消息
|
||||
await historyManager.addAiMessage(segment.content);
|
||||
} else if (segment.type === 'tool') {
|
||||
// 保存工具调用请求(作为AI消息的一部分)
|
||||
const toolRequest = {
|
||||
id: segment.askId || `tool_${Date.now()}`,
|
||||
name: segment.toolName || '',
|
||||
arguments: ''
|
||||
};
|
||||
await historyManager.addAiMessage('', [toolRequest]);
|
||||
|
||||
// 如果有工具执行结果,保存工具执行结果
|
||||
if (segment.toolResult) {
|
||||
await historyManager.addToolExecutionResult(
|
||||
toolRequest.id,
|
||||
segment.toolName || '',
|
||||
segment.toolResult
|
||||
);
|
||||
}
|
||||
} else if (segment.type === 'question') {
|
||||
// 保存问题(作为AI消息)
|
||||
await historyManager.addAiMessage(segment.question || '');
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("保存AI响应历史失败:", error);
|
||||
}
|
||||
|
||||
resolve();
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user