pi-cliproxyapi-provider 0.4.13 → 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/README.md +37 -3
- package/package.json +7 -2
- package/src/catalog.ts +1 -0
- package/src/commands.ts +42 -5
- package/src/config.ts +119 -3
- package/src/model-ui.ts +353 -0
- package/src/provider.ts +27 -3
- package/src/settings.ts +41 -5
- package/src/types.ts +16 -0
package/README.md
CHANGED
|
@@ -70,7 +70,7 @@ CLIPROXYAPI_AUTH_HEADER
|
|
|
70
70
|
CLIPROXYAPI_MODELS_DEV_ENABLED
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
-
Project config
|
|
73
|
+
Project config supports metadata aliases and bounded per-model overrides. Connection and auth settings such as `baseUrl`, `providerName`, `authRequired`, `authHeader`, and `headers` must be set in global config or environment variables.
|
|
74
74
|
|
|
75
75
|
### GPT-5.6 context window
|
|
76
76
|
|
|
@@ -91,7 +91,22 @@ The same setting can be placed in project `.pi/settings.json`; project settings
|
|
|
91
91
|
- `"canonical"` (default): advertise `272000` tokens and compact at Pi's conservative boundary.
|
|
92
92
|
- `"full"`: advertise the models.dev context limit, allowing Pi to retain substantially more history before compaction.
|
|
93
93
|
|
|
94
|
-
Use `"full"` only when the selected CLIProxyAPI route and upstream account actually support that limit. Requests above `272000` input tokens also use the higher models.dev context-pricing tier where one is defined.
|
|
94
|
+
Use `"full"` only when the selected CLIProxyAPI route and upstream account actually support that limit. Requests above `272000` input tokens also use the higher models.dev context-pricing tier where one is defined.
|
|
95
|
+
|
|
96
|
+
### Model and display configuration
|
|
97
|
+
|
|
98
|
+
Run `/cliproxyapi config` in Pi TUI mode to edit every package-level `settings.json` value. The tabbed panel has `Connection`, `Models`, and `Display` sections; it controls the GPT-5.6 context-window mode and whether the model selector shows the published strict tool-schema capability.
|
|
99
|
+
|
|
100
|
+
```json
|
|
101
|
+
{
|
|
102
|
+
"pi-cliproxyapi-provider": {
|
|
103
|
+
"gpt56ContextWindow": "canonical",
|
|
104
|
+
"showStrictMode": false
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`showStrictMode` defaults to `false` because the selector stays compact for normal use. Enable it when diagnosing tool-schema behavior; model details always show `Strict tool schema` explicitly. Saving through `/cliproxyapi config` reloads Pi. Select `Connection` to open the endpoint and authentication editor.
|
|
95
110
|
|
|
96
111
|
## Authenticate
|
|
97
112
|
|
|
@@ -122,6 +137,9 @@ export CLIPROXYAPI_API_KEY=your-key
|
|
|
122
137
|
/cliproxyapi refresh models # refresh CLIProxyAPI availability only
|
|
123
138
|
/cliproxyapi refresh metadata # refresh models.dev metadata only
|
|
124
139
|
/cliproxyapi aliases # show unmatched model IDs for metadata aliases
|
|
140
|
+
/cliproxyapi models # inspect effective model settings and set bounded overrides
|
|
141
|
+
/cliproxyapi config # tabbed connection, model, and display configuration
|
|
142
|
+
/cliproxyapi config connection # open endpoint and authentication editor
|
|
125
143
|
```
|
|
126
144
|
|
|
127
145
|
## Metadata aliases
|
|
@@ -142,17 +160,33 @@ Add project aliases manually to:
|
|
|
142
160
|
.pi/pi-cliproxyapi-provider/config.json
|
|
143
161
|
```
|
|
144
162
|
|
|
145
|
-
Project config
|
|
163
|
+
Project config reads `modelAliases` and `modelOverrides`; other fields are ignored.
|
|
146
164
|
|
|
147
165
|
```json
|
|
148
166
|
{
|
|
149
167
|
"modelAliases": {
|
|
150
168
|
"claude-opus-4-6-thinking": "anthropic/claude-opus-4-6",
|
|
151
169
|
"gpt-5.6-sol": "openai/gpt-5.6-sol"
|
|
170
|
+
},
|
|
171
|
+
"modelOverrides": {
|
|
172
|
+
"gpt-5.6-sol": {
|
|
173
|
+
"contextWindow": 512000,
|
|
174
|
+
"maxTokens": 32768
|
|
175
|
+
}
|
|
152
176
|
}
|
|
153
177
|
}
|
|
154
178
|
```
|
|
155
179
|
|
|
180
|
+
## Model inspector and overrides
|
|
181
|
+
|
|
182
|
+
Run `/cliproxyapi models` in Pi TUI mode to inspect the models in the current CPA snapshot. The selector shows the effective API, reasoning mode, and context window. The detail view also shows input modalities, cost, thinking levels, and the compatibility values that Pi will publish.
|
|
183
|
+
|
|
184
|
+
Only `reasoning`, `contextWindow`, and `maxTokens` are editable. Values are constrained to safe presets; choose `auto` to remove an override and restore the derived value after reload. API routing and compatibility stay provider-owned: GPT-5.6/Codex models remain on `openai-responses`, while the CLIProxyAPI workaround publishes `supportsStrictMode: false`.
|
|
185
|
+
|
|
186
|
+
For CPA Responses requests, the extension also applies the Codex-compatible function-tool wire contract used by `pi-codex-conversion`: each function tool explicitly carries `strict: null`. This preserves optional tool arguments such as `interactive_shell.listBackground` without replacing CPA authentication, transport, discovery, or streaming with the ChatGPT-backed `openai-codex-responses` provider.
|
|
187
|
+
|
|
188
|
+
Saving writes to an existing project config when present, otherwise to the global provider config, then reloads Pi. Project overrides take precedence over global overrides field by field.
|
|
189
|
+
|
|
156
190
|
## Snapshots and startup
|
|
157
191
|
|
|
158
192
|
```text
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-cliproxyapi-provider",
|
|
3
|
-
"version": "0.4.
|
|
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
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
|
|
134
|
-
|
|
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
|
-
|
|
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 {
|
|
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
|
+
}
|
package/src/model-ui.ts
ADDED
|
@@ -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/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 {
|
|
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
|
|
|
@@ -125,6 +131,20 @@ function emptyMatchMethods(): Record<MetadataMatchMethod, number> {
|
|
|
125
131
|
};
|
|
126
132
|
}
|
|
127
133
|
|
|
134
|
+
function applyModelOverride(
|
|
135
|
+
model: ProviderModelConfigLike,
|
|
136
|
+
overrides: ProviderModelOverrides,
|
|
137
|
+
): ProviderModelConfigLike {
|
|
138
|
+
const override = overrides[model.id];
|
|
139
|
+
if (!override) return model;
|
|
140
|
+
return {
|
|
141
|
+
...model,
|
|
142
|
+
...(override.reasoning !== undefined ? { reasoning: override.reasoning } : {}),
|
|
143
|
+
...(override.contextWindow !== undefined ? { contextWindow: override.contextWindow } : {}),
|
|
144
|
+
...(override.maxTokens !== undefined ? { maxTokens: override.maxTokens } : {}),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
128
148
|
export function buildUnavailableProviderModels(id = "login-required"): ProviderModelConfigLike[] {
|
|
129
149
|
return [{ id, name: id, ...cloneModelDefaults() }];
|
|
130
150
|
}
|
|
@@ -134,6 +154,7 @@ export function buildProviderModels(
|
|
|
134
154
|
catalog: ModelsDevCatalog,
|
|
135
155
|
aliases: Record<string, string>,
|
|
136
156
|
gpt56ContextWindow: Gpt56ContextWindowMode = "canonical",
|
|
157
|
+
overrides: ProviderModelOverrides = {},
|
|
137
158
|
): BuildProviderModelsResult {
|
|
138
159
|
const matchMethods = emptyMatchMethods();
|
|
139
160
|
const unmatchedModelIds: string[] = [];
|
|
@@ -143,12 +164,15 @@ export function buildProviderModels(
|
|
|
143
164
|
const match = findMetadataMatch(cpaModel, catalog, aliases);
|
|
144
165
|
if (!match) {
|
|
145
166
|
unmatchedModelIds.push(cpaModel.id);
|
|
146
|
-
return defaultModel(cpaModel, gpt56ContextWindow);
|
|
167
|
+
return applyModelOverride(defaultModel(cpaModel, gpt56ContextWindow), overrides);
|
|
147
168
|
}
|
|
148
169
|
|
|
149
170
|
enriched += 1;
|
|
150
171
|
matchMethods[match.method] += 1;
|
|
151
|
-
return
|
|
172
|
+
return applyModelOverride(
|
|
173
|
+
modelFromMetadata(cpaModel, match.metadata, gpt56ContextWindow),
|
|
174
|
+
overrides,
|
|
175
|
+
);
|
|
152
176
|
});
|
|
153
177
|
|
|
154
178
|
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
|
|
24
|
-
|
|
25
|
-
if (
|
|
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
|
-
|
|
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;
|
|
@@ -11,6 +26,7 @@ export interface CpaProviderConfig {
|
|
|
11
26
|
headers: Record<string, string>;
|
|
12
27
|
modelsDevEnabled: boolean;
|
|
13
28
|
modelAliases: Record<string, string>;
|
|
29
|
+
modelOverrides: ProviderModelOverrides;
|
|
14
30
|
}
|
|
15
31
|
|
|
16
32
|
export interface ModelsDevMetadata {
|