@earendil-works/pi-coding-agent 0.80.7 → 0.80.9
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/CHANGELOG.md +64 -0
- package/README.md +6 -6
- package/dist/bun/cli.d.ts.map +1 -1
- package/dist/bun/cli.js +2 -0
- package/dist/bun/cli.js.map +1 -1
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +1 -1
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/list-models.d.ts +2 -2
- package/dist/cli/list-models.d.ts.map +1 -1
- package/dist/cli/list-models.js +3 -3
- package/dist/cli/list-models.js.map +1 -1
- package/dist/core/agent-session-runtime.d.ts.map +1 -1
- package/dist/core/agent-session-runtime.js +3 -0
- package/dist/core/agent-session-runtime.js.map +1 -1
- package/dist/core/agent-session-services.d.ts +3 -6
- package/dist/core/agent-session-services.d.ts.map +1 -1
- package/dist/core/agent-session-services.js +10 -9
- package/dist/core/agent-session-services.js.map +1 -1
- package/dist/core/agent-session.d.ts +5 -6
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +57 -30
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/auth-storage.d.ts +15 -99
- package/dist/core/auth-storage.d.ts.map +1 -1
- package/dist/core/auth-storage.js +46 -259
- package/dist/core/auth-storage.js.map +1 -1
- package/dist/core/cache-stats.d.ts +2 -2
- package/dist/core/cache-stats.d.ts.map +1 -1
- package/dist/core/cache-stats.js +1 -1
- package/dist/core/cache-stats.js.map +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +8 -2
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +1 -0
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +3 -0
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +9 -2
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/model-config.d.ts +512 -0
- package/dist/core/model-config.d.ts.map +1 -0
- package/dist/core/model-config.js +243 -0
- package/dist/core/model-config.js.map +1 -0
- package/dist/core/model-registry.d.ts +13 -123
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +44 -747
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver.d.ts +6 -6
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +18 -18
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/model-runtime.d.ts +77 -0
- package/dist/core/model-runtime.d.ts.map +1 -0
- package/dist/core/model-runtime.js +418 -0
- package/dist/core/model-runtime.js.map +1 -0
- package/dist/core/models-store.d.ts +17 -0
- package/dist/core/models-store.d.ts.map +1 -0
- package/dist/core/models-store.js +45 -0
- package/dist/core/models-store.js.map +1 -0
- package/dist/core/provider-composer.d.ts +55 -0
- package/dist/core/provider-composer.d.ts.map +1 -0
- package/dist/core/provider-composer.js +375 -0
- package/dist/core/provider-composer.js.map +1 -0
- package/dist/core/radius.d.ts +0 -19
- package/dist/core/radius.d.ts.map +1 -1
- package/dist/core/radius.js +0 -28
- package/dist/core/radius.js.map +1 -1
- package/dist/core/remote-catalog-provider.d.ts +5 -0
- package/dist/core/remote-catalog-provider.d.ts.map +1 -0
- package/dist/core/remote-catalog-provider.js +83 -0
- package/dist/core/remote-catalog-provider.js.map +1 -0
- package/dist/core/runtime-credentials.d.ts +15 -0
- package/dist/core/runtime-credentials.d.ts.map +1 -0
- package/dist/core/runtime-credentials.js +36 -0
- package/dist/core/runtime-credentials.js.map +1 -0
- package/dist/core/sdk.d.ts +3 -6
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js +14 -25
- package/dist/core/sdk.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +11 -13
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
- package/dist/modes/interactive/components/assistant-message.js +22 -10
- package/dist/modes/interactive/components/assistant-message.js.map +1 -1
- package/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/dist/modes/interactive/components/footer.js +1 -1
- package/dist/modes/interactive/components/footer.js.map +1 -1
- package/dist/modes/interactive/components/login-dialog.d.ts +4 -8
- package/dist/modes/interactive/components/login-dialog.d.ts.map +1 -1
- package/dist/modes/interactive/components/login-dialog.js +14 -12
- package/dist/modes/interactive/components/login-dialog.js.map +1 -1
- package/dist/modes/interactive/components/model-selector.d.ts +11 -4
- package/dist/modes/interactive/components/model-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/model-selector.js +72 -41
- package/dist/modes/interactive/components/model-selector.js.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.d.ts +4 -4
- package/dist/modes/interactive/components/oauth-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/oauth-selector.js +14 -27
- package/dist/modes/interactive/components/oauth-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +6 -3
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +157 -143
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +2 -2
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/package-manager-cli.d.ts.map +1 -1
- package/dist/package-manager-cli.js +67 -5
- package/dist/package-manager-cli.js.map +1 -1
- package/docs/custom-provider.md +6 -11
- package/docs/extensions.md +22 -1
- package/docs/models.md +2 -0
- package/docs/packages.md +1 -0
- package/docs/providers.md +13 -1
- package/docs/sdk.md +40 -50
- package/docs/usage.md +1 -0
- package/examples/extensions/README.md +1 -0
- package/examples/extensions/custom-provider-anthropic/index.ts +1 -1
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/gondolin/package-lock.json +2 -2
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/kimi-deferred-tools.ts +61 -0
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/sdk/02-custom-model.ts +5 -8
- package/examples/sdk/09-api-keys-and-oauth.ts +13 -31
- package/examples/sdk/12-full-control.ts +7 -12
- package/examples/sdk/README.md +14 -18
- package/npm-shrinkwrap.json +12 -12
- package/package.json +4 -4
- package/dist/core/provider-display-names.d.ts +0 -2
- package/dist/core/provider-display-names.d.ts.map +0 -1
- package/dist/core/provider-display-names.js +0 -37
- package/dist/core/provider-display-names.js.map +0 -1
|
@@ -1,792 +1,89 @@
|
|
|
1
|
+
export { clearApiKeyCache } from "./provider-composer.js";
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
import { getModels, getProviders, registerApiProvider, resetApiProviders, } from "@earendil-works/pi-ai/compat";
|
|
5
|
-
import { registerOAuthProvider, resetOAuthProviders } from "@earendil-works/pi-ai/oauth";
|
|
6
|
-
import { existsSync, readFileSync } from "fs";
|
|
7
|
-
import { join } from "path";
|
|
8
|
-
import { Type } from "typebox";
|
|
9
|
-
import { Compile } from "typebox/compile";
|
|
10
|
-
import { getAgentDir } from "../config.js";
|
|
11
|
-
import { stripJsonComments } from "../utils/json.js";
|
|
12
|
-
import { normalizePath } from "../utils/paths.js";
|
|
13
|
-
import { BUILT_IN_PROVIDER_DISPLAY_NAMES } from "./provider-display-names.js";
|
|
14
|
-
import { registerCustomRadiusOAuthProvider } from "./radius.js";
|
|
15
|
-
import { clearConfigValueCache, getConfigValueEnvVarNames, isCommandConfigValue, isConfigValueConfigured, resolveConfigValueOrThrow, resolveConfigValueUncached, resolveHeadersOrThrow, } from "./resolve-config-value.js";
|
|
16
|
-
// Schema for OpenRouter routing preferences
|
|
17
|
-
const PercentileCutoffsSchema = Type.Object({
|
|
18
|
-
p50: Type.Optional(Type.Number()),
|
|
19
|
-
p75: Type.Optional(Type.Number()),
|
|
20
|
-
p90: Type.Optional(Type.Number()),
|
|
21
|
-
p99: Type.Optional(Type.Number()),
|
|
22
|
-
});
|
|
23
|
-
const OpenRouterRoutingSchema = Type.Object({
|
|
24
|
-
allow_fallbacks: Type.Optional(Type.Boolean()),
|
|
25
|
-
require_parameters: Type.Optional(Type.Boolean()),
|
|
26
|
-
data_collection: Type.Optional(Type.Union([Type.Literal("deny"), Type.Literal("allow")])),
|
|
27
|
-
zdr: Type.Optional(Type.Boolean()),
|
|
28
|
-
enforce_distillable_text: Type.Optional(Type.Boolean()),
|
|
29
|
-
order: Type.Optional(Type.Array(Type.String())),
|
|
30
|
-
only: Type.Optional(Type.Array(Type.String())),
|
|
31
|
-
ignore: Type.Optional(Type.Array(Type.String())),
|
|
32
|
-
quantizations: Type.Optional(Type.Array(Type.String())),
|
|
33
|
-
sort: Type.Optional(Type.Union([
|
|
34
|
-
Type.String(),
|
|
35
|
-
Type.Object({
|
|
36
|
-
by: Type.Optional(Type.String()),
|
|
37
|
-
partition: Type.Optional(Type.Union([Type.String(), Type.Null()])),
|
|
38
|
-
}),
|
|
39
|
-
])),
|
|
40
|
-
max_price: Type.Optional(Type.Object({
|
|
41
|
-
prompt: Type.Optional(Type.Union([Type.Number(), Type.String()])),
|
|
42
|
-
completion: Type.Optional(Type.Union([Type.Number(), Type.String()])),
|
|
43
|
-
image: Type.Optional(Type.Union([Type.Number(), Type.String()])),
|
|
44
|
-
audio: Type.Optional(Type.Union([Type.Number(), Type.String()])),
|
|
45
|
-
request: Type.Optional(Type.Union([Type.Number(), Type.String()])),
|
|
46
|
-
})),
|
|
47
|
-
preferred_min_throughput: Type.Optional(Type.Union([Type.Number(), PercentileCutoffsSchema])),
|
|
48
|
-
preferred_max_latency: Type.Optional(Type.Union([Type.Number(), PercentileCutoffsSchema])),
|
|
49
|
-
});
|
|
50
|
-
// Schema for Vercel AI Gateway routing preferences
|
|
51
|
-
const VercelGatewayRoutingSchema = Type.Object({
|
|
52
|
-
only: Type.Optional(Type.Array(Type.String())),
|
|
53
|
-
order: Type.Optional(Type.Array(Type.String())),
|
|
54
|
-
});
|
|
55
|
-
// Schema for thinking level support and provider-specific values
|
|
56
|
-
const ThinkingLevelMapValueSchema = Type.Union([Type.String(), Type.Null()]);
|
|
57
|
-
const ThinkingLevelMapSchema = Type.Object({
|
|
58
|
-
off: Type.Optional(ThinkingLevelMapValueSchema),
|
|
59
|
-
minimal: Type.Optional(ThinkingLevelMapValueSchema),
|
|
60
|
-
low: Type.Optional(ThinkingLevelMapValueSchema),
|
|
61
|
-
medium: Type.Optional(ThinkingLevelMapValueSchema),
|
|
62
|
-
high: Type.Optional(ThinkingLevelMapValueSchema),
|
|
63
|
-
xhigh: Type.Optional(ThinkingLevelMapValueSchema),
|
|
64
|
-
max: Type.Optional(ThinkingLevelMapValueSchema),
|
|
65
|
-
});
|
|
66
|
-
const ChatTemplateKwargScalarSchema = Type.Union([Type.String(), Type.Number(), Type.Boolean(), Type.Null()]);
|
|
67
|
-
const ChatTemplateKwargVariableSchema = Type.Object({
|
|
68
|
-
$var: Type.Union([Type.Literal("thinking.enabled"), Type.Literal("thinking.effort")]),
|
|
69
|
-
omitWhenOff: Type.Optional(Type.Boolean()),
|
|
70
|
-
});
|
|
71
|
-
const ChatTemplateKwargSchema = Type.Union([ChatTemplateKwargScalarSchema, ChatTemplateKwargVariableSchema]);
|
|
72
|
-
const OpenAICompletionsCompatSchema = Type.Object({
|
|
73
|
-
supportsStore: Type.Optional(Type.Boolean()),
|
|
74
|
-
supportsDeveloperRole: Type.Optional(Type.Boolean()),
|
|
75
|
-
supportsReasoningEffort: Type.Optional(Type.Boolean()),
|
|
76
|
-
supportsUsageInStreaming: Type.Optional(Type.Boolean()),
|
|
77
|
-
maxTokensField: Type.Optional(Type.Union([Type.Literal("max_completion_tokens"), Type.Literal("max_tokens")])),
|
|
78
|
-
requiresToolResultName: Type.Optional(Type.Boolean()),
|
|
79
|
-
requiresAssistantAfterToolResult: Type.Optional(Type.Boolean()),
|
|
80
|
-
requiresThinkingAsText: Type.Optional(Type.Boolean()),
|
|
81
|
-
requiresReasoningContentOnAssistantMessages: Type.Optional(Type.Boolean()),
|
|
82
|
-
thinkingFormat: Type.Optional(Type.Union([
|
|
83
|
-
Type.Literal("openai"),
|
|
84
|
-
Type.Literal("openrouter"),
|
|
85
|
-
Type.Literal("together"),
|
|
86
|
-
Type.Literal("deepseek"),
|
|
87
|
-
Type.Literal("zai"),
|
|
88
|
-
Type.Literal("qwen"),
|
|
89
|
-
Type.Literal("chat-template"),
|
|
90
|
-
Type.Literal("qwen-chat-template"),
|
|
91
|
-
Type.Literal("string-thinking"),
|
|
92
|
-
Type.Literal("ant-ling"),
|
|
93
|
-
])),
|
|
94
|
-
chatTemplateKwargs: Type.Optional(Type.Record(Type.String(), ChatTemplateKwargSchema)),
|
|
95
|
-
cacheControlFormat: Type.Optional(Type.Literal("anthropic")),
|
|
96
|
-
openRouterRouting: Type.Optional(OpenRouterRoutingSchema),
|
|
97
|
-
vercelGatewayRouting: Type.Optional(VercelGatewayRoutingSchema),
|
|
98
|
-
supportsStrictMode: Type.Optional(Type.Boolean()),
|
|
99
|
-
sendSessionAffinityHeaders: Type.Optional(Type.Boolean()),
|
|
100
|
-
sessionAffinityFormat: Type.Optional(Type.Union([Type.Literal("openai"), Type.Literal("openai-nosession"), Type.Literal("openrouter")])),
|
|
101
|
-
supportsLongCacheRetention: Type.Optional(Type.Boolean()),
|
|
102
|
-
});
|
|
103
|
-
const OpenAIResponsesCompatSchema = Type.Object({
|
|
104
|
-
supportsDeveloperRole: Type.Optional(Type.Boolean()),
|
|
105
|
-
sessionAffinityFormat: Type.Optional(Type.Union([Type.Literal("openai"), Type.Literal("openai-nosession"), Type.Literal("openrouter")])),
|
|
106
|
-
supportsLongCacheRetention: Type.Optional(Type.Boolean()),
|
|
107
|
-
supportsToolSearch: Type.Optional(Type.Boolean()),
|
|
108
|
-
});
|
|
109
|
-
const AnthropicMessagesCompatSchema = Type.Object({
|
|
110
|
-
supportsEagerToolInputStreaming: Type.Optional(Type.Boolean()),
|
|
111
|
-
supportsLongCacheRetention: Type.Optional(Type.Boolean()),
|
|
112
|
-
sendSessionAffinityHeaders: Type.Optional(Type.Boolean()),
|
|
113
|
-
supportsCacheControlOnTools: Type.Optional(Type.Boolean()),
|
|
114
|
-
forceAdaptiveThinking: Type.Optional(Type.Boolean()),
|
|
115
|
-
supportsToolReferences: Type.Optional(Type.Boolean()),
|
|
116
|
-
});
|
|
117
|
-
const ProviderCompatSchema = Type.Union([
|
|
118
|
-
OpenAICompletionsCompatSchema,
|
|
119
|
-
OpenAIResponsesCompatSchema,
|
|
120
|
-
AnthropicMessagesCompatSchema,
|
|
121
|
-
]);
|
|
122
|
-
const ModelCostRatesSchema = {
|
|
123
|
-
input: Type.Number(),
|
|
124
|
-
output: Type.Number(),
|
|
125
|
-
cacheRead: Type.Number(),
|
|
126
|
-
cacheWrite: Type.Number(),
|
|
127
|
-
};
|
|
128
|
-
const ModelCostTierSchema = Type.Object({
|
|
129
|
-
inputTokensAbove: Type.Number(),
|
|
130
|
-
...ModelCostRatesSchema,
|
|
131
|
-
});
|
|
132
|
-
const ModelCostSchema = Type.Object({
|
|
133
|
-
...ModelCostRatesSchema,
|
|
134
|
-
tiers: Type.Optional(Type.Array(ModelCostTierSchema)),
|
|
135
|
-
});
|
|
136
|
-
// Schema for custom model definition
|
|
137
|
-
// Most fields are optional with sensible defaults for local models (Ollama, LM Studio, etc.)
|
|
138
|
-
const ModelDefinitionSchema = Type.Object({
|
|
139
|
-
id: Type.String({ minLength: 1 }),
|
|
140
|
-
name: Type.Optional(Type.String({ minLength: 1 })),
|
|
141
|
-
api: Type.Optional(Type.String({ minLength: 1 })),
|
|
142
|
-
baseUrl: Type.Optional(Type.String({ minLength: 1 })),
|
|
143
|
-
reasoning: Type.Optional(Type.Boolean()),
|
|
144
|
-
thinkingLevelMap: Type.Optional(ThinkingLevelMapSchema),
|
|
145
|
-
input: Type.Optional(Type.Array(Type.Union([Type.Literal("text"), Type.Literal("image")]))),
|
|
146
|
-
cost: Type.Optional(ModelCostSchema),
|
|
147
|
-
contextWindow: Type.Optional(Type.Number()),
|
|
148
|
-
maxTokens: Type.Optional(Type.Number()),
|
|
149
|
-
headers: Type.Optional(Type.Record(Type.String(), Type.String())),
|
|
150
|
-
compat: Type.Optional(ProviderCompatSchema),
|
|
151
|
-
});
|
|
152
|
-
// Schema for per-model overrides (all fields optional, merged with built-in model)
|
|
153
|
-
const ModelOverrideSchema = Type.Object({
|
|
154
|
-
name: Type.Optional(Type.String({ minLength: 1 })),
|
|
155
|
-
reasoning: Type.Optional(Type.Boolean()),
|
|
156
|
-
thinkingLevelMap: Type.Optional(ThinkingLevelMapSchema),
|
|
157
|
-
input: Type.Optional(Type.Array(Type.Union([Type.Literal("text"), Type.Literal("image")]))),
|
|
158
|
-
cost: Type.Optional(Type.Object({
|
|
159
|
-
input: Type.Optional(Type.Number()),
|
|
160
|
-
output: Type.Optional(Type.Number()),
|
|
161
|
-
cacheRead: Type.Optional(Type.Number()),
|
|
162
|
-
cacheWrite: Type.Optional(Type.Number()),
|
|
163
|
-
tiers: Type.Optional(Type.Array(ModelCostTierSchema)),
|
|
164
|
-
})),
|
|
165
|
-
contextWindow: Type.Optional(Type.Number()),
|
|
166
|
-
maxTokens: Type.Optional(Type.Number()),
|
|
167
|
-
headers: Type.Optional(Type.Record(Type.String(), Type.String())),
|
|
168
|
-
compat: Type.Optional(ProviderCompatSchema),
|
|
169
|
-
});
|
|
170
|
-
const ProviderConfigSchema = Type.Object({
|
|
171
|
-
name: Type.Optional(Type.String({ minLength: 1 })),
|
|
172
|
-
baseUrl: Type.Optional(Type.String({ minLength: 1 })),
|
|
173
|
-
apiKey: Type.Optional(Type.String({ minLength: 1 })),
|
|
174
|
-
api: Type.Optional(Type.String({ minLength: 1 })),
|
|
175
|
-
/** OAuth flavor spoken by this provider's endpoint. Registers a sign-in
|
|
176
|
-
* provider with a dynamic model catalog (e.g. a custom Radius gateway). */
|
|
177
|
-
oauth: Type.Optional(Type.Literal("radius")),
|
|
178
|
-
headers: Type.Optional(Type.Record(Type.String(), Type.String())),
|
|
179
|
-
compat: Type.Optional(ProviderCompatSchema),
|
|
180
|
-
authHeader: Type.Optional(Type.Boolean()),
|
|
181
|
-
models: Type.Optional(Type.Array(ModelDefinitionSchema)),
|
|
182
|
-
modelOverrides: Type.Optional(Type.Record(Type.String(), ModelOverrideSchema)),
|
|
183
|
-
});
|
|
184
|
-
const ModelsConfigSchema = Type.Object({
|
|
185
|
-
providers: Type.Record(Type.String(), ProviderConfigSchema),
|
|
186
|
-
});
|
|
187
|
-
const validateModelsConfig = Compile(ModelsConfigSchema);
|
|
188
|
-
function formatValidationPath(error) {
|
|
189
|
-
if (error.keyword === "required") {
|
|
190
|
-
const requiredProperties = error.params.requiredProperties;
|
|
191
|
-
const requiredProperty = requiredProperties?.[0];
|
|
192
|
-
if (requiredProperty) {
|
|
193
|
-
const basePath = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
|
|
194
|
-
return basePath ? `${basePath}.${requiredProperty}` : requiredProperty;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
const path = error.instancePath.replace(/^\//, "").replace(/\//g, ".");
|
|
198
|
-
return path || "root";
|
|
199
|
-
}
|
|
200
|
-
function emptyCustomModelsResult(error) {
|
|
201
|
-
return { models: [], overrides: new Map(), modelOverrides: new Map(), error };
|
|
202
|
-
}
|
|
203
|
-
function mergeCompat(baseCompat, overrideCompat) {
|
|
204
|
-
if (!overrideCompat)
|
|
205
|
-
return baseCompat;
|
|
206
|
-
const base = baseCompat;
|
|
207
|
-
const override = overrideCompat;
|
|
208
|
-
const merged = { ...base, ...override };
|
|
209
|
-
const baseCompletions = base;
|
|
210
|
-
const overrideCompletions = override;
|
|
211
|
-
const mergedCompletions = merged;
|
|
212
|
-
if (baseCompletions?.openRouterRouting || overrideCompletions.openRouterRouting) {
|
|
213
|
-
mergedCompletions.openRouterRouting = {
|
|
214
|
-
...baseCompletions?.openRouterRouting,
|
|
215
|
-
...overrideCompletions.openRouterRouting,
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
if (baseCompletions?.vercelGatewayRouting || overrideCompletions.vercelGatewayRouting) {
|
|
219
|
-
mergedCompletions.vercelGatewayRouting = {
|
|
220
|
-
...baseCompletions?.vercelGatewayRouting,
|
|
221
|
-
...overrideCompletions.vercelGatewayRouting,
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
if (baseCompletions?.chatTemplateKwargs || overrideCompletions.chatTemplateKwargs) {
|
|
225
|
-
mergedCompletions.chatTemplateKwargs = {
|
|
226
|
-
...baseCompletions?.chatTemplateKwargs,
|
|
227
|
-
...overrideCompletions.chatTemplateKwargs,
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
return merged;
|
|
231
|
-
}
|
|
232
|
-
/**
|
|
233
|
-
* Deep merge a model override into a model.
|
|
234
|
-
* Handles nested objects (cost, compat) by merging rather than replacing.
|
|
235
|
-
*/
|
|
236
|
-
function applyModelOverride(model, override) {
|
|
237
|
-
const result = { ...model };
|
|
238
|
-
// Simple field overrides
|
|
239
|
-
if (override.name !== undefined)
|
|
240
|
-
result.name = override.name;
|
|
241
|
-
if (override.reasoning !== undefined)
|
|
242
|
-
result.reasoning = override.reasoning;
|
|
243
|
-
if (override.thinkingLevelMap !== undefined) {
|
|
244
|
-
result.thinkingLevelMap = { ...model.thinkingLevelMap, ...override.thinkingLevelMap };
|
|
245
|
-
}
|
|
246
|
-
if (override.input !== undefined)
|
|
247
|
-
result.input = override.input;
|
|
248
|
-
if (override.contextWindow !== undefined)
|
|
249
|
-
result.contextWindow = override.contextWindow;
|
|
250
|
-
if (override.maxTokens !== undefined)
|
|
251
|
-
result.maxTokens = override.maxTokens;
|
|
252
|
-
// Merge cost (partial override)
|
|
253
|
-
if (override.cost) {
|
|
254
|
-
result.cost = {
|
|
255
|
-
input: override.cost.input ?? model.cost.input,
|
|
256
|
-
output: override.cost.output ?? model.cost.output,
|
|
257
|
-
cacheRead: override.cost.cacheRead ?? model.cost.cacheRead,
|
|
258
|
-
cacheWrite: override.cost.cacheWrite ?? model.cost.cacheWrite,
|
|
259
|
-
tiers: override.cost.tiers ?? model.cost.tiers,
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
// Deep merge compat
|
|
263
|
-
result.compat = mergeCompat(model.compat, override.compat);
|
|
264
|
-
return result;
|
|
265
|
-
}
|
|
266
|
-
/** Clear the config value command cache. Exported for testing. */
|
|
267
|
-
export const clearApiKeyCache = clearConfigValueCache;
|
|
268
|
-
/**
|
|
269
|
-
* Model registry - loads and manages models, resolves API keys via AuthStorage.
|
|
3
|
+
* Synchronous compatibility facade exposed to extensions.
|
|
4
|
+
* Coding-agent internals use ModelRuntime directly.
|
|
270
5
|
*/
|
|
271
6
|
export class ModelRegistry {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
configModelOverrides = new Map();
|
|
276
|
-
registeredProviders = new Map();
|
|
277
|
-
loadError = undefined;
|
|
278
|
-
authStorage;
|
|
279
|
-
modelsJsonPath;
|
|
280
|
-
constructor(authStorage, modelsJsonPath) {
|
|
281
|
-
this.authStorage = authStorage;
|
|
282
|
-
this.modelsJsonPath = modelsJsonPath ? normalizePath(modelsJsonPath) : undefined;
|
|
283
|
-
this.loadModels();
|
|
7
|
+
runtime;
|
|
8
|
+
constructor(runtime) {
|
|
9
|
+
this.runtime = runtime;
|
|
284
10
|
}
|
|
285
|
-
|
|
286
|
-
return new ModelRegistry(authStorage, modelsJsonPath);
|
|
287
|
-
}
|
|
288
|
-
static inMemory(authStorage) {
|
|
289
|
-
return new ModelRegistry(authStorage, undefined);
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* Reload models from disk (built-in + custom from models.json).
|
|
293
|
-
*/
|
|
11
|
+
/** Reload models.json asynchronously. Await before making synchronous registry reads. */
|
|
294
12
|
refresh() {
|
|
295
|
-
this.
|
|
296
|
-
this.modelRequestHeaders.clear();
|
|
297
|
-
this.loadError = undefined;
|
|
298
|
-
// Ensure dynamic API/OAuth registrations are rebuilt from current provider state.
|
|
299
|
-
resetApiProviders();
|
|
300
|
-
resetOAuthProviders();
|
|
301
|
-
this.loadModels();
|
|
302
|
-
for (const [providerName, config] of this.registeredProviders.entries()) {
|
|
303
|
-
this.applyProviderConfig(providerName, config);
|
|
304
|
-
}
|
|
13
|
+
return this.runtime.reloadConfig();
|
|
305
14
|
}
|
|
306
|
-
/**
|
|
307
|
-
* Get any error from loading models.json (undefined if no error).
|
|
308
|
-
*/
|
|
309
15
|
getError() {
|
|
310
|
-
return this.
|
|
311
|
-
}
|
|
312
|
-
loadModels() {
|
|
313
|
-
// Load custom models and overrides from models.json
|
|
314
|
-
const { models: customModels, overrides, modelOverrides, error, } = this.modelsJsonPath ? this.loadCustomModels(this.modelsJsonPath) : emptyCustomModelsResult();
|
|
315
|
-
this.configModelOverrides = modelOverrides;
|
|
316
|
-
if (error) {
|
|
317
|
-
this.loadError = error;
|
|
318
|
-
// Keep built-in models even if custom models failed to load
|
|
319
|
-
}
|
|
320
|
-
const builtInModels = this.loadBuiltInModels(overrides, modelOverrides);
|
|
321
|
-
let combined = this.mergeCustomModels(builtInModels, customModels);
|
|
322
|
-
// Let OAuth providers modify their models (e.g., update baseUrl)
|
|
323
|
-
for (const oauthProvider of this.authStorage.getOAuthProviders()) {
|
|
324
|
-
const cred = this.authStorage.get(oauthProvider.id);
|
|
325
|
-
if (cred?.type === "oauth" && oauthProvider.modifyModels) {
|
|
326
|
-
combined = oauthProvider.modifyModels(combined, cred);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
this.models = combined;
|
|
330
|
-
}
|
|
331
|
-
/** Load built-in models and apply provider/model overrides */
|
|
332
|
-
loadBuiltInModels(overrides, modelOverrides) {
|
|
333
|
-
return getProviders().flatMap((provider) => {
|
|
334
|
-
const models = getModels(provider);
|
|
335
|
-
const providerOverride = overrides.get(provider);
|
|
336
|
-
const perModelOverrides = modelOverrides.get(provider);
|
|
337
|
-
return models.map((m) => {
|
|
338
|
-
let model = m;
|
|
339
|
-
// Apply provider-level baseUrl/headers/compat override
|
|
340
|
-
if (providerOverride) {
|
|
341
|
-
model = {
|
|
342
|
-
...model,
|
|
343
|
-
baseUrl: providerOverride.baseUrl ?? model.baseUrl,
|
|
344
|
-
compat: mergeCompat(model.compat, providerOverride.compat),
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
// Apply per-model override
|
|
348
|
-
const modelOverride = perModelOverrides?.get(m.id);
|
|
349
|
-
if (modelOverride) {
|
|
350
|
-
model = applyModelOverride(model, modelOverride);
|
|
351
|
-
}
|
|
352
|
-
return model;
|
|
353
|
-
});
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
getConfiguredModelOverride(providerName, modelId) {
|
|
357
|
-
return this.configModelOverrides.get(providerName)?.get(modelId);
|
|
358
|
-
}
|
|
359
|
-
applyConfiguredModelOverride(providerName, model) {
|
|
360
|
-
const modelOverride = this.getConfiguredModelOverride(providerName, model.id);
|
|
361
|
-
return modelOverride ? applyModelOverride(model, modelOverride) : model;
|
|
362
|
-
}
|
|
363
|
-
/** Merge custom models into built-in list by provider+id (custom wins on conflicts). */
|
|
364
|
-
mergeCustomModels(builtInModels, customModels) {
|
|
365
|
-
const merged = [...builtInModels];
|
|
366
|
-
for (const customModel of customModels) {
|
|
367
|
-
const existingIndex = merged.findIndex((m) => m.provider === customModel.provider && m.id === customModel.id);
|
|
368
|
-
if (existingIndex >= 0) {
|
|
369
|
-
merged[existingIndex] = customModel;
|
|
370
|
-
}
|
|
371
|
-
else {
|
|
372
|
-
merged.push(customModel);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
return merged;
|
|
376
|
-
}
|
|
377
|
-
loadCustomModels(modelsJsonPath) {
|
|
378
|
-
if (!existsSync(modelsJsonPath)) {
|
|
379
|
-
return emptyCustomModelsResult();
|
|
380
|
-
}
|
|
381
|
-
try {
|
|
382
|
-
const content = readFileSync(modelsJsonPath, "utf-8");
|
|
383
|
-
const parsed = JSON.parse(stripJsonComments(content));
|
|
384
|
-
if (!validateModelsConfig.Check(parsed)) {
|
|
385
|
-
const errors = validateModelsConfig
|
|
386
|
-
.Errors(parsed)
|
|
387
|
-
.map((error) => ` - ${formatValidationPath(error)}: ${error.message}`)
|
|
388
|
-
.join("\n") || "Unknown schema error";
|
|
389
|
-
return emptyCustomModelsResult(`Invalid models.json schema:\n${errors}\n\nFile: ${modelsJsonPath}`);
|
|
390
|
-
}
|
|
391
|
-
const config = parsed;
|
|
392
|
-
// Additional validation
|
|
393
|
-
this.validateConfig(config);
|
|
394
|
-
const overrides = new Map();
|
|
395
|
-
const modelOverrides = new Map();
|
|
396
|
-
for (const [providerName, providerConfig] of Object.entries(config.providers)) {
|
|
397
|
-
if (providerConfig.baseUrl || providerConfig.compat) {
|
|
398
|
-
overrides.set(providerName, {
|
|
399
|
-
baseUrl: providerConfig.baseUrl,
|
|
400
|
-
compat: providerConfig.compat,
|
|
401
|
-
});
|
|
402
|
-
}
|
|
403
|
-
if (providerConfig.oauth === "radius") {
|
|
404
|
-
// Must run before the modifyModels loop in loadModels() so the
|
|
405
|
-
// credential-cached catalog is injected on this load.
|
|
406
|
-
registerCustomRadiusOAuthProvider(providerName, providerConfig.name, providerConfig.baseUrl);
|
|
407
|
-
}
|
|
408
|
-
this.storeProviderRequestConfig(providerName, providerConfig);
|
|
409
|
-
if (providerConfig.modelOverrides) {
|
|
410
|
-
modelOverrides.set(providerName, new Map(Object.entries(providerConfig.modelOverrides)));
|
|
411
|
-
for (const [modelId, modelOverride] of Object.entries(providerConfig.modelOverrides)) {
|
|
412
|
-
this.storeModelHeaders(providerName, modelId, modelOverride.headers);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
return { models: this.parseModels(config), overrides, modelOverrides, error: undefined };
|
|
417
|
-
}
|
|
418
|
-
catch (error) {
|
|
419
|
-
if (error instanceof SyntaxError) {
|
|
420
|
-
return emptyCustomModelsResult(`Failed to parse models.json: ${error.message}\n\nFile: ${modelsJsonPath}`);
|
|
421
|
-
}
|
|
422
|
-
return emptyCustomModelsResult(`Failed to load models.json: ${error instanceof Error ? error.message : error}\n\nFile: ${modelsJsonPath}`);
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
validateConfig(config) {
|
|
426
|
-
const builtInProviders = new Set(getProviders());
|
|
427
|
-
for (const [providerName, providerConfig] of Object.entries(config.providers)) {
|
|
428
|
-
const isBuiltIn = builtInProviders.has(providerName);
|
|
429
|
-
const hasProviderApi = !!providerConfig.api;
|
|
430
|
-
const models = providerConfig.models ?? [];
|
|
431
|
-
const hasModelOverrides = providerConfig.modelOverrides && Object.keys(providerConfig.modelOverrides).length > 0;
|
|
432
|
-
if (providerConfig.oauth && !providerConfig.baseUrl) {
|
|
433
|
-
throw new Error(`Provider ${providerName}: "baseUrl" is required when "oauth" is set.`);
|
|
434
|
-
}
|
|
435
|
-
if (models.length === 0 && !providerConfig.oauth) {
|
|
436
|
-
// Override-only config: needs baseUrl, headers, compat, modelOverrides, or some combination.
|
|
437
|
-
if (!providerConfig.baseUrl && !providerConfig.headers && !providerConfig.compat && !hasModelOverrides) {
|
|
438
|
-
throw new Error(`Provider ${providerName}: must specify "baseUrl", "headers", "compat", "modelOverrides", or "models".`);
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
else if (!isBuiltIn) {
|
|
442
|
-
// Non-built-in providers with custom models require an endpoint.
|
|
443
|
-
// Auth can come from auth.json, --api-key, or provider request config.
|
|
444
|
-
if (!providerConfig.baseUrl) {
|
|
445
|
-
throw new Error(`Provider ${providerName}: "baseUrl" is required when defining custom models.`);
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
// Built-in providers with custom models: baseUrl/apiKey/api are optional,
|
|
449
|
-
// inherited from built-in models. Auth comes from env vars / auth storage.
|
|
450
|
-
for (const modelDef of models) {
|
|
451
|
-
const hasModelApi = !!modelDef.api;
|
|
452
|
-
if (!hasProviderApi && !hasModelApi && !isBuiltIn) {
|
|
453
|
-
throw new Error(`Provider ${providerName}, model ${modelDef.id}: no "api" specified. Set at provider or model level.`);
|
|
454
|
-
}
|
|
455
|
-
// For built-in providers, api is optional — inherited from built-in models.
|
|
456
|
-
if (!modelDef.id)
|
|
457
|
-
throw new Error(`Provider ${providerName}: model missing "id"`);
|
|
458
|
-
// Validate contextWindow/maxTokens only if provided (they have defaults)
|
|
459
|
-
if (modelDef.contextWindow !== undefined && modelDef.contextWindow <= 0)
|
|
460
|
-
throw new Error(`Provider ${providerName}, model ${modelDef.id}: invalid contextWindow`);
|
|
461
|
-
if (modelDef.maxTokens !== undefined && modelDef.maxTokens <= 0)
|
|
462
|
-
throw new Error(`Provider ${providerName}, model ${modelDef.id}: invalid maxTokens`);
|
|
463
|
-
}
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
parseModels(config) {
|
|
467
|
-
const models = [];
|
|
468
|
-
const builtInProviders = new Set(getProviders());
|
|
469
|
-
// Cache built-in defaults (api, baseUrl) per provider, extracted from first model.
|
|
470
|
-
const builtInDefaultsCache = new Map();
|
|
471
|
-
const getBuiltInDefaults = (providerName) => {
|
|
472
|
-
if (!builtInProviders.has(providerName))
|
|
473
|
-
return undefined;
|
|
474
|
-
if (builtInDefaultsCache.has(providerName))
|
|
475
|
-
return builtInDefaultsCache.get(providerName);
|
|
476
|
-
const builtIn = getModels(providerName);
|
|
477
|
-
if (builtIn.length === 0)
|
|
478
|
-
return undefined;
|
|
479
|
-
const defaults = { api: builtIn[0].api, baseUrl: builtIn[0].baseUrl };
|
|
480
|
-
builtInDefaultsCache.set(providerName, defaults);
|
|
481
|
-
return defaults;
|
|
482
|
-
};
|
|
483
|
-
for (const [providerName, providerConfig] of Object.entries(config.providers)) {
|
|
484
|
-
const modelDefs = providerConfig.models ?? [];
|
|
485
|
-
if (modelDefs.length === 0)
|
|
486
|
-
continue; // Override-only, no custom models
|
|
487
|
-
const builtInDefaults = getBuiltInDefaults(providerName);
|
|
488
|
-
for (const modelDef of modelDefs) {
|
|
489
|
-
const api = modelDef.api ?? providerConfig.api ?? builtInDefaults?.api;
|
|
490
|
-
if (!api)
|
|
491
|
-
continue;
|
|
492
|
-
const baseUrl = modelDef.baseUrl ?? providerConfig.baseUrl ?? builtInDefaults?.baseUrl;
|
|
493
|
-
if (!baseUrl)
|
|
494
|
-
continue;
|
|
495
|
-
const compat = mergeCompat(providerConfig.compat, modelDef.compat);
|
|
496
|
-
this.storeModelHeaders(providerName, modelDef.id, modelDef.headers);
|
|
497
|
-
const defaultCost = { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 };
|
|
498
|
-
models.push({
|
|
499
|
-
id: modelDef.id,
|
|
500
|
-
name: modelDef.name ?? modelDef.id,
|
|
501
|
-
api: api,
|
|
502
|
-
provider: providerName,
|
|
503
|
-
baseUrl,
|
|
504
|
-
reasoning: modelDef.reasoning ?? false,
|
|
505
|
-
thinkingLevelMap: modelDef.thinkingLevelMap,
|
|
506
|
-
input: (modelDef.input ?? ["text"]),
|
|
507
|
-
cost: modelDef.cost ?? defaultCost,
|
|
508
|
-
contextWindow: modelDef.contextWindow ?? 128000,
|
|
509
|
-
maxTokens: modelDef.maxTokens ?? 16384,
|
|
510
|
-
headers: undefined,
|
|
511
|
-
compat,
|
|
512
|
-
});
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
return models;
|
|
16
|
+
return this.runtime.getError();
|
|
516
17
|
}
|
|
517
|
-
/**
|
|
518
|
-
* Get all models (built-in + custom).
|
|
519
|
-
* If models.json had errors, returns only built-in models.
|
|
520
|
-
*/
|
|
521
18
|
getAll() {
|
|
522
|
-
return this.
|
|
19
|
+
return [...this.runtime.getModels()];
|
|
523
20
|
}
|
|
524
|
-
/**
|
|
525
|
-
* Get only models that have auth configured.
|
|
526
|
-
* This is a fast check that doesn't refresh OAuth tokens.
|
|
527
|
-
*/
|
|
528
21
|
getAvailable() {
|
|
529
|
-
return this.
|
|
22
|
+
return [...this.runtime.getAvailableSnapshot()];
|
|
530
23
|
}
|
|
531
|
-
/**
|
|
532
|
-
* Find a model by provider and ID.
|
|
533
|
-
*/
|
|
534
24
|
find(provider, modelId) {
|
|
535
|
-
return this.
|
|
25
|
+
return this.runtime.getModel(provider, modelId);
|
|
536
26
|
}
|
|
537
|
-
/**
|
|
538
|
-
* Get API key for a model.
|
|
539
|
-
*/
|
|
540
27
|
hasConfiguredAuth(model) {
|
|
541
|
-
|
|
542
|
-
return (this.authStorage.hasAuth(model.provider) ||
|
|
543
|
-
(providerApiKey !== undefined && isConfigValueConfigured(providerApiKey)));
|
|
544
|
-
}
|
|
545
|
-
getModelRequestKey(provider, modelId) {
|
|
546
|
-
return `${provider}:${modelId}`;
|
|
28
|
+
return this.runtime.hasConfiguredAuth(model.provider);
|
|
547
29
|
}
|
|
548
|
-
storeProviderRequestConfig(providerName, config) {
|
|
549
|
-
if (!config.apiKey && !config.headers && !config.authHeader) {
|
|
550
|
-
return;
|
|
551
|
-
}
|
|
552
|
-
this.providerRequestConfigs.set(providerName, {
|
|
553
|
-
apiKey: config.apiKey,
|
|
554
|
-
headers: config.headers,
|
|
555
|
-
authHeader: config.authHeader,
|
|
556
|
-
});
|
|
557
|
-
}
|
|
558
|
-
storeModelHeaders(providerName, modelId, headers) {
|
|
559
|
-
const key = this.getModelRequestKey(providerName, modelId);
|
|
560
|
-
if (!headers || Object.keys(headers).length === 0) {
|
|
561
|
-
this.modelRequestHeaders.delete(key);
|
|
562
|
-
return;
|
|
563
|
-
}
|
|
564
|
-
this.modelRequestHeaders.set(key, headers);
|
|
565
|
-
}
|
|
566
|
-
/**
|
|
567
|
-
* Get API key and request headers for a model.
|
|
568
|
-
*/
|
|
569
30
|
async getApiKeyAndHeaders(model) {
|
|
570
31
|
try {
|
|
571
|
-
const
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
(providerConfig?.apiKey
|
|
576
|
-
? resolveConfigValueOrThrow(providerConfig.apiKey, `API key for provider "${model.provider}"`, providerEnv)
|
|
577
|
-
: undefined);
|
|
578
|
-
const providerHeaders = resolveHeadersOrThrow(providerConfig?.headers, `provider "${model.provider}"`, providerEnv);
|
|
579
|
-
const modelHeaders = resolveHeadersOrThrow(this.modelRequestHeaders.get(this.getModelRequestKey(model.provider, model.id)), `model "${model.provider}/${model.id}"`, providerEnv);
|
|
580
|
-
let headers = model.headers || providerHeaders || modelHeaders
|
|
581
|
-
? { ...model.headers, ...providerHeaders, ...modelHeaders }
|
|
582
|
-
: undefined;
|
|
583
|
-
if (providerConfig?.authHeader) {
|
|
584
|
-
if (!apiKey) {
|
|
32
|
+
const resolution = await this.runtime.getAuth(model);
|
|
33
|
+
if (!resolution) {
|
|
34
|
+
const compatibility = this.runtime.getCompatibilityRequestConfig(model);
|
|
35
|
+
if (compatibility.authHeader) {
|
|
585
36
|
return { ok: false, error: `No API key found for "${model.provider}"` };
|
|
586
37
|
}
|
|
587
|
-
headers =
|
|
38
|
+
const headers = compatibility.headers
|
|
39
|
+
? Object.fromEntries(Object.entries(compatibility.headers).filter((entry) => entry[1] !== null))
|
|
40
|
+
: undefined;
|
|
41
|
+
return { ok: true, headers };
|
|
588
42
|
}
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
env: providerEnv && Object.keys(providerEnv).length > 0 ? providerEnv : undefined,
|
|
594
|
-
};
|
|
43
|
+
const headers = resolution.auth.headers
|
|
44
|
+
? Object.fromEntries(Object.entries(resolution.auth.headers).filter((entry) => entry[1] !== null))
|
|
45
|
+
: undefined;
|
|
46
|
+
return { ok: true, apiKey: resolution.auth.apiKey, headers, env: resolution.env };
|
|
595
47
|
}
|
|
596
48
|
catch (error) {
|
|
49
|
+
const cause = error instanceof Error ? error.cause : undefined;
|
|
50
|
+
const message = cause instanceof Error ? cause.message : error instanceof Error ? error.message : String(error);
|
|
597
51
|
return {
|
|
598
52
|
ok: false,
|
|
599
|
-
error:
|
|
53
|
+
error: message === "authHeader requires a resolved API key"
|
|
54
|
+
? `No API key found for "${model.provider}"`
|
|
55
|
+
: message,
|
|
600
56
|
};
|
|
601
57
|
}
|
|
602
58
|
}
|
|
603
|
-
/**
|
|
604
|
-
* Return auth status for a provider, including request auth configured in models.json.
|
|
605
|
-
* This intentionally does not execute command-backed config values.
|
|
606
|
-
*/
|
|
607
59
|
getProviderAuthStatus(provider) {
|
|
608
|
-
|
|
609
|
-
if (authStatus.source) {
|
|
610
|
-
return authStatus;
|
|
611
|
-
}
|
|
612
|
-
const providerApiKey = this.providerRequestConfigs.get(provider)?.apiKey;
|
|
613
|
-
if (!providerApiKey) {
|
|
614
|
-
return authStatus;
|
|
615
|
-
}
|
|
616
|
-
if (isCommandConfigValue(providerApiKey)) {
|
|
617
|
-
return { configured: true, source: "models_json_command" };
|
|
618
|
-
}
|
|
619
|
-
const envVarNames = getConfigValueEnvVarNames(providerApiKey);
|
|
620
|
-
if (envVarNames.length > 0) {
|
|
621
|
-
return isConfigValueConfigured(providerApiKey)
|
|
622
|
-
? { configured: true, source: "environment", label: envVarNames.join(", ") }
|
|
623
|
-
: { configured: false };
|
|
624
|
-
}
|
|
625
|
-
return { configured: true, source: "models_json_key" };
|
|
60
|
+
return this.runtime.getProviderAuthStatus(provider);
|
|
626
61
|
}
|
|
627
|
-
/**
|
|
628
|
-
* Get display name for a provider.
|
|
629
|
-
*/
|
|
630
62
|
getProviderDisplayName(provider) {
|
|
631
|
-
|
|
632
|
-
const oauthProvider = this.authStorage.getOAuthProviders().find((p) => p.id === provider);
|
|
633
|
-
return (registeredProvider?.name ??
|
|
634
|
-
registeredProvider?.oauth?.name ??
|
|
635
|
-
oauthProvider?.name ??
|
|
636
|
-
BUILT_IN_PROVIDER_DISPLAY_NAMES[provider] ??
|
|
637
|
-
provider);
|
|
63
|
+
return this.runtime.getProvider(provider)?.name ?? provider;
|
|
638
64
|
}
|
|
639
|
-
/**
|
|
640
|
-
* Get API key for a provider.
|
|
641
|
-
*/
|
|
642
65
|
async getApiKeyForProvider(provider) {
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
66
|
+
try {
|
|
67
|
+
return (await this.runtime.getAuth(provider))?.auth.apiKey;
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return undefined;
|
|
646
71
|
}
|
|
647
|
-
const providerApiKey = this.providerRequestConfigs.get(provider)?.apiKey;
|
|
648
|
-
return providerApiKey
|
|
649
|
-
? resolveConfigValueUncached(providerApiKey, this.authStorage.getProviderEnv(provider))
|
|
650
|
-
: undefined;
|
|
651
72
|
}
|
|
652
|
-
/**
|
|
653
|
-
* Check if a model is using OAuth credentials (subscription).
|
|
654
|
-
*/
|
|
655
73
|
isUsingOAuth(model) {
|
|
656
|
-
|
|
657
|
-
return cred?.type === "oauth";
|
|
74
|
+
return this.runtime.isUsingOAuth(model.provider);
|
|
658
75
|
}
|
|
659
|
-
/**
|
|
660
|
-
* Register a provider dynamically (from extensions).
|
|
661
|
-
*
|
|
662
|
-
* If provider has models: replaces all existing models for this provider.
|
|
663
|
-
* If provider has only baseUrl/headers: overrides existing models' URLs.
|
|
664
|
-
* If provider has oauth: registers OAuth provider for /login support.
|
|
665
|
-
*/
|
|
666
76
|
registerProvider(providerName, config) {
|
|
667
|
-
this.
|
|
668
|
-
this.applyProviderConfig(providerName, config);
|
|
669
|
-
this.upsertRegisteredProvider(providerName, config);
|
|
77
|
+
this.runtime.registerProvider(providerName, config);
|
|
670
78
|
}
|
|
671
|
-
/**
|
|
672
|
-
* Unregister a previously registered provider.
|
|
673
|
-
*
|
|
674
|
-
* Removes the provider from the registry and reloads models from disk so that
|
|
675
|
-
* built-in models overridden by this provider are restored to their original state.
|
|
676
|
-
* Also resets dynamic OAuth and API stream registrations before reapplying
|
|
677
|
-
* remaining dynamic providers.
|
|
678
|
-
* Has no effect if the provider was never registered.
|
|
679
|
-
*/
|
|
680
79
|
unregisterProvider(providerName) {
|
|
681
|
-
|
|
682
|
-
return;
|
|
683
|
-
this.registeredProviders.delete(providerName);
|
|
684
|
-
this.refresh();
|
|
80
|
+
this.runtime.unregisterProvider(providerName);
|
|
685
81
|
}
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
* If the provider is already registered, defined values in the incoming config
|
|
689
|
-
* override existing ones; undefined values are preserved from the stored config.
|
|
690
|
-
* If the provider is not registered, the incoming config is stored as-is.
|
|
691
|
-
*/
|
|
692
|
-
upsertRegisteredProvider(providerName, config) {
|
|
693
|
-
const existing = this.registeredProviders.get(providerName);
|
|
694
|
-
if (!existing) {
|
|
695
|
-
this.registeredProviders.set(providerName, config);
|
|
696
|
-
return;
|
|
697
|
-
}
|
|
698
|
-
for (const k of Object.keys(config)) {
|
|
699
|
-
if (config[k] !== undefined) {
|
|
700
|
-
existing[k] = config[k];
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
}
|
|
704
|
-
validateProviderConfig(providerName, config) {
|
|
705
|
-
if (config.streamSimple && !config.api) {
|
|
706
|
-
throw new Error(`Provider ${providerName}: "api" is required when registering streamSimple.`);
|
|
707
|
-
}
|
|
708
|
-
if (!config.models || config.models.length === 0) {
|
|
709
|
-
return;
|
|
710
|
-
}
|
|
711
|
-
if (!config.baseUrl) {
|
|
712
|
-
throw new Error(`Provider ${providerName}: "baseUrl" is required when defining models.`);
|
|
713
|
-
}
|
|
714
|
-
if (!config.apiKey && !config.oauth) {
|
|
715
|
-
throw new Error(`Provider ${providerName}: "apiKey" or "oauth" is required when defining models.`);
|
|
716
|
-
}
|
|
717
|
-
for (const modelDef of config.models) {
|
|
718
|
-
const api = modelDef.api || config.api;
|
|
719
|
-
if (!api) {
|
|
720
|
-
throw new Error(`Provider ${providerName}, model ${modelDef.id}: no "api" specified.`);
|
|
721
|
-
}
|
|
722
|
-
}
|
|
82
|
+
getRegisteredProviderConfig(providerName) {
|
|
83
|
+
return this.runtime.getRegisteredProviderConfig(providerName);
|
|
723
84
|
}
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
if (config.oauth) {
|
|
727
|
-
// Ensure the OAuth provider ID matches the provider name
|
|
728
|
-
const oauthProvider = {
|
|
729
|
-
...config.oauth,
|
|
730
|
-
id: providerName,
|
|
731
|
-
};
|
|
732
|
-
registerOAuthProvider(oauthProvider);
|
|
733
|
-
}
|
|
734
|
-
if (config.streamSimple) {
|
|
735
|
-
const streamSimple = config.streamSimple;
|
|
736
|
-
registerApiProvider({
|
|
737
|
-
api: config.api,
|
|
738
|
-
stream: (model, context, options) => streamSimple(model, context, options),
|
|
739
|
-
streamSimple,
|
|
740
|
-
}, `provider:${providerName}`);
|
|
741
|
-
}
|
|
742
|
-
this.storeProviderRequestConfig(providerName, config);
|
|
743
|
-
if (config.models && config.models.length > 0) {
|
|
744
|
-
// Full replacement: remove existing models for this provider
|
|
745
|
-
this.models = this.models.filter((m) => m.provider !== providerName);
|
|
746
|
-
// Parse and add new models
|
|
747
|
-
for (const modelDef of config.models) {
|
|
748
|
-
const api = modelDef.api || config.api;
|
|
749
|
-
const modelOverride = this.getConfiguredModelOverride(providerName, modelDef.id);
|
|
750
|
-
const headers = modelDef.headers || modelOverride?.headers
|
|
751
|
-
? { ...modelDef.headers, ...modelOverride?.headers }
|
|
752
|
-
: undefined;
|
|
753
|
-
this.storeModelHeaders(providerName, modelDef.id, headers);
|
|
754
|
-
const model = this.applyConfiguredModelOverride(providerName, {
|
|
755
|
-
id: modelDef.id,
|
|
756
|
-
name: modelDef.name,
|
|
757
|
-
api: api,
|
|
758
|
-
provider: providerName,
|
|
759
|
-
baseUrl: modelDef.baseUrl ?? config.baseUrl,
|
|
760
|
-
reasoning: modelDef.reasoning,
|
|
761
|
-
thinkingLevelMap: modelDef.thinkingLevelMap,
|
|
762
|
-
input: modelDef.input,
|
|
763
|
-
cost: modelDef.cost,
|
|
764
|
-
contextWindow: modelDef.contextWindow,
|
|
765
|
-
maxTokens: modelDef.maxTokens,
|
|
766
|
-
headers: undefined,
|
|
767
|
-
compat: modelDef.compat,
|
|
768
|
-
});
|
|
769
|
-
this.models.push(model);
|
|
770
|
-
}
|
|
771
|
-
// Apply OAuth modifyModels if credentials exist (e.g., to update baseUrl)
|
|
772
|
-
if (config.oauth?.modifyModels) {
|
|
773
|
-
const cred = this.authStorage.get(providerName);
|
|
774
|
-
if (cred?.type === "oauth") {
|
|
775
|
-
this.models = config.oauth.modifyModels(this.models, cred);
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
else if (config.baseUrl || config.headers) {
|
|
780
|
-
// Override-only: update baseUrl for existing models. Request headers are resolved per request.
|
|
781
|
-
this.models = this.models.map((m) => {
|
|
782
|
-
if (m.provider !== providerName)
|
|
783
|
-
return m;
|
|
784
|
-
return {
|
|
785
|
-
...m,
|
|
786
|
-
baseUrl: config.baseUrl ?? m.baseUrl,
|
|
787
|
-
};
|
|
788
|
-
});
|
|
789
|
-
}
|
|
85
|
+
getRegisteredProviderIds() {
|
|
86
|
+
return this.runtime.getRegisteredProviderIds();
|
|
790
87
|
}
|
|
791
88
|
}
|
|
792
89
|
//# sourceMappingURL=model-registry.js.map
|