@opengeni/core 2.6.4 → 2.7.5-canary.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,742 @@
1
+ import {
2
+ applyModelCatalogDocument,
3
+ configuredGatewayWorkspaceProductModelIds,
4
+ configuredGatewayOrganizationProductModelIds,
5
+ configuredModels,
6
+ configuredModelNotes,
7
+ configuredOpenRouterWorkspaceProductModelIds,
8
+ configuredOpenRouterOrganizationProductModelIds,
9
+ configuredProviders,
10
+ validateModelCatalogSettings,
11
+ withCodexCatalogProvider,
12
+ withOrganizationGatewayCatalogProvider,
13
+ withOrganizationOpenRouterCatalogProvider,
14
+ withWorkspaceGatewayCatalogProvider,
15
+ withWorkspaceOpenRouterCatalogProvider,
16
+ withXaiSubscriptionCatalogProvider,
17
+ WORKSPACE_GATEWAY_MODEL_ID_PREFIX,
18
+ WORKSPACE_OPENROUTER_MODEL_ID_PREFIX,
19
+ WORKSPACE_OPENROUTER_PROVIDER_ID,
20
+ ORGANIZATION_OPENROUTER_PROVIDER_ID,
21
+ ORGANIZATION_GATEWAY_MODEL_ID_PREFIX,
22
+ ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX,
23
+ type ConfiguredModel,
24
+ type Settings,
25
+ } from "@opengeni/config";
26
+ import {
27
+ evaluateWorkspaceModelPolicy,
28
+ type ModelAvailabilityV1,
29
+ type ModelCredentialReadinessV1,
30
+ type WorkspaceModelPolicyContract,
31
+ } from "@opengeni/contracts";
32
+ import {
33
+ getDeploymentModelCatalog,
34
+ getWorkspaceGatewayCustomModelForExecution,
35
+ getWorkspaceOpenRouterCustomModelForExecution,
36
+ getOrganizationModelProviderCustomModelForExecution,
37
+ listWorkspaceGatewayCustomModels,
38
+ listWorkspaceOpenRouterCustomModels,
39
+ listOrganizationModelProviderCustomModelsForWorkspace,
40
+ lockActiveOrganizationModelProviderCustomModelForAdmission,
41
+ lockActiveWorkspaceGatewayCustomModelForAdmission,
42
+ lockActiveWorkspaceOpenRouterCustomModelForAdmission,
43
+ type Database,
44
+ } from "@opengeni/db";
45
+
46
+ export type ResolvedCatalogSettings = {
47
+ settings: Settings;
48
+ source: "code" | "database";
49
+ version: number | null;
50
+ modelNotes: Record<string, string>;
51
+ };
52
+
53
+ /**
54
+ * Curated workspace Gateway products and workspace-owned custom slugs share
55
+ * one public prefix. Only the latter have a mutable catalog row whose active
56
+ * generation must be rechecked at a fresh acceptance commit boundary.
57
+ */
58
+ export function isWorkspaceGatewayCustomModelId(settings: Settings, modelId: string): boolean {
59
+ return (
60
+ modelId.startsWith(WORKSPACE_GATEWAY_MODEL_ID_PREFIX) &&
61
+ !configuredGatewayWorkspaceProductModelIds(settings).includes(modelId)
62
+ );
63
+ }
64
+
65
+ export function isWorkspaceOpenRouterCustomModelId(settings: Settings, modelId: string): boolean {
66
+ return (
67
+ modelId.startsWith(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX) &&
68
+ !configuredOpenRouterWorkspaceProductModelIds(settings).includes(modelId)
69
+ );
70
+ }
71
+
72
+ export type WorkspaceCustomModelReference = {
73
+ scope: "workspace" | "organization";
74
+ providerKind: "vercel_gateway" | "openrouter";
75
+ upstreamModelId: string;
76
+ };
77
+
78
+ export function workspaceCustomModelReference(
79
+ settings: Settings,
80
+ modelId: string,
81
+ ): WorkspaceCustomModelReference | null {
82
+ if (isWorkspaceGatewayCustomModelId(settings, modelId)) {
83
+ return {
84
+ scope: "workspace",
85
+ providerKind: "vercel_gateway",
86
+ upstreamModelId: modelId.slice(WORKSPACE_GATEWAY_MODEL_ID_PREFIX.length),
87
+ };
88
+ }
89
+ if (isWorkspaceOpenRouterCustomModelId(settings, modelId)) {
90
+ return {
91
+ scope: "workspace",
92
+ providerKind: "openrouter",
93
+ upstreamModelId: modelId.slice(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX.length),
94
+ };
95
+ }
96
+ if (
97
+ modelId.startsWith(ORGANIZATION_GATEWAY_MODEL_ID_PREFIX) &&
98
+ !configuredGatewayOrganizationProductModelIds(settings).includes(modelId)
99
+ ) {
100
+ return {
101
+ scope: "organization",
102
+ providerKind: "vercel_gateway",
103
+ upstreamModelId: modelId.slice(ORGANIZATION_GATEWAY_MODEL_ID_PREFIX.length),
104
+ };
105
+ }
106
+ if (
107
+ modelId.startsWith(ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX) &&
108
+ !configuredOpenRouterOrganizationProductModelIds(settings).includes(modelId)
109
+ ) {
110
+ return {
111
+ scope: "organization",
112
+ providerKind: "openrouter",
113
+ upstreamModelId: modelId.slice(ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX.length),
114
+ };
115
+ }
116
+ return null;
117
+ }
118
+
119
+ export function isWorkspaceCustomModelId(settings: Settings, modelId: string): boolean {
120
+ return workspaceCustomModelReference(settings, modelId) !== null;
121
+ }
122
+
123
+ export async function lockActiveCustomModelForAdmission(
124
+ db: Database,
125
+ input: {
126
+ accountId: string;
127
+ workspaceId: string;
128
+ reference: WorkspaceCustomModelReference;
129
+ },
130
+ ): Promise<boolean> {
131
+ if (input.reference.scope === "organization") {
132
+ return Boolean(
133
+ await lockActiveOrganizationModelProviderCustomModelForAdmission(db, {
134
+ accountId: input.accountId,
135
+ workspaceId: input.workspaceId,
136
+ providerKind: input.reference.providerKind,
137
+ upstreamModelId: input.reference.upstreamModelId,
138
+ }),
139
+ );
140
+ }
141
+ return Boolean(
142
+ input.reference.providerKind === "openrouter"
143
+ ? await lockActiveWorkspaceOpenRouterCustomModelForAdmission(db, {
144
+ accountId: input.accountId,
145
+ workspaceId: input.workspaceId,
146
+ upstreamModelId: input.reference.upstreamModelId,
147
+ })
148
+ : await lockActiveWorkspaceGatewayCustomModelForAdmission(db, {
149
+ accountId: input.accountId,
150
+ workspaceId: input.workspaceId,
151
+ upstreamModelId: input.reference.upstreamModelId,
152
+ }),
153
+ );
154
+ }
155
+
156
+ /**
157
+ * Resolve the deployment catalog without making synchronous env settings read
158
+ * Postgres. Database mode fails closed when the singleton is absent or invalid;
159
+ * code mode preserves the already-validated env catalog.
160
+ */
161
+ export async function resolveCatalogSettings(
162
+ db: Database,
163
+ envSettings: Settings,
164
+ ): Promise<ResolvedCatalogSettings> {
165
+ if (envSettings.modelCatalogSource === "code") {
166
+ validateModelCatalogSettings(envSettings);
167
+ return {
168
+ settings: envSettings,
169
+ source: "code",
170
+ version: null,
171
+ modelNotes: configuredModelNotes(envSettings),
172
+ };
173
+ }
174
+
175
+ const row = await getDeploymentModelCatalog(db);
176
+ if (!row) {
177
+ throw new Error("database model catalog source is configured but the singleton row is missing");
178
+ }
179
+ const settings = applyModelCatalogDocument(envSettings, row.document);
180
+ validateModelCatalogSettings(settings);
181
+ return {
182
+ settings,
183
+ source: "database",
184
+ version: row.version,
185
+ modelNotes: configuredModelNotes(settings),
186
+ };
187
+ }
188
+
189
+ /**
190
+ * Resolve the deployment catalog and add only the custom Gateway slugs owned by
191
+ * one workspace. Use this at model-bearing workspace boundaries; public config
192
+ * and deployment-operator surfaces must continue to use `resolveCatalogSettings`.
193
+ */
194
+ export async function resolveWorkspaceCatalogSettings(
195
+ db: Database,
196
+ envSettings: Settings,
197
+ input: {
198
+ accountId: string;
199
+ workspaceId: string;
200
+ retainedProductModelId?: string | null;
201
+ retainedProductModelIds?: readonly (string | null | undefined)[];
202
+ },
203
+ ): Promise<ResolvedCatalogSettings> {
204
+ // A few pure catalog tests inject the historical minimal DB port and mock the
205
+ // workspace model helpers directly. Real/injected runtime databases always
206
+ // expose transactions; keep that narrow test port compatible.
207
+ const supportsOrganizationProviderReads =
208
+ typeof (db as Database & { transaction?: unknown }).transaction === "function";
209
+ const retainedProductModelIds = [
210
+ ...(input.retainedProductModelIds ?? []),
211
+ input.retainedProductModelId,
212
+ ];
213
+ const retainedGatewayUpstreamModelIds = retainedProductModelIds.flatMap((productModelId) =>
214
+ productModelId?.startsWith(WORKSPACE_GATEWAY_MODEL_ID_PREFIX)
215
+ ? [productModelId.slice(WORKSPACE_GATEWAY_MODEL_ID_PREFIX.length)]
216
+ : [],
217
+ );
218
+ const retainedOpenRouterUpstreamModelIds = retainedProductModelIds.flatMap((productModelId) =>
219
+ productModelId?.startsWith(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX)
220
+ ? [productModelId.slice(WORKSPACE_OPENROUTER_MODEL_ID_PREFIX.length)]
221
+ : [],
222
+ );
223
+ const retainedOrganizationGatewayUpstreamModelIds = retainedProductModelIds.flatMap(
224
+ (productModelId) =>
225
+ productModelId?.startsWith(ORGANIZATION_GATEWAY_MODEL_ID_PREFIX)
226
+ ? [productModelId.slice(ORGANIZATION_GATEWAY_MODEL_ID_PREFIX.length)]
227
+ : [],
228
+ );
229
+ const retainedOrganizationOpenRouterUpstreamModelIds = retainedProductModelIds.flatMap(
230
+ (productModelId) =>
231
+ productModelId?.startsWith(ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX)
232
+ ? [productModelId.slice(ORGANIZATION_OPENROUTER_MODEL_ID_PREFIX.length)]
233
+ : [],
234
+ );
235
+ const [
236
+ resolved,
237
+ activeGatewayCustomModels,
238
+ activeOpenRouterCustomModels,
239
+ retainedGatewayCustomModels,
240
+ retainedOpenRouterCustomModels,
241
+ organizationGatewayCustomModels,
242
+ organizationOpenRouterCustomModels,
243
+ retainedOrganizationGatewayCustomModels,
244
+ retainedOrganizationOpenRouterCustomModels,
245
+ ] = await Promise.all([
246
+ resolveCatalogSettings(db, envSettings),
247
+ listWorkspaceGatewayCustomModels(db, {
248
+ accountId: input.accountId,
249
+ workspaceId: input.workspaceId,
250
+ }),
251
+ listWorkspaceOpenRouterCustomModels(db, {
252
+ accountId: input.accountId,
253
+ workspaceId: input.workspaceId,
254
+ }),
255
+ Promise.all(
256
+ [...new Set(retainedGatewayUpstreamModelIds)].map(
257
+ async (upstreamModelId) =>
258
+ await getWorkspaceGatewayCustomModelForExecution(db, {
259
+ accountId: input.accountId,
260
+ workspaceId: input.workspaceId,
261
+ upstreamModelId,
262
+ }),
263
+ ),
264
+ ),
265
+ Promise.all(
266
+ [...new Set(retainedOpenRouterUpstreamModelIds)].map(
267
+ async (upstreamModelId) =>
268
+ await getWorkspaceOpenRouterCustomModelForExecution(db, {
269
+ accountId: input.accountId,
270
+ workspaceId: input.workspaceId,
271
+ upstreamModelId,
272
+ }),
273
+ ),
274
+ ),
275
+ supportsOrganizationProviderReads
276
+ ? listOrganizationModelProviderCustomModelsForWorkspace(db, {
277
+ accountId: input.accountId,
278
+ workspaceId: input.workspaceId,
279
+ providerKind: "vercel_gateway",
280
+ })
281
+ : Promise.resolve([]),
282
+ supportsOrganizationProviderReads
283
+ ? listOrganizationModelProviderCustomModelsForWorkspace(db, {
284
+ accountId: input.accountId,
285
+ workspaceId: input.workspaceId,
286
+ providerKind: "openrouter",
287
+ })
288
+ : Promise.resolve([]),
289
+ supportsOrganizationProviderReads
290
+ ? Promise.all(
291
+ [...new Set(retainedOrganizationGatewayUpstreamModelIds)].map(
292
+ async (upstreamModelId) =>
293
+ await getOrganizationModelProviderCustomModelForExecution(db, {
294
+ accountId: input.accountId,
295
+ workspaceId: input.workspaceId,
296
+ providerKind: "vercel_gateway",
297
+ upstreamModelId,
298
+ }),
299
+ ),
300
+ )
301
+ : Promise.resolve([]),
302
+ supportsOrganizationProviderReads
303
+ ? Promise.all(
304
+ [...new Set(retainedOrganizationOpenRouterUpstreamModelIds)].map(
305
+ async (upstreamModelId) =>
306
+ await getOrganizationModelProviderCustomModelForExecution(db, {
307
+ accountId: input.accountId,
308
+ workspaceId: input.workspaceId,
309
+ providerKind: "openrouter",
310
+ upstreamModelId,
311
+ }),
312
+ ),
313
+ )
314
+ : Promise.resolve([]),
315
+ ]);
316
+ const includeRetainedModels = <T extends { upstreamModelId: string }>(
317
+ activeModels: readonly T[],
318
+ retainedModels: readonly (T | null)[],
319
+ ): T[] => {
320
+ const customModels = [...activeModels];
321
+ const includedUpstreamModelIds = new Set(activeModels.map((model) => model.upstreamModelId));
322
+ for (const retainedCustomModel of retainedModels) {
323
+ if (
324
+ retainedCustomModel &&
325
+ !includedUpstreamModelIds.has(retainedCustomModel.upstreamModelId)
326
+ ) {
327
+ customModels.push(retainedCustomModel);
328
+ includedUpstreamModelIds.add(retainedCustomModel.upstreamModelId);
329
+ }
330
+ }
331
+ return customModels;
332
+ };
333
+ const gatewayCustomModels = includeRetainedModels(
334
+ activeGatewayCustomModels,
335
+ retainedGatewayCustomModels,
336
+ );
337
+ const openRouterCustomModels = includeRetainedModels(
338
+ activeOpenRouterCustomModels,
339
+ retainedOpenRouterCustomModels,
340
+ );
341
+ const organizationGatewayModels = includeRetainedModels(
342
+ organizationGatewayCustomModels,
343
+ retainedOrganizationGatewayCustomModels,
344
+ );
345
+ const organizationOpenRouterModels = includeRetainedModels(
346
+ organizationOpenRouterCustomModels,
347
+ retainedOrganizationOpenRouterCustomModels,
348
+ );
349
+ return {
350
+ ...resolved,
351
+ settings: withOrganizationOpenRouterCatalogProvider(
352
+ withOrganizationGatewayCatalogProvider(
353
+ withWorkspaceOpenRouterCatalogProvider(
354
+ withWorkspaceGatewayCatalogProvider(resolved.settings, gatewayCustomModels),
355
+ openRouterCustomModels,
356
+ ),
357
+ organizationGatewayModels,
358
+ ),
359
+ organizationOpenRouterModels,
360
+ ),
361
+ };
362
+ }
363
+
364
+ export type ModelAvailabilityObservation = {
365
+ status: "available" | "degraded" | "unavailable";
366
+ reason: "not_entitled" | "provider_unhealthy" | null;
367
+ checkedAt: string;
368
+ };
369
+
370
+ export type ModelCredentialReadinessObservation =
371
+ | { status: "ready"; checkedAt: string }
372
+ | {
373
+ status: "not_ready";
374
+ reason: "prerequisites_missing" | "needs_reauth";
375
+ checkedAt: string;
376
+ }
377
+ | { status: "error"; reason: "resolver_error"; checkedAt: string };
378
+
379
+ export const MODEL_CREDENTIAL_READINESS_OBSERVATION_MAX_AGE_MS = 5 * 60_000;
380
+
381
+ export type WorkspaceModelSelectionInput = {
382
+ settings: Settings;
383
+ policy: WorkspaceModelPolicyContract | null;
384
+ codexSubscriptionActive: boolean;
385
+ xaiSubscriptionActive?: boolean;
386
+ workspaceGatewayConnectionActive?: boolean;
387
+ workspaceOpenRouterConnectionActive?: boolean;
388
+ organizationGatewayConnectionActive?: boolean;
389
+ organizationOpenRouterConnectionActive?: boolean;
390
+ workspaceGatewayCustomModels?: readonly {
391
+ upstreamModelId: string;
392
+ label?: string | null;
393
+ }[];
394
+ workspaceOpenRouterCustomModels?: readonly {
395
+ upstreamModelId: string;
396
+ label?: string | null;
397
+ }[];
398
+ organizationGatewayCustomModels?: readonly {
399
+ upstreamModelId: string;
400
+ label?: string | null;
401
+ }[];
402
+ organizationOpenRouterCustomModels?: readonly {
403
+ upstreamModelId: string;
404
+ label?: string | null;
405
+ }[];
406
+ credentialReadinessObservations?:
407
+ | Readonly<Record<string, ModelCredentialReadinessObservation>>
408
+ | undefined;
409
+ observations?: Readonly<Record<string, ModelAvailabilityObservation>> | undefined;
410
+ now?: Date | undefined;
411
+ credentialReadinessMaxAgeMs?: number | undefined;
412
+ };
413
+
414
+ export type WorkspaceModelSelection = {
415
+ model: ConfiguredModel;
416
+ credentialReadiness: ModelCredentialReadinessV1;
417
+ policyAllowed: boolean;
418
+ availability: ModelAvailabilityV1;
419
+ };
420
+
421
+ function modelDefinitionRunnable(model: ConfiguredModel): boolean {
422
+ return (
423
+ model.capabilities.inputModalities.includes("text") &&
424
+ model.capabilities.outputModalities.includes("text") &&
425
+ model.capabilities.transports.sse.runnable
426
+ );
427
+ }
428
+
429
+ function observedCredentialReadiness(input: {
430
+ observation: ModelCredentialReadinessObservation | undefined;
431
+ basis: "connection" | "resolver";
432
+ nowMs: number;
433
+ maxAgeMs: number;
434
+ }): ModelCredentialReadinessV1 {
435
+ if (!input.observation) {
436
+ return {
437
+ status: "not_ready",
438
+ reason: "prerequisites_missing",
439
+ basis: input.basis,
440
+ checkedAt: null,
441
+ };
442
+ }
443
+ const checkedAtMs = Date.parse(input.observation.checkedAt);
444
+ if (!Number.isFinite(checkedAtMs)) {
445
+ return {
446
+ status: "error",
447
+ reason: "resolver_error",
448
+ basis: input.basis,
449
+ checkedAt: null,
450
+ };
451
+ }
452
+ const checkedAt = new Date(checkedAtMs).toISOString();
453
+ if (Math.abs(input.nowMs - checkedAtMs) > input.maxAgeMs) {
454
+ return {
455
+ status: "not_ready",
456
+ reason: "observation_stale",
457
+ basis: input.basis,
458
+ checkedAt,
459
+ };
460
+ }
461
+ if (input.observation.status === "ready") {
462
+ return { status: "ready", reason: null, basis: input.basis, checkedAt };
463
+ }
464
+ if (input.observation.status === "not_ready") {
465
+ return {
466
+ status: "not_ready",
467
+ reason:
468
+ input.observation.reason === "needs_reauth" ? "needs_reauth" : "prerequisites_missing",
469
+ basis: input.basis,
470
+ checkedAt,
471
+ };
472
+ }
473
+ return {
474
+ status: "error",
475
+ reason: "resolver_error",
476
+ basis: input.basis,
477
+ checkedAt,
478
+ };
479
+ }
480
+
481
+ function credentialReadinessFor(input: {
482
+ model: ConfiguredModel;
483
+ provider: ReturnType<typeof configuredProviders>[number] | undefined;
484
+ codexSubscriptionActive: boolean;
485
+ xaiSubscriptionActive: boolean;
486
+ workspaceGatewayConnectionActive: boolean;
487
+ workspaceOpenRouterConnectionActive: boolean;
488
+ organizationGatewayConnectionActive: boolean;
489
+ organizationOpenRouterConnectionActive: boolean;
490
+ observation: ModelCredentialReadinessObservation | undefined;
491
+ nowMs: number;
492
+ maxAgeMs: number;
493
+ }): ModelCredentialReadinessV1 {
494
+ const source = input.model.credentialSource;
495
+ if (source.kind === "connected_subscription") {
496
+ const active =
497
+ source.provider === "xai" ? input.xaiSubscriptionActive : input.codexSubscriptionActive;
498
+ return active
499
+ ? { status: "ready", reason: null, basis: "connection", checkedAt: null }
500
+ : {
501
+ status: "not_ready",
502
+ reason: "needs_reauth",
503
+ basis: "connection",
504
+ checkedAt: null,
505
+ };
506
+ }
507
+ if (source.kind === "workspace_connection") {
508
+ const connectionActive =
509
+ input.model.providerId === WORKSPACE_OPENROUTER_PROVIDER_ID
510
+ ? input.workspaceOpenRouterConnectionActive
511
+ : input.workspaceGatewayConnectionActive;
512
+ return connectionActive
513
+ ? { status: "ready", reason: null, basis: "connection", checkedAt: null }
514
+ : {
515
+ status: "not_ready",
516
+ reason: "needs_reauth",
517
+ basis: "connection",
518
+ checkedAt: null,
519
+ };
520
+ }
521
+ if (source.kind === "organization_connection") {
522
+ const connectionActive =
523
+ input.model.providerId === ORGANIZATION_OPENROUTER_PROVIDER_ID
524
+ ? input.organizationOpenRouterConnectionActive
525
+ : input.organizationGatewayConnectionActive;
526
+ return connectionActive
527
+ ? { status: "ready", reason: null, basis: "connection", checkedAt: null }
528
+ : {
529
+ status: "not_ready",
530
+ reason: "needs_reauth",
531
+ basis: "connection",
532
+ checkedAt: null,
533
+ };
534
+ }
535
+ if (source.kind === "deployment" && source.mechanism === "none") {
536
+ return { status: "ready", reason: null, basis: "configuration", checkedAt: null };
537
+ }
538
+ if (source.kind === "deployment" && source.mechanism === "api_key") {
539
+ return input.provider?.apiKey
540
+ ? { status: "ready", reason: null, basis: "configuration", checkedAt: null }
541
+ : {
542
+ status: "not_ready",
543
+ reason: "missing_credential",
544
+ basis: "configuration",
545
+ checkedAt: null,
546
+ };
547
+ }
548
+ return observedCredentialReadiness({
549
+ observation: input.observation,
550
+ basis: "resolver",
551
+ nowMs: input.nowMs,
552
+ maxAgeMs: input.maxAgeMs,
553
+ });
554
+ }
555
+
556
+ function isXaiGrokModel(model: ConfiguredModel): boolean {
557
+ return model.providerId === "xai" && model.id.startsWith("xai/grok-");
558
+ }
559
+
560
+ function observationTimestamp(observation: ModelAvailabilityObservation | undefined): {
561
+ checkedAt: string | null;
562
+ checkedAtMs: number | null;
563
+ } {
564
+ if (!observation || typeof observation.checkedAt !== "string") {
565
+ return { checkedAt: null, checkedAtMs: null };
566
+ }
567
+ const checkedAtMs = Date.parse(observation.checkedAt);
568
+ if (!Number.isFinite(checkedAtMs)) {
569
+ return { checkedAt: null, checkedAtMs: null };
570
+ }
571
+ return { checkedAt: new Date(checkedAtMs).toISOString(), checkedAtMs };
572
+ }
573
+
574
+ function xaiGrokAvailabilityFor(input: {
575
+ observation: ModelAvailabilityObservation | undefined;
576
+ nowMs: number;
577
+ maxAgeMs: number;
578
+ }): ModelAvailabilityV1 {
579
+ const { checkedAt, checkedAtMs } = observationTimestamp(input.observation);
580
+ const freshSuccessfulObservation =
581
+ input.observation?.status === "available" &&
582
+ input.observation.reason === null &&
583
+ checkedAtMs !== null &&
584
+ checkedAtMs <= input.nowMs &&
585
+ input.nowMs - checkedAtMs <= input.maxAgeMs;
586
+
587
+ if (freshSuccessfulObservation) {
588
+ return {
589
+ status: "available",
590
+ selectable: true,
591
+ reason: null,
592
+ checkedAt,
593
+ };
594
+ }
595
+
596
+ return {
597
+ status: "unavailable",
598
+ selectable: false,
599
+ reason:
600
+ input.observation?.status === "unavailable"
601
+ ? (input.observation.reason ?? "provider_unhealthy")
602
+ : "provider_unhealthy",
603
+ checkedAt,
604
+ };
605
+ }
606
+
607
+ function availabilityFor(input: {
608
+ model: ConfiguredModel;
609
+ credentialReadiness: ModelCredentialReadinessV1;
610
+ policyAllowed: boolean;
611
+ observation?: ModelAvailabilityObservation | undefined;
612
+ nowMs: number;
613
+ maxAgeMs: number;
614
+ }): ModelAvailabilityV1 {
615
+ if (!modelDefinitionRunnable(input.model)) {
616
+ return {
617
+ status: "unavailable",
618
+ selectable: false,
619
+ reason: "unsupported",
620
+ checkedAt: null,
621
+ };
622
+ }
623
+ if (input.credentialReadiness.status !== "ready") {
624
+ return {
625
+ status: "unavailable",
626
+ selectable: false,
627
+ reason:
628
+ input.credentialReadiness.reason === "missing_credential"
629
+ ? "missing_credential"
630
+ : input.credentialReadiness.reason === "needs_reauth"
631
+ ? "needs_reauth"
632
+ : "credential_not_ready",
633
+ checkedAt: input.credentialReadiness.checkedAt,
634
+ };
635
+ }
636
+ if (!input.policyAllowed) {
637
+ return {
638
+ status: "unavailable",
639
+ selectable: false,
640
+ reason: "policy_blocked",
641
+ checkedAt: null,
642
+ };
643
+ }
644
+ if (isXaiGrokModel(input.model)) {
645
+ return xaiGrokAvailabilityFor({
646
+ observation: input.observation,
647
+ nowMs: input.nowMs,
648
+ maxAgeMs: input.maxAgeMs,
649
+ });
650
+ }
651
+ if (!input.observation) {
652
+ return { status: "unknown", selectable: true, reason: null, checkedAt: null };
653
+ }
654
+ if (input.observation.status === "unavailable") {
655
+ return {
656
+ status: "unavailable",
657
+ selectable: false,
658
+ reason: input.observation.reason ?? "provider_unhealthy",
659
+ checkedAt: input.observation.checkedAt,
660
+ };
661
+ }
662
+ return {
663
+ status: input.observation.status,
664
+ selectable: true,
665
+ reason: null,
666
+ checkedAt: input.observation.checkedAt,
667
+ };
668
+ }
669
+
670
+ /**
671
+ * One shared picker/tool decision. Catalog membership, credential readiness,
672
+ * workspace policy, and optional provider-health observations are evaluated in
673
+ * configured catalog order so every consumer exposes the same selectable set.
674
+ */
675
+ export function resolveWorkspaceModelSelection(
676
+ input: WorkspaceModelSelectionInput,
677
+ ): WorkspaceModelSelection[] {
678
+ const codexSettings = input.settings.codexSubscriptionEnabled
679
+ ? withCodexCatalogProvider(input.settings)
680
+ : input.settings;
681
+ const xaiSettings = input.settings.supergrokSubscriptionEnabled
682
+ ? withXaiSubscriptionCatalogProvider(codexSettings)
683
+ : codexSettings;
684
+ const catalogSettings = withOrganizationOpenRouterCatalogProvider(
685
+ withOrganizationGatewayCatalogProvider(
686
+ withWorkspaceOpenRouterCatalogProvider(
687
+ withWorkspaceGatewayCatalogProvider(xaiSettings, input.workspaceGatewayCustomModels ?? []),
688
+ input.workspaceOpenRouterCustomModels ?? [],
689
+ ),
690
+ input.organizationGatewayCustomModels ?? [],
691
+ ),
692
+ input.organizationOpenRouterCustomModels ?? [],
693
+ );
694
+ const providers = new Map(
695
+ configuredProviders(catalogSettings).map((provider) => [provider.id, provider]),
696
+ );
697
+ const requestedNowMs = input.now?.getTime();
698
+ const nowMs =
699
+ typeof requestedNowMs === "number" && Number.isFinite(requestedNowMs)
700
+ ? requestedNowMs
701
+ : Date.now();
702
+ const maxAgeMs =
703
+ typeof input.credentialReadinessMaxAgeMs === "number" &&
704
+ Number.isFinite(input.credentialReadinessMaxAgeMs) &&
705
+ input.credentialReadinessMaxAgeMs >= 0
706
+ ? input.credentialReadinessMaxAgeMs
707
+ : MODEL_CREDENTIAL_READINESS_OBSERVATION_MAX_AGE_MS;
708
+
709
+ return configuredModels(catalogSettings).map((model) => {
710
+ const provider = providers.get(model.providerId);
711
+ const policyAllowed = evaluateWorkspaceModelPolicy(input.policy, {
712
+ providerId: model.providerId,
713
+ modelId: model.id,
714
+ }).allowed;
715
+ const credentialReadiness = credentialReadinessFor({
716
+ model,
717
+ provider,
718
+ codexSubscriptionActive: input.codexSubscriptionActive,
719
+ xaiSubscriptionActive: input.xaiSubscriptionActive === true,
720
+ workspaceGatewayConnectionActive: input.workspaceGatewayConnectionActive === true,
721
+ workspaceOpenRouterConnectionActive: input.workspaceOpenRouterConnectionActive === true,
722
+ organizationGatewayConnectionActive: input.organizationGatewayConnectionActive === true,
723
+ organizationOpenRouterConnectionActive: input.organizationOpenRouterConnectionActive === true,
724
+ observation: input.credentialReadinessObservations?.[model.definitionVersion],
725
+ nowMs,
726
+ maxAgeMs,
727
+ });
728
+ return {
729
+ model,
730
+ credentialReadiness,
731
+ policyAllowed,
732
+ availability: availabilityFor({
733
+ model,
734
+ credentialReadiness,
735
+ policyAllowed,
736
+ observation: input.observations?.[model.definitionVersion],
737
+ nowMs,
738
+ maxAgeMs,
739
+ }),
740
+ };
741
+ });
742
+ }