feat: 实现会话记忆同步和知识图谱恢复机制
- 添加 memory_compacted SSE 事件处理 - 添加 CompactedMemory/CompactedMessage 类型定义 - 添加 COMPACTION_SUMMARY 消息类型 - 实现压缩数据存储到 conversation.json - 实现从 conversation.json 构建恢复数据 - 发送请求时附带 knowledgeData 用于恢复知识图谱
This commit is contained in:
@ -3,12 +3,15 @@
|
||||
* 整合 SSE 通信、工具执行、用户交互
|
||||
*/
|
||||
import * as vscode from 'vscode';
|
||||
import * as fs from 'fs';
|
||||
import * as path from 'path';
|
||||
import { startStreamDialog, generateTaskId, SSEController, SSECallbacks } from './sseHandler';
|
||||
import { executeToolCall, createToolExecutorContext, ToolExecutorContext } from './toolExecutor';
|
||||
import { userInteractionManager } from './userInteraction';
|
||||
import { getConfig } from '../config/settings';
|
||||
import type { DialogRequest, ToolCallRequest, AskUserEvent, RunMode, ToolConfirmEvent, PlanConfirmEvent } from '../types/api';
|
||||
import { submitToolConfirm, submitAnswer } from './apiClient';
|
||||
import { ChatHistoryManager } from '../utils/chatHistoryManager';
|
||||
|
||||
/**
|
||||
* 消息段落类型
|
||||
@ -152,6 +155,39 @@ export class DialogSession {
|
||||
return this.isActive;
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载知识图谱数据
|
||||
* 从 .iccoder/knowledge.json 读取
|
||||
*/
|
||||
private loadKnowledgeData(): string | null {
|
||||
console.log('[DialogSession] loadKnowledgeData 开始执行');
|
||||
|
||||
const workspaceFolders = vscode.workspace.workspaceFolders;
|
||||
if (!workspaceFolders || workspaceFolders.length === 0) {
|
||||
console.log('[DialogSession] 没有工作区文件夹');
|
||||
return null;
|
||||
}
|
||||
|
||||
const workspacePath = workspaceFolders[0].uri.fsPath;
|
||||
const knowledgePath = path.join(workspacePath, '.iccoder', 'knowledge.json');
|
||||
console.log('[DialogSession] 知识图谱路径:', knowledgePath);
|
||||
|
||||
try {
|
||||
const exists = fs.existsSync(knowledgePath);
|
||||
console.log('[DialogSession] 文件存在:', exists);
|
||||
|
||||
if (exists) {
|
||||
const content = fs.readFileSync(knowledgePath, 'utf-8');
|
||||
console.log('[DialogSession] 加载知识图谱成功, 长度:', content.length);
|
||||
return content;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('[DialogSession] 加载知识图谱失败:', error);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取工具操作描述(用于确认对话框)
|
||||
*/
|
||||
@ -210,13 +246,29 @@ export class DialogSession {
|
||||
this.currentTextSegment = null;
|
||||
|
||||
const config = getConfig();
|
||||
|
||||
// 获取压缩数据和新消息(用于后端重启后恢复)
|
||||
const historyManager = ChatHistoryManager.getInstance();
|
||||
const compactedData = await historyManager.loadCompactedData(this.taskId);
|
||||
const newMessages = historyManager.getNewMessagesSinceCompaction();
|
||||
|
||||
// 加载知识图谱数据
|
||||
const knowledgeData = this.loadKnowledgeData();
|
||||
console.log('[DialogSession] knowledgeData 加载结果:', knowledgeData ? `${knowledgeData.length} 字符` : 'null');
|
||||
|
||||
const request: DialogRequest = {
|
||||
taskId: this.taskId,
|
||||
message,
|
||||
userId: config.userId,
|
||||
mode: mode || 'agent'
|
||||
mode: mode || 'agent',
|
||||
compactedData: compactedData || undefined,
|
||||
newMessages: newMessages.length > 0 ? newMessages : undefined,
|
||||
knowledgeData: knowledgeData || undefined
|
||||
};
|
||||
|
||||
// 追踪用户消息
|
||||
historyManager.trackUserMessage(message);
|
||||
|
||||
const sseCallbacks: SSECallbacks = {
|
||||
onTextDelta: (data) => {
|
||||
this.accumulatedText += data.text;
|
||||
@ -466,6 +518,12 @@ export class DialogSession {
|
||||
}
|
||||
},
|
||||
|
||||
onMemoryCompacted: async (data) => {
|
||||
console.log('[DialogSession] onMemoryCompacted:', data.taskId);
|
||||
// 保存压缩数据到本地
|
||||
await historyManager.saveCompactedData(data.compactedData);
|
||||
},
|
||||
|
||||
onOpen: () => {
|
||||
console.log('[DialogSession] SSE 连接已建立');
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user