@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
|
@@ -17,11 +17,13 @@ import {
|
|
|
17
17
|
enqueueSessionWorkflowWakeIfRunnable,
|
|
18
18
|
getBillingBalance,
|
|
19
19
|
getScheduledTask,
|
|
20
|
+
ensureKnowledgeSourceSyncState,
|
|
20
21
|
getScheduledTaskPersonalConnectionDelegations,
|
|
21
22
|
getRig,
|
|
22
23
|
getVariableSet,
|
|
23
24
|
isCodexBilledTurn,
|
|
24
25
|
markScheduledTaskRunFailedIfQueued,
|
|
26
|
+
recordKnowledgeSourceSyncWake,
|
|
25
27
|
recordUsageEvent,
|
|
26
28
|
requireScheduledTaskTargetInTransaction,
|
|
27
29
|
requireSession,
|
|
@@ -30,6 +32,7 @@ import {
|
|
|
30
32
|
SessionSpawnDeniedDbError,
|
|
31
33
|
sumUsageQuantity,
|
|
32
34
|
updateScheduledTask,
|
|
35
|
+
updateScheduledTaskRun,
|
|
33
36
|
upsertSessionGoal,
|
|
34
37
|
} from "@opengeni/db";
|
|
35
38
|
import { appendAndPublishEvents, publishDurableSessionEvents } from "@opengeni/events";
|
|
@@ -41,12 +44,12 @@ import {
|
|
|
41
44
|
} from "./common";
|
|
42
45
|
import { withFirstPartyTools } from "./goals";
|
|
43
46
|
import type {
|
|
44
|
-
|
|
47
|
+
ControlActivityServices,
|
|
45
48
|
DispatchScheduledTaskRunInput,
|
|
46
49
|
DispatchScheduledTaskRunResult,
|
|
47
50
|
} from "./types";
|
|
48
51
|
|
|
49
|
-
export function createScheduledTaskActivities(services: () => Promise<
|
|
52
|
+
export function createScheduledTaskActivities(services: () => Promise<ControlActivityServices>) {
|
|
50
53
|
return {
|
|
51
54
|
dispatchScheduledTaskRun: async (
|
|
52
55
|
input: DispatchScheduledTaskRunInput,
|
|
@@ -55,7 +58,7 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
|
|
|
55
58
|
// Histories created before the manual-initiator workflow patch already
|
|
56
59
|
// contain this activity command. Reject their incomplete wire input here
|
|
57
60
|
// so replay consumes the recorded command and the retry loop settles.
|
|
58
|
-
if (input.triggerType
|
|
61
|
+
if (input.triggerType !== "scheduled" && !input.initiator) {
|
|
59
62
|
return { action: "blocked", reason: "malformed_manual_trigger" };
|
|
60
63
|
}
|
|
61
64
|
const task = await getScheduledTask(db, input.workspaceId, input.taskId);
|
|
@@ -65,6 +68,64 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
|
|
|
65
68
|
if (!task) {
|
|
66
69
|
return { action: "deleted" };
|
|
67
70
|
}
|
|
71
|
+
if (task.action.kind === "knowledge_source_sync") {
|
|
72
|
+
if (knowledgeSourceSyncEffectivelyPaused(task)) {
|
|
73
|
+
return { action: "blocked", reason: "knowledge_source_paused" };
|
|
74
|
+
}
|
|
75
|
+
const run = await createScheduledTaskRun(db, {
|
|
76
|
+
workspaceId: task.workspaceId,
|
|
77
|
+
taskId: task.id,
|
|
78
|
+
triggerType: input.triggerType,
|
|
79
|
+
producerKey:
|
|
80
|
+
input.producerKey ??
|
|
81
|
+
input.agentRunUsageIdempotencyKey ??
|
|
82
|
+
`knowledge-source-sync:${crypto.randomUUID()}`,
|
|
83
|
+
scheduledAt: null,
|
|
84
|
+
});
|
|
85
|
+
await ensureKnowledgeSourceSyncState(db, task);
|
|
86
|
+
await recordKnowledgeSourceSyncWake(db, {
|
|
87
|
+
accountId: task.accountId,
|
|
88
|
+
workspaceId: task.workspaceId,
|
|
89
|
+
sourceId: task.action.sourceId,
|
|
90
|
+
scheduledTaskId: task.id,
|
|
91
|
+
scheduledTaskRunId: run.id,
|
|
92
|
+
cause: input.triggerType,
|
|
93
|
+
producerKey:
|
|
94
|
+
input.producerKey ??
|
|
95
|
+
input.agentRunUsageIdempotencyKey ??
|
|
96
|
+
`knowledge-source-sync:${run.id}`,
|
|
97
|
+
sourceConfigGeneration: task.action.sourceConfigGeneration,
|
|
98
|
+
sourceLifecycleGeneration: task.action.sourceLifecycleGeneration,
|
|
99
|
+
});
|
|
100
|
+
await recordUsageEvent(db, {
|
|
101
|
+
accountId: task.accountId,
|
|
102
|
+
workspaceId: task.workspaceId,
|
|
103
|
+
eventType: "knowledge_source_sync.fired",
|
|
104
|
+
quantity: 1,
|
|
105
|
+
unit: "run",
|
|
106
|
+
sourceResourceType: "scheduled_task_run",
|
|
107
|
+
sourceResourceId: run.id,
|
|
108
|
+
initiator: input.initiator ?? { kind: "service", subjectId: "scheduler" },
|
|
109
|
+
initiatorContext: { scheduledTaskId: task.id, scheduledTaskRunId: run.id },
|
|
110
|
+
origin: "scheduled_task",
|
|
111
|
+
idempotencyKey: `usage:knowledge_source_sync.fired:${run.id}`,
|
|
112
|
+
});
|
|
113
|
+
if (run.status === "queued") {
|
|
114
|
+
await updateScheduledTaskRun(db, task.workspaceId, run.id, {
|
|
115
|
+
status: "dispatched",
|
|
116
|
+
actionKind: "knowledge_source_sync",
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
action: "knowledge_source_sync",
|
|
121
|
+
accountId: task.accountId,
|
|
122
|
+
workspaceId: task.workspaceId,
|
|
123
|
+
taskId: task.id,
|
|
124
|
+
scheduledTaskRunId: run.id,
|
|
125
|
+
sourceId: task.action.sourceId,
|
|
126
|
+
overlapPolicy: task.overlapPolicy as "skip" | "buffer_one",
|
|
127
|
+
};
|
|
128
|
+
}
|
|
68
129
|
const taskPersonalConnectionDelegations = await getScheduledTaskPersonalConnectionDelegations(
|
|
69
130
|
db,
|
|
70
131
|
task.workspaceId,
|
|
@@ -439,6 +500,7 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
|
|
|
439
500
|
],
|
|
440
501
|
};
|
|
441
502
|
},
|
|
503
|
+
{ activity: "semantic" },
|
|
442
504
|
);
|
|
443
505
|
await publishDurableSessionEvents(bus, task.workspaceId, session.id, goalEvents);
|
|
444
506
|
}
|
|
@@ -535,9 +597,20 @@ export function createScheduledTaskActivities(services: () => Promise<ActivitySe
|
|
|
535
597
|
};
|
|
536
598
|
}
|
|
537
599
|
|
|
600
|
+
function knowledgeSourceSyncEffectivelyPaused(task: {
|
|
601
|
+
status: "active" | "paused";
|
|
602
|
+
metadata: Record<string, unknown>;
|
|
603
|
+
}): boolean {
|
|
604
|
+
if (task.status === "paused") return true;
|
|
605
|
+
const value = task.metadata.knowledgeSourceSync;
|
|
606
|
+
if (!value || typeof value !== "object") return false;
|
|
607
|
+
const control = value as Record<string, unknown>;
|
|
608
|
+
return control.sourceEnabled === false || control.connectionPaused === true;
|
|
609
|
+
}
|
|
610
|
+
|
|
538
611
|
async function scheduledRunAdmissionDenial(
|
|
539
612
|
settings: Settings,
|
|
540
|
-
db:
|
|
613
|
+
db: ControlActivityServices["db"],
|
|
541
614
|
accountId: string,
|
|
542
615
|
workspaceId: string,
|
|
543
616
|
requestedAgentRuns: number,
|
|
@@ -18,7 +18,7 @@ import { publishDurableSessionEvents } from "@opengeni/events";
|
|
|
18
18
|
import { deliverFailedChildTurnToParent, notifyParentOfChildIdle } from "./parent-wake";
|
|
19
19
|
import { recordTurnsQueuedGauge } from "../observability-metrics";
|
|
20
20
|
import type {
|
|
21
|
-
|
|
21
|
+
ControlActivityServices,
|
|
22
22
|
ExpireSessionHumanInputInput,
|
|
23
23
|
ExpireSessionHumanInputResult,
|
|
24
24
|
PeekSessionWorkInput,
|
|
@@ -62,7 +62,7 @@ export type SessionStateActivityOverrides = Partial<{
|
|
|
62
62
|
export const WORKER_DEATH_MAX_REDISPATCHES = 3;
|
|
63
63
|
|
|
64
64
|
export function createSessionStateActivities(
|
|
65
|
-
services: () => Promise<
|
|
65
|
+
services: () => Promise<ControlActivityServices>,
|
|
66
66
|
overrides: SessionStateActivityOverrides = {},
|
|
67
67
|
) {
|
|
68
68
|
const settleSessionAttemptInterruptionsFn =
|
|
@@ -388,8 +388,8 @@ export function createSessionStateActivities(
|
|
|
388
388
|
}
|
|
389
389
|
|
|
390
390
|
async function refreshQueuedTurnsGauge(
|
|
391
|
-
db:
|
|
392
|
-
observability:
|
|
391
|
+
db: ControlActivityServices["db"],
|
|
392
|
+
observability: ControlActivityServices["observability"],
|
|
393
393
|
countQueuedTurnsFn: typeof countQueuedTurns,
|
|
394
394
|
recordTurnsQueuedGaugeFn: typeof recordTurnsQueuedGauge,
|
|
395
395
|
): Promise<void> {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SessionEventType } from "@opengeni/contracts";
|
|
2
|
+
import { TURN_ACTIVITY_CANCELLATION_HEARTBEAT_INTERVAL_MS } from "@opengeni/core";
|
|
2
3
|
import type { AppendEventInput } from "@opengeni/db";
|
|
3
4
|
import { Context } from "@temporalio/activity";
|
|
4
5
|
|
|
@@ -176,13 +177,14 @@ export function currentActivityContext(): Context | null {
|
|
|
176
177
|
export function startActivityHeartbeat(
|
|
177
178
|
context: Context | null,
|
|
178
179
|
details: Record<string, unknown>,
|
|
180
|
+
schedule: typeof setInterval = setInterval,
|
|
179
181
|
): ReturnType<typeof setInterval> | null {
|
|
180
182
|
if (!context) {
|
|
181
183
|
return null;
|
|
182
184
|
}
|
|
183
|
-
const timer =
|
|
185
|
+
const timer = schedule(() => {
|
|
184
186
|
context.heartbeat({ ...details, at: new Date().toISOString() });
|
|
185
|
-
},
|
|
187
|
+
}, TURN_ACTIVITY_CANCELLATION_HEARTBEAT_INTERVAL_MS);
|
|
186
188
|
if ("unref" in timer && typeof timer.unref === "function") {
|
|
187
189
|
timer.unref();
|
|
188
190
|
}
|
|
@@ -196,5 +198,42 @@ export async function nextStreamEvent<T>(
|
|
|
196
198
|
if (!context) {
|
|
197
199
|
return await iterator.next();
|
|
198
200
|
}
|
|
199
|
-
|
|
201
|
+
const signal = context.cancellationSignal;
|
|
202
|
+
if (signal.aborted) {
|
|
203
|
+
throw signal.reason;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// Context.cancelled never settles for a normally completed activity. Racing
|
|
207
|
+
// every token/event read against that lifetime promise leaves one losing
|
|
208
|
+
// Promise reaction attached for every event until the Context itself dies.
|
|
209
|
+
// Use the equivalent Temporal AbortSignal and remove each listener as soon as
|
|
210
|
+
// the iterator wins, so a long stream retains at most its one pending read.
|
|
211
|
+
return await new Promise<IteratorResult<T>>((resolve, reject) => {
|
|
212
|
+
let settled = false;
|
|
213
|
+
const settle = (complete: () => void) => {
|
|
214
|
+
if (settled) return;
|
|
215
|
+
settled = true;
|
|
216
|
+
signal.removeEventListener("abort", onAbort);
|
|
217
|
+
complete();
|
|
218
|
+
};
|
|
219
|
+
const onAbort = () => settle(() => reject(signal.reason));
|
|
220
|
+
|
|
221
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
222
|
+
if (signal.aborted) {
|
|
223
|
+
onAbort();
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
let next: PromiseLike<IteratorResult<T>>;
|
|
228
|
+
try {
|
|
229
|
+
next = iterator.next();
|
|
230
|
+
} catch (error) {
|
|
231
|
+
settle(() => reject(error));
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
void Promise.resolve(next).then(
|
|
235
|
+
(result) => settle(() => resolve(result)),
|
|
236
|
+
(error) => settle(() => reject(error)),
|
|
237
|
+
);
|
|
238
|
+
});
|
|
200
239
|
}
|
package/src/activities/types.ts
CHANGED
|
@@ -38,6 +38,18 @@ export type SignalCodexCapacityWorkflow = (input: {
|
|
|
38
38
|
wakeRevision: number;
|
|
39
39
|
}) => Promise<void>;
|
|
40
40
|
|
|
41
|
+
/** Start the versioned, per-box sandbox reaper from the history-stable legacy
|
|
42
|
+
* activity. The legacy Schedule workflow must keep its exact historical
|
|
43
|
+
* ScheduleActivity command across mixed-version worker pools. */
|
|
44
|
+
export type StartSandboxReaperWorkflow = () => Promise<"started" | "already_running">;
|
|
45
|
+
|
|
46
|
+
/** Start-or-observe the one durable reconciler for a paid video operation. */
|
|
47
|
+
export type StartVideoGenerationWorkflow = (input: {
|
|
48
|
+
accountId: string;
|
|
49
|
+
workspaceId: string;
|
|
50
|
+
operationId: string;
|
|
51
|
+
}) => Promise<"started" | "already_running">;
|
|
52
|
+
|
|
41
53
|
/** Exact activity-owned proof that the hard sandbox/tool fence physically
|
|
42
54
|
* drained. This is delivery evidence only: the workflow still validates the
|
|
43
55
|
* persisted attempt dispatch and commits the authoritative Postgres receipt. */
|
|
@@ -59,13 +71,13 @@ export type InspectSessionAttemptActivity = (input: {
|
|
|
59
71
|
activityId: string;
|
|
60
72
|
}) => Promise<"pending" | "settled">;
|
|
61
73
|
|
|
62
|
-
|
|
74
|
+
/** Services shared by both Temporal worker roles. Keep this graph free of the
|
|
75
|
+
* agent runtime and document parser so each process can load only its role. */
|
|
76
|
+
export type SharedActivityServices = {
|
|
63
77
|
settings: Settings;
|
|
64
78
|
db: Database;
|
|
65
79
|
bus: EventBus;
|
|
66
|
-
runtime: OpenGeniRuntime;
|
|
67
80
|
objectStorage: ObjectStorage | null;
|
|
68
|
-
documentServices: DocumentServices;
|
|
69
81
|
observability: Observability;
|
|
70
82
|
wakeSessionWorkflow: WakeSessionWorkflowSignal | null;
|
|
71
83
|
/** Durable signalWithStart fallback used only after the activity's direct
|
|
@@ -76,6 +88,10 @@ export type ActivityServices = {
|
|
|
76
88
|
inspectSessionAttemptActivity: InspectSessionAttemptActivity | null;
|
|
77
89
|
/** Revision-carrying capacity nudge; generic outbox repair is also sufficient. */
|
|
78
90
|
signalCodexCapacityWorkflow?: SignalCodexCapacityWorkflow | null;
|
|
91
|
+
/** Production control workers inject this Temporal client edge. A null edge
|
|
92
|
+
* deliberately retains the composite implementation for embedded/test hosts. */
|
|
93
|
+
startSandboxReaperWorkflow?: StartSandboxReaperWorkflow | null;
|
|
94
|
+
startVideoGenerationWorkflow?: StartVideoGenerationWorkflow | null;
|
|
79
95
|
// §7.5 P3 — host-entitlements port, the WORKER half of the same seam the API
|
|
80
96
|
// edge exposes on `AppDependencies`. When set, `ensureRunAllowed` (turn-entry
|
|
81
97
|
// AND the mid-stream budget valve) delegates the funding decision to
|
|
@@ -108,6 +124,23 @@ export type ActivityServices = {
|
|
|
108
124
|
connectionCredentials?: ConnectionCredentialsPort | null;
|
|
109
125
|
};
|
|
110
126
|
|
|
127
|
+
/** Control workers own short database and maintenance activities. Document
|
|
128
|
+
* parsing is resolved lazily by the indexing activity itself. */
|
|
129
|
+
export type ControlActivityServices = SharedActivityServices;
|
|
130
|
+
|
|
131
|
+
/** Turn workers own the model loop and never construct document parsers. */
|
|
132
|
+
export type TurnActivityServices = SharedActivityServices & {
|
|
133
|
+
runtime: OpenGeniRuntime;
|
|
134
|
+
/** Provider-free test/profiling seam; production injects the real runtime summarizer. */
|
|
135
|
+
summarizeContextForCompaction: typeof import("@opengeni/runtime").summarizeForCompaction;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
/** Full test/embedded harness retained as a source-compatible superset. */
|
|
139
|
+
export type ActivityServices = ControlActivityServices &
|
|
140
|
+
TurnActivityServices & {
|
|
141
|
+
documentServices: DocumentServices;
|
|
142
|
+
};
|
|
143
|
+
|
|
111
144
|
export type CodexCapacityWaitRef = {
|
|
112
145
|
waiterId: string;
|
|
113
146
|
generation: number;
|
|
@@ -134,6 +167,8 @@ export type ReconcileCodexCapacityWaitResult =
|
|
|
134
167
|
| { action: "resumed" | "paused" | "superseded" | "stale" };
|
|
135
168
|
|
|
136
169
|
export type ActivityDependencies = Partial<ActivityServices>;
|
|
170
|
+
export type ControlActivityDependencies = Partial<ControlActivityServices>;
|
|
171
|
+
export type TurnActivityDependencies = Partial<TurnActivityServices>;
|
|
137
172
|
|
|
138
173
|
export type RunAgentTurnInput = {
|
|
139
174
|
accountId: string;
|
|
@@ -145,6 +180,17 @@ export type RunAgentTurnInput = {
|
|
|
145
180
|
trigger: { kind: "next" } | { kind: "approval"; triggerEventId: string };
|
|
146
181
|
};
|
|
147
182
|
|
|
183
|
+
export type VideoGenerationTerminalStatus =
|
|
184
|
+
| "completed"
|
|
185
|
+
| "provider_failed"
|
|
186
|
+
| "cancelled_before_submit"
|
|
187
|
+
| "outcome_unknown"
|
|
188
|
+
| "retention_failed";
|
|
189
|
+
|
|
190
|
+
export type VideoGenerationReconcileResult =
|
|
191
|
+
| { action: "waiting"; delayMs: number }
|
|
192
|
+
| { action: "terminal"; status: VideoGenerationTerminalStatus };
|
|
193
|
+
|
|
148
194
|
export type SettleSessionInterruptionsInput = {
|
|
149
195
|
accountId: string;
|
|
150
196
|
workspaceId: string;
|
|
@@ -268,7 +314,10 @@ export type DispatchScheduledTaskRunInput = {
|
|
|
268
314
|
initiator?: never;
|
|
269
315
|
}
|
|
270
316
|
| {
|
|
271
|
-
triggerType: Extract<
|
|
317
|
+
triggerType: Extract<
|
|
318
|
+
ScheduledTaskTriggerType,
|
|
319
|
+
"manual" | "initial" | "provider_event" | "retry" | "repair"
|
|
320
|
+
>;
|
|
272
321
|
agentRunUsageIdempotencyKey: string;
|
|
273
322
|
/** Exact identity used by the API-side charge for this same trigger. */
|
|
274
323
|
initiator: TurnInitiator;
|
|
@@ -283,7 +332,8 @@ export type DispatchScheduledTaskRunResult =
|
|
|
283
332
|
| "insufficient_credits"
|
|
284
333
|
| "monthly_model_cost_limit"
|
|
285
334
|
| "monthly_agent_run_limit"
|
|
286
|
-
| "malformed_manual_trigger"
|
|
335
|
+
| "malformed_manual_trigger"
|
|
336
|
+
| "knowledge_source_paused";
|
|
287
337
|
}
|
|
288
338
|
| {
|
|
289
339
|
action: "start" | "signal";
|
|
@@ -293,6 +343,38 @@ export type DispatchScheduledTaskRunResult =
|
|
|
293
343
|
triggerEventId: string;
|
|
294
344
|
workflowId: string;
|
|
295
345
|
workflowWakeRevision: number | null;
|
|
346
|
+
}
|
|
347
|
+
| {
|
|
348
|
+
action: "knowledge_source_sync";
|
|
349
|
+
accountId: string;
|
|
350
|
+
workspaceId: string;
|
|
351
|
+
taskId: string;
|
|
352
|
+
scheduledTaskRunId: string;
|
|
353
|
+
sourceId: string;
|
|
354
|
+
overlapPolicy: "skip" | "buffer_one";
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
export type RunKnowledgeSourceSyncBatchInput = {
|
|
358
|
+
accountId: string;
|
|
359
|
+
workspaceId: string;
|
|
360
|
+
taskId: string;
|
|
361
|
+
scheduledTaskRunId: string;
|
|
362
|
+
sourceId: string;
|
|
363
|
+
overlapPolicy: "skip" | "buffer_one";
|
|
364
|
+
};
|
|
365
|
+
|
|
366
|
+
export type RunKnowledgeSourceSyncBatchResult =
|
|
367
|
+
| { action: "continue" }
|
|
368
|
+
| {
|
|
369
|
+
action: "complete";
|
|
370
|
+
bufferedWake: boolean;
|
|
371
|
+
bufferedScheduledTaskRunId?: string | null;
|
|
372
|
+
}
|
|
373
|
+
| { action: "skipped" | "buffered" }
|
|
374
|
+
| {
|
|
375
|
+
action: "failed";
|
|
376
|
+
bufferedWake: boolean;
|
|
377
|
+
bufferedScheduledTaskRunId?: string | null;
|
|
296
378
|
};
|
|
297
379
|
|
|
298
380
|
type DocumentIndexIdentity = {
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import {
|
|
2
|
+
VideoGenerationAcceptedReceipt,
|
|
3
|
+
type GenerateVideoToolInput,
|
|
4
|
+
type VideoGenerationCapabilities,
|
|
5
|
+
type VideoGenerationPolicy,
|
|
6
|
+
} from "@opengeni/contracts";
|
|
7
|
+
import {
|
|
8
|
+
normalizeVideoGenerationRequest,
|
|
9
|
+
videoGenerationAdmissionKey,
|
|
10
|
+
videoGenerationProviderIdempotencyKey,
|
|
11
|
+
videoGenerationRequestDigest,
|
|
12
|
+
} from "@opengeni/core";
|
|
13
|
+
import { videoGenerationCapabilitiesForPolicy } from "@opengeni/core";
|
|
14
|
+
import {
|
|
15
|
+
admitVideoGenerationOperation,
|
|
16
|
+
encryptEnvironmentValue,
|
|
17
|
+
getVideoGenerationOperation,
|
|
18
|
+
markVideoGenerationPrepared,
|
|
19
|
+
type Database,
|
|
20
|
+
type VideoGenerationReference,
|
|
21
|
+
} from "@opengeni/db";
|
|
22
|
+
import {
|
|
23
|
+
calculateVideoGenerationCreditCostMicros,
|
|
24
|
+
environmentsEncryptionKeyBytes,
|
|
25
|
+
type Settings,
|
|
26
|
+
} from "@opengeni/config";
|
|
27
|
+
import type { ObjectStorage } from "@opengeni/storage";
|
|
28
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
29
|
+
import {
|
|
30
|
+
inspectSandboxVideoReferences,
|
|
31
|
+
uploadAndVerifyVideoReferences,
|
|
32
|
+
videoReferenceStagingKey,
|
|
33
|
+
type SandboxCommandRunner,
|
|
34
|
+
} from "./video-reference-staging";
|
|
35
|
+
import { encryptVideoGenerationApiKey } from "./video-generation-credential";
|
|
36
|
+
|
|
37
|
+
export type VideoGenerationCredentialLease = Readonly<{
|
|
38
|
+
fundingSource: "opengeni_credits" | "workspace_gateway";
|
|
39
|
+
connectionId: string | null;
|
|
40
|
+
version: number;
|
|
41
|
+
credentialEncrypted: string;
|
|
42
|
+
apiKey: string;
|
|
43
|
+
}>;
|
|
44
|
+
|
|
45
|
+
export type AcceptedVideoGeneration = Readonly<{
|
|
46
|
+
receipt: ReturnType<typeof VideoGenerationAcceptedReceipt.parse>;
|
|
47
|
+
operationId: string;
|
|
48
|
+
requestDigest: string;
|
|
49
|
+
}>;
|
|
50
|
+
|
|
51
|
+
export function videoCapabilitiesForTurn(input: {
|
|
52
|
+
policy: VideoGenerationPolicy;
|
|
53
|
+
credential: VideoGenerationCredentialLease;
|
|
54
|
+
}): VideoGenerationCapabilities {
|
|
55
|
+
if (input.policy.fundingSource !== input.credential.fundingSource) {
|
|
56
|
+
throw new Error("Video generation funding changed before capability resolution");
|
|
57
|
+
}
|
|
58
|
+
return videoGenerationCapabilitiesForPolicy({
|
|
59
|
+
policy: input.policy,
|
|
60
|
+
credentialVersion: input.credential.version,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function managedVideoGenerationCredentialLease(
|
|
65
|
+
settings: Settings,
|
|
66
|
+
): VideoGenerationCredentialLease | null {
|
|
67
|
+
if (!settings.vercelAiGatewayApiKey) return null;
|
|
68
|
+
const encryptionKey = environmentsEncryptionKeyBytes(settings);
|
|
69
|
+
if (!encryptionKey) {
|
|
70
|
+
throw new Error("Managed video generation requires OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
|
|
71
|
+
}
|
|
72
|
+
return Object.freeze({
|
|
73
|
+
fundingSource: "opengeni_credits",
|
|
74
|
+
connectionId: null,
|
|
75
|
+
version: 1,
|
|
76
|
+
credentialEncrypted: encryptVideoGenerationApiKey(
|
|
77
|
+
encryptionKey,
|
|
78
|
+
settings.vercelAiGatewayApiKey,
|
|
79
|
+
),
|
|
80
|
+
apiKey: settings.vercelAiGatewayApiKey,
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export async function admitVideoGenerationRequest(input: {
|
|
85
|
+
db: Database;
|
|
86
|
+
storage: ObjectStorage;
|
|
87
|
+
settings: Settings;
|
|
88
|
+
accountId: string;
|
|
89
|
+
workspaceId: string;
|
|
90
|
+
sessionId: string;
|
|
91
|
+
turnId: string;
|
|
92
|
+
attemptId: string;
|
|
93
|
+
toolCallId: string;
|
|
94
|
+
toolInput: GenerateVideoToolInput;
|
|
95
|
+
policy: VideoGenerationPolicy;
|
|
96
|
+
credential: VideoGenerationCredentialLease;
|
|
97
|
+
runCommand?: SandboxCommandRunner;
|
|
98
|
+
signal?: AbortSignal;
|
|
99
|
+
}): Promise<AcceptedVideoGeneration> {
|
|
100
|
+
const capabilities = videoCapabilitiesForTurn({
|
|
101
|
+
policy: input.policy,
|
|
102
|
+
credential: input.credential,
|
|
103
|
+
});
|
|
104
|
+
const selectedModelId = input.toolInput.modelId ?? capabilities.defaultModelId;
|
|
105
|
+
const model = capabilities.models.find((candidate) => candidate.modelId === selectedModelId);
|
|
106
|
+
if (!model) throw new Error("The requested video model is not enabled for this workspace");
|
|
107
|
+
|
|
108
|
+
const sourceMode = input.toolInput.source?.mode ?? "text";
|
|
109
|
+
if (sourceMode !== "text" && !input.runCommand) {
|
|
110
|
+
throw new Error("Video references require an active sandbox");
|
|
111
|
+
}
|
|
112
|
+
const inspected = input.runCommand
|
|
113
|
+
? await inspectSandboxVideoReferences({
|
|
114
|
+
request: input.toolInput,
|
|
115
|
+
runCommand: input.runCommand,
|
|
116
|
+
})
|
|
117
|
+
: [];
|
|
118
|
+
const canonical = normalizeVideoGenerationRequest({
|
|
119
|
+
toolInput: input.toolInput,
|
|
120
|
+
model,
|
|
121
|
+
sealedReferences: inspected.map((reference) => ({
|
|
122
|
+
role: reference.role,
|
|
123
|
+
contentSha256: reference.sha256,
|
|
124
|
+
contentType: reference.contentType,
|
|
125
|
+
byteSize: reference.sizeBytes,
|
|
126
|
+
})),
|
|
127
|
+
});
|
|
128
|
+
const requestDigest = videoGenerationRequestDigest(canonical);
|
|
129
|
+
const shouldDebitManagedCredits =
|
|
130
|
+
input.credential.fundingSource === "opengeni_credits" &&
|
|
131
|
+
(input.settings.billingMode === "stripe" || input.settings.usageLimitsMode === "managed");
|
|
132
|
+
const pricedCostMicros = shouldDebitManagedCredits
|
|
133
|
+
? calculateVideoGenerationCreditCostMicros(input.settings, canonical)
|
|
134
|
+
: 0;
|
|
135
|
+
const admissionKey = videoGenerationAdmissionKey(input);
|
|
136
|
+
const candidateOperationId = randomUUID();
|
|
137
|
+
const expectedArtifactId = randomUUID();
|
|
138
|
+
const expectedFileId = randomUUID();
|
|
139
|
+
const providerIdempotencyKey = videoGenerationProviderIdempotencyKey({
|
|
140
|
+
operationId: candidateOperationId,
|
|
141
|
+
requestDigest,
|
|
142
|
+
});
|
|
143
|
+
const encryptionKey = environmentsEncryptionKeyBytes(input.settings);
|
|
144
|
+
if (!encryptionKey) {
|
|
145
|
+
throw new Error("Video generation requires OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
|
|
146
|
+
}
|
|
147
|
+
const now = Date.now();
|
|
148
|
+
const recoveryDeadlineAt = new Date(now + input.settings.videoGenerationRecoveryDeadlineMs);
|
|
149
|
+
const stagingKeys = inspected.map((reference) =>
|
|
150
|
+
videoReferenceStagingKey({
|
|
151
|
+
workspaceId: input.workspaceId,
|
|
152
|
+
operationId: candidateOperationId,
|
|
153
|
+
ordinal: reference.ordinal,
|
|
154
|
+
sha256: reference.sha256,
|
|
155
|
+
}),
|
|
156
|
+
);
|
|
157
|
+
const admitted = await admitVideoGenerationOperation(input.db, {
|
|
158
|
+
id: candidateOperationId,
|
|
159
|
+
accountId: input.accountId,
|
|
160
|
+
workspaceId: input.workspaceId,
|
|
161
|
+
sessionId: input.sessionId,
|
|
162
|
+
turnId: input.turnId,
|
|
163
|
+
attemptId: input.attemptId,
|
|
164
|
+
toolCallId: input.toolCallId,
|
|
165
|
+
admissionKey,
|
|
166
|
+
requestDigest,
|
|
167
|
+
promptDigest: createHash("sha256").update(canonical.prompt).digest("hex"),
|
|
168
|
+
requestEncrypted: encryptEnvironmentValue(encryptionKey, JSON.stringify(canonical)),
|
|
169
|
+
modelId: canonical.modelId,
|
|
170
|
+
sourceMode: canonical.sourceMode,
|
|
171
|
+
capabilityRevision: capabilities.capabilityRevision,
|
|
172
|
+
policyRevision: input.policy.revision,
|
|
173
|
+
fundingSource: input.credential.fundingSource,
|
|
174
|
+
pricedCostMicros,
|
|
175
|
+
connectionId: input.credential.connectionId,
|
|
176
|
+
credentialVersion: input.credential.version,
|
|
177
|
+
credentialEncrypted: input.credential.credentialEncrypted,
|
|
178
|
+
providerIdempotencyKey,
|
|
179
|
+
expectedArtifactId,
|
|
180
|
+
expectedFileId,
|
|
181
|
+
workspaceQuotaBytes: input.settings.videoGenerationWorkspaceQuotaBytes,
|
|
182
|
+
maxConcurrentPerWorkspace: input.settings.videoGenerationMaxConcurrentPerWorkspace,
|
|
183
|
+
recoveryDeadlineAt,
|
|
184
|
+
references: inspected.map((reference, index) => ({
|
|
185
|
+
ordinal: reference.ordinal,
|
|
186
|
+
role: reference.role,
|
|
187
|
+
contentType: reference.contentType,
|
|
188
|
+
sizeBytes: reference.sizeBytes,
|
|
189
|
+
sha256: reference.sha256,
|
|
190
|
+
stagingObjectKey: stagingKeys[index]!,
|
|
191
|
+
grantExpiresAt: null,
|
|
192
|
+
cleanupAfter: recoveryDeadlineAt,
|
|
193
|
+
})),
|
|
194
|
+
});
|
|
195
|
+
const operation = await getVideoGenerationOperation(
|
|
196
|
+
input.db,
|
|
197
|
+
input.workspaceId,
|
|
198
|
+
admitted.operation.id,
|
|
199
|
+
);
|
|
200
|
+
if (!operation) throw new Error("Admitted video generation operation disappeared");
|
|
201
|
+
if (operation.status === "preparing") {
|
|
202
|
+
assertStoredReferencePlan(operation.references, inspected);
|
|
203
|
+
if (inspected.length > 0) {
|
|
204
|
+
if (!input.runCommand) throw new Error("Prepared video references lost their sandbox");
|
|
205
|
+
await uploadAndVerifyVideoReferences({
|
|
206
|
+
storage: input.storage,
|
|
207
|
+
references: inspected,
|
|
208
|
+
stagingKeys: operation.references.map((reference) => {
|
|
209
|
+
if (!reference.stagingObjectKey) {
|
|
210
|
+
throw new Error("Video reference staging key is missing");
|
|
211
|
+
}
|
|
212
|
+
return reference.stagingObjectKey;
|
|
213
|
+
}),
|
|
214
|
+
runCommand: input.runCommand,
|
|
215
|
+
tempRoot: input.settings.videoGenerationTempDirectory,
|
|
216
|
+
ffprobePath: input.settings.videoGenerationFfprobePath,
|
|
217
|
+
uploadTtlSeconds: input.settings.videoGenerationReferenceUrlTtlSeconds,
|
|
218
|
+
...(input.signal ? { signal: input.signal } : {}),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
await markVideoGenerationPrepared(input.db, {
|
|
222
|
+
accountId: input.accountId,
|
|
223
|
+
workspaceId: input.workspaceId,
|
|
224
|
+
operationId: operation.id,
|
|
225
|
+
requestDigest,
|
|
226
|
+
});
|
|
227
|
+
} else if (
|
|
228
|
+
![
|
|
229
|
+
"prepared",
|
|
230
|
+
"accepted",
|
|
231
|
+
"submission_uncertain",
|
|
232
|
+
"provider_started",
|
|
233
|
+
"retaining",
|
|
234
|
+
"completed",
|
|
235
|
+
].includes(operation.status)
|
|
236
|
+
) {
|
|
237
|
+
throw new Error("The existing video generation request cannot be resumed");
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
receipt: VideoGenerationAcceptedReceipt.parse({
|
|
241
|
+
schemaVersion: 1,
|
|
242
|
+
status: "accepted",
|
|
243
|
+
operationId: operation.id,
|
|
244
|
+
}),
|
|
245
|
+
operationId: operation.id,
|
|
246
|
+
requestDigest,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function assertStoredReferencePlan(
|
|
251
|
+
stored: VideoGenerationReference[],
|
|
252
|
+
inspected: Awaited<ReturnType<typeof inspectSandboxVideoReferences>>,
|
|
253
|
+
): void {
|
|
254
|
+
if (
|
|
255
|
+
stored.length !== inspected.length ||
|
|
256
|
+
stored.some((reference, index) => {
|
|
257
|
+
const source = inspected[index];
|
|
258
|
+
return (
|
|
259
|
+
!source ||
|
|
260
|
+
reference.ordinal !== source.ordinal ||
|
|
261
|
+
reference.role !== source.role ||
|
|
262
|
+
reference.contentType !== source.contentType ||
|
|
263
|
+
reference.sizeBytes !== source.sizeBytes ||
|
|
264
|
+
reference.sha256 !== source.sha256
|
|
265
|
+
);
|
|
266
|
+
})
|
|
267
|
+
) {
|
|
268
|
+
throw new Error("Video reference bytes changed across tool-call recovery");
|
|
269
|
+
}
|
|
270
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { decryptEnvironmentValue, encryptEnvironmentValue } from "@opengeni/db";
|
|
2
|
+
|
|
3
|
+
/** One frozen, provider-neutral credential envelope for every video funding route. */
|
|
4
|
+
export function encryptVideoGenerationApiKey(key: Uint8Array, apiKey: string): string {
|
|
5
|
+
if (!apiKey.trim()) throw new Error("Video provider credential is empty");
|
|
6
|
+
return encryptEnvironmentValue(key, JSON.stringify({ apiKey }));
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Decrypt without ever attaching parser/decryption errors: JSON parser errors
|
|
11
|
+
* may quote plaintext and must not enter Temporal failure payloads or logs.
|
|
12
|
+
*/
|
|
13
|
+
export function decryptVideoGenerationApiKey(key: Uint8Array, stored: string): string {
|
|
14
|
+
let plaintext: string;
|
|
15
|
+
try {
|
|
16
|
+
plaintext = decryptEnvironmentValue(key, stored);
|
|
17
|
+
} catch {
|
|
18
|
+
throw new Error("Video provider credential lease could not be decrypted");
|
|
19
|
+
}
|
|
20
|
+
let decoded: unknown;
|
|
21
|
+
try {
|
|
22
|
+
decoded = JSON.parse(plaintext);
|
|
23
|
+
} catch {
|
|
24
|
+
throw new Error("Video provider credential lease is malformed");
|
|
25
|
+
}
|
|
26
|
+
const apiKey =
|
|
27
|
+
decoded && typeof decoded === "object" && !Array.isArray(decoded)
|
|
28
|
+
? (decoded as Record<string, unknown>).apiKey
|
|
29
|
+
: null;
|
|
30
|
+
if (typeof apiKey !== "string" || !apiKey.trim()) {
|
|
31
|
+
throw new Error("Video provider credential lease is malformed");
|
|
32
|
+
}
|
|
33
|
+
return apiKey;
|
|
34
|
+
}
|