Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cli/src/codex/codexAppServerClient.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { afterEach, describe, expect, it } from 'vitest';
import { resolveCodexAppServerCommand } from './codexAppServerClient';

const originalAppServerBin = process.env.HAPI_CODEX_APP_SERVER_BIN;
const originalCodexProfile = process.env.HAPI_CODEX_PROFILE;

afterEach(() => {
if (originalAppServerBin === undefined) {
delete process.env.HAPI_CODEX_APP_SERVER_BIN;
} else {
process.env.HAPI_CODEX_APP_SERVER_BIN = originalAppServerBin;
}

if (originalCodexProfile === undefined) {
delete process.env.HAPI_CODEX_PROFILE;
} else {
process.env.HAPI_CODEX_PROFILE = originalCodexProfile;
}
});

describe('resolveCodexAppServerCommand', () => {
it('starts app-server without a profile by default', () => {
process.env.HAPI_CODEX_APP_SERVER_BIN = '/tmp/codex';
delete process.env.HAPI_CODEX_PROFILE;

expect(resolveCodexAppServerCommand()).toEqual({
command: '/tmp/codex',
args: ['app-server']
});
});

it('starts app-server with the configured Codex profile', () => {
process.env.HAPI_CODEX_APP_SERVER_BIN = '/tmp/codex';
process.env.HAPI_CODEX_PROFILE = ' tapsvc ';

expect(resolveCodexAppServerCommand()).toEqual({
command: '/tmp/codex',
args: ['--profile', 'tapsvc', 'app-server']
});
});
});
24 changes: 19 additions & 5 deletions cli/src/codex/codexAppServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ type CodexCommandCandidate = {
version: number[] | null;
};

export type CodexAppServerCommand = {
command: string;
args: string[];
};

function parseCodexVersion(output: string): number[] | null {
const match = /(\d+)\.(\d+)\.(\d+)(?:[-+][^\s]+)?/u.exec(output);
if (!match) return null;
Expand Down Expand Up @@ -115,9 +120,15 @@ function compareVersion(a: number[] | null, b: number[] | null): number {
return 0;
}

function resolveCodexAppServerCommand(): string {
export function resolveCodexAppServerCommand(): CodexAppServerCommand {
const profile = process.env.HAPI_CODEX_PROFILE?.trim();
const args = profile ? ['--profile', profile, 'app-server'] : ['app-server'];

if (process.env.HAPI_CODEX_APP_SERVER_BIN) {
return process.env.HAPI_CODEX_APP_SERVER_BIN;
return {
command: process.env.HAPI_CODEX_APP_SERVER_BIN,
args
};
}

const candidates: CodexCommandCandidate[] = [{
Expand Down Expand Up @@ -154,7 +165,10 @@ function resolveCodexAppServerCommand(): string {
version: candidate.version?.join('.') ?? null
}))
});
return best.command;
return {
command: best.command,
args
};
}

export class CodexAppServerClient extends JsonLineParser {
Expand All @@ -179,8 +193,8 @@ export class CodexAppServerClient extends JsonLineParser {
}

const codexCommand = resolveCodexAppServerCommand();
logger.debug(`[CodexAppServer] Starting ${codexCommand} app-server`);
this.process = spawn(codexCommand, ['app-server'], {
logger.debug(`[CodexAppServer] Starting ${codexCommand.command} ${codexCommand.args.join(' ')}`);
this.process = spawn(codexCommand.command, codexCommand.args, {
env: Object.keys(process.env).reduce((acc, key) => {
const value = process.env[key];
if (typeof value === 'string') acc[key] = value;
Expand Down
Loading