Skip to content

Commit 04cb6cd

Browse files
committed
Share quote-aware CLI arg tokenization
1 parent 9dbbf9e commit 04cb6cd

9 files changed

Lines changed: 143 additions & 129 deletions

File tree

‎apps/server/src/provider/Layers/CodexAdapter.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ import {
6161
type CodexSessionRuntimeShape,
6262
} from "./CodexSessionRuntime.ts";
6363
import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
64-
import { resolveCodexLaunchArgs } from "./CodexProvider.ts";
64+
import { resolveCodexLaunchArgs } from "./codexLaunchArgs.ts";
6565
const isCodexAppServerProcessExitedError = Schema.is(CodexErrors.CodexAppServerProcessExitedError);
6666
const isCodexAppServerTransportError = Schema.is(CodexErrors.CodexAppServerTransportError);
6767
const isCodexSessionRuntimeThreadIdMissingError = Schema.is(

‎apps/server/src/provider/Layers/CodexProvider.ts‎

Lines changed: 1 addition & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { ServerSettingsError } from "@t3tools/contracts";
2626

2727
import { createModelCapabilities } from "@t3tools/shared/model";
2828
import { resolveSpawnCommand } from "@t3tools/shared/shell";
29+
import { codexAppServerArgs, resolveCodexLaunchArgs } from "./codexLaunchArgs.ts";
2930
import {
3031
AUTH_PROBE_TIMEOUT_MS,
3132
buildServerProvider,
@@ -286,99 +287,6 @@ export function buildCodexInitializeParams(): CodexSchema.V1InitializeParams {
286287
};
287288
}
288289

289-
export const T3CODE_CODEX_LAUNCH_ARGS_ENV = "T3CODE_CODEX_LAUNCH_ARGS";
290-
291-
export const resolveCodexLaunchArgs = (
292-
launchArgs?: string,
293-
environment: NodeJS.ProcessEnv = process.env,
294-
) => environment[T3CODE_CODEX_LAUNCH_ARGS_ENV]?.trim() || launchArgs?.trim() || "";
295-
296-
export const codexLaunchArgv = (launchArgs?: string): ReadonlyArray<string> => {
297-
const input = launchArgs?.trim();
298-
if (!input) return [];
299-
300-
const args: string[] = [];
301-
let current = "";
302-
let quote: "'" | '"' | undefined;
303-
let quoted = false;
304-
305-
for (let index = 0; index < input.length; index++) {
306-
const char = input[index];
307-
if (char === undefined) continue;
308-
309-
if (quote) {
310-
if (char === quote) {
311-
quote = undefined;
312-
quoted = true;
313-
} else if (char === "\\" && quote === '"') {
314-
const next = input[index + 1];
315-
if (next !== undefined && ['"', "\\", "$", "`"].includes(next)) {
316-
current += next;
317-
index++;
318-
} else {
319-
current += char;
320-
}
321-
} else {
322-
current += char;
323-
}
324-
continue;
325-
}
326-
327-
if (char === "'" || char === '"') {
328-
quote = char;
329-
quoted = true;
330-
} else if (/\s/.test(char)) {
331-
if (current || quoted) {
332-
args.push(current);
333-
current = "";
334-
quoted = false;
335-
}
336-
} else if (char === "\\") {
337-
const next = input[index + 1];
338-
if (next !== undefined && /\s/.test(next)) {
339-
current += next;
340-
index++;
341-
} else {
342-
current += char;
343-
}
344-
} else {
345-
current += char;
346-
}
347-
}
348-
349-
if (current || quoted) args.push(current);
350-
return args;
351-
};
352-
353-
export const codexAppServerArgs = (launchArgs?: string) => [
354-
"app-server",
355-
...codexLaunchArgv(launchArgs),
356-
];
357-
358-
export const codexExecLaunchArgs = (launchArgs?: string) => {
359-
const args = codexLaunchArgv(launchArgs);
360-
const execArgs: Array<string> = [];
361-
for (let index = 0; index < args.length; index++) {
362-
const arg = args[index];
363-
if (arg === undefined) continue;
364-
if (arg === "--strict-config" || arg.startsWith("--config=") || arg.startsWith("-c=")) {
365-
execArgs.push(arg);
366-
} else if (arg === "--config" || arg === "-c" || arg === "--enable" || arg === "--disable") {
367-
execArgs.push(arg);
368-
const value = args[index + 1];
369-
if (value !== undefined && !value.startsWith("-")) {
370-
execArgs.push(value);
371-
index++;
372-
} else {
373-
execArgs.pop();
374-
}
375-
} else if (arg.startsWith("--enable=") || arg.startsWith("--disable=")) {
376-
execArgs.push(arg);
377-
}
378-
}
379-
return execArgs;
380-
};
381-
382290
const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(function* (input: {
383291
readonly binaryPath: string;
384292
readonly homePath?: string;

‎apps/server/src/provider/Layers/CodexSessionRuntime.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {
1212
CODEX_DEFAULT_MODE_DEVELOPER_INSTRUCTIONS,
1313
CODEX_PLAN_MODE_DEVELOPER_INSTRUCTIONS,
1414
} from "../CodexDeveloperInstructions.ts";
15+
import { codexSessionAppServerArgs } from "./codexLaunchArgs.ts";
1516
import {
1617
buildTurnStartParams,
17-
codexSessionAppServerArgs,
1818
hasConfiguredMcpServer,
1919
isRecoverableThreadResumeError,
2020
openCodexThread,

‎apps/server/src/provider/Layers/CodexSessionRuntime.ts‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ import * as CodexErrors from "effect-codex-app-server/errors";
3535
import * as CodexRpc from "effect-codex-app-server/rpc";
3636
import * as EffectCodexSchema from "effect-codex-app-server/schema";
3737

38-
import { buildCodexInitializeParams, codexAppServerArgs } from "./CodexProvider.ts";
38+
import { buildCodexInitializeParams } from "./CodexProvider.ts";
39+
import { codexSessionAppServerArgs } from "./codexLaunchArgs.ts";
3940
import { expandHomePath } from "../../pathExpansion.ts";
4041
import {
4142
CODEX_DEFAULT_MODE_DEVELOPER_INSTRUCTIONS,
@@ -66,14 +67,6 @@ export function hasConfiguredMcpServer(appServerArgs: ReadonlyArray<string> | un
6667
return appServerArgs?.some((argument) => argument.includes("mcp_servers.")) === true;
6768
}
6869

69-
export const codexSessionAppServerArgs = (
70-
appServerArgs: ReadonlyArray<string> | undefined,
71-
launchArgs: string | undefined,
72-
) => {
73-
const launchAppServerArgs = codexAppServerArgs(launchArgs);
74-
return appServerArgs ? [...launchAppServerArgs, ...appServerArgs] : launchAppServerArgs;
75-
};
76-
7770
export const CodexResumeCursorSchema = Schema.Struct({
7871
threadId: Schema.String,
7972
});

‎apps/server/src/provider/Layers/codexLaunchArgs.test.ts‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import { describe, it } from "vite-plus/test";
55
import {
66
codexAppServerArgs,
77
codexExecLaunchArgs,
8-
codexLaunchArgv,
98
resolveCodexLaunchArgs,
10-
} from "./CodexProvider.ts";
9+
} from "./codexLaunchArgs.ts";
1110

1211
describe("resolveCodexLaunchArgs", () => {
1312
it("uses T3CODE_CODEX_LAUNCH_ARGS before configured settings", () => {
@@ -29,24 +28,6 @@ describe("resolveCodexLaunchArgs", () => {
2928
});
3029
});
3130

32-
describe("codexLaunchArgv", () => {
33-
it("preserves quoted values and escaped spaces", () => {
34-
NodeAssert.deepStrictEqual(
35-
codexLaunchArgv(
36-
String.raw`--config model="gpt 5" --enable foo\ bar --config=profile='work profile'`,
37-
),
38-
["--config", "model=gpt 5", "--enable", "foo bar", "--config=profile=work profile"],
39-
);
40-
});
41-
42-
it("preserves literal backslashes in path values", () => {
43-
NodeAssert.deepStrictEqual(
44-
codexLaunchArgv(String.raw`--config cacheDir=C:\Users\me --config "quoted=C:\Users\me"`),
45-
["--config", String.raw`cacheDir=C:\Users\me`, "--config", String.raw`quoted=C:\Users\me`],
46-
);
47-
});
48-
});
49-
5031
describe("codexAppServerArgs", () => {
5132
it("returns the app-server command for empty launch args", () => {
5233
NodeAssert.deepStrictEqual(codexAppServerArgs(""), ["app-server"]);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { tokenizeCliArgs } from "@t3tools/shared/cliArgs";
2+
3+
export const T3CODE_CODEX_LAUNCH_ARGS_ENV = "T3CODE_CODEX_LAUNCH_ARGS";
4+
5+
export const resolveCodexLaunchArgs = (
6+
launchArgs?: string,
7+
environment: NodeJS.ProcessEnv = process.env,
8+
) => environment[T3CODE_CODEX_LAUNCH_ARGS_ENV]?.trim() || launchArgs?.trim() || "";
9+
10+
export const codexLaunchArgv = (launchArgs?: string): ReadonlyArray<string> =>
11+
tokenizeCliArgs(launchArgs);
12+
13+
export const codexAppServerArgs = (launchArgs?: string) => [
14+
"app-server",
15+
...codexLaunchArgv(launchArgs),
16+
];
17+
18+
export const codexExecLaunchArgs = (launchArgs?: string) => {
19+
const args = codexLaunchArgv(launchArgs);
20+
const execArgs: Array<string> = [];
21+
22+
for (let index = 0; index < args.length; index++) {
23+
const arg = args[index];
24+
if (arg === undefined) continue;
25+
26+
if (arg === "--strict-config" || arg.startsWith("--config=") || arg.startsWith("-c=")) {
27+
execArgs.push(arg);
28+
} else if (arg === "--config" || arg === "-c" || arg === "--enable" || arg === "--disable") {
29+
const value = args[index + 1];
30+
if (value !== undefined && !value.startsWith("-")) {
31+
execArgs.push(arg, value);
32+
index++;
33+
}
34+
} else if (arg.startsWith("--enable=") || arg.startsWith("--disable=")) {
35+
execArgs.push(arg);
36+
}
37+
}
38+
39+
return execArgs;
40+
};
41+
42+
export const codexSessionAppServerArgs = (
43+
appServerArgs: ReadonlyArray<string> | undefined,
44+
launchArgs: string | undefined,
45+
) => {
46+
const launchAppServerArgs = codexAppServerArgs(launchArgs);
47+
return appServerArgs ? [...launchAppServerArgs, ...appServerArgs] : launchAppServerArgs;
48+
};

‎apps/server/src/textGeneration/CodexTextGeneration.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { resolveSpawnCommand } from "@t3tools/shared/shell";
1414
import { resolveAttachmentPath } from "../attachmentStore.ts";
1515
import * as ServerConfig from "../config.ts";
1616
import { expandHomePath } from "../pathExpansion.ts";
17-
import { codexExecLaunchArgs, resolveCodexLaunchArgs } from "../provider/Layers/CodexProvider.ts";
17+
import { codexExecLaunchArgs, resolveCodexLaunchArgs } from "../provider/Layers/codexLaunchArgs.ts";
1818
import { TextGenerationError } from "@t3tools/contracts";
1919
import * as TextGeneration from "./TextGeneration.ts";
2020
import {

‎packages/shared/src/cliArgs.test.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
11
import { describe, expect, it } from "vite-plus/test";
22

3-
import { parseCliArgs } from "./cliArgs.ts";
3+
import { parseCliArgs, tokenizeCliArgs } from "./cliArgs.ts";
4+
5+
describe("tokenizeCliArgs", () => {
6+
it("preserves quoted values and escaped spaces", () => {
7+
expect(
8+
tokenizeCliArgs(
9+
String.raw`--config model="gpt 5" --enable foo\ bar --config=profile='work profile'`,
10+
),
11+
).toEqual(["--config", "model=gpt 5", "--enable", "foo bar", "--config=profile=work profile"]);
12+
});
13+
14+
it("preserves literal backslashes in path values", () => {
15+
expect(
16+
tokenizeCliArgs(String.raw`--config cacheDir=C:\Users\me --config "quoted=C:\Users\me"`),
17+
).toEqual([
18+
"--config",
19+
String.raw`cacheDir=C:\Users\me`,
20+
"--config",
21+
String.raw`quoted=C:\Users\me`,
22+
]);
23+
});
24+
});
425

526
describe("parseCliArgs", () => {
627
it("returns empty result for empty string", () => {
@@ -57,6 +78,13 @@ describe("parseCliArgs", () => {
5778
});
5879
});
5980

81+
it("parses quoted --append-system-prompt with value and --chrome", () => {
82+
expect(parseCliArgs(`--append-system-prompt "always think step by step" --chrome`)).toEqual({
83+
flags: { "append-system-prompt": "always think step by step", chrome: null },
84+
positionals: [],
85+
});
86+
});
87+
6088
it("parses --max-budget-usd with numeric value", () => {
6189
expect(parseCliArgs("--chrome --max-budget-usd 5.00")).toEqual({
6290
flags: { chrome: null, "max-budget-usd": "5.00" },

‎packages/shared/src/cliArgs.ts‎

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,67 @@ export interface ParseCliArgsOptions {
77
readonly booleanFlags?: readonly string[];
88
}
99

10+
export function tokenizeCliArgs(args?: string): ReadonlyArray<string> {
11+
const input = args?.trim();
12+
if (!input) return [];
13+
14+
const tokens: string[] = [];
15+
let current = "";
16+
let quote: "'" | '"' | undefined;
17+
let quoted = false;
18+
19+
for (let index = 0; index < input.length; index++) {
20+
const char = input[index];
21+
if (char === undefined) continue;
22+
23+
if (quote) {
24+
if (char === quote) {
25+
quote = undefined;
26+
quoted = true;
27+
} else if (char === "\\" && quote === '"') {
28+
const next = input[index + 1];
29+
if (next !== undefined && ['"', "\\", "$", "`"].includes(next)) {
30+
current += next;
31+
index++;
32+
} else {
33+
current += char;
34+
}
35+
} else {
36+
current += char;
37+
}
38+
continue;
39+
}
40+
41+
if (char === "'" || char === '"') {
42+
quote = char;
43+
quoted = true;
44+
} else if (/\s/.test(char)) {
45+
if (current || quoted) {
46+
tokens.push(current);
47+
current = "";
48+
quoted = false;
49+
}
50+
} else if (char === "\\") {
51+
const next = input[index + 1];
52+
if (next !== undefined && /\s/.test(next)) {
53+
current += next;
54+
index++;
55+
} else {
56+
current += char;
57+
}
58+
} else {
59+
current += char;
60+
}
61+
}
62+
63+
if (current || quoted) tokens.push(current);
64+
return tokens;
65+
}
66+
1067
/**
1168
* Parse CLI-style arguments into flags and positionals.
1269
*
13-
* Accepts a string (split by whitespace) or a pre-split argv array.
70+
* Accepts a string (quote-aware tokenized) or a pre-split argv array.
1471
* Supports `--key value`, `--key=value`, and `--flag` (boolean) syntax.
1572
*
1673
* parseCliArgs("")
@@ -32,8 +89,7 @@ export function parseCliArgs(
3289
args: string | readonly string[],
3390
options?: ParseCliArgsOptions,
3491
): ParsedCliArgs {
35-
const tokens =
36-
typeof args === "string" ? args.trim().split(/\s+/).filter(Boolean) : Array.from(args);
92+
const tokens = typeof args === "string" ? tokenizeCliArgs(args) : Array.from(args);
3793
const booleanSet = options?.booleanFlags ? new Set(options.booleanFlags) : undefined;
3894

3995
const flags: Record<string, string | null> = {};

0 commit comments

Comments
 (0)