@opengeni/worker-bundle 0.20.1 → 0.22.0-canary.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/admission.d.ts +52 -0
- package/dist/activities/agent-turn/agent-build.d.ts +89 -0
- package/dist/activities/agent-turn/claim.d.ts +67 -0
- package/dist/activities/agent-turn/codex-capacity.d.ts +40 -0
- package/dist/activities/agent-turn/codex.d.ts +32 -0
- package/dist/activities/agent-turn/compaction-prep.d.ts +88 -0
- package/dist/activities/agent-turn/credential-leases.d.ts +56 -0
- package/dist/activities/agent-turn/errors.d.ts +253 -0
- package/dist/activities/agent-turn/failure-settlement.d.ts +33 -0
- package/dist/activities/agent-turn/file-resources.d.ts +37 -0
- package/dist/activities/agent-turn/finalization.d.ts +42 -0
- package/dist/activities/agent-turn/github-repository-bindings.d.ts +105 -0
- package/dist/activities/agent-turn/governance-model.d.ts +66 -0
- package/dist/activities/agent-turn/history-sink.d.ts +52 -0
- package/dist/activities/agent-turn/history.d.ts +63 -0
- package/dist/activities/agent-turn/media-artifacts.d.ts +145 -0
- package/dist/activities/agent-turn/model-usage.d.ts +218 -0
- package/dist/activities/agent-turn/quiescence.d.ts +135 -0
- package/dist/activities/agent-turn/run-credentials.d.ts +84 -0
- package/dist/activities/agent-turn/run.d.ts +3 -0
- package/dist/activities/agent-turn/sandbox-establish.d.ts +93 -0
- package/dist/activities/agent-turn/sandbox-provision.d.ts +89 -0
- package/dist/activities/agent-turn/sandbox-route.d.ts +151 -0
- package/dist/activities/agent-turn/sandbox-runtime.d.ts +44 -0
- package/dist/activities/agent-turn/stream-attempt.d.ts +107 -0
- package/dist/activities/agent-turn/tool-environment.d.ts +142 -0
- package/dist/activities/agent-turn/tool-policy.d.ts +127 -0
- package/dist/activities/agent-turn/tool-result-spill.d.ts +59 -0
- package/dist/activities/agent-turn/turn-context.d.ts +144 -0
- package/dist/activities/agent-turn/xai-capacity.d.ts +2 -0
- package/dist/activities/agent-turn.d.ts +12 -954
- package/dist/activities/automations.d.ts +16 -0
- package/dist/activities/common.d.ts +1 -1
- package/dist/activities/connector-attachments.d.ts +13 -0
- package/dist/activities/documents.d.ts +1 -0
- package/dist/activities/environment.d.ts +26 -15
- package/dist/activities/generated-images.d.ts +1 -1
- package/dist/activities/git-credential-renewal.d.ts +3 -1
- package/dist/activities/goals.d.ts +4 -1
- package/dist/activities/google-drive-provider.d.ts +29 -0
- package/dist/activities/google-drive-publication.d.ts +66 -0
- package/dist/activities/image-generation-references.d.ts +2 -0
- package/dist/activities/incident-telemetry-authority.d.ts +52 -0
- package/dist/activities/incident-telemetry-preflight.d.ts +71 -0
- package/dist/activities/knowledge-source-sync-driver.d.ts +21 -0
- package/dist/activities/knowledge-source-sync.d.ts +33 -1
- package/dist/activities/mcp-credentials.d.ts +9 -3
- package/dist/activities/open-suffix-resume.d.ts +77 -0
- package/dist/activities/packs.d.ts +3 -11
- package/dist/activities/parent-wake.d.ts +19 -0
- package/dist/activities/retained-screenshot-reaper.d.ts +1 -1
- package/dist/activities/retained-screenshots.d.ts +1 -1
- package/dist/activities/rig-verification.d.ts +1 -1
- package/dist/activities/run-input.d.ts +16 -7
- package/dist/activities/sandbox-images.d.ts +1 -14
- package/dist/activities/sandbox-lease.d.ts +21 -3
- package/dist/activities/scheduled-tasks.d.ts +47 -0
- package/dist/activities/types.d.ts +23 -2
- package/dist/activities/video-media-validation.d.ts +1 -1
- package/dist/activities/video-output-retention.d.ts +1 -2
- package/dist/activities/xai-auth.d.ts +3 -0
- package/dist/{activities-control-SMKF2OBE.js → activities-control-DKG22ONJ.js} +2946 -549
- package/dist/activities-control-DKG22ONJ.js.map +1 -0
- package/dist/activities-control.d.ts +6 -2
- package/dist/activities-turn-4IMC3MAA.js +21326 -0
- package/dist/activities-turn-4IMC3MAA.js.map +1 -0
- package/dist/activities.d.ts +7 -3
- package/dist/artifact-materializer-entry.js +41 -22
- package/dist/artifact-materializer-entry.js.map +1 -1
- package/dist/{chunk-N5TJIFCE.js → chunk-CE7TPKBS.js} +468 -1
- package/dist/chunk-CE7TPKBS.js.map +1 -0
- package/dist/chunk-CNYGVRKF.js +4698 -0
- package/dist/chunk-CNYGVRKF.js.map +1 -0
- package/dist/connection-provider-use.d.ts +18 -0
- package/dist/editable-artifact-materializer.d.ts +4 -3
- package/dist/github-rest-mcp.d.ts +26 -0
- package/dist/index.d.ts +10 -2
- package/dist/index.js +58 -14
- package/dist/index.js.map +1 -1
- package/dist/knowledge-source-sync-activity-policy.d.ts +2 -0
- package/dist/model-context-contributions.d.ts +8 -4
- package/dist/observability-metrics.d.ts +63 -4
- package/dist/opensandbox-kubernetes-inventory.d.ts +21 -0
- package/dist/personal-github-git-credentials.d.ts +9 -0
- package/dist/pr-review-credentials.d.ts +6 -0
- package/dist/sandbox-archive-storage.d.ts +45 -0
- package/dist/sandbox-resume.d.ts +65 -6
- package/dist/sandbox-routing.d.ts +38 -2
- package/dist/scheduled-alert-occurrence.d.ts +33 -0
- package/dist/workflow-bundle.js +87 -28
- package/dist/workflows/activities.d.ts +1 -0
- package/dist/workflows/automations.d.ts +6 -0
- package/dist/workflows/document-index.d.ts +1 -0
- package/dist/workflows.d.ts +1 -0
- package/package.json +17 -17
- package/src/activities/agent-turn/admission.ts +292 -0
- package/src/activities/agent-turn/agent-build.ts +802 -0
- package/src/activities/agent-turn/claim.ts +590 -0
- package/src/activities/agent-turn/codex-capacity.ts +720 -0
- package/src/activities/agent-turn/codex.ts +94 -0
- package/src/activities/agent-turn/compaction-prep.ts +722 -0
- package/src/activities/agent-turn/credential-leases.ts +240 -0
- package/src/activities/agent-turn/errors.ts +1068 -0
- package/src/activities/agent-turn/failure-settlement.ts +1436 -0
- package/src/activities/agent-turn/file-resources.ts +227 -0
- package/src/activities/agent-turn/finalization.ts +754 -0
- package/src/activities/agent-turn/github-repository-bindings.ts +300 -0
- package/src/activities/agent-turn/governance-model.ts +465 -0
- package/src/activities/agent-turn/history-sink.ts +139 -0
- package/src/activities/agent-turn/history.ts +200 -0
- package/src/activities/agent-turn/media-artifacts.ts +359 -0
- package/src/activities/agent-turn/model-usage.ts +823 -0
- package/src/activities/agent-turn/quiescence.ts +353 -0
- package/src/activities/agent-turn/run-credentials.ts +761 -0
- package/src/activities/agent-turn/run.ts +1530 -0
- package/src/activities/agent-turn/sandbox-establish.ts +1207 -0
- package/src/activities/agent-turn/sandbox-provision.ts +569 -0
- package/src/activities/agent-turn/sandbox-route.ts +358 -0
- package/src/activities/agent-turn/sandbox-runtime.ts +731 -0
- package/src/activities/agent-turn/stream-attempt.ts +1670 -0
- package/src/activities/agent-turn/tool-environment.ts +729 -0
- package/src/activities/agent-turn/tool-policy.ts +216 -0
- package/src/activities/agent-turn/tool-result-spill.ts +265 -0
- package/src/activities/agent-turn/turn-context.ts +281 -0
- package/src/activities/agent-turn/xai-capacity.ts +229 -0
- package/src/activities/agent-turn.ts +12 -12575
- package/src/activities/api-integrations.ts +3 -0
- package/src/activities/automations.ts +214 -0
- package/src/activities/codemode-dispatcher.ts +12 -1
- package/src/activities/common.ts +1 -5
- package/src/activities/connector-attachments.ts +168 -0
- package/src/activities/context-compaction.ts +18 -0
- package/src/activities/documents.ts +6 -4
- package/src/activities/environment.ts +151 -55
- package/src/activities/generated-images.ts +26 -11
- package/src/activities/git-credential-renewal.ts +13 -3
- package/src/activities/goals.ts +85 -19
- package/src/activities/google-drive-provider.ts +178 -0
- package/src/activities/google-drive-publication.ts +938 -0
- package/src/activities/image-generation-references.ts +11 -2
- package/src/activities/incident-telemetry-authority.ts +327 -0
- package/src/activities/incident-telemetry-preflight.ts +317 -0
- package/src/activities/knowledge-source-sync-driver.ts +23 -0
- package/src/activities/knowledge-source-sync.ts +556 -40
- package/src/activities/mcp-credentials.ts +191 -17
- package/src/activities/open-suffix-resume.ts +422 -0
- package/src/activities/packs.ts +12 -60
- package/src/activities/parent-wake.ts +64 -14
- package/src/activities/retained-screenshot-reaper.ts +6 -3
- package/src/activities/retained-screenshots.ts +15 -47
- package/src/activities/rig-verification.ts +1 -1
- package/src/activities/run-input.ts +128 -136
- package/src/activities/sandbox-images.ts +7 -166
- package/src/activities/sandbox-lease.ts +406 -15
- package/src/activities/scheduled-tasks.ts +2103 -411
- package/src/activities/session-state.ts +10 -2
- package/src/activities/types.ts +32 -2
- package/src/activities/video-media-validation.ts +3 -3
- package/src/activities/video-output-retention.ts +2 -57
- package/src/activities/xai-auth.ts +8 -0
- package/src/activities-control.ts +2 -0
- package/src/activities.ts +3 -0
- package/src/activity-services.ts +13 -4
- package/src/connection-provider-use.ts +36 -0
- package/src/editable-artifact-materializer-subprocess.ts +25 -14
- package/src/editable-artifact-materializer.ts +29 -18
- package/src/github-rest-mcp.ts +713 -0
- package/src/http.ts +16 -6
- package/src/index.ts +40 -5
- package/src/knowledge-source-sync-activity-policy.ts +2 -0
- package/src/model-context-contributions.ts +33 -10
- package/src/observability-metrics.ts +357 -0
- package/src/opensandbox-kubernetes-inventory.ts +292 -0
- package/src/personal-github-git-credentials.ts +331 -0
- package/src/pr-review-credentials.ts +163 -0
- package/src/sandbox-archive-storage.ts +159 -0
- package/src/sandbox-resume.ts +449 -63
- package/src/sandbox-routing.ts +314 -16
- package/src/scheduled-alert-occurrence.ts +176 -0
- package/src/workflows/activities.ts +18 -2
- package/src/workflows/automations.ts +11 -0
- package/src/workflows/session.ts +13 -3
- package/src/workflows.ts +1 -0
- package/dist/activities-control-SMKF2OBE.js.map +0 -1
- package/dist/activities-turn-IDS3WEHA.js +0 -16532
- package/dist/activities-turn-IDS3WEHA.js.map +0 -1
- package/dist/chunk-N5TJIFCE.js.map +0 -1
- package/dist/chunk-P6Z2SLAA.js +0 -3092
- package/dist/chunk-P6Z2SLAA.js.map +0 -1
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import type { Settings } from "@opengeni/config";
|
|
2
|
-
import type { ConnectionCredentialsPort
|
|
2
|
+
import type { ConnectionCredentialsPort } from "@opengeni/contracts";
|
|
3
3
|
import {
|
|
4
4
|
buildConnectionTokenResolver,
|
|
5
5
|
buildHostConnectionTokenResolver,
|
|
6
|
+
resolveAcceptedConnectionUse,
|
|
7
|
+
sessionTenancyProductActivated,
|
|
6
8
|
type Database,
|
|
7
9
|
type ResolveConnectionCredentialInput,
|
|
8
10
|
type ResolveConnectionCredentialResult,
|
|
11
|
+
type SessionTurnForExecution,
|
|
9
12
|
} from "@opengeni/db";
|
|
13
|
+
import { recordTenancyCompatibilityLaneUse, type Observability } from "@opengeni/observability";
|
|
10
14
|
|
|
11
15
|
export function connectionTokenResolverForTurn(input: {
|
|
12
16
|
db: Database;
|
|
@@ -17,23 +21,193 @@ export function connectionTokenResolverForTurn(input: {
|
|
|
17
21
|
sessionId: string;
|
|
18
22
|
rootSessionId: string;
|
|
19
23
|
attemptId: string;
|
|
20
|
-
turn:
|
|
24
|
+
turn: SessionTurnForExecution;
|
|
25
|
+
authorizeAcceptedUse?: typeof resolveAcceptedConnectionUse;
|
|
26
|
+
/** Test seam for the activation fence on pre-snapshot workspace refs. */
|
|
27
|
+
isSessionTenancyProductActivated?: typeof sessionTenancyProductActivated;
|
|
28
|
+
/** Optional; used only for content-free compatibility-lane counters. */
|
|
29
|
+
observability?: Observability | null | undefined;
|
|
21
30
|
}): (request: ResolveConnectionCredentialInput) => Promise<ResolveConnectionCredentialResult> {
|
|
22
31
|
const hostResolver = input.connectionCredentials?.mcpCredentials;
|
|
23
|
-
|
|
24
|
-
|
|
32
|
+
const baseResolver = hostResolver
|
|
33
|
+
? buildHostConnectionTokenResolver(hostResolver, {
|
|
34
|
+
accountId: input.accountId,
|
|
35
|
+
workspaceId: input.workspaceId,
|
|
36
|
+
sessionId: input.sessionId,
|
|
37
|
+
rootSessionId: input.rootSessionId,
|
|
38
|
+
turnId: input.turn.id,
|
|
39
|
+
attemptId: input.attemptId,
|
|
40
|
+
executionGeneration: input.turn.executionGeneration,
|
|
41
|
+
initiator: input.turn.initiator,
|
|
42
|
+
initiatorContext: input.turn.initiatorContext,
|
|
43
|
+
surface: "model",
|
|
44
|
+
})
|
|
45
|
+
: buildConnectionTokenResolver(input.db, input.settings);
|
|
46
|
+
return async (request) => {
|
|
47
|
+
const acceptedDelegation = input.turn.personalConnectionDelegations.find(
|
|
48
|
+
(delegation) =>
|
|
49
|
+
(delegation.connectionType === undefined ||
|
|
50
|
+
delegation.connectionType === "mcp" ||
|
|
51
|
+
(delegation.connectionType === "github_personal" &&
|
|
52
|
+
delegation.serverId === "github:personal" &&
|
|
53
|
+
request.serverId === "github:personal" &&
|
|
54
|
+
request.connectionRef.provider === "github" &&
|
|
55
|
+
request.connectionRef.kind === "oauth2" &&
|
|
56
|
+
request.connectionRef.providerDomain === "github.com" &&
|
|
57
|
+
request.credentialTarget === "http_api" &&
|
|
58
|
+
isGitHubApiDestination(request.destinationUrl))) &&
|
|
59
|
+
delegation.serverId === request.serverId &&
|
|
60
|
+
delegation.connectionId === request.connectionRef.connectionId,
|
|
61
|
+
);
|
|
62
|
+
const subjectScope: "subject" | "workspace" =
|
|
63
|
+
request.connectionRef.subjectScope === "subject" ? "subject" : "workspace";
|
|
64
|
+
// Every subject-scoped request must match an exact connection frozen on the
|
|
65
|
+
// accepted turn. This also hard-fences pre-cutover common-user turns that
|
|
66
|
+
// lack a userDelegation: the DB resolver denies those rows because only a
|
|
67
|
+
// true legacy_user connection is eligible for bounded compatibility.
|
|
68
|
+
if (
|
|
69
|
+
subjectScope === "subject" &&
|
|
70
|
+
(!acceptedDelegation || !request.connectionRef.connectionId)
|
|
71
|
+
) {
|
|
72
|
+
return {
|
|
73
|
+
status: "auth_needed",
|
|
74
|
+
reason: "personal_authority_unavailable",
|
|
75
|
+
providerDomain: request.connectionRef.providerDomain,
|
|
76
|
+
...(request.connectionRef.provider ? { provider: request.connectionRef.provider } : {}),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
// Workspace-scope requests now run through the same accepted-use authority
|
|
80
|
+
// (migration 0279): the exact workspace-owned connection is revalidated
|
|
81
|
+
// inside the canonical lifecycle fences and every use leaves an idempotent
|
|
82
|
+
// audit fact. A ref with no connection id is the bounded pre-snapshot
|
|
83
|
+
// legacy path - it cannot be authorized by exact identity, so it keeps the
|
|
84
|
+
// unprivileged resolution the old short-circuit used.
|
|
85
|
+
if (subjectScope === "workspace" && !request.connectionRef.connectionId) {
|
|
86
|
+
if (
|
|
87
|
+
await (input.isSessionTenancyProductActivated ?? sessionTenancyProductActivated)(
|
|
88
|
+
input.db,
|
|
89
|
+
input.workspaceId,
|
|
90
|
+
)
|
|
91
|
+
) {
|
|
92
|
+
return {
|
|
93
|
+
status: "auth_needed",
|
|
94
|
+
reason: "missing_connection",
|
|
95
|
+
providerDomain: request.connectionRef.providerDomain,
|
|
96
|
+
...(request.connectionRef.provider ? { provider: request.connectionRef.provider } : {}),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
// This lane writes no `connection_use_audit_facts` row, so this counter is
|
|
100
|
+
// the only evidence it was taken. Lane name only - never the server,
|
|
101
|
+
// provider domain, connection, or subject.
|
|
102
|
+
recordTenancyCompatibilityLaneUse(input.observability, "connection_pre_snapshot_ref");
|
|
103
|
+
return await baseResolver(request);
|
|
104
|
+
}
|
|
105
|
+
const credentialUseContext = {
|
|
106
|
+
accountId: input.accountId,
|
|
107
|
+
workspaceId: input.workspaceId,
|
|
108
|
+
sessionId: input.sessionId,
|
|
109
|
+
turnId: input.turn.id,
|
|
110
|
+
attemptId: input.attemptId,
|
|
111
|
+
executionGeneration: input.turn.executionGeneration,
|
|
112
|
+
physicalRequestId: crypto.randomUUID(),
|
|
113
|
+
usePhase: "credential_resolution" as const,
|
|
114
|
+
};
|
|
115
|
+
const authorityBinding = {
|
|
116
|
+
serverId: request.serverId,
|
|
117
|
+
// Both lane guards above require an exact connection id at this point.
|
|
118
|
+
...(request.connectionRef.connectionId
|
|
119
|
+
? { connectionId: request.connectionRef.connectionId }
|
|
120
|
+
: {}),
|
|
121
|
+
providerDomain: request.connectionRef.providerDomain,
|
|
122
|
+
...(request.connectionRef.kind ? { connectionKind: request.connectionRef.kind } : {}),
|
|
123
|
+
subjectScope,
|
|
124
|
+
...(subjectScope === "subject" && request.subjectId
|
|
125
|
+
? { ownerSubjectId: request.subjectId }
|
|
126
|
+
: {}),
|
|
127
|
+
};
|
|
128
|
+
const authorize = async (
|
|
129
|
+
context: Omit<typeof credentialUseContext, "usePhase"> & {
|
|
130
|
+
usePhase: "credential_resolution" | "provider_request";
|
|
131
|
+
},
|
|
132
|
+
) =>
|
|
133
|
+
await (input.authorizeAcceptedUse ?? resolveAcceptedConnectionUse)(input.db, {
|
|
134
|
+
...context,
|
|
135
|
+
...authorityBinding,
|
|
136
|
+
});
|
|
137
|
+
const withProviderRequestAuthorization = (
|
|
138
|
+
result: ResolveConnectionCredentialResult,
|
|
139
|
+
): ResolveConnectionCredentialResult => {
|
|
140
|
+
if (result.status !== "ok") return result;
|
|
141
|
+
return {
|
|
142
|
+
...result,
|
|
143
|
+
authorizeProviderRequest: async () => {
|
|
144
|
+
const authorization = await authorize({
|
|
145
|
+
...credentialUseContext,
|
|
146
|
+
physicalRequestId: crypto.randomUUID(),
|
|
147
|
+
usePhase: "provider_request",
|
|
148
|
+
});
|
|
149
|
+
return authorization.status === "authorized";
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
// One place records the `legacy_user` lane for both resolution paths: the
|
|
154
|
+
// scope comes back on the resolver result when the DB resolver authorized
|
|
155
|
+
// internally, and on the resolution itself when the host resolver did.
|
|
156
|
+
const recordAuthorizedScope = (scope: "workspace" | "user" | "legacy_user" | undefined) => {
|
|
157
|
+
if (scope === "legacy_user") {
|
|
158
|
+
recordTenancyCompatibilityLaneUse(input.observability, "connection_legacy_user");
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
if (!hostResolver) {
|
|
162
|
+
const result = await baseResolver({
|
|
163
|
+
...request,
|
|
164
|
+
connectionUseContext: credentialUseContext,
|
|
165
|
+
});
|
|
166
|
+
if (result.status === "ok") recordAuthorizedScope(result.connectionUseAttribution?.scope);
|
|
167
|
+
return withProviderRequestAuthorization(result);
|
|
168
|
+
}
|
|
169
|
+
const authorization = await authorize(credentialUseContext);
|
|
170
|
+
if (authorization.status === "denied") {
|
|
171
|
+
return {
|
|
172
|
+
status: "auth_needed",
|
|
173
|
+
reason:
|
|
174
|
+
request.connectionRef.subjectScope === "subject"
|
|
175
|
+
? "personal_authority_unavailable"
|
|
176
|
+
: "missing_connection",
|
|
177
|
+
providerDomain: request.connectionRef.providerDomain,
|
|
178
|
+
...(request.connectionRef.provider ? { provider: request.connectionRef.provider } : {}),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
recordAuthorizedScope(authorization.attribution.scope);
|
|
182
|
+
const result = await baseResolver({
|
|
183
|
+
...request,
|
|
184
|
+
connectionUseContext: credentialUseContext,
|
|
185
|
+
connectionUseAuthority: authorization.attribution,
|
|
186
|
+
connectionRef: {
|
|
187
|
+
...request.connectionRef,
|
|
188
|
+
connectionId: authorization.attribution.connectionId,
|
|
189
|
+
kind: authorization.connectionKind,
|
|
190
|
+
subjectScope: authorization.attribution.scope === "workspace" ? "workspace" : "subject",
|
|
191
|
+
},
|
|
192
|
+
...(authorization.attribution.ownerSubjectId
|
|
193
|
+
? { subjectId: authorization.attribution.ownerSubjectId }
|
|
194
|
+
: {}),
|
|
195
|
+
});
|
|
196
|
+
return withProviderRequestAuthorization(result);
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function isGitHubApiDestination(value: string): boolean {
|
|
201
|
+
try {
|
|
202
|
+
const url = new URL(value);
|
|
203
|
+
return (
|
|
204
|
+
url.protocol === "https:" &&
|
|
205
|
+
url.origin === "https://api.github.com" &&
|
|
206
|
+
url.username === "" &&
|
|
207
|
+
url.password === "" &&
|
|
208
|
+
url.hash === ""
|
|
209
|
+
);
|
|
210
|
+
} catch {
|
|
211
|
+
return false;
|
|
25
212
|
}
|
|
26
|
-
return buildHostConnectionTokenResolver(hostResolver, {
|
|
27
|
-
accountId: input.accountId,
|
|
28
|
-
workspaceId: input.workspaceId,
|
|
29
|
-
sessionId: input.sessionId,
|
|
30
|
-
rootSessionId: input.rootSessionId,
|
|
31
|
-
turnId: input.turn.id,
|
|
32
|
-
attemptId: input.attemptId,
|
|
33
|
-
executionGeneration: input.turn.executionGeneration,
|
|
34
|
-
initiator: input.turn.initiator,
|
|
35
|
-
initiatorContext: input.turn.initiatorContext,
|
|
36
|
-
surface: "model",
|
|
37
|
-
allowOfficialGmailRestDestination: input.settings.gmailRestAdapterEnabled,
|
|
38
|
-
});
|
|
39
213
|
}
|
|
@@ -0,0 +1,422 @@
|
|
|
1
|
+
import type { Agent } from "@openai/agents";
|
|
2
|
+
import {
|
|
3
|
+
OPEN_SUFFIX_RUN_STATE_BLOB,
|
|
4
|
+
approvalIdentifier,
|
|
5
|
+
type HumanInputResponse,
|
|
6
|
+
type SessionEvent,
|
|
7
|
+
} from "@opengeni/contracts";
|
|
8
|
+
import {
|
|
9
|
+
appendSessionHistoryItems,
|
|
10
|
+
getActiveSessionHistoryItems,
|
|
11
|
+
interruptedToolCallResult,
|
|
12
|
+
listTurnOpenSuffixToolCalls,
|
|
13
|
+
nextSessionHistoryPosition,
|
|
14
|
+
recordPendingSessionToolCallResult,
|
|
15
|
+
type Database,
|
|
16
|
+
type OpenSuffixPendingToolCall,
|
|
17
|
+
} from "@opengeni/db";
|
|
18
|
+
import {
|
|
19
|
+
functionCallResultItem,
|
|
20
|
+
invokePreparedAgentTool,
|
|
21
|
+
sanitizeHistoryItemsForModel,
|
|
22
|
+
} from "@opengeni/runtime";
|
|
23
|
+
|
|
24
|
+
export type OpenSuffixResumeOutcome =
|
|
25
|
+
| { action: "continue" }
|
|
26
|
+
| { action: "requires_action" }
|
|
27
|
+
| { action: "cancelled" };
|
|
28
|
+
|
|
29
|
+
type OpenSuffixSettle = (input: {
|
|
30
|
+
events: Array<
|
|
31
|
+
| { type: "session.requiresAction"; payload: { approvals: unknown[] } }
|
|
32
|
+
| { type: "session.status.changed"; payload: { status: "requires_action" } }
|
|
33
|
+
>;
|
|
34
|
+
turnStatus: "requires_action";
|
|
35
|
+
sessionStatus: "requires_action";
|
|
36
|
+
activeTurnId: string;
|
|
37
|
+
runState: {
|
|
38
|
+
serializedRunState: string;
|
|
39
|
+
pendingApprovals: unknown[];
|
|
40
|
+
};
|
|
41
|
+
}) => Promise<boolean>;
|
|
42
|
+
|
|
43
|
+
function argumentsJsonFromCallItem(callItem: Record<string, unknown>): string {
|
|
44
|
+
const raw = callItem.arguments;
|
|
45
|
+
if (typeof raw === "string") {
|
|
46
|
+
return raw;
|
|
47
|
+
}
|
|
48
|
+
return JSON.stringify(raw ?? {});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function toolNameFromCallItem(callItem: Record<string, unknown>): string {
|
|
52
|
+
return typeof callItem.name === "string" && callItem.name.length > 0 ? callItem.name : "tool";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function pendingApprovalProjection(row: OpenSuffixPendingToolCall): unknown {
|
|
56
|
+
return {
|
|
57
|
+
id: row.callId,
|
|
58
|
+
name: toolNameFromCallItem(row.callItem),
|
|
59
|
+
arguments: row.callItem.arguments ?? null,
|
|
60
|
+
raw: row.callItem,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function remainingSuffixApprovals(
|
|
65
|
+
rows: readonly OpenSuffixPendingToolCall[],
|
|
66
|
+
kinds: ReadonlySet<OpenSuffixPendingToolCall["interruptionKind"]>,
|
|
67
|
+
): unknown[] {
|
|
68
|
+
return rows
|
|
69
|
+
.filter((row) => row.resultItem == null && kinds.has(row.interruptionKind))
|
|
70
|
+
.map(pendingApprovalProjection);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Matches the original pause `session.requiresAction` payload: ordinary approvals only. */
|
|
74
|
+
export function remainingPendingApprovalsFromSuffix(
|
|
75
|
+
rows: readonly OpenSuffixPendingToolCall[],
|
|
76
|
+
): unknown[] {
|
|
77
|
+
return remainingSuffixApprovals(rows, new Set(["approval"]));
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Leftover `pendingApprovals` snapshot includes interaction interventions. */
|
|
81
|
+
export function remainingRunStatePendingApprovalsFromSuffix(
|
|
82
|
+
rows: readonly OpenSuffixPendingToolCall[],
|
|
83
|
+
): unknown[] {
|
|
84
|
+
return remainingSuffixApprovals(rows, new Set(["approval", "interaction_intervention"]));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function openSuffixHistoryItems(
|
|
88
|
+
row: OpenSuffixPendingToolCall,
|
|
89
|
+
resultItem: Record<string, unknown>,
|
|
90
|
+
): Record<string, unknown>[] {
|
|
91
|
+
return sanitizeHistoryItemsForModel([
|
|
92
|
+
...row.tiedReasoningItems,
|
|
93
|
+
row.callItem,
|
|
94
|
+
resultItem,
|
|
95
|
+
]) as Record<string, unknown>[];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function matchingOpenSuffixCallId(input: {
|
|
99
|
+
trigger: Pick<SessionEvent, "type" | "payload">;
|
|
100
|
+
humanInputToolCallId?: string | undefined;
|
|
101
|
+
}): string | null {
|
|
102
|
+
if (input.trigger.type === "user.humanInputResponse") {
|
|
103
|
+
return input.humanInputToolCallId ?? null;
|
|
104
|
+
}
|
|
105
|
+
if (input.trigger.type === "user.approvalDecision") {
|
|
106
|
+
const payload = input.trigger.payload as { approvalId?: unknown };
|
|
107
|
+
return typeof payload.approvalId === "string" ? payload.approvalId : null;
|
|
108
|
+
}
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function historyItemCallId(item: Record<string, unknown>): string | null {
|
|
113
|
+
const value = item.callId ?? item.call_id ?? item.id;
|
|
114
|
+
return typeof value === "string" && value.length > 0 ? value : null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function resolveOpenSuffixResumeTarget(
|
|
118
|
+
rows: readonly OpenSuffixPendingToolCall[],
|
|
119
|
+
callId: string | null,
|
|
120
|
+
): OpenSuffixPendingToolCall | null {
|
|
121
|
+
if (!callId) {
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
return rows.find((row) => row.callId === callId) ?? null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** True when the paired call and result already exist in durable history. */
|
|
128
|
+
export function openSuffixPairPresentInHistory(
|
|
129
|
+
items: readonly Record<string, unknown>[],
|
|
130
|
+
callId: string,
|
|
131
|
+
): boolean {
|
|
132
|
+
let sawCall = false;
|
|
133
|
+
for (const item of items) {
|
|
134
|
+
if (historyItemCallId(item) !== callId) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
const type = typeof item.type === "string" ? item.type : null;
|
|
138
|
+
if (type === "function_call" || type === "computer_call") {
|
|
139
|
+
sawCall = true;
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (sawCall && (type === "function_call_result" || type === "computer_call_result")) {
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function memberResult(input: {
|
|
150
|
+
callId: string;
|
|
151
|
+
name: string;
|
|
152
|
+
output: unknown;
|
|
153
|
+
resultItem?: Record<string, unknown>;
|
|
154
|
+
}): { resultItem: Record<string, unknown>; eventOutput: unknown } {
|
|
155
|
+
return {
|
|
156
|
+
resultItem:
|
|
157
|
+
input.resultItem ??
|
|
158
|
+
functionCallResultItem({
|
|
159
|
+
callId: input.callId,
|
|
160
|
+
name: input.name,
|
|
161
|
+
output: input.output,
|
|
162
|
+
}),
|
|
163
|
+
eventOutput: input.output,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async function resultItemForOpenSuffixMember(input: {
|
|
168
|
+
agent: Agent<any, any>;
|
|
169
|
+
row: OpenSuffixPendingToolCall;
|
|
170
|
+
trigger: Pick<SessionEvent, "type" | "payload">;
|
|
171
|
+
humanInputResume: {
|
|
172
|
+
requestId: string;
|
|
173
|
+
toolCallId: string;
|
|
174
|
+
response: HumanInputResponse;
|
|
175
|
+
} | null;
|
|
176
|
+
}): Promise<{ resultItem: Record<string, unknown>; eventOutput: unknown }> {
|
|
177
|
+
const name = toolNameFromCallItem(input.row.callItem);
|
|
178
|
+
const rejected = (): { resultItem: Record<string, unknown>; eventOutput: unknown } => {
|
|
179
|
+
const payload = input.trigger.payload as { message?: unknown; decision?: unknown };
|
|
180
|
+
const message =
|
|
181
|
+
typeof payload.message === "string" && payload.message.trim().length > 0
|
|
182
|
+
? payload.message
|
|
183
|
+
: "Tool approval was rejected.";
|
|
184
|
+
const interrupted = interruptedToolCallResult({
|
|
185
|
+
callType: input.row.callType,
|
|
186
|
+
callId: input.row.callId,
|
|
187
|
+
callItem: input.row.callItem,
|
|
188
|
+
reason: message,
|
|
189
|
+
});
|
|
190
|
+
return memberResult({
|
|
191
|
+
callId: input.row.callId,
|
|
192
|
+
name,
|
|
193
|
+
output: message,
|
|
194
|
+
...(interrupted ? { resultItem: interrupted } : {}),
|
|
195
|
+
});
|
|
196
|
+
};
|
|
197
|
+
if (input.row.interruptionKind === "human_input") {
|
|
198
|
+
if (!input.humanInputResume || input.humanInputResume.toolCallId !== input.row.callId) {
|
|
199
|
+
throw new Error("Human-input open suffix is missing its durable response");
|
|
200
|
+
}
|
|
201
|
+
const output = JSON.stringify({
|
|
202
|
+
requestId: input.humanInputResume.requestId,
|
|
203
|
+
...input.humanInputResume.response,
|
|
204
|
+
});
|
|
205
|
+
return memberResult({
|
|
206
|
+
callId: input.row.callId,
|
|
207
|
+
name,
|
|
208
|
+
output,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const payload = input.trigger.payload as { decision?: unknown };
|
|
212
|
+
if (payload.decision !== "approve") {
|
|
213
|
+
return rejected();
|
|
214
|
+
}
|
|
215
|
+
try {
|
|
216
|
+
const output = await invokePreparedAgentTool({
|
|
217
|
+
agent: input.agent,
|
|
218
|
+
toolName: name,
|
|
219
|
+
argumentsJson: argumentsJsonFromCallItem(input.row.callItem),
|
|
220
|
+
callId: input.row.callId,
|
|
221
|
+
});
|
|
222
|
+
return memberResult({
|
|
223
|
+
callId: input.row.callId,
|
|
224
|
+
name,
|
|
225
|
+
output,
|
|
226
|
+
});
|
|
227
|
+
} catch (error) {
|
|
228
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
229
|
+
return memberResult({
|
|
230
|
+
callId: input.row.callId,
|
|
231
|
+
name,
|
|
232
|
+
output: message,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export async function settleOpenSuffixResumeIfNeeded(input: {
|
|
238
|
+
db: Database;
|
|
239
|
+
agent: Agent<any, any>;
|
|
240
|
+
accountId: string;
|
|
241
|
+
workspaceId: string;
|
|
242
|
+
sessionId: string;
|
|
243
|
+
turnId: string;
|
|
244
|
+
executionGeneration: number;
|
|
245
|
+
attemptId: string;
|
|
246
|
+
trigger: Pick<SessionEvent, "type" | "payload">;
|
|
247
|
+
humanInputResume: {
|
|
248
|
+
requestId: string;
|
|
249
|
+
toolCallId: string;
|
|
250
|
+
response: HumanInputResponse;
|
|
251
|
+
} | null;
|
|
252
|
+
modelToolOutputTruncationTokens?: number;
|
|
253
|
+
settle: OpenSuffixSettle;
|
|
254
|
+
publish?: (
|
|
255
|
+
events: Array<{ type: "agent.toolCall.output"; payload: { id: string; output: unknown } }>,
|
|
256
|
+
) => Promise<unknown>;
|
|
257
|
+
}): Promise<OpenSuffixResumeOutcome> {
|
|
258
|
+
if (
|
|
259
|
+
input.trigger.type !== "user.approvalDecision" &&
|
|
260
|
+
input.trigger.type !== "user.humanInputResponse"
|
|
261
|
+
) {
|
|
262
|
+
return { action: "continue" };
|
|
263
|
+
}
|
|
264
|
+
const rows = await listTurnOpenSuffixToolCalls(
|
|
265
|
+
input.db,
|
|
266
|
+
input.workspaceId,
|
|
267
|
+
input.sessionId,
|
|
268
|
+
input.turnId,
|
|
269
|
+
);
|
|
270
|
+
if (rows.length === 0) {
|
|
271
|
+
return { action: "continue" };
|
|
272
|
+
}
|
|
273
|
+
const callId = matchingOpenSuffixCallId({
|
|
274
|
+
trigger: input.trigger,
|
|
275
|
+
humanInputToolCallId: input.humanInputResume?.toolCallId,
|
|
276
|
+
});
|
|
277
|
+
const target = resolveOpenSuffixResumeTarget(rows, callId);
|
|
278
|
+
if (!target || !callId) {
|
|
279
|
+
throw new Error("Open suffix resume event does not match an interruption");
|
|
280
|
+
}
|
|
281
|
+
const truncationTokens =
|
|
282
|
+
input.modelToolOutputTruncationTokens ?? target.modelToolOutputTruncationTokens ?? undefined;
|
|
283
|
+
let resultItem = target.resultItem;
|
|
284
|
+
let eventOutput: unknown = resultItem?.output;
|
|
285
|
+
if (!resultItem) {
|
|
286
|
+
const member = await resultItemForOpenSuffixMember({
|
|
287
|
+
agent: input.agent,
|
|
288
|
+
row: target,
|
|
289
|
+
trigger: input.trigger,
|
|
290
|
+
humanInputResume: input.humanInputResume,
|
|
291
|
+
});
|
|
292
|
+
resultItem = member.resultItem;
|
|
293
|
+
eventOutput = member.eventOutput;
|
|
294
|
+
const recorded = await recordPendingSessionToolCallResult(input.db, {
|
|
295
|
+
accountId: input.accountId,
|
|
296
|
+
workspaceId: input.workspaceId,
|
|
297
|
+
sessionId: input.sessionId,
|
|
298
|
+
turnId: input.turnId,
|
|
299
|
+
executionGeneration: input.executionGeneration,
|
|
300
|
+
attemptId: input.attemptId,
|
|
301
|
+
callId: target.callId,
|
|
302
|
+
...(truncationTokens !== undefined
|
|
303
|
+
? { modelToolOutputTruncationTokens: truncationTokens }
|
|
304
|
+
: {}),
|
|
305
|
+
resultItem,
|
|
306
|
+
eventOutput,
|
|
307
|
+
});
|
|
308
|
+
if (!recorded.accepted) {
|
|
309
|
+
return { action: "cancelled" };
|
|
310
|
+
}
|
|
311
|
+
if (!recorded.recorded) {
|
|
312
|
+
const latest = resolveOpenSuffixResumeTarget(
|
|
313
|
+
await listTurnOpenSuffixToolCalls(
|
|
314
|
+
input.db,
|
|
315
|
+
input.workspaceId,
|
|
316
|
+
input.sessionId,
|
|
317
|
+
input.turnId,
|
|
318
|
+
),
|
|
319
|
+
callId,
|
|
320
|
+
);
|
|
321
|
+
resultItem = latest?.resultItem ?? resultItem;
|
|
322
|
+
} else if (input.publish) {
|
|
323
|
+
await input.publish([
|
|
324
|
+
{
|
|
325
|
+
type: "agent.toolCall.output",
|
|
326
|
+
payload: { id: target.callId, output: eventOutput },
|
|
327
|
+
},
|
|
328
|
+
]);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
if (!resultItem) {
|
|
332
|
+
throw new Error(`Open suffix resume for ${target.callId} has no result item`);
|
|
333
|
+
}
|
|
334
|
+
const history = await getActiveSessionHistoryItems(input.db, input.workspaceId, input.sessionId);
|
|
335
|
+
if (
|
|
336
|
+
!openSuffixPairPresentInHistory(
|
|
337
|
+
history.map((row) => row.item),
|
|
338
|
+
callId,
|
|
339
|
+
)
|
|
340
|
+
) {
|
|
341
|
+
const historyItems = openSuffixHistoryItems(target, resultItem);
|
|
342
|
+
if (historyItems.length === 0) {
|
|
343
|
+
throw new Error(`Open suffix resume for ${target.callId} produced no paired history`);
|
|
344
|
+
}
|
|
345
|
+
const nextPosition = await nextSessionHistoryPosition(
|
|
346
|
+
input.db,
|
|
347
|
+
input.workspaceId,
|
|
348
|
+
input.sessionId,
|
|
349
|
+
);
|
|
350
|
+
const appended = await appendSessionHistoryItems(input.db, {
|
|
351
|
+
accountId: input.accountId,
|
|
352
|
+
workspaceId: input.workspaceId,
|
|
353
|
+
sessionId: input.sessionId,
|
|
354
|
+
turnId: input.turnId,
|
|
355
|
+
expectedExecutionGeneration: input.executionGeneration,
|
|
356
|
+
expectedAttemptId: input.attemptId,
|
|
357
|
+
...(truncationTokens !== undefined
|
|
358
|
+
? { modelToolOutputTruncationTokens: truncationTokens }
|
|
359
|
+
: {}),
|
|
360
|
+
items: historyItems.map((item, offset) => ({
|
|
361
|
+
position: nextPosition + offset,
|
|
362
|
+
item,
|
|
363
|
+
})),
|
|
364
|
+
});
|
|
365
|
+
if (!appended) {
|
|
366
|
+
return { action: "cancelled" };
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
const remaining = (
|
|
370
|
+
await listTurnOpenSuffixToolCalls(input.db, input.workspaceId, input.sessionId, input.turnId)
|
|
371
|
+
).filter((row) => row.resultItem == null);
|
|
372
|
+
if (remaining.length === 0) {
|
|
373
|
+
return { action: "continue" };
|
|
374
|
+
}
|
|
375
|
+
const requiresActionApprovals = remainingPendingApprovalsFromSuffix(remaining);
|
|
376
|
+
const pendingApprovals = remainingRunStatePendingApprovalsFromSuffix(remaining);
|
|
377
|
+
const settled = await input.settle({
|
|
378
|
+
events: [
|
|
379
|
+
...(requiresActionApprovals.length > 0
|
|
380
|
+
? [
|
|
381
|
+
{
|
|
382
|
+
type: "session.requiresAction" as const,
|
|
383
|
+
payload: { approvals: requiresActionApprovals },
|
|
384
|
+
},
|
|
385
|
+
]
|
|
386
|
+
: []),
|
|
387
|
+
{
|
|
388
|
+
type: "session.status.changed" as const,
|
|
389
|
+
payload: { status: "requires_action" as const },
|
|
390
|
+
},
|
|
391
|
+
],
|
|
392
|
+
turnStatus: "requires_action",
|
|
393
|
+
sessionStatus: "requires_action",
|
|
394
|
+
activeTurnId: input.turnId,
|
|
395
|
+
runState: {
|
|
396
|
+
serializedRunState: OPEN_SUFFIX_RUN_STATE_BLOB,
|
|
397
|
+
pendingApprovals,
|
|
398
|
+
},
|
|
399
|
+
});
|
|
400
|
+
return settled ? { action: "requires_action" } : { action: "cancelled" };
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
export function interruptionCallIdsFromPause(input: {
|
|
404
|
+
humanInputRequests: Array<{ toolCallId: string }>;
|
|
405
|
+
interactionInterventionRequests: Array<{ toolCallId: string }>;
|
|
406
|
+
pendingApprovals: unknown[];
|
|
407
|
+
}): string[] {
|
|
408
|
+
const ids = new Set<string>();
|
|
409
|
+
for (const request of input.humanInputRequests) {
|
|
410
|
+
ids.add(request.toolCallId);
|
|
411
|
+
}
|
|
412
|
+
for (const request of input.interactionInterventionRequests) {
|
|
413
|
+
ids.add(request.toolCallId);
|
|
414
|
+
}
|
|
415
|
+
for (const approval of input.pendingApprovals) {
|
|
416
|
+
const id = approvalIdentifier(approval);
|
|
417
|
+
if (id) {
|
|
418
|
+
ids.add(id);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return [...ids];
|
|
422
|
+
}
|