@vite-hub/shell 0.0.0 → 0.0.1
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 +54 -1
- package/dist/analyze-BhMXNtZg.js +126 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +3 -0
- package/dist/providers/cloudflare.d.ts +28 -0
- package/dist/providers/cloudflare.js +83 -0
- package/dist/providers/just-bash.d.ts +15 -0
- package/dist/providers/just-bash.js +71 -0
- package/dist/session-Qzhl3bqv.js +129 -0
- package/dist/types-BNtJGjIv.d.ts +100 -0
- package/dist/workspace.d.ts +77 -0
- package/dist/workspace.js +733 -0
- package/package.json +47 -5
|
@@ -0,0 +1,733 @@
|
|
|
1
|
+
import { t as createShellRuntime } from "./session-Qzhl3bqv.js";
|
|
2
|
+
import { createJustBashProvider } from "./providers/just-bash.js";
|
|
3
|
+
import { posix } from "node:path";
|
|
4
|
+
//#region src/workspace/filesystem.ts
|
|
5
|
+
const workspaceMountPoint = "/workspace";
|
|
6
|
+
function createEscapeError(path) {
|
|
7
|
+
return /* @__PURE__ */ new Error(`[vitehub] Workspace path escapes the workspace root: "${path}".`);
|
|
8
|
+
}
|
|
9
|
+
function createReadonlyError() {
|
|
10
|
+
return /* @__PURE__ */ new Error("[vitehub] Workspace filesystem is read-only.");
|
|
11
|
+
}
|
|
12
|
+
function toShellContent(content) {
|
|
13
|
+
return typeof content === "string" ? new TextEncoder().encode(content) : content;
|
|
14
|
+
}
|
|
15
|
+
function decodeContent(content, encoding) {
|
|
16
|
+
if (encoding === "base64") return Buffer.from(content).toString("base64");
|
|
17
|
+
if (encoding === "hex") return Buffer.from(content).toString("hex");
|
|
18
|
+
if (encoding === "ascii" || encoding === "latin1") return Buffer.from(content).toString(encoding);
|
|
19
|
+
return new TextDecoder().decode(content);
|
|
20
|
+
}
|
|
21
|
+
function normalizeInputPath(path) {
|
|
22
|
+
return path.replace(/\\/g, "/");
|
|
23
|
+
}
|
|
24
|
+
function normalizeAbsolutePath(path) {
|
|
25
|
+
const normalized = posix.normalize(path);
|
|
26
|
+
return normalized === "." ? "/" : normalized;
|
|
27
|
+
}
|
|
28
|
+
function statFromEntry(entry) {
|
|
29
|
+
return {
|
|
30
|
+
isDirectory: entry.type === "directory",
|
|
31
|
+
isFile: entry.type === "file",
|
|
32
|
+
isSymbolicLink: false,
|
|
33
|
+
mode: entry.type === "directory" ? 16877 : 33188,
|
|
34
|
+
mtime: /* @__PURE__ */ new Date(0),
|
|
35
|
+
size: entry.type === "file" ? entry.size || 0 : 0
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
async function copyWorkspacePath(workspace, from, to) {
|
|
39
|
+
if ((await workspace.stat(from)).type === "file") {
|
|
40
|
+
await workspace.writeFile(to, await workspace.readFile(from, { encoding: "binary" }));
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const entries = await workspace.list(from, { recursive: true });
|
|
44
|
+
const directories = entries.filter((entry) => entry.type === "directory").sort((left, right) => left.path.length - right.path.length);
|
|
45
|
+
const files = entries.filter((entry) => entry.type === "file").sort((left, right) => left.path.localeCompare(right.path));
|
|
46
|
+
await workspace.mkdir(to, { recursive: true });
|
|
47
|
+
for (const entry of directories) {
|
|
48
|
+
const relativePath = from ? entry.path.slice(from.length + 1) : entry.path;
|
|
49
|
+
await workspace.mkdir(posix.join(to, relativePath), { recursive: true });
|
|
50
|
+
}
|
|
51
|
+
for (const entry of files) {
|
|
52
|
+
const relativePath = from ? entry.path.slice(from.length + 1) : entry.path;
|
|
53
|
+
await workspace.writeFile(posix.join(to, relativePath), await workspace.readFile(entry.path, { encoding: "binary" }));
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
var WorkspaceFileSystem = class {
|
|
57
|
+
writeFs;
|
|
58
|
+
#paths = [workspaceMountPoint];
|
|
59
|
+
#refreshPromise;
|
|
60
|
+
constructor(workspace, writeFs) {
|
|
61
|
+
this.workspace = workspace;
|
|
62
|
+
this.writeFs = writeFs;
|
|
63
|
+
}
|
|
64
|
+
async readFile(path, options) {
|
|
65
|
+
return decodeContent(await this.readFileBuffer(path), typeof options === "string" ? options : options?.encoding);
|
|
66
|
+
}
|
|
67
|
+
async readFileBuffer(path) {
|
|
68
|
+
const relativePath = this.#toRelativePath(path);
|
|
69
|
+
return toShellContent(await this.workspace.readFile(relativePath, { encoding: "binary" }));
|
|
70
|
+
}
|
|
71
|
+
async writeFile(path, content, _options) {
|
|
72
|
+
await this.#requireWritable().writeFile(this.#toRelativePath(path), content);
|
|
73
|
+
await this.#refreshPaths();
|
|
74
|
+
}
|
|
75
|
+
async appendFile(path, content, _options) {
|
|
76
|
+
const workspace = this.#requireWritable();
|
|
77
|
+
const relativePath = this.#toRelativePath(path);
|
|
78
|
+
const current = toShellContent(await workspace.readFile(relativePath, { encoding: "binary" }).catch(() => new Uint8Array()));
|
|
79
|
+
const next = toShellContent(content);
|
|
80
|
+
const merged = new Uint8Array(current.byteLength + next.byteLength);
|
|
81
|
+
merged.set(current, 0);
|
|
82
|
+
merged.set(next, current.byteLength);
|
|
83
|
+
await workspace.writeFile(relativePath, merged);
|
|
84
|
+
await this.#refreshPaths();
|
|
85
|
+
}
|
|
86
|
+
async exists(path) {
|
|
87
|
+
try {
|
|
88
|
+
const relativePath = this.#toRelativePath(path);
|
|
89
|
+
return await this.workspace.exists(relativePath);
|
|
90
|
+
} catch {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async stat(path) {
|
|
95
|
+
const absolutePath = this.#resolveFromRoot(path);
|
|
96
|
+
if (absolutePath === "/workspace") return statFromEntry({
|
|
97
|
+
path: "",
|
|
98
|
+
type: "directory"
|
|
99
|
+
});
|
|
100
|
+
return statFromEntry(await this.workspace.stat(this.#toRelativePath(absolutePath)));
|
|
101
|
+
}
|
|
102
|
+
async mkdir(path, options) {
|
|
103
|
+
await this.#requireWritable().mkdir(this.#toRelativePath(path), { recursive: options?.recursive });
|
|
104
|
+
await this.#refreshPaths();
|
|
105
|
+
}
|
|
106
|
+
async readdir(path) {
|
|
107
|
+
return (await this.readdirWithFileTypes(path)).map((entry) => entry.name);
|
|
108
|
+
}
|
|
109
|
+
async readdirWithFileTypes(path) {
|
|
110
|
+
const absolutePath = this.#resolveFromRoot(path);
|
|
111
|
+
const relativePath = absolutePath === "/workspace" ? "" : this.#toRelativePath(absolutePath);
|
|
112
|
+
return (await this.workspace.list(relativePath, { recursive: false })).map((entry) => {
|
|
113
|
+
const name = relativePath ? entry.path.slice(relativePath.length + 1) : entry.path;
|
|
114
|
+
return {
|
|
115
|
+
isDirectory: entry.type === "directory",
|
|
116
|
+
isFile: entry.type === "file",
|
|
117
|
+
isSymbolicLink: false,
|
|
118
|
+
name
|
|
119
|
+
};
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
async rm(path, options) {
|
|
123
|
+
await this.#requireWritable().rm(this.#toRelativePath(path), {
|
|
124
|
+
force: options?.force,
|
|
125
|
+
recursive: options?.recursive
|
|
126
|
+
});
|
|
127
|
+
await this.#refreshPaths();
|
|
128
|
+
}
|
|
129
|
+
async cp(src, dest, _options) {
|
|
130
|
+
await copyWorkspacePath(this.#requireWritable(), this.#toRelativePath(src), this.#toRelativePath(dest));
|
|
131
|
+
await this.#refreshPaths();
|
|
132
|
+
}
|
|
133
|
+
async mv(src, dest) {
|
|
134
|
+
const workspace = this.#requireWritable();
|
|
135
|
+
const from = this.#toRelativePath(src);
|
|
136
|
+
await copyWorkspacePath(workspace, from, this.#toRelativePath(dest));
|
|
137
|
+
await workspace.rm(from, {
|
|
138
|
+
recursive: true,
|
|
139
|
+
force: true
|
|
140
|
+
});
|
|
141
|
+
await this.#refreshPaths();
|
|
142
|
+
}
|
|
143
|
+
resolvePath(base, path) {
|
|
144
|
+
const normalizedInput = normalizeInputPath(path);
|
|
145
|
+
if (!normalizedInput || normalizedInput === ".") return this.#resolveAbsolute(base);
|
|
146
|
+
if (normalizedInput === "/" || normalizedInput === "/workspace") return workspaceMountPoint;
|
|
147
|
+
const normalizedBase = this.#resolveAbsolute(base);
|
|
148
|
+
if (normalizedInput === ".." && normalizedBase === "/workspace") return workspaceMountPoint;
|
|
149
|
+
const absolute = normalizedInput.startsWith("/") ? normalizeAbsolutePath(normalizedInput) : normalizeAbsolutePath(posix.join(normalizedBase, normalizedInput));
|
|
150
|
+
if (absolute === "/workspace" || absolute.startsWith(`/workspace/`)) return absolute;
|
|
151
|
+
throw createEscapeError(path);
|
|
152
|
+
}
|
|
153
|
+
getAllPaths() {
|
|
154
|
+
return [...this.#paths];
|
|
155
|
+
}
|
|
156
|
+
async chmod(_path, _mode) {
|
|
157
|
+
throw new Error("chmod is not supported by the workspace filesystem.");
|
|
158
|
+
}
|
|
159
|
+
async symlink(_target, _linkPath) {
|
|
160
|
+
throw new Error("symlink is not supported by the workspace filesystem.");
|
|
161
|
+
}
|
|
162
|
+
async link(_existingPath, _newPath) {
|
|
163
|
+
throw new Error("link is not supported by the workspace filesystem.");
|
|
164
|
+
}
|
|
165
|
+
async readlink(_path) {
|
|
166
|
+
throw new Error("readlink is not supported by the workspace filesystem.");
|
|
167
|
+
}
|
|
168
|
+
async lstat(path) {
|
|
169
|
+
return await this.stat(path);
|
|
170
|
+
}
|
|
171
|
+
async realpath(path) {
|
|
172
|
+
return this.#resolveFromRoot(path);
|
|
173
|
+
}
|
|
174
|
+
async utimes(_path, _atime, _mtime) {
|
|
175
|
+
throw new Error("utimes is not supported by the workspace filesystem.");
|
|
176
|
+
}
|
|
177
|
+
#requireWritable() {
|
|
178
|
+
if (!this.writeFs) throw createReadonlyError();
|
|
179
|
+
return this.workspace;
|
|
180
|
+
}
|
|
181
|
+
#resolveAbsolute(path) {
|
|
182
|
+
const normalized = normalizeInputPath(path || "/workspace");
|
|
183
|
+
if (normalized === "/" || normalized === "/workspace") return workspaceMountPoint;
|
|
184
|
+
const absolute = normalizeAbsolutePath(normalized.startsWith("/") ? normalized : `${workspaceMountPoint}/${normalized}`);
|
|
185
|
+
if (absolute === "/workspace" || absolute.startsWith(`/workspace/`)) return absolute;
|
|
186
|
+
throw createEscapeError(path);
|
|
187
|
+
}
|
|
188
|
+
#resolveFromRoot(path) {
|
|
189
|
+
return this.resolvePath(workspaceMountPoint, path);
|
|
190
|
+
}
|
|
191
|
+
#toRelativePath(path) {
|
|
192
|
+
const absolutePath = this.#resolveFromRoot(path);
|
|
193
|
+
if (absolutePath === "/workspace") return "";
|
|
194
|
+
return absolutePath.slice(`${workspaceMountPoint}/`.length);
|
|
195
|
+
}
|
|
196
|
+
async #refreshPaths() {
|
|
197
|
+
this.#refreshPromise ||= (async () => {
|
|
198
|
+
const entries = await this.workspace.list("", { recursive: true });
|
|
199
|
+
this.#paths = [workspaceMountPoint, ...entries.map((entry) => entry.path ? `${workspaceMountPoint}/${entry.path}` : workspaceMountPoint).sort((left, right) => left.localeCompare(right))];
|
|
200
|
+
})();
|
|
201
|
+
try {
|
|
202
|
+
await this.#refreshPromise;
|
|
203
|
+
} finally {
|
|
204
|
+
this.#refreshPromise = void 0;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
function createReadonlyWorkspaceFs(workspace) {
|
|
209
|
+
return new WorkspaceFileSystem(workspace, false);
|
|
210
|
+
}
|
|
211
|
+
function createWritableWorkspaceFs(workspace) {
|
|
212
|
+
return new WorkspaceFileSystem(workspace, true);
|
|
213
|
+
}
|
|
214
|
+
//#endregion
|
|
215
|
+
//#region src/workspace/command-analysis.ts
|
|
216
|
+
function analyzeWorkspaceInspectionCommand(command) {
|
|
217
|
+
return splitShellCommandSegments(command).map((segment) => {
|
|
218
|
+
const words = parseShellWords(segment.command);
|
|
219
|
+
return {
|
|
220
|
+
...segment,
|
|
221
|
+
paths: shellPathArguments(words),
|
|
222
|
+
searchRecursive: hasRecursiveGrepFlag(words),
|
|
223
|
+
words
|
|
224
|
+
};
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
function shellPathArguments(words) {
|
|
228
|
+
switch (words[0]) {
|
|
229
|
+
case "rg":
|
|
230
|
+
case "grep": return commandPathArguments(words[0], words);
|
|
231
|
+
case "find": return findPathArguments(words);
|
|
232
|
+
case "cat":
|
|
233
|
+
case "head":
|
|
234
|
+
case "tail":
|
|
235
|
+
case "wc":
|
|
236
|
+
case "ls": return fileCommandPathArguments(words);
|
|
237
|
+
default: return [];
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
function findPathArguments(words) {
|
|
241
|
+
const paths = [];
|
|
242
|
+
let collectingPaths = true;
|
|
243
|
+
const args = words.slice(1);
|
|
244
|
+
for (let index = 0; index < args.length; index++) {
|
|
245
|
+
const arg = args[index];
|
|
246
|
+
if (isShellOperator(arg)) break;
|
|
247
|
+
if (arg === "--") {
|
|
248
|
+
paths.push(...pathArgumentsUntilShellBoundary(args.slice(index + 1)));
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
if (collectingPaths && isFindLeadingOption(arg)) continue;
|
|
252
|
+
if (arg.startsWith("-")) break;
|
|
253
|
+
collectingPaths = false;
|
|
254
|
+
paths.push(arg);
|
|
255
|
+
}
|
|
256
|
+
return paths;
|
|
257
|
+
}
|
|
258
|
+
function fileCommandPathArguments(words) {
|
|
259
|
+
const paths = [];
|
|
260
|
+
for (let index = 1; index < words.length; index++) {
|
|
261
|
+
const arg = words[index];
|
|
262
|
+
if (arg === "--") {
|
|
263
|
+
paths.push(...pathArgumentsUntilShellBoundary(words.slice(index + 1)));
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
if (isShellOperator(arg)) break;
|
|
267
|
+
if (arg.startsWith("-")) {
|
|
268
|
+
if (takesFileCommandOptionValue(words[0], arg)) index += 1;
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
paths.push(arg);
|
|
272
|
+
}
|
|
273
|
+
return paths;
|
|
274
|
+
}
|
|
275
|
+
function isShellOperator(arg) {
|
|
276
|
+
return arg === "&&" || arg === "||" || isRedirectOperator(arg);
|
|
277
|
+
}
|
|
278
|
+
function isRedirectOperator(arg) {
|
|
279
|
+
return /^(?:\d*)[<>]+&?\d*$/.test(arg) || /^(?:\d*)[<>]/.test(arg);
|
|
280
|
+
}
|
|
281
|
+
function isSeparateRedirectOperator(arg) {
|
|
282
|
+
return /^(?:\d*)[<>]+&?\d*$/.test(arg);
|
|
283
|
+
}
|
|
284
|
+
function isFindLeadingOption(arg) {
|
|
285
|
+
return arg === "-H" || arg === "-L" || arg === "-P";
|
|
286
|
+
}
|
|
287
|
+
function commandPathArguments(command, words) {
|
|
288
|
+
const args = words.slice(1);
|
|
289
|
+
const paths = [];
|
|
290
|
+
let sawPattern = false;
|
|
291
|
+
for (let index = 0; index < args.length; index++) {
|
|
292
|
+
const arg = args[index];
|
|
293
|
+
if (arg === "--") {
|
|
294
|
+
paths.push(...searchPathArgumentsAfterTerminator(args.slice(index + 1), sawPattern));
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
if (isRedirectOperator(arg)) {
|
|
298
|
+
if (sawPattern) break;
|
|
299
|
+
if (isSeparateRedirectOperator(arg)) index += 1;
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
if (isShellOperator(arg) && sawPattern) break;
|
|
303
|
+
if (arg.startsWith("-")) {
|
|
304
|
+
if (takesOptionValue(command, arg)) {
|
|
305
|
+
if (takesSearchPatternOptionValue(arg)) sawPattern = true;
|
|
306
|
+
index += 1;
|
|
307
|
+
} else if (takesInlineOptionValue(command, arg)) continue;
|
|
308
|
+
else if (takesInlineSearchPatternOptionValue(arg)) sawPattern = true;
|
|
309
|
+
continue;
|
|
310
|
+
}
|
|
311
|
+
if (!sawPattern) {
|
|
312
|
+
sawPattern = true;
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
315
|
+
paths.push(arg);
|
|
316
|
+
}
|
|
317
|
+
return paths;
|
|
318
|
+
}
|
|
319
|
+
function searchPathArgumentsAfterTerminator(args, sawPattern) {
|
|
320
|
+
if (sawPattern) return pathArgumentsUntilShellBoundary(args);
|
|
321
|
+
return pathArgumentsUntilShellBoundary(args.slice(1));
|
|
322
|
+
}
|
|
323
|
+
function takesOptionValue(command, arg) {
|
|
324
|
+
const grepOptions = [
|
|
325
|
+
"-A",
|
|
326
|
+
"-B",
|
|
327
|
+
"-C",
|
|
328
|
+
"-e",
|
|
329
|
+
"-f",
|
|
330
|
+
"-m",
|
|
331
|
+
"-d",
|
|
332
|
+
"--after-context",
|
|
333
|
+
"--before-context",
|
|
334
|
+
"--context",
|
|
335
|
+
"--directories",
|
|
336
|
+
"--exclude",
|
|
337
|
+
"--exclude-dir",
|
|
338
|
+
"--exclude-from",
|
|
339
|
+
"--file",
|
|
340
|
+
"--include",
|
|
341
|
+
"--max-count",
|
|
342
|
+
"--regexp"
|
|
343
|
+
];
|
|
344
|
+
const rgOptions = [
|
|
345
|
+
...grepOptions,
|
|
346
|
+
"-g",
|
|
347
|
+
"-t",
|
|
348
|
+
"-T",
|
|
349
|
+
"--glob",
|
|
350
|
+
"--iglob",
|
|
351
|
+
"--ignore-file",
|
|
352
|
+
"--max-depth",
|
|
353
|
+
"--max-filesize",
|
|
354
|
+
"--pre",
|
|
355
|
+
"--type",
|
|
356
|
+
"--type-add",
|
|
357
|
+
"--type-clear",
|
|
358
|
+
"--type-not"
|
|
359
|
+
];
|
|
360
|
+
return (command === "grep" ? grepOptions : rgOptions).includes(arg);
|
|
361
|
+
}
|
|
362
|
+
function takesInlineOptionValue(command, arg) {
|
|
363
|
+
const shared = arg.startsWith("-d") && arg !== "-d" || arg.startsWith("--directories=") || arg.startsWith("--exclude=") || arg.startsWith("--exclude-dir=") || arg.startsWith("--exclude-from=") || arg.startsWith("--include=");
|
|
364
|
+
if (command === "grep") return shared;
|
|
365
|
+
return shared || arg.startsWith("--iglob=") || arg.startsWith("--ignore-file=") || arg.startsWith("--max-depth=") || arg.startsWith("--max-filesize=") || arg.startsWith("--pre=") || arg.startsWith("--type=") || arg.startsWith("--type-add=") || arg.startsWith("--type-clear=") || arg.startsWith("--type-not=");
|
|
366
|
+
}
|
|
367
|
+
function takesSearchPatternOptionValue(arg) {
|
|
368
|
+
return arg === "-e" || arg === "-f" || arg === "--regexp" || arg === "--file";
|
|
369
|
+
}
|
|
370
|
+
function takesInlineSearchPatternOptionValue(arg) {
|
|
371
|
+
return arg.startsWith("--regexp=") || arg.startsWith("--file=") || arg.startsWith("-e") && arg !== "-e" || arg.startsWith("-f") && arg !== "-f";
|
|
372
|
+
}
|
|
373
|
+
function takesFileCommandOptionValue(command, arg) {
|
|
374
|
+
if (command === "head" || command === "tail") return arg === "-c" || arg === "-n" || arg === "--bytes" || arg === "--lines";
|
|
375
|
+
if (command === "ls") return arg === "-I" || arg === "--ignore";
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
function pathArgumentsUntilShellBoundary(args) {
|
|
379
|
+
const paths = [];
|
|
380
|
+
for (const arg of args) {
|
|
381
|
+
if (isShellOperator(arg)) break;
|
|
382
|
+
paths.push(arg);
|
|
383
|
+
}
|
|
384
|
+
return paths;
|
|
385
|
+
}
|
|
386
|
+
function splitShellCommandSegments(command) {
|
|
387
|
+
const segments = [];
|
|
388
|
+
let current = "";
|
|
389
|
+
let quote;
|
|
390
|
+
let escaped = false;
|
|
391
|
+
let followsPipe = false;
|
|
392
|
+
for (let index = 0; index < command.length; index++) {
|
|
393
|
+
const char = command[index];
|
|
394
|
+
if (escaped) {
|
|
395
|
+
current += char;
|
|
396
|
+
escaped = false;
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
if (char === "\\") {
|
|
400
|
+
current += char;
|
|
401
|
+
escaped = true;
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
if (quote) {
|
|
405
|
+
if (char === quote) quote = void 0;
|
|
406
|
+
current += char;
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
if (char === "'" || char === "\"") {
|
|
410
|
+
quote = char;
|
|
411
|
+
current += char;
|
|
412
|
+
continue;
|
|
413
|
+
}
|
|
414
|
+
const next = command[index + 1];
|
|
415
|
+
if (char === "&" && next === "&") {
|
|
416
|
+
segments.push({
|
|
417
|
+
command: current,
|
|
418
|
+
followsPipe,
|
|
419
|
+
separatorAfter: "&&"
|
|
420
|
+
});
|
|
421
|
+
current = "";
|
|
422
|
+
followsPipe = false;
|
|
423
|
+
index += 1;
|
|
424
|
+
continue;
|
|
425
|
+
}
|
|
426
|
+
if (char === "|" && next === "|") {
|
|
427
|
+
segments.push({
|
|
428
|
+
command: current,
|
|
429
|
+
followsPipe,
|
|
430
|
+
separatorAfter: "||"
|
|
431
|
+
});
|
|
432
|
+
current = "";
|
|
433
|
+
followsPipe = false;
|
|
434
|
+
index += 1;
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (char === "|" || char === ";" || char === "\n") {
|
|
438
|
+
segments.push({
|
|
439
|
+
command: current,
|
|
440
|
+
followsPipe,
|
|
441
|
+
separatorAfter: char
|
|
442
|
+
});
|
|
443
|
+
current = "";
|
|
444
|
+
followsPipe = char === "|";
|
|
445
|
+
continue;
|
|
446
|
+
}
|
|
447
|
+
current += char;
|
|
448
|
+
}
|
|
449
|
+
segments.push({
|
|
450
|
+
command: current,
|
|
451
|
+
followsPipe
|
|
452
|
+
});
|
|
453
|
+
return segments;
|
|
454
|
+
}
|
|
455
|
+
function hasRecursiveGrepFlag(words) {
|
|
456
|
+
const args = words.slice(1);
|
|
457
|
+
for (let index = 0; index < args.length; index++) {
|
|
458
|
+
const arg = args[index];
|
|
459
|
+
if (arg === "--recursive" || arg === "-r" || arg === "-R") return true;
|
|
460
|
+
if (arg === "--directories" || arg === "-d") return args[index + 1] === "recurse";
|
|
461
|
+
if (arg === "--directories=recurse" || arg === "-drecurse") return true;
|
|
462
|
+
if (arg.startsWith("-d") && arg !== "-d") continue;
|
|
463
|
+
if (takesOptionValue("grep", arg)) {
|
|
464
|
+
index += 1;
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
if (takesInlineSearchPatternOptionValue(arg)) continue;
|
|
468
|
+
if (!/^-[^-]/.test(arg)) return false;
|
|
469
|
+
if ((arg.startsWith("-e") || arg.startsWith("-f")) && arg.length > 2) return false;
|
|
470
|
+
if (arg.includes("r") || arg.includes("R")) return true;
|
|
471
|
+
}
|
|
472
|
+
return false;
|
|
473
|
+
}
|
|
474
|
+
function parseShellWords(command) {
|
|
475
|
+
const words = [];
|
|
476
|
+
let current = "";
|
|
477
|
+
let quote;
|
|
478
|
+
let escaped = false;
|
|
479
|
+
for (const char of command) {
|
|
480
|
+
if (escaped) {
|
|
481
|
+
current += char;
|
|
482
|
+
escaped = false;
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
if (char === "\\") {
|
|
486
|
+
escaped = true;
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
if (quote) {
|
|
490
|
+
if (char === quote) quote = void 0;
|
|
491
|
+
else current += char;
|
|
492
|
+
continue;
|
|
493
|
+
}
|
|
494
|
+
if (char === "'" || char === "\"") {
|
|
495
|
+
quote = char;
|
|
496
|
+
continue;
|
|
497
|
+
}
|
|
498
|
+
if (/\s/.test(char)) {
|
|
499
|
+
if (current) {
|
|
500
|
+
words.push(current);
|
|
501
|
+
current = "";
|
|
502
|
+
}
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
current += char;
|
|
506
|
+
}
|
|
507
|
+
if (current) words.push(current);
|
|
508
|
+
return words;
|
|
509
|
+
}
|
|
510
|
+
//#endregion
|
|
511
|
+
//#region src/workspace/path.ts
|
|
512
|
+
function cleanWorkspaceShellPath(path = ".") {
|
|
513
|
+
const trimmed = path.trim() || ".";
|
|
514
|
+
if (trimmed === "." || trimmed === "./" || trimmed === "/" || trimmed === "/workspace") return "";
|
|
515
|
+
return normalizeSafeShellPath(trimmed.replace(/^\/workspace(\/|$)/, ""));
|
|
516
|
+
}
|
|
517
|
+
function cleanWorkspaceMutationPath(path) {
|
|
518
|
+
const normalized = cleanWorkspaceShellPath(path);
|
|
519
|
+
if (!normalized) throw new Error("[vitehub] Workspace root is not a valid mutation target.");
|
|
520
|
+
return normalized;
|
|
521
|
+
}
|
|
522
|
+
function normalizeSafeShellPath(path) {
|
|
523
|
+
const normalized = posix.normalize(path.replace(/\\/g, "/")).replace(/^\//, "");
|
|
524
|
+
if (normalized === ".") return "";
|
|
525
|
+
if (normalized === ".." || normalized.startsWith("../")) throw new Error(`[vitehub] Workspace path escapes the workspace root: "${path}".`);
|
|
526
|
+
return normalized;
|
|
527
|
+
}
|
|
528
|
+
//#endregion
|
|
529
|
+
//#region src/workspace/inspection.ts
|
|
530
|
+
async function runWorkspaceInspectionCommand(_input, command, options) {
|
|
531
|
+
const maxOutputLength = options.maxOutputLength || 3e4;
|
|
532
|
+
const timeout = options.timeout || 3e4;
|
|
533
|
+
const preflight = await preflightWorkspaceInspectionCommand(command, options.fs, options.broadSearchPaths, options.cwd);
|
|
534
|
+
if (preflight) return preflight;
|
|
535
|
+
const missingPath = await preflightMissingWorkspacePath(command, options.fs, options.cwd);
|
|
536
|
+
if (missingPath) return missingPath;
|
|
537
|
+
const result = await createShellRuntime({
|
|
538
|
+
policy: {
|
|
539
|
+
maxOutputLength,
|
|
540
|
+
timeout
|
|
541
|
+
},
|
|
542
|
+
provider: createJustBashProvider({
|
|
543
|
+
commands: options.commands,
|
|
544
|
+
cwd: options.cwd || "/workspace",
|
|
545
|
+
fs: options.fs
|
|
546
|
+
})
|
|
547
|
+
}).exec(command, {
|
|
548
|
+
cwd: options.cwd || "/workspace",
|
|
549
|
+
timeout
|
|
550
|
+
});
|
|
551
|
+
const noMatchFeedback = searchNoMatchFeedback(command, result, options.broadSearchPaths, options.cwd);
|
|
552
|
+
return noMatchFeedback ? {
|
|
553
|
+
...result,
|
|
554
|
+
stdout: noMatchFeedback,
|
|
555
|
+
workspaceGuardrail: { kind: "no_match" }
|
|
556
|
+
} : result;
|
|
557
|
+
}
|
|
558
|
+
async function preflightWorkspaceInspectionCommand(command, fs, broadSearchPaths = [], cwd = workspaceMountPoint) {
|
|
559
|
+
try {
|
|
560
|
+
let currentCwd = cwd;
|
|
561
|
+
let skipAndChain = false;
|
|
562
|
+
let skipOrChain = false;
|
|
563
|
+
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
564
|
+
if (skipAndChain) {
|
|
565
|
+
skipAndChain = segment.separatorAfter === "&&";
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
if (skipOrChain) {
|
|
569
|
+
skipOrChain = segment.separatorAfter === "||";
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
const words = segment.words;
|
|
573
|
+
if (words[0] === "cd") {
|
|
574
|
+
const path = words[1] || "/workspace";
|
|
575
|
+
if (!isWorkspacePathCandidate(path)) continue;
|
|
576
|
+
const resolvedPath = resolveWorkspaceShellPath(currentCwd, path);
|
|
577
|
+
const directory = await workspacePathIsDirectory(fs, resolvedPath);
|
|
578
|
+
if (segment.separatorAfter === "&&" && !directory) skipAndChain = true;
|
|
579
|
+
if (segment.separatorAfter === "||" && directory) skipOrChain = true;
|
|
580
|
+
if (directory) currentCwd = resolvedPath ? posix.join(workspaceMountPoint, resolvedPath) : workspaceMountPoint;
|
|
581
|
+
continue;
|
|
582
|
+
}
|
|
583
|
+
if (isBroadWorkspaceSearch(segment, broadSearchPaths, currentCwd)) return broadWorkspaceSearchFeedback(broadSearchPaths);
|
|
584
|
+
}
|
|
585
|
+
} catch {
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
async function preflightMissingWorkspacePath(command, fs, cwd = workspaceMountPoint) {
|
|
590
|
+
try {
|
|
591
|
+
let currentCwd = cwd;
|
|
592
|
+
let skipAndChain = false;
|
|
593
|
+
let skipOrChain = false;
|
|
594
|
+
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
595
|
+
if (skipAndChain) {
|
|
596
|
+
skipAndChain = segment.separatorAfter === "&&";
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
if (skipOrChain) {
|
|
600
|
+
skipOrChain = segment.separatorAfter === "||";
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
const words = segment.words;
|
|
604
|
+
if (words[0] === "cd") {
|
|
605
|
+
const path = words[1] || "/workspace";
|
|
606
|
+
if (!isWorkspacePathCandidate(path)) continue;
|
|
607
|
+
const resolvedPath = resolveWorkspaceShellPath(currentCwd, path);
|
|
608
|
+
const directory = await workspacePathIsDirectory(fs, resolvedPath);
|
|
609
|
+
if (segment.separatorAfter === "&&" && !directory) skipAndChain = true;
|
|
610
|
+
if (segment.separatorAfter === "||" && directory) skipOrChain = true;
|
|
611
|
+
if (directory) currentCwd = resolvedPath ? posix.join(workspaceMountPoint, resolvedPath) : workspaceMountPoint;
|
|
612
|
+
continue;
|
|
613
|
+
}
|
|
614
|
+
for (const path of segment.paths) {
|
|
615
|
+
if (!isConcreteWorkspacePath(path)) continue;
|
|
616
|
+
const resolvedPath = resolveWorkspaceShellPath(currentCwd, path);
|
|
617
|
+
if (await fs.exists(resolvedPath)) continue;
|
|
618
|
+
if (segment.separatorAfter === "||") continue;
|
|
619
|
+
return missingWorkspacePathFeedback(command, resolvedPath);
|
|
620
|
+
}
|
|
621
|
+
if (segment.separatorAfter === "&&" || segment.separatorAfter === "||") return void 0;
|
|
622
|
+
}
|
|
623
|
+
} catch {
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
async function workspacePathIsDirectory(fs, path) {
|
|
628
|
+
if (path === "") return true;
|
|
629
|
+
try {
|
|
630
|
+
return (await fs.stat(path)).isDirectory;
|
|
631
|
+
} catch {
|
|
632
|
+
return false;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
function missingWorkspacePathFeedback(command, resolvedPath) {
|
|
636
|
+
return {
|
|
637
|
+
command,
|
|
638
|
+
event: "command_finished",
|
|
639
|
+
exitCode: 0,
|
|
640
|
+
stderr: "",
|
|
641
|
+
stdout: [
|
|
642
|
+
`[vitehub] Workspace path is not mounted: ${resolvedPath}`,
|
|
643
|
+
"The agent cannot inspect files outside the configured workspace sources.",
|
|
644
|
+
"If this path should exist, update the Agent workspace source configuration or materialize the correct mounted source.",
|
|
645
|
+
"Otherwise answer that the requested evidence is unavailable in the current workspace."
|
|
646
|
+
].join("\n") + "\n",
|
|
647
|
+
workspaceGuardrail: {
|
|
648
|
+
kind: "missing_path",
|
|
649
|
+
path: resolvedPath
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
}
|
|
653
|
+
function isBroadWorkspaceSearch(segment, broadSearchPaths = [], cwd = workspaceMountPoint) {
|
|
654
|
+
const name = segment.words[0];
|
|
655
|
+
if (name === "rg" || name === "grep") {
|
|
656
|
+
const paths = segment.paths;
|
|
657
|
+
const pipeSafeStdinFilter = name === "grep" && segment.followsPipe && !segment.searchRecursive;
|
|
658
|
+
return paths.length === 0 && !pipeSafeStdinFilter || paths.length > 4 || paths.some((path) => isBroadWorkspacePath(path, broadSearchPaths, cwd));
|
|
659
|
+
}
|
|
660
|
+
if (name === "find") {
|
|
661
|
+
const paths = segment.paths;
|
|
662
|
+
return paths.length === 0 && isBroadWorkspacePath(".", broadSearchPaths, cwd) || paths.some((path) => isBroadWorkspacePath(path, broadSearchPaths, cwd));
|
|
663
|
+
}
|
|
664
|
+
return false;
|
|
665
|
+
}
|
|
666
|
+
function broadWorkspaceSearchFeedback(broadSearchPaths = []) {
|
|
667
|
+
const hint = broadSearchPaths.length ? ` Try one of these paths: ${broadSearchPaths.map((path) => `"${path}"`).join(", ")}.` : " Use a narrow mounted source or subdirectory path instead.";
|
|
668
|
+
const feedback = [
|
|
669
|
+
"[vitehub] Workspace search is too broad for this agent tool.",
|
|
670
|
+
"Use one specific mounted subdirectory or file path.",
|
|
671
|
+
"If the requested evidence is not in the mounted workspace paths, answer that it is unavailable instead of trying broader searches."
|
|
672
|
+
].join("\n") + "\n";
|
|
673
|
+
return {
|
|
674
|
+
command: "",
|
|
675
|
+
event: "policy_denied",
|
|
676
|
+
exitCode: 126,
|
|
677
|
+
stderr: `[vitehub] Workspace root search is too broad.${hint}\n`,
|
|
678
|
+
stdout: feedback,
|
|
679
|
+
workspaceGuardrail: { kind: "broad_search" }
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
function isBroadWorkspacePath(path, broadSearchPaths = [], cwd = workspaceMountPoint) {
|
|
683
|
+
const normalized = cleanWorkspaceShellPath(resolvedWorkspaceCwd(cwd, path)) || ".";
|
|
684
|
+
return normalized === "." || broadSearchPaths.includes(normalized);
|
|
685
|
+
}
|
|
686
|
+
function searchNoMatchFeedback(command, result, broadSearchPaths = [], cwd = workspaceMountPoint) {
|
|
687
|
+
if (result.exitCode !== 1 || result.stderr || result.stdout) return "";
|
|
688
|
+
try {
|
|
689
|
+
let currentCwd = cwd;
|
|
690
|
+
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
691
|
+
const words = segment.words;
|
|
692
|
+
if (words[0] === "cd") {
|
|
693
|
+
const path = words[1] || "/workspace";
|
|
694
|
+
if (isWorkspacePathCandidate(path)) currentCwd = resolvedWorkspaceCwd(currentCwd, path);
|
|
695
|
+
continue;
|
|
696
|
+
}
|
|
697
|
+
if (words[0] !== "rg" && words[0] !== "grep") {
|
|
698
|
+
if (segment.separatorAfter === "&&") return "";
|
|
699
|
+
continue;
|
|
700
|
+
}
|
|
701
|
+
if (!segment.paths.some((path) => isBroadWorkspacePath(path, broadSearchPaths, currentCwd))) {
|
|
702
|
+
if (segment.separatorAfter === "&&") return "";
|
|
703
|
+
continue;
|
|
704
|
+
}
|
|
705
|
+
return [
|
|
706
|
+
"[vitehub] Search returned no matches in the mounted workspace source.",
|
|
707
|
+
"If the expected file is outside this mounted source, tell the user the evidence is unavailable in the current workspace instead of continuing broad searches.",
|
|
708
|
+
"Use a more specific mounted subdirectory only if the user request can be answered from the listed workspace paths."
|
|
709
|
+
].join("\n") + "\n";
|
|
710
|
+
}
|
|
711
|
+
} catch {
|
|
712
|
+
return "";
|
|
713
|
+
}
|
|
714
|
+
return "";
|
|
715
|
+
}
|
|
716
|
+
function isConcreteWorkspacePath(path) {
|
|
717
|
+
return isWorkspacePathCandidate(path) && cleanWorkspaceShellPath(path) !== "";
|
|
718
|
+
}
|
|
719
|
+
function isWorkspacePathCandidate(path) {
|
|
720
|
+
return Boolean(path) && path !== "-" && !path.includes("$") && !path.includes("`") && !path.includes("*") && !path.includes("?") && !path.includes("[");
|
|
721
|
+
}
|
|
722
|
+
function resolveWorkspaceShellPath(cwd, path) {
|
|
723
|
+
return cleanWorkspaceShellPath(resolvedWorkspaceCwd(cwd, path));
|
|
724
|
+
}
|
|
725
|
+
function resolvedWorkspaceCwd(cwd, path) {
|
|
726
|
+
if (path.startsWith("/") && !path.startsWith("/workspace")) return path;
|
|
727
|
+
if (path.startsWith("/workspace")) return path;
|
|
728
|
+
const normalizedCwd = cleanWorkspaceShellPath(cwd);
|
|
729
|
+
const resolvedPath = normalizedCwd ? posix.join(normalizedCwd, path) : path;
|
|
730
|
+
return posix.join(workspaceMountPoint, resolvedPath);
|
|
731
|
+
}
|
|
732
|
+
//#endregion
|
|
733
|
+
export { cleanWorkspaceMutationPath, cleanWorkspaceShellPath, createReadonlyWorkspaceFs, createWritableWorkspaceFs, runWorkspaceInspectionCommand, workspaceMountPoint };
|