@opengeni/worker-bundle 0.15.1 → 0.16.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.
@@ -755,6 +755,7 @@ export type ModelUsageBillingRecord = {
755
755
  /** Same quantity written to usage_events.model.cost when present; else 0. */
756
756
  pricedCostMicros: number;
757
757
  normalizedUsage: ModelCallUsageNormalization;
758
+ upstreamProvider?: string;
758
759
  };
759
760
  export declare function recordModelUsageAndDebitCredits(settings: Settings, db: ActivityServices["db"], input: {
760
761
  accountId: string;
@@ -764,6 +765,8 @@ export declare function recordModelUsageAndDebitCredits(settings: Settings, db:
764
765
  turnAttemptId: string;
765
766
  model: string;
766
767
  externallyBilled: boolean;
768
+ gatewayManaged?: boolean;
769
+ gatewayBilling?: ModelResponseUsage["gatewayBilling"];
767
770
  usage?: ModelUsageInput | ModelCallUsageInput | null;
768
771
  normalizedUsage?: ModelCallUsageNormalization;
769
772
  sourceKey: string;
package/dist/index.js CHANGED
@@ -114,6 +114,9 @@ import {
114
114
  getEnrollment,
115
115
  abandonRecordingForTurnAttempt,
116
116
  markSessionAttemptQuiesced,
117
+ getOrCreatePreferenceRegistrySnapshot,
118
+ getOrCreateWorkspaceInstructionPolicySnapshot,
119
+ PreferenceRegistryInitiatorError,
117
120
  beginConnectorActionExecution,
118
121
  completeConnectorActionExecution,
119
122
  prepareConnectorActionApproval
@@ -130,8 +133,11 @@ import {
130
133
  sanitizeHistoryItemsForModel as sanitizeHistoryItemsForModel2,
131
134
  appendPersistentSessionSettings,
132
135
  appendSessionInstructions,
136
+ appendWorkspaceGovernance,
133
137
  appendWorkspaceMemory,
134
138
  composeAgentInstructions,
139
+ hasActiveWorkspaceInstructionPolicy,
140
+ renderWorkspaceGovernanceContext,
135
141
  summarizeForCompaction as summarizeForCompaction2,
136
142
  requestRemoteCompactionV2,
137
143
  serializedToolsForRemoteCompaction,
@@ -187,6 +193,7 @@ function connectionTokenResolverForTurn(input) {
187
193
  // src/activities/agent-turn.ts
188
194
  import {
189
195
  assertTurnExecutionPolicyMatchesConfigV1,
196
+ calculateGatewayReportedCostMicros,
190
197
  calculateModelUsageCostMicros,
191
198
  configuredModelPricing,
192
199
  configuredStaticUsageLimits as configuredStaticUsageLimits2,
@@ -195,7 +202,9 @@ import {
195
202
  sandboxWarmRateMicrosPerSecond,
196
203
  serviceTierForLatencyMode,
197
204
  settingsWithResolvedModelContext,
198
- resolveTurnExecutionPolicyV1
205
+ resolveTurnExecutionPolicyV1,
206
+ OPENGENI_GATEWAY_MODELS,
207
+ OPENGENI_GATEWAY_PROVIDER_ID
199
208
  } from "@opengeni/config";
200
209
  import { ApplicationFailure, CancelledFailure as CancelledFailure2 } from "@temporalio/activity";
201
210
 
@@ -1008,7 +1017,8 @@ import {
1008
1017
  classifyCodexUsageLimitError,
1009
1018
  codexRequestStorage,
1010
1019
  isCodexTransportError,
1011
- withCodexRequestOverrides
1020
+ withCodexRequestOverrides,
1021
+ selectCodexCredentialId
1012
1022
  } from "@opengeni/codex";
1013
1023
 
1014
1024
  // src/activities/common.ts
@@ -6718,6 +6728,8 @@ async function processModelResponseUsageEvent(input) {
6718
6728
  externallyBilled: input.externallyBilled,
6719
6729
  usage: responseUsage.usage,
6720
6730
  normalizedUsage,
6731
+ gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
6732
+ gatewayBilling: responseUsage.gatewayBilling,
6721
6733
  sourceKey,
6722
6734
  ...input.latencyMode ? { latencyMode: input.latencyMode } : {},
6723
6735
  observability: input.observability
@@ -6809,6 +6821,8 @@ async function processCompactionModelUsageEvent(input) {
6809
6821
  externallyBilled: input.externallyBilled,
6810
6822
  usage: input.usage.usage,
6811
6823
  normalizedUsage,
6824
+ gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
6825
+ gatewayBilling: input.usage.gatewayBilling,
6812
6826
  sourceKey,
6813
6827
  observability: input.observability
6814
6828
  });
@@ -8645,10 +8659,27 @@ function createRunAgentTurnActivity(services) {
8645
8659
  const rigName = rigVersion && session.rigId ? await getRigName(db, input.workspaceId, session.rigId) ?? "rig" : null;
8646
8660
  rigId = session.rigId ?? "";
8647
8661
  rigVersionId = session.rigVersionId ?? "";
8648
- const workspaceAgentInstructions = await resolveWorkspaceAgentInstructions(
8649
- db,
8650
- input.workspaceId
8651
- );
8662
+ const governanceClaims = {
8663
+ accountId: input.accountId,
8664
+ workspaceId: input.workspaceId,
8665
+ sessionId: input.sessionId,
8666
+ turnId: turn.id,
8667
+ attemptId: input.attemptId,
8668
+ executionGeneration: turn.executionGeneration
8669
+ };
8670
+ const [workspaceAgentInstructions, instructionPolicySnapshot, preferenceSnapshot] = await Promise.all([
8671
+ resolveWorkspaceAgentInstructions(db, input.workspaceId),
8672
+ getOrCreateWorkspaceInstructionPolicySnapshot(db, governanceClaims),
8673
+ getOrCreatePreferenceRegistrySnapshot(db, governanceClaims).catch((error) => {
8674
+ if (error instanceof PreferenceRegistryInitiatorError) return null;
8675
+ throw error;
8676
+ })
8677
+ ]);
8678
+ const workspaceGovernance = renderWorkspaceGovernanceContext({
8679
+ instructionPolicy: instructionPolicySnapshot,
8680
+ preferences: preferenceSnapshot
8681
+ });
8682
+ const structuredWorkspacePolicyActive = hasActiveWorkspaceInstructionPolicy(instructionPolicySnapshot);
8652
8683
  const workspaceMemory = await resolveWorkspaceMemoryBlock(db, input.workspaceId);
8653
8684
  const baseRunSettings = {
8654
8685
  // IMAGE PRECEDENCE (M3): rig > pack > deployment. settingsWithRigImage runs
@@ -8925,19 +8956,22 @@ function createRunAgentTurnActivity(services) {
8925
8956
  const persistentSessionSettings = {
8926
8957
  titleIsSet: Boolean(session.title?.trim())
8927
8958
  };
8928
- const compactionInstructions = appendPersistentSessionSettings(
8929
- appendSessionInstructions(
8930
- appendWorkspaceMemory(
8931
- composeAgentInstructions(
8932
- workspaceAgentInstructions ?? modelRunSettings.agentInstructionsTemplate,
8933
- void 0,
8934
- rigVersion && rigName ? { name: rigName, version: rigVersion.version } : void 0
8959
+ const compactionInstructions = appendWorkspaceMemory(
8960
+ appendPersistentSessionSettings(
8961
+ appendSessionInstructions(
8962
+ appendWorkspaceGovernance(
8963
+ composeAgentInstructions(
8964
+ structuredWorkspacePolicyActive ? modelRunSettings.agentInstructionsTemplate : workspaceAgentInstructions ?? modelRunSettings.agentInstructionsTemplate,
8965
+ void 0,
8966
+ rigVersion && rigName ? { name: rigName, version: rigVersion.version } : void 0
8967
+ ),
8968
+ workspaceGovernance ?? void 0
8935
8969
  ),
8936
- workspaceMemory ?? void 0
8970
+ session.instructions ?? void 0
8937
8971
  ),
8938
- session.instructions ?? void 0
8972
+ persistentSessionSettings
8939
8973
  ),
8940
- persistentSessionSettings
8974
+ workspaceMemory ?? void 0
8941
8975
  );
8942
8976
  const requested = await isSessionCompactionRequested(
8943
8977
  db,
@@ -9912,7 +9946,8 @@ function createRunAgentTurnActivity(services) {
9912
9946
  skillLibrarySkills: skillLibraryRuntime.skillLibrarySkills,
9913
9947
  skillLibrarySelections: skillLibraryRuntime.skillLibrarySelections
9914
9948
  } : {},
9915
- ...workspaceAgentInstructions ? { instructionsTemplate: workspaceAgentInstructions } : {},
9949
+ ...!structuredWorkspacePolicyActive && workspaceAgentInstructions ? { instructionsTemplate: workspaceAgentInstructions } : {},
9950
+ ...workspaceGovernance ? { workspaceGovernance } : {},
9916
9951
  ...workspaceMemory ? { workspaceMemory } : {},
9917
9952
  // Per-session persona tier (session > workspace > deployment default).
9918
9953
  // Composed system-level AFTER the workspace persona so it refines it for
@@ -12564,6 +12599,17 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
12564
12599
  const inputTokens = sanitizedUsage.inputTokens ?? 0;
12565
12600
  const outputTokens = sanitizedUsage.outputTokens ?? 0;
12566
12601
  const totalTokens = sanitizedUsage.totalTokens ?? 0;
12602
+ const gatewayBilling = input.gatewayManaged ? input.gatewayBilling : void 0;
12603
+ if (gatewayBilling) {
12604
+ const gatewayModel = Object.values(OPENGENI_GATEWAY_MODELS).find(
12605
+ (candidate) => candidate.productId === input.model
12606
+ );
12607
+ if (!gatewayModel || !gatewayModel.providers.includes(gatewayBilling.finalProvider)) {
12608
+ throw new Error(
12609
+ `AI Gateway reported unapproved provider ${gatewayBilling.finalProvider} for ${input.model}`
12610
+ );
12611
+ }
12612
+ }
12567
12613
  if (input.externallyBilled) {
12568
12614
  await recordUsageEvent(db, {
12569
12615
  accountId: input.accountId,
@@ -12600,17 +12646,20 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
12600
12646
  });
12601
12647
  }
12602
12648
  const shouldDebit = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
12603
- if (!shouldDebit || totalTokens === 0) {
12649
+ if (!shouldDebit || totalTokens === 0 && !gatewayBilling) {
12604
12650
  return {
12605
12651
  billingPath: "opengeni_credits",
12606
12652
  pricedCostMicros: 0,
12607
- normalizedUsage
12653
+ normalizedUsage,
12654
+ ...gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}
12608
12655
  };
12609
12656
  }
12610
12657
  if (!configuredModelPricing(settings)[input.model]) {
12611
12658
  throw new Error(`Missing model pricing for ${input.model}`);
12612
12659
  }
12613
- const costMicros = calculateModelUsageCostMicros(settings, input.model, sanitizedUsage, {
12660
+ const costMicros = gatewayBilling ? calculateGatewayReportedCostMicros(settings, input.model, gatewayBilling.inferenceCostUsd, {
12661
+ inputTokens
12662
+ }) : calculateModelUsageCostMicros(settings, input.model, sanitizedUsage, {
12614
12663
  latencyMode: input.latencyMode ?? "standard"
12615
12664
  });
12616
12665
  await recordUsageEvent(db, {
@@ -12647,7 +12696,8 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
12647
12696
  // Additive: the prompt-cache slice of this call's input tokens, so the
12648
12697
  // per-call debit record carries cache efficiency alongside the token
12649
12698
  // counts. 0 when the provider did not report cached tokens.
12650
- cachedTokens: normalizedUsage.telemetry.cachedTokens ?? 0
12699
+ cachedTokens: normalizedUsage.telemetry.cachedTokens ?? 0,
12700
+ ...gatewayBilling ? { gatewayProvider: gatewayBilling.finalProvider } : {}
12651
12701
  }
12652
12702
  });
12653
12703
  recordCreditMicros(input.observability, "usage", result.debitedMicros);
@@ -12655,7 +12705,8 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
12655
12705
  return {
12656
12706
  billingPath: "opengeni_credits",
12657
12707
  pricedCostMicros: costMicros,
12658
- normalizedUsage
12708
+ normalizedUsage,
12709
+ ...gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}
12659
12710
  };
12660
12711
  }
12661
12712
  async function recordAuthoritativeModelCallFact(input) {
@@ -12668,7 +12719,7 @@ async function recordAuthoritativeModelCallFact(input) {
12668
12719
  turnId: input.turnId,
12669
12720
  turnAttemptId: input.turnAttemptId,
12670
12721
  sourceKey: input.sourceKey,
12671
- provider: input.provider,
12722
+ provider: input.billing.upstreamProvider ?? input.provider,
12672
12723
  providerApi: input.providerApi,
12673
12724
  model: input.model,
12674
12725
  billingPath: input.billing.billingPath,