@cursor/july 0.1.103 → 0.1.104
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/dist/channels/change-monitors/change-monitors-channel.d.ts +5 -0
- package/dist/channels/change-monitors/change-monitors-channel.d.ts.map +1 -0
- package/dist/channels/change-monitors/change-monitors-channel.js +41 -0
- package/dist/channels/change-monitors/index.d.ts +4 -0
- package/dist/channels/change-monitors/index.d.ts.map +1 -0
- package/dist/channels/change-monitors/index.js +2 -0
- package/dist/channels/change-monitors/types.d.ts +39 -0
- package/dist/channels/change-monitors/types.d.ts.map +1 -0
- package/dist/channels/change-monitors/types.js +1 -0
- package/dist/channels/github/defaults.d.ts.map +1 -1
- package/dist/channels/github/defaults.js +4 -0
- package/dist/channels/github/progress.d.ts +22 -0
- package/dist/channels/github/progress.d.ts.map +1 -1
- package/dist/channels/github/progress.js +32 -5
- package/dist/channels/issues/index.d.ts +4 -0
- package/dist/channels/issues/index.d.ts.map +1 -0
- package/dist/channels/issues/index.js +2 -0
- package/dist/channels/issues/issues-channel.d.ts +5 -0
- package/dist/channels/issues/issues-channel.d.ts.map +1 -0
- package/dist/channels/issues/issues-channel.js +37 -0
- package/dist/channels/issues/types.d.ts +33 -0
- package/dist/channels/issues/types.d.ts.map +1 -0
- package/dist/channels/issues/types.js +1 -0
- package/dist/docs/404.html +2 -2
- package/dist/docs/ab.html +4 -4
- package/dist/docs/assets/{app.DW9pXkpL.js → app.DbMWUyGb.js} +1 -1
- package/dist/docs/assets/chunks/@localSearchIndexroot.Bqsulb9K.js +1 -0
- package/dist/docs/assets/chunks/{VPLocalSearchBox.CdEIUwDg.js → VPLocalSearchBox.DcNeTh9o.js} +1 -1
- package/dist/docs/assets/chunks/{theme.YZMHgPgg.js → theme.DMjhY3gl.js} +2 -2
- package/dist/docs/assets/{guides_github.md.DKYiT9do.js → guides_github.md.c0gdGJ-D.js} +1 -1
- package/dist/docs/assets/{guides_github.md.DKYiT9do.lean.js → guides_github.md.c0gdGJ-D.lean.js} +1 -1
- package/dist/docs/assets/{reference_channels.md.Dz8Cnudp.js → reference_channels.md.DBGcaB7m.js} +2 -2
- package/dist/docs/assets/{reference_channels.md.Dz8Cnudp.lean.js → reference_channels.md.DBGcaB7m.lean.js} +1 -1
- package/dist/docs/building-with-agents.html +4 -4
- package/dist/docs/concepts.html +4 -4
- package/dist/docs/deployment.html +4 -4
- package/dist/docs/design/filesystem.md +376 -0
- package/dist/docs/evals.html +4 -4
- package/dist/docs/guides/agent-to-agent.html +4 -4
- package/dist/docs/guides/cloud-agents.html +4 -4
- package/dist/docs/guides/convert-automation.html +4 -4
- package/dist/docs/guides/github.html +6 -6
- package/dist/docs/guides/github.md +5 -0
- package/dist/docs/guides/human-in-the-loop.html +4 -4
- package/dist/docs/guides/mcp-oauth.html +4 -4
- package/dist/docs/guides/opentelemetry.html +4 -4
- package/dist/docs/guides/slack.html +4 -4
- package/dist/docs/guides/webhooks.html +4 -4
- package/dist/docs/hashmap.json +1 -1
- package/dist/docs/hillclimbing.html +4 -4
- package/dist/docs/index.html +4 -4
- package/dist/docs/llms-full.txt +406 -0
- package/dist/docs/llms.txt +1 -0
- package/dist/docs/quickstart.html +4 -4
- package/dist/docs/reference/agent-config.html +4 -4
- package/dist/docs/reference/artifacts.html +4 -4
- package/dist/docs/reference/channels.html +6 -6
- package/dist/docs/reference/channels.md +20 -0
- package/dist/docs/reference/cli.html +4 -4
- package/dist/docs/reference/connections.html +4 -4
- package/dist/docs/reference/extensions.html +4 -4
- package/dist/docs/reference/hooks.html +4 -4
- package/dist/docs/reference/http-api.html +4 -4
- package/dist/docs/reference/instructions.html +4 -4
- package/dist/docs/reference/playground.html +4 -4
- package/dist/docs/reference/project-layout.html +4 -4
- package/dist/docs/reference/prompt.html +4 -4
- package/dist/docs/reference/result.html +4 -4
- package/dist/docs/reference/schedules.html +4 -4
- package/dist/docs/reference/sessions.html +4 -4
- package/dist/docs/reference/skills.html +4 -4
- package/dist/docs/reference/subagents.html +4 -4
- package/dist/docs/reference/tools.html +4 -4
- package/dist/docs/scaffolding-agents.html +4 -4
- package/dist/docs/skills/ab.html +4 -4
- package/dist/docs/skills/create-agent.html +4 -4
- package/dist/docs/skills/debug.html +4 -4
- package/dist/docs/skills/evals.html +4 -4
- package/dist/docs/skills/framework-map.html +4 -4
- package/dist/docs/skills/github.html +4 -4
- package/dist/docs/skills/hillclimb.html +4 -4
- package/dist/docs/skills/index.html +4 -4
- package/dist/docs/skills/mcp-auth.html +4 -4
- package/dist/docs/skills/otel.html +4 -4
- package/dist/docs/skills/setup-slack.html +4 -4
- package/dist/docs/storage.html +4 -4
- package/dist/docs/templates/agentic-owners.html +4 -4
- package/dist/docs/templates/agents-md.html +4 -4
- package/dist/docs/templates/code-wiki.html +4 -4
- package/dist/docs/templates/demo.html +4 -4
- package/dist/docs/templates/pr-autofixer.html +4 -4
- package/dist/docs/templates/security-help.html +4 -4
- package/dist/docs/templates/security-reviewer.html +4 -4
- package/dist/docs/templates/triage.html +4 -4
- package/dist/docs/troubleshooting.html +4 -4
- package/dist/filesystem.d.ts +440 -0
- package/dist/filesystem.d.ts.map +1 -0
- package/dist/filesystem.js +1694 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/internal/continuation-channel.d.ts +4 -2
- package/dist/internal/continuation-channel.d.ts.map +1 -1
- package/dist/internal/continuation-channel.js +21 -12
- package/dist/internal/continuation-identity.d.ts +13 -5
- package/dist/internal/continuation-identity.d.ts.map +1 -1
- package/dist/internal/continuation-identity.js +74 -35
- package/dist/internal/discovery/agent.d.ts.map +1 -1
- package/dist/internal/discovery/agent.js +80 -8
- package/dist/internal/discovery/info.d.ts.map +1 -1
- package/dist/internal/discovery/info.js +3 -0
- package/dist/internal/factory-resource-channel.d.ts +33 -0
- package/dist/internal/factory-resource-channel.d.ts.map +1 -0
- package/dist/internal/factory-resource-channel.js +77 -0
- package/dist/internal/filesystem/errors.d.ts +64 -0
- package/dist/internal/filesystem/errors.d.ts.map +1 -0
- package/dist/internal/filesystem/errors.js +175 -0
- package/dist/internal/filesystem/format.d.ts +28 -0
- package/dist/internal/filesystem/format.d.ts.map +1 -0
- package/dist/internal/filesystem/format.js +213 -0
- package/dist/internal/filesystem/merge.d.ts +34 -0
- package/dist/internal/filesystem/merge.d.ts.map +1 -0
- package/dist/internal/filesystem/merge.js +196 -0
- package/dist/internal/filesystem/scratch.d.ts +59 -0
- package/dist/internal/filesystem/scratch.d.ts.map +1 -0
- package/dist/internal/filesystem/scratch.js +212 -0
- package/dist/internal/filesystem/sdk-schemas.d.ts +29 -0
- package/dist/internal/filesystem/sdk-schemas.d.ts.map +1 -0
- package/dist/internal/filesystem/sdk-schemas.js +153 -0
- package/dist/internal/filesystem/tools.d.ts +42 -0
- package/dist/internal/filesystem/tools.d.ts.map +1 -0
- package/dist/internal/filesystem/tools.js +618 -0
- package/dist/internal/filesystem/walk.d.ts +74 -0
- package/dist/internal/filesystem/walk.d.ts.map +1 -0
- package/dist/internal/filesystem/walk.js +339 -0
- package/dist/internal/guest-network.d.ts +5 -2
- package/dist/internal/guest-network.d.ts.map +1 -1
- package/dist/internal/guest-network.js +7 -2
- package/dist/internal/hosted-delivery.d.ts.map +1 -1
- package/dist/internal/hosted-delivery.js +12 -2
- package/dist/internal/sdk-runner.d.ts +1 -1
- package/dist/internal/sdk-runner.d.ts.map +1 -1
- package/dist/internal/sdk-runner.js +3 -1
- package/dist/internal/session-engine.d.ts +22 -0
- package/dist/internal/session-engine.d.ts.map +1 -1
- package/dist/internal/session-engine.js +362 -40
- package/dist/internal/testing/factory-resource-fixtures.d.ts +48 -0
- package/dist/internal/testing/factory-resource-fixtures.d.ts.map +1 -0
- package/dist/internal/testing/factory-resource-fixtures.js +41 -0
- package/dist/playground/assets/{index-59KvgREd.js → index-8YiozU89.js} +38 -38
- package/dist/playground/index.html +1 -1
- package/dist/types.d.ts +51 -3
- package/dist/types.d.ts.map +1 -1
- package/docs/design/filesystem.md +376 -0
- package/docs/guides/github.md +5 -0
- package/docs/reference/channels.md +20 -0
- package/package.json +16 -2
- package/src/channels/change-monitors/change-monitors-channel.ts +62 -0
- package/src/channels/change-monitors/index.ts +8 -0
- package/src/channels/change-monitors/types.ts +44 -0
- package/src/channels/github/defaults.ts +4 -0
- package/src/channels/github/progress.ts +76 -10
- package/src/channels/issues/index.ts +9 -0
- package/src/channels/issues/issues-channel.ts +51 -0
- package/src/channels/issues/types.ts +41 -0
- package/src/filesystem.ts +2074 -0
- package/src/index.ts +28 -0
- package/src/internal/continuation-channel.ts +29 -12
- package/src/internal/continuation-identity.ts +122 -37
- package/src/internal/discovery/agent.ts +87 -1
- package/src/internal/discovery/info.ts +3 -0
- package/src/internal/factory-resource-channel.ts +122 -0
- package/src/internal/filesystem/errors.ts +220 -0
- package/src/internal/filesystem/format.ts +276 -0
- package/src/internal/filesystem/merge.ts +195 -0
- package/src/internal/filesystem/scratch.ts +255 -0
- package/src/internal/filesystem/sdk-schemas.ts +204 -0
- package/src/internal/filesystem/tools.ts +821 -0
- package/src/internal/filesystem/walk.ts +369 -0
- package/src/internal/guest-network.ts +8 -2
- package/src/internal/hosted-delivery.ts +18 -1
- package/src/internal/sdk-runner.ts +4 -0
- package/src/internal/session-engine.ts +439 -29
- package/src/internal/testing/factory-resource-fixtures.ts +85 -0
- package/src/types.ts +49 -3
- package/dist/docs/assets/chunks/@localSearchIndexroot.nrl1ifBq.js +0 -1
|
@@ -0,0 +1,2074 @@
|
|
|
1
|
+
import { Buffer } from "node:buffer";
|
|
2
|
+
import {
|
|
3
|
+
mkdir,
|
|
4
|
+
readdir,
|
|
5
|
+
readFile,
|
|
6
|
+
rm,
|
|
7
|
+
stat,
|
|
8
|
+
writeFile,
|
|
9
|
+
} from "node:fs/promises";
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
import type { ToolName } from "@cursor/sdk";
|
|
12
|
+
import { minimatch } from "minimatch";
|
|
13
|
+
import { FileConflictError } from "./files.js";
|
|
14
|
+
import {
|
|
15
|
+
EditError,
|
|
16
|
+
FileNotFoundError,
|
|
17
|
+
InvalidGrepPatternError,
|
|
18
|
+
isFileNotFoundError,
|
|
19
|
+
isInvalidGrepPatternError,
|
|
20
|
+
NoMountError,
|
|
21
|
+
PathError,
|
|
22
|
+
ReadOnlyMountError,
|
|
23
|
+
remapPathError,
|
|
24
|
+
WriteFailedError,
|
|
25
|
+
} from "./internal/filesystem/errors.js";
|
|
26
|
+
import { MEMORY_DIR_NAME } from "./memory.js";
|
|
27
|
+
|
|
28
|
+
export {
|
|
29
|
+
DeleteNotSupportedError,
|
|
30
|
+
EditError,
|
|
31
|
+
FileNotFoundError,
|
|
32
|
+
InvalidGrepPatternError,
|
|
33
|
+
isFileNotFoundError,
|
|
34
|
+
isInvalidGrepPatternError,
|
|
35
|
+
NoMountError,
|
|
36
|
+
PathError,
|
|
37
|
+
ReadOnlyMountError,
|
|
38
|
+
WriteFailedError,
|
|
39
|
+
} from "./internal/filesystem/errors.js";
|
|
40
|
+
|
|
41
|
+
import { mergeSorted } from "./internal/filesystem/merge.js";
|
|
42
|
+
import {
|
|
43
|
+
listFilesFromPaths,
|
|
44
|
+
mergeSortedByPath,
|
|
45
|
+
statFromFilePaths,
|
|
46
|
+
} from "./internal/filesystem/walk.js";
|
|
47
|
+
import type { HostFilesApi, HostFilesScope } from "./types.js";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Status envelope used by walk helpers such as `lsFromList`.
|
|
51
|
+
* FileSystem methods throw; they do not return this.
|
|
52
|
+
* `@cursor/july` does not export it.
|
|
53
|
+
*/
|
|
54
|
+
export type FileSystemResult<T> =
|
|
55
|
+
| { status: "success"; value: T }
|
|
56
|
+
| { status: "error"; error: { message: string } };
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* One file in a `list` / `glob` stream.
|
|
60
|
+
*
|
|
61
|
+
* `path` is an absolute posix path (`/src/a.ts`). No trailing slash
|
|
62
|
+
* except `/`. `size` is optional byte length of the file contents.
|
|
63
|
+
*/
|
|
64
|
+
export interface DirEntry {
|
|
65
|
+
path: string;
|
|
66
|
+
size?: number;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Existence probe. `directory` means this path is a prefix of at
|
|
71
|
+
* least one file (virtual trees infer directories) or a real
|
|
72
|
+
* directory on disk. `missing` is a value, not an error.
|
|
73
|
+
*/
|
|
74
|
+
export type FileInfo =
|
|
75
|
+
| { kind: "file" }
|
|
76
|
+
| { kind: "directory" }
|
|
77
|
+
| { kind: "missing" };
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Native Grep `output_mode` envelope. Not a FileSystem method
|
|
81
|
+
* result. The `grep` verb yields {@link GrepResult}; synthesis
|
|
82
|
+
* shapes this union after draining the stream.
|
|
83
|
+
*/
|
|
84
|
+
export type GrepUnionResult =
|
|
85
|
+
| {
|
|
86
|
+
type: "content";
|
|
87
|
+
output: {
|
|
88
|
+
matches: Array<{
|
|
89
|
+
file: string;
|
|
90
|
+
lineNumber?: number;
|
|
91
|
+
line: string;
|
|
92
|
+
beforeContext?: string[];
|
|
93
|
+
afterContext?: string[];
|
|
94
|
+
}>;
|
|
95
|
+
totalMatches: number;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
| { type: "files"; output: { files: string[]; count: number } }
|
|
99
|
+
| {
|
|
100
|
+
type: "count";
|
|
101
|
+
output: {
|
|
102
|
+
counts: Array<{ file: string; count: number }>;
|
|
103
|
+
total: number;
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* One content hit from `grep`. `path` is absolute posix.
|
|
109
|
+
* `lineNumber` is 1-indexed. `line` is the full matching line,
|
|
110
|
+
* without the trailing newline.
|
|
111
|
+
*/
|
|
112
|
+
export interface GrepResult {
|
|
113
|
+
path: string;
|
|
114
|
+
lineNumber: number;
|
|
115
|
+
line: string;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Content-search query. No `output_mode` or `offset` (those
|
|
120
|
+
* belong to the native Grep tool).
|
|
121
|
+
*
|
|
122
|
+
* `pattern` is a JavaScript regular expression source. Invalid
|
|
123
|
+
* source must throw {@link InvalidGrepPatternError} with message
|
|
124
|
+
* `invalid grep pattern: ${pattern}`. `path` omitted / `""` /
|
|
125
|
+
* `"/"` searches the whole tree; a file path searches that file
|
|
126
|
+
* only. A slash-free `glob` (`*.ts`) matches the basename at any
|
|
127
|
+
* depth under `path`.
|
|
128
|
+
*
|
|
129
|
+
* `maxResults` is optional predicate pushdown: stop after this
|
|
130
|
+
* many hits so a remote or large tree does not materialize a
|
|
131
|
+
* full scan. Native Grep still applies `head_limit` / `offset`
|
|
132
|
+
* after the merge. Omitted means no FileSystem-level cap.
|
|
133
|
+
*/
|
|
134
|
+
export interface GrepQuery {
|
|
135
|
+
pattern: string;
|
|
136
|
+
path?: string;
|
|
137
|
+
glob?: string;
|
|
138
|
+
caseInsensitive?: boolean;
|
|
139
|
+
maxResults?: number;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* One semantic hit from `search`. `path` is absolute posix.
|
|
144
|
+
* `score` is higher-is-better. Trees without an index yield
|
|
145
|
+
* nothing; they do not invent lexical stand-ins.
|
|
146
|
+
*/
|
|
147
|
+
export interface SearchResult {
|
|
148
|
+
path: string;
|
|
149
|
+
snippet: string;
|
|
150
|
+
score: number;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Semantic-search query. `targetDirectories` limits the scan
|
|
155
|
+
* when an index exists; omitted means the whole tree.
|
|
156
|
+
*
|
|
157
|
+
* `maxResults` is optional predicate pushdown: stop after this
|
|
158
|
+
* many hits so an index does not retrieve a large recall set.
|
|
159
|
+
* Native SemanticSearch still applies `numResults` after the
|
|
160
|
+
* merge. Omitted means no FileSystem-level cap.
|
|
161
|
+
*/
|
|
162
|
+
export interface SearchQuery {
|
|
163
|
+
query: string;
|
|
164
|
+
targetDirectories?: string[];
|
|
165
|
+
maxResults?: number;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Exact, non-overlapping str-replace. `oldText` / `newText` are
|
|
170
|
+
* literal strings, not regex. Splice must keep `$`, `$&`, `$1`
|
|
171
|
+
* in `newText` as written (`String.prototype.replace` must not
|
|
172
|
+
* be used).
|
|
173
|
+
*/
|
|
174
|
+
export interface EditQuery {
|
|
175
|
+
path: string;
|
|
176
|
+
oldText: string;
|
|
177
|
+
newText: string;
|
|
178
|
+
replaceAll?: boolean;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* StrReplace success. `occurrencesModified` is the number of
|
|
183
|
+
* non-overlapping replacements (1 unless `replaceAll`).
|
|
184
|
+
*/
|
|
185
|
+
export interface EditResult {
|
|
186
|
+
occurrencesModified: number;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Path verbs for the tree the agent runs on.
|
|
191
|
+
*
|
|
192
|
+
* Every method is required. Trees without an index implement
|
|
193
|
+
* `search` as an empty iterator. Methods do not fall back to the
|
|
194
|
+
* host tree. Diagnostics are not a FileSystem concern.
|
|
195
|
+
*
|
|
196
|
+
* Paths are a single posix namespace rooted at `/`. Query paths
|
|
197
|
+
* and yielded paths are absolute (`/src/a.ts`). No trailing
|
|
198
|
+
* slash except `/`. `""`, `"/"`, and an omitted `prefix` / `dir`
|
|
199
|
+
* / `path` (where optional) mean the root. Prefix tests are
|
|
200
|
+
* path-segment: `/agent` matches `/agent/skills`, not
|
|
201
|
+
* `/agent-tools`.
|
|
202
|
+
*
|
|
203
|
+
* Async iterators are already sorted. `unionFs` k-way merges
|
|
204
|
+
* them and does not re-sort a source.
|
|
205
|
+
*
|
|
206
|
+
* - `list` / `glob`: lexicographic by `path`. Paths are unique.
|
|
207
|
+
* - `grep`: by `path`, then `lineNumber` ascending. Keep every hit.
|
|
208
|
+
* - `search`: by `score` descending, then `path` ascending.
|
|
209
|
+
*
|
|
210
|
+
* Native-tool pagination (`head_limit`, `offset`, `output_mode`,
|
|
211
|
+
* `numResults`) stays on the synthesized tools. `grep` /
|
|
212
|
+
* `search` take optional `maxResults` as predicate pushdown so
|
|
213
|
+
* a tree can stop retrieving once the tool's cap is known.
|
|
214
|
+
* Native `LS` is built from `list` + `stat`; there is no `ls`
|
|
215
|
+
* method.
|
|
216
|
+
*
|
|
217
|
+
* Errors (authors must follow these, not invent parallel
|
|
218
|
+
* spellings):
|
|
219
|
+
*
|
|
220
|
+
* - `list` / `stat` / `glob` must not throw for a missing path.
|
|
221
|
+
* Missing is an empty iterator or `{ kind: "missing" }`.
|
|
222
|
+
* - `read` / `write` / `delete` throw {@link PathError}
|
|
223
|
+
* for recoverable failures. `error.message` is the string the
|
|
224
|
+
* synthesized native tool shows the model. `error.path` is the
|
|
225
|
+
* absolute posix path the composer remaps.
|
|
226
|
+
* - `grep` throws for an invalid regex (and a missing host
|
|
227
|
+
* search root). A missing virtual path yields nothing.
|
|
228
|
+
* - `search` yields nothing when there is no index. An index
|
|
229
|
+
* failure may throw.
|
|
230
|
+
* - Missing-file message on `read` / `delete` is
|
|
231
|
+
* `not found: ${path}` (normalized absolute posix).
|
|
232
|
+
*/
|
|
233
|
+
export interface FileSystem {
|
|
234
|
+
/**
|
|
235
|
+
* Recursive file scan under `prefix`. Files only; directories
|
|
236
|
+
* are inferred from path prefixes. A file is not under itself,
|
|
237
|
+
* so listing a file path is empty. Empty is not "missing"
|
|
238
|
+
* (`stat` answers that). Yields lexicographic unique
|
|
239
|
+
* {@link DirEntry} rows. Omitted / `""` / `"/"` means the root.
|
|
240
|
+
*
|
|
241
|
+
* Errors: none. A missing or file-path prefix yields an empty
|
|
242
|
+
* iterator. Do not throw.
|
|
243
|
+
*/
|
|
244
|
+
list(prefix?: string): AsyncIterable<DirEntry>;
|
|
245
|
+
/**
|
|
246
|
+
* Existence probe: exact file, a prefix that has files, or
|
|
247
|
+
* missing. Does not throw. Native LS lists first and calls
|
|
248
|
+
* this only when the iterator is empty.
|
|
249
|
+
*
|
|
250
|
+
* Errors: none. Unknown paths are `{ kind: "missing" }`.
|
|
251
|
+
*/
|
|
252
|
+
stat(path: string): Promise<FileInfo>;
|
|
253
|
+
/**
|
|
254
|
+
* Same scan as `list`, filtered by `pattern` relative to
|
|
255
|
+
* `dir`. Yields lexicographic unique {@link DirEntry} rows.
|
|
256
|
+
* `dir` omitted / `""` / `"/"` means the tree root. Matching
|
|
257
|
+
* is relative to `dir`, not basename-anywhere (`*.ts` under
|
|
258
|
+
* `/src` is `/src/a.ts`, not `/src/nested/c.ts`).
|
|
259
|
+
*
|
|
260
|
+
* Errors: none. A missing `dir` or a pattern that matches
|
|
261
|
+
* nothing yields an empty iterator. Do not throw for a bad
|
|
262
|
+
* glob (it matches nothing).
|
|
263
|
+
*/
|
|
264
|
+
glob(pattern: string, dir?: string): AsyncIterable<DirEntry>;
|
|
265
|
+
/**
|
|
266
|
+
* Read the whole file. Text files return a string. Image
|
|
267
|
+
* bytes (`jpeg` / `png` / `gif` / `webp`) return a
|
|
268
|
+
* `Uint8Array` so native Read can base64 them into an image
|
|
269
|
+
* content part. Native Read applies `offset` / `limit` and
|
|
270
|
+
* numbers lines on text only.
|
|
271
|
+
*
|
|
272
|
+
* Errors (throw `Error`):
|
|
273
|
+
* - Missing file, or a virtual directory with no file inode:
|
|
274
|
+
* `not found: ${path}`.
|
|
275
|
+
* - Host I/O (EISDIR, EACCES, …): the OS message, or the same
|
|
276
|
+
* `not found: ${path}` fallback.
|
|
277
|
+
* - `unionFs` with no mount: `no mount for path: ${path}`.
|
|
278
|
+
*/
|
|
279
|
+
read(path: string): Promise<string | Uint8Array>;
|
|
280
|
+
/**
|
|
281
|
+
* Content matches. No output-mode or offset. Honor
|
|
282
|
+
* `maxResults` when set. Yields {@link GrepResult} ordered
|
|
283
|
+
* by path, then line number.
|
|
284
|
+
*
|
|
285
|
+
* Errors (throw `Error`):
|
|
286
|
+
* - Invalid `pattern`: `invalid grep pattern: ${pattern}`.
|
|
287
|
+
* - Host search root does not exist: the OS message, or
|
|
288
|
+
* `not found: ${searchRoot}`. Native Grep turns the throw
|
|
289
|
+
* into `{ status: "error" }`.
|
|
290
|
+
*
|
|
291
|
+
* A missing virtual `path` yields nothing (not an error).
|
|
292
|
+
*/
|
|
293
|
+
grep(query: GrepQuery): AsyncIterable<GrepResult>;
|
|
294
|
+
/**
|
|
295
|
+
* Create or overwrite a file. Parents are created as needed.
|
|
296
|
+
* Native Write formats `Wrote contents to ${path}` from the
|
|
297
|
+
* path. The Write tool maps native `fileText` onto `contents`.
|
|
298
|
+
*
|
|
299
|
+
* Errors (throw `Error`):
|
|
300
|
+
* - Host I/O: the OS message, or `write failed: ${path}`.
|
|
301
|
+
* - `unionFs` with no mount: `no mount for path: ${path}`.
|
|
302
|
+
* - A read-only mount may use `read-only mount` or
|
|
303
|
+
* `read-only mount: ${path}`.
|
|
304
|
+
*
|
|
305
|
+
* Missing parents are not an error; create them. There is no
|
|
306
|
+
* `not found` for `write`.
|
|
307
|
+
*/
|
|
308
|
+
write(path: string, contents: string): Promise<void>;
|
|
309
|
+
/**
|
|
310
|
+
* Remove one file. Directories are not deleted; listing them
|
|
311
|
+
* is empty and they have no inode of their own on a virtual
|
|
312
|
+
* tree.
|
|
313
|
+
*
|
|
314
|
+
* Errors (throw `Error`):
|
|
315
|
+
* - Missing file, or a virtual directory: `not found: ${path}`.
|
|
316
|
+
* - Host I/O: the OS message, or `not found: ${path}`.
|
|
317
|
+
* - `unionFs` with no mount: `no mount for path: ${path}`.
|
|
318
|
+
*/
|
|
319
|
+
delete(path: string): Promise<void>;
|
|
320
|
+
/**
|
|
321
|
+
* Semantic hits. Trees without an index yield nothing (do
|
|
322
|
+
* not register the host index). Honor `maxResults` when set.
|
|
323
|
+
* Ordered by score descending, then path.
|
|
324
|
+
*
|
|
325
|
+
* Errors: none for a tree without an index (empty iterator).
|
|
326
|
+
* An index that fails mid-scan may throw; native
|
|
327
|
+
* SemanticSearch turns that into `{ status: "error" }` with
|
|
328
|
+
* the thrown message or `search failed`.
|
|
329
|
+
*/
|
|
330
|
+
search(query: SearchQuery): AsyncIterable<SearchResult>;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export const FILE_SYSTEM_TOOL_NAMES = [
|
|
334
|
+
"Read",
|
|
335
|
+
"LS",
|
|
336
|
+
"Glob",
|
|
337
|
+
"Grep",
|
|
338
|
+
"Write",
|
|
339
|
+
"StrReplace",
|
|
340
|
+
"Delete",
|
|
341
|
+
"SemanticSearch",
|
|
342
|
+
] as const;
|
|
343
|
+
|
|
344
|
+
export type FileSystemToolName = (typeof FILE_SYSTEM_TOOL_NAMES)[number];
|
|
345
|
+
|
|
346
|
+
const FILE_SYSTEM_TOOL_NAME_SET = new Set<string>(FILE_SYSTEM_TOOL_NAMES);
|
|
347
|
+
|
|
348
|
+
const FILE_SYSTEM_TOOL_ALIASES: Readonly<Record<string, FileSystemToolName>> = {
|
|
349
|
+
read: "Read",
|
|
350
|
+
Read: "Read",
|
|
351
|
+
ls: "LS",
|
|
352
|
+
LS: "LS",
|
|
353
|
+
glob: "Glob",
|
|
354
|
+
Glob: "Glob",
|
|
355
|
+
grep: "Grep",
|
|
356
|
+
Grep: "Grep",
|
|
357
|
+
write: "Write",
|
|
358
|
+
Write: "Write",
|
|
359
|
+
edit: "StrReplace",
|
|
360
|
+
StrReplace: "StrReplace",
|
|
361
|
+
delete: "Delete",
|
|
362
|
+
Delete: "Delete",
|
|
363
|
+
semSearch: "SemanticSearch",
|
|
364
|
+
SemanticSearch: "SemanticSearch",
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
export function canonicalizeFileSystemToolName(
|
|
368
|
+
name: string
|
|
369
|
+
): FileSystemToolName | undefined {
|
|
370
|
+
return FILE_SYSTEM_TOOL_ALIASES[name];
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
export function isFileSystemToolName(name: string): name is FileSystemToolName {
|
|
374
|
+
return FILE_SYSTEM_TOOL_NAME_SET.has(name);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export function isFileSystemToolAlias(name: string): boolean {
|
|
378
|
+
return canonicalizeFileSystemToolName(name) !== undefined;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** Direct `callTool` names that mutate the tree and must wait on a busy turn. */
|
|
382
|
+
export function isMutatingFileSystemTool(name: string): boolean {
|
|
383
|
+
const canonical = canonicalizeFileSystemToolName(name);
|
|
384
|
+
return (
|
|
385
|
+
canonical === "Write" ||
|
|
386
|
+
canonical === "StrReplace" ||
|
|
387
|
+
canonical === "Delete"
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
export function isFileSystemMethodPresent(
|
|
392
|
+
root: FileSystem,
|
|
393
|
+
method: keyof FileSystem
|
|
394
|
+
): boolean {
|
|
395
|
+
return typeof root[method] === "function";
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const ADVERTISED_FILE_SYSTEM_TOOL_NAMES: FileSystemToolName[] = [
|
|
399
|
+
"Read",
|
|
400
|
+
"LS",
|
|
401
|
+
"Glob",
|
|
402
|
+
"Grep",
|
|
403
|
+
"Write",
|
|
404
|
+
"StrReplace",
|
|
405
|
+
"Delete",
|
|
406
|
+
"SemanticSearch",
|
|
407
|
+
];
|
|
408
|
+
|
|
409
|
+
export function fileSystemToolNames(_root?: FileSystem): FileSystemToolName[] {
|
|
410
|
+
return [...ADVERTISED_FILE_SYSTEM_TOOL_NAMES];
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function fileSystemMethodForTool(
|
|
414
|
+
name: FileSystemToolName
|
|
415
|
+
): keyof FileSystem | undefined {
|
|
416
|
+
switch (name) {
|
|
417
|
+
case "Read":
|
|
418
|
+
return "read";
|
|
419
|
+
case "LS":
|
|
420
|
+
return "list";
|
|
421
|
+
case "Glob":
|
|
422
|
+
return "glob";
|
|
423
|
+
case "Grep":
|
|
424
|
+
return "grep";
|
|
425
|
+
case "Write":
|
|
426
|
+
case "StrReplace":
|
|
427
|
+
return "write";
|
|
428
|
+
case "Delete":
|
|
429
|
+
return "delete";
|
|
430
|
+
case "SemanticSearch":
|
|
431
|
+
return "search";
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function fileSystemSatisfiesFileTool(root: FileSystem, name: string): boolean {
|
|
436
|
+
const canonical = canonicalizeFileSystemToolName(name);
|
|
437
|
+
if (canonical === undefined) {
|
|
438
|
+
return true;
|
|
439
|
+
}
|
|
440
|
+
const method = fileSystemMethodForTool(canonical);
|
|
441
|
+
return method !== undefined && isFileSystemMethodPresent(root, method);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
export class FileSystemConfigError extends Error {
|
|
445
|
+
constructor(message: string) {
|
|
446
|
+
super(message);
|
|
447
|
+
this.name = "FileSystemConfigError";
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
function allowlistHas(tools: readonly ToolName[], ...names: string[]): boolean {
|
|
452
|
+
return tools.some((name) => names.includes(name));
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
export function resolveRootHarnessAllowlist(
|
|
456
|
+
tools?: readonly ToolName[]
|
|
457
|
+
): ToolName[] {
|
|
458
|
+
if (tools === undefined) {
|
|
459
|
+
// File verbs are synthesized, including SemanticSearch from
|
|
460
|
+
// `search`. Web tools are safe under the `/tmp` scratch
|
|
461
|
+
// overlay. Todos are harness conversation state. Do not
|
|
462
|
+
// unlock shell or task.
|
|
463
|
+
return ["webSearch", "webFetch", "updateTodos", "readTodos", "mcp"];
|
|
464
|
+
}
|
|
465
|
+
if (allowlistHas(tools, "shell", "Shell")) {
|
|
466
|
+
throw new FileSystemConfigError(
|
|
467
|
+
'`root` cannot be combined with `tools` containing `"shell"`. ' +
|
|
468
|
+
"Shell is not a FileSystem method and must not bind the host shell onto a virtual tree."
|
|
469
|
+
);
|
|
470
|
+
}
|
|
471
|
+
if (allowlistHas(tools, "task", "Task")) {
|
|
472
|
+
throw new FileSystemConfigError(
|
|
473
|
+
"`root` cannot list `task`: a child keeps the host toolset and " +
|
|
474
|
+
"would read the real workspace. Sharing `root` with subagents " +
|
|
475
|
+
"needs a Cursor SDK inherit, not an overridden Task tool."
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
if (allowlistHas(tools, "readLints", "ReadLints")) {
|
|
479
|
+
throw new FileSystemConfigError(
|
|
480
|
+
"`root` cannot list `readLints`: FileSystem has no diagnostics " +
|
|
481
|
+
"verb, so the host language service would run against the real workspace."
|
|
482
|
+
);
|
|
483
|
+
}
|
|
484
|
+
const extras = tools.filter((name) => !isFileSystemToolAlias(name));
|
|
485
|
+
return withMcpGrant(extras);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
export function resolveFileSystemTools(options: {
|
|
489
|
+
root: FileSystem;
|
|
490
|
+
tools?: readonly ToolName[];
|
|
491
|
+
}): ToolName[] {
|
|
492
|
+
const harness = resolveRootHarnessAllowlist(options.tools);
|
|
493
|
+
if (options.tools === undefined) {
|
|
494
|
+
return harness;
|
|
495
|
+
}
|
|
496
|
+
const implemented = fileSystemToolNames(options.root);
|
|
497
|
+
const missing: string[] = [];
|
|
498
|
+
for (const name of options.tools) {
|
|
499
|
+
if (!fileSystemSatisfiesFileTool(options.root, name)) {
|
|
500
|
+
missing.push(name);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
if (missing.length > 0) {
|
|
504
|
+
throw new FileSystemConfigError(
|
|
505
|
+
`\`tools\` names FileSystem method(s) that are not implemented: ${missing.join(", ")}. ` +
|
|
506
|
+
`Implemented file tools: ${implemented.length > 0 ? implemented.join(", ") : "(none)"}.`
|
|
507
|
+
);
|
|
508
|
+
}
|
|
509
|
+
return harness;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
export function withMcpGrant(tools: readonly ToolName[]): ToolName[] {
|
|
513
|
+
return tools.includes("mcp") ? [...tools] : [...tools, "mcp"];
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Native Cursor SDK file tools replaced by synthesized FileSystem tools
|
|
518
|
+
* on every local turn. SDK allowlist names (`edit`, not `Write`).
|
|
519
|
+
*/
|
|
520
|
+
export const REPLACED_NATIVE_FILE_TOOLS: ToolName[] = [
|
|
521
|
+
"read",
|
|
522
|
+
"ls",
|
|
523
|
+
"grep",
|
|
524
|
+
"glob",
|
|
525
|
+
"edit",
|
|
526
|
+
"delete",
|
|
527
|
+
];
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Host-default harness options: keep shell / webSearch / sandbox, but
|
|
531
|
+
* hide the native file tools the synthesized FileSystem now owns.
|
|
532
|
+
*
|
|
533
|
+
* Omitted `tools` leaves the SDK default set and only excludes the
|
|
534
|
+
* replaced natives. `[]` stays empty. A non-empty allowlist drops file
|
|
535
|
+
* aliases (those ride synthesized tools) and grants `mcp`.
|
|
536
|
+
*/
|
|
537
|
+
export function resolveHostHarnessToolOptions(tools?: readonly ToolName[]): {
|
|
538
|
+
tools?: ToolName[];
|
|
539
|
+
disallowedTools?: ToolName[];
|
|
540
|
+
} {
|
|
541
|
+
if (tools === undefined) {
|
|
542
|
+
return { disallowedTools: [...REPLACED_NATIVE_FILE_TOOLS] };
|
|
543
|
+
}
|
|
544
|
+
if (tools.length === 0) {
|
|
545
|
+
return { tools: [] };
|
|
546
|
+
}
|
|
547
|
+
return {
|
|
548
|
+
tools: withMcpGrant(tools.filter((name) => !isReplacedHostFileAlias(name))),
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
/** File aliases host-default synthesizes. Native `semSearch` stays. */
|
|
553
|
+
function isReplacedHostFileAlias(name: string): boolean {
|
|
554
|
+
const canonical = canonicalizeFileSystemToolName(name);
|
|
555
|
+
return canonical !== undefined && canonical !== "SemanticSearch";
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
export type GrepOutputMode = "content" | "files_with_matches" | "count";
|
|
559
|
+
|
|
560
|
+
/** Decode a {@link FileSystem.read} result as UTF-8 text. */
|
|
561
|
+
export function fileSystemText(content: string | Uint8Array): string {
|
|
562
|
+
return typeof content === "string"
|
|
563
|
+
? content
|
|
564
|
+
: Buffer.from(content).toString("utf8");
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
/**
|
|
568
|
+
* Native Read image formats. Magic bytes first, then extension.
|
|
569
|
+
* Returns undefined for text (and for any other binary).
|
|
570
|
+
*/
|
|
571
|
+
export function detectImageMimeType(
|
|
572
|
+
bytes: Uint8Array,
|
|
573
|
+
filePath?: string
|
|
574
|
+
): string | undefined {
|
|
575
|
+
if (
|
|
576
|
+
bytes.length >= 3 &&
|
|
577
|
+
bytes[0] === 0xff &&
|
|
578
|
+
bytes[1] === 0xd8 &&
|
|
579
|
+
bytes[2] === 0xff
|
|
580
|
+
) {
|
|
581
|
+
return "image/jpeg";
|
|
582
|
+
}
|
|
583
|
+
if (
|
|
584
|
+
bytes.length >= 8 &&
|
|
585
|
+
bytes[0] === 0x89 &&
|
|
586
|
+
bytes[1] === 0x50 &&
|
|
587
|
+
bytes[2] === 0x4e &&
|
|
588
|
+
bytes[3] === 0x47
|
|
589
|
+
) {
|
|
590
|
+
return "image/png";
|
|
591
|
+
}
|
|
592
|
+
if (
|
|
593
|
+
bytes.length >= 6 &&
|
|
594
|
+
bytes[0] === 0x47 &&
|
|
595
|
+
bytes[1] === 0x49 &&
|
|
596
|
+
bytes[2] === 0x46
|
|
597
|
+
) {
|
|
598
|
+
return "image/gif";
|
|
599
|
+
}
|
|
600
|
+
if (
|
|
601
|
+
bytes.length >= 12 &&
|
|
602
|
+
bytes[0] === 0x52 &&
|
|
603
|
+
bytes[1] === 0x49 &&
|
|
604
|
+
bytes[2] === 0x46 &&
|
|
605
|
+
bytes[3] === 0x46 &&
|
|
606
|
+
bytes[8] === 0x57 &&
|
|
607
|
+
bytes[9] === 0x45 &&
|
|
608
|
+
bytes[10] === 0x42 &&
|
|
609
|
+
bytes[11] === 0x50
|
|
610
|
+
) {
|
|
611
|
+
return "image/webp";
|
|
612
|
+
}
|
|
613
|
+
if (filePath === undefined) {
|
|
614
|
+
return undefined;
|
|
615
|
+
}
|
|
616
|
+
const lower = filePath.toLowerCase();
|
|
617
|
+
if (lower.endsWith(".jpg") || lower.endsWith(".jpeg")) {
|
|
618
|
+
return "image/jpeg";
|
|
619
|
+
}
|
|
620
|
+
if (lower.endsWith(".png")) {
|
|
621
|
+
return "image/png";
|
|
622
|
+
}
|
|
623
|
+
if (lower.endsWith(".gif")) {
|
|
624
|
+
return "image/gif";
|
|
625
|
+
}
|
|
626
|
+
if (lower.endsWith(".webp")) {
|
|
627
|
+
return "image/webp";
|
|
628
|
+
}
|
|
629
|
+
return undefined;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
export function fileSystemImage(
|
|
633
|
+
content: string | Uint8Array,
|
|
634
|
+
path: string
|
|
635
|
+
): { mimeType: string; bytes: Uint8Array } | undefined {
|
|
636
|
+
if (!(content instanceof Uint8Array)) {
|
|
637
|
+
return undefined;
|
|
638
|
+
}
|
|
639
|
+
const mimeType = detectImageMimeType(content, path);
|
|
640
|
+
if (mimeType === undefined) {
|
|
641
|
+
return undefined;
|
|
642
|
+
}
|
|
643
|
+
return { mimeType, bytes: content };
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
function compileGrepPattern(
|
|
647
|
+
pattern: string,
|
|
648
|
+
caseInsensitive?: boolean,
|
|
649
|
+
path = "/"
|
|
650
|
+
): RegExp {
|
|
651
|
+
try {
|
|
652
|
+
return new RegExp(pattern, caseInsensitive === true ? "i" : undefined);
|
|
653
|
+
} catch {
|
|
654
|
+
throw new InvalidGrepPatternError(pattern, path);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
function* grepFileContent(args: {
|
|
659
|
+
path: string;
|
|
660
|
+
content: string;
|
|
661
|
+
regex: RegExp;
|
|
662
|
+
}): Generator<GrepResult> {
|
|
663
|
+
const lines = args.content.split("\n");
|
|
664
|
+
for (let i = 0; i < lines.length; i++) {
|
|
665
|
+
const line = lines[i]!;
|
|
666
|
+
if (args.regex.test(line)) {
|
|
667
|
+
yield { path: args.path, lineNumber: i + 1, line };
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
function normalizeMaxResults(
|
|
673
|
+
maxResults: number | undefined
|
|
674
|
+
): number | undefined {
|
|
675
|
+
if (maxResults === undefined || !Number.isFinite(maxResults)) {
|
|
676
|
+
return undefined;
|
|
677
|
+
}
|
|
678
|
+
return Math.max(0, Math.floor(maxResults));
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
async function* limitResults<T>(
|
|
682
|
+
source: AsyncIterable<T>,
|
|
683
|
+
maxResults: number | undefined
|
|
684
|
+
): AsyncGenerator<T> {
|
|
685
|
+
const cap = normalizeMaxResults(maxResults);
|
|
686
|
+
if (cap === 0) {
|
|
687
|
+
return;
|
|
688
|
+
}
|
|
689
|
+
let n = 0;
|
|
690
|
+
for await (const item of source) {
|
|
691
|
+
yield item;
|
|
692
|
+
n++;
|
|
693
|
+
if (cap !== undefined && n >= cap) {
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
|
|
699
|
+
function grepPushdownLimit(
|
|
700
|
+
query: GrepQuery,
|
|
701
|
+
opts: { outputMode: GrepOutputMode; headLimit?: number; offset: number }
|
|
702
|
+
): number | undefined {
|
|
703
|
+
// Count and files_with_matches paginate after grouping by file.
|
|
704
|
+
// A line-level cap would hide later files once one file fills the window.
|
|
705
|
+
if (opts.outputMode === "count" || opts.outputMode === "files_with_matches") {
|
|
706
|
+
return query.maxResults;
|
|
707
|
+
}
|
|
708
|
+
const headLimit =
|
|
709
|
+
opts.headLimit !== undefined && opts.headLimit > 0
|
|
710
|
+
? opts.headLimit
|
|
711
|
+
: undefined;
|
|
712
|
+
if (headLimit === undefined) {
|
|
713
|
+
return query.maxResults;
|
|
714
|
+
}
|
|
715
|
+
const toolCap = opts.offset + headLimit;
|
|
716
|
+
return query.maxResults === undefined
|
|
717
|
+
? toolCap
|
|
718
|
+
: Math.min(query.maxResults, toolCap);
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
function compareGrepResult(left: GrepResult, right: GrepResult): number {
|
|
722
|
+
return left.path < right.path
|
|
723
|
+
? -1
|
|
724
|
+
: left.path > right.path
|
|
725
|
+
? 1
|
|
726
|
+
: left.lineNumber - right.lineNumber;
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
function compareSearchResult(left: SearchResult, right: SearchResult): number {
|
|
730
|
+
if (right.score !== left.score) {
|
|
731
|
+
return right.score - left.score;
|
|
732
|
+
}
|
|
733
|
+
return left.path < right.path ? -1 : left.path > right.path ? 1 : 0;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
export async function collectGrepMatches(
|
|
737
|
+
root: FileSystem,
|
|
738
|
+
query: GrepQuery,
|
|
739
|
+
opts: { outputMode: GrepOutputMode; headLimit?: number; offset?: number }
|
|
740
|
+
): Promise<GrepResult[]> {
|
|
741
|
+
const matches: GrepResult[] = [];
|
|
742
|
+
const offset = opts.offset !== undefined && opts.offset > 0 ? opts.offset : 0;
|
|
743
|
+
const headLimit =
|
|
744
|
+
opts.headLimit !== undefined && opts.headLimit > 0
|
|
745
|
+
? opts.headLimit
|
|
746
|
+
: undefined;
|
|
747
|
+
const skippedFiles = new Set<string>();
|
|
748
|
+
const acceptedFiles = new Set<string>();
|
|
749
|
+
let skippedItems = 0;
|
|
750
|
+
let takenItems = 0;
|
|
751
|
+
const pushdown = grepPushdownLimit(query, {
|
|
752
|
+
outputMode: opts.outputMode,
|
|
753
|
+
offset,
|
|
754
|
+
...(opts.headLimit === undefined ? {} : { headLimit: opts.headLimit }),
|
|
755
|
+
});
|
|
756
|
+
const grepQuery =
|
|
757
|
+
pushdown === undefined ? query : { ...query, maxResults: pushdown };
|
|
758
|
+
for await (const match of root.grep(grepQuery)) {
|
|
759
|
+
if (opts.outputMode === "content") {
|
|
760
|
+
if (skippedItems < offset) {
|
|
761
|
+
skippedItems++;
|
|
762
|
+
continue;
|
|
763
|
+
}
|
|
764
|
+
matches.push(match);
|
|
765
|
+
takenItems++;
|
|
766
|
+
if (headLimit !== undefined && takenItems >= headLimit) {
|
|
767
|
+
break;
|
|
768
|
+
}
|
|
769
|
+
continue;
|
|
770
|
+
}
|
|
771
|
+
if (skippedFiles.has(match.path)) {
|
|
772
|
+
continue;
|
|
773
|
+
}
|
|
774
|
+
if (!acceptedFiles.has(match.path)) {
|
|
775
|
+
if (skippedItems < offset) {
|
|
776
|
+
skippedFiles.add(match.path);
|
|
777
|
+
skippedItems++;
|
|
778
|
+
continue;
|
|
779
|
+
}
|
|
780
|
+
if (headLimit !== undefined && takenItems >= headLimit) {
|
|
781
|
+
break;
|
|
782
|
+
}
|
|
783
|
+
acceptedFiles.add(match.path);
|
|
784
|
+
takenItems++;
|
|
785
|
+
}
|
|
786
|
+
matches.push(match);
|
|
787
|
+
}
|
|
788
|
+
return matches;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
export type GrepContentHit = GrepResult & {
|
|
792
|
+
beforeContext?: string[];
|
|
793
|
+
afterContext?: string[];
|
|
794
|
+
};
|
|
795
|
+
|
|
796
|
+
export function shapeGrepUnion(
|
|
797
|
+
matches: GrepContentHit[],
|
|
798
|
+
outputMode: GrepOutputMode
|
|
799
|
+
): GrepUnionResult {
|
|
800
|
+
if (outputMode === "files_with_matches") {
|
|
801
|
+
const files = [...new Set(matches.map((match) => match.path))];
|
|
802
|
+
return { type: "files", output: { files, count: files.length } };
|
|
803
|
+
}
|
|
804
|
+
if (outputMode === "count") {
|
|
805
|
+
const counts = new Map<string, number>();
|
|
806
|
+
for (const match of matches) {
|
|
807
|
+
counts.set(match.path, (counts.get(match.path) ?? 0) + 1);
|
|
808
|
+
}
|
|
809
|
+
return {
|
|
810
|
+
type: "count",
|
|
811
|
+
output: {
|
|
812
|
+
counts: [...counts].map(([file, count]) => ({ file, count })),
|
|
813
|
+
total: matches.length,
|
|
814
|
+
},
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
return {
|
|
818
|
+
type: "content",
|
|
819
|
+
output: {
|
|
820
|
+
matches: matches.map((match) => ({
|
|
821
|
+
file: match.path,
|
|
822
|
+
lineNumber: match.lineNumber,
|
|
823
|
+
line: match.line,
|
|
824
|
+
...(match.beforeContext === undefined
|
|
825
|
+
? {}
|
|
826
|
+
: { beforeContext: match.beforeContext }),
|
|
827
|
+
...(match.afterContext === undefined
|
|
828
|
+
? {}
|
|
829
|
+
: { afterContext: match.afterContext }),
|
|
830
|
+
})),
|
|
831
|
+
totalMatches: matches.length,
|
|
832
|
+
},
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
export function normalizeFsPath(input: string): string {
|
|
837
|
+
const slashes = input.replace(/\\/g, "/");
|
|
838
|
+
if (slashes === "") {
|
|
839
|
+
return "/";
|
|
840
|
+
}
|
|
841
|
+
const withSlash = slashes.startsWith("/") ? slashes : `/${slashes}`;
|
|
842
|
+
const resolved = path.posix.normalize(withSlash);
|
|
843
|
+
if (resolved.length > 1 && resolved.endsWith("/")) {
|
|
844
|
+
return resolved.slice(0, -1);
|
|
845
|
+
}
|
|
846
|
+
return resolved === "." || resolved === "" ? "/" : resolved;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
/** Path-segment prefix: `/agent` matches `/agent/skills`, not `/agent-tools`. */
|
|
850
|
+
export function pathHasSegmentPrefix(args: {
|
|
851
|
+
path: string;
|
|
852
|
+
prefix: string;
|
|
853
|
+
}): boolean {
|
|
854
|
+
const p = normalizeFsPath(args.path);
|
|
855
|
+
const pre = normalizeFsPath(args.prefix);
|
|
856
|
+
if (pre === "/") {
|
|
857
|
+
return true;
|
|
858
|
+
}
|
|
859
|
+
return p === pre || p.startsWith(`${pre}/`);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
function matchGlob(relativePath: string, globPattern: string): boolean {
|
|
863
|
+
return minimatch(relativePath, globPattern, { dot: true, nocase: false });
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
function pathMatchesGlob(
|
|
867
|
+
filePath: string,
|
|
868
|
+
dir: string,
|
|
869
|
+
globPattern: string,
|
|
870
|
+
options: { matchBasename?: boolean } = {}
|
|
871
|
+
): boolean {
|
|
872
|
+
const posixFile = filePath.replace(/\\/g, "/");
|
|
873
|
+
const posixDir = dir.replace(/\\/g, "/");
|
|
874
|
+
const relative =
|
|
875
|
+
posixDir === "/" || posixFile.startsWith(`${posixDir}/`)
|
|
876
|
+
? posixDir === "/"
|
|
877
|
+
? posixFile.replace(/^\//, "")
|
|
878
|
+
: posixFile.slice(posixDir.length + 1)
|
|
879
|
+
: posixFile.replace(/^\//, "");
|
|
880
|
+
if (
|
|
881
|
+
matchGlob(relative, globPattern) ||
|
|
882
|
+
matchGlob(posixFile.replace(/^\//, ""), globPattern)
|
|
883
|
+
) {
|
|
884
|
+
return true;
|
|
885
|
+
}
|
|
886
|
+
// Native grep `--glob '*.ts'` matches the basename at any depth.
|
|
887
|
+
// Glob `*.ts` stays relative to `targetDirectory` and does not.
|
|
888
|
+
if (options.matchBasename === true && !globPattern.includes("/")) {
|
|
889
|
+
const slash = relative.lastIndexOf("/");
|
|
890
|
+
const base = slash === -1 ? relative : relative.slice(slash + 1);
|
|
891
|
+
return matchGlob(base, globPattern);
|
|
892
|
+
}
|
|
893
|
+
return false;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
/**
|
|
897
|
+
* Resolve a per-send `workspaceFiles` key onto the virtual `/` namespace.
|
|
898
|
+
* Refuses paths that escape the root, and the reserved `memory` name.
|
|
899
|
+
* Host-default workspaces symlink `memory/` to durable agent state, so a
|
|
900
|
+
* seed under that name would write through the link.
|
|
901
|
+
*/
|
|
902
|
+
export function virtualWorkspaceWritePath(relativePath: string): string {
|
|
903
|
+
if (relativePath.includes("\0")) {
|
|
904
|
+
throw new FileSystemConfigError(
|
|
905
|
+
`Refusing to write outside the virtual root: ${relativePath}`
|
|
906
|
+
);
|
|
907
|
+
}
|
|
908
|
+
const posix = relativePath.replaceAll("\\", "/");
|
|
909
|
+
const base = "/virtual-root";
|
|
910
|
+
const target = path.posix.normalize(path.posix.join(base, posix));
|
|
911
|
+
if (target !== base && !target.startsWith(`${base}/`)) {
|
|
912
|
+
throw new FileSystemConfigError(
|
|
913
|
+
`Refusing to write outside the virtual root: ${relativePath}`
|
|
914
|
+
);
|
|
915
|
+
}
|
|
916
|
+
const sliced = target.slice(base.length);
|
|
917
|
+
const dest = sliced === "" ? "/" : sliced;
|
|
918
|
+
if (
|
|
919
|
+
pathHasSegmentPrefix({
|
|
920
|
+
path: dest,
|
|
921
|
+
prefix: `/${MEMORY_DIR_NAME}`,
|
|
922
|
+
})
|
|
923
|
+
) {
|
|
924
|
+
throw new FileSystemConfigError(
|
|
925
|
+
`"${MEMORY_DIR_NAME}" is reserved for shared session memory; refusing to write ${relativePath}`
|
|
926
|
+
);
|
|
927
|
+
}
|
|
928
|
+
return dest;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
export async function writeWorkspaceFilesToRoot(
|
|
932
|
+
root: FileSystem,
|
|
933
|
+
files: Record<
|
|
934
|
+
string,
|
|
935
|
+
string | { encoding: "base64"; data: string; mimeType?: string }
|
|
936
|
+
>
|
|
937
|
+
): Promise<void> {
|
|
938
|
+
for (const [relativePath, contents] of Object.entries(files)) {
|
|
939
|
+
// Refuse escapes, then write the sanitized relative dest so
|
|
940
|
+
// `localFileSystem` joins against cwd and `tempFs` still normalizes
|
|
941
|
+
// `a.txt` → `/a.txt`. Never forward an absolute host path.
|
|
942
|
+
const dest = virtualWorkspaceWritePath(relativePath);
|
|
943
|
+
const relativeDest = dest.replace(/^\//, "");
|
|
944
|
+
if (relativeDest === "") {
|
|
945
|
+
throw new FileSystemConfigError(
|
|
946
|
+
`Refusing to write the virtual root: ${relativePath}`
|
|
947
|
+
);
|
|
948
|
+
}
|
|
949
|
+
const fileText =
|
|
950
|
+
typeof contents === "string"
|
|
951
|
+
? contents
|
|
952
|
+
: Buffer.from(contents.data, "base64").toString("utf8");
|
|
953
|
+
try {
|
|
954
|
+
await root.write(relativeDest, fileText);
|
|
955
|
+
} catch (error) {
|
|
956
|
+
throw new FileSystemConfigError(
|
|
957
|
+
`workspaceFiles failed to write ${relativePath}: ${
|
|
958
|
+
error instanceof Error ? error.message : "write failed"
|
|
959
|
+
}`
|
|
960
|
+
);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
function replaceLiteral(args: {
|
|
966
|
+
existing: string;
|
|
967
|
+
oldText: string;
|
|
968
|
+
newText: string;
|
|
969
|
+
replaceAll: boolean;
|
|
970
|
+
}): string {
|
|
971
|
+
if (args.replaceAll) {
|
|
972
|
+
return args.existing.split(args.oldText).join(args.newText);
|
|
973
|
+
}
|
|
974
|
+
const index = args.existing.indexOf(args.oldText);
|
|
975
|
+
if (index === -1) {
|
|
976
|
+
return args.existing;
|
|
977
|
+
}
|
|
978
|
+
return (
|
|
979
|
+
args.existing.slice(0, index) +
|
|
980
|
+
args.newText +
|
|
981
|
+
args.existing.slice(index + args.oldText.length)
|
|
982
|
+
);
|
|
983
|
+
}
|
|
984
|
+
|
|
985
|
+
function countNonOverlapping(haystack: string, needle: string): number {
|
|
986
|
+
let count = 0;
|
|
987
|
+
let from = 0;
|
|
988
|
+
while (from <= haystack.length - needle.length) {
|
|
989
|
+
const index = haystack.indexOf(needle, from);
|
|
990
|
+
if (index === -1) {
|
|
991
|
+
break;
|
|
992
|
+
}
|
|
993
|
+
count++;
|
|
994
|
+
from = index + needle.length;
|
|
995
|
+
}
|
|
996
|
+
return count;
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
/**
|
|
1000
|
+
* Exact, non-overlapping str-replace. Uniqueness is the FileSystem
|
|
1001
|
+
* contract: more than one match fails unless `replaceAll`.
|
|
1002
|
+
*/
|
|
1003
|
+
function applyExactEdit(args: {
|
|
1004
|
+
existing: string;
|
|
1005
|
+
oldText: string;
|
|
1006
|
+
newText: string;
|
|
1007
|
+
replaceAll: boolean;
|
|
1008
|
+
path: string;
|
|
1009
|
+
}): { contents: string; occurrencesModified: number } {
|
|
1010
|
+
if (args.oldText === "") {
|
|
1011
|
+
throw new EditError(args.path, "oldText is empty");
|
|
1012
|
+
}
|
|
1013
|
+
if (args.oldText === args.newText) {
|
|
1014
|
+
throw new EditError(args.path, "oldText and newText are exactly the same");
|
|
1015
|
+
}
|
|
1016
|
+
const occurrences = countNonOverlapping(args.existing, args.oldText);
|
|
1017
|
+
if (occurrences === 0) {
|
|
1018
|
+
throw new EditError(
|
|
1019
|
+
args.path,
|
|
1020
|
+
"The string to replace was not found in the file"
|
|
1021
|
+
);
|
|
1022
|
+
}
|
|
1023
|
+
if (occurrences > 1 && !args.replaceAll) {
|
|
1024
|
+
throw new EditError(
|
|
1025
|
+
args.path,
|
|
1026
|
+
"The string to replace was found multiple times in the file. Use replace_all to replace all occurrences, or include more context to only edit one occurrence."
|
|
1027
|
+
);
|
|
1028
|
+
}
|
|
1029
|
+
return {
|
|
1030
|
+
contents: replaceLiteral({
|
|
1031
|
+
existing: args.existing,
|
|
1032
|
+
oldText: args.oldText,
|
|
1033
|
+
newText: args.newText,
|
|
1034
|
+
replaceAll: args.replaceAll,
|
|
1035
|
+
}),
|
|
1036
|
+
occurrencesModified: occurrences,
|
|
1037
|
+
};
|
|
1038
|
+
}
|
|
1039
|
+
|
|
1040
|
+
/**
|
|
1041
|
+
* Exact str-replace as `read` + apply + `write`. Uniqueness is
|
|
1042
|
+
* this helper's contract; FileSystem implementations do not
|
|
1043
|
+
* implement edit. Native StrReplace calls this.
|
|
1044
|
+
*/
|
|
1045
|
+
export async function strReplaceOnFileSystem(
|
|
1046
|
+
root: Pick<FileSystem, "read" | "write">,
|
|
1047
|
+
query: EditQuery
|
|
1048
|
+
): Promise<EditResult> {
|
|
1049
|
+
const existing = await root.read(query.path);
|
|
1050
|
+
if (typeof existing !== "string") {
|
|
1051
|
+
throw new EditError(query.path, `${query.path} is not text`);
|
|
1052
|
+
}
|
|
1053
|
+
const applied = applyExactEdit({
|
|
1054
|
+
existing,
|
|
1055
|
+
oldText: query.oldText,
|
|
1056
|
+
newText: query.newText,
|
|
1057
|
+
replaceAll: query.replaceAll === true,
|
|
1058
|
+
path: query.path,
|
|
1059
|
+
});
|
|
1060
|
+
await root.write(query.path, applied.contents);
|
|
1061
|
+
return { occurrencesModified: applied.occurrencesModified };
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
/** Native Read offset/limit: 1-indexed, negative offset counts from the end. */
|
|
1065
|
+
export function sliceReadContent(
|
|
1066
|
+
content: string,
|
|
1067
|
+
offset?: number,
|
|
1068
|
+
limit?: number
|
|
1069
|
+
): string {
|
|
1070
|
+
if (offset === undefined && limit === undefined) {
|
|
1071
|
+
return content;
|
|
1072
|
+
}
|
|
1073
|
+
const lines = content === "" ? [] : content.split("\n");
|
|
1074
|
+
const total = lines.length;
|
|
1075
|
+
const effectiveOffset = offset ?? 1;
|
|
1076
|
+
const effectiveLimit =
|
|
1077
|
+
limit ?? (effectiveOffset < 0 ? Math.abs(effectiveOffset) : total);
|
|
1078
|
+
const startIndex =
|
|
1079
|
+
effectiveOffset < 0
|
|
1080
|
+
? Math.max(0, total + effectiveOffset)
|
|
1081
|
+
: Math.max(0, effectiveOffset - 1);
|
|
1082
|
+
const endIndex = Math.min(total, startIndex + effectiveLimit);
|
|
1083
|
+
return lines.slice(startIndex, endIndex).join("\n");
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
async function* emptySearch(
|
|
1087
|
+
_query: SearchQuery
|
|
1088
|
+
): AsyncGenerator<SearchResult> {}
|
|
1089
|
+
|
|
1090
|
+
async function* emptyStream(): AsyncGenerator<never> {}
|
|
1091
|
+
|
|
1092
|
+
/**
|
|
1093
|
+
* A FileSystem with no files. Use as an overlay leaf or a missing
|
|
1094
|
+
* catalog so a miss stays {@link FileNotFoundError} instead of
|
|
1095
|
+
* {@link NoMountError}. Writes and deletes throw
|
|
1096
|
+
* {@link ReadOnlyMountError} — an empty tree is never a sink.
|
|
1097
|
+
*/
|
|
1098
|
+
export function emptyFs(): FileSystem {
|
|
1099
|
+
return {
|
|
1100
|
+
list: () => emptyStream(),
|
|
1101
|
+
glob: () => emptyStream(),
|
|
1102
|
+
async *grep(query) {
|
|
1103
|
+
compileGrepPattern(
|
|
1104
|
+
query.pattern,
|
|
1105
|
+
query.caseInsensitive,
|
|
1106
|
+
query.path === undefined || query.path === ""
|
|
1107
|
+
? "/"
|
|
1108
|
+
: normalizeFsPath(query.path)
|
|
1109
|
+
);
|
|
1110
|
+
yield* emptyStream();
|
|
1111
|
+
},
|
|
1112
|
+
search: emptySearch,
|
|
1113
|
+
async stat(path) {
|
|
1114
|
+
return statFromFilePaths([], path);
|
|
1115
|
+
},
|
|
1116
|
+
async read(path) {
|
|
1117
|
+
throw new FileNotFoundError(normalizeFsPath(path));
|
|
1118
|
+
},
|
|
1119
|
+
async write(path) {
|
|
1120
|
+
throw new ReadOnlyMountError(normalizeFsPath(path));
|
|
1121
|
+
},
|
|
1122
|
+
async delete(path) {
|
|
1123
|
+
throw new ReadOnlyMountError(normalizeFsPath(path));
|
|
1124
|
+
},
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
/**
|
|
1129
|
+
* In-memory FileSystem for tests and fixtures. Keys are absolute
|
|
1130
|
+
* posix paths (`/a.txt`) and are normalized on ingest. Honors the
|
|
1131
|
+
* {@link FileSystem} order and path contracts.
|
|
1132
|
+
* `search` is empty. Creates `/agent-tools` on first scratch
|
|
1133
|
+
* write the same way any other path is created.
|
|
1134
|
+
*/
|
|
1135
|
+
export function tempFs(
|
|
1136
|
+
files: Record<string, string | Uint8Array> = {}
|
|
1137
|
+
): FileSystem {
|
|
1138
|
+
const store = new Map<string, string | Uint8Array>();
|
|
1139
|
+
for (const [path, content] of Object.entries(files)) {
|
|
1140
|
+
store.set(normalizeFsPath(path), content);
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
const listed = {
|
|
1144
|
+
list(prefix?: string) {
|
|
1145
|
+
return listFilesFromPaths(
|
|
1146
|
+
[...store.entries()].map(([filePath, content]) => ({
|
|
1147
|
+
path: filePath,
|
|
1148
|
+
size:
|
|
1149
|
+
typeof content === "string" ? content.length : content.byteLength,
|
|
1150
|
+
})),
|
|
1151
|
+
prefix
|
|
1152
|
+
);
|
|
1153
|
+
},
|
|
1154
|
+
async stat(path: string) {
|
|
1155
|
+
return statFromFilePaths(store.keys(), path);
|
|
1156
|
+
},
|
|
1157
|
+
};
|
|
1158
|
+
|
|
1159
|
+
return {
|
|
1160
|
+
list(prefix) {
|
|
1161
|
+
return listed.list(prefix);
|
|
1162
|
+
},
|
|
1163
|
+
async stat(path) {
|
|
1164
|
+
return listed.stat(path);
|
|
1165
|
+
},
|
|
1166
|
+
async read(inputPath) {
|
|
1167
|
+
const path = normalizeFsPath(inputPath);
|
|
1168
|
+
const content = store.get(path);
|
|
1169
|
+
if (content === undefined) {
|
|
1170
|
+
throw new FileNotFoundError(path);
|
|
1171
|
+
}
|
|
1172
|
+
return content;
|
|
1173
|
+
},
|
|
1174
|
+
async *grep(query) {
|
|
1175
|
+
yield* limitResults(uncapped(), query.maxResults);
|
|
1176
|
+
async function* uncapped() {
|
|
1177
|
+
const regex = compileGrepPattern(
|
|
1178
|
+
query.pattern,
|
|
1179
|
+
query.caseInsensitive,
|
|
1180
|
+
query.path === undefined || query.path === ""
|
|
1181
|
+
? "/"
|
|
1182
|
+
: normalizeFsPath(query.path)
|
|
1183
|
+
);
|
|
1184
|
+
const scope =
|
|
1185
|
+
query.path === undefined || query.path === ""
|
|
1186
|
+
? "/"
|
|
1187
|
+
: normalizeFsPath(query.path);
|
|
1188
|
+
const exact = store.get(scope);
|
|
1189
|
+
if (scope !== "/" && exact !== undefined) {
|
|
1190
|
+
if (
|
|
1191
|
+
query.glob !== undefined &&
|
|
1192
|
+
!pathMatchesGlob(scope, scope, query.glob, {
|
|
1193
|
+
matchBasename: true,
|
|
1194
|
+
})
|
|
1195
|
+
) {
|
|
1196
|
+
return;
|
|
1197
|
+
}
|
|
1198
|
+
yield* grepFileContent({
|
|
1199
|
+
path: scope,
|
|
1200
|
+
content: fileSystemText(exact),
|
|
1201
|
+
regex,
|
|
1202
|
+
});
|
|
1203
|
+
return;
|
|
1204
|
+
}
|
|
1205
|
+
for await (const entry of listed.list(
|
|
1206
|
+
scope === "/" ? undefined : scope
|
|
1207
|
+
)) {
|
|
1208
|
+
if (
|
|
1209
|
+
query.glob !== undefined &&
|
|
1210
|
+
!pathMatchesGlob(entry.path, scope, query.glob, {
|
|
1211
|
+
matchBasename: true,
|
|
1212
|
+
})
|
|
1213
|
+
) {
|
|
1214
|
+
continue;
|
|
1215
|
+
}
|
|
1216
|
+
const content = store.get(entry.path);
|
|
1217
|
+
if (content === undefined) {
|
|
1218
|
+
continue;
|
|
1219
|
+
}
|
|
1220
|
+
yield* grepFileContent({
|
|
1221
|
+
path: entry.path,
|
|
1222
|
+
content: fileSystemText(content),
|
|
1223
|
+
regex,
|
|
1224
|
+
});
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
},
|
|
1228
|
+
async *glob(pattern, dir) {
|
|
1229
|
+
const scope =
|
|
1230
|
+
dir === undefined || dir === "" ? "/" : normalizeFsPath(dir);
|
|
1231
|
+
yield* globFromList(listed.list(scope === "/" ? undefined : scope), {
|
|
1232
|
+
pattern,
|
|
1233
|
+
dir: scope,
|
|
1234
|
+
});
|
|
1235
|
+
},
|
|
1236
|
+
async write(inputPath, contents) {
|
|
1237
|
+
store.set(normalizeFsPath(inputPath), contents);
|
|
1238
|
+
},
|
|
1239
|
+
async delete(inputPath) {
|
|
1240
|
+
const path = normalizeFsPath(inputPath);
|
|
1241
|
+
if (!store.has(path)) {
|
|
1242
|
+
throw new FileNotFoundError(path);
|
|
1243
|
+
}
|
|
1244
|
+
store.delete(path);
|
|
1245
|
+
},
|
|
1246
|
+
search: emptySearch,
|
|
1247
|
+
};
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
function fsErrorMessage(error: unknown, fallback: string): string {
|
|
1251
|
+
return error instanceof Error ? error.message : fallback;
|
|
1252
|
+
}
|
|
1253
|
+
|
|
1254
|
+
function throwLocalReadError(error: unknown, filePath: string): never {
|
|
1255
|
+
throw new FileNotFoundError(
|
|
1256
|
+
filePath,
|
|
1257
|
+
fsErrorMessage(error, `not found: ${filePath}`)
|
|
1258
|
+
);
|
|
1259
|
+
}
|
|
1260
|
+
|
|
1261
|
+
function skipWalkDir(name: string): boolean {
|
|
1262
|
+
return name === ".git" || name === "node_modules";
|
|
1263
|
+
}
|
|
1264
|
+
|
|
1265
|
+
async function walkLocalFiles(rootDir: string): Promise<string[]> {
|
|
1266
|
+
const found: string[] = [];
|
|
1267
|
+
const stack = [rootDir];
|
|
1268
|
+
while (stack.length > 0) {
|
|
1269
|
+
const current = stack.pop();
|
|
1270
|
+
if (current === undefined) {
|
|
1271
|
+
break;
|
|
1272
|
+
}
|
|
1273
|
+
let entries: Array<{
|
|
1274
|
+
name: string;
|
|
1275
|
+
isDirectory(): boolean;
|
|
1276
|
+
isFile(): boolean;
|
|
1277
|
+
}>;
|
|
1278
|
+
try {
|
|
1279
|
+
entries = await readdir(current, { withFileTypes: true });
|
|
1280
|
+
} catch {
|
|
1281
|
+
continue;
|
|
1282
|
+
}
|
|
1283
|
+
for (const entry of entries) {
|
|
1284
|
+
const full = path.join(current, entry.name);
|
|
1285
|
+
// Same as the pre-root host walk and native LS/Glob/Grep: Dirent
|
|
1286
|
+
// isDirectory/isFile do not follow links, so workspace `memory/`
|
|
1287
|
+
// and other symlinks are not enumerated. Exact-path read/stat still
|
|
1288
|
+
// follow.
|
|
1289
|
+
if (entry.isDirectory()) {
|
|
1290
|
+
if (!skipWalkDir(entry.name)) {
|
|
1291
|
+
stack.push(full);
|
|
1292
|
+
}
|
|
1293
|
+
} else if (entry.isFile()) {
|
|
1294
|
+
found.push(full);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
}
|
|
1298
|
+
found.sort((left, right) => left.localeCompare(right));
|
|
1299
|
+
return found;
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
async function* listLocalFiles(rootDir: string): AsyncGenerator<DirEntry> {
|
|
1303
|
+
for (const filePath of await walkLocalFiles(rootDir)) {
|
|
1304
|
+
yield { path: filePath };
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
/**
|
|
1309
|
+
* Host-disk FileSystem. Relative paths resolve against `cwd`; absolute
|
|
1310
|
+
* paths are used as-is — the same contract as Cursor SDK native file
|
|
1311
|
+
* tools. `search` yields empty results (no host index).
|
|
1312
|
+
*
|
|
1313
|
+
* Internal. Not part of the public Agent SDK surface.
|
|
1314
|
+
*/
|
|
1315
|
+
export function localFileSystem(options: { cwd: string }): FileSystem {
|
|
1316
|
+
const cwd = path.resolve(options.cwd);
|
|
1317
|
+
|
|
1318
|
+
const resolvePath = (input: string): string =>
|
|
1319
|
+
path.isAbsolute(input) ? path.resolve(input) : path.resolve(cwd, input);
|
|
1320
|
+
|
|
1321
|
+
return {
|
|
1322
|
+
async *list(prefix) {
|
|
1323
|
+
const dirPath =
|
|
1324
|
+
prefix === undefined || prefix === "" || prefix === "."
|
|
1325
|
+
? cwd
|
|
1326
|
+
: resolvePath(prefix);
|
|
1327
|
+
yield* listLocalFiles(dirPath);
|
|
1328
|
+
},
|
|
1329
|
+
async stat(probePath) {
|
|
1330
|
+
const filePath = resolvePath(probePath);
|
|
1331
|
+
try {
|
|
1332
|
+
const info = await stat(filePath);
|
|
1333
|
+
return { kind: info.isDirectory() ? "directory" : "file" } as const;
|
|
1334
|
+
} catch {
|
|
1335
|
+
return { kind: "missing" } as const;
|
|
1336
|
+
}
|
|
1337
|
+
},
|
|
1338
|
+
async read(inputPath) {
|
|
1339
|
+
const filePath = resolvePath(inputPath);
|
|
1340
|
+
try {
|
|
1341
|
+
const bytes = await readFile(filePath);
|
|
1342
|
+
if (detectImageMimeType(bytes, filePath) !== undefined) {
|
|
1343
|
+
return new Uint8Array(bytes);
|
|
1344
|
+
}
|
|
1345
|
+
return bytes.toString("utf8");
|
|
1346
|
+
} catch (error) {
|
|
1347
|
+
throwLocalReadError(error, filePath);
|
|
1348
|
+
}
|
|
1349
|
+
},
|
|
1350
|
+
async *grep(query) {
|
|
1351
|
+
yield* limitResults(uncapped(), query.maxResults);
|
|
1352
|
+
async function* uncapped() {
|
|
1353
|
+
const searchRoot = resolvePath(query.path ?? cwd);
|
|
1354
|
+
const regex = compileGrepPattern(
|
|
1355
|
+
query.pattern,
|
|
1356
|
+
query.caseInsensitive,
|
|
1357
|
+
searchRoot
|
|
1358
|
+
);
|
|
1359
|
+
let filesToSearch: string[];
|
|
1360
|
+
try {
|
|
1361
|
+
const info = await stat(searchRoot);
|
|
1362
|
+
filesToSearch = info.isFile()
|
|
1363
|
+
? [searchRoot]
|
|
1364
|
+
: await walkLocalFiles(searchRoot);
|
|
1365
|
+
} catch (error) {
|
|
1366
|
+
throwLocalReadError(error, searchRoot);
|
|
1367
|
+
}
|
|
1368
|
+
for (const filePath of filesToSearch) {
|
|
1369
|
+
if (
|
|
1370
|
+
query.glob !== undefined &&
|
|
1371
|
+
!pathMatchesGlob(filePath, searchRoot, query.glob, {
|
|
1372
|
+
matchBasename: true,
|
|
1373
|
+
})
|
|
1374
|
+
) {
|
|
1375
|
+
continue;
|
|
1376
|
+
}
|
|
1377
|
+
let content: string;
|
|
1378
|
+
try {
|
|
1379
|
+
content = await readFile(filePath, "utf8");
|
|
1380
|
+
} catch {
|
|
1381
|
+
continue;
|
|
1382
|
+
}
|
|
1383
|
+
yield* grepFileContent({ path: filePath, content, regex });
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
},
|
|
1387
|
+
async *glob(pattern, dir) {
|
|
1388
|
+
const dirPath =
|
|
1389
|
+
dir === undefined || dir === "" || dir === "." ? cwd : resolvePath(dir);
|
|
1390
|
+
yield* globFromList(listLocalFiles(dirPath), {
|
|
1391
|
+
pattern,
|
|
1392
|
+
dir: dirPath,
|
|
1393
|
+
});
|
|
1394
|
+
},
|
|
1395
|
+
async write(inputPath, contents) {
|
|
1396
|
+
const filePath = resolvePath(inputPath);
|
|
1397
|
+
try {
|
|
1398
|
+
await mkdir(path.dirname(filePath), { recursive: true });
|
|
1399
|
+
await writeFile(filePath, contents, "utf8");
|
|
1400
|
+
} catch (error) {
|
|
1401
|
+
throw new WriteFailedError(
|
|
1402
|
+
filePath,
|
|
1403
|
+
fsErrorMessage(error, `write failed: ${filePath}`)
|
|
1404
|
+
);
|
|
1405
|
+
}
|
|
1406
|
+
},
|
|
1407
|
+
async delete(inputPath) {
|
|
1408
|
+
const filePath = resolvePath(inputPath);
|
|
1409
|
+
try {
|
|
1410
|
+
await rm(filePath);
|
|
1411
|
+
} catch (error) {
|
|
1412
|
+
throwLocalReadError(error, filePath);
|
|
1413
|
+
}
|
|
1414
|
+
},
|
|
1415
|
+
search: emptySearch,
|
|
1416
|
+
};
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
interface MountEntry {
|
|
1420
|
+
prefix: string;
|
|
1421
|
+
fs: FileSystem;
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
function longestMount(
|
|
1425
|
+
path: string,
|
|
1426
|
+
mounts: MountEntry[]
|
|
1427
|
+
): MountEntry | undefined {
|
|
1428
|
+
let best: MountEntry | undefined;
|
|
1429
|
+
for (const mount of mounts) {
|
|
1430
|
+
if (!pathHasSegmentPrefix({ path, prefix: mount.prefix })) {
|
|
1431
|
+
continue;
|
|
1432
|
+
}
|
|
1433
|
+
if (best === undefined || mount.prefix.length > best.prefix.length) {
|
|
1434
|
+
best = mount;
|
|
1435
|
+
}
|
|
1436
|
+
}
|
|
1437
|
+
return best;
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
/** Namespace path → child path. `/host/a.md` on `/host` is `/a.md`. */
|
|
1441
|
+
function toChildPath(path: string, prefix: string): string {
|
|
1442
|
+
if (prefix === "/") {
|
|
1443
|
+
return path;
|
|
1444
|
+
}
|
|
1445
|
+
if (path === prefix) {
|
|
1446
|
+
return "/";
|
|
1447
|
+
}
|
|
1448
|
+
if (path.startsWith(`${prefix}/`)) {
|
|
1449
|
+
return path.slice(prefix.length);
|
|
1450
|
+
}
|
|
1451
|
+
return path;
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
/** Child path → namespace path. `/a.md` on `/host` is `/host/a.md`. */
|
|
1455
|
+
function toParentPath(path: string, prefix: string): string {
|
|
1456
|
+
if (prefix === "/") {
|
|
1457
|
+
return path;
|
|
1458
|
+
}
|
|
1459
|
+
if (path === "/" || path === "") {
|
|
1460
|
+
return prefix;
|
|
1461
|
+
}
|
|
1462
|
+
return path.startsWith("/") ? `${prefix}${path}` : `${prefix}/${path}`;
|
|
1463
|
+
}
|
|
1464
|
+
|
|
1465
|
+
async function* mapMountEntries(
|
|
1466
|
+
source: AsyncIterable<DirEntry>,
|
|
1467
|
+
prefix: string
|
|
1468
|
+
): AsyncIterable<DirEntry> {
|
|
1469
|
+
try {
|
|
1470
|
+
for await (const entry of source) {
|
|
1471
|
+
yield { ...entry, path: toParentPath(entry.path, prefix) };
|
|
1472
|
+
}
|
|
1473
|
+
} catch (error) {
|
|
1474
|
+
throw remapPathError(error, prefix);
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
async function* mapMountGrep(
|
|
1479
|
+
source: AsyncIterable<GrepResult>,
|
|
1480
|
+
prefix: string
|
|
1481
|
+
): AsyncIterable<GrepResult> {
|
|
1482
|
+
try {
|
|
1483
|
+
for await (const hit of source) {
|
|
1484
|
+
yield { ...hit, path: toParentPath(hit.path, prefix) };
|
|
1485
|
+
}
|
|
1486
|
+
} catch (error) {
|
|
1487
|
+
throw remapPathError(error, prefix);
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
|
|
1491
|
+
async function* mapMountSearch(
|
|
1492
|
+
source: AsyncIterable<SearchResult>,
|
|
1493
|
+
prefix: string
|
|
1494
|
+
): AsyncIterable<SearchResult> {
|
|
1495
|
+
try {
|
|
1496
|
+
for await (const hit of source) {
|
|
1497
|
+
yield { ...hit, path: toParentPath(hit.path, prefix) };
|
|
1498
|
+
}
|
|
1499
|
+
} catch (error) {
|
|
1500
|
+
throw remapPathError(error, prefix);
|
|
1501
|
+
}
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
function childSearchQuery(
|
|
1505
|
+
query: SearchQuery,
|
|
1506
|
+
prefix: string
|
|
1507
|
+
): SearchQuery | undefined {
|
|
1508
|
+
if (
|
|
1509
|
+
query.targetDirectories === undefined ||
|
|
1510
|
+
query.targetDirectories.length === 0
|
|
1511
|
+
) {
|
|
1512
|
+
return query;
|
|
1513
|
+
}
|
|
1514
|
+
const dirs: string[] = [];
|
|
1515
|
+
for (const dir of query.targetDirectories) {
|
|
1516
|
+
const normalized = normalizeFsPath(dir);
|
|
1517
|
+
// `/` is every mount, the same way grep at `/` fans out.
|
|
1518
|
+
if (normalized === "/") {
|
|
1519
|
+
return { ...query, targetDirectories: undefined };
|
|
1520
|
+
}
|
|
1521
|
+
if (!pathHasSegmentPrefix({ path: normalized, prefix })) {
|
|
1522
|
+
continue;
|
|
1523
|
+
}
|
|
1524
|
+
const child = toChildPath(normalized, prefix);
|
|
1525
|
+
if (child === "/") {
|
|
1526
|
+
return { ...query, targetDirectories: undefined };
|
|
1527
|
+
}
|
|
1528
|
+
dirs.push(child);
|
|
1529
|
+
}
|
|
1530
|
+
if (dirs.length === 0) {
|
|
1531
|
+
return undefined;
|
|
1532
|
+
}
|
|
1533
|
+
return { ...query, targetDirectories: dirs };
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
/**
|
|
1537
|
+
* Longest-prefix compose, path-segment match. Every verb is
|
|
1538
|
+
* defined. Does not synthesize `shell`. `/agent` is not a
|
|
1539
|
+
* prefix of `/agent-tools`.
|
|
1540
|
+
*
|
|
1541
|
+
* The record keys are mount points. Each child is a FileSystem
|
|
1542
|
+
* rooted at `/`. `unionFs` strips the prefix on the way in and
|
|
1543
|
+
* re-prefixes yields (and error paths) on the way out:
|
|
1544
|
+
* `unionFs({ "/host": hostFs(...) })` — the child never sees
|
|
1545
|
+
* `/host`.
|
|
1546
|
+
*
|
|
1547
|
+
* A path with no mount:
|
|
1548
|
+
* - `read` / `write` / `delete`: throw
|
|
1549
|
+
* `no mount for path: ${path}`
|
|
1550
|
+
* - `list` / `glob` / `grep`: empty iterator
|
|
1551
|
+
* - `stat`: `{ kind: "missing" }` (`/` and exact mount prefixes
|
|
1552
|
+
* are `{ kind: "directory" }`)
|
|
1553
|
+
*
|
|
1554
|
+
* Root `list` / `glob` / `grep` / `search` k-way merge already
|
|
1555
|
+
* sorted sources. List and glob use `unique` so an earlier
|
|
1556
|
+
* mount shadows a later one; grep and search keep every hit.
|
|
1557
|
+
*/
|
|
1558
|
+
export function unionFs(mounts: Record<string, FileSystem>): FileSystem {
|
|
1559
|
+
const entries: MountEntry[] = Object.entries(mounts).map(([prefix, fs]) => ({
|
|
1560
|
+
prefix: normalizeFsPath(prefix),
|
|
1561
|
+
fs,
|
|
1562
|
+
}));
|
|
1563
|
+
entries.sort((a, b) => b.prefix.length - a.prefix.length);
|
|
1564
|
+
|
|
1565
|
+
const noMount = (path: string): never => {
|
|
1566
|
+
throw new NoMountError(path);
|
|
1567
|
+
};
|
|
1568
|
+
|
|
1569
|
+
const forward = async <T>(
|
|
1570
|
+
path: string,
|
|
1571
|
+
run: (fs: FileSystem, childPath: string) => Promise<T>
|
|
1572
|
+
): Promise<T> => {
|
|
1573
|
+
const mount = longestMount(path, entries);
|
|
1574
|
+
if (mount === undefined) {
|
|
1575
|
+
return noMount(path);
|
|
1576
|
+
}
|
|
1577
|
+
const childPath = toChildPath(path, mount.prefix);
|
|
1578
|
+
try {
|
|
1579
|
+
return await run(mount.fs, childPath);
|
|
1580
|
+
} catch (error) {
|
|
1581
|
+
throw remapPathError(error, mount.prefix);
|
|
1582
|
+
}
|
|
1583
|
+
};
|
|
1584
|
+
|
|
1585
|
+
const isRootPath = (value: string | undefined): boolean =>
|
|
1586
|
+
value === undefined || value === "" || value === "/";
|
|
1587
|
+
|
|
1588
|
+
return {
|
|
1589
|
+
async *list(prefix) {
|
|
1590
|
+
if (isRootPath(prefix)) {
|
|
1591
|
+
yield* mergeSortedByPath(
|
|
1592
|
+
entries.map((mount) =>
|
|
1593
|
+
mapMountEntries(mount.fs.list(), mount.prefix)
|
|
1594
|
+
),
|
|
1595
|
+
{ unique: true }
|
|
1596
|
+
);
|
|
1597
|
+
return;
|
|
1598
|
+
}
|
|
1599
|
+
const path = normalizeFsPath(prefix ?? "/");
|
|
1600
|
+
const mount = longestMount(path, entries);
|
|
1601
|
+
if (mount === undefined) {
|
|
1602
|
+
return;
|
|
1603
|
+
}
|
|
1604
|
+
const childPath = toChildPath(path, mount.prefix);
|
|
1605
|
+
yield* mapMountEntries(
|
|
1606
|
+
mount.fs.list(childPath === "/" ? undefined : childPath),
|
|
1607
|
+
mount.prefix
|
|
1608
|
+
);
|
|
1609
|
+
},
|
|
1610
|
+
async stat(path) {
|
|
1611
|
+
const normalized = normalizeFsPath(path);
|
|
1612
|
+
if (normalized === "/") {
|
|
1613
|
+
return { kind: "directory" } as const;
|
|
1614
|
+
}
|
|
1615
|
+
if (entries.some((entry) => entry.prefix === normalized)) {
|
|
1616
|
+
return { kind: "directory" } as const;
|
|
1617
|
+
}
|
|
1618
|
+
const mount = longestMount(normalized, entries);
|
|
1619
|
+
if (mount === undefined) {
|
|
1620
|
+
return { kind: "missing" } as const;
|
|
1621
|
+
}
|
|
1622
|
+
return mount.fs.stat(toChildPath(normalized, mount.prefix));
|
|
1623
|
+
},
|
|
1624
|
+
async read(path) {
|
|
1625
|
+
const normalized = normalizeFsPath(path);
|
|
1626
|
+
return forward(normalized, (fs, childPath) => fs.read(childPath));
|
|
1627
|
+
},
|
|
1628
|
+
async write(path, contents) {
|
|
1629
|
+
const normalized = normalizeFsPath(path);
|
|
1630
|
+
return forward(normalized, (fs, childPath) =>
|
|
1631
|
+
fs.write(childPath, contents)
|
|
1632
|
+
);
|
|
1633
|
+
},
|
|
1634
|
+
async delete(path) {
|
|
1635
|
+
const normalized = normalizeFsPath(path);
|
|
1636
|
+
return forward(normalized, (fs, childPath) => fs.delete(childPath));
|
|
1637
|
+
},
|
|
1638
|
+
async *grep(query) {
|
|
1639
|
+
yield* limitResults(uncapped(), query.maxResults);
|
|
1640
|
+
async function* uncapped() {
|
|
1641
|
+
const scope =
|
|
1642
|
+
query.path === undefined || query.path === ""
|
|
1643
|
+
? "/"
|
|
1644
|
+
: normalizeFsPath(query.path);
|
|
1645
|
+
if (scope === "/") {
|
|
1646
|
+
yield* mergeSorted(
|
|
1647
|
+
entries.map((mount) =>
|
|
1648
|
+
mapMountGrep(
|
|
1649
|
+
mount.fs.grep({ ...query, path: undefined }),
|
|
1650
|
+
mount.prefix
|
|
1651
|
+
)
|
|
1652
|
+
),
|
|
1653
|
+
{ compare: compareGrepResult }
|
|
1654
|
+
);
|
|
1655
|
+
return;
|
|
1656
|
+
}
|
|
1657
|
+
const mount = longestMount(scope, entries);
|
|
1658
|
+
if (mount === undefined) {
|
|
1659
|
+
return;
|
|
1660
|
+
}
|
|
1661
|
+
const childPath = toChildPath(scope, mount.prefix);
|
|
1662
|
+
yield* mapMountGrep(
|
|
1663
|
+
mount.fs.grep({
|
|
1664
|
+
...query,
|
|
1665
|
+
path: childPath === "/" ? undefined : childPath,
|
|
1666
|
+
}),
|
|
1667
|
+
mount.prefix
|
|
1668
|
+
);
|
|
1669
|
+
}
|
|
1670
|
+
},
|
|
1671
|
+
async *search(query) {
|
|
1672
|
+
yield* limitResults(uncapped(), query.maxResults);
|
|
1673
|
+
async function* uncapped() {
|
|
1674
|
+
const sources: AsyncIterable<SearchResult>[] = [];
|
|
1675
|
+
for (const mount of entries) {
|
|
1676
|
+
const childQuery = childSearchQuery(query, mount.prefix);
|
|
1677
|
+
if (childQuery === undefined) {
|
|
1678
|
+
continue;
|
|
1679
|
+
}
|
|
1680
|
+
sources.push(
|
|
1681
|
+
mapMountSearch(mount.fs.search(childQuery), mount.prefix)
|
|
1682
|
+
);
|
|
1683
|
+
}
|
|
1684
|
+
yield* mergeSorted(sources, { compare: compareSearchResult });
|
|
1685
|
+
}
|
|
1686
|
+
},
|
|
1687
|
+
async *glob(pattern, dir) {
|
|
1688
|
+
const scope =
|
|
1689
|
+
dir === undefined || dir === "" ? "/" : normalizeFsPath(dir);
|
|
1690
|
+
if (scope === "/") {
|
|
1691
|
+
yield* mergeSortedByPath(
|
|
1692
|
+
entries.map((mount) =>
|
|
1693
|
+
mapMountEntries(mount.fs.glob(pattern), mount.prefix)
|
|
1694
|
+
),
|
|
1695
|
+
{ unique: true }
|
|
1696
|
+
);
|
|
1697
|
+
return;
|
|
1698
|
+
}
|
|
1699
|
+
const mount = longestMount(scope, entries);
|
|
1700
|
+
if (mount === undefined) {
|
|
1701
|
+
return;
|
|
1702
|
+
}
|
|
1703
|
+
const childPath = toChildPath(scope, mount.prefix);
|
|
1704
|
+
yield* mapMountEntries(
|
|
1705
|
+
mount.fs.glob(pattern, childPath === "/" ? undefined : childPath),
|
|
1706
|
+
mount.prefix
|
|
1707
|
+
);
|
|
1708
|
+
},
|
|
1709
|
+
};
|
|
1710
|
+
}
|
|
1711
|
+
|
|
1712
|
+
async function collectLayerPaths(layer: FileSystem): Promise<string[]> {
|
|
1713
|
+
const paths: string[] = [];
|
|
1714
|
+
try {
|
|
1715
|
+
for await (const entry of layer.list()) {
|
|
1716
|
+
paths.push(entry.path);
|
|
1717
|
+
}
|
|
1718
|
+
} catch {
|
|
1719
|
+
// A layer that cannot list also cannot own paths.
|
|
1720
|
+
}
|
|
1721
|
+
return paths;
|
|
1722
|
+
}
|
|
1723
|
+
|
|
1724
|
+
/**
|
|
1725
|
+
* Same-root overlay: earlier layers win. Read-only.
|
|
1726
|
+
*
|
|
1727
|
+
* A downed layer costs its own files, not the overlay. Mutations
|
|
1728
|
+
* throw `read-only mount` / `read-only mount: ${path}`. Empty
|
|
1729
|
+
* `layers` throws `overlayFs needs at least one layer.`
|
|
1730
|
+
*/
|
|
1731
|
+
export function overlayFs(layers: readonly FileSystem[]): FileSystem {
|
|
1732
|
+
if (layers.length === 0) {
|
|
1733
|
+
throw new Error("overlayFs needs at least one layer.");
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
const walkLayers = async <T>(
|
|
1737
|
+
collect: (layer: FileSystem) => AsyncIterable<T> | Promise<AsyncIterable<T>>
|
|
1738
|
+
): Promise<{ streams: T[][]; failure: unknown; served: boolean }> => {
|
|
1739
|
+
const streams: T[][] = [];
|
|
1740
|
+
let failure: unknown;
|
|
1741
|
+
let served = false;
|
|
1742
|
+
for (const layer of layers) {
|
|
1743
|
+
const collected: T[] = [];
|
|
1744
|
+
try {
|
|
1745
|
+
for await (const entry of await collect(layer)) {
|
|
1746
|
+
collected.push(entry);
|
|
1747
|
+
}
|
|
1748
|
+
} catch (error) {
|
|
1749
|
+
failure ??= error;
|
|
1750
|
+
continue;
|
|
1751
|
+
}
|
|
1752
|
+
served = true;
|
|
1753
|
+
streams.push(collected);
|
|
1754
|
+
}
|
|
1755
|
+
return { streams, failure, served };
|
|
1756
|
+
};
|
|
1757
|
+
|
|
1758
|
+
const collectOverlayHits = async <T extends { path: string }>(
|
|
1759
|
+
queryFn: (layer: FileSystem) => AsyncIterable<T>,
|
|
1760
|
+
rethrow?: (error: unknown) => boolean
|
|
1761
|
+
): Promise<T[][]> => {
|
|
1762
|
+
const streams: T[][] = [];
|
|
1763
|
+
const shadowed = new Set<string>();
|
|
1764
|
+
let failure: unknown;
|
|
1765
|
+
let served = false;
|
|
1766
|
+
for (const layer of layers) {
|
|
1767
|
+
const collected: T[] = [];
|
|
1768
|
+
try {
|
|
1769
|
+
for await (const hit of queryFn(layer)) {
|
|
1770
|
+
if (!shadowed.has(hit.path)) {
|
|
1771
|
+
collected.push(hit);
|
|
1772
|
+
}
|
|
1773
|
+
}
|
|
1774
|
+
served = true;
|
|
1775
|
+
} catch (error) {
|
|
1776
|
+
if (rethrow?.(error) === true) {
|
|
1777
|
+
throw error;
|
|
1778
|
+
}
|
|
1779
|
+
failure ??= error;
|
|
1780
|
+
continue;
|
|
1781
|
+
}
|
|
1782
|
+
streams.push(collected);
|
|
1783
|
+
for (const path of await collectLayerPaths(layer)) {
|
|
1784
|
+
shadowed.add(path);
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
if (!served && failure !== undefined) {
|
|
1788
|
+
throw failure;
|
|
1789
|
+
}
|
|
1790
|
+
return streams;
|
|
1791
|
+
};
|
|
1792
|
+
|
|
1793
|
+
return {
|
|
1794
|
+
async *list(prefix) {
|
|
1795
|
+
const { streams, failure, served } = await walkLayers((layer) =>
|
|
1796
|
+
layer.list(prefix)
|
|
1797
|
+
);
|
|
1798
|
+
if (!served && failure !== undefined) {
|
|
1799
|
+
throw failure;
|
|
1800
|
+
}
|
|
1801
|
+
yield* mergeSortedByPath(streams, { unique: true });
|
|
1802
|
+
},
|
|
1803
|
+
async stat(path) {
|
|
1804
|
+
let failure: unknown;
|
|
1805
|
+
let sawMissing = false;
|
|
1806
|
+
for (const layer of layers) {
|
|
1807
|
+
try {
|
|
1808
|
+
const info = await layer.stat(path);
|
|
1809
|
+
if (info.kind !== "missing") {
|
|
1810
|
+
return info;
|
|
1811
|
+
}
|
|
1812
|
+
sawMissing = true;
|
|
1813
|
+
} catch (error) {
|
|
1814
|
+
failure ??= error;
|
|
1815
|
+
}
|
|
1816
|
+
}
|
|
1817
|
+
if (!sawMissing && failure !== undefined) {
|
|
1818
|
+
throw failure;
|
|
1819
|
+
}
|
|
1820
|
+
return { kind: "missing" } as const;
|
|
1821
|
+
},
|
|
1822
|
+
async *glob(pattern, dir) {
|
|
1823
|
+
const { streams, failure, served } = await walkLayers((layer) =>
|
|
1824
|
+
layer.glob(pattern, dir)
|
|
1825
|
+
);
|
|
1826
|
+
if (!served && failure !== undefined) {
|
|
1827
|
+
throw failure;
|
|
1828
|
+
}
|
|
1829
|
+
yield* mergeSortedByPath(streams, { unique: true });
|
|
1830
|
+
},
|
|
1831
|
+
async read(path) {
|
|
1832
|
+
let failure: unknown;
|
|
1833
|
+
for (const layer of layers) {
|
|
1834
|
+
try {
|
|
1835
|
+
return await layer.read(path);
|
|
1836
|
+
} catch (error) {
|
|
1837
|
+
if (isFileNotFoundError(error)) {
|
|
1838
|
+
continue;
|
|
1839
|
+
}
|
|
1840
|
+
failure ??= error;
|
|
1841
|
+
}
|
|
1842
|
+
}
|
|
1843
|
+
if (failure !== undefined) {
|
|
1844
|
+
throw failure;
|
|
1845
|
+
}
|
|
1846
|
+
throw new FileNotFoundError(path);
|
|
1847
|
+
},
|
|
1848
|
+
async *grep(query) {
|
|
1849
|
+
const { maxResults, ...uncappedQuery } = query;
|
|
1850
|
+
yield* limitResults(
|
|
1851
|
+
mergeSorted(
|
|
1852
|
+
await collectOverlayHits(
|
|
1853
|
+
(layer) => layer.grep(uncappedQuery),
|
|
1854
|
+
(error) => isInvalidGrepPatternError(error)
|
|
1855
|
+
),
|
|
1856
|
+
{ compare: compareGrepResult }
|
|
1857
|
+
),
|
|
1858
|
+
maxResults
|
|
1859
|
+
);
|
|
1860
|
+
},
|
|
1861
|
+
async write(path) {
|
|
1862
|
+
throw new ReadOnlyMountError(path);
|
|
1863
|
+
},
|
|
1864
|
+
async delete(path) {
|
|
1865
|
+
throw new ReadOnlyMountError(path);
|
|
1866
|
+
},
|
|
1867
|
+
async *search(query) {
|
|
1868
|
+
const { maxResults, ...uncappedQuery } = query;
|
|
1869
|
+
yield* limitResults(
|
|
1870
|
+
mergeSorted(
|
|
1871
|
+
await collectOverlayHits((layer) => layer.search(uncappedQuery)),
|
|
1872
|
+
{ compare: compareSearchResult }
|
|
1873
|
+
),
|
|
1874
|
+
maxResults
|
|
1875
|
+
);
|
|
1876
|
+
},
|
|
1877
|
+
};
|
|
1878
|
+
}
|
|
1879
|
+
|
|
1880
|
+
function hostKey(path: string): string {
|
|
1881
|
+
const normalized = normalizeFsPath(path);
|
|
1882
|
+
return normalized === "/" ? "" : normalized.slice(1);
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
function isHostConflict(error: unknown): boolean {
|
|
1886
|
+
return (
|
|
1887
|
+
error instanceof FileConflictError ||
|
|
1888
|
+
(error instanceof Error &&
|
|
1889
|
+
(error.name === "FileConflictError" ||
|
|
1890
|
+
error.message.includes("lost a race")))
|
|
1891
|
+
);
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
/**
|
|
1895
|
+
* `HostFilesApi` as a FileSystem rooted at `/`.
|
|
1896
|
+
*
|
|
1897
|
+
* Mount it with `unionFs({ "/host": hostFs(files, scope) })`. The
|
|
1898
|
+
* child never sees `/host` — `unionFs` owns the prefix. `scope` is
|
|
1899
|
+
* the HostFiles partition, not a path prefix. `search` is empty
|
|
1900
|
+
* (HostFiles is not an index).
|
|
1901
|
+
*/
|
|
1902
|
+
export function hostFs(files: HostFilesApi, scope: HostFilesScope): FileSystem {
|
|
1903
|
+
const callOptions = { scope };
|
|
1904
|
+
const readEtags = new Map<string, string>();
|
|
1905
|
+
|
|
1906
|
+
const listed = {
|
|
1907
|
+
async *list(prefix?: string) {
|
|
1908
|
+
const entries = await files.list(undefined, callOptions);
|
|
1909
|
+
yield* listFilesFromPaths(
|
|
1910
|
+
entries.map((entry) => ({
|
|
1911
|
+
path: normalizeFsPath(entry.path),
|
|
1912
|
+
size: entry.size,
|
|
1913
|
+
})),
|
|
1914
|
+
prefix
|
|
1915
|
+
);
|
|
1916
|
+
},
|
|
1917
|
+
async stat(path: string) {
|
|
1918
|
+
const entries = await files.list(undefined, callOptions);
|
|
1919
|
+
return statFromFilePaths(
|
|
1920
|
+
entries.map((entry) => normalizeFsPath(entry.path)),
|
|
1921
|
+
path
|
|
1922
|
+
);
|
|
1923
|
+
},
|
|
1924
|
+
};
|
|
1925
|
+
|
|
1926
|
+
const readText = async (inputPath: string): Promise<string> => {
|
|
1927
|
+
const path = normalizeFsPath(inputPath);
|
|
1928
|
+
const key = hostKey(path);
|
|
1929
|
+
if (key === "") {
|
|
1930
|
+
throw new FileNotFoundError(path);
|
|
1931
|
+
}
|
|
1932
|
+
const [content, stat] = await Promise.all([
|
|
1933
|
+
files.read(key, callOptions),
|
|
1934
|
+
files.stat(key, callOptions),
|
|
1935
|
+
]);
|
|
1936
|
+
if (content === undefined) {
|
|
1937
|
+
throw new FileNotFoundError(path);
|
|
1938
|
+
}
|
|
1939
|
+
if (stat?.etag !== undefined) {
|
|
1940
|
+
readEtags.set(key, stat.etag);
|
|
1941
|
+
}
|
|
1942
|
+
return content;
|
|
1943
|
+
};
|
|
1944
|
+
|
|
1945
|
+
const writeHost = async (
|
|
1946
|
+
inputPath: string,
|
|
1947
|
+
contents: string
|
|
1948
|
+
): Promise<void> => {
|
|
1949
|
+
const path = normalizeFsPath(inputPath);
|
|
1950
|
+
const key = hostKey(path);
|
|
1951
|
+
if (key === "") {
|
|
1952
|
+
throw new FileNotFoundError(path);
|
|
1953
|
+
}
|
|
1954
|
+
try {
|
|
1955
|
+
if (files.capabilities.conflictDetection) {
|
|
1956
|
+
const etag = readEtags.get(key);
|
|
1957
|
+
await files.write(key, contents, {
|
|
1958
|
+
...callOptions,
|
|
1959
|
+
// No cached etag: unconditional create/overwrite. After a
|
|
1960
|
+
// successful read or write, CAS on that etag.
|
|
1961
|
+
...(etag === undefined ? {} : { ifMatch: etag }),
|
|
1962
|
+
});
|
|
1963
|
+
} else {
|
|
1964
|
+
await files.write(key, contents, callOptions);
|
|
1965
|
+
}
|
|
1966
|
+
} catch (error) {
|
|
1967
|
+
if (isHostConflict(error)) {
|
|
1968
|
+
throw new PathError(
|
|
1969
|
+
path,
|
|
1970
|
+
`${path} changed since you read it — read it again and rewrite.`
|
|
1971
|
+
);
|
|
1972
|
+
}
|
|
1973
|
+
throw error;
|
|
1974
|
+
}
|
|
1975
|
+
const stat = await files.stat(key, callOptions);
|
|
1976
|
+
if (stat?.etag !== undefined) {
|
|
1977
|
+
readEtags.set(key, stat.etag);
|
|
1978
|
+
} else {
|
|
1979
|
+
readEtags.delete(key);
|
|
1980
|
+
}
|
|
1981
|
+
};
|
|
1982
|
+
|
|
1983
|
+
return {
|
|
1984
|
+
list(prefix) {
|
|
1985
|
+
return listed.list(prefix);
|
|
1986
|
+
},
|
|
1987
|
+
async stat(path) {
|
|
1988
|
+
return listed.stat(path);
|
|
1989
|
+
},
|
|
1990
|
+
async read(path) {
|
|
1991
|
+
return readText(path);
|
|
1992
|
+
},
|
|
1993
|
+
async *grep(query) {
|
|
1994
|
+
yield* limitResults(uncapped(), query.maxResults);
|
|
1995
|
+
async function* uncapped() {
|
|
1996
|
+
const scopePath =
|
|
1997
|
+
query.path === undefined || query.path === ""
|
|
1998
|
+
? "/"
|
|
1999
|
+
: normalizeFsPath(query.path);
|
|
2000
|
+
const regex = compileGrepPattern(
|
|
2001
|
+
query.pattern,
|
|
2002
|
+
query.caseInsensitive,
|
|
2003
|
+
scopePath
|
|
2004
|
+
);
|
|
2005
|
+
const info = await listed.stat(scopePath);
|
|
2006
|
+
if (scopePath !== "/" && info.kind === "file") {
|
|
2007
|
+
if (
|
|
2008
|
+
query.glob !== undefined &&
|
|
2009
|
+
!pathMatchesGlob(scopePath, scopePath, query.glob, {
|
|
2010
|
+
matchBasename: true,
|
|
2011
|
+
})
|
|
2012
|
+
) {
|
|
2013
|
+
return;
|
|
2014
|
+
}
|
|
2015
|
+
yield* grepFileContent({
|
|
2016
|
+
path: scopePath,
|
|
2017
|
+
content: await readText(scopePath),
|
|
2018
|
+
regex,
|
|
2019
|
+
});
|
|
2020
|
+
return;
|
|
2021
|
+
}
|
|
2022
|
+
for await (const entry of listed.list(
|
|
2023
|
+
scopePath === "/" ? undefined : scopePath
|
|
2024
|
+
)) {
|
|
2025
|
+
if (
|
|
2026
|
+
query.glob !== undefined &&
|
|
2027
|
+
!pathMatchesGlob(entry.path, scopePath, query.glob, {
|
|
2028
|
+
matchBasename: true,
|
|
2029
|
+
})
|
|
2030
|
+
) {
|
|
2031
|
+
continue;
|
|
2032
|
+
}
|
|
2033
|
+
yield* grepFileContent({
|
|
2034
|
+
path: entry.path,
|
|
2035
|
+
content: await readText(entry.path),
|
|
2036
|
+
regex,
|
|
2037
|
+
});
|
|
2038
|
+
}
|
|
2039
|
+
}
|
|
2040
|
+
},
|
|
2041
|
+
async *glob(pattern, dir) {
|
|
2042
|
+
const dirPath =
|
|
2043
|
+
dir === undefined || dir === "" ? "/" : normalizeFsPath(dir);
|
|
2044
|
+
yield* globFromList(listed.list(dirPath === "/" ? undefined : dirPath), {
|
|
2045
|
+
pattern,
|
|
2046
|
+
dir: dirPath,
|
|
2047
|
+
});
|
|
2048
|
+
},
|
|
2049
|
+
async write(inputPath, contents) {
|
|
2050
|
+
await writeHost(inputPath, contents);
|
|
2051
|
+
},
|
|
2052
|
+
async delete(inputPath) {
|
|
2053
|
+
const path = normalizeFsPath(inputPath);
|
|
2054
|
+
const key = hostKey(path);
|
|
2055
|
+
if (key === "" || !(await files.exists(key, callOptions))) {
|
|
2056
|
+
throw new FileNotFoundError(path);
|
|
2057
|
+
}
|
|
2058
|
+
await files.remove(key, callOptions);
|
|
2059
|
+
readEtags.delete(key);
|
|
2060
|
+
},
|
|
2061
|
+
search: emptySearch,
|
|
2062
|
+
};
|
|
2063
|
+
}
|
|
2064
|
+
|
|
2065
|
+
async function* globFromList(
|
|
2066
|
+
source: AsyncIterable<DirEntry>,
|
|
2067
|
+
options: { pattern: string; dir: string }
|
|
2068
|
+
): AsyncGenerator<DirEntry> {
|
|
2069
|
+
for await (const entry of source) {
|
|
2070
|
+
if (pathMatchesGlob(entry.path, options.dir, options.pattern)) {
|
|
2071
|
+
yield entry;
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
}
|