@opengeni/worker-bundle 0.16.29 → 0.20.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-run-admission.d.ts +12 -0
- package/dist/activities/agent-turn.d.ts +110 -19
- package/dist/activities/api-integrations.d.ts +21 -0
- package/dist/activities/browser-state-artifact-reaper.d.ts +29 -0
- package/dist/activities/capabilities.d.ts +6 -0
- package/dist/activities/codemode-dispatcher.d.ts +35 -0
- package/dist/activities/codemode-token-renewal.d.ts +23 -0
- package/dist/activities/codex-capacity.d.ts +6 -5
- package/dist/activities/codex-image-generation.d.ts +21 -0
- package/dist/activities/context-compaction.d.ts +1 -0
- package/dist/activities/documents.d.ts +3 -2
- package/dist/activities/environment.d.ts +17 -8
- package/dist/activities/expiring-material-renewal.d.ts +1 -1
- package/dist/activities/file-upload-reaper.d.ts +7 -2
- package/dist/activities/gateway-image-generation.d.ts +39 -0
- package/dist/activities/gateway-video-generation.d.ts +53 -0
- package/dist/activities/generated-images.d.ts +112 -0
- package/dist/activities/git-credential-renewal.d.ts +1 -1
- package/dist/activities/goals.d.ts +2 -2
- package/dist/activities/google-drive-changes.d.ts +99 -0
- package/dist/activities/image-generation-operation.d.ts +74 -0
- package/dist/activities/image-generation-references.d.ts +25 -0
- package/dist/activities/knowledge-source-sync-driver.d.ts +23 -0
- package/dist/activities/knowledge-source-sync.d.ts +75 -0
- package/dist/activities/packs.d.ts +21 -36
- package/dist/activities/parent-wake.d.ts +1 -1
- package/dist/activities/retained-screenshot-reaper.d.ts +2 -2
- package/dist/activities/retained-screenshots.d.ts +36 -0
- package/dist/activities/rig-verification.d.ts +82 -5
- package/dist/activities/run-credential-renewal.d.ts +1 -1
- package/dist/activities/run-input.d.ts +3 -1
- package/dist/activities/sandbox-images.d.ts +14 -0
- package/dist/activities/sandbox-lease.d.ts +47 -9
- package/dist/activities/scheduled-tasks.d.ts +2 -2
- package/dist/activities/session-state.d.ts +7 -5
- package/dist/activities/site-auth-maintenance.d.ts +27 -0
- package/dist/activities/streaming.d.ts +1 -1
- package/dist/activities/types.d.ts +87 -5
- package/dist/activities/video-generation-admission.d.ts +43 -0
- package/dist/activities/video-generation-credential.d.ts +24 -0
- package/dist/activities/video-generation-reconciliation.d.ts +13 -0
- package/dist/activities/video-media-validation.d.ts +34 -0
- package/dist/activities/video-output-retention.d.ts +36 -0
- package/dist/activities/video-reference-staging.d.ts +55 -0
- package/dist/activities/workflow-wake.d.ts +2 -2
- package/dist/activities/workspace-capture.d.ts +10 -1
- package/dist/activities/xai-auth.d.ts +23 -0
- package/dist/activities/xai-image-generation.d.ts +26 -0
- package/dist/activities/xai-video-generation.d.ts +18 -0
- package/dist/activities-control-XURE4VKN.js +7221 -0
- package/dist/activities-control-XURE4VKN.js.map +1 -0
- package/dist/activities-control.d.ts +297 -0
- package/dist/activities-turn-6EBXGGLS.js +16036 -0
- package/dist/activities-turn-6EBXGGLS.js.map +1 -0
- package/dist/activities-turn.d.ts +17 -0
- package/dist/activities.d.ts +138 -150
- package/dist/activity-services.d.ts +10 -0
- package/dist/artifact-materializer-entry.d.ts +6 -0
- package/dist/artifact-materializer-entry.js +2662 -0
- package/dist/artifact-materializer-entry.js.map +1 -0
- package/dist/artifact-outbox-entry.d.ts +2 -0
- package/dist/artifact-outbox-entry.js +681 -0
- package/dist/artifact-outbox-entry.js.map +1 -0
- package/dist/chunk-5GRJIP5H.js +3092 -0
- package/dist/chunk-5GRJIP5H.js.map +1 -0
- package/dist/chunk-GRFQTTSQ.js +448 -0
- package/dist/chunk-GRFQTTSQ.js.map +1 -0
- package/dist/chunk-TYNPCG4U.js +906 -0
- package/dist/chunk-TYNPCG4U.js.map +1 -0
- package/dist/concurrency.d.ts +108 -5
- package/dist/editable-artifact-hint-broker.d.ts +49 -0
- package/dist/editable-artifact-materializer-launcher.d.ts +49 -0
- package/dist/editable-artifact-materializer-service.d.ts +108 -0
- package/dist/editable-artifact-materializer-storage.d.ts +12 -0
- package/dist/editable-artifact-materializer-subprocess.d.ts +50 -0
- package/dist/editable-artifact-materializer.d.ts +262 -0
- package/dist/editable-artifact-outbox-dispatcher.d.ts +44 -0
- package/dist/editable-artifact-outbox-service.d.ts +70 -0
- package/dist/host-export.d.ts +1 -1
- package/dist/index.d.ts +189 -10
- package/dist/index.js +715 -17380
- package/dist/index.js.map +1 -1
- package/dist/memory-pressure-guard.d.ts +57 -0
- package/dist/model-checkpoint-memory-collector.d.ts +23 -0
- package/dist/model-context-contributions.d.ts +40 -0
- package/dist/observability-metrics.d.ts +53 -1
- package/dist/sandbox-reaper-contract.d.ts +40 -0
- package/dist/sandbox-reaper-timeout.d.ts +14 -0
- package/dist/sandbox-resume.d.ts +34 -7
- package/dist/sandbox-routing.d.ts +6 -0
- package/dist/worker-service-lifecycle.d.ts +6 -2
- package/dist/workflow-bundle.js +1094 -370
- package/dist/workflows/activities.d.ts +8 -2
- package/dist/workflows/document-index.d.ts +1 -1
- package/dist/workflows/knowledge-source-sync.d.ts +13 -0
- package/dist/workflows/rig-verification.d.ts +1 -1
- package/dist/workflows/sandbox-reaper.d.ts +8 -0
- package/dist/workflows/scheduled-tasks.d.ts +1 -1
- package/dist/workflows/session.d.ts +2 -2
- package/dist/workflows/site-auth-maintenance.d.ts +2 -0
- package/dist/workflows/video-generation.d.ts +10 -0
- package/dist/workflows/workflow-wake.d.ts +1 -1
- package/dist/workflows.d.ts +10 -7
- package/package.json +35 -15
- package/src/activities/agent-run-admission.ts +76 -0
- package/src/activities/agent-turn.ts +2600 -340
- package/src/activities/api-integrations.ts +138 -0
- package/src/activities/browser-state-artifact-reaper.ts +120 -0
- package/src/activities/capabilities.ts +10 -0
- package/src/activities/codemode-dispatcher.ts +335 -0
- package/src/activities/codemode-token-renewal.ts +64 -0
- package/src/activities/codex-capacity.ts +54 -7
- package/src/activities/codex-image-generation.ts +56 -0
- package/src/activities/context-compaction.ts +38 -3
- package/src/activities/documents.ts +14 -4
- package/src/activities/environment.ts +54 -45
- package/src/activities/expiring-material-renewal.ts +1 -1
- package/src/activities/file-upload-reaper.ts +34 -3
- package/src/activities/gateway-image-generation.ts +146 -0
- package/src/activities/gateway-video-generation.ts +298 -0
- package/src/activities/generated-images.ts +860 -0
- package/src/activities/goals.ts +8 -3
- package/src/activities/google-drive-changes.ts +454 -0
- package/src/activities/image-generation-operation.ts +333 -0
- package/src/activities/image-generation-references.ts +118 -0
- package/src/activities/knowledge-source-sync-driver.ts +27 -0
- package/src/activities/knowledge-source-sync.ts +2840 -0
- package/src/activities/mcp-credentials.ts +1 -0
- package/src/activities/packs.ts +75 -152
- package/src/activities/parent-wake.ts +13 -1
- package/src/activities/retained-screenshot-reaper.ts +2 -2
- package/src/activities/retained-screenshots.ts +453 -28
- package/src/activities/rig-verification.ts +554 -43
- package/src/activities/run-input.ts +22 -6
- package/src/activities/sandbox-images.ts +167 -0
- package/src/activities/sandbox-lease.ts +1065 -483
- package/src/activities/scheduled-tasks.ts +104 -74
- package/src/activities/session-state.ts +22 -4
- package/src/activities/site-auth-maintenance.ts +269 -0
- package/src/activities/streaming.ts +42 -3
- package/src/activities/types.ts +104 -5
- package/src/activities/video-generation-admission.ts +303 -0
- package/src/activities/video-generation-credential.ts +112 -0
- package/src/activities/video-generation-reconciliation.ts +771 -0
- package/src/activities/video-media-validation.ts +332 -0
- package/src/activities/video-output-retention.ts +314 -0
- package/src/activities/video-reference-staging.ts +350 -0
- package/src/activities/workflow-wake.ts +2 -2
- package/src/activities/workspace-capture.ts +44 -10
- package/src/activities/xai-auth.ts +122 -0
- package/src/activities/xai-image-generation.ts +86 -0
- package/src/activities/xai-video-generation.ts +69 -0
- package/src/activities-control.ts +58 -0
- package/src/activities-turn.ts +40 -0
- package/src/activities.ts +53 -124
- package/src/activity-services.ts +62 -0
- package/src/artifact-materializer-entry.ts +35 -0
- package/src/artifact-outbox-entry.ts +29 -0
- package/src/concurrency.ts +484 -6
- package/src/editable-artifact-hint-broker.ts +235 -0
- package/src/editable-artifact-materializer-launcher.ts +515 -0
- package/src/editable-artifact-materializer-service.ts +787 -0
- package/src/editable-artifact-materializer-storage.ts +413 -0
- package/src/editable-artifact-materializer-subprocess.ts +956 -0
- package/src/editable-artifact-materializer.ts +1289 -0
- package/src/editable-artifact-outbox-dispatcher.ts +244 -0
- package/src/editable-artifact-outbox-service.ts +438 -0
- package/src/index.ts +328 -72
- package/src/memory-pressure-guard.ts +489 -0
- package/src/model-checkpoint-memory-collector.ts +123 -0
- package/src/model-context-contributions.ts +159 -0
- package/src/observability-metrics.ts +297 -13
- package/src/sandbox-reaper-contract.ts +73 -0
- package/src/sandbox-reaper-timeout.ts +79 -0
- package/src/sandbox-resume.ts +294 -153
- package/src/sandbox-routing.ts +32 -24
- package/src/worker-service-lifecycle.ts +80 -14
- package/src/workflows/activities.ts +31 -0
- package/src/workflows/file-upload-reaper.ts +9 -1
- package/src/workflows/knowledge-source-sync.ts +83 -0
- package/src/workflows/sandbox-reaper.ts +183 -21
- package/src/workflows/scheduled-tasks.ts +42 -6
- package/src/workflows/session.ts +20 -6
- package/src/workflows/site-auth-maintenance.ts +14 -0
- package/src/workflows/video-generation.ts +36 -0
- package/src/workflows.ts +18 -1
- package/dist/activities/toolspace-token-renewal.d.ts +0 -23
- package/src/activities/toolspace-token-renewal.ts +0 -64
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
fetchCodexUsageForAccount,
|
|
3
3
|
getCodexCapacityWaitForSession,
|
|
4
|
+
getXaiCapacityWaitForSession,
|
|
4
5
|
listCodexAccountStatuses,
|
|
5
6
|
listPendingCodexCapacityWakeTargets,
|
|
6
7
|
reconcileCodexCapacityWait as reconcileCodexCapacityWaitDb,
|
|
8
|
+
reconcileXaiCapacityWait as reconcileXaiCapacityWaitDb,
|
|
7
9
|
type CodexCapacityWakeTarget,
|
|
8
10
|
type CodexCapacitySelectionContext,
|
|
9
11
|
} from "@opengeni/db";
|
|
@@ -16,7 +18,7 @@ import {
|
|
|
16
18
|
selectCodexCredentialLeaseForTurn,
|
|
17
19
|
} from "./codex-rotation";
|
|
18
20
|
import type {
|
|
19
|
-
|
|
21
|
+
ControlActivityServices,
|
|
20
22
|
GetCodexCapacityWaitInput,
|
|
21
23
|
ReconcileCodexCapacityWaitInput,
|
|
22
24
|
ReconcileCodexCapacityWaitResult,
|
|
@@ -24,10 +26,10 @@ import type {
|
|
|
24
26
|
|
|
25
27
|
type CodexCapacitySignalServices = {
|
|
26
28
|
signalCodexCapacityWorkflow?:
|
|
27
|
-
| NonNullable<
|
|
29
|
+
| NonNullable<ControlActivityServices["signalCodexCapacityWorkflow"]>
|
|
28
30
|
| null
|
|
29
31
|
| undefined;
|
|
30
|
-
wakeSessionWorkflow:
|
|
32
|
+
wakeSessionWorkflow: ControlActivityServices["wakeSessionWorkflow"];
|
|
31
33
|
};
|
|
32
34
|
|
|
33
35
|
/**
|
|
@@ -74,7 +76,7 @@ export async function signalCodexCapacityWakeTargets(
|
|
|
74
76
|
|
|
75
77
|
/** Repair a commit/signal crash edge by redelivering every pending revision. */
|
|
76
78
|
export async function signalPendingCodexCapacityWakeTargets(
|
|
77
|
-
services: CodexCapacitySignalServices & { db:
|
|
79
|
+
services: CodexCapacitySignalServices & { db: ControlActivityServices["db"] },
|
|
78
80
|
workspaceId: string,
|
|
79
81
|
): Promise<void> {
|
|
80
82
|
const targets = await listPendingCodexCapacityWakeTargets(services.db, workspaceId).catch(
|
|
@@ -130,7 +132,7 @@ export function codexCapacityDecision(
|
|
|
130
132
|
}
|
|
131
133
|
|
|
132
134
|
async function refreshCapacityMetadata(
|
|
133
|
-
services:
|
|
135
|
+
services: ControlActivityServices,
|
|
134
136
|
workspaceId: string,
|
|
135
137
|
): Promise<void> {
|
|
136
138
|
const accounts = await listCodexAccountStatuses(services.db, workspaceId).catch(() => []);
|
|
@@ -151,12 +153,21 @@ async function refreshCapacityMetadata(
|
|
|
151
153
|
);
|
|
152
154
|
}
|
|
153
155
|
|
|
154
|
-
export function createCodexCapacityActivities(services: () => Promise<
|
|
156
|
+
export function createCodexCapacityActivities(services: () => Promise<ControlActivityServices>) {
|
|
155
157
|
async function getCodexCapacityWait(input: GetCodexCapacityWaitInput) {
|
|
156
158
|
const { db } = await services();
|
|
157
|
-
const
|
|
159
|
+
const codexWaiter = await getCodexCapacityWaitForSession(
|
|
160
|
+
db,
|
|
161
|
+
input.workspaceId,
|
|
162
|
+
input.sessionId,
|
|
163
|
+
);
|
|
164
|
+
const xaiWaiter = codexWaiter
|
|
165
|
+
? null
|
|
166
|
+
: await getXaiCapacityWaitForSession(db, input.workspaceId, input.sessionId);
|
|
167
|
+
const waiter = codexWaiter ?? xaiWaiter;
|
|
158
168
|
return waiter
|
|
159
169
|
? {
|
|
170
|
+
...(xaiWaiter ? { provider: "xai" as const } : {}),
|
|
160
171
|
waiterId: waiter.id,
|
|
161
172
|
generation: waiter.generation,
|
|
162
173
|
// A capacity mutation may have committed while its Temporal signal
|
|
@@ -176,6 +187,42 @@ export function createCodexCapacityActivities(services: () => Promise<ActivitySe
|
|
|
176
187
|
input: ReconcileCodexCapacityWaitInput,
|
|
177
188
|
): Promise<ReconcileCodexCapacityWaitResult> {
|
|
178
189
|
const resolved = await services();
|
|
190
|
+
if (input.provider === "xai") {
|
|
191
|
+
const current = await getXaiCapacityWaitForSession(
|
|
192
|
+
resolved.db,
|
|
193
|
+
input.workspaceId,
|
|
194
|
+
input.sessionId,
|
|
195
|
+
);
|
|
196
|
+
if (!current || current.id !== input.waiterId || current.generation !== input.generation) {
|
|
197
|
+
return { action: "stale" };
|
|
198
|
+
}
|
|
199
|
+
const result = await reconcileXaiCapacityWaitDb(resolved.db, {
|
|
200
|
+
accountId: input.accountId,
|
|
201
|
+
workspaceId: input.workspaceId,
|
|
202
|
+
sessionId: input.sessionId,
|
|
203
|
+
waiterId: input.waiterId,
|
|
204
|
+
generation: input.generation,
|
|
205
|
+
});
|
|
206
|
+
if (result.events.length > 0) {
|
|
207
|
+
try {
|
|
208
|
+
await resolved.bus.publish(input.workspaceId, input.sessionId, result.events);
|
|
209
|
+
} catch {
|
|
210
|
+
// Postgres is authoritative; SSE replay/gap fill repairs missed fanout.
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (result.action === "resumed") return { action: "resumed" };
|
|
214
|
+
if (result.action === "waiting") {
|
|
215
|
+
return {
|
|
216
|
+
action: "waiting",
|
|
217
|
+
provider: "xai",
|
|
218
|
+
waiterId: result.waiter.id,
|
|
219
|
+
generation: result.waiter.generation,
|
|
220
|
+
nextCheckAt: result.waiter.nextCheckAt.toISOString(),
|
|
221
|
+
wakeRevision: result.waiter.wakeRevision,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
return { action: result.action };
|
|
225
|
+
}
|
|
179
226
|
const current = await getCodexCapacityWaitForSession(
|
|
180
227
|
resolved.db,
|
|
181
228
|
input.workspaceId,
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {
|
|
2
|
+
CODEX_PROVIDER_ID,
|
|
3
|
+
generateCodexSubscriptionImage,
|
|
4
|
+
type CodexRequestContext,
|
|
5
|
+
} from "@opengeni/codex";
|
|
6
|
+
import type { Database } from "@opengeni/db";
|
|
7
|
+
import type { ObjectStorage } from "@opengeni/storage";
|
|
8
|
+
import type { GeneratedImageReceipt } from "./generated-images";
|
|
9
|
+
import {
|
|
10
|
+
executeImageGenerationOperation,
|
|
11
|
+
imageProviderBindingHash,
|
|
12
|
+
} from "./image-generation-operation";
|
|
13
|
+
import type { ResolvedImageGenerationReference } from "./image-generation-references";
|
|
14
|
+
|
|
15
|
+
const CODEX_IMAGE_MODEL = "gpt-image-2";
|
|
16
|
+
|
|
17
|
+
/** Execute the same standalone subscription image path used by Codex clients. */
|
|
18
|
+
export async function executeCodexImageGeneration(input: {
|
|
19
|
+
db: Database;
|
|
20
|
+
objectStorage: ObjectStorage | null;
|
|
21
|
+
accountId: string;
|
|
22
|
+
workspaceId: string;
|
|
23
|
+
sessionId: string;
|
|
24
|
+
turnId: string;
|
|
25
|
+
attemptId: string;
|
|
26
|
+
toolCallId: string;
|
|
27
|
+
prompt: string;
|
|
28
|
+
references?: readonly ResolvedImageGenerationReference[];
|
|
29
|
+
credentialId: string;
|
|
30
|
+
codexContext: Pick<CodexRequestContext, "clientVersion" | "getToken" | "refresh">;
|
|
31
|
+
abortSignal?: AbortSignal;
|
|
32
|
+
}): Promise<GeneratedImageReceipt> {
|
|
33
|
+
const providerBindingHash = imageProviderBindingHash(CODEX_PROVIDER_ID, input.credentialId);
|
|
34
|
+
return await executeImageGenerationOperation({
|
|
35
|
+
...input,
|
|
36
|
+
providerId: CODEX_PROVIDER_ID,
|
|
37
|
+
providerBindingHash,
|
|
38
|
+
modelId: CODEX_IMAGE_MODEL,
|
|
39
|
+
...(input.references ? { referenceDigests: input.references } : {}),
|
|
40
|
+
generate: async () => {
|
|
41
|
+
const generated = await generateCodexSubscriptionImage({
|
|
42
|
+
prompt: input.prompt,
|
|
43
|
+
...(input.references ? { references: input.references } : {}),
|
|
44
|
+
turnId: input.turnId,
|
|
45
|
+
context: input.codexContext,
|
|
46
|
+
...(input.abortSignal ? { abortSignal: input.abortSignal } : {}),
|
|
47
|
+
});
|
|
48
|
+
return {
|
|
49
|
+
toolCallId: input.toolCallId,
|
|
50
|
+
providerItemId: null,
|
|
51
|
+
bytes: generated.bytes,
|
|
52
|
+
declaredMediaType: generated.declaredMediaType,
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
applyContextCompaction,
|
|
3
|
-
|
|
3
|
+
getActiveSessionHistoryItemsPaged,
|
|
4
4
|
recordSkippedContextCompaction,
|
|
5
5
|
recordStartedContextCompaction,
|
|
6
6
|
type Database,
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
estimateTokens,
|
|
17
17
|
latestCompactionReplacementFingerprint,
|
|
18
18
|
prepareCompactionPromptInput,
|
|
19
|
+
projectRemoteCompactionOverflowRetryInput,
|
|
19
20
|
sanitizeHistoryItemsForModel,
|
|
20
21
|
summarizeForCompaction,
|
|
21
22
|
type CompactionItem,
|
|
@@ -112,7 +113,9 @@ export async function maybeCompactContext(
|
|
|
112
113
|
}
|
|
113
114
|
const useRemoteV2 = options.codexCompactionMode === "remote_v2";
|
|
114
115
|
|
|
115
|
-
|
|
116
|
+
// Preserve the complete ordered transcript while bounding each Postgres
|
|
117
|
+
// driver result frame beside the decoded history already held by this turn.
|
|
118
|
+
const active = await getActiveSessionHistoryItemsPaged(db, scope.workspaceId, scope.sessionId);
|
|
116
119
|
if (active.length === 0) {
|
|
117
120
|
let requestConsumed = false;
|
|
118
121
|
if (options.clearRequestedCompaction) {
|
|
@@ -325,7 +328,19 @@ async function compactContextRemoteV2(
|
|
|
325
328
|
// Codex remote_v2: on a valid compaction item, install and recompute usage.
|
|
326
329
|
// No local "must shrink / must differ" gate — that is portable-only.
|
|
327
330
|
// Fail closed on provider/extract failure — no portable fallback.
|
|
328
|
-
|
|
331
|
+
let providerCalls = 1;
|
|
332
|
+
let rewrittenToolOutputs = 0;
|
|
333
|
+
let compactionItem: CompactionItem;
|
|
334
|
+
try {
|
|
335
|
+
compactionItem = await options.requestRemoteCompactionV2(settings, items);
|
|
336
|
+
} catch (error) {
|
|
337
|
+
if (!isExactContextLengthExceeded(error)) throw error;
|
|
338
|
+
const retry = projectRemoteCompactionOverflowRetryInput(items);
|
|
339
|
+
if (retry.rewrittenToolOutputs === 0) throw error;
|
|
340
|
+
providerCalls = 2;
|
|
341
|
+
rewrittenToolOutputs = retry.rewrittenToolOutputs;
|
|
342
|
+
compactionItem = await options.requestRemoteCompactionV2(settings, retry.input);
|
|
343
|
+
}
|
|
329
344
|
const replacementHistory = buildRemoteV2ReplacementHistory(canonicalItems, compactionItem);
|
|
330
345
|
const estimatedTokensAfter = estimateTokens(await projectForWire(replacementHistory));
|
|
331
346
|
const replacementFingerprint = compactionReplacementFingerprint(replacementHistory);
|
|
@@ -351,6 +366,8 @@ async function compactContextRemoteV2(
|
|
|
351
366
|
implementation: REMOTE_COMPACTION_V2_IMPLEMENTATION,
|
|
352
367
|
estimatedTokensBefore,
|
|
353
368
|
estimatedTokensAfter,
|
|
369
|
+
compactionInputToolOutputsRewritten: rewrittenToolOutputs,
|
|
370
|
+
compactionInputProviderCalls: providerCalls,
|
|
354
371
|
},
|
|
355
372
|
});
|
|
356
373
|
if (!applied.applied) {
|
|
@@ -523,3 +540,21 @@ export function isContextWindowExceeded(error: unknown, seen = new WeakSet<objec
|
|
|
523
540
|
isContextWindowExceeded(record.diagnostics, seen)
|
|
524
541
|
);
|
|
525
542
|
}
|
|
543
|
+
|
|
544
|
+
export function isExactContextLengthExceeded(
|
|
545
|
+
error: unknown,
|
|
546
|
+
seen = new WeakSet<object>(),
|
|
547
|
+
): boolean {
|
|
548
|
+
if (!error || typeof error !== "object") return false;
|
|
549
|
+
if (seen.has(error)) return false;
|
|
550
|
+
seen.add(error);
|
|
551
|
+
const record = error as Record<string, unknown>;
|
|
552
|
+
if (record.code === "context_length_exceeded") {
|
|
553
|
+
return true;
|
|
554
|
+
}
|
|
555
|
+
return (
|
|
556
|
+
isExactContextLengthExceeded(record.cause, seen) ||
|
|
557
|
+
isExactContextLengthExceeded(record.error, seen) ||
|
|
558
|
+
isExactContextLengthExceeded(record.diagnostics, seen)
|
|
559
|
+
);
|
|
560
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { DocumentServices } from "@opengeni/documents";
|
|
2
2
|
import { configuredStaticUsageLimits } from "@opengeni/config";
|
|
3
3
|
import {
|
|
4
4
|
getBillingBalance,
|
|
@@ -8,15 +8,25 @@ import {
|
|
|
8
8
|
sumUsageQuantity,
|
|
9
9
|
withWorkspaceUsageLock,
|
|
10
10
|
} from "@opengeni/db";
|
|
11
|
-
import type {
|
|
11
|
+
import type { ControlActivityServices, IndexDocumentInput } from "./types";
|
|
12
12
|
|
|
13
|
-
export function createDocumentActivities(
|
|
13
|
+
export function createDocumentActivities(
|
|
14
|
+
services: () => Promise<ControlActivityServices>,
|
|
15
|
+
resolveDocumentServices?: () => Promise<DocumentServices>,
|
|
16
|
+
) {
|
|
14
17
|
return {
|
|
15
18
|
indexDocument: async (input: IndexDocumentInput) => {
|
|
16
|
-
const { settings, db, objectStorage
|
|
19
|
+
const { settings, db, objectStorage } = await services();
|
|
17
20
|
if (!objectStorage) {
|
|
18
21
|
throw new Error("object storage is not configured");
|
|
19
22
|
}
|
|
23
|
+
if (!resolveDocumentServices) {
|
|
24
|
+
throw new Error("document services are not configured");
|
|
25
|
+
}
|
|
26
|
+
const [{ getDocument, indexDocumentNow }, documentServices] = await Promise.all([
|
|
27
|
+
import("@opengeni/documents"),
|
|
28
|
+
resolveDocumentServices(),
|
|
29
|
+
]);
|
|
20
30
|
const context = await rlsContextForWorkspace(db, input.workspaceId);
|
|
21
31
|
if (context.accountId !== input.accountId) {
|
|
22
32
|
throw new Error("document account/workspace authority mismatch");
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
applyGitAuthPointerEnvironment,
|
|
3
|
-
|
|
3
|
+
codemodeWorkspaceUrl,
|
|
4
|
+
resolveFirstPartyDelegationSecret,
|
|
4
5
|
stableSandboxEnvironmentForRun,
|
|
5
6
|
type Settings,
|
|
6
7
|
} from "@opengeni/config";
|
|
@@ -67,35 +68,40 @@ export type GitHubTokenMintAuthorization = (selection: {
|
|
|
67
68
|
repositoryIds: number[];
|
|
68
69
|
}) => Promise<void>;
|
|
69
70
|
|
|
70
|
-
export const
|
|
71
|
+
export const CODEMODE_TOKEN_TTL_SECONDS = 60 * 60;
|
|
71
72
|
|
|
72
|
-
export type
|
|
73
|
+
export type MintedSandboxCodemodeToken = {
|
|
73
74
|
token: string;
|
|
74
75
|
expiresAt: Date;
|
|
75
76
|
};
|
|
76
77
|
|
|
77
|
-
export type
|
|
78
|
+
export type SandboxCodemodeAuthority = {
|
|
78
79
|
sessionId: string;
|
|
80
|
+
turnId: string;
|
|
81
|
+
attemptId: string;
|
|
82
|
+
executionGeneration: number;
|
|
79
83
|
};
|
|
80
84
|
|
|
81
|
-
export async function
|
|
85
|
+
export async function mintSandboxCodemodeToken(
|
|
82
86
|
settings: Settings,
|
|
83
87
|
scope: ConnectionScope,
|
|
84
|
-
authority:
|
|
88
|
+
authority: SandboxCodemodeAuthority,
|
|
85
89
|
nowMs = Date.now(),
|
|
86
|
-
): Promise<
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
const token = await signDelegatedAccessToken(settings.delegationSecret, {
|
|
90
|
+
): Promise<MintedSandboxCodemodeToken | undefined> {
|
|
91
|
+
const delegationSecret = resolveFirstPartyDelegationSecret(settings);
|
|
92
|
+
if (!delegationSecret) return undefined;
|
|
93
|
+
const expiresAtSeconds = Math.floor(nowMs / 1000) + CODEMODE_TOKEN_TTL_SECONDS;
|
|
94
|
+
const token = await signDelegatedAccessToken(delegationSecret, {
|
|
92
95
|
accountId: scope.accountId,
|
|
93
96
|
workspaceId: scope.workspaceId,
|
|
94
|
-
subjectId: `sandbox:${authority.
|
|
95
|
-
subjectLabel: "sandbox
|
|
96
|
-
permissions: ["
|
|
97
|
+
subjectId: `sandbox:${authority.attemptId}`,
|
|
98
|
+
subjectLabel: "sandbox Codemode",
|
|
99
|
+
permissions: ["codemode:call"],
|
|
97
100
|
sessionId: authority.sessionId,
|
|
98
|
-
|
|
101
|
+
turnId: authority.turnId,
|
|
102
|
+
attemptId: authority.attemptId,
|
|
103
|
+
executionGeneration: authority.executionGeneration,
|
|
104
|
+
principalKind: "agent_attempt",
|
|
99
105
|
exp: expiresAtSeconds,
|
|
100
106
|
});
|
|
101
107
|
return { token, expiresAt: new Date(expiresAtSeconds * 1000) };
|
|
@@ -203,9 +209,15 @@ export async function sandboxEnvironmentForRun(
|
|
|
203
209
|
// value later. `= {}` default so the non-optional reads below are safe.
|
|
204
210
|
options: RunGitCredentialOptions & {
|
|
205
211
|
skipGitHubToken?: boolean;
|
|
206
|
-
|
|
212
|
+
/**
|
|
213
|
+
* Codemode credential delivery for this attempt. Managed sandboxes receive
|
|
214
|
+
* stable URL/file pointers in their manifest and the bearer through the
|
|
215
|
+
* lifecycle file writer. Connected Machines receive no manifest pointers;
|
|
216
|
+
* the worker injects the minted bearer only into exact exec requests.
|
|
217
|
+
*/
|
|
218
|
+
codemodeDelivery?: "managed_file" | "transient_exec" | "none";
|
|
207
219
|
deferGitHubToken?: boolean;
|
|
208
|
-
|
|
220
|
+
codemodeAuthority?: SandboxCodemodeAuthority;
|
|
209
221
|
} = {},
|
|
210
222
|
): Promise<{
|
|
211
223
|
environment: Record<string, string>;
|
|
@@ -213,8 +225,8 @@ export async function sandboxEnvironmentForRun(
|
|
|
213
225
|
gitTokens?: GitTokenSeeds;
|
|
214
226
|
gitTokenExpiresAt?: GitTokenExpiries;
|
|
215
227
|
gitCredentialBindings?: GitCredentialBindingSeed[];
|
|
216
|
-
|
|
217
|
-
|
|
228
|
+
codemodeToken?: string;
|
|
229
|
+
codemodeTokenExpiresAt?: Date;
|
|
218
230
|
}> {
|
|
219
231
|
// Precedence: deployment allowlist < git identity < workspace environment
|
|
220
232
|
// < backend-aware HOME (the STABLE base, shared with the API-direct attach
|
|
@@ -235,25 +247,22 @@ export async function sandboxEnvironmentForRun(
|
|
|
235
247
|
// proactively renews every selected provider behind these stable pointers.
|
|
236
248
|
const stableOptions = options.scope ? { workspaceId: options.scope.workspaceId } : {};
|
|
237
249
|
const environment = stableSandboxEnvironmentForRun(settings, workspaceEnvironment, stableOptions);
|
|
238
|
-
|
|
239
|
-
//
|
|
240
|
-
//
|
|
241
|
-
//
|
|
242
|
-
if (
|
|
243
|
-
delete environment.
|
|
244
|
-
delete environment.
|
|
250
|
+
const codemodeDelivery = options.codemodeDelivery ?? "managed_file";
|
|
251
|
+
// Connected Machines keep their machine-wide environment untouched. Their
|
|
252
|
+
// URL, package hint, and direct bearer are projected only onto an exact child
|
|
253
|
+
// exec request by SelfhostedSession, never into the manifest or filesystem.
|
|
254
|
+
if (codemodeDelivery !== "managed_file") {
|
|
255
|
+
delete environment.OPENGENI_CODEMODE_URL;
|
|
256
|
+
delete environment.OPENGENI_CODEMODE_TOKEN_FILE;
|
|
245
257
|
delete environment.OPENGENI_OGTOOL_PACKAGE_SPEC;
|
|
246
258
|
}
|
|
247
|
-
const
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
? await
|
|
259
|
+
const codemodeScope = options.scope;
|
|
260
|
+
const codemodeToken =
|
|
261
|
+
codemodeDelivery !== "none" && codemodeScope && options.codemodeAuthority
|
|
262
|
+
? await mintSandboxCodemodeToken(settings, codemodeScope, options.codemodeAuthority)
|
|
251
263
|
: undefined;
|
|
252
|
-
if (
|
|
253
|
-
environment.
|
|
254
|
-
settings,
|
|
255
|
-
toolspaceScope.workspaceId,
|
|
256
|
-
);
|
|
264
|
+
if (codemodeDelivery === "managed_file" && codemodeToken && codemodeScope) {
|
|
265
|
+
environment.OPENGENI_CODEMODE_URL ??= codemodeWorkspaceUrl(settings, codemodeScope.workspaceId);
|
|
257
266
|
}
|
|
258
267
|
const selections = gitCredentialSelections(resources);
|
|
259
268
|
// NO-TOKEN SKIP (Stage D, change B): when the turn's EFFECTIVE compute backend is
|
|
@@ -268,10 +277,10 @@ export async function sandboxEnvironmentForRun(
|
|
|
268
277
|
if (selections.length === 0 || options.skipGitHubToken) {
|
|
269
278
|
return {
|
|
270
279
|
environment,
|
|
271
|
-
...(
|
|
280
|
+
...(codemodeToken
|
|
272
281
|
? {
|
|
273
|
-
|
|
274
|
-
|
|
282
|
+
codemodeToken: codemodeToken.token,
|
|
283
|
+
codemodeTokenExpiresAt: codemodeToken.expiresAt,
|
|
275
284
|
}
|
|
276
285
|
: {}),
|
|
277
286
|
};
|
|
@@ -283,10 +292,10 @@ export async function sandboxEnvironmentForRun(
|
|
|
283
292
|
);
|
|
284
293
|
return {
|
|
285
294
|
environment,
|
|
286
|
-
...(
|
|
295
|
+
...(codemodeToken
|
|
287
296
|
? {
|
|
288
|
-
|
|
289
|
-
|
|
297
|
+
codemodeToken: codemodeToken.token,
|
|
298
|
+
codemodeTokenExpiresAt: codemodeToken.expiresAt,
|
|
290
299
|
}
|
|
291
300
|
: {}),
|
|
292
301
|
};
|
|
@@ -313,10 +322,10 @@ export async function sandboxEnvironmentForRun(
|
|
|
313
322
|
...(Object.keys(minted.gitTokens).length > 0 ? { gitTokens: minted.gitTokens } : {}),
|
|
314
323
|
...(Object.keys(minted.expiresAt).length > 0 ? { gitTokenExpiresAt: minted.expiresAt } : {}),
|
|
315
324
|
...(minted.bindings.length > 0 ? { gitCredentialBindings: minted.bindings } : {}),
|
|
316
|
-
...(
|
|
325
|
+
...(codemodeToken
|
|
317
326
|
? {
|
|
318
|
-
|
|
319
|
-
|
|
327
|
+
codemodeToken: codemodeToken.token,
|
|
328
|
+
codemodeTokenExpiresAt: codemodeToken.expiresAt,
|
|
320
329
|
}
|
|
321
330
|
: {}),
|
|
322
331
|
};
|
|
@@ -14,7 +14,7 @@ export type ExpiringMaterialRenewalController = {
|
|
|
14
14
|
|
|
15
15
|
export type ExpiringMaterialRenewalPublicErrorClass =
|
|
16
16
|
| "RunCredentialRenewalOperationError"
|
|
17
|
-
| "
|
|
17
|
+
| "CodemodeTokenRenewalOperationError";
|
|
18
18
|
|
|
19
19
|
export type ExpiringMaterialRenewalOptions<
|
|
20
20
|
T,
|
|
@@ -3,10 +3,12 @@ import {
|
|
|
3
3
|
claimExpiredFileUploadCleanup,
|
|
4
4
|
completeDueTranscriptionRecordingObjectCleanup,
|
|
5
5
|
completeExpiredFileUploadCleanup,
|
|
6
|
+
claimVideoGenerationOperations,
|
|
6
7
|
purgeExpiredTranscriptionRecordings,
|
|
7
8
|
} from "@opengeni/db";
|
|
8
9
|
import type { ObjectStorage } from "@opengeni/storage";
|
|
9
|
-
import type {
|
|
10
|
+
import type { ControlActivityServices } from "./types";
|
|
11
|
+
import { randomUUID } from "node:crypto";
|
|
10
12
|
|
|
11
13
|
export const FILE_UPLOAD_CLEANUP_GRACE_MS = 60 * 60 * 1_000;
|
|
12
14
|
export const FILE_UPLOAD_CLEANUP_CLAIM_TIMEOUT_MS = 10 * 60 * 1_000;
|
|
@@ -72,7 +74,7 @@ export type FileUploadReaperActivityOptions = {
|
|
|
72
74
|
* settled terminally. One provider failure never aborts the rest of the batch.
|
|
73
75
|
*/
|
|
74
76
|
export function createFileUploadReaperActivities(
|
|
75
|
-
services: () => Promise<
|
|
77
|
+
services: () => Promise<ControlActivityServices>,
|
|
76
78
|
options: FileUploadReaperActivityOptions = {},
|
|
77
79
|
) {
|
|
78
80
|
const graceMs = options.graceMs ?? FILE_UPLOAD_CLEANUP_GRACE_MS;
|
|
@@ -176,5 +178,34 @@ export function createFileUploadReaperActivities(
|
|
|
176
178
|
return { claimed, deleted, failed };
|
|
177
179
|
}
|
|
178
180
|
|
|
179
|
-
|
|
181
|
+
async function recoverVideoGenerationWorkflows(): Promise<{
|
|
182
|
+
claimed: number;
|
|
183
|
+
started: number;
|
|
184
|
+
failed: number;
|
|
185
|
+
}> {
|
|
186
|
+
const { db, startVideoGenerationWorkflow, observability } = await services();
|
|
187
|
+
if (!startVideoGenerationWorkflow) return { claimed: 0, started: 0, failed: 0 };
|
|
188
|
+
const claims = await claimVideoGenerationOperations(db, {
|
|
189
|
+
owner: `video-workflow-recovery:${randomUUID()}`,
|
|
190
|
+
leaseSeconds: 120,
|
|
191
|
+
limit: 100,
|
|
192
|
+
});
|
|
193
|
+
let started = 0;
|
|
194
|
+
let failed = 0;
|
|
195
|
+
for (const claim of claims) {
|
|
196
|
+
try {
|
|
197
|
+
await startVideoGenerationWorkflow(claim);
|
|
198
|
+
started += 1;
|
|
199
|
+
} catch (error) {
|
|
200
|
+
failed += 1;
|
|
201
|
+
observability.warn("Video generation workflow recovery start failed", {
|
|
202
|
+
operationId: claim.operationId,
|
|
203
|
+
errorClass: error instanceof Error ? error.name : "UnknownError",
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return { claimed: claims.length, started, failed };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return { reapExpiredFileUploads, recoverVideoGenerationWorkflows };
|
|
180
211
|
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import type { Database } from "@opengeni/db";
|
|
2
|
+
import { readJsonBase64Field, readResponseTextBounded, type FetchLike } from "@opengeni/network";
|
|
3
|
+
import type { ObjectStorage } from "@opengeni/storage";
|
|
4
|
+
import { type GeneratedImageOutput, type GeneratedImageReceipt } from "./generated-images";
|
|
5
|
+
import {
|
|
6
|
+
executeImageGenerationOperation,
|
|
7
|
+
imageProviderBindingHash,
|
|
8
|
+
} from "./image-generation-operation";
|
|
9
|
+
import type { ResolvedImageGenerationReference } from "./image-generation-references";
|
|
10
|
+
|
|
11
|
+
const GATEWAY_IMAGE_PROVIDER_ID = "vercel-ai-gateway";
|
|
12
|
+
const GATEWAY_IMAGE_URL = "https://ai-gateway.vercel.sh/v3/ai/image-model";
|
|
13
|
+
const GATEWAY_PROTOCOL_VERSION = "0.0.1";
|
|
14
|
+
const GATEWAY_IMAGE_SPECIFICATION_VERSION = "3";
|
|
15
|
+
const GATEWAY_IMAGE_RESPONSE_MAX_BYTES = 90 * 1024 * 1024;
|
|
16
|
+
const GATEWAY_IMAGE_MAX_BYTES = 64 * 1024 * 1024;
|
|
17
|
+
const GATEWAY_IMAGE_ERROR_MAX_BYTES = 64 * 1024;
|
|
18
|
+
|
|
19
|
+
export class GatewayImageApiError extends Error {
|
|
20
|
+
constructor(
|
|
21
|
+
readonly status: number,
|
|
22
|
+
message: string,
|
|
23
|
+
) {
|
|
24
|
+
super(message);
|
|
25
|
+
this.name = "GatewayImageApiError";
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export async function executeGatewayImageGeneration(input: {
|
|
30
|
+
db: Database;
|
|
31
|
+
objectStorage: ObjectStorage | null;
|
|
32
|
+
accountId: string;
|
|
33
|
+
workspaceId: string;
|
|
34
|
+
sessionId: string;
|
|
35
|
+
turnId: string;
|
|
36
|
+
attemptId: string;
|
|
37
|
+
apiKey: string;
|
|
38
|
+
modelId: string;
|
|
39
|
+
prompt: string;
|
|
40
|
+
references?: readonly ResolvedImageGenerationReference[];
|
|
41
|
+
toolCallId: string;
|
|
42
|
+
abortSignal?: AbortSignal;
|
|
43
|
+
}): Promise<GeneratedImageReceipt> {
|
|
44
|
+
const providerBindingHash = imageProviderBindingHash(GATEWAY_IMAGE_PROVIDER_ID, input.apiKey);
|
|
45
|
+
return await executeImageGenerationOperation({
|
|
46
|
+
...input,
|
|
47
|
+
providerId: GATEWAY_IMAGE_PROVIDER_ID,
|
|
48
|
+
providerBindingHash,
|
|
49
|
+
...(input.references ? { referenceDigests: input.references } : {}),
|
|
50
|
+
generate: async () =>
|
|
51
|
+
await generateGatewayImage({
|
|
52
|
+
apiKey: input.apiKey,
|
|
53
|
+
modelId: input.modelId,
|
|
54
|
+
prompt: input.prompt,
|
|
55
|
+
...(input.references ? { references: input.references } : {}),
|
|
56
|
+
toolCallId: input.toolCallId,
|
|
57
|
+
...(input.abortSignal ? { abortSignal: input.abortSignal } : {}),
|
|
58
|
+
}),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* One explicit Vercel AI Gateway image-protocol adapter. The caller owns durable
|
|
64
|
+
* operation admission; this function never retries an outcome-ambiguous paid
|
|
65
|
+
* request. The response is decoded directly from its JSON stream so neither
|
|
66
|
+
* the complete envelope nor its multi-megabyte base64 string enters memory.
|
|
67
|
+
*/
|
|
68
|
+
export async function generateGatewayImage(input: {
|
|
69
|
+
apiKey: string;
|
|
70
|
+
modelId: string;
|
|
71
|
+
prompt: string;
|
|
72
|
+
references?: readonly ResolvedImageGenerationReference[];
|
|
73
|
+
toolCallId: string;
|
|
74
|
+
abortSignal?: AbortSignal;
|
|
75
|
+
fetch?: FetchLike;
|
|
76
|
+
}): Promise<GeneratedImageOutput> {
|
|
77
|
+
if (!input.apiKey.trim()) throw new Error("AI Gateway image generation credential is empty");
|
|
78
|
+
if (!input.modelId.trim()) throw new Error("AI Gateway image generation model is empty");
|
|
79
|
+
const response = await (input.fetch ?? globalThis.fetch)(GATEWAY_IMAGE_URL, {
|
|
80
|
+
method: "POST",
|
|
81
|
+
redirect: "error",
|
|
82
|
+
headers: {
|
|
83
|
+
Authorization: `Bearer ${input.apiKey}`,
|
|
84
|
+
accept: "application/json",
|
|
85
|
+
"content-type": "application/json",
|
|
86
|
+
"ai-gateway-auth-method": "api-key",
|
|
87
|
+
"ai-gateway-protocol-version": GATEWAY_PROTOCOL_VERSION,
|
|
88
|
+
"ai-image-model-specification-version": GATEWAY_IMAGE_SPECIFICATION_VERSION,
|
|
89
|
+
"ai-model-id": input.modelId,
|
|
90
|
+
"user-agent": "opengeni/image-generation",
|
|
91
|
+
},
|
|
92
|
+
body: JSON.stringify({
|
|
93
|
+
prompt: input.prompt,
|
|
94
|
+
n: 1,
|
|
95
|
+
...(input.references?.length
|
|
96
|
+
? {
|
|
97
|
+
files: input.references.map((reference) => ({
|
|
98
|
+
type: "file",
|
|
99
|
+
mediaType: reference.mediaType,
|
|
100
|
+
data: Buffer.from(reference.bytes).toString("base64"),
|
|
101
|
+
})),
|
|
102
|
+
}
|
|
103
|
+
: {}),
|
|
104
|
+
}),
|
|
105
|
+
...(input.abortSignal ? { signal: input.abortSignal } : {}),
|
|
106
|
+
});
|
|
107
|
+
if (!response.ok) {
|
|
108
|
+
const detail = await readResponseTextBounded(
|
|
109
|
+
response,
|
|
110
|
+
GATEWAY_IMAGE_ERROR_MAX_BYTES,
|
|
111
|
+
"AI Gateway image error",
|
|
112
|
+
input.abortSignal ? { signal: input.abortSignal } : {},
|
|
113
|
+
).catch(() => "");
|
|
114
|
+
throw new GatewayImageApiError(
|
|
115
|
+
response.status,
|
|
116
|
+
detail
|
|
117
|
+
? `AI Gateway image generation failed (${response.status}): ${boundedErrorMessage(detail)}`
|
|
118
|
+
: `AI Gateway image generation failed (${response.status})`,
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
const bytes = await readJsonBase64Field(response, {
|
|
122
|
+
fieldName: "images",
|
|
123
|
+
shape: "first_array_string",
|
|
124
|
+
maxResponseBytes: GATEWAY_IMAGE_RESPONSE_MAX_BYTES,
|
|
125
|
+
maxDecodedBytes: GATEWAY_IMAGE_MAX_BYTES,
|
|
126
|
+
label: "AI Gateway image generation",
|
|
127
|
+
...(input.abortSignal ? { signal: input.abortSignal } : {}),
|
|
128
|
+
});
|
|
129
|
+
return {
|
|
130
|
+
toolCallId: input.toolCallId,
|
|
131
|
+
providerItemId: null,
|
|
132
|
+
bytes,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function boundedErrorMessage(body: string): string {
|
|
137
|
+
let message = body;
|
|
138
|
+
try {
|
|
139
|
+
const value = JSON.parse(body) as { error?: { message?: unknown }; message?: unknown };
|
|
140
|
+
const candidate = value.error?.message ?? value.message;
|
|
141
|
+
if (typeof candidate === "string") message = candidate;
|
|
142
|
+
} catch {
|
|
143
|
+
// Preserve a bounded non-JSON provider diagnostic.
|
|
144
|
+
}
|
|
145
|
+
return message.replace(/\s+/g, " ").trim().slice(0, 1_000);
|
|
146
|
+
}
|