import { type Api, type Context, clampThinkingLevel, type Model, type SimpleStreamOptions, streamOpenAICodexResponses, streamSimpleOpenAICodexResponses, } from "@earendil-works/pi-ai"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { SettingsManager } from "@earendil-works/pi-coding-agent"; const PROVIDER = "openai-codex"; const API = "openai-codex-responses"; const SETTINGS_KEY = "fast"; type FastSettings = { enable?: boolean }; function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } function parseFastSettings(value: unknown): FastSettings { if (!isRecord(value)) return {}; return typeof value.enable === "boolean" ? { enable: value.enable } : {}; } function readEnabled(cwd: string): boolean { const settings = SettingsManager.create(cwd); const globalFast = parseFastSettings( (settings.getGlobalSettings() as Record)[SETTINGS_KEY], ); const projectFast = parseFastSettings( (settings.getProjectSettings() as Record)[SETTINGS_KEY], ); return projectFast.enable ?? globalFast.enable ?? false; } export default function fast(pi: ExtensionAPI): void { let enabled = readEnabled(process.cwd()); function refresh(cwd: string): void { enabled = readEnabled(cwd); } pi.registerProvider(PROVIDER, { api: API, streamSimple( model: Model, context: Context, options?: SimpleStreamOptions, ) { if (!enabled) return streamSimpleOpenAICodexResponses( model as Model, context, options, ); const clampedReasoning = options?.reasoning ? clampThinkingLevel(model, options.reasoning) : undefined; const reasoningEffort = clampedReasoning === "off" ? undefined : clampedReasoning; return streamOpenAICodexResponses(model as Model, context, { ...options, reasoningEffort, serviceTier: "priority", }); }, }); pi.on("session_start", (_event, ctx) => refresh(ctx.cwd)); pi.on("model_select", (_event, ctx) => refresh(ctx.cwd)); } export const _test = { parseFastSettings };