1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
from datetime import UTC, datetime
from uuid import UUID
from personal_agent.memory.models import LongTermMemory, MemoryCategory, WorkingMemory
from personal_agent.memory.store import LongTermMemoryStore
from personal_agent.tools.base import Tool, ToolError, ToolResult
class TaskStartTool(Tool):
def __init__(self, memory: WorkingMemory) -> None:
self.memory = memory
@property
def name(self) -> str:
return "task_start"
@property
def description(self) -> str:
return (
"Record the user's ongoing project or goal as the current task. "
"Use this when the user intends to work on something across multiple "
"turns or over a period of time. Do not use it for one-off requests "
"or temporary side conversations."
)
@property
def schema(self) -> dict[str, object]:
return {
"type": "object",
"properties": {
"objective": {
"type": "string",
"description": (
"A concise description of the durable goal the user wants to pursue. "
"Describe what they are trying to accomplish, not mutable implementation "
"details, preferences, or constraints. For example: "
"'Build an image tagging API' or "
"'Design and implement a Rust CLI that converts directory trees into "
"static HTML.'"
),
},
},
"required": ["objective"],
}
def execute(self, arguments: dict[str, object]) -> ToolResult:
objective = arguments.get("objective")
if not isinstance(objective, str):
raise ToolError("objective must be a string")
if not objective.strip():
raise ToolError("objective must not be empty")
self.memory.start_task(objective)
return ToolResult(content=f"Started task: {objective}")
class MemoryAddFactTool(Tool):
def __init__(self, memory: WorkingMemory) -> None:
self.memory = memory
@property
def name(self) -> str:
return "working_memory_add_fact"
@property
def description(self) -> str:
return (
"Add a fact to the current task's working memory. Working memory is "
"temporary and is used to track information needed during the current "
"task or conversation. Do not use this for durable user preferences "
"or facts that should survive the conversation."
)
@property
def schema(self) -> dict[str, object]:
return {
"type": "object",
"properties": {
"fact": {
"type": "string",
"description": "A fact worth remembering during the current task.",
},
},
"required": ["fact"],
}
def execute(self, arguments: dict[str, object]) -> ToolResult:
fact = arguments.get("fact")
if not isinstance(fact, str):
raise ToolError("fact must be a string")
self.memory.add_fact(fact)
return ToolResult(content=f"Added working-memory fact: {fact}")
class MemoryAddNoteTool(Tool):
def __init__(self, memory: WorkingMemory) -> None:
self.memory = memory
@property
def name(self) -> str:
return "working_memory_add_note"
@property
def description(self) -> str:
return (
"Add a temporary note to the current task's working memory. Use this "
"for information that may help with the current task or conversation. "
"Do not use this for durable information that should survive the "
"conversation."
)
@property
def schema(self) -> dict[str, object]:
return {
"type": "object",
"properties": {
"note": {
"type": "string",
"description": "A useful observation, reminder, or note for the current task.",
},
},
"required": ["note"],
}
def execute(self, arguments: dict[str, object]) -> ToolResult:
note = arguments.get("note")
if not isinstance(note, str):
raise ToolError("note must be a string")
self.memory.add_note(note)
return ToolResult(content=f"Added working-memory note: {note}")
class MemoryRemoveFactTool(Tool):
def __init__(self, memory: WorkingMemory) -> None:
self.memory = memory
@property
def name(self) -> str:
return "working_memory_remove_fact"
@property
def description(self) -> str:
return (
"Remove a fact from the current task's working memory. This only "
"changes temporary working memory; it does not delete persistent "
"long-term memory."
)
@property
def schema(self) -> dict[str, object]:
return {
"type": "object",
"properties": {
"fact": {
"type": "string",
"description": "The exact fact to remove from working memory.",
},
},
"required": ["fact"],
}
def execute(self, arguments: dict[str, object]) -> ToolResult:
fact = arguments.get("fact")
if not isinstance(fact, str):
raise ToolError("fact must be a string")
self.memory.remove_fact(fact)
return ToolResult(content=f"Removed fact: {fact}")
class MemoryRemoveNoteTool(Tool):
def __init__(self, memory: WorkingMemory) -> None:
self.memory = memory
@property
def name(self) -> str:
return "working_memory_remove_note"
@property
def description(self) -> str:
return (
"Remove a note from the current task's working memory. This only "
"changes temporary working memory; it does not delete persistent "
"long-term memory."
)
@property
def schema(self) -> dict[str, object]:
return {
"type": "object",
"properties": {
"note": {
"type": "string",
"description": "The exact note to remove from working memory.",
},
},
"required": ["note"],
}
def execute(self, arguments: dict[str, object]) -> ToolResult:
note = arguments.get("note")
if not isinstance(note, str):
raise ToolError("note must be a string")
self.memory.remove_note(note)
return ToolResult(content=f"Removed note: {note}")
class LongTermMemorySaveTool(Tool):
def __init__(self, store: LongTermMemoryStore) -> None:
self.store = store
@property
def name(self) -> str:
return "long_term_memory_save"
@property
def description(self) -> str:
return (
"Save persistent information for use in future conversations. Use "
"this for durable user preferences, facts, decisions, goals, "
"relationships, or meaningful past experiences. This is long-term "
"memory, not temporary working memory."
)
@property
def schema(self) -> dict[str, object]:
return {
"type": "object",
"properties": {
"content": {
"type": "string",
"description": "The durable information to remember.",
},
"category": {
"type": "string",
"enum": ["semantic", "episodic"],
"description": (
"semantic for stable facts/preferences; "
"episodic for meaningful past events."
),
},
"importance": {
"type": "integer",
"enum": [1, 2, 3],
"description": (
"How useful this memory is likely to be in future "
"conversations: 1 = useful but minor, "
"2 = normally useful, "
"3 = especially important for future behavior. "
"Importance measure future usefullness, not emotional intensity."
),
},
},
"required": ["content", "category", "importance"],
}
def execute(self, arguments: dict[str, object]) -> ToolResult:
content = arguments.get("content")
category = arguments.get("category")
importance = arguments.get("importance")
if not isinstance(content, str):
raise ToolError("content must be a string")
if not content.strip():
raise ToolError("content must not be empty")
if not isinstance(category, str):
raise ToolError("category must be a string")
if not isinstance(importance, int) or isinstance(importance, bool):
raise ToolError("importance must be an integer")
if importance not in (1, 2, 3):
raise ToolError("importance must be between 1 and 3")
try:
memory_category = MemoryCategory(category)
except ValueError:
raise ToolError("category must be 'semantic' or 'episodic'")
memory = LongTermMemory.create(content, memory_category, importance)
self.store.add(memory)
return ToolResult(content=f"Saved memory: {memory.id}")
class LongTermMemorySearchTool(Tool):
def __init__(self, store: LongTermMemoryStore) -> None:
self.store = store
@property
def name(self) -> str:
return "long_term_memory_search"
@property
def description(self) -> str:
return (
"Search persistent long-term memories from previous conversations. "
"Use this when information from past conversations may be relevant, "
"when the user asks what you remember, or when checking whether a "
"durable fact or preference is already stored. This does not search "
"working memory."
)
@property
def schema(self) -> dict[str, object]:
return {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "The information to search for.",
},
"category": {
"type": "string",
"enum": ["semantic", "episodic"],
"description": "Optionally restrict results to one category.",
},
"limit": {
"type": "integer",
"minimum": 1,
"description": "Maximum number of memories to return.",
},
},
"required": ["query"],
}
def execute(self, arguments: dict[str, object]) -> ToolResult:
query = arguments.get("query")
category = arguments.get("category")
limit = arguments.get("limit", 10)
if not isinstance(query, str):
raise ToolError("query must be a string")
if not query.strip():
raise ToolError("query must not be empty")
if category is not None:
if not isinstance(category, str):
raise ToolError("category must be a string")
try:
memory_category = MemoryCategory(category)
except ValueError:
raise ToolError("category must be 'semantic' or 'episodic'")
else:
memory_category = None
if not isinstance(limit, int) or isinstance(limit, bool):
raise ToolError("limit must be an integer")
if limit <= 0:
raise ToolError("limit must be positive")
memories = self.store.search(
query=query,
category=memory_category,
limit=limit,
)
if not memories:
return ToolResult(content="No matching memories found.")
now = datetime.now(UTC)
lines = [
(
f"- [{memory.category.value}] {memory.id}\n"
f" content: {memory.content}\n"
f" created_at: {memory.created_at.isoformat()}\n"
f" importance: {memory.importance}"
f" stale: {(now - memory.created_at).days >= 365}"
)
for memory in memories
]
return ToolResult(content="\n".join(lines))
class LongTermMemoryDeleteTool(Tool):
def __init__(self, store: LongTermMemoryStore) -> None:
self.store = store
@property
def name(self) -> str:
return "long_term_memory_delete"
@property
def description(self) -> str:
return (
"Delete a specific persistent long-term memory by its ID. This "
"deletes long-term memory, not working memory. When replacing or "
"forgetting an existing memory, search for it first to obtain its ID."
)
@property
def schema(self) -> dict[str, object]:
return {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "The exact UUID of the memory to delete.",
},
},
"required": ["id"],
}
def execute(self, arguments: dict[str, object]) -> ToolResult:
memory_id = arguments.get("id")
if not isinstance(memory_id, str):
raise ToolError("id must be a string")
try:
parsed_id = UUID(memory_id)
except ValueError:
raise ToolError("id must be a valid UUID")
self.store.delete(parsed_id)
return ToolResult(content=f"Deleted memory: {parsed_id}")
|