fix(coding-agent): decouple codex session cleanup

This commit is contained in:
Mario Zechner
2026-05-04 00:45:56 +02:00
parent 5fa277b320
commit 23420012ab
4 changed files with 30 additions and 7 deletions

View File

@@ -16,14 +16,10 @@ export type {
OpenAICodexResponsesOptions,
OpenAICodexWebSocketDebugStats,
} from "./providers/openai-codex-responses.js";
export {
closeOpenAICodexWebSocketSessions,
getOpenAICodexWebSocketDebugStats,
resetOpenAICodexWebSocketDebugStats,
} from "./providers/openai-codex-responses.js";
export type { OpenAICompletionsOptions } from "./providers/openai-completions.js";
export type { OpenAIResponsesOptions } from "./providers/openai-responses.js";
export * from "./providers/register-builtins.js";
export * from "./session-resources.js";
export * from "./stream.js";
export * from "./types.js";
export * from "./utils/diagnostics.js";

View File

@@ -22,6 +22,7 @@ if (typeof process !== "undefined" && (process.versions?.node || process.version
import { getEnvApiKey } from "../env-api-keys.js";
import { clampThinkingLevel } from "../models.js";
import { registerSessionResourceCleanup } from "../session-resources.js";
import type {
Api,
AssistantMessage,
@@ -682,6 +683,8 @@ export function closeOpenAICodexWebSocketSessions(sessionId?: string): void {
websocketSessionCache.clear();
}
registerSessionResourceCleanup(closeOpenAICodexWebSocketSessions);
function isWebSocketSseFallbackActive(sessionId: string | undefined): boolean {
return sessionId ? websocketSseFallbackSessions.has(sessionId) : false;
}

View File

@@ -0,0 +1,24 @@
export type SessionResourceCleanup = (sessionId?: string) => void;
const sessionResourceCleanups = new Set<SessionResourceCleanup>();
export function registerSessionResourceCleanup(cleanup: SessionResourceCleanup): () => void {
sessionResourceCleanups.add(cleanup);
return () => {
sessionResourceCleanups.delete(cleanup);
};
}
export function cleanupSessionResources(sessionId?: string): void {
const errors: unknown[] = [];
for (const cleanup of sessionResourceCleanups) {
try {
cleanup(sessionId);
} catch (error) {
errors.push(error);
}
}
if (errors.length > 0) {
throw new AggregateError(errors, "Failed to cleanup session resources");
}
}