@vite-hub/shell 0.0.0 → 0.0.2
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-Bu5Ueqph.js +126 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +3 -0
- package/dist/just-bash-D84udCFy.d.ts +27 -0
- package/dist/parse-D9VT4P6N.js +41 -0
- package/dist/providers/cloudflare.d.ts +28 -0
- package/dist/providers/cloudflare.js +44 -0
- package/dist/providers/just-bash.d.ts +2 -0
- package/dist/providers/just-bash.js +277 -0
- package/dist/session-0OrrlmE9.js +178 -0
- package/dist/types-CNRdehmi.d.ts +101 -0
- package/dist/workspace.d.ts +80 -0
- package/dist/workspace.js +896 -0
- package/package.json +50 -5
|
@@ -0,0 +1,896 @@
|
|
|
1
|
+
import { t as createShellRuntime } from "./session-0OrrlmE9.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
|
+
workspace;
|
|
58
|
+
writeFs;
|
|
59
|
+
#paths = [workspaceMountPoint];
|
|
60
|
+
#refreshPromise;
|
|
61
|
+
constructor(workspace, writeFs) {
|
|
62
|
+
this.workspace = workspace;
|
|
63
|
+
this.writeFs = writeFs;
|
|
64
|
+
}
|
|
65
|
+
async readFile(path, options) {
|
|
66
|
+
return decodeContent(await this.readFileBuffer(path), typeof options === "string" ? options : options?.encoding);
|
|
67
|
+
}
|
|
68
|
+
async readFileBuffer(path) {
|
|
69
|
+
const relativePath = this.#toRelativePath(path);
|
|
70
|
+
return toShellContent(await this.workspace.readFile(relativePath, { encoding: "binary" }));
|
|
71
|
+
}
|
|
72
|
+
async writeFile(path, content, _options) {
|
|
73
|
+
await this.#requireWritable().writeFile(this.#toRelativePath(path), content);
|
|
74
|
+
await this.#refreshPaths();
|
|
75
|
+
}
|
|
76
|
+
async appendFile(path, content, _options) {
|
|
77
|
+
const workspace = this.#requireWritable();
|
|
78
|
+
const relativePath = this.#toRelativePath(path);
|
|
79
|
+
const current = toShellContent(await workspace.readFile(relativePath, { encoding: "binary" }).catch(() => new Uint8Array()));
|
|
80
|
+
const next = toShellContent(content);
|
|
81
|
+
const merged = new Uint8Array(current.byteLength + next.byteLength);
|
|
82
|
+
merged.set(current, 0);
|
|
83
|
+
merged.set(next, current.byteLength);
|
|
84
|
+
await workspace.writeFile(relativePath, merged);
|
|
85
|
+
await this.#refreshPaths();
|
|
86
|
+
}
|
|
87
|
+
async exists(path) {
|
|
88
|
+
try {
|
|
89
|
+
const relativePath = this.#toRelativePath(path);
|
|
90
|
+
return await this.workspace.exists(relativePath);
|
|
91
|
+
} catch {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async stat(path) {
|
|
96
|
+
const absolutePath = this.#resolveFromRoot(path);
|
|
97
|
+
if (absolutePath === "/workspace") return statFromEntry({
|
|
98
|
+
path: "",
|
|
99
|
+
type: "directory"
|
|
100
|
+
});
|
|
101
|
+
return statFromEntry(await this.workspace.stat(this.#toRelativePath(absolutePath)));
|
|
102
|
+
}
|
|
103
|
+
async mkdir(path, options) {
|
|
104
|
+
await this.#requireWritable().mkdir(this.#toRelativePath(path), { recursive: options?.recursive });
|
|
105
|
+
await this.#refreshPaths();
|
|
106
|
+
}
|
|
107
|
+
async readdir(path) {
|
|
108
|
+
return (await this.readdirWithFileTypes(path)).map((entry) => entry.name);
|
|
109
|
+
}
|
|
110
|
+
async readdirWithFileTypes(path) {
|
|
111
|
+
const absolutePath = this.#resolveFromRoot(path);
|
|
112
|
+
const relativePath = absolutePath === "/workspace" ? "" : this.#toRelativePath(absolutePath);
|
|
113
|
+
return (await this.workspace.list(relativePath, { recursive: false })).map((entry) => {
|
|
114
|
+
const name = relativePath ? entry.path.slice(relativePath.length + 1) : entry.path;
|
|
115
|
+
return {
|
|
116
|
+
isDirectory: entry.type === "directory",
|
|
117
|
+
isFile: entry.type === "file",
|
|
118
|
+
isSymbolicLink: false,
|
|
119
|
+
name
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
async rm(path, options) {
|
|
124
|
+
await this.#requireWritable().rm(this.#toRelativePath(path), {
|
|
125
|
+
force: options?.force,
|
|
126
|
+
recursive: options?.recursive
|
|
127
|
+
});
|
|
128
|
+
await this.#refreshPaths();
|
|
129
|
+
}
|
|
130
|
+
async cp(src, dest, _options) {
|
|
131
|
+
await copyWorkspacePath(this.#requireWritable(), this.#toRelativePath(src), this.#toRelativePath(dest));
|
|
132
|
+
await this.#refreshPaths();
|
|
133
|
+
}
|
|
134
|
+
async mv(src, dest) {
|
|
135
|
+
const workspace = this.#requireWritable();
|
|
136
|
+
const from = this.#toRelativePath(src);
|
|
137
|
+
await copyWorkspacePath(workspace, from, this.#toRelativePath(dest));
|
|
138
|
+
await workspace.rm(from, {
|
|
139
|
+
recursive: true,
|
|
140
|
+
force: true
|
|
141
|
+
});
|
|
142
|
+
await this.#refreshPaths();
|
|
143
|
+
}
|
|
144
|
+
resolvePath(base, path) {
|
|
145
|
+
const normalizedInput = normalizeInputPath(path);
|
|
146
|
+
if (!normalizedInput || normalizedInput === ".") return this.#resolveAbsolute(base);
|
|
147
|
+
if (normalizedInput === "/" || normalizedInput === "/workspace") return workspaceMountPoint;
|
|
148
|
+
const normalizedBase = this.#resolveAbsolute(base);
|
|
149
|
+
if (normalizedInput === ".." && normalizedBase === "/workspace") return workspaceMountPoint;
|
|
150
|
+
const absolute = normalizedInput.startsWith("/") ? normalizeAbsolutePath(normalizedInput) : normalizeAbsolutePath(posix.join(normalizedBase, normalizedInput));
|
|
151
|
+
if (absolute === "/workspace" || absolute.startsWith(`/workspace/`)) return absolute;
|
|
152
|
+
throw createEscapeError(path);
|
|
153
|
+
}
|
|
154
|
+
getAllPaths() {
|
|
155
|
+
return [...this.#paths];
|
|
156
|
+
}
|
|
157
|
+
async chmod(_path, _mode) {
|
|
158
|
+
throw new Error("chmod is not supported by the workspace filesystem.");
|
|
159
|
+
}
|
|
160
|
+
async symlink(_target, _linkPath) {
|
|
161
|
+
throw new Error("symlink is not supported by the workspace filesystem.");
|
|
162
|
+
}
|
|
163
|
+
async link(_existingPath, _newPath) {
|
|
164
|
+
throw new Error("link is not supported by the workspace filesystem.");
|
|
165
|
+
}
|
|
166
|
+
async readlink(_path) {
|
|
167
|
+
throw new Error("readlink is not supported by the workspace filesystem.");
|
|
168
|
+
}
|
|
169
|
+
async lstat(path) {
|
|
170
|
+
return await this.stat(path);
|
|
171
|
+
}
|
|
172
|
+
async realpath(path) {
|
|
173
|
+
return this.#resolveFromRoot(path);
|
|
174
|
+
}
|
|
175
|
+
async utimes(_path, _atime, _mtime) {
|
|
176
|
+
throw new Error("utimes is not supported by the workspace filesystem.");
|
|
177
|
+
}
|
|
178
|
+
#requireWritable() {
|
|
179
|
+
if (!this.writeFs) throw createReadonlyError();
|
|
180
|
+
return this.workspace;
|
|
181
|
+
}
|
|
182
|
+
#resolveAbsolute(path) {
|
|
183
|
+
const normalized = normalizeInputPath(path || "/workspace");
|
|
184
|
+
if (normalized === "/" || normalized === "/workspace") return workspaceMountPoint;
|
|
185
|
+
const absolute = normalizeAbsolutePath(normalized.startsWith("/") ? normalized : `${workspaceMountPoint}/${normalized}`);
|
|
186
|
+
if (absolute === "/workspace" || absolute.startsWith(`/workspace/`)) return absolute;
|
|
187
|
+
throw createEscapeError(path);
|
|
188
|
+
}
|
|
189
|
+
#resolveFromRoot(path) {
|
|
190
|
+
return this.resolvePath(workspaceMountPoint, path);
|
|
191
|
+
}
|
|
192
|
+
#toRelativePath(path) {
|
|
193
|
+
const absolutePath = this.#resolveFromRoot(path);
|
|
194
|
+
if (absolutePath === "/workspace") return "";
|
|
195
|
+
return absolutePath.slice(`${workspaceMountPoint}/`.length);
|
|
196
|
+
}
|
|
197
|
+
async #refreshPaths() {
|
|
198
|
+
this.#refreshPromise ||= (async () => {
|
|
199
|
+
const entries = await this.workspace.list("", { recursive: true });
|
|
200
|
+
this.#paths = [workspaceMountPoint, ...entries.map((entry) => entry.path ? `${workspaceMountPoint}/${entry.path}` : workspaceMountPoint).sort((left, right) => left.localeCompare(right))];
|
|
201
|
+
})();
|
|
202
|
+
try {
|
|
203
|
+
await this.#refreshPromise;
|
|
204
|
+
} finally {
|
|
205
|
+
this.#refreshPromise = void 0;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
function createReadonlyWorkspaceFs(workspace) {
|
|
210
|
+
return new WorkspaceFileSystem(workspace, false);
|
|
211
|
+
}
|
|
212
|
+
function createWritableWorkspaceFs(workspace) {
|
|
213
|
+
return new WorkspaceFileSystem(workspace, true);
|
|
214
|
+
}
|
|
215
|
+
//#endregion
|
|
216
|
+
//#region src/workspace/command-analysis.ts
|
|
217
|
+
function analyzeWorkspaceInspectionCommand(command) {
|
|
218
|
+
return splitShellCommandSegments(command).map((segment) => {
|
|
219
|
+
const words = parseShellWords(segment.command);
|
|
220
|
+
return {
|
|
221
|
+
...segment,
|
|
222
|
+
paths: shellPathArguments(words),
|
|
223
|
+
searchRecursive: hasRecursiveGrepFlag(words),
|
|
224
|
+
words
|
|
225
|
+
};
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
function shellPathArguments(words) {
|
|
229
|
+
switch (words[0]) {
|
|
230
|
+
case "rg":
|
|
231
|
+
case "grep": return commandPathArguments(words[0], words);
|
|
232
|
+
case "find": return findPathArguments(words);
|
|
233
|
+
case "cat":
|
|
234
|
+
case "head":
|
|
235
|
+
case "tail":
|
|
236
|
+
case "wc":
|
|
237
|
+
case "ls": return fileCommandPathArguments(words);
|
|
238
|
+
default: return [];
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
function findPathArguments(words) {
|
|
242
|
+
const paths = [];
|
|
243
|
+
let collectingPaths = true;
|
|
244
|
+
const args = words.slice(1);
|
|
245
|
+
for (let index = 0; index < args.length; index++) {
|
|
246
|
+
const arg = args[index];
|
|
247
|
+
if (isShellOperator(arg)) break;
|
|
248
|
+
if (arg === "--") {
|
|
249
|
+
paths.push(...pathArgumentsUntilShellBoundary(args.slice(index + 1)));
|
|
250
|
+
break;
|
|
251
|
+
}
|
|
252
|
+
if (collectingPaths && isFindLeadingOption(arg)) continue;
|
|
253
|
+
if (arg.startsWith("-")) break;
|
|
254
|
+
collectingPaths = false;
|
|
255
|
+
paths.push(arg);
|
|
256
|
+
}
|
|
257
|
+
return paths;
|
|
258
|
+
}
|
|
259
|
+
function fileCommandPathArguments(words) {
|
|
260
|
+
const paths = [];
|
|
261
|
+
for (let index = 1; index < words.length; index++) {
|
|
262
|
+
const arg = words[index];
|
|
263
|
+
if (arg === "--") {
|
|
264
|
+
paths.push(...pathArgumentsUntilShellBoundary(words.slice(index + 1)));
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
if (isShellOperator(arg)) break;
|
|
268
|
+
if (arg.startsWith("-")) {
|
|
269
|
+
if (takesFileCommandOptionValue(words[0], arg)) index += 1;
|
|
270
|
+
continue;
|
|
271
|
+
}
|
|
272
|
+
paths.push(arg);
|
|
273
|
+
}
|
|
274
|
+
return paths;
|
|
275
|
+
}
|
|
276
|
+
function isShellOperator(arg) {
|
|
277
|
+
return arg === "&&" || arg === "||" || isRedirectOperator(arg);
|
|
278
|
+
}
|
|
279
|
+
function isRedirectOperator(arg) {
|
|
280
|
+
return /^(?:\d*)[<>]+&?\d*$/.test(arg) || /^(?:\d*)[<>]/.test(arg);
|
|
281
|
+
}
|
|
282
|
+
function isSeparateRedirectOperator(arg) {
|
|
283
|
+
return /^(?:\d*)[<>]+&?\d*$/.test(arg);
|
|
284
|
+
}
|
|
285
|
+
function isFindLeadingOption(arg) {
|
|
286
|
+
return arg === "-H" || arg === "-L" || arg === "-P";
|
|
287
|
+
}
|
|
288
|
+
function commandPathArguments(command, words) {
|
|
289
|
+
const args = words.slice(1);
|
|
290
|
+
const paths = [];
|
|
291
|
+
let sawPattern = false;
|
|
292
|
+
for (let index = 0; index < args.length; index++) {
|
|
293
|
+
const arg = args[index];
|
|
294
|
+
if (arg === "--") {
|
|
295
|
+
paths.push(...searchPathArgumentsAfterTerminator(args.slice(index + 1), sawPattern));
|
|
296
|
+
break;
|
|
297
|
+
}
|
|
298
|
+
if (isRedirectOperator(arg)) {
|
|
299
|
+
if (sawPattern) break;
|
|
300
|
+
if (isSeparateRedirectOperator(arg)) index += 1;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
if (isShellOperator(arg) && sawPattern) break;
|
|
304
|
+
if (arg.startsWith("-")) {
|
|
305
|
+
if (takesOptionValue(command, arg)) {
|
|
306
|
+
if (takesSearchPatternOptionValue(arg)) sawPattern = true;
|
|
307
|
+
index += 1;
|
|
308
|
+
} else if (takesInlineOptionValue(command, arg)) continue;
|
|
309
|
+
else if (takesInlineSearchPatternOptionValue(arg)) sawPattern = true;
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
if (!sawPattern) {
|
|
313
|
+
sawPattern = true;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
paths.push(arg);
|
|
317
|
+
}
|
|
318
|
+
return paths;
|
|
319
|
+
}
|
|
320
|
+
function searchPathArgumentsAfterTerminator(args, sawPattern) {
|
|
321
|
+
if (sawPattern) return pathArgumentsUntilShellBoundary(args);
|
|
322
|
+
return pathArgumentsUntilShellBoundary(args.slice(1));
|
|
323
|
+
}
|
|
324
|
+
function takesOptionValue(command, arg) {
|
|
325
|
+
const grepOptions = [
|
|
326
|
+
"-A",
|
|
327
|
+
"-B",
|
|
328
|
+
"-C",
|
|
329
|
+
"-e",
|
|
330
|
+
"-f",
|
|
331
|
+
"-m",
|
|
332
|
+
"-d",
|
|
333
|
+
"--after-context",
|
|
334
|
+
"--before-context",
|
|
335
|
+
"--context",
|
|
336
|
+
"--directories",
|
|
337
|
+
"--exclude",
|
|
338
|
+
"--exclude-dir",
|
|
339
|
+
"--exclude-from",
|
|
340
|
+
"--file",
|
|
341
|
+
"--include",
|
|
342
|
+
"--max-count",
|
|
343
|
+
"--regexp"
|
|
344
|
+
];
|
|
345
|
+
const rgOptions = [
|
|
346
|
+
...grepOptions,
|
|
347
|
+
"-g",
|
|
348
|
+
"-t",
|
|
349
|
+
"-T",
|
|
350
|
+
"--glob",
|
|
351
|
+
"--iglob",
|
|
352
|
+
"--ignore-file",
|
|
353
|
+
"--max-depth",
|
|
354
|
+
"--max-filesize",
|
|
355
|
+
"--pre",
|
|
356
|
+
"--type",
|
|
357
|
+
"--type-add",
|
|
358
|
+
"--type-clear",
|
|
359
|
+
"--type-not"
|
|
360
|
+
];
|
|
361
|
+
return (command === "grep" ? grepOptions : rgOptions).includes(arg);
|
|
362
|
+
}
|
|
363
|
+
function takesInlineOptionValue(command, arg) {
|
|
364
|
+
const shared = arg.startsWith("-d") && arg !== "-d" || arg.startsWith("--directories=") || arg.startsWith("--exclude=") || arg.startsWith("--exclude-dir=") || arg.startsWith("--exclude-from=") || arg.startsWith("--include=");
|
|
365
|
+
if (command === "grep") return shared;
|
|
366
|
+
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=");
|
|
367
|
+
}
|
|
368
|
+
function takesSearchPatternOptionValue(arg) {
|
|
369
|
+
return arg === "-e" || arg === "-f" || arg === "--regexp" || arg === "--file";
|
|
370
|
+
}
|
|
371
|
+
function takesInlineSearchPatternOptionValue(arg) {
|
|
372
|
+
return arg.startsWith("--regexp=") || arg.startsWith("--file=") || arg.startsWith("-e") && arg !== "-e" || arg.startsWith("-f") && arg !== "-f";
|
|
373
|
+
}
|
|
374
|
+
function takesFileCommandOptionValue(command, arg) {
|
|
375
|
+
if (command === "head" || command === "tail") return arg === "-c" || arg === "-n" || arg === "--bytes" || arg === "--lines";
|
|
376
|
+
if (command === "ls") return arg === "-I" || arg === "--ignore";
|
|
377
|
+
return false;
|
|
378
|
+
}
|
|
379
|
+
function pathArgumentsUntilShellBoundary(args) {
|
|
380
|
+
const paths = [];
|
|
381
|
+
for (const arg of args) {
|
|
382
|
+
if (isShellOperator(arg)) break;
|
|
383
|
+
paths.push(arg);
|
|
384
|
+
}
|
|
385
|
+
return paths;
|
|
386
|
+
}
|
|
387
|
+
function splitShellCommandSegments(command) {
|
|
388
|
+
const segments = [];
|
|
389
|
+
let current = "";
|
|
390
|
+
let quote;
|
|
391
|
+
let escaped = false;
|
|
392
|
+
let followsPipe = false;
|
|
393
|
+
for (let index = 0; index < command.length; index++) {
|
|
394
|
+
const char = command[index];
|
|
395
|
+
if (escaped) {
|
|
396
|
+
current += char;
|
|
397
|
+
escaped = false;
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
if (char === "\\") {
|
|
401
|
+
current += char;
|
|
402
|
+
escaped = true;
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
if (quote) {
|
|
406
|
+
if (char === quote) quote = void 0;
|
|
407
|
+
current += char;
|
|
408
|
+
continue;
|
|
409
|
+
}
|
|
410
|
+
if (char === "'" || char === "\"") {
|
|
411
|
+
quote = char;
|
|
412
|
+
current += char;
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
const next = command[index + 1];
|
|
416
|
+
if (char === "&" && next === "&") {
|
|
417
|
+
segments.push({
|
|
418
|
+
command: current,
|
|
419
|
+
followsPipe,
|
|
420
|
+
separatorAfter: "&&"
|
|
421
|
+
});
|
|
422
|
+
current = "";
|
|
423
|
+
followsPipe = false;
|
|
424
|
+
index += 1;
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (char === "|" && next === "|") {
|
|
428
|
+
segments.push({
|
|
429
|
+
command: current,
|
|
430
|
+
followsPipe,
|
|
431
|
+
separatorAfter: "||"
|
|
432
|
+
});
|
|
433
|
+
current = "";
|
|
434
|
+
followsPipe = false;
|
|
435
|
+
index += 1;
|
|
436
|
+
continue;
|
|
437
|
+
}
|
|
438
|
+
if (char === "|" || char === ";" || char === "\n") {
|
|
439
|
+
segments.push({
|
|
440
|
+
command: current,
|
|
441
|
+
followsPipe,
|
|
442
|
+
separatorAfter: char
|
|
443
|
+
});
|
|
444
|
+
current = "";
|
|
445
|
+
followsPipe = char === "|";
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
current += char;
|
|
449
|
+
}
|
|
450
|
+
segments.push({
|
|
451
|
+
command: current,
|
|
452
|
+
followsPipe
|
|
453
|
+
});
|
|
454
|
+
return segments;
|
|
455
|
+
}
|
|
456
|
+
function hasRecursiveGrepFlag(words) {
|
|
457
|
+
const args = words.slice(1);
|
|
458
|
+
for (let index = 0; index < args.length; index++) {
|
|
459
|
+
const arg = args[index];
|
|
460
|
+
if (arg === "--recursive" || arg === "-r" || arg === "-R") return true;
|
|
461
|
+
if (arg === "--directories" || arg === "-d") return args[index + 1] === "recurse";
|
|
462
|
+
if (arg === "--directories=recurse" || arg === "-drecurse") return true;
|
|
463
|
+
if (arg.startsWith("-d") && arg !== "-d") continue;
|
|
464
|
+
if (takesOptionValue("grep", arg)) {
|
|
465
|
+
index += 1;
|
|
466
|
+
continue;
|
|
467
|
+
}
|
|
468
|
+
if (takesInlineSearchPatternOptionValue(arg)) continue;
|
|
469
|
+
if (!/^-[^-]/.test(arg)) return false;
|
|
470
|
+
if ((arg.startsWith("-e") || arg.startsWith("-f")) && arg.length > 2) return false;
|
|
471
|
+
if (arg.includes("r") || arg.includes("R")) return true;
|
|
472
|
+
}
|
|
473
|
+
return false;
|
|
474
|
+
}
|
|
475
|
+
function parseShellWords(command) {
|
|
476
|
+
const words = [];
|
|
477
|
+
let current = "";
|
|
478
|
+
let quote;
|
|
479
|
+
let escaped = false;
|
|
480
|
+
for (const char of command) {
|
|
481
|
+
if (escaped) {
|
|
482
|
+
current += char;
|
|
483
|
+
escaped = false;
|
|
484
|
+
continue;
|
|
485
|
+
}
|
|
486
|
+
if (char === "\\") {
|
|
487
|
+
escaped = true;
|
|
488
|
+
continue;
|
|
489
|
+
}
|
|
490
|
+
if (quote) {
|
|
491
|
+
if (char === quote) quote = void 0;
|
|
492
|
+
else current += char;
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
if (char === "'" || char === "\"") {
|
|
496
|
+
quote = char;
|
|
497
|
+
continue;
|
|
498
|
+
}
|
|
499
|
+
if (/\s/.test(char)) {
|
|
500
|
+
if (current) {
|
|
501
|
+
words.push(current);
|
|
502
|
+
current = "";
|
|
503
|
+
}
|
|
504
|
+
continue;
|
|
505
|
+
}
|
|
506
|
+
current += char;
|
|
507
|
+
}
|
|
508
|
+
if (current) words.push(current);
|
|
509
|
+
return words;
|
|
510
|
+
}
|
|
511
|
+
//#endregion
|
|
512
|
+
//#region src/workspace/path.ts
|
|
513
|
+
function cleanWorkspaceShellPath(path = ".") {
|
|
514
|
+
const trimmed = path.trim() || ".";
|
|
515
|
+
if (trimmed === "." || trimmed === "./" || trimmed === "/" || trimmed === "/workspace") return "";
|
|
516
|
+
return normalizeSafeShellPath(trimmed.replace(/^\/workspace(\/|$)/, ""));
|
|
517
|
+
}
|
|
518
|
+
function cleanWorkspaceMutationPath(path) {
|
|
519
|
+
const normalized = cleanWorkspaceShellPath(path);
|
|
520
|
+
if (!normalized) throw new Error("[vitehub] Workspace root is not a valid mutation target.");
|
|
521
|
+
return normalized;
|
|
522
|
+
}
|
|
523
|
+
function normalizeSafeShellPath(path) {
|
|
524
|
+
const normalized = posix.normalize(path.replace(/\\/g, "/")).replace(/^\//, "");
|
|
525
|
+
if (normalized === ".") return "";
|
|
526
|
+
if (normalized === ".." || normalized.startsWith("../")) throw new Error(`[vitehub] Workspace path escapes the workspace root: "${path}".`);
|
|
527
|
+
return normalized;
|
|
528
|
+
}
|
|
529
|
+
//#endregion
|
|
530
|
+
//#region src/workspace/inspection.ts
|
|
531
|
+
async function runWorkspaceInspectionCommand(_input, command, options) {
|
|
532
|
+
const maxOutputLength = options.maxOutputLength || 3e4;
|
|
533
|
+
const timeout = options.timeout || 3e4;
|
|
534
|
+
const provider = options.networkGrants && usesWorkspaceNetworkGrant(command) ? void 0 : options.provider;
|
|
535
|
+
const unsupportedSyntax = preflightUnsupportedWorkspaceSyntax(command, options.commands, Boolean(provider));
|
|
536
|
+
if (unsupportedSyntax) return unsupportedSyntax;
|
|
537
|
+
const preflight = await preflightWorkspaceInspectionCommand(command, options.fs, options.broadSearchPaths, options.cwd);
|
|
538
|
+
if (preflight) return preflight;
|
|
539
|
+
const pipedBroadSearch = preflightPipedSearchWithoutPath(command, options.broadSearchPaths);
|
|
540
|
+
if (pipedBroadSearch) return pipedBroadSearch;
|
|
541
|
+
const missingPath = await preflightMissingWorkspacePath(command, options.fs, options.cwd);
|
|
542
|
+
if (missingPath) return missingPath;
|
|
543
|
+
const unsupported = preflightUnsupportedWorkspaceCommand(command, options.commands, Boolean(provider));
|
|
544
|
+
if (unsupported) return unsupported;
|
|
545
|
+
const runtime = createShellRuntime({
|
|
546
|
+
policy: {
|
|
547
|
+
maxOutputLength,
|
|
548
|
+
timeout
|
|
549
|
+
},
|
|
550
|
+
provider: provider ?? createJustBashProvider({
|
|
551
|
+
commands: options.commands,
|
|
552
|
+
cwd: options.cwd || "/workspace",
|
|
553
|
+
fs: options.fs,
|
|
554
|
+
networkGrants: options.networkGrants
|
|
555
|
+
})
|
|
556
|
+
});
|
|
557
|
+
const cwd = options.cwd || "/workspace";
|
|
558
|
+
const result = await runtime.exec(command, {
|
|
559
|
+
cwd,
|
|
560
|
+
timeout,
|
|
561
|
+
workspacePaths: workspaceSessionPaths(command, cwd)
|
|
562
|
+
});
|
|
563
|
+
const noMatchFeedback = searchNoMatchFeedback(command, result, options.broadSearchPaths, options.cwd);
|
|
564
|
+
return noMatchFeedback ? {
|
|
565
|
+
...result,
|
|
566
|
+
stdout: noMatchFeedback,
|
|
567
|
+
workspaceGuardrail: { kind: "no_match" }
|
|
568
|
+
} : result;
|
|
569
|
+
}
|
|
570
|
+
function workspaceSessionPaths(command, cwd = workspaceMountPoint) {
|
|
571
|
+
try {
|
|
572
|
+
const paths = /* @__PURE__ */ new Set();
|
|
573
|
+
let currentCwd = cwd;
|
|
574
|
+
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
575
|
+
const words = segment.words;
|
|
576
|
+
if (words[0] === "cd") {
|
|
577
|
+
const path = words[1] || "/workspace";
|
|
578
|
+
if (isWorkspacePathCandidate(path)) currentCwd = resolvedWorkspaceCwd(currentCwd, path);
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
for (const path of segment.paths) {
|
|
582
|
+
const resolvedPath = resolveConcreteWorkspacePath(currentCwd, path);
|
|
583
|
+
if (resolvedPath) paths.add(resolvedPath);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
return paths.size ? [...paths].sort() : void 0;
|
|
587
|
+
} catch {
|
|
588
|
+
return;
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
function preflightPipedSearchWithoutPath(command, broadSearchPaths = []) {
|
|
592
|
+
try {
|
|
593
|
+
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
594
|
+
if (segment.separatorAfter !== "|" || segment.paths.length) continue;
|
|
595
|
+
const name = segment.words[0];
|
|
596
|
+
if (name === "rg" || name === "grep") return broadWorkspaceSearchFeedback(broadSearchPaths);
|
|
597
|
+
}
|
|
598
|
+
} catch {
|
|
599
|
+
return;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
function usesWorkspaceNetworkGrant(command) {
|
|
603
|
+
try {
|
|
604
|
+
return analyzeWorkspaceInspectionCommand(command).some((segment) => workspaceExecutable(segment.words)?.name === "curl");
|
|
605
|
+
} catch {
|
|
606
|
+
return false;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
async function preflightWorkspaceInspectionCommand(command, fs, broadSearchPaths = [], cwd = workspaceMountPoint) {
|
|
610
|
+
try {
|
|
611
|
+
let currentCwd = cwd;
|
|
612
|
+
let skipAndChain = false;
|
|
613
|
+
let skipOrChain = false;
|
|
614
|
+
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
615
|
+
if (skipAndChain) {
|
|
616
|
+
skipAndChain = segment.separatorAfter === "&&";
|
|
617
|
+
continue;
|
|
618
|
+
}
|
|
619
|
+
if (skipOrChain) {
|
|
620
|
+
skipOrChain = segment.separatorAfter === "||";
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
const words = segment.words;
|
|
624
|
+
if (words[0] === "cd") {
|
|
625
|
+
const path = words[1] || "/workspace";
|
|
626
|
+
if (!isWorkspacePathCandidate(path)) continue;
|
|
627
|
+
const resolvedPath = resolveWorkspaceShellPath(currentCwd, path);
|
|
628
|
+
const directory = await workspacePathIsDirectory(fs, resolvedPath);
|
|
629
|
+
if (segment.separatorAfter === "&&" && !directory) skipAndChain = true;
|
|
630
|
+
if (segment.separatorAfter === "||" && directory) skipOrChain = true;
|
|
631
|
+
if (directory) currentCwd = resolvedPath ? posix.join(workspaceMountPoint, resolvedPath) : workspaceMountPoint;
|
|
632
|
+
continue;
|
|
633
|
+
}
|
|
634
|
+
if (isBroadWorkspaceSearch(segment, broadSearchPaths, currentCwd)) return broadWorkspaceSearchFeedback(broadSearchPaths);
|
|
635
|
+
}
|
|
636
|
+
} catch {
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
async function preflightMissingWorkspacePath(command, fs, cwd = workspaceMountPoint) {
|
|
641
|
+
try {
|
|
642
|
+
let currentCwd = cwd;
|
|
643
|
+
let skipAndChain = false;
|
|
644
|
+
let skipOrChain = false;
|
|
645
|
+
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
646
|
+
if (skipAndChain) {
|
|
647
|
+
skipAndChain = segment.separatorAfter === "&&";
|
|
648
|
+
continue;
|
|
649
|
+
}
|
|
650
|
+
if (skipOrChain) {
|
|
651
|
+
skipOrChain = segment.separatorAfter === "||";
|
|
652
|
+
continue;
|
|
653
|
+
}
|
|
654
|
+
const words = segment.words;
|
|
655
|
+
if (words[0] === "cd") {
|
|
656
|
+
const path = words[1] || "/workspace";
|
|
657
|
+
if (!isWorkspacePathCandidate(path)) continue;
|
|
658
|
+
const resolvedPath = resolveWorkspaceShellPath(currentCwd, path);
|
|
659
|
+
const directory = await workspacePathIsDirectory(fs, resolvedPath);
|
|
660
|
+
if (segment.separatorAfter === "&&" && !directory) skipAndChain = true;
|
|
661
|
+
if (segment.separatorAfter === "||" && directory) skipOrChain = true;
|
|
662
|
+
if (directory) currentCwd = resolvedPath ? posix.join(workspaceMountPoint, resolvedPath) : workspaceMountPoint;
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
for (const path of segment.paths) {
|
|
666
|
+
if (!isConcreteWorkspacePath(path)) continue;
|
|
667
|
+
const resolvedPath = resolveWorkspaceShellPath(currentCwd, path);
|
|
668
|
+
if (await fs.exists(resolvedPath)) continue;
|
|
669
|
+
if (segment.separatorAfter === "||") continue;
|
|
670
|
+
return missingWorkspacePathFeedback(command, resolvedPath);
|
|
671
|
+
}
|
|
672
|
+
if (segment.separatorAfter === "&&" || segment.separatorAfter === "||") return void 0;
|
|
673
|
+
}
|
|
674
|
+
} catch {
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
}
|
|
678
|
+
async function workspacePathIsDirectory(fs, path) {
|
|
679
|
+
if (path === "") return true;
|
|
680
|
+
try {
|
|
681
|
+
return (await fs.stat(path)).isDirectory;
|
|
682
|
+
} catch {
|
|
683
|
+
return false;
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
function missingWorkspacePathFeedback(command, resolvedPath) {
|
|
687
|
+
return {
|
|
688
|
+
command,
|
|
689
|
+
event: "command_finished",
|
|
690
|
+
exitCode: 0,
|
|
691
|
+
stderr: "",
|
|
692
|
+
stdout: [
|
|
693
|
+
`[vitehub] Workspace path is not mounted: ${resolvedPath}`,
|
|
694
|
+
"Configured Workspace Sources do not expose this path.",
|
|
695
|
+
"This usually means the Workspace Source configuration or materialized source selection does not include it."
|
|
696
|
+
].join("\n") + "\n",
|
|
697
|
+
workspaceGuardrail: {
|
|
698
|
+
kind: "missing_path",
|
|
699
|
+
path: resolvedPath
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
}
|
|
703
|
+
function isBroadWorkspaceSearch(segment, broadSearchPaths = [], cwd = workspaceMountPoint) {
|
|
704
|
+
const name = segment.words[0];
|
|
705
|
+
if (name === "rg" || name === "grep") {
|
|
706
|
+
const paths = segment.paths;
|
|
707
|
+
const pipeSafeStdinFilter = name === "grep" && segment.followsPipe && !segment.searchRecursive;
|
|
708
|
+
return paths.length === 0 && !pipeSafeStdinFilter || paths.length > 4 || paths.some((path) => isBroadWorkspacePath(path, broadSearchPaths, cwd));
|
|
709
|
+
}
|
|
710
|
+
if (name === "find") {
|
|
711
|
+
const paths = segment.paths;
|
|
712
|
+
return paths.length === 0 && isBroadWorkspacePath(".", broadSearchPaths, cwd) || paths.some((path) => isBroadWorkspacePath(path, broadSearchPaths, cwd));
|
|
713
|
+
}
|
|
714
|
+
return false;
|
|
715
|
+
}
|
|
716
|
+
function broadWorkspaceSearchFeedback(broadSearchPaths = []) {
|
|
717
|
+
const hint = broadSearchPaths.length ? ` Mounted search roots: ${broadSearchPaths.map((path) => `"${path}"`).join(", ")}.` : " Expected a mounted source or subdirectory path.";
|
|
718
|
+
const feedback = [
|
|
719
|
+
"[vitehub] Workspace search is too broad for this agent tool.",
|
|
720
|
+
"The command targets the workspace root or too many paths.",
|
|
721
|
+
"Workspace searches are bounded to mounted source subdirectories."
|
|
722
|
+
].join("\n") + "\n";
|
|
723
|
+
return {
|
|
724
|
+
command: "",
|
|
725
|
+
event: "policy_denied",
|
|
726
|
+
exitCode: 126,
|
|
727
|
+
stderr: `[vitehub] Workspace root search is too broad.${hint}\n`,
|
|
728
|
+
stdout: feedback,
|
|
729
|
+
workspaceGuardrail: { kind: "broad_search" }
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
function isBroadWorkspacePath(path, broadSearchPaths = [], cwd = workspaceMountPoint) {
|
|
733
|
+
const normalized = cleanWorkspaceShellPath(resolvedWorkspaceCwd(cwd, path)) || ".";
|
|
734
|
+
return normalized === "." || broadSearchPaths.includes(normalized);
|
|
735
|
+
}
|
|
736
|
+
function searchNoMatchFeedback(command, result, broadSearchPaths = [], cwd = workspaceMountPoint) {
|
|
737
|
+
if (result.exitCode !== 1 || result.stderr || result.stdout) return "";
|
|
738
|
+
try {
|
|
739
|
+
let currentCwd = cwd;
|
|
740
|
+
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
741
|
+
const words = segment.words;
|
|
742
|
+
if (words[0] === "cd") {
|
|
743
|
+
const path = words[1] || "/workspace";
|
|
744
|
+
if (isWorkspacePathCandidate(path)) currentCwd = resolvedWorkspaceCwd(currentCwd, path);
|
|
745
|
+
continue;
|
|
746
|
+
}
|
|
747
|
+
if (words[0] !== "rg" && words[0] !== "grep") {
|
|
748
|
+
if (segment.separatorAfter === "&&") return "";
|
|
749
|
+
continue;
|
|
750
|
+
}
|
|
751
|
+
if (!segment.paths.some((path) => isBroadWorkspacePath(path, broadSearchPaths, currentCwd))) {
|
|
752
|
+
if (segment.separatorAfter === "&&") return "";
|
|
753
|
+
continue;
|
|
754
|
+
}
|
|
755
|
+
return ["[vitehub] Search returned no matches in the mounted workspace source.", "Expected content may be outside the mounted workspace paths or under a different mounted subdirectory."].join("\n") + "\n";
|
|
756
|
+
}
|
|
757
|
+
} catch {
|
|
758
|
+
return "";
|
|
759
|
+
}
|
|
760
|
+
return "";
|
|
761
|
+
}
|
|
762
|
+
function preflightUnsupportedWorkspaceCommand(command, commands, strictProvider = false) {
|
|
763
|
+
const syntax = preflightUnsupportedWorkspaceSyntax(command, commands, strictProvider);
|
|
764
|
+
if (syntax) return syntax;
|
|
765
|
+
if (!strictProvider && usesCompoundShellSyntax(command)) return void 0;
|
|
766
|
+
if (!commands) return void 0;
|
|
767
|
+
const allowed = new Set([
|
|
768
|
+
...commands,
|
|
769
|
+
"cd",
|
|
770
|
+
"false",
|
|
771
|
+
"true"
|
|
772
|
+
]);
|
|
773
|
+
for (const segment of analyzeWorkspaceInspectionCommand(command)) {
|
|
774
|
+
const executable = workspaceExecutable(segment.words);
|
|
775
|
+
if (!executable || allowed.has(executable.name)) continue;
|
|
776
|
+
return unsupportedWorkspaceCommandFeedback(command, executable.name, commands);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
function preflightUnsupportedWorkspaceSyntax(command, commands, strictProvider = false) {
|
|
780
|
+
if (usesShellSubstitution(command)) {
|
|
781
|
+
if (strictProvider || commands) return unsupportedWorkspaceCommandFeedback(command, "shell substitution", commands);
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
if (usesCompoundShellSyntax(command)) {
|
|
785
|
+
if (strictProvider) return unsupportedWorkspaceCommandFeedback(command, "compound shell syntax", commands);
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
function unsupportedWorkspaceCommandFeedback(command, name, commands = []) {
|
|
790
|
+
const available = [...new Set(commands)].sort();
|
|
791
|
+
const formatted = available.length ? available.map((command) => `\`${command}\``).join(", ") : "none";
|
|
792
|
+
return {
|
|
793
|
+
command,
|
|
794
|
+
event: "policy_denied",
|
|
795
|
+
exitCode: 126,
|
|
796
|
+
stderr: `[vitehub] Unsupported workspace shell command: ${name}. Available commands: ${available.join(", ") || "none"}.\n`,
|
|
797
|
+
stdout: [
|
|
798
|
+
`[vitehub] Workspace shell command is not available: ${name}`,
|
|
799
|
+
`Available workspace commands: ${formatted}.`,
|
|
800
|
+
"This shell only supports configured Workspace inspection commands."
|
|
801
|
+
].join("\n") + "\n"
|
|
802
|
+
};
|
|
803
|
+
}
|
|
804
|
+
function usesShellSubstitution(command) {
|
|
805
|
+
let quote;
|
|
806
|
+
let escaped = false;
|
|
807
|
+
for (let index = 0; index < command.length; index++) {
|
|
808
|
+
const char = command[index];
|
|
809
|
+
const next = command[index + 1];
|
|
810
|
+
if (escaped) {
|
|
811
|
+
escaped = false;
|
|
812
|
+
continue;
|
|
813
|
+
}
|
|
814
|
+
if (char === "\\") {
|
|
815
|
+
escaped = true;
|
|
816
|
+
continue;
|
|
817
|
+
}
|
|
818
|
+
if (quote === "'") {
|
|
819
|
+
if (char === "'") quote = void 0;
|
|
820
|
+
continue;
|
|
821
|
+
}
|
|
822
|
+
if (quote === "\"") {
|
|
823
|
+
if (char === "\"") quote = void 0;
|
|
824
|
+
else if (char === "`" || char === "$" && next === "(" || (char === "<" || char === ">") && next === "(") return true;
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
if (char === "'" || char === "\"") {
|
|
828
|
+
quote = char;
|
|
829
|
+
continue;
|
|
830
|
+
}
|
|
831
|
+
if (char === "`" || char === "$" && next === "(" || (char === "<" || char === ">") && next === "(") return true;
|
|
832
|
+
}
|
|
833
|
+
return false;
|
|
834
|
+
}
|
|
835
|
+
function workspaceExecutable(words) {
|
|
836
|
+
for (const [index, word] of words.entries()) {
|
|
837
|
+
if (isAssignmentWord(word)) continue;
|
|
838
|
+
return {
|
|
839
|
+
index,
|
|
840
|
+
name: word
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
function usesCompoundShellSyntax(command) {
|
|
845
|
+
return analyzeWorkspaceInspectionCommand(command).some((segment) => isCompoundShellSyntaxWord(segment.words[0] || ""));
|
|
846
|
+
}
|
|
847
|
+
function isCompoundShellSyntaxWord(word) {
|
|
848
|
+
return word === "{" || word.startsWith("(") || /^[A-Za-z_][A-Za-z0-9_]*\(\)$/.test(word) || shellSyntaxWords.has(word);
|
|
849
|
+
}
|
|
850
|
+
function isAssignmentWord(word) {
|
|
851
|
+
return /^[A-Za-z_][A-Za-z0-9_]*=.*/.test(word);
|
|
852
|
+
}
|
|
853
|
+
const shellSyntaxWords = new Set([
|
|
854
|
+
"case",
|
|
855
|
+
"do",
|
|
856
|
+
"done",
|
|
857
|
+
"elif",
|
|
858
|
+
"else",
|
|
859
|
+
"esac",
|
|
860
|
+
"fi",
|
|
861
|
+
"for",
|
|
862
|
+
"function",
|
|
863
|
+
"if",
|
|
864
|
+
"select",
|
|
865
|
+
"then",
|
|
866
|
+
"time",
|
|
867
|
+
"until",
|
|
868
|
+
"while"
|
|
869
|
+
]);
|
|
870
|
+
function isConcreteWorkspacePath(path) {
|
|
871
|
+
return isWorkspacePathCandidate(path) && cleanWorkspaceShellPath(path) !== "";
|
|
872
|
+
}
|
|
873
|
+
function resolveConcreteWorkspacePath(cwd, path) {
|
|
874
|
+
if (!isWorkspacePathCandidate(path)) return void 0;
|
|
875
|
+
try {
|
|
876
|
+
const resolvedPath = resolveWorkspaceShellPath(cwd, path);
|
|
877
|
+
return resolvedPath === "" ? void 0 : resolvedPath;
|
|
878
|
+
} catch {
|
|
879
|
+
return;
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
function isWorkspacePathCandidate(path) {
|
|
883
|
+
return Boolean(path) && path !== "-" && !path.includes("$") && !path.includes("`") && !path.includes("*") && !path.includes("?") && !path.includes("[");
|
|
884
|
+
}
|
|
885
|
+
function resolveWorkspaceShellPath(cwd, path) {
|
|
886
|
+
return cleanWorkspaceShellPath(resolvedWorkspaceCwd(cwd, path));
|
|
887
|
+
}
|
|
888
|
+
function resolvedWorkspaceCwd(cwd, path) {
|
|
889
|
+
if (path.startsWith("/") && !path.startsWith("/workspace")) return path;
|
|
890
|
+
if (path.startsWith("/workspace")) return path;
|
|
891
|
+
const normalizedCwd = cleanWorkspaceShellPath(cwd);
|
|
892
|
+
const resolvedPath = normalizedCwd ? posix.join(normalizedCwd, path) : path;
|
|
893
|
+
return posix.join(workspaceMountPoint, resolvedPath);
|
|
894
|
+
}
|
|
895
|
+
//#endregion
|
|
896
|
+
export { cleanWorkspaceMutationPath, cleanWorkspaceShellPath, createReadonlyWorkspaceFs, createWritableWorkspaceFs, runWorkspaceInspectionCommand, workspaceMountPoint };
|