forked from openai/codex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat-command.ts
More file actions
53 lines (46 loc) · 1.89 KB
/
format-command.ts
File metadata and controls
53 lines (46 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { quote } from "shell-quote";
/**
* Format the args of an exec command for display as a single string. Prefer
* this to doing `args.join(" ")` as this will handle quoting and escaping
* correctly. See unit test for details.
*/
export function formatCommandForDisplay(command: Array<string>): string {
// The model often wraps arbitrary shell commands in an invocation that looks
// like:
//
// ["bash", "-lc", "'<actual command>'"]
//
// When displaying these back to the user, we do NOT want to show the
// boiler‑plate "bash -lc" wrapper. Instead, we want to surface only the
// actual command that bash will evaluate.
// Historically we detected this by first quoting the entire command array
// with `shell‑quote` and then using a regular expression to peel off the
// `bash -lc '…'` prefix. However, that approach was brittle (it depended on
// the exact quoting behavior of `shell-quote`) and unnecessarily
// inefficient.
// A simpler and more robust approach is to look at the raw command array
// itself. If it matches the shape produced by our exec helpers—exactly three
// entries where the first two are «bash» and «-lc»—then we can return the
// third entry directly (after stripping surrounding single quotes if they
// are present).
try {
if (
command.length === 3 &&
command[0] === "bash" &&
command[1] === "-lc" &&
typeof command[2] === "string"
) {
let inner = command[2];
// Some callers wrap the actual command in single quotes (e.g. `'echo foo'`).
// For display purposes we want to drop those outer quotes so that the
// rendered command looks exactly like what the user typed.
if (inner.startsWith("'") && inner.endsWith("'")) {
inner = inner.slice(1, -1);
}
return inner;
}
return quote(command);
} catch (err) {
return command.join(" ");
}
}