feat: v0.4.0 四阶段迭代 — 安全加固 + 工程基线 + 架构重构 + 双 Provider 扩展
P0 安全修复: - API Key 加密存储(safeStorage 密钥链,版本化前缀,历史明文平滑兼容) - 间接提示注入防护(SecurityScanHook 工具结果深扫描,网络工具脱敏/本地工具警示分级) - error:report IPC 断链修复(渲染进程错误上报落 electron-log + 审计) - abort 信号贯通工具层(run_command/dev-tools 子进程随会话中断终止) - run_command 沙箱加固(cd 系统目录/敏感文件读取拦截 + chcp 前缀剥离防解析退化) - .env 真实生效(dotenv 回退加载,应用内配置优先) P1 工程基础: - ESLint 9 flat config + 全部 34 条存量 warnings 清零(零容忍基线) - 测试基线 118 用例 11 文件(token/文件防护/权限/沙箱/注入/命令/引擎/注册表/审计链/摘要分层) - test:electron 双模式(ELECTRON_RUN_AS_NODE 跑 Electron ABI,SQLite 套件全执行) - SessionRecorder 多会话隔离 + 9 种 TRACE 事件补全(含最终轮 iteration_end) - Provider 故障转移(重试耗尽/不可重试一次性切换 fallback + 前端通知) - MCP 真就绪(等待全部连接完成再广播 tools:ready) - SLO/HealthChecker 真实接入(60s 巡检 + 托盘状态) - CONFIG_DEFAULTS 单一来源(消除 SEED 双源漂移) P2 架构升级: - handlers.ts 1940 行拆分为 13 个 IPC 域模块(防重入注册 + 多窗口广播) - AgentEngineManager 每会话独立引擎(LRU 30 + adapter 工厂隔离 abort 信号) - TaskOrchestrator EngineProvider 改造 + abortByParent 联动中断 SubAgent - 会话摘要分层上下文(session_summaries 滚动摘要 + 截断游标清理防因果污染) - 消息编辑重发/重新生成(truncateAfter IPC + store 动作 + UI) - Markdown 导出 / WebSearch 并行抓取(并发 3)/ 记忆 TF 缓存 / 版本构建期注入 P3 能力扩展: - OpenAI Adapter(o 系列推理模型 reasoning_effort/max_completion_tokens) - Anthropic Adapter(原生 Messages API:tool_use 块/角色合并/thinking budget/图片 base64/SSE 事件机) - 设置页/Onboarding 六 Provider 全链路接入
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* IPC Memory Handlers — 记忆系统域(P2-9 从 handlers.ts 拆分)
|
||||
*/
|
||||
|
||||
import { ipcMain } from 'electron';
|
||||
import type { IPCContext } from './context';
|
||||
import type { MemoryType } from '../harness/memory/manager';
|
||||
|
||||
const VALID_MEMORY_TYPES: readonly MemoryType[] = ['episodic', 'semantic', 'working'];
|
||||
|
||||
export function registerMemoryHandlers(ctx: IPCContext): void {
|
||||
const { memoryManager, sessionService } = ctx;
|
||||
|
||||
ipcMain.handle('db:searchMemories', async (_event, query: unknown, options: unknown) => {
|
||||
// M-37 修复: query 和 options 校验
|
||||
if (typeof query !== 'string' || !query.trim()) {
|
||||
return [];
|
||||
}
|
||||
// 构造合法的搜索选项(仅保留已知字段,强制类型安全)
|
||||
const searchOptions: { topK?: number; sessionId?: string; type?: MemoryType; minImportance?: number } = {};
|
||||
if (options && typeof options === 'object') {
|
||||
const opts = options as Record<string, unknown>;
|
||||
// topK 限制范围 1-100(防止过大查询)
|
||||
if (opts.topK !== undefined) {
|
||||
const topK = Number(opts.topK);
|
||||
if (Number.isFinite(topK) && topK >= 1 && topK <= 100) {
|
||||
searchOptions.topK = topK;
|
||||
} else {
|
||||
searchOptions.topK = 10; // 默认值
|
||||
}
|
||||
}
|
||||
if (typeof opts.sessionId === 'string') searchOptions.sessionId = opts.sessionId;
|
||||
// type 必须是合法的 MemoryType 枚举值
|
||||
if (typeof opts.type === 'string' && (VALID_MEMORY_TYPES as readonly string[]).includes(opts.type)) {
|
||||
searchOptions.type = opts.type as MemoryType;
|
||||
}
|
||||
if (typeof opts.minImportance === 'number' && Number.isFinite(opts.minImportance)) {
|
||||
searchOptions.minImportance = opts.minImportance;
|
||||
}
|
||||
}
|
||||
return memoryManager.search(query, searchOptions);
|
||||
});
|
||||
|
||||
// ===== v0.2.0: 记忆系统增强查询(Memory Viewer UI) =====
|
||||
const VALID_MEMORY_TYPES_LIST: readonly string[] = ['episodic', 'semantic', 'working'];
|
||||
|
||||
ipcMain.handle('memory:listAll', async (_event, options?: unknown) => {
|
||||
// M-49 修复: 校验 options.type 枚举和 limit 范围
|
||||
let limit = 100;
|
||||
let type: string | undefined;
|
||||
if (options && typeof options === 'object') {
|
||||
const opts = options as Record<string, unknown>;
|
||||
if (opts.type !== undefined) {
|
||||
if (typeof opts.type !== 'string' || !VALID_MEMORY_TYPES_LIST.includes(opts.type)) {
|
||||
return { success: false, error: `Invalid type (must be one of: ${VALID_MEMORY_TYPES_LIST.join(', ')})` };
|
||||
}
|
||||
type = opts.type;
|
||||
}
|
||||
if (opts.limit !== undefined) {
|
||||
const num = Number(opts.limit);
|
||||
// 限制 1-1000 范围,SQLite 中 LIMIT -1 表示无限制,需阻止
|
||||
if (!Number.isFinite(num) || num < 1 || num > 1000) {
|
||||
return { success: false, error: 'Invalid limit (must be 1-1000)' };
|
||||
}
|
||||
limit = Math.floor(num);
|
||||
}
|
||||
}
|
||||
const db = sessionService.getDB();
|
||||
const results: Record<string, unknown[]> = {};
|
||||
try {
|
||||
if (!type || type === 'episodic') {
|
||||
const rows = db.prepare('SELECT * FROM episodic_memories ORDER BY created_at DESC LIMIT ?').all(limit) as Array<Record<string, unknown>>;
|
||||
results.episodic = rows.map((r) => ({ ...r, type: 'episodic', content: r.content ?? '' }));
|
||||
}
|
||||
if (!type || type === 'semantic') {
|
||||
const rows = db.prepare('SELECT * FROM semantic_memories ORDER BY updated_at DESC LIMIT ?').all(limit) as Array<Record<string, unknown>>;
|
||||
results.semantic = rows.map((r) => ({ ...r, type: 'semantic', content: r.value ?? r.key ?? '', importance: r.confidence ?? 0, created_at: r.created_at ?? r.updated_at }));
|
||||
}
|
||||
if (!type || type === 'working') {
|
||||
const rows = db.prepare('SELECT * FROM working_memories ORDER BY updated_at DESC LIMIT ?').all(limit) as Array<Record<string, unknown>>;
|
||||
results.working = rows.map((r) => ({ ...r, type: 'working', content: r.value ?? r.key ?? '', importance: 0.5, created_at: r.updated_at ?? Date.now() }));
|
||||
}
|
||||
return { success: true, data: results };
|
||||
} catch (error) {
|
||||
return { success: false, error: (error as Error).message };
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('memory:delete', async (_event, type: unknown, id: unknown) => {
|
||||
// M-50 修复: 校验 type 枚举(防止三元表达式默认映射到 working_memories)和 id 类型
|
||||
if (typeof type !== 'string' || !VALID_MEMORY_TYPES_LIST.includes(type)) {
|
||||
return { success: false, error: `Invalid type (must be one of: ${VALID_MEMORY_TYPES_LIST.join(', ')})` };
|
||||
}
|
||||
if (typeof id !== 'string' || !id) {
|
||||
return { success: false, error: 'Invalid memory id' };
|
||||
}
|
||||
const db = sessionService.getDB();
|
||||
try {
|
||||
const table = type === 'episodic' ? 'episodic_memories' : type === 'semantic' ? 'semantic_memories' : 'working_memories';
|
||||
db.prepare(`DELETE FROM ${table} WHERE id = ?`).run(id);
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
return { success: false, error: (error as Error).message };
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user