@opengeni/worker-bundle 0.15.0 → 0.15.1
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 +8 -0
- package/dist/activities/capabilities.d.ts +1 -0
- package/dist/index.js +183 -10
- package/dist/index.js.map +1 -1
- package/package.json +11 -11
- package/src/activities/agent-turn.ts +218 -8
- 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.15.
|
|
3
|
+
"version": "0.15.1",
|
|
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": {
|
|
@@ -41,16 +41,16 @@
|
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"@opengeni/agent-proto": "^0.3.0",
|
|
43
43
|
"@opengeni/codex": "^0.2.9",
|
|
44
|
-
"@opengeni/config": "^0.10.
|
|
45
|
-
"@opengeni/contracts": "^0.31.
|
|
46
|
-
"@opengeni/core": "^0.18.
|
|
47
|
-
"@opengeni/db": "^0.22.
|
|
48
|
-
"@opengeni/documents": "^0.2.
|
|
49
|
-
"@opengeni/events": "^0.3.
|
|
50
|
-
"@opengeni/github": "^0.4.
|
|
51
|
-
"@opengeni/observability": "^0.4.
|
|
52
|
-
"@opengeni/runtime": "^0.17.
|
|
53
|
-
"@opengeni/storage": "^0.2.
|
|
44
|
+
"@opengeni/config": "^0.10.1",
|
|
45
|
+
"@opengeni/contracts": "^0.31.1",
|
|
46
|
+
"@opengeni/core": "^0.18.1",
|
|
47
|
+
"@opengeni/db": "^0.22.2",
|
|
48
|
+
"@opengeni/documents": "^0.2.70",
|
|
49
|
+
"@opengeni/events": "^0.3.60",
|
|
50
|
+
"@opengeni/github": "^0.4.17",
|
|
51
|
+
"@opengeni/observability": "^0.4.4",
|
|
52
|
+
"@opengeni/runtime": "^0.17.1",
|
|
53
|
+
"@opengeni/storage": "^0.2.54",
|
|
54
54
|
"@temporalio/activity": "^1.17.0",
|
|
55
55
|
"@temporalio/client": "^1.17.0",
|
|
56
56
|
"@temporalio/worker": "^1.17.0",
|
|
@@ -97,6 +97,7 @@ import {
|
|
|
97
97
|
composeAgentInstructions,
|
|
98
98
|
summarizeForCompaction,
|
|
99
99
|
requestRemoteCompactionV2,
|
|
100
|
+
serializedToolsForRemoteCompaction,
|
|
100
101
|
REMOTE_COMPACTION_V2_BETA_FEATURE,
|
|
101
102
|
REMOTE_COMPACTION_V2_IMPLEMENTATION,
|
|
102
103
|
CompactionProviderResponseError,
|
|
@@ -160,6 +161,7 @@ import {
|
|
|
160
161
|
settingsWithCodexCredential,
|
|
161
162
|
settingsWithEnabledCapabilityMcpServers,
|
|
162
163
|
settingsWithSessionMcpServersForRun,
|
|
164
|
+
settingsWithWorkspaceGatewayCredential,
|
|
163
165
|
} from "./capabilities";
|
|
164
166
|
import {
|
|
165
167
|
CODEX_USAGE_EXHAUSTED_PCT,
|
|
@@ -1850,6 +1852,8 @@ export function shouldStartOnTurnRecording(params: {
|
|
|
1850
1852
|
* • a "chat" (OpenAIChatCompletionsModel wire) provider → "disabled": it cannot
|
|
1851
1853
|
* receive a screenshot through a proven visual transport, so computer use fails
|
|
1852
1854
|
* closed instead of serializing the image as text.
|
|
1855
|
+
* • Vercel Gateway providers → "disabled": their curated models support ordinary
|
|
1856
|
+
* function tools and vision input, not OpenAI's hosted computer tool.
|
|
1853
1857
|
* • everything else — built-in Azure/OpenAI responses, registry "responses"
|
|
1854
1858
|
* providers, AND the LEGACY global-client fallback (resolveTurnModel returned
|
|
1855
1859
|
* null) — → "hosted": real Responses hosted-tool support.
|
|
@@ -1867,12 +1871,28 @@ export function computerToolModeForTurn(
|
|
|
1867
1871
|
if (resolvedModel.provider.kind === "codex-subscription") {
|
|
1868
1872
|
return "function-image";
|
|
1869
1873
|
}
|
|
1874
|
+
if (
|
|
1875
|
+
resolvedModel.provider.kind === "vercel-gateway-managed" ||
|
|
1876
|
+
resolvedModel.provider.kind === "vercel-gateway-workspace"
|
|
1877
|
+
) {
|
|
1878
|
+
return "disabled";
|
|
1879
|
+
}
|
|
1870
1880
|
if (resolvedModel.provider.api === "chat") {
|
|
1871
1881
|
return "disabled";
|
|
1872
1882
|
}
|
|
1873
1883
|
return "hosted";
|
|
1874
1884
|
}
|
|
1875
1885
|
|
|
1886
|
+
/** Gateway models do not advertise OpenAI's hosted apply_patch/view_image tool types. */
|
|
1887
|
+
export function structuredToolTransportForTurn(
|
|
1888
|
+
resolvedModel: { provider: { kind: RegistryProviderKind } } | null,
|
|
1889
|
+
): boolean {
|
|
1890
|
+
if (!resolvedModel) return true;
|
|
1891
|
+
return !["codex-subscription", "vercel-gateway-managed", "vercel-gateway-workspace"].includes(
|
|
1892
|
+
resolvedModel.provider.kind,
|
|
1893
|
+
);
|
|
1894
|
+
}
|
|
1895
|
+
|
|
1876
1896
|
/**
|
|
1877
1897
|
* Native web search is a runtime capability, not part of the session's MCP
|
|
1878
1898
|
* allow-list. Attach it whenever the resolved provider advertises runnable
|
|
@@ -3086,12 +3106,17 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
3086
3106
|
mcpSettings,
|
|
3087
3107
|
input.workspaceId,
|
|
3088
3108
|
);
|
|
3089
|
-
const
|
|
3109
|
+
const codexSettings = await settingsWithCodexCredential(
|
|
3090
3110
|
db,
|
|
3091
3111
|
input.workspaceId,
|
|
3092
3112
|
mcpSettings,
|
|
3093
3113
|
codexSubscriptionActive,
|
|
3094
3114
|
);
|
|
3115
|
+
const capabilitySettings = await settingsWithWorkspaceGatewayCredential(
|
|
3116
|
+
db,
|
|
3117
|
+
input.workspaceId,
|
|
3118
|
+
codexSettings,
|
|
3119
|
+
);
|
|
3095
3120
|
runtime.configure(capabilitySettings);
|
|
3096
3121
|
const session = await requireSession(db, input.workspaceId, input.sessionId);
|
|
3097
3122
|
const claim = await claimSessionWorkForAttempt(db, input.workspaceId, {
|
|
@@ -4312,17 +4337,61 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
4312
4337
|
...(systemInstructions ? { systemInstructions } : {}),
|
|
4313
4338
|
...(promptCacheKey ? { promptCacheKey } : {}),
|
|
4314
4339
|
});
|
|
4340
|
+
// Prompt-cache prefix for remote_v2 MUST match ordinary turns:
|
|
4341
|
+
// tools → instructions → history. Filled after buildAgent for every
|
|
4342
|
+
// compact path (including operator /compact, which now builds the agent
|
|
4343
|
+
// first so it does not send empty tools/instructions).
|
|
4344
|
+
let remoteCompactionTools: Awaited<ReturnType<typeof serializedToolsForRemoteCompaction>> =
|
|
4345
|
+
[];
|
|
4346
|
+
let remoteCompactionInstructions = "";
|
|
4347
|
+
let remoteCompactionToolsReady = false;
|
|
4348
|
+
let remoteCompactionAgent: Parameters<typeof serializedToolsForRemoteCompaction>[0] | null =
|
|
4349
|
+
null;
|
|
4315
4350
|
const remoteCompactionRequester =
|
|
4316
4351
|
resolvedModel && isCodexTurn
|
|
4317
4352
|
? (s: Settings, m: Array<Record<string, unknown>>) =>
|
|
4318
|
-
withCodexRemoteCompaction(() =>
|
|
4319
|
-
|
|
4353
|
+
withCodexRemoteCompaction(async () => {
|
|
4354
|
+
// Lazily serialize tools here so EmptyCompactionSummaryError is
|
|
4355
|
+
// thrown inside the compaction try/settlement handlers, not as a
|
|
4356
|
+
// raw activity failure before maybeCompactContext runs.
|
|
4357
|
+
if (!remoteCompactionInstructions.trim()) {
|
|
4358
|
+
throw new EmptyCompactionSummaryError({
|
|
4359
|
+
stage: "remote_v2_instructions",
|
|
4360
|
+
reason: "agent_missing_system_instructions",
|
|
4361
|
+
});
|
|
4362
|
+
}
|
|
4363
|
+
if (!remoteCompactionToolsReady) {
|
|
4364
|
+
if (!remoteCompactionAgent) {
|
|
4365
|
+
throw new EmptyCompactionSummaryError({
|
|
4366
|
+
stage: "remote_v2_tools",
|
|
4367
|
+
reason: "agent_missing_for_tools",
|
|
4368
|
+
});
|
|
4369
|
+
}
|
|
4370
|
+
try {
|
|
4371
|
+
remoteCompactionTools =
|
|
4372
|
+
await serializedToolsForRemoteCompaction(remoteCompactionAgent);
|
|
4373
|
+
} catch (error) {
|
|
4374
|
+
// Tool schemas sit before instructions in the cache prefix.
|
|
4375
|
+
// Failing open to [] would reintroduce a massive pre-compact
|
|
4376
|
+
// cache bust — fail closed so we never send a tools mismatch
|
|
4377
|
+
// on purpose.
|
|
4378
|
+
throw new EmptyCompactionSummaryError({
|
|
4379
|
+
stage: "remote_v2_tools",
|
|
4380
|
+
reason: "serialize_tools_failed",
|
|
4381
|
+
error: String(error),
|
|
4382
|
+
});
|
|
4383
|
+
}
|
|
4384
|
+
remoteCompactionToolsReady = true;
|
|
4385
|
+
}
|
|
4386
|
+
return requestRemoteCompactionV2(s, m, {
|
|
4320
4387
|
client: resolvedModel.client,
|
|
4321
4388
|
model: turnExecutionPolicy.upstreamModelId,
|
|
4389
|
+
systemInstructions: remoteCompactionInstructions,
|
|
4322
4390
|
onUsage: recordCompactionUsage,
|
|
4391
|
+
tools: remoteCompactionTools,
|
|
4323
4392
|
...(promptCacheKey ? { promptCacheKey } : {}),
|
|
4324
|
-
})
|
|
4325
|
-
)
|
|
4393
|
+
});
|
|
4394
|
+
})
|
|
4326
4395
|
: undefined;
|
|
4327
4396
|
const publishCompactionLiveEvents = async (events: SessionEvent[]) => {
|
|
4328
4397
|
await publishDurableSessionEvents(bus, input.workspaceId, input.sessionId, events);
|
|
@@ -4348,7 +4417,17 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
4348
4417
|
: {}),
|
|
4349
4418
|
} as const;
|
|
4350
4419
|
|
|
4351
|
-
|
|
4420
|
+
// Operator /compact:
|
|
4421
|
+
// - portable (incl. Codex portable): early maintenance path — no
|
|
4422
|
+
// prepareTools/sandbox; summarizer only needs composed instructions.
|
|
4423
|
+
// - remote_v2: fall through to prepareTools/buildAgent so the compact
|
|
4424
|
+
// request reuses the ordinary tools→instructions cache prefix, then
|
|
4425
|
+
// settle without inference. (Requester is also wired for Codex portable
|
|
4426
|
+
// turns but unused there — gate on the frozen session mode.)
|
|
4427
|
+
const compactionOnlyTurn = turn.source === "compaction";
|
|
4428
|
+
const remoteV2CompactionNeedsAgentPrefix =
|
|
4429
|
+
Boolean(remoteCompactionRequester) && session.codexCompactionMode === "remote_v2";
|
|
4430
|
+
if (compactionOnlyTurn && !remoteV2CompactionNeedsAgentPrefix) {
|
|
4352
4431
|
const persistentSessionSettings = {
|
|
4353
4432
|
titleIsSet: Boolean(session.title?.trim()),
|
|
4354
4433
|
};
|
|
@@ -5592,7 +5671,7 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
5592
5671
|
// (built-in OpenAI/Azure = real hosted support; registry "chat"
|
|
5593
5672
|
// providers = the SDK's own ChatCompletions detection) keeps the SDK
|
|
5594
5673
|
// default.
|
|
5595
|
-
structuredToolTransport: resolvedModel
|
|
5674
|
+
structuredToolTransport: structuredToolTransportForTurn(resolvedModel),
|
|
5596
5675
|
// EXPLICIT computer-use tool transport, derived from the resolved provider's
|
|
5597
5676
|
// authoritative wire identity (codex → function-image, chat → disabled,
|
|
5598
5677
|
// responses → hosted) so the runtime never string-sniffs the model instance's
|
|
@@ -5877,9 +5956,140 @@ export function createRunAgentTurnActivity(services: () => Promise<ActivityServi
|
|
|
5877
5956
|
},
|
|
5878
5957
|
);
|
|
5879
5958
|
}
|
|
5959
|
+
const agentInstructions = typeof agent.instructions === "string" ? agent.instructions : "";
|
|
5880
5960
|
const compactSummarizer = compactionSummarizerFor(
|
|
5881
|
-
|
|
5961
|
+
agentInstructions.trim() ? agentInstructions : undefined,
|
|
5882
5962
|
);
|
|
5963
|
+
if (remoteCompactionRequester) {
|
|
5964
|
+
// Exact byte match with the ordinary turn prefix (CLI base_instructions).
|
|
5965
|
+
// Tools serialize lazily inside the requester so setup failures settle as
|
|
5966
|
+
// compaction failures rather than raw activity crashes.
|
|
5967
|
+
remoteCompactionInstructions = agentInstructions;
|
|
5968
|
+
remoteCompactionAgent = agent;
|
|
5969
|
+
}
|
|
5970
|
+
|
|
5971
|
+
if (compactionOnlyTurn) {
|
|
5972
|
+
const requested = await isSessionCompactionRequested(
|
|
5973
|
+
db,
|
|
5974
|
+
input.workspaceId,
|
|
5975
|
+
input.sessionId,
|
|
5976
|
+
);
|
|
5977
|
+
let outcome: Awaited<ReturnType<typeof maybeCompactContext>> | null = null;
|
|
5978
|
+
if (requested) {
|
|
5979
|
+
try {
|
|
5980
|
+
outcome = await waitForTurnOperation(
|
|
5981
|
+
maybeCompactContext(
|
|
5982
|
+
db,
|
|
5983
|
+
modelRunSettings,
|
|
5984
|
+
{
|
|
5985
|
+
accountId: input.accountId,
|
|
5986
|
+
workspaceId: input.workspaceId,
|
|
5987
|
+
sessionId: input.sessionId,
|
|
5988
|
+
turnId: turn.id,
|
|
5989
|
+
executionGeneration,
|
|
5990
|
+
attemptId: input.attemptId,
|
|
5991
|
+
},
|
|
5992
|
+
session.lastInputTokens,
|
|
5993
|
+
compactSummarizer,
|
|
5994
|
+
{
|
|
5995
|
+
force: true,
|
|
5996
|
+
clearRequestedCompaction: true,
|
|
5997
|
+
trigger: "operator",
|
|
5998
|
+
codexAccount: {
|
|
5999
|
+
currentCodexCredentialId: effectiveCodexCredentialId,
|
|
6000
|
+
},
|
|
6001
|
+
...compactionModeOptions,
|
|
6002
|
+
},
|
|
6003
|
+
),
|
|
6004
|
+
cancellationSignal,
|
|
6005
|
+
undefined,
|
|
6006
|
+
);
|
|
6007
|
+
} catch (error) {
|
|
6008
|
+
// Codex retries retryable checkpoint-provider failures rather than
|
|
6009
|
+
// treating them as a semantic compaction result. Keep the operator
|
|
6010
|
+
// request pending and let the ordinary same-turn provider/capacity
|
|
6011
|
+
// recovery path re-dispatch this exact maintenance execution.
|
|
6012
|
+
if (shouldRecoverCompactionProviderFailure(error)) throw error;
|
|
6013
|
+
if (error instanceof TurnAttemptFencedError) throw error;
|
|
6014
|
+
const landmark = await settleFailedContextCompactionLandmark(
|
|
6015
|
+
db,
|
|
6016
|
+
{
|
|
6017
|
+
accountId: input.accountId,
|
|
6018
|
+
workspaceId: input.workspaceId,
|
|
6019
|
+
sessionId: input.sessionId,
|
|
6020
|
+
turnId: turn.id,
|
|
6021
|
+
executionGeneration,
|
|
6022
|
+
attemptId: input.attemptId,
|
|
6023
|
+
},
|
|
6024
|
+
{
|
|
6025
|
+
clearRequestedCompaction: true,
|
|
6026
|
+
publishLiveEvents: publishCompactionLiveEvents,
|
|
6027
|
+
},
|
|
6028
|
+
);
|
|
6029
|
+
if (!isCompactionSummaryFailure(error)) throw error;
|
|
6030
|
+
const errorMessage = String(redact(compactionFailureReasonFromError(error)));
|
|
6031
|
+
if (
|
|
6032
|
+
!(await settle!({
|
|
6033
|
+
events: [
|
|
6034
|
+
{
|
|
6035
|
+
type: "turn.failed",
|
|
6036
|
+
payload: {
|
|
6037
|
+
error: errorMessage,
|
|
6038
|
+
code: "context_compaction_failed",
|
|
6039
|
+
retryable: false,
|
|
6040
|
+
recovery: "user_message",
|
|
6041
|
+
compacted: false,
|
|
6042
|
+
},
|
|
6043
|
+
},
|
|
6044
|
+
{
|
|
6045
|
+
type: "session.status.changed",
|
|
6046
|
+
payload: { status: "idle" },
|
|
6047
|
+
},
|
|
6048
|
+
],
|
|
6049
|
+
turnStatus: "failed",
|
|
6050
|
+
sessionStatus: "idle",
|
|
6051
|
+
activeTurnId: null,
|
|
6052
|
+
...(landmark.requestConsumed ? {} : { consumeRequestedCompactionFailure: true }),
|
|
6053
|
+
}))
|
|
6054
|
+
) {
|
|
6055
|
+
return claimedResult({ status: "cancelled" });
|
|
6056
|
+
}
|
|
6057
|
+
turnMetricOutcome = "failed";
|
|
6058
|
+
activityStatus = "idle";
|
|
6059
|
+
activityError = error;
|
|
6060
|
+
return claimedResult({ status: "idle" });
|
|
6061
|
+
}
|
|
6062
|
+
if (outcome.events.length > 0) {
|
|
6063
|
+
if (outcome.compacted) {
|
|
6064
|
+
recordContextCompaction(observability, "operator");
|
|
6065
|
+
}
|
|
6066
|
+
await publishCompactionOutcomeEvents(outcome.events);
|
|
6067
|
+
}
|
|
6068
|
+
}
|
|
6069
|
+
if (
|
|
6070
|
+
!(await settle!({
|
|
6071
|
+
events: [
|
|
6072
|
+
{
|
|
6073
|
+
type: "turn.completed",
|
|
6074
|
+
payload: {
|
|
6075
|
+
maintenance: "context_compaction",
|
|
6076
|
+
result: outcome?.compacted ? "compacted" : (outcome?.reason ?? "already_applied"),
|
|
6077
|
+
},
|
|
6078
|
+
},
|
|
6079
|
+
{ type: "session.status.changed", payload: { status: "idle" } },
|
|
6080
|
+
],
|
|
6081
|
+
turnStatus: "completed",
|
|
6082
|
+
sessionStatus: "idle",
|
|
6083
|
+
activeTurnId: null,
|
|
6084
|
+
}))
|
|
6085
|
+
) {
|
|
6086
|
+
return claimedResult({ status: "cancelled" });
|
|
6087
|
+
}
|
|
6088
|
+
turnMetricOutcome = "completed";
|
|
6089
|
+
activityStatus = "idle";
|
|
6090
|
+
return claimedResult({ status: "idle" });
|
|
6091
|
+
}
|
|
6092
|
+
|
|
5883
6093
|
// Pre-turn durable context compaction. When the single Codex-parity
|
|
5884
6094
|
// threshold is crossed, this summarizes active history and rebuilds active
|
|
5885
6095
|
// history as [user messages..., summary] BEFORE the model input is read.
|
|
@@ -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
|
+
}
|