From 07870dff45ebd355fe352a77b7487cc1e2ca0b01 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Fri, 29 May 2026 21:19:46 +0200 Subject: [PATCH] refactor: share codex app server start context --- .../codex/src/app-server/shared-client.ts | 99 +++++++++---------- 1 file changed, 46 insertions(+), 53 deletions(-) diff --git a/extensions/codex/src/app-server/shared-client.ts b/extensions/codex/src/app-server/shared-client.ts index 85a8c2f4788..83bacfc4199 100644 --- a/extensions/codex/src/app-server/shared-client.ts +++ b/extensions/codex/src/app-server/shared-client.ts @@ -96,7 +96,7 @@ function readLegacySharedCodexAppServerClientState( return value as LegacySharedCodexAppServerClientState; } -type SharedCodexAppServerClientOptions = { +type CodexAppServerClientOptions = { startOptions?: CodexAppServerStartOptions; timeoutMs?: number; authProfileId?: string | null; @@ -104,14 +104,47 @@ type SharedCodexAppServerClientOptions = { config?: Parameters[0]["config"]; }; +type ResolvedCodexAppServerClientStartContext = { + agentDir: string; + usesNativeAuth: boolean; + authProfileId: string | undefined; + startOptions: CodexAppServerStartOptions; +}; + +async function resolveCodexAppServerClientStartContext( + options?: CodexAppServerClientOptions, +): Promise { + const agentDir = options?.agentDir ?? resolveDefaultAgentDir(options?.config ?? {}); + const usesNativeAuth = options?.authProfileId === null; + const requestedAuthProfileId = + options?.authProfileId === null ? undefined : options?.authProfileId; + const authProfileId = usesNativeAuth + ? undefined + : resolveCodexAppServerAuthProfileIdForAgent({ + authProfileId: requestedAuthProfileId, + agentDir, + config: options?.config, + }); + const requestedStartOptions = + options?.startOptions ?? resolveCodexAppServerRuntimeOptions().start; + const managedStartOptions = await resolveManagedCodexAppServerStartOptions(requestedStartOptions); + const startOptions = await bridgeCodexAppServerStartOptions({ + startOptions: managedStartOptions, + agentDir, + authProfileId: usesNativeAuth ? null : authProfileId, + config: options?.config, + }); + return { agentDir, usesNativeAuth, authProfileId, startOptions }; +} + export async function getSharedCodexAppServerClient( - options?: SharedCodexAppServerClientOptions, + options?: CodexAppServerClientOptions, ): Promise { return (await acquireSharedCodexAppServerClient(options)).client; } export async function getLeasedSharedCodexAppServerClient( - options?: SharedCodexAppServerClientOptions, + options?: CodexAppServerClientOptions, ): Promise { const acquired = await acquireSharedCodexAppServerClient(options, { leased: true }); const state = getSharedCodexAppServerClientState(); @@ -139,36 +172,18 @@ export function releaseLeasedSharedCodexAppServerClient(client: CodexAppServerCl } async function acquireSharedCodexAppServerClient( - options?: SharedCodexAppServerClientOptions, + options?: CodexAppServerClientOptions, ): Promise<{ client: CodexAppServerClient }>; async function acquireSharedCodexAppServerClient( - options: SharedCodexAppServerClientOptions | undefined, + options: CodexAppServerClientOptions | undefined, leaseOptions: { leased: true }, ): Promise<{ client: CodexAppServerClient; release: () => void }>; async function acquireSharedCodexAppServerClient( - options?: SharedCodexAppServerClientOptions, + options?: CodexAppServerClientOptions, leaseOptions?: { leased: true }, ): Promise<{ client: CodexAppServerClient; release?: () => void }> { - const agentDir = options?.agentDir ?? resolveDefaultAgentDir(options?.config ?? {}); - const usesNativeAuth = options?.authProfileId === null; - const requestedAuthProfileId = - options?.authProfileId === null ? undefined : options?.authProfileId; - const authProfileId = usesNativeAuth - ? undefined - : resolveCodexAppServerAuthProfileIdForAgent({ - authProfileId: requestedAuthProfileId, - agentDir, - config: options?.config, - }); - const requestedStartOptions = - options?.startOptions ?? resolveCodexAppServerRuntimeOptions().start; - const managedStartOptions = await resolveManagedCodexAppServerStartOptions(requestedStartOptions); - const startOptions = await bridgeCodexAppServerStartOptions({ - startOptions: managedStartOptions, - agentDir, - authProfileId: usesNativeAuth ? null : authProfileId, - config: options?.config, - }); + const { agentDir, usesNativeAuth, authProfileId, startOptions } = + await resolveCodexAppServerClientStartContext(options); const fallbackApiKeyCacheKey = authProfileId ? undefined : resolveCodexAppServerFallbackApiKeyCacheKey({ startOptions }); @@ -219,33 +234,11 @@ async function acquireSharedCodexAppServerClient( } } -export async function createIsolatedCodexAppServerClient(options?: { - startOptions?: CodexAppServerStartOptions; - timeoutMs?: number; - authProfileId?: string | null; - agentDir?: string; - config?: Parameters[0]["config"]; -}): Promise { - const agentDir = options?.agentDir ?? resolveDefaultAgentDir(options?.config ?? {}); - const usesNativeAuth = options?.authProfileId === null; - const requestedAuthProfileId = - options?.authProfileId === null ? undefined : options?.authProfileId; - const authProfileId = usesNativeAuth - ? undefined - : resolveCodexAppServerAuthProfileIdForAgent({ - authProfileId: requestedAuthProfileId, - agentDir, - config: options?.config, - }); - const requestedStartOptions = - options?.startOptions ?? resolveCodexAppServerRuntimeOptions().start; - const managedStartOptions = await resolveManagedCodexAppServerStartOptions(requestedStartOptions); - const startOptions = await bridgeCodexAppServerStartOptions({ - startOptions: managedStartOptions, - agentDir, - authProfileId: usesNativeAuth ? null : authProfileId, - config: options?.config, - }); +export async function createIsolatedCodexAppServerClient( + options?: CodexAppServerClientOptions, +): Promise { + const { agentDir, usesNativeAuth, authProfileId, startOptions } = + await resolveCodexAppServerClientStartContext(options); const client = CodexAppServerClient.start(startOptions); const initialize = client.initialize(); try {