pi-cliproxyapi-provider 0.4.12 → 0.4.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-cliproxyapi-provider",
3
- "version": "0.4.12",
3
+ "version": "0.4.14",
4
4
  "description": "Pi provider package for CLIProxyAPI with automatic model discovery and models.dev enrichment.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -42,7 +42,8 @@
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@earendil-works/pi-ai": ">=0.84.0",
45
- "@earendil-works/pi-coding-agent": ">=0.84.0"
45
+ "@earendil-works/pi-coding-agent": ">=0.84.0",
46
+ "@earendil-works/pi-tui": ">=0.84.0"
46
47
  },
47
48
  "peerDependenciesMeta": {
48
49
  "@earendil-works/pi-coding-agent": {
@@ -50,11 +51,15 @@
50
51
  },
51
52
  "@earendil-works/pi-ai": {
52
53
  "optional": true
54
+ },
55
+ "@earendil-works/pi-tui": {
56
+ "optional": true
53
57
  }
54
58
  },
55
59
  "devDependencies": {
56
60
  "@earendil-works/pi-ai": "0.84.1",
57
61
  "@earendil-works/pi-coding-agent": "0.84.1",
62
+ "@earendil-works/pi-tui": "0.84.1",
58
63
  "typescript": "^5.9.3"
59
64
  }
60
65
  }
package/src/catalog.ts CHANGED
@@ -228,6 +228,7 @@ export class ProviderCatalog {
228
228
  metadata,
229
229
  this.options.config.modelAliases,
230
230
  this.options.gpt56ContextWindow,
231
+ this.options.config.modelOverrides,
231
232
  ),
232
233
  };
233
234
  return this.snapshot;
package/src/commands.ts CHANGED
@@ -2,6 +2,8 @@ import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-c
2
2
  import { DEFAULT_CONFIG, loadConfig, globalConfigPath, readConfigFile, writeConfigFile, type ConfigLayer } from "./config.ts";
3
3
  import type { ProviderCatalog, CatalogSnapshot, RefreshTarget, SourceRefreshResult } from "./catalog.ts";
4
4
  import type { ProviderRuntime } from "./runtime.ts";
5
+ import { openModelInspector, openProviderConfig } from "./model-ui.ts";
6
+ import { loadProviderSettings } from "./settings.ts";
5
7
 
6
8
  function errorText(error: unknown): string {
7
9
  return error instanceof Error ? error.message : String(error);
@@ -43,7 +45,7 @@ function capabilityCount(snapshot: CatalogSnapshot, key: "reasoning" | "image"):
43
45
 
44
46
  export async function runConfig(ctx: ExtensionCommandContext): Promise<void> {
45
47
  if (!ctx.hasUI) {
46
- ctx.ui.notify("/cliproxyapi config requires an interactive UI.", "warning");
48
+ ctx.ui.notify("/cliproxyapi config connection requires an interactive UI.", "warning");
47
49
  return;
48
50
  }
49
51
 
@@ -81,6 +83,7 @@ export async function runConfig(ctx: ExtensionCommandContext): Promise<void> {
81
83
  : false;
82
84
 
83
85
  writeConfigFile(path, {
86
+ ...existing,
84
87
  providerName: providerNameInput || defaults.providerName,
85
88
  baseUrl: baseUrlInput || defaults.baseUrl,
86
89
  authRequired,
@@ -117,8 +120,21 @@ function parseRefreshTarget(value: string | undefined): RefreshTarget | undefine
117
120
  return undefined;
118
121
  }
119
122
 
123
+ const CLIPROXYAPI_HELP = [
124
+ "CLIProxyAPI provider commands:",
125
+ " /cliproxyapi status Show provider and model-catalog status",
126
+ " /cliproxyapi refresh Refresh CPA models and models.dev metadata",
127
+ " /cliproxyapi refresh models Refresh CPA models only",
128
+ " /cliproxyapi refresh metadata Refresh models.dev metadata only",
129
+ " /cliproxyapi aliases Show unmatched model IDs for alias configuration",
130
+ " /cliproxyapi models Inspect models and set bounded overrides",
131
+ " /cliproxyapi config Configure model behavior and display",
132
+ " /cliproxyapi config connection Configure provider endpoint and auth",
133
+ " /cliproxyapi help Show this help",
134
+ ].join("\n");
135
+
120
136
  export function cliproxyapiArgumentCompletions(prefix: string): Array<{ value: string; label: string }> {
121
- return ["config", "status", "refresh", "refresh models", "refresh metadata", "aliases", "help"]
137
+ return ["config", "config connection", "status", "refresh", "refresh models", "refresh metadata", "aliases", "models", "help"]
122
138
  .filter((item) => item.startsWith(prefix))
123
139
  .map((value) => ({ value, label: value }));
124
140
  }
@@ -130,8 +146,26 @@ export function registerCliproxyapiCommand(pi: ExtensionAPI, runtime?: ProviderR
130
146
  return cliproxyapiArgumentCompletions(prefix);
131
147
  },
132
148
  async handler(args, ctx) {
133
- const [subcommand = "help", option] = args.trim().split(/\s+/);
134
- if (subcommand === "config") return runConfig(ctx);
149
+ const commandArgs = args.trim();
150
+ const [subcommand, option] = commandArgs ? commandArgs.split(/\s+/) : ["help"];
151
+ if (subcommand === "help") {
152
+ ctx.ui.notify(CLIPROXYAPI_HELP, "info");
153
+ return;
154
+ }
155
+ if (subcommand === "config") {
156
+ if (option === "connection") return runConfig(ctx);
157
+ if (option) {
158
+ ctx.ui.notify("Usage: /cliproxyapi config [connection]", "warning");
159
+ return;
160
+ }
161
+ const action = await openProviderConfig(ctx, loadProviderSettings(ctx.cwd), loadConfig(ctx.cwd));
162
+ if (action === "connection") return runConfig(ctx);
163
+ return;
164
+ }
165
+ if (!["status", "refresh", "aliases", "models"].includes(subcommand)) {
166
+ ctx.ui.notify(`${CLIPROXYAPI_HELP}\n\nUnknown command: ${subcommand}`, "warning");
167
+ return;
168
+ }
135
169
  if (!runtime || !catalog) {
136
170
  ctx.ui.notify("CLIProxyAPI provider is unavailable. Run /cliproxyapi config and reload pi.", "error");
137
171
  return;
@@ -170,7 +204,10 @@ export function registerCliproxyapiCommand(pi: ExtensionAPI, runtime?: ProviderR
170
204
  ctx.ui.notify(body, snapshot.built.stats.unmatched ? "warning" : "info");
171
205
  return;
172
206
  }
173
- ctx.ui.notify("Usage: /cliproxyapi config|status|refresh [models|metadata]|aliases", "info");
207
+ if (subcommand === "models") {
208
+ await openModelInspector(ctx, catalog, config.modelOverrides, loadProviderSettings(ctx.cwd).showStrictMode);
209
+ return;
210
+ }
174
211
  },
175
212
  });
176
213
  }
package/src/config.ts CHANGED
@@ -1,10 +1,19 @@
1
1
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
2
2
  import { homedir } from "node:os";
3
3
  import { dirname, join } from "node:path";
4
- import type { CpaProviderConfig } from "./types.ts";
4
+ import type {
5
+ CpaProviderConfig,
6
+ ProviderModelOverride,
7
+ ProviderModelOverrideLayer,
8
+ ProviderModelOverrideLayers,
9
+ ProviderModelOverrides,
10
+ } from "./types.ts";
5
11
 
6
12
  export type ConfigLayer = Partial<CpaProviderConfig>;
7
13
 
14
+ export const CONTEXT_WINDOW_PRESETS = [128000, 272000, 512000, 1000000] as const;
15
+ export const MAX_TOKEN_PRESETS = [4096, 8192, 16384, 32768, 65536, 128000] as const;
16
+
8
17
  export const DEFAULT_CONFIG: CpaProviderConfig = {
9
18
  providerName: "cpa",
10
19
  baseUrl: "http://localhost:8317/v1",
@@ -13,6 +22,7 @@ export const DEFAULT_CONFIG: CpaProviderConfig = {
13
22
  headers: {},
14
23
  modelsDevEnabled: true,
15
24
  modelAliases: {},
25
+ modelOverrides: {},
16
26
  };
17
27
 
18
28
  export function globalConfigPath(): string {
@@ -50,9 +60,29 @@ function safeProjectConfig(projectConfig?: ConfigLayer): ConfigLayer | undefined
50
60
  if (!projectConfig) return undefined;
51
61
  return {
52
62
  ...(projectConfig.modelAliases !== undefined ? { modelAliases: projectConfig.modelAliases } : {}),
63
+ ...(projectConfig.modelOverrides !== undefined ? { modelOverrides: projectConfig.modelOverrides } : {}),
53
64
  };
54
65
  }
55
66
 
67
+ function mergeModelOverrides(
68
+ base: ProviderModelOverrides,
69
+ layer: ProviderModelOverrideLayers | undefined,
70
+ ): ProviderModelOverrides {
71
+ if (!layer) return base;
72
+ const merged = { ...base };
73
+ for (const [modelId, override] of Object.entries(layer)) {
74
+ const next = { ...merged[modelId] } as ProviderModelOverride;
75
+ for (const field of ["reasoning", "contextWindow", "maxTokens"] as const) {
76
+ const value = override[field];
77
+ if (value === null) delete next[field];
78
+ else if (value !== undefined) (next as Record<string, boolean | number>)[field] = value;
79
+ }
80
+ if (Object.keys(next).length === 0) delete merged[modelId];
81
+ else merged[modelId] = next;
82
+ }
83
+ return merged;
84
+ }
85
+
56
86
  function projectConfigLayer(value: unknown, path: string): ConfigLayer {
57
87
  if (!value || typeof value !== "object" || Array.isArray(value)) {
58
88
  throw new Error(`Project config file must contain a JSON object: ${path}`);
@@ -62,8 +92,11 @@ function projectConfigLayer(value: unknown, path: string): ConfigLayer {
62
92
  if (record.modelAliases !== undefined && !isStringMap(record.modelAliases)) {
63
93
  throw new Error(`modelAliases must be an object with string values in project config file: ${path}`);
64
94
  }
95
+ const modelOverrides = record.modelOverrides === undefined
96
+ ? undefined
97
+ : parseModelOverrides(record.modelOverrides, "project");
65
98
 
66
- return safeProjectConfig(record as ConfigLayer) ?? {};
99
+ return safeProjectConfig({ ...record, ...(modelOverrides ? { modelOverrides } : {}) } as ConfigLayer) ?? {};
67
100
  }
68
101
 
69
102
  function mergeLayer(base: CpaProviderConfig, layer?: ConfigLayer): CpaProviderConfig {
@@ -73,6 +106,7 @@ function mergeLayer(base: CpaProviderConfig, layer?: ConfigLayer): CpaProviderCo
73
106
  ...layer,
74
107
  headers: { ...base.headers, ...(layer.headers ?? {}) },
75
108
  modelAliases: { ...base.modelAliases, ...(layer.modelAliases ?? {}) },
109
+ modelOverrides: mergeModelOverrides(base.modelOverrides, layer.modelOverrides),
76
110
  };
77
111
  }
78
112
 
@@ -105,6 +139,50 @@ function isStringMap(value: unknown): value is Record<string, string> {
105
139
  && Object.values(value).every((entry) => typeof entry === "string");
106
140
  }
107
141
 
142
+ function isAllowedPreset(value: unknown, presets: readonly number[]): value is number {
143
+ return typeof value === "number" && presets.includes(value);
144
+ }
145
+
146
+ function parseModelOverrides(value: unknown, scope: string): ProviderModelOverrideLayers {
147
+ if (!value || typeof value !== "object" || Array.isArray(value)) {
148
+ throw new Error(`modelOverrides must be an object in ${scope} config file`);
149
+ }
150
+
151
+ const parsed: ProviderModelOverrideLayers = {};
152
+ for (const [modelId, rawOverride] of Object.entries(value)) {
153
+ if (!modelId.trim()) throw new Error(`modelOverrides keys must be non-empty model IDs in ${scope} config file`);
154
+ if (!rawOverride || typeof rawOverride !== "object" || Array.isArray(rawOverride)) {
155
+ throw new Error(`modelOverrides.${modelId} must be an object in ${scope} config file`);
156
+ }
157
+
158
+ const record = rawOverride as Record<string, unknown>;
159
+ const unknown = Object.keys(record).filter(
160
+ (key) => key !== "reasoning" && key !== "contextWindow" && key !== "maxTokens",
161
+ );
162
+ if (unknown.length > 0) {
163
+ throw new Error(`modelOverrides.${modelId} contains unsupported fields: ${unknown.join(", ")}`);
164
+ }
165
+ const nullable = scope === "project";
166
+ if (record.reasoning !== undefined && typeof record.reasoning !== "boolean" && !(nullable && record.reasoning === null)) {
167
+ throw new Error(`modelOverrides.${modelId}.reasoning must be a boolean${nullable ? " or null" : ""} in ${scope} config file`);
168
+ }
169
+ const presets = { contextWindow: CONTEXT_WINDOW_PRESETS, maxTokens: MAX_TOKEN_PRESETS };
170
+ for (const field of ["contextWindow", "maxTokens"] as const) {
171
+ const value = record[field];
172
+ if (value !== undefined && !isAllowedPreset(value, presets[field]) && !(nullable && value === null)) {
173
+ throw new Error(`modelOverrides.${modelId}.${field} must be one of ${presets[field].join(", ")}${nullable ? " or null" : ""} in ${scope} config file`);
174
+ }
175
+ }
176
+
177
+ parsed[modelId] = {
178
+ ...(record.reasoning !== undefined ? { reasoning: record.reasoning as boolean | null } : {}),
179
+ ...(record.contextWindow !== undefined ? { contextWindow: record.contextWindow as number | null } : {}),
180
+ ...(record.maxTokens !== undefined ? { maxTokens: record.maxTokens as number | null } : {}),
181
+ } satisfies ProviderModelOverrideLayer;
182
+ }
183
+ return parsed;
184
+ }
185
+
108
186
  function validateConfigLayer(value: unknown, path: string): ConfigLayer {
109
187
  if (!value || typeof value !== "object" || Array.isArray(value)) {
110
188
  throw new Error(`Config file must contain a JSON object: ${path}`);
@@ -131,8 +209,11 @@ function validateConfigLayer(value: unknown, path: string): ConfigLayer {
131
209
  if (record.modelAliases !== undefined && !isStringMap(record.modelAliases)) {
132
210
  throw new Error(`modelAliases must be an object with string values in config file: ${path}`);
133
211
  }
212
+ const modelOverrides = record.modelOverrides === undefined
213
+ ? undefined
214
+ : parseModelOverrides(record.modelOverrides, "global");
134
215
 
135
- return record as ConfigLayer;
216
+ return { ...record, ...(modelOverrides ? { modelOverrides } : {}) } as ConfigLayer;
136
217
  }
137
218
 
138
219
  export function readConfigFile(path: string): ConfigLayer | undefined {
@@ -153,3 +234,38 @@ export function writeConfigFile(path: string, config: ConfigLayer): void {
153
234
  mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
154
235
  writeFileSync(path, `${JSON.stringify(config, null, 2)}\n`, { encoding: "utf8", mode: 0o600 });
155
236
  }
237
+
238
+ export function loadModelOverrideLayers(cwd: string): {
239
+ global: ProviderModelOverrideLayers;
240
+ project: ProviderModelOverrideLayers;
241
+ } {
242
+ return {
243
+ global: (readConfigFile(globalConfigPath())?.modelOverrides ?? {}) as ProviderModelOverrideLayers,
244
+ project: (readProjectConfigFile(projectConfigPath(cwd))?.modelOverrides ?? {}) as ProviderModelOverrideLayers,
245
+ };
246
+ }
247
+
248
+ export function saveModelOverride(
249
+ cwd: string,
250
+ modelId: string,
251
+ override: ProviderModelOverrideLayer,
252
+ ): { path: string; overrides: ProviderModelOverrideLayers } {
253
+ const path = projectConfigPath(cwd);
254
+ let raw: Record<string, unknown> = {};
255
+ if (existsSync(path)) {
256
+ const parsed: unknown = JSON.parse(readFileSync(path, "utf8"));
257
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
258
+ throw new Error(`Config file must contain a JSON object: ${path}`);
259
+ }
260
+ raw = parsed as Record<string, unknown>;
261
+ }
262
+
263
+ const existing = raw.modelOverrides === undefined
264
+ ? {}
265
+ : parseModelOverrides(raw.modelOverrides, "project");
266
+ const next = { ...existing };
267
+ if (Object.keys(override).length === 0) delete next[modelId];
268
+ else next[modelId] = override;
269
+ writeConfigFile(path, { ...raw, modelOverrides: next } as ConfigLayer);
270
+ return { path, overrides: next };
271
+ }
@@ -0,0 +1,353 @@
1
+ import { DynamicBorder, getSettingsListTheme, type ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
2
+ import { Container, Key, matchesKey, type SelectItem, SelectList, type SettingItem, SettingsList, Text, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";
3
+ import {
4
+ CONTEXT_WINDOW_PRESETS,
5
+ loadModelOverrideLayers,
6
+ MAX_TOKEN_PRESETS,
7
+ saveModelOverride,
8
+ } from "./config.ts";
9
+ import type { ProviderCatalog } from "./catalog.ts";
10
+ import { normalizeProviderModels } from "./registration.ts";
11
+ import { saveProviderSettings, type ProviderSettings } from "./settings.ts";
12
+ import type {
13
+ CpaProviderConfig,
14
+ ProviderModelConfigLike,
15
+ ProviderModelOverride,
16
+ ProviderModelOverrideLayer,
17
+ } from "./types.ts";
18
+
19
+ function modelOverride(model: ProviderModelConfigLike, overrides: Record<string, ProviderModelOverride>): ProviderModelOverride {
20
+ return overrides[model.id] ?? {};
21
+ }
22
+
23
+ function valuesFor(current: number, presets: number[]): string[] {
24
+ return ["auto", ...new Set([current, ...presets]).values()].map(String);
25
+ }
26
+
27
+ function effectiveReasoning(model: ProviderModelConfigLike, override: ProviderModelOverrideLayer): boolean {
28
+ return typeof override.reasoning === "boolean" ? override.reasoning : model.reasoning;
29
+ }
30
+
31
+ function effectiveNumber(
32
+ model: ProviderModelConfigLike,
33
+ override: ProviderModelOverrideLayer,
34
+ field: "contextWindow" | "maxTokens",
35
+ ): number {
36
+ return typeof override[field] === "number" ? override[field] : model[field];
37
+ }
38
+
39
+ function strictModeLabel(model: ProviderModelConfigLike): string {
40
+ const strict = (model.compat as { supportsStrictMode?: boolean } | undefined)?.supportsStrictMode;
41
+ return strict === false ? "disabled" : strict === true ? "enabled" : "auto";
42
+ }
43
+
44
+ function formattedOtherCompat(model: ProviderModelConfigLike): string {
45
+ const entries = Object.entries(model.compat ?? {}).filter(([key]) => key !== "supportsStrictMode");
46
+ return entries.length === 0 ? "none" : entries.map(([key, value]) => `${key}=${String(value)}`).join(", ");
47
+ }
48
+
49
+ function detailItems(model: ProviderModelConfigLike, override: ProviderModelOverrideLayer): SettingItem[] {
50
+ const reasoning = effectiveReasoning(model, override);
51
+ const contextWindow = effectiveNumber(model, override, "contextWindow");
52
+ const maxTokens = effectiveNumber(model, override, "maxTokens");
53
+ return [
54
+ {
55
+ id: "reasoning",
56
+ label: "Reasoning",
57
+ description: `Effective: ${reasoning ? "on" : "off"}. ${override.reasoning === undefined ? "Metadata/capability default." : "User override."}`,
58
+ currentValue: typeof override.reasoning !== "boolean" ? "auto" : override.reasoning ? "on" : "off",
59
+ values: ["auto", "on", "off"],
60
+ },
61
+ {
62
+ id: "contextWindow",
63
+ label: "Context window",
64
+ description: `Effective: ${contextWindow} tokens. ${override.contextWindow === undefined ? "Derived default." : "User override."}`,
65
+ currentValue: typeof override.contextWindow !== "number" ? "auto" : String(override.contextWindow),
66
+ values: valuesFor(contextWindow, [...CONTEXT_WINDOW_PRESETS]),
67
+ },
68
+ {
69
+ id: "maxTokens",
70
+ label: "Max output tokens",
71
+ description: `Effective: ${maxTokens} tokens. ${override.maxTokens === undefined ? "Derived default." : "User override."}`,
72
+ currentValue: typeof override.maxTokens !== "number" ? "auto" : String(override.maxTokens),
73
+ values: valuesFor(maxTokens, [...MAX_TOKEN_PRESETS]),
74
+ },
75
+ ];
76
+ }
77
+
78
+ function details(model: ProviderModelConfigLike): string[] {
79
+ return [
80
+ `Name: ${model.name}`,
81
+ `API: ${model.api ?? "openai-completions (provider default)"}`,
82
+ `Input: ${model.input.join(", ")}`,
83
+ `Cost: in ${model.cost.input}, out ${model.cost.output}, cache read ${model.cost.cacheRead}, cache write ${model.cost.cacheWrite}`,
84
+ `Thinking map: ${model.thinkingLevelMap ? Object.keys(model.thinkingLevelMap).join(", ") : "none"}`,
85
+ `Other compat: ${formattedOtherCompat(model)}`,
86
+ ];
87
+ }
88
+
89
+ function modelSelectorItems(
90
+ models: ProviderModelConfigLike[],
91
+ overrides: Record<string, ProviderModelOverride>,
92
+ showStrictMode: boolean,
93
+ ): SelectItem[] {
94
+ const sorted = [...models].sort((left, right) => left.id.localeCompare(right.id));
95
+ const idWidth = Math.max(...sorted.map((model) => visibleWidth(model.id)));
96
+ const apiWidth = Math.max(...sorted.map((model) => visibleWidth(model.api ?? "completions")));
97
+ const modeWidth = Math.max(...sorted.map((model) => visibleWidth(model.reasoning ? "reasoning" : "standard")));
98
+ const strictWidth = showStrictMode
99
+ ? Math.max(...sorted.map((model) => visibleWidth(strictModeLabel(model))))
100
+ : 0;
101
+ const contextWidth = Math.max(...sorted.map((model) => String(model.contextWindow).length));
102
+ const pad = (value: string, width: number) => `${value}${" ".repeat(Math.max(0, width - visibleWidth(value)))}`;
103
+
104
+ return sorted.map((model) => {
105
+ const override = modelOverride(model, overrides);
106
+ const overrideLabel = Object.keys(override).length > 0 ? " override" : "";
107
+ const api = model.api ?? "completions";
108
+ const mode = model.reasoning ? "reasoning" : "standard";
109
+ const strict = strictModeLabel(model);
110
+ const context = String(model.contextWindow).padStart(contextWidth);
111
+ return {
112
+ value: model.id,
113
+ label: pad(model.id, idWidth),
114
+ description: [
115
+ pad(api, apiWidth),
116
+ pad(mode, modeWidth),
117
+ ...(showStrictMode ? [`${pad(strict, strictWidth)} strict`] : []),
118
+ `${context} ctx${overrideLabel}`,
119
+ ].join(" "),
120
+ };
121
+ });
122
+ }
123
+
124
+ type ConfigTab = "Connection" | "Models" | "Display";
125
+
126
+ const CONFIG_TABS: ConfigTab[] = ["Connection", "Models", "Display"];
127
+
128
+ function providerSettingsItems(
129
+ tab: ConfigTab,
130
+ settings: ProviderSettings,
131
+ connection: CpaProviderConfig,
132
+ ): SettingItem[] {
133
+ if (tab === "Connection") {
134
+ return [{
135
+ id: "connection",
136
+ label: "Endpoint and authentication",
137
+ description: `${connection.providerName} ${connection.baseUrl} ${connection.authRequired ? "credentials required" : "no credentials"}`,
138
+ currentValue: "open",
139
+ values: ["open", "edit"],
140
+ }];
141
+ }
142
+ if (tab === "Models") return [
143
+ {
144
+ id: "gpt56ContextWindow",
145
+ label: "GPT-5.6 context window",
146
+ description: "canonical advertises 272000 tokens; full uses the models.dev limit.",
147
+ currentValue: settings.gpt56ContextWindow,
148
+ values: ["canonical", "full"],
149
+ },
150
+ ];
151
+ return [
152
+ {
153
+ id: "showStrictMode",
154
+ label: "Show strict tool schema",
155
+ description: "Show the published strict-schema compatibility value in the model selector.",
156
+ currentValue: settings.showStrictMode ? "enabled" : "disabled",
157
+ values: ["disabled", "enabled"],
158
+ },
159
+ ];
160
+ }
161
+
162
+ function nextTab(tab: ConfigTab, direction: 1 | -1): ConfigTab {
163
+ const index = CONFIG_TABS.indexOf(tab);
164
+ return CONFIG_TABS[(index + direction + CONFIG_TABS.length) % CONFIG_TABS.length] ?? "Connection";
165
+ }
166
+
167
+ export async function openProviderConfig(
168
+ ctx: ExtensionCommandContext,
169
+ settings: ProviderSettings,
170
+ connection: CpaProviderConfig,
171
+ ): Promise<"connection" | undefined> {
172
+ if (ctx.mode !== "tui") {
173
+ ctx.ui.notify("/cliproxyapi config requires interactive TUI mode.", "warning");
174
+ return;
175
+ }
176
+
177
+ const edited: ProviderSettings = { ...settings };
178
+ const action = await ctx.ui.custom<"connection" | undefined>((tui, theme, _keybindings, done) => {
179
+ let activeTab: ConfigTab = "Connection";
180
+ let list: SettingsList;
181
+ const makeList = () => new SettingsList(
182
+ providerSettingsItems(activeTab, edited, connection),
183
+ 8,
184
+ getSettingsListTheme(),
185
+ (id, value) => {
186
+ if (id === "connection" && value === "edit") {
187
+ done("connection");
188
+ return;
189
+ }
190
+ if (id === "gpt56ContextWindow" && (value === "canonical" || value === "full")) {
191
+ edited.gpt56ContextWindow = value;
192
+ }
193
+ if (id === "showStrictMode" && (value === "enabled" || value === "disabled")) {
194
+ edited.showStrictMode = value === "enabled";
195
+ }
196
+ list.updateValue(id, value);
197
+ tui.requestRender();
198
+ },
199
+ () => done(undefined),
200
+ { enableSearch: true },
201
+ );
202
+ list = makeList();
203
+ return {
204
+ render: (width) => {
205
+ const border = theme.fg("border", "─".repeat(Math.max(0, width)));
206
+ const tabs = CONFIG_TABS.map((tab) => tab === activeTab
207
+ ? theme.fg("accent", theme.bold(tab))
208
+ : theme.fg("muted", tab));
209
+ return [
210
+ border,
211
+ ` ${tabs.join(theme.fg("muted", " / "))}`,
212
+ border,
213
+ ...list.render(width),
214
+ theme.fg("dim", " Enter/Space changes Tab switches sections Esc saves and reloads Pi"),
215
+ border,
216
+ ].map((line) => truncateToWidth(line, width, ""));
217
+ },
218
+ invalidate: () => list.invalidate(),
219
+ handleInput: (data) => {
220
+ if (matchesKey(data, Key.tab)) {
221
+ activeTab = nextTab(activeTab, 1);
222
+ list = makeList();
223
+ tui.requestRender();
224
+ return;
225
+ }
226
+ if (matchesKey(data, Key.shift("tab"))) {
227
+ activeTab = nextTab(activeTab, -1);
228
+ list = makeList();
229
+ tui.requestRender();
230
+ return;
231
+ }
232
+ list.handleInput(data);
233
+ },
234
+ };
235
+ });
236
+
237
+ if (action === "connection") return action;
238
+ if (JSON.stringify(settings) === JSON.stringify(edited)) return;
239
+ try {
240
+ const path = saveProviderSettings(ctx.cwd, edited);
241
+ ctx.ui.notify(`Saved CLIProxyAPI configuration to ${path}. Reloading Pi...`, "info");
242
+ await ctx.reload();
243
+ } catch (error) {
244
+ const message = error instanceof Error ? error.message : String(error);
245
+ ctx.ui.notify(`Could not save CLIProxyAPI configuration: ${message}`, "error");
246
+ }
247
+ }
248
+
249
+ export async function openModelInspector(
250
+ ctx: ExtensionCommandContext,
251
+ catalog: ProviderCatalog,
252
+ overrides: Record<string, ProviderModelOverride>,
253
+ showStrictMode: boolean,
254
+ ): Promise<void> {
255
+ if (ctx.mode !== "tui") {
256
+ ctx.ui.notify("/cliproxyapi models requires interactive TUI mode.", "warning");
257
+ return;
258
+ }
259
+
260
+ const snapshot = catalog.current() ?? await catalog.load();
261
+ const models = normalizeProviderModels(snapshot.built.models);
262
+ if (models.length === 0) {
263
+ ctx.ui.notify("No available models in the current CLIProxyAPI snapshot. Run /cliproxyapi refresh models first.", "warning");
264
+ return;
265
+ }
266
+
267
+ const selectedId = await ctx.ui.custom<string | undefined>((tui, theme, _keybindings, done) => {
268
+ const container = new Container();
269
+ container.addChild(new DynamicBorder((text) => theme.fg("border", text)));
270
+ container.addChild(new Text(theme.fg("accent", theme.bold("CLIProxyAPI models")), 1, 0));
271
+ container.addChild(new Text(theme.fg("muted", "Select a model to inspect or override bounded limits."), 1, 0));
272
+ const list = new SelectList(modelSelectorItems(models, overrides, showStrictMode), Math.min(models.length, 12), {
273
+ selectedPrefix: (text) => theme.fg("accent", text),
274
+ selectedText: (text) => theme.fg("accent", text),
275
+ description: (text) => theme.fg("muted", text),
276
+ scrollInfo: (text) => theme.fg("dim", text),
277
+ noMatch: (text) => theme.fg("warning", text),
278
+ });
279
+ list.onSelect = (item) => done(item.value);
280
+ list.onCancel = () => done(undefined);
281
+ container.addChild(list);
282
+ container.addChild(new Text(theme.fg("dim", "↑↓ navigate Enter inspect Esc close"), 1, 0));
283
+ container.addChild(new DynamicBorder((text) => theme.fg("border", text)));
284
+ return {
285
+ render: (width) => container.render(width),
286
+ invalidate: () => container.invalidate(),
287
+ handleInput: (data) => { list.handleInput(data); tui.requestRender(); },
288
+ };
289
+ });
290
+ if (!selectedId) return;
291
+
292
+ const model = models.find((candidate) => candidate.id === selectedId);
293
+ if (!model) return;
294
+ const layers = loadModelOverrideLayers(ctx.cwd);
295
+ const globalOverride = layers.global[model.id] ?? {};
296
+ const original: ProviderModelOverrideLayer = layers.project[model.id] ?? {};
297
+ const edited: ProviderModelOverrideLayer = { ...original };
298
+ const selectAuto = (field: keyof ProviderModelOverrideLayer) => {
299
+ if (globalOverride[field] === undefined) delete edited[field];
300
+ else edited[field] = null;
301
+ };
302
+
303
+ await ctx.ui.custom<void>((tui, theme, _keybindings, done) => {
304
+ const container = new Container();
305
+ container.addChild(new DynamicBorder((text) => theme.fg("border", text)));
306
+ container.addChild(new Text(theme.fg("accent", theme.bold(model.id)), 1, 0));
307
+ container.addChild(new Text(theme.fg("accent", `Strict tool schema: ${strictModeLabel(model)}`), 1, 0));
308
+ for (const line of details(model)) container.addChild(new Text(theme.fg("muted", line), 1, 0));
309
+
310
+ const onChange = (id: string, value: string) => {
311
+ if (id === "reasoning") {
312
+ if (value === "auto") selectAuto("reasoning");
313
+ else edited.reasoning = value === "on";
314
+ }
315
+ if (id === "contextWindow") {
316
+ if (value === "auto") selectAuto("contextWindow");
317
+ else edited.contextWindow = Number(value);
318
+ }
319
+ if (id === "maxTokens") {
320
+ if (value === "auto") selectAuto("maxTokens");
321
+ else edited.maxTokens = Number(value);
322
+ }
323
+ list.updateValue(id, value);
324
+ tui.requestRender();
325
+ };
326
+ const list = new SettingsList(
327
+ detailItems(model, edited),
328
+ 8,
329
+ getSettingsListTheme(),
330
+ onChange,
331
+ () => done(undefined),
332
+ { enableSearch: true },
333
+ );
334
+ container.addChild(list);
335
+ container.addChild(new Text(theme.fg("dim", "Enter/Space changes Esc saves and reloads Pi"), 1, 0));
336
+ container.addChild(new DynamicBorder((text) => theme.fg("border", text)));
337
+ return {
338
+ render: (width) => container.render(width),
339
+ invalidate: () => { container.invalidate(); list.invalidate(); },
340
+ handleInput: (data) => list.handleInput(data),
341
+ };
342
+ });
343
+
344
+ if (JSON.stringify(original) === JSON.stringify(edited)) return;
345
+ try {
346
+ const saved = saveModelOverride(ctx.cwd, model.id, edited);
347
+ ctx.ui.notify(`Saved overrides for ${model.id} to ${saved.path}. Reloading Pi...`, "info");
348
+ await ctx.reload();
349
+ } catch (error) {
350
+ const message = error instanceof Error ? error.message : String(error);
351
+ ctx.ui.notify(`Could not save model overrides: ${message}`, "error");
352
+ }
353
+ }