/** * Model and thinking footer segment. * * Owns footer segment `model` in the `llm` zone. */ 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 STATUS_ID = "model-info"; const SEGMENT_ID = "model"; const FAST_SETTINGS_KEY = "fast"; const CODEX_PROVIDER = "openai-codex"; const FAST_BADGE = "fast"; function hasFooter(): boolean { const footer = (globalThis as any)[FOOTER_KEY]; return footer?.loaded === true && footer.version >= 1; } function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } function enabledSetting(value: unknown): boolean | undefined { if (!isRecord(value)) return undefined; return typeof value.enable === "boolean" ? value.enable : undefined; } function fastEnabled(cwd: string): boolean { const settings = SettingsManager.create(cwd); const globalEnabled = enabledSetting( (settings.getGlobalSettings() as Record)[ FAST_SETTINGS_KEY ], ); const projectEnabled = enabledSetting( (settings.getProjectSettings() as Record)[ FAST_SETTINGS_KEY ], ); return projectEnabled ?? globalEnabled ?? false; } function modelLabel(ctx: Pick): string { const modelId = ctx.model?.id ?? "no-model"; return modelId.split("/").pop() ?? modelId; } function thinkingLevelFromBranch( ctx: Pick, ): string { const branch = ctx.sessionManager.getBranch(); for (let i = branch.length - 1; i >= 0; i--) { const entry = branch[i] as | { type?: unknown; thinkingLevel?: unknown } | undefined; if ( entry?.type === "thinking_level_change" && typeof entry.thinkingLevel === "string" ) return entry.thinkingLevel; } return "off"; } function isPriority(ctx: Pick): boolean { return ctx.model?.provider === CODEX_PROVIDER && fastEnabled(ctx.cwd); } function modelInfoLabel( ctx: Pick, thinking?: string, ): string { const parts = [ `${modelLabel(ctx)}:${thinking ?? thinkingLevelFromBranch(ctx)}`, ]; if (isPriority(ctx)) parts.push(FAST_BADGE); return parts.join(" "); } function publish( pi: ExtensionAPI, ctx: ExtensionContext, thinking?: string, ): void { const text = modelInfoLabel(ctx, thinking); if (hasFooter()) { ctx.ui.setStatus(STATUS_ID, undefined); pi.events.emit("footer:segment", { id: SEGMENT_ID, text, color: "syntaxKeyword" as ThemeColor, zone: "llm", order: 0, }); return; } pi.events.emit("footer:segment", { id: SEGMENT_ID, text: undefined }); ctx.ui.setStatus(STATUS_ID, text); } function clear(pi: ExtensionAPI, ctx?: ExtensionContext): void { pi.events.emit("footer:segment", { id: SEGMENT_ID, text: undefined }); if (ctx?.hasUI) ctx.ui.setStatus(STATUS_ID, undefined); } export default function modelInfoExtension(pi: ExtensionAPI): void { function refresh(ctx: ExtensionContext, thinking?: string): void { if (!ctx.hasUI) return; publish(pi, ctx, thinking); } pi.on("session_start", (_event, ctx) => refresh(ctx)); pi.on("model_select", (_event, ctx) => refresh(ctx)); pi.on("thinking_level_select", (event, ctx) => refresh(ctx, event.level)); pi.on("session_compact", (_event, ctx) => refresh(ctx)); pi.on("session_shutdown", (_event, ctx) => clear(pi, ctx)); } export const __test = { enabledSetting, modelInfoLabel, modelLabel, thinkingLevelFromBranch, };