@contractspec/bundle.library 3.0.0 → 3.2.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.
Files changed (50) hide show
  1. package/.turbo/turbo-build.log +178 -166
  2. package/AGENTS.md +19 -12
  3. package/CHANGELOG.md +74 -0
  4. package/dist/application/context-storage/index.d.ts +18 -0
  5. package/dist/application/context-storage/index.js +29 -0
  6. package/dist/application/index.d.ts +1 -0
  7. package/dist/application/index.js +662 -2
  8. package/dist/application/mcp/cliMcp.js +12 -2
  9. package/dist/application/mcp/common.d.ts +11 -1
  10. package/dist/application/mcp/common.js +12 -2
  11. package/dist/application/mcp/contractsMcp.d.ts +51 -0
  12. package/dist/application/mcp/contractsMcp.js +531 -0
  13. package/dist/application/mcp/contractsMcpResources.d.ts +7 -0
  14. package/dist/application/mcp/contractsMcpResources.js +124 -0
  15. package/dist/application/mcp/contractsMcpTools.d.ts +9 -0
  16. package/dist/application/mcp/contractsMcpTools.js +200 -0
  17. package/dist/application/mcp/contractsMcpTypes.d.ts +50 -0
  18. package/dist/application/mcp/contractsMcpTypes.js +1 -0
  19. package/dist/application/mcp/docsMcp.js +12 -2
  20. package/dist/application/mcp/index.d.ts +2 -0
  21. package/dist/application/mcp/index.js +635 -2
  22. package/dist/application/mcp/internalMcp.js +12 -2
  23. package/dist/application/mcp/providerRankingMcp.d.ts +46 -0
  24. package/dist/application/mcp/providerRankingMcp.js +494 -0
  25. package/dist/node/application/context-storage/index.js +28 -0
  26. package/dist/node/application/index.js +662 -2
  27. package/dist/node/application/mcp/cliMcp.js +12 -2
  28. package/dist/node/application/mcp/common.js +12 -2
  29. package/dist/node/application/mcp/contractsMcp.js +530 -0
  30. package/dist/node/application/mcp/contractsMcpResources.js +123 -0
  31. package/dist/node/application/mcp/contractsMcpTools.js +199 -0
  32. package/dist/node/application/mcp/contractsMcpTypes.js +0 -0
  33. package/dist/node/application/mcp/docsMcp.js +12 -2
  34. package/dist/node/application/mcp/index.js +635 -2
  35. package/dist/node/application/mcp/internalMcp.js +12 -2
  36. package/dist/node/application/mcp/providerRankingMcp.js +493 -0
  37. package/package.json +113 -25
  38. package/src/application/context-storage/index.ts +58 -0
  39. package/src/application/index.ts +1 -0
  40. package/src/application/mcp/common.ts +28 -1
  41. package/src/application/mcp/contractsMcp.ts +34 -0
  42. package/src/application/mcp/contractsMcpResources.ts +142 -0
  43. package/src/application/mcp/contractsMcpTools.ts +246 -0
  44. package/src/application/mcp/contractsMcpTypes.ts +47 -0
  45. package/src/application/mcp/index.ts +2 -0
  46. package/src/application/mcp/providerRankingMcp.ts +380 -0
  47. package/src/components/docs/generated/docs-index._common.json +879 -1
  48. package/src/components/docs/generated/docs-index.manifest.json +5 -5
  49. package/src/components/docs/generated/docs-index.metrics.json +8 -0
  50. package/src/components/docs/generated/docs-index.platform-integrations.json +8 -0
@@ -0,0 +1,380 @@
1
+ import {
2
+ definePrompt,
3
+ defineResourceTemplate,
4
+ installOp,
5
+ OperationSpecRegistry,
6
+ PromptRegistry,
7
+ ResourceRegistry,
8
+ } from '@contractspec/lib.contracts-spec';
9
+ import {
10
+ BenchmarkIngestCommand,
11
+ BenchmarkRunCustomCommand,
12
+ RankingRefreshCommand,
13
+ } from '@contractspec/lib.contracts-spec/provider-ranking';
14
+ import z from 'zod';
15
+ import { createMcpElysiaHandler } from './common';
16
+ import { appLogger } from '../../infrastructure/elysia/logger';
17
+ import type { ProviderRankingStore } from '@contractspec/lib.provider-ranking/store';
18
+ import type {
19
+ ProviderTransportSupport,
20
+ ProviderAuthSupport,
21
+ } from '@contractspec/lib.provider-ranking/types';
22
+ import { InMemoryProviderRankingStore } from '@contractspec/lib.provider-ranking/in-memory-store';
23
+ import { createDefaultIngesterRegistry } from '@contractspec/lib.provider-ranking/ingesters';
24
+ import { computeModelRankings } from '@contractspec/lib.provider-ranking/scoring';
25
+ import { normalizeBenchmarkResults } from '@contractspec/lib.provider-ranking/scoring';
26
+
27
+ const TransportFilterSchema = z
28
+ .enum(['rest', 'mcp', 'webhook', 'sdk'])
29
+ .optional();
30
+ const AuthFilterSchema = z
31
+ .enum([
32
+ 'api-key',
33
+ 'oauth2',
34
+ 'bearer',
35
+ 'header',
36
+ 'basic',
37
+ 'webhook-signing',
38
+ 'service-account',
39
+ ])
40
+ .optional();
41
+
42
+ const RANKING_TAGS = ['ranking', 'mcp', 'ai'];
43
+ const RANKING_OWNERS = ['platform.ai'];
44
+
45
+ let sharedStore: ProviderRankingStore | null = null;
46
+
47
+ function getStore(): ProviderRankingStore {
48
+ if (!sharedStore) {
49
+ sharedStore = new InMemoryProviderRankingStore();
50
+ }
51
+ return sharedStore;
52
+ }
53
+
54
+ function buildRankingResources() {
55
+ const resources = new ResourceRegistry();
56
+
57
+ resources.register(
58
+ defineResourceTemplate({
59
+ meta: {
60
+ uriTemplate: 'ranking://leaderboard',
61
+ title: 'AI Model Leaderboard',
62
+ description:
63
+ 'Current ranked list of AI models by composite score. Supports optional transport and authMethod query filters.',
64
+ mimeType: 'application/json',
65
+ tags: RANKING_TAGS,
66
+ },
67
+ input: z.object({
68
+ transport: TransportFilterSchema,
69
+ authMethod: AuthFilterSchema,
70
+ }),
71
+ resolve: async ({ transport, authMethod }) => {
72
+ const store = getStore();
73
+ const result = await store.listModelRankings({
74
+ limit: 100,
75
+ requiredTransport: transport as ProviderTransportSupport | undefined,
76
+ requiredAuthMethod: authMethod as ProviderAuthSupport | undefined,
77
+ });
78
+ return {
79
+ uri: 'ranking://leaderboard',
80
+ mimeType: 'application/json',
81
+ data: JSON.stringify(result, null, 2),
82
+ };
83
+ },
84
+ })
85
+ );
86
+
87
+ resources.register(
88
+ defineResourceTemplate({
89
+ meta: {
90
+ uriTemplate: 'ranking://leaderboard/{dimension}',
91
+ title: 'AI Model Leaderboard by Dimension',
92
+ description:
93
+ 'Ranked list of AI models filtered by a specific dimension. Supports optional transport and authMethod query filters.',
94
+ mimeType: 'application/json',
95
+ tags: RANKING_TAGS,
96
+ },
97
+ input: z.object({
98
+ dimension: z.string(),
99
+ transport: TransportFilterSchema,
100
+ authMethod: AuthFilterSchema,
101
+ }),
102
+ resolve: async ({ dimension, transport, authMethod }) => {
103
+ const store = getStore();
104
+ const result = await store.listModelRankings({
105
+ dimension: dimension as
106
+ | 'coding'
107
+ | 'reasoning'
108
+ | 'agentic'
109
+ | 'cost'
110
+ | 'latency'
111
+ | 'context'
112
+ | 'safety'
113
+ | 'custom',
114
+ limit: 100,
115
+ requiredTransport: transport as ProviderTransportSupport | undefined,
116
+ requiredAuthMethod: authMethod as ProviderAuthSupport | undefined,
117
+ });
118
+ return {
119
+ uri: `ranking://leaderboard/${encodeURIComponent(dimension)}`,
120
+ mimeType: 'application/json',
121
+ data: JSON.stringify(result, null, 2),
122
+ };
123
+ },
124
+ })
125
+ );
126
+
127
+ resources.register(
128
+ defineResourceTemplate({
129
+ meta: {
130
+ uriTemplate: 'ranking://model/{modelId}',
131
+ title: 'AI Model Profile',
132
+ description:
133
+ 'Detailed profile for a specific AI model including scores and benchmarks.',
134
+ mimeType: 'application/json',
135
+ tags: RANKING_TAGS,
136
+ },
137
+ input: z.object({ modelId: z.string() }),
138
+ resolve: async ({ modelId }) => {
139
+ const store = getStore();
140
+ const profile = await store.getModelProfile(modelId);
141
+ if (!profile) {
142
+ return {
143
+ uri: `ranking://model/${encodeURIComponent(modelId)}`,
144
+ mimeType: 'application/json',
145
+ data: JSON.stringify({ error: 'not_found', modelId }),
146
+ };
147
+ }
148
+ return {
149
+ uri: `ranking://model/${encodeURIComponent(modelId)}`,
150
+ mimeType: 'application/json',
151
+ data: JSON.stringify(profile, null, 2),
152
+ };
153
+ },
154
+ })
155
+ );
156
+
157
+ resources.register(
158
+ defineResourceTemplate({
159
+ meta: {
160
+ uriTemplate: 'ranking://results',
161
+ title: 'Benchmark Results',
162
+ description: 'List of raw benchmark results from all ingested sources.',
163
+ mimeType: 'application/json',
164
+ tags: RANKING_TAGS,
165
+ },
166
+ input: z.object({}),
167
+ resolve: async () => {
168
+ const store = getStore();
169
+ const result = await store.listBenchmarkResults({ limit: 200 });
170
+ return {
171
+ uri: 'ranking://results',
172
+ mimeType: 'application/json',
173
+ data: JSON.stringify(result, null, 2),
174
+ };
175
+ },
176
+ })
177
+ );
178
+
179
+ return resources;
180
+ }
181
+
182
+ function buildRankingPrompts() {
183
+ const prompts = new PromptRegistry();
184
+
185
+ prompts.register(
186
+ definePrompt({
187
+ meta: {
188
+ key: 'ranking.advisor',
189
+ version: '1.0.0',
190
+ title: 'AI Model Advisor',
191
+ description:
192
+ 'Which AI model is best for a given task? Uses the leaderboard to recommend.',
193
+ tags: RANKING_TAGS,
194
+ stability: 'beta',
195
+ owners: RANKING_OWNERS,
196
+ },
197
+ args: [
198
+ {
199
+ name: 'task',
200
+ description: 'The task or use case to recommend a model for.',
201
+ required: true,
202
+ schema: z.string(),
203
+ },
204
+ {
205
+ name: 'priority',
206
+ description:
207
+ 'Priority dimension (coding, reasoning, cost, latency, etc.).',
208
+ required: false,
209
+ schema: z.string().optional(),
210
+ },
211
+ {
212
+ name: 'transport',
213
+ description: 'Required transport type (rest, mcp, webhook, sdk).',
214
+ required: false,
215
+ schema: TransportFilterSchema,
216
+ },
217
+ {
218
+ name: 'authMethod',
219
+ description: 'Required auth method (api-key, oauth2, bearer, etc.).',
220
+ required: false,
221
+ schema: AuthFilterSchema,
222
+ },
223
+ ],
224
+ input: z.object({
225
+ task: z.string(),
226
+ priority: z.string().optional(),
227
+ transport: TransportFilterSchema,
228
+ authMethod: AuthFilterSchema,
229
+ }),
230
+ render: async ({ task, priority, transport, authMethod }) => {
231
+ const constraints: string[] = [];
232
+ if (priority) constraints.push(`Prioritize: ${priority}.`);
233
+ if (transport) constraints.push(`Required transport: ${transport}.`);
234
+ if (authMethod) constraints.push(`Required auth: ${authMethod}.`);
235
+
236
+ return [
237
+ {
238
+ type: 'text' as const,
239
+ text: `Recommend the best AI model for: "${task}".${constraints.length ? ` ${constraints.join(' ')}` : ''} Use the leaderboard data to justify your recommendation.`,
240
+ },
241
+ {
242
+ type: 'resource' as const,
243
+ uri: priority
244
+ ? `ranking://leaderboard/${priority}`
245
+ : 'ranking://leaderboard',
246
+ title: 'Leaderboard',
247
+ },
248
+ ];
249
+ },
250
+ })
251
+ );
252
+
253
+ return prompts;
254
+ }
255
+
256
+ function buildRankingOps() {
257
+ const registry = new OperationSpecRegistry();
258
+ const ingesterRegistry = createDefaultIngesterRegistry();
259
+
260
+ installOp(registry, BenchmarkIngestCommand, async (args) => {
261
+ const store = getStore();
262
+ const source = args.source as string;
263
+ const ingester = ingesterRegistry.get(
264
+ source as Parameters<typeof ingesterRegistry.get>[0]
265
+ );
266
+ if (!ingester) {
267
+ throw new Error(`No ingester registered for source: ${source}`);
268
+ }
269
+
270
+ const rawResults = await ingester.ingest({
271
+ sourceUrl: args.sourceUrl as string | undefined,
272
+ dimensions: args.dimensions as string[] | undefined as Parameters<
273
+ typeof ingester.ingest
274
+ >[0] extends undefined
275
+ ? never
276
+ : NonNullable<Parameters<typeof ingester.ingest>[0]>['dimensions'],
277
+ });
278
+ const normalized = normalizeBenchmarkResults(rawResults);
279
+
280
+ for (const result of normalized) {
281
+ await store.upsertBenchmarkResult(result);
282
+ }
283
+
284
+ return {
285
+ ingestionId: `ingest-${source}-${Date.now()}`,
286
+ source,
287
+ resultsCount: normalized.length,
288
+ status: 'completed',
289
+ ingestedAt: new Date(),
290
+ };
291
+ });
292
+
293
+ installOp(registry, BenchmarkRunCustomCommand, async (args) => {
294
+ return {
295
+ runId: `custom-${Date.now()}`,
296
+ evalSuiteKey: args.evalSuiteKey,
297
+ modelId: args.modelId,
298
+ status: 'started',
299
+ startedAt: new Date(),
300
+ };
301
+ });
302
+
303
+ installOp(registry, RankingRefreshCommand, async (args) => {
304
+ const store = getStore();
305
+ const allResults = [];
306
+ let offset = 0;
307
+ const pageSize = 500;
308
+
309
+ while (true) {
310
+ const page = await store.listBenchmarkResults({
311
+ limit: pageSize,
312
+ offset,
313
+ });
314
+ allResults.push(...page.results);
315
+ if (allResults.length >= page.total || page.results.length < pageSize)
316
+ break;
317
+ offset += pageSize;
318
+ }
319
+
320
+ const existingRankings = new Map(
321
+ (await store.listModelRankings({ limit: 10000 })).rankings.map((r) => [
322
+ r.modelId,
323
+ r,
324
+ ])
325
+ );
326
+
327
+ const weightOverrides = args.weightOverrides
328
+ ? Array.isArray(args.weightOverrides)
329
+ ? args.weightOverrides
330
+ : [args.weightOverrides]
331
+ : undefined;
332
+
333
+ const newRankings = computeModelRankings(
334
+ allResults,
335
+ weightOverrides
336
+ ? {
337
+ weightOverrides: weightOverrides as Parameters<
338
+ typeof computeModelRankings
339
+ >[1] extends undefined
340
+ ? never
341
+ : NonNullable<
342
+ Parameters<typeof computeModelRankings>[1]
343
+ >['weightOverrides'],
344
+ }
345
+ : undefined,
346
+ existingRankings
347
+ );
348
+
349
+ for (const ranking of newRankings) {
350
+ await store.upsertModelRanking(ranking);
351
+ }
352
+
353
+ return {
354
+ modelsRanked: newRankings.length,
355
+ updatedAt: new Date(),
356
+ status: 'completed',
357
+ };
358
+ });
359
+
360
+ return registry;
361
+ }
362
+
363
+ export function createProviderRankingMcpHandler(path = '/api/mcp/ranking') {
364
+ return createMcpElysiaHandler({
365
+ logger: appLogger,
366
+ path,
367
+ serverName: 'contractspec-ranking-mcp',
368
+ ops: buildRankingOps(),
369
+ resources: buildRankingResources(),
370
+ prompts: buildRankingPrompts(),
371
+ });
372
+ }
373
+
374
+ /**
375
+ * Allows external callers to inject a custom store
376
+ * (e.g. PostgresProviderRankingStore from the module layer).
377
+ */
378
+ export function setProviderRankingStore(store: ProviderRankingStore): void {
379
+ sharedStore = store;
380
+ }