// @generated by scripts/build-runtime.mjs; do not edit. // @ts-nocheck -- the generated entry uses a .ts extension for Pi's Jiti loader. import { resolveCompactionRouteForApi, terminalText } from "./chunk-BD6B7C5K.ts"; // src/settings-menu.ts import { runMenu } from "@narumitw/pi-tui-kit"; function timeoutLabel(milliseconds) { return `${milliseconds / 6e4} min`; } function retentionLabel(tokens) { return `${tokens / 1e3}K tokens`; } function protocolLabel(protocol) { switch (protocol) { case "auto": return "Auto"; case "remote-v2": return "Remote V2"; case "responses-compact": return "Responses Compact"; } } async function update(runtime, ctx, patch, signal) { try { await runtime.update(patch, signal); if (signal.aborted) return { kind: "rejected" }; ctx.ui.notify("Responses compaction settings saved.", "info"); return { kind: "stay" }; } catch (error) { if (signal.aborted) return { kind: "rejected" }; ctx.ui.notify( `Could not save pi-codex-compact.json: ${terminalText(error instanceof Error ? error.message : String(error))}`, "error" ); return { kind: "rejected" }; } } function createCodexCompactMenu(runtime, options = {}) { return { start: "main", screens: { main: ({ state }) => ({ kind: "actions", title: "Responses Compaction", lines: [ `Remote compaction: ${state.settings.enabled ? "On" : "Off"}`, `Protocol setting: ${protocolLabel(state.settings.protocol)}`, `Active model: ${terminalText(options.status?.model ?? "none")}`, `Compact route: ${terminalText(compactRoute(state, options.status))}` ], items: [ { id: "compact-now", label: "Compact now", description: "Close this menu and compact the active session immediately.", action: "compact-now" }, state.kind === "invalid" ? { id: "settings", label: "Settings", description: "Read-only until the invalid settings file is repaired.", to: "invalid" } : { id: "settings", label: "Settings", to: "settings" }, { id: "close", label: "Close", close: true } ], hint: "close" }), settings: ({ state }) => ({ kind: "settings", title: "Responses Compaction Settings", lines: [`User settings \xB7 ${terminalText(state.path)}`], items: [ { id: "enabled", label: "Remote compaction", description: "Use a supported Responses compaction protocol.", currentValue: state.settings.enabled ? "On" : "Off", values: ["On", "Off"], action: "set-enabled" }, { id: "protocol", label: "Protocol", description: "Choose automatically or force one supported remote protocol.", currentValue: protocolLabel(state.settings.protocol), values: ["Auto", "Remote V2", "Responses Compact"], action: "set-protocol" }, { id: "requestTimeoutMs", label: "Request timeout", description: "Maximum time for the extension-owned remote compaction request.", currentValue: timeoutLabel(state.settings.requestTimeoutMs), values: ["2 min", "5 min", "10 min"], action: "set-timeout" }, { id: "maxRetries", label: "Transport retries", description: "Retry transient provider failures before falling back to Pi.", currentValue: String(state.settings.maxRetries), values: ["0", "1", "2"], action: "set-retries" }, { id: "replacementTokenBudget", label: "Retained user history", description: "Approximate user-message budget kept beside the opaque checkpoint.", currentValue: retentionLabel(state.settings.replacementTokenBudget), values: ["32K tokens", "64K tokens", "96K tokens", "128K tokens"], action: "set-retention" }, { id: "notifyOnFallback", label: "Fallback notifications", description: "Warn when remote compaction fails and Pi native takes over.", currentValue: state.settings.notifyOnFallback ? "On" : "Off", values: ["On", "Off"], action: "set-notify" } ] }), invalid: ({ state }) => ({ kind: "detail", title: "Codex Compact Settings \xB7 Read only", lines: [ `Invalid settings file: ${terminalText(state.path)}`, `Issue: ${terminalText(state.issue ?? "unknown validation error")}`, "Built-in defaults are active. Repair the file and run /reload; it will not be overwritten." ], hint: "back" }) }, actions: { "compact-now": async () => { options.onCompactRequested?.(); return { kind: "close" }; }, "set-enabled": ({ ctx, value, signal }) => update(runtime, ctx, { enabled: value === "On" }, signal), "set-protocol": ({ ctx, value, signal }) => update( runtime, ctx, { protocol: value === "Remote V2" ? "remote-v2" : value === "Responses Compact" ? "responses-compact" : "auto" }, signal ), "set-timeout": ({ ctx, value, signal }) => update(runtime, ctx, { requestTimeoutMs: Number.parseInt(value ?? "5", 10) * 6e4 }, signal), "set-retries": ({ ctx, value, signal }) => update(runtime, ctx, { maxRetries: Number.parseInt(value ?? "2", 10) }, signal), "set-retention": ({ ctx, value, signal }) => update(runtime, ctx, { replacementTokenBudget: Number.parseInt(value ?? "64", 10) * 1e3 }, signal), "set-notify": ({ ctx, value, signal }) => update(runtime, ctx, { notifyOnFallback: value === "On" }, signal) } }; } async function showCodexCompactMenu(runtime, ctx, owner) { if (ctx.mode === "rpc" && ctx.hasUI) { ctx.ui.notify(`Edit Responses compaction settings at ${terminalText(runtime.get().path)}.`, "info"); return; } if (ctx.mode !== "tui") { throw new Error("/codex-compact requires TUI or RPC UI support"); } if (owner.signal.aborted || !owner.isCurrent()) return; let compactRequested = false; await runMenu( ctx, createCodexCompactMenu(runtime, { onCompactRequested: () => { compactRequested = true; }, status: compactMenuStatus(ctx) }), { getState: () => runtime.get(), signal: owner.signal, isCurrent: owner.isCurrent } ); if (!compactRequested || owner.signal.aborted || !owner.isCurrent()) return; ctx.compact({ onError: (error) => { if (!owner.signal.aborted && owner.isCurrent()) { ctx.ui.notify(`Compaction failed: ${terminalText(error.message)}`, "error"); } } }); } function compactMenuStatus(ctx) { const model = ctx.model; return { model: model ? `${model.provider}/${model.id}` : "none", api: model?.api }; } function compactRoute(state, status) { const route = resolveCompactionRouteForApi(status?.api, state.settings); if (route.kind === "native") return `Pi native (${route.reason})`; return route.protocol === "remote-v2" ? "Responses Remote V2" : "Responses Compact API"; } export { compactMenuStatus, createCodexCompactMenu, showCodexCompactMenu }; //# sourceMappingURL=settings-menu-SZZ437BH.ts.map