refactor: share codex app-server client factory

This commit is contained in:
Peter Steinberger
2026-04-20 16:57:34 +01:00
parent f3bc22d577
commit f0ef3070fa
4 changed files with 48 additions and 39 deletions

View File

@@ -0,0 +1,26 @@
import type { CodexAppServerClient } from "./client.js";
import type { CodexAppServerStartOptions } from "./config.js";
import { getSharedCodexAppServerClient } from "./shared-client.js";
export type CodexAppServerClientFactory = (
startOptions?: CodexAppServerStartOptions,
authProfileId?: string,
) => Promise<CodexAppServerClient>;
export const defaultCodexAppServerClientFactory: CodexAppServerClientFactory = (
startOptions,
authProfileId,
) => getSharedCodexAppServerClient({ startOptions, authProfileId });
export function createCodexAppServerClientFactoryTestHooks(
setFactory: (factory: CodexAppServerClientFactory) => void,
) {
return {
setCodexAppServerClientFactoryForTests(factory: CodexAppServerClientFactory): void {
setFactory(factory);
},
resetCodexAppServerClientFactoryForTests(): void {
setFactory(defaultCodexAppServerClientFactory);
},
} as const;
}

View File

@@ -3,16 +3,14 @@ import {
type CompactEmbeddedPiSessionParams,
type EmbeddedPiCompactResult,
} from "openclaw/plugin-sdk/agent-harness";
import {
createCodexAppServerClientFactoryTestHooks,
defaultCodexAppServerClientFactory,
} from "./client-factory.js";
import type { CodexAppServerClient, CodexServerNotificationHandler } from "./client.js";
import { resolveCodexAppServerRuntimeOptions, type CodexAppServerStartOptions } from "./config.js";
import { resolveCodexAppServerRuntimeOptions } from "./config.js";
import { isJsonObject, type CodexServerNotification, type JsonObject } from "./protocol.js";
import { readCodexAppServerBinding } from "./session-binding.js";
import { getSharedCodexAppServerClient } from "./shared-client.js";
type CodexAppServerClientFactory = (
startOptions?: CodexAppServerStartOptions,
authProfileId?: string,
) => Promise<CodexAppServerClient>;
type CodexNativeCompactionCompletion = {
signal: "thread/compacted" | "item/completed";
turnId?: string;
@@ -26,8 +24,7 @@ type CodexNativeCompactionWaiter = {
const DEFAULT_CODEX_COMPACTION_WAIT_TIMEOUT_MS = 5 * 60 * 1000;
let clientFactory: CodexAppServerClientFactory = (startOptions, authProfileId) =>
getSharedCodexAppServerClient({ startOptions, authProfileId });
let clientFactory = defaultCodexAppServerClientFactory;
export async function maybeCompactCodexAppServerSession(
params: CompactEmbeddedPiSessionParams,
@@ -219,12 +216,6 @@ function formatCompactionError(error: unknown): string {
return String(error);
}
export const __testing = {
setCodexAppServerClientFactoryForTests(factory: CodexAppServerClientFactory): void {
clientFactory = factory;
},
resetCodexAppServerClientFactoryForTests(): void {
clientFactory = (startOptions, authProfileId) =>
getSharedCodexAppServerClient({ startOptions, authProfileId });
},
} as const;
export const __testing = createCodexAppServerClientFactoryTestHooks((factory) => {
clientFactory = factory;
});

View File

@@ -18,8 +18,12 @@ import {
type EmbeddedRunAttemptResult,
} from "openclaw/plugin-sdk/agent-harness";
import { handleCodexAppServerApprovalRequest } from "./approval-bridge.js";
import {
createCodexAppServerClientFactoryTestHooks,
defaultCodexAppServerClientFactory,
} from "./client-factory.js";
import { isCodexAppServerApprovalRequest, type CodexAppServerClient } from "./client.js";
import { resolveCodexAppServerRuntimeOptions, type CodexAppServerStartOptions } from "./config.js";
import { resolveCodexAppServerRuntimeOptions } from "./config.js";
import { createCodexDynamicToolBridge } from "./dynamic-tools.js";
import { CodexAppServerEventProjector } from "./event-projector.js";
import {
@@ -31,17 +35,11 @@ import {
type JsonValue,
} from "./protocol.js";
import { readCodexAppServerBinding, type CodexAppServerThreadBinding } from "./session-binding.js";
import { clearSharedCodexAppServerClient, getSharedCodexAppServerClient } from "./shared-client.js";
import { clearSharedCodexAppServerClient } from "./shared-client.js";
import { buildTurnStartParams, startOrResumeThread } from "./thread-lifecycle.js";
import { mirrorCodexAppServerTranscript } from "./transcript-mirror.js";
type CodexAppServerClientFactory = (
startOptions?: CodexAppServerStartOptions,
authProfileId?: string,
) => Promise<CodexAppServerClient>;
let clientFactory: CodexAppServerClientFactory = (startOptions, authProfileId) =>
getSharedCodexAppServerClient({ startOptions, authProfileId });
let clientFactory = defaultCodexAppServerClientFactory;
export async function runCodexAppServerAttempt(
params: EmbeddedRunAttemptParams,
@@ -487,12 +485,6 @@ function handleApprovalRequest(params: {
});
}
export const __testing = {
setCodexAppServerClientFactoryForTests(factory: CodexAppServerClientFactory): void {
clientFactory = factory;
},
resetCodexAppServerClientFactoryForTests(): void {
clientFactory = (startOptions, authProfileId) =>
getSharedCodexAppServerClient({ startOptions, authProfileId });
},
} as const;
export const __testing = createCodexAppServerClientFactoryTestHooks((factory) => {
clientFactory = factory;
});

View File

@@ -61,11 +61,11 @@ describe("Codex app-server websocket transport", () => {
});
function rawDataToText(data: RawData): string {
if (Buffer.isBuffer(data)) {
return data.toString("utf8");
}
if (Array.isArray(data)) {
return Buffer.concat(data).toString("utf8");
}
if (data instanceof ArrayBuffer) {
return Buffer.from(new Uint8Array(data)).toString("utf8");
}
return Buffer.from(data).toString("utf8");
}