refactor: optimize CodexProvider to use a shared SDK client instance

This commit is contained in:
Haileyesus
2026-04-06 22:58:10 +03:00
parent f576b8e6d2
commit 7cd429697b

View File

@@ -57,6 +57,8 @@ type CodexSdkModule = {
* Codex SDK provider implementation. * Codex SDK provider implementation.
*/ */
export class CodexProvider extends BaseSdkProvider { export class CodexProvider extends BaseSdkProvider {
private codexClientPromise: Promise<CodexSdkClient> | null = null;
constructor() { constructor() {
super('codex', { super('codex', {
supportsRuntimePermissionRequests: false, supportsRuntimePermissionRequests: false,
@@ -111,8 +113,7 @@ export class CodexProvider extends BaseSdkProvider {
stream: AsyncIterable<unknown>; stream: AsyncIterable<unknown>;
stop: () => Promise<boolean>; stop: () => Promise<boolean>;
}> { }> {
const sdkModule = await this.loadCodexSdkModule(); const client = await this.getCodexClient();
const client = new sdkModule.Codex();
const threadOptions: Record<string, unknown> = { const threadOptions: Record<string, unknown> = {
model: input.model, model: input.model,
@@ -139,6 +140,22 @@ export class CodexProvider extends BaseSdkProvider {
}; };
} }
/**
* Returns a shared Codex SDK client instance for this provider.
*/
private async getCodexClient(): Promise<CodexSdkClient> {
if (!this.codexClientPromise) {
this.codexClientPromise = this.loadCodexSdkModule()
.then((sdkModule) => new sdkModule.Codex())
.catch((error) => {
this.codexClientPromise = null;
throw error;
});
}
return this.codexClientPromise;
}
/** /**
* Builds Codex prompt items. Images are sent as `local_image` entries for SDK-native image support. * Builds Codex prompt items. Images are sent as `local_image` entries for SDK-native image support.
*/ */