@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.
Files changed (188) hide show
  1. package/dist/activities/agent-run-admission.d.ts +12 -0
  2. package/dist/activities/agent-turn.d.ts +110 -19
  3. package/dist/activities/api-integrations.d.ts +21 -0
  4. package/dist/activities/browser-state-artifact-reaper.d.ts +29 -0
  5. package/dist/activities/capabilities.d.ts +6 -0
  6. package/dist/activities/codemode-dispatcher.d.ts +35 -0
  7. package/dist/activities/codemode-token-renewal.d.ts +23 -0
  8. package/dist/activities/codex-capacity.d.ts +6 -5
  9. package/dist/activities/codex-image-generation.d.ts +21 -0
  10. package/dist/activities/context-compaction.d.ts +1 -0
  11. package/dist/activities/documents.d.ts +3 -2
  12. package/dist/activities/environment.d.ts +17 -8
  13. package/dist/activities/expiring-material-renewal.d.ts +1 -1
  14. package/dist/activities/file-upload-reaper.d.ts +7 -2
  15. package/dist/activities/gateway-image-generation.d.ts +39 -0
  16. package/dist/activities/gateway-video-generation.d.ts +53 -0
  17. package/dist/activities/generated-images.d.ts +112 -0
  18. package/dist/activities/git-credential-renewal.d.ts +1 -1
  19. package/dist/activities/goals.d.ts +2 -2
  20. package/dist/activities/google-drive-changes.d.ts +99 -0
  21. package/dist/activities/image-generation-operation.d.ts +74 -0
  22. package/dist/activities/image-generation-references.d.ts +25 -0
  23. package/dist/activities/knowledge-source-sync-driver.d.ts +23 -0
  24. package/dist/activities/knowledge-source-sync.d.ts +75 -0
  25. package/dist/activities/packs.d.ts +21 -36
  26. package/dist/activities/parent-wake.d.ts +1 -1
  27. package/dist/activities/retained-screenshot-reaper.d.ts +2 -2
  28. package/dist/activities/retained-screenshots.d.ts +36 -0
  29. package/dist/activities/rig-verification.d.ts +82 -5
  30. package/dist/activities/run-credential-renewal.d.ts +1 -1
  31. package/dist/activities/run-input.d.ts +3 -1
  32. package/dist/activities/sandbox-images.d.ts +14 -0
  33. package/dist/activities/sandbox-lease.d.ts +47 -9
  34. package/dist/activities/scheduled-tasks.d.ts +2 -2
  35. package/dist/activities/session-state.d.ts +7 -5
  36. package/dist/activities/site-auth-maintenance.d.ts +27 -0
  37. package/dist/activities/streaming.d.ts +1 -1
  38. package/dist/activities/types.d.ts +87 -5
  39. package/dist/activities/video-generation-admission.d.ts +43 -0
  40. package/dist/activities/video-generation-credential.d.ts +24 -0
  41. package/dist/activities/video-generation-reconciliation.d.ts +13 -0
  42. package/dist/activities/video-media-validation.d.ts +34 -0
  43. package/dist/activities/video-output-retention.d.ts +36 -0
  44. package/dist/activities/video-reference-staging.d.ts +55 -0
  45. package/dist/activities/workflow-wake.d.ts +2 -2
  46. package/dist/activities/workspace-capture.d.ts +10 -1
  47. package/dist/activities/xai-auth.d.ts +23 -0
  48. package/dist/activities/xai-image-generation.d.ts +26 -0
  49. package/dist/activities/xai-video-generation.d.ts +18 -0
  50. package/dist/activities-control-XURE4VKN.js +7221 -0
  51. package/dist/activities-control-XURE4VKN.js.map +1 -0
  52. package/dist/activities-control.d.ts +297 -0
  53. package/dist/activities-turn-6EBXGGLS.js +16036 -0
  54. package/dist/activities-turn-6EBXGGLS.js.map +1 -0
  55. package/dist/activities-turn.d.ts +17 -0
  56. package/dist/activities.d.ts +138 -150
  57. package/dist/activity-services.d.ts +10 -0
  58. package/dist/artifact-materializer-entry.d.ts +6 -0
  59. package/dist/artifact-materializer-entry.js +2662 -0
  60. package/dist/artifact-materializer-entry.js.map +1 -0
  61. package/dist/artifact-outbox-entry.d.ts +2 -0
  62. package/dist/artifact-outbox-entry.js +681 -0
  63. package/dist/artifact-outbox-entry.js.map +1 -0
  64. package/dist/chunk-5GRJIP5H.js +3092 -0
  65. package/dist/chunk-5GRJIP5H.js.map +1 -0
  66. package/dist/chunk-GRFQTTSQ.js +448 -0
  67. package/dist/chunk-GRFQTTSQ.js.map +1 -0
  68. package/dist/chunk-TYNPCG4U.js +906 -0
  69. package/dist/chunk-TYNPCG4U.js.map +1 -0
  70. package/dist/concurrency.d.ts +108 -5
  71. package/dist/editable-artifact-hint-broker.d.ts +49 -0
  72. package/dist/editable-artifact-materializer-launcher.d.ts +49 -0
  73. package/dist/editable-artifact-materializer-service.d.ts +108 -0
  74. package/dist/editable-artifact-materializer-storage.d.ts +12 -0
  75. package/dist/editable-artifact-materializer-subprocess.d.ts +50 -0
  76. package/dist/editable-artifact-materializer.d.ts +262 -0
  77. package/dist/editable-artifact-outbox-dispatcher.d.ts +44 -0
  78. package/dist/editable-artifact-outbox-service.d.ts +70 -0
  79. package/dist/host-export.d.ts +1 -1
  80. package/dist/index.d.ts +189 -10
  81. package/dist/index.js +715 -17380
  82. package/dist/index.js.map +1 -1
  83. package/dist/memory-pressure-guard.d.ts +57 -0
  84. package/dist/model-checkpoint-memory-collector.d.ts +23 -0
  85. package/dist/model-context-contributions.d.ts +40 -0
  86. package/dist/observability-metrics.d.ts +53 -1
  87. package/dist/sandbox-reaper-contract.d.ts +40 -0
  88. package/dist/sandbox-reaper-timeout.d.ts +14 -0
  89. package/dist/sandbox-resume.d.ts +34 -7
  90. package/dist/sandbox-routing.d.ts +6 -0
  91. package/dist/worker-service-lifecycle.d.ts +6 -2
  92. package/dist/workflow-bundle.js +1094 -370
  93. package/dist/workflows/activities.d.ts +8 -2
  94. package/dist/workflows/document-index.d.ts +1 -1
  95. package/dist/workflows/knowledge-source-sync.d.ts +13 -0
  96. package/dist/workflows/rig-verification.d.ts +1 -1
  97. package/dist/workflows/sandbox-reaper.d.ts +8 -0
  98. package/dist/workflows/scheduled-tasks.d.ts +1 -1
  99. package/dist/workflows/session.d.ts +2 -2
  100. package/dist/workflows/site-auth-maintenance.d.ts +2 -0
  101. package/dist/workflows/video-generation.d.ts +10 -0
  102. package/dist/workflows/workflow-wake.d.ts +1 -1
  103. package/dist/workflows.d.ts +10 -7
  104. package/package.json +35 -15
  105. package/src/activities/agent-run-admission.ts +76 -0
  106. package/src/activities/agent-turn.ts +2600 -340
  107. package/src/activities/api-integrations.ts +138 -0
  108. package/src/activities/browser-state-artifact-reaper.ts +120 -0
  109. package/src/activities/capabilities.ts +10 -0
  110. package/src/activities/codemode-dispatcher.ts +335 -0
  111. package/src/activities/codemode-token-renewal.ts +64 -0
  112. package/src/activities/codex-capacity.ts +54 -7
  113. package/src/activities/codex-image-generation.ts +56 -0
  114. package/src/activities/context-compaction.ts +38 -3
  115. package/src/activities/documents.ts +14 -4
  116. package/src/activities/environment.ts +54 -45
  117. package/src/activities/expiring-material-renewal.ts +1 -1
  118. package/src/activities/file-upload-reaper.ts +34 -3
  119. package/src/activities/gateway-image-generation.ts +146 -0
  120. package/src/activities/gateway-video-generation.ts +298 -0
  121. package/src/activities/generated-images.ts +860 -0
  122. package/src/activities/goals.ts +8 -3
  123. package/src/activities/google-drive-changes.ts +454 -0
  124. package/src/activities/image-generation-operation.ts +333 -0
  125. package/src/activities/image-generation-references.ts +118 -0
  126. package/src/activities/knowledge-source-sync-driver.ts +27 -0
  127. package/src/activities/knowledge-source-sync.ts +2840 -0
  128. package/src/activities/mcp-credentials.ts +1 -0
  129. package/src/activities/packs.ts +75 -152
  130. package/src/activities/parent-wake.ts +13 -1
  131. package/src/activities/retained-screenshot-reaper.ts +2 -2
  132. package/src/activities/retained-screenshots.ts +453 -28
  133. package/src/activities/rig-verification.ts +554 -43
  134. package/src/activities/run-input.ts +22 -6
  135. package/src/activities/sandbox-images.ts +167 -0
  136. package/src/activities/sandbox-lease.ts +1065 -483
  137. package/src/activities/scheduled-tasks.ts +104 -74
  138. package/src/activities/session-state.ts +22 -4
  139. package/src/activities/site-auth-maintenance.ts +269 -0
  140. package/src/activities/streaming.ts +42 -3
  141. package/src/activities/types.ts +104 -5
  142. package/src/activities/video-generation-admission.ts +303 -0
  143. package/src/activities/video-generation-credential.ts +112 -0
  144. package/src/activities/video-generation-reconciliation.ts +771 -0
  145. package/src/activities/video-media-validation.ts +332 -0
  146. package/src/activities/video-output-retention.ts +314 -0
  147. package/src/activities/video-reference-staging.ts +350 -0
  148. package/src/activities/workflow-wake.ts +2 -2
  149. package/src/activities/workspace-capture.ts +44 -10
  150. package/src/activities/xai-auth.ts +122 -0
  151. package/src/activities/xai-image-generation.ts +86 -0
  152. package/src/activities/xai-video-generation.ts +69 -0
  153. package/src/activities-control.ts +58 -0
  154. package/src/activities-turn.ts +40 -0
  155. package/src/activities.ts +53 -124
  156. package/src/activity-services.ts +62 -0
  157. package/src/artifact-materializer-entry.ts +35 -0
  158. package/src/artifact-outbox-entry.ts +29 -0
  159. package/src/concurrency.ts +484 -6
  160. package/src/editable-artifact-hint-broker.ts +235 -0
  161. package/src/editable-artifact-materializer-launcher.ts +515 -0
  162. package/src/editable-artifact-materializer-service.ts +787 -0
  163. package/src/editable-artifact-materializer-storage.ts +413 -0
  164. package/src/editable-artifact-materializer-subprocess.ts +956 -0
  165. package/src/editable-artifact-materializer.ts +1289 -0
  166. package/src/editable-artifact-outbox-dispatcher.ts +244 -0
  167. package/src/editable-artifact-outbox-service.ts +438 -0
  168. package/src/index.ts +328 -72
  169. package/src/memory-pressure-guard.ts +489 -0
  170. package/src/model-checkpoint-memory-collector.ts +123 -0
  171. package/src/model-context-contributions.ts +159 -0
  172. package/src/observability-metrics.ts +297 -13
  173. package/src/sandbox-reaper-contract.ts +73 -0
  174. package/src/sandbox-reaper-timeout.ts +79 -0
  175. package/src/sandbox-resume.ts +294 -153
  176. package/src/sandbox-routing.ts +32 -24
  177. package/src/worker-service-lifecycle.ts +80 -14
  178. package/src/workflows/activities.ts +31 -0
  179. package/src/workflows/file-upload-reaper.ts +9 -1
  180. package/src/workflows/knowledge-source-sync.ts +83 -0
  181. package/src/workflows/sandbox-reaper.ts +183 -21
  182. package/src/workflows/scheduled-tasks.ts +42 -6
  183. package/src/workflows/session.ts +20 -6
  184. package/src/workflows/site-auth-maintenance.ts +14 -0
  185. package/src/workflows/video-generation.ts +36 -0
  186. package/src/workflows.ts +18 -1
  187. package/dist/activities/toolspace-token-renewal.d.ts +0 -23
  188. package/src/activities/toolspace-token-renewal.ts +0 -64
@@ -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 = setInterval(() => {
185
+ const timer = schedule(() => {
184
186
  context.heartbeat({ ...details, at: new Date().toISOString() });
185
- }, 10_000);
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
- return await Promise.race([iterator.next(), context.cancelled]);
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
  }
@@ -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
- export type ActivityServices = {
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,13 +124,34 @@ 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 = {
145
+ /** Absent only for Temporal histories written before provider-tagged waits. */
146
+ provider?: "codex" | "xai";
112
147
  waiterId: string;
113
148
  generation: number;
114
149
  nextCheckAt: string;
115
150
  wakeRevision: number;
116
151
  };
117
152
 
153
+ export type XaiCapacityWaitRef = CodexCapacityWaitRef & { provider: "xai" };
154
+
118
155
  export type GetCodexCapacityWaitInput = {
119
156
  workspaceId: string;
120
157
  sessionId: string;
@@ -127,6 +164,8 @@ export type ReconcileCodexCapacityWaitInput = {
127
164
  waiterId: string;
128
165
  generation: number;
129
166
  cause: "timer" | "signal" | "queue" | "recovery";
167
+ /** Absent only for Codex waits and pre-provider-tagged workflow histories. */
168
+ provider?: "codex" | "xai";
130
169
  };
131
170
 
132
171
  export type ReconcileCodexCapacityWaitResult =
@@ -134,6 +173,8 @@ export type ReconcileCodexCapacityWaitResult =
134
173
  | { action: "resumed" | "paused" | "superseded" | "stale" };
135
174
 
136
175
  export type ActivityDependencies = Partial<ActivityServices>;
176
+ export type ControlActivityDependencies = Partial<ControlActivityServices>;
177
+ export type TurnActivityDependencies = Partial<TurnActivityServices>;
137
178
 
138
179
  export type RunAgentTurnInput = {
139
180
  accountId: string;
@@ -145,6 +186,17 @@ export type RunAgentTurnInput = {
145
186
  trigger: { kind: "next" } | { kind: "approval"; triggerEventId: string };
146
187
  };
147
188
 
189
+ export type VideoGenerationTerminalStatus =
190
+ | "completed"
191
+ | "provider_failed"
192
+ | "cancelled_before_submit"
193
+ | "outcome_unknown"
194
+ | "retention_failed";
195
+
196
+ export type VideoGenerationReconcileResult =
197
+ | { action: "waiting"; delayMs: number }
198
+ | { action: "terminal"; status: VideoGenerationTerminalStatus };
199
+
148
200
  export type SettleSessionInterruptionsInput = {
149
201
  accountId: string;
150
202
  workspaceId: string;
@@ -240,6 +292,17 @@ export type ExpireSessionHumanInputResult = {
240
292
  action: "expired" | "stale" | "not_found";
241
293
  };
242
294
 
295
+ export type ExpireSessionInteractionInterventionInput = {
296
+ accountId: string;
297
+ workspaceId: string;
298
+ sessionId: string;
299
+ interventionId: string;
300
+ };
301
+
302
+ export type ExpireSessionInteractionInterventionResult = {
303
+ action: "expired" | "stale" | "not_found";
304
+ };
305
+
243
306
  export type MarkSessionIdleInput = {
244
307
  workspaceId: string;
245
308
  sessionId: string;
@@ -268,7 +331,10 @@ export type DispatchScheduledTaskRunInput = {
268
331
  initiator?: never;
269
332
  }
270
333
  | {
271
- triggerType: Extract<ScheduledTaskTriggerType, "manual">;
334
+ triggerType: Extract<
335
+ ScheduledTaskTriggerType,
336
+ "manual" | "initial" | "provider_event" | "retry" | "repair"
337
+ >;
272
338
  agentRunUsageIdempotencyKey: string;
273
339
  /** Exact identity used by the API-side charge for this same trigger. */
274
340
  initiator: TurnInitiator;
@@ -283,7 +349,8 @@ export type DispatchScheduledTaskRunResult =
283
349
  | "insufficient_credits"
284
350
  | "monthly_model_cost_limit"
285
351
  | "monthly_agent_run_limit"
286
- | "malformed_manual_trigger";
352
+ | "malformed_manual_trigger"
353
+ | "knowledge_source_paused";
287
354
  }
288
355
  | {
289
356
  action: "start" | "signal";
@@ -293,6 +360,38 @@ export type DispatchScheduledTaskRunResult =
293
360
  triggerEventId: string;
294
361
  workflowId: string;
295
362
  workflowWakeRevision: number | null;
363
+ }
364
+ | {
365
+ action: "knowledge_source_sync";
366
+ accountId: string;
367
+ workspaceId: string;
368
+ taskId: string;
369
+ scheduledTaskRunId: string;
370
+ sourceId: string;
371
+ overlapPolicy: "skip" | "buffer_one";
372
+ };
373
+
374
+ export type RunKnowledgeSourceSyncBatchInput = {
375
+ accountId: string;
376
+ workspaceId: string;
377
+ taskId: string;
378
+ scheduledTaskRunId: string;
379
+ sourceId: string;
380
+ overlapPolicy: "skip" | "buffer_one";
381
+ };
382
+
383
+ export type RunKnowledgeSourceSyncBatchResult =
384
+ | { action: "continue" }
385
+ | {
386
+ action: "complete";
387
+ bufferedWake: boolean;
388
+ bufferedScheduledTaskRunId?: string | null;
389
+ }
390
+ | { action: "skipped" | "buffered" }
391
+ | {
392
+ action: "failed";
393
+ bufferedWake: boolean;
394
+ bufferedScheduledTaskRunId?: string | null;
296
395
  };
297
396
 
298
397
  type DocumentIndexIdentity = {
@@ -0,0 +1,303 @@
1
+ import {
2
+ VideoGenerationAcceptedReceipt,
3
+ type GenerateVideoToolInput,
4
+ type VideoGenerationCapabilities,
5
+ type VideoGenerationPolicy,
6
+ type XaiProviderAccountAuthoritySnapshotV1,
7
+ } from "@opengeni/contracts";
8
+ import {
9
+ normalizeVideoGenerationRequest,
10
+ videoGenerationAdmissionKey,
11
+ videoGenerationProviderIdempotencyKey,
12
+ videoGenerationRequestDigest,
13
+ } from "@opengeni/core";
14
+ import { videoGenerationCapabilitiesForPolicy } from "@opengeni/core";
15
+ import {
16
+ admitVideoGenerationOperation,
17
+ encryptEnvironmentValue,
18
+ getVideoGenerationOperation,
19
+ markVideoGenerationPrepared,
20
+ type Database,
21
+ type VideoGenerationReference,
22
+ type XaiCredentialForRun,
23
+ } from "@opengeni/db";
24
+ import {
25
+ calculateVideoGenerationCreditCostMicros,
26
+ environmentsEncryptionKeyBytes,
27
+ type Settings,
28
+ } from "@opengeni/config";
29
+ import type { ObjectStorage } from "@opengeni/storage";
30
+ import { createHash, randomUUID } from "node:crypto";
31
+ import {
32
+ inspectSandboxVideoReferences,
33
+ uploadAndVerifyVideoReferences,
34
+ videoReferenceStagingKey,
35
+ type SandboxCommandRunner,
36
+ } from "./video-reference-staging";
37
+ import {
38
+ encryptVideoGenerationApiKey,
39
+ encryptVideoGenerationXaiCredential,
40
+ } from "./video-generation-credential";
41
+
42
+ export type VideoGenerationCredentialLease = Readonly<{
43
+ fundingSource: "opengeni_credits" | "workspace_gateway" | "supergrok_subscription";
44
+ connectionId: string | null;
45
+ version: number;
46
+ credentialEncrypted: string;
47
+ }>;
48
+
49
+ export type AcceptedVideoGeneration = Readonly<{
50
+ receipt: ReturnType<typeof VideoGenerationAcceptedReceipt.parse>;
51
+ operationId: string;
52
+ requestDigest: string;
53
+ }>;
54
+
55
+ export function videoCapabilitiesForTurn(input: {
56
+ policy: VideoGenerationPolicy;
57
+ credential: VideoGenerationCredentialLease;
58
+ }): VideoGenerationCapabilities {
59
+ if (input.policy.fundingSource !== input.credential.fundingSource) {
60
+ throw new Error("Video generation funding changed before capability resolution");
61
+ }
62
+ return videoGenerationCapabilitiesForPolicy({
63
+ policy: input.policy,
64
+ credentialVersion: input.credential.version,
65
+ });
66
+ }
67
+
68
+ export function managedVideoGenerationCredentialLease(
69
+ settings: Settings,
70
+ ): VideoGenerationCredentialLease | null {
71
+ if (!settings.vercelAiGatewayApiKey) return null;
72
+ const encryptionKey = environmentsEncryptionKeyBytes(settings);
73
+ if (!encryptionKey) {
74
+ throw new Error("Managed video generation requires OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
75
+ }
76
+ return Object.freeze({
77
+ fundingSource: "opengeni_credits",
78
+ connectionId: null,
79
+ version: 1,
80
+ credentialEncrypted: encryptVideoGenerationApiKey(
81
+ encryptionKey,
82
+ settings.vercelAiGatewayApiKey,
83
+ ),
84
+ });
85
+ }
86
+
87
+ export function xaiVideoGenerationCredentialLease(input: {
88
+ settings: Settings;
89
+ credential: XaiCredentialForRun;
90
+ subjectId: string;
91
+ authoritySnapshot: XaiProviderAccountAuthoritySnapshotV1;
92
+ }): VideoGenerationCredentialLease {
93
+ const encryptionKey = environmentsEncryptionKeyBytes(input.settings);
94
+ if (!encryptionKey) {
95
+ throw new Error("SuperGrok video generation requires OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
96
+ }
97
+ const { accessToken, refreshToken } = input.credential.secret;
98
+ const userId = input.credential.providerAccountId;
99
+ if (!accessToken || !refreshToken || !userId) {
100
+ throw new Error("The selected SuperGrok account cannot fund video generation");
101
+ }
102
+ return Object.freeze({
103
+ fundingSource: "supergrok_subscription",
104
+ connectionId: null,
105
+ version: input.credential.version,
106
+ credentialEncrypted: encryptVideoGenerationXaiCredential(encryptionKey, {
107
+ accessToken,
108
+ refreshToken,
109
+ userId,
110
+ credentialId: input.credential.id,
111
+ subjectId: input.subjectId,
112
+ authoritySnapshot: input.authoritySnapshot,
113
+ }),
114
+ });
115
+ }
116
+
117
+ export async function admitVideoGenerationRequest(input: {
118
+ db: Database;
119
+ storage: ObjectStorage;
120
+ settings: Settings;
121
+ accountId: string;
122
+ workspaceId: string;
123
+ sessionId: string;
124
+ turnId: string;
125
+ attemptId: string;
126
+ toolCallId: string;
127
+ toolInput: GenerateVideoToolInput;
128
+ policy: VideoGenerationPolicy;
129
+ credential: VideoGenerationCredentialLease;
130
+ runCommand?: SandboxCommandRunner;
131
+ signal?: AbortSignal;
132
+ }): Promise<AcceptedVideoGeneration> {
133
+ const capabilities = videoCapabilitiesForTurn({
134
+ policy: input.policy,
135
+ credential: input.credential,
136
+ });
137
+ const selectedModelId = input.toolInput.modelId ?? capabilities.defaultModelId;
138
+ const model = capabilities.models.find((candidate) => candidate.modelId === selectedModelId);
139
+ if (!model) throw new Error("The requested video model is not enabled for this workspace");
140
+
141
+ const sourceMode = input.toolInput.source?.mode ?? "text";
142
+ if (sourceMode !== "text" && !input.runCommand) {
143
+ throw new Error("Video references require an active sandbox");
144
+ }
145
+ const inspected = input.runCommand
146
+ ? await inspectSandboxVideoReferences({
147
+ request: input.toolInput,
148
+ runCommand: input.runCommand,
149
+ })
150
+ : [];
151
+ const canonical = normalizeVideoGenerationRequest({
152
+ toolInput: input.toolInput,
153
+ model,
154
+ sealedReferences: inspected.map((reference) => ({
155
+ role: reference.role,
156
+ contentSha256: reference.sha256,
157
+ contentType: reference.contentType,
158
+ byteSize: reference.sizeBytes,
159
+ })),
160
+ });
161
+ const requestDigest = videoGenerationRequestDigest(canonical);
162
+ const shouldDebitManagedCredits =
163
+ input.credential.fundingSource === "opengeni_credits" &&
164
+ (input.settings.billingMode === "stripe" || input.settings.usageLimitsMode === "managed");
165
+ const pricedCostMicros = shouldDebitManagedCredits
166
+ ? calculateVideoGenerationCreditCostMicros(input.settings, canonical)
167
+ : 0;
168
+ const admissionKey = videoGenerationAdmissionKey(input);
169
+ const candidateOperationId = randomUUID();
170
+ const expectedArtifactId = randomUUID();
171
+ const expectedFileId = randomUUID();
172
+ const providerIdempotencyKey = videoGenerationProviderIdempotencyKey({
173
+ operationId: candidateOperationId,
174
+ requestDigest,
175
+ });
176
+ const encryptionKey = environmentsEncryptionKeyBytes(input.settings);
177
+ if (!encryptionKey) {
178
+ throw new Error("Video generation requires OPENGENI_ENVIRONMENTS_ENCRYPTION_KEY");
179
+ }
180
+ const now = Date.now();
181
+ const recoveryDeadlineAt = new Date(now + input.settings.videoGenerationRecoveryDeadlineMs);
182
+ const stagingKeys = inspected.map((reference) =>
183
+ videoReferenceStagingKey({
184
+ workspaceId: input.workspaceId,
185
+ operationId: candidateOperationId,
186
+ ordinal: reference.ordinal,
187
+ sha256: reference.sha256,
188
+ }),
189
+ );
190
+ const admitted = await admitVideoGenerationOperation(input.db, {
191
+ id: candidateOperationId,
192
+ accountId: input.accountId,
193
+ workspaceId: input.workspaceId,
194
+ sessionId: input.sessionId,
195
+ turnId: input.turnId,
196
+ attemptId: input.attemptId,
197
+ toolCallId: input.toolCallId,
198
+ admissionKey,
199
+ requestDigest,
200
+ promptDigest: createHash("sha256").update(canonical.prompt).digest("hex"),
201
+ requestEncrypted: encryptEnvironmentValue(encryptionKey, JSON.stringify(canonical)),
202
+ modelId: canonical.modelId,
203
+ sourceMode: canonical.sourceMode,
204
+ capabilityRevision: capabilities.capabilityRevision,
205
+ policyRevision: input.policy.revision,
206
+ fundingSource: input.credential.fundingSource,
207
+ pricedCostMicros,
208
+ connectionId: input.credential.connectionId,
209
+ credentialVersion: input.credential.version,
210
+ credentialEncrypted: input.credential.credentialEncrypted,
211
+ providerIdempotencyKey,
212
+ expectedArtifactId,
213
+ expectedFileId,
214
+ workspaceQuotaBytes: input.settings.videoGenerationWorkspaceQuotaBytes,
215
+ maxConcurrentPerWorkspace: input.settings.videoGenerationMaxConcurrentPerWorkspace,
216
+ recoveryDeadlineAt,
217
+ references: inspected.map((reference, index) => ({
218
+ ordinal: reference.ordinal,
219
+ role: reference.role,
220
+ contentType: reference.contentType,
221
+ sizeBytes: reference.sizeBytes,
222
+ sha256: reference.sha256,
223
+ stagingObjectKey: stagingKeys[index]!,
224
+ grantExpiresAt: null,
225
+ cleanupAfter: recoveryDeadlineAt,
226
+ })),
227
+ });
228
+ const operation = await getVideoGenerationOperation(
229
+ input.db,
230
+ input.workspaceId,
231
+ admitted.operation.id,
232
+ );
233
+ if (!operation) throw new Error("Admitted video generation operation disappeared");
234
+ if (operation.status === "preparing") {
235
+ assertStoredReferencePlan(operation.references, inspected);
236
+ if (inspected.length > 0) {
237
+ if (!input.runCommand) throw new Error("Prepared video references lost their sandbox");
238
+ await uploadAndVerifyVideoReferences({
239
+ storage: input.storage,
240
+ references: inspected,
241
+ stagingKeys: operation.references.map((reference) => {
242
+ if (!reference.stagingObjectKey) {
243
+ throw new Error("Video reference staging key is missing");
244
+ }
245
+ return reference.stagingObjectKey;
246
+ }),
247
+ runCommand: input.runCommand,
248
+ tempRoot: input.settings.videoGenerationTempDirectory,
249
+ ffprobePath: input.settings.videoGenerationFfprobePath,
250
+ uploadTtlSeconds: input.settings.videoGenerationReferenceUrlTtlSeconds,
251
+ ...(input.signal ? { signal: input.signal } : {}),
252
+ });
253
+ }
254
+ await markVideoGenerationPrepared(input.db, {
255
+ accountId: input.accountId,
256
+ workspaceId: input.workspaceId,
257
+ operationId: operation.id,
258
+ requestDigest,
259
+ });
260
+ } else if (
261
+ ![
262
+ "prepared",
263
+ "accepted",
264
+ "submission_uncertain",
265
+ "provider_started",
266
+ "retaining",
267
+ "completed",
268
+ ].includes(operation.status)
269
+ ) {
270
+ throw new Error("The existing video generation request cannot be resumed");
271
+ }
272
+ return {
273
+ receipt: VideoGenerationAcceptedReceipt.parse({
274
+ schemaVersion: 1,
275
+ status: "accepted",
276
+ operationId: operation.id,
277
+ }),
278
+ operationId: operation.id,
279
+ requestDigest,
280
+ };
281
+ }
282
+
283
+ function assertStoredReferencePlan(
284
+ stored: VideoGenerationReference[],
285
+ inspected: Awaited<ReturnType<typeof inspectSandboxVideoReferences>>,
286
+ ): void {
287
+ if (
288
+ stored.length !== inspected.length ||
289
+ stored.some((reference, index) => {
290
+ const source = inspected[index];
291
+ return (
292
+ !source ||
293
+ reference.ordinal !== source.ordinal ||
294
+ reference.role !== source.role ||
295
+ reference.contentType !== source.contentType ||
296
+ reference.sizeBytes !== source.sizeBytes ||
297
+ reference.sha256 !== source.sha256
298
+ );
299
+ })
300
+ ) {
301
+ throw new Error("Video reference bytes changed across tool-call recovery");
302
+ }
303
+ }
@@ -0,0 +1,112 @@
1
+ import { decryptEnvironmentValue, encryptEnvironmentValue } from "@opengeni/db";
2
+ import type { XaiProviderAccountAuthoritySnapshotV1 } from "@opengeni/contracts";
3
+
4
+ export type VideoGenerationProviderCredential =
5
+ | Readonly<{ kind: "api-key"; apiKey: string }>
6
+ | Readonly<{
7
+ kind: "xai-subscription";
8
+ accessToken: string;
9
+ refreshToken: string;
10
+ userId: string;
11
+ credentialId: string;
12
+ subjectId: string;
13
+ authoritySnapshot: XaiProviderAccountAuthoritySnapshotV1;
14
+ }>;
15
+
16
+ /** One frozen, provider-neutral credential envelope for every video funding route. */
17
+ export function encryptVideoGenerationApiKey(key: Uint8Array, apiKey: string): string {
18
+ if (!apiKey.trim()) throw new Error("Video provider credential is empty");
19
+ return encryptEnvironmentValue(key, JSON.stringify({ kind: "api-key", apiKey }));
20
+ }
21
+
22
+ export function encryptVideoGenerationXaiCredential(
23
+ key: Uint8Array,
24
+ credential: Omit<
25
+ Extract<VideoGenerationProviderCredential, { kind: "xai-subscription" }>,
26
+ "kind"
27
+ >,
28
+ ): string {
29
+ if (
30
+ !credential.accessToken.trim() ||
31
+ !credential.refreshToken.trim() ||
32
+ !credential.userId.trim() ||
33
+ !credential.credentialId.trim() ||
34
+ !credential.subjectId.trim()
35
+ ) {
36
+ throw new Error("SuperGrok video credential is incomplete");
37
+ }
38
+ return encryptEnvironmentValue(key, JSON.stringify({ kind: "xai-subscription", ...credential }));
39
+ }
40
+
41
+ /**
42
+ * Decrypt without ever attaching parser/decryption errors: JSON parser errors
43
+ * may quote plaintext and must not enter Temporal failure payloads or logs.
44
+ */
45
+ export function decryptVideoGenerationCredential(
46
+ key: Uint8Array,
47
+ stored: string,
48
+ ): VideoGenerationProviderCredential {
49
+ let plaintext: string;
50
+ try {
51
+ plaintext = decryptEnvironmentValue(key, stored);
52
+ } catch {
53
+ throw new Error("Video provider credential lease could not be decrypted");
54
+ }
55
+ let decoded: unknown;
56
+ try {
57
+ decoded = JSON.parse(plaintext);
58
+ } catch {
59
+ throw new Error("Video provider credential lease is malformed");
60
+ }
61
+ const row =
62
+ decoded && typeof decoded === "object" && !Array.isArray(decoded)
63
+ ? (decoded as Record<string, unknown>)
64
+ : null;
65
+ if (row?.kind === "api-key" || (row?.kind === undefined && typeof row?.apiKey === "string")) {
66
+ if (typeof row.apiKey !== "string" || !row.apiKey.trim()) {
67
+ throw new Error("Video provider credential lease is malformed");
68
+ }
69
+ return Object.freeze({ kind: "api-key", apiKey: row.apiKey });
70
+ }
71
+ const authority =
72
+ row?.authoritySnapshot &&
73
+ typeof row.authoritySnapshot === "object" &&
74
+ !Array.isArray(row.authoritySnapshot)
75
+ ? (row.authoritySnapshot as Record<string, unknown>)
76
+ : null;
77
+ if (
78
+ row?.kind !== "xai-subscription" ||
79
+ typeof row.accessToken !== "string" ||
80
+ !row.accessToken.trim() ||
81
+ typeof row.refreshToken !== "string" ||
82
+ !row.refreshToken.trim() ||
83
+ typeof row.userId !== "string" ||
84
+ !row.userId.trim() ||
85
+ typeof row.credentialId !== "string" ||
86
+ !row.credentialId.trim() ||
87
+ typeof row.subjectId !== "string" ||
88
+ !row.subjectId.trim() ||
89
+ authority?.version !== 1 ||
90
+ (authority.scope !== "workspace" && authority.scope !== "user") ||
91
+ (authority.scope === "user" && !Number.isSafeInteger(authority.authorityGeneration))
92
+ ) {
93
+ throw new Error("Video provider credential lease is malformed");
94
+ }
95
+ return Object.freeze({
96
+ kind: "xai-subscription",
97
+ accessToken: row.accessToken,
98
+ refreshToken: row.refreshToken,
99
+ userId: row.userId,
100
+ credentialId: row.credentialId,
101
+ subjectId: row.subjectId,
102
+ authoritySnapshot: authority as XaiProviderAccountAuthoritySnapshotV1,
103
+ });
104
+ }
105
+
106
+ export function decryptVideoGenerationApiKey(key: Uint8Array, stored: string): string {
107
+ const credential = decryptVideoGenerationCredential(key, stored);
108
+ if (credential.kind !== "api-key") {
109
+ throw new Error("Video provider credential is not an API key");
110
+ }
111
+ return credential.apiKey;
112
+ }