@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
|
@@ -0,0 +1,860 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COMPUTER_SCREENSHOT_MAX_DIMENSION,
|
|
3
|
+
COMPUTER_SCREENSHOT_MAX_PIXELS,
|
|
4
|
+
GENERATED_IMAGE_MAX_BYTES,
|
|
5
|
+
GeneratedImageReceiptSchema,
|
|
6
|
+
retainedGeneratedImageReferenceFromFile,
|
|
7
|
+
type GeneratedImageReceipt,
|
|
8
|
+
} from "@opengeni/contracts";
|
|
9
|
+
import {
|
|
10
|
+
completeFileUpload,
|
|
11
|
+
prepareGeneratedImageArtifact,
|
|
12
|
+
recordGeneratedImageArtifactError,
|
|
13
|
+
settleGeneratedImageArtifactReady,
|
|
14
|
+
type Database,
|
|
15
|
+
type GeneratedImageArtifact,
|
|
16
|
+
type GeneratedImageSourceStrategy,
|
|
17
|
+
} from "@opengeni/db";
|
|
18
|
+
import type { ObjectHead, ObjectStorage } from "@opengeni/storage";
|
|
19
|
+
import { createHash } from "node:crypto";
|
|
20
|
+
import { ScreenshotValidationError, validateRetainableSessionImage } from "./retained-screenshots";
|
|
21
|
+
|
|
22
|
+
const GENERATED_IMAGE_MARKER = "generated_image";
|
|
23
|
+
const UPLOAD_INTENT_TTL_MS = 60 * 60_000;
|
|
24
|
+
|
|
25
|
+
export type GeneratedImageMediaType = "image/png" | "image/jpeg" | "image/webp";
|
|
26
|
+
|
|
27
|
+
export type ValidatedGeneratedImage = {
|
|
28
|
+
bytes: Uint8Array;
|
|
29
|
+
mediaType: GeneratedImageMediaType;
|
|
30
|
+
extension: "png" | "jpg" | "webp";
|
|
31
|
+
sizeBytes: number;
|
|
32
|
+
sha256: string;
|
|
33
|
+
width: number;
|
|
34
|
+
height: number;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type GeneratedImageOutput = {
|
|
38
|
+
toolCallId: string;
|
|
39
|
+
providerItemId: string | null;
|
|
40
|
+
bytes: Uint8Array;
|
|
41
|
+
declaredMediaType?: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type { GeneratedImageReceipt };
|
|
45
|
+
|
|
46
|
+
export type RetainedGeneratedImage = {
|
|
47
|
+
receipt: GeneratedImageReceipt;
|
|
48
|
+
artifact: GeneratedImageArtifact;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export class GeneratedImageValidationError extends Error {
|
|
52
|
+
constructor(message: string) {
|
|
53
|
+
super(message);
|
|
54
|
+
this.name = "GeneratedImageValidationError";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Extract the completed Responses hosted-tool payload before JSON serialization. */
|
|
59
|
+
export function generatedImageFromSdkEvent(event: unknown): GeneratedImageOutput | null {
|
|
60
|
+
if (!event || typeof event !== "object") return null;
|
|
61
|
+
const streamEvent = event as {
|
|
62
|
+
type?: unknown;
|
|
63
|
+
item?: { type?: unknown; rawItem?: unknown; id?: unknown };
|
|
64
|
+
};
|
|
65
|
+
if (streamEvent.type !== "run_item_stream_event") return null;
|
|
66
|
+
const item = streamEvent.item;
|
|
67
|
+
if (!item || item.type !== "tool_call_item") return null;
|
|
68
|
+
if (!item.rawItem || typeof item.rawItem !== "object" || Array.isArray(item.rawItem)) return null;
|
|
69
|
+
const raw = item.rawItem as Record<string, unknown>;
|
|
70
|
+
if (
|
|
71
|
+
raw.type !== "hosted_tool_call" ||
|
|
72
|
+
raw.name !== "image_generation_call" ||
|
|
73
|
+
raw.status !== "completed"
|
|
74
|
+
) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
const providerData =
|
|
78
|
+
raw.providerData && typeof raw.providerData === "object" && !Array.isArray(raw.providerData)
|
|
79
|
+
? (raw.providerData as Record<string, unknown>)
|
|
80
|
+
: null;
|
|
81
|
+
const encoded =
|
|
82
|
+
typeof raw.output === "string"
|
|
83
|
+
? raw.output
|
|
84
|
+
: typeof providerData?.result === "string"
|
|
85
|
+
? providerData.result
|
|
86
|
+
: null;
|
|
87
|
+
if (!encoded) return null;
|
|
88
|
+
const providerItemId = raw.id ?? providerData?.id ?? item.id;
|
|
89
|
+
const toolCallId = providerItemId;
|
|
90
|
+
if (typeof toolCallId !== "string" || toolCallId.length === 0) return null;
|
|
91
|
+
return {
|
|
92
|
+
toolCallId,
|
|
93
|
+
providerItemId: typeof providerItemId === "string" ? providerItemId : null,
|
|
94
|
+
bytes: decodeGeneratedImageBase64(encoded),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Classify the native terminal item independently from its current payload shape. */
|
|
99
|
+
export function isCompletedGeneratedImageSdkEvent(event: unknown): boolean {
|
|
100
|
+
if (!isRecord(event) || event.type !== "run_item_stream_event") return false;
|
|
101
|
+
const item = event.item;
|
|
102
|
+
if (!isRecord(item) || item.type !== "tool_call_item" || !isRecord(item.rawItem)) return false;
|
|
103
|
+
return (
|
|
104
|
+
item.rawItem.type === "hosted_tool_call" &&
|
|
105
|
+
item.rawItem.name === "image_generation_call" &&
|
|
106
|
+
item.rawItem.status === "completed"
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function generatedImagesFromHistory(
|
|
111
|
+
history: ReadonlyArray<Record<string, unknown>>,
|
|
112
|
+
retainedProviderItems?: Pick<ReadonlyMap<string, unknown>, "has">,
|
|
113
|
+
): GeneratedImageOutput[] {
|
|
114
|
+
const outputs: GeneratedImageOutput[] = [];
|
|
115
|
+
for (const item of history) {
|
|
116
|
+
if (
|
|
117
|
+
item.type !== "hosted_tool_call" ||
|
|
118
|
+
item.name !== "image_generation_call" ||
|
|
119
|
+
item.status !== "completed" ||
|
|
120
|
+
typeof item.id !== "string" ||
|
|
121
|
+
(typeof item.output !== "string" &&
|
|
122
|
+
!(
|
|
123
|
+
item.providerData &&
|
|
124
|
+
typeof item.providerData === "object" &&
|
|
125
|
+
!Array.isArray(item.providerData) &&
|
|
126
|
+
typeof (item.providerData as Record<string, unknown>).result === "string"
|
|
127
|
+
))
|
|
128
|
+
) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if (retainedProviderItems?.has(item.id)) continue;
|
|
132
|
+
if (generatedImageReceiptFromUnknown(item.output)) continue;
|
|
133
|
+
outputs.push({
|
|
134
|
+
toolCallId: item.id,
|
|
135
|
+
providerItemId: item.id,
|
|
136
|
+
bytes: decodeGeneratedImageBase64(
|
|
137
|
+
typeof item.output === "string"
|
|
138
|
+
? item.output
|
|
139
|
+
: ((item.providerData as Record<string, unknown>).result as string),
|
|
140
|
+
),
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return outputs;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Refuse to serialize a completed native item unless its bytes have a receipt. */
|
|
147
|
+
export function assertGeneratedImageHistoryRetained(
|
|
148
|
+
history: ReadonlyArray<Record<string, unknown>>,
|
|
149
|
+
receiptsByProviderItemId: Pick<ReadonlyMap<string, unknown>, "has">,
|
|
150
|
+
): void {
|
|
151
|
+
for (const item of history) {
|
|
152
|
+
if (
|
|
153
|
+
item.type !== "hosted_tool_call" ||
|
|
154
|
+
item.name !== "image_generation_call" ||
|
|
155
|
+
item.status !== "completed"
|
|
156
|
+
) {
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (
|
|
160
|
+
typeof item.id !== "string" ||
|
|
161
|
+
(!receiptsByProviderItemId.has(item.id) && !generatedImageReceiptFromUnknown(item.output))
|
|
162
|
+
) {
|
|
163
|
+
throw new Error("Completed native image history has no retained-artifact receipt");
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function decodeGeneratedImageBase64(value: string): Uint8Array {
|
|
169
|
+
if (value.length === 0 || value.length % 4 !== 0) {
|
|
170
|
+
throw new GeneratedImageValidationError("generated image base64 has invalid length");
|
|
171
|
+
}
|
|
172
|
+
const padding = value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0;
|
|
173
|
+
const decodedBytes = (value.length / 4) * 3 - padding;
|
|
174
|
+
if (decodedBytes <= 0 || decodedBytes > GENERATED_IMAGE_MAX_BYTES) {
|
|
175
|
+
throw new GeneratedImageValidationError("generated image exceeds the byte limit");
|
|
176
|
+
}
|
|
177
|
+
if (!/^[A-Za-z0-9+/]+={0,2}$/.test(value)) {
|
|
178
|
+
throw new GeneratedImageValidationError("generated image base64 is malformed");
|
|
179
|
+
}
|
|
180
|
+
const lastSextet = base64Sextet(value.charCodeAt(value.length - padding - 1));
|
|
181
|
+
if (
|
|
182
|
+
(padding === 2 && (lastSextet & 0x0f) !== 0) ||
|
|
183
|
+
(padding === 1 && (lastSextet & 0x03) !== 0)
|
|
184
|
+
) {
|
|
185
|
+
throw new GeneratedImageValidationError("generated image base64 is non-canonical");
|
|
186
|
+
}
|
|
187
|
+
const bytes = Buffer.from(value, "base64");
|
|
188
|
+
if (bytes.byteLength !== decodedBytes) {
|
|
189
|
+
throw new GeneratedImageValidationError("generated image base64 is non-canonical");
|
|
190
|
+
}
|
|
191
|
+
return bytes;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Validate signatures and dimensions; never trust provider-declared MIME metadata. */
|
|
195
|
+
export function validateGeneratedImage(input: {
|
|
196
|
+
bytes: Uint8Array;
|
|
197
|
+
declaredMediaType?: string;
|
|
198
|
+
}): ValidatedGeneratedImage {
|
|
199
|
+
try {
|
|
200
|
+
return validateRetainableSessionImage(
|
|
201
|
+
{
|
|
202
|
+
bytes: input.bytes,
|
|
203
|
+
...(input.declaredMediaType ? { declaredMediaType: input.declaredMediaType } : {}),
|
|
204
|
+
},
|
|
205
|
+
{
|
|
206
|
+
maxBytes: GENERATED_IMAGE_MAX_BYTES,
|
|
207
|
+
maxDimension: COMPUTER_SCREENSHOT_MAX_DIMENSION,
|
|
208
|
+
maxPixels: COMPUTER_SCREENSHOT_MAX_PIXELS,
|
|
209
|
+
},
|
|
210
|
+
);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
if (error instanceof ScreenshotValidationError) {
|
|
213
|
+
throw new GeneratedImageValidationError(error.message);
|
|
214
|
+
}
|
|
215
|
+
throw error;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
type GeneratedImageIdentityInput = {
|
|
220
|
+
workspaceId: string;
|
|
221
|
+
sessionId: string;
|
|
222
|
+
turnId: string;
|
|
223
|
+
providerId: string;
|
|
224
|
+
providerBindingHash: string;
|
|
225
|
+
toolCallId: string;
|
|
226
|
+
} & (
|
|
227
|
+
| { sourceStrategy: "native_hosted"; providerItemId: string }
|
|
228
|
+
| { sourceStrategy: "provider_adapter"; providerItemId: null }
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
export function generatedImageIdentity(input: GeneratedImageIdentityInput): {
|
|
232
|
+
artifactId: string;
|
|
233
|
+
uploadId: string;
|
|
234
|
+
settlementKey: string;
|
|
235
|
+
} {
|
|
236
|
+
const hash = createHash("sha256")
|
|
237
|
+
.update("opengeni:generated-image:v2\0")
|
|
238
|
+
.update(input.workspaceId)
|
|
239
|
+
.update("\0")
|
|
240
|
+
.update(input.sourceStrategy)
|
|
241
|
+
.update("\0")
|
|
242
|
+
.update(input.providerId)
|
|
243
|
+
.update("\0")
|
|
244
|
+
.update(input.providerBindingHash)
|
|
245
|
+
.update("\0");
|
|
246
|
+
if (input.sourceStrategy === "native_hosted") {
|
|
247
|
+
hash.update(input.providerItemId);
|
|
248
|
+
} else {
|
|
249
|
+
hash
|
|
250
|
+
.update(input.sessionId)
|
|
251
|
+
.update("\0")
|
|
252
|
+
.update(input.turnId)
|
|
253
|
+
.update("\0")
|
|
254
|
+
.update(input.toolCallId);
|
|
255
|
+
}
|
|
256
|
+
const settlementKey = hash.digest("hex");
|
|
257
|
+
return {
|
|
258
|
+
settlementKey,
|
|
259
|
+
artifactId: uuidFromDigest(settlementKey, 0),
|
|
260
|
+
uploadId: uuidFromDigest(settlementKey, 16),
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export async function retainGeneratedImage(input: {
|
|
265
|
+
db: Database;
|
|
266
|
+
objectStorage: ObjectStorage | null;
|
|
267
|
+
accountId: string;
|
|
268
|
+
workspaceId: string;
|
|
269
|
+
sessionId: string;
|
|
270
|
+
turnId: string;
|
|
271
|
+
attemptId: string;
|
|
272
|
+
sourceStrategy: GeneratedImageSourceStrategy;
|
|
273
|
+
providerId: string;
|
|
274
|
+
providerBindingHash: string;
|
|
275
|
+
output: GeneratedImageOutput;
|
|
276
|
+
now?: Date;
|
|
277
|
+
}): Promise<RetainedGeneratedImage> {
|
|
278
|
+
if (!input.objectStorage) {
|
|
279
|
+
throw new Error("Generated image retention requires configured object storage");
|
|
280
|
+
}
|
|
281
|
+
const image = validateGeneratedImage(input.output);
|
|
282
|
+
const identityInput = {
|
|
283
|
+
workspaceId: input.workspaceId,
|
|
284
|
+
sessionId: input.sessionId,
|
|
285
|
+
turnId: input.turnId,
|
|
286
|
+
providerId: input.providerId,
|
|
287
|
+
providerBindingHash: input.providerBindingHash,
|
|
288
|
+
toolCallId: input.output.toolCallId,
|
|
289
|
+
};
|
|
290
|
+
let identity: ReturnType<typeof generatedImageIdentity>;
|
|
291
|
+
if (input.sourceStrategy === "native_hosted") {
|
|
292
|
+
if (!input.output.providerItemId) {
|
|
293
|
+
throw new Error("Native generated image has no provider item identity");
|
|
294
|
+
}
|
|
295
|
+
identity = generatedImageIdentity({
|
|
296
|
+
...identityInput,
|
|
297
|
+
sourceStrategy: "native_hosted",
|
|
298
|
+
providerItemId: input.output.providerItemId,
|
|
299
|
+
});
|
|
300
|
+
} else {
|
|
301
|
+
if (input.output.providerItemId !== null) {
|
|
302
|
+
throw new Error("Adapter-generated image cannot carry a native provider item identity");
|
|
303
|
+
}
|
|
304
|
+
identity = generatedImageIdentity({
|
|
305
|
+
...identityInput,
|
|
306
|
+
sourceStrategy: "provider_adapter",
|
|
307
|
+
providerItemId: null,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
const filename = `generated-image-${identity.artifactId}.${image.extension}`;
|
|
311
|
+
const sandboxPath = `/workspace/generated-images/${filename}`;
|
|
312
|
+
const objectKey = `workspaces/${input.workspaceId}/files/${identity.artifactId}/generated/${filename}`;
|
|
313
|
+
const prepared = await prepareGeneratedImageArtifact(input.db, {
|
|
314
|
+
artifactId: identity.artifactId,
|
|
315
|
+
uploadId: identity.uploadId,
|
|
316
|
+
accountId: input.accountId,
|
|
317
|
+
workspaceId: input.workspaceId,
|
|
318
|
+
sessionId: input.sessionId,
|
|
319
|
+
turnId: input.turnId,
|
|
320
|
+
attemptId: input.attemptId,
|
|
321
|
+
settlementKey: identity.settlementKey,
|
|
322
|
+
toolCallId: input.output.toolCallId,
|
|
323
|
+
sourceStrategy: input.sourceStrategy,
|
|
324
|
+
providerId: input.providerId,
|
|
325
|
+
providerBindingHash: input.providerBindingHash,
|
|
326
|
+
providerItemId: input.output.providerItemId,
|
|
327
|
+
mediaType: image.mediaType,
|
|
328
|
+
sizeBytes: image.sizeBytes,
|
|
329
|
+
sha256: image.sha256,
|
|
330
|
+
width: image.width,
|
|
331
|
+
height: image.height,
|
|
332
|
+
sandboxPath,
|
|
333
|
+
filename,
|
|
334
|
+
safeFilename: filename,
|
|
335
|
+
bucket: input.objectStorage.bucket,
|
|
336
|
+
objectKey,
|
|
337
|
+
uploadExpiresAt: new Date((input.now ?? new Date()).getTime() + UPLOAD_INTENT_TTL_MS),
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
if (prepared.artifact.status === "ready") {
|
|
341
|
+
await verifyReadyGeneratedImage(input.objectStorage, prepared.artifact, image);
|
|
342
|
+
return retainedGeneratedImageFromArtifact(prepared.artifact);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
try {
|
|
346
|
+
// A crash may leave the file complete while the correlation row is pending.
|
|
347
|
+
// Verify and settle that state without rewriting the object.
|
|
348
|
+
if (
|
|
349
|
+
prepared.artifact.file.status !== "ready" ||
|
|
350
|
+
prepared.artifact.uploadStatus !== "completed"
|
|
351
|
+
) {
|
|
352
|
+
if (!(await input.objectStorage.fileExists(prepared.artifact.file))) {
|
|
353
|
+
await input.objectStorage.putObject({
|
|
354
|
+
key: objectKey,
|
|
355
|
+
contentType: image.mediaType,
|
|
356
|
+
body: image.bytes,
|
|
357
|
+
sha256: image.sha256,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
assertStoredGeneratedImageHead(
|
|
361
|
+
await input.objectStorage.headFile(prepared.artifact.file),
|
|
362
|
+
image,
|
|
363
|
+
);
|
|
364
|
+
await completeFileUpload(input.db, input.workspaceId, identity.uploadId);
|
|
365
|
+
} else {
|
|
366
|
+
await verifyReadyGeneratedImage(input.objectStorage, prepared.artifact, image);
|
|
367
|
+
}
|
|
368
|
+
const settled = await settleGeneratedImageArtifactReady(input.db, {
|
|
369
|
+
accountId: input.accountId,
|
|
370
|
+
workspaceId: input.workspaceId,
|
|
371
|
+
artifactId: identity.artifactId,
|
|
372
|
+
settlementKey: identity.settlementKey,
|
|
373
|
+
});
|
|
374
|
+
return retainedGeneratedImageFromArtifact(settled);
|
|
375
|
+
} catch (error) {
|
|
376
|
+
await recordGeneratedImageArtifactError(input.db, {
|
|
377
|
+
accountId: input.accountId,
|
|
378
|
+
workspaceId: input.workspaceId,
|
|
379
|
+
artifactId: identity.artifactId,
|
|
380
|
+
settlementKey: identity.settlementKey,
|
|
381
|
+
error: error instanceof Error ? error.message : String(error),
|
|
382
|
+
}).catch(() => undefined);
|
|
383
|
+
throw error;
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Replace provider bytes before they can enter event/history serialization. */
|
|
388
|
+
export function compactGeneratedImageHistory(
|
|
389
|
+
history: Array<Record<string, unknown>>,
|
|
390
|
+
receiptsByProviderItemId: ReadonlyMap<string, GeneratedImageReceipt>,
|
|
391
|
+
): Array<Record<string, unknown>> {
|
|
392
|
+
if (receiptsByProviderItemId.size === 0) return history;
|
|
393
|
+
return history.map((item) => {
|
|
394
|
+
if (item.type !== "hosted_tool_call" || item.name !== "image_generation_call") return item;
|
|
395
|
+
const id = typeof item.id === "string" ? item.id : null;
|
|
396
|
+
const receipt = id ? receiptsByProviderItemId.get(id) : undefined;
|
|
397
|
+
if (!receipt) return item;
|
|
398
|
+
const existingReceipt = generatedImageReceiptFromUnknown(item.output);
|
|
399
|
+
const providerDataContainsBytes =
|
|
400
|
+
isRecord(item.providerData) && typeof item.providerData.result === "string";
|
|
401
|
+
if (
|
|
402
|
+
existingReceipt?.artifact.artifactId === receipt.artifact.artifactId &&
|
|
403
|
+
!providerDataContainsBytes
|
|
404
|
+
) {
|
|
405
|
+
return item;
|
|
406
|
+
}
|
|
407
|
+
return {
|
|
408
|
+
...item,
|
|
409
|
+
output: receipt,
|
|
410
|
+
...(item.providerData ? { providerData: withoutNativeImageResult(item.providerData) } : {}),
|
|
411
|
+
};
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export function compactGeneratedImageSdkEvent(
|
|
416
|
+
event: unknown,
|
|
417
|
+
receipt: GeneratedImageReceipt,
|
|
418
|
+
): unknown {
|
|
419
|
+
if (!event || typeof event !== "object") return event;
|
|
420
|
+
const streamEvent = event as Record<string, unknown>;
|
|
421
|
+
const item = streamEvent.item;
|
|
422
|
+
if (!item || typeof item !== "object" || Array.isArray(item)) return event;
|
|
423
|
+
const itemRecord = item as Record<string, unknown>;
|
|
424
|
+
const raw = itemRecord.rawItem;
|
|
425
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return event;
|
|
426
|
+
const rawRecord = raw as Record<string, unknown>;
|
|
427
|
+
if (rawRecord.type !== "hosted_tool_call" || rawRecord.name !== "image_generation_call") {
|
|
428
|
+
return event;
|
|
429
|
+
}
|
|
430
|
+
return {
|
|
431
|
+
...streamEvent,
|
|
432
|
+
item: {
|
|
433
|
+
...itemRecord,
|
|
434
|
+
rawItem: {
|
|
435
|
+
...rawRecord,
|
|
436
|
+
output: receipt,
|
|
437
|
+
...(rawRecord.providerData
|
|
438
|
+
? { providerData: withoutNativeImageResult(rawRecord.providerData) }
|
|
439
|
+
: {}),
|
|
440
|
+
},
|
|
441
|
+
},
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
export function collectGeneratedImageReceipts(
|
|
446
|
+
history: ReadonlyArray<Record<string, unknown>>,
|
|
447
|
+
target: Map<string, GeneratedImageReceipt> = new Map(),
|
|
448
|
+
): Map<string, GeneratedImageReceipt> {
|
|
449
|
+
for (const item of history) {
|
|
450
|
+
if (item.type !== "hosted_tool_call" || item.name !== "image_generation_call") continue;
|
|
451
|
+
if (typeof item.id !== "string") continue;
|
|
452
|
+
const receipt = generatedImageReceiptFromUnknown(item.output);
|
|
453
|
+
if (receipt) target.set(item.id, receipt);
|
|
454
|
+
}
|
|
455
|
+
return target;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export function collectGeneratedImageArtifactReceipts(
|
|
459
|
+
history: ReadonlyArray<Record<string, unknown>>,
|
|
460
|
+
target: Map<string, GeneratedImageReceipt> = new Map(),
|
|
461
|
+
): Map<string, GeneratedImageReceipt> {
|
|
462
|
+
for (const item of history) {
|
|
463
|
+
const candidates = Array.isArray(item.output) ? item.output : [item.output];
|
|
464
|
+
for (const candidate of candidates) {
|
|
465
|
+
const receipt = generatedImageReceiptFromUnknown(candidate);
|
|
466
|
+
if (receipt) target.set(receipt.artifact.artifactId, receipt);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
return target;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Project native image calls to one provider-neutral, cache-stable model fact.
|
|
474
|
+
* The canonical hosted-tool item remains available to the UI, but its provider
|
|
475
|
+
* id and compact receipt are never replayed as a counterfeit native result.
|
|
476
|
+
*/
|
|
477
|
+
export function projectGeneratedImageHistoryForModel(
|
|
478
|
+
history: Array<Record<string, unknown>>,
|
|
479
|
+
): Array<Record<string, unknown>> {
|
|
480
|
+
let changed = false;
|
|
481
|
+
const projected: Array<Record<string, unknown>> = [];
|
|
482
|
+
for (const item of history) {
|
|
483
|
+
if (item.type !== "hosted_tool_call" || item.name !== "image_generation_call") {
|
|
484
|
+
projected.push(item);
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
const receipt = generatedImageReceiptFromUnknown(item.output);
|
|
488
|
+
if (!receipt) {
|
|
489
|
+
projected.push(item);
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
changed = true;
|
|
493
|
+
projected.push(generatedImageModelMessage(receipt, "history"));
|
|
494
|
+
}
|
|
495
|
+
return changed ? projected : history;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Build the attempt-local RunState view consumed by the Agents SDK on resume.
|
|
500
|
+
*
|
|
501
|
+
* OpenAI serializes a hosted image call from `providerData.result`, not from
|
|
502
|
+
* the SDK item's generic `output`. Our durable state deliberately replaces
|
|
503
|
+
* those bytes with a compact receipt, so replaying the hosted item would send
|
|
504
|
+
* a counterfeit `result: null`. Convert it to the same provider-neutral fact
|
|
505
|
+
* used for ordinary history instead. Durable state is never modified.
|
|
506
|
+
*/
|
|
507
|
+
export function projectGeneratedImageRunStateForModel(serialized: string): string {
|
|
508
|
+
const parsed = parseRunState(serialized);
|
|
509
|
+
if (!parsed) return serialized;
|
|
510
|
+
let changed = false;
|
|
511
|
+
|
|
512
|
+
visitRunStateProtocolItems(parsed, (items) => {
|
|
513
|
+
const projected = projectGeneratedImageRunStateItemsForModel(items);
|
|
514
|
+
if (projected !== items) {
|
|
515
|
+
items.splice(0, items.length, ...projected);
|
|
516
|
+
changed = true;
|
|
517
|
+
}
|
|
518
|
+
});
|
|
519
|
+
visitRunStateItemWrappers(parsed, (wrappers) => {
|
|
520
|
+
for (let index = 0; index < wrappers.length; index += 1) {
|
|
521
|
+
const projected = projectGeneratedImageRunItemWrapper(wrappers[index]!);
|
|
522
|
+
if (projected !== wrappers[index]) {
|
|
523
|
+
wrappers[index] = projected;
|
|
524
|
+
changed = true;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
});
|
|
528
|
+
|
|
529
|
+
return changed ? JSON.stringify(parsed) : serialized;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
function projectGeneratedImageRunStateItemsForModel(
|
|
533
|
+
items: Array<Record<string, unknown>>,
|
|
534
|
+
): Array<Record<string, unknown>> {
|
|
535
|
+
let changed = false;
|
|
536
|
+
const projected = items.map((item) => {
|
|
537
|
+
const receipt = generatedImageReceiptFromHostedItem(item);
|
|
538
|
+
if (!receipt) return item;
|
|
539
|
+
changed = true;
|
|
540
|
+
return generatedImageModelMessage(receipt, "run_state");
|
|
541
|
+
});
|
|
542
|
+
return changed ? projected : items;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
export function compactGeneratedImageRunState(
|
|
546
|
+
serialized: string,
|
|
547
|
+
receiptsByProviderItemId: ReadonlyMap<string, GeneratedImageReceipt>,
|
|
548
|
+
): string {
|
|
549
|
+
if (receiptsByProviderItemId.size === 0) return serialized;
|
|
550
|
+
let parsed: Record<string, unknown>;
|
|
551
|
+
try {
|
|
552
|
+
const value: unknown = JSON.parse(serialized);
|
|
553
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return serialized;
|
|
554
|
+
parsed = value as Record<string, unknown>;
|
|
555
|
+
} catch {
|
|
556
|
+
return serialized;
|
|
557
|
+
}
|
|
558
|
+
let changed = false;
|
|
559
|
+
visitRunStateProtocolItems(parsed, (items) => {
|
|
560
|
+
const compacted = compactGeneratedImageRunStateItems(items, receiptsByProviderItemId);
|
|
561
|
+
if (compacted.some((item, index) => item !== items[index])) {
|
|
562
|
+
items.splice(0, items.length, ...compacted);
|
|
563
|
+
changed = true;
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
visitRunStateItemWrappers(parsed, (wrappers) => {
|
|
567
|
+
for (const wrapper of wrappers) {
|
|
568
|
+
if (!isRecord(wrapper) || !isRecord(wrapper.rawItem)) continue;
|
|
569
|
+
const compacted = compactGeneratedImageRunStateItems(
|
|
570
|
+
[wrapper.rawItem],
|
|
571
|
+
receiptsByProviderItemId,
|
|
572
|
+
)[0];
|
|
573
|
+
if (compacted && compacted !== wrapper.rawItem) {
|
|
574
|
+
wrapper.rawItem = compacted;
|
|
575
|
+
changed = true;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
return changed ? JSON.stringify(parsed) : serialized;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
function compactGeneratedImageRunStateItems(
|
|
583
|
+
items: Array<Record<string, unknown>>,
|
|
584
|
+
receiptsByProviderItemId: ReadonlyMap<string, GeneratedImageReceipt>,
|
|
585
|
+
): Array<Record<string, unknown>> {
|
|
586
|
+
const compacted = compactGeneratedImageHistory(items, receiptsByProviderItemId);
|
|
587
|
+
let serializedReceipt = false;
|
|
588
|
+
const serialized = compacted.map((item) => {
|
|
589
|
+
const receipt = generatedImageReceiptFromUnknown(item.output);
|
|
590
|
+
if (!receipt || typeof item.output === "string") return item;
|
|
591
|
+
serializedReceipt = true;
|
|
592
|
+
return { ...item, output: JSON.stringify(receipt) };
|
|
593
|
+
});
|
|
594
|
+
return serializedReceipt ? serialized : compacted;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
export function collectGeneratedImageRunStateArtifactReceipts(
|
|
598
|
+
serialized: string,
|
|
599
|
+
target: Map<string, GeneratedImageReceipt> = new Map(),
|
|
600
|
+
): Map<string, GeneratedImageReceipt> {
|
|
601
|
+
try {
|
|
602
|
+
const value: unknown = JSON.parse(serialized);
|
|
603
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return target;
|
|
604
|
+
visitRunStateProtocolItems(value as Record<string, unknown>, (items) => {
|
|
605
|
+
collectGeneratedImageArtifactReceipts(items, target);
|
|
606
|
+
});
|
|
607
|
+
visitRunStateItemWrappers(value as Record<string, unknown>, (wrappers) => {
|
|
608
|
+
for (const wrapper of wrappers) {
|
|
609
|
+
if (isRecord(wrapper) && isRecord(wrapper.rawItem)) {
|
|
610
|
+
collectGeneratedImageArtifactReceipts([wrapper.rawItem], target);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
});
|
|
614
|
+
} catch {
|
|
615
|
+
// A provider-owned or legacy state format is opaque to this collector.
|
|
616
|
+
}
|
|
617
|
+
return target;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
export function generatedImageReceiptFromUnknown(value: unknown): GeneratedImageReceipt | null {
|
|
621
|
+
if (typeof value === "string" && value.length <= 4_096) {
|
|
622
|
+
try {
|
|
623
|
+
return generatedImageReceiptFromUnknown(JSON.parse(value));
|
|
624
|
+
} catch {
|
|
625
|
+
return null;
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
// Function-tool results are durably normalized to the Agents SDK's typed
|
|
629
|
+
// text envelope. Unwrap that canonical transport shape before validating the
|
|
630
|
+
// closed receipt; otherwise a later turn sees the path in model history but
|
|
631
|
+
// cannot discover the artifact that must be copied into its sandbox.
|
|
632
|
+
if (
|
|
633
|
+
isRecord(value) &&
|
|
634
|
+
value.type === "text" &&
|
|
635
|
+
typeof value.text === "string" &&
|
|
636
|
+
value.text.length <= 4_096
|
|
637
|
+
) {
|
|
638
|
+
return generatedImageReceiptFromUnknown(value.text);
|
|
639
|
+
}
|
|
640
|
+
const parsed = GeneratedImageReceiptSchema.safeParse(value);
|
|
641
|
+
return parsed.success ? parsed.data : null;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function visitRunStateProtocolItems(
|
|
645
|
+
root: Record<string, unknown>,
|
|
646
|
+
visitor: (items: Array<Record<string, unknown>>) => void,
|
|
647
|
+
): void {
|
|
648
|
+
if (Array.isArray(root.originalInput)) {
|
|
649
|
+
visitor(root.originalInput as Array<Record<string, unknown>>);
|
|
650
|
+
}
|
|
651
|
+
if (Array.isArray(root.modelResponses)) {
|
|
652
|
+
for (const response of root.modelResponses) {
|
|
653
|
+
if (!response || typeof response !== "object" || Array.isArray(response)) continue;
|
|
654
|
+
const output = (response as Record<string, unknown>).output;
|
|
655
|
+
if (Array.isArray(output)) visitor(output as Array<Record<string, unknown>>);
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
if (root.lastModelResponse && typeof root.lastModelResponse === "object") {
|
|
659
|
+
const output = (root.lastModelResponse as Record<string, unknown>).output;
|
|
660
|
+
if (Array.isArray(output)) visitor(output as Array<Record<string, unknown>>);
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
function visitRunStateItemWrappers(
|
|
665
|
+
root: Record<string, unknown>,
|
|
666
|
+
visitor: (items: Array<Record<string, unknown>>) => void,
|
|
667
|
+
): void {
|
|
668
|
+
if (Array.isArray(root.generatedItems)) {
|
|
669
|
+
visitor(root.generatedItems as Array<Record<string, unknown>>);
|
|
670
|
+
}
|
|
671
|
+
const processed = root.lastProcessedResponse;
|
|
672
|
+
if (isRecord(processed) && Array.isArray(processed.newItems)) {
|
|
673
|
+
visitor(processed.newItems as Array<Record<string, unknown>>);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
function projectGeneratedImageRunItemWrapper(
|
|
678
|
+
value: Record<string, unknown>,
|
|
679
|
+
): Record<string, unknown> {
|
|
680
|
+
if (value.type !== "tool_call_item" || !isRecord(value.rawItem) || !isRecord(value.agent)) {
|
|
681
|
+
return value;
|
|
682
|
+
}
|
|
683
|
+
const receipt = generatedImageReceiptFromHostedItem(value.rawItem);
|
|
684
|
+
if (!receipt) return value;
|
|
685
|
+
return {
|
|
686
|
+
type: "message_output_item",
|
|
687
|
+
rawItem: generatedImageModelMessage(receipt, "run_state"),
|
|
688
|
+
agent: value.agent,
|
|
689
|
+
};
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
function generatedImageReceiptFromHostedItem(
|
|
693
|
+
item: Record<string, unknown>,
|
|
694
|
+
): GeneratedImageReceipt | null {
|
|
695
|
+
if (item.type !== "hosted_tool_call" || item.name !== "image_generation_call") {
|
|
696
|
+
return null;
|
|
697
|
+
}
|
|
698
|
+
return generatedImageReceiptFromUnknown(item.output);
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
function generatedImageModelFact(receipt: GeneratedImageReceipt): string {
|
|
702
|
+
return [
|
|
703
|
+
"Generated image artifact:",
|
|
704
|
+
`Artifact ID: ${receipt.artifact.artifactId}`,
|
|
705
|
+
`Sandbox path: ${receipt.sandboxPath}`,
|
|
706
|
+
`Format: ${receipt.artifact.contentType}`,
|
|
707
|
+
`Dimensions: ${receipt.artifact.dimensions?.width ?? "?"}x${receipt.artifact.dimensions?.height ?? "?"}`,
|
|
708
|
+
].join("\n");
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
function generatedImageModelMessage(
|
|
712
|
+
receipt: GeneratedImageReceipt,
|
|
713
|
+
shape: "history" | "run_state",
|
|
714
|
+
): Record<string, unknown> {
|
|
715
|
+
const text = generatedImageModelFact(receipt);
|
|
716
|
+
return shape === "history"
|
|
717
|
+
? { type: "message", role: "assistant", content: text }
|
|
718
|
+
: {
|
|
719
|
+
type: "message",
|
|
720
|
+
role: "assistant",
|
|
721
|
+
status: "completed",
|
|
722
|
+
content: [{ type: "output_text", text }],
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
function parseRunState(serialized: string): Record<string, unknown> | null {
|
|
727
|
+
try {
|
|
728
|
+
const parsed: unknown = JSON.parse(serialized);
|
|
729
|
+
return isRecord(parsed) ? parsed : null;
|
|
730
|
+
} catch {
|
|
731
|
+
return null;
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
736
|
+
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
export function retainedGeneratedImageFromArtifact(
|
|
740
|
+
artifact: GeneratedImageArtifact,
|
|
741
|
+
): RetainedGeneratedImage {
|
|
742
|
+
const reference = retainedGeneratedImageReferenceFromFile({
|
|
743
|
+
...artifact.file,
|
|
744
|
+
width: artifact.width,
|
|
745
|
+
height: artifact.height,
|
|
746
|
+
});
|
|
747
|
+
if (!reference)
|
|
748
|
+
throw new Error(`Ready generated image receipt is invalid: ${artifact.artifactId}`);
|
|
749
|
+
return {
|
|
750
|
+
artifact,
|
|
751
|
+
receipt: {
|
|
752
|
+
type: GENERATED_IMAGE_MARKER,
|
|
753
|
+
artifact: reference,
|
|
754
|
+
sandboxPath: artifact.sandboxPath,
|
|
755
|
+
},
|
|
756
|
+
};
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
/** Verify a recovered ready correlation against the immutable object metadata. */
|
|
760
|
+
export async function verifyReadyGeneratedImageArtifact(
|
|
761
|
+
storage: ObjectStorage,
|
|
762
|
+
artifact: GeneratedImageArtifact,
|
|
763
|
+
): Promise<void> {
|
|
764
|
+
if (artifact.status !== "ready" || artifact.file.status !== "ready") {
|
|
765
|
+
throw new Error("Generated image artifact is not durably ready");
|
|
766
|
+
}
|
|
767
|
+
assertStoredGeneratedImageHead(await storage.headFile(artifact.file), {
|
|
768
|
+
sizeBytes: artifact.sizeBytes,
|
|
769
|
+
mediaType: artifact.mediaType as GeneratedImageMediaType,
|
|
770
|
+
sha256: artifact.sha256,
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
/**
|
|
775
|
+
* Finish a crash-interrupted upload from immutable DB/object metadata without
|
|
776
|
+
* provider replay. A missing object is not proof that generation failed: the
|
|
777
|
+
* paid outcome remains unknown and the caller may check again later.
|
|
778
|
+
*/
|
|
779
|
+
export async function recoverGeneratedImageArtifact(input: {
|
|
780
|
+
db: Database;
|
|
781
|
+
storage: ObjectStorage;
|
|
782
|
+
accountId: string;
|
|
783
|
+
workspaceId: string;
|
|
784
|
+
artifact: GeneratedImageArtifact;
|
|
785
|
+
}): Promise<GeneratedImageArtifact | null> {
|
|
786
|
+
const { artifact } = input;
|
|
787
|
+
if (artifact.status === "ready") {
|
|
788
|
+
await verifyReadyGeneratedImageArtifact(input.storage, artifact);
|
|
789
|
+
return artifact;
|
|
790
|
+
}
|
|
791
|
+
if (artifact.status !== "pending") return null;
|
|
792
|
+
if (!(await input.storage.fileExists(artifact.file))) return null;
|
|
793
|
+
assertStoredGeneratedImageHead(await input.storage.headFile(artifact.file), {
|
|
794
|
+
sizeBytes: artifact.sizeBytes,
|
|
795
|
+
mediaType: artifact.mediaType as GeneratedImageMediaType,
|
|
796
|
+
sha256: artifact.sha256,
|
|
797
|
+
});
|
|
798
|
+
if (artifact.file.status !== "ready" || artifact.uploadStatus !== "completed") {
|
|
799
|
+
if (!artifact.uploadId) return null;
|
|
800
|
+
await completeFileUpload(input.db, input.workspaceId, artifact.uploadId);
|
|
801
|
+
}
|
|
802
|
+
return await settleGeneratedImageArtifactReady(input.db, {
|
|
803
|
+
accountId: input.accountId,
|
|
804
|
+
workspaceId: input.workspaceId,
|
|
805
|
+
artifactId: artifact.artifactId,
|
|
806
|
+
settlementKey: artifact.settlementKey,
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
async function verifyReadyGeneratedImage(
|
|
811
|
+
storage: ObjectStorage,
|
|
812
|
+
artifact: GeneratedImageArtifact,
|
|
813
|
+
expected: ValidatedGeneratedImage,
|
|
814
|
+
): Promise<void> {
|
|
815
|
+
assertStoredGeneratedImageHead(await storage.headFile(artifact.file), expected);
|
|
816
|
+
if (
|
|
817
|
+
artifact.sizeBytes !== expected.sizeBytes ||
|
|
818
|
+
artifact.sha256 !== expected.sha256 ||
|
|
819
|
+
artifact.mediaType !== expected.mediaType ||
|
|
820
|
+
artifact.width !== expected.width ||
|
|
821
|
+
artifact.height !== expected.height
|
|
822
|
+
) {
|
|
823
|
+
throw new Error("ready generated image metadata does not match provider bytes");
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
function assertStoredGeneratedImageHead(
|
|
828
|
+
head: ObjectHead,
|
|
829
|
+
expected: Pick<ValidatedGeneratedImage, "sizeBytes" | "mediaType" | "sha256">,
|
|
830
|
+
): void {
|
|
831
|
+
if (head.ContentLength !== expected.sizeBytes)
|
|
832
|
+
throw new Error("generated image object size mismatch");
|
|
833
|
+
if (head.ContentType !== expected.mediaType)
|
|
834
|
+
throw new Error("generated image object MIME mismatch");
|
|
835
|
+
if (head.Metadata?.sha256 !== expected.sha256) {
|
|
836
|
+
throw new Error("generated image object SHA-256 metadata mismatch");
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
function uuidFromDigest(digest: string, startByte: number): string {
|
|
841
|
+
const source = Buffer.from(digest, "hex");
|
|
842
|
+
const bytes = Uint8Array.from(source.subarray(startByte, startByte + 16));
|
|
843
|
+
bytes[6] = (bytes[6]! & 0x0f) | 0x80;
|
|
844
|
+
bytes[8] = (bytes[8]! & 0x3f) | 0x80;
|
|
845
|
+
const hex = Buffer.from(bytes).toString("hex");
|
|
846
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
function base64Sextet(code: number): number {
|
|
850
|
+
if (code >= 0x41 && code <= 0x5a) return code - 0x41;
|
|
851
|
+
if (code >= 0x61 && code <= 0x7a) return code - 0x61 + 26;
|
|
852
|
+
if (code >= 0x30 && code <= 0x39) return code - 0x30 + 52;
|
|
853
|
+
return code === 0x2b ? 62 : 63;
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
function withoutNativeImageResult(value: unknown): unknown {
|
|
857
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return value;
|
|
858
|
+
const { result: _result, ...rest } = value as Record<string, unknown>;
|
|
859
|
+
return rest;
|
|
860
|
+
}
|