import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; export type ThinkingLevel = ReturnType; export const THINKING_LEVELS = ["off", "minimal", "low", "medium", "high", "xhigh", "max"] as const satisfies readonly ThinkingLevel[]; export type ThinkingDirection = "down" | "up"; export interface ModelLike { reasoning?: unknown; thinkingLevelMap?: Partial>; } const THINKING_LEVEL_RANK: Record = { off: 0, minimal: 1, low: 2, medium: 3, high: 4, xhigh: 5, max: 6, }; export function isThinkingLevel(value: unknown): value is ThinkingLevel { return typeof value === "string" && (THINKING_LEVELS as readonly string[]).includes(value); } export function rankThinkingLevel(level: ThinkingLevel): number { return THINKING_LEVEL_RANK[level]; } export function supportedThinkingLevelsFromModel(model: unknown): ThinkingLevel[] | undefined { if (!model || typeof model !== "object") return undefined; const candidate = model as ModelLike; if (!candidate.reasoning) return ["off"]; return THINKING_LEVELS.filter((level) => { const mapped = candidate.thinkingLevelMap?.[level]; if (mapped === null) return false; if (level === "xhigh" || level === "max") return mapped !== undefined; return true; }); } export function nextThinkingLevel( current: ThinkingLevel, available: readonly ThinkingLevel[], direction: ThinkingDirection, ): ThinkingLevel | undefined { const availableSet = new Set(available); const sorted = THINKING_LEVELS.filter((level) => availableSet.has(level)); const currentRank = rankThinkingLevel(current); if (direction === "up") { return sorted.find((level) => rankThinkingLevel(level) > currentRank); } for (let index = sorted.length - 1; index >= 0; index -= 1) { const level = sorted[index]; if (level && rankThinkingLevel(level) < currentRank) return level; } return undefined; }