@opengeni/db 3.7.4 → 3.8.2-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.
Files changed (77) hide show
  1. package/dist/automations.d.ts +4 -0
  2. package/dist/canonical-human-identities.js +3 -3
  3. package/dist/{chunk-O56DM5GZ.js → chunk-53A2PEHT.js} +328 -31
  4. package/dist/chunk-53A2PEHT.js.map +1 -0
  5. package/dist/{chunk-SZAWDXQC.js → chunk-5S4BQAHX.js} +3 -3
  6. package/dist/{chunk-S3TDDCS2.js → chunk-CITYUGMW.js} +3 -3
  7. package/dist/{chunk-4NAR7FBA.js → chunk-F5YOZEYQ.js} +49 -1
  8. package/dist/chunk-F5YOZEYQ.js.map +1 -0
  9. package/dist/{chunk-F6PBS6WE.js → chunk-HZV7O6JR.js} +2 -2
  10. package/dist/{chunk-5NNCGFQY.js → chunk-RLQVPOVR.js} +2 -2
  11. package/dist/{chunk-DZCYW2WT.js → chunk-UKOGHUGC.js} +2 -2
  12. package/dist/{chunk-US4IS6IV.js → chunk-X6TO5HII.js} +2 -2
  13. package/dist/{chunk-U4WPPEBH.js → chunk-Y53WEFYB.js} +2 -2
  14. package/dist/connection-token-resolver.d.ts +4 -0
  15. package/dist/editable-artifact-durable-export.js +2 -2
  16. package/dist/editable-artifacts.js +3 -3
  17. package/dist/index.d.ts +164 -14
  18. package/dist/index.js +7327 -5442
  19. package/dist/index.js.map +1 -1
  20. package/dist/insights.d.ts +9 -0
  21. package/dist/managed-auth-session-sets.js +3 -3
  22. package/dist/model-catalog.d.ts +153 -0
  23. package/dist/new-session-drafts.d.ts +12 -0
  24. package/dist/organization-membership-lifecycle.d.ts +8 -1
  25. package/dist/organization-model-providers.d.ts +101 -0
  26. package/dist/pack-components.d.ts +1 -0
  27. package/dist/pr-review.d.ts +9 -0
  28. package/dist/provision-roles.js +1 -1
  29. package/dist/runtime-posture.d.ts +7 -7
  30. package/dist/schema.d.ts +989 -2
  31. package/dist/schema.js +11 -1
  32. package/dist/session-background-commands.js +3 -3
  33. package/dist/session-control.d.ts +35 -0
  34. package/dist/session-queue-commands.d.ts +17 -0
  35. package/dist/session-tenancy.js +3 -3
  36. package/dist/video-generation.js +3 -3
  37. package/dist/xai-subscription.d.ts +8 -0
  38. package/drizzle/0387_bound_host_export_session_payloads.sql +165 -0
  39. package/drizzle/0388_sandbox_provider_deadline_interactions.sql +491 -0
  40. package/drizzle/0389_model_catalog_and_gateway_custom_models.sql +241 -0
  41. package/drizzle/0390_organization_model_provider_connections.sql +197 -0
  42. package/drizzle/0391_sandbox_provider_deadline_interaction_followup.sql +295 -0
  43. package/drizzle/0392_context_compaction_pending_observability.sql +198 -0
  44. package/drizzle/0393_workspace_memory_and_learning_defaults.sql +102 -0
  45. package/drizzle/0394_session_selected_skill_activation.sql +20 -0
  46. package/drizzle/0395_scheduled_task_unclaimed_occurrence_invalidation.sql +154 -0
  47. package/drizzle/0396_model_call_equivalent_credit_cost.sql +28 -0
  48. package/drizzle/0397_sandbox_deadline_rotation_preemption.sql +113 -0
  49. package/drizzle/0398_organization_workspace_management_entry.sql +206 -0
  50. package/drizzle/0399_additional_managed_organization_creation.sql +691 -0
  51. package/package.json +6 -6
  52. package/src/automations.ts +9 -1
  53. package/src/connection-token-resolver.ts +12 -0
  54. package/src/index.ts +1551 -272
  55. package/src/insights-model-bundle.ts +54 -5
  56. package/src/insights.ts +53 -7
  57. package/src/model-catalog.ts +682 -0
  58. package/src/new-session-drafts.ts +66 -21
  59. package/src/organization-membership-lifecycle.ts +47 -5
  60. package/src/organization-model-providers.ts +614 -0
  61. package/src/pack-components.ts +15 -2
  62. package/src/pr-review.ts +19 -0
  63. package/src/provision-roles.ts +2 -0
  64. package/src/runtime-posture.ts +48 -0
  65. package/src/schema.ts +348 -32
  66. package/src/session-control.ts +170 -0
  67. package/src/session-queue-commands.ts +157 -82
  68. package/src/xai-subscription.ts +66 -14
  69. package/dist/chunk-4NAR7FBA.js.map +0 -1
  70. package/dist/chunk-O56DM5GZ.js.map +0 -1
  71. /package/dist/{chunk-SZAWDXQC.js.map → chunk-5S4BQAHX.js.map} +0 -0
  72. /package/dist/{chunk-S3TDDCS2.js.map → chunk-CITYUGMW.js.map} +0 -0
  73. /package/dist/{chunk-F6PBS6WE.js.map → chunk-HZV7O6JR.js.map} +0 -0
  74. /package/dist/{chunk-5NNCGFQY.js.map → chunk-RLQVPOVR.js.map} +0 -0
  75. /package/dist/{chunk-DZCYW2WT.js.map → chunk-UKOGHUGC.js.map} +0 -0
  76. /package/dist/{chunk-US4IS6IV.js.map → chunk-X6TO5HII.js.map} +0 -0
  77. /package/dist/{chunk-U4WPPEBH.js.map → chunk-Y53WEFYB.js.map} +0 -0
@@ -116,6 +116,8 @@ function mapModelRows(value: unknown, label: string): ModelCallFactAggregateRow[
116
116
  pricedCostMicros: numberValue(row, "pricedCostMicros"),
117
117
  estimatedProviderCostMicros: numberValue(row, "estimatedProviderCostMicros"),
118
118
  estimatedProviderCostKnownCalls: numberValue(row, "estimatedProviderCostKnownCalls"),
119
+ equivalentCreditCostMicros: numberValue(row, "equivalentCreditCostMicros"),
120
+ equivalentCreditCostKnownCalls: numberValue(row, "equivalentCreditCostKnownCalls"),
119
121
  }));
120
122
  }
121
123
 
@@ -126,6 +128,8 @@ function mapRootRows(value: unknown, label: string): RootSessionDriverRow[] {
126
128
  pricedCostMicros: numberValue(row, "pricedCostMicros"),
127
129
  estimatedProviderCostMicros: numberValue(row, "estimatedProviderCostMicros"),
128
130
  estimatedProviderCostKnownCalls: numberValue(row, "estimatedProviderCostKnownCalls"),
131
+ equivalentCreditCostMicros: numberValue(row, "equivalentCreditCostMicros"),
132
+ equivalentCreditCostKnownCalls: numberValue(row, "equivalentCreditCostKnownCalls"),
129
133
  totalTokens: numberValue(row, "totalTokens"),
130
134
  cachedTokens: numberValue(row, "cachedTokens"),
131
135
  cacheInputTokens: numberValue(row, "cacheInputTokens"),
@@ -153,6 +157,8 @@ function mapBundle(value: unknown): WorkspaceInsightsModelBundle {
153
157
  costMicros: numberValue(row, "costMicros"),
154
158
  estimatedProviderCostMicros: numberValue(row, "estimatedProviderCostMicros"),
155
159
  estimatedProviderCostKnownCalls: numberValue(row, "estimatedProviderCostKnownCalls"),
160
+ equivalentCreditCostMicros: numberValue(row, "equivalentCreditCostMicros"),
161
+ equivalentCreditCostKnownCalls: numberValue(row, "equivalentCreditCostKnownCalls"),
156
162
  inputTokens: numberValue(row, "inputTokens"),
157
163
  outputTokens: numberValue(row, "outputTokens"),
158
164
  cachedTokens: numberValue(row, "cachedTokens"),
@@ -173,6 +179,8 @@ function mapBundle(value: unknown): WorkspaceInsightsModelBundle {
173
179
  pricedCostMicros: numberValue(row, "pricedCostMicros"),
174
180
  estimatedProviderCostMicros: numberValue(row, "estimatedProviderCostMicros"),
175
181
  estimatedProviderCostKnownCalls: numberValue(row, "estimatedProviderCostKnownCalls"),
182
+ equivalentCreditCostMicros: numberValue(row, "equivalentCreditCostMicros"),
183
+ equivalentCreditCostKnownCalls: numberValue(row, "equivalentCreditCostKnownCalls"),
176
184
  totalTokens: numberValue(row, "totalTokens"),
177
185
  cachedTokens: numberValue(row, "cachedTokens"),
178
186
  cacheInputTokens: numberValue(row, "cacheInputTokens"),
@@ -203,6 +211,7 @@ function mapBundle(value: unknown): WorkspaceInsightsModelBundle {
203
211
  totalTokens: nullableNumber(row, "totalTokens"),
204
212
  pricedCostMicros: numberValue(row, "pricedCostMicros"),
205
213
  estimatedProviderCostMicros: nullableNumber(row, "estimatedProviderCostMicros"),
214
+ equivalentCreditCostMicros: nullableNumber(row, "equivalentCreditCostMicros"),
206
215
  pricingSource: nullableString(row, "pricingSource"),
207
216
  })),
208
217
  promptContributions: {
@@ -261,6 +270,7 @@ export async function readWorkspaceInsightsModelBundle(
261
270
  fact.total_tokens,
262
271
  fact.priced_cost_micros,
263
272
  fact.estimated_provider_cost_micros,
273
+ fact.equivalent_credit_cost_micros,
264
274
  fact.pricing_source,
265
275
  fact.context_contributions,
266
276
  fact.occurred_at,
@@ -286,7 +296,8 @@ export async function readWorkspaceInsightsModelBundle(
286
296
  fact.reasoning_tokens,
287
297
  fact.total_tokens,
288
298
  fact.priced_cost_micros,
289
- fact.estimated_provider_cost_micros
299
+ fact.estimated_provider_cost_micros,
300
+ fact.equivalent_credit_cost_micros
290
301
  from opengeni_private.visible_workspace_insights_model_call_facts(
291
302
  ${input.workspaceId},
292
303
  ${input.priorSince.toISOString()}::timestamp with time zone,
@@ -340,7 +351,11 @@ export async function readWorkspaceInsightsModelBundle(
340
351
  coalesce(sum(fact.estimated_provider_cost_micros), 0)::bigint
341
352
  as estimated_provider_cost_micros,
342
353
  count(fact.estimated_provider_cost_micros)::bigint
343
- as estimated_provider_cost_known_calls
354
+ as estimated_provider_cost_known_calls,
355
+ coalesce(sum(fact.equivalent_credit_cost_micros), 0)::bigint
356
+ as equivalent_credit_cost_micros,
357
+ count(fact.equivalent_credit_cost_micros)::bigint
358
+ as equivalent_credit_cost_known_calls
344
359
  from current_visible fact
345
360
  group by fact.provider, fact.model, fact.billing_path
346
361
  ), prior_model_rows as (
@@ -368,7 +383,11 @@ export async function readWorkspaceInsightsModelBundle(
368
383
  coalesce(sum(fact.estimated_provider_cost_micros), 0)::bigint
369
384
  as estimated_provider_cost_micros,
370
385
  count(fact.estimated_provider_cost_micros)::bigint
371
- as estimated_provider_cost_known_calls
386
+ as estimated_provider_cost_known_calls,
387
+ coalesce(sum(fact.equivalent_credit_cost_micros), 0)::bigint
388
+ as equivalent_credit_cost_micros,
389
+ count(fact.equivalent_credit_cost_micros)::bigint
390
+ as equivalent_credit_cost_known_calls
372
391
  from prior_visible fact
373
392
  group by fact.provider, fact.model, fact.billing_path
374
393
  ), current_series_rows as (
@@ -381,6 +400,10 @@ export async function readWorkspaceInsightsModelBundle(
381
400
  as estimated_provider_cost_micros,
382
401
  count(fact.estimated_provider_cost_micros)::bigint
383
402
  as estimated_provider_cost_known_calls,
403
+ coalesce(sum(fact.equivalent_credit_cost_micros), 0)::bigint
404
+ as equivalent_credit_cost_micros,
405
+ count(fact.equivalent_credit_cost_micros)::bigint
406
+ as equivalent_credit_cost_known_calls,
384
407
  coalesce(sum(fact.input_tokens), 0)::bigint as input_tokens,
385
408
  coalesce(sum(fact.output_tokens), 0)::bigint as output_tokens,
386
409
  coalesce(sum(fact.cached_tokens), 0)::bigint as cached_tokens,
@@ -408,6 +431,10 @@ export async function readWorkspaceInsightsModelBundle(
408
431
  as estimated_provider_cost_micros,
409
432
  count(fact.estimated_provider_cost_micros)::bigint
410
433
  as estimated_provider_cost_known_calls,
434
+ coalesce(sum(fact.equivalent_credit_cost_micros), 0)::bigint
435
+ as equivalent_credit_cost_micros,
436
+ count(fact.equivalent_credit_cost_micros)::bigint
437
+ as equivalent_credit_cost_known_calls,
411
438
  coalesce(sum(fact.total_tokens), 0)::bigint as total_tokens,
412
439
  coalesce(sum(fact.cached_tokens), 0)::bigint as cached_tokens,
413
440
  coalesce(sum(fact.input_tokens) filter (
@@ -433,6 +460,10 @@ export async function readWorkspaceInsightsModelBundle(
433
460
  as estimated_provider_cost_micros,
434
461
  count(fact.estimated_provider_cost_micros)::bigint
435
462
  as estimated_provider_cost_known_calls,
463
+ coalesce(sum(fact.equivalent_credit_cost_micros), 0)::bigint
464
+ as equivalent_credit_cost_micros,
465
+ count(fact.equivalent_credit_cost_micros)::bigint
466
+ as equivalent_credit_cost_known_calls,
436
467
  coalesce(sum(fact.total_tokens), 0)::bigint as total_tokens,
437
468
  coalesce(sum(fact.cached_tokens), 0)::bigint as cached_tokens,
438
469
  coalesce(sum(fact.input_tokens) filter (
@@ -454,6 +485,10 @@ export async function readWorkspaceInsightsModelBundle(
454
485
  as estimated_provider_cost_micros,
455
486
  count(fact.estimated_provider_cost_micros)::bigint
456
487
  as estimated_provider_cost_known_calls,
488
+ coalesce(sum(fact.equivalent_credit_cost_micros), 0)::bigint
489
+ as equivalent_credit_cost_micros,
490
+ count(fact.equivalent_credit_cost_micros)::bigint
491
+ as equivalent_credit_cost_known_calls,
457
492
  coalesce(sum(fact.total_tokens), 0)::bigint as total_tokens,
458
493
  coalesce(sum(fact.cached_tokens), 0)::bigint as cached_tokens,
459
494
  coalesce(sum(fact.input_tokens) filter (
@@ -491,6 +526,7 @@ export async function readWorkspaceInsightsModelBundle(
491
526
  fact.total_tokens,
492
527
  fact.priced_cost_micros,
493
528
  fact.estimated_provider_cost_micros,
529
+ fact.equivalent_credit_cost_micros,
494
530
  fact.pricing_source
495
531
  from current_visible fact
496
532
  left join selected_sessions session
@@ -539,7 +575,9 @@ export async function readWorkspaceInsightsModelBundle(
539
575
  'cacheKnownCalls', cache_known_calls,
540
576
  'pricedCostMicros', priced_cost_micros,
541
577
  'estimatedProviderCostMicros', estimated_provider_cost_micros,
542
- 'estimatedProviderCostKnownCalls', estimated_provider_cost_known_calls
578
+ 'estimatedProviderCostKnownCalls', estimated_provider_cost_known_calls,
579
+ 'equivalentCreditCostMicros', equivalent_credit_cost_micros,
580
+ 'equivalentCreditCostKnownCalls', equivalent_credit_cost_known_calls
543
581
  ) order by provider, model, billing_path)
544
582
  from current_model_rows
545
583
  ), '[]'::jsonb),
@@ -560,7 +598,9 @@ export async function readWorkspaceInsightsModelBundle(
560
598
  'cacheKnownCalls', cache_known_calls,
561
599
  'pricedCostMicros', priced_cost_micros,
562
600
  'estimatedProviderCostMicros', estimated_provider_cost_micros,
563
- 'estimatedProviderCostKnownCalls', estimated_provider_cost_known_calls
601
+ 'estimatedProviderCostKnownCalls', estimated_provider_cost_known_calls,
602
+ 'equivalentCreditCostMicros', equivalent_credit_cost_micros,
603
+ 'equivalentCreditCostKnownCalls', equivalent_credit_cost_known_calls
564
604
  ) order by provider, model, billing_path)
565
605
  from prior_model_rows
566
606
  ), '[]'::jsonb),
@@ -570,6 +610,8 @@ export async function readWorkspaceInsightsModelBundle(
570
610
  'costMicros', cost_micros,
571
611
  'estimatedProviderCostMicros', estimated_provider_cost_micros,
572
612
  'estimatedProviderCostKnownCalls', estimated_provider_cost_known_calls,
613
+ 'equivalentCreditCostMicros', equivalent_credit_cost_micros,
614
+ 'equivalentCreditCostKnownCalls', equivalent_credit_cost_known_calls,
573
615
  'inputTokens', input_tokens,
574
616
  'outputTokens', output_tokens,
575
617
  'cachedTokens', cached_tokens,
@@ -590,6 +632,8 @@ export async function readWorkspaceInsightsModelBundle(
590
632
  'pricedCostMicros', priced_cost_micros,
591
633
  'estimatedProviderCostMicros', estimated_provider_cost_micros,
592
634
  'estimatedProviderCostKnownCalls', estimated_provider_cost_known_calls,
635
+ 'equivalentCreditCostMicros', equivalent_credit_cost_micros,
636
+ 'equivalentCreditCostKnownCalls', equivalent_credit_cost_known_calls,
593
637
  'totalTokens', total_tokens,
594
638
  'cachedTokens', cached_tokens,
595
639
  'cacheInputTokens', cache_input_tokens
@@ -603,6 +647,8 @@ export async function readWorkspaceInsightsModelBundle(
603
647
  'pricedCostMicros', priced_cost_micros,
604
648
  'estimatedProviderCostMicros', estimated_provider_cost_micros,
605
649
  'estimatedProviderCostKnownCalls', estimated_provider_cost_known_calls,
650
+ 'equivalentCreditCostMicros', equivalent_credit_cost_micros,
651
+ 'equivalentCreditCostKnownCalls', equivalent_credit_cost_known_calls,
606
652
  'totalTokens', total_tokens,
607
653
  'cachedTokens', cached_tokens,
608
654
  'cacheInputTokens', cache_input_tokens
@@ -615,6 +661,8 @@ export async function readWorkspaceInsightsModelBundle(
615
661
  'pricedCostMicros', priced_cost_micros,
616
662
  'estimatedProviderCostMicros', estimated_provider_cost_micros,
617
663
  'estimatedProviderCostKnownCalls', estimated_provider_cost_known_calls,
664
+ 'equivalentCreditCostMicros', equivalent_credit_cost_micros,
665
+ 'equivalentCreditCostKnownCalls', equivalent_credit_cost_known_calls,
618
666
  'totalTokens', total_tokens,
619
667
  'cachedTokens', cached_tokens,
620
668
  'cacheInputTokens', cache_input_tokens,
@@ -651,6 +699,7 @@ export async function readWorkspaceInsightsModelBundle(
651
699
  'totalTokens', total_tokens,
652
700
  'pricedCostMicros', priced_cost_micros,
653
701
  'estimatedProviderCostMicros', estimated_provider_cost_micros,
702
+ 'equivalentCreditCostMicros', equivalent_credit_cost_micros,
654
703
  'pricingSource', pricing_source
655
704
  ) order by occurred_at desc, id desc)
656
705
  from recent_rows
package/src/insights.ts CHANGED
@@ -29,6 +29,8 @@ export type ModelCallFactAggregateRow = {
29
29
  pricedCostMicros: number;
30
30
  estimatedProviderCostMicros: number;
31
31
  estimatedProviderCostKnownCalls: number;
32
+ equivalentCreditCostMicros: number;
33
+ equivalentCreditCostKnownCalls: number;
32
34
  };
33
35
 
34
36
  export type InsightsDayBucket = {
@@ -60,6 +62,8 @@ export type RootSessionDriverRow = {
60
62
  pricedCostMicros: number;
61
63
  estimatedProviderCostMicros: number;
62
64
  estimatedProviderCostKnownCalls: number;
65
+ equivalentCreditCostMicros: number;
66
+ equivalentCreditCostKnownCalls: number;
63
67
  totalTokens: number;
64
68
  cachedTokens: number;
65
69
  cacheInputTokens: number;
@@ -70,6 +74,8 @@ export type ScheduleFactAggregate = {
70
74
  pricedCostMicros: number;
71
75
  estimatedProviderCostMicros: number;
72
76
  estimatedProviderCostKnownCalls: number;
77
+ equivalentCreditCostMicros: number;
78
+ equivalentCreditCostKnownCalls: number;
73
79
  totalTokens: number;
74
80
  cachedTokens: number;
75
81
  cacheInputTokens: number;
@@ -134,6 +140,7 @@ export type RecentModelCallRow = {
134
140
  totalTokens: number | null;
135
141
  pricedCostMicros: number;
136
142
  estimatedProviderCostMicros: number | null;
143
+ equivalentCreditCostMicros: number | null;
137
144
  pricingSource: string | null;
138
145
  };
139
146
 
@@ -360,6 +367,8 @@ export async function aggregateModelCallFacts(
360
367
  pricedCostMicros: sql<number>`coalesce(sum(${modelCallFacts.pricedCostMicros}) filter (where ${modelCallFacts.billingPath} = 'opengeni_credits'), 0)`,
361
368
  estimatedProviderCostMicros: sql<number>`coalesce(sum(${modelCallFacts.estimatedProviderCostMicros}), 0)`,
362
369
  estimatedProviderCostKnownCalls: sql<number>`count(${modelCallFacts.estimatedProviderCostMicros})::int`,
370
+ equivalentCreditCostMicros: sql<number>`coalesce(sum(${modelCallFacts.equivalentCreditCostMicros}), 0)`,
371
+ equivalentCreditCostKnownCalls: sql<number>`count(${modelCallFacts.equivalentCreditCostMicros})::int`,
363
372
  })
364
373
  .from(source)
365
374
  .where(and(...clauses))
@@ -381,6 +390,8 @@ export async function aggregateModelCallFacts(
381
390
  pricedCostMicros: Number(row.pricedCostMicros),
382
391
  estimatedProviderCostMicros: Number(row.estimatedProviderCostMicros),
383
392
  estimatedProviderCostKnownCalls: Number(row.estimatedProviderCostKnownCalls),
393
+ equivalentCreditCostMicros: Number(row.equivalentCreditCostMicros),
394
+ equivalentCreditCostKnownCalls: Number(row.equivalentCreditCostKnownCalls),
384
395
  }));
385
396
  });
386
397
  }
@@ -389,6 +400,8 @@ export type ModelCallFactSeriesAggregate = {
389
400
  costMicros: number;
390
401
  estimatedProviderCostMicros: number;
391
402
  estimatedProviderCostKnownCalls: number;
403
+ equivalentCreditCostMicros: number;
404
+ equivalentCreditCostKnownCalls: number;
392
405
  inputTokens: number;
393
406
  outputTokens: number;
394
407
  cachedTokens: number;
@@ -436,6 +449,8 @@ async function aggregateModelCallFactsByBucket(
436
449
  costMicros: sql<number>`coalesce(sum(${modelCallFacts.pricedCostMicros}) filter (where ${modelCallFacts.billingPath} = 'opengeni_credits'), 0)`,
437
450
  estimatedProviderCostMicros: sql<number>`coalesce(sum(${modelCallFacts.estimatedProviderCostMicros}), 0)`,
438
451
  estimatedProviderCostKnownCalls: sql<number>`count(${modelCallFacts.estimatedProviderCostMicros})::int`,
452
+ equivalentCreditCostMicros: sql<number>`coalesce(sum(${modelCallFacts.equivalentCreditCostMicros}), 0)`,
453
+ equivalentCreditCostKnownCalls: sql<number>`count(${modelCallFacts.equivalentCreditCostMicros})::int`,
439
454
  inputTokens: sql<number>`coalesce(sum(${modelCallFacts.inputTokens}), 0)`,
440
455
  outputTokens: sql<number>`coalesce(sum(${modelCallFacts.outputTokens}), 0)`,
441
456
  cachedTokens: sql<number>`coalesce(sum(${modelCallFacts.cachedTokens}), 0)`,
@@ -457,6 +472,8 @@ async function aggregateModelCallFactsByBucket(
457
472
  costMicros: Number(row.costMicros),
458
473
  estimatedProviderCostMicros: Number(row.estimatedProviderCostMicros),
459
474
  estimatedProviderCostKnownCalls: Number(row.estimatedProviderCostKnownCalls),
475
+ equivalentCreditCostMicros: Number(row.equivalentCreditCostMicros),
476
+ equivalentCreditCostKnownCalls: Number(row.equivalentCreditCostKnownCalls),
460
477
  inputTokens: Number(row.inputTokens),
461
478
  outputTokens: Number(row.outputTokens),
462
479
  cachedTokens: Number(row.cachedTokens),
@@ -630,6 +647,8 @@ export async function aggregateRootSessionDrivers(
630
647
  pricedCostMicros: sql<number>`coalesce(sum(${modelCallFacts.pricedCostMicros}) filter (where ${modelCallFacts.billingPath} = 'opengeni_credits'), 0)`,
631
648
  estimatedProviderCostMicros: sql<number>`coalesce(sum(${modelCallFacts.estimatedProviderCostMicros}), 0)`,
632
649
  estimatedProviderCostKnownCalls: sql<number>`count(${modelCallFacts.estimatedProviderCostMicros})::int`,
650
+ equivalentCreditCostMicros: sql<number>`coalesce(sum(${modelCallFacts.equivalentCreditCostMicros}), 0)`,
651
+ equivalentCreditCostKnownCalls: sql<number>`count(${modelCallFacts.equivalentCreditCostMicros})::int`,
633
652
  totalTokens: sql<number>`coalesce(sum(${modelCallFacts.totalTokens}), 0)`,
634
653
  cachedTokens: sql<number>`coalesce(sum(${modelCallFacts.cachedTokens}), 0)`,
635
654
  cacheInputTokens: sql<number>`coalesce(sum(${modelCallFacts.inputTokens}) filter (where ${modelCallFacts.cachedTokens} is not null and ${modelCallFacts.inputTokens} is not null), 0)`,
@@ -659,6 +678,8 @@ export async function aggregateRootSessionDrivers(
659
678
  pricedCostMicros: Number(row.pricedCostMicros),
660
679
  estimatedProviderCostMicros: Number(row.estimatedProviderCostMicros),
661
680
  estimatedProviderCostKnownCalls: Number(row.estimatedProviderCostKnownCalls),
681
+ equivalentCreditCostMicros: Number(row.equivalentCreditCostMicros),
682
+ equivalentCreditCostKnownCalls: Number(row.equivalentCreditCostKnownCalls),
662
683
  totalTokens: Number(row.totalTokens),
663
684
  cachedTokens: Number(row.cachedTokens),
664
685
  cacheInputTokens: Number(row.cacheInputTokens),
@@ -735,6 +756,9 @@ export async function listRecentModelCalls(
735
756
  estimatedProviderCostMicros: sql<
736
757
  number | null
737
758
  >`${modelCallFacts.estimatedProviderCostMicros}`,
759
+ equivalentCreditCostMicros: sql<
760
+ number | null
761
+ >`${modelCallFacts.equivalentCreditCostMicros}`,
738
762
  pricingSource: sql<string | null>`${modelCallFacts.pricingSource}`,
739
763
  })
740
764
  .from(source)
@@ -763,6 +787,10 @@ export async function listRecentModelCalls(
763
787
  row.estimatedProviderCostMicros,
764
788
  "estimated provider cost",
765
789
  ),
790
+ equivalentCreditCostMicros: insightsNullableNumber(
791
+ row.equivalentCreditCostMicros,
792
+ "equivalent credit cost",
793
+ ),
766
794
  }));
767
795
  });
768
796
  }
@@ -886,6 +914,8 @@ export async function aggregateScheduleFacts(
886
914
  pricedCostMicros: sql<number>`coalesce(sum(${modelCallFacts.pricedCostMicros}) filter (where ${modelCallFacts.billingPath} = 'opengeni_credits'), 0)`,
887
915
  estimatedProviderCostMicros: sql<number>`coalesce(sum(${modelCallFacts.estimatedProviderCostMicros}), 0)`,
888
916
  estimatedProviderCostKnownCalls: sql<number>`count(${modelCallFacts.estimatedProviderCostMicros})::int`,
917
+ equivalentCreditCostMicros: sql<number>`coalesce(sum(${modelCallFacts.equivalentCreditCostMicros}), 0)`,
918
+ equivalentCreditCostKnownCalls: sql<number>`count(${modelCallFacts.equivalentCreditCostMicros})::int`,
889
919
  totalTokens: sql<number>`coalesce(sum(${modelCallFacts.totalTokens}), 0)`,
890
920
  cachedTokens: sql<number>`coalesce(sum(${modelCallFacts.cachedTokens}), 0)`,
891
921
  cacheInputTokens: sql<number>`coalesce(sum(${modelCallFacts.inputTokens}) filter (where ${modelCallFacts.cachedTokens} is not null and ${modelCallFacts.inputTokens} is not null), 0)`,
@@ -906,6 +936,8 @@ export async function aggregateScheduleFacts(
906
936
  pricedCostMicros: Number(row.pricedCostMicros),
907
937
  estimatedProviderCostMicros: Number(row.estimatedProviderCostMicros),
908
938
  estimatedProviderCostKnownCalls: Number(row.estimatedProviderCostKnownCalls),
939
+ equivalentCreditCostMicros: Number(row.equivalentCreditCostMicros),
940
+ equivalentCreditCostKnownCalls: Number(row.equivalentCreditCostKnownCalls),
909
941
  totalTokens: Number(row.totalTokens),
910
942
  cachedTokens: Number(row.cachedTokens),
911
943
  cacheInputTokens: Number(row.cacheInputTokens),
@@ -1106,7 +1138,7 @@ export async function backfillModelCallFactsFromSessionEvents(
1106
1138
 
1107
1139
  let considered = 0;
1108
1140
  let upserted = 0;
1109
- let cursorOccurredAt = input.since;
1141
+ let cursorOccurredAt = input.since.toISOString();
1110
1142
  let cursorId = "00000000-0000-0000-0000-000000000000";
1111
1143
 
1112
1144
  while (considered < limit) {
@@ -1122,6 +1154,7 @@ export async function backfillModelCallFactsFromSessionEvents(
1122
1154
  turnAttemptId: schema.sessionEvents.turnAttemptId,
1123
1155
  payload: schema.sessionEvents.payload,
1124
1156
  occurredAt: schema.sessionEvents.occurredAt,
1157
+ cursorOccurredAt: sql<string>`${schema.sessionEvents.occurredAt}::text`,
1125
1158
  })
1126
1159
  .from(schema.sessionEvents)
1127
1160
  .where(
@@ -1132,7 +1165,7 @@ export async function backfillModelCallFactsFromSessionEvents(
1132
1165
  lt(schema.sessionEvents.occurredAt, until),
1133
1166
  sql`${schema.sessionEvents.turnId} is not null`,
1134
1167
  // Keyset on (occurred_at, id) so same-millisecond bursts cannot be skipped.
1135
- sql`(${schema.sessionEvents.occurredAt}, ${schema.sessionEvents.id}) > (${cursorOccurredAt}, ${cursorId}::uuid)`,
1168
+ sql`(${schema.sessionEvents.occurredAt}, ${schema.sessionEvents.id}) > (${cursorOccurredAt}::timestamptz, ${cursorId}::uuid)`,
1136
1169
  ),
1137
1170
  )
1138
1171
  .orderBy(schema.sessionEvents.occurredAt, schema.sessionEvents.id)
@@ -1141,7 +1174,7 @@ export async function backfillModelCallFactsFromSessionEvents(
1141
1174
  if (page.length === 0) break;
1142
1175
  considered += page.length;
1143
1176
  const last = page[page.length - 1]!;
1144
- cursorOccurredAt = last.occurredAt;
1177
+ cursorOccurredAt = last.cursorOccurredAt;
1145
1178
  cursorId = last.id;
1146
1179
 
1147
1180
  const batchUpserted = await withRlsContext(db, context, async (scopedDb) => {
@@ -1158,6 +1191,15 @@ export async function backfillModelCallFactsFromSessionEvents(
1158
1191
  const providerApi = typeof payload.providerApi === "string" ? payload.providerApi : null;
1159
1192
  const model = typeof payload.model === "string" ? payload.model : null;
1160
1193
  if (!sourceKey || !provider || !providerApi || !model) continue;
1194
+ const upstreamProvider =
1195
+ typeof payload.upstreamProvider === "string" &&
1196
+ /^[a-z0-9][a-z0-9-]{0,63}$/.test(payload.upstreamProvider)
1197
+ ? payload.upstreamProvider
1198
+ : null;
1199
+ const durableBillingPath =
1200
+ payload.billingPath === "external" || payload.billingPath === "opengeni_credits"
1201
+ ? payload.billingPath
1202
+ : null;
1161
1203
 
1162
1204
  const sourceResourceId = `${event.turnId}:${sourceKey}`;
1163
1205
  const [cost] = await scopedDb
@@ -1183,11 +1225,15 @@ export async function backfillModelCallFactsFromSessionEvents(
1183
1225
  )
1184
1226
  .limit(1);
1185
1227
 
1186
- // External turns always write model.cost=0 and never model.tokens.
1187
- // Credits turns with totalTokens=0 write neither — keep those as credits.
1228
+ // New usage events carry the accepted billing authority because a
1229
+ // deployment-funded free call legitimately writes both model.tokens
1230
+ // and model.cost=0. Older events predate that field: their external
1231
+ // subscription/workspace calls wrote cost=0 without tokens, while
1232
+ // zero-token credits calls wrote neither.
1188
1233
  const pricedCostMicros = cost ? Number(cost.quantity) : 0;
1189
1234
  const billingPath =
1190
- cost != null && pricedCostMicros === 0 && !tokenRow ? "external" : "opengeni_credits";
1235
+ durableBillingPath ??
1236
+ (cost != null && pricedCostMicros === 0 && !tokenRow ? "external" : "opengeni_credits");
1191
1237
 
1192
1238
  const [turn] = await scopedDb
1193
1239
  .select({
@@ -1239,7 +1285,7 @@ export async function backfillModelCallFactsFromSessionEvents(
1239
1285
  turnId: event.turnId,
1240
1286
  turnAttemptId: event.turnAttemptId,
1241
1287
  sourceKey,
1242
- provider,
1288
+ provider: upstreamProvider ?? provider,
1243
1289
  providerApi,
1244
1290
  model,
1245
1291
  billingPath,