deepagents 0.1.5rc2__py3-none-any.whl → 0.2.1__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/backends/__init__.py +1 -2
- deepagents/backends/composite.py +16 -27
- deepagents/backends/filesystem.py +54 -29
- deepagents/backends/state.py +34 -5
- deepagents/backends/store.py +35 -6
- deepagents/backends/utils.py +44 -17
- deepagents/middleware/filesystem.py +84 -53
- deepagents/middleware/patch_tool_calls.py +3 -3
- deepagents/middleware/subagents.py +0 -1
- {deepagents-0.1.5rc2.dist-info → deepagents-0.2.1.dist-info}/METADATA +6 -42
- deepagents-0.2.1.dist-info/RECORD +18 -0
- deepagents-0.1.5rc2.dist-info/RECORD +0 -18
- {deepagents-0.1.5rc2.dist-info → deepagents-0.2.1.dist-info}/WHEEL +0 -0
- {deepagents-0.1.5rc2.dist-info → deepagents-0.2.1.dist-info}/licenses/LICENSE +0 -0
- {deepagents-0.1.5rc2.dist-info → deepagents-0.2.1.dist-info}/top_level.txt +0 -0
deepagents/backends/__init__.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""Memory backends for pluggable file storage."""
|
|
2
2
|
|
|
3
|
-
from deepagents.backends.composite import CompositeBackend
|
|
3
|
+
from deepagents.backends.composite import CompositeBackend
|
|
4
4
|
from deepagents.backends.filesystem import FilesystemBackend
|
|
5
5
|
from deepagents.backends.state import StateBackend
|
|
6
6
|
from deepagents.backends.store import StoreBackend
|
|
@@ -9,7 +9,6 @@ from deepagents.backends.protocol import BackendProtocol
|
|
|
9
9
|
__all__ = [
|
|
10
10
|
"BackendProtocol",
|
|
11
11
|
"CompositeBackend",
|
|
12
|
-
"build_composite_state_backend",
|
|
13
12
|
"FilesystemBackend",
|
|
14
13
|
"StateBackend",
|
|
15
14
|
"StoreBackend",
|
deepagents/backends/composite.py
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
"""CompositeBackend: Route operations to different backends based on path prefix."""
|
|
2
2
|
|
|
3
|
-
from typing import
|
|
3
|
+
from typing import Optional
|
|
4
4
|
|
|
5
|
-
from
|
|
6
|
-
|
|
7
|
-
from deepagents.backends.protocol import BackendProtocol, BackendFactory, WriteResult, EditResult
|
|
5
|
+
from deepagents.backends.protocol import BackendProtocol, WriteResult, EditResult
|
|
8
6
|
from deepagents.backends.state import StateBackend
|
|
9
7
|
from deepagents.backends.utils import FileInfo, GrepMatch
|
|
10
|
-
from deepagents.backends.protocol import BackendFactory
|
|
11
8
|
|
|
12
9
|
|
|
13
10
|
class CompositeBackend:
|
|
@@ -48,13 +45,14 @@ class CompositeBackend:
|
|
|
48
45
|
return self.default, key
|
|
49
46
|
|
|
50
47
|
def ls_info(self, path: str) -> list[FileInfo]:
|
|
51
|
-
"""List files
|
|
52
|
-
|
|
48
|
+
"""List files and directories in the specified directory (non-recursive).
|
|
49
|
+
|
|
53
50
|
Args:
|
|
54
51
|
path: Absolute path to directory.
|
|
55
|
-
|
|
52
|
+
|
|
56
53
|
Returns:
|
|
57
|
-
List of FileInfo-like dicts with route prefixes added.
|
|
54
|
+
List of FileInfo-like dicts with route prefixes added, for files and directories directly in the directory.
|
|
55
|
+
Directories have a trailing / in their path and is_dir=True.
|
|
58
56
|
"""
|
|
59
57
|
# Check if path matches a specific route
|
|
60
58
|
for route_prefix, backend in self.sorted_routes:
|
|
@@ -75,11 +73,14 @@ class CompositeBackend:
|
|
|
75
73
|
results: list[FileInfo] = []
|
|
76
74
|
results.extend(self.default.ls_info(path))
|
|
77
75
|
for route_prefix, backend in self.sorted_routes:
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
76
|
+
# Add the route itself as a directory (e.g., /memories/)
|
|
77
|
+
results.append({
|
|
78
|
+
"path": route_prefix,
|
|
79
|
+
"is_dir": True,
|
|
80
|
+
"size": 0,
|
|
81
|
+
"modified_at": "",
|
|
82
|
+
})
|
|
83
|
+
|
|
83
84
|
results.sort(key=lambda x: x.get("path", ""))
|
|
84
85
|
return results
|
|
85
86
|
|
|
@@ -220,16 +221,4 @@ class CompositeBackend:
|
|
|
220
221
|
return res
|
|
221
222
|
|
|
222
223
|
|
|
223
|
-
|
|
224
|
-
runtime: ToolRuntime,
|
|
225
|
-
*,
|
|
226
|
-
routes: dict[str, BackendProtocol | BackendFactory],
|
|
227
|
-
) -> BackendProtocol:
|
|
228
|
-
built_routes: dict[str, BackendProtocol] = {}
|
|
229
|
-
for k, v in routes.items():
|
|
230
|
-
if isinstance(v, BackendProtocol):
|
|
231
|
-
built_routes[k] = v
|
|
232
|
-
else:
|
|
233
|
-
built_routes[k] = v(runtime)
|
|
234
|
-
default_state = StateBackend(runtime)
|
|
235
|
-
return CompositeBackend(default=default_state, routes=built_routes)
|
|
224
|
+
|
|
@@ -13,16 +13,12 @@ import json
|
|
|
13
13
|
import subprocess
|
|
14
14
|
from datetime import datetime
|
|
15
15
|
from pathlib import Path
|
|
16
|
-
from typing import
|
|
17
|
-
|
|
18
|
-
if TYPE_CHECKING:
|
|
19
|
-
from langchain.tools import ToolRuntime
|
|
16
|
+
from typing import Optional
|
|
20
17
|
|
|
21
18
|
from .utils import (
|
|
22
19
|
check_empty_content,
|
|
23
20
|
format_content_with_line_numbers,
|
|
24
21
|
perform_string_replacement,
|
|
25
|
-
truncate_if_too_long,
|
|
26
22
|
)
|
|
27
23
|
import wcmatch.glob as wcglob
|
|
28
24
|
from deepagents.backends.utils import FileInfo, GrepMatch
|
|
@@ -51,7 +47,7 @@ class FilesystemBackend:
|
|
|
51
47
|
all file paths will be resolved relative to this directory.
|
|
52
48
|
If not provided, uses the current working directory.
|
|
53
49
|
"""
|
|
54
|
-
self.cwd = Path(root_dir) if root_dir else Path.cwd()
|
|
50
|
+
self.cwd = Path(root_dir).resolve() if root_dir else Path.cwd()
|
|
55
51
|
self.virtual_mode = virtual_mode
|
|
56
52
|
self.max_file_size_bytes = max_file_size_mb * 1024 * 1024
|
|
57
53
|
|
|
@@ -77,7 +73,7 @@ class FilesystemBackend:
|
|
|
77
73
|
try:
|
|
78
74
|
full.relative_to(self.cwd)
|
|
79
75
|
except ValueError:
|
|
80
|
-
raise ValueError(f"Path outside root directory: {
|
|
76
|
+
raise ValueError(f"Path:{full} outside root directory: {self.cwd}") from None
|
|
81
77
|
return full
|
|
82
78
|
|
|
83
79
|
path = Path(key)
|
|
@@ -86,13 +82,14 @@ class FilesystemBackend:
|
|
|
86
82
|
return (self.cwd / path).resolve()
|
|
87
83
|
|
|
88
84
|
def ls_info(self, path: str) -> list[FileInfo]:
|
|
89
|
-
"""List files
|
|
85
|
+
"""List files and directories in the specified directory (non-recursive).
|
|
90
86
|
|
|
91
87
|
Args:
|
|
92
88
|
path: Absolute directory path to list files from.
|
|
93
|
-
|
|
89
|
+
|
|
94
90
|
Returns:
|
|
95
|
-
List of FileInfo-like dicts.
|
|
91
|
+
List of FileInfo-like dicts for files and directories directly in the directory.
|
|
92
|
+
Directories have a trailing / in their path and is_dir=True.
|
|
96
93
|
"""
|
|
97
94
|
dir_path = self._resolve_path(path)
|
|
98
95
|
if not dir_path.exists() or not dir_path.is_dir():
|
|
@@ -105,18 +102,22 @@ class FilesystemBackend:
|
|
|
105
102
|
if not cwd_str.endswith("/"):
|
|
106
103
|
cwd_str += "/"
|
|
107
104
|
|
|
108
|
-
#
|
|
105
|
+
# List only direct children (non-recursive)
|
|
109
106
|
try:
|
|
110
|
-
for
|
|
107
|
+
for child_path in dir_path.iterdir():
|
|
111
108
|
try:
|
|
112
|
-
is_file =
|
|
109
|
+
is_file = child_path.is_file()
|
|
110
|
+
is_dir = child_path.is_dir()
|
|
113
111
|
except OSError:
|
|
114
112
|
continue
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
113
|
+
|
|
114
|
+
abs_path = str(child_path)
|
|
115
|
+
|
|
116
|
+
if not self.virtual_mode:
|
|
117
|
+
# Non-virtual mode: use absolute paths
|
|
118
|
+
if is_file:
|
|
118
119
|
try:
|
|
119
|
-
st =
|
|
120
|
+
st = child_path.stat()
|
|
120
121
|
results.append({
|
|
121
122
|
"path": abs_path,
|
|
122
123
|
"is_dir": False,
|
|
@@ -125,8 +126,19 @@ class FilesystemBackend:
|
|
|
125
126
|
})
|
|
126
127
|
except OSError:
|
|
127
128
|
results.append({"path": abs_path, "is_dir": False})
|
|
128
|
-
|
|
129
|
-
|
|
129
|
+
elif is_dir:
|
|
130
|
+
try:
|
|
131
|
+
st = child_path.stat()
|
|
132
|
+
results.append({
|
|
133
|
+
"path": abs_path + "/",
|
|
134
|
+
"is_dir": True,
|
|
135
|
+
"size": 0,
|
|
136
|
+
"modified_at": datetime.fromtimestamp(st.st_mtime).isoformat(),
|
|
137
|
+
})
|
|
138
|
+
except OSError:
|
|
139
|
+
results.append({"path": abs_path + "/", "is_dir": True})
|
|
140
|
+
else:
|
|
141
|
+
# Virtual mode: strip cwd prefix
|
|
130
142
|
if abs_path.startswith(cwd_str):
|
|
131
143
|
relative_path = abs_path[len(cwd_str):]
|
|
132
144
|
elif abs_path.startswith(str(self.cwd)):
|
|
@@ -137,16 +149,29 @@ class FilesystemBackend:
|
|
|
137
149
|
relative_path = abs_path
|
|
138
150
|
|
|
139
151
|
virt_path = "/" + relative_path
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
152
|
+
|
|
153
|
+
if is_file:
|
|
154
|
+
try:
|
|
155
|
+
st = child_path.stat()
|
|
156
|
+
results.append({
|
|
157
|
+
"path": virt_path,
|
|
158
|
+
"is_dir": False,
|
|
159
|
+
"size": int(st.st_size),
|
|
160
|
+
"modified_at": datetime.fromtimestamp(st.st_mtime).isoformat(),
|
|
161
|
+
})
|
|
162
|
+
except OSError:
|
|
163
|
+
results.append({"path": virt_path, "is_dir": False})
|
|
164
|
+
elif is_dir:
|
|
165
|
+
try:
|
|
166
|
+
st = child_path.stat()
|
|
167
|
+
results.append({
|
|
168
|
+
"path": virt_path + "/",
|
|
169
|
+
"is_dir": True,
|
|
170
|
+
"size": 0,
|
|
171
|
+
"modified_at": datetime.fromtimestamp(st.st_mtime).isoformat(),
|
|
172
|
+
})
|
|
173
|
+
except OSError:
|
|
174
|
+
results.append({"path": virt_path + "/", "is_dir": True})
|
|
150
175
|
except (OSError, PermissionError):
|
|
151
176
|
pass
|
|
152
177
|
|
deepagents/backends/state.py
CHANGED
|
@@ -40,19 +40,38 @@ class StateBackend:
|
|
|
40
40
|
self.runtime = runtime
|
|
41
41
|
|
|
42
42
|
def ls_info(self, path: str) -> list[FileInfo]:
|
|
43
|
-
"""List files
|
|
44
|
-
|
|
43
|
+
"""List files and directories in the specified directory (non-recursive).
|
|
44
|
+
|
|
45
45
|
Args:
|
|
46
46
|
path: Absolute path to directory.
|
|
47
|
-
|
|
47
|
+
|
|
48
48
|
Returns:
|
|
49
|
-
List of FileInfo-like dicts.
|
|
49
|
+
List of FileInfo-like dicts for files and directories directly in the directory.
|
|
50
|
+
Directories have a trailing / in their path and is_dir=True.
|
|
50
51
|
"""
|
|
51
52
|
files = self.runtime.state.get("files", {})
|
|
52
53
|
infos: list[FileInfo] = []
|
|
54
|
+
subdirs: set[str] = set()
|
|
55
|
+
|
|
56
|
+
# Normalize path to have trailing slash for proper prefix matching
|
|
57
|
+
normalized_path = path if path.endswith("/") else path + "/"
|
|
58
|
+
|
|
53
59
|
for k, fd in files.items():
|
|
54
|
-
if
|
|
60
|
+
# Check if file is in the specified directory or a subdirectory
|
|
61
|
+
if not k.startswith(normalized_path):
|
|
62
|
+
continue
|
|
63
|
+
|
|
64
|
+
# Get the relative path after the directory
|
|
65
|
+
relative = k[len(normalized_path):]
|
|
66
|
+
|
|
67
|
+
# If relative path contains '/', it's in a subdirectory
|
|
68
|
+
if "/" in relative:
|
|
69
|
+
# Extract the immediate subdirectory name
|
|
70
|
+
subdir_name = relative.split("/")[0]
|
|
71
|
+
subdirs.add(normalized_path + subdir_name + "/")
|
|
55
72
|
continue
|
|
73
|
+
|
|
74
|
+
# This is a file directly in the current directory
|
|
56
75
|
size = len("\n".join(fd.get("content", [])))
|
|
57
76
|
infos.append({
|
|
58
77
|
"path": k,
|
|
@@ -60,6 +79,16 @@ class StateBackend:
|
|
|
60
79
|
"size": int(size),
|
|
61
80
|
"modified_at": fd.get("modified_at", ""),
|
|
62
81
|
})
|
|
82
|
+
|
|
83
|
+
# Add directories to the results
|
|
84
|
+
for subdir in sorted(subdirs):
|
|
85
|
+
infos.append({
|
|
86
|
+
"path": subdir,
|
|
87
|
+
"is_dir": True,
|
|
88
|
+
"size": 0,
|
|
89
|
+
"modified_at": "",
|
|
90
|
+
})
|
|
91
|
+
|
|
63
92
|
infos.sort(key=lambda x: x.get("path", ""))
|
|
64
93
|
return infos
|
|
65
94
|
|
deepagents/backends/store.py
CHANGED
|
@@ -179,24 +179,43 @@ class StoreBackend:
|
|
|
179
179
|
return all_items
|
|
180
180
|
|
|
181
181
|
def ls_info(self, path: str) -> list[FileInfo]:
|
|
182
|
-
"""List files
|
|
183
|
-
|
|
182
|
+
"""List files and directories in the specified directory (non-recursive).
|
|
183
|
+
|
|
184
184
|
Args:
|
|
185
185
|
path: Absolute path to directory.
|
|
186
|
-
|
|
186
|
+
|
|
187
187
|
Returns:
|
|
188
|
-
List of FileInfo-like dicts.
|
|
188
|
+
List of FileInfo-like dicts for files and directories directly in the directory.
|
|
189
|
+
Directories have a trailing / in their path and is_dir=True.
|
|
189
190
|
"""
|
|
190
191
|
store = self._get_store()
|
|
191
192
|
namespace = self._get_namespace()
|
|
192
|
-
|
|
193
|
+
|
|
193
194
|
# Retrieve all items and filter by path prefix locally to avoid
|
|
194
195
|
# coupling to store-specific filter semantics
|
|
195
196
|
items = self._search_store_paginated(store, namespace)
|
|
196
197
|
infos: list[FileInfo] = []
|
|
198
|
+
subdirs: set[str] = set()
|
|
199
|
+
|
|
200
|
+
# Normalize path to have trailing slash for proper prefix matching
|
|
201
|
+
normalized_path = path if path.endswith("/") else path + "/"
|
|
202
|
+
|
|
197
203
|
for item in items:
|
|
198
|
-
if
|
|
204
|
+
# Check if file is in the specified directory or a subdirectory
|
|
205
|
+
if not str(item.key).startswith(normalized_path):
|
|
199
206
|
continue
|
|
207
|
+
|
|
208
|
+
# Get the relative path after the directory
|
|
209
|
+
relative = str(item.key)[len(normalized_path):]
|
|
210
|
+
|
|
211
|
+
# If relative path contains '/', it's in a subdirectory
|
|
212
|
+
if "/" in relative:
|
|
213
|
+
# Extract the immediate subdirectory name
|
|
214
|
+
subdir_name = relative.split("/")[0]
|
|
215
|
+
subdirs.add(normalized_path + subdir_name + "/")
|
|
216
|
+
continue
|
|
217
|
+
|
|
218
|
+
# This is a file directly in the current directory
|
|
200
219
|
try:
|
|
201
220
|
fd = self._convert_store_item_to_file_data(item)
|
|
202
221
|
except ValueError:
|
|
@@ -208,6 +227,16 @@ class StoreBackend:
|
|
|
208
227
|
"size": int(size),
|
|
209
228
|
"modified_at": fd.get("modified_at", ""),
|
|
210
229
|
})
|
|
230
|
+
|
|
231
|
+
# Add directories to the results
|
|
232
|
+
for subdir in sorted(subdirs):
|
|
233
|
+
infos.append({
|
|
234
|
+
"path": subdir,
|
|
235
|
+
"is_dir": True,
|
|
236
|
+
"size": 0,
|
|
237
|
+
"modified_at": "",
|
|
238
|
+
})
|
|
239
|
+
|
|
211
240
|
infos.sort(key=lambda x: x.get("path", ""))
|
|
212
241
|
return infos
|
|
213
242
|
|
deepagents/backends/utils.py
CHANGED
|
@@ -12,7 +12,7 @@ from pathlib import Path
|
|
|
12
12
|
from typing import Any, Literal, TypedDict, List, Dict
|
|
13
13
|
|
|
14
14
|
EMPTY_CONTENT_WARNING = "System reminder: File exists but has empty contents"
|
|
15
|
-
MAX_LINE_LENGTH =
|
|
15
|
+
MAX_LINE_LENGTH = 10000
|
|
16
16
|
LINE_NUMBER_WIDTH = 6
|
|
17
17
|
TOOL_RESULT_TOKEN_LIMIT = 20000 # Same threshold as eviction
|
|
18
18
|
TRUNCATION_GUIDANCE = "... [results truncated, try being more specific with your parameters]"
|
|
@@ -37,18 +37,29 @@ class GrepMatch(TypedDict):
|
|
|
37
37
|
text: str
|
|
38
38
|
|
|
39
39
|
|
|
40
|
+
def sanitize_tool_call_id(tool_call_id: str) -> str:
|
|
41
|
+
"""Sanitize tool_call_id to prevent path traversal and separator issues.
|
|
42
|
+
|
|
43
|
+
Replaces dangerous characters (., /, \) with underscores.
|
|
44
|
+
"""
|
|
45
|
+
sanitized = tool_call_id.replace(".", "_").replace("/", "_").replace("\\", "_")
|
|
46
|
+
return sanitized
|
|
47
|
+
|
|
48
|
+
|
|
40
49
|
def format_content_with_line_numbers(
|
|
41
50
|
content: str | list[str],
|
|
42
51
|
start_line: int = 1,
|
|
43
52
|
) -> str:
|
|
44
53
|
"""Format file content with line numbers (cat -n style).
|
|
45
|
-
|
|
54
|
+
|
|
55
|
+
Chunks lines longer than MAX_LINE_LENGTH with continuation markers (e.g., 5.1, 5.2).
|
|
56
|
+
|
|
46
57
|
Args:
|
|
47
58
|
content: File content as string or list of lines
|
|
48
59
|
start_line: Starting line number (default: 1)
|
|
49
|
-
|
|
60
|
+
|
|
50
61
|
Returns:
|
|
51
|
-
Formatted content with line numbers
|
|
62
|
+
Formatted content with line numbers and continuation markers
|
|
52
63
|
"""
|
|
53
64
|
if isinstance(content, str):
|
|
54
65
|
lines = content.split("\n")
|
|
@@ -56,11 +67,29 @@ def format_content_with_line_numbers(
|
|
|
56
67
|
lines = lines[:-1]
|
|
57
68
|
else:
|
|
58
69
|
lines = content
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
70
|
+
|
|
71
|
+
result_lines = []
|
|
72
|
+
for i, line in enumerate(lines):
|
|
73
|
+
line_num = i + start_line
|
|
74
|
+
|
|
75
|
+
if len(line) <= MAX_LINE_LENGTH:
|
|
76
|
+
result_lines.append(f"{line_num:{LINE_NUMBER_WIDTH}d}\t{line}")
|
|
77
|
+
else:
|
|
78
|
+
# Split long line into chunks with continuation markers
|
|
79
|
+
num_chunks = (len(line) + MAX_LINE_LENGTH - 1) // MAX_LINE_LENGTH
|
|
80
|
+
for chunk_idx in range(num_chunks):
|
|
81
|
+
start = chunk_idx * MAX_LINE_LENGTH
|
|
82
|
+
end = min(start + MAX_LINE_LENGTH, len(line))
|
|
83
|
+
chunk = line[start:end]
|
|
84
|
+
if chunk_idx == 0:
|
|
85
|
+
# First chunk: use normal line number
|
|
86
|
+
result_lines.append(f"{line_num:{LINE_NUMBER_WIDTH}d}\t{chunk}")
|
|
87
|
+
else:
|
|
88
|
+
# Continuation chunks: use decimal notation (e.g., 5.1, 5.2)
|
|
89
|
+
continuation_marker = f"{line_num}.{chunk_idx}"
|
|
90
|
+
result_lines.append(f"{continuation_marker:>{LINE_NUMBER_WIDTH}}\t{chunk}")
|
|
91
|
+
|
|
92
|
+
return "\n".join(result_lines)
|
|
64
93
|
|
|
65
94
|
|
|
66
95
|
def check_empty_content(content: str) -> str | None:
|
|
@@ -91,18 +120,17 @@ def file_data_to_string(file_data: dict[str, Any]) -> str:
|
|
|
91
120
|
|
|
92
121
|
def create_file_data(content: str, created_at: str | None = None) -> dict[str, Any]:
|
|
93
122
|
"""Create a FileData object with timestamps.
|
|
94
|
-
|
|
123
|
+
|
|
95
124
|
Args:
|
|
96
125
|
content: File content as string
|
|
97
126
|
created_at: Optional creation timestamp (ISO format)
|
|
98
|
-
|
|
127
|
+
|
|
99
128
|
Returns:
|
|
100
129
|
FileData dict with content and timestamps
|
|
101
130
|
"""
|
|
102
131
|
lines = content.split("\n") if isinstance(content, str) else content
|
|
103
|
-
lines = [line[i:i+MAX_LINE_LENGTH] for line in lines for i in range(0, len(line) or 1, MAX_LINE_LENGTH)]
|
|
104
132
|
now = datetime.now(UTC).isoformat()
|
|
105
|
-
|
|
133
|
+
|
|
106
134
|
return {
|
|
107
135
|
"content": lines,
|
|
108
136
|
"created_at": created_at or now,
|
|
@@ -112,18 +140,17 @@ def create_file_data(content: str, created_at: str | None = None) -> dict[str, A
|
|
|
112
140
|
|
|
113
141
|
def update_file_data(file_data: dict[str, Any], content: str) -> dict[str, Any]:
|
|
114
142
|
"""Update FileData with new content, preserving creation timestamp.
|
|
115
|
-
|
|
143
|
+
|
|
116
144
|
Args:
|
|
117
145
|
file_data: Existing FileData dict
|
|
118
146
|
content: New content as string
|
|
119
|
-
|
|
147
|
+
|
|
120
148
|
Returns:
|
|
121
149
|
Updated FileData dict
|
|
122
150
|
"""
|
|
123
151
|
lines = content.split("\n") if isinstance(content, str) else content
|
|
124
|
-
lines = [line[i:i+MAX_LINE_LENGTH] for line in lines for i in range(0, len(line) or 1, MAX_LINE_LENGTH)]
|
|
125
152
|
now = datetime.now(UTC).isoformat()
|
|
126
|
-
|
|
153
|
+
|
|
127
154
|
return {
|
|
128
155
|
"content": lines,
|
|
129
156
|
"created_at": file_data["created_at"],
|
|
@@ -24,11 +24,11 @@ from typing_extensions import TypedDict
|
|
|
24
24
|
from deepagents.backends.protocol import BackendProtocol, BackendFactory, WriteResult, EditResult
|
|
25
25
|
from deepagents.backends import StateBackend
|
|
26
26
|
from deepagents.backends.utils import (
|
|
27
|
-
create_file_data,
|
|
28
27
|
update_file_data,
|
|
29
28
|
format_content_with_line_numbers,
|
|
30
29
|
format_grep_matches,
|
|
31
30
|
truncate_if_too_long,
|
|
31
|
+
sanitize_tool_call_id,
|
|
32
32
|
)
|
|
33
33
|
|
|
34
34
|
EMPTY_CONTENT_WARNING = "System reminder: File exists but has empty contents"
|
|
@@ -227,6 +227,15 @@ All file paths must start with a /.
|
|
|
227
227
|
|
|
228
228
|
|
|
229
229
|
def _get_backend(backend: BACKEND_TYPES, runtime: ToolRuntime) -> BackendProtocol:
|
|
230
|
+
"""Get the resolved backend instance from backend or factory.
|
|
231
|
+
|
|
232
|
+
Args:
|
|
233
|
+
backend: Backend instance or factory function.
|
|
234
|
+
runtime: The tool runtime context.
|
|
235
|
+
|
|
236
|
+
Returns:
|
|
237
|
+
Resolved backend instance.
|
|
238
|
+
"""
|
|
230
239
|
if callable(backend):
|
|
231
240
|
return backend(runtime)
|
|
232
241
|
return backend
|
|
@@ -532,6 +541,19 @@ class FilesystemMiddleware(AgentMiddleware):
|
|
|
532
541
|
|
|
533
542
|
self.tools = _get_filesystem_tools(self.backend, custom_tool_descriptions)
|
|
534
543
|
|
|
544
|
+
def _get_backend(self, runtime: ToolRuntime) -> BackendProtocol:
|
|
545
|
+
"""Get the resolved backend instance from backend or factory.
|
|
546
|
+
|
|
547
|
+
Args:
|
|
548
|
+
runtime: The tool runtime context.
|
|
549
|
+
|
|
550
|
+
Returns:
|
|
551
|
+
Resolved backend instance.
|
|
552
|
+
"""
|
|
553
|
+
if callable(self.backend):
|
|
554
|
+
return self.backend(runtime)
|
|
555
|
+
return self.backend
|
|
556
|
+
|
|
535
557
|
def wrap_model_call(
|
|
536
558
|
self,
|
|
537
559
|
request: ModelRequest,
|
|
@@ -568,54 +590,70 @@ class FilesystemMiddleware(AgentMiddleware):
|
|
|
568
590
|
request.system_prompt = request.system_prompt + "\n\n" + self.system_prompt if request.system_prompt else self.system_prompt
|
|
569
591
|
return await handler(request)
|
|
570
592
|
|
|
571
|
-
def
|
|
593
|
+
def _process_large_message(
|
|
594
|
+
self,
|
|
595
|
+
message: ToolMessage,
|
|
596
|
+
resolved_backend: BackendProtocol,
|
|
597
|
+
) -> tuple[ToolMessage, dict[str, FileData] | None]:
|
|
598
|
+
content = message.content
|
|
599
|
+
if not isinstance(content, str) or len(content) <= 4 * self.tool_token_limit_before_evict:
|
|
600
|
+
return message, None
|
|
601
|
+
|
|
602
|
+
sanitized_id = sanitize_tool_call_id(message.tool_call_id)
|
|
603
|
+
file_path = f"/large_tool_results/{sanitized_id}"
|
|
604
|
+
result = resolved_backend.write(file_path, content)
|
|
605
|
+
if result.error:
|
|
606
|
+
return message, None
|
|
607
|
+
content_sample = format_content_with_line_numbers(content.splitlines()[:10], start_line=1)
|
|
608
|
+
processed_message = ToolMessage(
|
|
609
|
+
TOO_LARGE_TOOL_MSG.format(
|
|
610
|
+
tool_call_id=message.tool_call_id,
|
|
611
|
+
file_path=file_path,
|
|
612
|
+
content_sample=content_sample,
|
|
613
|
+
),
|
|
614
|
+
tool_call_id=message.tool_call_id,
|
|
615
|
+
)
|
|
616
|
+
return processed_message, result.files_update
|
|
617
|
+
|
|
618
|
+
def _intercept_large_tool_result(self, tool_result: ToolMessage | Command, runtime: ToolRuntime) -> ToolMessage | Command:
|
|
572
619
|
if isinstance(tool_result, ToolMessage) and isinstance(tool_result.content, str):
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
)
|
|
587
|
-
],
|
|
588
|
-
"files": {file_path: file_data},
|
|
589
|
-
}
|
|
590
|
-
return Command(update=state_update)
|
|
620
|
+
if not (self.tool_token_limit_before_evict and
|
|
621
|
+
len(tool_result.content) > 4 * self.tool_token_limit_before_evict):
|
|
622
|
+
return tool_result
|
|
623
|
+
resolved_backend = self._get_backend(runtime)
|
|
624
|
+
processed_message, files_update = self._process_large_message(
|
|
625
|
+
tool_result,
|
|
626
|
+
resolved_backend,
|
|
627
|
+
)
|
|
628
|
+
return (Command(update={
|
|
629
|
+
"files": files_update,
|
|
630
|
+
"messages": [processed_message],
|
|
631
|
+
}) if files_update is not None else processed_message)
|
|
632
|
+
|
|
591
633
|
elif isinstance(tool_result, Command):
|
|
592
634
|
update = tool_result.update
|
|
593
635
|
if update is None:
|
|
594
636
|
return tool_result
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
for message in
|
|
600
|
-
if self.tool_token_limit_before_evict and
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
file_updates[file_path] = file_data
|
|
616
|
-
continue
|
|
617
|
-
edited_message_updates.append(message)
|
|
618
|
-
return Command(update={**update, "messages": edited_message_updates, "files": file_updates})
|
|
637
|
+
command_messages = update.get("messages", [])
|
|
638
|
+
accumulated_file_updates = dict(update.get("files", {}))
|
|
639
|
+
resolved_backend = self._get_backend(runtime)
|
|
640
|
+
processed_messages = []
|
|
641
|
+
for message in command_messages:
|
|
642
|
+
if not (self.tool_token_limit_before_evict and
|
|
643
|
+
isinstance(message, ToolMessage) and
|
|
644
|
+
isinstance(message.content, str) and
|
|
645
|
+
len(message.content) > 4 * self.tool_token_limit_before_evict):
|
|
646
|
+
processed_messages.append(message)
|
|
647
|
+
continue
|
|
648
|
+
processed_message, files_update = self._process_large_message(
|
|
649
|
+
message,
|
|
650
|
+
resolved_backend,
|
|
651
|
+
)
|
|
652
|
+
processed_messages.append(processed_message)
|
|
653
|
+
if files_update is not None:
|
|
654
|
+
accumulated_file_updates.update(files_update)
|
|
655
|
+
return Command(update={**update, "messages": processed_messages, "files": accumulated_file_updates})
|
|
656
|
+
|
|
619
657
|
return tool_result
|
|
620
658
|
|
|
621
659
|
def wrap_tool_call(
|
|
@@ -636,7 +674,7 @@ class FilesystemMiddleware(AgentMiddleware):
|
|
|
636
674
|
return handler(request)
|
|
637
675
|
|
|
638
676
|
tool_result = handler(request)
|
|
639
|
-
return self._intercept_large_tool_result(tool_result)
|
|
677
|
+
return self._intercept_large_tool_result(tool_result, request.runtime)
|
|
640
678
|
|
|
641
679
|
async def awrap_tool_call(
|
|
642
680
|
self,
|
|
@@ -656,11 +694,4 @@ class FilesystemMiddleware(AgentMiddleware):
|
|
|
656
694
|
return await handler(request)
|
|
657
695
|
|
|
658
696
|
tool_result = await handler(request)
|
|
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)
|
|
697
|
+
return self._intercept_large_tool_result(tool_result, request.runtime)
|
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
from typing import Any
|
|
4
4
|
|
|
5
5
|
from langchain.agents.middleware import AgentMiddleware, AgentState
|
|
6
|
-
from langchain_core.messages import
|
|
7
|
-
from langgraph.graph.message import REMOVE_ALL_MESSAGES
|
|
6
|
+
from langchain_core.messages import ToolMessage
|
|
8
7
|
from langgraph.runtime import Runtime
|
|
8
|
+
from langgraph.types import Overwrite
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
class PatchToolCallsMiddleware(AgentMiddleware):
|
|
@@ -41,4 +41,4 @@ class PatchToolCallsMiddleware(AgentMiddleware):
|
|
|
41
41
|
)
|
|
42
42
|
)
|
|
43
43
|
|
|
44
|
-
return {"messages":
|
|
44
|
+
return {"messages": Overwrite(patched_messages)}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: deepagents
|
|
3
|
-
Version: 0.1
|
|
3
|
+
Version: 0.2.1
|
|
4
4
|
Summary: General purpose 'deep agent' with sub-agent spawning, todo list capabilities, and mock file system. Built on LangGraph.
|
|
5
5
|
License: MIT
|
|
6
6
|
Requires-Python: <4.0,>=3.11
|
|
7
7
|
Description-Content-Type: text/markdown
|
|
8
8
|
License-File: LICENSE
|
|
9
9
|
Requires-Dist: langchain-anthropic<2.0.0,>=1.0.0
|
|
10
|
-
Requires-Dist: langchain<2.0.0,>=1.0.
|
|
10
|
+
Requires-Dist: langchain<2.0.0,>=1.0.2
|
|
11
11
|
Requires-Dist: langchain-core<2.0.0,>=1.0.0
|
|
12
12
|
Requires-Dist: wcmatch
|
|
13
13
|
Provides-Extra: dev
|
|
@@ -28,7 +28,7 @@ a **planning tool**, **sub agents**, access to a **file system**, and a **detail
|
|
|
28
28
|
|
|
29
29
|
<img src="deep_agents.png" alt="deep agent" width="600"/>
|
|
30
30
|
|
|
31
|
-
`deepagents` is a Python package that implements these in a general purpose way so that you can easily create a Deep Agent for your application.
|
|
31
|
+
`deepagents` is a Python package that implements these in a general purpose way so that you can easily create a Deep Agent for your application. For a full overview and quickstart of `deepagents`, the best resource is our [docs](https://docs.langchain.com/oss/python/deepagents/overview).
|
|
32
32
|
|
|
33
33
|
**Acknowledgements: This project was primarily inspired by Claude Code, and initially was largely an attempt to see what made Claude Code general purpose, and make it even more so.**
|
|
34
34
|
|
|
@@ -107,7 +107,7 @@ in the same way you would any LangGraph agent.
|
|
|
107
107
|
|
|
108
108
|
**Context Management**
|
|
109
109
|
|
|
110
|
-
File system tools (`ls`, `read_file`, `write_file`, `edit_file`) allow agents to offload large context to memory, preventing context window overflow and enabling work with variable-length tool results.
|
|
110
|
+
File system tools (`ls`, `read_file`, `write_file`, `edit_file`, `glob`, `grep`) allow agents to offload large context to memory, preventing context window overflow and enabling work with variable-length tool results.
|
|
111
111
|
|
|
112
112
|
**Subagent Spawning**
|
|
113
113
|
|
|
@@ -314,33 +314,6 @@ agent = create_deep_agent(
|
|
|
314
314
|
)
|
|
315
315
|
```
|
|
316
316
|
|
|
317
|
-
### `backend`
|
|
318
|
-
Deep agents come with a local filesystem to offload memory to. By default, this filesystem is stored in state (ephemeral, transient to a single thread).
|
|
319
|
-
|
|
320
|
-
You can configure persistent long-term memory using a composite backend that routes a path prefix (for example, `/memories/`) to a persistent store.
|
|
321
|
-
|
|
322
|
-
```python
|
|
323
|
-
from deepagents import create_deep_agent
|
|
324
|
-
from deepagents.backends import build_composite_state_backend, StoreBackend
|
|
325
|
-
from langgraph.store.memory import InMemoryStore
|
|
326
|
-
|
|
327
|
-
store = InMemoryStore() # Or any other Store implementation
|
|
328
|
-
|
|
329
|
-
# Provide a backend factory to the agent/middleware.
|
|
330
|
-
# This builds a state-backed composite at runtime and routes /memories/ to StoreBackend.
|
|
331
|
-
backend_factory = lambda rt: build_composite_state_backend(
|
|
332
|
-
rt,
|
|
333
|
-
routes={
|
|
334
|
-
"/memories/": (lambda r: StoreBackend(r)),
|
|
335
|
-
},
|
|
336
|
-
)
|
|
337
|
-
|
|
338
|
-
agent = create_deep_agent(
|
|
339
|
-
backend=backend_factory,
|
|
340
|
-
store=store,
|
|
341
|
-
)
|
|
342
|
-
```
|
|
343
|
-
|
|
344
317
|
### `interrupt_on`
|
|
345
318
|
A common reality for agents is that some tool operations may be sensitive and require human approval before execution. Deep Agents supports human-in-the-loop workflows through LangGraph’s interrupt capabilities. You can configure which tools require approval using a checkpointer.
|
|
346
319
|
|
|
@@ -413,11 +386,7 @@ Context engineering is one of the main challenges in building effective agents.
|
|
|
413
386
|
```python
|
|
414
387
|
from langchain.agents import create_agent
|
|
415
388
|
from deepagents.middleware.filesystem import FilesystemMiddleware
|
|
416
|
-
|
|
417
|
-
StateBackend,
|
|
418
|
-
CompositeBackend,
|
|
419
|
-
StoreBackend,
|
|
420
|
-
)
|
|
389
|
+
|
|
421
390
|
|
|
422
391
|
# FilesystemMiddleware is included by default in create_deep_agent
|
|
423
392
|
# You can customize it if building a custom agent
|
|
@@ -425,12 +394,7 @@ agent = create_agent(
|
|
|
425
394
|
model="anthropic:claude-sonnet-4-20250514",
|
|
426
395
|
middleware=[
|
|
427
396
|
FilesystemMiddleware(
|
|
428
|
-
backend
|
|
429
|
-
# For persistent memory, use CompositeBackend:
|
|
430
|
-
# backend=CompositeBackend(
|
|
431
|
-
# default=lambda rt: StateBackend(rt)
|
|
432
|
-
# routes={"/memories/": lambda rt: StoreBackend(rt)}
|
|
433
|
-
# )
|
|
397
|
+
backend=..., # Optional: customize storage backend
|
|
434
398
|
system_prompt="Write to the filesystem when...", # Optional custom system prompt override
|
|
435
399
|
custom_tool_descriptions={
|
|
436
400
|
"ls": "Use the ls tool when...",
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
deepagents/__init__.py,sha256=9BVNn4lfF5N8l2KY8Ttxi82zO609I-fGqoSIF7DAxiU,342
|
|
2
|
+
deepagents/graph.py,sha256=6hXBwvQDwbUtiF8Tgwf1jbvRHwJGLEJq89fy-DpVez0,6106
|
|
3
|
+
deepagents/backends/__init__.py,sha256=qb2dt2axTQsh8BGqW2EDyJq8GazC8_z87MQYo_VXhBw,457
|
|
4
|
+
deepagents/backends/composite.py,sha256=kyW0H346s1XIxNkRHRbDF9SSR-zeigeEFO8bizn5iFg,8555
|
|
5
|
+
deepagents/backends/filesystem.py,sha256=U9Tmf8BDTqKbw-gQjJkJOYG1nXQo9FhxyfzN97W9Z_8,18470
|
|
6
|
+
deepagents/backends/protocol.py,sha256=fwqJa_Ec6F4BoNYz0bcPHL_fiKksxw2RoyA6x5wr7dc,4181
|
|
7
|
+
deepagents/backends/state.py,sha256=BxMNm1kDpxtgzIzpuF78h1NuYh9VIpXqnUbbETGe4Y4,6584
|
|
8
|
+
deepagents/backends/store.py,sha256=VsPSj6ayABPjkKiN6CcvOGm7YCWKuWP_ltJWvFJ1nF0,13358
|
|
9
|
+
deepagents/backends/utils.py,sha256=vQDMFMjf7pmfKqprpTlF7851FWmswZnMdLj-cezTsBk,14432
|
|
10
|
+
deepagents/middleware/__init__.py,sha256=J7372TNGR27OU4C3uuQMryHHpXOBjFV_4aEZ_AoQ6n0,284
|
|
11
|
+
deepagents/middleware/filesystem.py,sha256=jxtwma6xWE-EQeASs6rtnoiiLqb_HT4c-cxsLFquneE,27882
|
|
12
|
+
deepagents/middleware/patch_tool_calls.py,sha256=PdNhxPaQqwnFkhEAZEE2kEzadTNAOO3_iJRA30WqpGE,1981
|
|
13
|
+
deepagents/middleware/subagents.py,sha256=JxXwZvi41pBKKMguKlyVqwjCoydnZboWEgJGkWOCIY8,23503
|
|
14
|
+
deepagents-0.2.1.dist-info/licenses/LICENSE,sha256=c__BaxUCK69leo2yEKynf8lWndu8iwYwge1CbyqAe-E,1071
|
|
15
|
+
deepagents-0.2.1.dist-info/METADATA,sha256=kXdfjz1KdcYpKWWll_QpeRnFcYpZGnjNSuChET2dp48,18660
|
|
16
|
+
deepagents-0.2.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
17
|
+
deepagents-0.2.1.dist-info/top_level.txt,sha256=drAzchOzPNePwpb3_pbPuvLuayXkN7SNqeIKMBWJoAo,11
|
|
18
|
+
deepagents-0.2.1.dist-info/RECORD,,
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
deepagents/__init__.py,sha256=9BVNn4lfF5N8l2KY8Ttxi82zO609I-fGqoSIF7DAxiU,342
|
|
2
|
-
deepagents/graph.py,sha256=6hXBwvQDwbUtiF8Tgwf1jbvRHwJGLEJq89fy-DpVez0,6106
|
|
3
|
-
deepagents/backends/__init__.py,sha256=F2wSsZZcFeLDVb_thia6I7SviPuFmxhFHDn_VzfJ__U,525
|
|
4
|
-
deepagents/backends/composite.py,sha256=ZJBSXcfx07dk97buA013L7u0TK2EAj8aOITpLsvw8OQ,8985
|
|
5
|
-
deepagents/backends/filesystem.py,sha256=oq3oI9W5kfmEXItOqKIGHx_PTKxjQTDh8IqVuV5ZFzw,17067
|
|
6
|
-
deepagents/backends/protocol.py,sha256=fwqJa_Ec6F4BoNYz0bcPHL_fiKksxw2RoyA6x5wr7dc,4181
|
|
7
|
-
deepagents/backends/state.py,sha256=Mm2U5IcTDxvJMhG3K1HGODCW70Qseb2arjPe5JewnO0,5439
|
|
8
|
-
deepagents/backends/store.py,sha256=NuxQK5znhbR5uz1PH6BfiWu9mGHWSk6TcKgb5ZuoJaM,12209
|
|
9
|
-
deepagents/backends/utils.py,sha256=0VK5_NJbolZs4iPzB53I6kyFpDdjgxLYGl4ob8pKYyw,13347
|
|
10
|
-
deepagents/middleware/__init__.py,sha256=J7372TNGR27OU4C3uuQMryHHpXOBjFV_4aEZ_AoQ6n0,284
|
|
11
|
-
deepagents/middleware/filesystem.py,sha256=-h75Rvy6uJtDL8a-zTilNe8bMKLSxaUHsgtP1j7liqE,27120
|
|
12
|
-
deepagents/middleware/patch_tool_calls.py,sha256=Cu8rUpt1GjrYgfMvZG6wOowvnmFeYTCauOJhlltNPmo,2045
|
|
13
|
-
deepagents/middleware/subagents.py,sha256=hYW0cJV36fryu67S3H70VnM3SessiOz_vm3gSScC4a4,23535
|
|
14
|
-
deepagents-0.1.5rc2.dist-info/licenses/LICENSE,sha256=c__BaxUCK69leo2yEKynf8lWndu8iwYwge1CbyqAe-E,1071
|
|
15
|
-
deepagents-0.1.5rc2.dist-info/METADATA,sha256=ajSklItkJktz56K8zozEo3dZEJLakRMSETSugbFvXm8,19808
|
|
16
|
-
deepagents-0.1.5rc2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
17
|
-
deepagents-0.1.5rc2.dist-info/top_level.txt,sha256=drAzchOzPNePwpb3_pbPuvLuayXkN7SNqeIKMBWJoAo,11
|
|
18
|
-
deepagents-0.1.5rc2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|