@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.
- package/dist/activities/agent-turn.d.ts +11 -0
- package/dist/activities/capabilities.d.ts +1 -0
- package/dist/index.js +257 -33
- package/dist/index.js.map +1 -1
- package/package.json +12 -12
- package/src/activities/agent-turn.ts +306 -36
- package/src/activities/capabilities.ts +13 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opengeni/worker-bundle",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"description": "OpenGeni worker entry and reusable embedded lifecycle, shipped with a release-coherent pre-bundled Temporal workflow artifact.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -40,17 +40,17 @@
|
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@opengeni/agent-proto": "^0.3.0",
|
|
43
|
-
"@opengeni/codex": "^0.2.
|
|
44
|
-
"@opengeni/config": "^0.10.
|
|
45
|
-
"@opengeni/contracts": "^0.
|
|
46
|
-
"@opengeni/core": "^0.
|
|
47
|
-
"@opengeni/db": "^0.
|
|
48
|
-
"@opengeni/documents": "^0.2.
|
|
49
|
-
"@opengeni/events": "^0.3.
|
|
50
|
-
"@opengeni/github": "^0.4.
|
|
51
|
-
"@opengeni/observability": "^0.4.
|
|
52
|
-
"@opengeni/runtime": "^0.
|
|
53
|
-
"@opengeni/storage": "^0.2.
|
|
43
|
+
"@opengeni/codex": "^0.2.10",
|
|
44
|
+
"@opengeni/config": "^0.10.3",
|
|
45
|
+
"@opengeni/contracts": "^0.32.0",
|
|
46
|
+
"@opengeni/core": "^0.19.0",
|
|
47
|
+
"@opengeni/db": "^0.23.0",
|
|
48
|
+
"@opengeni/documents": "^0.2.72",
|
|
49
|
+
"@opengeni/events": "^0.3.62",
|
|
50
|
+
"@opengeni/github": "^0.4.19",
|
|
51
|
+
"@opengeni/observability": "^0.4.6",
|
|
52
|
+
"@opengeni/runtime": "^0.18.0",
|
|
53
|
+
"@opengeni/storage": "^0.2.56",
|
|
54
54
|
"@temporalio/activity": "^1.17.0",
|
|
55
55
|
"@temporalio/client": "^1.17.0",
|
|
56
56
|
"@temporalio/worker": "^1.17.0",
|
|
@@ -70,6 +70,9 @@ import {
|
|
|
70
70
|
getEnrollment,
|
|
71
71
|
abandonRecordingForTurnAttempt,
|
|
72
72
|
markSessionAttemptQuiesced,
|
|
73
|
+
getOrCreatePreferenceRegistrySnapshot,
|
|
74
|
+
getOrCreateWorkspaceInstructionPolicySnapshot,
|
|
75
|
+
PreferenceRegistryInitiatorError,
|
|
73
76
|
beginConnectorActionExecution,
|
|
74
77
|
completeConnectorActionExecution,
|
|
75
78
|
prepareConnectorActionApproval,
|
|
@@ -93,10 +96,14 @@ import {
|
|
|
93
96
|
sanitizeHistoryItemsForModel,
|
|
94
97
|
appendPersistentSessionSettings,
|
|
95
98
|
appendSessionInstructions,
|
|
99
|
+
appendWorkspaceGovernance,
|
|
96
100
|
appendWorkspaceMemory,
|
|
97
101
|
composeAgentInstructions,
|
|
102
|
+
hasActiveWorkspaceInstructionPolicy,
|
|
103
|
+
renderWorkspaceGovernanceContext,
|
|
98
104
|
summarizeForCompaction,
|
|
99
105
|
requestRemoteCompactionV2,
|
|
106
|
+
serializedToolsForRemoteCompaction,
|
|
100
107
|
REMOTE_COMPACTION_V2_BETA_FEATURE,
|
|
101
108
|
REMOTE_COMPACTION_V2_IMPLEMENTATION,
|
|
102
109
|
CompactionProviderResponseError,
|
|
@@ -141,6 +148,7 @@ import {
|
|
|
141
148
|
import { connectionTokenResolverForTurn } from "./mcp-credentials";
|
|
142
149
|
import {
|
|
143
150
|
assertTurnExecutionPolicyMatchesConfigV1,
|
|
151
|
+
calculateGatewayReportedCostMicros,
|
|
144
152
|
calculateModelUsageCostMicros,
|
|
145
153
|
configuredModelPricing,
|
|
146
154
|
configuredStaticUsageLimits,
|
|
@@ -150,6 +158,8 @@ import {
|
|
|
150
158
|
serviceTierForLatencyMode,
|
|
151
159
|
settingsWithResolvedModelContext,
|
|
152
160
|
resolveTurnExecutionPolicyV1,
|
|
161
|
+
OPENGENI_GATEWAY_MODELS,
|
|
162
|
+
OPENGENI_GATEWAY_PROVIDER_ID,
|
|
153
163
|
type ModelUsageInput,
|
|
154
164
|
type ModelProviderApi,
|
|
155
165
|
type RegistryProviderKind,
|
|
@@ -160,6 +170,7 @@ import {
|
|
|
160
170
|
settingsWithCodexCredential,
|
|
161
171
|
settingsWithEnabledCapabilityMcpServers,
|
|
162
172
|
settingsWithSessionMcpServersForRun,
|
|
173
|
+
settingsWithWorkspaceGatewayCredential,
|
|
163
174
|
} from "./capabilities";
|
|
164
175
|
import {
|
|
165
176
|
CODEX_USAGE_EXHAUSTED_PCT,
|
|
@@ -199,6 +210,7 @@ import {
|
|
|
199
210
|
codexRequestStorage,
|
|
200
211
|
isCodexTransportError,
|
|
201
212
|
withCodexRequestOverrides,
|
|
213
|
+
selectCodexCredentialId,
|
|
202
214
|
type CodexRequestContext,
|
|
203
215
|
type CodexUsageHeaderSnapshot,
|
|
204
216
|
} from "@opengeni/codex";
|
|
@@ -497,14 +509,7 @@ export function selectCodexCredentialForTurn(args: {
|
|
|
497
509
|
activeCredentialId: string | null;
|
|
498
510
|
connectedIds: Set<string>;
|
|
499
511
|
}): string | null {
|
|
500
|
-
|
|
501
|
-
if (pin && connectedIds.has(pin)) {
|
|
502
|
-
return pin;
|
|
503
|
-
}
|
|
504
|
-
if (active && connectedIds.has(active)) {
|
|
505
|
-
return active;
|
|
506
|
-
}
|
|
507
|
-
return null;
|
|
512
|
+
return selectCodexCredentialId(args);
|
|
508
513
|
}
|
|
509
514
|
|
|
510
515
|
export function filterUnmaterializedSandboxFileDownloads(
|
|
@@ -1184,6 +1189,8 @@ export async function processModelResponseUsageEvent(input: {
|
|
|
1184
1189
|
externallyBilled: input.externallyBilled,
|
|
1185
1190
|
usage: responseUsage.usage,
|
|
1186
1191
|
normalizedUsage,
|
|
1192
|
+
gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
|
|
1193
|
+
gatewayBilling: responseUsage.gatewayBilling,
|
|
1187
1194
|
sourceKey,
|
|
1188
1195
|
...(input.latencyMode ? { latencyMode: input.latencyMode } : {}),
|
|
1189
1196
|
observability: input.observability,
|
|
@@ -1310,6 +1317,8 @@ export async function processCompactionModelUsageEvent(input: {
|
|
|
1310
1317
|
externallyBilled: input.externallyBilled,
|
|
1311
1318
|
usage: input.usage.usage,
|
|
1312
1319
|
normalizedUsage,
|
|
1320
|
+
gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
|
|
1321
|
+
gatewayBilling: input.usage.gatewayBilling,
|
|
1313
1322
|
sourceKey,
|
|
1314
1323
|
observability: input.observability,
|
|
1315
1324
|
});
|
|
@@ -1850,6 +1859,8 @@ export function shouldStartOnTurnRecording(params: {
|
|
|
1850
1859
|
* • a "chat" (OpenAIChatCompletionsModel wire) provider → "disabled": it cannot
|
|
1851
1860
|
* receive a screenshot through a proven visual transport, so computer use fails
|
|
1852
1861
|
* closed instead of serializing the image as text.
|
|
1862
|
+
* • Vercel Gateway providers → "disabled": their curated models support ordinary
|
|
1863
|
+
* function tools and vision input, not OpenAI's hosted computer tool.
|
|
1853
1864
|
* • everything else — built-in Azure/OpenAI responses, registry "responses"
|
|
1854
1865
|
* providers, AND the LEGACY global-client fallback (resolveTurnModel returned
|
|
1855
1866
|
* null) — → "hosted": real Responses hosted-tool support.
|
|
@@ -1867,12 +1878,28 @@ export function computerToolModeForTurn(
|
|
|
1867
1878
|
if (resolvedModel.provider.kind === "codex-subscription") {
|
|
1868
1879
|
return "function-image";
|
|
1869
1880
|
}
|
|
1881
|
+
if (
|
|
1882
|
+
resolvedModel.provider.kind === "vercel-gateway-managed" ||
|
|
1883
|
+
resolvedModel.provider.kind === "vercel-gateway-workspace"
|
|
1884
|
+
) {
|
|
1885
|
+
return "disabled";
|
|
1886
|
+
}
|
|
1870
1887
|
if (resolvedModel.provider.api === "chat") {
|
|
1871
1888
|
return "disabled";
|
|
1872
1889
|
}
|
|
1873
1890
|
return "hosted";
|
|
1874
1891
|
}
|
|
1875
1892
|
|
|
1893
|
+
/** Gateway models do not advertise OpenAI's hosted apply_patch/view_image tool types. */
|
|
1894
|
+
export function structuredToolTransportForTurn(
|
|
1895
|
+
resolvedModel: { provider: { kind: RegistryProviderKind } } | null,
|
|
1896
|
+
): boolean {
|
|
1897
|
+
if (!resolvedModel) return true;
|
|
1898
|
+
return !["codex-subscription", "vercel-gateway-managed", "vercel-gateway-workspace"].includes(
|
|
1899
|
+
resolvedModel.provider.kind,
|
|
1900
|
+
);
|
|
1901
|
+
}
|
|
1902
|
+
|
|
1876
1903
|
/**
|
|
1877
1904
|
* Native web search is a runtime capability, not part of the session's MCP
|
|
1878
1905
|
* allow-list. Attach it whenever the resolved provider advertises runnable
|
|
@@ -3086,12 +3113,17 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
3086
3113
|
mcpSettings,
|
|
3087
3114
|
input.workspaceId,
|
|
3088
3115
|
);
|
|
3089
|
-
const
|
|
3116
|
+
const codexSettings = await settingsWithCodexCredential(
|
|
3090
3117
|
db,
|
|
3091
3118
|
input.workspaceId,
|
|
3092
3119
|
mcpSettings,
|
|
3093
3120
|
codexSubscriptionActive,
|
|
3094
3121
|
);
|
|
3122
|
+
const capabilitySettings = await settingsWithWorkspaceGatewayCredential(
|
|
3123
|
+
db,
|
|
3124
|
+
input.workspaceId,
|
|
3125
|
+
codexSettings,
|
|
3126
|
+
);
|
|
3095
3127
|
runtime.configure(capabilitySettings);
|
|
3096
3128
|
const session = await requireSession(db, input.workspaceId, input.sessionId);
|
|
3097
3129
|
const claim = await claimSessionWorkForAttempt(db, input.workspaceId, {
|
|
@@ -4028,10 +4060,29 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
4028
4060
|
// deployment default). null means the workspace has no override, so the
|
|
4029
4061
|
// runtime falls back to runSettings.agentInstructionsTemplate (the
|
|
4030
4062
|
// deployment default, byte-identical to the historical preamble).
|
|
4031
|
-
const
|
|
4032
|
-
|
|
4033
|
-
input.workspaceId,
|
|
4034
|
-
|
|
4063
|
+
const governanceClaims = {
|
|
4064
|
+
accountId: input.accountId,
|
|
4065
|
+
workspaceId: input.workspaceId,
|
|
4066
|
+
sessionId: input.sessionId,
|
|
4067
|
+
turnId: turn.id,
|
|
4068
|
+
attemptId: input.attemptId,
|
|
4069
|
+
executionGeneration: turn.executionGeneration,
|
|
4070
|
+
};
|
|
4071
|
+
const [workspaceAgentInstructions, instructionPolicySnapshot, preferenceSnapshot] =
|
|
4072
|
+
await Promise.all([
|
|
4073
|
+
resolveWorkspaceAgentInstructions(db, input.workspaceId),
|
|
4074
|
+
getOrCreateWorkspaceInstructionPolicySnapshot(db, governanceClaims),
|
|
4075
|
+
getOrCreatePreferenceRegistrySnapshot(db, governanceClaims).catch((error) => {
|
|
4076
|
+
if (error instanceof PreferenceRegistryInitiatorError) return null;
|
|
4077
|
+
throw error;
|
|
4078
|
+
}),
|
|
4079
|
+
]);
|
|
4080
|
+
const workspaceGovernance = renderWorkspaceGovernanceContext({
|
|
4081
|
+
instructionPolicy: instructionPolicySnapshot,
|
|
4082
|
+
preferences: preferenceSnapshot,
|
|
4083
|
+
});
|
|
4084
|
+
const structuredWorkspacePolicyActive =
|
|
4085
|
+
hasActiveWorkspaceInstructionPolicy(instructionPolicySnapshot);
|
|
4035
4086
|
const workspaceMemory = await resolveWorkspaceMemoryBlock(db, input.workspaceId);
|
|
4036
4087
|
const baseRunSettings = {
|
|
4037
4088
|
// IMAGE PRECEDENCE (M3): rig > pack > deployment. settingsWithRigImage runs
|
|
@@ -4312,17 +4363,61 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
4312
4363
|
...(systemInstructions ? { systemInstructions } : {}),
|
|
4313
4364
|
...(promptCacheKey ? { promptCacheKey } : {}),
|
|
4314
4365
|
});
|
|
4366
|
+
// Prompt-cache prefix for remote_v2 MUST match ordinary turns:
|
|
4367
|
+
// tools → instructions → history. Filled after buildAgent for every
|
|
4368
|
+
// compact path (including operator /compact, which now builds the agent
|
|
4369
|
+
// first so it does not send empty tools/instructions).
|
|
4370
|
+
let remoteCompactionTools: Awaited<ReturnType<typeof serializedToolsForRemoteCompaction>> =
|
|
4371
|
+
[];
|
|
4372
|
+
let remoteCompactionInstructions = "";
|
|
4373
|
+
let remoteCompactionToolsReady = false;
|
|
4374
|
+
let remoteCompactionAgent: Parameters<typeof serializedToolsForRemoteCompaction>[0] | null =
|
|
4375
|
+
null;
|
|
4315
4376
|
const remoteCompactionRequester =
|
|
4316
4377
|
resolvedModel && isCodexTurn
|
|
4317
4378
|
? (s: Settings, m: Array<Record<string, unknown>>) =>
|
|
4318
|
-
withCodexRemoteCompaction(() =>
|
|
4319
|
-
|
|
4379
|
+
withCodexRemoteCompaction(async () => {
|
|
4380
|
+
// Lazily serialize tools here so EmptyCompactionSummaryError is
|
|
4381
|
+
// thrown inside the compaction try/settlement handlers, not as a
|
|
4382
|
+
// raw activity failure before maybeCompactContext runs.
|
|
4383
|
+
if (!remoteCompactionInstructions.trim()) {
|
|
4384
|
+
throw new EmptyCompactionSummaryError({
|
|
4385
|
+
stage: "remote_v2_instructions",
|
|
4386
|
+
reason: "agent_missing_system_instructions",
|
|
4387
|
+
});
|
|
4388
|
+
}
|
|
4389
|
+
if (!remoteCompactionToolsReady) {
|
|
4390
|
+
if (!remoteCompactionAgent) {
|
|
4391
|
+
throw new EmptyCompactionSummaryError({
|
|
4392
|
+
stage: "remote_v2_tools",
|
|
4393
|
+
reason: "agent_missing_for_tools",
|
|
4394
|
+
});
|
|
4395
|
+
}
|
|
4396
|
+
try {
|
|
4397
|
+
remoteCompactionTools =
|
|
4398
|
+
await serializedToolsForRemoteCompaction(remoteCompactionAgent);
|
|
4399
|
+
} catch (error) {
|
|
4400
|
+
// Tool schemas sit before instructions in the cache prefix.
|
|
4401
|
+
// Failing open to [] would reintroduce a massive pre-compact
|
|
4402
|
+
// cache bust — fail closed so we never send a tools mismatch
|
|
4403
|
+
// on purpose.
|
|
4404
|
+
throw new EmptyCompactionSummaryError({
|
|
4405
|
+
stage: "remote_v2_tools",
|
|
4406
|
+
reason: "serialize_tools_failed",
|
|
4407
|
+
error: String(error),
|
|
4408
|
+
});
|
|
4409
|
+
}
|
|
4410
|
+
remoteCompactionToolsReady = true;
|
|
4411
|
+
}
|
|
4412
|
+
return requestRemoteCompactionV2(s, m, {
|
|
4320
4413
|
client: resolvedModel.client,
|
|
4321
4414
|
model: turnExecutionPolicy.upstreamModelId,
|
|
4415
|
+
systemInstructions: remoteCompactionInstructions,
|
|
4322
4416
|
onUsage: recordCompactionUsage,
|
|
4417
|
+
tools: remoteCompactionTools,
|
|
4323
4418
|
...(promptCacheKey ? { promptCacheKey } : {}),
|
|
4324
|
-
})
|
|
4325
|
-
)
|
|
4419
|
+
});
|
|
4420
|
+
})
|
|
4326
4421
|
: undefined;
|
|
4327
4422
|
const publishCompactionLiveEvents = async (events: SessionEvent[]) => {
|
|
4328
4423
|
await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, events);
|
|
@@ -4348,23 +4443,40 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
4348
4443
|
: {}),
|
|
4349
4444
|
} as const;
|
|
4350
4445
|
|
|
4351
|
-
|
|
4446
|
+
// Operator /compact:
|
|
4447
|
+
// - portable (incl. Codex portable): early maintenance path — no
|
|
4448
|
+
// prepareTools/sandbox; summarizer only needs composed instructions.
|
|
4449
|
+
// - remote_v2: fall through to prepareTools/buildAgent so the compact
|
|
4450
|
+
// request reuses the ordinary tools→instructions cache prefix, then
|
|
4451
|
+
// settle without inference. (Requester is also wired for Codex portable
|
|
4452
|
+
// turns but unused there — gate on the frozen session mode.)
|
|
4453
|
+
const compactionOnlyTurn = turn.source === "compaction";
|
|
4454
|
+
const remoteV2CompactionNeedsAgentPrefix =
|
|
4455
|
+
Boolean(remoteCompactionRequester) && session.codexCompactionMode === "remote_v2";
|
|
4456
|
+
if (compactionOnlyTurn && !remoteV2CompactionNeedsAgentPrefix) {
|
|
4352
4457
|
const persistentSessionSettings = {
|
|
4353
4458
|
titleIsSet: Boolean(session.title?.trim()),
|
|
4354
4459
|
};
|
|
4355
|
-
const compactionInstructions =
|
|
4356
|
-
|
|
4357
|
-
|
|
4358
|
-
|
|
4359
|
-
|
|
4360
|
-
|
|
4361
|
-
|
|
4460
|
+
const compactionInstructions = appendWorkspaceMemory(
|
|
4461
|
+
appendPersistentSessionSettings(
|
|
4462
|
+
appendSessionInstructions(
|
|
4463
|
+
appendWorkspaceGovernance(
|
|
4464
|
+
composeAgentInstructions(
|
|
4465
|
+
structuredWorkspacePolicyActive
|
|
4466
|
+
? modelRunSettings.agentInstructionsTemplate
|
|
4467
|
+
: (workspaceAgentInstructions ?? modelRunSettings.agentInstructionsTemplate),
|
|
4468
|
+
undefined,
|
|
4469
|
+
rigVersion && rigName
|
|
4470
|
+
? { name: rigName, version: rigVersion.version }
|
|
4471
|
+
: undefined,
|
|
4472
|
+
),
|
|
4473
|
+
workspaceGovernance ?? undefined,
|
|
4362
4474
|
),
|
|
4363
|
-
|
|
4475
|
+
session.instructions ?? undefined,
|
|
4364
4476
|
),
|
|
4365
|
-
|
|
4477
|
+
persistentSessionSettings,
|
|
4366
4478
|
),
|
|
4367
|
-
|
|
4479
|
+
workspaceMemory ?? undefined,
|
|
4368
4480
|
);
|
|
4369
4481
|
const requested = await isSessionCompactionRequested(
|
|
4370
4482
|
db,
|
|
@@ -5592,7 +5704,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
5592
5704
|
// (built-in OpenAI/Azure = real hosted support; registry "chat"
|
|
5593
5705
|
// providers = the SDK's own ChatCompletions detection) keeps the SDK
|
|
5594
5706
|
// default.
|
|
5595
|
-
structuredToolTransport: resolvedModel
|
|
5707
|
+
structuredToolTransport: structuredToolTransportForTurn(resolvedModel),
|
|
5596
5708
|
// EXPLICIT computer-use tool transport, derived from the resolved provider's
|
|
5597
5709
|
// authoritative wire identity (codex → function-image, chat → disabled,
|
|
5598
5710
|
// responses → hosted) so the runtime never string-sniffs the model instance's
|
|
@@ -5629,7 +5741,10 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
5629
5741
|
skillLibrarySelections: skillLibraryRuntime.skillLibrarySelections,
|
|
5630
5742
|
}
|
|
5631
5743
|
: {}),
|
|
5632
|
-
...(
|
|
5744
|
+
...(!structuredWorkspacePolicyActive && workspaceAgentInstructions
|
|
5745
|
+
? { instructionsTemplate: workspaceAgentInstructions }
|
|
5746
|
+
: {}),
|
|
5747
|
+
...(workspaceGovernance ? { workspaceGovernance } : {}),
|
|
5633
5748
|
...(workspaceMemory ? { workspaceMemory } : {}),
|
|
5634
5749
|
// Per-session persona tier (session > workspace > deployment default).
|
|
5635
5750
|
// Composed system-level AFTER the workspace persona so it refines it for
|
|
@@ -5877,9 +5992,140 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
5877
5992
|
},
|
|
5878
5993
|
);
|
|
5879
5994
|
}
|
|
5995
|
+
const agentInstructions = typeof agent.instructions === "string" ? agent.instructions : "";
|
|
5880
5996
|
const compactSummarizer = compactionSummarizerFor(
|
|
5881
|
-
|
|
5997
|
+
agentInstructions.trim() ? agentInstructions : undefined,
|
|
5882
5998
|
);
|
|
5999
|
+
if (remoteCompactionRequester) {
|
|
6000
|
+
// Exact byte match with the ordinary turn prefix (CLI base_instructions).
|
|
6001
|
+
// Tools serialize lazily inside the requester so setup failures settle as
|
|
6002
|
+
// compaction failures rather than raw activity crashes.
|
|
6003
|
+
remoteCompactionInstructions = agentInstructions;
|
|
6004
|
+
remoteCompactionAgent = agent;
|
|
6005
|
+
}
|
|
6006
|
+
|
|
6007
|
+
if (compactionOnlyTurn) {
|
|
6008
|
+
const requested = await isSessionCompactionRequested(
|
|
6009
|
+
db,
|
|
6010
|
+
input.workspaceId,
|
|
6011
|
+
input.sessionId,
|
|
6012
|
+
);
|
|
6013
|
+
let outcome: Awaited<ReturnType<typeof maybeCompactContext>> | null = null;
|
|
6014
|
+
if (requested) {
|
|
6015
|
+
try {
|
|
6016
|
+
outcome = await waitForTurnOperation(
|
|
6017
|
+
maybeCompactContext(
|
|
6018
|
+
db,
|
|
6019
|
+
modelRunSettings,
|
|
6020
|
+
{
|
|
6021
|
+
accountId: input.accountId,
|
|
6022
|
+
workspaceId: input.workspaceId,
|
|
6023
|
+
sessionId: input.sessionId,
|
|
6024
|
+
turnId: turn.id,
|
|
6025
|
+
executionGeneration,
|
|
6026
|
+
attemptId: input.attemptId,
|
|
6027
|
+
},
|
|
6028
|
+
session.lastInputTokens,
|
|
6029
|
+
compactSummarizer,
|
|
6030
|
+
{
|
|
6031
|
+
force: true,
|
|
6032
|
+
clearRequestedCompaction: true,
|
|
6033
|
+
trigger: "operator",
|
|
6034
|
+
codexAccount: {
|
|
6035
|
+
currentCodexCredentialId: effectiveCodexCredentialId,
|
|
6036
|
+
},
|
|
6037
|
+
...compactionModeOptions,
|
|
6038
|
+
},
|
|
6039
|
+
),
|
|
6040
|
+
cancellationSignal,
|
|
6041
|
+
undefined,
|
|
6042
|
+
);
|
|
6043
|
+
} catch (error) {
|
|
6044
|
+
// Codex retries retryable checkpoint-provider failures rather than
|
|
6045
|
+
// treating them as a semantic compaction result. Keep the operator
|
|
6046
|
+
// request pending and let the ordinary same-turn provider/capacity
|
|
6047
|
+
// recovery path re-dispatch this exact maintenance execution.
|
|
6048
|
+
if (shouldRecoverCompactionProviderFailure(error)) throw error;
|
|
6049
|
+
if (error instanceof TurnAttemptFencedError) throw error;
|
|
6050
|
+
const landmark = await settleFailedContextCompactionLandmark(
|
|
6051
|
+
db,
|
|
6052
|
+
{
|
|
6053
|
+
accountId: input.accountId,
|
|
6054
|
+
workspaceId: input.workspaceId,
|
|
6055
|
+
sessionId: input.sessionId,
|
|
6056
|
+
turnId: turn.id,
|
|
6057
|
+
executionGeneration,
|
|
6058
|
+
attemptId: input.attemptId,
|
|
6059
|
+
},
|
|
6060
|
+
{
|
|
6061
|
+
clearRequestedCompaction: true,
|
|
6062
|
+
publishLiveEvents: publishCompactionLiveEvents,
|
|
6063
|
+
},
|
|
6064
|
+
);
|
|
6065
|
+
if (!isCompactionSummaryFailure(error)) throw error;
|
|
6066
|
+
const errorMessage = String(redact(compactionFailureReasonFromError(error)));
|
|
6067
|
+
if (
|
|
6068
|
+
!(await settle!({
|
|
6069
|
+
events: [
|
|
6070
|
+
{
|
|
6071
|
+
type: "turn.failed",
|
|
6072
|
+
payload: {
|
|
6073
|
+
error: errorMessage,
|
|
6074
|
+
code: "context_compaction_failed",
|
|
6075
|
+
retryable: false,
|
|
6076
|
+
recovery: "user_message",
|
|
6077
|
+
compacted: false,
|
|
6078
|
+
},
|
|
6079
|
+
},
|
|
6080
|
+
{
|
|
6081
|
+
type: "session.status.changed",
|
|
6082
|
+
payload: { status: "idle" },
|
|
6083
|
+
},
|
|
6084
|
+
],
|
|
6085
|
+
turnStatus: "failed",
|
|
6086
|
+
sessionStatus: "idle",
|
|
6087
|
+
activeTurnId: null,
|
|
6088
|
+
...(landmark.requestConsumed ? {} : { consumeRequestedCompactionFailure: true }),
|
|
6089
|
+
}))
|
|
6090
|
+
) {
|
|
6091
|
+
return claimedResult({ status: "cancelled" });
|
|
6092
|
+
}
|
|
6093
|
+
turnMetricOutcome = "failed";
|
|
6094
|
+
activityStatus = "idle";
|
|
6095
|
+
activityError = error;
|
|
6096
|
+
return claimedResult({ status: "idle" });
|
|
6097
|
+
}
|
|
6098
|
+
if (outcome.events.length > 0) {
|
|
6099
|
+
if (outcome.compacted) {
|
|
6100
|
+
recordContextCompaction(observability, "operator");
|
|
6101
|
+
}
|
|
6102
|
+
await publishCompactionOutcomeEvents(outcome.events);
|
|
6103
|
+
}
|
|
6104
|
+
}
|
|
6105
|
+
if (
|
|
6106
|
+
!(await settle!({
|
|
6107
|
+
events: [
|
|
6108
|
+
{
|
|
6109
|
+
type: "turn.completed",
|
|
6110
|
+
payload: {
|
|
6111
|
+
maintenance: "context_compaction",
|
|
6112
|
+
result: outcome?.compacted ? "compacted" : (outcome?.reason ?? "already_applied"),
|
|
6113
|
+
},
|
|
6114
|
+
},
|
|
6115
|
+
{ type: "session.status.changed", payload: { status: "idle" } },
|
|
6116
|
+
],
|
|
6117
|
+
turnStatus: "completed",
|
|
6118
|
+
sessionStatus: "idle",
|
|
6119
|
+
activeTurnId: null,
|
|
6120
|
+
}))
|
|
6121
|
+
) {
|
|
6122
|
+
return claimedResult({ status: "cancelled" });
|
|
6123
|
+
}
|
|
6124
|
+
turnMetricOutcome = "completed";
|
|
6125
|
+
activityStatus = "idle";
|
|
6126
|
+
return claimedResult({ status: "idle" });
|
|
6127
|
+
}
|
|
6128
|
+
|
|
5883
6129
|
// Pre-turn durable context compaction. When the single Codex-parity
|
|
5884
6130
|
// threshold is crossed, this summarizes active history and rebuilds active
|
|
5885
6131
|
// history as [user messages..., summary] BEFORE the model input is read.
|
|
@@ -9001,6 +9247,7 @@ export type ModelUsageBillingRecord = {
|
|
|
9001
9247
|
/** Same quantity written to usage_events.model.cost when present; else 0. */
|
|
9002
9248
|
pricedCostMicros: number;
|
|
9003
9249
|
normalizedUsage: ModelCallUsageNormalization;
|
|
9250
|
+
upstreamProvider?: string;
|
|
9004
9251
|
};
|
|
9005
9252
|
|
|
9006
9253
|
// Exported for unit testing the external-billing bypass; not part of the activity surface.
|
|
@@ -9015,6 +9262,8 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
9015
9262
|
turnAttemptId: string;
|
|
9016
9263
|
model: string;
|
|
9017
9264
|
externallyBilled: boolean;
|
|
9265
|
+
gatewayManaged?: boolean;
|
|
9266
|
+
gatewayBilling?: ModelResponseUsage["gatewayBilling"];
|
|
9018
9267
|
usage?: ModelUsageInput | ModelCallUsageInput | null;
|
|
9019
9268
|
normalizedUsage?: ModelCallUsageNormalization;
|
|
9020
9269
|
sourceKey: string;
|
|
@@ -9030,6 +9279,20 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
9030
9279
|
const inputTokens = sanitizedUsage.inputTokens ?? 0;
|
|
9031
9280
|
const outputTokens = sanitizedUsage.outputTokens ?? 0;
|
|
9032
9281
|
const totalTokens = sanitizedUsage.totalTokens ?? 0;
|
|
9282
|
+
const gatewayBilling = input.gatewayManaged ? input.gatewayBilling : undefined;
|
|
9283
|
+
if (gatewayBilling) {
|
|
9284
|
+
const gatewayModel = Object.values(OPENGENI_GATEWAY_MODELS).find(
|
|
9285
|
+
(candidate) => candidate.productId === input.model,
|
|
9286
|
+
);
|
|
9287
|
+
if (
|
|
9288
|
+
!gatewayModel ||
|
|
9289
|
+
!(gatewayModel.providers as readonly string[]).includes(gatewayBilling.finalProvider)
|
|
9290
|
+
) {
|
|
9291
|
+
throw new Error(
|
|
9292
|
+
`AI Gateway reported unapproved provider ${gatewayBilling.finalProvider} for ${input.model}`,
|
|
9293
|
+
);
|
|
9294
|
+
}
|
|
9295
|
+
}
|
|
9033
9296
|
// An externally billed turn is paid outside OpenGeni, so it
|
|
9034
9297
|
// consumes ZERO OpenGeni credits and must never feed an OpenGeni cap. A
|
|
9035
9298
|
// codex/<slug> model has no entry in configuredModelPricing, so the normal path
|
|
@@ -9075,19 +9338,24 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
9075
9338
|
});
|
|
9076
9339
|
}
|
|
9077
9340
|
const shouldDebit = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
|
|
9078
|
-
if (!shouldDebit || totalTokens === 0) {
|
|
9341
|
+
if (!shouldDebit || (totalTokens === 0 && !gatewayBilling)) {
|
|
9079
9342
|
return {
|
|
9080
9343
|
billingPath: "opengeni_credits",
|
|
9081
9344
|
pricedCostMicros: 0,
|
|
9082
9345
|
normalizedUsage,
|
|
9346
|
+
...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
|
|
9083
9347
|
};
|
|
9084
9348
|
}
|
|
9085
9349
|
if (!configuredModelPricing(settings)[input.model]) {
|
|
9086
9350
|
throw new Error(`Missing model pricing for ${input.model}`);
|
|
9087
9351
|
}
|
|
9088
|
-
const costMicros =
|
|
9089
|
-
|
|
9090
|
-
|
|
9352
|
+
const costMicros = gatewayBilling
|
|
9353
|
+
? calculateGatewayReportedCostMicros(settings, input.model, gatewayBilling.inferenceCostUsd, {
|
|
9354
|
+
inputTokens,
|
|
9355
|
+
})
|
|
9356
|
+
: calculateModelUsageCostMicros(settings, input.model, sanitizedUsage, {
|
|
9357
|
+
latencyMode: input.latencyMode ?? "standard",
|
|
9358
|
+
});
|
|
9091
9359
|
await recordUsageEvent(db, {
|
|
9092
9360
|
accountId: input.accountId,
|
|
9093
9361
|
workspaceId: input.workspaceId,
|
|
@@ -9123,6 +9391,7 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
9123
9391
|
// per-call debit record carries cache efficiency alongside the token
|
|
9124
9392
|
// counts. 0 when the provider did not report cached tokens.
|
|
9125
9393
|
cachedTokens: normalizedUsage.telemetry.cachedTokens ?? 0,
|
|
9394
|
+
...(gatewayBilling ? { gatewayProvider: gatewayBilling.finalProvider } : {}),
|
|
9126
9395
|
},
|
|
9127
9396
|
});
|
|
9128
9397
|
recordCreditMicros(input.observability, "usage", result.debitedMicros);
|
|
@@ -9131,6 +9400,7 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
9131
9400
|
billingPath: "opengeni_credits",
|
|
9132
9401
|
pricedCostMicros: costMicros,
|
|
9133
9402
|
normalizedUsage,
|
|
9403
|
+
...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
|
|
9134
9404
|
};
|
|
9135
9405
|
}
|
|
9136
9406
|
|
|
@@ -9158,7 +9428,7 @@ export async function recordAuthoritativeModelCallFact(input: {
|
|
|
9158
9428
|
turnId: input.turnId,
|
|
9159
9429
|
turnAttemptId: input.turnAttemptId,
|
|
9160
9430
|
sourceKey: input.sourceKey,
|
|
9161
|
-
provider: input.provider,
|
|
9431
|
+
provider: input.billing.upstreamProvider ?? input.provider,
|
|
9162
9432
|
providerApi: input.providerApi,
|
|
9163
9433
|
model: input.model,
|
|
9164
9434
|
billingPath: input.billing.billingPath,
|
|
@@ -2,12 +2,15 @@ import {
|
|
|
2
2
|
environmentsEncryptionKeyBytes,
|
|
3
3
|
type Settings,
|
|
4
4
|
withCodexCatalogProvider,
|
|
5
|
+
withWorkspaceGatewayCatalogProvider,
|
|
6
|
+
withWorkspaceGatewayCredential,
|
|
5
7
|
} from "@opengeni/config";
|
|
6
8
|
import { settingsWithEnabledCapabilityMcpServers } from "@opengeni/core";
|
|
7
9
|
import {
|
|
8
10
|
listSessionMcpServerMetadata,
|
|
9
11
|
listSessionMcpServersForRun,
|
|
10
12
|
workspaceCodexSubscriptionActive,
|
|
13
|
+
loadWorkspaceVercelAiGatewayApiKey,
|
|
11
14
|
type Database,
|
|
12
15
|
type SessionMcpServerForRun,
|
|
13
16
|
} from "@opengeni/db";
|
|
@@ -110,3 +113,13 @@ export async function settingsWithCodexCredential(
|
|
|
110
113
|
export function withCodexProvider(settings: Settings): Settings {
|
|
111
114
|
return withCodexCatalogProvider(settings);
|
|
112
115
|
}
|
|
116
|
+
|
|
117
|
+
export async function settingsWithWorkspaceGatewayCredential(
|
|
118
|
+
db: Database,
|
|
119
|
+
workspaceId: string,
|
|
120
|
+
settings: Settings,
|
|
121
|
+
): Promise<Settings> {
|
|
122
|
+
const catalogSettings = withWorkspaceGatewayCatalogProvider(settings);
|
|
123
|
+
const apiKey = await loadWorkspaceVercelAiGatewayApiKey(db, settings, workspaceId);
|
|
124
|
+
return apiKey ? withWorkspaceGatewayCredential(catalogSettings, apiKey) : catalogSettings;
|
|
125
|
+
}
|