@opengeni/worker-bundle 0.16.29 → 0.17.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/activities/agent-turn.d.ts +85 -19
- package/dist/activities/api-integrations.d.ts +21 -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 +5 -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 +11 -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 +13 -26
- 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 +4 -4
- package/dist/activities/streaming.d.ts +1 -1
- package/dist/activities/types.d.ts +71 -5
- package/dist/activities/video-generation-admission.d.ts +38 -0
- package/dist/activities/video-generation-credential.d.ts +7 -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 +49 -0
- package/dist/activities/workflow-wake.d.ts +2 -2
- package/dist/activities/workspace-capture.d.ts +10 -1
- package/dist/activities-control-44KBWLSM.js +6885 -0
- package/dist/activities-control-44KBWLSM.js.map +1 -0
- package/dist/activities-control.d.ts +289 -0
- package/dist/activities-turn-K4CEDEGJ.js +14675 -0
- package/dist/activities-turn-K4CEDEGJ.js.map +1 -0
- package/dist/activities-turn.d.ts +17 -0
- package/dist/activities.d.ts +130 -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-E3RI3I33.js +828 -0
- package/dist/chunk-E3RI3I33.js.map +1 -0
- package/dist/chunk-GRFQTTSQ.js +448 -0
- package/dist/chunk-GRFQTTSQ.js.map +1 -0
- package/dist/chunk-KKCRGAGZ.js +3091 -0
- package/dist/chunk-KKCRGAGZ.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 +176 -10
- package/dist/index.js +662 -17374
- package/dist/index.js.map +1 -1
- package/dist/memory-pressure-guard.d.ts +57 -0
- package/dist/model-checkpoint-memory-collector.d.ts +22 -0
- package/dist/observability-metrics.d.ts +33 -0
- 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 +2 -0
- package/dist/worker-service-lifecycle.d.ts +6 -2
- package/dist/workflow-bundle.js +1043 -363
- package/dist/workflows/activities.d.ts +7 -1
- 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/video-generation.d.ts +10 -0
- package/dist/workflows/workflow-wake.d.ts +1 -1
- package/dist/workflows.d.ts +9 -7
- package/package.json +34 -15
- package/src/activities/agent-turn.ts +1470 -246
- package/src/activities/api-integrations.ts +138 -0
- package/src/activities/codemode-dispatcher.ts +335 -0
- package/src/activities/codemode-token-renewal.ts +64 -0
- package/src/activities/codex-capacity.ts +6 -6
- 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 +45 -42
- 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 +6 -2
- 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 +55 -55
- 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 +77 -4
- package/src/activities/session-state.ts +4 -4
- package/src/activities/streaming.ts +42 -3
- package/src/activities/types.ts +87 -5
- package/src/activities/video-generation-admission.ts +270 -0
- package/src/activities/video-generation-credential.ts +34 -0
- package/src/activities/video-generation-reconciliation.ts +578 -0
- package/src/activities/video-media-validation.ts +326 -0
- package/src/activities/video-output-retention.ts +314 -0
- package/src/activities/video-reference-staging.ts +263 -0
- package/src/activities/workflow-wake.ts +2 -2
- package/src/activities/workspace-capture.ts +44 -10
- package/src/activities-control.ts +54 -0
- package/src/activities-turn.ts +40 -0
- package/src/activities.ts +48 -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 +277 -72
- package/src/memory-pressure-guard.ts +489 -0
- package/src/model-checkpoint-memory-collector.ts +116 -0
- package/src/observability-metrics.ts +199 -4
- 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 +19 -24
- package/src/worker-service-lifecycle.ts +80 -14
- package/src/workflows/activities.ts +30 -0
- package/src/workflows/file-upload-reaper.ts +3 -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/video-generation.ts +36 -0
- package/src/workflows.ts +17 -1
- package/dist/activities/toolspace-token-renewal.d.ts +0 -23
- package/src/activities/toolspace-token-renewal.ts +0 -64
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
import type { GeneratedVideoFacts } from "@opengeni/contracts";
|
|
2
|
+
import type { ObjectStorage } from "@opengeni/storage";
|
|
3
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
4
|
+
import { open, mkdir, rm, stat } from "node:fs/promises";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { spawn } from "node:child_process";
|
|
7
|
+
import sharp from "sharp";
|
|
8
|
+
|
|
9
|
+
const RANGE_BYTES = 1024 * 1024;
|
|
10
|
+
const PROBE_STDOUT_MAX_BYTES = 64 * 1024;
|
|
11
|
+
const PROBE_STDERR_MAX_BYTES = 16 * 1024;
|
|
12
|
+
const PROBE_TIMEOUT_MS = 20_000;
|
|
13
|
+
|
|
14
|
+
export type VerifiedTempFile = Readonly<{
|
|
15
|
+
directory: string;
|
|
16
|
+
path: string;
|
|
17
|
+
sizeBytes: number;
|
|
18
|
+
sha256: string;
|
|
19
|
+
contentType: string;
|
|
20
|
+
cleanup: () => Promise<void>;
|
|
21
|
+
}>;
|
|
22
|
+
|
|
23
|
+
export async function copyVersionedObjectToVerifiedTemp(input: {
|
|
24
|
+
storage: ObjectStorage;
|
|
25
|
+
key: string;
|
|
26
|
+
expectedSizeBytes: number;
|
|
27
|
+
expectedSha256: string;
|
|
28
|
+
expectedContentType: string;
|
|
29
|
+
maxBytes: number;
|
|
30
|
+
tempRoot: string;
|
|
31
|
+
signal?: AbortSignal;
|
|
32
|
+
}): Promise<VerifiedTempFile> {
|
|
33
|
+
if (!input.storage.headObject || !input.storage.getObjectRange) {
|
|
34
|
+
throw new Error("Object storage lacks versioned range reads");
|
|
35
|
+
}
|
|
36
|
+
assertSha256(input.expectedSha256);
|
|
37
|
+
if (
|
|
38
|
+
!Number.isSafeInteger(input.expectedSizeBytes) ||
|
|
39
|
+
input.expectedSizeBytes <= 0 ||
|
|
40
|
+
input.expectedSizeBytes > input.maxBytes
|
|
41
|
+
) {
|
|
42
|
+
throw new Error("Media staging size is invalid");
|
|
43
|
+
}
|
|
44
|
+
throwIfAborted(input.signal);
|
|
45
|
+
const head = await input.storage.headObject(input.key);
|
|
46
|
+
if (
|
|
47
|
+
!head ||
|
|
48
|
+
head.ContentLength !== input.expectedSizeBytes ||
|
|
49
|
+
head.ContentType !== input.expectedContentType ||
|
|
50
|
+
head.Metadata?.sha256 !== input.expectedSha256 ||
|
|
51
|
+
!head.VersionToken
|
|
52
|
+
) {
|
|
53
|
+
throw new Error("Media staging metadata does not match its sealed reference");
|
|
54
|
+
}
|
|
55
|
+
const versionToken = head.VersionToken;
|
|
56
|
+
await mkdir(input.tempRoot, { recursive: true, mode: 0o700 });
|
|
57
|
+
const directory = join(input.tempRoot, `media-${randomUUID()}`);
|
|
58
|
+
const path = join(directory, "payload");
|
|
59
|
+
await mkdir(directory, { recursive: false, mode: 0o700 });
|
|
60
|
+
const handle = await open(path, "wx", 0o600);
|
|
61
|
+
const hash = createHash("sha256");
|
|
62
|
+
let offset = 0;
|
|
63
|
+
try {
|
|
64
|
+
while (offset < input.expectedSizeBytes) {
|
|
65
|
+
throwIfAborted(input.signal);
|
|
66
|
+
const endInclusive = Math.min(input.expectedSizeBytes - 1, offset + RANGE_BYTES - 1);
|
|
67
|
+
const range = await input.storage.getObjectRange({
|
|
68
|
+
key: input.key,
|
|
69
|
+
start: offset,
|
|
70
|
+
endInclusive,
|
|
71
|
+
expectedVersionToken: versionToken,
|
|
72
|
+
});
|
|
73
|
+
if (
|
|
74
|
+
!range ||
|
|
75
|
+
range.versionToken !== versionToken ||
|
|
76
|
+
range.bytes.byteLength !== endInclusive - offset + 1
|
|
77
|
+
) {
|
|
78
|
+
throw new Error("Media staging object changed during range read");
|
|
79
|
+
}
|
|
80
|
+
await writeAll(handle, range.bytes, offset);
|
|
81
|
+
hash.update(range.bytes);
|
|
82
|
+
offset += range.bytes.byteLength;
|
|
83
|
+
}
|
|
84
|
+
await handle.sync();
|
|
85
|
+
} catch (error) {
|
|
86
|
+
await handle.close().catch(() => undefined);
|
|
87
|
+
await rm(directory, { recursive: true, force: true });
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
await handle.close();
|
|
91
|
+
const digest = hash.digest("hex");
|
|
92
|
+
if (offset !== input.expectedSizeBytes || digest !== input.expectedSha256) {
|
|
93
|
+
await rm(directory, { recursive: true, force: true });
|
|
94
|
+
throw new Error("Media staging bytes do not match their sealed digest");
|
|
95
|
+
}
|
|
96
|
+
const after = await input.storage.headObject(input.key);
|
|
97
|
+
if (!after || after.VersionToken !== versionToken) {
|
|
98
|
+
await rm(directory, { recursive: true, force: true });
|
|
99
|
+
throw new Error("Media staging object changed after verification");
|
|
100
|
+
}
|
|
101
|
+
return Object.freeze({
|
|
102
|
+
directory,
|
|
103
|
+
path,
|
|
104
|
+
sizeBytes: offset,
|
|
105
|
+
sha256: digest,
|
|
106
|
+
contentType: input.expectedContentType,
|
|
107
|
+
cleanup: async () => await rm(directory, { recursive: true, force: true }),
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export async function validateImageReference(path: string): Promise<{
|
|
112
|
+
contentType: "image/jpeg" | "image/png" | "image/webp";
|
|
113
|
+
width: number;
|
|
114
|
+
height: number;
|
|
115
|
+
}> {
|
|
116
|
+
const metadata = await sharp(path, { failOn: "error", limitInputPixels: 67_108_864 }).metadata();
|
|
117
|
+
const contentType =
|
|
118
|
+
metadata.format === "jpeg"
|
|
119
|
+
? "image/jpeg"
|
|
120
|
+
: metadata.format === "png"
|
|
121
|
+
? "image/png"
|
|
122
|
+
: metadata.format === "webp"
|
|
123
|
+
? "image/webp"
|
|
124
|
+
: null;
|
|
125
|
+
if (
|
|
126
|
+
!contentType ||
|
|
127
|
+
!metadata.width ||
|
|
128
|
+
!metadata.height ||
|
|
129
|
+
metadata.width > 8_192 ||
|
|
130
|
+
metadata.height > 8_192
|
|
131
|
+
) {
|
|
132
|
+
throw new Error("Video image reference is not a supported JPEG, PNG, or WebP image");
|
|
133
|
+
}
|
|
134
|
+
return { contentType, width: metadata.width, height: metadata.height };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export async function validateGeneratedVideo(input: {
|
|
138
|
+
path: string;
|
|
139
|
+
ffprobePath: string;
|
|
140
|
+
expectedDurationSeconds?: number;
|
|
141
|
+
}): Promise<GeneratedVideoFacts> {
|
|
142
|
+
const facts = await probeBrowserCompatibleMp4(input.path, input.ffprobePath);
|
|
143
|
+
if (
|
|
144
|
+
input.expectedDurationSeconds !== undefined &&
|
|
145
|
+
Math.abs(facts.durationSeconds - input.expectedDurationSeconds) > 3
|
|
146
|
+
) {
|
|
147
|
+
throw new Error("Generated video duration differs from the requested duration");
|
|
148
|
+
}
|
|
149
|
+
return facts;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export async function validateVideoReference(input: {
|
|
153
|
+
path: string;
|
|
154
|
+
ffprobePath: string;
|
|
155
|
+
}): Promise<GeneratedVideoFacts> {
|
|
156
|
+
return await probeBrowserCompatibleMp4(input.path, input.ffprobePath);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function probeBrowserCompatibleMp4(
|
|
160
|
+
path: string,
|
|
161
|
+
ffprobePath: string,
|
|
162
|
+
): Promise<GeneratedVideoFacts> {
|
|
163
|
+
const file = await stat(path);
|
|
164
|
+
if (!file.isFile() || file.size <= 0) throw new Error("Media probe input is not a regular file");
|
|
165
|
+
const result = await spawnBounded(ffprobePath, [
|
|
166
|
+
"-v",
|
|
167
|
+
"error",
|
|
168
|
+
"-protocol_whitelist",
|
|
169
|
+
"file,pipe",
|
|
170
|
+
"-probesize",
|
|
171
|
+
"8388608",
|
|
172
|
+
"-analyzeduration",
|
|
173
|
+
"5000000",
|
|
174
|
+
"-show_entries",
|
|
175
|
+
"format=format_name,duration:stream=codec_type,codec_name,width,height,avg_frame_rate",
|
|
176
|
+
"-of",
|
|
177
|
+
"json",
|
|
178
|
+
"--",
|
|
179
|
+
path,
|
|
180
|
+
]);
|
|
181
|
+
let decoded: unknown;
|
|
182
|
+
try {
|
|
183
|
+
decoded = JSON.parse(result.stdout);
|
|
184
|
+
} catch {
|
|
185
|
+
throw new Error("Media probe returned malformed metadata");
|
|
186
|
+
}
|
|
187
|
+
const root = record(decoded);
|
|
188
|
+
const format = record(root?.format);
|
|
189
|
+
const streams = Array.isArray(root?.streams) ? root.streams.map(record).filter(Boolean) : [];
|
|
190
|
+
const video = streams.filter((stream) => stream?.codec_type === "video");
|
|
191
|
+
const audio = streams.filter((stream) => stream?.codec_type === "audio");
|
|
192
|
+
const durationSeconds = finitePositive(format?.duration);
|
|
193
|
+
const width = positiveInteger(video[0]?.width);
|
|
194
|
+
const height = positiveInteger(video[0]?.height);
|
|
195
|
+
const fps = parseFrameRate(video[0]?.avg_frame_rate);
|
|
196
|
+
if (
|
|
197
|
+
!stringValue(format?.format_name)
|
|
198
|
+
?.split(",")
|
|
199
|
+
.some((value) => value === "mp4") ||
|
|
200
|
+
video.length !== 1 ||
|
|
201
|
+
audio.length > 1 ||
|
|
202
|
+
video[0]?.codec_name !== "h264" ||
|
|
203
|
+
(audio.length === 1 && audio[0]?.codec_name !== "aac") ||
|
|
204
|
+
!durationSeconds ||
|
|
205
|
+
durationSeconds > 120 ||
|
|
206
|
+
!width ||
|
|
207
|
+
!height ||
|
|
208
|
+
width > 8_192 ||
|
|
209
|
+
height > 8_192 ||
|
|
210
|
+
!fps ||
|
|
211
|
+
fps > 120
|
|
212
|
+
) {
|
|
213
|
+
throw new Error("Video is not a supported browser-compatible H.264 MP4");
|
|
214
|
+
}
|
|
215
|
+
return {
|
|
216
|
+
durationSeconds,
|
|
217
|
+
width,
|
|
218
|
+
height,
|
|
219
|
+
fps,
|
|
220
|
+
hasAudio: audio.length === 1,
|
|
221
|
+
videoCodec: "h264",
|
|
222
|
+
audioCodec: audio.length === 1 ? "aac" : null,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async function spawnBounded(
|
|
227
|
+
command: string,
|
|
228
|
+
args: string[],
|
|
229
|
+
): Promise<{ stdout: string; stderr: string }> {
|
|
230
|
+
return await new Promise((resolve, reject) => {
|
|
231
|
+
const child = spawn(command, args, {
|
|
232
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
233
|
+
env: { PATH: process.env.PATH ?? "/usr/local/bin:/usr/bin:/bin", LC_ALL: "C" },
|
|
234
|
+
});
|
|
235
|
+
const stdout: Buffer[] = [];
|
|
236
|
+
const stderr: Buffer[] = [];
|
|
237
|
+
let stdoutBytes = 0;
|
|
238
|
+
let stderrBytes = 0;
|
|
239
|
+
let settled = false;
|
|
240
|
+
const fail = (error: Error) => {
|
|
241
|
+
if (settled) return;
|
|
242
|
+
settled = true;
|
|
243
|
+
clearTimeout(timer);
|
|
244
|
+
child.kill("SIGKILL");
|
|
245
|
+
reject(error);
|
|
246
|
+
};
|
|
247
|
+
const timer = setTimeout(() => fail(new Error("Media probe timed out")), PROBE_TIMEOUT_MS);
|
|
248
|
+
child.stdout.on("data", (chunk: Buffer) => {
|
|
249
|
+
stdoutBytes += chunk.byteLength;
|
|
250
|
+
if (stdoutBytes > PROBE_STDOUT_MAX_BYTES) fail(new Error("Media probe output is too large"));
|
|
251
|
+
else stdout.push(Buffer.from(chunk));
|
|
252
|
+
});
|
|
253
|
+
child.stderr.on("data", (chunk: Buffer) => {
|
|
254
|
+
stderrBytes += chunk.byteLength;
|
|
255
|
+
if (stderrBytes > PROBE_STDERR_MAX_BYTES) fail(new Error("Media probe error is too large"));
|
|
256
|
+
else stderr.push(Buffer.from(chunk));
|
|
257
|
+
});
|
|
258
|
+
child.once("error", (error) => fail(error));
|
|
259
|
+
child.once("close", (code) => {
|
|
260
|
+
if (settled) return;
|
|
261
|
+
settled = true;
|
|
262
|
+
clearTimeout(timer);
|
|
263
|
+
const stderrText = Buffer.concat(stderr).toString("utf8").trim();
|
|
264
|
+
if (code !== 0) {
|
|
265
|
+
reject(
|
|
266
|
+
new Error(
|
|
267
|
+
stderrText ? `Media probe failed: ${stderrText.slice(0, 1_000)}` : "Media probe failed",
|
|
268
|
+
),
|
|
269
|
+
);
|
|
270
|
+
} else {
|
|
271
|
+
resolve({ stdout: Buffer.concat(stdout).toString("utf8"), stderr: stderrText });
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
async function writeAll(
|
|
278
|
+
handle: Awaited<ReturnType<typeof open>>,
|
|
279
|
+
chunk: Uint8Array,
|
|
280
|
+
offset: number,
|
|
281
|
+
): Promise<void> {
|
|
282
|
+
let written = 0;
|
|
283
|
+
while (written < chunk.byteLength) {
|
|
284
|
+
const result = await handle.write(chunk, written, chunk.byteLength - written, offset + written);
|
|
285
|
+
if (result.bytesWritten <= 0) throw new Error("Media temporary file write stalled");
|
|
286
|
+
written += result.bytesWritten;
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function parseFrameRate(value: unknown): number | null {
|
|
291
|
+
const text = stringValue(value);
|
|
292
|
+
if (!text) return null;
|
|
293
|
+
const [left, right] = text.split("/");
|
|
294
|
+
const numerator = Number(left);
|
|
295
|
+
const denominator = right === undefined ? 1 : Number(right);
|
|
296
|
+
const fps = numerator / denominator;
|
|
297
|
+
return Number.isFinite(fps) && fps > 0 ? fps : null;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
function positiveInteger(value: unknown): number | null {
|
|
301
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
302
|
+
return Number.isSafeInteger(parsed) && parsed > 0 ? parsed : null;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
function finitePositive(value: unknown): number | null {
|
|
306
|
+
const parsed = typeof value === "number" ? value : Number(value);
|
|
307
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function stringValue(value: unknown): string | null {
|
|
311
|
+
return typeof value === "string" && value.trim() ? value : null;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
function record(value: unknown): Record<string, unknown> | null {
|
|
315
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
316
|
+
? (value as Record<string, unknown>)
|
|
317
|
+
: null;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
function assertSha256(value: string): void {
|
|
321
|
+
if (!/^[0-9a-f]{64}$/u.test(value)) throw new Error("Media SHA-256 is invalid");
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function throwIfAborted(signal?: AbortSignal): void {
|
|
325
|
+
if (signal?.aborted) throw signal.reason ?? new Error("Media operation was cancelled");
|
|
326
|
+
}
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import type { Settings } from "@opengeni/config";
|
|
2
|
+
import { GENERATED_VIDEO_MAX_BYTES, type GeneratedVideoFacts } from "@opengeni/contracts";
|
|
3
|
+
import { pinnedFetch, type FetchLike } from "@opengeni/network";
|
|
4
|
+
import type { ObjectStorage } from "@opengeni/storage";
|
|
5
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
6
|
+
import { createReadStream } from "node:fs";
|
|
7
|
+
import { mkdir, open, rm } from "node:fs/promises";
|
|
8
|
+
import { join } from "node:path";
|
|
9
|
+
import { validateGeneratedVideo, type VerifiedTempFile } from "./video-media-validation";
|
|
10
|
+
|
|
11
|
+
const DOWNLOAD_TIMEOUT_MS = 15 * 60_000;
|
|
12
|
+
const READ_STALL_TIMEOUT_MS = 45_000;
|
|
13
|
+
const MAX_REDIRECTS = 3;
|
|
14
|
+
const STORAGE_RANGE_BYTES = 1024 * 1024;
|
|
15
|
+
type StreamReadResult = Awaited<ReturnType<ReadableStreamDefaultReader<Uint8Array>["read"]>>;
|
|
16
|
+
|
|
17
|
+
export type RetainedGeneratedVideo = Readonly<{
|
|
18
|
+
bucket: string;
|
|
19
|
+
objectKey: string;
|
|
20
|
+
sizeBytes: number;
|
|
21
|
+
sha256: string;
|
|
22
|
+
facts: GeneratedVideoFacts;
|
|
23
|
+
}>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Treat the provider's result URL and bytes as hostile. DNS is pinned, every
|
|
27
|
+
* redirect is revalidated, and the body streams to a private temporary file
|
|
28
|
+
* under hard time/size limits before ffprobe sees it.
|
|
29
|
+
*/
|
|
30
|
+
export async function downloadGeneratedVideoToVerifiedTemp(input: {
|
|
31
|
+
url: string;
|
|
32
|
+
mediaType: string;
|
|
33
|
+
settings: Settings;
|
|
34
|
+
expectedDurationSeconds: number;
|
|
35
|
+
fetch?: FetchLike;
|
|
36
|
+
signal?: AbortSignal;
|
|
37
|
+
}): Promise<{ temp: VerifiedTempFile; facts: GeneratedVideoFacts }> {
|
|
38
|
+
if (normalizedMediaType(input.mediaType) !== "video/mp4") {
|
|
39
|
+
throw new Error("Video provider returned an unsupported media type");
|
|
40
|
+
}
|
|
41
|
+
const timeoutSignal = AbortSignal.timeout(DOWNLOAD_TIMEOUT_MS);
|
|
42
|
+
const signal = input.signal ? AbortSignal.any([input.signal, timeoutSignal]) : timeoutSignal;
|
|
43
|
+
const response = await fetchFollowingSafeRedirects(input.url, {
|
|
44
|
+
settings: input.settings,
|
|
45
|
+
signal,
|
|
46
|
+
...(input.fetch ? { fetch: input.fetch } : {}),
|
|
47
|
+
});
|
|
48
|
+
if (!response.ok || !response.body) {
|
|
49
|
+
await response.body?.cancel().catch(() => undefined);
|
|
50
|
+
throw new Error("Video provider output could not be downloaded");
|
|
51
|
+
}
|
|
52
|
+
if (normalizedMediaType(response.headers.get("content-type")) !== "video/mp4") {
|
|
53
|
+
await response.body.cancel().catch(() => undefined);
|
|
54
|
+
throw new Error("Video provider output is not an MP4 response");
|
|
55
|
+
}
|
|
56
|
+
const declaredBytes = parseContentLength(response.headers.get("content-length"));
|
|
57
|
+
if (declaredBytes !== null && declaredBytes > GENERATED_VIDEO_MAX_BYTES) {
|
|
58
|
+
await response.body.cancel().catch(() => undefined);
|
|
59
|
+
throw new Error("Generated video exceeds the retained output limit");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
await mkdir(input.settings.videoGenerationTempDirectory, { recursive: true, mode: 0o700 });
|
|
63
|
+
const directory = join(input.settings.videoGenerationTempDirectory, `output-${randomUUID()}`);
|
|
64
|
+
const path = join(directory, "video.mp4");
|
|
65
|
+
await mkdir(directory, { recursive: false, mode: 0o700 });
|
|
66
|
+
const handle = await open(path, "wx", 0o600);
|
|
67
|
+
const reader = response.body.getReader();
|
|
68
|
+
const digest = createHash("sha256");
|
|
69
|
+
let sizeBytes = 0;
|
|
70
|
+
try {
|
|
71
|
+
for (;;) {
|
|
72
|
+
throwIfAborted(signal);
|
|
73
|
+
const chunk = await readChunkWithStallLimit(reader, signal);
|
|
74
|
+
if (chunk.done) break;
|
|
75
|
+
if (chunk.value.byteLength === 0) continue;
|
|
76
|
+
sizeBytes += chunk.value.byteLength;
|
|
77
|
+
if (sizeBytes > GENERATED_VIDEO_MAX_BYTES) {
|
|
78
|
+
throw new Error("Generated video exceeds the retained output limit");
|
|
79
|
+
}
|
|
80
|
+
await writeAll(handle, chunk.value, sizeBytes - chunk.value.byteLength);
|
|
81
|
+
digest.update(chunk.value);
|
|
82
|
+
}
|
|
83
|
+
await handle.sync();
|
|
84
|
+
} catch (error) {
|
|
85
|
+
await reader.cancel(error).catch(() => undefined);
|
|
86
|
+
await handle.close().catch(() => undefined);
|
|
87
|
+
await rm(directory, { recursive: true, force: true });
|
|
88
|
+
throw error;
|
|
89
|
+
} finally {
|
|
90
|
+
try {
|
|
91
|
+
reader.releaseLock();
|
|
92
|
+
} catch {
|
|
93
|
+
// The aborted transport may retain the lock until cancellation settles.
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
await handle.close();
|
|
97
|
+
if (sizeBytes <= 0 || (declaredBytes !== null && sizeBytes !== declaredBytes)) {
|
|
98
|
+
await rm(directory, { recursive: true, force: true });
|
|
99
|
+
throw new Error("Generated video response was truncated");
|
|
100
|
+
}
|
|
101
|
+
const sha256 = digest.digest("hex");
|
|
102
|
+
const temp: VerifiedTempFile = Object.freeze({
|
|
103
|
+
directory,
|
|
104
|
+
path,
|
|
105
|
+
sizeBytes,
|
|
106
|
+
sha256,
|
|
107
|
+
contentType: "video/mp4",
|
|
108
|
+
cleanup: async () => await rm(directory, { recursive: true, force: true }),
|
|
109
|
+
});
|
|
110
|
+
try {
|
|
111
|
+
const facts = await validateGeneratedVideo({
|
|
112
|
+
path,
|
|
113
|
+
ffprobePath: input.settings.videoGenerationFfprobePath,
|
|
114
|
+
expectedDurationSeconds: input.expectedDurationSeconds,
|
|
115
|
+
});
|
|
116
|
+
return { temp, facts };
|
|
117
|
+
} catch (error) {
|
|
118
|
+
await temp.cleanup();
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** Stream verified bytes into an immutable content-addressed object and hash
|
|
124
|
+
* the stored generation back through version-fenced Range reads. */
|
|
125
|
+
export async function retainVerifiedGeneratedVideo(input: {
|
|
126
|
+
storage: ObjectStorage;
|
|
127
|
+
temp: VerifiedTempFile;
|
|
128
|
+
facts: GeneratedVideoFacts;
|
|
129
|
+
signal?: AbortSignal;
|
|
130
|
+
}): Promise<RetainedGeneratedVideo> {
|
|
131
|
+
if (
|
|
132
|
+
!input.storage.putObjectStreamIfAbsent ||
|
|
133
|
+
!input.storage.headObject ||
|
|
134
|
+
!input.storage.getObjectRange
|
|
135
|
+
) {
|
|
136
|
+
throw new Error("Object storage lacks bounded immutable video primitives");
|
|
137
|
+
}
|
|
138
|
+
const objectKey = `video-generation/objects/sha256/${input.temp.sha256}.mp4`;
|
|
139
|
+
const chunks = fileChunks(input.temp.path, input.signal);
|
|
140
|
+
await input.storage.putObjectStreamIfAbsent({
|
|
141
|
+
key: objectKey,
|
|
142
|
+
contentType: "video/mp4",
|
|
143
|
+
chunks,
|
|
144
|
+
byteSize: input.temp.sizeBytes,
|
|
145
|
+
sha256: input.temp.sha256,
|
|
146
|
+
...(input.signal ? { signal: input.signal } : {}),
|
|
147
|
+
});
|
|
148
|
+
await verifyStoredObject({
|
|
149
|
+
storage: input.storage,
|
|
150
|
+
key: objectKey,
|
|
151
|
+
expectedSizeBytes: input.temp.sizeBytes,
|
|
152
|
+
expectedSha256: input.temp.sha256,
|
|
153
|
+
...(input.signal ? { signal: input.signal } : {}),
|
|
154
|
+
});
|
|
155
|
+
return Object.freeze({
|
|
156
|
+
bucket: input.storage.bucket,
|
|
157
|
+
objectKey,
|
|
158
|
+
sizeBytes: input.temp.sizeBytes,
|
|
159
|
+
sha256: input.temp.sha256,
|
|
160
|
+
facts: input.facts,
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function fetchFollowingSafeRedirects(
|
|
165
|
+
rawUrl: string,
|
|
166
|
+
input: { settings: Settings; fetch?: FetchLike; signal: AbortSignal },
|
|
167
|
+
): Promise<Response> {
|
|
168
|
+
let url = requireSafeOutputUrl(rawUrl);
|
|
169
|
+
for (let redirect = 0; redirect <= MAX_REDIRECTS; redirect += 1) {
|
|
170
|
+
const response = await pinnedFetch(
|
|
171
|
+
url,
|
|
172
|
+
{
|
|
173
|
+
method: "GET",
|
|
174
|
+
headers: { accept: "video/mp4", "accept-encoding": "identity" },
|
|
175
|
+
redirect: "manual",
|
|
176
|
+
signal: input.signal,
|
|
177
|
+
},
|
|
178
|
+
{
|
|
179
|
+
environment: input.settings.environment,
|
|
180
|
+
// Provider output must never inherit an integration-level private-net override.
|
|
181
|
+
integrationsAllowPrivateNetworkTargets: false,
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
label: "video provider output",
|
|
185
|
+
requireHttpsOutsideLocalTest: true,
|
|
186
|
+
...(input.fetch ? { fetchImpl: input.fetch } : {}),
|
|
187
|
+
},
|
|
188
|
+
);
|
|
189
|
+
if (![301, 302, 303, 307, 308].includes(response.status)) return response;
|
|
190
|
+
const location = response.headers.get("location");
|
|
191
|
+
await response.body?.cancel().catch(() => undefined);
|
|
192
|
+
if (!location || redirect === MAX_REDIRECTS) {
|
|
193
|
+
throw new Error("Video provider output redirect is invalid");
|
|
194
|
+
}
|
|
195
|
+
url = requireSafeOutputUrl(new URL(location, url).toString());
|
|
196
|
+
}
|
|
197
|
+
throw new Error("Video provider output has too many redirects");
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function requireSafeOutputUrl(rawUrl: string): string {
|
|
201
|
+
const url = new URL(rawUrl);
|
|
202
|
+
if (url.protocol !== "https:" || url.username || url.password || url.hash) {
|
|
203
|
+
throw new Error("Video provider output URL is invalid");
|
|
204
|
+
}
|
|
205
|
+
return url.toString();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async function verifyStoredObject(input: {
|
|
209
|
+
storage: ObjectStorage;
|
|
210
|
+
key: string;
|
|
211
|
+
expectedSizeBytes: number;
|
|
212
|
+
expectedSha256: string;
|
|
213
|
+
signal?: AbortSignal;
|
|
214
|
+
}): Promise<void> {
|
|
215
|
+
const head = await input.storage.headObject!(input.key);
|
|
216
|
+
if (
|
|
217
|
+
!head ||
|
|
218
|
+
head.ContentLength !== input.expectedSizeBytes ||
|
|
219
|
+
head.ContentType !== "video/mp4" ||
|
|
220
|
+
head.Metadata?.sha256 !== input.expectedSha256 ||
|
|
221
|
+
!head.VersionToken
|
|
222
|
+
) {
|
|
223
|
+
throw new Error("Retained video object metadata is inconsistent");
|
|
224
|
+
}
|
|
225
|
+
const hash = createHash("sha256");
|
|
226
|
+
let offset = 0;
|
|
227
|
+
while (offset < input.expectedSizeBytes) {
|
|
228
|
+
throwIfAborted(input.signal);
|
|
229
|
+
const endInclusive = Math.min(input.expectedSizeBytes - 1, offset + STORAGE_RANGE_BYTES - 1);
|
|
230
|
+
const range = await input.storage.getObjectRange!({
|
|
231
|
+
key: input.key,
|
|
232
|
+
start: offset,
|
|
233
|
+
endInclusive,
|
|
234
|
+
expectedVersionToken: head.VersionToken,
|
|
235
|
+
});
|
|
236
|
+
if (
|
|
237
|
+
!range ||
|
|
238
|
+
range.versionToken !== head.VersionToken ||
|
|
239
|
+
range.bytes.byteLength !== endInclusive - offset + 1
|
|
240
|
+
) {
|
|
241
|
+
throw new Error("Retained video object changed during verification");
|
|
242
|
+
}
|
|
243
|
+
hash.update(range.bytes);
|
|
244
|
+
offset += range.bytes.byteLength;
|
|
245
|
+
}
|
|
246
|
+
if (hash.digest("hex") !== input.expectedSha256) {
|
|
247
|
+
throw new Error("Retained video object digest is inconsistent");
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async function* fileChunks(path: string, signal?: AbortSignal): AsyncIterable<Uint8Array> {
|
|
252
|
+
const stream = createReadStream(path, { highWaterMark: STORAGE_RANGE_BYTES });
|
|
253
|
+
try {
|
|
254
|
+
for await (const chunk of stream) {
|
|
255
|
+
throwIfAborted(signal);
|
|
256
|
+
yield chunk instanceof Uint8Array ? chunk : new Uint8Array(chunk);
|
|
257
|
+
}
|
|
258
|
+
} finally {
|
|
259
|
+
stream.destroy();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
async function readChunkWithStallLimit(
|
|
264
|
+
reader: ReadableStreamDefaultReader<Uint8Array>,
|
|
265
|
+
signal: AbortSignal,
|
|
266
|
+
): Promise<StreamReadResult> {
|
|
267
|
+
if (signal.aborted) throw signal.reason;
|
|
268
|
+
let timeout: ReturnType<typeof setTimeout> | undefined;
|
|
269
|
+
try {
|
|
270
|
+
return await Promise.race([
|
|
271
|
+
reader.read(),
|
|
272
|
+
new Promise<never>((_, reject) => {
|
|
273
|
+
timeout = setTimeout(
|
|
274
|
+
() => reject(new Error("Generated video download stalled")),
|
|
275
|
+
READ_STALL_TIMEOUT_MS,
|
|
276
|
+
);
|
|
277
|
+
}),
|
|
278
|
+
]);
|
|
279
|
+
} finally {
|
|
280
|
+
if (timeout) clearTimeout(timeout);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
async function writeAll(
|
|
285
|
+
handle: Awaited<ReturnType<typeof open>>,
|
|
286
|
+
chunk: Uint8Array,
|
|
287
|
+
offset: number,
|
|
288
|
+
): Promise<void> {
|
|
289
|
+
let written = 0;
|
|
290
|
+
while (written < chunk.byteLength) {
|
|
291
|
+
const result = await handle.write(chunk, written, chunk.byteLength - written, offset + written);
|
|
292
|
+
if (result.bytesWritten <= 0) throw new Error("Generated video write stalled");
|
|
293
|
+
written += result.bytesWritten;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function normalizedMediaType(value: string | null): string | null {
|
|
298
|
+
const normalized = value?.split(";", 1)[0]?.trim().toLowerCase();
|
|
299
|
+
return normalized || null;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function parseContentLength(value: string | null): number | null {
|
|
303
|
+
if (value === null) return null;
|
|
304
|
+
if (!/^\d+$/u.test(value.trim())) throw new Error("Generated video length is invalid");
|
|
305
|
+
const parsed = Number(value);
|
|
306
|
+
if (!Number.isSafeInteger(parsed) || parsed < 0) {
|
|
307
|
+
throw new Error("Generated video length is invalid");
|
|
308
|
+
}
|
|
309
|
+
return parsed;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function throwIfAborted(signal?: AbortSignal): void {
|
|
313
|
+
if (signal?.aborted) throw signal.reason ?? new Error("Video retention was cancelled");
|
|
314
|
+
}
|