pi-cliproxyapi-provider 0.4.13 → 0.15.0

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.
@@ -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
+ }
package/src/models-dev.ts CHANGED
@@ -22,7 +22,8 @@ export function parseModelsDevCatalog(payload: unknown): ModelsDevCatalog {
22
22
  for (const [modelId, metadata] of Object.entries(provider.models as Record<string, unknown>)) {
23
23
  if (!isMetadata(metadata)) continue;
24
24
  const canonicalId = metadata.id.includes("/") ? metadata.id : `${key}/${modelId}`;
25
- catalog[canonicalId] = { ...metadata, id: canonicalId };
25
+ const catalogKey = canonicalId.startsWith(`${key}/`) ? canonicalId : `${key}/${canonicalId}`;
26
+ catalog[catalogKey] = { ...metadata, id: canonicalId, sourceProvider: key };
26
27
  }
27
28
  continue;
28
29
  }
@@ -48,6 +49,10 @@ export async function fetchModelsDevCatalog(timeoutMs?: number, signal?: AbortSi
48
49
  }, timeoutMs, "models.dev fetch", signal);
49
50
  }
50
51
 
52
+ export function hasSourceProviderMetadata(catalog: ModelsDevCatalog): boolean {
53
+ return Object.values(catalog).every((metadata) => typeof metadata.sourceProvider === "string");
54
+ }
55
+
51
56
  export async function readBundledModelsDevFallback(path: string): Promise<ModelsDevCatalog> {
52
57
  return parseModelsDevCatalog(JSON.parse(await readFile(path, "utf8")));
53
58
  }
package/src/provider.ts CHANGED
@@ -3,7 +3,13 @@ import { findMetadataMatch, type MetadataMatchMethod } from "./matching.ts";
3
3
  import { getModelApiOverride, isGpt56Model, type ModelApiContext } from "./model-api.ts";
4
4
  import { getModelCapabilityOverrides } from "./model-capabilities.ts";
5
5
  import type { Gpt56ContextWindowMode } from "./settings.ts";
6
- import type { InputModality, ModelsDevCatalog, ModelsDevMetadata, ProviderModelConfigLike } from "./types.ts";
6
+ import type {
7
+ InputModality,
8
+ ModelsDevCatalog,
9
+ ModelsDevMetadata,
10
+ ProviderModelConfigLike,
11
+ ProviderModelOverrides,
12
+ } from "./types.ts";
7
13
 
8
14
  export const GPT_5_6_CANONICAL_CONTEXT_WINDOW = 272000;
9
15
 
@@ -120,8 +126,24 @@ function emptyMatchMethods(): Record<MetadataMatchMethod, number> {
120
126
  alias: 0,
121
127
  exact: 0,
122
128
  "owner-prefix": 0,
129
+ "owner-hint": 0,
123
130
  suffix: 0,
124
131
  "normalized-suffix": 0,
132
+ "provider-fallback": 0,
133
+ };
134
+ }
135
+
136
+ function applyModelOverride(
137
+ model: ProviderModelConfigLike,
138
+ overrides: ProviderModelOverrides,
139
+ ): ProviderModelConfigLike {
140
+ const override = overrides[model.id];
141
+ if (!override) return model;
142
+ return {
143
+ ...model,
144
+ ...(override.reasoning !== undefined ? { reasoning: override.reasoning } : {}),
145
+ ...(override.contextWindow !== undefined ? { contextWindow: override.contextWindow } : {}),
146
+ ...(override.maxTokens !== undefined ? { maxTokens: override.maxTokens } : {}),
125
147
  };
126
148
  }
127
149
 
@@ -134,21 +156,26 @@ export function buildProviderModels(
134
156
  catalog: ModelsDevCatalog,
135
157
  aliases: Record<string, string>,
136
158
  gpt56ContextWindow: Gpt56ContextWindowMode = "canonical",
159
+ overrides: ProviderModelOverrides = {},
160
+ metadataFallbackProvider: string | null = "openrouter",
137
161
  ): BuildProviderModelsResult {
138
162
  const matchMethods = emptyMatchMethods();
139
163
  const unmatchedModelIds: string[] = [];
140
164
  let enriched = 0;
141
165
 
142
166
  const models = cpaModels.map((cpaModel) => {
143
- const match = findMetadataMatch(cpaModel, catalog, aliases);
167
+ const match = findMetadataMatch(cpaModel, catalog, aliases, metadataFallbackProvider);
144
168
  if (!match) {
145
169
  unmatchedModelIds.push(cpaModel.id);
146
- return defaultModel(cpaModel, gpt56ContextWindow);
170
+ return applyModelOverride(defaultModel(cpaModel, gpt56ContextWindow), overrides);
147
171
  }
148
172
 
149
173
  enriched += 1;
150
174
  matchMethods[match.method] += 1;
151
- return modelFromMetadata(cpaModel, match.metadata, gpt56ContextWindow);
175
+ return applyModelOverride(
176
+ modelFromMetadata(cpaModel, match.metadata, gpt56ContextWindow),
177
+ overrides,
178
+ );
152
179
  });
153
180
 
154
181
  return {
package/src/settings.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { SettingsManager } from "@earendil-works/pi-coding-agent";
1
+ import { CONFIG_DIR_NAME, getAgentDir, SettingsManager } from "@earendil-works/pi-coding-agent";
2
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
3
+ import { dirname, join } from "node:path";
2
4
 
3
5
  export const PROVIDER_SETTINGS_NAMESPACE = "pi-cliproxyapi-provider";
4
6
 
@@ -6,10 +8,12 @@ export type Gpt56ContextWindowMode = "canonical" | "full";
6
8
 
7
9
  export interface ProviderSettings {
8
10
  gpt56ContextWindow: Gpt56ContextWindowMode;
11
+ showStrictMode: boolean;
9
12
  }
10
13
 
11
14
  export const DEFAULT_PROVIDER_SETTINGS: ProviderSettings = {
12
15
  gpt56ContextWindow: "canonical",
16
+ showStrictMode: false,
13
17
  };
14
18
 
15
19
  function parseSettingsLayer(settings: unknown, scope: string): Partial<ProviderSettings> {
@@ -20,14 +24,20 @@ function parseSettingsLayer(settings: unknown, scope: string): Partial<ProviderS
20
24
  throw new Error(`${PROVIDER_SETTINGS_NAMESPACE} must be an object in ${scope} settings.json`);
21
25
  }
22
26
 
23
- const value = (namespace as Record<string, unknown>).gpt56ContextWindow;
24
- if (value === undefined) return {};
25
- if (value !== "canonical" && value !== "full") {
27
+ const record = namespace as Record<string, unknown>;
28
+ const gpt56ContextWindow = record.gpt56ContextWindow;
29
+ if (gpt56ContextWindow !== undefined && gpt56ContextWindow !== "canonical" && gpt56ContextWindow !== "full") {
26
30
  throw new Error(
27
31
  `${PROVIDER_SETTINGS_NAMESPACE}.gpt56ContextWindow must be "canonical" or "full" in ${scope} settings.json`,
28
32
  );
29
33
  }
30
- return { gpt56ContextWindow: value };
34
+ if (record.showStrictMode !== undefined && typeof record.showStrictMode !== "boolean") {
35
+ throw new Error(`${PROVIDER_SETTINGS_NAMESPACE}.showStrictMode must be a boolean in ${scope} settings.json`);
36
+ }
37
+ return {
38
+ ...(gpt56ContextWindow !== undefined ? { gpt56ContextWindow } : {}),
39
+ ...(record.showStrictMode !== undefined ? { showStrictMode: record.showStrictMode } : {}),
40
+ };
31
41
  }
32
42
 
33
43
  export function loadProviderSettings(cwd: string, agentDir?: string): ProviderSettings {
@@ -38,3 +48,29 @@ export function loadProviderSettings(cwd: string, agentDir?: string): ProviderSe
38
48
  ...parseSettingsLayer(manager.getProjectSettings(), "project"),
39
49
  };
40
50
  }
51
+
52
+ function settingsPath(cwd: string): string {
53
+ const projectPath = join(cwd, CONFIG_DIR_NAME, "settings.json");
54
+ return existsSync(projectPath) ? projectPath : join(getAgentDir(), "settings.json");
55
+ }
56
+
57
+ export function saveProviderSettings(cwd: string, patch: Partial<ProviderSettings>): string {
58
+ const path = settingsPath(cwd);
59
+ let root: Record<string, unknown> = {};
60
+ if (existsSync(path)) {
61
+ const parsed: unknown = JSON.parse(readFileSync(path, "utf8"));
62
+ if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
63
+ throw new Error(`Settings file must contain a JSON object: ${path}`);
64
+ }
65
+ root = parsed as Record<string, unknown>;
66
+ }
67
+
68
+ const existing = parseSettingsLayer(root, path);
69
+ const next = { ...existing, ...patch };
70
+ mkdirSync(dirname(path), { recursive: true, mode: 0o700 });
71
+ writeFileSync(path, `${JSON.stringify({ ...root, [PROVIDER_SETTINGS_NAMESPACE]: next }, null, 2)}\n`, {
72
+ encoding: "utf8",
73
+ mode: 0o600,
74
+ });
75
+ return path;
76
+ }
package/src/types.ts CHANGED
@@ -3,6 +3,21 @@ import type { ProviderModelConfig } from "@earendil-works/pi-coding-agent";
3
3
 
4
4
  export type InputModality = "text" | "image";
5
5
 
6
+ export interface ProviderModelOverride {
7
+ reasoning?: boolean;
8
+ contextWindow?: number;
9
+ maxTokens?: number;
10
+ }
11
+
12
+ export interface ProviderModelOverrideLayer {
13
+ reasoning?: boolean | null;
14
+ contextWindow?: number | null;
15
+ maxTokens?: number | null;
16
+ }
17
+
18
+ export type ProviderModelOverrides = Record<string, ProviderModelOverride>;
19
+ export type ProviderModelOverrideLayers = Record<string, ProviderModelOverrideLayer>;
20
+
6
21
  export interface CpaProviderConfig {
7
22
  providerName: string;
8
23
  baseUrl: string;
@@ -10,11 +25,15 @@ export interface CpaProviderConfig {
10
25
  authHeader: boolean;
11
26
  headers: Record<string, string>;
12
27
  modelsDevEnabled: boolean;
28
+ metadataFallbackProvider: string | null;
13
29
  modelAliases: Record<string, string>;
30
+ modelOverrides: ProviderModelOverrides;
14
31
  }
15
32
 
16
33
  export interface ModelsDevMetadata {
17
34
  id: string;
35
+ /** models.dev provider key retained for owner-hint matching. */
36
+ sourceProvider?: string;
18
37
  name?: string;
19
38
  reasoning?: boolean;
20
39
  modalities?: {