@opengeni/db 3.7.4-canary.0 → 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 (62) hide show
  1. package/dist/canonical-human-identities.js +3 -3
  2. package/dist/{chunk-DXJ2WY6W.js → chunk-53A2PEHT.js} +241 -31
  3. package/dist/chunk-53A2PEHT.js.map +1 -0
  4. package/dist/{chunk-JS3AWDKZ.js → chunk-5S4BQAHX.js} +3 -3
  5. package/dist/{chunk-JP6IYGYI.js → chunk-CITYUGMW.js} +3 -3
  6. package/dist/{chunk-6FIVOJ3V.js → chunk-F5YOZEYQ.js} +45 -1
  7. package/dist/chunk-F5YOZEYQ.js.map +1 -0
  8. package/dist/{chunk-ZTSJPFDH.js → chunk-HZV7O6JR.js} +2 -2
  9. package/dist/{chunk-VDXHLVDE.js → chunk-RLQVPOVR.js} +2 -2
  10. package/dist/{chunk-LHKB62GC.js → chunk-UKOGHUGC.js} +2 -2
  11. package/dist/{chunk-QL4AHBYD.js → chunk-X6TO5HII.js} +2 -2
  12. package/dist/{chunk-3ZMN4EXC.js → chunk-Y53WEFYB.js} +2 -2
  13. package/dist/connection-token-resolver.d.ts +4 -0
  14. package/dist/editable-artifact-durable-export.js +2 -2
  15. package/dist/editable-artifacts.js +3 -3
  16. package/dist/index.d.ts +59 -12
  17. package/dist/index.js +5259 -4374
  18. package/dist/index.js.map +1 -1
  19. package/dist/insights.d.ts +9 -0
  20. package/dist/managed-auth-session-sets.js +3 -3
  21. package/dist/new-session-drafts.d.ts +12 -0
  22. package/dist/organization-membership-lifecycle.d.ts +8 -1
  23. package/dist/organization-model-providers.d.ts +101 -0
  24. package/dist/pack-components.d.ts +1 -0
  25. package/dist/provision-roles.js +1 -1
  26. package/dist/runtime-posture.d.ts +5 -5
  27. package/dist/schema.d.ts +645 -0
  28. package/dist/schema.js +7 -1
  29. package/dist/session-background-commands.js +3 -3
  30. package/dist/session-tenancy.js +3 -3
  31. package/dist/video-generation.js +3 -3
  32. package/drizzle/0390_organization_model_provider_connections.sql +197 -0
  33. package/drizzle/0391_sandbox_provider_deadline_interaction_followup.sql +295 -0
  34. package/drizzle/0392_context_compaction_pending_observability.sql +198 -0
  35. package/drizzle/0393_workspace_memory_and_learning_defaults.sql +102 -0
  36. package/drizzle/0394_session_selected_skill_activation.sql +20 -0
  37. package/drizzle/0395_scheduled_task_unclaimed_occurrence_invalidation.sql +154 -0
  38. package/drizzle/0396_model_call_equivalent_credit_cost.sql +28 -0
  39. package/drizzle/0397_sandbox_deadline_rotation_preemption.sql +113 -0
  40. package/drizzle/0398_organization_workspace_management_entry.sql +206 -0
  41. package/drizzle/0399_additional_managed_organization_creation.sql +691 -0
  42. package/package.json +6 -6
  43. package/src/connection-token-resolver.ts +12 -0
  44. package/src/index.ts +701 -155
  45. package/src/insights-model-bundle.ts +54 -5
  46. package/src/insights.ts +32 -0
  47. package/src/new-session-drafts.ts +66 -21
  48. package/src/organization-membership-lifecycle.ts +47 -5
  49. package/src/organization-model-providers.ts +614 -0
  50. package/src/pack-components.ts +15 -2
  51. package/src/provision-roles.ts +2 -0
  52. package/src/runtime-posture.ts +44 -0
  53. package/src/schema.ts +250 -30
  54. package/dist/chunk-6FIVOJ3V.js.map +0 -1
  55. package/dist/chunk-DXJ2WY6W.js.map +0 -1
  56. /package/dist/{chunk-JS3AWDKZ.js.map → chunk-5S4BQAHX.js.map} +0 -0
  57. /package/dist/{chunk-JP6IYGYI.js.map → chunk-CITYUGMW.js.map} +0 -0
  58. /package/dist/{chunk-ZTSJPFDH.js.map → chunk-HZV7O6JR.js.map} +0 -0
  59. /package/dist/{chunk-VDXHLVDE.js.map → chunk-RLQVPOVR.js.map} +0 -0
  60. /package/dist/{chunk-LHKB62GC.js.map → chunk-UKOGHUGC.js.map} +0 -0
  61. /package/dist/{chunk-QL4AHBYD.js.map → chunk-X6TO5HII.js.map} +0 -0
  62. /package/dist/{chunk-3ZMN4EXC.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),
@@ -357,23 +357,10 @@ export async function rememberNewSessionSelectionInTransaction(
357
357
  return Boolean(updated);
358
358
  }
359
359
 
360
- /**
361
- * Replace one exact accepted draft with the next-create safe seed. The row is
362
- * identity-serialized before the revision check so even the absent-row
363
- * revision-zero case cannot race a first save. Exact create callers receive a
364
- * typed conflict; legacy revision-only callers retain the old false/no-op
365
- * result. A missing row is an idempotent no-op.
366
- */
367
- export async function seedNewSessionDraftInTransaction(
360
+ async function lockCurrentNewSessionDraft(
368
361
  db: Database,
369
- input: {
370
- workspaceId: string;
371
- subjectId: string;
372
- expectedRevision: number;
373
- expectedSnapshot?: NewSessionDraftSnapshot;
374
- acceptedSelection?: AcceptedNewSessionSelection;
375
- },
376
- ): Promise<boolean> {
362
+ input: { workspaceId: string; subjectId: string },
363
+ ): Promise<NewSessionDraftRow | undefined> {
377
364
  await lockNewSessionDraftIdentity(db, input.workspaceId, input.subjectId);
378
365
  const [current] = await db
379
366
  .select()
@@ -386,14 +373,23 @@ export async function seedNewSessionDraftInTransaction(
386
373
  )
387
374
  .for("update")
388
375
  .limit(1);
376
+ return current;
377
+ }
378
+
379
+ async function lockExactNewSessionDraft(
380
+ db: Database,
381
+ input: {
382
+ workspaceId: string;
383
+ subjectId: string;
384
+ expectedRevision: number;
385
+ expectedSnapshot: NewSessionDraftSnapshot;
386
+ },
387
+ ): Promise<NewSessionDraftRow> {
388
+ const current = await lockCurrentNewSessionDraft(db, input);
389
389
  if (!current || current.revision !== input.expectedRevision) {
390
- if (input.expectedSnapshot) {
391
- throw new NewSessionDraftConflictError(current?.revision ?? 0);
392
- }
393
- return false;
390
+ throw new NewSessionDraftConflictError(current?.revision ?? 0);
394
391
  }
395
392
  if (
396
- input.expectedSnapshot &&
397
393
  stableJson({
398
394
  text: current.text,
399
395
  resources: current.resources,
@@ -407,6 +403,55 @@ export async function seedNewSessionDraftInTransaction(
407
403
  ) {
408
404
  throw new NewSessionDraftConflictError(current.revision);
409
405
  }
406
+ return current;
407
+ }
408
+
409
+ /**
410
+ * Fence a fresh session shell on the exact actor-private draft it represents.
411
+ * The caller keeps this row lock through its create transaction, so a draft
412
+ * that was already stale cannot leave a committed, uninitialized session.
413
+ * The later atomic initializer still performs the authoritative consume.
414
+ */
415
+ export async function assertExactNewSessionDraftInTransaction(
416
+ db: Database,
417
+ input: {
418
+ workspaceId: string;
419
+ subjectId: string;
420
+ expectedRevision: number;
421
+ expectedSnapshot: NewSessionDraftSnapshot;
422
+ },
423
+ ): Promise<void> {
424
+ await lockExactNewSessionDraft(db, input);
425
+ }
426
+
427
+ /**
428
+ * Replace one exact accepted draft with the next-create safe seed. The row is
429
+ * identity-serialized before the revision check so even the absent-row
430
+ * revision-zero case cannot race a first save. Exact create callers receive a
431
+ * typed conflict; legacy revision-only callers retain the old false/no-op
432
+ * result. A missing row is an idempotent no-op.
433
+ */
434
+ export async function seedNewSessionDraftInTransaction(
435
+ db: Database,
436
+ input: {
437
+ workspaceId: string;
438
+ subjectId: string;
439
+ expectedRevision: number;
440
+ expectedSnapshot?: NewSessionDraftSnapshot;
441
+ acceptedSelection?: AcceptedNewSessionSelection;
442
+ },
443
+ ): Promise<boolean> {
444
+ const current = input.expectedSnapshot
445
+ ? await lockExactNewSessionDraft(db, {
446
+ workspaceId: input.workspaceId,
447
+ subjectId: input.subjectId,
448
+ expectedRevision: input.expectedRevision,
449
+ expectedSnapshot: input.expectedSnapshot,
450
+ })
451
+ : await lockCurrentNewSessionDraft(db, input);
452
+ if (!current || current.revision !== input.expectedRevision) {
453
+ return false;
454
+ }
410
455
 
411
456
  const parsedOptions = NewSessionDraftOptions.safeParse(publicNewSessionDraftOptions(current));
412
457
  const options = parsedOptions.success ? parsedOptions.data : {};
@@ -1,4 +1,5 @@
1
1
  import {
2
+ CreateAdditionalOrganizationResponse,
2
3
  CreateOrganizationResponse,
3
4
  ListOrganizationAdministrationMembersResponse,
4
5
  ListOrganizationInvitationsResponse,
@@ -18,6 +19,7 @@ import {
18
19
  OrganizationRetentionPolicy,
19
20
  OrganizationSummary,
20
21
  RevokeOrganizationWorkspaceMemberResponse,
22
+ type CreateAdditionalOrganizationResponse as CreateAdditionalOrganizationResponseType,
21
23
  type CreateOrganizationResponse as CreateOrganizationResponseType,
22
24
  type OrganizationAdministrationMember as OrganizationAdministrationMemberType,
23
25
  type OrganizationAdministrationOverview as OrganizationAdministrationOverviewType,
@@ -74,6 +76,32 @@ export async function createManagedOrganization(
74
76
  });
75
77
  }
76
78
 
79
+ export async function createAdditionalManagedOrganization(
80
+ db: Database,
81
+ input: {
82
+ subjectId: string;
83
+ subjectLabel: string;
84
+ name: string;
85
+ workspaceName: string;
86
+ operationId: string;
87
+ },
88
+ ): Promise<CreateAdditionalOrganizationResponseType> {
89
+ return await db.transaction(async (tx) => {
90
+ await setSubjectRlsContext(tx as unknown as Database, input.subjectId);
91
+ const [row] = await rawRows<{ result: unknown }>(
92
+ tx,
93
+ sql`select create_additional_managed_organization(
94
+ ${input.subjectId},
95
+ ${input.subjectLabel},
96
+ ${input.name},
97
+ ${input.workspaceName},
98
+ ${input.operationId}::uuid
99
+ ) as result`,
100
+ );
101
+ return CreateAdditionalOrganizationResponse.parse(row?.result);
102
+ });
103
+ }
104
+
77
105
  /**
78
106
  * Bounded replay of one exact organization-lifecycle transaction after a
79
107
  * PostgreSQL deadlock abort.
@@ -208,7 +236,10 @@ async function settleOrganizationMembershipProtocols(
208
236
  db: Database,
209
237
  settlements: OrganizationMembershipProtocolSettlement[],
210
238
  ): Promise<void> {
211
- const [prior] = await rawRows<{ subject_id: string; initiating_human_subject_id: string }>(
239
+ const [prior] = await rawRows<{
240
+ subject_id: string;
241
+ initiating_human_subject_id: string;
242
+ }>(
212
243
  db,
213
244
  sql`select
214
245
  coalesce(current_setting('opengeni.subject_id', true), '') as subject_id,
@@ -436,7 +467,9 @@ async function runOrganizationWorkspaceCommand(
436
467
  { accountId: command.organizationId, workspaceId: null },
437
468
  async (scopedDb) => {
438
469
  await setSubjectRlsContext(scopedDb, command.actorSubjectId);
439
- const [row] = await rawRows<{ result: OrganizationWorkspaceCommandResult }>(
470
+ const [row] = await rawRows<{
471
+ result: OrganizationWorkspaceCommandResult;
472
+ }>(
440
473
  scopedDb,
441
474
  sql`select organization_workspace_command(${JSON.stringify(command)}::jsonb) as result`,
442
475
  );
@@ -455,7 +488,10 @@ export async function createOrganizationWorkspace(
455
488
  operationId: string;
456
489
  },
457
490
  ): Promise<OrganizationWorkspaceAccessType> {
458
- const command = await runOrganizationWorkspaceCommand(db, { action: "create", ...input });
491
+ const command = await runOrganizationWorkspaceCommand(db, {
492
+ action: "create",
493
+ ...input,
494
+ });
459
495
  const overview = await getOrganizationAdministrationOverview(db, input);
460
496
  const workspace = overview.workspaces.find((candidate) => candidate.id === command.workspaceId);
461
497
  if (!workspace) throw new Error("Created organization workspace is missing from its overview");
@@ -615,7 +651,9 @@ export async function assertActiveManagedHumanOrganizationMembership(
615
651
  db: Database,
616
652
  input: { accountId: string; subjectId: string },
617
653
  ): Promise<number | null> {
618
- const [row] = await rawRows<{ authorization_revision: number | string | null }>(
654
+ const [row] = await rawRows<{
655
+ authorization_revision: number | string | null;
656
+ }>(
619
657
  db,
620
658
  sql`select assert_active_managed_human_organization_membership(
621
659
  ${input.accountId}::uuid,
@@ -1098,7 +1136,11 @@ export async function previewOrganizationRetentionDeletions(
1098
1136
 
1099
1137
  export async function claimOrganizationRetentionDeletion(
1100
1138
  db: Database,
1101
- input: { organizationId: string; operationId: string; excludedMembershipIds?: string[] },
1139
+ input: {
1140
+ organizationId: string;
1141
+ operationId: string;
1142
+ excludedMembershipIds?: string[];
1143
+ },
1102
1144
  ): Promise<OrganizationRetentionDeletionClaimType | null> {
1103
1145
  return await runRetentionCapability(db, input.organizationId, async (scopedDb) => {
1104
1146
  const excludedMembershipIds = input.excludedMembershipIds ?? [];