|
| 1 | +import type { ITool, ToolExecutionResult, ToolExecutionContext, JSONSchemaObject } from '../../core/tools/ITool.js'; |
| 2 | +import type { MarkdownWorkingMemory } from './MarkdownWorkingMemory.js'; |
| 3 | + |
| 4 | +interface UpdateInput { |
| 5 | + content: string; |
| 6 | +} |
| 7 | + |
| 8 | +interface UpdateOutput { |
| 9 | + tokensUsed: number; |
| 10 | + truncated: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +/** |
| 14 | + * Tool that lets the agent fully replace its persistent markdown working memory. |
| 15 | + * The agent should call this whenever it learns new persistent context about the |
| 16 | + * user, session, or ongoing tasks that should survive across conversations. |
| 17 | + */ |
| 18 | +export class UpdateWorkingMemoryTool implements ITool<UpdateInput, UpdateOutput> { |
| 19 | + readonly id = 'update-working-memory-v1'; |
| 20 | + readonly name = 'update_working_memory'; |
| 21 | + readonly displayName = 'Update Working Memory'; |
| 22 | + readonly description = |
| 23 | + 'Replace your persistent working memory with updated content. ' + |
| 24 | + 'Use this to store user preferences, ongoing context, project notes, ' + |
| 25 | + 'and anything that should persist across conversations. ' + |
| 26 | + 'You must provide the COMPLETE updated content (full replacement, not a patch).'; |
| 27 | + readonly category = 'memory'; |
| 28 | + readonly hasSideEffects = true; |
| 29 | + readonly inputSchema: JSONSchemaObject = { |
| 30 | + type: 'object', |
| 31 | + properties: { |
| 32 | + content: { |
| 33 | + type: 'string', |
| 34 | + description: 'The full markdown content to replace working memory with.', |
| 35 | + }, |
| 36 | + }, |
| 37 | + required: ['content'], |
| 38 | + }; |
| 39 | + |
| 40 | + constructor(private readonly memory: MarkdownWorkingMemory) {} |
| 41 | + |
| 42 | + async execute(args: UpdateInput, _context: ToolExecutionContext): Promise<ToolExecutionResult<UpdateOutput>> { |
| 43 | + const result = this.memory.write(args.content); |
| 44 | + if (!result.success) { |
| 45 | + return { success: false, error: result.error ?? 'Failed to write working memory' }; |
| 46 | + } |
| 47 | + return { |
| 48 | + success: true, |
| 49 | + output: { tokensUsed: result.tokensUsed, truncated: result.truncated }, |
| 50 | + }; |
| 51 | + } |
| 52 | +} |
0 commit comments