Merge Code Mode flow support into main
All checks were successful
ci / check (push) Successful in 30s

This commit is contained in:
matamune 2026-05-13 03:17:06 +00:00
parent 0860aeb42b
commit 0d3c2e2ab6
Signed by: matamune
GPG key ID: 3BB8E7D3B968A324
24 changed files with 2630 additions and 18 deletions

View file

@ -10,6 +10,11 @@ import {
requireJsonRpcResult,
stringifyJsonRpc,
} from "./rpc.ts";
import {
CODEX_FLOWS_CODE_MODE,
DEFAULT_CODE_MODE_CODEX_PACKAGE,
codexFlowsMode,
} from "../mode.ts";
type PendingRequest = {
resolve: (value: JsonRpcResponse) => void;
@ -28,6 +33,14 @@ export type CodexStdioTransportOptions = {
requestTimeoutMs?: number;
};
export type ResolvedCodexStdioCommand = {
command: string;
args: string[];
};
export const DEFAULT_CODEX_COMMAND = "codex";
export const DEFAULT_CODEX_NPM_PACKAGE = DEFAULT_CODE_MODE_CODEX_PACKAGE;
export class CodexStdioTransport extends CodexEventEmitter {
readonly requestTimeoutMs: number;
#codexCommand: string;
@ -40,10 +53,9 @@ export class CodexStdioTransport extends CodexEventEmitter {
constructor(options: CodexStdioTransportOptions = {}) {
super();
this.#codexCommand = options.codexCommand ?? "codex";
const appServerSocket =
options.appServerSocket ?? process.env.CODEX_WORKSPACE_APP_SERVER_SOCK;
this.#args = options.args ?? defaultCodexArgs(appServerSocket);
const command = resolveCodexStdioCommand(options, { ...process.env, ...options.env });
this.#codexCommand = command.command;
this.#args = command.args;
this.#cwd = options.cwd;
this.#env = options.env;
this.requestTimeoutMs = options.requestTimeoutMs ?? 60_000;
@ -200,6 +212,28 @@ export class CodexStdioTransport extends CodexEventEmitter {
}
}
export function resolveCodexStdioCommand(
options: Pick<CodexStdioTransportOptions, "appServerSocket" | "args" | "codexCommand"> = {},
env: Record<string, string | undefined> = process.env,
): ResolvedCodexStdioCommand {
const explicitCommand = options.codexCommand ?? env.CODEX_APP_SERVER_CODEX_COMMAND;
const appServerSocket = options.appServerSocket ?? env.CODEX_WORKSPACE_APP_SERVER_SOCK;
const args = options.args ?? defaultCodexArgs(appServerSocket);
if (explicitCommand?.trim()) {
return { command: explicitCommand, args };
}
const packageName = env.CODEX_APP_SERVER_CODEX_PACKAGE?.trim();
if (packageName || codexFlowsMode(env) === CODEX_FLOWS_CODE_MODE) {
return {
command: env.CODEX_APP_SERVER_BUNX_COMMAND?.trim() || "bunx",
args: [packageName || DEFAULT_CODE_MODE_CODEX_PACKAGE, ...args],
};
}
return { command: DEFAULT_CODEX_COMMAND, args };
}
function killChildProcessGroup(
child: AppServerProcess,
signal: NodeJS.Signals,

View file

@ -5,6 +5,10 @@ export {
} from "./app-server/client.ts";
export {
CodexStdioTransport,
DEFAULT_CODEX_COMMAND,
DEFAULT_CODEX_NPM_PACKAGE,
resolveCodexStdioCommand,
type ResolvedCodexStdioCommand,
type CodexStdioTransportOptions,
} from "./app-server/stdio-transport.ts";
export {
@ -49,3 +53,9 @@ export type {
CodexUsageWindow,
WaitForLoginOptions,
} from "./app-server/auth.ts";
export {
CODEX_FLOWS_CODE_MODE,
DEFAULT_CODE_MODE_CODEX_PACKAGE,
codexFlowsCodeModeEnabled,
codexFlowsMode,
} from "./mode.ts";

View file

@ -0,0 +1,20 @@
export const CODEX_FLOWS_CODE_MODE = "code-mode";
export const DEFAULT_CODE_MODE_CODEX_PACKAGE = "@peezy.tech/codex";
export function codexFlowsMode(
env: Record<string, string | undefined> = process.env,
): string | undefined {
const value = env.CODEX_FLOWS_MODE?.trim().toLowerCase();
return value || undefined;
}
export function codexFlowsCodeModeEnabled(
env: Record<string, string | undefined> = process.env,
): boolean {
return booleanEnv(env.CODEX_FLOWS_ENABLE_CODE_MODE) || codexFlowsMode(env) === CODEX_FLOWS_CODE_MODE;
}
function booleanEnv(value: string | undefined): boolean {
const normalized = value?.trim().toLowerCase();
return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
}