deepagents 0.0.11rc1__py3-none-any.whl → 0.0.12rc2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- deepagents/__init__.py +7 -5
- deepagents/graph.py +112 -114
- deepagents/middleware/__init__.py +6 -0
- deepagents/middleware/filesystem.py +1125 -0
- deepagents/middleware/subagents.py +481 -0
- {deepagents-0.0.11rc1.dist-info → deepagents-0.0.12rc2.dist-info}/METADATA +13 -12
- deepagents-0.0.12rc2.dist-info/RECORD +10 -0
- {deepagents-0.0.11rc1.dist-info → deepagents-0.0.12rc2.dist-info}/top_level.txt +0 -1
- deepagents/middleware.py +0 -198
- deepagents/model.py +0 -5
- deepagents/prompts.py +0 -423
- deepagents/state.py +0 -33
- deepagents/tools.py +0 -201
- deepagents/types.py +0 -21
- deepagents-0.0.11rc1.dist-info/RECORD +0 -17
- tests/test_deepagents.py +0 -136
- tests/test_hitl.py +0 -51
- tests/test_middleware.py +0 -57
- tests/utils.py +0 -81
- {deepagents-0.0.11rc1.dist-info → deepagents-0.0.12rc2.dist-info}/WHEEL +0 -0
- {deepagents-0.0.11rc1.dist-info → deepagents-0.0.12rc2.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,1125 @@
|
|
|
1
|
+
"""Middleware for providing filesystem tools to an agent."""
|
|
2
|
+
# ruff: noqa: E501
|
|
3
|
+
|
|
4
|
+
from collections.abc import Awaitable, Callable, Sequence
|
|
5
|
+
from typing import TYPE_CHECKING, Annotated, Any, NotRequired
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from langgraph.runtime import Runtime
|
|
9
|
+
|
|
10
|
+
import os
|
|
11
|
+
from datetime import UTC, datetime
|
|
12
|
+
from typing import TYPE_CHECKING, Literal
|
|
13
|
+
|
|
14
|
+
from langchain.agents.middleware.types import (
|
|
15
|
+
AgentMiddleware,
|
|
16
|
+
AgentState,
|
|
17
|
+
ModelRequest,
|
|
18
|
+
ModelResponse,
|
|
19
|
+
)
|
|
20
|
+
from langchain.tools.tool_node import InjectedState, ToolCallRequest
|
|
21
|
+
from langchain_core.messages import ToolMessage
|
|
22
|
+
from langchain_core.tools import BaseTool, InjectedToolCallId, tool
|
|
23
|
+
from langgraph.config import get_config
|
|
24
|
+
from langgraph.runtime import Runtime, get_runtime
|
|
25
|
+
from langgraph.store.base import BaseStore, Item
|
|
26
|
+
from langgraph.types import Command
|
|
27
|
+
from typing_extensions import TypedDict
|
|
28
|
+
|
|
29
|
+
MEMORIES_PREFIX = "/memories/"
|
|
30
|
+
EMPTY_CONTENT_WARNING = "System reminder: File exists but has empty contents"
|
|
31
|
+
MAX_LINE_LENGTH = 2000
|
|
32
|
+
LINE_NUMBER_WIDTH = 6
|
|
33
|
+
DEFAULT_READ_OFFSET = 0
|
|
34
|
+
DEFAULT_READ_LIMIT = 2000
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class FileData(TypedDict):
|
|
38
|
+
"""Data structure for storing file contents with metadata."""
|
|
39
|
+
|
|
40
|
+
content: list[str]
|
|
41
|
+
"""Lines of the file."""
|
|
42
|
+
|
|
43
|
+
created_at: str
|
|
44
|
+
"""ISO 8601 timestamp of file creation."""
|
|
45
|
+
|
|
46
|
+
modified_at: str
|
|
47
|
+
"""ISO 8601 timestamp of last modification."""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _file_data_reducer(left: dict[str, FileData] | None, right: dict[str, FileData | None]) -> dict[str, FileData]:
|
|
51
|
+
"""Merge file updates with support for deletions.
|
|
52
|
+
|
|
53
|
+
This reducer enables file deletion by treating `None` values in the right
|
|
54
|
+
dictionary as deletion markers. It's designed to work with LangGraph's
|
|
55
|
+
state management where annotated reducers control how state updates merge.
|
|
56
|
+
|
|
57
|
+
Args:
|
|
58
|
+
left: Existing files dictionary. May be `None` during initialization.
|
|
59
|
+
right: New files dictionary to merge. Files with `None` values are
|
|
60
|
+
treated as deletion markers and removed from the result.
|
|
61
|
+
|
|
62
|
+
Returns:
|
|
63
|
+
Merged dictionary where right overwrites left for matching keys,
|
|
64
|
+
and `None` values in right trigger deletions.
|
|
65
|
+
|
|
66
|
+
Example:
|
|
67
|
+
```python
|
|
68
|
+
existing = {"/file1.txt": FileData(...), "/file2.txt": FileData(...)}
|
|
69
|
+
updates = {"/file2.txt": None, "/file3.txt": FileData(...)}
|
|
70
|
+
result = file_data_reducer(existing, updates)
|
|
71
|
+
# Result: {"/file1.txt": FileData(...), "/file3.txt": FileData(...)}
|
|
72
|
+
```
|
|
73
|
+
"""
|
|
74
|
+
if left is None:
|
|
75
|
+
# Filter out None values when initializing
|
|
76
|
+
return {k: v for k, v in right.items() if v is not None}
|
|
77
|
+
|
|
78
|
+
# Merge, filtering out None values (deletions)
|
|
79
|
+
result = {**left}
|
|
80
|
+
for key, value in right.items():
|
|
81
|
+
if value is None:
|
|
82
|
+
result.pop(key, None)
|
|
83
|
+
else:
|
|
84
|
+
result[key] = value
|
|
85
|
+
return result
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _validate_path(path: str, *, allowed_prefixes: Sequence[str] | None = None) -> str:
|
|
89
|
+
"""Validate and normalize file path for security.
|
|
90
|
+
|
|
91
|
+
Ensures paths are safe to use by preventing directory traversal attacks
|
|
92
|
+
and enforcing consistent formatting. All paths are normalized to use
|
|
93
|
+
forward slashes and start with a leading slash.
|
|
94
|
+
|
|
95
|
+
Args:
|
|
96
|
+
path: The path to validate and normalize.
|
|
97
|
+
allowed_prefixes: Optional list of allowed path prefixes. If provided,
|
|
98
|
+
the normalized path must start with one of these prefixes.
|
|
99
|
+
|
|
100
|
+
Returns:
|
|
101
|
+
Normalized canonical path starting with `/` and using forward slashes.
|
|
102
|
+
|
|
103
|
+
Raises:
|
|
104
|
+
ValueError: If path contains traversal sequences (`..` or `~`) or does
|
|
105
|
+
not start with an allowed prefix when `allowed_prefixes` is specified.
|
|
106
|
+
|
|
107
|
+
Example:
|
|
108
|
+
```python
|
|
109
|
+
validate_path("foo/bar") # Returns: "/foo/bar"
|
|
110
|
+
validate_path("/./foo//bar") # Returns: "/foo/bar"
|
|
111
|
+
validate_path("../etc/passwd") # Raises ValueError
|
|
112
|
+
validate_path("/data/file.txt", allowed_prefixes=["/data/"]) # OK
|
|
113
|
+
validate_path("/etc/file.txt", allowed_prefixes=["/data/"]) # Raises ValueError
|
|
114
|
+
```
|
|
115
|
+
"""
|
|
116
|
+
# Reject paths with traversal attempts
|
|
117
|
+
if ".." in path or path.startswith("~"):
|
|
118
|
+
msg = f"Path traversal not allowed: {path}"
|
|
119
|
+
raise ValueError(msg)
|
|
120
|
+
|
|
121
|
+
# Normalize path (resolve ., //, etc.)
|
|
122
|
+
normalized = os.path.normpath(path)
|
|
123
|
+
|
|
124
|
+
# Convert to forward slashes for consistency
|
|
125
|
+
normalized = normalized.replace("\\", "/")
|
|
126
|
+
|
|
127
|
+
# Ensure path starts with /
|
|
128
|
+
if not normalized.startswith("/"):
|
|
129
|
+
normalized = f"/{normalized}"
|
|
130
|
+
|
|
131
|
+
# Check allowed prefixes if specified
|
|
132
|
+
if allowed_prefixes is not None and not any(normalized.startswith(prefix) for prefix in allowed_prefixes):
|
|
133
|
+
msg = f"Path must start with one of {allowed_prefixes}: {path}"
|
|
134
|
+
raise ValueError(msg)
|
|
135
|
+
|
|
136
|
+
return normalized
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _format_content_with_line_numbers(
|
|
140
|
+
content: str | list[str],
|
|
141
|
+
*,
|
|
142
|
+
format_style: Literal["pipe", "tab"] = "pipe",
|
|
143
|
+
start_line: int = 1,
|
|
144
|
+
) -> str:
|
|
145
|
+
r"""Format file content with line numbers for display.
|
|
146
|
+
|
|
147
|
+
Converts file content to a numbered format similar to `cat -n` output,
|
|
148
|
+
with support for two different formatting styles.
|
|
149
|
+
|
|
150
|
+
Args:
|
|
151
|
+
content: File content as a string or list of lines.
|
|
152
|
+
format_style: Format style for line numbers:
|
|
153
|
+
- `"pipe"`: Compact format like `"1|content"`
|
|
154
|
+
- `"tab"`: Right-aligned format like `" 1\tcontent"` (lines truncated at 2000 chars)
|
|
155
|
+
start_line: Starting line number (default: 1).
|
|
156
|
+
|
|
157
|
+
Returns:
|
|
158
|
+
Formatted content with line numbers prepended to each line.
|
|
159
|
+
|
|
160
|
+
Example:
|
|
161
|
+
```python
|
|
162
|
+
content = "Hello\nWorld"
|
|
163
|
+
format_content_with_line_numbers(content, format_style="pipe")
|
|
164
|
+
# Returns: "1|Hello\n2|World"
|
|
165
|
+
|
|
166
|
+
format_content_with_line_numbers(content, format_style="tab", start_line=10)
|
|
167
|
+
# Returns: " 10\tHello\n 11\tWorld"
|
|
168
|
+
```
|
|
169
|
+
"""
|
|
170
|
+
if isinstance(content, str):
|
|
171
|
+
lines = content.split("\n")
|
|
172
|
+
# Remove trailing empty line from split
|
|
173
|
+
if lines and lines[-1] == "":
|
|
174
|
+
lines = lines[:-1]
|
|
175
|
+
else:
|
|
176
|
+
lines = content
|
|
177
|
+
|
|
178
|
+
if format_style == "pipe":
|
|
179
|
+
return "\n".join(f"{i + start_line}|{line}" for i, line in enumerate(lines))
|
|
180
|
+
|
|
181
|
+
# Tab format with defined width and line truncation
|
|
182
|
+
return "\n".join(f"{i + start_line:{LINE_NUMBER_WIDTH}d}\t{line[:MAX_LINE_LENGTH]}" for i, line in enumerate(lines))
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def _create_file_data(
|
|
186
|
+
content: str | list[str],
|
|
187
|
+
*,
|
|
188
|
+
created_at: str | None = None,
|
|
189
|
+
) -> FileData:
|
|
190
|
+
r"""Create a FileData object with automatic timestamp generation.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
content: File content as a string or list of lines.
|
|
194
|
+
created_at: Optional creation timestamp in ISO 8601 format.
|
|
195
|
+
If `None`, uses the current UTC time.
|
|
196
|
+
|
|
197
|
+
Returns:
|
|
198
|
+
FileData object with content and timestamps.
|
|
199
|
+
|
|
200
|
+
Example:
|
|
201
|
+
```python
|
|
202
|
+
file_data = create_file_data("Hello\nWorld")
|
|
203
|
+
# Returns: {"content": ["Hello", "World"], "created_at": "2024-...",
|
|
204
|
+
# "modified_at": "2024-..."}
|
|
205
|
+
```
|
|
206
|
+
"""
|
|
207
|
+
lines = content.split("\n") if isinstance(content, str) else content
|
|
208
|
+
now = datetime.now(UTC).isoformat()
|
|
209
|
+
|
|
210
|
+
return {
|
|
211
|
+
"content": lines,
|
|
212
|
+
"created_at": created_at or now,
|
|
213
|
+
"modified_at": now,
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def _update_file_data(
|
|
218
|
+
file_data: FileData,
|
|
219
|
+
content: str | list[str],
|
|
220
|
+
) -> FileData:
|
|
221
|
+
"""Update FileData with new content while preserving creation timestamp.
|
|
222
|
+
|
|
223
|
+
Args:
|
|
224
|
+
file_data: Existing FileData object to update.
|
|
225
|
+
content: New file content as a string or list of lines.
|
|
226
|
+
|
|
227
|
+
Returns:
|
|
228
|
+
Updated FileData object with new content and updated `modified_at`
|
|
229
|
+
timestamp. The `created_at` timestamp is preserved from the original.
|
|
230
|
+
|
|
231
|
+
Example:
|
|
232
|
+
```python
|
|
233
|
+
original = create_file_data("Hello")
|
|
234
|
+
updated = update_file_data(original, "Hello World")
|
|
235
|
+
# updated["created_at"] == original["created_at"]
|
|
236
|
+
# updated["modified_at"] > original["modified_at"]
|
|
237
|
+
```
|
|
238
|
+
"""
|
|
239
|
+
lines = content.split("\n") if isinstance(content, str) else content
|
|
240
|
+
now = datetime.now(UTC).isoformat()
|
|
241
|
+
|
|
242
|
+
return {
|
|
243
|
+
"content": lines,
|
|
244
|
+
"created_at": file_data["created_at"],
|
|
245
|
+
"modified_at": now,
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
|
|
249
|
+
def _file_data_to_string(file_data: FileData) -> str:
|
|
250
|
+
r"""Convert FileData to plain string content.
|
|
251
|
+
|
|
252
|
+
Joins the lines stored in FileData with newline characters to produce
|
|
253
|
+
a single string representation of the file content.
|
|
254
|
+
|
|
255
|
+
Args:
|
|
256
|
+
file_data: FileData object containing lines of content.
|
|
257
|
+
|
|
258
|
+
Returns:
|
|
259
|
+
File content as a single string with lines joined by newlines.
|
|
260
|
+
|
|
261
|
+
Example:
|
|
262
|
+
```python
|
|
263
|
+
file_data = {
|
|
264
|
+
"content": ["Hello", "World"],
|
|
265
|
+
"created_at": "...",
|
|
266
|
+
"modified_at": "...",
|
|
267
|
+
}
|
|
268
|
+
file_data_to_string(file_data) # Returns: "Hello\nWorld"
|
|
269
|
+
```
|
|
270
|
+
"""
|
|
271
|
+
return "\n".join(file_data["content"])
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
def _check_empty_content(content: str) -> str | None:
|
|
275
|
+
"""Check if file content is empty and return a warning message.
|
|
276
|
+
|
|
277
|
+
Args:
|
|
278
|
+
content: File content to check.
|
|
279
|
+
|
|
280
|
+
Returns:
|
|
281
|
+
Warning message string if content is empty or contains only whitespace,
|
|
282
|
+
`None` otherwise.
|
|
283
|
+
|
|
284
|
+
Example:
|
|
285
|
+
```python
|
|
286
|
+
check_empty_content("") # Returns: "System reminder: File exists but has empty contents"
|
|
287
|
+
check_empty_content(" ") # Returns: "System reminder: File exists but has empty contents"
|
|
288
|
+
check_empty_content("Hello") # Returns: None
|
|
289
|
+
```
|
|
290
|
+
"""
|
|
291
|
+
if not content or content.strip() == "":
|
|
292
|
+
return EMPTY_CONTENT_WARNING
|
|
293
|
+
return None
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _has_memories_prefix(file_path: str) -> bool:
|
|
297
|
+
"""Check if a file path is in the longterm memory filesystem.
|
|
298
|
+
|
|
299
|
+
Longterm memory files are distinguished by the `/memories/` path prefix.
|
|
300
|
+
|
|
301
|
+
Args:
|
|
302
|
+
file_path: File path to check.
|
|
303
|
+
|
|
304
|
+
Returns:
|
|
305
|
+
`True` if the file path starts with `/memories/`, `False` otherwise.
|
|
306
|
+
|
|
307
|
+
Example:
|
|
308
|
+
```python
|
|
309
|
+
has_memories_prefix("/memories/notes.txt") # Returns: True
|
|
310
|
+
has_memories_prefix("/temp/file.txt") # Returns: False
|
|
311
|
+
```
|
|
312
|
+
"""
|
|
313
|
+
return file_path.startswith(MEMORIES_PREFIX)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def _append_memories_prefix(file_path: str) -> str:
|
|
317
|
+
"""Add the longterm memory prefix to a file path.
|
|
318
|
+
|
|
319
|
+
Args:
|
|
320
|
+
file_path: File path to prefix.
|
|
321
|
+
|
|
322
|
+
Returns:
|
|
323
|
+
File path with `/memories` prepended.
|
|
324
|
+
|
|
325
|
+
Example:
|
|
326
|
+
```python
|
|
327
|
+
append_memories_prefix("/notes.txt") # Returns: "/memories/notes.txt"
|
|
328
|
+
```
|
|
329
|
+
"""
|
|
330
|
+
return f"/memories{file_path}"
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def _strip_memories_prefix(file_path: str) -> str:
|
|
334
|
+
"""Remove the longterm memory prefix from a file path.
|
|
335
|
+
|
|
336
|
+
Args:
|
|
337
|
+
file_path: File path potentially containing the memories prefix.
|
|
338
|
+
|
|
339
|
+
Returns:
|
|
340
|
+
File path with `/memories` removed if present at the start.
|
|
341
|
+
|
|
342
|
+
Example:
|
|
343
|
+
```python
|
|
344
|
+
strip_memories_prefix("/memories/notes.txt") # Returns: "/notes.txt"
|
|
345
|
+
strip_memories_prefix("/notes.txt") # Returns: "/notes.txt"
|
|
346
|
+
```
|
|
347
|
+
"""
|
|
348
|
+
if file_path.startswith(MEMORIES_PREFIX):
|
|
349
|
+
return file_path[len(MEMORIES_PREFIX) - 1 :] # Keep the leading slash
|
|
350
|
+
return file_path
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
class FilesystemState(AgentState):
|
|
354
|
+
"""State for the filesystem middleware."""
|
|
355
|
+
|
|
356
|
+
files: Annotated[NotRequired[dict[str, FileData]], _file_data_reducer]
|
|
357
|
+
"""Files in the filesystem."""
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
LIST_FILES_TOOL_DESCRIPTION = """Lists all files in the filesystem, optionally filtering by directory.
|
|
361
|
+
|
|
362
|
+
Usage:
|
|
363
|
+
- The list_files tool will return a list of all files in the filesystem.
|
|
364
|
+
- You can optionally provide a path parameter to list files in a specific directory.
|
|
365
|
+
- This is very useful for exploring the file system and finding the right file to read or edit.
|
|
366
|
+
- You should almost ALWAYS use this tool before using the Read or Edit tools."""
|
|
367
|
+
LIST_FILES_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT = f"\n- Files from the longterm filesystem will be prefixed with the {MEMORIES_PREFIX} path."
|
|
368
|
+
|
|
369
|
+
READ_FILE_TOOL_DESCRIPTION = """Reads a file from the filesystem. You can access any file directly by using this tool.
|
|
370
|
+
Assume this tool is able to read all files on the machine. If the User provides a path to a file assume that path is valid. It is okay to read a file that does not exist; an error will be returned.
|
|
371
|
+
|
|
372
|
+
Usage:
|
|
373
|
+
- The file_path parameter must be an absolute path, not a relative path
|
|
374
|
+
- By default, it reads up to 2000 lines starting from the beginning of the file
|
|
375
|
+
- You can optionally specify a line offset and limit (especially handy for long files), but it's recommended to read the whole file by not providing these parameters
|
|
376
|
+
- Any lines longer than 2000 characters will be truncated
|
|
377
|
+
- Results are returned using cat -n format, with line numbers starting at 1
|
|
378
|
+
- You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful.
|
|
379
|
+
- If you read a file that exists but has empty contents you will receive a system reminder warning in place of file contents.
|
|
380
|
+
- You should ALWAYS make sure a file has been read before editing it."""
|
|
381
|
+
READ_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT = f"\n- file_paths prefixed with the {MEMORIES_PREFIX} path will be read from the longterm filesystem."
|
|
382
|
+
|
|
383
|
+
EDIT_FILE_TOOL_DESCRIPTION = """Performs exact string replacements in files.
|
|
384
|
+
|
|
385
|
+
Usage:
|
|
386
|
+
- You must use your `Read` tool at least once in the conversation before editing. This tool will error if you attempt an edit without reading the file.
|
|
387
|
+
- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the old_string or new_string.
|
|
388
|
+
- ALWAYS prefer editing existing files. NEVER write new files unless explicitly required.
|
|
389
|
+
- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.
|
|
390
|
+
- The edit will FAIL if `old_string` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use `replace_all` to change every instance of `old_string`.
|
|
391
|
+
- Use `replace_all` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance."""
|
|
392
|
+
EDIT_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT = (
|
|
393
|
+
f"\n- You can edit files in the longterm filesystem by prefixing the filename with the {MEMORIES_PREFIX} path."
|
|
394
|
+
)
|
|
395
|
+
|
|
396
|
+
WRITE_FILE_TOOL_DESCRIPTION = """Writes to a new file in the filesystem.
|
|
397
|
+
|
|
398
|
+
Usage:
|
|
399
|
+
- The file_path parameter must be an absolute path, not a relative path
|
|
400
|
+
- The content parameter must be a string
|
|
401
|
+
- The write_file tool will create the a new file.
|
|
402
|
+
- Prefer to edit existing files over creating new ones when possible.
|
|
403
|
+
- file_paths prefixed with the /memories/ path will be written to the longterm filesystem."""
|
|
404
|
+
WRITE_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT = (
|
|
405
|
+
f"\n- file_paths prefixed with the {MEMORIES_PREFIX} path will be written to the longterm filesystem."
|
|
406
|
+
)
|
|
407
|
+
|
|
408
|
+
FILESYSTEM_SYSTEM_PROMPT = """## Filesystem Tools `ls`, `read_file`, `write_file`, `edit_file`
|
|
409
|
+
|
|
410
|
+
You have access to a filesystem which you can interact with using these tools.
|
|
411
|
+
All file paths must start with a /.
|
|
412
|
+
|
|
413
|
+
- ls: list all files in the filesystem
|
|
414
|
+
- read_file: read a file from the filesystem
|
|
415
|
+
- write_file: write to a file in the filesystem
|
|
416
|
+
- edit_file: edit a file in the filesystem"""
|
|
417
|
+
FILESYSTEM_SYSTEM_PROMPT_LONGTERM_SUPPLEMENT = f"""
|
|
418
|
+
|
|
419
|
+
You also have access to a longterm filesystem in which you can store files that you want to keep around for longer than the current conversation.
|
|
420
|
+
In order to interact with the longterm filesystem, you can use those same tools, but filenames must be prefixed with the {MEMORIES_PREFIX} path.
|
|
421
|
+
Remember, to interact with the longterm filesystem, you must prefix the filename with the {MEMORIES_PREFIX} path."""
|
|
422
|
+
|
|
423
|
+
|
|
424
|
+
def _get_namespace() -> tuple[str] | tuple[str, str]:
|
|
425
|
+
"""Get the namespace for longterm filesystem storage.
|
|
426
|
+
|
|
427
|
+
Returns a tuple for organizing files in the store. If an assistant_id is available
|
|
428
|
+
in the config metadata, returns a 2-tuple of (assistant_id, "filesystem") to provide
|
|
429
|
+
per-assistant isolation. Otherwise, returns a 1-tuple of ("filesystem",) for shared storage.
|
|
430
|
+
|
|
431
|
+
Returns:
|
|
432
|
+
Namespace tuple for store operations, either `(assistant_id, "filesystem")` or `("filesystem",)`.
|
|
433
|
+
"""
|
|
434
|
+
namespace = "filesystem"
|
|
435
|
+
config = get_config()
|
|
436
|
+
if config is None:
|
|
437
|
+
return (namespace,)
|
|
438
|
+
assistant_id = config.get("metadata", {}).get("assistant_id")
|
|
439
|
+
if assistant_id is None:
|
|
440
|
+
return (namespace,)
|
|
441
|
+
return (assistant_id, "filesystem")
|
|
442
|
+
|
|
443
|
+
|
|
444
|
+
def _get_store(runtime: Runtime[Any]) -> BaseStore:
|
|
445
|
+
"""Get the store from the runtime, raising an error if unavailable.
|
|
446
|
+
|
|
447
|
+
Args:
|
|
448
|
+
runtime: The LangGraph runtime containing the store.
|
|
449
|
+
|
|
450
|
+
Returns:
|
|
451
|
+
The BaseStore instance for longterm file storage.
|
|
452
|
+
|
|
453
|
+
Raises:
|
|
454
|
+
ValueError: If longterm memory is enabled but no store is available in runtime.
|
|
455
|
+
"""
|
|
456
|
+
if runtime.store is None:
|
|
457
|
+
msg = "Longterm memory is enabled, but no store is available"
|
|
458
|
+
raise ValueError(msg)
|
|
459
|
+
return runtime.store
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
def _convert_store_item_to_file_data(store_item: Item) -> FileData:
|
|
463
|
+
"""Convert a store Item to FileData format.
|
|
464
|
+
|
|
465
|
+
Args:
|
|
466
|
+
store_item: The store Item containing file data.
|
|
467
|
+
|
|
468
|
+
Returns:
|
|
469
|
+
FileData with content, created_at, and modified_at fields.
|
|
470
|
+
|
|
471
|
+
Raises:
|
|
472
|
+
ValueError: If required fields are missing or have incorrect types.
|
|
473
|
+
"""
|
|
474
|
+
if "content" not in store_item.value or not isinstance(store_item.value["content"], list):
|
|
475
|
+
msg = f"Store item does not contain valid content field. Got: {store_item.value.keys()}"
|
|
476
|
+
raise ValueError(msg)
|
|
477
|
+
if "created_at" not in store_item.value or not isinstance(store_item.value["created_at"], str):
|
|
478
|
+
msg = f"Store item does not contain valid created_at field. Got: {store_item.value.keys()}"
|
|
479
|
+
raise ValueError(msg)
|
|
480
|
+
if "modified_at" not in store_item.value or not isinstance(store_item.value["modified_at"], str):
|
|
481
|
+
msg = f"Store item does not contain valid modified_at field. Got: {store_item.value.keys()}"
|
|
482
|
+
raise ValueError(msg)
|
|
483
|
+
return FileData(
|
|
484
|
+
content=store_item.value["content"],
|
|
485
|
+
created_at=store_item.value["created_at"],
|
|
486
|
+
modified_at=store_item.value["modified_at"],
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
|
|
490
|
+
def _convert_file_data_to_store_item(file_data: FileData) -> dict[str, Any]:
|
|
491
|
+
"""Convert FileData to a dict suitable for store.put().
|
|
492
|
+
|
|
493
|
+
Args:
|
|
494
|
+
file_data: The FileData to convert.
|
|
495
|
+
|
|
496
|
+
Returns:
|
|
497
|
+
Dictionary with content, created_at, and modified_at fields.
|
|
498
|
+
"""
|
|
499
|
+
return {
|
|
500
|
+
"content": file_data["content"],
|
|
501
|
+
"created_at": file_data["created_at"],
|
|
502
|
+
"modified_at": file_data["modified_at"],
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
|
|
506
|
+
def _get_file_data_from_state(state: FilesystemState, file_path: str) -> FileData:
|
|
507
|
+
"""Retrieve file data from the agent's state.
|
|
508
|
+
|
|
509
|
+
Args:
|
|
510
|
+
state: The current filesystem state.
|
|
511
|
+
file_path: The path of the file to retrieve.
|
|
512
|
+
|
|
513
|
+
Returns:
|
|
514
|
+
The FileData for the requested file.
|
|
515
|
+
|
|
516
|
+
Raises:
|
|
517
|
+
ValueError: If the file is not found in state.
|
|
518
|
+
"""
|
|
519
|
+
mock_filesystem = state.get("files", {})
|
|
520
|
+
if file_path not in mock_filesystem:
|
|
521
|
+
msg = f"File '{file_path}' not found"
|
|
522
|
+
raise ValueError(msg)
|
|
523
|
+
return mock_filesystem[file_path]
|
|
524
|
+
|
|
525
|
+
|
|
526
|
+
def _ls_tool_generator(custom_description: str | None = None, *, long_term_memory: bool) -> BaseTool:
|
|
527
|
+
"""Generate the ls (list files) tool.
|
|
528
|
+
|
|
529
|
+
Args:
|
|
530
|
+
custom_description: Optional custom description for the tool.
|
|
531
|
+
long_term_memory: Whether to enable longterm memory support.
|
|
532
|
+
|
|
533
|
+
Returns:
|
|
534
|
+
Configured ls tool that lists files from state and optionally from longterm store.
|
|
535
|
+
"""
|
|
536
|
+
tool_description = LIST_FILES_TOOL_DESCRIPTION
|
|
537
|
+
if custom_description:
|
|
538
|
+
tool_description = custom_description
|
|
539
|
+
elif long_term_memory:
|
|
540
|
+
tool_description += LIST_FILES_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT
|
|
541
|
+
|
|
542
|
+
def _get_filenames_from_state(state: FilesystemState) -> list[str]:
|
|
543
|
+
"""Extract list of filenames from the filesystem state.
|
|
544
|
+
|
|
545
|
+
Args:
|
|
546
|
+
state: The current filesystem state.
|
|
547
|
+
|
|
548
|
+
Returns:
|
|
549
|
+
List of file paths in the state.
|
|
550
|
+
"""
|
|
551
|
+
files_dict = state.get("files", {})
|
|
552
|
+
return list(files_dict.keys())
|
|
553
|
+
|
|
554
|
+
def _filter_files_by_path(filenames: list[str], path: str | None) -> list[str]:
|
|
555
|
+
"""Filter filenames by path prefix.
|
|
556
|
+
|
|
557
|
+
Args:
|
|
558
|
+
filenames: List of file paths to filter.
|
|
559
|
+
path: Optional path prefix to filter by.
|
|
560
|
+
|
|
561
|
+
Returns:
|
|
562
|
+
Filtered list of file paths matching the prefix.
|
|
563
|
+
"""
|
|
564
|
+
if path is None:
|
|
565
|
+
return filenames
|
|
566
|
+
normalized_path = _validate_path(path)
|
|
567
|
+
return [f for f in filenames if f.startswith(normalized_path)]
|
|
568
|
+
|
|
569
|
+
if long_term_memory:
|
|
570
|
+
|
|
571
|
+
@tool(description=tool_description)
|
|
572
|
+
def ls(state: Annotated[FilesystemState, InjectedState], path: str | None = None) -> list[str]:
|
|
573
|
+
files = _get_filenames_from_state(state)
|
|
574
|
+
# Add filenames from longterm memory
|
|
575
|
+
runtime = get_runtime()
|
|
576
|
+
store = _get_store(runtime)
|
|
577
|
+
namespace = _get_namespace()
|
|
578
|
+
longterm_files = store.search(namespace)
|
|
579
|
+
longterm_files_prefixed = [_append_memories_prefix(f.key) for f in longterm_files]
|
|
580
|
+
files.extend(longterm_files_prefixed)
|
|
581
|
+
return _filter_files_by_path(files, path)
|
|
582
|
+
else:
|
|
583
|
+
|
|
584
|
+
@tool(description=tool_description)
|
|
585
|
+
def ls(state: Annotated[FilesystemState, InjectedState], path: str | None = None) -> list[str]:
|
|
586
|
+
files = _get_filenames_from_state(state)
|
|
587
|
+
return _filter_files_by_path(files, path)
|
|
588
|
+
|
|
589
|
+
return ls
|
|
590
|
+
|
|
591
|
+
|
|
592
|
+
def _read_file_tool_generator(custom_description: str | None = None, *, long_term_memory: bool) -> BaseTool:
|
|
593
|
+
"""Generate the read_file tool.
|
|
594
|
+
|
|
595
|
+
Args:
|
|
596
|
+
custom_description: Optional custom description for the tool.
|
|
597
|
+
long_term_memory: Whether to enable longterm memory support.
|
|
598
|
+
|
|
599
|
+
Returns:
|
|
600
|
+
Configured read_file tool that reads files from state and optionally from longterm store.
|
|
601
|
+
"""
|
|
602
|
+
tool_description = READ_FILE_TOOL_DESCRIPTION
|
|
603
|
+
if custom_description:
|
|
604
|
+
tool_description = custom_description
|
|
605
|
+
elif long_term_memory:
|
|
606
|
+
tool_description += READ_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT
|
|
607
|
+
|
|
608
|
+
def _read_file_data_content(file_data: FileData, offset: int, limit: int) -> str:
|
|
609
|
+
"""Read and format file content with line numbers.
|
|
610
|
+
|
|
611
|
+
Args:
|
|
612
|
+
file_data: The file data to read.
|
|
613
|
+
offset: Line offset to start reading from (0-indexed).
|
|
614
|
+
limit: Maximum number of lines to read.
|
|
615
|
+
|
|
616
|
+
Returns:
|
|
617
|
+
Formatted file content with line numbers, or an error message.
|
|
618
|
+
"""
|
|
619
|
+
content = _file_data_to_string(file_data)
|
|
620
|
+
empty_msg = _check_empty_content(content)
|
|
621
|
+
if empty_msg:
|
|
622
|
+
return empty_msg
|
|
623
|
+
lines = content.splitlines()
|
|
624
|
+
start_idx = offset
|
|
625
|
+
end_idx = min(start_idx + limit, len(lines))
|
|
626
|
+
if start_idx >= len(lines):
|
|
627
|
+
return f"Error: Line offset {offset} exceeds file length ({len(lines)} lines)"
|
|
628
|
+
selected_lines = lines[start_idx:end_idx]
|
|
629
|
+
return _format_content_with_line_numbers(selected_lines, format_style="tab", start_line=start_idx + 1)
|
|
630
|
+
|
|
631
|
+
if long_term_memory:
|
|
632
|
+
|
|
633
|
+
@tool(description=tool_description)
|
|
634
|
+
def read_file(
|
|
635
|
+
file_path: str,
|
|
636
|
+
state: Annotated[FilesystemState, InjectedState],
|
|
637
|
+
offset: int = DEFAULT_READ_OFFSET,
|
|
638
|
+
limit: int = DEFAULT_READ_LIMIT,
|
|
639
|
+
) -> str:
|
|
640
|
+
file_path = _validate_path(file_path)
|
|
641
|
+
if _has_memories_prefix(file_path):
|
|
642
|
+
stripped_file_path = _strip_memories_prefix(file_path)
|
|
643
|
+
runtime = get_runtime()
|
|
644
|
+
store = _get_store(runtime)
|
|
645
|
+
namespace = _get_namespace()
|
|
646
|
+
item: Item | None = store.get(namespace, stripped_file_path)
|
|
647
|
+
if item is None:
|
|
648
|
+
return f"Error: File '{file_path}' not found"
|
|
649
|
+
file_data = _convert_store_item_to_file_data(item)
|
|
650
|
+
else:
|
|
651
|
+
try:
|
|
652
|
+
file_data = _get_file_data_from_state(state, file_path)
|
|
653
|
+
except ValueError as e:
|
|
654
|
+
return str(e)
|
|
655
|
+
return _read_file_data_content(file_data, offset, limit)
|
|
656
|
+
|
|
657
|
+
else:
|
|
658
|
+
|
|
659
|
+
@tool(description=tool_description)
|
|
660
|
+
def read_file(
|
|
661
|
+
file_path: str,
|
|
662
|
+
state: Annotated[FilesystemState, InjectedState],
|
|
663
|
+
offset: int = DEFAULT_READ_OFFSET,
|
|
664
|
+
limit: int = DEFAULT_READ_LIMIT,
|
|
665
|
+
) -> str:
|
|
666
|
+
file_path = _validate_path(file_path)
|
|
667
|
+
try:
|
|
668
|
+
file_data = _get_file_data_from_state(state, file_path)
|
|
669
|
+
except ValueError as e:
|
|
670
|
+
return str(e)
|
|
671
|
+
return _read_file_data_content(file_data, offset, limit)
|
|
672
|
+
|
|
673
|
+
return read_file
|
|
674
|
+
|
|
675
|
+
|
|
676
|
+
def _write_file_tool_generator(custom_description: str | None = None, *, long_term_memory: bool) -> BaseTool:
|
|
677
|
+
"""Generate the write_file tool.
|
|
678
|
+
|
|
679
|
+
Args:
|
|
680
|
+
custom_description: Optional custom description for the tool.
|
|
681
|
+
long_term_memory: Whether to enable longterm memory support.
|
|
682
|
+
|
|
683
|
+
Returns:
|
|
684
|
+
Configured write_file tool that creates new files in state or longterm store.
|
|
685
|
+
"""
|
|
686
|
+
tool_description = WRITE_FILE_TOOL_DESCRIPTION
|
|
687
|
+
if custom_description:
|
|
688
|
+
tool_description = custom_description
|
|
689
|
+
elif long_term_memory:
|
|
690
|
+
tool_description += WRITE_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT
|
|
691
|
+
|
|
692
|
+
def _write_file_to_state(state: FilesystemState, tool_call_id: str, file_path: str, content: str) -> Command | str:
|
|
693
|
+
"""Write a new file to the filesystem state.
|
|
694
|
+
|
|
695
|
+
Args:
|
|
696
|
+
state: The current filesystem state.
|
|
697
|
+
tool_call_id: ID of the tool call for generating ToolMessage.
|
|
698
|
+
file_path: The path where the file should be written.
|
|
699
|
+
content: The content to write to the file.
|
|
700
|
+
|
|
701
|
+
Returns:
|
|
702
|
+
Command to update state with new file, or error string if file exists.
|
|
703
|
+
"""
|
|
704
|
+
mock_filesystem = state.get("files", {})
|
|
705
|
+
existing = mock_filesystem.get(file_path)
|
|
706
|
+
if existing:
|
|
707
|
+
return f"Cannot write to {file_path} because it already exists. Read and then make an edit, or write to a new path."
|
|
708
|
+
new_file_data = _create_file_data(content)
|
|
709
|
+
return Command(
|
|
710
|
+
update={
|
|
711
|
+
"files": {file_path: new_file_data},
|
|
712
|
+
"messages": [ToolMessage(f"Updated file {file_path}", tool_call_id=tool_call_id)],
|
|
713
|
+
}
|
|
714
|
+
)
|
|
715
|
+
|
|
716
|
+
if long_term_memory:
|
|
717
|
+
|
|
718
|
+
@tool(description=tool_description)
|
|
719
|
+
def write_file(
|
|
720
|
+
file_path: str,
|
|
721
|
+
content: str,
|
|
722
|
+
state: Annotated[FilesystemState, InjectedState],
|
|
723
|
+
tool_call_id: Annotated[str, InjectedToolCallId],
|
|
724
|
+
) -> Command | str:
|
|
725
|
+
file_path = _validate_path(file_path)
|
|
726
|
+
if _has_memories_prefix(file_path):
|
|
727
|
+
stripped_file_path = _strip_memories_prefix(file_path)
|
|
728
|
+
runtime = get_runtime()
|
|
729
|
+
store = _get_store(runtime)
|
|
730
|
+
namespace = _get_namespace()
|
|
731
|
+
if store.get(namespace, stripped_file_path) is not None:
|
|
732
|
+
return f"Cannot write to {file_path} because it already exists. Read and then make an edit, or write to a new path."
|
|
733
|
+
new_file_data = _create_file_data(content)
|
|
734
|
+
store.put(namespace, stripped_file_path, _convert_file_data_to_store_item(new_file_data))
|
|
735
|
+
return f"Updated longterm memories file {file_path}"
|
|
736
|
+
return _write_file_to_state(state, tool_call_id, file_path, content)
|
|
737
|
+
|
|
738
|
+
else:
|
|
739
|
+
|
|
740
|
+
@tool(description=tool_description)
|
|
741
|
+
def write_file(
|
|
742
|
+
file_path: str,
|
|
743
|
+
content: str,
|
|
744
|
+
state: Annotated[FilesystemState, InjectedState],
|
|
745
|
+
tool_call_id: Annotated[str, InjectedToolCallId],
|
|
746
|
+
) -> Command | str:
|
|
747
|
+
file_path = _validate_path(file_path)
|
|
748
|
+
return _write_file_to_state(state, tool_call_id, file_path, content)
|
|
749
|
+
|
|
750
|
+
return write_file
|
|
751
|
+
|
|
752
|
+
|
|
753
|
+
def _edit_file_tool_generator(custom_description: str | None = None, *, long_term_memory: bool) -> BaseTool:
|
|
754
|
+
"""Generate the edit_file tool.
|
|
755
|
+
|
|
756
|
+
Args:
|
|
757
|
+
custom_description: Optional custom description for the tool.
|
|
758
|
+
long_term_memory: Whether to enable longterm memory support.
|
|
759
|
+
|
|
760
|
+
Returns:
|
|
761
|
+
Configured edit_file tool that performs string replacements in files.
|
|
762
|
+
"""
|
|
763
|
+
tool_description = EDIT_FILE_TOOL_DESCRIPTION
|
|
764
|
+
if custom_description:
|
|
765
|
+
tool_description = custom_description
|
|
766
|
+
elif long_term_memory:
|
|
767
|
+
tool_description += EDIT_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT
|
|
768
|
+
|
|
769
|
+
def _perform_file_edit(
|
|
770
|
+
file_data: FileData,
|
|
771
|
+
old_string: str,
|
|
772
|
+
new_string: str,
|
|
773
|
+
*,
|
|
774
|
+
replace_all: bool = False,
|
|
775
|
+
) -> tuple[FileData, str] | str:
|
|
776
|
+
"""Perform string replacement on file data.
|
|
777
|
+
|
|
778
|
+
Args:
|
|
779
|
+
file_data: The file data to edit.
|
|
780
|
+
old_string: String to find and replace.
|
|
781
|
+
new_string: Replacement string.
|
|
782
|
+
replace_all: If True, replace all occurrences.
|
|
783
|
+
|
|
784
|
+
Returns:
|
|
785
|
+
Tuple of (updated_file_data, success_message) on success,
|
|
786
|
+
or error string on failure.
|
|
787
|
+
"""
|
|
788
|
+
content = _file_data_to_string(file_data)
|
|
789
|
+
occurrences = content.count(old_string)
|
|
790
|
+
if occurrences == 0:
|
|
791
|
+
return f"Error: String not found in file: '{old_string}'"
|
|
792
|
+
if occurrences > 1 and not replace_all:
|
|
793
|
+
return f"Error: String '{old_string}' appears {occurrences} times in file. Use replace_all=True to replace all instances, or provide a more specific string with surrounding context."
|
|
794
|
+
new_content = content.replace(old_string, new_string)
|
|
795
|
+
new_file_data = _update_file_data(file_data, new_content)
|
|
796
|
+
result_msg = f"Successfully replaced {occurrences} instance(s) of the string"
|
|
797
|
+
return new_file_data, result_msg
|
|
798
|
+
|
|
799
|
+
if long_term_memory:
|
|
800
|
+
|
|
801
|
+
@tool(description=tool_description)
|
|
802
|
+
def edit_file(
|
|
803
|
+
file_path: str,
|
|
804
|
+
old_string: str,
|
|
805
|
+
new_string: str,
|
|
806
|
+
state: Annotated[FilesystemState, InjectedState],
|
|
807
|
+
tool_call_id: Annotated[str, InjectedToolCallId],
|
|
808
|
+
*,
|
|
809
|
+
replace_all: bool = False,
|
|
810
|
+
) -> Command | str:
|
|
811
|
+
file_path = _validate_path(file_path)
|
|
812
|
+
is_longterm_memory = _has_memories_prefix(file_path)
|
|
813
|
+
|
|
814
|
+
# Retrieve file data from appropriate storage
|
|
815
|
+
if is_longterm_memory:
|
|
816
|
+
stripped_file_path = _strip_memories_prefix(file_path)
|
|
817
|
+
runtime = get_runtime()
|
|
818
|
+
store = _get_store(runtime)
|
|
819
|
+
namespace = _get_namespace()
|
|
820
|
+
item: Item | None = store.get(namespace, stripped_file_path)
|
|
821
|
+
if item is None:
|
|
822
|
+
return f"Error: File '{file_path}' not found"
|
|
823
|
+
file_data = _convert_store_item_to_file_data(item)
|
|
824
|
+
else:
|
|
825
|
+
try:
|
|
826
|
+
file_data = _get_file_data_from_state(state, file_path)
|
|
827
|
+
except ValueError as e:
|
|
828
|
+
return str(e)
|
|
829
|
+
|
|
830
|
+
# Perform the edit
|
|
831
|
+
result = _perform_file_edit(file_data, old_string, new_string, replace_all=replace_all)
|
|
832
|
+
if isinstance(result, str): # Error message
|
|
833
|
+
return result
|
|
834
|
+
|
|
835
|
+
new_file_data, result_msg = result
|
|
836
|
+
full_msg = f"{result_msg} in '{file_path}'"
|
|
837
|
+
|
|
838
|
+
# Save to appropriate storage
|
|
839
|
+
if is_longterm_memory:
|
|
840
|
+
store.put(namespace, stripped_file_path, _convert_file_data_to_store_item(new_file_data))
|
|
841
|
+
return full_msg
|
|
842
|
+
|
|
843
|
+
return Command(
|
|
844
|
+
update={
|
|
845
|
+
"files": {file_path: new_file_data},
|
|
846
|
+
"messages": [ToolMessage(full_msg, tool_call_id=tool_call_id)],
|
|
847
|
+
}
|
|
848
|
+
)
|
|
849
|
+
else:
|
|
850
|
+
|
|
851
|
+
@tool(description=tool_description)
|
|
852
|
+
def edit_file(
|
|
853
|
+
file_path: str,
|
|
854
|
+
old_string: str,
|
|
855
|
+
new_string: str,
|
|
856
|
+
state: Annotated[FilesystemState, InjectedState],
|
|
857
|
+
tool_call_id: Annotated[str, InjectedToolCallId],
|
|
858
|
+
*,
|
|
859
|
+
replace_all: bool = False,
|
|
860
|
+
) -> Command | str:
|
|
861
|
+
file_path = _validate_path(file_path)
|
|
862
|
+
|
|
863
|
+
# Retrieve file data from state
|
|
864
|
+
try:
|
|
865
|
+
file_data = _get_file_data_from_state(state, file_path)
|
|
866
|
+
except ValueError as e:
|
|
867
|
+
return str(e)
|
|
868
|
+
|
|
869
|
+
# Perform the edit
|
|
870
|
+
result = _perform_file_edit(file_data, old_string, new_string, replace_all=replace_all)
|
|
871
|
+
if isinstance(result, str): # Error message
|
|
872
|
+
return result
|
|
873
|
+
|
|
874
|
+
new_file_data, result_msg = result
|
|
875
|
+
full_msg = f"{result_msg} in '{file_path}'"
|
|
876
|
+
|
|
877
|
+
return Command(
|
|
878
|
+
update={
|
|
879
|
+
"files": {file_path: new_file_data},
|
|
880
|
+
"messages": [ToolMessage(full_msg, tool_call_id=tool_call_id)],
|
|
881
|
+
}
|
|
882
|
+
)
|
|
883
|
+
|
|
884
|
+
return edit_file
|
|
885
|
+
|
|
886
|
+
|
|
887
|
+
TOOL_GENERATORS = {
|
|
888
|
+
"ls": _ls_tool_generator,
|
|
889
|
+
"read_file": _read_file_tool_generator,
|
|
890
|
+
"write_file": _write_file_tool_generator,
|
|
891
|
+
"edit_file": _edit_file_tool_generator,
|
|
892
|
+
}
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
def _get_filesystem_tools(custom_tool_descriptions: dict[str, str] | None = None, *, long_term_memory: bool) -> list[BaseTool]:
|
|
896
|
+
"""Get filesystem tools.
|
|
897
|
+
|
|
898
|
+
Args:
|
|
899
|
+
custom_tool_descriptions: Optional custom descriptions for tools.
|
|
900
|
+
long_term_memory: Whether to enable longterm memory support.
|
|
901
|
+
|
|
902
|
+
Returns:
|
|
903
|
+
List of configured filesystem tools (ls, read_file, write_file, edit_file).
|
|
904
|
+
"""
|
|
905
|
+
if custom_tool_descriptions is None:
|
|
906
|
+
custom_tool_descriptions = {}
|
|
907
|
+
tools = []
|
|
908
|
+
for tool_name, tool_generator in TOOL_GENERATORS.items():
|
|
909
|
+
tool = tool_generator(custom_tool_descriptions.get(tool_name), long_term_memory=long_term_memory)
|
|
910
|
+
tools.append(tool)
|
|
911
|
+
return tools
|
|
912
|
+
|
|
913
|
+
|
|
914
|
+
TOO_LARGE_TOOL_MSG = """Tool result too large, the result of this tool call {tool_call_id} was saved in the filesystem at this path: {file_path}
|
|
915
|
+
You can read the result from the filesystem by using the read_file tool, but make sure to only read part of the result at a time.
|
|
916
|
+
You can do this by specifying an offset and limit in the read_file tool call.
|
|
917
|
+
For example, to read the first 100 lines, you can use the read_file tool with offset=0 and limit=100.
|
|
918
|
+
|
|
919
|
+
Here are the first 10 lines of the result:
|
|
920
|
+
{content_sample}
|
|
921
|
+
"""
|
|
922
|
+
|
|
923
|
+
|
|
924
|
+
class FilesystemMiddleware(AgentMiddleware):
|
|
925
|
+
"""Middleware for providing filesystem tools to an agent.
|
|
926
|
+
|
|
927
|
+
This middleware adds four filesystem tools to the agent: ls, read_file, write_file,
|
|
928
|
+
and edit_file. Files can be stored in two locations:
|
|
929
|
+
- Short-term: In the agent's state (ephemeral, lasts only for the conversation)
|
|
930
|
+
- Long-term: In a persistent store (persists across conversations when enabled)
|
|
931
|
+
|
|
932
|
+
Args:
|
|
933
|
+
long_term_memory: Whether to enable longterm memory support.
|
|
934
|
+
system_prompt_extension: Optional custom system prompt override.
|
|
935
|
+
custom_tool_descriptions: Optional custom tool descriptions override.
|
|
936
|
+
|
|
937
|
+
Raises:
|
|
938
|
+
ValueError: If longterm memory is enabled but no store is available.
|
|
939
|
+
|
|
940
|
+
Example:
|
|
941
|
+
```python
|
|
942
|
+
from langchain.agents.middleware.filesystem import FilesystemMiddleware
|
|
943
|
+
from langchain.agents import create_agent
|
|
944
|
+
|
|
945
|
+
# Short-term memory only
|
|
946
|
+
agent = create_agent(middleware=[FilesystemMiddleware(long_term_memory=False)])
|
|
947
|
+
|
|
948
|
+
# With long-term memory
|
|
949
|
+
agent = create_agent(middleware=[FilesystemMiddleware(long_term_memory=True)])
|
|
950
|
+
```
|
|
951
|
+
"""
|
|
952
|
+
|
|
953
|
+
state_schema = FilesystemState
|
|
954
|
+
|
|
955
|
+
def __init__(
|
|
956
|
+
self,
|
|
957
|
+
*,
|
|
958
|
+
long_term_memory: bool = False,
|
|
959
|
+
system_prompt: str | None = None,
|
|
960
|
+
custom_tool_descriptions: dict[str, str] | None = None,
|
|
961
|
+
tool_token_limit_before_evict: int | None = 50000,
|
|
962
|
+
) -> None:
|
|
963
|
+
"""Initialize the filesystem middleware.
|
|
964
|
+
|
|
965
|
+
Args:
|
|
966
|
+
long_term_memory: Whether to enable longterm memory support.
|
|
967
|
+
system_prompt: Optional custom system prompt override.
|
|
968
|
+
custom_tool_descriptions: Optional custom tool descriptions override.
|
|
969
|
+
tool_token_limit_before_evict: Optional token limit before evicting a tool result to the filesystem.
|
|
970
|
+
"""
|
|
971
|
+
self.long_term_memory = long_term_memory
|
|
972
|
+
self.tool_token_limit_before_evict = tool_token_limit_before_evict
|
|
973
|
+
self.system_prompt = FILESYSTEM_SYSTEM_PROMPT
|
|
974
|
+
if system_prompt is not None:
|
|
975
|
+
self.system_prompt = system_prompt
|
|
976
|
+
elif long_term_memory:
|
|
977
|
+
self.system_prompt += FILESYSTEM_SYSTEM_PROMPT_LONGTERM_SUPPLEMENT
|
|
978
|
+
|
|
979
|
+
self.tools = _get_filesystem_tools(custom_tool_descriptions, long_term_memory=long_term_memory)
|
|
980
|
+
|
|
981
|
+
def before_agent(self, state: AgentState, runtime: Runtime[Any]) -> dict[str, Any] | None: # noqa: ARG002
|
|
982
|
+
"""Validate that store is available if longterm memory is enabled.
|
|
983
|
+
|
|
984
|
+
Args:
|
|
985
|
+
state: The state of the agent.
|
|
986
|
+
runtime: The LangGraph runtime.
|
|
987
|
+
|
|
988
|
+
Returns:
|
|
989
|
+
The unmodified model request.
|
|
990
|
+
|
|
991
|
+
Raises:
|
|
992
|
+
ValueError: If long_term_memory is True but runtime.store is None.
|
|
993
|
+
"""
|
|
994
|
+
if self.long_term_memory and runtime.store is None:
|
|
995
|
+
msg = "Longterm memory is enabled, but no store is available"
|
|
996
|
+
raise ValueError(msg)
|
|
997
|
+
return None
|
|
998
|
+
|
|
999
|
+
def wrap_model_call(
|
|
1000
|
+
self,
|
|
1001
|
+
request: ModelRequest,
|
|
1002
|
+
handler: Callable[[ModelRequest], ModelResponse],
|
|
1003
|
+
) -> ModelResponse:
|
|
1004
|
+
"""Update the system prompt to include instructions on using the filesystem.
|
|
1005
|
+
|
|
1006
|
+
Args:
|
|
1007
|
+
request: The model request being processed.
|
|
1008
|
+
handler: The handler function to call with the modified request.
|
|
1009
|
+
|
|
1010
|
+
Returns:
|
|
1011
|
+
The model response from the handler.
|
|
1012
|
+
"""
|
|
1013
|
+
if self.system_prompt is not None:
|
|
1014
|
+
request.system_prompt = request.system_prompt + "\n\n" + self.system_prompt if request.system_prompt else self.system_prompt
|
|
1015
|
+
return handler(request)
|
|
1016
|
+
|
|
1017
|
+
async def awrap_model_call(
|
|
1018
|
+
self,
|
|
1019
|
+
request: ModelRequest,
|
|
1020
|
+
handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
|
|
1021
|
+
) -> ModelResponse:
|
|
1022
|
+
"""(async) Update the system prompt to include instructions on using the filesystem.
|
|
1023
|
+
|
|
1024
|
+
Args:
|
|
1025
|
+
request: The model request being processed.
|
|
1026
|
+
handler: The handler function to call with the modified request.
|
|
1027
|
+
|
|
1028
|
+
Returns:
|
|
1029
|
+
The model response from the handler.
|
|
1030
|
+
"""
|
|
1031
|
+
if self.system_prompt is not None:
|
|
1032
|
+
request.system_prompt = request.system_prompt + "\n\n" + self.system_prompt if request.system_prompt else self.system_prompt
|
|
1033
|
+
return await handler(request)
|
|
1034
|
+
|
|
1035
|
+
def _intercept_large_tool_result(self, tool_result: ToolMessage | Command) -> ToolMessage | Command:
|
|
1036
|
+
if isinstance(tool_result, ToolMessage) and isinstance(tool_result.content, str):
|
|
1037
|
+
content = tool_result.content
|
|
1038
|
+
if self.tool_token_limit_before_evict and len(content) > 4 * self.tool_token_limit_before_evict:
|
|
1039
|
+
file_path = f"/large_tool_results/{tool_result.tool_call_id}"
|
|
1040
|
+
file_data = _create_file_data(content)
|
|
1041
|
+
state_update = {
|
|
1042
|
+
"messages": [
|
|
1043
|
+
ToolMessage(
|
|
1044
|
+
TOO_LARGE_TOOL_MSG.format(
|
|
1045
|
+
tool_call_id=tool_result.tool_call_id,
|
|
1046
|
+
file_path=file_path,
|
|
1047
|
+
content_sample=_format_content_with_line_numbers(file_data["content"][:10], format_style="tab", start_line=1),
|
|
1048
|
+
),
|
|
1049
|
+
tool_call_id=tool_result.tool_call_id,
|
|
1050
|
+
)
|
|
1051
|
+
],
|
|
1052
|
+
"files": {file_path: file_data},
|
|
1053
|
+
}
|
|
1054
|
+
return Command(update=state_update)
|
|
1055
|
+
elif isinstance(tool_result, Command):
|
|
1056
|
+
update = tool_result.update
|
|
1057
|
+
if update is None:
|
|
1058
|
+
return tool_result
|
|
1059
|
+
message_updates = update.get("messages", [])
|
|
1060
|
+
file_updates = update.get("files", {})
|
|
1061
|
+
|
|
1062
|
+
edited_message_updates = []
|
|
1063
|
+
for message in message_updates:
|
|
1064
|
+
if self.tool_token_limit_before_evict and isinstance(message, ToolMessage) and isinstance(message.content, str):
|
|
1065
|
+
content = message.content
|
|
1066
|
+
if len(content) > 4 * self.tool_token_limit_before_evict:
|
|
1067
|
+
file_path = f"/large_tool_results/{message.tool_call_id}"
|
|
1068
|
+
file_data = _create_file_data(content)
|
|
1069
|
+
edited_message_updates.append(
|
|
1070
|
+
ToolMessage(
|
|
1071
|
+
TOO_LARGE_TOOL_MSG.format(
|
|
1072
|
+
tool_call_id=message.tool_call_id,
|
|
1073
|
+
file_path=file_path,
|
|
1074
|
+
content_sample=_format_content_with_line_numbers(file_data["content"][:10], format_style="tab", start_line=1),
|
|
1075
|
+
),
|
|
1076
|
+
tool_call_id=message.tool_call_id,
|
|
1077
|
+
)
|
|
1078
|
+
)
|
|
1079
|
+
file_updates[file_path] = file_data
|
|
1080
|
+
continue
|
|
1081
|
+
edited_message_updates.append(message)
|
|
1082
|
+
return Command(update={**update, "messages": edited_message_updates, "files": file_updates})
|
|
1083
|
+
return tool_result
|
|
1084
|
+
|
|
1085
|
+
def wrap_tool_call(
|
|
1086
|
+
self,
|
|
1087
|
+
request: ToolCallRequest,
|
|
1088
|
+
handler: Callable[[ToolCallRequest], ToolMessage | Command],
|
|
1089
|
+
) -> ToolMessage | Command:
|
|
1090
|
+
"""Check the size of the tool call result and evict to filesystem if too large.
|
|
1091
|
+
|
|
1092
|
+
Args:
|
|
1093
|
+
request: The tool call request being processed.
|
|
1094
|
+
handler: The handler function to call with the modified request.
|
|
1095
|
+
|
|
1096
|
+
Returns:
|
|
1097
|
+
The raw ToolMessage, or a pseudo tool message with the ToolResult in state.
|
|
1098
|
+
"""
|
|
1099
|
+
# If no token limit specified, or if it is a filesystem tool, do not evict
|
|
1100
|
+
if self.tool_token_limit_before_evict is None or request.tool_call["name"] in TOOL_GENERATORS:
|
|
1101
|
+
return handler(request)
|
|
1102
|
+
|
|
1103
|
+
tool_result = handler(request)
|
|
1104
|
+
return self._intercept_large_tool_result(tool_result)
|
|
1105
|
+
|
|
1106
|
+
async def awrap_tool_call(
|
|
1107
|
+
self,
|
|
1108
|
+
request: ToolCallRequest,
|
|
1109
|
+
handler: Callable[[ToolCallRequest], Awaitable[ToolMessage | Command]],
|
|
1110
|
+
) -> ToolMessage | Command:
|
|
1111
|
+
"""(async)Check the size of the tool call result and evict to filesystem if too large.
|
|
1112
|
+
|
|
1113
|
+
Args:
|
|
1114
|
+
request: The tool call request being processed.
|
|
1115
|
+
handler: The handler function to call with the modified request.
|
|
1116
|
+
|
|
1117
|
+
Returns:
|
|
1118
|
+
The raw ToolMessage, or a pseudo tool message with the ToolResult in state.
|
|
1119
|
+
"""
|
|
1120
|
+
# If no token limit specified, or if it is a filesystem tool, do not evict
|
|
1121
|
+
if self.tool_token_limit_before_evict is None or request.tool_call["name"] in TOOL_GENERATORS:
|
|
1122
|
+
return await handler(request)
|
|
1123
|
+
|
|
1124
|
+
tool_result = await handler(request)
|
|
1125
|
+
return self._intercept_large_tool_result(tool_result)
|