@contractspec/lib.ai-providers 3.0.0 → 3.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/dist/browser/factory.js +58 -84
- package/dist/browser/index.js +227 -84
- package/dist/browser/legacy.js +58 -84
- package/dist/browser/selector-types.js +0 -0
- package/dist/browser/selector.js +693 -0
- package/dist/browser/validation.js +58 -84
- package/dist/factory.js +58 -84
- package/dist/index.d.ts +3 -0
- package/dist/index.js +227 -84
- package/dist/legacy.js +58 -84
- package/dist/node/factory.js +58 -84
- package/dist/node/index.js +227 -84
- package/dist/node/legacy.js +58 -84
- package/dist/node/selector-types.js +0 -0
- package/dist/node/selector.js +693 -0
- package/dist/node/validation.js +58 -84
- package/dist/selector-types.d.ts +50 -0
- package/dist/selector-types.js +1 -0
- package/dist/selector.d.ts +16 -0
- package/dist/selector.js +694 -0
- package/dist/types.d.ts +12 -0
- package/dist/validation.js +58 -84
- package/package.json +37 -8
|
@@ -0,0 +1,693 @@
|
|
|
1
|
+
// src/models.ts
|
|
2
|
+
var DEFAULT_MODELS = {
|
|
3
|
+
ollama: "llama3.2",
|
|
4
|
+
openai: "gpt-4o",
|
|
5
|
+
anthropic: "claude-sonnet-4-20250514",
|
|
6
|
+
mistral: "mistral-large-latest",
|
|
7
|
+
gemini: "gemini-2.0-flash"
|
|
8
|
+
};
|
|
9
|
+
var MODELS = [
|
|
10
|
+
{
|
|
11
|
+
id: "llama3.2",
|
|
12
|
+
name: "Llama 3.2",
|
|
13
|
+
provider: "ollama",
|
|
14
|
+
contextWindow: 128000,
|
|
15
|
+
capabilities: {
|
|
16
|
+
vision: false,
|
|
17
|
+
tools: true,
|
|
18
|
+
reasoning: false,
|
|
19
|
+
streaming: true
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
id: "codellama",
|
|
24
|
+
name: "Code Llama",
|
|
25
|
+
provider: "ollama",
|
|
26
|
+
contextWindow: 16000,
|
|
27
|
+
capabilities: {
|
|
28
|
+
vision: false,
|
|
29
|
+
tools: false,
|
|
30
|
+
reasoning: false,
|
|
31
|
+
streaming: true
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
id: "deepseek-coder",
|
|
36
|
+
name: "DeepSeek Coder",
|
|
37
|
+
provider: "ollama",
|
|
38
|
+
contextWindow: 16000,
|
|
39
|
+
capabilities: {
|
|
40
|
+
vision: false,
|
|
41
|
+
tools: false,
|
|
42
|
+
reasoning: false,
|
|
43
|
+
streaming: true
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
id: "mistral",
|
|
48
|
+
name: "Mistral 7B",
|
|
49
|
+
provider: "ollama",
|
|
50
|
+
contextWindow: 32000,
|
|
51
|
+
capabilities: {
|
|
52
|
+
vision: false,
|
|
53
|
+
tools: false,
|
|
54
|
+
reasoning: false,
|
|
55
|
+
streaming: true
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
id: "gpt-4o",
|
|
60
|
+
name: "GPT-4o",
|
|
61
|
+
provider: "openai",
|
|
62
|
+
contextWindow: 128000,
|
|
63
|
+
capabilities: {
|
|
64
|
+
vision: true,
|
|
65
|
+
tools: true,
|
|
66
|
+
reasoning: false,
|
|
67
|
+
streaming: true
|
|
68
|
+
},
|
|
69
|
+
costPerMillion: { input: 2.5, output: 10 }
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
id: "gpt-4o-mini",
|
|
73
|
+
name: "GPT-4o Mini",
|
|
74
|
+
provider: "openai",
|
|
75
|
+
contextWindow: 128000,
|
|
76
|
+
capabilities: {
|
|
77
|
+
vision: true,
|
|
78
|
+
tools: true,
|
|
79
|
+
reasoning: false,
|
|
80
|
+
streaming: true
|
|
81
|
+
},
|
|
82
|
+
costPerMillion: { input: 0.15, output: 0.6 }
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
id: "o1",
|
|
86
|
+
name: "o1",
|
|
87
|
+
provider: "openai",
|
|
88
|
+
contextWindow: 200000,
|
|
89
|
+
capabilities: {
|
|
90
|
+
vision: true,
|
|
91
|
+
tools: true,
|
|
92
|
+
reasoning: true,
|
|
93
|
+
streaming: true
|
|
94
|
+
},
|
|
95
|
+
costPerMillion: { input: 15, output: 60 }
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
id: "o1-mini",
|
|
99
|
+
name: "o1 Mini",
|
|
100
|
+
provider: "openai",
|
|
101
|
+
contextWindow: 128000,
|
|
102
|
+
capabilities: {
|
|
103
|
+
vision: false,
|
|
104
|
+
tools: true,
|
|
105
|
+
reasoning: true,
|
|
106
|
+
streaming: true
|
|
107
|
+
},
|
|
108
|
+
costPerMillion: { input: 3, output: 12 }
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
id: "claude-sonnet-4-20250514",
|
|
112
|
+
name: "Claude Sonnet 4",
|
|
113
|
+
provider: "anthropic",
|
|
114
|
+
contextWindow: 200000,
|
|
115
|
+
capabilities: {
|
|
116
|
+
vision: true,
|
|
117
|
+
tools: true,
|
|
118
|
+
reasoning: true,
|
|
119
|
+
streaming: true
|
|
120
|
+
},
|
|
121
|
+
costPerMillion: { input: 3, output: 15 }
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: "claude-3-5-sonnet-20241022",
|
|
125
|
+
name: "Claude 3.5 Sonnet",
|
|
126
|
+
provider: "anthropic",
|
|
127
|
+
contextWindow: 200000,
|
|
128
|
+
capabilities: {
|
|
129
|
+
vision: true,
|
|
130
|
+
tools: true,
|
|
131
|
+
reasoning: false,
|
|
132
|
+
streaming: true
|
|
133
|
+
},
|
|
134
|
+
costPerMillion: { input: 3, output: 15 }
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
id: "claude-3-5-haiku-20241022",
|
|
138
|
+
name: "Claude 3.5 Haiku",
|
|
139
|
+
provider: "anthropic",
|
|
140
|
+
contextWindow: 200000,
|
|
141
|
+
capabilities: {
|
|
142
|
+
vision: true,
|
|
143
|
+
tools: true,
|
|
144
|
+
reasoning: false,
|
|
145
|
+
streaming: true
|
|
146
|
+
},
|
|
147
|
+
costPerMillion: { input: 0.8, output: 4 }
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
id: "mistral-large-latest",
|
|
151
|
+
name: "Mistral Large",
|
|
152
|
+
provider: "mistral",
|
|
153
|
+
contextWindow: 128000,
|
|
154
|
+
capabilities: {
|
|
155
|
+
vision: false,
|
|
156
|
+
tools: true,
|
|
157
|
+
reasoning: false,
|
|
158
|
+
streaming: true
|
|
159
|
+
},
|
|
160
|
+
costPerMillion: { input: 2, output: 6 }
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
id: "mistral-medium-latest",
|
|
164
|
+
name: "Mistral Medium",
|
|
165
|
+
provider: "mistral",
|
|
166
|
+
contextWindow: 128000,
|
|
167
|
+
capabilities: {
|
|
168
|
+
vision: false,
|
|
169
|
+
tools: true,
|
|
170
|
+
reasoning: false,
|
|
171
|
+
streaming: true
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
id: "codestral-latest",
|
|
176
|
+
name: "Codestral",
|
|
177
|
+
provider: "mistral",
|
|
178
|
+
contextWindow: 256000,
|
|
179
|
+
capabilities: {
|
|
180
|
+
vision: false,
|
|
181
|
+
tools: true,
|
|
182
|
+
reasoning: false,
|
|
183
|
+
streaming: true
|
|
184
|
+
},
|
|
185
|
+
costPerMillion: { input: 0.2, output: 0.6 }
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
id: "devstral-small-latest",
|
|
189
|
+
name: "Devstral Small",
|
|
190
|
+
provider: "mistral",
|
|
191
|
+
contextWindow: 128000,
|
|
192
|
+
capabilities: {
|
|
193
|
+
vision: false,
|
|
194
|
+
tools: true,
|
|
195
|
+
reasoning: true,
|
|
196
|
+
streaming: true
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
id: "magistral-medium-latest",
|
|
201
|
+
name: "Magistral Medium",
|
|
202
|
+
provider: "mistral",
|
|
203
|
+
contextWindow: 128000,
|
|
204
|
+
capabilities: {
|
|
205
|
+
vision: false,
|
|
206
|
+
tools: true,
|
|
207
|
+
reasoning: true,
|
|
208
|
+
streaming: true
|
|
209
|
+
}
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
id: "pixtral-large-latest",
|
|
213
|
+
name: "Pixtral Large",
|
|
214
|
+
provider: "mistral",
|
|
215
|
+
contextWindow: 128000,
|
|
216
|
+
capabilities: {
|
|
217
|
+
vision: true,
|
|
218
|
+
tools: true,
|
|
219
|
+
reasoning: false,
|
|
220
|
+
streaming: true
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
id: "mistral-small-latest",
|
|
225
|
+
name: "Mistral Small",
|
|
226
|
+
provider: "mistral",
|
|
227
|
+
contextWindow: 32000,
|
|
228
|
+
capabilities: {
|
|
229
|
+
vision: false,
|
|
230
|
+
tools: true,
|
|
231
|
+
reasoning: false,
|
|
232
|
+
streaming: true
|
|
233
|
+
},
|
|
234
|
+
costPerMillion: { input: 0.2, output: 0.6 }
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
id: "gemini-2.0-flash",
|
|
238
|
+
name: "Gemini 2.0 Flash",
|
|
239
|
+
provider: "gemini",
|
|
240
|
+
contextWindow: 1e6,
|
|
241
|
+
capabilities: {
|
|
242
|
+
vision: true,
|
|
243
|
+
tools: true,
|
|
244
|
+
reasoning: false,
|
|
245
|
+
streaming: true
|
|
246
|
+
},
|
|
247
|
+
costPerMillion: { input: 0.075, output: 0.3 }
|
|
248
|
+
},
|
|
249
|
+
{
|
|
250
|
+
id: "gemini-2.5-pro-preview-06-05",
|
|
251
|
+
name: "Gemini 2.5 Pro",
|
|
252
|
+
provider: "gemini",
|
|
253
|
+
contextWindow: 1e6,
|
|
254
|
+
capabilities: {
|
|
255
|
+
vision: true,
|
|
256
|
+
tools: true,
|
|
257
|
+
reasoning: true,
|
|
258
|
+
streaming: true
|
|
259
|
+
},
|
|
260
|
+
costPerMillion: { input: 1.25, output: 10 }
|
|
261
|
+
},
|
|
262
|
+
{
|
|
263
|
+
id: "gemini-2.5-flash-preview-05-20",
|
|
264
|
+
name: "Gemini 2.5 Flash",
|
|
265
|
+
provider: "gemini",
|
|
266
|
+
contextWindow: 1e6,
|
|
267
|
+
capabilities: {
|
|
268
|
+
vision: true,
|
|
269
|
+
tools: true,
|
|
270
|
+
reasoning: true,
|
|
271
|
+
streaming: true
|
|
272
|
+
},
|
|
273
|
+
costPerMillion: { input: 0.15, output: 0.6 }
|
|
274
|
+
}
|
|
275
|
+
];
|
|
276
|
+
function getModelsForProvider(provider) {
|
|
277
|
+
return MODELS.filter((m) => m.provider === provider);
|
|
278
|
+
}
|
|
279
|
+
function getModelInfo(modelId) {
|
|
280
|
+
return MODELS.find((m) => m.id === modelId);
|
|
281
|
+
}
|
|
282
|
+
function getRecommendedModels(provider) {
|
|
283
|
+
const normalizedProvider = provider === "claude" ? "anthropic" : provider === "custom" ? "openai" : provider;
|
|
284
|
+
return getModelsForProvider(normalizedProvider).map((m) => m.id);
|
|
285
|
+
}
|
|
286
|
+
function getDefaultModel(provider) {
|
|
287
|
+
return DEFAULT_MODELS[provider];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// src/factory.ts
|
|
291
|
+
import { createAnthropic } from "@ai-sdk/anthropic";
|
|
292
|
+
import { createGoogleGenerativeAI } from "@ai-sdk/google";
|
|
293
|
+
import { createMistral } from "@ai-sdk/mistral";
|
|
294
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
295
|
+
import { createOllama } from "ollama-ai-provider";
|
|
296
|
+
class BaseProvider {
|
|
297
|
+
name;
|
|
298
|
+
model;
|
|
299
|
+
mode;
|
|
300
|
+
config;
|
|
301
|
+
transport;
|
|
302
|
+
authMethod;
|
|
303
|
+
apiVersion;
|
|
304
|
+
customHeaders;
|
|
305
|
+
cachedModel = null;
|
|
306
|
+
constructor(config) {
|
|
307
|
+
this.name = config.provider;
|
|
308
|
+
this.model = config.model ?? DEFAULT_MODELS[config.provider];
|
|
309
|
+
this.mode = this.determineMode(config);
|
|
310
|
+
this.config = config;
|
|
311
|
+
this.transport = config.transport;
|
|
312
|
+
this.authMethod = config.authMethod;
|
|
313
|
+
this.apiVersion = config.apiVersion;
|
|
314
|
+
this.customHeaders = config.customHeaders;
|
|
315
|
+
}
|
|
316
|
+
getModel() {
|
|
317
|
+
if (!this.cachedModel) {
|
|
318
|
+
this.cachedModel = this.createModel();
|
|
319
|
+
}
|
|
320
|
+
return this.cachedModel;
|
|
321
|
+
}
|
|
322
|
+
async listModels() {
|
|
323
|
+
if (this.name === "ollama") {
|
|
324
|
+
return this.listOllamaModels();
|
|
325
|
+
}
|
|
326
|
+
return getModelsForProvider(this.name);
|
|
327
|
+
}
|
|
328
|
+
async validate() {
|
|
329
|
+
if (this.name === "ollama") {
|
|
330
|
+
return this.validateOllama();
|
|
331
|
+
}
|
|
332
|
+
if (this.mode === "byok" && !this.config.apiKey) {
|
|
333
|
+
return {
|
|
334
|
+
valid: false,
|
|
335
|
+
error: `API key required for ${this.name}`
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
if (this.mode === "managed" && !this.config.proxyUrl && !this.config.organizationId) {
|
|
339
|
+
return {
|
|
340
|
+
valid: false,
|
|
341
|
+
error: "Managed mode requires proxyUrl or organizationId"
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
return { valid: true };
|
|
345
|
+
}
|
|
346
|
+
determineMode(config) {
|
|
347
|
+
if (config.provider === "ollama")
|
|
348
|
+
return "local";
|
|
349
|
+
if (config.apiKey)
|
|
350
|
+
return "byok";
|
|
351
|
+
return "managed";
|
|
352
|
+
}
|
|
353
|
+
createModel() {
|
|
354
|
+
const { baseUrl, proxyUrl, apiKey } = this.config;
|
|
355
|
+
const headers = this.customHeaders;
|
|
356
|
+
if (this.name === "ollama") {
|
|
357
|
+
const provider = createOllama({ baseURL: baseUrl, headers });
|
|
358
|
+
return provider(this.model);
|
|
359
|
+
}
|
|
360
|
+
if (this.mode === "managed" && proxyUrl) {
|
|
361
|
+
const provider = createOpenAI({ baseURL: proxyUrl, apiKey, headers });
|
|
362
|
+
return provider(this.model);
|
|
363
|
+
}
|
|
364
|
+
switch (this.name) {
|
|
365
|
+
case "openai": {
|
|
366
|
+
const provider = createOpenAI({ apiKey, headers });
|
|
367
|
+
return provider(this.model);
|
|
368
|
+
}
|
|
369
|
+
case "anthropic": {
|
|
370
|
+
const provider = createAnthropic({ apiKey, headers });
|
|
371
|
+
return provider(this.model);
|
|
372
|
+
}
|
|
373
|
+
case "mistral": {
|
|
374
|
+
const provider = createMistral({ apiKey, headers });
|
|
375
|
+
return provider(this.model);
|
|
376
|
+
}
|
|
377
|
+
case "gemini": {
|
|
378
|
+
const provider = createGoogleGenerativeAI({ apiKey, headers });
|
|
379
|
+
return provider(this.model);
|
|
380
|
+
}
|
|
381
|
+
default:
|
|
382
|
+
throw new Error(`Unknown provider: ${this.name}`);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
async listOllamaModels() {
|
|
386
|
+
try {
|
|
387
|
+
const baseUrl = this.config.baseUrl ?? "http://localhost:11434";
|
|
388
|
+
const response = await fetch(`${baseUrl}/api/tags`);
|
|
389
|
+
if (!response.ok) {
|
|
390
|
+
return getModelsForProvider("ollama");
|
|
391
|
+
}
|
|
392
|
+
const data = await response.json();
|
|
393
|
+
const models = data.models ?? [];
|
|
394
|
+
return models.map((m) => ({
|
|
395
|
+
id: m.name,
|
|
396
|
+
name: m.name,
|
|
397
|
+
provider: "ollama",
|
|
398
|
+
contextWindow: 8000,
|
|
399
|
+
capabilities: {
|
|
400
|
+
vision: false,
|
|
401
|
+
tools: false,
|
|
402
|
+
reasoning: false,
|
|
403
|
+
streaming: true
|
|
404
|
+
}
|
|
405
|
+
}));
|
|
406
|
+
} catch {
|
|
407
|
+
return getModelsForProvider("ollama");
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
async validateOllama() {
|
|
411
|
+
try {
|
|
412
|
+
const baseUrl = this.config.baseUrl ?? "http://localhost:11434";
|
|
413
|
+
const response = await fetch(`${baseUrl}/api/tags`);
|
|
414
|
+
if (!response.ok) {
|
|
415
|
+
return {
|
|
416
|
+
valid: false,
|
|
417
|
+
error: `Ollama server returned ${response.status}`
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
const data = await response.json();
|
|
421
|
+
const models = data.models ?? [];
|
|
422
|
+
const hasModel = models.some((m) => m.name === this.model);
|
|
423
|
+
if (!hasModel) {
|
|
424
|
+
return {
|
|
425
|
+
valid: false,
|
|
426
|
+
error: `Model "${this.model}" not found. Available: ${models.map((m) => m.name).join(", ")}`
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
return { valid: true };
|
|
430
|
+
} catch (error) {
|
|
431
|
+
const baseUrl = this.config.baseUrl ?? "http://localhost:11434";
|
|
432
|
+
return {
|
|
433
|
+
valid: false,
|
|
434
|
+
error: `Cannot connect to Ollama at ${baseUrl}: ${error instanceof Error ? error.message : String(error)}`
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
function createProvider(config) {
|
|
440
|
+
return new BaseProvider(config);
|
|
441
|
+
}
|
|
442
|
+
function createProviderFromEnv() {
|
|
443
|
+
const provider = process.env.CONTRACTSPEC_AI_PROVIDER ?? "openai";
|
|
444
|
+
const model = process.env.CONTRACTSPEC_AI_MODEL;
|
|
445
|
+
let apiKey;
|
|
446
|
+
switch (provider) {
|
|
447
|
+
case "openai":
|
|
448
|
+
apiKey = process.env.OPENAI_API_KEY;
|
|
449
|
+
break;
|
|
450
|
+
case "anthropic":
|
|
451
|
+
apiKey = process.env.ANTHROPIC_API_KEY;
|
|
452
|
+
break;
|
|
453
|
+
case "mistral":
|
|
454
|
+
apiKey = process.env.MISTRAL_API_KEY;
|
|
455
|
+
break;
|
|
456
|
+
case "gemini":
|
|
457
|
+
apiKey = process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY;
|
|
458
|
+
break;
|
|
459
|
+
case "ollama":
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
const transport = process.env.CONTRACTSPEC_AI_TRANSPORT;
|
|
463
|
+
const apiVersion = process.env.CONTRACTSPEC_AI_API_VERSION;
|
|
464
|
+
return createProvider({
|
|
465
|
+
provider,
|
|
466
|
+
model,
|
|
467
|
+
apiKey,
|
|
468
|
+
baseUrl: process.env.OLLAMA_BASE_URL,
|
|
469
|
+
proxyUrl: process.env.CONTRACTSPEC_AI_PROXY_URL,
|
|
470
|
+
organizationId: process.env.CONTRACTSPEC_ORG_ID,
|
|
471
|
+
transport,
|
|
472
|
+
apiVersion
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
function getAvailableProviders() {
|
|
476
|
+
const providers = [];
|
|
477
|
+
providers.push({
|
|
478
|
+
provider: "ollama",
|
|
479
|
+
available: true,
|
|
480
|
+
mode: "local",
|
|
481
|
+
transports: ["rest", "sdk"],
|
|
482
|
+
authMethods: []
|
|
483
|
+
});
|
|
484
|
+
const openaiKey = process.env.OPENAI_API_KEY;
|
|
485
|
+
providers.push({
|
|
486
|
+
provider: "openai",
|
|
487
|
+
available: Boolean(openaiKey) || Boolean(process.env.CONTRACTSPEC_AI_PROXY_URL),
|
|
488
|
+
mode: openaiKey ? "byok" : "managed",
|
|
489
|
+
reason: !openaiKey ? "Set OPENAI_API_KEY for BYOK mode" : undefined,
|
|
490
|
+
transports: ["rest", "sdk"],
|
|
491
|
+
authMethods: ["api-key"]
|
|
492
|
+
});
|
|
493
|
+
const anthropicKey = process.env.ANTHROPIC_API_KEY;
|
|
494
|
+
providers.push({
|
|
495
|
+
provider: "anthropic",
|
|
496
|
+
available: Boolean(anthropicKey) || Boolean(process.env.CONTRACTSPEC_AI_PROXY_URL),
|
|
497
|
+
mode: anthropicKey ? "byok" : "managed",
|
|
498
|
+
reason: !anthropicKey ? "Set ANTHROPIC_API_KEY for BYOK mode" : undefined,
|
|
499
|
+
transports: ["rest", "sdk"],
|
|
500
|
+
authMethods: ["api-key"]
|
|
501
|
+
});
|
|
502
|
+
const mistralKey = process.env.MISTRAL_API_KEY;
|
|
503
|
+
providers.push({
|
|
504
|
+
provider: "mistral",
|
|
505
|
+
available: Boolean(mistralKey) || Boolean(process.env.CONTRACTSPEC_AI_PROXY_URL),
|
|
506
|
+
mode: mistralKey ? "byok" : "managed",
|
|
507
|
+
reason: !mistralKey ? "Set MISTRAL_API_KEY for BYOK mode" : undefined,
|
|
508
|
+
transports: ["rest", "sdk"],
|
|
509
|
+
authMethods: ["api-key"]
|
|
510
|
+
});
|
|
511
|
+
const geminiKey = process.env.GOOGLE_API_KEY ?? process.env.GEMINI_API_KEY;
|
|
512
|
+
providers.push({
|
|
513
|
+
provider: "gemini",
|
|
514
|
+
available: Boolean(geminiKey) || Boolean(process.env.CONTRACTSPEC_AI_PROXY_URL),
|
|
515
|
+
mode: geminiKey ? "byok" : "managed",
|
|
516
|
+
reason: !geminiKey ? "Set GOOGLE_API_KEY for BYOK mode" : undefined,
|
|
517
|
+
transports: ["rest", "sdk"],
|
|
518
|
+
authMethods: ["api-key"]
|
|
519
|
+
});
|
|
520
|
+
return providers;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// src/selector.ts
|
|
524
|
+
function createModelSelector(options) {
|
|
525
|
+
const { store, fallbackModels, defaultConstraints } = options;
|
|
526
|
+
const catalog = fallbackModels ?? MODELS;
|
|
527
|
+
return {
|
|
528
|
+
async select(context) {
|
|
529
|
+
const merged = mergeConstraints(defaultConstraints, context.constraints);
|
|
530
|
+
if (context.priorities?.length) {
|
|
531
|
+
return selectMultiObjective(store, catalog, context.priorities, merged);
|
|
532
|
+
}
|
|
533
|
+
const dimension = context.taskDimension ?? "reasoning";
|
|
534
|
+
return selectByDimension(store, catalog, dimension, merged);
|
|
535
|
+
},
|
|
536
|
+
async selectAndCreate(context) {
|
|
537
|
+
const selection = await this.select(context);
|
|
538
|
+
const model = createProvider({
|
|
539
|
+
provider: selection.providerKey,
|
|
540
|
+
model: selection.modelId
|
|
541
|
+
}).getModel();
|
|
542
|
+
return { model, selection };
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
}
|
|
546
|
+
async function selectByDimension(store, catalog, dimension, constraints) {
|
|
547
|
+
const { rankings } = await store.listModelRankings({ dimension, limit: 50 });
|
|
548
|
+
const eligible = filterRankings(rankings, catalog, constraints);
|
|
549
|
+
const topCandidate = eligible[0];
|
|
550
|
+
if (topCandidate) {
|
|
551
|
+
const dimScore = topCandidate.dimensionScores[dimension]?.score ?? topCandidate.compositeScore;
|
|
552
|
+
return {
|
|
553
|
+
modelId: topCandidate.modelId,
|
|
554
|
+
providerKey: topCandidate.providerKey,
|
|
555
|
+
score: dimScore,
|
|
556
|
+
reason: `Top-ranked for "${dimension}" (score ${Math.round(dimScore)})`,
|
|
557
|
+
alternatives: eligible.slice(1, 4).map((r) => ({
|
|
558
|
+
modelId: r.modelId,
|
|
559
|
+
providerKey: r.providerKey,
|
|
560
|
+
score: r.dimensionScores[dimension]?.score ?? r.compositeScore
|
|
561
|
+
}))
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
return fallbackFromCatalog(catalog, constraints, dimension);
|
|
565
|
+
}
|
|
566
|
+
async function selectMultiObjective(store, catalog, priorities, constraints) {
|
|
567
|
+
const { rankings } = await store.listModelRankings({ limit: 100 });
|
|
568
|
+
const eligible = filterRankings(rankings, catalog, constraints);
|
|
569
|
+
if (eligible.length === 0) {
|
|
570
|
+
const primaryDim = priorities.reduce((a, b) => b.weight > a.weight ? b : a).dimension;
|
|
571
|
+
return fallbackFromCatalog(catalog, constraints, primaryDim);
|
|
572
|
+
}
|
|
573
|
+
const totalWeight = priorities.reduce((sum, p) => sum + p.weight, 0) || 1;
|
|
574
|
+
const scored = eligible.map((r) => {
|
|
575
|
+
let weightedScore = 0;
|
|
576
|
+
for (const p of priorities) {
|
|
577
|
+
const dimScore = r.dimensionScores[p.dimension]?.score ?? 0;
|
|
578
|
+
weightedScore += dimScore * (p.weight / totalWeight);
|
|
579
|
+
}
|
|
580
|
+
return { ranking: r, weightedScore };
|
|
581
|
+
});
|
|
582
|
+
scored.sort((a, b) => b.weightedScore - a.weightedScore);
|
|
583
|
+
const best = scored[0];
|
|
584
|
+
if (!best) {
|
|
585
|
+
const primaryDim = priorities.reduce((a, b) => b.weight > a.weight ? b : a).dimension;
|
|
586
|
+
return fallbackFromCatalog(catalog, constraints, primaryDim);
|
|
587
|
+
}
|
|
588
|
+
const dims = priorities.map((p) => p.dimension).join(", ");
|
|
589
|
+
return {
|
|
590
|
+
modelId: best.ranking.modelId,
|
|
591
|
+
providerKey: best.ranking.providerKey,
|
|
592
|
+
score: Math.round(best.weightedScore * 100) / 100,
|
|
593
|
+
reason: `Multi-objective optimum across [${dims}]`,
|
|
594
|
+
alternatives: scored.slice(1, 4).map((s) => ({
|
|
595
|
+
modelId: s.ranking.modelId,
|
|
596
|
+
providerKey: s.ranking.providerKey,
|
|
597
|
+
score: Math.round(s.weightedScore * 100) / 100
|
|
598
|
+
}))
|
|
599
|
+
};
|
|
600
|
+
}
|
|
601
|
+
function filterRankings(rankings, catalog, constraints) {
|
|
602
|
+
return rankings.filter((r) => {
|
|
603
|
+
if (constraints.allowedProviders?.length) {
|
|
604
|
+
if (!constraints.allowedProviders.includes(r.providerKey))
|
|
605
|
+
return false;
|
|
606
|
+
}
|
|
607
|
+
if (constraints.excludeModels?.length) {
|
|
608
|
+
if (constraints.excludeModels.includes(r.modelId))
|
|
609
|
+
return false;
|
|
610
|
+
}
|
|
611
|
+
const info = getModelInfo(r.modelId) ?? catalog.find((m) => m.id === r.modelId);
|
|
612
|
+
if (!info)
|
|
613
|
+
return true;
|
|
614
|
+
if (constraints.minContextWindow && info.contextWindow < constraints.minContextWindow) {
|
|
615
|
+
return false;
|
|
616
|
+
}
|
|
617
|
+
if (constraints.maxCostPerMillionInput && info.costPerMillion) {
|
|
618
|
+
if (info.costPerMillion.input > constraints.maxCostPerMillionInput)
|
|
619
|
+
return false;
|
|
620
|
+
}
|
|
621
|
+
if (constraints.maxCostPerMillionOutput && info.costPerMillion) {
|
|
622
|
+
if (info.costPerMillion.output > constraints.maxCostPerMillionOutput)
|
|
623
|
+
return false;
|
|
624
|
+
}
|
|
625
|
+
if (constraints.requiredCapabilities?.length) {
|
|
626
|
+
for (const cap of constraints.requiredCapabilities) {
|
|
627
|
+
if (!info.capabilities[cap])
|
|
628
|
+
return false;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
return true;
|
|
632
|
+
});
|
|
633
|
+
}
|
|
634
|
+
function fallbackFromCatalog(catalog, constraints, dimension) {
|
|
635
|
+
let eligible = catalog.filter((m) => m.costPerMillion != null);
|
|
636
|
+
const {
|
|
637
|
+
allowedProviders,
|
|
638
|
+
excludeModels,
|
|
639
|
+
minContextWindow,
|
|
640
|
+
requiredCapabilities
|
|
641
|
+
} = constraints;
|
|
642
|
+
if (allowedProviders?.length) {
|
|
643
|
+
eligible = eligible.filter((m) => allowedProviders.includes(m.provider));
|
|
644
|
+
}
|
|
645
|
+
if (excludeModels?.length) {
|
|
646
|
+
eligible = eligible.filter((m) => !excludeModels.includes(m.id));
|
|
647
|
+
}
|
|
648
|
+
if (minContextWindow) {
|
|
649
|
+
eligible = eligible.filter((m) => m.contextWindow >= minContextWindow);
|
|
650
|
+
}
|
|
651
|
+
if (requiredCapabilities?.length) {
|
|
652
|
+
eligible = eligible.filter((m) => requiredCapabilities.every((cap) => m.capabilities[cap]));
|
|
653
|
+
}
|
|
654
|
+
if (eligible.length === 0) {
|
|
655
|
+
eligible = catalog.slice(0, 5);
|
|
656
|
+
}
|
|
657
|
+
eligible.sort((a, b) => {
|
|
658
|
+
const costA = a.costPerMillion ? (a.costPerMillion.input + a.costPerMillion.output) / 2 : 999;
|
|
659
|
+
const costB = b.costPerMillion ? (b.costPerMillion.input + b.costPerMillion.output) / 2 : 999;
|
|
660
|
+
return b.contextWindow / 1e5 - costB - (a.contextWindow / 1e5 - costA);
|
|
661
|
+
});
|
|
662
|
+
const best = eligible[0];
|
|
663
|
+
if (!best) {
|
|
664
|
+
return {
|
|
665
|
+
modelId: "unknown",
|
|
666
|
+
providerKey: "openai",
|
|
667
|
+
score: 0,
|
|
668
|
+
reason: `No eligible models found for "${dimension}"`,
|
|
669
|
+
alternatives: []
|
|
670
|
+
};
|
|
671
|
+
}
|
|
672
|
+
return {
|
|
673
|
+
modelId: best.id,
|
|
674
|
+
providerKey: best.provider,
|
|
675
|
+
score: 0,
|
|
676
|
+
reason: `Fallback from catalog (no ranking data for "${dimension}")`,
|
|
677
|
+
alternatives: eligible.slice(1, 4).map((m) => ({
|
|
678
|
+
modelId: m.id,
|
|
679
|
+
providerKey: m.provider,
|
|
680
|
+
score: 0
|
|
681
|
+
}))
|
|
682
|
+
};
|
|
683
|
+
}
|
|
684
|
+
function mergeConstraints(defaults, overrides) {
|
|
685
|
+
if (!defaults)
|
|
686
|
+
return overrides ?? {};
|
|
687
|
+
if (!overrides)
|
|
688
|
+
return defaults;
|
|
689
|
+
return { ...defaults, ...overrides };
|
|
690
|
+
}
|
|
691
|
+
export {
|
|
692
|
+
createModelSelector
|
|
693
|
+
};
|