@jadelclemens/pi-model-map 0.1.0 → 0.1.1
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 +20 -31
- package/examples/cursor.json +0 -3
- package/package.json +3 -3
- package/src/catalog.ts +23 -57
- package/src/constants.ts +0 -8
- package/src/index.ts +61 -191
- package/src/mapping.ts +2 -20
- package/src/warnings.ts +2 -72
package/README.md
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
# pi-model-map
|
|
1
|
+
# @jadelclemens/pi-model-map
|
|
2
2
|
|
|
3
3
|
JSON-configurable Pi extension that collapses provider-specific baked-in effort/thinking/fast model variants into canonical model IDs, controlled by Pi's native effort selector plus two extra mapping-only toggles: `/pi-model-map-thinking` and `/pi-model-map-fast`.
|
|
4
4
|
|
|
5
5
|
> **Terminology note:** this README calls Pi's `/thinking` selector **"effort"** throughout (as most other harnesses do — `low`/`medium`/`high`/etc.), to avoid confusion with the separate "thinking mode" axis described below.
|
|
6
6
|
|
|
7
|
-
The core resolution function in [`src/mapping.ts`](src/mapping.ts) — the canonical-id/effort-level → backend-id lookup — is ported from `@open-cursor/pi-agent`'s `toCursorId()` / `MODEL_MAP` index-building in [`src/models/mapping.ts`](https://github.com/brandonkramer/open-cursor/blob/main/integrations/pi-agent/src/models/mapping.ts). Everything else — the JSON config format, the `mappings`/`map` schema (including the `default`/`thinking`/`fast`/`thinking-fast` variant axes), multi-provider `"provider/modelId"` delegate references, loading multiple config files from a `model-map/` directory, per-
|
|
7
|
+
The core resolution function in [`src/mapping.ts`](src/mapping.ts) — the canonical-id/effort-level → backend-id lookup — is ported from `@open-cursor/pi-agent`'s `toCursorId()` / `MODEL_MAP` index-building in [`src/models/mapping.ts`](https://github.com/brandonkramer/open-cursor/blob/main/integrations/pi-agent/src/models/mapping.ts). Everything else — the JSON config format, the `mappings`/`map` schema (including the `default`/`thinking`/`fast`/`thinking-fast` variant axes), multi-provider `"provider/modelId"` delegate references, loading multiple config files from a `model-map/` directory, per-family registration via Pi's `registerVirtualModel`, the `/pi-model-map-thinking` / `/pi-model-map-fast` commands, and the namespace-clash warnings — is original to pi-model-map and has no equivalent in open-cursor. See [Credits & License](#credits--license) below for the precise breakdown.
|
|
8
8
|
|
|
9
9
|
## Why this exists
|
|
10
10
|
|
|
@@ -50,14 +50,11 @@ Each file registers a separate provider namespace (e.g. `cursor/...`). Add anoth
|
|
|
50
50
|
|
|
51
51
|
```jsonc
|
|
52
52
|
{
|
|
53
|
-
// Provider
|
|
53
|
+
// Provider the virtual models are listed under in /model. Defaults to the filename stem
|
|
54
|
+
// (cursor.json → "cursor"). May be a provider that also has physical models; a family id
|
|
55
|
+
// equal to a physical model id of that provider hides the physical one.
|
|
54
56
|
"provider": "cursor",
|
|
55
57
|
|
|
56
|
-
// Pi catalog metadata (not used for auth — see below).
|
|
57
|
-
"api": "cursor-agent",
|
|
58
|
-
"baseUrl": "https://api2.cursor.sh",
|
|
59
|
-
"name": "Cursor (model map)",
|
|
60
|
-
|
|
61
58
|
// Optional: path to the delegate provider's live catalog cache, used only to warn if a
|
|
62
59
|
// backend id below has been renamed/retired upstream (see "Verifying backend ids still
|
|
63
60
|
// exist" below). "~" expands to $HOME; relative paths resolve against this config file.
|
|
@@ -70,7 +67,6 @@ Each file registers a separate provider namespace (e.g. `cursor/...`). Add anoth
|
|
|
70
67
|
"claude-opus-5": {
|
|
71
68
|
// Display/catalog metadata (all optional)
|
|
72
69
|
"name": "Claude Opus 5 (Cursor)",
|
|
73
|
-
"reasoning": true,
|
|
74
70
|
// "offSupported" overrides the automatic check for whether Pi's "off" thinking level
|
|
75
71
|
// means anything real for this family; see "Hiding a misleading 'off' level" below.
|
|
76
72
|
// "offSupported": false,
|
|
@@ -141,7 +137,7 @@ At request time, pi-model-map picks a key based on the current `/pi-model-map-th
|
|
|
141
137
|
```
|
|
142
138
|
cursor/claude-opus-5 + /thinking high + /pi-model-map-thinking on
|
|
143
139
|
→ map.high.thinking = "cursor-agent/claude-opus-5-thinking-high"
|
|
144
|
-
→
|
|
140
|
+
→ route() returns the physical model cursor-agent/claude-opus-5-thinking-high
|
|
145
141
|
```
|
|
146
142
|
|
|
147
143
|
This also means:
|
|
@@ -150,13 +146,15 @@ This also means:
|
|
|
150
146
|
- **Different levels, or different variants within one level, can in principle delegate to different providers**, though this isn't a typical use case.
|
|
151
147
|
- pi-model-map resolves every distinct provider referenced anywhere in `map` and validates/monitors each one independently (see warnings below).
|
|
152
148
|
|
|
153
|
-
###
|
|
154
|
-
|
|
155
|
-
Pi requires an `apiKey` (or `oauth`) when extension providers register models, even when `streamSimple` only delegates elsewhere. pi-model-map sets a placeholder for its **own** provider registration:
|
|
149
|
+
### Virtual models, not providers
|
|
156
150
|
|
|
157
|
-
`
|
|
151
|
+
Each canonical family is registered with Pi's `registerVirtualModel()`. Per request, `route()` resolves the backend and returns that **physical** model; Pi then streams it with the delegate's own credentials. Consequences:
|
|
158
152
|
|
|
159
|
-
|
|
153
|
+
- Assistant messages, `/session` cost, context limits and compaction use the physical backend. The footer shows `canonical • effort → backend`. Resuming a session restores the canonical selection.
|
|
154
|
+
- Backends must be physical models. A `map` entry pointing at another virtual model (e.g. `roundrobin/*`, `billing/*`) fails at request time.
|
|
155
|
+
- A backend that isn't registered (or lacks credentials) fails the request with a clear error. Families with no registered backend at all are unregistered on session start.
|
|
156
|
+
- `contextWindow`/`maxTokens`/`input` are only shown before the first response; per-family `reasoning` and `cost` no longer exist (Pi bills the backend's own cost).
|
|
157
|
+
- Config-level `api`, `baseUrl` and `name` fields are ignored.
|
|
160
158
|
|
|
161
159
|
### Naming convention
|
|
162
160
|
|
|
@@ -216,23 +214,14 @@ It's also still checked at request time as a final backstop (in case a session's
|
|
|
216
214
|
|
|
217
215
|
It tracks only the *most recently checked* model+level combination, not "every combination ever warned about this session" — so cycling `/thinking xhigh` -> `/thinking max` -> `/thinking xhigh` warns all three times (each is a genuine change from whatever was checked immediately before it), while sending several turns in a row without changing the model or effort level does not repeat the warning, since request-time checks see the same combination the preceding selection-time check already reported. Turning `/pi-model-map-thinking` on and back off again while staying on the same affected level also re-warns, since that's a real change in whether the toggle mismatch applies.
|
|
218
216
|
|
|
219
|
-
##
|
|
220
|
-
|
|
221
|
-
Pi's `registerProvider(id, …)` **replaces** any existing registration for that `id`.
|
|
222
|
-
|
|
223
|
-
| Your config | Safe? |
|
|
224
|
-
| --------------------------------------------------------------- | ------------------------------------------------------------------------------------------------ |
|
|
225
|
-
| `provider: "cursor"`, `map` values reference `cursor-agent/...` | Yes — distinct namespaces |
|
|
226
|
-
| `provider: "cursor-agent"` | **No** — replaces `@open-cursor/pi-agent`; any `map` entry referencing `cursor-agent/...` breaks |
|
|
217
|
+
## Startup checks
|
|
227
218
|
|
|
228
|
-
|
|
219
|
+
Per config file:
|
|
229
220
|
|
|
230
|
-
1. **Rejects** `provider`
|
|
231
|
-
2. **
|
|
232
|
-
3. **Warns** if `
|
|
233
|
-
4. **
|
|
234
|
-
5. **Warns** (only if `source` is set) about any backend id in `mappings` that's missing from the referenced live catalog file, or if that file can't be read/parsed as configured
|
|
235
|
-
6. **On session start**, verifies each distinct delegate provider referenced in `map` is registered, warns if delegation would call itself, and records any mapped backends that Pi's live registry has not registered (log file above)
|
|
221
|
+
1. **Rejects** duplicate `provider` ids across files in the same directory.
|
|
222
|
+
2. **Warns** if a single config file's `mappings` reference more than one distinct delegate provider overall, and separately (more specifically) if a *single mapping* splits its own effort levels/variants across more than one provider (see below).
|
|
223
|
+
3. **Warns** (only if `source` is set) about any backend id in `mappings` that's missing from the referenced live catalog file, or if that file can't be read/parsed as configured.
|
|
224
|
+
4. **On session start**, verifies each distinct delegate provider referenced in `map` is registered, records any mapped backends that Pi's live registry has not registered (log file above), and unregisters families with no registered backend.
|
|
236
225
|
|
|
237
226
|
### One config file, one delegate provider
|
|
238
227
|
|
|
@@ -263,7 +252,7 @@ PI_MODEL_MAP_DEBUG=1 pi
|
|
|
263
252
|
[pi-model-map:debug] cursor/claude-opus-5 (reasoning=high, thinking=true, fast=false) -> cursor-agent/claude-opus-5-thinking-high
|
|
264
253
|
```
|
|
265
254
|
|
|
266
|
-
This is off by default (silent, like every other Pi provider) and is printed via `console.error` from inside the extension's `
|
|
255
|
+
This is off by default (silent, like every other Pi provider) and is printed via `console.error` from inside the extension's `route()` handler — i.e. it reflects the exact backend reference `toBackendRef()` resolved and handed off to the delegate provider, not a guess. Each line is printed with a leading blank line, since Pi's TUI doesn't otherwise insert one before extension stderr output and it would otherwise print immediately after your echoed input on the same terminal line.
|
|
267
256
|
|
|
268
257
|
If you want to independently confirm the *actual outbound request* (not just what pi-model-map thinks it resolved to), Pi's own `before_provider_request` extension hook fires right before the payload is sent and includes the real serialized model/request body — see the "Extension Hooks" section of Pi's own `docs/extensions.md` for a ready-to-use snippet (e.g. `pi.on("before_provider_request", (event) => console.log(JSON.stringify(event.payload, null, 2)))`). Pi's hidden `/debug` command also writes the last messages sent to the LLM to `~/.pi/agent/pi-debug.log`.
|
|
269
258
|
|
package/examples/cursor.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jadelclemens/pi-model-map",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "JSON-configurable Pi extension that collapses baked-effort/thinking/fast model variants into canonical models with native effort selection and /pi-model-map-thinking, /pi-model-map-fast toggles",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.ts",
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"LICENSE"
|
|
37
37
|
],
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@earendil-works/pi-ai": "
|
|
40
|
-
"@earendil-works/pi-coding-agent": "
|
|
39
|
+
"@earendil-works/pi-ai": ">=0.99.0",
|
|
40
|
+
"@earendil-works/pi-coding-agent": ">=0.99.0"
|
|
41
41
|
},
|
|
42
42
|
"pi": {
|
|
43
43
|
"extensions": [
|
package/src/catalog.ts
CHANGED
|
@@ -1,63 +1,29 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ModelThinkingLevel } from "@earendil-works/pi-ai";
|
|
2
|
+
import type { ExtensionVirtualModel } from "@earendil-works/pi-coding-agent";
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
+
import type { ModelMapper } from "./mapping.js";
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
input: 0,
|
|
7
|
-
output: 0,
|
|
8
|
-
cacheRead: 0,
|
|
9
|
-
cacheWrite: 0,
|
|
10
|
-
};
|
|
6
|
+
export type VirtualModelMeta = Omit<ExtensionVirtualModel, "route">;
|
|
11
7
|
|
|
12
8
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
* (minimal..high), omitting the key means "supported via provider default" rather than
|
|
17
|
-
* "unsupported." Since pi-model-map families often only support a subset of levels (e.g. a
|
|
18
|
-
* family with only low/high/max backends), every unsupported level must be set to `null`
|
|
19
|
-
* explicitly so Pi's /thinking UI and pi.setThinkingLevel() clamping correctly restrict
|
|
20
|
-
* selection to what this family's "map" actually defines. See docs/models.md's
|
|
21
|
-
* "Thinking Level Map" section.
|
|
9
|
+
* Virtual-model metadata for one canonical family. `thinkingLevels` lists exactly what the
|
|
10
|
+
* family's `map` defines, plus `off` unless it has no real non-reasoning backend
|
|
11
|
+
* (see `ModelMapper.hasDistinctOffLevel` / `MappingEntry.offSupported`).
|
|
22
12
|
*/
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
mapper: ModelMapper,
|
|
40
|
-
api: Api,
|
|
41
|
-
baseUrl: string,
|
|
42
|
-
): Model<Api>[] => {
|
|
43
|
-
return mapper.getCanonicalFamilies().map((canonicalId) => {
|
|
44
|
-
const metadata = mapper.getModelMetadata(canonicalId);
|
|
45
|
-
const supportedLevels = mapper.getSupportedThinkingLevels(canonicalId);
|
|
46
|
-
const offSupported = metadata?.offSupported ?? mapper.hasDistinctOffLevel(canonicalId);
|
|
47
|
-
const thinkingLevelMap = buildThinkingLevelMap(supportedLevels, offSupported);
|
|
48
|
-
|
|
49
|
-
return {
|
|
50
|
-
id: canonicalId,
|
|
51
|
-
name: metadata?.name ?? canonicalId,
|
|
52
|
-
api,
|
|
53
|
-
provider: mapper.provider,
|
|
54
|
-
baseUrl,
|
|
55
|
-
reasoning: metadata?.reasoning ?? supportedLevels.length > 0,
|
|
56
|
-
input: metadata?.input ?? ["text", "image"],
|
|
57
|
-
cost: metadata?.cost ?? defaultCost,
|
|
58
|
-
contextWindow: metadata?.contextWindow ?? 1_000_000,
|
|
59
|
-
maxTokens: metadata?.maxTokens ?? 128_000,
|
|
60
|
-
thinkingLevelMap,
|
|
61
|
-
} satisfies Model<Api>;
|
|
62
|
-
});
|
|
13
|
+
export const buildVirtualModelMeta = (mapper: ModelMapper, canonicalId: string): VirtualModelMeta => {
|
|
14
|
+
const metadata = mapper.getModelMetadata(canonicalId);
|
|
15
|
+
const offSupported = metadata?.offSupported ?? mapper.hasDistinctOffLevel(canonicalId);
|
|
16
|
+
const levels: ModelThinkingLevel[] = [
|
|
17
|
+
...(offSupported ? (["off"] as const) : []),
|
|
18
|
+
...mapper.getSupportedThinkingLevels(canonicalId),
|
|
19
|
+
];
|
|
20
|
+
return {
|
|
21
|
+
provider: mapper.provider,
|
|
22
|
+
id: canonicalId,
|
|
23
|
+
name: metadata?.name ?? canonicalId,
|
|
24
|
+
thinkingLevels: levels.length > 0 ? levels : ["off"],
|
|
25
|
+
contextWindow: metadata?.contextWindow,
|
|
26
|
+
maxTokens: metadata?.maxTokens,
|
|
27
|
+
input: metadata?.input,
|
|
28
|
+
};
|
|
63
29
|
};
|
package/src/constants.ts
CHANGED
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pi requires an apiKey (or oauth) when extension providers register models, even when
|
|
3
|
-
* streamSimple only delegates to another provider that owns real authentication.
|
|
4
|
-
* This value is never used for API calls.
|
|
5
|
-
*/
|
|
6
|
-
export const FAKE_REGISTRATION_API_KEY =
|
|
7
|
-
"PI_MODEL_MAP_FAKE_KEY_NEEDED_FOR_PROVIDER_REGISTRATION";
|
|
8
|
-
|
|
9
1
|
export const CONFIG_DIR_NAME = "model-map";
|
|
10
2
|
|
|
11
3
|
/** Command name prefix for the /pi-model-map-* command family. */
|
package/src/index.ts
CHANGED
|
@@ -1,51 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {
|
|
3
|
-
Api,
|
|
4
|
-
AssistantMessageEventStream,
|
|
5
|
-
Model,
|
|
6
|
-
ModelThinkingLevel,
|
|
7
|
-
SimpleStreamOptions,
|
|
8
|
-
ThinkingLevel,
|
|
9
|
-
TranscriptContext,
|
|
10
|
-
} from "@earendil-works/pi-ai";
|
|
1
|
+
import type { ModelThinkingLevel, ThinkingLevel } from "@earendil-works/pi-ai";
|
|
11
2
|
import type {
|
|
12
3
|
ExtensionAPI,
|
|
13
4
|
ExtensionCommandContext,
|
|
14
5
|
ExtensionContext,
|
|
15
6
|
} from "@earendil-works/pi-coding-agent";
|
|
16
7
|
|
|
17
|
-
import {
|
|
8
|
+
import { buildVirtualModelMeta } from "./catalog.js";
|
|
18
9
|
import { createModelMappers } from "./config.js";
|
|
19
|
-
import { COMMAND_PREFIX
|
|
10
|
+
import { COMMAND_PREFIX } from "./constants.js";
|
|
20
11
|
import { logFilePath, pluginWarn } from "./log.js";
|
|
21
12
|
import type { ModelMapper } from "./mapping.js";
|
|
22
13
|
import { checkAgainstSource } from "./source-check.js";
|
|
23
14
|
import { warnOnSessionStart } from "./warnings.js";
|
|
24
15
|
|
|
25
|
-
type DelegateProvider = {
|
|
26
|
-
streamSimple?: (
|
|
27
|
-
model: Model<Api>,
|
|
28
|
-
context: TranscriptContext,
|
|
29
|
-
options?: SimpleStreamOptions,
|
|
30
|
-
) => AssistantMessageEventStream;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
type Registration = {
|
|
34
|
-
mapper: ModelMapper;
|
|
35
|
-
streamMapped: (
|
|
36
|
-
model: Model<Api>,
|
|
37
|
-
context: TranscriptContext,
|
|
38
|
-
options?: SimpleStreamOptions,
|
|
39
|
-
) => AssistantMessageEventStream;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
/** Drop credentials Pi resolved for this virtual mapping provider so nested registry streaming can re-resolve the delegate. */
|
|
43
|
-
const withoutVirtualProviderAuth = (options?: SimpleStreamOptions): SimpleStreamOptions | undefined => {
|
|
44
|
-
if (!options) return options;
|
|
45
|
-
const { apiKey: _apiKey, headers: _headers, env: _env, ...rest } = options;
|
|
46
|
-
return rest;
|
|
47
|
-
};
|
|
48
|
-
|
|
49
16
|
/** ModelThinkingLevel's "off" isn't a real ThinkingLevel -- normalize it to undefined for the mapper APIs that expect `ThinkingLevel | undefined`. */
|
|
50
17
|
const asReasoningLevel = (level: ModelThinkingLevel | undefined): ThinkingLevel | undefined =>
|
|
51
18
|
level && level !== "off" ? level : undefined;
|
|
@@ -72,7 +39,6 @@ export default (pi: ExtensionAPI) => {
|
|
|
72
39
|
const mappers = createModelMappers();
|
|
73
40
|
let lastCtx: ExtensionContext | null = null;
|
|
74
41
|
let warnedOnSession = false;
|
|
75
|
-
const registrations: Registration[] = [];
|
|
76
42
|
|
|
77
43
|
// Session-wide mapping toggles. These do not add or emulate any real API parameter —
|
|
78
44
|
// they only pick which pre-baked backend model ID a "provider/modelId" map entry
|
|
@@ -82,34 +48,9 @@ export default (pi: ExtensionAPI) => {
|
|
|
82
48
|
let thinkingEnabled = false;
|
|
83
49
|
let fastEnabled = false;
|
|
84
50
|
|
|
85
|
-
const assertDelegateAvailable = (
|
|
86
|
-
mapper: ModelMapper,
|
|
87
|
-
delegateProvider: string,
|
|
88
|
-
ownStream: Registration["streamMapped"],
|
|
89
|
-
): void => {
|
|
90
|
-
const delegate = lastCtx?.modelRegistry.getProvider(
|
|
91
|
-
delegateProvider,
|
|
92
|
-
) as DelegateProvider | undefined;
|
|
93
|
-
|
|
94
|
-
if (delegate?.streamSimple === ownStream) {
|
|
95
|
-
throw new Error(
|
|
96
|
-
`[pi-model-map] Cannot delegate to "${delegateProvider}" (${mapper.source}) because this mapping ` +
|
|
97
|
-
`replaced that provider namespace (provider="${mapper.provider}"). ` +
|
|
98
|
-
`Use a unique provider name and keep the "provider/modelId" delegate as "${delegateProvider}/...".`,
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
if (!delegate?.streamSimple) {
|
|
103
|
-
throw new Error(
|
|
104
|
-
`[pi-model-map] Delegate provider "${delegateProvider}" is not available for ${mapper.provider} (${mapper.source}). ` +
|
|
105
|
-
`Install and authenticate the delegate connector first.`,
|
|
106
|
-
);
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
|
|
110
51
|
/**
|
|
111
52
|
* Warn if `canonicalId` at effort level `reasoning` has no non-thinking backend at all, so
|
|
112
|
-
* `/pi-model-map-thinking off` is silently overridden. Shared by
|
|
53
|
+
* `/pi-model-map-thinking off` is silently overridden. Shared by route() (checked on every
|
|
113
54
|
* request) and the model_select / thinking_level_select handlers (checked the moment the
|
|
114
55
|
* selection changes, before any request is ever sent).
|
|
115
56
|
*
|
|
@@ -141,7 +82,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
141
82
|
|
|
142
83
|
/**
|
|
143
84
|
* Warn if `canonicalId` was asked for an effort level that has no entry at all in its map, so
|
|
144
|
-
* `toBackendRef` silently falls back to the "default" level instead. Shared by
|
|
85
|
+
* `toBackendRef` silently falls back to the "default" level instead. Shared by route()
|
|
145
86
|
* (checked on every request -- the only place this is guaranteed to see the level that will
|
|
146
87
|
* actually be sent, since a wrapper like pi-provider-manager's roundrobin can pick a different
|
|
147
88
|
* candidate per request without this extension ever seeing a model_select/thinking_level_select
|
|
@@ -172,110 +113,45 @@ export default (pi: ExtensionAPI) => {
|
|
|
172
113
|
);
|
|
173
114
|
};
|
|
174
115
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
);
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
);
|
|
198
|
-
}
|
|
199
|
-
assertDelegateAvailable(mapper, ref.provider, streamMapped);
|
|
200
|
-
const registered = lastCtx?.modelRegistry.find(ref.provider, ref.id);
|
|
201
|
-
const backendModel: Model<Api> = registered
|
|
202
|
-
? { ...registered, id: ref.id, provider: ref.provider }
|
|
203
|
-
: { ...model, id: ref.id, provider: ref.provider };
|
|
204
|
-
|
|
205
|
-
// Nested dispatch through ModelRegistry.streamSimple resolves the delegate's
|
|
206
|
-
// request-time auth (including headers and baseUrl). Incoming options.apiKey is
|
|
207
|
-
// this mapping provider's placeholder registration key and must not be forwarded.
|
|
208
|
-
const out = createAssistantMessageEventStream();
|
|
209
|
-
void (async () => {
|
|
210
|
-
try {
|
|
211
|
-
const ctx = lastCtx;
|
|
212
|
-
if (!ctx) {
|
|
213
|
-
throw new Error(
|
|
214
|
-
`[pi-model-map] No session context to stream ${ref.provider}/${ref.id} (${mapper.source}).`,
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
if (DEBUG && !registered) {
|
|
218
|
-
console.error(
|
|
219
|
-
`\n[pi-model-map:debug] no registry metadata for ${ref.provider}/${ref.id}; ` +
|
|
220
|
-
`delegating with the virtual model's api/baseUrl`,
|
|
221
|
-
);
|
|
222
|
-
}
|
|
223
|
-
const inner = ctx.modelRegistry.streamSimple(
|
|
224
|
-
backendModel,
|
|
225
|
-
context,
|
|
226
|
-
withoutVirtualProviderAuth(options),
|
|
116
|
+
/** Provider ids may be shared with physical models, so match on the family id too. */
|
|
117
|
+
const mapperFor = (provider: string | undefined, id: string): ModelMapper | undefined =>
|
|
118
|
+
mappers.find((m) => m.provider === provider && m.getCanonicalFamilies().includes(id));
|
|
119
|
+
|
|
120
|
+
const registerFamily = (mapper: ModelMapper, id: string): void => {
|
|
121
|
+
pi.registerVirtualModel({
|
|
122
|
+
...buildVirtualModelMeta(mapper, id),
|
|
123
|
+
route: (request, ctx) => {
|
|
124
|
+
lastCtx = ctx;
|
|
125
|
+
const reasoning = asReasoningLevel(request.thinkingLevel);
|
|
126
|
+
const ref = mapper.toBackendRef(id, reasoning, thinkingEnabled, fastEnabled);
|
|
127
|
+
if (!ref) {
|
|
128
|
+
throw new Error(`[pi-model-map] No mapping found for "${id}" in ${mapper.source}.`);
|
|
129
|
+
}
|
|
130
|
+
checkThinkingOnlyLevel(mapper, id, reasoning);
|
|
131
|
+
checkUnmappedLevel(mapper, id, reasoning);
|
|
132
|
+
if (DEBUG) {
|
|
133
|
+
// Leading "\n": Pi's TUI doesn't insert a newline before extension stderr output.
|
|
134
|
+
console.error(
|
|
135
|
+
`\n[pi-model-map:debug] ${mapper.provider}/${id} ` +
|
|
136
|
+
`(reasoning=${reasoning ?? "default"}, thinking=${thinkingEnabled}, fast=${fastEnabled}) ` +
|
|
137
|
+
`-> ${ref.provider}/${ref.id}`,
|
|
227
138
|
);
|
|
228
|
-
for await (const event of inner) {
|
|
229
|
-
out.push(event);
|
|
230
|
-
}
|
|
231
|
-
} catch (error) {
|
|
232
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
233
|
-
out.push({
|
|
234
|
-
type: "error",
|
|
235
|
-
reason: "error",
|
|
236
|
-
error: {
|
|
237
|
-
role: "assistant",
|
|
238
|
-
content: [],
|
|
239
|
-
api: backendModel.api,
|
|
240
|
-
provider: backendModel.provider,
|
|
241
|
-
model: backendModel.id,
|
|
242
|
-
usage: {
|
|
243
|
-
input: 0,
|
|
244
|
-
output: 0,
|
|
245
|
-
cacheRead: 0,
|
|
246
|
-
cacheWrite: 0,
|
|
247
|
-
totalTokens: 0,
|
|
248
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
249
|
-
},
|
|
250
|
-
stopReason: "error",
|
|
251
|
-
errorMessage: `[pi-model-map] ${message}`,
|
|
252
|
-
timestamp: Date.now(),
|
|
253
|
-
},
|
|
254
|
-
});
|
|
255
139
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
apiKey: FAKE_REGISTRATION_API_KEY,
|
|
266
|
-
baseUrl: mapper.baseUrl,
|
|
267
|
-
models: buildProviderModels(mapper, mapper.api, mapper.baseUrl),
|
|
268
|
-
streamSimple: streamMapped,
|
|
140
|
+
const model = ctx.modelRegistry.find(ref.provider, ref.id);
|
|
141
|
+
if (!model) {
|
|
142
|
+
throw new Error(
|
|
143
|
+
`[pi-model-map] Backend ${ref.provider}/${ref.id} for ${mapper.provider}/${id} is not registered ` +
|
|
144
|
+
`(${mapper.source}). Install and authenticate the delegate provider first.`,
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return { model, thinkingLevel: request.thinkingLevel };
|
|
148
|
+
},
|
|
269
149
|
});
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const activeMapperForModel = (ctx: ExtensionContext): ModelMapper | undefined => {
|
|
273
|
-
const modelProvider = ctx.model?.provider;
|
|
274
|
-
return registrations.find((r) => r.mapper.provider === modelProvider)?.mapper;
|
|
275
150
|
};
|
|
276
151
|
|
|
277
|
-
|
|
278
|
-
|
|
152
|
+
for (const mapper of mappers) {
|
|
153
|
+
for (const id of mapper.getCanonicalFamilies()) registerFamily(mapper, id);
|
|
154
|
+
}
|
|
279
155
|
|
|
280
156
|
const missingBackends = (mapper: ModelMapper, canonicalId: string, ctx: ExtensionContext): string[] => {
|
|
281
157
|
return mapper.getFamilyBackendRefs(canonicalId)
|
|
@@ -286,8 +162,8 @@ export default (pi: ExtensionAPI) => {
|
|
|
286
162
|
/** Registry availability is a session-start diagnostic, not a per-request check. */
|
|
287
163
|
const scanRegistryAvailability = (ctx: ExtensionContext): number => {
|
|
288
164
|
let issues = 0;
|
|
289
|
-
for (const
|
|
290
|
-
warnOnSessionStart(ctx, mapper
|
|
165
|
+
for (const mapper of mappers) {
|
|
166
|
+
warnOnSessionStart(ctx, mapper);
|
|
291
167
|
const availableFamilies = mapper.getCanonicalFamilies().filter((id) =>
|
|
292
168
|
mapper.getFamilyBackendRefs(id).some((ref) => ctx.modelRegistry.find(ref.provider, ref.id)),
|
|
293
169
|
);
|
|
@@ -307,16 +183,10 @@ export default (pi: ExtensionAPI) => {
|
|
|
307
183
|
issues++;
|
|
308
184
|
}
|
|
309
185
|
}
|
|
310
|
-
const
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
api: mapper.api,
|
|
315
|
-
apiKey: FAKE_REGISTRATION_API_KEY,
|
|
316
|
-
baseUrl: mapper.baseUrl,
|
|
317
|
-
models,
|
|
318
|
-
streamSimple: streamMapped,
|
|
319
|
-
});
|
|
186
|
+
for (const id of mapper.getCanonicalFamilies()) {
|
|
187
|
+
if (availableFamilies.includes(id)) registerFamily(mapper, id);
|
|
188
|
+
else pi.unregisterVirtualModel(mapper.provider, id);
|
|
189
|
+
}
|
|
320
190
|
}
|
|
321
191
|
return issues;
|
|
322
192
|
};
|
|
@@ -330,7 +200,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
330
200
|
|
|
331
201
|
const applyThinking = (ctx: ExtensionCommandContext, arg: ToggleArg): void => {
|
|
332
202
|
const next = arg === "toggle" ? !thinkingEnabled : arg === "on";
|
|
333
|
-
const mapper =
|
|
203
|
+
const mapper = ctx.model && mapperFor(ctx.model.provider, ctx.model.id);
|
|
334
204
|
if (next && mapper && ctx.model && !mapper.supportsThinkingToggle(ctx.model.id)) {
|
|
335
205
|
ctx.ui.notify(
|
|
336
206
|
`No "thinking" variant configured for ${ctx.model.id} in ${mapper.source}. Falling back to the default mapping.`,
|
|
@@ -351,7 +221,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
351
221
|
|
|
352
222
|
const applyFast = (ctx: ExtensionCommandContext, arg: ToggleArg): void => {
|
|
353
223
|
const next = arg === "toggle" ? !fastEnabled : arg === "on";
|
|
354
|
-
const mapper =
|
|
224
|
+
const mapper = ctx.model && mapperFor(ctx.model.provider, ctx.model.id);
|
|
355
225
|
if (next && mapper && ctx.model && !mapper.supportsFastToggle(ctx.model.id)) {
|
|
356
226
|
ctx.ui.notify(
|
|
357
227
|
`No "fast" variant configured for ${ctx.model.id} in ${mapper.source}. Falling back to the default mapping.`,
|
|
@@ -419,7 +289,7 @@ export default (pi: ExtensionAPI) => {
|
|
|
419
289
|
let checked = 0;
|
|
420
290
|
let stale = 0;
|
|
421
291
|
let skipped = 0;
|
|
422
|
-
for (const
|
|
292
|
+
for (const mapper of mappers) {
|
|
423
293
|
const result = checkAgainstSource(mapper);
|
|
424
294
|
if (!result) {
|
|
425
295
|
skipped++;
|
|
@@ -481,11 +351,11 @@ export default (pi: ExtensionAPI) => {
|
|
|
481
351
|
// existed, or before an extension reload, rather than only catching it on the next change
|
|
482
352
|
// or the next request.
|
|
483
353
|
if (ctx.model) {
|
|
484
|
-
const
|
|
485
|
-
if (
|
|
354
|
+
const mapper = mapperFor(ctx.model.provider, ctx.model.id);
|
|
355
|
+
if (mapper) {
|
|
486
356
|
const level = asReasoningLevel(ctx.thinkingLevel);
|
|
487
|
-
checkThinkingOnlyLevel(
|
|
488
|
-
checkUnmappedLevel(
|
|
357
|
+
checkThinkingOnlyLevel(mapper, ctx.model.id, level);
|
|
358
|
+
checkUnmappedLevel(mapper, ctx.model.id, level);
|
|
489
359
|
}
|
|
490
360
|
}
|
|
491
361
|
warnedOnSession = true;
|
|
@@ -497,26 +367,26 @@ export default (pi: ExtensionAPI) => {
|
|
|
497
367
|
pi.on("before_agent_start", rememberContext);
|
|
498
368
|
|
|
499
369
|
// Proactively catch the same "this effort level is thinking-only" situation the moment the
|
|
500
|
-
// model or thinking level changes, rather than only at request time (
|
|
370
|
+
// model or thinking level changes, rather than only at request time (route() already
|
|
501
371
|
// covers request time; these two cover selection time).
|
|
502
372
|
pi.on("model_select", (event, ctx) => {
|
|
503
373
|
lastCtx = ctx;
|
|
504
|
-
const
|
|
505
|
-
if (
|
|
374
|
+
const mapper = mapperFor(event.model.provider, event.model.id);
|
|
375
|
+
if (mapper) {
|
|
506
376
|
const level = asReasoningLevel(ctx.thinkingLevel);
|
|
507
|
-
checkThinkingOnlyLevel(
|
|
508
|
-
checkUnmappedLevel(
|
|
377
|
+
checkThinkingOnlyLevel(mapper, event.model.id, level);
|
|
378
|
+
checkUnmappedLevel(mapper, event.model.id, level);
|
|
509
379
|
}
|
|
510
380
|
});
|
|
511
381
|
|
|
512
382
|
pi.on("thinking_level_select", (event, ctx) => {
|
|
513
383
|
lastCtx = ctx;
|
|
514
384
|
if (!ctx.model) return;
|
|
515
|
-
const
|
|
516
|
-
if (
|
|
385
|
+
const mapper = mapperFor(ctx.model.provider, ctx.model.id);
|
|
386
|
+
if (mapper) {
|
|
517
387
|
const level = asReasoningLevel(event.level);
|
|
518
|
-
checkThinkingOnlyLevel(
|
|
519
|
-
checkUnmappedLevel(
|
|
388
|
+
checkThinkingOnlyLevel(mapper, ctx.model.id, level);
|
|
389
|
+
checkUnmappedLevel(mapper, ctx.model.id, level);
|
|
520
390
|
}
|
|
521
391
|
});
|
|
522
392
|
};
|
package/src/mapping.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ThinkingLevel } from "@earendil-works/pi-ai";
|
|
2
2
|
|
|
3
3
|
export interface BackendRef {
|
|
4
4
|
provider: string;
|
|
@@ -56,7 +56,6 @@ const VARIANT_JSON_KEYS: Record<Variant, keyof VariantRefs> = {
|
|
|
56
56
|
|
|
57
57
|
export interface MappingEntry {
|
|
58
58
|
name?: string;
|
|
59
|
-
reasoning?: boolean;
|
|
60
59
|
/**
|
|
61
60
|
* Whether Pi's "off" thinking level (no reasoning param sent at all) is meaningful for this
|
|
62
61
|
* family. Defaults to an automatic check: if this family's "default" map level's `default`
|
|
@@ -71,24 +70,13 @@ export interface MappingEntry {
|
|
|
71
70
|
contextWindow?: number;
|
|
72
71
|
maxTokens?: number;
|
|
73
72
|
input?: ("text" | "image")[];
|
|
74
|
-
cost?: {
|
|
75
|
-
input: number;
|
|
76
|
-
output: number;
|
|
77
|
-
cacheRead: number;
|
|
78
|
-
cacheWrite: number;
|
|
79
|
-
};
|
|
80
73
|
/** Effort level -> backend reference(s). Required. */
|
|
81
74
|
map: EffortMap;
|
|
82
75
|
}
|
|
83
76
|
|
|
84
77
|
export interface ModelMapConfig {
|
|
78
|
+
/** Provider the virtual models are listed under. Defaults to the config filename stem. */
|
|
85
79
|
provider?: string;
|
|
86
|
-
/** Display name in Pi's provider UI. Defaults to provider id. */
|
|
87
|
-
name?: string;
|
|
88
|
-
/** Catalog metadata for registered models. */
|
|
89
|
-
api?: Api;
|
|
90
|
-
/** Catalog metadata for registered models. Defaults to empty string. */
|
|
91
|
-
baseUrl?: string;
|
|
92
80
|
/**
|
|
93
81
|
* Path to a JSON file describing the delegate provider's live model catalog (e.g. a cache file
|
|
94
82
|
* the delegate extension itself maintains). When set, pi-model-map warns at config-load time
|
|
@@ -225,9 +213,6 @@ const normalizeLevelMapping = (value: LevelMapping): Partial<Record<Variant, str
|
|
|
225
213
|
export class ModelMapper {
|
|
226
214
|
readonly source: string;
|
|
227
215
|
readonly provider: string;
|
|
228
|
-
readonly displayName: string;
|
|
229
|
-
readonly api: Api;
|
|
230
|
-
readonly baseUrl: string;
|
|
231
216
|
/** Raw `source`/`sourceListPath`/`sourceIdField` config, or undefined if `source` was omitted. */
|
|
232
217
|
readonly catalogSource?: { path: string; listPath?: string; idField: string };
|
|
233
218
|
private readonly mappings: Record<string, MappingEntry>;
|
|
@@ -246,9 +231,6 @@ export class ModelMapper {
|
|
|
246
231
|
constructor(config: ModelMapConfig, source: string, providerDefault?: string) {
|
|
247
232
|
this.source = source;
|
|
248
233
|
this.provider = config.provider ?? providerDefault ?? "model-map";
|
|
249
|
-
this.displayName = config.name ?? this.provider;
|
|
250
|
-
this.api = (config.api ?? "openai-completions") as Api;
|
|
251
|
-
this.baseUrl = config.baseUrl ?? "";
|
|
252
234
|
this.catalogSource = config.source
|
|
253
235
|
? { path: config.source, listPath: config.sourceListPath, idField: config.sourceIdField ?? "modelId" }
|
|
254
236
|
: undefined;
|
package/src/warnings.ts
CHANGED
|
@@ -6,44 +6,7 @@ import { checkAgainstSource } from "./source-check.js";
|
|
|
6
6
|
|
|
7
7
|
const WARN_PREFIX = "[pi-model-map]";
|
|
8
8
|
|
|
9
|
-
/** Provider IDs that are commonly registered by other Pi extensions or built-ins. */
|
|
10
|
-
const KNOWN_PROVIDER_IDS = new Set([
|
|
11
|
-
"anthropic",
|
|
12
|
-
"openai",
|
|
13
|
-
"google",
|
|
14
|
-
"google-vertex",
|
|
15
|
-
"openrouter",
|
|
16
|
-
"cursor-agent",
|
|
17
|
-
"github-copilot",
|
|
18
|
-
"azure-openai",
|
|
19
|
-
"bedrock",
|
|
20
|
-
]);
|
|
21
|
-
|
|
22
9
|
export const validateProviderConfig = (mapper: ModelMapper): void => {
|
|
23
|
-
const label = `${mapper.source} (${mapper.provider})`;
|
|
24
|
-
const delegateProviders = mapper.getDelegateProviders();
|
|
25
|
-
|
|
26
|
-
if (delegateProviders.includes(mapper.provider)) {
|
|
27
|
-
throw new Error(
|
|
28
|
-
`${WARN_PREFIX} ${label}: provider "${mapper.provider}" is also used as a delegate target in ` +
|
|
29
|
-
`one or more "map" entries. Using the same ID replaces the delegate provider and breaks streaming delegation.`,
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (mapper.provider === "cursor-agent") {
|
|
34
|
-
pluginWarn(
|
|
35
|
-
`${WARN_PREFIX} WARNING: provider is set to "cursor-agent". ` +
|
|
36
|
-
`This replaces @open-cursor/pi-agent's provider registration and prevents delegation ` +
|
|
37
|
-
`to the real Cursor backend. Prefer a unique provider name (e.g. "cursor").`,
|
|
38
|
-
);
|
|
39
|
-
} else if (KNOWN_PROVIDER_IDS.has(mapper.provider)) {
|
|
40
|
-
pluginWarn(
|
|
41
|
-
`${WARN_PREFIX} WARNING: provider "${mapper.provider}" may overwrite an existing provider namespace ` +
|
|
42
|
-
`registered by Pi or another extension. Models will appear as ${mapper.provider}/<family>, ` +
|
|
43
|
-
`but the original provider's models and behavior may be replaced.`,
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
10
|
checkMultiProviderUsage(mapper);
|
|
48
11
|
checkSourceCatalog(mapper);
|
|
49
12
|
};
|
|
@@ -118,46 +81,13 @@ const checkMultiProviderUsage = (mapper: ModelMapper): void => {
|
|
|
118
81
|
}
|
|
119
82
|
};
|
|
120
83
|
|
|
121
|
-
export const warnOnSessionStart = (
|
|
122
|
-
ctx: ExtensionContext,
|
|
123
|
-
mapper: ModelMapper,
|
|
124
|
-
ownStream?: unknown,
|
|
125
|
-
): void => {
|
|
84
|
+
export const warnOnSessionStart = (ctx: ExtensionContext, mapper: ModelMapper): void => {
|
|
126
85
|
for (const delegateProvider of mapper.getDelegateProviders()) {
|
|
127
|
-
|
|
128
|
-
if (!delegate) {
|
|
86
|
+
if (!ctx.modelRegistry.getProvider(delegateProvider)) {
|
|
129
87
|
pluginWarn(
|
|
130
88
|
`${WARN_PREFIX} delegate provider "${delegateProvider}" is not registered. ` +
|
|
131
89
|
`Install and authenticate it before using ${mapper.provider} models that reference it.`,
|
|
132
90
|
);
|
|
133
|
-
continue;
|
|
134
91
|
}
|
|
135
|
-
|
|
136
|
-
if (ownStream && delegate.streamSimple === ownStream) {
|
|
137
|
-
pluginWarn(
|
|
138
|
-
`${WARN_PREFIX} delegate provider "${delegateProvider}" resolves to this extension's ` +
|
|
139
|
-
`own stream handler. Delegation is broken — likely because provider "${mapper.provider}" ` +
|
|
140
|
-
`replaced "${delegateProvider}"'s registration. Use a unique provider name.`,
|
|
141
|
-
);
|
|
142
|
-
continue;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!delegate.streamSimple) {
|
|
146
|
-
pluginWarn(
|
|
147
|
-
`${WARN_PREFIX} delegate provider "${delegateProvider}" has no streamSimple handler.`,
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const existing = ctx.modelRegistry.getProvider(mapper.provider);
|
|
153
|
-
if (
|
|
154
|
-
KNOWN_PROVIDER_IDS.has(mapper.provider) &&
|
|
155
|
-
existing &&
|
|
156
|
-
!mapper.provider.startsWith("model-map")
|
|
157
|
-
) {
|
|
158
|
-
pluginWarn(
|
|
159
|
-
`${WARN_PREFIX} provider "${mapper.provider}" is active alongside known provider namespaces. ` +
|
|
160
|
-
`Verify /model shows the families you expect and that delegation still works.`,
|
|
161
|
-
);
|
|
162
92
|
}
|
|
163
93
|
};
|