import { type Api, type Context, clampThinkingLevel, type Model, type SimpleStreamOptions, } from "@earendil-works/pi-ai"; import { streamOpenAICodexResponses, streamSimpleOpenAICodexResponses, } from "@earendil-works/pi-ai/compat"; import type { ExtensionAPI, ExtensionContext, ThemeColor, } from "@earendil-works/pi-coding-agent"; import { SettingsManager } from "@earendil-works/pi-coding-agent"; const FOOTER_KEY = Symbol.for("@bugabinga/pi-ext-footer"); const PROVIDER = "openai-codex"; const API = "openai-codex-responses"; const SETTINGS_KEY = "fast"; const STATUS_ID = "fast"; const SEGMENT_ID = "fast"; type FastSettings = { enable?: boolean }; type FooterGlobal = typeof globalThis & { [FOOTER_KEY]?: { loaded?: boolean; version?: number }; }; 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; } function hasFooter(): boolean { const footer = (globalThis as FooterGlobal)[FOOTER_KEY]; return footer?.loaded === true && (footer.version ?? 0) >= 1; } function fastActive( ctx: Pick, enabled: boolean, ): boolean { return enabled && ctx.model?.provider === PROVIDER; } export default function fast(pi: ExtensionAPI): void { let enabled = readEnabled(process.cwd()); function refresh(cwd: string): void { enabled = readEnabled(cwd); } function clear(ctx?: ExtensionContext): void { pi.events.emit("footer:segment", { id: SEGMENT_ID, text: undefined }); if (ctx?.hasUI) ctx.ui.setStatus(STATUS_ID, undefined); } function publish(ctx: ExtensionContext): void { if (!ctx.hasUI) return; if (!fastActive(ctx, enabled)) { clear(ctx); return; } if (hasFooter()) { ctx.ui.setStatus(STATUS_ID, undefined); pi.events.emit("footer:segment", { id: SEGMENT_ID, text: "fast", color: "warning" as ThemeColor, zone: "llm", order: 0.5, }); return; } pi.events.emit("footer:segment", { id: SEGMENT_ID, text: undefined }); ctx.ui.setStatus(STATUS_ID, "fast"); } 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); publish(ctx); }); pi.on("model_select", (_event, ctx) => { refresh(ctx.cwd); publish(ctx); }); pi.on("session_shutdown", (_event, ctx) => clear(ctx)); } export const _test = { fastActive, parseFastSettings };