-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathprotocol.ts
More file actions
224 lines (209 loc) · 7.01 KB
/
protocol.ts
File metadata and controls
224 lines (209 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* Protocol definition for pluggable memory backends.
*
* This module defines the BackendProtocol that all backend implementations
* must follow. Backends can store files in different locations (state, filesystem,
* database, etc.) and provide a uniform interface for file operations.
*/
import type { BaseStore } from "@langchain/langgraph-checkpoint";
/**
* Structured file listing info.
*
* Minimal contract used across backends. Only "path" is required.
* Other fields are best-effort and may be absent depending on backend.
*/
export interface FileInfo {
/** File path */
path: string;
/** Whether this is a directory */
is_dir?: boolean;
/** File size in bytes (approximate) */
size?: number;
/** ISO 8601 timestamp of last modification */
modified_at?: string;
}
/**
* Structured grep match entry.
*/
export interface GrepMatch {
/** File path where match was found */
path: string;
/** Line number (1-indexed) */
line: number;
/** The matching line text */
text: string;
}
/**
* File data structure used by backends.
*
* All file data is represented as objects with this structure:
*/
export interface FileData {
/** Lines of text content */
content: string[];
/** ISO format timestamp of creation */
created_at: string;
/** ISO format timestamp of last modification */
modified_at: string;
}
/**
* Result from backend write operations.
*
* Checkpoint backends populate filesUpdate with {file_path: file_data} for LangGraph state.
* External backends set filesUpdate to null (already persisted to disk/S3/database/etc).
*/
export interface WriteResult {
/** Error message on failure, undefined on success */
error?: string;
/** File path of written file, undefined on failure */
path?: string;
/**
* State update dict for checkpoint backends, null for external storage.
* Checkpoint backends populate this with {file_path: file_data} for LangGraph state.
* External backends set null (already persisted to disk/S3/database/etc).
*/
filesUpdate?: Record<string, FileData> | null;
}
/**
* Result from backend edit operations.
*
* Checkpoint backends populate filesUpdate with {file_path: file_data} for LangGraph state.
* External backends set filesUpdate to null (already persisted to disk/S3/database/etc).
*/
export interface EditResult {
/** Error message on failure, undefined on success */
error?: string;
/** File path of edited file, undefined on failure */
path?: string;
/**
* State update dict for checkpoint backends, null for external storage.
* Checkpoint backends populate this with {file_path: file_data} for LangGraph state.
* External backends set null (already persisted to disk/S3/database/etc).
*/
filesUpdate?: Record<string, FileData> | null;
/** Number of replacements made, undefined on failure */
occurrences?: number;
}
/**
* Protocol for pluggable memory backends (single, unified).
*
* Backends can store files in different locations (state, filesystem, database, etc.)
* and provide a uniform interface for file operations.
*
* All file data is represented as objects with the FileData structure.
*
* Methods can return either direct values or Promises, allowing both
* synchronous and asynchronous implementations.
*/
export interface BackendProtocol {
/**
* Structured listing with file metadata.
*
* Lists files and directories in the specified directory (non-recursive).
* Directories have a trailing / in their path and is_dir=true.
*
* @param path - Absolute path to directory
* @returns List of FileInfo objects for files and directories directly in the directory
*/
lsInfo(path: string): FileInfo[] | Promise<FileInfo[]>;
/**
* Read file content with line numbers or an error string.
*
* @param filePath - Absolute file path
* @param offset - Line offset to start reading from (0-indexed), default 0
* @param limit - Maximum number of lines to read, default 2000
* @returns Formatted file content with line numbers, or error message
*/
read(
filePath: string,
offset?: number,
limit?: number,
): string | Promise<string>;
/**
* Read file content as raw FileData.
*
* @param filePath - Absolute file path
* @returns Raw file content as FileData
*/
readRaw(filePath: string): FileData | Promise<FileData>;
/**
* Structured search results or error string for invalid input.
*
* Searches file contents for a regex pattern.
*
* @param pattern - Regex pattern to search for
* @param path - Base path to search from (default: null)
* @param glob - Optional glob pattern to filter files (e.g., "*.py")
* @returns List of GrepMatch objects or error string for invalid regex
*/
grepRaw(
pattern: string,
path?: string | null,
glob?: string | null,
): GrepMatch[] | string | Promise<GrepMatch[] | string>;
/**
* Structured glob matching returning FileInfo objects.
*
* @param pattern - Glob pattern (e.g., `*.py`, `**\/*.ts`)
* @param path - Base path to search from (default: "/")
* @returns List of FileInfo objects matching the pattern
*/
globInfo(pattern: string, path?: string): FileInfo[] | Promise<FileInfo[]>;
/**
* Create a new file.
*
* @param filePath - Absolute file path
* @param content - File content as string
* @returns WriteResult with error populated on failure
*/
write(filePath: string, content: string): WriteResult | Promise<WriteResult>;
/**
* Edit a file by replacing string occurrences.
*
* @param filePath - Absolute file path
* @param oldString - String to find and replace
* @param newString - Replacement string
* @param replaceAll - If true, replace all occurrences (default: false)
* @returns EditResult with error, path, filesUpdate, and occurrences
*/
edit(
filePath: string,
oldString: string,
newString: string,
replaceAll?: boolean,
): EditResult | Promise<EditResult>;
}
/**
* State and store container for backend initialization.
*
* This provides a clean interface for what backends need to access:
* - state: Current agent state (with files, messages, etc.)
* - store: Optional persistent store for cross-conversation data
*
* Different contexts build this differently:
* - Tools: Extract state via getCurrentTaskInput(config)
* - Middleware: Use request.state directly
*/
export interface StateAndStore {
/** Current agent state with files, messages, etc. */
state: unknown;
/** Optional BaseStore for persistent cross-conversation storage */
store?: BaseStore;
/** Optional assistant ID for per-assistant isolation in store */
assistantId?: string;
}
/**
* Factory function type for creating backend instances.
*
* Backends receive StateAndStore which contains the current state
* and optional store, extracted from the execution context.
*
* @example
* ```typescript
* // Using in middleware
* const middleware = createFilesystemMiddleware({
* backend: (stateAndStore) => new StateBackend(stateAndStore)
* });
* ```
*/
export type BackendFactory = (stateAndStore: StateAndStore) => BackendProtocol;