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
|
"""Memory tools: the model's own read and write access to the store.
``memory_save`` is how the model records something it learned. ``memory_search``
is how it looks for something it might already know. Both are deliberately
plain: the model hands over text, the store does the ranking, and the result
comes back as text the model can read and act on.
"""
from __future__ import annotations
from typing import Any, Dict
from ..errors import ToolError
from ..memory import (
SCOPE_GLOBAL,
SCOPE_PROJECT,
VALID_SCOPES,
MemoryStore,
)
from .base import BaseTool
class MemorySaveTool(BaseTool):
"""Write one thing the model learned into the store."""
def __init__(self, store: MemoryStore, project: str = "") -> None:
self.store = store
self.project = project
@property
def name(self) -> str:
return "memory_save"
@property
def description(self) -> str:
return (
"Save something you learned so you can recall it later. Use it for "
"anything non-obvious: a gotcha, a fact about this project, a "
"convention, a command that works, a URL, a correction the user "
"gave you. One fact per call. Be specific enough that the fact is "
"useful months from now."
)
@property
def parameters(self) -> Dict[str, Any]:
return {
"type": "object",
"properties": {
"body": {
"type": "string",
"description": "The fact to remember, in one or two sentences.",
},
"scope": {
"type": "string",
"enum": list(VALID_SCOPES),
"description": (
"'project' (default) keeps it visible only in this "
"workspace. 'global' makes it visible everywhere — use "
"that only for lessons that are genuinely general."
),
},
"tags": {
"type": "string",
"description": "Optional comma-separated keywords, e.g. 'go,channels'.",
},
},
"required": ["body"],
}
async def execute(
self,
body: str,
scope: str = SCOPE_PROJECT,
tags: str = "",
**kwargs: Any,
) -> str:
if body is None or not str(body).strip():
raise ToolError("'body' is required and must be non-empty.")
if scope not in VALID_SCOPES:
raise ToolError(
f"Scope must be one of {', '.join(VALID_SCOPES)}, got {scope!r}."
)
if scope == SCOPE_GLOBAL:
project = None
else:
project = self.project or None
memory = self.store.save(
str(body), scope=scope, project=project, source="model", tags=str(tags)
)
preview = memory.body if len(memory.body) <= 80 else memory.body[:80] + "..."
return f"Saved memory {memory.id} ({memory.scope}): {preview}"
class MemorySearchTool(BaseTool):
"""Look for something the model might already know."""
def __init__(self, store: MemoryStore, project: str = "") -> None:
self.store = store
self.project = project
@property
def name(self) -> str:
return "memory_search"
@property
def description(self) -> str:
return (
"Search what you have learned before. Use this before answering "
"something you might already know, and whenever a question mentions "
"a project, a command, or a technology you have seen before."
)
@property
def parameters(self) -> Dict[str, Any]:
return {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": (
"Keywords describing what you are looking for. A few "
"specific words work better than a whole sentence."
),
},
"limit": {
"type": "integer",
"description": "Maximum memories to return. Defaults to 5.",
},
},
"required": ["query"],
}
async def execute(self, query: str, limit: int = 5, **kwargs: Any) -> str:
if not query or not str(query).strip():
return "No memories matched."
try:
count = max(1, min(int(limit or 5), 20))
except (TypeError, ValueError):
count = 5
hits = self.store.search(str(query), project=self.project or None, limit=count)
if not hits:
return "No memories matched."
lines = [f"{len(hits)} remembered:"]
for index, (memory, _score) in enumerate(hits, 1):
scope = memory.scope if memory.scope == SCOPE_GLOBAL else "project"
lines.append(f"{index}. [{scope}] {memory.body}")
return "\n".join(lines)
__all__ = ["MemorySaveTool", "MemorySearchTool"]
|