deepagents 0.1.3__py3-none-any.whl → 0.1.5rc1__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.
@@ -2,15 +2,11 @@
2
2
  # ruff: noqa: E501
3
3
 
4
4
  from collections.abc import Awaitable, Callable, Sequence
5
- from typing import TYPE_CHECKING, Annotated, Any
5
+ from typing import Annotated
6
6
  from typing_extensions import NotRequired
7
7
 
8
- if TYPE_CHECKING:
9
- from langgraph.runtime import Runtime
10
-
11
8
  import os
12
- from datetime import UTC, datetime
13
- from typing import TYPE_CHECKING, Literal
9
+ from typing import Literal, Optional
14
10
 
15
11
  from langchain.agents.middleware.types import (
16
12
  AgentMiddleware,
@@ -22,18 +18,28 @@ from langchain.tools import ToolRuntime
22
18
  from langchain.tools.tool_node import ToolCallRequest
23
19
  from langchain_core.messages import ToolMessage
24
20
  from langchain_core.tools import BaseTool, tool
25
- from langgraph.config import get_config
26
- from langgraph.runtime import Runtime
27
- from langgraph.store.base import BaseStore, Item
28
21
  from langgraph.types import Command
29
22
  from typing_extensions import TypedDict
30
23
 
31
- MEMORIES_PREFIX = "/memories/"
24
+ from deepagents.backends.protocol import BackendProtocol, BackendFactory, WriteResult, EditResult
25
+ from deepagents.backends import StateBackend
26
+ from deepagents.backends.utils import (
27
+ create_file_data,
28
+ update_file_data,
29
+ format_content_with_line_numbers,
30
+ format_grep_matches,
31
+ truncate_if_too_long,
32
+ )
33
+
32
34
  EMPTY_CONTENT_WARNING = "System reminder: File exists but has empty contents"
33
35
  MAX_LINE_LENGTH = 2000
34
36
  LINE_NUMBER_WIDTH = 6
35
37
  DEFAULT_READ_OFFSET = 0
36
38
  DEFAULT_READ_LIMIT = 2000
39
+ BACKEND_TYPES = (
40
+ BackendProtocol
41
+ | BackendFactory
42
+ )
37
43
 
38
44
 
39
45
  class FileData(TypedDict):
@@ -74,10 +80,8 @@ def _file_data_reducer(left: dict[str, FileData] | None, right: dict[str, FileDa
74
80
  ```
75
81
  """
76
82
  if left is None:
77
- # Filter out None values when initializing
78
83
  return {k: v for k, v in right.items() if v is not None}
79
84
 
80
- # Merge, filtering out None values (deletions)
81
85
  result = {**left}
82
86
  for key, value in right.items():
83
87
  if value is None:
@@ -115,243 +119,22 @@ def _validate_path(path: str, *, allowed_prefixes: Sequence[str] | None = None)
115
119
  validate_path("/etc/file.txt", allowed_prefixes=["/data/"]) # Raises ValueError
116
120
  ```
117
121
  """
118
- # Reject paths with traversal attempts
119
122
  if ".." in path or path.startswith("~"):
120
123
  msg = f"Path traversal not allowed: {path}"
121
124
  raise ValueError(msg)
122
125
 
123
- # Normalize path (resolve ., //, etc.)
124
126
  normalized = os.path.normpath(path)
125
-
126
- # Convert to forward slashes for consistency
127
127
  normalized = normalized.replace("\\", "/")
128
128
 
129
- # Ensure path starts with /
130
129
  if not normalized.startswith("/"):
131
130
  normalized = f"/{normalized}"
132
131
 
133
- # Check allowed prefixes if specified
134
132
  if allowed_prefixes is not None and not any(normalized.startswith(prefix) for prefix in allowed_prefixes):
135
133
  msg = f"Path must start with one of {allowed_prefixes}: {path}"
136
134
  raise ValueError(msg)
137
135
 
138
136
  return normalized
139
137
 
140
-
141
- def _format_content_with_line_numbers(
142
- content: str | list[str],
143
- *,
144
- format_style: Literal["pipe", "tab"] = "pipe",
145
- start_line: int = 1,
146
- ) -> str:
147
- r"""Format file content with line numbers for display.
148
-
149
- Converts file content to a numbered format similar to `cat -n` output,
150
- with support for two different formatting styles.
151
-
152
- Args:
153
- content: File content as a string or list of lines.
154
- format_style: Format style for line numbers:
155
- - `"pipe"`: Compact format like `"1|content"`
156
- - `"tab"`: Right-aligned format like `" 1\tcontent"` (lines truncated at 2000 chars)
157
- start_line: Starting line number (default: 1).
158
-
159
- Returns:
160
- Formatted content with line numbers prepended to each line.
161
-
162
- Example:
163
- ```python
164
- content = "Hello\nWorld"
165
- format_content_with_line_numbers(content, format_style="pipe")
166
- # Returns: "1|Hello\n2|World"
167
-
168
- format_content_with_line_numbers(content, format_style="tab", start_line=10)
169
- # Returns: " 10\tHello\n 11\tWorld"
170
- ```
171
- """
172
- if isinstance(content, str):
173
- lines = content.split("\n")
174
- # Remove trailing empty line from split
175
- if lines and lines[-1] == "":
176
- lines = lines[:-1]
177
- else:
178
- lines = content
179
-
180
- if format_style == "pipe":
181
- return "\n".join(f"{i + start_line}|{line}" for i, line in enumerate(lines))
182
-
183
- # Tab format with defined width and line truncation
184
- return "\n".join(f"{i + start_line:{LINE_NUMBER_WIDTH}d}\t{line[:MAX_LINE_LENGTH]}" for i, line in enumerate(lines))
185
-
186
-
187
- def _create_file_data(
188
- content: str | list[str],
189
- *,
190
- created_at: str | None = None,
191
- ) -> FileData:
192
- r"""Create a FileData object with automatic timestamp generation.
193
-
194
- Args:
195
- content: File content as a string or list of lines.
196
- created_at: Optional creation timestamp in ISO 8601 format.
197
- If `None`, uses the current UTC time.
198
-
199
- Returns:
200
- FileData object with content and timestamps.
201
-
202
- Example:
203
- ```python
204
- file_data = create_file_data("Hello\nWorld")
205
- # Returns: {"content": ["Hello", "World"], "created_at": "2024-...",
206
- # "modified_at": "2024-..."}
207
- ```
208
- """
209
- lines = content.split("\n") if isinstance(content, str) else content
210
- now = datetime.now(UTC).isoformat()
211
-
212
- return {
213
- "content": lines,
214
- "created_at": created_at or now,
215
- "modified_at": now,
216
- }
217
-
218
-
219
- def _update_file_data(
220
- file_data: FileData,
221
- content: str | list[str],
222
- ) -> FileData:
223
- """Update FileData with new content while preserving creation timestamp.
224
-
225
- Args:
226
- file_data: Existing FileData object to update.
227
- content: New file content as a string or list of lines.
228
-
229
- Returns:
230
- Updated FileData object with new content and updated `modified_at`
231
- timestamp. The `created_at` timestamp is preserved from the original.
232
-
233
- Example:
234
- ```python
235
- original = create_file_data("Hello")
236
- updated = update_file_data(original, "Hello World")
237
- # updated["created_at"] == original["created_at"]
238
- # updated["modified_at"] > original["modified_at"]
239
- ```
240
- """
241
- lines = content.split("\n") if isinstance(content, str) else content
242
- now = datetime.now(UTC).isoformat()
243
-
244
- return {
245
- "content": lines,
246
- "created_at": file_data["created_at"],
247
- "modified_at": now,
248
- }
249
-
250
-
251
- def _file_data_to_string(file_data: FileData) -> str:
252
- r"""Convert FileData to plain string content.
253
-
254
- Joins the lines stored in FileData with newline characters to produce
255
- a single string representation of the file content.
256
-
257
- Args:
258
- file_data: FileData object containing lines of content.
259
-
260
- Returns:
261
- File content as a single string with lines joined by newlines.
262
-
263
- Example:
264
- ```python
265
- file_data = {
266
- "content": ["Hello", "World"],
267
- "created_at": "...",
268
- "modified_at": "...",
269
- }
270
- file_data_to_string(file_data) # Returns: "Hello\nWorld"
271
- ```
272
- """
273
- return "\n".join(file_data["content"])
274
-
275
-
276
- def _check_empty_content(content: str) -> str | None:
277
- """Check if file content is empty and return a warning message.
278
-
279
- Args:
280
- content: File content to check.
281
-
282
- Returns:
283
- Warning message string if content is empty or contains only whitespace,
284
- `None` otherwise.
285
-
286
- Example:
287
- ```python
288
- check_empty_content("") # Returns: "System reminder: File exists but has empty contents"
289
- check_empty_content(" ") # Returns: "System reminder: File exists but has empty contents"
290
- check_empty_content("Hello") # Returns: None
291
- ```
292
- """
293
- if not content or content.strip() == "":
294
- return EMPTY_CONTENT_WARNING
295
- return None
296
-
297
-
298
- def _has_memories_prefix(file_path: str) -> bool:
299
- """Check if a file path is in the longterm memory filesystem.
300
-
301
- Longterm memory files are distinguished by the `/memories/` path prefix.
302
-
303
- Args:
304
- file_path: File path to check.
305
-
306
- Returns:
307
- `True` if the file path starts with `/memories/`, `False` otherwise.
308
-
309
- Example:
310
- ```python
311
- has_memories_prefix("/memories/notes.txt") # Returns: True
312
- has_memories_prefix("/temp/file.txt") # Returns: False
313
- ```
314
- """
315
- return file_path.startswith(MEMORIES_PREFIX)
316
-
317
-
318
- def _append_memories_prefix(file_path: str) -> str:
319
- """Add the longterm memory prefix to a file path.
320
-
321
- Args:
322
- file_path: File path to prefix.
323
-
324
- Returns:
325
- File path with `/memories` prepended.
326
-
327
- Example:
328
- ```python
329
- append_memories_prefix("/notes.txt") # Returns: "/memories/notes.txt"
330
- ```
331
- """
332
- return f"/memories{file_path}"
333
-
334
-
335
- def _strip_memories_prefix(file_path: str) -> str:
336
- """Remove the longterm memory prefix from a file path.
337
-
338
- Args:
339
- file_path: File path potentially containing the memories prefix.
340
-
341
- Returns:
342
- File path with `/memories` removed if present at the start.
343
-
344
- Example:
345
- ```python
346
- strip_memories_prefix("/memories/notes.txt") # Returns: "/notes.txt"
347
- strip_memories_prefix("/notes.txt") # Returns: "/notes.txt"
348
- ```
349
- """
350
- if file_path.startswith(MEMORIES_PREFIX):
351
- return file_path[len(MEMORIES_PREFIX) - 1 :] # Keep the leading slash
352
- return file_path
353
-
354
-
355
138
  class FilesystemState(AgentState):
356
139
  """State for the filesystem middleware."""
357
140
 
@@ -359,14 +142,13 @@ class FilesystemState(AgentState):
359
142
  """Files in the filesystem."""
360
143
 
361
144
 
362
- LIST_FILES_TOOL_DESCRIPTION = """Lists all files in the filesystem, optionally filtering by directory.
145
+ LIST_FILES_TOOL_DESCRIPTION = """Lists all files in the filesystem, filtering by directory.
363
146
 
364
147
  Usage:
365
- - The list_files tool will return a list of all files in the filesystem.
366
- - You can optionally provide a path parameter to list files in a specific directory.
148
+ - The path parameter must be an absolute path, not a relative path
149
+ - The list_files tool will return a list of all files in the specified directory.
367
150
  - This is very useful for exploring the file system and finding the right file to read or edit.
368
151
  - You should almost ALWAYS use this tool before using the Read or Edit tools."""
369
- LIST_FILES_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT = f"\n- Files from the longterm filesystem will be prefixed with the {MEMORIES_PREFIX} path."
370
152
 
371
153
  READ_FILE_TOOL_DESCRIPTION = """Reads a file from the filesystem. You can access any file directly by using this tool.
372
154
  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.
@@ -380,7 +162,6 @@ Usage:
380
162
  - 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.
381
163
  - If you read a file that exists but has empty contents you will receive a system reminder warning in place of file contents.
382
164
  - You should ALWAYS make sure a file has been read before editing it."""
383
- READ_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT = f"\n- file_paths prefixed with the {MEMORIES_PREFIX} path will be read from the longterm filesystem."
384
165
 
385
166
  EDIT_FILE_TOOL_DESCRIPTION = """Performs exact string replacements in files.
386
167
 
@@ -391,9 +172,7 @@ Usage:
391
172
  - Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.
392
173
  - 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`.
393
174
  - Use `replace_all` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance."""
394
- EDIT_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT = (
395
- f"\n- You can edit files in the longterm filesystem by prefixing the filename with the {MEMORIES_PREFIX} path."
396
- )
175
+
397
176
 
398
177
  WRITE_FILE_TOOL_DESCRIPTION = """Writes to a new file in the filesystem.
399
178
 
@@ -401,487 +180,253 @@ Usage:
401
180
  - The file_path parameter must be an absolute path, not a relative path
402
181
  - The content parameter must be a string
403
182
  - The write_file tool will create the a new file.
404
- - Prefer to edit existing files over creating new ones when possible.
405
- - file_paths prefixed with the /memories/ path will be written to the longterm filesystem."""
406
- WRITE_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT = (
407
- f"\n- file_paths prefixed with the {MEMORIES_PREFIX} path will be written to the longterm filesystem."
408
- )
409
-
410
- FILESYSTEM_SYSTEM_PROMPT = """## Filesystem Tools `ls`, `read_file`, `write_file`, `edit_file`
183
+ - Prefer to edit existing files over creating new ones when possible."""
411
184
 
412
- You have access to a filesystem which you can interact with using these tools.
413
- All file paths must start with a /.
414
-
415
- - ls: list all files in the filesystem
416
- - read_file: read a file from the filesystem
417
- - write_file: write to a file in the filesystem
418
- - edit_file: edit a file in the filesystem"""
419
- FILESYSTEM_SYSTEM_PROMPT_LONGTERM_SUPPLEMENT = f"""
420
185
 
421
- 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.
422
- In order to interact with the longterm filesystem, you can use those same tools, but filenames must be prefixed with the {MEMORIES_PREFIX} path.
423
- Remember, to interact with the longterm filesystem, you must prefix the filename with the {MEMORIES_PREFIX} path."""
186
+ GLOB_TOOL_DESCRIPTION = """Find files matching a glob pattern.
424
187
 
188
+ Usage:
189
+ - The glob tool finds files by matching patterns with wildcards
190
+ - Supports standard glob patterns: `*` (any characters), `**` (any directories), `?` (single character)
191
+ - Patterns can be absolute (starting with `/`) or relative
192
+ - Returns a list of absolute file paths that match the pattern
425
193
 
426
- def _get_namespace() -> tuple[str] | tuple[str, str]:
427
- """Get the namespace for longterm filesystem storage.
428
-
429
- Returns a tuple for organizing files in the store. If an assistant_id is available
430
- in the config metadata, returns a 2-tuple of (assistant_id, "filesystem") to provide
431
- per-assistant isolation. Otherwise, returns a 1-tuple of ("filesystem",) for shared storage.
194
+ Examples:
195
+ - `**/*.py` - Find all Python files
196
+ - `*.txt` - Find all text files in root
197
+ - `/subdir/**/*.md` - Find all markdown files under /subdir"""
432
198
 
433
- Returns:
434
- Namespace tuple for store operations, either `(assistant_id, "filesystem")` or `("filesystem",)`.
435
- """
436
- namespace = "filesystem"
437
- config = get_config()
438
- if config is None:
439
- return (namespace,)
440
- assistant_id = config.get("metadata", {}).get("assistant_id")
441
- if assistant_id is None:
442
- return (namespace,)
443
- return (assistant_id, "filesystem")
199
+ GREP_TOOL_DESCRIPTION = """Search for a pattern in files.
444
200
 
201
+ Usage:
202
+ - The grep tool searches for text patterns across files
203
+ - The pattern parameter is the text to search for (literal string, not regex)
204
+ - The path parameter filters which directory to search in (default is the current working directory)
205
+ - The glob parameter accepts a glob pattern to filter which files to search (e.g., `*.py`)
206
+ - The output_mode parameter controls the output format:
207
+ - `files_with_matches`: List only file paths containing matches (default)
208
+ - `content`: Show matching lines with file path and line numbers
209
+ - `count`: Show count of matches per file
210
+
211
+ Examples:
212
+ - Search all files: `grep(pattern="TODO")`
213
+ - Search Python files only: `grep(pattern="import", glob="*.py")`
214
+ - Show matching lines: `grep(pattern="error", output_mode="content")`"""
215
+
216
+ FILESYSTEM_SYSTEM_PROMPT = """## Filesystem Tools `ls`, `read_file`, `write_file`, `edit_file`, `glob`, `grep`
445
217
 
446
- def _get_store(runtime: ToolRuntime[None, FilesystemState]) -> BaseStore:
447
- """Get the store from the runtime, raising an error if unavailable.
218
+ You have access to a filesystem which you can interact with using these tools.
219
+ All file paths must start with a /.
448
220
 
449
- Args:
450
- runtime: The LangGraph runtime containing the store.
221
+ - ls: list files in a directory (requires absolute path)
222
+ - read_file: read a file from the filesystem
223
+ - write_file: write to a file in the filesystem
224
+ - edit_file: edit a file in the filesystem
225
+ - glob: find files matching a pattern (e.g., "**/*.py")
226
+ - grep: search for text within files"""
451
227
 
452
- Returns:
453
- The BaseStore instance for longterm file storage.
454
228
 
455
- Raises:
456
- ValueError: If longterm memory is enabled but no store is available in runtime.
457
- """
458
- if runtime.store is None:
459
- msg = "Longterm memory is enabled, but no store is available"
460
- raise ValueError(msg)
461
- return runtime.store
229
+ def _get_backend(backend: BACKEND_TYPES, runtime: ToolRuntime) -> BackendProtocol:
230
+ if callable(backend):
231
+ return backend(runtime)
232
+ return backend
462
233
 
463
234
 
464
- def _convert_store_item_to_file_data(store_item: Item) -> FileData:
465
- """Convert a store Item to FileData format.
235
+ def _ls_tool_generator(
236
+ backend: BackendProtocol | Callable[[ToolRuntime], BackendProtocol],
237
+ custom_description: str | None = None,
238
+ ) -> BaseTool:
239
+ """Generate the ls (list files) tool.
466
240
 
467
241
  Args:
468
- store_item: The store Item containing file data.
242
+ backend: Backend to use for file storage, or a factory function that takes runtime and returns a backend.
243
+ custom_description: Optional custom description for the tool.
469
244
 
470
245
  Returns:
471
- FileData with content, created_at, and modified_at fields.
472
-
473
- Raises:
474
- ValueError: If required fields are missing or have incorrect types.
246
+ Configured ls tool that lists files using the backend.
475
247
  """
476
- if "content" not in store_item.value or not isinstance(store_item.value["content"], list):
477
- msg = f"Store item does not contain valid content field. Got: {store_item.value.keys()}"
478
- raise ValueError(msg)
479
- if "created_at" not in store_item.value or not isinstance(store_item.value["created_at"], str):
480
- msg = f"Store item does not contain valid created_at field. Got: {store_item.value.keys()}"
481
- raise ValueError(msg)
482
- if "modified_at" not in store_item.value or not isinstance(store_item.value["modified_at"], str):
483
- msg = f"Store item does not contain valid modified_at field. Got: {store_item.value.keys()}"
484
- raise ValueError(msg)
485
- return FileData(
486
- content=store_item.value["content"],
487
- created_at=store_item.value["created_at"],
488
- modified_at=store_item.value["modified_at"],
489
- )
248
+ tool_description = custom_description or LIST_FILES_TOOL_DESCRIPTION
490
249
 
250
+ @tool(description=tool_description)
251
+ def ls(runtime: ToolRuntime[None, FilesystemState], path: str) -> list[str]:
252
+ resolved_backend = _get_backend(backend, runtime)
253
+ validated_path = _validate_path(path)
254
+ infos = resolved_backend.ls_info(validated_path)
255
+ return [fi.get("path", "") for fi in infos]
491
256
 
492
- def _convert_file_data_to_store_item(file_data: FileData) -> dict[str, Any]:
493
- """Convert FileData to a dict suitable for store.put().
494
-
495
- Args:
496
- file_data: The FileData to convert.
497
-
498
- Returns:
499
- Dictionary with content, created_at, and modified_at fields.
500
- """
501
- return {
502
- "content": file_data["content"],
503
- "created_at": file_data["created_at"],
504
- "modified_at": file_data["modified_at"],
505
- }
257
+ return ls
506
258
 
507
259
 
508
- def _get_file_data_from_state(state: FilesystemState, file_path: str) -> FileData:
509
- """Retrieve file data from the agent's state.
260
+ def _read_file_tool_generator(
261
+ backend: BackendProtocol | Callable[[ToolRuntime], BackendProtocol],
262
+ custom_description: str | None = None,
263
+ ) -> BaseTool:
264
+ """Generate the read_file tool.
510
265
 
511
266
  Args:
512
- state: The current filesystem state.
513
- file_path: The path of the file to retrieve.
267
+ backend: Backend to use for file storage, or a factory function that takes runtime and returns a backend.
268
+ custom_description: Optional custom description for the tool.
514
269
 
515
270
  Returns:
516
- The FileData for the requested file.
517
-
518
- Raises:
519
- ValueError: If the file is not found in state.
271
+ Configured read_file tool that reads files using the backend.
520
272
  """
521
- mock_filesystem = state.get("files", {})
522
- if file_path not in mock_filesystem:
523
- msg = f"File '{file_path}' not found"
524
- raise ValueError(msg)
525
- return mock_filesystem[file_path]
273
+ tool_description = custom_description or READ_FILE_TOOL_DESCRIPTION
274
+
275
+ @tool(description=tool_description)
276
+ def read_file(
277
+ file_path: str,
278
+ runtime: ToolRuntime[None, FilesystemState],
279
+ offset: int = DEFAULT_READ_OFFSET,
280
+ limit: int = DEFAULT_READ_LIMIT,
281
+ ) -> str:
282
+ resolved_backend = _get_backend(backend, runtime)
283
+ file_path = _validate_path(file_path)
284
+ return resolved_backend.read(file_path, offset=offset, limit=limit)
526
285
 
286
+ return read_file
527
287
 
528
- def _ls_tool_generator(custom_description: str | None = None, *, long_term_memory: bool) -> BaseTool:
529
- """Generate the ls (list files) tool.
288
+
289
+ def _write_file_tool_generator(
290
+ backend: BackendProtocol | Callable[[ToolRuntime], BackendProtocol],
291
+ custom_description: str | None = None,
292
+ ) -> BaseTool:
293
+ """Generate the write_file tool.
530
294
 
531
295
  Args:
296
+ backend: Backend to use for file storage, or a factory function that takes runtime and returns a backend.
532
297
  custom_description: Optional custom description for the tool.
533
- long_term_memory: Whether to enable longterm memory support.
534
298
 
535
299
  Returns:
536
- Configured ls tool that lists files from state and optionally from longterm store.
300
+ Configured write_file tool that creates new files using the backend.
537
301
  """
538
- tool_description = LIST_FILES_TOOL_DESCRIPTION
539
- if custom_description:
540
- tool_description = custom_description
541
- elif long_term_memory:
542
- tool_description += LIST_FILES_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT
543
-
544
- def _get_filenames_from_state(state: FilesystemState) -> list[str]:
545
- """Extract list of filenames from the filesystem state.
546
-
547
- Args:
548
- state: The current filesystem state.
549
-
550
- Returns:
551
- List of file paths in the state.
552
- """
553
- files_dict = state.get("files", {})
554
- return list(files_dict.keys())
555
-
556
- def _filter_files_by_path(filenames: list[str], path: str | None) -> list[str]:
557
- """Filter filenames by path prefix.
558
-
559
- Args:
560
- filenames: List of file paths to filter.
561
- path: Optional path prefix to filter by.
302
+ tool_description = custom_description or WRITE_FILE_TOOL_DESCRIPTION
303
+
304
+ @tool(description=tool_description)
305
+ def write_file(
306
+ file_path: str,
307
+ content: str,
308
+ runtime: ToolRuntime[None, FilesystemState],
309
+ ) -> Command | str:
310
+ resolved_backend = _get_backend(backend, runtime)
311
+ file_path = _validate_path(file_path)
312
+ res: WriteResult = resolved_backend.write(file_path, content)
313
+ if res.error:
314
+ return res.error
315
+ # If backend returns state update, wrap into Command with ToolMessage
316
+ if res.files_update is not None:
317
+ return Command(update={
318
+ "files": res.files_update,
319
+ "messages": [
320
+ ToolMessage(
321
+ content=f"Updated file {res.path}",
322
+ tool_call_id=runtime.tool_call_id,
323
+ )
324
+ ],
325
+ })
326
+ return f"Updated file {res.path}"
562
327
 
563
- Returns:
564
- Filtered list of file paths matching the prefix.
565
- """
566
- if path is None:
567
- return filenames
568
- normalized_path = _validate_path(path)
569
- return [f for f in filenames if f.startswith(normalized_path)]
570
-
571
- if long_term_memory:
572
-
573
- @tool(description=tool_description)
574
- def ls(runtime: ToolRuntime[None, FilesystemState], path: str | None = None) -> list[str]:
575
- files = _get_filenames_from_state(runtime.state)
576
- # Add filenames from longterm memory
577
- store = _get_store(runtime)
578
- namespace = _get_namespace()
579
- longterm_files = store.search(namespace)
580
- longterm_files_prefixed = [_append_memories_prefix(f.key) for f in longterm_files]
581
- files.extend(longterm_files_prefixed)
582
- return _filter_files_by_path(files, path)
583
- else:
584
-
585
- @tool(description=tool_description)
586
- def ls(runtime: ToolRuntime[None, FilesystemState], path: str | None = None) -> list[str]:
587
- files = _get_filenames_from_state(runtime.state)
588
- return _filter_files_by_path(files, path)
589
-
590
- return ls
328
+ return write_file
591
329
 
592
330
 
593
- def _read_file_tool_generator(custom_description: str | None = None, *, long_term_memory: bool) -> BaseTool:
594
- """Generate the read_file tool.
331
+ def _edit_file_tool_generator(
332
+ backend: BackendProtocol | Callable[[ToolRuntime], BackendProtocol],
333
+ custom_description: str | None = None,
334
+ ) -> BaseTool:
335
+ """Generate the edit_file tool.
595
336
 
596
337
  Args:
338
+ backend: Backend to use for file storage, or a factory function that takes runtime and returns a backend.
597
339
  custom_description: Optional custom description for the tool.
598
- long_term_memory: Whether to enable longterm memory support.
599
340
 
600
341
  Returns:
601
- Configured read_file tool that reads files from state and optionally from longterm store.
342
+ Configured edit_file tool that performs string replacements in files using the backend.
602
343
  """
603
- tool_description = READ_FILE_TOOL_DESCRIPTION
604
- if custom_description:
605
- tool_description = custom_description
606
- elif long_term_memory:
607
- tool_description += READ_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT
344
+ tool_description = custom_description or EDIT_FILE_TOOL_DESCRIPTION
608
345
 
609
- def _read_file_data_content(file_data: FileData, offset: int, limit: int) -> str:
610
- """Read and format file content with line numbers.
611
-
612
- Args:
613
- file_data: The file data to read.
614
- offset: Line offset to start reading from (0-indexed).
615
- limit: Maximum number of lines to read.
616
-
617
- Returns:
618
- Formatted file content with line numbers, or an error message.
619
- """
620
- content = _file_data_to_string(file_data)
621
- empty_msg = _check_empty_content(content)
622
- if empty_msg:
623
- return empty_msg
624
- lines = content.splitlines()
625
- start_idx = offset
626
- end_idx = min(start_idx + limit, len(lines))
627
- if start_idx >= len(lines):
628
- return f"Error: Line offset {offset} exceeds file length ({len(lines)} lines)"
629
- selected_lines = lines[start_idx:end_idx]
630
- return _format_content_with_line_numbers(selected_lines, format_style="tab", start_line=start_idx + 1)
631
-
632
- if long_term_memory:
633
-
634
- @tool(description=tool_description)
635
- def read_file(
636
- file_path: str,
637
- runtime: ToolRuntime[None, FilesystemState],
638
- offset: int = DEFAULT_READ_OFFSET,
639
- limit: int = DEFAULT_READ_LIMIT,
640
- ) -> str:
641
- file_path = _validate_path(file_path)
642
- if _has_memories_prefix(file_path):
643
- stripped_file_path = _strip_memories_prefix(file_path)
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(runtime.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
- runtime: ToolRuntime[None, FilesystemState],
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(runtime.state, file_path)
669
- except ValueError as e:
670
- return str(e)
671
- return _read_file_data_content(file_data, offset, limit)
346
+ @tool(description=tool_description)
347
+ def edit_file(
348
+ file_path: str,
349
+ old_string: str,
350
+ new_string: str,
351
+ runtime: ToolRuntime[None, FilesystemState],
352
+ *,
353
+ replace_all: bool = False,
354
+ ) -> Command | str:
355
+ resolved_backend = _get_backend(backend, runtime)
356
+ file_path = _validate_path(file_path)
357
+ res: EditResult = resolved_backend.edit(file_path, old_string, new_string, replace_all=replace_all)
358
+ if res.error:
359
+ return res.error
360
+ if res.files_update is not None:
361
+ return Command(update={
362
+ "files": res.files_update,
363
+ "messages": [
364
+ ToolMessage(
365
+ content=f"Successfully replaced {res.occurrences} instance(s) of the string in '{res.path}'",
366
+ tool_call_id=runtime.tool_call_id,
367
+ )
368
+ ],
369
+ })
370
+ return f"Successfully replaced {res.occurrences} instance(s) of the string in '{res.path}'"
672
371
 
673
- return read_file
372
+ return edit_file
674
373
 
675
374
 
676
- def _write_file_tool_generator(custom_description: str | None = None, *, long_term_memory: bool) -> BaseTool:
677
- """Generate the write_file tool.
375
+ def _glob_tool_generator(
376
+ backend: BackendProtocol | Callable[[ToolRuntime], BackendProtocol],
377
+ custom_description: str | None = None,
378
+ ) -> BaseTool:
379
+ """Generate the glob tool.
678
380
 
679
381
  Args:
382
+ backend: Backend to use for file storage, or a factory function that takes runtime and returns a backend.
680
383
  custom_description: Optional custom description for the tool.
681
- long_term_memory: Whether to enable longterm memory support.
682
384
 
683
385
  Returns:
684
- Configured write_file tool that creates new files in state or longterm store.
386
+ Configured glob tool that finds files by pattern using the backend.
685
387
  """
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
388
+ tool_description = custom_description or GLOB_TOOL_DESCRIPTION
691
389
 
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.
390
+ @tool(description=tool_description)
391
+ def glob(pattern: str, runtime: ToolRuntime[None, FilesystemState], path: str = "/") -> list[str]:
392
+ resolved_backend = _get_backend(backend, runtime)
393
+ infos = resolved_backend.glob_info(pattern, path=path)
394
+ return [fi.get("path", "") for fi in infos]
694
395
 
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.
396
+ return glob
700
397
 
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
- runtime: ToolRuntime[None, FilesystemState],
723
- ) -> Command | str:
724
- file_path = _validate_path(file_path)
725
- if not runtime.tool_call_id:
726
- value_error_msg = "Tool call ID is required for write_file invocation"
727
- raise ValueError(value_error_msg)
728
- if _has_memories_prefix(file_path):
729
- stripped_file_path = _strip_memories_prefix(file_path)
730
- store = _get_store(runtime)
731
- namespace = _get_namespace()
732
- if store.get(namespace, stripped_file_path) is not None:
733
- return f"Cannot write to {file_path} because it already exists. Read and then make an edit, or write to a new path."
734
- new_file_data = _create_file_data(content)
735
- store.put(namespace, stripped_file_path, _convert_file_data_to_store_item(new_file_data))
736
- return f"Updated longterm memories file {file_path}"
737
- return _write_file_to_state(runtime.state, runtime.tool_call_id, file_path, content)
738
-
739
- else:
740
-
741
- @tool(description=tool_description)
742
- def write_file(
743
- file_path: str,
744
- content: str,
745
- runtime: ToolRuntime[None, FilesystemState],
746
- ) -> Command | str:
747
- file_path = _validate_path(file_path)
748
- if not runtime.tool_call_id:
749
- value_error_msg = "Tool call ID is required for write_file invocation"
750
- raise ValueError(value_error_msg)
751
- return _write_file_to_state(runtime.state, runtime.tool_call_id, file_path, content)
752
-
753
- return write_file
754
398
 
755
-
756
- def _edit_file_tool_generator(custom_description: str | None = None, *, long_term_memory: bool) -> BaseTool:
757
- """Generate the edit_file tool.
399
+ def _grep_tool_generator(
400
+ backend: BackendProtocol | Callable[[ToolRuntime], BackendProtocol],
401
+ custom_description: str | None = None,
402
+ ) -> BaseTool:
403
+ """Generate the grep tool.
758
404
 
759
405
  Args:
406
+ backend: Backend to use for file storage, or a factory function that takes runtime and returns a backend.
760
407
  custom_description: Optional custom description for the tool.
761
- long_term_memory: Whether to enable longterm memory support.
762
408
 
763
409
  Returns:
764
- Configured edit_file tool that performs string replacements in files.
410
+ Configured grep tool that searches for patterns in files using the backend.
765
411
  """
766
- tool_description = EDIT_FILE_TOOL_DESCRIPTION
767
- if custom_description:
768
- tool_description = custom_description
769
- elif long_term_memory:
770
- tool_description += EDIT_FILE_TOOL_DESCRIPTION_LONGTERM_SUPPLEMENT
771
-
772
- def _perform_file_edit(
773
- file_data: FileData,
774
- old_string: str,
775
- new_string: str,
776
- *,
777
- replace_all: bool = False,
778
- ) -> tuple[FileData, str] | str:
779
- """Perform string replacement on file data.
780
-
781
- Args:
782
- file_data: The file data to edit.
783
- old_string: String to find and replace.
784
- new_string: Replacement string.
785
- replace_all: If True, replace all occurrences.
786
-
787
- Returns:
788
- Tuple of (updated_file_data, success_message) on success,
789
- or error string on failure.
790
- """
791
- content = _file_data_to_string(file_data)
792
- occurrences = content.count(old_string)
793
- if occurrences == 0:
794
- return f"Error: String not found in file: '{old_string}'"
795
- if occurrences > 1 and not replace_all:
796
- 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."
797
- new_content = content.replace(old_string, new_string)
798
- new_file_data = _update_file_data(file_data, new_content)
799
- result_msg = f"Successfully replaced {occurrences} instance(s) of the string"
800
- return new_file_data, result_msg
801
-
802
- if long_term_memory:
803
-
804
- @tool(description=tool_description)
805
- def edit_file(
806
- file_path: str,
807
- old_string: str,
808
- new_string: str,
809
- runtime: ToolRuntime[None, FilesystemState],
810
- *,
811
- replace_all: bool = False,
812
- ) -> Command | str:
813
- file_path = _validate_path(file_path)
814
- is_longterm_memory = _has_memories_prefix(file_path)
815
-
816
- # Retrieve file data from appropriate storage
817
- if is_longterm_memory:
818
- stripped_file_path = _strip_memories_prefix(file_path)
819
- store = _get_store(runtime)
820
- namespace = _get_namespace()
821
- item: Item | None = store.get(namespace, stripped_file_path)
822
- if item is None:
823
- return f"Error: File '{file_path}' not found"
824
- file_data = _convert_store_item_to_file_data(item)
825
- else:
826
- try:
827
- file_data = _get_file_data_from_state(runtime.state, file_path)
828
- except ValueError as e:
829
- return str(e)
830
-
831
- # Perform the edit
832
- result = _perform_file_edit(file_data, old_string, new_string, replace_all=replace_all)
833
- if isinstance(result, str): # Error message
834
- return result
835
-
836
- new_file_data, result_msg = result
837
- full_msg = f"{result_msg} in '{file_path}'"
838
-
839
- # Save to appropriate storage
840
- if is_longterm_memory:
841
- store.put(namespace, stripped_file_path, _convert_file_data_to_store_item(new_file_data))
842
- return full_msg
843
-
844
- return Command(
845
- update={
846
- "files": {file_path: new_file_data},
847
- "messages": [ToolMessage(full_msg, tool_call_id=runtime.tool_call_id)],
848
- }
849
- )
850
- else:
851
-
852
- @tool(description=tool_description)
853
- def edit_file(
854
- file_path: str,
855
- old_string: str,
856
- new_string: str,
857
- runtime: ToolRuntime[None, FilesystemState],
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(runtime.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=runtime.tool_call_id)],
881
- }
882
- )
883
-
884
- return edit_file
412
+ tool_description = custom_description or GREP_TOOL_DESCRIPTION
413
+
414
+ @tool(description=tool_description)
415
+ def grep(
416
+ pattern: str,
417
+ runtime: ToolRuntime[None, FilesystemState],
418
+ path: Optional[str] = None,
419
+ glob: str | None = None,
420
+ output_mode: Literal["files_with_matches", "content", "count"] = "files_with_matches",
421
+ ) -> str:
422
+ resolved_backend = _get_backend(backend, runtime)
423
+ raw = resolved_backend.grep_raw(pattern, path=path, glob=glob)
424
+ if isinstance(raw, str):
425
+ return raw
426
+ formatted = format_grep_matches(raw, output_mode)
427
+ return truncate_if_too_long(formatted) # type: ignore[arg-type]
428
+
429
+ return grep
885
430
 
886
431
 
887
432
  TOOL_GENERATORS = {
@@ -889,24 +434,29 @@ TOOL_GENERATORS = {
889
434
  "read_file": _read_file_tool_generator,
890
435
  "write_file": _write_file_tool_generator,
891
436
  "edit_file": _edit_file_tool_generator,
437
+ "glob": _glob_tool_generator,
438
+ "grep": _grep_tool_generator,
892
439
  }
893
440
 
894
441
 
895
- def _get_filesystem_tools(custom_tool_descriptions: dict[str, str] | None = None, *, long_term_memory: bool) -> list[BaseTool]:
442
+ def _get_filesystem_tools(
443
+ backend: BackendProtocol,
444
+ custom_tool_descriptions: dict[str, str] | None = None,
445
+ ) -> list[BaseTool]:
896
446
  """Get filesystem tools.
897
447
 
898
448
  Args:
449
+ backend: Backend to use for file storage, or a factory function that takes runtime and returns a backend.
899
450
  custom_tool_descriptions: Optional custom descriptions for tools.
900
- long_term_memory: Whether to enable longterm memory support.
901
451
 
902
452
  Returns:
903
- List of configured filesystem tools (ls, read_file, write_file, edit_file).
453
+ List of configured filesystem tools (ls, read_file, write_file, edit_file, glob, grep).
904
454
  """
905
455
  if custom_tool_descriptions is None:
906
456
  custom_tool_descriptions = {}
907
457
  tools = []
908
458
  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)
459
+ tool = tool_generator(backend, custom_tool_descriptions.get(tool_name))
910
460
  tools.append(tool)
911
461
  return tools
912
462
 
@@ -924,29 +474,33 @@ Here are the first 10 lines of the result:
924
474
  class FilesystemMiddleware(AgentMiddleware):
925
475
  """Middleware for providing filesystem tools to an agent.
926
476
 
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)
477
+ This middleware adds six filesystem tools to the agent: ls, read_file, write_file,
478
+ edit_file, glob, and grep. Files can be stored using any backend that implements
479
+ the BackendProtocol.
931
480
 
932
481
  Args:
933
- long_term_memory: Whether to enable longterm memory support.
934
- system_prompt_extension: Optional custom system prompt override.
482
+ backend: Backend for file storage. If not provided, defaults to StateBackend
483
+ (ephemeral storage in agent state). For persistent storage or hybrid setups,
484
+ use CompositeBackend with custom routes.
485
+ system_prompt: Optional custom system prompt override.
935
486
  custom_tool_descriptions: Optional custom tool descriptions override.
936
-
937
- Raises:
938
- ValueError: If longterm memory is enabled but no store is available.
487
+ tool_token_limit_before_evict: Optional token limit before evicting a tool result to the filesystem.
939
488
 
940
489
  Example:
941
490
  ```python
942
- from langchain.agents.middleware.filesystem import FilesystemMiddleware
491
+ from deepagents.middleware.filesystem import FilesystemMiddleware
492
+ from deepagents.memory.backends import StateBackend, StoreBackend, CompositeBackend
943
493
  from langchain.agents import create_agent
944
494
 
945
- # Short-term memory only
946
- agent = create_agent(middleware=[FilesystemMiddleware(long_term_memory=False)])
495
+ # Ephemeral storage only (default)
496
+ agent = create_agent(middleware=[FilesystemMiddleware()])
947
497
 
948
- # With long-term memory
949
- agent = create_agent(middleware=[FilesystemMiddleware(long_term_memory=True)])
498
+ # With hybrid storage (ephemeral + persistent /memories/)
499
+ backend = CompositeBackend(
500
+ default=StateBackend(),
501
+ routes={"/memories/": StoreBackend()}
502
+ )
503
+ agent = create_agent(middleware=[FilesystemMiddleware(memory_backend=backend)])
950
504
  ```
951
505
  """
952
506
 
@@ -955,7 +509,7 @@ class FilesystemMiddleware(AgentMiddleware):
955
509
  def __init__(
956
510
  self,
957
511
  *,
958
- long_term_memory: bool = False,
512
+ backend: BACKEND_TYPES | None = None,
959
513
  system_prompt: str | None = None,
960
514
  custom_tool_descriptions: dict[str, str] | None = None,
961
515
  tool_token_limit_before_evict: int | None = 20000,
@@ -963,38 +517,20 @@ class FilesystemMiddleware(AgentMiddleware):
963
517
  """Initialize the filesystem middleware.
964
518
 
965
519
  Args:
966
- long_term_memory: Whether to enable longterm memory support.
520
+ backend: Backend for file storage, or a factory callable. Defaults to StateBackend if not provided.
967
521
  system_prompt: Optional custom system prompt override.
968
522
  custom_tool_descriptions: Optional custom tool descriptions override.
969
523
  tool_token_limit_before_evict: Optional token limit before evicting a tool result to the filesystem.
970
524
  """
971
- self.long_term_memory = long_term_memory
972
525
  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
526
 
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.
527
+ # Use provided backend or default to StateBackend factory
528
+ self.backend = backend if backend is not None else (lambda rt: StateBackend(rt))
983
529
 
984
- Args:
985
- state: The state of the agent.
986
- runtime: The LangGraph runtime.
987
-
988
- Returns:
989
- The unmodified model request.
530
+ # Set system prompt (allow full override)
531
+ self.system_prompt = system_prompt if system_prompt is not None else FILESYSTEM_SYSTEM_PROMPT
990
532
 
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
533
+ self.tools = _get_filesystem_tools(self.backend, custom_tool_descriptions)
998
534
 
999
535
  def wrap_model_call(
1000
536
  self,
@@ -1037,14 +573,14 @@ class FilesystemMiddleware(AgentMiddleware):
1037
573
  content = tool_result.content
1038
574
  if self.tool_token_limit_before_evict and len(content) > 4 * self.tool_token_limit_before_evict:
1039
575
  file_path = f"/large_tool_results/{tool_result.tool_call_id}"
1040
- file_data = _create_file_data(content)
576
+ file_data = create_file_data(content)
1041
577
  state_update = {
1042
578
  "messages": [
1043
579
  ToolMessage(
1044
580
  TOO_LARGE_TOOL_MSG.format(
1045
581
  tool_call_id=tool_result.tool_call_id,
1046
582
  file_path=file_path,
1047
- content_sample=_format_content_with_line_numbers(file_data["content"][:10], format_style="tab", start_line=1),
583
+ content_sample=format_content_with_line_numbers(file_data["content"][:10], start_line=1),
1048
584
  ),
1049
585
  tool_call_id=tool_result.tool_call_id,
1050
586
  )
@@ -1065,13 +601,13 @@ class FilesystemMiddleware(AgentMiddleware):
1065
601
  content = message.content
1066
602
  if len(content) > 4 * self.tool_token_limit_before_evict:
1067
603
  file_path = f"/large_tool_results/{message.tool_call_id}"
1068
- file_data = _create_file_data(content)
604
+ file_data = create_file_data(content)
1069
605
  edited_message_updates.append(
1070
606
  ToolMessage(
1071
607
  TOO_LARGE_TOOL_MSG.format(
1072
608
  tool_call_id=message.tool_call_id,
1073
609
  file_path=file_path,
1074
- content_sample=_format_content_with_line_numbers(file_data["content"][:10], format_style="tab", start_line=1),
610
+ content_sample=format_content_with_line_numbers(file_data["content"][:10], start_line=1),
1075
611
  ),
1076
612
  tool_call_id=message.tool_call_id,
1077
613
  )
@@ -1096,7 +632,6 @@ class FilesystemMiddleware(AgentMiddleware):
1096
632
  Returns:
1097
633
  The raw ToolMessage, or a pseudo tool message with the ToolResult in state.
1098
634
  """
1099
- # If no token limit specified, or if it is a filesystem tool, do not evict
1100
635
  if self.tool_token_limit_before_evict is None or request.tool_call["name"] in TOOL_GENERATORS:
1101
636
  return handler(request)
1102
637
 
@@ -1117,9 +652,15 @@ class FilesystemMiddleware(AgentMiddleware):
1117
652
  Returns:
1118
653
  The raw ToolMessage, or a pseudo tool message with the ToolResult in state.
1119
654
  """
1120
- # If no token limit specified, or if it is a filesystem tool, do not evict
1121
655
  if self.tool_token_limit_before_evict is None or request.tool_call["name"] in TOOL_GENERATORS:
1122
656
  return await handler(request)
1123
657
 
1124
658
  tool_result = await handler(request)
1125
659
  return self._intercept_large_tool_result(tool_result)
660
+
661
+ # Back-compat aliases expected by some tests
662
+ def _create_file_data(content: str):
663
+ return create_file_data(content)
664
+
665
+ def _update_file_data(file_data: dict, content: str):
666
+ return update_file_data(file_data, content)