repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/model-info/index.ts
RawFile outline
- FOOTER_KEY line 14
- STATUS_ID line 15
- SEGMENT_ID line 16
- FAST_SETTINGS_KEY line 17
- CODEX_PROVIDER line 18
- FAST_BADGE line 19
- hasFooter line 21
- isRecord line 26
- enabledSetting line 30
- fastEnabled line 35
- modelLabel line 50
- thinkingLevelFromBranch line 55
- thinkingLevel line 61
- type line 61
- isPriority line 72
- modelInfoLabel line 76
- publish line 87
- clear line 110
- modelInfoExtension line 115
- refresh line 116
- __test line 128
/**
* 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<string, unknown> {
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<string, unknown>)[
FAST_SETTINGS_KEY
],
);
const projectEnabled = enabledSetting(
(settings.getProjectSettings() as Record<string, unknown>)[
FAST_SETTINGS_KEY
],
);
return projectEnabled ?? globalEnabled ?? false;
}
function modelLabel(ctx: Pick<ExtensionContext, "model">): string {
const modelId = ctx.model?.id ?? "no-model";
return modelId.split("/").pop() ?? modelId;
}
function thinkingLevelFromBranch(
ctx: Pick<ExtensionContext, "sessionManager">,
): 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<ExtensionContext, "cwd" | "model">): boolean {
return ctx.model?.provider === CODEX_PROVIDER && fastEnabled(ctx.cwd);
}
function modelInfoLabel(
ctx: Pick<ExtensionContext, "cwd" | "model" | "sessionManager">,
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,
};