Files
metona-ai-desktop/electron/harness/tools/built-in/memory.ts
T
thzxx 9b45c445bf
CI / 类型检查 + Lint + 单元测试 (push) Failing after 9m8s
CI / 全量测试 (Electron ABI) (push) Failing after 6m0s
CI / 产物编译验证 (push) Successful in 10m58s
feat: v0.8.1 记忆深化 · 观测闭环 · 体验收口 — 窗口/输出上限全局单一配置 · 2478 用例全量回归 + E2E 冒烟
硬性契约:删除代码中一切写死的上下文窗口与最大输出上限(含六家模型元信息
钳制与全部兜底值)——唯一合法来源是设置面板「上下文长度」(llm.contextWindow)
与「最大输出上限」(llm.maxTokens),跨 Provider/模型原样透传。

P0 正确性收口:
- 迁移 11/12(SCHEMA_VERSION 5):记忆表 embedding 列 + 分 Provider 窗口键清理
- 记忆生命周期接线:会话终态清理 working memory / episodic 90 天 TTL / access_count 回写
- 回放缓冲模块化 + 会话终态清理(杜绝 4MB/会话内存滞留)
- i18n 收口:主进程 main-locale(zh/en,ui.locale 热切换)+ 渲染层 17 处出层

P1 能力演进:
- 本地向量混合检索:0.6×向量余弦 + 0.4×TF-IDF,Ollama embeddings 首次投产,
  存量记忆惰性回填,嵌入不可用自动回退 TF-IDF
- MEMORY.md 维护闭环:固化去重消除截断盲区;两阶段维护(AI 建议 → 用户确认 →
  原子改写 + 语义记忆双轨同步 + 审计);>50KB 告警
- 可观测闭环:cacheTokens 引擎→前端透传(Token 面板命中率/成本行)+ 输入框
  上下文占用指示条
- MCP Prompts/Resources 对话可用:/mcp:{server}:{prompt} 与 @mcp:{server}:{uri}

P2 体验补全:
- 工具自定义策略(正则白/黑名单 + 频率 + 强制确认,热生效)
- 连续 ≥3 同类工具确认聚合为单弹框
- 会话消息游标分页(首屏 200 条向上翻页)
- 开机自启;Playwright + Electron E2E 冒烟(本地 mock LLM 零外联)

Review 回归修复:MCP 大小写失配 / 分页状态复位 / 清空=未配置语义(Number(null)=0
隐患)/ MEMORY.md 告警位置 / working_memories FK(迁移 13)/ 全局配置层废键清理;
附带根治权限加固启动时序、代理回环放行、safeStorage 降级、悬空 symlink 逃逸。

验证:typecheck/lint 0 问题;test:electron 2478/2478(0 跳过);E2E 2/2;
docs/v0.8.1-迭代实施清单.md 全项留档。
2026-09-08 09:35:58 +08:00

128 lines
4.1 KiB
TypeScript

/**
* 记忆工具(2 个)
*
* memory_store, memory_search
*
* @see docs/MetonaAI-Desktop 架构与交互设计.html — 9 个基础工具
*/
import type { IMetonaTool, ToolExecutionContext } from '../../types/metona-tool';
import type { MetonaToolDef } from '../../../harness/types';
import { MetonaToolCategory, MetonaRiskLevel } from '../../../harness/types';
import type { MemoryManager } from '../../memory/manager';
// ===== 7. memory_store =====
export class MemoryStoreTool implements IMetonaTool {
readonly definition: MetonaToolDef = {
name: 'memory_store',
description:
'Store a piece of information in persistent memory. Useful for remembering important facts, decisions, or user preferences across sessions.',
parameters: {
type: 'object',
properties: {
content: { type: 'string', description: 'The memory content to store' },
type: {
type: 'string',
description:
'Memory type: "episodic" (events), "semantic" (knowledge), or "working" (task state)',
enum: ['episodic', 'semantic', 'working'],
},
importance: { type: 'number', description: 'Importance score 0-1 (default 0.5)' },
source: {
type: 'string',
description: 'Source of the memory',
enum: ['user_input', 'tool_result', 'agent_thought', 'imported'],
},
},
required: ['content', 'type'],
},
category: MetonaToolCategory.DATABASE,
riskLevel: MetonaRiskLevel.MEDIUM,
requiresPermission: false,
timeoutMs: 10_000,
};
constructor(private memoryManager: MemoryManager) {}
async execute(args: Record<string, unknown>, context: ToolExecutionContext): Promise<unknown> {
const content = args.content as string;
const type = args.type as 'episodic' | 'semantic' | 'working';
const importance = (args.importance as number) ?? 0.5;
const source =
(args.source as 'user_input' | 'tool_result' | 'agent_thought' | 'imported') ??
'agent_thought';
// v0.3.0 修复: store() 是同步方法,移除多余的 await 避免误导维护者
const id = this.memoryManager.store({
type,
content,
source,
importance,
sessionId: context.sessionId,
});
return { id, type, content_preview: content.slice(0, 100), importance };
}
}
// ===== 8. memory_search =====
export class MemorySearchTool implements IMetonaTool {
readonly definition: MetonaToolDef = {
name: 'memory_search',
description:
'Search persistent memory for relevant information. Returns memories sorted by relevance.',
parameters: {
type: 'object',
properties: {
query: { type: 'string', description: 'Search query or keywords' },
type: {
type: 'string',
description: 'Filter by memory type',
enum: ['episodic', 'semantic', 'working'],
},
topK: { type: 'number', description: 'Number of results (default 5)' },
threshold: {
type: 'number',
description:
'Minimum importance score 0-1 (default 0.7). Filters memories by importance, not search relevance.',
},
},
required: ['query'],
},
category: MetonaToolCategory.DATABASE,
riskLevel: MetonaRiskLevel.SAFE,
requiresPermission: false,
timeoutMs: 10_000,
};
constructor(private memoryManager: MemoryManager) {}
async execute(args: Record<string, unknown>, _context: ToolExecutionContext): Promise<unknown> {
const query = args.query as string;
const type = args.type as 'episodic' | 'semantic' | 'working' | undefined;
const topK = (args.topK as number) ?? 5;
const threshold = (args.threshold as number) ?? 0.7;
// v0.8.1: search() 升级为 async(向量混合检索),查询向量异步生成
const results = await this.memoryManager.search(query, {
topK,
type,
minImportance: threshold,
});
return {
query,
results: results.map((r) => ({
id: r.id,
type: r.type,
content: r.content.slice(0, 500),
importance: r.importance,
score: r.score,
})),
count: results.length,
};
}
}