@opengeni/worker-bundle 0.15.0 → 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.
@@ -579,6 +579,8 @@ export declare function shouldStartOnTurnRecording(params: {
579
579
  * • a "chat" (OpenAIChatCompletionsModel wire) provider → "disabled": it cannot
580
580
  * receive a screenshot through a proven visual transport, so computer use fails
581
581
  * closed instead of serializing the image as text.
582
+ * • Vercel Gateway providers → "disabled": their curated models support ordinary
583
+ * function tools and vision input, not OpenAI's hosted computer tool.
582
584
  * • everything else — built-in Azure/OpenAI responses, registry "responses"
583
585
  * providers, AND the LEGACY global-client fallback (resolveTurnModel returned
584
586
  * null) — → "hosted": real Responses hosted-tool support.
@@ -591,6 +593,12 @@ export declare function computerToolModeForTurn(resolvedModel: {
591
593
  api: ModelProviderApi;
592
594
  };
593
595
  } | null): ComputerToolMode;
596
+ /** Gateway models do not advertise OpenAI's hosted apply_patch/view_image tool types. */
597
+ export declare function structuredToolTransportForTurn(resolvedModel: {
598
+ provider: {
599
+ kind: RegistryProviderKind;
600
+ };
601
+ } | null): boolean;
594
602
  /**
595
603
  * Native web search is a runtime capability, not part of the session's MCP
596
604
  * allow-list. Attach it whenever the resolved provider advertises runnable
@@ -747,6 +755,7 @@ export type ModelUsageBillingRecord = {
747
755
  /** Same quantity written to usage_events.model.cost when present; else 0. */
748
756
  pricedCostMicros: number;
749
757
  normalizedUsage: ModelCallUsageNormalization;
758
+ upstreamProvider?: string;
750
759
  };
751
760
  export declare function recordModelUsageAndDebitCredits(settings: Settings, db: ActivityServices["db"], input: {
752
761
  accountId: string;
@@ -756,6 +765,8 @@ export declare function recordModelUsageAndDebitCredits(settings: Settings, db:
756
765
  turnAttemptId: string;
757
766
  model: string;
758
767
  externallyBilled: boolean;
768
+ gatewayManaged?: boolean;
769
+ gatewayBilling?: ModelResponseUsage["gatewayBilling"];
759
770
  usage?: ModelUsageInput | ModelCallUsageInput | null;
760
771
  normalizedUsage?: ModelCallUsageNormalization;
761
772
  sourceKey: string;
@@ -16,3 +16,4 @@ export declare function settingsWithSessionMcpServers(settings: Settings, server
16
16
  export declare function settingsWithCodexCredential(db: Database, workspaceId: string, settings: Settings, activeOverride?: boolean): Promise<Settings>;
17
17
  /** Pure: append the synthetic codex-subscription provider, idempotently. */
18
18
  export declare function withCodexProvider(settings: Settings): Settings;
19
+ export declare function settingsWithWorkspaceGatewayCredential(db: Database, workspaceId: string, settings: Settings): Promise<Settings>;
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,10 +133,14 @@ 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,
143
+ serializedToolsForRemoteCompaction,
137
144
  REMOTE_COMPACTION_V2_BETA_FEATURE,
138
145
  REMOTE_COMPACTION_V2_IMPLEMENTATION as REMOTE_COMPACTION_V2_IMPLEMENTATION2,
139
146
  CompactionProviderResponseError,
@@ -186,6 +193,7 @@ function connectionTokenResolverForTurn(input) {
186
193
  // src/activities/agent-turn.ts
187
194
  import {
188
195
  assertTurnExecutionPolicyMatchesConfigV1,
196
+ calculateGatewayReportedCostMicros,
189
197
  calculateModelUsageCostMicros,
190
198
  configuredModelPricing,
191
199
  configuredStaticUsageLimits as configuredStaticUsageLimits2,
@@ -194,20 +202,25 @@ import {
194
202
  sandboxWarmRateMicrosPerSecond,
195
203
  serviceTierForLatencyMode,
196
204
  settingsWithResolvedModelContext,
197
- resolveTurnExecutionPolicyV1
205
+ resolveTurnExecutionPolicyV1,
206
+ OPENGENI_GATEWAY_MODELS,
207
+ OPENGENI_GATEWAY_PROVIDER_ID
198
208
  } from "@opengeni/config";
199
209
  import { ApplicationFailure, CancelledFailure as CancelledFailure2 } from "@temporalio/activity";
200
210
 
201
211
  // src/activities/capabilities.ts
202
212
  import {
203
213
  environmentsEncryptionKeyBytes,
204
- withCodexCatalogProvider
214
+ withCodexCatalogProvider,
215
+ withWorkspaceGatewayCatalogProvider,
216
+ withWorkspaceGatewayCredential
205
217
  } from "@opengeni/config";
206
218
  import { settingsWithEnabledCapabilityMcpServers } from "@opengeni/core";
207
219
  import {
208
220
  listSessionMcpServerMetadata,
209
221
  listSessionMcpServersForRun,
210
- workspaceCodexSubscriptionActive
222
+ workspaceCodexSubscriptionActive,
223
+ loadWorkspaceVercelAiGatewayApiKey
211
224
  } from "@opengeni/db";
212
225
  async function settingsWithSessionMcpServersForRun(db, workspaceId, sessionId, attemptId, settings, options) {
213
226
  const encryptionKey = environmentsEncryptionKeyBytes(settings);
@@ -266,6 +279,11 @@ async function settingsWithCodexCredential(db, workspaceId, settings, activeOver
266
279
  function withCodexProvider(settings) {
267
280
  return withCodexCatalogProvider(settings);
268
281
  }
282
+ async function settingsWithWorkspaceGatewayCredential(db, workspaceId, settings) {
283
+ const catalogSettings = withWorkspaceGatewayCatalogProvider(settings);
284
+ const apiKey = await loadWorkspaceVercelAiGatewayApiKey(db, settings, workspaceId);
285
+ return apiKey ? withWorkspaceGatewayCredential(catalogSettings, apiKey) : catalogSettings;
286
+ }
269
287
 
270
288
  // src/activities/codex-rotation.ts
271
289
  function effectiveRotationStrategy(_stored) {
@@ -999,7 +1017,8 @@ import {
999
1017
  classifyCodexUsageLimitError,
1000
1018
  codexRequestStorage,
1001
1019
  isCodexTransportError,
1002
- withCodexRequestOverrides
1020
+ withCodexRequestOverrides,
1021
+ selectCodexCredentialId
1003
1022
  } from "@opengeni/codex";
1004
1023
 
1005
1024
  // src/activities/common.ts
@@ -6709,6 +6728,8 @@ async function processModelResponseUsageEvent(input) {
6709
6728
  externallyBilled: input.externallyBilled,
6710
6729
  usage: responseUsage.usage,
6711
6730
  normalizedUsage,
6731
+ gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
6732
+ gatewayBilling: responseUsage.gatewayBilling,
6712
6733
  sourceKey,
6713
6734
  ...input.latencyMode ? { latencyMode: input.latencyMode } : {},
6714
6735
  observability: input.observability
@@ -6800,6 +6821,8 @@ async function processCompactionModelUsageEvent(input) {
6800
6821
  externallyBilled: input.externallyBilled,
6801
6822
  usage: input.usage.usage,
6802
6823
  normalizedUsage,
6824
+ gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
6825
+ gatewayBilling: input.usage.gatewayBilling,
6803
6826
  sourceKey,
6804
6827
  observability: input.observability
6805
6828
  });
@@ -7067,11 +7090,20 @@ function computerToolModeForTurn(resolvedModel) {
7067
7090
  if (resolvedModel.provider.kind === "codex-subscription") {
7068
7091
  return "function-image";
7069
7092
  }
7093
+ if (resolvedModel.provider.kind === "vercel-gateway-managed" || resolvedModel.provider.kind === "vercel-gateway-workspace") {
7094
+ return "disabled";
7095
+ }
7070
7096
  if (resolvedModel.provider.api === "chat") {
7071
7097
  return "disabled";
7072
7098
  }
7073
7099
  return "hosted";
7074
7100
  }
7101
+ function structuredToolTransportForTurn(resolvedModel) {
7102
+ if (!resolvedModel) return true;
7103
+ return !["codex-subscription", "vercel-gateway-managed", "vercel-gateway-workspace"].includes(
7104
+ resolvedModel.provider.kind
7105
+ );
7106
+ }
7075
7107
  function hostedWebSearchForTurn(resolvedModel, deploymentWebSearchEnabled) {
7076
7108
  return resolvedModel?.configured.hostedWebSearch ?? deploymentWebSearchEnabled;
7077
7109
  }
@@ -7862,12 +7894,17 @@ function createRunAgentTurnActivity(services) {
7862
7894
  mcpSettings,
7863
7895
  input.workspaceId
7864
7896
  );
7865
- const capabilitySettings = await settingsWithCodexCredential(
7897
+ const codexSettings = await settingsWithCodexCredential(
7866
7898
  db,
7867
7899
  input.workspaceId,
7868
7900
  mcpSettings,
7869
7901
  codexSubscriptionActive
7870
7902
  );
7903
+ const capabilitySettings = await settingsWithWorkspaceGatewayCredential(
7904
+ db,
7905
+ input.workspaceId,
7906
+ codexSettings
7907
+ );
7871
7908
  runtime.configure(capabilitySettings);
7872
7909
  const session = await requireSession2(db, input.workspaceId, input.sessionId);
7873
7910
  const claim = await claimSessionWorkForAttempt(db, input.workspaceId, {
@@ -8622,10 +8659,27 @@ function createRunAgentTurnActivity(services) {
8622
8659
  const rigName = rigVersion && session.rigId ? await getRigName(db, input.workspaceId, session.rigId) ?? "rig" : null;
8623
8660
  rigId = session.rigId ?? "";
8624
8661
  rigVersionId = session.rigVersionId ?? "";
8625
- const workspaceAgentInstructions = await resolveWorkspaceAgentInstructions(
8626
- db,
8627
- input.workspaceId
8628
- );
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);
8629
8683
  const workspaceMemory = await resolveWorkspaceMemoryBlock(db, input.workspaceId);
8630
8684
  const baseRunSettings = {
8631
8685
  // IMAGE PRECEDENCE (M3): rig > pack > deployment. settingsWithRigImage runs
@@ -8840,14 +8894,44 @@ function createRunAgentTurnActivity(services) {
8840
8894
  ...systemInstructions ? { systemInstructions } : {},
8841
8895
  ...promptCacheKey ? { promptCacheKey } : {}
8842
8896
  });
8843
- const remoteCompactionRequester = resolvedModel && isCodexTurn ? (s, m) => withCodexRemoteCompaction(
8844
- () => requestRemoteCompactionV2(s, m, {
8897
+ let remoteCompactionTools = [];
8898
+ let remoteCompactionInstructions = "";
8899
+ let remoteCompactionToolsReady = false;
8900
+ let remoteCompactionAgent = null;
8901
+ const remoteCompactionRequester = resolvedModel && isCodexTurn ? (s, m) => withCodexRemoteCompaction(async () => {
8902
+ if (!remoteCompactionInstructions.trim()) {
8903
+ throw new EmptyCompactionSummaryError2({
8904
+ stage: "remote_v2_instructions",
8905
+ reason: "agent_missing_system_instructions"
8906
+ });
8907
+ }
8908
+ if (!remoteCompactionToolsReady) {
8909
+ if (!remoteCompactionAgent) {
8910
+ throw new EmptyCompactionSummaryError2({
8911
+ stage: "remote_v2_tools",
8912
+ reason: "agent_missing_for_tools"
8913
+ });
8914
+ }
8915
+ try {
8916
+ remoteCompactionTools = await serializedToolsForRemoteCompaction(remoteCompactionAgent);
8917
+ } catch (error) {
8918
+ throw new EmptyCompactionSummaryError2({
8919
+ stage: "remote_v2_tools",
8920
+ reason: "serialize_tools_failed",
8921
+ error: String(error)
8922
+ });
8923
+ }
8924
+ remoteCompactionToolsReady = true;
8925
+ }
8926
+ return requestRemoteCompactionV2(s, m, {
8845
8927
  client: resolvedModel.client,
8846
8928
  model: turnExecutionPolicy.upstreamModelId,
8929
+ systemInstructions: remoteCompactionInstructions,
8847
8930
  onUsage: recordCompactionUsage,
8931
+ tools: remoteCompactionTools,
8848
8932
  ...promptCacheKey ? { promptCacheKey } : {}
8849
- })
8850
- ) : void 0;
8933
+ });
8934
+ }) : void 0;
8851
8935
  const publishCompactionLiveEvents = async (events) => {
8852
8936
  await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, events);
8853
8937
  };
@@ -8866,23 +8950,28 @@ function createRunAgentTurnActivity(services) {
8866
8950
  ...remoteCompactionRequester ? { requestRemoteCompactionV2: remoteCompactionRequester } : {},
8867
8951
  ...effectiveCodexCredentialId ? { producerCodexCredentialId: effectiveCodexCredentialId } : {}
8868
8952
  };
8869
- if (turn.source === "compaction") {
8953
+ const compactionOnlyTurn = turn.source === "compaction";
8954
+ const remoteV2CompactionNeedsAgentPrefix = Boolean(remoteCompactionRequester) && session.codexCompactionMode === "remote_v2";
8955
+ if (compactionOnlyTurn && !remoteV2CompactionNeedsAgentPrefix) {
8870
8956
  const persistentSessionSettings = {
8871
8957
  titleIsSet: Boolean(session.title?.trim())
8872
8958
  };
8873
- const compactionInstructions = appendPersistentSessionSettings(
8874
- appendSessionInstructions(
8875
- appendWorkspaceMemory(
8876
- composeAgentInstructions(
8877
- workspaceAgentInstructions ?? modelRunSettings.agentInstructionsTemplate,
8878
- void 0,
8879
- 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
8880
8969
  ),
8881
- workspaceMemory ?? void 0
8970
+ session.instructions ?? void 0
8882
8971
  ),
8883
- session.instructions ?? void 0
8972
+ persistentSessionSettings
8884
8973
  ),
8885
- persistentSessionSettings
8974
+ workspaceMemory ?? void 0
8886
8975
  );
8887
8976
  const requested = await isSessionCompactionRequested(
8888
8977
  db,
@@ -9824,7 +9913,7 @@ function createRunAgentTurnActivity(services) {
9824
9913
  // (built-in OpenAI/Azure = real hosted support; registry "chat"
9825
9914
  // providers = the SDK's own ChatCompletions detection) keeps the SDK
9826
9915
  // default.
9827
- structuredToolTransport: resolvedModel.provider.kind !== "codex-subscription",
9916
+ structuredToolTransport: structuredToolTransportForTurn(resolvedModel),
9828
9917
  // EXPLICIT computer-use tool transport, derived from the resolved provider's
9829
9918
  // authoritative wire identity (codex → function-image, chat → disabled,
9830
9919
  // responses → hosted) so the runtime never string-sniffs the model instance's
@@ -9857,7 +9946,8 @@ function createRunAgentTurnActivity(services) {
9857
9946
  skillLibrarySkills: skillLibraryRuntime.skillLibrarySkills,
9858
9947
  skillLibrarySelections: skillLibraryRuntime.skillLibrarySelections
9859
9948
  } : {},
9860
- ...workspaceAgentInstructions ? { instructionsTemplate: workspaceAgentInstructions } : {},
9949
+ ...!structuredWorkspacePolicyActive && workspaceAgentInstructions ? { instructionsTemplate: workspaceAgentInstructions } : {},
9950
+ ...workspaceGovernance ? { workspaceGovernance } : {},
9861
9951
  ...workspaceMemory ? { workspaceMemory } : {},
9862
9952
  // Per-session persona tier (session > workspace > deployment default).
9863
9953
  // Composed system-level AFTER the workspace persona so it refines it for
@@ -10069,9 +10159,127 @@ function createRunAgentTurnActivity(services) {
10069
10159
  }
10070
10160
  );
10071
10161
  }
10162
+ const agentInstructions = typeof agent.instructions === "string" ? agent.instructions : "";
10072
10163
  const compactSummarizer = compactionSummarizerFor(
10073
- typeof agent.instructions === "string" ? agent.instructions : void 0
10164
+ agentInstructions.trim() ? agentInstructions : void 0
10074
10165
  );
10166
+ if (remoteCompactionRequester) {
10167
+ remoteCompactionInstructions = agentInstructions;
10168
+ remoteCompactionAgent = agent;
10169
+ }
10170
+ if (compactionOnlyTurn) {
10171
+ const requested = await isSessionCompactionRequested(
10172
+ db,
10173
+ input.workspaceId,
10174
+ input.sessionId
10175
+ );
10176
+ let outcome = null;
10177
+ if (requested) {
10178
+ try {
10179
+ outcome = await waitForTurnOperation(
10180
+ maybeCompactContext(
10181
+ db,
10182
+ modelRunSettings,
10183
+ {
10184
+ accountId: input.accountId,
10185
+ workspaceId: input.workspaceId,
10186
+ sessionId: input.sessionId,
10187
+ turnId: turn.id,
10188
+ executionGeneration,
10189
+ attemptId: input.attemptId
10190
+ },
10191
+ session.lastInputTokens,
10192
+ compactSummarizer,
10193
+ {
10194
+ force: true,
10195
+ clearRequestedCompaction: true,
10196
+ trigger: "operator",
10197
+ codexAccount: {
10198
+ currentCodexCredentialId: effectiveCodexCredentialId
10199
+ },
10200
+ ...compactionModeOptions
10201
+ }
10202
+ ),
10203
+ cancellationSignal,
10204
+ void 0
10205
+ );
10206
+ } catch (error) {
10207
+ if (shouldRecoverCompactionProviderFailure(error)) throw error;
10208
+ if (error instanceof TurnAttemptFencedError) throw error;
10209
+ const landmark = await settleFailedContextCompactionLandmark(
10210
+ db,
10211
+ {
10212
+ accountId: input.accountId,
10213
+ workspaceId: input.workspaceId,
10214
+ sessionId: input.sessionId,
10215
+ turnId: turn.id,
10216
+ executionGeneration,
10217
+ attemptId: input.attemptId
10218
+ },
10219
+ {
10220
+ clearRequestedCompaction: true,
10221
+ publishLiveEvents: publishCompactionLiveEvents
10222
+ }
10223
+ );
10224
+ if (!isCompactionSummaryFailure(error)) throw error;
10225
+ const errorMessage = String(redact(compactionFailureReasonFromError(error)));
10226
+ if (!await settle({
10227
+ events: [
10228
+ {
10229
+ type: "turn.failed",
10230
+ payload: {
10231
+ error: errorMessage,
10232
+ code: "context_compaction_failed",
10233
+ retryable: false,
10234
+ recovery: "user_message",
10235
+ compacted: false
10236
+ }
10237
+ },
10238
+ {
10239
+ type: "session.status.changed",
10240
+ payload: { status: "idle" }
10241
+ }
10242
+ ],
10243
+ turnStatus: "failed",
10244
+ sessionStatus: "idle",
10245
+ activeTurnId: null,
10246
+ ...landmark.requestConsumed ? {} : { consumeRequestedCompactionFailure: true }
10247
+ })) {
10248
+ return claimedResult({ status: "cancelled" });
10249
+ }
10250
+ turnMetricOutcome = "failed";
10251
+ activityStatus = "idle";
10252
+ activityError = error;
10253
+ return claimedResult({ status: "idle" });
10254
+ }
10255
+ if (outcome.events.length > 0) {
10256
+ if (outcome.compacted) {
10257
+ recordContextCompaction(observability, "operator");
10258
+ }
10259
+ await publishCompactionOutcomeEvents(outcome.events);
10260
+ }
10261
+ }
10262
+ if (!await settle({
10263
+ events: [
10264
+ {
10265
+ type: "turn.completed",
10266
+ payload: {
10267
+ maintenance: "context_compaction",
10268
+ result: outcome?.compacted ? "compacted" : outcome?.reason ?? "already_applied"
10269
+ }
10270
+ },
10271
+ { type: "session.status.changed", payload: { status: "idle" } }
10272
+ ],
10273
+ turnStatus: "completed",
10274
+ sessionStatus: "idle",
10275
+ activeTurnId: null
10276
+ })) {
10277
+ return claimedResult({ status: "cancelled" });
10278
+ }
10279
+ turnMetricOutcome = "completed";
10280
+ activityStatus = "idle";
10281
+ return claimedResult({ status: "idle" });
10282
+ }
10075
10283
  if (triggerType === "user.message" || triggerType === "system.update.delivered") {
10076
10284
  let forced = false;
10077
10285
  try {
@@ -12391,6 +12599,17 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
12391
12599
  const inputTokens = sanitizedUsage.inputTokens ?? 0;
12392
12600
  const outputTokens = sanitizedUsage.outputTokens ?? 0;
12393
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
+ }
12394
12613
  if (input.externallyBilled) {
12395
12614
  await recordUsageEvent(db, {
12396
12615
  accountId: input.accountId,
@@ -12427,17 +12646,20 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
12427
12646
  });
12428
12647
  }
12429
12648
  const shouldDebit = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
12430
- if (!shouldDebit || totalTokens === 0) {
12649
+ if (!shouldDebit || totalTokens === 0 && !gatewayBilling) {
12431
12650
  return {
12432
12651
  billingPath: "opengeni_credits",
12433
12652
  pricedCostMicros: 0,
12434
- normalizedUsage
12653
+ normalizedUsage,
12654
+ ...gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}
12435
12655
  };
12436
12656
  }
12437
12657
  if (!configuredModelPricing(settings)[input.model]) {
12438
12658
  throw new Error(`Missing model pricing for ${input.model}`);
12439
12659
  }
12440
- 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, {
12441
12663
  latencyMode: input.latencyMode ?? "standard"
12442
12664
  });
12443
12665
  await recordUsageEvent(db, {
@@ -12474,7 +12696,8 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
12474
12696
  // Additive: the prompt-cache slice of this call's input tokens, so the
12475
12697
  // per-call debit record carries cache efficiency alongside the token
12476
12698
  // counts. 0 when the provider did not report cached tokens.
12477
- cachedTokens: normalizedUsage.telemetry.cachedTokens ?? 0
12699
+ cachedTokens: normalizedUsage.telemetry.cachedTokens ?? 0,
12700
+ ...gatewayBilling ? { gatewayProvider: gatewayBilling.finalProvider } : {}
12478
12701
  }
12479
12702
  });
12480
12703
  recordCreditMicros(input.observability, "usage", result.debitedMicros);
@@ -12482,7 +12705,8 @@ async function recordModelUsageAndDebitCredits(settings, db, input) {
12482
12705
  return {
12483
12706
  billingPath: "opengeni_credits",
12484
12707
  pricedCostMicros: costMicros,
12485
- normalizedUsage
12708
+ normalizedUsage,
12709
+ ...gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}
12486
12710
  };
12487
12711
  }
12488
12712
  async function recordAuthoritativeModelCallFact(input) {
@@ -12495,7 +12719,7 @@ async function recordAuthoritativeModelCallFact(input) {
12495
12719
  turnId: input.turnId,
12496
12720
  turnAttemptId: input.turnAttemptId,
12497
12721
  sourceKey: input.sourceKey,
12498
- provider: input.provider,
12722
+ provider: input.billing.upstreamProvider ?? input.provider,
12499
12723
  providerApi: input.providerApi,
12500
12724
  model: input.model,
12501
12725
  billingPath: input.billing.billingPath,