@opengeni/db 1.3.0 → 1.4.2

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 (91) hide show
  1. package/dist/canonical-human-identities.js +3 -3
  2. package/dist/capability-reconciliation.js +2 -2
  3. package/dist/{chunk-7QWWTVYH.js → chunk-5ROHLEN6.js} +29 -4
  4. package/dist/chunk-5ROHLEN6.js.map +1 -0
  5. package/dist/{chunk-AQMH2ZEH.js → chunk-A4J7EECR.js} +2 -2
  6. package/dist/{chunk-VQX54TK3.js → chunk-B5C5SBSI.js} +3 -3
  7. package/dist/{chunk-OI6YZXTU.js → chunk-DKFEUGM4.js} +2 -2
  8. package/dist/{chunk-HYBQABBJ.js → chunk-HUKBIZGH.js} +2 -2
  9. package/dist/{chunk-LRSIODCQ.js → chunk-IT3EYY2C.js} +6 -4
  10. package/dist/{chunk-LRSIODCQ.js.map → chunk-IT3EYY2C.js.map} +1 -1
  11. package/dist/{chunk-DSNEIW5E.js → chunk-JL6TBB76.js} +2 -1
  12. package/dist/chunk-JL6TBB76.js.map +1 -0
  13. package/dist/{chunk-OEFNTGR3.js → chunk-JWBEE4CL.js} +4 -4
  14. package/dist/{chunk-BRVBLQAL.js → chunk-K5MTNKJU.js} +4 -4
  15. package/dist/{chunk-6UKNJ43T.js → chunk-NZ2SP55O.js} +2 -2
  16. package/dist/{chunk-3XHC5KC5.js → chunk-OKCBARX6.js} +2 -2
  17. package/dist/{chunk-PNWWNEDM.js → chunk-OV5AUIBA.js} +2 -2
  18. package/dist/{chunk-BID3CNN3.js → chunk-TODVDGMO.js} +36 -18
  19. package/dist/{chunk-BID3CNN3.js.map → chunk-TODVDGMO.js.map} +1 -1
  20. package/dist/{chunk-6S5T6FSP.js → chunk-W75DRYG3.js} +5 -5
  21. package/dist/{chunk-7S3O33NM.js → chunk-Y72DP3JF.js} +2 -2
  22. package/dist/codemode-approvals.d.ts +7 -1
  23. package/dist/connect-attempts.js +3 -3
  24. package/dist/connection-metadata.d.ts +416 -0
  25. package/dist/credit-balances.d.ts +25 -4
  26. package/dist/database.d.ts +7 -1
  27. package/dist/editable-artifact-durable-export.js +2 -2
  28. package/dist/editable-artifacts.js +3 -3
  29. package/dist/index.d.ts +51 -8
  30. package/dist/index.js +10826 -10129
  31. package/dist/index.js.map +1 -1
  32. package/dist/legacy-tool-action-reviews.d.ts +75 -0
  33. package/dist/managed-auth-session-sets.js +3 -3
  34. package/dist/mcp-operations.js +2 -2
  35. package/dist/modal-native-live-origin.d.ts +75 -0
  36. package/dist/organization-integration-policy.js +3 -3
  37. package/dist/organization-slack-bots.d.ts +1 -1
  38. package/dist/provision-roles.js +1 -1
  39. package/dist/retained-provider-commands.d.ts +21 -21
  40. package/dist/retained-provider-commands.js +6 -6
  41. package/dist/runtime-posture.d.ts +1 -1
  42. package/dist/sandbox-interaction-idle.d.ts +22 -0
  43. package/dist/schema.js +1 -1
  44. package/dist/session-background-commands.js +4 -4
  45. package/dist/session-command-output.js +5 -5
  46. package/dist/session-event-slices.js +3 -3
  47. package/dist/session-mcp-credential-rotation.js +5 -5
  48. package/dist/session-realtime-billing.d.ts +48 -0
  49. package/dist/session-realtime.d.ts +8 -2
  50. package/dist/session-tenancy.js +3 -3
  51. package/dist/video-generation.js +3 -3
  52. package/dist/voice-transcription-settlement.d.ts +36 -0
  53. package/dist/workspace-tool-defaults.js +4 -4
  54. package/drizzle/0623_voice_transcription_attribution.sql +128 -0
  55. package/drizzle/0624_azure_live_realtime_model.sql +30 -0
  56. package/drizzle/0625_transcription_billing_refusal_codes.sql +29 -0
  57. package/drizzle/0632_modal_native_live_origin.sql +239 -0
  58. package/package.json +7 -7
  59. package/src/codemode-approvals.ts +52 -22
  60. package/src/codemode-operations.ts +198 -181
  61. package/src/connection-metadata.ts +166 -0
  62. package/src/credit-balances.ts +58 -17
  63. package/src/database.ts +9 -0
  64. package/src/index.ts +540 -412
  65. package/src/insights-unified.ts +2 -1
  66. package/src/legacy-tool-action-reviews.ts +242 -0
  67. package/src/modal-native-live-origin.ts +148 -0
  68. package/src/organization-slack-bots.ts +1 -1
  69. package/src/provision-roles.ts +12 -0
  70. package/src/runtime-posture.ts +32 -3
  71. package/src/sandbox-interaction-idle.ts +91 -0
  72. package/src/schema.ts +1 -0
  73. package/src/session-realtime-billing.ts +135 -0
  74. package/src/session-realtime-ledger.ts +9 -4
  75. package/src/session-realtime.ts +20 -2
  76. package/src/tool-action-reviews.ts +129 -18
  77. package/src/transcription-recordings.ts +3 -1
  78. package/src/voice-transcription-settlement.ts +94 -0
  79. package/dist/chunk-7QWWTVYH.js.map +0 -1
  80. package/dist/chunk-DSNEIW5E.js.map +0 -1
  81. /package/dist/{chunk-AQMH2ZEH.js.map → chunk-A4J7EECR.js.map} +0 -0
  82. /package/dist/{chunk-VQX54TK3.js.map → chunk-B5C5SBSI.js.map} +0 -0
  83. /package/dist/{chunk-OI6YZXTU.js.map → chunk-DKFEUGM4.js.map} +0 -0
  84. /package/dist/{chunk-HYBQABBJ.js.map → chunk-HUKBIZGH.js.map} +0 -0
  85. /package/dist/{chunk-OEFNTGR3.js.map → chunk-JWBEE4CL.js.map} +0 -0
  86. /package/dist/{chunk-BRVBLQAL.js.map → chunk-K5MTNKJU.js.map} +0 -0
  87. /package/dist/{chunk-6UKNJ43T.js.map → chunk-NZ2SP55O.js.map} +0 -0
  88. /package/dist/{chunk-3XHC5KC5.js.map → chunk-OKCBARX6.js.map} +0 -0
  89. /package/dist/{chunk-PNWWNEDM.js.map → chunk-OV5AUIBA.js.map} +0 -0
  90. /package/dist/{chunk-6S5T6FSP.js.map → chunk-W75DRYG3.js.map} +0 -0
  91. /package/dist/{chunk-7S3O33NM.js.map → chunk-Y72DP3JF.js.map} +0 -0
@@ -38,7 +38,8 @@ export async function getBillingBalance(
38
38
  else policy->'offers'->(g.metadata->>'creditOfferId')->'eligibleModelIds' end,
39
39
  policy->'defaultModelIds'
40
40
  ) from current_policy), to_jsonb(g.eligible_model_ids)),
41
- 'remainingMicros', g.amount_micros - coalesce(used.amount, 0)
41
+ 'remainingMicros', g.amount_micros - coalesce(used.amount, 0),
42
+ 'coversVoice', g.source_type = 'verified_signup_trial'
42
43
  ) order by g.created_at, g.id)
43
44
  from ${schema.creditLedgerEntries} g
44
45
  left join (
@@ -66,13 +67,28 @@ export async function getBillingBalance(
66
67
  });
67
68
  }
68
69
 
69
- /** Missing model means a non-model resource, which can only use general credits. */
70
- export function spendableCreditMicros(balance: BillingBalance, modelId?: string): number {
70
+ /**
71
+ * What a charge pays for. A canonical model ID uses grants scoped to that
72
+ * model; {@link VOICE_CREDIT_USAGE} (dictation and live voice) uses grants
73
+ * that cover voice, which are the verified-signup trial credits. Omitted means
74
+ * a non-model resource, which can only use general credits.
75
+ */
76
+ export type CreditUsage = string | { readonly kind: "voice" } | undefined;
77
+
78
+ /** Deployment-funded dictation and live voice. */
79
+ export const VOICE_CREDIT_USAGE: { readonly kind: "voice" } = Object.freeze({ kind: "voice" });
80
+
81
+ function grantCovers(grant: PromotionalCreditBalance, usage: CreditUsage): boolean {
82
+ if (!usage) return false;
83
+ if (typeof usage === "string") return grant.eligibleModelIds.includes(usage);
84
+ return grant.coversVoice === true;
85
+ }
86
+
87
+ export function spendableCreditMicros(balance: BillingBalance, usage?: CreditUsage): number {
71
88
  return (
72
89
  Math.max(0, balance.generalBalanceMicros ?? balance.balanceMicros) +
73
90
  (balance.promotionalCredits ?? []).reduce(
74
- (sum, grant) =>
75
- sum + (modelId && grant.eligibleModelIds.includes(modelId) ? grant.remainingMicros : 0),
91
+ (sum, grant) => sum + (grantCovers(grant, usage) ? grant.remainingMicros : 0),
76
92
  0,
77
93
  )
78
94
  );
@@ -81,29 +97,54 @@ export function spendableCreditMicros(balance: BillingBalance, modelId?: string)
81
97
  export async function getSpendableCreditBalance(
82
98
  db: Database,
83
99
  accountId: string,
84
- modelId?: string,
100
+ usage?: CreditUsage,
85
101
  policyRevision?: number,
86
102
  ): Promise<CreditBalanceSnapshot> {
87
103
  const balance = await getBillingBalance(db, accountId, policyRevision);
88
- return { ...balance, balanceMicros: spendableCreditMicros(balance, modelId) };
104
+ return { ...balance, balanceMicros: spendableCreditMicros(balance, usage) };
105
+ }
106
+
107
+ /** Covering grants pay first, oldest first; the rest is general credit. */
108
+ function allocateToCoveringGrants(
109
+ balance: BillingBalance,
110
+ amountMicros: number,
111
+ usage: CreditUsage,
112
+ ) {
113
+ let remaining = amountMicros;
114
+ const allocations: { grantEntryId: string; amountMicros: number }[] = [];
115
+ for (const grant of balance.promotionalCredits ?? []) {
116
+ if (!remaining || !grantCovers(grant, usage) || grant.remainingMicros <= 0) continue;
117
+ const allocated = Math.min(remaining, grant.remainingMicros);
118
+ allocations.push({ grantEntryId: grant.grantId, amountMicros: allocated });
119
+ remaining -= allocated;
120
+ }
121
+ return allocations;
89
122
  }
90
123
 
91
124
  export function planCreditDebit(
92
125
  balance: BillingBalance,
93
126
  requestedMicros: number,
94
- modelId?: string,
127
+ usage?: CreditUsage,
95
128
  ) {
96
129
  if (!Number.isSafeInteger(requestedMicros) || requestedMicros <= 0) {
97
130
  throw new Error("credit debit requires a positive, safe integer micro amount");
98
131
  }
99
- const debitedMicros = Math.min(requestedMicros, spendableCreditMicros(balance, modelId));
100
- let remaining = debitedMicros;
101
- const allocations: { grantEntryId: string; amountMicros: number }[] = [];
102
- for (const grant of balance.promotionalCredits ?? []) {
103
- if (!remaining || !modelId || !grant.eligibleModelIds.includes(modelId)) continue;
104
- const amountMicros = Math.min(remaining, grant.remainingMicros);
105
- allocations.push({ grantEntryId: grant.grantId, amountMicros });
106
- remaining -= amountMicros;
132
+ const debitedMicros = Math.min(requestedMicros, spendableCreditMicros(balance, usage));
133
+ return { debitedMicros, allocations: allocateToCoveringGrants(balance, debitedMicros, usage) };
134
+ }
135
+
136
+ /**
137
+ * Allocations for an already-consumed charge of the full amount: covering
138
+ * grants pay what they can and general credit takes the rest, possibly going
139
+ * negative.
140
+ */
141
+ export function planPostUseCreditAllocations(
142
+ balance: BillingBalance,
143
+ amountMicros: number,
144
+ usage: CreditUsage,
145
+ ) {
146
+ if (!Number.isSafeInteger(amountMicros) || amountMicros <= 0) {
147
+ throw new Error("credit debit requires a positive, safe integer micro amount");
107
148
  }
108
- return { debitedMicros, allocations };
149
+ return allocateToCoveringGrants(balance, amountMicros, usage);
109
150
  }
package/src/database.ts CHANGED
@@ -898,17 +898,26 @@ export async function withSessionActivityRlsContext<T>(
898
898
  );
899
899
  }
900
900
 
901
+ /**
902
+ * `organizationMembershipFence` is required when the callback takes the
903
+ * organization-membership lifecycle lock (for example external-actor
904
+ * reauthorization): that lock precedes the session-tenancy fence and the
905
+ * canonical control/workspace/session prefix, never follows it.
906
+ */
901
907
  export async function withWorkspaceSessionActivityRls<T>(
902
908
  db: Database,
903
909
  workspaceId: string,
904
910
  fn: (db: SessionActivityDatabase) => Promise<T>,
905
911
  transactionConfig?: PgTransactionConfig,
912
+ organizationMembershipFence = false,
906
913
  ): Promise<T> {
907
914
  return await withSessionActivityRlsContext(
908
915
  db,
909
916
  { ...(await rlsContextForWorkspace(db, workspaceId)), workspaceId },
910
917
  fn,
911
918
  transactionConfig,
919
+ "shared",
920
+ organizationMembershipFence,
912
921
  );
913
922
  }
914
923