@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.
- package/dist/activities/agent-turn.d.ts +3 -0
- package/dist/index.js +74 -23
- package/dist/index.js.map +1 -1
- package/package.json +12 -12
- package/src/activities/agent-turn.ts +88 -28
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,8 +96,11 @@ 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,
|
|
100
106
|
serializedToolsForRemoteCompaction,
|
|
@@ -142,6 +148,7 @@ import {
|
|
|
142
148
|
import { connectionTokenResolverForTurn } from "./mcp-credentials";
|
|
143
149
|
import {
|
|
144
150
|
assertTurnExecutionPolicyMatchesConfigV1,
|
|
151
|
+
calculateGatewayReportedCostMicros,
|
|
145
152
|
calculateModelUsageCostMicros,
|
|
146
153
|
configuredModelPricing,
|
|
147
154
|
configuredStaticUsageLimits,
|
|
@@ -151,6 +158,8 @@ import {
|
|
|
151
158
|
serviceTierForLatencyMode,
|
|
152
159
|
settingsWithResolvedModelContext,
|
|
153
160
|
resolveTurnExecutionPolicyV1,
|
|
161
|
+
OPENGENI_GATEWAY_MODELS,
|
|
162
|
+
OPENGENI_GATEWAY_PROVIDER_ID,
|
|
154
163
|
type ModelUsageInput,
|
|
155
164
|
type ModelProviderApi,
|
|
156
165
|
type RegistryProviderKind,
|
|
@@ -201,6 +210,7 @@ import {
|
|
|
201
210
|
codexRequestStorage,
|
|
202
211
|
isCodexTransportError,
|
|
203
212
|
withCodexRequestOverrides,
|
|
213
|
+
selectCodexCredentialId,
|
|
204
214
|
type CodexRequestContext,
|
|
205
215
|
type CodexUsageHeaderSnapshot,
|
|
206
216
|
} from "@opengeni/codex";
|
|
@@ -499,14 +509,7 @@ export function selectCodexCredentialForTurn(args: {
|
|
|
499
509
|
activeCredentialId: string | null;
|
|
500
510
|
connectedIds: Set<string>;
|
|
501
511
|
}): string | null {
|
|
502
|
-
|
|
503
|
-
if (pin && connectedIds.has(pin)) {
|
|
504
|
-
return pin;
|
|
505
|
-
}
|
|
506
|
-
if (active && connectedIds.has(active)) {
|
|
507
|
-
return active;
|
|
508
|
-
}
|
|
509
|
-
return null;
|
|
512
|
+
return selectCodexCredentialId(args);
|
|
510
513
|
}
|
|
511
514
|
|
|
512
515
|
export function filterUnmaterializedSandboxFileDownloads(
|
|
@@ -1186,6 +1189,8 @@ export async function processModelResponseUsageEvent(input: {
|
|
|
1186
1189
|
externallyBilled: input.externallyBilled,
|
|
1187
1190
|
usage: responseUsage.usage,
|
|
1188
1191
|
normalizedUsage,
|
|
1192
|
+
gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
|
|
1193
|
+
gatewayBilling: responseUsage.gatewayBilling,
|
|
1189
1194
|
sourceKey,
|
|
1190
1195
|
...(input.latencyMode ? { latencyMode: input.latencyMode } : {}),
|
|
1191
1196
|
observability: input.observability,
|
|
@@ -1312,6 +1317,8 @@ export async function processCompactionModelUsageEvent(input: {
|
|
|
1312
1317
|
externallyBilled: input.externallyBilled,
|
|
1313
1318
|
usage: input.usage.usage,
|
|
1314
1319
|
normalizedUsage,
|
|
1320
|
+
gatewayManaged: input.provider === OPENGENI_GATEWAY_PROVIDER_ID,
|
|
1321
|
+
gatewayBilling: input.usage.gatewayBilling,
|
|
1315
1322
|
sourceKey,
|
|
1316
1323
|
observability: input.observability,
|
|
1317
1324
|
});
|
|
@@ -4053,10 +4060,29 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
4053
4060
|
// deployment default). null means the workspace has no override, so the
|
|
4054
4061
|
// runtime falls back to runSettings.agentInstructionsTemplate (the
|
|
4055
4062
|
// deployment default, byte-identical to the historical preamble).
|
|
4056
|
-
const
|
|
4057
|
-
|
|
4058
|
-
input.workspaceId,
|
|
4059
|
-
|
|
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);
|
|
4060
4086
|
const workspaceMemory = await resolveWorkspaceMemoryBlock(db, input.workspaceId);
|
|
4061
4087
|
const baseRunSettings = {
|
|
4062
4088
|
// IMAGE PRECEDENCE (M3): rig > pack > deployment. settingsWithRigImage runs
|
|
@@ -4431,19 +4457,26 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
4431
4457
|
const persistentSessionSettings = {
|
|
4432
4458
|
titleIsSet: Boolean(session.title?.trim()),
|
|
4433
4459
|
};
|
|
4434
|
-
const compactionInstructions =
|
|
4435
|
-
|
|
4436
|
-
|
|
4437
|
-
|
|
4438
|
-
|
|
4439
|
-
|
|
4440
|
-
|
|
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,
|
|
4441
4474
|
),
|
|
4442
|
-
|
|
4475
|
+
session.instructions ?? undefined,
|
|
4443
4476
|
),
|
|
4444
|
-
|
|
4477
|
+
persistentSessionSettings,
|
|
4445
4478
|
),
|
|
4446
|
-
|
|
4479
|
+
workspaceMemory ?? undefined,
|
|
4447
4480
|
);
|
|
4448
4481
|
const requested = await isSessionCompactionRequested(
|
|
4449
4482
|
db,
|
|
@@ -5708,7 +5741,10 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
5708
5741
|
skillLibrarySelections: skillLibraryRuntime.skillLibrarySelections,
|
|
5709
5742
|
}
|
|
5710
5743
|
: {}),
|
|
5711
|
-
...(
|
|
5744
|
+
...(!structuredWorkspacePolicyActive && workspaceAgentInstructions
|
|
5745
|
+
? { instructionsTemplate: workspaceAgentInstructions }
|
|
5746
|
+
: {}),
|
|
5747
|
+
...(workspaceGovernance ? { workspaceGovernance } : {}),
|
|
5712
5748
|
...(workspaceMemory ? { workspaceMemory } : {}),
|
|
5713
5749
|
// Per-session persona tier (session > workspace > deployment default).
|
|
5714
5750
|
// Composed system-level AFTER the workspace persona so it refines it for
|
|
@@ -9211,6 +9247,7 @@ export type ModelUsageBillingRecord = {
|
|
|
9211
9247
|
/** Same quantity written to usage_events.model.cost when present; else 0. */
|
|
9212
9248
|
pricedCostMicros: number;
|
|
9213
9249
|
normalizedUsage: ModelCallUsageNormalization;
|
|
9250
|
+
upstreamProvider?: string;
|
|
9214
9251
|
};
|
|
9215
9252
|
|
|
9216
9253
|
// Exported for unit testing the external-billing bypass; not part of the activity surface.
|
|
@@ -9225,6 +9262,8 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
9225
9262
|
turnAttemptId: string;
|
|
9226
9263
|
model: string;
|
|
9227
9264
|
externallyBilled: boolean;
|
|
9265
|
+
gatewayManaged?: boolean;
|
|
9266
|
+
gatewayBilling?: ModelResponseUsage["gatewayBilling"];
|
|
9228
9267
|
usage?: ModelUsageInput | ModelCallUsageInput | null;
|
|
9229
9268
|
normalizedUsage?: ModelCallUsageNormalization;
|
|
9230
9269
|
sourceKey: string;
|
|
@@ -9240,6 +9279,20 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
9240
9279
|
const inputTokens = sanitizedUsage.inputTokens ?? 0;
|
|
9241
9280
|
const outputTokens = sanitizedUsage.outputTokens ?? 0;
|
|
9242
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
|
+
}
|
|
9243
9296
|
// An externally billed turn is paid outside OpenGeni, so it
|
|
9244
9297
|
// consumes ZERO OpenGeni credits and must never feed an OpenGeni cap. A
|
|
9245
9298
|
// codex/<slug> model has no entry in configuredModelPricing, so the normal path
|
|
@@ -9285,19 +9338,24 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
9285
9338
|
});
|
|
9286
9339
|
}
|
|
9287
9340
|
const shouldDebit = settings.billingMode === "stripe" || settings.usageLimitsMode === "managed";
|
|
9288
|
-
if (!shouldDebit || totalTokens === 0) {
|
|
9341
|
+
if (!shouldDebit || (totalTokens === 0 && !gatewayBilling)) {
|
|
9289
9342
|
return {
|
|
9290
9343
|
billingPath: "opengeni_credits",
|
|
9291
9344
|
pricedCostMicros: 0,
|
|
9292
9345
|
normalizedUsage,
|
|
9346
|
+
...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
|
|
9293
9347
|
};
|
|
9294
9348
|
}
|
|
9295
9349
|
if (!configuredModelPricing(settings)[input.model]) {
|
|
9296
9350
|
throw new Error(`Missing model pricing for ${input.model}`);
|
|
9297
9351
|
}
|
|
9298
|
-
const costMicros =
|
|
9299
|
-
|
|
9300
|
-
|
|
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
|
+
});
|
|
9301
9359
|
await recordUsageEvent(db, {
|
|
9302
9360
|
accountId: input.accountId,
|
|
9303
9361
|
workspaceId: input.workspaceId,
|
|
@@ -9333,6 +9391,7 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
9333
9391
|
// per-call debit record carries cache efficiency alongside the token
|
|
9334
9392
|
// counts. 0 when the provider did not report cached tokens.
|
|
9335
9393
|
cachedTokens: normalizedUsage.telemetry.cachedTokens ?? 0,
|
|
9394
|
+
...(gatewayBilling ? { gatewayProvider: gatewayBilling.finalProvider } : {}),
|
|
9336
9395
|
},
|
|
9337
9396
|
});
|
|
9338
9397
|
recordCreditMicros(input.observability, "usage", result.debitedMicros);
|
|
@@ -9341,6 +9400,7 @@ export async function recordModelUsageAndDebitCredits(
|
|
|
9341
9400
|
billingPath: "opengeni_credits",
|
|
9342
9401
|
pricedCostMicros: costMicros,
|
|
9343
9402
|
normalizedUsage,
|
|
9403
|
+
...(gatewayBilling ? { upstreamProvider: gatewayBilling.finalProvider } : {}),
|
|
9344
9404
|
};
|
|
9345
9405
|
}
|
|
9346
9406
|
|
|
@@ -9368,7 +9428,7 @@ export async function recordAuthoritativeModelCallFact(input: {
|
|
|
9368
9428
|
turnId: input.turnId,
|
|
9369
9429
|
turnAttemptId: input.turnAttemptId,
|
|
9370
9430
|
sourceKey: input.sourceKey,
|
|
9371
|
-
provider: input.provider,
|
|
9431
|
+
provider: input.billing.upstreamProvider ?? input.provider,
|
|
9372
9432
|
providerApi: input.providerApi,
|
|
9373
9433
|
model: input.model,
|
|
9374
9434
|
billingPath: input.billing.billingPath,
|