@opengeni/worker-bundle 0.17.2 → 0.20.1

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 (78) hide show
  1. package/dist/activities/agent-run-admission.d.ts +12 -0
  2. package/dist/activities/agent-turn.d.ts +49 -1
  3. package/dist/activities/browser-state-artifact-reaper.d.ts +29 -0
  4. package/dist/activities/capabilities.d.ts +6 -0
  5. package/dist/activities/codex-capacity.d.ts +1 -0
  6. package/dist/activities/environment.d.ts +7 -1
  7. package/dist/activities/packs.d.ts +8 -10
  8. package/dist/activities/run-input.d.ts +8 -0
  9. package/dist/activities/session-state.d.ts +7 -3
  10. package/dist/activities/site-auth-maintenance.d.ts +27 -0
  11. package/dist/activities/types.d.ts +43 -0
  12. package/dist/activities/video-generation-admission.d.ts +9 -4
  13. package/dist/activities/video-generation-credential.d.ts +17 -0
  14. package/dist/activities/video-reference-staging.d.ts +7 -1
  15. package/dist/activities/xai-auth.d.ts +23 -0
  16. package/dist/activities/xai-image-generation.d.ts +26 -0
  17. package/dist/activities/xai-video-generation.d.ts +18 -0
  18. package/dist/{activities-control-44KBWLSM.js → activities-control-SMKF2OBE.js} +438 -55
  19. package/dist/activities-control-SMKF2OBE.js.map +1 -0
  20. package/dist/activities-control.d.ts +10 -2
  21. package/dist/{activities-turn-K4CEDEGJ.js → activities-turn-IDS3WEHA.js} +6704 -4847
  22. package/dist/activities-turn-IDS3WEHA.js.map +1 -0
  23. package/dist/activities.d.ts +11 -3
  24. package/dist/{chunk-E3RI3I33.js → chunk-N5TJIFCE.js} +156 -10
  25. package/dist/chunk-N5TJIFCE.js.map +1 -0
  26. package/dist/{chunk-KKCRGAGZ.js → chunk-P6Z2SLAA.js} +99 -98
  27. package/dist/chunk-P6Z2SLAA.js.map +1 -0
  28. package/dist/index.d.ts +14 -1
  29. package/dist/index.js +49 -2
  30. package/dist/index.js.map +1 -1
  31. package/dist/model-checkpoint-memory-collector.d.ts +1 -0
  32. package/dist/model-context-contributions.d.ts +40 -0
  33. package/dist/observability-metrics.d.ts +57 -1
  34. package/dist/sandbox-routing.d.ts +4 -0
  35. package/dist/workflow-bundle.js +232 -18
  36. package/dist/workflows/activities.d.ts +1 -1
  37. package/dist/workflows/session.d.ts +17 -1
  38. package/dist/workflows/site-auth-maintenance.d.ts +2 -0
  39. package/dist/workflows.d.ts +1 -0
  40. package/package.json +16 -15
  41. package/src/activities/agent-run-admission.ts +76 -0
  42. package/src/activities/agent-turn.ts +2777 -1241
  43. package/src/activities/browser-state-artifact-reaper.ts +120 -0
  44. package/src/activities/capabilities.ts +10 -0
  45. package/src/activities/codex-capacity.ts +48 -1
  46. package/src/activities/environment.ts +14 -8
  47. package/src/activities/goals.ts +2 -1
  48. package/src/activities/packs.ts +45 -122
  49. package/src/activities/parent-wake.ts +13 -1
  50. package/src/activities/run-input.ts +137 -45
  51. package/src/activities/scheduled-tasks.ts +30 -73
  52. package/src/activities/session-state.ts +87 -5
  53. package/src/activities/site-auth-maintenance.ts +269 -0
  54. package/src/activities/types.ts +44 -0
  55. package/src/activities/video-generation-admission.ts +37 -4
  56. package/src/activities/video-generation-credential.ts +84 -6
  57. package/src/activities/video-generation-reconciliation.ts +212 -19
  58. package/src/activities/video-media-validation.ts +8 -2
  59. package/src/activities/video-reference-staging.ts +121 -34
  60. package/src/activities/xai-auth.ts +122 -0
  61. package/src/activities/xai-image-generation.ts +86 -0
  62. package/src/activities/xai-video-generation.ts +69 -0
  63. package/src/activities-control.ts +4 -0
  64. package/src/activities.ts +7 -0
  65. package/src/index.ts +51 -0
  66. package/src/model-checkpoint-memory-collector.ts +8 -1
  67. package/src/model-context-contributions.ts +159 -0
  68. package/src/observability-metrics.ts +282 -12
  69. package/src/sandbox-routing.ts +13 -0
  70. package/src/workflows/activities.ts +1 -0
  71. package/src/workflows/file-upload-reaper.ts +8 -2
  72. package/src/workflows/session.ts +219 -14
  73. package/src/workflows/site-auth-maintenance.ts +14 -0
  74. package/src/workflows.ts +1 -0
  75. package/dist/activities-control-44KBWLSM.js.map +0 -1
  76. package/dist/activities-turn-K4CEDEGJ.js.map +0 -1
  77. package/dist/chunk-E3RI3I33.js.map +0 -1
  78. package/dist/chunk-KKCRGAGZ.js.map +0 -1
@@ -0,0 +1,269 @@
1
+ import { resolveTurnExecutionPolicyV1 } from "@opengeni/config";
2
+ import {
3
+ DEFAULT_FIRST_PARTY_MCP_PERMISSIONS,
4
+ SITE_AUTH_MAINTENANCE_CONNECTION_CONTEXT_KEY,
5
+ SITE_AUTH_MAINTENANCE_OPERATION_CONTEXT_KEY,
6
+ type FirstPartyMcpToolName,
7
+ } from "@opengeni/contracts";
8
+ import {
9
+ assertWorkspaceModelPolicyAllows,
10
+ canonicalConfiguredModel,
11
+ createAndStartSessionWithOutcome,
12
+ recordWorkspaceUsage,
13
+ } from "@opengeni/core";
14
+ import {
15
+ claimSiteAuthMaintenance,
16
+ confirmSiteAuthMaintenanceSessionInTransaction,
17
+ deferSiteAuthMaintenance,
18
+ type SiteAuthMaintenanceClaim,
19
+ } from "@opengeni/db";
20
+ import { agentRunAdmissionDenial } from "./agent-run-admission";
21
+ import type { ControlActivityServices } from "./types";
22
+
23
+ export const SITE_AUTH_MAINTENANCE_CLAIM_TIMEOUT_MS = 10 * 60 * 1_000;
24
+ export const SITE_AUTH_MAINTENANCE_BATCH_SIZE = 16;
25
+
26
+ const MAINTENANCE_TOOLS = [
27
+ "interaction_discover",
28
+ "browser_open",
29
+ "browser_tabs",
30
+ "browser_observe",
31
+ "browser_act",
32
+ "browser_debug",
33
+ "browser_auth",
34
+ "interaction_request_human",
35
+ "browser_identity",
36
+ "browser_lifecycle",
37
+ ] as const satisfies readonly FirstPartyMcpToolName[];
38
+
39
+ export type MaintainSiteAuthConnectionsResult = {
40
+ claimed: number;
41
+ started: number;
42
+ deferred: number;
43
+ };
44
+
45
+ export type SiteAuthMaintenanceOptions = {
46
+ claimTimeoutMs?: number;
47
+ batchSize?: number;
48
+ claim?: typeof claimSiteAuthMaintenance;
49
+ confirm?: typeof confirmSiteAuthMaintenanceSessionInTransaction;
50
+ defer?: typeof deferSiteAuthMaintenance;
51
+ createSession?: typeof createAndStartSessionWithOutcome;
52
+ admit?: typeof agentRunAdmissionDenial;
53
+ recordUsage?: typeof recordWorkspaceUsage;
54
+ assertModelPolicy?: typeof assertWorkspaceModelPolicyAllows;
55
+ };
56
+
57
+ /** Dispatch bounded, auditable agent sessions for due authentication checks.
58
+ * A hidden claim id/session id pair makes every crash point replayable. */
59
+ export function createSiteAuthMaintenanceActivities(
60
+ services: () => Promise<ControlActivityServices>,
61
+ options: SiteAuthMaintenanceOptions = {},
62
+ ) {
63
+ const claimTimeoutMs = options.claimTimeoutMs ?? SITE_AUTH_MAINTENANCE_CLAIM_TIMEOUT_MS;
64
+ const batchSize = options.batchSize ?? SITE_AUTH_MAINTENANCE_BATCH_SIZE;
65
+ const claim = options.claim ?? claimSiteAuthMaintenance;
66
+ const confirm = options.confirm ?? confirmSiteAuthMaintenanceSessionInTransaction;
67
+ const defer = options.defer ?? deferSiteAuthMaintenance;
68
+ const createSession = options.createSession ?? createAndStartSessionWithOutcome;
69
+ const admit = options.admit ?? agentRunAdmissionDenial;
70
+ const recordUsage = options.recordUsage ?? recordWorkspaceUsage;
71
+ const assertModelPolicy = options.assertModelPolicy ?? assertWorkspaceModelPolicyAllows;
72
+
73
+ async function maintainSiteAuthConnections(): Promise<MaintainSiteAuthConnectionsResult> {
74
+ const service = await services();
75
+ const claims = await claim(service.db, { claimTimeoutMs, limit: batchSize });
76
+ const result: MaintainSiteAuthConnectionsResult = {
77
+ claimed: claims.length,
78
+ started: 0,
79
+ deferred: 0,
80
+ };
81
+ for (const maintenance of claims) {
82
+ try {
83
+ await dispatchMaintenanceSession(service, maintenance, {
84
+ confirm,
85
+ createSession,
86
+ admit,
87
+ recordUsage,
88
+ assertModelPolicy,
89
+ });
90
+ result.started += 1;
91
+ } catch (error) {
92
+ const released = await defer(service.db, {
93
+ accountId: maintenance.accountId,
94
+ workspaceId: maintenance.workspaceId,
95
+ siteAuthConnectionId: maintenance.siteAuthConnectionId,
96
+ operationId: maintenance.operationId,
97
+ sessionId: maintenance.sessionId,
98
+ retryAt: maintenanceRetryAt(maintenance),
99
+ }).catch(() => false);
100
+ if (released) result.deferred += 1;
101
+ service.observability.warn("site auth maintenance dispatch deferred", {
102
+ workspaceId: maintenance.workspaceId,
103
+ siteAuthConnectionId: maintenance.siteAuthConnectionId,
104
+ action: maintenance.action,
105
+ errorCategory: maintenanceErrorCategory(error),
106
+ claimReleased: released,
107
+ });
108
+ }
109
+ }
110
+ if (result.claimed > 0) {
111
+ service.observability.info("site auth maintenance swept", result);
112
+ }
113
+ return result;
114
+ }
115
+
116
+ return { maintainSiteAuthConnections };
117
+ }
118
+
119
+ async function dispatchMaintenanceSession(
120
+ service: ControlActivityServices,
121
+ maintenance: SiteAuthMaintenanceClaim,
122
+ dependencies: {
123
+ confirm: typeof confirmSiteAuthMaintenanceSessionInTransaction;
124
+ createSession: typeof createAndStartSessionWithOutcome;
125
+ admit: typeof agentRunAdmissionDenial;
126
+ recordUsage: typeof recordWorkspaceUsage;
127
+ assertModelPolicy: typeof assertWorkspaceModelPolicyAllows;
128
+ },
129
+ ): Promise<void> {
130
+ const model = canonicalConfiguredModel(service.settings, service.settings.openaiModel);
131
+ if (!model) throw new Error("site auth maintenance has no configured model");
132
+ await dependencies.assertModelPolicy(
133
+ service.db,
134
+ service.settings,
135
+ maintenance.workspaceId,
136
+ model,
137
+ );
138
+ const denial = await dependencies.admit(service, {
139
+ accountId: maintenance.accountId,
140
+ workspaceId: maintenance.workspaceId,
141
+ model,
142
+ requestedAgentRuns: 1,
143
+ });
144
+ if (denial) throw new Error(`site auth maintenance admission denied: ${denial}`);
145
+ const reasoningEffort = service.settings.openaiReasoningEffort;
146
+ const turnExecutionPolicy = resolveTurnExecutionPolicyV1(service.settings, {
147
+ modelId: model,
148
+ requestedModelId: null,
149
+ modelSource: "deployment",
150
+ reasoningEffort,
151
+ reasoningSource: "deployment",
152
+ latencyMode: "standard",
153
+ latencyModeSource: "deployment",
154
+ });
155
+ const created = await dependencies.createSession({
156
+ requestedSessionId: maintenance.sessionId,
157
+ db: service.db,
158
+ bus: service.bus,
159
+ workflowClient: {
160
+ wakeSessionWorkflow: async (input) => {
161
+ await service.wakeSessionWorkflow?.(input);
162
+ },
163
+ },
164
+ beforeCreateCommit: async (tx, sessionId) => {
165
+ const confirmed = await dependencies.confirm(tx, {
166
+ accountId: maintenance.accountId,
167
+ workspaceId: maintenance.workspaceId,
168
+ siteAuthConnectionId: maintenance.siteAuthConnectionId,
169
+ operationId: maintenance.operationId,
170
+ sessionId,
171
+ });
172
+ if (!confirmed) throw new Error("site auth maintenance claim changed before session start");
173
+ },
174
+ accountId: maintenance.accountId,
175
+ workspaceId: maintenance.workspaceId,
176
+ initialMessage: maintenancePrompt(maintenance),
177
+ resources: [],
178
+ tools: [{ kind: "mcp", id: "opengeni" }],
179
+ toolPolicy: { mode: "explicit", inheritedFromSessionId: null },
180
+ model,
181
+ reasoningEffort,
182
+ turnExecutionPolicy,
183
+ sandboxBackend: service.settings.sandboxBackend,
184
+ metadata: {
185
+ role: "site_auth_maintenance",
186
+ [SITE_AUTH_MAINTENANCE_CONNECTION_CONTEXT_KEY]: maintenance.siteAuthConnectionId,
187
+ [SITE_AUTH_MAINTENANCE_OPERATION_CONTEXT_KEY]: maintenance.operationId,
188
+ },
189
+ createdBy: {
190
+ kind: "service",
191
+ subjectId: "site-auth-maintenance",
192
+ label: "OpenGeni authentication maintenance",
193
+ },
194
+ createdByContext: {
195
+ [SITE_AUTH_MAINTENANCE_CONNECTION_CONTEXT_KEY]: maintenance.siteAuthConnectionId,
196
+ [SITE_AUTH_MAINTENANCE_OPERATION_CONTEXT_KEY]: maintenance.operationId,
197
+ },
198
+ instructions:
199
+ "Complete only the exact browser-auth maintenance run in the initial request. Use the canonical browser/auth tools, preserve their causal fences, expose no secrets, never publish a BrowserIdentity revision, and do not perform unrelated work.",
200
+ policyRole: "site_auth_maintenance",
201
+ firstPartyMcpPermissions: [...DEFAULT_FIRST_PARTY_MCP_PERMISSIONS],
202
+ firstPartyMcpTools: [...MAINTENANCE_TOOLS],
203
+ createIdempotencyKey: `site-auth-maintenance:${maintenance.operationId}`,
204
+ subjectId: "site-auth-maintenance",
205
+ });
206
+ try {
207
+ await dependencies.recordUsage(
208
+ { db: service.db, settings: service.settings },
209
+ {
210
+ accountId: maintenance.accountId,
211
+ workspaceId: maintenance.workspaceId,
212
+ subjectId: "site-auth-maintenance",
213
+ eventType: "agent_run.created",
214
+ quantity: 1,
215
+ unit: "run",
216
+ sourceResourceType: "site_auth_connection",
217
+ sourceResourceId: maintenance.siteAuthConnectionId,
218
+ sessionId: created.session.id,
219
+ initiator: created.session.createdBy,
220
+ initiatorContext: created.session.createdByContext,
221
+ origin: "system",
222
+ idempotencyKey: `agent_run.created:site-auth-maintenance:${maintenance.operationId}`,
223
+ },
224
+ );
225
+ } catch (error) {
226
+ service.observability.warn("site auth maintenance usage recording failed", {
227
+ workspaceId: maintenance.workspaceId,
228
+ siteAuthConnectionId: maintenance.siteAuthConnectionId,
229
+ errorCategory: maintenanceErrorCategory(error),
230
+ });
231
+ }
232
+ }
233
+
234
+ function maintenancePrompt(maintenance: SiteAuthMaintenanceClaim): string {
235
+ const action = maintenance.action === "repair" ? "repair authentication" : "check authentication";
236
+ return [
237
+ `Perform one scheduled SiteAuthConnection maintenance run: ${action}.`,
238
+ `Connection id: ${maintenance.siteAuthConnectionId}`,
239
+ `Connection: ${maintenance.name} (${maintenance.accountLabel})`,
240
+ `Purpose: ${maintenance.action}`,
241
+ maintenance.loginUrl ? `Login URL: ${maintenance.loginUrl}` : null,
242
+ maintenance.verificationUrlPrefixes.length > 0
243
+ ? `Verification URL prefixes: ${maintenance.verificationUrlPrefixes.join(", ")}`
244
+ : "No dedicated verification URL is configured; use the connection's allowed origins.",
245
+ maintenance.preferredIdentityId
246
+ ? `Preferred BrowserIdentity: ${maintenance.preferredIdentityId}`
247
+ : "No preferred BrowserIdentity is configured.",
248
+ maintenance.preferredPlacement
249
+ ? `Preferred placement: ${JSON.stringify(maintenance.preferredPlacement)}`
250
+ : "Use the normal managed-browser placement.",
251
+ maintenance.preferredNetworkRouteId
252
+ ? `Preferred NetworkRoute: ${maintenance.preferredNetworkRouteId}`
253
+ : "No preferred NetworkRoute is configured.",
254
+ "First call browser_auth get_connection and ensure this exact maintenance action is still active. Open a new browser (do not reuse an unrelated session) with the preferred identity, placement, and network route. Observe the current page, start one AuthRun with the exact purpose above, and settle it as verified, failed, cancelled, or a durable human intervention. For an external_provider authority use advance_external. If authentication is already valid, verify it without logging in again. If a health check finds invalid authentication, report it failed; automatic repair, when enabled, is a separate maintenance run. Never save/publish browser state in this run.",
255
+ ]
256
+ .filter((line): line is string => line !== null)
257
+ .join("\n");
258
+ }
259
+
260
+ function maintenanceRetryAt(maintenance: SiteAuthMaintenanceClaim): Date {
261
+ const retrySeconds = Math.min(maintenance.healthPolicy.intervalSeconds ?? 300, 15 * 60);
262
+ return new Date(Date.now() + retrySeconds * 1_000);
263
+ }
264
+
265
+ function maintenanceErrorCategory(error: unknown): string {
266
+ if (!error || typeof error !== "object") return "unknown";
267
+ const name = (error as { name?: unknown }).name;
268
+ return typeof name === "string" && name.length <= 100 ? name : "unknown";
269
+ }
@@ -142,12 +142,16 @@ export type ActivityServices = ControlActivityServices &
142
142
  };
143
143
 
144
144
  export type CodexCapacityWaitRef = {
145
+ /** Absent only for Temporal histories written before provider-tagged waits. */
146
+ provider?: "codex" | "xai";
145
147
  waiterId: string;
146
148
  generation: number;
147
149
  nextCheckAt: string;
148
150
  wakeRevision: number;
149
151
  };
150
152
 
153
+ export type XaiCapacityWaitRef = CodexCapacityWaitRef & { provider: "xai" };
154
+
151
155
  export type GetCodexCapacityWaitInput = {
152
156
  workspaceId: string;
153
157
  sessionId: string;
@@ -160,6 +164,8 @@ export type ReconcileCodexCapacityWaitInput = {
160
164
  waiterId: string;
161
165
  generation: number;
162
166
  cause: "timer" | "signal" | "queue" | "recovery";
167
+ /** Absent only for Codex waits and pre-provider-tagged workflow histories. */
168
+ provider?: "codex" | "xai";
163
169
  };
164
170
 
165
171
  export type ReconcileCodexCapacityWaitResult =
@@ -224,9 +230,36 @@ export type FailSessionAttemptInput = {
224
230
  workspaceId: string;
225
231
  sessionId: string;
226
232
  attemptId: string;
233
+ /** Added in v2; old Temporal histories derive the session's canonical id. */
234
+ workflowId?: string;
235
+ /** Added in v2; old Temporal histories use the bounded 60-second floor. */
236
+ retryDelayMs?: number;
237
+ /** Added in v3. Upgraded turn workers classify failures from the atomic
238
+ * admission transaction; omission is a rolling-deploy/legacy unknown and
239
+ * deliberately keeps the recoverable wake behavior. */
240
+ preClaimFailureDisposition?: PreClaimFailureDisposition;
241
+ /** The workflow admission trigger is required to re-evaluate the same
242
+ * durable admission branch before terminally settling a permanent failure. */
243
+ trigger?: RunAgentTurnInput["trigger"];
227
244
  error?: string;
228
245
  };
229
246
 
247
+ export type FailSessionAttemptResult =
248
+ | { action: "failed" }
249
+ | { action: "unclaimed" }
250
+ | { action: "terminal" }
251
+ | { action: "stale" };
252
+
253
+ export type PreClaimFailureDisposition = "retryable" | "permanent";
254
+
255
+ export type PreClaimFailureDetail = {
256
+ disposition: PreClaimFailureDisposition;
257
+ code: "db_deadlock" | "db_serialization_failure" | "db_failure" | "claim_invariant";
258
+ };
259
+
260
+ export const PRE_CLAIM_FAILURE_TYPE = "OpenGeniPreClaimFailure";
261
+ export const PRE_CLAIM_FAILURE_MESSAGE = "Agent turn admission failed before attempt claim.";
262
+
230
263
  export type RecoverDispatchInput = {
231
264
  accountId: string;
232
265
  workspaceId: string;
@@ -286,6 +319,17 @@ export type ExpireSessionHumanInputResult = {
286
319
  action: "expired" | "stale" | "not_found";
287
320
  };
288
321
 
322
+ export type ExpireSessionInteractionInterventionInput = {
323
+ accountId: string;
324
+ workspaceId: string;
325
+ sessionId: string;
326
+ interventionId: string;
327
+ };
328
+
329
+ export type ExpireSessionInteractionInterventionResult = {
330
+ action: "expired" | "stale" | "not_found";
331
+ };
332
+
289
333
  export type MarkSessionIdleInput = {
290
334
  workspaceId: string;
291
335
  sessionId: string;
@@ -3,6 +3,7 @@ import {
3
3
  type GenerateVideoToolInput,
4
4
  type VideoGenerationCapabilities,
5
5
  type VideoGenerationPolicy,
6
+ type XaiProviderAccountAuthoritySnapshotV1,
6
7
  } from "@opengeni/contracts";
7
8
  import {
8
9
  normalizeVideoGenerationRequest,
@@ -18,6 +19,7 @@ import {
18
19
  markVideoGenerationPrepared,
19
20
  type Database,
20
21
  type VideoGenerationReference,
22
+ type XaiCredentialForRun,
21
23
  } from "@opengeni/db";
22
24
  import {
23
25
  calculateVideoGenerationCreditCostMicros,
@@ -32,14 +34,16 @@ import {
32
34
  videoReferenceStagingKey,
33
35
  type SandboxCommandRunner,
34
36
  } from "./video-reference-staging";
35
- import { encryptVideoGenerationApiKey } from "./video-generation-credential";
37
+ import {
38
+ encryptVideoGenerationApiKey,
39
+ encryptVideoGenerationXaiCredential,
40
+ } from "./video-generation-credential";
36
41
 
37
42
  export type VideoGenerationCredentialLease = Readonly<{
38
- fundingSource: "opengeni_credits" | "workspace_gateway";
43
+ fundingSource: "opengeni_credits" | "workspace_gateway" | "supergrok_subscription";
39
44
  connectionId: string | null;
40
45
  version: number;
41
46
  credentialEncrypted: string;
42
- apiKey: string;
43
47
  }>;
44
48
 
45
49
  export type AcceptedVideoGeneration = Readonly<{
@@ -77,7 +81,36 @@ export function managedVideoGenerationCredentialLease(
77
81
  encryptionKey,
78
82
  settings.vercelAiGatewayApiKey,
79
83
  ),
80
- apiKey: settings.vercelAiGatewayApiKey,
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
+ }),
81
114
  });
82
115
  }
83
116
 
@@ -1,16 +1,51 @@
1
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
+ }>;
2
15
 
3
16
  /** One frozen, provider-neutral credential envelope for every video funding route. */
4
17
  export function encryptVideoGenerationApiKey(key: Uint8Array, apiKey: string): string {
5
18
  if (!apiKey.trim()) throw new Error("Video provider credential is empty");
6
- return encryptEnvironmentValue(key, JSON.stringify({ apiKey }));
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 }));
7
39
  }
8
40
 
9
41
  /**
10
42
  * Decrypt without ever attaching parser/decryption errors: JSON parser errors
11
43
  * may quote plaintext and must not enter Temporal failure payloads or logs.
12
44
  */
13
- export function decryptVideoGenerationApiKey(key: Uint8Array, stored: string): string {
45
+ export function decryptVideoGenerationCredential(
46
+ key: Uint8Array,
47
+ stored: string,
48
+ ): VideoGenerationProviderCredential {
14
49
  let plaintext: string;
15
50
  try {
16
51
  plaintext = decryptEnvironmentValue(key, stored);
@@ -23,12 +58,55 @@ export function decryptVideoGenerationApiKey(key: Uint8Array, stored: string): s
23
58
  } catch {
24
59
  throw new Error("Video provider credential lease is malformed");
25
60
  }
26
- const apiKey =
61
+ const row =
27
62
  decoded && typeof decoded === "object" && !Array.isArray(decoded)
28
- ? (decoded as Record<string, unknown>).apiKey
63
+ ? (decoded as Record<string, unknown>)
29
64
  : null;
30
- if (typeof apiKey !== "string" || !apiKey.trim()) {
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
+ ) {
31
93
  throw new Error("Video provider credential lease is malformed");
32
94
  }
33
- return apiKey;
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;
34
112
  }