@cloudflare/think 0.7.3 → 0.8.0
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.
- package/README.md +420 -43
- package/dist/chat-sdk-CJwYlbp9.js +669 -0
- package/dist/chat-sdk-CJwYlbp9.js.map +1 -0
- package/dist/extensions/index.d.ts +2 -2
- package/dist/{hook-proxy-5248W-ea.js → hook-proxy-DHwqL8MV.js} +1 -1
- package/dist/{hook-proxy-5248W-ea.js.map → hook-proxy-DHwqL8MV.js.map} +1 -1
- package/dist/index-CYp8VDIe.d.ts +2202 -0
- package/dist/{index-BsB4GUsL.d.ts → index-DS00xJwR.d.ts} +2 -2
- package/dist/{manager-DCbSiSWT.d.ts → manager-C1zjft70.d.ts} +1 -1
- package/dist/messengers/index.d.ts +110 -0
- package/dist/messengers/index.js +2 -0
- package/dist/messengers/telegram.d.ts +62 -0
- package/dist/messengers/telegram.js +84 -0
- package/dist/messengers/telegram.js.map +1 -0
- package/dist/think.d.ts +73 -1160
- package/dist/think.js +2188 -164
- package/dist/think.js.map +1 -1
- package/dist/tools/extensions.d.ts +1 -1
- package/dist/tools/workspace.d.ts +35 -334
- package/dist/tools/workspace.js +283 -3
- package/dist/tools/workspace.js.map +1 -1
- package/dist/workflows.d.ts +78 -0
- package/dist/workflows.js +119 -0
- package/dist/workflows.js.map +1 -0
- package/dist/workspace-Bri0ANBX.d.ts +342 -0
- package/package.json +24 -7
|
@@ -1,337 +1,36 @@
|
|
|
1
|
-
import {
|
|
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
|
-
writeFile(path: string, content: string): Promise<void>;
|
|
32
|
-
}
|
|
33
|
-
interface ListOperations {
|
|
34
|
-
readDir(
|
|
35
|
-
dir: string,
|
|
36
|
-
opts?: {
|
|
37
|
-
limit?: number;
|
|
38
|
-
offset?: number;
|
|
39
|
-
}
|
|
40
|
-
): Promise<FileInfo[]> | FileInfo[];
|
|
41
|
-
}
|
|
42
|
-
interface FindOperations {
|
|
43
|
-
glob(pattern: string): Promise<FileInfo[]> | FileInfo[];
|
|
44
|
-
}
|
|
45
|
-
interface DeleteOperations {
|
|
46
|
-
rm(
|
|
47
|
-
path: string,
|
|
48
|
-
opts?: {
|
|
49
|
-
recursive?: boolean;
|
|
50
|
-
force?: boolean;
|
|
51
|
-
}
|
|
52
|
-
): Promise<void>;
|
|
53
|
-
}
|
|
54
|
-
interface GrepOperations {
|
|
55
|
-
glob(pattern: string): Promise<FileInfo[]> | FileInfo[];
|
|
56
|
-
readFile(path: string): Promise<string | null>;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Create a complete set of AI SDK tools backed by a workspace.
|
|
60
|
-
*
|
|
61
|
-
* Accepts either a concrete `Workspace` from `@cloudflare/shell` or any
|
|
62
|
-
* `WorkspaceLike` implementation — e.g. a proxy that forwards calls to a
|
|
63
|
-
* shared workspace owned by a parent DO.
|
|
64
|
-
*
|
|
65
|
-
* ```ts
|
|
66
|
-
* import { Workspace } from "@cloudflare/shell";
|
|
67
|
-
* import { createWorkspaceTools } from "@cloudflare/think";
|
|
68
|
-
*
|
|
69
|
-
* class MyAgent extends Agent<Env> {
|
|
70
|
-
* workspace = new Workspace({ sql: this.ctx.storage.sql, name: () => this.name });
|
|
71
|
-
*
|
|
72
|
-
* async onChatMessage() {
|
|
73
|
-
* const tools = createWorkspaceTools(this.workspace);
|
|
74
|
-
* const result = streamText({ model, tools, messages });
|
|
75
|
-
* return result.toUIMessageStreamResponse();
|
|
76
|
-
* }
|
|
77
|
-
* }
|
|
78
|
-
* ```
|
|
79
|
-
*/
|
|
80
|
-
declare function createWorkspaceTools(workspace: WorkspaceLike): {
|
|
81
|
-
read: import("ai").Tool<
|
|
82
|
-
{
|
|
83
|
-
path: string;
|
|
84
|
-
offset?: number | undefined;
|
|
85
|
-
limit?: number | undefined;
|
|
86
|
-
},
|
|
87
|
-
ReadToolOutput
|
|
88
|
-
>;
|
|
89
|
-
write: import("ai").Tool<
|
|
90
|
-
{
|
|
91
|
-
path: string;
|
|
92
|
-
content: string;
|
|
93
|
-
},
|
|
94
|
-
{
|
|
95
|
-
path: string;
|
|
96
|
-
bytesWritten: number;
|
|
97
|
-
lines: number;
|
|
98
|
-
}
|
|
99
|
-
>;
|
|
100
|
-
edit: import("ai").Tool<
|
|
101
|
-
{
|
|
102
|
-
path: string;
|
|
103
|
-
old_string: string;
|
|
104
|
-
new_string: string;
|
|
105
|
-
},
|
|
106
|
-
| {
|
|
107
|
-
error: string;
|
|
108
|
-
path?: undefined;
|
|
109
|
-
created?: undefined;
|
|
110
|
-
lines?: undefined;
|
|
111
|
-
replaced?: undefined;
|
|
112
|
-
fuzzyMatch?: undefined;
|
|
113
|
-
}
|
|
114
|
-
| {
|
|
115
|
-
path: string;
|
|
116
|
-
created: boolean;
|
|
117
|
-
lines: number;
|
|
118
|
-
error?: undefined;
|
|
119
|
-
replaced?: undefined;
|
|
120
|
-
fuzzyMatch?: undefined;
|
|
121
|
-
}
|
|
122
|
-
| {
|
|
123
|
-
path: string;
|
|
124
|
-
replaced: boolean;
|
|
125
|
-
fuzzyMatch: boolean;
|
|
126
|
-
lines: number;
|
|
127
|
-
error?: undefined;
|
|
128
|
-
created?: undefined;
|
|
129
|
-
}
|
|
130
|
-
| {
|
|
131
|
-
path: string;
|
|
132
|
-
replaced: boolean;
|
|
133
|
-
lines: number;
|
|
134
|
-
error?: undefined;
|
|
135
|
-
created?: undefined;
|
|
136
|
-
fuzzyMatch?: undefined;
|
|
137
|
-
}
|
|
138
|
-
>;
|
|
139
|
-
list: import("ai").Tool<
|
|
140
|
-
{
|
|
141
|
-
path: string;
|
|
142
|
-
limit?: number | undefined;
|
|
143
|
-
offset?: number | undefined;
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
path: string;
|
|
147
|
-
count: number;
|
|
148
|
-
entries: string[];
|
|
149
|
-
}
|
|
150
|
-
>;
|
|
151
|
-
find: import("ai").Tool<
|
|
152
|
-
{
|
|
153
|
-
pattern: string;
|
|
154
|
-
},
|
|
155
|
-
Record<string, unknown>
|
|
156
|
-
>;
|
|
157
|
-
grep: import("ai").Tool<
|
|
158
|
-
{
|
|
159
|
-
query: string;
|
|
160
|
-
include?: string | undefined;
|
|
161
|
-
fixedString?: boolean | undefined;
|
|
162
|
-
caseSensitive?: boolean | undefined;
|
|
163
|
-
contextLines?: number | undefined;
|
|
164
|
-
},
|
|
165
|
-
Record<string, unknown>
|
|
166
|
-
>;
|
|
167
|
-
delete: import("ai").Tool<
|
|
168
|
-
{
|
|
169
|
-
path: string;
|
|
170
|
-
recursive?: boolean | undefined;
|
|
171
|
-
},
|
|
172
|
-
{
|
|
173
|
-
deleted: string;
|
|
174
|
-
}
|
|
175
|
-
>;
|
|
176
|
-
};
|
|
177
|
-
type TextReadToolOutput = {
|
|
178
|
-
path: string;
|
|
179
|
-
content: string;
|
|
180
|
-
totalLines: number;
|
|
181
|
-
fromLine?: number;
|
|
182
|
-
toLine?: number;
|
|
183
|
-
};
|
|
184
|
-
type ImageReadToolOutput = {
|
|
185
|
-
kind: "image";
|
|
186
|
-
path: string;
|
|
187
|
-
name: string;
|
|
188
|
-
mediaType: string;
|
|
189
|
-
sizeBytes: number;
|
|
190
|
-
};
|
|
191
|
-
type FileReadToolOutput = {
|
|
192
|
-
kind: "file";
|
|
193
|
-
path: string;
|
|
194
|
-
name: string;
|
|
195
|
-
mediaType: string;
|
|
196
|
-
sizeBytes: number;
|
|
197
|
-
};
|
|
198
|
-
type BinaryReadToolOutput = {
|
|
199
|
-
kind: "binary";
|
|
200
|
-
path: string;
|
|
201
|
-
name: string;
|
|
202
|
-
mediaType: string;
|
|
203
|
-
sizeBytes: number;
|
|
204
|
-
unsupported: true;
|
|
205
|
-
};
|
|
206
|
-
type ReadToolError = {
|
|
207
|
-
error: string;
|
|
208
|
-
};
|
|
209
|
-
type ReadToolOutput =
|
|
210
|
-
| TextReadToolOutput
|
|
211
|
-
| ImageReadToolOutput
|
|
212
|
-
| FileReadToolOutput
|
|
213
|
-
| BinaryReadToolOutput
|
|
214
|
-
| ReadToolError;
|
|
215
|
-
interface ReadToolOptions {
|
|
216
|
-
ops: ReadOperations;
|
|
217
|
-
}
|
|
218
|
-
declare function createReadTool(options: ReadToolOptions): import("ai").Tool<
|
|
219
|
-
{
|
|
220
|
-
path: string;
|
|
221
|
-
offset?: number | undefined;
|
|
222
|
-
limit?: number | undefined;
|
|
223
|
-
},
|
|
224
|
-
ReadToolOutput
|
|
225
|
-
>;
|
|
226
|
-
interface WriteToolOptions {
|
|
227
|
-
ops: WriteOperations;
|
|
228
|
-
}
|
|
229
|
-
declare function createWriteTool(options: WriteToolOptions): import("ai").Tool<
|
|
230
|
-
{
|
|
231
|
-
path: string;
|
|
232
|
-
content: string;
|
|
233
|
-
},
|
|
234
|
-
{
|
|
235
|
-
path: string;
|
|
236
|
-
bytesWritten: number;
|
|
237
|
-
lines: number;
|
|
238
|
-
}
|
|
239
|
-
>;
|
|
240
|
-
interface EditToolOptions {
|
|
241
|
-
ops: EditOperations;
|
|
242
|
-
}
|
|
243
|
-
declare function createEditTool(options: EditToolOptions): import("ai").Tool<
|
|
244
|
-
{
|
|
245
|
-
path: string;
|
|
246
|
-
old_string: string;
|
|
247
|
-
new_string: string;
|
|
248
|
-
},
|
|
249
|
-
| {
|
|
250
|
-
error: string;
|
|
251
|
-
path?: undefined;
|
|
252
|
-
created?: undefined;
|
|
253
|
-
lines?: undefined;
|
|
254
|
-
replaced?: undefined;
|
|
255
|
-
fuzzyMatch?: undefined;
|
|
256
|
-
}
|
|
257
|
-
| {
|
|
258
|
-
path: string;
|
|
259
|
-
created: boolean;
|
|
260
|
-
lines: number;
|
|
261
|
-
error?: undefined;
|
|
262
|
-
replaced?: undefined;
|
|
263
|
-
fuzzyMatch?: undefined;
|
|
264
|
-
}
|
|
265
|
-
| {
|
|
266
|
-
path: string;
|
|
267
|
-
replaced: boolean;
|
|
268
|
-
fuzzyMatch: boolean;
|
|
269
|
-
lines: number;
|
|
270
|
-
error?: undefined;
|
|
271
|
-
created?: undefined;
|
|
272
|
-
}
|
|
273
|
-
| {
|
|
274
|
-
path: string;
|
|
275
|
-
replaced: boolean;
|
|
276
|
-
lines: number;
|
|
277
|
-
error?: undefined;
|
|
278
|
-
created?: undefined;
|
|
279
|
-
fuzzyMatch?: undefined;
|
|
280
|
-
}
|
|
281
|
-
>;
|
|
282
|
-
interface ListToolOptions {
|
|
283
|
-
ops: ListOperations;
|
|
284
|
-
}
|
|
285
|
-
declare function createListTool(options: ListToolOptions): import("ai").Tool<
|
|
286
|
-
{
|
|
287
|
-
path: string;
|
|
288
|
-
limit?: number | undefined;
|
|
289
|
-
offset?: number | undefined;
|
|
290
|
-
},
|
|
291
|
-
{
|
|
292
|
-
path: string;
|
|
293
|
-
count: number;
|
|
294
|
-
entries: string[];
|
|
295
|
-
}
|
|
296
|
-
>;
|
|
297
|
-
interface FindToolOptions {
|
|
298
|
-
ops: FindOperations;
|
|
299
|
-
}
|
|
300
|
-
declare function createFindTool(options: FindToolOptions): import("ai").Tool<
|
|
301
|
-
{
|
|
302
|
-
pattern: string;
|
|
303
|
-
},
|
|
304
|
-
Record<string, unknown>
|
|
305
|
-
>;
|
|
306
|
-
interface GrepToolOptions {
|
|
307
|
-
ops: GrepOperations;
|
|
308
|
-
}
|
|
309
|
-
declare function createGrepTool(options: GrepToolOptions): import("ai").Tool<
|
|
310
|
-
{
|
|
311
|
-
query: string;
|
|
312
|
-
include?: string | undefined;
|
|
313
|
-
fixedString?: boolean | undefined;
|
|
314
|
-
caseSensitive?: boolean | undefined;
|
|
315
|
-
contextLines?: number | undefined;
|
|
316
|
-
},
|
|
317
|
-
Record<string, unknown>
|
|
318
|
-
>;
|
|
319
|
-
interface DeleteToolOptions {
|
|
320
|
-
ops: DeleteOperations;
|
|
321
|
-
}
|
|
322
|
-
declare function createDeleteTool(
|
|
323
|
-
options: DeleteToolOptions
|
|
324
|
-
): import("ai").Tool<
|
|
325
|
-
{
|
|
326
|
-
path: string;
|
|
327
|
-
recursive?: boolean | undefined;
|
|
328
|
-
},
|
|
329
|
-
{
|
|
330
|
-
deleted: string;
|
|
331
|
-
}
|
|
332
|
-
>;
|
|
333
|
-
//#endregion
|
|
1
|
+
import {
|
|
2
|
+
C as createFindTool,
|
|
3
|
+
D as createWorkspaceTools,
|
|
4
|
+
E as createReadTool,
|
|
5
|
+
O as createWriteTool,
|
|
6
|
+
S as createEditTool,
|
|
7
|
+
T as createListTool,
|
|
8
|
+
_ as WorkspaceToolsOptions,
|
|
9
|
+
a as EditOperations,
|
|
10
|
+
b as createBashTool,
|
|
11
|
+
c as FindToolOptions,
|
|
12
|
+
d as ListOperations,
|
|
13
|
+
f as ListToolOptions,
|
|
14
|
+
g as WorkspaceTools,
|
|
15
|
+
h as WorkspaceLike,
|
|
16
|
+
i as DeleteToolOptions,
|
|
17
|
+
l as GrepOperations,
|
|
18
|
+
m as ReadToolOptions,
|
|
19
|
+
n as BashToolOptions,
|
|
20
|
+
o as EditToolOptions,
|
|
21
|
+
p as ReadOperations,
|
|
22
|
+
r as DeleteOperations,
|
|
23
|
+
s as FindOperations,
|
|
24
|
+
t as BashOperations,
|
|
25
|
+
u as GrepToolOptions,
|
|
26
|
+
v as WriteOperations,
|
|
27
|
+
w as createGrepTool,
|
|
28
|
+
x as createDeleteTool,
|
|
29
|
+
y as WriteToolOptions
|
|
30
|
+
} from "../workspace-Bri0ANBX.js";
|
|
334
31
|
export {
|
|
32
|
+
BashOperations,
|
|
33
|
+
BashToolOptions,
|
|
335
34
|
DeleteOperations,
|
|
336
35
|
DeleteToolOptions,
|
|
337
36
|
EditOperations,
|
|
@@ -345,8 +44,11 @@ export {
|
|
|
345
44
|
ReadOperations,
|
|
346
45
|
ReadToolOptions,
|
|
347
46
|
WorkspaceLike,
|
|
47
|
+
WorkspaceTools,
|
|
48
|
+
WorkspaceToolsOptions,
|
|
348
49
|
WriteOperations,
|
|
349
50
|
WriteToolOptions,
|
|
51
|
+
createBashTool,
|
|
350
52
|
createDeleteTool,
|
|
351
53
|
createEditTool,
|
|
352
54
|
createFindTool,
|
|
@@ -356,4 +58,3 @@ export {
|
|
|
356
58
|
createWorkspaceTools,
|
|
357
59
|
createWriteTool
|
|
358
60
|
};
|
|
359
|
-
//# sourceMappingURL=workspace.d.ts.map
|
package/dist/tools/workspace.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { tool } from "ai";
|
|
2
|
+
import { Bash } from "just-bash";
|
|
2
3
|
import { z } from "zod";
|
|
3
4
|
//#region src/tools/workspace.ts
|
|
4
5
|
function workspaceReadOps(ws) {
|
|
@@ -35,6 +36,17 @@ function workspaceGrepOps(ws) {
|
|
|
35
36
|
readFile: (path) => ws.readFile(path)
|
|
36
37
|
};
|
|
37
38
|
}
|
|
39
|
+
function workspaceBashOps(ws) {
|
|
40
|
+
const maybeBytesWriter = ws;
|
|
41
|
+
return {
|
|
42
|
+
readDir: (dir, opts) => ws.readDir(dir, opts),
|
|
43
|
+
readFileBytes: (path) => ws.readFileBytes(path),
|
|
44
|
+
writeFile: (path, content) => ws.writeFile(path, content),
|
|
45
|
+
writeFileBytes: maybeBytesWriter.writeFileBytes ? (path, content) => maybeBytesWriter.writeFileBytes(path, content) : void 0,
|
|
46
|
+
mkdir: (path, opts) => ws.mkdir(path, opts),
|
|
47
|
+
rm: (path, opts) => ws.rm(path, opts)
|
|
48
|
+
};
|
|
49
|
+
}
|
|
38
50
|
/**
|
|
39
51
|
* Create a complete set of AI SDK tools backed by a workspace.
|
|
40
52
|
*
|
|
@@ -57,8 +69,8 @@ function workspaceGrepOps(ws) {
|
|
|
57
69
|
* }
|
|
58
70
|
* ```
|
|
59
71
|
*/
|
|
60
|
-
function createWorkspaceTools(workspace) {
|
|
61
|
-
|
|
72
|
+
function createWorkspaceTools(workspace, options = {}) {
|
|
73
|
+
const tools = {
|
|
62
74
|
read: createReadTool({ ops: workspaceReadOps(workspace) }),
|
|
63
75
|
write: createWriteTool({ ops: workspaceWriteOps(workspace) }),
|
|
64
76
|
edit: createEditTool({ ops: workspaceEditOps(workspace) }),
|
|
@@ -67,6 +79,11 @@ function createWorkspaceTools(workspace) {
|
|
|
67
79
|
grep: createGrepTool({ ops: workspaceGrepOps(workspace) }),
|
|
68
80
|
delete: createDeleteTool({ ops: workspaceDeleteOps(workspace) })
|
|
69
81
|
};
|
|
82
|
+
if (options.bash !== false) tools.bash = createBashTool({
|
|
83
|
+
...typeof options.bash === "object" ? options.bash : {},
|
|
84
|
+
ops: workspaceBashOps(workspace)
|
|
85
|
+
});
|
|
86
|
+
return tools;
|
|
70
87
|
}
|
|
71
88
|
const MAX_LINES = 2e3;
|
|
72
89
|
const MAX_LINE_LENGTH = 2e3;
|
|
@@ -564,6 +581,269 @@ function createGrepTool(options) {
|
|
|
564
581
|
function escapeRegex(s) {
|
|
565
582
|
return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
566
583
|
}
|
|
584
|
+
const DEFAULT_BASH_TIMEOUT_MS = 3e4;
|
|
585
|
+
const DEFAULT_BASH_MAX_WORKSPACE_FILES = 1e3;
|
|
586
|
+
const DEFAULT_BASH_MAX_WORKSPACE_FILE_BYTES = 1e6;
|
|
587
|
+
const DEFAULT_BASH_MAX_OUTPUT_BYTES = 64e3;
|
|
588
|
+
const BASH_READDIR_PAGE_SIZE = 1e3;
|
|
589
|
+
const BASH_EXCLUDED_SYNC_PREFIXES = [
|
|
590
|
+
"/dev/",
|
|
591
|
+
"/proc/",
|
|
592
|
+
"/sys/"
|
|
593
|
+
];
|
|
594
|
+
function createBashTool(options) {
|
|
595
|
+
return tool({
|
|
596
|
+
description: "Run a Bash script against the workspace. Use for shell-style workflows that combine multiple file operations. The script runs in a sandboxed virtual filesystem mounted from the workspace; changed files are written back.",
|
|
597
|
+
inputSchema: z.object({
|
|
598
|
+
script: z.string().describe("Bash script to run"),
|
|
599
|
+
cwd: z.string().optional().describe("Working directory for the script. Defaults to /")
|
|
600
|
+
}),
|
|
601
|
+
execute: async ({ script, cwd }) => {
|
|
602
|
+
const timeout = options.timeout ?? DEFAULT_BASH_TIMEOUT_MS;
|
|
603
|
+
const maxOutputBytes = options.maxOutputBytes ?? DEFAULT_BASH_MAX_OUTPUT_BYTES;
|
|
604
|
+
const snapshot = await snapshotWorkspaceForBash(options);
|
|
605
|
+
const bash = new Bash({
|
|
606
|
+
files: snapshot.files,
|
|
607
|
+
cwd: normalizeWorkspacePath(cwd ?? "/"),
|
|
608
|
+
defenseInDepth: true,
|
|
609
|
+
network: options.network ? {} : void 0
|
|
610
|
+
});
|
|
611
|
+
const controller = new AbortController();
|
|
612
|
+
const timer = setTimeout(() => controller.abort(), timeout);
|
|
613
|
+
let result;
|
|
614
|
+
try {
|
|
615
|
+
for (const directory of snapshot.directories) await bash.fs.mkdir(directory, { recursive: true }).catch(() => {});
|
|
616
|
+
try {
|
|
617
|
+
result = await bash.exec(script, {
|
|
618
|
+
cwd: normalizeWorkspacePath(cwd ?? "/"),
|
|
619
|
+
signal: controller.signal,
|
|
620
|
+
rawScript: true
|
|
621
|
+
});
|
|
622
|
+
} catch (error) {
|
|
623
|
+
result = {
|
|
624
|
+
stdout: "",
|
|
625
|
+
stderr: errorMessage(error),
|
|
626
|
+
exitCode: controller.signal.aborted ? 124 : 1
|
|
627
|
+
};
|
|
628
|
+
}
|
|
629
|
+
const sync = await syncBashFilesToWorkspace({
|
|
630
|
+
ops: options.ops,
|
|
631
|
+
bash,
|
|
632
|
+
initialFiles: snapshot.initialFiles,
|
|
633
|
+
initialDirectories: snapshot.initialDirectories,
|
|
634
|
+
protectedPaths: snapshot.protectedPaths
|
|
635
|
+
});
|
|
636
|
+
return {
|
|
637
|
+
stdout: truncateToolOutput(result.stdout, maxOutputBytes),
|
|
638
|
+
stderr: truncateToolOutput(result.stderr, maxOutputBytes),
|
|
639
|
+
exitCode: result.exitCode,
|
|
640
|
+
changedFiles: sync.changedFiles,
|
|
641
|
+
...snapshot.skippedFiles.length > 0 ? { skippedFiles: snapshot.skippedFiles } : {},
|
|
642
|
+
...sync.errors.length > 0 ? { errors: sync.errors } : {}
|
|
643
|
+
};
|
|
644
|
+
} finally {
|
|
645
|
+
clearTimeout(timer);
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
async function snapshotWorkspaceForBash(options) {
|
|
651
|
+
const maxFiles = options.maxWorkspaceFiles ?? DEFAULT_BASH_MAX_WORKSPACE_FILES;
|
|
652
|
+
const maxFileBytes = options.maxWorkspaceFileBytes ?? DEFAULT_BASH_MAX_WORKSPACE_FILE_BYTES;
|
|
653
|
+
const files = {};
|
|
654
|
+
const initialFiles = /* @__PURE__ */ new Map();
|
|
655
|
+
const initialDirectories = new Set(["/"]);
|
|
656
|
+
const protectedPaths = /* @__PURE__ */ new Set();
|
|
657
|
+
const skippedFiles = [];
|
|
658
|
+
const pending = ["/"];
|
|
659
|
+
while (pending.length > 0) {
|
|
660
|
+
const dir = pending.shift();
|
|
661
|
+
const entries = await readAllBashDirEntries(options.ops, dir);
|
|
662
|
+
for (const entry of entries) {
|
|
663
|
+
const path = normalizeWorkspacePath(entry.path);
|
|
664
|
+
if (entry.type === "directory") {
|
|
665
|
+
initialDirectories.add(path);
|
|
666
|
+
pending.push(path);
|
|
667
|
+
continue;
|
|
668
|
+
}
|
|
669
|
+
if (entry.type !== "file") continue;
|
|
670
|
+
if (initialFiles.size >= maxFiles || entry.size > maxFileBytes) {
|
|
671
|
+
protectedPaths.add(path);
|
|
672
|
+
skippedFiles.push(path);
|
|
673
|
+
continue;
|
|
674
|
+
}
|
|
675
|
+
const bytes = await options.ops.readFileBytes(path);
|
|
676
|
+
if (bytes === null) {
|
|
677
|
+
protectedPaths.add(path);
|
|
678
|
+
skippedFiles.push(path);
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
files[path] = bytes;
|
|
682
|
+
initialFiles.set(path, bytes);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
return {
|
|
686
|
+
files,
|
|
687
|
+
initialFiles,
|
|
688
|
+
initialDirectories,
|
|
689
|
+
protectedPaths,
|
|
690
|
+
directories: [...initialDirectories].sort((a, b) => a.localeCompare(b)),
|
|
691
|
+
skippedFiles
|
|
692
|
+
};
|
|
693
|
+
}
|
|
694
|
+
async function readAllBashDirEntries(ops, dir) {
|
|
695
|
+
const entries = [];
|
|
696
|
+
let offset = 0;
|
|
697
|
+
while (true) {
|
|
698
|
+
const page = await ops.readDir(dir, {
|
|
699
|
+
limit: BASH_READDIR_PAGE_SIZE,
|
|
700
|
+
offset
|
|
701
|
+
});
|
|
702
|
+
entries.push(...page);
|
|
703
|
+
if (page.length !== BASH_READDIR_PAGE_SIZE) break;
|
|
704
|
+
offset += page.length;
|
|
705
|
+
}
|
|
706
|
+
return entries;
|
|
707
|
+
}
|
|
708
|
+
async function syncBashFilesToWorkspace({ ops, bash, initialFiles, initialDirectories, protectedPaths }) {
|
|
709
|
+
const changedFiles = {
|
|
710
|
+
created: [],
|
|
711
|
+
updated: [],
|
|
712
|
+
deleted: [],
|
|
713
|
+
directoriesCreated: [],
|
|
714
|
+
directoriesDeleted: []
|
|
715
|
+
};
|
|
716
|
+
const errors = [];
|
|
717
|
+
const finalFiles = /* @__PURE__ */ new Map();
|
|
718
|
+
const finalDirectories = new Set(["/"]);
|
|
719
|
+
for (const rawPath of bash.fs.getAllPaths()) {
|
|
720
|
+
const path = normalizeWorkspacePath(rawPath);
|
|
721
|
+
if (!shouldSyncBashPath(path, initialFiles, protectedPaths)) continue;
|
|
722
|
+
const stat = await bash.fs.stat(path).catch(() => null);
|
|
723
|
+
if (stat?.isDirectory) {
|
|
724
|
+
finalDirectories.add(path);
|
|
725
|
+
continue;
|
|
726
|
+
}
|
|
727
|
+
if (stat?.isFile) finalFiles.set(path, await bash.fs.readFileBuffer(path));
|
|
728
|
+
}
|
|
729
|
+
for (const path of [...finalDirectories].sort((a, b) => a.localeCompare(b))) {
|
|
730
|
+
if (path === "/" || initialDirectories.has(path)) continue;
|
|
731
|
+
if (hasProtectedDescendant(path, protectedPaths)) {
|
|
732
|
+
errors.push(`Skipped creating directory ${path}: contains protected paths.`);
|
|
733
|
+
continue;
|
|
734
|
+
}
|
|
735
|
+
if (!await Promise.resolve(ops.mkdir(path, { recursive: true })).then(() => true).catch((error) => {
|
|
736
|
+
errors.push(`Failed to create directory ${path}: ${errorMessage(error)}`);
|
|
737
|
+
return false;
|
|
738
|
+
})) continue;
|
|
739
|
+
changedFiles.directoriesCreated.push(path);
|
|
740
|
+
}
|
|
741
|
+
for (const [path, bytes] of finalFiles) {
|
|
742
|
+
if (protectedPaths.has(path)) {
|
|
743
|
+
errors.push(`Skipped writing protected workspace file: ${path}`);
|
|
744
|
+
continue;
|
|
745
|
+
}
|
|
746
|
+
const existing = initialFiles.get(path);
|
|
747
|
+
if (existing && bytesEqual(existing, bytes)) continue;
|
|
748
|
+
if (!await writeWorkspaceBytes(ops, path, bytes, errors)) continue;
|
|
749
|
+
if (existing) changedFiles.updated.push(path);
|
|
750
|
+
else changedFiles.created.push(path);
|
|
751
|
+
}
|
|
752
|
+
for (const path of [...initialFiles.keys()].sort((a, b) => b.localeCompare(a))) {
|
|
753
|
+
if (finalFiles.has(path) || protectedPaths.has(path)) continue;
|
|
754
|
+
if (!await ops.rm(path, { force: true }).then(() => true).catch((error) => {
|
|
755
|
+
errors.push(`Failed to delete ${path}: ${errorMessage(error)}`);
|
|
756
|
+
return false;
|
|
757
|
+
})) continue;
|
|
758
|
+
changedFiles.deleted.push(path);
|
|
759
|
+
}
|
|
760
|
+
for (const path of [...initialDirectories].sort((a, b) => b.localeCompare(a))) {
|
|
761
|
+
if (path === "/" || finalDirectories.has(path)) continue;
|
|
762
|
+
if (hasProtectedDescendant(path, protectedPaths)) {
|
|
763
|
+
errors.push(`Skipped deleting protected workspace directory: ${path}`);
|
|
764
|
+
continue;
|
|
765
|
+
}
|
|
766
|
+
if (!await ops.rm(path, {
|
|
767
|
+
recursive: true,
|
|
768
|
+
force: true
|
|
769
|
+
}).then(() => true).catch((error) => {
|
|
770
|
+
errors.push(`Failed to delete directory ${path}: ${errorMessage(error)}`);
|
|
771
|
+
return false;
|
|
772
|
+
})) continue;
|
|
773
|
+
changedFiles.directoriesDeleted.push(path);
|
|
774
|
+
}
|
|
775
|
+
changedFiles.created.sort();
|
|
776
|
+
changedFiles.updated.sort();
|
|
777
|
+
changedFiles.deleted.sort();
|
|
778
|
+
changedFiles.directoriesCreated.sort();
|
|
779
|
+
changedFiles.directoriesDeleted.sort();
|
|
780
|
+
return {
|
|
781
|
+
changedFiles,
|
|
782
|
+
errors
|
|
783
|
+
};
|
|
784
|
+
}
|
|
785
|
+
async function writeWorkspaceBytes(ops, path, bytes, errors) {
|
|
786
|
+
const parent = parentDir(path);
|
|
787
|
+
if (parent !== "/") {
|
|
788
|
+
if (!await Promise.resolve(ops.mkdir(parent, { recursive: true })).then(() => true).catch((error) => {
|
|
789
|
+
errors.push(`Failed to create parent directory ${parent}: ${errorMessage(error)}`);
|
|
790
|
+
return false;
|
|
791
|
+
})) return false;
|
|
792
|
+
}
|
|
793
|
+
if (ops.writeFileBytes) return ops.writeFileBytes(path, bytes).then(() => true).catch((error) => {
|
|
794
|
+
errors.push(`Failed to write ${path}: ${errorMessage(error)}`);
|
|
795
|
+
return false;
|
|
796
|
+
});
|
|
797
|
+
if (!looksLikeText(bytes)) {
|
|
798
|
+
errors.push(`Could not persist binary file ${path}: workspace does not support writeFileBytes.`);
|
|
799
|
+
return false;
|
|
800
|
+
}
|
|
801
|
+
const content = new TextDecoder().decode(bytes);
|
|
802
|
+
return ops.writeFile(path, content).then(() => true).catch((error) => {
|
|
803
|
+
errors.push(`Failed to write ${path}: ${errorMessage(error)}`);
|
|
804
|
+
return false;
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
function shouldSyncBashPath(path, initialFiles, protectedPaths) {
|
|
808
|
+
if (path === "/") return false;
|
|
809
|
+
if (initialFiles.has(path)) return true;
|
|
810
|
+
if (protectedPaths.has(path)) return true;
|
|
811
|
+
if (path.startsWith("/tmp/")) return false;
|
|
812
|
+
return !BASH_EXCLUDED_SYNC_PREFIXES.some((prefix) => path.startsWith(prefix));
|
|
813
|
+
}
|
|
814
|
+
function hasProtectedDescendant(path, protectedPaths) {
|
|
815
|
+
const prefix = path.endsWith("/") ? path : `${path}/`;
|
|
816
|
+
for (const protectedPath of protectedPaths) if (protectedPath.startsWith(prefix)) return true;
|
|
817
|
+
return false;
|
|
818
|
+
}
|
|
819
|
+
function normalizeWorkspacePath(path) {
|
|
820
|
+
const parts = [];
|
|
821
|
+
const raw = path.startsWith("/") ? path : `/${path}`;
|
|
822
|
+
for (const part of raw.split("/")) {
|
|
823
|
+
if (!part || part === ".") continue;
|
|
824
|
+
if (part === "..") parts.pop();
|
|
825
|
+
else parts.push(part);
|
|
826
|
+
}
|
|
827
|
+
return `/${parts.join("/")}`;
|
|
828
|
+
}
|
|
829
|
+
function parentDir(path) {
|
|
830
|
+
const normalized = normalizeWorkspacePath(path);
|
|
831
|
+
const index = normalized.lastIndexOf("/");
|
|
832
|
+
return index <= 0 ? "/" : normalized.slice(0, index);
|
|
833
|
+
}
|
|
834
|
+
function bytesEqual(a, b) {
|
|
835
|
+
if (a.byteLength !== b.byteLength) return false;
|
|
836
|
+
for (let i = 0; i < a.byteLength; i++) if (a[i] !== b[i]) return false;
|
|
837
|
+
return true;
|
|
838
|
+
}
|
|
839
|
+
function truncateToolOutput(value, maxBytes) {
|
|
840
|
+
const bytes = new TextEncoder().encode(value);
|
|
841
|
+
if (bytes.byteLength <= maxBytes) return value;
|
|
842
|
+
return `${new TextDecoder().decode(bytes.slice(0, maxBytes))}\n... (${formatSize(bytes.byteLength - maxBytes)} truncated)`;
|
|
843
|
+
}
|
|
844
|
+
function errorMessage(error) {
|
|
845
|
+
return error instanceof Error ? error.message : String(error);
|
|
846
|
+
}
|
|
567
847
|
function createDeleteTool(options) {
|
|
568
848
|
const { ops } = options;
|
|
569
849
|
return tool({
|
|
@@ -582,6 +862,6 @@ function createDeleteTool(options) {
|
|
|
582
862
|
});
|
|
583
863
|
}
|
|
584
864
|
//#endregion
|
|
585
|
-
export { createDeleteTool, createEditTool, createFindTool, createGrepTool, createListTool, createReadTool, createWorkspaceTools, createWriteTool };
|
|
865
|
+
export { createBashTool, createDeleteTool, createEditTool, createFindTool, createGrepTool, createListTool, createReadTool, createWorkspaceTools, createWriteTool };
|
|
586
866
|
|
|
587
867
|
//# sourceMappingURL=workspace.js.map
|