Skip to content

Commit 3fe1840

Browse files
committed
feat(memory): add update_working_memory and read_working_memory tools
1 parent 0ed30c4 commit 3fe1840

2 files changed

Lines changed: 91 additions & 0 deletions

File tree

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import type { ITool, ToolExecutionResult, ToolExecutionContext, JSONSchemaObject } from '../../core/tools/ITool.js';
2+
import type { MarkdownWorkingMemory } from './MarkdownWorkingMemory.js';
3+
4+
interface ReadOutput {
5+
content: string;
6+
tokensUsed: number;
7+
}
8+
9+
/**
10+
* Tool that lets the agent explicitly read its persistent working memory.
11+
* The memory is also injected into the system prompt automatically,
12+
* but this tool is useful when the agent wants to reason about its
13+
* memory before deciding what to update.
14+
*/
15+
export class ReadWorkingMemoryTool implements ITool<Record<string, never>, ReadOutput> {
16+
readonly id = 'read-working-memory-v1';
17+
readonly name = 'read_working_memory';
18+
readonly displayName = 'Read Working Memory';
19+
readonly description =
20+
'Read your current persistent working memory contents. ' +
21+
'This is also available in your system prompt, but use this tool ' +
22+
'when you need to inspect your memory before updating it.';
23+
readonly category = 'memory';
24+
readonly hasSideEffects = false;
25+
readonly inputSchema: JSONSchemaObject = {
26+
type: 'object',
27+
properties: {},
28+
};
29+
30+
constructor(private readonly memory: MarkdownWorkingMemory) {}
31+
32+
async execute(_args: Record<string, never>, _context: ToolExecutionContext): Promise<ToolExecutionResult<ReadOutput>> {
33+
const content = this.memory.read();
34+
return {
35+
success: true,
36+
output: { content, tokensUsed: this.memory.estimateTokens() },
37+
};
38+
}
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)