@jadelclemens/pi-model-map 0.0.0-stage → 0.1.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,163 @@
1
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
2
+
3
+ import { pluginWarn } from "./log.js";
4
+ import type { ModelMapper } from "./mapping.js";
5
+ import { checkAgainstSource } from "./source-check.js";
6
+
7
+ const WARN_PREFIX = "[pi-model-map]";
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
+ 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
+ checkMultiProviderUsage(mapper);
48
+ checkSourceCatalog(mapper);
49
+ };
50
+
51
+ /**
52
+ * Opt-in: only runs when a config file sets "source". Warns if the source catalog file can't be
53
+ * read/parsed as configured, or if any backend id referenced in "mappings" is missing from it --
54
+ * usually meaning the delegate provider renamed or retired that model since the config was written.
55
+ */
56
+ const checkSourceCatalog = (mapper: ModelMapper): void => {
57
+ const result = checkAgainstSource(mapper);
58
+ if (!result) return;
59
+
60
+ const label = `${mapper.source} (${mapper.provider})`;
61
+
62
+ if (result.error) {
63
+ pluginWarn(
64
+ `${WARN_PREFIX} WARNING: ${label}: "source" is set to "${result.resolvedPath}" but it ${result.error}. ` +
65
+ `Skipping the backend-id freshness check.`,
66
+ );
67
+ return;
68
+ }
69
+
70
+ if (result.missingIds.length === 0) return;
71
+
72
+ for (const id of result.missingIds) {
73
+ const locations = mapper.getBackendIdLocations(id).join(", ");
74
+ pluginWarn(
75
+ `${WARN_PREFIX} WARNING: ${label}: backend id "${id}" (referenced at ${locations}) was not found in ` +
76
+ `"${result.resolvedPath}". The provider may have renamed or retired this model -- verify the mapping ` +
77
+ `still resolves to something real.`,
78
+ );
79
+ }
80
+ };
81
+
82
+ /**
83
+ * pi-model-map expects each config file to delegate to exactly one real backend provider.
84
+ * "provider/modelId" syntax on every map entry exists for explicitness (and to let one file
85
+ * mix effort levels/variants freely), not to fan a single mapping out across multiple
86
+ * distinct backends. Grouping unrelated providers into one virtual model (e.g. a "thinking
87
+ * tier" that round-robins between different providers' models) is out of scope here — that's
88
+ * a different kind of plugin. This only warns; it does not block loading, since a config that
89
+ * intentionally mixes delegates for some other reason will still work mechanically.
90
+ */
91
+ const checkMultiProviderUsage = (mapper: ModelMapper): void => {
92
+ const label = `${mapper.source} (${mapper.provider})`;
93
+ const allProviders = mapper.getDelegateProviders();
94
+
95
+ for (const canonicalId of mapper.getCanonicalFamilies()) {
96
+ const familyProviders = mapper.getFamilyDelegateProviders(canonicalId);
97
+ if (familyProviders.length > 1) {
98
+ pluginWarn(
99
+ `${WARN_PREFIX} WARNING: ${label}: mappings.${canonicalId} references multiple delegate ` +
100
+ `providers (${familyProviders.join(", ")}) across its own effort levels/variants. ` +
101
+ `pi-model-map maps one canonical model to one backend provider; mixing providers within a ` +
102
+ `single mapping (e.g. to round-robin between a "cursor-agent" model and an "anthropic" model ` +
103
+ `under one canonical id) is out of scope and not supported by this project's resolution logic — ` +
104
+ `whichever provider a given effort level/variant happens to reference is what will be used, ` +
105
+ `with no cross-provider fallback or selection.`,
106
+ );
107
+ }
108
+ }
109
+
110
+ if (allProviders.length > 1) {
111
+ pluginWarn(
112
+ `${WARN_PREFIX} WARNING: ${label}: this config file references ${allProviders.length} distinct ` +
113
+ `delegate providers overall (${allProviders.join(", ")}). Each config file is intended to delegate ` +
114
+ `to a single backend provider; use separate config files (one provider each) if you want to map ` +
115
+ `models from multiple providers. If this is unintentional, check for a typo in a "provider/modelId" ` +
116
+ `reference.`,
117
+ );
118
+ }
119
+ };
120
+
121
+ export const warnOnSessionStart = (
122
+ ctx: ExtensionContext,
123
+ mapper: ModelMapper,
124
+ ownStream?: unknown,
125
+ ): void => {
126
+ for (const delegateProvider of mapper.getDelegateProviders()) {
127
+ const delegate = ctx.modelRegistry.getProvider(delegateProvider);
128
+ if (!delegate) {
129
+ pluginWarn(
130
+ `${WARN_PREFIX} delegate provider "${delegateProvider}" is not registered. ` +
131
+ `Install and authenticate it before using ${mapper.provider} models that reference it.`,
132
+ );
133
+ continue;
134
+ }
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
+ }
163
+ };