Files
metona-ai-desktop/electron/harness/tools/built-in/memory.ts
T
thzxx 6f08759f63 v0.1.2: 全项目代码审查修复 + 子代理编排器 + 上下文压缩 + 并行工具执行
后端修复: Ollama adapter 移除死代码/修复超时硬编码/iteration硬编码/pullModel无超时/流异常断开补发DONE; SSE解析器支持非字符串arguments; Sandbox fail-closed安全加固; IPC移除未使用变量

新增功能: TaskOrchestrator子任务编排器; DelegateTaskTool委派工具; Agent Loop并行工具执行; 上下文自动压缩; 记忆检索注入System Prompt

前端修复: useAgentStream text_delta thought累积bug; 工具调用状态正确流转; 修复重复stateChange事件; TraceStep.thought正确填充
2026-07-06 22:48:33 +08:00

105 lines
3.8 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';
const id = await 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;
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,
};
}
}