import type { ExtensionContext } from "@earendil-works/pi-coding-agent"; import type { Api, Model, ProviderHeaders } from "@earendil-works/pi-ai"; import { providerHistory } from "./compaction-checkpoint.ts"; import type { ImageDetail } from "./config.ts"; import type { ResponsesInputItem } from "./responses-item-schema.ts"; export interface CodexToolAuthentication { apiKey: string; headers?: ProviderHeaders; } export type ToolAuthenticationContext = { modelRegistry: Pick; }; export type ToolHistoryContext = { sessionManager: Pick; }; export async function codexToolAuthentication( ctx: ToolAuthenticationContext, model: Model, ): Promise { const authentication = await ctx.modelRegistry.getApiKeyAndHeaders(model); if (!authentication.ok) throw new Error(authentication.error); if (!authentication.apiKey) throw new Error("OpenAI Codex authentication is unavailable."); const result: CodexToolAuthentication = { apiKey: authentication.apiKey, }; if (authentication.headers) result.headers = authentication.headers; return result; } export function codexToolHistory( ctx: ToolHistoryContext, model: Model, imageDetail: ImageDetail = "auto", ): ResponsesInputItem[] { return providerHistory({ branch: ctx.sessionManager.getBranch(), wireModel: model, imageDetail, }); }