@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,333 @@
|
|
|
1
|
+
import {
|
|
2
|
+
beginImageGenerationOperation,
|
|
3
|
+
completeImageGenerationOperation,
|
|
4
|
+
getGeneratedImageArtifact,
|
|
5
|
+
markImageGenerationOperationOutcomeUnknown,
|
|
6
|
+
markImageGenerationOperationRetentionFailed,
|
|
7
|
+
prepareImageGenerationOperation,
|
|
8
|
+
type Database,
|
|
9
|
+
type ImageGenerationOperationStatus,
|
|
10
|
+
} from "@opengeni/db";
|
|
11
|
+
import type { ObjectStorage } from "@opengeni/storage";
|
|
12
|
+
import { createHash } from "node:crypto";
|
|
13
|
+
import {
|
|
14
|
+
generatedImageIdentity,
|
|
15
|
+
recoverGeneratedImageArtifact,
|
|
16
|
+
retainGeneratedImage,
|
|
17
|
+
retainedGeneratedImageFromArtifact,
|
|
18
|
+
verifyReadyGeneratedImageArtifact,
|
|
19
|
+
type GeneratedImageOutput,
|
|
20
|
+
type GeneratedImageReceipt,
|
|
21
|
+
} from "./generated-images";
|
|
22
|
+
|
|
23
|
+
export class ImageGenerationOutcomeUnknownError extends Error {
|
|
24
|
+
constructor(readonly operationId: string) {
|
|
25
|
+
super(
|
|
26
|
+
"The image provider request may have completed, but no durable result was recovered. It was not retried to avoid duplicate work or charges.",
|
|
27
|
+
);
|
|
28
|
+
this.name = "ImageGenerationOutcomeUnknownError";
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class ImageGenerationRetentionFailedError extends Error {
|
|
33
|
+
constructor(
|
|
34
|
+
readonly operationId: string,
|
|
35
|
+
cause?: unknown,
|
|
36
|
+
) {
|
|
37
|
+
super(
|
|
38
|
+
"The image was generated, but OpenGeni could not save it. The paid provider request was not repeated.",
|
|
39
|
+
cause === undefined ? undefined : { cause },
|
|
40
|
+
);
|
|
41
|
+
this.name = "ImageGenerationRetentionFailedError";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type ImageGenerationOperationPorts = {
|
|
46
|
+
prepare: typeof prepareImageGenerationOperation;
|
|
47
|
+
begin: typeof beginImageGenerationOperation;
|
|
48
|
+
retain: typeof retainGeneratedImage;
|
|
49
|
+
complete: typeof completeImageGenerationOperation;
|
|
50
|
+
markOutcomeUnknown: typeof markImageGenerationOperationOutcomeUnknown;
|
|
51
|
+
markRetentionFailed: typeof markImageGenerationOperationRetentionFailed;
|
|
52
|
+
recover: typeof recoverCompletedImage;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const imageGenerationOperationPorts: ImageGenerationOperationPorts = {
|
|
56
|
+
prepare: prepareImageGenerationOperation,
|
|
57
|
+
begin: beginImageGenerationOperation,
|
|
58
|
+
retain: retainGeneratedImage,
|
|
59
|
+
complete: completeImageGenerationOperation,
|
|
60
|
+
markOutcomeUnknown: markImageGenerationOperationOutcomeUnknown,
|
|
61
|
+
markRetentionFailed: markImageGenerationOperationRetentionFailed,
|
|
62
|
+
recover: recoverCompletedImage,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Durable admission fence shared by every client-executed image provider.
|
|
67
|
+
* Provider execution is permitted at most once after the prepared ->
|
|
68
|
+
* provider_started transition. A crash at that boundary may conservatively
|
|
69
|
+
* leave the outcome unknown; a retry may recover a ready artifact, but never
|
|
70
|
+
* blindly replays the paid operation.
|
|
71
|
+
*/
|
|
72
|
+
export type ExecuteImageGenerationOperationInput = {
|
|
73
|
+
db: Database;
|
|
74
|
+
objectStorage: ObjectStorage | null;
|
|
75
|
+
accountId: string;
|
|
76
|
+
workspaceId: string;
|
|
77
|
+
sessionId: string;
|
|
78
|
+
turnId: string;
|
|
79
|
+
attemptId: string;
|
|
80
|
+
toolCallId: string;
|
|
81
|
+
providerId: string;
|
|
82
|
+
providerBindingHash: string;
|
|
83
|
+
modelId: string;
|
|
84
|
+
prompt: string;
|
|
85
|
+
referenceDigests?: readonly Readonly<{ mediaType: string; sha256: string }>[];
|
|
86
|
+
generate: () => Promise<GeneratedImageOutput>;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export async function executeImageGenerationOperation(
|
|
90
|
+
input: ExecuteImageGenerationOperationInput,
|
|
91
|
+
ports: ImageGenerationOperationPorts = imageGenerationOperationPorts,
|
|
92
|
+
): Promise<GeneratedImageReceipt> {
|
|
93
|
+
const identity = imageGenerationOperationIdentity({
|
|
94
|
+
workspaceId: input.workspaceId,
|
|
95
|
+
sessionId: input.sessionId,
|
|
96
|
+
turnId: input.turnId,
|
|
97
|
+
providerId: input.providerId,
|
|
98
|
+
providerBindingHash: input.providerBindingHash,
|
|
99
|
+
modelId: input.modelId,
|
|
100
|
+
prompt: input.prompt,
|
|
101
|
+
...(input.referenceDigests ? { referenceDigests: input.referenceDigests } : {}),
|
|
102
|
+
toolCallId: input.toolCallId,
|
|
103
|
+
});
|
|
104
|
+
const prepared = await ports.prepare(input.db, {
|
|
105
|
+
id: identity.operationId,
|
|
106
|
+
accountId: input.accountId,
|
|
107
|
+
workspaceId: input.workspaceId,
|
|
108
|
+
sessionId: input.sessionId,
|
|
109
|
+
turnId: input.turnId,
|
|
110
|
+
attemptId: input.attemptId,
|
|
111
|
+
operationKey: identity.operationKey,
|
|
112
|
+
toolCallId: input.toolCallId,
|
|
113
|
+
providerId: input.providerId,
|
|
114
|
+
providerBindingHash: input.providerBindingHash,
|
|
115
|
+
modelId: input.modelId,
|
|
116
|
+
requestDigest: identity.requestDigest,
|
|
117
|
+
expectedArtifactId: identity.artifactId,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
if (prepared.operation.status !== "prepared") {
|
|
121
|
+
const recovered = await ports.recover(input, {
|
|
122
|
+
artifactId: identity.artifactId,
|
|
123
|
+
operationId: identity.operationId,
|
|
124
|
+
operationKey: identity.operationKey,
|
|
125
|
+
});
|
|
126
|
+
if (recovered) return recovered;
|
|
127
|
+
throw unrecoveredOperationError(prepared.operation.status, identity.operationId);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const begun = await ports.begin(input.db, {
|
|
131
|
+
accountId: input.accountId,
|
|
132
|
+
workspaceId: input.workspaceId,
|
|
133
|
+
operationId: identity.operationId,
|
|
134
|
+
operationKey: identity.operationKey,
|
|
135
|
+
});
|
|
136
|
+
if (!begun.started || begun.operation.status !== "provider_started") {
|
|
137
|
+
const recovered = await ports.recover(input, {
|
|
138
|
+
artifactId: identity.artifactId,
|
|
139
|
+
operationId: identity.operationId,
|
|
140
|
+
operationKey: identity.operationKey,
|
|
141
|
+
});
|
|
142
|
+
if (recovered) return recovered;
|
|
143
|
+
throw unrecoveredOperationError(begun.operation.status, identity.operationId);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
let output: GeneratedImageOutput;
|
|
147
|
+
try {
|
|
148
|
+
output = await input.generate();
|
|
149
|
+
} catch (error) {
|
|
150
|
+
await ports
|
|
151
|
+
.markOutcomeUnknown(input.db, {
|
|
152
|
+
accountId: input.accountId,
|
|
153
|
+
workspaceId: input.workspaceId,
|
|
154
|
+
operationId: identity.operationId,
|
|
155
|
+
operationKey: identity.operationKey,
|
|
156
|
+
error: error instanceof Error ? error.message : String(error),
|
|
157
|
+
})
|
|
158
|
+
.catch(() => undefined);
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
try {
|
|
163
|
+
if (output.toolCallId !== input.toolCallId || output.providerItemId !== null) {
|
|
164
|
+
throw new Error("Client image provider returned a mismatched operation identity");
|
|
165
|
+
}
|
|
166
|
+
const retained = await ports.retain({
|
|
167
|
+
db: input.db,
|
|
168
|
+
objectStorage: input.objectStorage,
|
|
169
|
+
accountId: input.accountId,
|
|
170
|
+
workspaceId: input.workspaceId,
|
|
171
|
+
sessionId: input.sessionId,
|
|
172
|
+
turnId: input.turnId,
|
|
173
|
+
attemptId: input.attemptId,
|
|
174
|
+
sourceStrategy: "provider_adapter",
|
|
175
|
+
providerId: input.providerId,
|
|
176
|
+
providerBindingHash: input.providerBindingHash,
|
|
177
|
+
output,
|
|
178
|
+
});
|
|
179
|
+
await ports.complete(input.db, {
|
|
180
|
+
accountId: input.accountId,
|
|
181
|
+
workspaceId: input.workspaceId,
|
|
182
|
+
operationId: identity.operationId,
|
|
183
|
+
operationKey: identity.operationKey,
|
|
184
|
+
});
|
|
185
|
+
return retained.receipt;
|
|
186
|
+
} catch (error) {
|
|
187
|
+
await ports
|
|
188
|
+
.markRetentionFailed(input.db, {
|
|
189
|
+
accountId: input.accountId,
|
|
190
|
+
workspaceId: input.workspaceId,
|
|
191
|
+
operationId: identity.operationId,
|
|
192
|
+
operationKey: identity.operationKey,
|
|
193
|
+
error: error instanceof Error ? error.message : String(error),
|
|
194
|
+
})
|
|
195
|
+
.catch(() => undefined);
|
|
196
|
+
throw new ImageGenerationRetentionFailedError(identity.operationId, error);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function unrecoveredOperationError(
|
|
201
|
+
status: ImageGenerationOperationStatus,
|
|
202
|
+
operationId: string,
|
|
203
|
+
): Error {
|
|
204
|
+
return status === "retention_failed"
|
|
205
|
+
? new ImageGenerationRetentionFailedError(operationId)
|
|
206
|
+
: new ImageGenerationOutcomeUnknownError(operationId);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function imageProviderBindingHash(providerId: string, credentialIdentity: string): string {
|
|
210
|
+
if (!providerId.trim() || !credentialIdentity.trim()) {
|
|
211
|
+
throw new Error("Image provider binding identity is empty");
|
|
212
|
+
}
|
|
213
|
+
return digest("opengeni:image-provider-binding:v1\0", providerId, credentialIdentity);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Stable across worker/Temporal attempt retries; the attempt id is provenance, not identity. */
|
|
217
|
+
export function imageGenerationOperationIdentity(input: {
|
|
218
|
+
workspaceId: string;
|
|
219
|
+
sessionId: string;
|
|
220
|
+
turnId: string;
|
|
221
|
+
toolCallId: string;
|
|
222
|
+
providerId: string;
|
|
223
|
+
providerBindingHash: string;
|
|
224
|
+
modelId: string;
|
|
225
|
+
prompt: string;
|
|
226
|
+
referenceDigests?: readonly Readonly<{ mediaType: string; sha256: string }>[];
|
|
227
|
+
}): {
|
|
228
|
+
operationId: string;
|
|
229
|
+
operationKey: string;
|
|
230
|
+
requestDigest: string;
|
|
231
|
+
artifactId: string;
|
|
232
|
+
} {
|
|
233
|
+
const referenceDigests = input.referenceDigests ?? [];
|
|
234
|
+
const requestDigest =
|
|
235
|
+
referenceDigests.length === 0
|
|
236
|
+
? digest("opengeni:image-generation-request:v1\0", input.modelId, input.prompt)
|
|
237
|
+
: digest(
|
|
238
|
+
"opengeni:image-generation-request:v2\0",
|
|
239
|
+
input.modelId,
|
|
240
|
+
input.prompt,
|
|
241
|
+
...referenceDigests.flatMap((reference) => [reference.mediaType, reference.sha256]),
|
|
242
|
+
);
|
|
243
|
+
const operationKey = digest(
|
|
244
|
+
"opengeni:image-generation-operation:v2\0",
|
|
245
|
+
input.workspaceId,
|
|
246
|
+
input.sessionId,
|
|
247
|
+
input.turnId,
|
|
248
|
+
input.toolCallId,
|
|
249
|
+
);
|
|
250
|
+
const artifact = generatedImageIdentity({
|
|
251
|
+
workspaceId: input.workspaceId,
|
|
252
|
+
sessionId: input.sessionId,
|
|
253
|
+
turnId: input.turnId,
|
|
254
|
+
sourceStrategy: "provider_adapter",
|
|
255
|
+
providerId: input.providerId,
|
|
256
|
+
providerBindingHash: input.providerBindingHash,
|
|
257
|
+
providerItemId: null,
|
|
258
|
+
toolCallId: input.toolCallId,
|
|
259
|
+
});
|
|
260
|
+
return {
|
|
261
|
+
operationId: uuidFromDigest(operationKey),
|
|
262
|
+
operationKey,
|
|
263
|
+
requestDigest,
|
|
264
|
+
artifactId: artifact.artifactId,
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async function recoverCompletedImage(
|
|
269
|
+
input: Pick<
|
|
270
|
+
Parameters<typeof executeImageGenerationOperation>[0],
|
|
271
|
+
| "db"
|
|
272
|
+
| "objectStorage"
|
|
273
|
+
| "accountId"
|
|
274
|
+
| "workspaceId"
|
|
275
|
+
| "sessionId"
|
|
276
|
+
| "turnId"
|
|
277
|
+
| "toolCallId"
|
|
278
|
+
| "providerId"
|
|
279
|
+
| "providerBindingHash"
|
|
280
|
+
>,
|
|
281
|
+
identity: { artifactId: string; operationId: string; operationKey: string },
|
|
282
|
+
): Promise<GeneratedImageReceipt | null> {
|
|
283
|
+
const artifact = await getGeneratedImageArtifact(
|
|
284
|
+
input.db,
|
|
285
|
+
input.workspaceId,
|
|
286
|
+
identity.artifactId,
|
|
287
|
+
);
|
|
288
|
+
if (!artifact) return null;
|
|
289
|
+
if (
|
|
290
|
+
artifact.sourceStrategy !== "provider_adapter" ||
|
|
291
|
+
artifact.sessionId !== input.sessionId ||
|
|
292
|
+
artifact.turnId !== input.turnId ||
|
|
293
|
+
artifact.toolCallId !== input.toolCallId ||
|
|
294
|
+
artifact.providerId !== input.providerId ||
|
|
295
|
+
artifact.providerBindingHash !== input.providerBindingHash ||
|
|
296
|
+
artifact.providerItemId !== null
|
|
297
|
+
) {
|
|
298
|
+
throw new Error("Recovered generated image does not match its provider operation");
|
|
299
|
+
}
|
|
300
|
+
if (!input.objectStorage) {
|
|
301
|
+
throw new Error("Recovered generated image requires configured object storage");
|
|
302
|
+
}
|
|
303
|
+
const recovered = await recoverGeneratedImageArtifact({
|
|
304
|
+
db: input.db,
|
|
305
|
+
storage: input.objectStorage,
|
|
306
|
+
accountId: input.accountId,
|
|
307
|
+
workspaceId: input.workspaceId,
|
|
308
|
+
artifact,
|
|
309
|
+
});
|
|
310
|
+
if (!recovered) return null;
|
|
311
|
+
await verifyReadyGeneratedImageArtifact(input.objectStorage, recovered);
|
|
312
|
+
await completeImageGenerationOperation(input.db, {
|
|
313
|
+
accountId: input.accountId,
|
|
314
|
+
workspaceId: input.workspaceId,
|
|
315
|
+
operationId: identity.operationId,
|
|
316
|
+
operationKey: identity.operationKey,
|
|
317
|
+
});
|
|
318
|
+
return retainedGeneratedImageFromArtifact(recovered).receipt;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function digest(prefix: string, ...values: string[]): string {
|
|
322
|
+
const hash = createHash("sha256").update(prefix);
|
|
323
|
+
for (const value of values) hash.update(value).update("\0");
|
|
324
|
+
return hash.digest("hex");
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function uuidFromDigest(digestValue: string): string {
|
|
328
|
+
const bytes = Uint8Array.from(Buffer.from(digestValue.slice(0, 32), "hex"));
|
|
329
|
+
bytes[6] = (bytes[6]! & 0x0f) | 0x80;
|
|
330
|
+
bytes[8] = (bytes[8]! & 0x3f) | 0x80;
|
|
331
|
+
const hex = Buffer.from(bytes).toString("hex");
|
|
332
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
333
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { type ImageGenerationReference, type FileAsset } from "@opengeni/contracts";
|
|
2
|
+
import { getGeneratedImageArtifact, requireFile, type Database } from "@opengeni/db";
|
|
3
|
+
import type { ObjectStorage } from "@opengeni/storage";
|
|
4
|
+
import { validateGeneratedImage, type GeneratedImageMediaType } from "./generated-images";
|
|
5
|
+
|
|
6
|
+
export const IMAGE_GENERATION_REFERENCE_MAX_BYTES = 30 * 1024 * 1024;
|
|
7
|
+
export const IMAGE_GENERATION_REFERENCES_MAX_TOTAL_BYTES = 64 * 1024 * 1024;
|
|
8
|
+
|
|
9
|
+
export type ResolvedImageGenerationReference = Readonly<{
|
|
10
|
+
mediaType: GeneratedImageMediaType;
|
|
11
|
+
bytes: Uint8Array;
|
|
12
|
+
sizeBytes: number;
|
|
13
|
+
sha256: string;
|
|
14
|
+
}>;
|
|
15
|
+
|
|
16
|
+
export type SandboxImageReferenceReader = (path: string, maxBytes: number) => Promise<Uint8Array>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolve ordered model-facing references without accepting arbitrary URLs.
|
|
20
|
+
* Stored files remain workspace-RLS scoped; sandbox reads stay rooted beneath
|
|
21
|
+
* /workspace. Provider adapters receive only validated immutable bytes.
|
|
22
|
+
*/
|
|
23
|
+
export async function resolveImageGenerationReferences(input: {
|
|
24
|
+
db: Database;
|
|
25
|
+
objectStorage: ObjectStorage;
|
|
26
|
+
workspaceId: string;
|
|
27
|
+
references: readonly ImageGenerationReference[];
|
|
28
|
+
readSandboxFile?: SandboxImageReferenceReader;
|
|
29
|
+
}): Promise<ResolvedImageGenerationReference[]> {
|
|
30
|
+
const resolved: ResolvedImageGenerationReference[] = [];
|
|
31
|
+
let totalBytes = 0;
|
|
32
|
+
|
|
33
|
+
for (const reference of input.references) {
|
|
34
|
+
const source = await referenceBytes(input, reference);
|
|
35
|
+
if (source.bytes.byteLength > IMAGE_GENERATION_REFERENCE_MAX_BYTES) {
|
|
36
|
+
throw new Error("Image reference exceeds the per-image byte limit");
|
|
37
|
+
}
|
|
38
|
+
totalBytes += source.bytes.byteLength;
|
|
39
|
+
if (totalBytes > IMAGE_GENERATION_REFERENCES_MAX_TOTAL_BYTES) {
|
|
40
|
+
throw new Error("Image references exceed the combined byte limit");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const image = validateGeneratedImage({ bytes: source.bytes });
|
|
44
|
+
if (source.file) assertStoredFileMatches(source.file, image);
|
|
45
|
+
resolved.push({
|
|
46
|
+
mediaType: image.mediaType,
|
|
47
|
+
bytes: source.bytes,
|
|
48
|
+
sizeBytes: image.sizeBytes,
|
|
49
|
+
sha256: image.sha256,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return resolved;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function referenceBytes(
|
|
57
|
+
input: {
|
|
58
|
+
db: Database;
|
|
59
|
+
objectStorage: ObjectStorage;
|
|
60
|
+
workspaceId: string;
|
|
61
|
+
readSandboxFile?: SandboxImageReferenceReader;
|
|
62
|
+
},
|
|
63
|
+
reference: ImageGenerationReference,
|
|
64
|
+
): Promise<{ bytes: Uint8Array; file?: FileAsset }> {
|
|
65
|
+
if (reference.kind === "sandbox_path") {
|
|
66
|
+
if (!input.readSandboxFile) {
|
|
67
|
+
throw new Error("Sandbox image references require an active sandbox");
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
bytes: await input.readSandboxFile(reference.path, IMAGE_GENERATION_REFERENCE_MAX_BYTES + 1),
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const file =
|
|
75
|
+
reference.kind === "file"
|
|
76
|
+
? await requireFile(input.db, input.workspaceId, reference.fileId)
|
|
77
|
+
: await artifactFile(input.db, input.workspaceId, reference.artifactId);
|
|
78
|
+
if (file.status !== "ready" || !file.sha256) {
|
|
79
|
+
throw new Error("Image reference file is not durably ready");
|
|
80
|
+
}
|
|
81
|
+
if (file.sizeBytes <= 0 || file.sizeBytes > IMAGE_GENERATION_REFERENCE_MAX_BYTES) {
|
|
82
|
+
throw new Error("Image reference file exceeds the supported size");
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
bytes: await readStoredFile(input.objectStorage, file),
|
|
86
|
+
file,
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async function readStoredFile(objectStorage: ObjectStorage, file: FileAsset): Promise<Uint8Array> {
|
|
91
|
+
const bytes = await objectStorage.getFileRange(file, {
|
|
92
|
+
start: 0,
|
|
93
|
+
end: file.sizeBytes - 1,
|
|
94
|
+
});
|
|
95
|
+
if (!bytes) throw new Error("Image reference file bytes are unavailable");
|
|
96
|
+
return bytes;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function artifactFile(
|
|
100
|
+
db: Database,
|
|
101
|
+
workspaceId: string,
|
|
102
|
+
artifactId: string,
|
|
103
|
+
): Promise<FileAsset> {
|
|
104
|
+
const artifact = await getGeneratedImageArtifact(db, workspaceId, artifactId);
|
|
105
|
+
if (!artifact || artifact.status !== "ready") {
|
|
106
|
+
throw new Error(`Generated image artifact not found or unavailable: ${artifactId}`);
|
|
107
|
+
}
|
|
108
|
+
return artifact.file;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function assertStoredFileMatches(
|
|
112
|
+
file: FileAsset,
|
|
113
|
+
image: Pick<ResolvedImageGenerationReference, "sizeBytes" | "sha256">,
|
|
114
|
+
): void {
|
|
115
|
+
if (file.sizeBytes !== image.sizeBytes || file.sha256 !== image.sha256) {
|
|
116
|
+
throw new Error("Image reference bytes do not match their durable file metadata");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type KnowledgeSourceSyncDriverInventory<Entry, StopReason extends string> = {
|
|
2
|
+
status: "complete" | "paused";
|
|
3
|
+
stopReason: StopReason | null;
|
|
4
|
+
entries: Entry[];
|
|
5
|
+
checkpoint: Record<string, unknown> | null;
|
|
6
|
+
providerCursor: Record<string, unknown> | null;
|
|
7
|
+
authoritativeFullScan: boolean;
|
|
8
|
+
cursorInvalidated: boolean;
|
|
9
|
+
providerRequests: number;
|
|
10
|
+
elapsedMs: number;
|
|
11
|
+
hardLimitReached: boolean;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/** Provider port for deterministic knowledge ingestion. Implementations own
|
|
15
|
+
* provider JSON and byte transfer; the shared activity owns authority,
|
|
16
|
+
* persistence, indexing obligations, telemetry, and checkpoint settlement. */
|
|
17
|
+
export type KnowledgeSourceSyncDriver<Entry, StopReason extends string> = {
|
|
18
|
+
providerKey: string;
|
|
19
|
+
providerDomain: string;
|
|
20
|
+
providerCoordinationKey: string;
|
|
21
|
+
inventory: (
|
|
22
|
+
executionCheckpoint: Record<string, unknown> | null,
|
|
23
|
+
providerCursor: Record<string, unknown> | null,
|
|
24
|
+
) => Promise<KnowledgeSourceSyncDriverInventory<Entry, StopReason>>;
|
|
25
|
+
fetchContent: (entry: Entry, maxBytes: number) => Promise<Uint8Array>;
|
|
26
|
+
citationLocator: (entry: Entry) => Record<string, unknown>;
|
|
27
|
+
};
|