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 全链路接入
82 lines
3.0 KiB
TypeScript
82 lines
3.0 KiB
TypeScript
/**
|
||
* Post-Tool Hooks — 工具执行后钩子
|
||
*
|
||
* 用途:结果验证、审计日志写入、记忆触发、错误恢复建议。
|
||
*
|
||
* @see docs/生产级通用 AI Agent 智能体桌面应用:完整设计与构建指南.html — 第五章
|
||
*/
|
||
|
||
import type { MetonaToolCall, MetonaToolResult } from '../types';
|
||
import type { AuditService } from '../../services/audit.service';
|
||
import type { MemoryManager } from '../memory/manager';
|
||
import log from 'electron-log';
|
||
|
||
export interface PostToolHook {
|
||
/**
|
||
* 工具执行后钩子
|
||
*
|
||
* P0-2: 返回修改后的 MetonaToolResult 可替换原始结果(如 SecurityScanHook 对
|
||
* 网页内容脱敏);返回 void / undefined 表示保持原结果不变。
|
||
*/
|
||
afterExecute(
|
||
toolCall: MetonaToolCall,
|
||
result: MetonaToolResult,
|
||
sessionId: string,
|
||
): Promise<MetonaToolResult | void>;
|
||
}
|
||
|
||
/** 审计日志钩子 */
|
||
export class AuditLogHook implements PostToolHook {
|
||
constructor(private auditService: AuditService) {}
|
||
|
||
async afterExecute(toolCall: MetonaToolCall, result: MetonaToolResult, sessionId: string): Promise<void> {
|
||
// #17 修复: AuditHook 应为 "fire and forget",hook 失败不应影响工具执行链
|
||
// 虽然 AuditService.log() 内部已 try-catch,但 hook 层再加一层防御,
|
||
// 确保任何意外异常(如 getDB 抛错、JSON.stringify 失败)都不会冒泡到 ToolRegistry
|
||
try {
|
||
this.auditService.logToolCall({
|
||
sessionId,
|
||
iteration: toolCall.iteration,
|
||
toolName: toolCall.name,
|
||
args: toolCall.args,
|
||
outcome: result.success ? 'success' : 'error',
|
||
result: result.result,
|
||
error: result.error,
|
||
durationMs: result.durationMs,
|
||
});
|
||
} catch (err) {
|
||
log.error('[AuditLogHook] Failed to log audit:', err);
|
||
// 不抛出,让工具执行链继续
|
||
}
|
||
}
|
||
}
|
||
|
||
/** 记忆触发钩子 */
|
||
export class MemoryTriggerHook implements PostToolHook {
|
||
/** 仅对搜索类工具触发记忆,避免每次 read_file 都产生低价值记忆条目 */
|
||
private memorableTools = ['web_search', 'memory_search'];
|
||
|
||
/** 单次工具结果存储上限(字符),防止过大内容淹没记忆系统 */
|
||
private readonly MAX_MEMORY_CONTENT = 500;
|
||
|
||
constructor(private memoryManager: MemoryManager) {}
|
||
|
||
async afterExecute(toolCall: MetonaToolCall, result: MetonaToolResult, sessionId: string): Promise<void> {
|
||
if (this.memorableTools.includes(toolCall.name) && result.success) {
|
||
const content = typeof result.result === 'string' ? result.result : JSON.stringify(result.result);
|
||
try {
|
||
this.memoryManager.store({
|
||
type: 'episodic',
|
||
content: `Tool ${toolCall.name} returned: ${content.slice(0, this.MAX_MEMORY_CONTENT)}`,
|
||
source: 'tool_result',
|
||
sessionId,
|
||
importance: 0.6,
|
||
});
|
||
} catch (error) {
|
||
// 记忆存储失败不应影响工具执行结果
|
||
log.error('[MemoryTriggerHook] Failed to store episodic memory:', error);
|
||
}
|
||
}
|
||
}
|
||
}
|