@microfox/ai-worker 1.0.2 → 1.0.3

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/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/handler.ts","../src/mongoJobStore.ts","../src/redisJobStore.ts","../src/queueJobStore.ts","../src/config.ts","../src/queue.ts"],"sourcesContent":["/**\n * @microfox/ai-worker\n * Worker runtime for ai-router - SQS-based async agent execution\n */\n\nimport { dispatch, dispatchLocal, getWorkersTriggerUrl, type DispatchOptions, type DispatchResult } from './client.js';\nimport { createLambdaHandler, type WorkerHandler, type JobStore, type DispatchWorkerOptions, SQS_MAX_DELAY_SECONDS } from './handler.js';\nimport type { ZodType, z } from 'zod';\n\nexport * from './client.js';\nexport * from './handler.js';\nexport * from './config.js';\nexport * from './queue.js';\n\n/**\n * Schedule event configuration for a worker.\n * Supports both simple rate/cron strings and full configuration objects.\n * \n * @example Simple rate/cron\n * ```typescript\n * schedule: 'rate(2 hours)'\n * // or\n * schedule: 'cron(0 12 * * ? *)'\n * ```\n * \n * @example Full configuration\n * ```typescript\n * schedule: {\n * rate: 'rate(10 minutes)',\n * enabled: true,\n * input: { key1: 'value1' }\n * }\n * ```\n * \n * @example Multiple schedules\n * ```typescript\n * schedule: [\n * 'rate(2 hours)',\n * { rate: 'cron(0 12 * * ? *)', enabled: false }\n * ]\n * ```\n */\nexport interface ScheduleEventConfig {\n /**\n * Schedule rate using either rate() or cron() syntax.\n * Can be a string or array of strings for multiple schedules.\n * \n * @example 'rate(2 hours)' or 'cron(0 12 * * ? *)'\n * @example ['cron(0 0/4 ? * MON-FRI *)', 'cron(0 2 ? * SAT-SUN *)']\n */\n rate: string | string[];\n /**\n * Whether the schedule is enabled (default: true).\n */\n enabled?: boolean;\n /**\n * Input payload to pass to the function.\n */\n input?: Record<string, any>;\n /**\n * JSONPath expression to select part of the event data as input.\n */\n inputPath?: string;\n /**\n * Input transformer configuration for custom input mapping.\n */\n inputTransformer?: {\n inputPathsMap?: Record<string, string>;\n inputTemplate?: string;\n };\n /**\n * Name of the schedule event.\n */\n name?: string;\n /**\n * Description of the schedule event.\n */\n description?: string;\n /**\n * Method to use: 'eventBus' (default) or 'scheduler'.\n * Use 'scheduler' for higher limits (1M events vs 300).\n */\n method?: 'eventBus' | 'scheduler';\n /**\n * Timezone for the schedule (only used with method: 'scheduler').\n * @example 'America/New_York'\n */\n timezone?: string;\n}\n\nexport type ScheduleConfig = \n | string \n | ScheduleEventConfig \n | (string | ScheduleEventConfig)[];\n\n/**\n * Configuration for a worker's Lambda function deployment.\n * \n * **Best Practice**: Export this as a separate const from your worker file:\n * ```typescript\n * export const workerConfig: WorkerConfig = {\n * timeout: 900,\n * memorySize: 2048,\n * layers: ['arn:aws:lambda:${aws:region}:${aws:accountId}:layer:ffmpeg:1'],\n * schedule: 'rate(2 hours)',\n * };\n * ```\n * \n * The CLI will automatically extract it from the export. You do not need to pass it to `createWorker()`.\n */\nexport interface WorkerConfig {\n /**\n * Lambda function timeout in seconds (max 900).\n */\n timeout?: number;\n /**\n * Lambda function memory size in MB (128-10240).\n */\n memorySize?: number;\n /**\n * Optional Lambda layers ARNs to attach to this worker function.\n *\n * This is primarily used by @microfox/ai-worker-cli when generating serverless.yml.\n * Supports CloudFormation pseudo-parameters like ${aws:region} and ${aws:accountId}.\n *\n * Example:\n * layers: ['arn:aws:lambda:${aws:region}:${aws:accountId}:layer:ffmpeg:1']\n */\n layers?: string[];\n /**\n * Schedule events configuration for this worker.\n * Allows multiple schedule events to be attached to the same function.\n * \n * @example Simple rate\n * ```typescript\n * schedule: 'rate(2 hours)'\n * ```\n * \n * @example Multiple schedules\n * ```typescript\n * schedule: [\n * 'rate(2 hours)',\n * { rate: 'cron(0 12 * * ? *)', enabled: true, input: { key: 'value' } }\n * ]\n * ```\n * \n * @example Using scheduler method with timezone\n * ```typescript\n * schedule: {\n * method: 'scheduler',\n * rate: 'cron(0 0/4 ? * MON-FRI *)',\n * timezone: 'America/New_York',\n * input: { key1: 'value1' }\n * }\n * ```\n */\n schedule?: ScheduleConfig;\n\n /**\n * SQS queue settings for this worker (used by @microfox/ai-worker-cli when generating serverless.yml).\n *\n * Notes:\n * - To effectively disable retries, set `maxReceiveCount: 1` (requires DLQ; the CLI will create one).\n * - SQS does not support `maxReceiveCount: 0`.\n * - `messageRetentionPeriod` is in seconds (max 1209600 = 14 days).\n */\n sqs?: {\n /**\n * How many receives before sending to DLQ.\n * Use 1 to avoid retries.\n */\n maxReceiveCount?: number;\n /**\n * How long messages are retained in the main queue (seconds).\n */\n messageRetentionPeriod?: number;\n /**\n * Visibility timeout for the main queue (seconds).\n * If not set, CLI defaults to (worker timeout + 60s).\n */\n visibilityTimeout?: number;\n /**\n * DLQ message retention period (seconds).\n * Defaults to `messageRetentionPeriod` (or 14 days).\n */\n deadLetterMessageRetentionPeriod?: number;\n };\n}\n\nexport interface WorkerAgentConfig<INPUT_SCHEMA extends ZodType<any>, OUTPUT> {\n id: string;\n inputSchema: INPUT_SCHEMA;\n outputSchema: ZodType<OUTPUT>;\n handler: WorkerHandler<z.infer<INPUT_SCHEMA>, OUTPUT>;\n /**\n * @deprecated Prefer exporting `workerConfig` as a separate const from your worker file.\n * The CLI will automatically extract it from the export. This parameter is kept for backward compatibility.\n */\n workerConfig?: WorkerConfig;\n}\n\nexport interface WorkerAgent<INPUT_SCHEMA extends ZodType<any>, OUTPUT> {\n id: string;\n dispatch: (\n input: z.input<INPUT_SCHEMA>,\n options: DispatchOptions\n ) => Promise<DispatchResult>;\n handler: WorkerHandler<z.infer<INPUT_SCHEMA>, OUTPUT>;\n inputSchema: INPUT_SCHEMA;\n outputSchema: ZodType<OUTPUT>;\n workerConfig?: WorkerConfig;\n}\n\n/**\n * Creates a worker agent that can be dispatched to SQS/Lambda.\n *\n * In development mode (NODE_ENV === 'development' and WORKERS_LOCAL_MODE !== 'false'),\n * dispatch() will run the handler immediately in the same process.\n *\n * In production, dispatch() sends a message to SQS which triggers a Lambda function.\n *\n * @template INPUT_SCHEMA - The Zod schema type (e.g., `typeof InputSchema`).\n * Used to derive both:\n * - Pre-parse input type via `z.input<INPUT_SCHEMA>` for `dispatch()` (preserves optional fields)\n * - Parsed input type via `z.infer<INPUT_SCHEMA>` for handler (defaults applied)\n * @template OUTPUT - The output type returned by the handler. Use `z.infer<typeof OutputSchema>`.\n *\n * @param config - Worker agent configuration\n * @returns A worker agent object with a dispatch method\n *\n * @example\n * ```typescript\n * const InputSchema = z.object({\n * url: z.string().url(),\n * timeout: z.number().optional().default(5000), // optional with default\n * });\n *\n * export const worker = createWorker<typeof InputSchema, Output>({\n * // dispatch() accepts { url: string, timeout?: number } (pre-parse, optional preserved)\n * // handler receives { url: string, timeout: number } (parsed, default applied)\n * });\n * ```\n */\nexport function createWorker<INPUT_SCHEMA extends ZodType<any>, OUTPUT>(\n config: WorkerAgentConfig<INPUT_SCHEMA, OUTPUT>\n): WorkerAgent<INPUT_SCHEMA, OUTPUT> {\n const { id, inputSchema, outputSchema, handler } = config;\n\n const agent: WorkerAgent<INPUT_SCHEMA, OUTPUT> = {\n id,\n handler,\n inputSchema,\n outputSchema,\n\n async dispatch(input: z.input<INPUT_SCHEMA>, options: DispatchOptions): Promise<DispatchResult> {\n const mode = options.mode ?? 'auto';\n const envWantsLocal =\n process.env.NODE_ENV === 'development' &&\n process.env.WORKERS_LOCAL_MODE !== 'false';\n // Check if we're in local development mode\n const isLocal = mode === 'local' || (mode === 'auto' && envWantsLocal);\n\n if (isLocal) {\n // Local mode: run handler immediately\n // Parse input to apply defaults and get the final parsed type\n const parsedInput = inputSchema.parse(input);\n const localJobId = options.jobId || `local-${Date.now()}`;\n \n // Try to get direct job store access in local mode (same process as Next.js app)\n // This allows direct DB updates without needing HTTP/webhook URLs\n let directJobStore: {\n updateJob: (jobId: string, data: any) => Promise<void>;\n setJob?: (jobId: string, data: any) => Promise<void>;\n } | null = null;\n\n // Path constants for job store imports\n const nextJsPathAlias = '@/app/api/workflows/stores/jobStore';\n const explicitPath = process.env.WORKER_JOB_STORE_MODULE_PATH;\n\n // Reliable approach: try Next.js path alias first, then explicit env var\n // The @/ alias works at runtime in Next.js context\n const resolveJobStore = async () => {\n // Option 1: Try Next.js path alias (works in Next.js runtime context)\n try {\n const module = await import(nextJsPathAlias);\n if (module?.updateJob) {\n return { updateJob: module.updateJob, setJob: module.setJob };\n }\n } catch {\n // Path alias not available (not in Next.js context or alias not configured)\n }\n\n // Option 2: Use explicit env var if provided (for custom setups)\n if (explicitPath) {\n try {\n const module = await import(explicitPath).catch(() => {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n return require(explicitPath);\n });\n if (module?.updateJob) {\n return { updateJob: module.updateJob, setJob: module.setJob };\n }\n } catch {\n // Explicit path failed\n }\n }\n\n return null;\n };\n\n directJobStore = await resolveJobStore();\n if (directJobStore) {\n console.log('[Worker] Using direct job store in local mode (no HTTP needed)');\n }\n\n // Derive job store URL from webhook URL or environment (fallback for HTTP mode)\n let jobStoreUrl: string | undefined;\n if (options.webhookUrl) {\n try {\n const webhookUrlObj = new URL(options.webhookUrl);\n jobStoreUrl = webhookUrlObj.pathname.replace(/\\/webhook$/, '');\n jobStoreUrl = `${webhookUrlObj.origin}${jobStoreUrl}`;\n } catch {\n // Invalid URL, skip job store URL\n }\n }\n jobStoreUrl = jobStoreUrl || process.env.WORKER_JOB_STORE_URL;\n\n // Create job store interface for local mode\n // Prefer direct DB access, fallback to HTTP calls if needed\n const createLocalJobStore = (\n directStore: typeof directJobStore,\n httpUrl?: string\n ): JobStore | undefined => {\n // If we have direct job store access, use it (no HTTP needed)\n if (directStore) {\n return {\n update: async (update) => {\n try {\n // Build update payload\n const updatePayload: any = {};\n \n if (update.status !== undefined) {\n updatePayload.status = update.status;\n }\n if (update.metadata !== undefined) {\n updatePayload.metadata = update.metadata;\n }\n if (update.progress !== undefined) {\n // Merge progress into metadata\n updatePayload.metadata = {\n ...updatePayload.metadata,\n progress: update.progress,\n progressMessage: update.progressMessage,\n };\n }\n if (update.output !== undefined) {\n updatePayload.output = update.output;\n }\n if (update.error !== undefined) {\n updatePayload.error = update.error;\n }\n\n await directStore.updateJob(localJobId, updatePayload);\n console.log('[Worker] Local job updated (direct DB):', {\n jobId: localJobId,\n workerId: id,\n updates: Object.keys(updatePayload),\n });\n } catch (error: any) {\n console.warn('[Worker] Failed to update local job (direct DB):', {\n jobId: localJobId,\n workerId: id,\n error: error?.message || String(error),\n });\n }\n },\n get: async () => {\n try {\n // Use the same direct store that has updateJob - it should also have getJob\n if (directStore) {\n // Try to import getJob from the same module\n const nextJsPath = '@/app/api/workflows/stores/jobStore';\n const explicitPath = process.env.WORKER_JOB_STORE_MODULE_PATH;\n \n for (const importPath of [nextJsPath, explicitPath].filter(Boolean)) {\n try {\n const module = await import(importPath!);\n if (module?.getJob) {\n return await module.getJob(localJobId);\n }\n } catch {\n // Continue\n }\n }\n }\n return null;\n } catch (error: any) {\n console.warn('[Worker] Failed to get local job (direct DB):', {\n jobId: localJobId,\n workerId: id,\n error: error?.message || String(error),\n });\n return null;\n }\n },\n appendInternalJob: async (entry: { jobId: string; workerId: string }) => {\n try {\n const nextJsPath = '@/app/api/workflows/stores/jobStore';\n const explicitPath = process.env.WORKER_JOB_STORE_MODULE_PATH;\n for (const importPath of [nextJsPath, explicitPath].filter(Boolean)) {\n try {\n const module = await import(importPath!);\n if (typeof module?.appendInternalJob === 'function') {\n await module.appendInternalJob(localJobId, entry);\n return;\n }\n } catch {\n // Continue\n }\n }\n } catch (error: any) {\n console.warn('[Worker] Failed to appendInternalJob (direct DB):', { localJobId, error: error?.message || String(error) });\n }\n },\n getJob: async (otherJobId: string) => {\n try {\n const nextJsPath = '@/app/api/workflows/stores/jobStore';\n const explicitPath = process.env.WORKER_JOB_STORE_MODULE_PATH;\n for (const importPath of [nextJsPath, explicitPath].filter(Boolean)) {\n try {\n const module = await import(importPath!);\n if (typeof module?.getJob === 'function') {\n return await module.getJob(otherJobId);\n }\n } catch {\n // Continue\n }\n }\n } catch (error: any) {\n console.warn('[Worker] Failed to getJob (direct DB):', { otherJobId, error: error?.message || String(error) });\n }\n return null;\n },\n };\n }\n\n // Fallback to HTTP calls if no direct access\n if (!httpUrl) {\n return undefined;\n }\n\n // Use HTTP calls to update job store\n return {\n update: async (update) => {\n try {\n // Build update payload\n const updatePayload: any = { jobId: localJobId, workerId: id };\n \n if (update.status !== undefined) {\n updatePayload.status = update.status;\n }\n if (update.metadata !== undefined) {\n updatePayload.metadata = update.metadata;\n }\n if (update.progress !== undefined) {\n // Merge progress into metadata\n updatePayload.metadata = {\n ...updatePayload.metadata,\n progress: update.progress,\n progressMessage: update.progressMessage,\n };\n }\n if (update.output !== undefined) {\n updatePayload.output = update.output;\n }\n if (update.error !== undefined) {\n updatePayload.error = update.error;\n }\n\n const response = await fetch(`${httpUrl}/update`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify(updatePayload),\n });\n if (!response.ok) {\n throw new Error(`Job store update failed: ${response.status} ${response.statusText}`);\n }\n console.log('[Worker] Local job updated (HTTP):', {\n jobId: localJobId,\n workerId: id,\n updates: Object.keys(updatePayload),\n });\n } catch (error: any) {\n console.warn('[Worker] Failed to update local job (HTTP):', {\n jobId: localJobId,\n workerId: id,\n error: error?.message || String(error),\n });\n }\n },\n get: async () => {\n try {\n // GET /api/workflows/workers/:workerId/:jobId\n const response = await fetch(`${httpUrl}/${id}/${localJobId}`, {\n method: 'GET',\n headers: { 'Content-Type': 'application/json' },\n });\n\n if (!response.ok) {\n if (response.status === 404) {\n return null;\n }\n throw new Error(`Job store get failed: ${response.status} ${response.statusText}`);\n }\n\n return await response.json();\n } catch (error: any) {\n console.warn('[Worker] Failed to get local job (HTTP):', {\n jobId: localJobId,\n workerId: id,\n error: error?.message || String(error),\n });\n return null;\n }\n },\n };\n };\n\n const jobStore = createLocalJobStore(directJobStore, jobStoreUrl);\n\n const DEFAULT_POLL_INTERVAL_MS = 2000;\n const DEFAULT_POLL_TIMEOUT_MS = 15 * 60 * 1000;\n\n const createLocalDispatchWorker = (\n parentJobId: string,\n parentWorkerId: string,\n parentContext: Record<string, any>,\n store: JobStore | undefined\n ): ((\n workerId: string,\n input: unknown,\n options?: DispatchWorkerOptions\n ) => Promise<{ jobId: string; messageId?: string; output?: unknown }>) => {\n return async (\n calleeWorkerId: string,\n input: unknown,\n options?: DispatchWorkerOptions\n ): Promise<{ jobId: string; messageId?: string; output?: unknown }> => {\n const childJobId =\n options?.jobId ||\n `job-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;\n const metadata = options?.metadata ?? {};\n const serializedContext: Record<string, any> = {};\n if (parentContext.requestId) serializedContext.requestId = parentContext.requestId;\n const messageBody = {\n workerId: calleeWorkerId,\n jobId: childJobId,\n input: input ?? {},\n context: serializedContext,\n webhookUrl: options?.webhookUrl,\n metadata,\n timestamp: new Date().toISOString(),\n };\n let triggerUrl: string;\n try {\n triggerUrl = getWorkersTriggerUrl();\n } catch (e: any) {\n throw new Error(\n `Local dispatchWorker requires WORKER_BASE_URL (or similar) for worker \"${calleeWorkerId}\": ${e?.message ?? e}`\n );\n }\n const headers: Record<string, string> = { 'Content-Type': 'application/json' };\n const triggerKey = process.env.WORKERS_TRIGGER_API_KEY;\n if (triggerKey) headers['x-workers-trigger-key'] = triggerKey;\n\n // Fire-and-forget with delay: schedule trigger after delay, return immediately (no computation/wait in caller).\n if (options?.await !== true && options?.delaySeconds != null && options.delaySeconds > 0) {\n const sec = Math.min(SQS_MAX_DELAY_SECONDS, Math.max(0, Math.floor(options.delaySeconds)));\n const storeRef = store;\n setTimeout(() => {\n fetch(triggerUrl, {\n method: 'POST',\n headers,\n body: JSON.stringify({ workerId: calleeWorkerId, body: messageBody }),\n })\n .then(async (response) => {\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n console.error(\n `[Worker] Delayed trigger failed for \"${calleeWorkerId}\": ${response.status} ${response.statusText}${text ? ` - ${text}` : ''}`\n );\n return;\n }\n if (storeRef?.appendInternalJob) {\n await storeRef.appendInternalJob({ jobId: childJobId, workerId: calleeWorkerId });\n }\n })\n .catch((err) => {\n console.error('[Worker] Delayed trigger error:', { calleeWorkerId, jobId: childJobId, error: err?.message ?? err });\n });\n }, sec * 1000);\n return { jobId: childJobId, messageId: undefined };\n }\n\n const response = await fetch(triggerUrl, {\n method: 'POST',\n headers,\n body: JSON.stringify({ workerId: calleeWorkerId, body: messageBody }),\n });\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n throw new Error(\n `Failed to trigger worker \"${calleeWorkerId}\": ${response.status} ${response.statusText}${text ? ` - ${text}` : ''}`\n );\n }\n const data = (await response.json().catch(() => ({}))) as any;\n const messageId = data?.messageId ? String(data.messageId) : `trigger-${childJobId}`;\n\n if (store?.appendInternalJob) {\n await store.appendInternalJob({ jobId: childJobId, workerId: calleeWorkerId });\n }\n\n if (options?.await && store?.getJob) {\n const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;\n const pollTimeoutMs = options.pollTimeoutMs ?? DEFAULT_POLL_TIMEOUT_MS;\n const deadline = Date.now() + pollTimeoutMs;\n while (Date.now() < deadline) {\n const child = await store.getJob(childJobId);\n if (!child) {\n await new Promise((r) => setTimeout(r, pollIntervalMs));\n continue;\n }\n if (child.status === 'completed') {\n return { jobId: childJobId, messageId, output: child.output };\n }\n if (child.status === 'failed') {\n const err = child.error;\n throw new Error(err?.message ?? `Child worker ${calleeWorkerId} failed`);\n }\n await new Promise((r) => setTimeout(r, pollIntervalMs));\n }\n throw new Error(\n `Child worker ${calleeWorkerId} (${childJobId}) did not complete within ${pollTimeoutMs}ms`\n );\n }\n\n return { jobId: childJobId, messageId };\n };\n };\n\n // Create initial job record if we have job store access\n if (directJobStore?.setJob) {\n try {\n await directJobStore.setJob(localJobId, {\n jobId: localJobId,\n workerId: id,\n status: 'queued',\n input: parsedInput,\n metadata: options.metadata || {},\n });\n } catch (error: any) {\n console.warn('[Worker] Failed to create initial job record:', {\n jobId: localJobId,\n workerId: id,\n error: error?.message || String(error),\n });\n // Continue - job will still be created when status is updated\n }\n }\n\n const baseContext = { jobId: localJobId, workerId: id };\n const handlerContext = {\n ...baseContext,\n ...(jobStore ? { jobStore } : {}),\n dispatchWorker: createLocalDispatchWorker(\n localJobId,\n id,\n baseContext,\n jobStore\n ),\n };\n\n try {\n // Update status to running before execution\n if (jobStore) {\n await jobStore.update({ status: 'running' });\n }\n\n const output = await dispatchLocal(handler, parsedInput, handlerContext);\n\n // Update status to completed before webhook\n if (jobStore) {\n await jobStore.update({ status: 'completed', output });\n }\n\n // Only send webhook if webhookUrl is provided\n if (options.webhookUrl) {\n try {\n await fetch(options.webhookUrl, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n jobId: localJobId,\n workerId: id,\n status: 'success',\n output,\n metadata: options.metadata,\n }),\n });\n } catch (error) {\n console.warn('[Worker] Local webhook call failed:', error);\n }\n }\n\n return {\n messageId: `local-${Date.now()}`,\n status: 'queued',\n jobId: localJobId,\n };\n } catch (error: any) {\n // Update status to failed before webhook\n if (jobStore) {\n await jobStore.update({\n status: 'failed',\n error: {\n message: error.message || 'Unknown error',\n stack: error.stack,\n name: error.name || 'Error',\n },\n });\n }\n\n // Only send error webhook if webhookUrl is provided\n if (options.webhookUrl) {\n try {\n await fetch(options.webhookUrl, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n jobId: localJobId,\n workerId: id,\n status: 'error',\n error: {\n message: error.message || 'Unknown error',\n stack: error.stack,\n name: error.name || 'Error',\n },\n metadata: options.metadata,\n }),\n });\n } catch (webhookError) {\n console.warn('[Worker] Local error webhook call failed:', webhookError);\n }\n }\n throw error;\n }\n }\n\n // Production mode: dispatch to SQS\n return dispatch(id, input, inputSchema, options);\n },\n };\n\n return agent;\n}\n\n/**\n * Creates a Lambda handler entrypoint for a worker agent.\n * This is used by the deployment script to generate Lambda entrypoints.\n *\n * @param agent - The worker agent\n * @returns A Lambda handler function\n */\nexport function createLambdaEntrypoint<INPUT_SCHEMA extends ZodType<any>, OUTPUT>(\n agent: WorkerAgent<INPUT_SCHEMA, OUTPUT>\n) {\n return createLambdaHandler(agent.handler, agent.outputSchema);\n}\n","/**\n * Client for dispatching background worker jobs.\n *\n * In production, dispatching happens via the workers HTTP API:\n * POST /workers/trigger -> enqueues message to SQS on the workers service side\n *\n * This avoids requiring AWS credentials in your Next.js app.\n */\n\nimport type { ZodType, z } from 'zod';\nimport type { WorkerQueueConfig, WorkerQueueContext } from './queue.js';\n\nexport interface WorkerQueueRegistry {\n getQueueById(queueId: string): WorkerQueueConfig | undefined;\n invokeMapInput?: (\n queueId: string,\n stepIndex: number,\n prevOutput: unknown,\n initialInput: unknown\n ) => Promise<unknown> | unknown;\n}\n\nexport interface DispatchOptions {\n /**\n * Optional webhook callback URL to notify when the job finishes.\n * Only called when provided. Default: no webhook (use job store / MongoDB only).\n */\n webhookUrl?: string;\n /**\n * Controls how dispatch executes.\n * - \"auto\" (default): local inline execution in development unless WORKERS_LOCAL_MODE=false.\n * - \"local\": force inline execution (no SQS).\n * - \"remote\": force SQS/Lambda dispatch even in development.\n */\n mode?: 'auto' | 'local' | 'remote';\n jobId?: string;\n metadata?: Record<string, any>;\n /**\n * In-memory queue registry for dispatchQueue. Required when using dispatchQueue.\n * Pass a registry that imports from your .queue.ts definitions (works on Vercel/serverless).\n */\n registry?: WorkerQueueRegistry;\n /**\n * Optional callback to create a queue job record before dispatching.\n * Called with queueJobId (= first worker's jobId), queueId, and firstStep.\n */\n onCreateQueueJob?: (params: {\n queueJobId: string;\n queueId: string;\n firstStep: { workerId: string; workerJobId: string };\n metadata?: Record<string, unknown>;\n }) => Promise<void>;\n}\n\nexport interface DispatchResult {\n messageId: string;\n status: 'queued';\n jobId: string;\n}\n\nexport interface DispatchQueueResult extends DispatchResult {\n queueId: string;\n}\n\nexport interface SerializedContext {\n requestId?: string;\n userId?: string;\n traceId?: string;\n [key: string]: any;\n}\n\n/**\n * Derives the full /workers/trigger URL from env.\n * Exported for use by local dispatchWorker (worker-to-worker in dev).\n *\n * Preferred env vars:\n * - WORKER_BASE_URL: base URL of the workers service (e.g. https://.../prod)\n * - NEXT_PUBLIC_WORKER_BASE_URL: same, but exposed to the browser\n *\n * Legacy env vars (still supported for backwards compatibility):\n * - WORKERS_TRIGGER_API_URL / NEXT_PUBLIC_WORKERS_TRIGGER_API_URL\n * - WORKERS_CONFIG_API_URL / NEXT_PUBLIC_WORKERS_CONFIG_API_URL\n */\nexport function getWorkersTriggerUrl(): string {\n const raw =\n process.env.WORKER_BASE_URL ||\n process.env.NEXT_PUBLIC_WORKER_BASE_URL ||\n process.env.WORKERS_TRIGGER_API_URL ||\n process.env.NEXT_PUBLIC_WORKERS_TRIGGER_API_URL ||\n process.env.WORKERS_CONFIG_API_URL ||\n process.env.NEXT_PUBLIC_WORKERS_CONFIG_API_URL;\n\n if (!raw) {\n throw new Error(\n 'WORKER_BASE_URL (preferred) or NEXT_PUBLIC_WORKER_BASE_URL is required for background workers'\n );\n }\n\n const url = new URL(raw);\n url.search = '';\n url.hash = '';\n\n const path = url.pathname || '';\n\n // If the user pointed at a specific endpoint, normalize back to the service root.\n url.pathname = path.replace(/\\/?workers\\/(trigger|config)\\/?$/, '');\n\n const basePath = url.pathname.replace(/\\/+$/, '');\n url.pathname = `${basePath}/workers/trigger`.replace(/\\/+$/, '');\n\n return url.toString();\n}\n\n/**\n * Serializes context data for transmission to Lambda.\n * Only serializes safe, JSON-compatible properties.\n */\nfunction serializeContext(ctx: any): SerializedContext {\n const serialized: SerializedContext = {};\n\n if (ctx.requestId) {\n serialized.requestId = ctx.requestId;\n }\n\n // Extract any additional serializable metadata\n if (ctx.metadata && typeof ctx.metadata === 'object') {\n Object.assign(serialized, ctx.metadata);\n }\n\n // Allow custom context serialization via a helper property\n if (ctx._serializeContext && typeof ctx._serializeContext === 'function') {\n const custom = ctx._serializeContext();\n Object.assign(serialized, custom);\n }\n\n return serialized;\n}\n\n\n/**\n * Dispatches a background worker job to SQS.\n *\n * @param workerId - The ID of the worker to dispatch\n * @param input - The input data for the worker (will be validated against inputSchema)\n * @param inputSchema - Zod schema for input validation\n * @param options - Dispatch options including webhook URL\n * @param ctx - Optional context object (only serializable parts will be sent)\n * @returns Promise resolving to dispatch result with messageId and jobId\n */\nexport async function dispatch<INPUT_SCHEMA extends ZodType<any>>(\n workerId: string,\n input: z.input<INPUT_SCHEMA>,\n inputSchema: INPUT_SCHEMA,\n options: DispatchOptions,\n ctx?: any\n): Promise<DispatchResult> {\n // Validate input against schema\n const validatedInput = inputSchema.parse(input);\n\n // Generate job ID if not provided\n const jobId =\n options.jobId || `job-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;\n\n // Resolve /workers/trigger endpoint URL\n const triggerUrl = getWorkersTriggerUrl();\n\n // Serialize context (only safe, JSON-compatible parts)\n const serializedContext = ctx ? serializeContext(ctx) : {};\n\n // Job updates use MongoDB only; never pass jobStoreUrl/origin URL.\n const messageBody = {\n workerId,\n jobId,\n input: validatedInput,\n context: serializedContext,\n webhookUrl: options.webhookUrl,\n metadata: options.metadata || {},\n timestamp: new Date().toISOString(),\n };\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n const triggerKey = process.env.WORKERS_TRIGGER_API_KEY;\n if (triggerKey) {\n headers['x-workers-trigger-key'] = triggerKey;\n }\n\n const response = await fetch(triggerUrl, {\n method: 'POST',\n headers,\n body: JSON.stringify({\n workerId,\n body: messageBody,\n }),\n });\n\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n throw new Error(\n `Failed to trigger worker \"${workerId}\": ${response.status} ${response.statusText}${text ? ` - ${text}` : ''}`\n );\n }\n\n const data = (await response.json().catch(() => ({}))) as any;\n const messageId = data?.messageId ? String(data.messageId) : `trigger-${jobId}`;\n\n return {\n messageId,\n status: 'queued',\n jobId,\n };\n}\n\n/**\n * Local development mode: runs the handler immediately in the same process.\n * This bypasses SQS and Lambda for faster iteration during development.\n *\n * @param handler - The worker handler function\n * @param input - The input data\n * @param ctx - The context object\n * @returns The handler result\n */\nexport async function dispatchLocal<INPUT, OUTPUT>(\n handler: (params: { input: INPUT; ctx: any }) => Promise<OUTPUT>,\n input: INPUT,\n ctx?: any\n): Promise<OUTPUT> {\n return handler({ input, ctx: ctx || {} });\n}\n\n/**\n * Dispatches a queue by ID, using a generated registry of .queue.ts\n * definitions to determine the first worker and initial input mapping.\n *\n * This API intentionally mirrors the ergonomics of dispatching a single\n * worker, but under the hood it embeds queue context into the job input\n * so queue-aware wrappers can chain subsequent steps.\n */\nexport async function dispatchQueue<InitialInput = any>(\n queueId: string,\n initialInput: InitialInput,\n options: DispatchOptions = {},\n ctx?: any\n): Promise<DispatchQueueResult> {\n const registry = options.registry;\n if (!registry?.getQueueById) {\n throw new Error(\n 'dispatchQueue requires options.registry with getQueueById. ' +\n 'Use getQueueRegistry() from your workflows registry (e.g. app/api/workflows/registry/workers) and pass { registry: await getQueueRegistry() }.'\n );\n }\n const { getQueueById, invokeMapInput } = registry;\n const queue = getQueueById(queueId);\n\n if (!queue) {\n throw new Error(`Worker queue \"${queueId}\" not found in registry`);\n }\n\n if (!queue.steps || queue.steps.length === 0) {\n throw new Error(`Worker queue \"${queueId}\" has no steps defined`);\n }\n\n const stepIndex = 0;\n const firstStep = queue.steps[stepIndex];\n const firstWorkerId = firstStep.workerId;\n\n if (!firstWorkerId) {\n throw new Error(\n `Worker queue \"${queueId}\" has an invalid first step (missing workerId)`\n );\n }\n\n // Compute the first step's input:\n // - If a mapping function is configured and the registry exposes invokeMapInput,\n // use it (prevOutput is undefined for the first step).\n // - Otherwise, default to the initial input.\n let firstInput: unknown = initialInput;\n if (firstStep.mapInputFromPrev && typeof invokeMapInput === 'function') {\n firstInput = await invokeMapInput(\n queueId,\n stepIndex,\n undefined,\n initialInput\n );\n }\n\n const jobId =\n options.jobId ||\n `job-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;\n\n const queueContext: WorkerQueueContext<InitialInput> = {\n id: queueId,\n stepIndex,\n initialInput,\n queueJobId: jobId,\n };\n\n // Create queue job record if callback provided (for progress tracking)\n if (options.onCreateQueueJob) {\n try {\n await options.onCreateQueueJob({\n queueJobId: jobId,\n queueId,\n firstStep: { workerId: firstWorkerId, workerJobId: jobId },\n metadata: options.metadata as Record<string, unknown> | undefined,\n });\n } catch (err: any) {\n console.warn('[dispatchQueue] onCreateQueueJob failed:', err?.message ?? err);\n }\n }\n\n // Embed queue context into the worker input under a reserved key.\n const normalizedFirstInput =\n firstInput !== null && typeof firstInput === 'object'\n ? (firstInput as Record<string, any>)\n : { value: firstInput };\n\n const inputWithQueue = {\n ...normalizedFirstInput,\n __workerQueue: queueContext,\n };\n\n const metadataWithQueue = {\n ...(options.metadata || {}),\n __workerQueue: queueContext,\n };\n\n const triggerUrl = getWorkersTriggerUrl();\n const serializedContext = ctx ? serializeContext(ctx) : {};\n\n const messageBody = {\n workerId: firstWorkerId,\n jobId,\n input: inputWithQueue,\n context: serializedContext,\n webhookUrl: options.webhookUrl,\n metadata: metadataWithQueue,\n timestamp: new Date().toISOString(),\n };\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n const triggerKey = process.env.WORKERS_TRIGGER_API_KEY;\n if (triggerKey) {\n headers['x-workers-trigger-key'] = triggerKey;\n }\n\n const response = await fetch(triggerUrl, {\n method: 'POST',\n headers,\n body: JSON.stringify({\n workerId: firstWorkerId,\n body: messageBody,\n }),\n });\n\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n throw new Error(\n `Failed to trigger queue \"${queueId}\" (worker \"${firstWorkerId}\"): ` +\n `${response.status} ${response.statusText}${text ? ` - ${text}` : ''}`\n );\n }\n\n const data = (await response.json().catch(() => ({}))) as any;\n const messageId = data?.messageId ? String(data.messageId) : `trigger-${jobId}`;\n\n return {\n queueId,\n messageId,\n status: 'queued',\n jobId,\n };\n}\n\n","/**\n * Generic Lambda handler wrapper for worker agents.\n * Handles SQS events, executes user handlers, and sends webhook callbacks.\n * Job store: MongoDB only. Never uses HTTP/origin URL for job updates.\n */\n\nimport type { SQSEvent, SQSRecord, Context as LambdaContext } from 'aws-lambda';\nimport type { ZodType } from 'zod';\nimport { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs';\nimport {\n createMongoJobStore,\n upsertJob,\n isMongoJobStoreConfigured,\n getJobById as getMongoJobById,\n} from './mongoJobStore';\nimport {\n createRedisJobStore,\n upsertRedisJob,\n isRedisJobStoreConfigured,\n loadJob as loadRedisJob,\n} from './redisJobStore';\nimport {\n appendQueueJobStepInStore,\n updateQueueJobStepInStore,\n upsertInitialQueueJob,\n} from './queueJobStore';\n\nexport interface JobStoreUpdate {\n status?: 'queued' | 'running' | 'completed' | 'failed';\n metadata?: Record<string, any>;\n progress?: number;\n progressMessage?: string;\n output?: any;\n error?: {\n message: string;\n stack?: string;\n name?: string;\n };\n}\n\nexport interface JobRecord {\n jobId: string;\n workerId: string;\n status: 'queued' | 'running' | 'completed' | 'failed';\n input: any;\n output?: any;\n error?: { message: string; stack?: string };\n metadata?: Record<string, any>;\n internalJobs?: Array<{ jobId: string; workerId: string }>;\n createdAt: string;\n updatedAt: string;\n completedAt?: string;\n}\n\nexport interface JobStore {\n /**\n * Update job in job store.\n * @param update - Update object with status, metadata, progress, output, or error\n */\n update(update: JobStoreUpdate): Promise<void>;\n /**\n * Get current job record from job store.\n * @returns Job record or null if not found\n */\n get(): Promise<JobRecord | null>;\n /**\n * Append an internal (child) job to the current job's internalJobs list.\n * Used when this worker dispatches another worker (fire-and-forget or await).\n */\n appendInternalJob?(entry: { jobId: string; workerId: string }): Promise<void>;\n /**\n * Get any job by jobId (e.g. to poll child job status when await: true).\n * @returns Job record or null if not found\n */\n getJob?(jobId: string): Promise<JobRecord | null>;\n}\n\n/** Max SQS delay in seconds (AWS limit). */\nexport const SQS_MAX_DELAY_SECONDS = 900;\n\n/** Options for ctx.dispatchWorker (worker-to-worker). */\nexport interface DispatchWorkerOptions {\n webhookUrl?: string;\n metadata?: Record<string, any>;\n /** Optional job ID for the child job (default: generated). */\n jobId?: string;\n /** If true, poll job store until child completes or fails; otherwise fire-and-forget. */\n await?: boolean;\n pollIntervalMs?: number;\n pollTimeoutMs?: number;\n /**\n * Delay before the child is invoked (fire-and-forget only; ignored when await is true).\n * Uses SQS DelaySeconds (0–900). In local mode, waits this many seconds before sending the trigger request.\n */\n delaySeconds?: number;\n}\n\nexport interface WorkerHandlerParams<INPUT, OUTPUT> {\n input: INPUT;\n ctx: {\n jobId: string;\n workerId: string;\n requestId?: string;\n /**\n * Job store interface for updating and retrieving job state.\n * Uses MongoDB directly when configured; never HTTP/origin URL.\n */\n jobStore?: JobStore;\n /**\n * Dispatch another worker (fire-and-forget or await). Uses WORKER_QUEUE_URL_<SANITIZED_ID> env.\n * Always provided by the runtime (Lambda and local).\n */\n dispatchWorker: (\n workerId: string,\n input: unknown,\n options?: DispatchWorkerOptions\n ) => Promise<{ jobId: string; messageId?: string; output?: unknown }>;\n [key: string]: any;\n };\n}\n\nexport type WorkerHandler<INPUT, OUTPUT> = (\n params: WorkerHandlerParams<INPUT, OUTPUT>\n) => Promise<OUTPUT>;\n\n/** Result of getNextStep for queue chaining. */\nexport interface QueueNextStep {\n workerId: string;\n delaySeconds?: number;\n mapInputFromPrev?: string;\n}\n\n/** Runtime helpers for queue-aware wrappers (provided by generated registry). */\nexport interface QueueRuntime {\n getNextStep(queueId: string, stepIndex: number): QueueNextStep | undefined;\n invokeMapInput?(\n queueId: string,\n stepIndex: number,\n prevOutput: unknown,\n initialInput: unknown\n ): Promise<unknown> | unknown;\n}\n\nconst WORKER_QUEUE_KEY = '__workerQueue';\nasync function notifyQueueJobStep(\n queueJobId: string,\n action: 'start' | 'complete' | 'fail' | 'append',\n params: {\n stepIndex?: number;\n workerJobId: string;\n workerId?: string;\n output?: unknown;\n error?: { message: string };\n input?: unknown;\n }\n): Promise<void> {\n try {\n if (action === 'append') {\n if (!params.workerId || !params.workerJobId) return;\n await appendQueueJobStepInStore({\n queueJobId,\n workerId: params.workerId,\n workerJobId: params.workerJobId,\n });\n if (process.env.DEBUG_WORKER_QUEUES === '1') {\n console.log('[Worker] Queue job step appended', {\n queueJobId,\n workerId: params.workerId,\n workerJobId: params.workerJobId,\n });\n }\n return;\n }\n\n if (params.stepIndex === undefined) return;\n\n const status =\n action === 'start'\n ? 'running'\n : action === 'complete'\n ? 'completed'\n : action === 'fail'\n ? 'failed'\n : undefined;\n if (!status) return;\n\n await updateQueueJobStepInStore({\n queueJobId,\n stepIndex: params.stepIndex,\n workerId: params.workerId || '',\n workerJobId: params.workerJobId,\n status,\n input: params.input,\n output: params.output,\n error: params.error,\n });\n if (process.env.DEBUG_WORKER_QUEUES === '1') {\n console.log('[Worker] Queue job step updated', {\n queueJobId,\n action,\n stepIndex: params.stepIndex,\n status,\n });\n }\n } catch (err: any) {\n console.warn('[Worker] Queue job update error:', {\n queueJobId,\n action,\n error: err?.message ?? String(err),\n });\n }\n}\n\n/**\n * Wraps a user handler so that when the job has __workerQueue context (from\n * dispatchQueue or queue cron), it dispatches the next worker in the sequence\n * after the handler completes. Uses literal worker IDs so the CLI env injection\n * picks up WORKER_QUEUE_URL_* for next-step workers.\n */\nexport function wrapHandlerForQueue<INPUT, OUTPUT>(\n handler: WorkerHandler<INPUT, OUTPUT>,\n queueRuntime: QueueRuntime\n): WorkerHandler<INPUT & { __workerQueue?: { id: string; stepIndex: number; initialInput: unknown } }, OUTPUT> {\n return async (params) => {\n const queueContext = (params.input as any)?.[WORKER_QUEUE_KEY];\n const output = await handler(params);\n\n if (!queueContext || typeof queueContext !== 'object' || !queueContext.id) {\n return output;\n }\n\n const { id: queueId, stepIndex, initialInput, queueJobId } = queueContext;\n const next = queueRuntime.getNextStep(queueId, stepIndex);\n if (!next) {\n return output;\n }\n\n const childJobId = `job-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;\n if (queueJobId) {\n await notifyQueueJobStep(queueJobId, 'append', {\n workerJobId: childJobId,\n workerId: next.workerId,\n });\n }\n\n let nextInput: unknown = output;\n if (next.mapInputFromPrev && typeof queueRuntime.invokeMapInput === 'function') {\n nextInput = await queueRuntime.invokeMapInput(queueId, stepIndex + 1, output, initialInput);\n }\n\n const nextInputWithQueue = {\n ...(nextInput !== null && typeof nextInput === 'object' ? (nextInput as Record<string, unknown>) : { value: nextInput }),\n [WORKER_QUEUE_KEY]: {\n id: queueId,\n stepIndex: stepIndex + 1,\n initialInput,\n queueJobId,\n },\n };\n\n const debug = process.env.AI_WORKER_QUEUES_DEBUG === '1';\n if (debug) {\n console.log('[Worker] Queue chain dispatching next:', {\n queueId,\n fromStep: stepIndex,\n nextWorkerId: next.workerId,\n delaySeconds: next.delaySeconds,\n });\n }\n\n await params.ctx.dispatchWorker(next.workerId, nextInputWithQueue, {\n await: false,\n delaySeconds: next.delaySeconds,\n jobId: childJobId,\n });\n\n return output;\n };\n}\n\nexport interface SQSMessageBody {\n workerId: string;\n jobId: string;\n input: any;\n context: Record<string, any>;\n webhookUrl?: string;\n /** @deprecated Never use. Job updates use MongoDB only. */\n jobStoreUrl?: string;\n metadata?: Record<string, any>;\n timestamp: string;\n}\n\nexport interface WebhookPayload {\n jobId: string;\n workerId: string;\n status: 'success' | 'error';\n output?: any;\n error?: {\n message: string;\n stack?: string;\n name?: string;\n };\n metadata?: Record<string, any>;\n}\n\nconst DEFAULT_POLL_INTERVAL_MS = 2000;\nconst DEFAULT_POLL_TIMEOUT_MS = 15 * 60 * 1000; // 15 minutes\n\nfunction sanitizeWorkerIdForEnv(workerId: string): string {\n return workerId.replace(/-/g, '_').toUpperCase();\n}\n\nfunction getQueueUrlForWorker(calleeWorkerId: string): string | undefined {\n const key = `WORKER_QUEUE_URL_${sanitizeWorkerIdForEnv(calleeWorkerId)}`;\n return process.env[key]?.trim() || undefined;\n}\n\n/**\n * Create dispatchWorker for use in handler context (Lambda).\n * Sends message to SQS, appends to parent internalJobs, optionally polls until child completes.\n */\nfunction createDispatchWorker(\n parentJobId: string,\n parentWorkerId: string,\n parentContext: Record<string, any>,\n jobStore: JobStore | undefined\n): (\n workerId: string,\n input: unknown,\n options?: DispatchWorkerOptions\n) => Promise<{ jobId: string; messageId?: string; output?: unknown }> {\n return async (\n calleeWorkerId: string,\n input: unknown,\n options?: DispatchWorkerOptions\n ): Promise<{ jobId: string; messageId?: string; output?: unknown }> => {\n const childJobId =\n options?.jobId ||\n `job-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;\n const metadata = options?.metadata ?? {};\n const serializedContext: Record<string, any> = {};\n if (parentContext.requestId) serializedContext.requestId = parentContext.requestId;\n\n const messageBody: SQSMessageBody = {\n workerId: calleeWorkerId,\n jobId: childJobId,\n input: input ?? {},\n context: serializedContext,\n webhookUrl: options?.webhookUrl,\n metadata,\n timestamp: new Date().toISOString(),\n };\n\n const queueUrl = getQueueUrlForWorker(calleeWorkerId);\n\n if (queueUrl) {\n const region = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';\n const sqs = new SQSClient({ region });\n // SQS message timer (per-message DelaySeconds): message stays invisible for N seconds.\n // Calling worker returns immediately; no computation during delay. See:\n // https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-delay-queues.html\n const delaySeconds =\n options?.await !== true && options?.delaySeconds != null\n ? Math.min(SQS_MAX_DELAY_SECONDS, Math.max(0, Math.floor(options.delaySeconds)))\n : undefined;\n const sendResult = await sqs.send(\n new SendMessageCommand({\n QueueUrl: queueUrl,\n MessageBody: JSON.stringify(messageBody),\n ...(delaySeconds !== undefined && delaySeconds > 0 ? { DelaySeconds: delaySeconds } : {}),\n })\n );\n const messageId = sendResult.MessageId ?? undefined;\n\n if (jobStore?.appendInternalJob) {\n await jobStore.appendInternalJob({ jobId: childJobId, workerId: calleeWorkerId });\n }\n\n if (options?.await && jobStore?.getJob) {\n const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;\n const pollTimeoutMs = options.pollTimeoutMs ?? DEFAULT_POLL_TIMEOUT_MS;\n const deadline = Date.now() + pollTimeoutMs;\n while (Date.now() < deadline) {\n const child = await jobStore.getJob(childJobId);\n if (!child) {\n await new Promise((r) => setTimeout(r, pollIntervalMs));\n continue;\n }\n if (child.status === 'completed') {\n return { jobId: childJobId, messageId, output: child.output };\n }\n if (child.status === 'failed') {\n const err = child.error;\n throw new Error(\n err?.message ?? `Child worker ${calleeWorkerId} failed`\n );\n }\n await new Promise((r) => setTimeout(r, pollIntervalMs));\n }\n throw new Error(\n `Child worker ${calleeWorkerId} (${childJobId}) did not complete within ${pollTimeoutMs}ms`\n );\n }\n\n return { jobId: childJobId, messageId };\n }\n\n // Fallback: no queue URL (e.g. local dev). Caller (index.ts) should provide in-process dispatch.\n throw new Error(\n `WORKER_QUEUE_URL_${sanitizeWorkerIdForEnv(calleeWorkerId)} is not set. ` +\n 'Configure queue URL for worker-to-worker dispatch, or run in local mode.'\n );\n };\n}\n\n/**\n * Sends a webhook callback to the specified URL.\n */\nasync function sendWebhook(\n webhookUrl: string,\n payload: WebhookPayload\n): Promise<void> {\n try {\n const response = await fetch(webhookUrl, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'User-Agent': 'ai-router-worker/1.0',\n },\n body: JSON.stringify(payload),\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => '');\n console.error('[Worker] Webhook callback failed:', {\n url: webhookUrl,\n status: response.status,\n statusText: response.statusText,\n errorText,\n });\n // Don't throw - webhook failures shouldn't fail the Lambda\n } else {\n console.log('[Worker] Webhook callback successful:', {\n url: webhookUrl,\n status: response.status,\n });\n }\n } catch (error: any) {\n console.error('[Worker] Webhook callback error:', {\n url: webhookUrl,\n error: error?.message || String(error),\n stack: error?.stack,\n });\n // Don't throw - webhook failures shouldn't fail the Lambda\n }\n}\n\n/**\n * Creates a Lambda handler function that processes SQS events for workers.\n * Job store: MongoDB only. Never uses HTTP/origin URL for job updates.\n *\n * @param handler - The user's worker handler function\n * @param outputSchema - Optional Zod schema for output validation\n * @returns A Lambda handler function\n */\nexport function createLambdaHandler<INPUT, OUTPUT>(\n handler: WorkerHandler<INPUT, OUTPUT>,\n outputSchema?: ZodType<OUTPUT>\n): (event: SQSEvent, context: LambdaContext) => Promise<void> {\n return async (event: SQSEvent, lambdaContext: LambdaContext) => {\n const promises = event.Records.map(async (record: SQSRecord) => {\n let messageBody: SQSMessageBody | null = null;\n try {\n messageBody = JSON.parse(record.body) as SQSMessageBody;\n\n const { workerId, jobId, input, context, webhookUrl, metadata = {} } =\n messageBody;\n\n // Idempotency: skip if this job was already completed or failed (e.g. SQS redelivery or duplicate trigger).\n // Only the Lambda that processes a message creates/updates that job's key; parent workers only append to internalJobs and poll – they never write child job documents.\n const raw = (process.env.WORKER_DATABASE_TYPE || 'upstash-redis').toLowerCase();\n const jobStoreType: 'mongodb' | 'upstash-redis' =\n raw === 'mongodb' ? 'mongodb' : 'upstash-redis';\n if (jobStoreType === 'upstash-redis' && isRedisJobStoreConfigured()) {\n const existing = await loadRedisJob(jobId);\n if (existing && (existing.status === 'completed' || existing.status === 'failed')) {\n console.log('[Worker] Skipping already terminal job (idempotent):', {\n jobId,\n workerId,\n status: existing.status,\n });\n return;\n }\n } else if (jobStoreType === 'mongodb' || isMongoJobStoreConfigured()) {\n const existing = await getMongoJobById(jobId);\n if (existing && (existing.status === 'completed' || existing.status === 'failed')) {\n console.log('[Worker] Skipping already terminal job (idempotent):', {\n jobId,\n workerId,\n status: existing.status,\n });\n return;\n }\n }\n\n // Select job store and upsert this message's job only (never write child job documents from parent).\n let jobStore: JobStore | undefined;\n if (\n jobStoreType === 'upstash-redis' &&\n isRedisJobStoreConfigured()\n ) {\n await upsertRedisJob(jobId, workerId, input, metadata);\n jobStore = createRedisJobStore(workerId, jobId, input, metadata);\n } else if (\n jobStoreType === 'mongodb' ||\n isMongoJobStoreConfigured()\n ) {\n await upsertJob(jobId, workerId, input, metadata);\n jobStore = createMongoJobStore(workerId, jobId, input, metadata);\n }\n\n const baseContext = {\n jobId,\n workerId,\n requestId: context.requestId || lambdaContext.awsRequestId,\n ...context,\n };\n const handlerContext = {\n ...baseContext,\n ...(jobStore ? { jobStore } : {}),\n dispatchWorker: createDispatchWorker(\n jobId,\n workerId,\n baseContext,\n jobStore\n ),\n };\n\n if (jobStore) {\n try {\n await jobStore.update({ status: 'running' });\n console.log('[Worker] Job status updated to running:', {\n jobId,\n workerId,\n });\n } catch (error: any) {\n console.warn('[Worker] Failed to update status to running:', {\n jobId,\n workerId,\n error: error?.message || String(error),\n });\n }\n }\n\n const queueCtx = (input as any)?.__workerQueue ?? metadata?.__workerQueue;\n if (queueCtx?.queueJobId && typeof queueCtx.stepIndex === 'number') {\n // Ensure initial queue job exists (mainly for cron/queue-starter paths)\n if (queueCtx.stepIndex === 0) {\n try {\n await upsertInitialQueueJob({\n queueJobId: queueCtx.queueJobId,\n queueId: queueCtx.id,\n firstWorkerId: workerId,\n firstWorkerJobId: jobId,\n metadata,\n });\n } catch (e: any) {\n console.warn('[Worker] Failed to upsert initial queue job:', {\n queueJobId: queueCtx.queueJobId,\n queueId: queueCtx.id,\n error: e?.message ?? String(e),\n });\n }\n }\n await notifyQueueJobStep(queueCtx.queueJobId, 'start', {\n stepIndex: queueCtx.stepIndex,\n workerJobId: jobId,\n workerId,\n input,\n });\n }\n\n let output: OUTPUT;\n try {\n output = await handler({\n input: input as INPUT,\n ctx: handlerContext,\n });\n\n if (outputSchema) {\n output = outputSchema.parse(output);\n }\n } catch (error: any) {\n const errorPayload: WebhookPayload = {\n jobId,\n workerId,\n status: 'error',\n error: {\n message: error.message || 'Unknown error',\n stack: error.stack,\n name: error.name || 'Error',\n },\n metadata,\n };\n\n if (jobStore) {\n try {\n await jobStore.update({\n status: 'failed',\n error: errorPayload.error,\n });\n console.log('[Worker] Job status updated to failed:', {\n jobId,\n workerId,\n });\n } catch (updateError: any) {\n console.warn('[Worker] Failed to update job store on error:', {\n jobId,\n workerId,\n error: updateError?.message || String(updateError),\n });\n }\n }\n\n const queueCtxFail = (input as any)?.__workerQueue ?? metadata?.__workerQueue;\n if (queueCtxFail?.queueJobId && typeof queueCtxFail.stepIndex === 'number') {\n await notifyQueueJobStep(queueCtxFail.queueJobId, 'fail', {\n stepIndex: queueCtxFail.stepIndex,\n workerJobId: jobId,\n workerId,\n error: errorPayload.error,\n });\n }\n\n if (webhookUrl) {\n await sendWebhook(webhookUrl, errorPayload);\n }\n throw error;\n }\n\n if (jobStore) {\n try {\n await jobStore.update({\n status: 'completed',\n output,\n });\n console.log('[Worker] Job status updated to completed:', {\n jobId,\n workerId,\n });\n } catch (updateError: any) {\n console.warn('[Worker] Failed to update job store on success:', {\n jobId,\n workerId,\n error: updateError?.message || String(updateError),\n });\n }\n }\n\n const queueCtxSuccess = (input as any)?.__workerQueue ?? metadata?.__workerQueue;\n if (queueCtxSuccess?.queueJobId && typeof queueCtxSuccess.stepIndex === 'number') {\n await notifyQueueJobStep(queueCtxSuccess.queueJobId, 'complete', {\n stepIndex: queueCtxSuccess.stepIndex,\n workerJobId: jobId,\n workerId,\n output,\n });\n }\n\n console.log('[Worker] Job completed:', {\n jobId,\n workerId,\n output,\n });\n\n const successPayload: WebhookPayload = {\n jobId,\n workerId,\n status: 'success',\n output,\n metadata,\n };\n\n if (webhookUrl) {\n await sendWebhook(webhookUrl, successPayload);\n }\n } catch (error: any) {\n console.error('[Worker] Error processing SQS record:', {\n jobId: messageBody?.jobId ?? '(parse failed)',\n workerId: messageBody?.workerId ?? '(parse failed)',\n error: error?.message || String(error),\n stack: error?.stack,\n });\n throw error;\n }\n });\n\n await Promise.all(promises);\n };\n}\n","/**\n * MongoDB-backed job store for Lambda workers.\n * Updates jobs directly in MongoDB; never uses HTTP/origin URL.\n *\n * Env: MONGODB_WORKER_URI (or MONGODB_URI), MONGODB_WORKER_DB (or MONGODB_DB),\n * MONGODB_WORKER_JOBS_COLLECTION (default: worker_jobs).\n */\n\nimport { MongoClient, type Collection } from 'mongodb';\nimport type { JobStore, JobStoreUpdate } from './handler';\n\nconst uri = process.env.MONGODB_WORKER_URI || process.env.DATABASE_MONGODB_URI || process.env.MONGODB_URI;\nconst dbName =\n process.env.MONGODB_WORKER_DB ||\n process.env.MONGODB_DB ||\n 'worker';\nconst collectionName =\n process.env.MONGODB_WORKER_JOBS_COLLECTION || 'worker_jobs';\n\ntype InternalJobEntry = { jobId: string; workerId: string };\n\ntype Doc = {\n _id: string;\n jobId: string;\n workerId: string;\n status: 'queued' | 'running' | 'completed' | 'failed';\n input: any;\n output?: any;\n error?: { message: string; stack?: string; name?: string };\n metadata?: Record<string, any>;\n internalJobs?: InternalJobEntry[];\n createdAt: string;\n updatedAt: string;\n completedAt?: string;\n};\n\nlet clientPromise: Promise<MongoClient> | null = null;\n\nfunction getClient(): Promise<MongoClient> {\n if (!uri) {\n throw new Error(\n 'MongoDB URI required for job store. Set DATABASE_MONGODB_URI or MONGODB_URI.'\n );\n }\n if (!clientPromise) {\n clientPromise = new MongoClient(uri, {\n maxPoolSize: 10,\n minPoolSize: 0,\n serverSelectionTimeoutMS: 10_000,\n }).connect();\n }\n return clientPromise;\n}\n\nasync function getCollection(): Promise<Collection<Doc>> {\n const client = await getClient();\n return client.db(dbName).collection<Doc>(collectionName);\n}\n\n/**\n * Load a job by id (read-only). Used for idempotency check before processing.\n */\nexport async function getJobById(jobId: string): Promise<{\n jobId: string;\n workerId: string;\n status: 'queued' | 'running' | 'completed' | 'failed';\n input: any;\n output?: any;\n error?: { message: string; stack?: string };\n metadata?: Record<string, any>;\n internalJobs?: Array<{ jobId: string; workerId: string }>;\n createdAt: string;\n updatedAt: string;\n completedAt?: string;\n} | null> {\n try {\n const coll = await getCollection();\n const doc = await coll.findOne({ _id: jobId });\n if (!doc) return null;\n const { _id, ...r } = doc;\n return r as any;\n } catch (e: any) {\n console.error('[Worker] MongoDB getJobById failed:', {\n jobId,\n error: e?.message ?? String(e),\n });\n return null;\n }\n}\n\n/**\n * Create a JobStore that reads/writes directly to MongoDB.\n * Caller must ensure the job exists (upsert on first use).\n */\nexport function createMongoJobStore(\n workerId: string,\n jobId: string,\n input: any,\n metadata: Record<string, any>\n): JobStore {\n return {\n update: async (update: JobStoreUpdate): Promise<void> => {\n try {\n const coll = await getCollection();\n const now = new Date().toISOString();\n const existing = await coll.findOne({ _id: jobId });\n\n let metadataUpdate: Record<string, any> = { ...(existing?.metadata ?? {}) };\n if (update.metadata) {\n Object.assign(metadataUpdate, update.metadata);\n }\n if (update.progress !== undefined || update.progressMessage !== undefined) {\n metadataUpdate.progress = update.progress;\n metadataUpdate.progressMessage = update.progressMessage;\n }\n\n const set: Partial<Doc> = {\n updatedAt: now,\n metadata: metadataUpdate,\n };\n if (update.status !== undefined) {\n set.status = update.status;\n if (['completed', 'failed'].includes(update.status) && !existing?.completedAt) {\n set.completedAt = now;\n }\n }\n if (update.output !== undefined) set.output = update.output;\n if (update.error !== undefined) set.error = update.error;\n\n if (existing) {\n await coll.updateOne({ _id: jobId }, { $set: set });\n } else {\n const doc: Doc = {\n _id: jobId,\n jobId,\n workerId,\n status: (update.status as Doc['status']) ?? 'queued',\n input: input ?? {},\n output: update.output,\n error: update.error,\n metadata: metadataUpdate,\n createdAt: now,\n updatedAt: now,\n completedAt: set.completedAt,\n };\n if (doc.status === 'completed' || doc.status === 'failed') {\n doc.completedAt = doc.completedAt ?? now;\n }\n await coll.updateOne({ _id: jobId }, { $set: doc }, { upsert: true });\n }\n } catch (e: any) {\n console.error('[Worker] MongoDB job store update failed:', {\n jobId,\n workerId,\n error: e?.message ?? String(e),\n });\n }\n },\n get: async () => {\n try {\n const coll = await getCollection();\n const doc = await coll.findOne({ _id: jobId });\n if (!doc) return null;\n const { _id, ...r } = doc;\n return r as any;\n } catch (e: any) {\n console.error('[Worker] MongoDB job store get failed:', {\n jobId,\n workerId,\n error: e?.message ?? String(e),\n });\n return null;\n }\n },\n appendInternalJob: async (entry: { jobId: string; workerId: string }): Promise<void> => {\n try {\n const coll = await getCollection();\n await coll.updateOne(\n { _id: jobId },\n { $push: { internalJobs: entry } }\n );\n } catch (e: any) {\n console.error('[Worker] MongoDB job store appendInternalJob failed:', {\n jobId,\n workerId,\n error: e?.message ?? String(e),\n });\n }\n },\n getJob: async (otherJobId: string): Promise<{\n jobId: string;\n workerId: string;\n status: 'queued' | 'running' | 'completed' | 'failed';\n input: any;\n output?: any;\n error?: { message: string; stack?: string };\n metadata?: Record<string, any>;\n internalJobs?: Array<{ jobId: string; workerId: string }>;\n createdAt: string;\n updatedAt: string;\n completedAt?: string;\n } | null> => {\n try {\n const coll = await getCollection();\n const doc = await coll.findOne({ _id: otherJobId });\n if (!doc) return null;\n const { _id, ...r } = doc;\n return r as any;\n } catch (e: any) {\n console.error('[Worker] MongoDB job store getJob failed:', {\n otherJobId,\n error: e?.message ?? String(e),\n });\n return null;\n }\n },\n };\n}\n\n/**\n * Upsert initial job record in MongoDB (queued).\n * Call this when the Lambda starts processing a message.\n */\nexport async function upsertJob(\n jobId: string,\n workerId: string,\n input: any,\n metadata: Record<string, any>\n): Promise<void> {\n const coll = await getCollection();\n const now = new Date().toISOString();\n await coll.updateOne(\n { _id: jobId },\n {\n $set: {\n _id: jobId,\n jobId,\n workerId,\n status: 'queued',\n input: input ?? {},\n metadata: metadata ?? {},\n createdAt: now,\n updatedAt: now,\n },\n },\n { upsert: true }\n );\n}\n\nexport function isMongoJobStoreConfigured(): boolean {\n return Boolean(uri?.trim());\n}\n","import { Redis } from '@upstash/redis';\nimport type { JobStore, JobStoreUpdate, JobRecord } from './handler';\n\n// Canonical: WORKER_* first, then UPSTASH_* / REDIS_* / WORKFLOW_* fallbacks\nconst redisUrl =\n process.env.WORKER_UPSTASH_REDIS_REST_URL ||\n process.env.UPSTASH_REDIS_REST_URL ||\n process.env.UPSTASH_REDIS_URL;\nconst redisToken =\n process.env.WORKER_UPSTASH_REDIS_REST_TOKEN ||\n process.env.UPSTASH_REDIS_REST_TOKEN ||\n process.env.UPSTASH_REDIS_TOKEN;\nconst jobKeyPrefix =\n process.env.WORKER_UPSTASH_REDIS_JOBS_PREFIX ||\n process.env.UPSTASH_REDIS_KEY_PREFIX ||\n process.env.REDIS_WORKER_JOB_PREFIX ||\n 'worker:jobs:';\nconst defaultTtlSeconds = 60 * 60 * 24 * 7; // 7 days\nconst jobTtlSeconds =\n typeof process.env.WORKER_JOBS_TTL_SECONDS === 'string'\n ? parseInt(process.env.WORKER_JOBS_TTL_SECONDS, 10) || defaultTtlSeconds\n : typeof process.env.REDIS_WORKER_JOB_TTL_SECONDS === 'string'\n ? parseInt(process.env.REDIS_WORKER_JOB_TTL_SECONDS, 10) || defaultTtlSeconds\n : typeof process.env.WORKFLOW_JOBS_TTL_SECONDS === 'string'\n ? parseInt(process.env.WORKFLOW_JOBS_TTL_SECONDS, 10) || defaultTtlSeconds\n : defaultTtlSeconds;\n\nlet redisClient: Redis | null = null;\n\nfunction getRedis(): Redis {\n if (!redisUrl || !redisToken) {\n throw new Error(\n 'Upstash Redis configuration missing. Set WORKER_UPSTASH_REDIS_REST_URL and WORKER_UPSTASH_REDIS_REST_TOKEN (or UPSTASH_REDIS_REST_URL/UPSTASH_REDIS_REST_TOKEN).'\n );\n }\n if (!redisClient) {\n redisClient = new Redis({\n url: redisUrl,\n token: redisToken,\n });\n }\n return redisClient;\n}\n\nfunction jobKey(jobId: string): string {\n return `${jobKeyPrefix}${jobId}`;\n}\n\n/** Separate LIST key for internal job refs; each RPUSH is atomic so no race when appending multiple. */\nfunction internalListKey(jobId: string): string {\n return `${jobKeyPrefix}${jobId}:internal`;\n}\n\nexport function isRedisJobStoreConfigured(): boolean {\n return Boolean((redisUrl || '').trim() && (redisToken || '').trim());\n}\n\n/** Load a job by id (read-only). Used for idempotency check before processing. */\nexport async function loadJob(jobId: string): Promise<JobRecord | null> {\n const redis = getRedis();\n const key = jobKey(jobId);\n const data = await redis.hgetall<Record<string, string>>(key);\n if (!data || Object.keys(data).length === 0) return null;\n const parseJson = <T>(val?: string | null): T | undefined => {\n if (!val) return undefined;\n try {\n return JSON.parse(val) as T;\n } catch {\n return undefined;\n }\n };\n // Prefer atomic list key for internal jobs; fallback to hash field for old records\n const listKey = internalListKey(jobId);\n const listItems = await redis.lrange<string>(listKey, 0, -1);\n let internalJobs: Array<{ jobId: string; workerId: string }> | undefined;\n if (listItems && listItems.length > 0) {\n internalJobs = listItems.map((s) => {\n try {\n return JSON.parse(s) as { jobId: string; workerId: string };\n } catch {\n return null;\n }\n }).filter(Boolean) as Array<{ jobId: string; workerId: string }>;\n } else {\n internalJobs = parseJson<Array<{ jobId: string; workerId: string }>>(data.internalJobs);\n }\n const record: JobRecord = {\n jobId: data.jobId,\n workerId: data.workerId,\n status: (data.status as JobRecord['status']) || 'queued',\n input: parseJson<any>(data.input) ?? {},\n output: parseJson<any>(data.output),\n error: parseJson<any>(data.error),\n metadata: parseJson<Record<string, any>>(data.metadata) ?? {},\n internalJobs,\n createdAt: data.createdAt,\n updatedAt: data.updatedAt,\n completedAt: data.completedAt,\n };\n return record;\n}\n\nexport function createRedisJobStore(\n workerId: string,\n jobId: string,\n input: any,\n metadata: Record<string, any>\n): JobStore {\n return {\n update: async (update: JobStoreUpdate): Promise<void> => {\n const redis = getRedis();\n const key = jobKey(jobId);\n const now = new Date().toISOString();\n\n // Load existing to merge metadata/progress if needed\n const existing = await loadJob(jobId);\n const next: Partial<JobRecord> = {};\n\n // Start from existing metadata\n const mergedMeta: Record<string, any> = { ...(existing?.metadata ?? {}) };\n if (update.metadata) {\n Object.assign(mergedMeta, update.metadata);\n }\n if (update.progress !== undefined || update.progressMessage !== undefined) {\n mergedMeta.progress = update.progress;\n mergedMeta.progressMessage = update.progressMessage;\n }\n\n next.metadata = mergedMeta;\n if (update.status !== undefined) {\n next.status = update.error ? 'failed' : update.status;\n if ((update.status === 'completed' || update.status === 'failed') && !existing?.completedAt) {\n next.completedAt = now;\n }\n }\n if (update.output !== undefined) next.output = update.output;\n if (update.error !== undefined) next.error = update.error;\n\n const toSet: Record<string, string> = {};\n if (next.status) toSet['status'] = String(next.status);\n if (next.output !== undefined) toSet['output'] = JSON.stringify(next.output);\n if (next.error !== undefined) toSet['error'] = JSON.stringify(next.error);\n if (next.metadata !== undefined) toSet['metadata'] = JSON.stringify(next.metadata);\n if (next.completedAt) {\n toSet['completedAt'] = next.completedAt;\n }\n toSet['updatedAt'] = now;\n\n await redis.hset(key, toSet);\n if (jobTtlSeconds > 0) {\n await redis.expire(key, jobTtlSeconds);\n }\n },\n get: async () => {\n return loadJob(jobId);\n },\n appendInternalJob: async (entry) => {\n const redis = getRedis();\n const listKey = internalListKey(jobId);\n await redis.rpush(listKey, JSON.stringify(entry));\n const mainKey = jobKey(jobId);\n await redis.hset(mainKey, { updatedAt: new Date().toISOString() });\n if (jobTtlSeconds > 0) {\n await redis.expire(listKey, jobTtlSeconds);\n await redis.expire(mainKey, jobTtlSeconds);\n }\n },\n getJob: async (otherJobId: string) => {\n return loadJob(otherJobId);\n },\n };\n}\n\nexport async function upsertRedisJob(\n jobId: string,\n workerId: string,\n input: any,\n metadata: Record<string, any>\n): Promise<void> {\n const redis = getRedis();\n const key = jobKey(jobId);\n const now = new Date().toISOString();\n const doc: Partial<JobRecord> = {\n jobId,\n workerId,\n status: 'queued',\n input,\n metadata,\n createdAt: now,\n updatedAt: now,\n };\n const toSet: Record<string, string> = {\n jobId: jobId,\n workerId: workerId,\n status: doc.status!,\n input: JSON.stringify(doc.input ?? {}),\n metadata: JSON.stringify(doc.metadata ?? {}),\n createdAt: now,\n updatedAt: now,\n };\n await redis.hset(key, toSet);\n if (jobTtlSeconds > 0) {\n await redis.expire(key, jobTtlSeconds);\n }\n}\n\n","/**\n * Queue job store for worker queues (MongoDB or Upstash Redis).\n *\n * Mirrors the worker_jobs pattern but optimized for queues:\n * - MongoDB: collection `queue_jobs` (configurable via MONGODB_QUEUE_JOBS_COLLECTION)\n * - Upstash Redis: JSON blob per queue job with compact step entries\n *\n * This module is runtime-only (used by Lambda workers). Next.js APIs can read\n * the same collections/keys to show queue progress.\n */\n\nimport type { Redis } from '@upstash/redis';\nimport { Redis as UpstashRedis } from '@upstash/redis';\nimport { MongoClient, type Collection } from 'mongodb';\n\ntype QueueJobStep = {\n workerId: string;\n workerJobId: string;\n status: 'queued' | 'running' | 'completed' | 'failed';\n input?: unknown;\n output?: unknown;\n error?: { message: string };\n startedAt?: string;\n completedAt?: string;\n};\n\ntype QueueJobDoc = {\n _id: string; // queueJobId\n id: string;\n queueId: string;\n status: 'running' | 'completed' | 'failed' | 'partial';\n steps: QueueJobStep[];\n metadata?: Record<string, unknown>;\n createdAt: string;\n updatedAt: string;\n completedAt?: string;\n};\n\n// === Mongo backend (shares connection pattern with mongoJobStore) ===\n\nconst mongoUri = process.env.DATABASE_MONGODB_URI || process.env.MONGODB_URI;\nconst mongoDbName =\n process.env.DATABASE_MONGODB_DB ||\n process.env.MONGODB_DB ||\n 'mediamake';\nconst mongoQueueCollectionName =\n process.env.MONGODB_QUEUE_JOBS_COLLECTION || 'queue_jobs';\n\nlet mongoClientPromise: Promise<MongoClient> | null = null;\n\nasync function getMongoClient(): Promise<MongoClient> {\n if (!mongoUri) {\n throw new Error(\n 'MongoDB URI required for queue job store. Set DATABASE_MONGODB_URI or MONGODB_URI.'\n );\n }\n if (!mongoClientPromise) {\n mongoClientPromise = new MongoClient(mongoUri, {\n maxPoolSize: 10,\n minPoolSize: 0,\n serverSelectionTimeoutMS: 10_000,\n }).connect();\n }\n return mongoClientPromise;\n}\n\nasync function getMongoQueueCollection(): Promise<Collection<QueueJobDoc>> {\n const client = await getMongoClient();\n return client.db(mongoDbName).collection<QueueJobDoc>(mongoQueueCollectionName);\n}\n\n// === Redis backend (Upstash) ===\n\nconst redisUrl =\n process.env.WORKER_UPSTASH_REDIS_REST_URL ||\n process.env.UPSTASH_REDIS_REST_URL ||\n process.env.UPSTASH_REDIS_URL;\nconst redisToken =\n process.env.WORKER_UPSTASH_REDIS_REST_TOKEN ||\n process.env.UPSTASH_REDIS_REST_TOKEN ||\n process.env.UPSTASH_REDIS_TOKEN;\nconst queueKeyPrefix =\n process.env.WORKER_UPSTASH_REDIS_QUEUE_PREFIX ||\n process.env.UPSTASH_REDIS_QUEUE_PREFIX ||\n 'worker:queue-jobs:';\n\nconst defaultTtlSeconds = 60 * 60 * 24 * 7; // 7 days\nconst queueJobTtlSeconds =\n typeof process.env.WORKER_QUEUE_JOBS_TTL_SECONDS === 'string'\n ? parseInt(process.env.WORKER_QUEUE_JOBS_TTL_SECONDS, 10) || defaultTtlSeconds\n : typeof process.env.WORKER_JOBS_TTL_SECONDS === 'string'\n ? parseInt(process.env.WORKER_JOBS_TTL_SECONDS, 10) || defaultTtlSeconds\n : defaultTtlSeconds;\n\nlet redisClient: Redis | null = null;\n\nfunction getRedis(): Redis {\n if (!redisUrl || !redisToken) {\n throw new Error(\n 'Upstash Redis configuration missing for queue job store. Set WORKER_UPSTASH_REDIS_REST_URL and WORKER_UPSTASH_REDIS_REST_TOKEN (or UPSTASH_REDIS_REST_URL/UPSTASH_REDIS_REST_TOKEN).'\n );\n }\n if (!redisClient) {\n redisClient = new UpstashRedis({\n url: redisUrl,\n token: redisToken,\n });\n }\n return redisClient;\n}\n\nfunction queueKey(id: string): string {\n return `${queueKeyPrefix}${id}`;\n}\n\ntype QueueJobRecord = Omit<QueueJobDoc, '_id'>;\n\n/** Hash values from Upstash hgetall may be auto-parsed (array/object) or raw strings. */\nfunction stepsFromHash(val: unknown): QueueJobStep[] {\n if (Array.isArray(val)) return val as QueueJobStep[];\n if (typeof val === 'string') {\n try {\n const parsed = JSON.parse(val) as QueueJobStep[];\n return Array.isArray(parsed) ? parsed : [];\n } catch {\n return [];\n }\n }\n return [];\n}\n\nfunction metadataFromHash(val: unknown): Record<string, unknown> {\n if (val && typeof val === 'object' && !Array.isArray(val)) return val as Record<string, unknown>;\n if (typeof val === 'string') {\n try {\n const parsed = JSON.parse(val) as Record<string, unknown>;\n return parsed && typeof parsed === 'object' ? parsed : {};\n } catch {\n return {};\n }\n }\n return {};\n}\n\nasync function loadQueueJobRedis(queueJobId: string): Promise<QueueJobRecord | null> {\n const redis = getRedis();\n const key = queueKey(queueJobId);\n const data = await redis.hgetall(key);\n if (!data || typeof data !== 'object' || Object.keys(data).length === 0) return null;\n const d = data as Record<string, unknown>;\n const record: QueueJobRecord = {\n id: (d.id === undefined ? queueJobId : String(d.id)) as string,\n queueId: String(d.queueId ?? ''),\n status: (String(d.status ?? 'running') as QueueJobRecord['status']),\n steps: stepsFromHash(d.steps),\n metadata: metadataFromHash(d.metadata),\n createdAt: String(d.createdAt ?? new Date().toISOString()),\n updatedAt: String(d.updatedAt ?? new Date().toISOString()),\n completedAt: d.completedAt != null ? String(d.completedAt) : undefined,\n };\n return record;\n}\n\nasync function saveQueueJobRedis(record: QueueJobRecord): Promise<void> {\n const redis = getRedis();\n const key = queueKey(record.id);\n const now = new Date().toISOString();\n const toSet: Record<string, string> = {\n id: record.id,\n queueId: record.queueId,\n status: record.status,\n steps: JSON.stringify(record.steps || []),\n metadata: JSON.stringify(record.metadata || {}),\n createdAt: record.createdAt || now,\n updatedAt: record.updatedAt || now,\n };\n if (record.completedAt) {\n toSet.completedAt = record.completedAt;\n }\n await redis.hset(key, toSet);\n if (queueJobTtlSeconds > 0) {\n await redis.expire(key, queueJobTtlSeconds);\n }\n}\n\n// === Backend selection ===\n\nfunction getStoreType(): 'mongodb' | 'upstash-redis' {\n const t = (process.env.WORKER_DATABASE_TYPE || 'upstash-redis').toLowerCase();\n return t === 'mongodb' ? 'mongodb' : 'upstash-redis';\n}\n\nfunction preferMongo(): boolean {\n return getStoreType() === 'mongodb' && Boolean(mongoUri?.trim());\n}\n\nfunction preferRedis(): boolean {\n return getStoreType() !== 'mongodb' && Boolean((redisUrl || '').trim() && (redisToken || '').trim());\n}\n\n// === Public API used from handler.ts ===\n\nexport async function upsertInitialQueueJob(options: {\n queueJobId: string;\n queueId: string;\n firstWorkerId: string;\n firstWorkerJobId: string;\n metadata?: Record<string, any>;\n}): Promise<void> {\n const { queueJobId, queueId, firstWorkerId, firstWorkerJobId, metadata } = options;\n const now = new Date().toISOString();\n\n if (preferMongo()) {\n const coll = await getMongoQueueCollection();\n const existing = await coll.findOne({ _id: queueJobId });\n if (existing) {\n const steps = existing.steps ?? [];\n if (steps.length === 0) {\n steps.push({\n workerId: firstWorkerId,\n workerJobId: firstWorkerJobId,\n status: 'queued',\n });\n }\n await coll.updateOne(\n { _id: queueJobId },\n {\n $set: {\n steps,\n updatedAt: now,\n },\n }\n );\n } else {\n const doc: QueueJobDoc = {\n _id: queueJobId,\n id: queueJobId,\n queueId,\n status: 'running',\n steps: [\n {\n workerId: firstWorkerId,\n workerJobId: firstWorkerJobId,\n status: 'queued',\n },\n ],\n metadata: metadata ?? {},\n createdAt: now,\n updatedAt: now,\n };\n await coll.updateOne(\n { _id: queueJobId },\n { $set: doc },\n { upsert: true }\n );\n }\n return;\n }\n\n if (preferRedis()) {\n const existing = await loadQueueJobRedis(queueJobId);\n if (existing) {\n // Ensure we have at least one step\n if (!existing.steps || existing.steps.length === 0) {\n existing.steps = [\n {\n workerId: firstWorkerId,\n workerJobId: firstWorkerJobId,\n status: 'queued',\n },\n ];\n }\n existing.updatedAt = now;\n await saveQueueJobRedis(existing);\n } else {\n const record: QueueJobRecord = {\n id: queueJobId,\n queueId,\n status: 'running',\n steps: [\n {\n workerId: firstWorkerId,\n workerJobId: firstWorkerJobId,\n status: 'queued',\n },\n ],\n metadata: metadata ?? {},\n createdAt: now,\n updatedAt: now,\n };\n await saveQueueJobRedis(record);\n }\n }\n}\n\nexport async function updateQueueJobStepInStore(options: {\n queueJobId: string;\n queueId?: string;\n stepIndex: number;\n workerId: string;\n workerJobId: string;\n status: 'running' | 'completed' | 'failed';\n input?: unknown;\n output?: unknown;\n error?: { message: string };\n}): Promise<void> {\n const { queueJobId, stepIndex, status, input, output, error } = options;\n const now = new Date().toISOString();\n\n if (preferMongo()) {\n const coll = await getMongoQueueCollection();\n const existing = await coll.findOne({ _id: queueJobId });\n if (!existing) return;\n const step = existing.steps[stepIndex];\n if (!step) return;\n\n const mergedStep: QueueJobStep = {\n ...step,\n status,\n ...(input !== undefined && { input }),\n ...(output !== undefined && { output }),\n ...(error !== undefined && { error }),\n startedAt: step.startedAt ?? (status === 'running' ? now : step.startedAt),\n completedAt:\n step.completedAt ??\n (status === 'completed' || status === 'failed' ? now : step.completedAt),\n };\n\n const setDoc: Partial<QueueJobDoc> & { steps: QueueJobStep[] } = {\n steps: existing.steps,\n updatedAt: now,\n };\n setDoc.steps[stepIndex] = mergedStep;\n if (status === 'failed') {\n setDoc.status = 'failed';\n if (!existing.completedAt) setDoc.completedAt = now;\n } else if (status === 'completed' && stepIndex === existing.steps.length - 1) {\n setDoc.status = 'completed';\n if (!existing.completedAt) setDoc.completedAt = now;\n }\n\n await coll.updateOne(\n { _id: queueJobId },\n {\n $set: setDoc,\n }\n );\n return;\n }\n\n if (preferRedis()) {\n const existing = await loadQueueJobRedis(queueJobId);\n if (!existing) {\n // No queue job; nothing to update\n return;\n }\n const steps = existing.steps || [];\n const step = steps[stepIndex];\n if (!step) {\n return;\n }\n step.status = status;\n if (input !== undefined) step.input = input;\n if (output !== undefined) step.output = output;\n if (error !== undefined) step.error = error;\n if (status === 'running') {\n step.startedAt = step.startedAt ?? now;\n }\n if (status === 'completed' || status === 'failed') {\n step.completedAt = step.completedAt ?? now;\n }\n\n existing.steps = steps;\n existing.updatedAt = now;\n if (status === 'failed') {\n existing.status = 'failed';\n existing.completedAt = existing.completedAt ?? now;\n } else if (status === 'completed' && stepIndex === steps.length - 1) {\n existing.status = 'completed';\n existing.completedAt = existing.completedAt ?? now;\n }\n await saveQueueJobRedis(existing);\n }\n}\n\nexport async function appendQueueJobStepInStore(options: {\n queueJobId: string;\n queueId?: string;\n workerId: string;\n workerJobId: string;\n}): Promise<void> {\n const { queueJobId, workerId, workerJobId } = options;\n const now = new Date().toISOString();\n\n if (preferMongo()) {\n const coll = await getMongoQueueCollection();\n await coll.updateOne(\n { _id: queueJobId },\n {\n $push: {\n steps: {\n workerId,\n workerJobId,\n status: 'queued',\n } as QueueJobStep,\n },\n $set: { updatedAt: now },\n }\n );\n return;\n }\n\n if (preferRedis()) {\n const existing = await loadQueueJobRedis(queueJobId);\n if (!existing) return;\n const steps = existing.steps || [];\n steps.push({\n workerId,\n workerJobId,\n status: 'queued',\n });\n existing.steps = steps;\n existing.updatedAt = now;\n await saveQueueJobRedis(existing);\n }\n}\n\n","/**\n * Workers-config client for resolving queue URLs from the workers-config API Lambda.\n */\n\nexport interface WorkersConfig {\n version: string;\n stage: string;\n region: string;\n workers: Record<\n string,\n {\n queueUrl: string;\n region: string;\n }\n >;\n}\n\nlet cachedConfig: WorkersConfig | null = null;\nlet cacheExpiry: number = 0;\nconst CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes\n\n/**\n * Fetches the workers configuration from the workers-config API.\n * Results are cached for 5 minutes to reduce API calls.\n *\n * @param apiUrl - The URL of the workers-config API endpoint\n * @param apiKey - Optional API key for authentication (sent as x-workers-config-key header)\n * @returns The workers configuration mapping worker IDs to queue URLs\n */\nexport async function getWorkersConfig(\n apiUrl: string,\n apiKey?: string\n): Promise<WorkersConfig> {\n const now = Date.now();\n\n // Return cached config if still valid\n if (cachedConfig && now < cacheExpiry) {\n return cachedConfig;\n }\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n if (apiKey) {\n headers['x-workers-config-key'] = apiKey;\n }\n\n const response = await fetch(apiUrl, {\n method: 'GET',\n headers,\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to fetch workers config: ${response.status} ${response.statusText}`\n );\n }\n\n const config = (await response.json()) as WorkersConfig;\n cachedConfig = config;\n cacheExpiry = now + CACHE_TTL_MS;\n\n return config;\n}\n\n/**\n * Resolves the queue URL for a specific worker ID.\n * Throws an error if the worker ID is not found in the configuration.\n *\n * @param workerId - The ID of the worker\n * @param apiUrl - The URL of the workers-config API endpoint\n * @param apiKey - Optional API key for authentication\n * @returns The queue URL for the worker\n */\nexport async function resolveQueueUrl(\n workerId: string,\n apiUrl: string,\n apiKey?: string\n): Promise<string> {\n const config = await getWorkersConfig(apiUrl, apiKey);\n const worker = config.workers[workerId];\n\n if (!worker) {\n throw new Error(\n `Worker \"${workerId}\" not found in workers config. Available workers: ${Object.keys(config.workers).join(', ')}`\n );\n }\n\n return worker.queueUrl;\n}\n\n/**\n * Clears the cached workers configuration.\n * Useful for testing or when you need to force a refresh.\n */\nexport function clearWorkersConfigCache(): void {\n cachedConfig = null;\n cacheExpiry = 0;\n}\n","/**\n * Queue definition and context types for worker queues.\n *\n * These types are used at code-time by .queue.ts files and at runtime\n * by the client and generated registry/queue wrappers.\n */\n\nexport interface WorkerQueueStep {\n /** Worker ID for this step. Must match an existing worker id. */\n workerId: string;\n /**\n * Optional delay (in seconds) before this step is executed.\n * Implemented via SQS DelaySeconds (0–900).\n */\n delaySeconds?: number;\n /**\n * Optional name of a mapping function exported from the .queue.ts file\n * that derives this step's input from the previous step's output and\n * the original initial input.\n */\n mapInputFromPrev?: string;\n}\n\nexport interface WorkerQueueConfig<InitialInput = any, StepOutput = any> {\n /** Stable queue identifier, e.g. \"cost-usage\". */\n id: string;\n /** Ordered list of workers forming the queue. */\n steps: WorkerQueueStep[];\n /**\n * Optional schedule for the queue (cron or rate).\n * When set, the CLI generates a queue-starter Lambda triggered by this schedule.\n * Example: 'cron(0 3 * * ? *)' for daily at 03:00 UTC.\n */\n schedule?: string | { rate: string; enabled?: boolean; input?: Record<string, any> };\n // The generic parameters are reserved for future typing improvements and\n // are intentionally unused here (config is structural at runtime).\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _initialInputType?: InitialInput;\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _stepOutputType?: StepOutput;\n}\n\n/**\n * Queue execution context that is embedded into job input/metadata so\n * queue-aware wrappers can determine where they are in the queue.\n */\nexport interface WorkerQueueContext<InitialInput = any> {\n id: string;\n stepIndex: number;\n initialInput: InitialInput;\n /** Queue job ID (same as first worker's jobId) for tracking progress. */\n queueJobId?: string;\n}\n\n/**\n * Identity helper for defining worker queues in .queue.ts files.\n * This is primarily for type safety and CLI discovery.\n */\nexport function defineWorkerQueue<T extends WorkerQueueConfig>(config: T): T {\n return config;\n}\n\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmFO,SAAS,uBAA+B;AAC7C,QAAM,MACJ,QAAQ,IAAI,mBACZ,QAAQ,IAAI,+BACZ,QAAQ,IAAI,2BACZ,QAAQ,IAAI,uCACZ,QAAQ,IAAI,0BACZ,QAAQ,IAAI;AAEd,MAAI,CAAC,KAAK;AACR,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AAAA,EACF;AAEA,QAAM,MAAM,IAAI,IAAI,GAAG;AACvB,MAAI,SAAS;AACb,MAAI,OAAO;AAEX,QAAM,OAAO,IAAI,YAAY;AAG7B,MAAI,WAAW,KAAK,QAAQ,oCAAoC,EAAE;AAElE,QAAM,WAAW,IAAI,SAAS,QAAQ,QAAQ,EAAE;AAChD,MAAI,WAAW,GAAG,QAAQ,mBAAmB,QAAQ,QAAQ,EAAE;AAE/D,SAAO,IAAI,SAAS;AACtB;AAMA,SAAS,iBAAiB,KAA6B;AACrD,QAAM,aAAgC,CAAC;AAEvC,MAAI,IAAI,WAAW;AACjB,eAAW,YAAY,IAAI;AAAA,EAC7B;AAGA,MAAI,IAAI,YAAY,OAAO,IAAI,aAAa,UAAU;AACpD,WAAO,OAAO,YAAY,IAAI,QAAQ;AAAA,EACxC;AAGA,MAAI,IAAI,qBAAqB,OAAO,IAAI,sBAAsB,YAAY;AACxE,UAAM,SAAS,IAAI,kBAAkB;AACrC,WAAO,OAAO,YAAY,MAAM;AAAA,EAClC;AAEA,SAAO;AACT;AAaA,eAAsB,SACpB,UACA,OACA,aACA,SACA,KACyB;AAEzB,QAAM,iBAAiB,YAA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Q;AAAA,kBACR;AAAA,kBACA,UAAU,QAAQ;AAAA,gBACpB,CAAC;AAAA,cACH,CAAC;AAAA,YACH,SAAS,OAAO;AACd,sBAAQ,KAAK,uCAAuC,KAAK;AAAA,YAC3D;AAAA,UACF;AAEA,iBAAO;AAAA,YACL,WAAW,SAAS,KAAK,IAAI,CAAC;AAAA,YAC9B,QAAQ;AAAA,YACR,OAAO;AAAA,UACT;AAAA,QACF,SAAS,OAAY;AAEnB,cAAI,UAAU;AACZ,kBAAM,SAAS,OAAO;AAAA,cACpB,QAAQ;AAAA,cACR,OAAO;AAAA,gBACL,SAAS,MAAM,WAAW;AAAA,gBAC1B,OAAO,MAAM;AAAA,gBACb,MAAM,MAAM,QAAQ;AAAA,cACtB;AAAA,YACF,CAAC;AAAA,UACH;AAGA,cAAI,QAAQ,YAAY;AACtB,gBAAI;AACF,oBAAM,MAAM,QAAQ,YAAY;AAAA,gBAC9B,QAAQ;AAAA,gBACR,SAAS,EAAE,gBAAgB,mBAAmB;AAAA,gBAC9C,MAAM,KAAK,UAAU;AAAA,kBACnB,OAAO;AAAA,kBACP,UAAU;AAAA,kBACV,QAAQ;AAAA,kBACR,OAAO;AAAA,oBACL,SAAS,MAAM,WAAW;AAAA,oBAC1B,OAAO,MAAM;AAAA,oBACb,MAAM,MAAM,QAAQ;AAAA,kBACtB;AAAA,kBACA,UAAU,QAAQ;AAAA,gBACpB,CAAC;AAAA,cACH,CAAC;AAAA,YACH,SAAS,cAAc;AACrB,sBAAQ,KAAK,6CAA6C,YAAY;AAAA,YACxE;AAAA,UACF;AACA,gBAAM;AAAA,QACR;AAAA,MACF;AAGA,aAAO,SAAS,IAAI,OAAO,aAAa,OAAO;AAAA,IACjD;AAAA,EACF;AAEA,SAAO;AACT;AASO,SAAS,uBACd,OACA;AACA,SAAO,oBAAoB,MAAM,SAAS,MAAM,YAAY;AAC9D;","names":["import_redis","import_mongodb","redisUrl","redisToken","defaultTtlSeconds","redisClient","getRedis","UpstashRedis","module","explicitPath","DEFAULT_POLL_INTERVAL_MS","DEFAULT_POLL_TIMEOUT_MS","input","options","response"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/client.ts","../src/handler.ts","../src/mongoJobStore.ts","../src/redisJobStore.ts","../src/queueJobStore.ts","../src/config.ts","../src/queue.ts"],"sourcesContent":["/**\n * @microfox/ai-worker\n * Worker runtime for ai-router - SQS-based async agent execution\n */\n\nimport { dispatch, dispatchLocal, getWorkersTriggerUrl, type DispatchOptions, type DispatchResult } from './client.js';\nimport { createLambdaHandler, createWorkerLogger, type WorkerHandler, type JobStore, type DispatchWorkerOptions, SQS_MAX_DELAY_SECONDS } from './handler.js';\nimport type { ZodType, z } from 'zod';\n\nexport * from './client.js';\nexport * from './handler.js';\nexport * from './config.js';\nexport * from './queue.js';\n\n/**\n * Schedule event configuration for a worker.\n * Supports both simple rate/cron strings and full configuration objects.\n * \n * @example Simple rate/cron\n * ```typescript\n * schedule: 'rate(2 hours)'\n * // or\n * schedule: 'cron(0 12 * * ? *)'\n * ```\n * \n * @example Full configuration\n * ```typescript\n * schedule: {\n * rate: 'rate(10 minutes)',\n * enabled: true,\n * input: { key1: 'value1' }\n * }\n * ```\n * \n * @example Multiple schedules\n * ```typescript\n * schedule: [\n * 'rate(2 hours)',\n * { rate: 'cron(0 12 * * ? *)', enabled: false }\n * ]\n * ```\n */\nexport interface ScheduleEventConfig {\n /**\n * Schedule rate using either rate() or cron() syntax.\n * Can be a string or array of strings for multiple schedules.\n * \n * @example 'rate(2 hours)' or 'cron(0 12 * * ? *)'\n * @example ['cron(0 0/4 ? * MON-FRI *)', 'cron(0 2 ? * SAT-SUN *)']\n */\n rate: string | string[];\n /**\n * Whether the schedule is enabled (default: true).\n */\n enabled?: boolean;\n /**\n * Input payload to pass to the function.\n */\n input?: Record<string, any>;\n /**\n * JSONPath expression to select part of the event data as input.\n */\n inputPath?: string;\n /**\n * Input transformer configuration for custom input mapping.\n */\n inputTransformer?: {\n inputPathsMap?: Record<string, string>;\n inputTemplate?: string;\n };\n /**\n * Name of the schedule event.\n */\n name?: string;\n /**\n * Description of the schedule event.\n */\n description?: string;\n /**\n * Method to use: 'eventBus' (default) or 'scheduler'.\n * Use 'scheduler' for higher limits (1M events vs 300).\n */\n method?: 'eventBus' | 'scheduler';\n /**\n * Timezone for the schedule (only used with method: 'scheduler').\n * @example 'America/New_York'\n */\n timezone?: string;\n}\n\nexport type ScheduleConfig = \n | string \n | ScheduleEventConfig \n | (string | ScheduleEventConfig)[];\n\n/**\n * Configuration for a worker's Lambda function deployment.\n * \n * **Best Practice**: Export this as a separate const from your worker file:\n * ```typescript\n * export const workerConfig: WorkerConfig = {\n * timeout: 900,\n * memorySize: 2048,\n * layers: ['arn:aws:lambda:${aws:region}:${aws:accountId}:layer:ffmpeg:1'],\n * schedule: 'rate(2 hours)',\n * };\n * ```\n * \n * The CLI will automatically extract it from the export. You do not need to pass it to `createWorker()`.\n */\nexport interface WorkerConfig {\n /**\n * Lambda function timeout in seconds (max 900).\n */\n timeout?: number;\n /**\n * Lambda function memory size in MB (128-10240).\n */\n memorySize?: number;\n /**\n * Optional Lambda layers ARNs to attach to this worker function.\n *\n * This is primarily used by @microfox/ai-worker-cli when generating serverless.yml.\n * Supports CloudFormation pseudo-parameters like ${aws:region} and ${aws:accountId}.\n *\n * Example:\n * layers: ['arn:aws:lambda:${aws:region}:${aws:accountId}:layer:ffmpeg:1']\n */\n layers?: string[];\n /**\n * Schedule events configuration for this worker.\n * Allows multiple schedule events to be attached to the same function.\n * \n * @example Simple rate\n * ```typescript\n * schedule: 'rate(2 hours)'\n * ```\n * \n * @example Multiple schedules\n * ```typescript\n * schedule: [\n * 'rate(2 hours)',\n * { rate: 'cron(0 12 * * ? *)', enabled: true, input: { key: 'value' } }\n * ]\n * ```\n * \n * @example Using scheduler method with timezone\n * ```typescript\n * schedule: {\n * method: 'scheduler',\n * rate: 'cron(0 0/4 ? * MON-FRI *)',\n * timezone: 'America/New_York',\n * input: { key1: 'value1' }\n * }\n * ```\n */\n schedule?: ScheduleConfig;\n\n /**\n * SQS queue settings for this worker (used by @microfox/ai-worker-cli when generating serverless.yml).\n *\n * Notes:\n * - To effectively disable retries, set `maxReceiveCount: 1` (requires DLQ; the CLI will create one).\n * - SQS does not support `maxReceiveCount: 0`.\n * - `messageRetentionPeriod` is in seconds (max 1209600 = 14 days).\n */\n sqs?: {\n /**\n * How many receives before sending to DLQ.\n * Use 1 to avoid retries.\n */\n maxReceiveCount?: number;\n /**\n * How long messages are retained in the main queue (seconds).\n */\n messageRetentionPeriod?: number;\n /**\n * Visibility timeout for the main queue (seconds).\n * If not set, CLI defaults to (worker timeout + 60s).\n */\n visibilityTimeout?: number;\n /**\n * DLQ message retention period (seconds).\n * Defaults to `messageRetentionPeriod` (or 14 days).\n */\n deadLetterMessageRetentionPeriod?: number;\n };\n}\n\nexport interface WorkerAgentConfig<INPUT_SCHEMA extends ZodType<any>, OUTPUT> {\n id: string;\n inputSchema: INPUT_SCHEMA;\n outputSchema: ZodType<OUTPUT>;\n handler: WorkerHandler<z.infer<INPUT_SCHEMA>, OUTPUT>;\n /**\n * @deprecated Prefer exporting `workerConfig` as a separate const from your worker file.\n * The CLI will automatically extract it from the export. This parameter is kept for backward compatibility.\n */\n workerConfig?: WorkerConfig;\n}\n\nexport interface WorkerAgent<INPUT_SCHEMA extends ZodType<any>, OUTPUT> {\n id: string;\n dispatch: (\n input: z.input<INPUT_SCHEMA>,\n options: DispatchOptions\n ) => Promise<DispatchResult>;\n handler: WorkerHandler<z.infer<INPUT_SCHEMA>, OUTPUT>;\n inputSchema: INPUT_SCHEMA;\n outputSchema: ZodType<OUTPUT>;\n workerConfig?: WorkerConfig;\n}\n\n/**\n * Creates a worker agent that can be dispatched to SQS/Lambda.\n *\n * In development mode (NODE_ENV === 'development' and WORKERS_LOCAL_MODE !== 'false'),\n * dispatch() will run the handler immediately in the same process.\n *\n * In production, dispatch() sends a message to SQS which triggers a Lambda function.\n *\n * @template INPUT_SCHEMA - The Zod schema type (e.g., `typeof InputSchema`).\n * Used to derive both:\n * - Pre-parse input type via `z.input<INPUT_SCHEMA>` for `dispatch()` (preserves optional fields)\n * - Parsed input type via `z.infer<INPUT_SCHEMA>` for handler (defaults applied)\n * @template OUTPUT - The output type returned by the handler. Use `z.infer<typeof OutputSchema>`.\n *\n * @param config - Worker agent configuration\n * @returns A worker agent object with a dispatch method\n *\n * @example\n * ```typescript\n * const InputSchema = z.object({\n * url: z.string().url(),\n * timeout: z.number().optional().default(5000), // optional with default\n * });\n *\n * export const worker = createWorker<typeof InputSchema, Output>({\n * // dispatch() accepts { url: string, timeout?: number } (pre-parse, optional preserved)\n * // handler receives { url: string, timeout: number } (parsed, default applied)\n * });\n * ```\n */\nexport function createWorker<INPUT_SCHEMA extends ZodType<any>, OUTPUT>(\n config: WorkerAgentConfig<INPUT_SCHEMA, OUTPUT>\n): WorkerAgent<INPUT_SCHEMA, OUTPUT> {\n const { id, inputSchema, outputSchema, handler } = config;\n\n const agent: WorkerAgent<INPUT_SCHEMA, OUTPUT> = {\n id,\n handler,\n inputSchema,\n outputSchema,\n\n async dispatch(input: z.input<INPUT_SCHEMA>, options: DispatchOptions): Promise<DispatchResult> {\n const mode = options.mode ?? 'auto';\n const envWantsLocal =\n process.env.NODE_ENV === 'development' &&\n process.env.WORKERS_LOCAL_MODE !== 'false';\n // Check if we're in local development mode\n const isLocal = mode === 'local' || (mode === 'auto' && envWantsLocal);\n\n if (isLocal) {\n // Local mode: run handler immediately\n // Parse input to apply defaults and get the final parsed type\n const parsedInput = inputSchema.parse(input);\n const localJobId = options.jobId || `local-${Date.now()}`;\n \n // Try to get direct job store access in local mode (same process as Next.js app)\n // This allows direct DB updates without needing HTTP/webhook URLs\n let directJobStore: {\n updateJob: (jobId: string, data: any) => Promise<void>;\n setJob?: (jobId: string, data: any) => Promise<void>;\n } | null = null;\n\n // Path constants for job store imports\n const nextJsPathAlias = '@/app/api/workflows/stores/jobStore';\n const explicitPath = process.env.WORKER_JOB_STORE_MODULE_PATH;\n\n // Reliable approach: try Next.js path alias first, then explicit env var\n // The @/ alias works at runtime in Next.js context\n const resolveJobStore = async () => {\n // Option 1: Try Next.js path alias (works in Next.js runtime context)\n try {\n const module = await import(nextJsPathAlias);\n if (module?.updateJob) {\n return { updateJob: module.updateJob, setJob: module.setJob };\n }\n } catch {\n // Path alias not available (not in Next.js context or alias not configured)\n }\n\n // Option 2: Use explicit env var if provided (for custom setups)\n if (explicitPath) {\n try {\n const module = await import(explicitPath).catch(() => {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n return require(explicitPath);\n });\n if (module?.updateJob) {\n return { updateJob: module.updateJob, setJob: module.setJob };\n }\n } catch {\n // Explicit path failed\n }\n }\n\n return null;\n };\n\n directJobStore = await resolveJobStore();\n if (directJobStore) {\n console.log('[Worker] Using direct job store in local mode (no HTTP needed)');\n }\n\n // Derive job store URL from webhook URL or environment (fallback for HTTP mode)\n let jobStoreUrl: string | undefined;\n if (options.webhookUrl) {\n try {\n const webhookUrlObj = new URL(options.webhookUrl);\n jobStoreUrl = webhookUrlObj.pathname.replace(/\\/webhook$/, '');\n jobStoreUrl = `${webhookUrlObj.origin}${jobStoreUrl}`;\n } catch {\n // Invalid URL, skip job store URL\n }\n }\n jobStoreUrl = jobStoreUrl || process.env.WORKER_JOB_STORE_URL;\n\n // Create job store interface for local mode\n // Prefer direct DB access, fallback to HTTP calls if needed\n const createLocalJobStore = (\n directStore: typeof directJobStore,\n httpUrl?: string\n ): JobStore | undefined => {\n // If we have direct job store access, use it (no HTTP needed)\n if (directStore) {\n return {\n update: async (update) => {\n try {\n // Build update payload\n const updatePayload: any = {};\n \n if (update.status !== undefined) {\n updatePayload.status = update.status;\n }\n if (update.metadata !== undefined) {\n updatePayload.metadata = update.metadata;\n }\n if (update.progress !== undefined) {\n // Merge progress into metadata\n updatePayload.metadata = {\n ...updatePayload.metadata,\n progress: update.progress,\n progressMessage: update.progressMessage,\n };\n }\n if (update.output !== undefined) {\n updatePayload.output = update.output;\n }\n if (update.error !== undefined) {\n updatePayload.error = update.error;\n }\n\n await directStore.updateJob(localJobId, updatePayload);\n console.log('[Worker] Local job updated (direct DB):', {\n jobId: localJobId,\n workerId: id,\n updates: Object.keys(updatePayload),\n });\n } catch (error: any) {\n console.warn('[Worker] Failed to update local job (direct DB):', {\n jobId: localJobId,\n workerId: id,\n error: error?.message || String(error),\n });\n }\n },\n get: async () => {\n try {\n // Use the same direct store that has updateJob - it should also have getJob\n if (directStore) {\n // Try to import getJob from the same module\n const nextJsPath = '@/app/api/workflows/stores/jobStore';\n const explicitPath = process.env.WORKER_JOB_STORE_MODULE_PATH;\n \n for (const importPath of [nextJsPath, explicitPath].filter(Boolean)) {\n try {\n const module = await import(importPath!);\n if (module?.getJob) {\n return await module.getJob(localJobId);\n }\n } catch {\n // Continue\n }\n }\n }\n return null;\n } catch (error: any) {\n console.warn('[Worker] Failed to get local job (direct DB):', {\n jobId: localJobId,\n workerId: id,\n error: error?.message || String(error),\n });\n return null;\n }\n },\n appendInternalJob: async (entry: { jobId: string; workerId: string }) => {\n try {\n const nextJsPath = '@/app/api/workflows/stores/jobStore';\n const explicitPath = process.env.WORKER_JOB_STORE_MODULE_PATH;\n for (const importPath of [nextJsPath, explicitPath].filter(Boolean)) {\n try {\n const module = await import(importPath!);\n if (typeof module?.appendInternalJob === 'function') {\n await module.appendInternalJob(localJobId, entry);\n return;\n }\n } catch {\n // Continue\n }\n }\n } catch (error: any) {\n console.warn('[Worker] Failed to appendInternalJob (direct DB):', { localJobId, error: error?.message || String(error) });\n }\n },\n getJob: async (otherJobId: string) => {\n try {\n const nextJsPath = '@/app/api/workflows/stores/jobStore';\n const explicitPath = process.env.WORKER_JOB_STORE_MODULE_PATH;\n for (const importPath of [nextJsPath, explicitPath].filter(Boolean)) {\n try {\n const module = await import(importPath!);\n if (typeof module?.getJob === 'function') {\n return await module.getJob(otherJobId);\n }\n } catch {\n // Continue\n }\n }\n } catch (error: any) {\n console.warn('[Worker] Failed to getJob (direct DB):', { otherJobId, error: error?.message || String(error) });\n }\n return null;\n },\n };\n }\n\n // Fallback to HTTP calls if no direct access\n if (!httpUrl) {\n return undefined;\n }\n\n // Use HTTP calls to update job store\n return {\n update: async (update) => {\n try {\n // Build update payload\n const updatePayload: any = { jobId: localJobId, workerId: id };\n \n if (update.status !== undefined) {\n updatePayload.status = update.status;\n }\n if (update.metadata !== undefined) {\n updatePayload.metadata = update.metadata;\n }\n if (update.progress !== undefined) {\n // Merge progress into metadata\n updatePayload.metadata = {\n ...updatePayload.metadata,\n progress: update.progress,\n progressMessage: update.progressMessage,\n };\n }\n if (update.output !== undefined) {\n updatePayload.output = update.output;\n }\n if (update.error !== undefined) {\n updatePayload.error = update.error;\n }\n\n const response = await fetch(`${httpUrl}/update`, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify(updatePayload),\n });\n if (!response.ok) {\n throw new Error(`Job store update failed: ${response.status} ${response.statusText}`);\n }\n console.log('[Worker] Local job updated (HTTP):', {\n jobId: localJobId,\n workerId: id,\n updates: Object.keys(updatePayload),\n });\n } catch (error: any) {\n console.warn('[Worker] Failed to update local job (HTTP):', {\n jobId: localJobId,\n workerId: id,\n error: error?.message || String(error),\n });\n }\n },\n get: async () => {\n try {\n // GET /api/workflows/workers/:workerId/:jobId\n const response = await fetch(`${httpUrl}/${id}/${localJobId}`, {\n method: 'GET',\n headers: { 'Content-Type': 'application/json' },\n });\n\n if (!response.ok) {\n if (response.status === 404) {\n return null;\n }\n throw new Error(`Job store get failed: ${response.status} ${response.statusText}`);\n }\n\n return await response.json();\n } catch (error: any) {\n console.warn('[Worker] Failed to get local job (HTTP):', {\n jobId: localJobId,\n workerId: id,\n error: error?.message || String(error),\n });\n return null;\n }\n },\n };\n };\n\n const jobStore = createLocalJobStore(directJobStore, jobStoreUrl);\n\n const DEFAULT_POLL_INTERVAL_MS = 2000;\n const DEFAULT_POLL_TIMEOUT_MS = 15 * 60 * 1000;\n\n const createLocalDispatchWorker = (\n parentJobId: string,\n parentWorkerId: string,\n parentContext: Record<string, any>,\n store: JobStore | undefined\n ): ((\n workerId: string,\n input: unknown,\n options?: DispatchWorkerOptions\n ) => Promise<{ jobId: string; messageId?: string; output?: unknown }>) => {\n return async (\n calleeWorkerId: string,\n input: unknown,\n options?: DispatchWorkerOptions\n ): Promise<{ jobId: string; messageId?: string; output?: unknown }> => {\n const childJobId =\n options?.jobId ||\n `job-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;\n const metadata = options?.metadata ?? {};\n const serializedContext: Record<string, any> = {};\n if (parentContext.requestId) serializedContext.requestId = parentContext.requestId;\n const messageBody = {\n workerId: calleeWorkerId,\n jobId: childJobId,\n input: input ?? {},\n context: serializedContext,\n webhookUrl: options?.webhookUrl,\n metadata,\n timestamp: new Date().toISOString(),\n };\n let triggerUrl: string;\n try {\n triggerUrl = getWorkersTriggerUrl();\n } catch (e: any) {\n throw new Error(\n `Local dispatchWorker requires WORKER_BASE_URL (or similar) for worker \"${calleeWorkerId}\": ${e?.message ?? e}`\n );\n }\n const headers: Record<string, string> = { 'Content-Type': 'application/json' };\n const triggerKey = process.env.WORKERS_TRIGGER_API_KEY;\n if (triggerKey) headers['x-workers-trigger-key'] = triggerKey;\n\n // Fire-and-forget with delay: schedule trigger after delay, return immediately (no computation/wait in caller).\n if (options?.await !== true && options?.delaySeconds != null && options.delaySeconds > 0) {\n const sec = Math.min(SQS_MAX_DELAY_SECONDS, Math.max(0, Math.floor(options.delaySeconds)));\n const storeRef = store;\n setTimeout(() => {\n fetch(triggerUrl, {\n method: 'POST',\n headers,\n body: JSON.stringify({ workerId: calleeWorkerId, body: messageBody }),\n })\n .then(async (response) => {\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n console.error(\n `[Worker] Delayed trigger failed for \"${calleeWorkerId}\": ${response.status} ${response.statusText}${text ? ` - ${text}` : ''}`\n );\n return;\n }\n if (storeRef?.appendInternalJob) {\n await storeRef.appendInternalJob({ jobId: childJobId, workerId: calleeWorkerId });\n }\n })\n .catch((err) => {\n console.error('[Worker] Delayed trigger error:', { calleeWorkerId, jobId: childJobId, error: err?.message ?? err });\n });\n }, sec * 1000);\n return { jobId: childJobId, messageId: undefined };\n }\n\n const response = await fetch(triggerUrl, {\n method: 'POST',\n headers,\n body: JSON.stringify({ workerId: calleeWorkerId, body: messageBody }),\n });\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n throw new Error(\n `Failed to trigger worker \"${calleeWorkerId}\": ${response.status} ${response.statusText}${text ? ` - ${text}` : ''}`\n );\n }\n const data = (await response.json().catch(() => ({}))) as any;\n const messageId = data?.messageId ? String(data.messageId) : `trigger-${childJobId}`;\n\n if (store?.appendInternalJob) {\n await store.appendInternalJob({ jobId: childJobId, workerId: calleeWorkerId });\n }\n\n if (options?.await && store?.getJob) {\n const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;\n const pollTimeoutMs = options.pollTimeoutMs ?? DEFAULT_POLL_TIMEOUT_MS;\n const deadline = Date.now() + pollTimeoutMs;\n while (Date.now() < deadline) {\n const child = await store.getJob(childJobId);\n if (!child) {\n await new Promise((r) => setTimeout(r, pollIntervalMs));\n continue;\n }\n if (child.status === 'completed') {\n return { jobId: childJobId, messageId, output: child.output };\n }\n if (child.status === 'failed') {\n const err = child.error;\n throw new Error(err?.message ?? `Child worker ${calleeWorkerId} failed`);\n }\n await new Promise((r) => setTimeout(r, pollIntervalMs));\n }\n throw new Error(\n `Child worker ${calleeWorkerId} (${childJobId}) did not complete within ${pollTimeoutMs}ms`\n );\n }\n\n return { jobId: childJobId, messageId };\n };\n };\n\n // Create initial job record if we have job store access\n if (directJobStore?.setJob) {\n try {\n await directJobStore.setJob(localJobId, {\n jobId: localJobId,\n workerId: id,\n status: 'queued',\n input: parsedInput,\n metadata: options.metadata || {},\n });\n } catch (error: any) {\n console.warn('[Worker] Failed to create initial job record:', {\n jobId: localJobId,\n workerId: id,\n error: error?.message || String(error),\n });\n // Continue - job will still be created when status is updated\n }\n }\n\n const baseContext = { jobId: localJobId, workerId: id };\n const handlerContext = {\n ...baseContext,\n ...(jobStore ? { jobStore } : {}),\n logger: createWorkerLogger(localJobId, id),\n dispatchWorker: createLocalDispatchWorker(\n localJobId,\n id,\n baseContext,\n jobStore\n ),\n };\n\n try {\n // Update status to running before execution\n if (jobStore) {\n await jobStore.update({ status: 'running' });\n }\n\n const output = await dispatchLocal(handler, parsedInput, handlerContext);\n\n // Update status to completed before webhook\n if (jobStore) {\n await jobStore.update({ status: 'completed', output });\n }\n\n // Only send webhook if webhookUrl is provided\n if (options.webhookUrl) {\n try {\n await fetch(options.webhookUrl, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n jobId: localJobId,\n workerId: id,\n status: 'success',\n output,\n metadata: options.metadata,\n }),\n });\n } catch (error) {\n console.warn('[Worker] Local webhook call failed:', error);\n }\n }\n\n return {\n messageId: `local-${Date.now()}`,\n status: 'queued',\n jobId: localJobId,\n };\n } catch (error: any) {\n // Update status to failed before webhook\n if (jobStore) {\n await jobStore.update({\n status: 'failed',\n error: {\n message: error.message || 'Unknown error',\n stack: error.stack,\n name: error.name || 'Error',\n },\n });\n }\n\n // Only send error webhook if webhookUrl is provided\n if (options.webhookUrl) {\n try {\n await fetch(options.webhookUrl, {\n method: 'POST',\n headers: { 'Content-Type': 'application/json' },\n body: JSON.stringify({\n jobId: localJobId,\n workerId: id,\n status: 'error',\n error: {\n message: error.message || 'Unknown error',\n stack: error.stack,\n name: error.name || 'Error',\n },\n metadata: options.metadata,\n }),\n });\n } catch (webhookError) {\n console.warn('[Worker] Local error webhook call failed:', webhookError);\n }\n }\n throw error;\n }\n }\n\n // Production mode: dispatch to SQS\n return dispatch(id, input, inputSchema, options);\n },\n };\n\n return agent;\n}\n\n/**\n * Creates a Lambda handler entrypoint for a worker agent.\n * This is used by the deployment script to generate Lambda entrypoints.\n *\n * @param agent - The worker agent\n * @returns A Lambda handler function\n */\nexport function createLambdaEntrypoint<INPUT_SCHEMA extends ZodType<any>, OUTPUT>(\n agent: WorkerAgent<INPUT_SCHEMA, OUTPUT>\n) {\n return createLambdaHandler(agent.handler, agent.outputSchema);\n}\n","/**\n * Client for dispatching background worker jobs.\n *\n * In production, dispatching happens via the workers HTTP API:\n * POST /workers/trigger -> enqueues message to SQS on the workers service side\n *\n * This avoids requiring AWS credentials in your Next.js app.\n */\n\nimport type { ZodType, z } from 'zod';\nimport type { WorkerQueueConfig } from './queue.js';\n\nexport interface WorkerQueueRegistry {\n getQueueById(queueId: string): WorkerQueueConfig | undefined;\n /** (initialInput, previousOutputs) for best DX: derive next input from original request and all prior step outputs. */\n invokeMapInput?: (\n queueId: string,\n stepIndex: number,\n initialInput: unknown,\n previousOutputs: Array<{ stepIndex: number; workerId: string; output: unknown }>\n ) => Promise<unknown> | unknown;\n}\n\nexport interface DispatchOptions {\n /**\n * Optional webhook callback URL to notify when the job finishes.\n * Only called when provided. Default: no webhook (use job store / MongoDB only).\n */\n webhookUrl?: string;\n /**\n * Controls how dispatch executes.\n * - \"auto\" (default): local inline execution in development unless WORKERS_LOCAL_MODE=false.\n * - \"local\": force inline execution (no SQS).\n * - \"remote\": force SQS/Lambda dispatch even in development.\n */\n mode?: 'auto' | 'local' | 'remote';\n jobId?: string;\n metadata?: Record<string, any>;\n /**\n * In-memory queue registry for dispatchQueue. Required when using dispatchQueue.\n * Pass a registry that imports from your .queue.ts definitions (works on Vercel/serverless).\n */\n registry?: WorkerQueueRegistry;\n /**\n * Optional callback to create a queue job record before dispatching.\n * Called with queueJobId (= first worker's jobId), queueId, and firstStep.\n */\n onCreateQueueJob?: (params: {\n queueJobId: string;\n queueId: string;\n firstStep: { workerId: string; workerJobId: string };\n metadata?: Record<string, unknown>;\n }) => Promise<void>;\n}\n\nexport interface DispatchResult {\n messageId: string;\n status: 'queued';\n jobId: string;\n}\n\nexport interface DispatchQueueResult extends DispatchResult {\n queueId: string;\n}\n\nexport interface SerializedContext {\n requestId?: string;\n userId?: string;\n traceId?: string;\n [key: string]: any;\n}\n\n/**\n * Derives the full /workers/trigger URL from env.\n * Exported for use by local dispatchWorker (worker-to-worker in dev).\n * Server-side only; clients should use useWorkflowJob with your app's /api/workflows routes.\n *\n * Env vars:\n * - WORKER_BASE_URL: base URL of the workers service (e.g. https://.../prod)\n * - WORKERS_TRIGGER_API_URL / WORKERS_CONFIG_API_URL: legacy, still supported\n */\nexport function getWorkersTriggerUrl(): string {\n const raw =\n process.env.WORKER_BASE_URL ||\n process.env.WORKERS_TRIGGER_API_URL ||\n process.env.WORKERS_CONFIG_API_URL;\n\n if (!raw) {\n throw new Error(\n 'WORKER_BASE_URL is required for background workers. Set it server-side only.'\n );\n }\n\n const url = new URL(raw);\n url.search = '';\n url.hash = '';\n\n const path = url.pathname || '';\n\n // If the user pointed at a specific endpoint, normalize back to the service root.\n url.pathname = path.replace(/\\/?workers\\/(trigger|config)\\/?$/, '');\n\n const basePath = url.pathname.replace(/\\/+$/, '');\n url.pathname = `${basePath}/workers/trigger`.replace(/\\/+$/, '');\n\n return url.toString();\n}\n\n/**\n * URL for the queue start endpoint (dispatch proxy). Use this so queue starts\n * go through the queue handler Lambda for easier debugging (one log stream per queue).\n */\nexport function getQueueStartUrl(queueId: string): string {\n const raw =\n process.env.WORKER_BASE_URL ||\n process.env.WORKERS_TRIGGER_API_URL ||\n process.env.WORKERS_CONFIG_API_URL;\n\n if (!raw) {\n throw new Error(\n 'WORKER_BASE_URL is required for background workers. Set it server-side only.'\n );\n }\n\n const url = new URL(raw);\n url.search = '';\n url.hash = '';\n\n const path = url.pathname || '';\n url.pathname = path.replace(/\\/?workers\\/(trigger|config)\\/?$/, '');\n const basePath = url.pathname.replace(/\\/+$/, '');\n const safeSegment = encodeURIComponent(queueId);\n url.pathname = `${basePath}/queues/${safeSegment}/start`.replace(/\\/+$/, '');\n\n return url.toString();\n}\n\n/**\n * Serializes context data for transmission to Lambda.\n * Only serializes safe, JSON-compatible properties.\n */\nfunction serializeContext(ctx: any): SerializedContext {\n const serialized: SerializedContext = {};\n\n if (ctx.requestId) {\n serialized.requestId = ctx.requestId;\n }\n\n // Extract any additional serializable metadata\n if (ctx.metadata && typeof ctx.metadata === 'object') {\n Object.assign(serialized, ctx.metadata);\n }\n\n // Allow custom context serialization via a helper property\n if (ctx._serializeContext && typeof ctx._serializeContext === 'function') {\n const custom = ctx._serializeContext();\n Object.assign(serialized, custom);\n }\n\n return serialized;\n}\n\n\n/**\n * Dispatches a background worker job to SQS.\n *\n * @param workerId - The ID of the worker to dispatch\n * @param input - The input data for the worker (will be validated against inputSchema)\n * @param inputSchema - Zod schema for input validation\n * @param options - Dispatch options including webhook URL\n * @param ctx - Optional context object (only serializable parts will be sent)\n * @returns Promise resolving to dispatch result with messageId and jobId\n */\nexport async function dispatch<INPUT_SCHEMA extends ZodType<any>>(\n workerId: string,\n input: z.input<INPUT_SCHEMA>,\n inputSchema: INPUT_SCHEMA,\n options: DispatchOptions,\n ctx?: any\n): Promise<DispatchResult> {\n // Validate input against schema\n const validatedInput = inputSchema.parse(input);\n\n // Generate job ID if not provided\n const jobId =\n options.jobId || `job-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;\n\n // Resolve /workers/trigger endpoint URL\n const triggerUrl = getWorkersTriggerUrl();\n\n // Serialize context (only safe, JSON-compatible parts)\n const serializedContext = ctx ? serializeContext(ctx) : {};\n\n // Job updates use MongoDB only; never pass jobStoreUrl/origin URL.\n const messageBody = {\n workerId,\n jobId,\n input: validatedInput,\n context: serializedContext,\n webhookUrl: options.webhookUrl,\n metadata: options.metadata || {},\n timestamp: new Date().toISOString(),\n };\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n const triggerKey = process.env.WORKERS_TRIGGER_API_KEY;\n if (triggerKey) {\n headers['x-workers-trigger-key'] = triggerKey;\n }\n\n const response = await fetch(triggerUrl, {\n method: 'POST',\n headers,\n body: JSON.stringify({\n workerId,\n body: messageBody,\n }),\n });\n\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n throw new Error(\n `Failed to trigger worker \"${workerId}\": ${response.status} ${response.statusText}${text ? ` - ${text}` : ''}`\n );\n }\n\n const data = (await response.json().catch(() => ({}))) as any;\n const messageId = data?.messageId ? String(data.messageId) : `trigger-${jobId}`;\n\n return {\n messageId,\n status: 'queued',\n jobId,\n };\n}\n\n/**\n * Dispatch a worker by ID without importing the worker module.\n * Sends to the workers trigger API (WORKER_BASE_URL). No input schema validation at call site.\n *\n * @param workerId - The worker ID (e.g. 'echo', 'data-processor')\n * @param input - Input payload (object or undefined)\n * @param options - Optional jobId, webhookUrl, metadata\n * @param ctx - Optional context (serializable parts sent in the request)\n * @returns Promise resolving to { messageId, status: 'queued', jobId }\n */\nexport async function dispatchWorker(\n workerId: string,\n input?: Record<string, unknown>,\n options: DispatchOptions = {},\n ctx?: any\n): Promise<DispatchResult> {\n const jobId =\n options.jobId || `job-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;\n const triggerUrl = getWorkersTriggerUrl();\n const serializedContext = ctx ? serializeContext(ctx) : {};\n const messageBody = {\n workerId,\n jobId,\n input: input ?? {},\n context: serializedContext,\n webhookUrl: options.webhookUrl,\n metadata: options.metadata || {},\n timestamp: new Date().toISOString(),\n };\n const headers: Record<string, string> = { 'Content-Type': 'application/json' };\n const triggerKey = process.env.WORKERS_TRIGGER_API_KEY;\n if (triggerKey) headers['x-workers-trigger-key'] = triggerKey;\n const response = await fetch(triggerUrl, {\n method: 'POST',\n headers,\n body: JSON.stringify({ workerId, body: messageBody }),\n });\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n throw new Error(\n `Failed to trigger worker \"${workerId}\": ${response.status} ${response.statusText}${text ? ` - ${text}` : ''}`\n );\n }\n const data = (await response.json().catch(() => ({}))) as any;\n const messageId = data?.messageId ? String(data.messageId) : `trigger-${jobId}`;\n return { messageId, status: 'queued', jobId };\n}\n\n/**\n * Local development mode: runs the handler immediately in the same process.\n * This bypasses SQS and Lambda for faster iteration during development.\n *\n * @param handler - The worker handler function\n * @param input - The input data\n * @param ctx - The context object\n * @returns The handler result\n */\nexport async function dispatchLocal<INPUT, OUTPUT>(\n handler: (params: { input: INPUT; ctx: any }) => Promise<OUTPUT>,\n input: INPUT,\n ctx?: any\n): Promise<OUTPUT> {\n return handler({ input, ctx: ctx || {} });\n}\n\n/**\n * Dispatches a queue by ID. POSTs to the queue-start API; the queue-start handler creates the queue job.\n * Pass the first worker's input directly (no registry required).\n */\nexport async function dispatchQueue<InitialInput = any>(\n queueId: string,\n initialInput?: InitialInput,\n options: DispatchOptions = {},\n _ctx?: any\n): Promise<DispatchQueueResult> {\n const jobId =\n options.jobId || `job-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;\n const queueStartUrl = getQueueStartUrl(queueId);\n const normalizedInput =\n initialInput !== null && typeof initialInput === 'object'\n ? (initialInput as Record<string, unknown>)\n : { value: initialInput };\n const headers: Record<string, string> = { 'Content-Type': 'application/json' };\n const triggerKey = process.env.WORKERS_TRIGGER_API_KEY;\n if (triggerKey) headers['x-workers-trigger-key'] = triggerKey;\n const response = await fetch(queueStartUrl, {\n method: 'POST',\n headers,\n body: JSON.stringify({\n input: normalizedInput,\n initialInput: normalizedInput,\n metadata: options.metadata ?? {},\n jobId,\n ...(options.webhookUrl ? { webhookUrl: options.webhookUrl } : {}),\n }),\n });\n if (!response.ok) {\n const text = await response.text().catch(() => '');\n throw new Error(\n `Failed to start queue \"${queueId}\": ${response.status} ${response.statusText}${text ? ` - ${text}` : ''}`\n );\n }\n const data = (await response.json().catch(() => ({}))) as any;\n const messageId = data?.messageId ?? data?.jobId ?? `queue-${jobId}`;\n return { queueId, messageId, status: 'queued', jobId };\n}\n\n","/**\n * Generic Lambda handler wrapper for worker agents.\n * Handles SQS events, executes user handlers, and sends webhook callbacks.\n * Job store: MongoDB only. Never uses HTTP/origin URL for job updates.\n */\n\nimport type { SQSEvent, SQSRecord, Context as LambdaContext } from 'aws-lambda';\nimport type { ZodType } from 'zod';\nimport { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs';\nimport {\n createMongoJobStore,\n upsertJob,\n isMongoJobStoreConfigured,\n getJobById as getMongoJobById,\n} from './mongoJobStore';\nimport {\n createRedisJobStore,\n upsertRedisJob,\n isRedisJobStoreConfigured,\n loadJob as loadRedisJob,\n} from './redisJobStore';\nimport {\n appendQueueJobStepInStore,\n updateQueueJobStepInStore,\n upsertInitialQueueJob,\n getQueueJob,\n} from './queueJobStore';\n\nexport interface JobStoreUpdate {\n status?: 'queued' | 'running' | 'completed' | 'failed';\n metadata?: Record<string, any>;\n progress?: number;\n progressMessage?: string;\n output?: any;\n error?: {\n message: string;\n stack?: string;\n name?: string;\n };\n}\n\nexport interface JobRecord {\n jobId: string;\n workerId: string;\n status: 'queued' | 'running' | 'completed' | 'failed';\n input: any;\n output?: any;\n error?: { message: string; stack?: string };\n metadata?: Record<string, any>;\n internalJobs?: Array<{ jobId: string; workerId: string }>;\n createdAt: string;\n updatedAt: string;\n completedAt?: string;\n}\n\nexport interface JobStore {\n /**\n * Update job in job store.\n * @param update - Update object with status, metadata, progress, output, or error\n */\n update(update: JobStoreUpdate): Promise<void>;\n /**\n * Get current job record from job store.\n * @returns Job record or null if not found\n */\n get(): Promise<JobRecord | null>;\n /**\n * Append an internal (child) job to the current job's internalJobs list.\n * Used when this worker dispatches another worker (fire-and-forget or await).\n */\n appendInternalJob?(entry: { jobId: string; workerId: string }): Promise<void>;\n /**\n * Get any job by jobId (e.g. to poll child job status when await: true).\n * @returns Job record or null if not found\n */\n getJob?(jobId: string): Promise<JobRecord | null>;\n}\n\n/** Max SQS delay in seconds (AWS limit). */\nexport const SQS_MAX_DELAY_SECONDS = 900;\n\n/** Options for ctx.dispatchWorker (worker-to-worker). */\nexport interface DispatchWorkerOptions {\n webhookUrl?: string;\n metadata?: Record<string, any>;\n /** Optional job ID for the child job (default: generated). */\n jobId?: string;\n /** If true, poll job store until child completes or fails; otherwise fire-and-forget. */\n await?: boolean;\n pollIntervalMs?: number;\n pollTimeoutMs?: number;\n /**\n * Delay before the child is invoked (fire-and-forget only; ignored when await is true).\n * Uses SQS DelaySeconds (0–900). In local mode, waits this many seconds before sending the trigger request.\n */\n delaySeconds?: number;\n}\n\n/**\n * Logger provided on ctx with prefixed levels: [INFO], [WARN], [ERROR], [DEBUG].\n * Each method accepts a message and optional data (logged as JSON).\n */\nexport interface WorkerLogger {\n info(message: string, data?: Record<string, unknown>): void;\n warn(message: string, data?: Record<string, unknown>): void;\n error(message: string, data?: Record<string, unknown>): void;\n debug(message: string, data?: Record<string, unknown>): void;\n}\n\nexport function createWorkerLogger(jobId: string, workerId: string): WorkerLogger {\n const prefix = (level: string) => `[${level}] [${workerId}] [${jobId}]`;\n return {\n info(msg: string, data?: Record<string, unknown>) {\n console.log(prefix('INFO'), msg, data !== undefined ? JSON.stringify(data) : '');\n },\n warn(msg: string, data?: Record<string, unknown>) {\n console.warn(prefix('WARN'), msg, data !== undefined ? JSON.stringify(data) : '');\n },\n error(msg: string, data?: Record<string, unknown>) {\n console.error(prefix('ERROR'), msg, data !== undefined ? JSON.stringify(data) : '');\n },\n debug(msg: string, data?: Record<string, unknown>) {\n if (process.env.DEBUG || process.env.WORKER_DEBUG) {\n console.debug(prefix('DEBUG'), msg, data !== undefined ? JSON.stringify(data) : '');\n }\n },\n };\n}\n\nexport interface WorkerHandlerParams<INPUT, OUTPUT> {\n input: INPUT;\n ctx: {\n jobId: string;\n workerId: string;\n requestId?: string;\n /**\n * Job store interface for updating and retrieving job state.\n * Uses MongoDB directly when configured; never HTTP/origin URL.\n */\n jobStore?: JobStore;\n /**\n * Logger with prefixed levels: ctx.logger.info(), .warn(), .error(), .debug().\n */\n logger: WorkerLogger;\n /**\n * Dispatch another worker (fire-and-forget or await). Uses WORKER_QUEUE_URL_<SANITIZED_ID> env.\n * Always provided by the runtime (Lambda and local).\n */\n dispatchWorker: (\n workerId: string,\n input: unknown,\n options?: DispatchWorkerOptions\n ) => Promise<{ jobId: string; messageId?: string; output?: unknown }>;\n [key: string]: any;\n };\n}\n\nexport type WorkerHandler<INPUT, OUTPUT> = (\n params: WorkerHandlerParams<INPUT, OUTPUT>\n) => Promise<OUTPUT>;\n\n/** Result of getNextStep for queue chaining. */\nexport interface QueueNextStep {\n workerId: string;\n delaySeconds?: number;\n mapInputFromPrev?: string;\n}\n\n/** One previous step's output (for mapInputFromPrev context). */\nexport interface QueueStepOutput {\n stepIndex: number;\n workerId: string;\n output: unknown;\n}\n\n/** Runtime helpers for queue-aware wrappers (provided by generated registry). */\nexport interface QueueRuntime {\n getNextStep(queueId: string, stepIndex: number): QueueNextStep | undefined;\n /** Optional: when provided, mapping can use outputs from any previous step. */\n getQueueJob?(queueJobId: string): Promise<{ steps: Array<{ workerId: string; output?: unknown }> } | null>;\n /** (initialInput, previousOutputs) – previousOutputs includes outputs for steps 0..stepIndex-1 and current step. */\n invokeMapInput?(\n queueId: string,\n stepIndex: number,\n initialInput: unknown,\n previousOutputs: QueueStepOutput[]\n ): Promise<unknown> | unknown;\n}\n\nconst WORKER_QUEUE_KEY = '__workerQueue';\nasync function notifyQueueJobStep(\n queueJobId: string,\n action: 'start' | 'complete' | 'fail' | 'append',\n params: {\n stepIndex?: number;\n workerJobId: string;\n workerId?: string;\n output?: unknown;\n error?: { message: string };\n input?: unknown;\n queueId?: string;\n }\n): Promise<void> {\n try {\n if (action === 'append') {\n if (!params.workerId || !params.workerJobId) return;\n await appendQueueJobStepInStore({\n queueJobId,\n workerId: params.workerId,\n workerJobId: params.workerJobId,\n });\n if (process.env.DEBUG_WORKER_QUEUES === '1') {\n console.log('[Worker] Queue job step appended', {\n queueJobId,\n workerId: params.workerId,\n workerJobId: params.workerJobId,\n });\n }\n return;\n }\n\n if (params.stepIndex === undefined) return;\n\n const status =\n action === 'start'\n ? 'running'\n : action === 'complete'\n ? 'completed'\n : action === 'fail'\n ? 'failed'\n : undefined;\n if (!status) return;\n\n await updateQueueJobStepInStore({\n queueJobId,\n stepIndex: params.stepIndex,\n workerId: params.workerId || '',\n workerJobId: params.workerJobId,\n status,\n input: params.input,\n output: params.output,\n error: params.error,\n });\n // Always log queue step updates so logs show which queue and step ran\n console.log('[Worker] Queue job step updated', {\n queueId: params.queueId ?? queueJobId,\n queueJobId,\n stepIndex: params.stepIndex,\n workerId: params.workerId,\n status,\n });\n } catch (err: any) {\n console.warn('[Worker] Queue job update error:', {\n queueJobId,\n action,\n error: err?.message ?? String(err),\n });\n }\n}\n\n/**\n * Wraps a user handler so that when the job has __workerQueue context (from\n * dispatchQueue or queue cron), it dispatches the next worker in the sequence\n * after the handler completes. Uses literal worker IDs so the CLI env injection\n * picks up WORKER_QUEUE_URL_* for next-step workers.\n */\nexport function wrapHandlerForQueue<INPUT, OUTPUT>(\n handler: WorkerHandler<INPUT, OUTPUT>,\n queueRuntime: QueueRuntime\n): WorkerHandler<INPUT & { __workerQueue?: { id: string; stepIndex: number; initialInput: unknown; queueJobId?: string } }, OUTPUT> {\n return async (params) => {\n const queueContext = (params.input as any)?.[WORKER_QUEUE_KEY];\n const output = await handler(params);\n\n if (!queueContext || typeof queueContext !== 'object' || !queueContext.id) {\n return output;\n }\n\n const { id: queueId, stepIndex, initialInput, queueJobId } = queueContext;\n const jobId = (params.ctx as any)?.jobId;\n const workerId = (params.ctx as any)?.workerId ?? '';\n\n const next = queueRuntime.getNextStep(queueId, stepIndex);\n const childJobId = next ? `job-${Date.now()}-${Math.random().toString(36).slice(2, 11)}` : undefined;\n if (next && queueJobId) {\n // Append next step first so updateQueueJobStepInStore(complete) sees steps.length > 1\n await notifyQueueJobStep(queueJobId, 'append', {\n workerJobId: childJobId!,\n workerId: next.workerId,\n });\n }\n\n // Notify current step complete (after append when there's next, so queue isn't marked completed yet)\n if (queueJobId && typeof stepIndex === 'number') {\n await notifyQueueJobStep(queueJobId, 'complete', {\n queueId,\n stepIndex,\n workerJobId: jobId,\n workerId,\n output,\n });\n }\n\n if (!next) {\n return output;\n }\n\n let nextInput: unknown = output;\n if (next.mapInputFromPrev && typeof queueRuntime.invokeMapInput === 'function') {\n let previousOutputs: QueueStepOutput[] = [];\n if (queueJobId && typeof queueRuntime.getQueueJob === 'function') {\n try {\n const job = await queueRuntime.getQueueJob(queueJobId);\n if (job?.steps) {\n const fromStore = job.steps\n .slice(0, stepIndex)\n .map((s, i) => ({ stepIndex: i, workerId: s.workerId, output: s.output }));\n previousOutputs = fromStore.concat([\n { stepIndex, workerId: (params.ctx as any)?.workerId ?? '', output },\n ]);\n }\n } catch (e: any) {\n if (process.env.AI_WORKER_QUEUES_DEBUG === '1') {\n console.warn('[Worker] getQueueJob failed, mapping without previousOutputs:', e?.message ?? e);\n }\n }\n }\n nextInput = await queueRuntime.invokeMapInput(\n queueId,\n stepIndex + 1,\n initialInput,\n previousOutputs\n );\n }\n\n const nextInputWithQueue = {\n ...(nextInput !== null && typeof nextInput === 'object' ? (nextInput as Record<string, unknown>) : { value: nextInput }),\n [WORKER_QUEUE_KEY]: {\n id: queueId,\n stepIndex: stepIndex + 1,\n initialInput,\n queueJobId,\n },\n };\n\n const debug = process.env.AI_WORKER_QUEUES_DEBUG === '1';\n if (debug) {\n console.log('[Worker] Queue chain dispatching next:', {\n queueId,\n fromStep: stepIndex,\n nextWorkerId: next.workerId,\n delaySeconds: next.delaySeconds,\n });\n }\n\n await params.ctx.dispatchWorker(next.workerId, nextInputWithQueue, {\n await: false,\n delaySeconds: next.delaySeconds,\n jobId: childJobId,\n });\n\n return output;\n };\n}\n\nexport interface SQSMessageBody {\n workerId: string;\n jobId: string;\n input: any;\n context: Record<string, any>;\n webhookUrl?: string;\n /** @deprecated Never use. Job updates use MongoDB only. */\n jobStoreUrl?: string;\n metadata?: Record<string, any>;\n timestamp: string;\n}\n\nexport interface WebhookPayload {\n jobId: string;\n workerId: string;\n status: 'success' | 'error';\n output?: any;\n error?: {\n message: string;\n stack?: string;\n name?: string;\n };\n metadata?: Record<string, any>;\n}\n\nconst DEFAULT_POLL_INTERVAL_MS = 2000;\nconst DEFAULT_POLL_TIMEOUT_MS = 15 * 60 * 1000; // 15 minutes\n\nfunction sanitizeWorkerIdForEnv(workerId: string): string {\n return workerId.replace(/-/g, '_').toUpperCase();\n}\n\nfunction getQueueUrlForWorker(calleeWorkerId: string): string | undefined {\n const key = `WORKER_QUEUE_URL_${sanitizeWorkerIdForEnv(calleeWorkerId)}`;\n return process.env[key]?.trim() || undefined;\n}\n\n/**\n * Create dispatchWorker for use in handler context (Lambda).\n * Sends message to SQS, appends to parent internalJobs, optionally polls until child completes.\n */\nfunction createDispatchWorker(\n parentJobId: string,\n parentWorkerId: string,\n parentContext: Record<string, any>,\n jobStore: JobStore | undefined\n): (\n workerId: string,\n input: unknown,\n options?: DispatchWorkerOptions\n) => Promise<{ jobId: string; messageId?: string; output?: unknown }> {\n return async (\n calleeWorkerId: string,\n input: unknown,\n options?: DispatchWorkerOptions\n ): Promise<{ jobId: string; messageId?: string; output?: unknown }> => {\n const childJobId =\n options?.jobId ||\n `job-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;\n const metadata = options?.metadata ?? {};\n const serializedContext: Record<string, any> = {};\n if (parentContext.requestId) serializedContext.requestId = parentContext.requestId;\n\n const messageBody: SQSMessageBody = {\n workerId: calleeWorkerId,\n jobId: childJobId,\n input: input ?? {},\n context: serializedContext,\n webhookUrl: options?.webhookUrl,\n metadata,\n timestamp: new Date().toISOString(),\n };\n\n const queueUrl = getQueueUrlForWorker(calleeWorkerId);\n\n if (queueUrl) {\n const region = process.env.AWS_REGION || process.env.AWS_DEFAULT_REGION || 'us-east-1';\n const sqs = new SQSClient({ region });\n // SQS message timer (per-message DelaySeconds): message stays invisible for N seconds.\n // Calling worker returns immediately; no computation during delay. See:\n // https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-delay-queues.html\n const delaySeconds =\n options?.await !== true && options?.delaySeconds != null\n ? Math.min(SQS_MAX_DELAY_SECONDS, Math.max(0, Math.floor(options.delaySeconds)))\n : undefined;\n const sendResult = await sqs.send(\n new SendMessageCommand({\n QueueUrl: queueUrl,\n MessageBody: JSON.stringify(messageBody),\n ...(delaySeconds !== undefined && delaySeconds > 0 ? { DelaySeconds: delaySeconds } : {}),\n })\n );\n const messageId = sendResult.MessageId ?? undefined;\n\n if (jobStore?.appendInternalJob) {\n await jobStore.appendInternalJob({ jobId: childJobId, workerId: calleeWorkerId });\n }\n\n if (options?.await && jobStore?.getJob) {\n const pollIntervalMs = options.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;\n const pollTimeoutMs = options.pollTimeoutMs ?? DEFAULT_POLL_TIMEOUT_MS;\n const deadline = Date.now() + pollTimeoutMs;\n while (Date.now() < deadline) {\n const child = await jobStore.getJob(childJobId);\n if (!child) {\n await new Promise((r) => setTimeout(r, pollIntervalMs));\n continue;\n }\n if (child.status === 'completed') {\n return { jobId: childJobId, messageId, output: child.output };\n }\n if (child.status === 'failed') {\n const err = child.error;\n throw new Error(\n err?.message ?? `Child worker ${calleeWorkerId} failed`\n );\n }\n await new Promise((r) => setTimeout(r, pollIntervalMs));\n }\n throw new Error(\n `Child worker ${calleeWorkerId} (${childJobId}) did not complete within ${pollTimeoutMs}ms`\n );\n }\n\n return { jobId: childJobId, messageId };\n }\n\n // Fallback: no queue URL (e.g. local dev). Caller (index.ts) should provide in-process dispatch.\n throw new Error(\n `WORKER_QUEUE_URL_${sanitizeWorkerIdForEnv(calleeWorkerId)} is not set. ` +\n 'Configure queue URL for worker-to-worker dispatch, or run in local mode.'\n );\n };\n}\n\n/**\n * Sends a webhook callback to the specified URL.\n */\nasync function sendWebhook(\n webhookUrl: string,\n payload: WebhookPayload\n): Promise<void> {\n try {\n const response = await fetch(webhookUrl, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'User-Agent': 'ai-router-worker/1.0',\n },\n body: JSON.stringify(payload),\n });\n\n if (!response.ok) {\n const errorText = await response.text().catch(() => '');\n console.error('[Worker] Webhook callback failed:', {\n url: webhookUrl,\n status: response.status,\n statusText: response.statusText,\n errorText,\n });\n // Don't throw - webhook failures shouldn't fail the Lambda\n } else {\n console.log('[Worker] Webhook callback successful:', {\n url: webhookUrl,\n status: response.status,\n });\n }\n } catch (error: any) {\n console.error('[Worker] Webhook callback error:', {\n url: webhookUrl,\n error: error?.message || String(error),\n stack: error?.stack,\n });\n // Don't throw - webhook failures shouldn't fail the Lambda\n }\n}\n\n/**\n * Creates a Lambda handler function that processes SQS events for workers.\n * Job store: MongoDB only. Never uses HTTP/origin URL for job updates.\n *\n * @param handler - The user's worker handler function\n * @param outputSchema - Optional Zod schema for output validation\n * @returns A Lambda handler function\n */\nexport function createLambdaHandler<INPUT, OUTPUT>(\n handler: WorkerHandler<INPUT, OUTPUT>,\n outputSchema?: ZodType<OUTPUT>\n): (event: SQSEvent, context: LambdaContext) => Promise<void> {\n return async (event: SQSEvent, lambdaContext: LambdaContext) => {\n const promises = event.Records.map(async (record: SQSRecord) => {\n let messageBody: SQSMessageBody | null = null;\n try {\n messageBody = JSON.parse(record.body) as SQSMessageBody;\n\n const { workerId, jobId, input, context, webhookUrl, metadata = {} } =\n messageBody;\n\n // Idempotency: skip if this job was already completed or failed (e.g. SQS redelivery or duplicate trigger).\n // Only the Lambda that processes a message creates/updates that job's key; parent workers only append to internalJobs and poll – they never write child job documents.\n const raw = (process.env.WORKER_DATABASE_TYPE || 'upstash-redis').toLowerCase();\n const jobStoreType: 'mongodb' | 'upstash-redis' =\n raw === 'mongodb' ? 'mongodb' : 'upstash-redis';\n if (jobStoreType === 'upstash-redis' && isRedisJobStoreConfigured()) {\n const existing = await loadRedisJob(jobId);\n if (existing && (existing.status === 'completed' || existing.status === 'failed')) {\n console.log('[Worker] Skipping already terminal job (idempotent):', {\n jobId,\n workerId,\n status: existing.status,\n });\n return;\n }\n } else if (jobStoreType === 'mongodb' || isMongoJobStoreConfigured()) {\n const existing = await getMongoJobById(jobId);\n if (existing && (existing.status === 'completed' || existing.status === 'failed')) {\n console.log('[Worker] Skipping already terminal job (idempotent):', {\n jobId,\n workerId,\n status: existing.status,\n });\n return;\n }\n }\n\n // Select job store and upsert this message's job only (never write child job documents from parent).\n let jobStore: JobStore | undefined;\n if (\n jobStoreType === 'upstash-redis' &&\n isRedisJobStoreConfigured()\n ) {\n await upsertRedisJob(jobId, workerId, input, metadata);\n jobStore = createRedisJobStore(workerId, jobId, input, metadata);\n } else if (\n jobStoreType === 'mongodb' ||\n isMongoJobStoreConfigured()\n ) {\n await upsertJob(jobId, workerId, input, metadata);\n jobStore = createMongoJobStore(workerId, jobId, input, metadata);\n }\n\n const baseContext = {\n jobId,\n workerId,\n requestId: context.requestId || lambdaContext.awsRequestId,\n ...context,\n };\n const handlerContext = {\n ...baseContext,\n ...(jobStore ? { jobStore } : {}),\n logger: createWorkerLogger(jobId, workerId),\n dispatchWorker: createDispatchWorker(\n jobId,\n workerId,\n baseContext,\n jobStore\n ),\n };\n\n if (jobStore) {\n try {\n await jobStore.update({ status: 'running' });\n const queueCtxForLog = (input as any)?.__workerQueue ?? metadata?.__workerQueue;\n console.log('[Worker] Job status updated to running:', {\n jobId,\n workerId,\n ...(queueCtxForLog?.id && { queueId: queueCtxForLog.id }),\n ...(queueCtxForLog?.queueJobId && { queueJobId: queueCtxForLog.queueJobId }),\n });\n } catch (error: any) {\n console.warn('[Worker] Failed to update status to running:', {\n jobId,\n workerId,\n error: error?.message || String(error),\n });\n }\n }\n\n const queueCtx = (input as any)?.__workerQueue ?? metadata?.__workerQueue;\n if (queueCtx?.queueJobId && typeof queueCtx.stepIndex === 'number') {\n // Ensure initial queue job exists (mainly for cron/queue-starter paths)\n if (queueCtx.stepIndex === 0) {\n try {\n await upsertInitialQueueJob({\n queueJobId: queueCtx.queueJobId,\n queueId: queueCtx.id,\n firstWorkerId: workerId,\n firstWorkerJobId: jobId,\n metadata,\n });\n } catch (e: any) {\n console.warn('[Worker] Failed to upsert initial queue job:', {\n queueJobId: queueCtx.queueJobId,\n queueId: queueCtx.id,\n error: e?.message ?? String(e),\n });\n }\n }\n await notifyQueueJobStep(queueCtx.queueJobId, 'start', {\n queueId: queueCtx.id,\n stepIndex: queueCtx.stepIndex,\n workerJobId: jobId,\n workerId,\n input,\n });\n }\n\n let output: OUTPUT;\n try {\n output = await handler({\n input: input as INPUT,\n ctx: handlerContext,\n });\n\n if (outputSchema) {\n output = outputSchema.parse(output);\n }\n } catch (error: any) {\n const errorPayload: WebhookPayload = {\n jobId,\n workerId,\n status: 'error',\n error: {\n message: error.message || 'Unknown error',\n stack: error.stack,\n name: error.name || 'Error',\n },\n metadata,\n };\n\n if (jobStore) {\n try {\n await jobStore.update({\n status: 'failed',\n error: errorPayload.error,\n });\n console.log('[Worker] Job status updated to failed:', {\n jobId,\n workerId,\n });\n } catch (updateError: any) {\n console.warn('[Worker] Failed to update job store on error:', {\n jobId,\n workerId,\n error: updateError?.message || String(updateError),\n });\n }\n }\n\n const queueCtxFail = (input as any)?.__workerQueue ?? metadata?.__workerQueue;\n if (queueCtxFail?.queueJobId && typeof queueCtxFail.stepIndex === 'number') {\n await notifyQueueJobStep(queueCtxFail.queueJobId, 'fail', {\n queueId: queueCtxFail.id,\n stepIndex: queueCtxFail.stepIndex,\n workerJobId: jobId,\n workerId,\n error: errorPayload.error,\n });\n }\n\n if (webhookUrl) {\n await sendWebhook(webhookUrl, errorPayload);\n }\n throw error;\n }\n\n if (jobStore) {\n try {\n await jobStore.update({\n status: 'completed',\n output,\n });\n console.log('[Worker] Job status updated to completed:', {\n jobId,\n workerId,\n });\n } catch (updateError: any) {\n console.warn('[Worker] Failed to update job store on success:', {\n jobId,\n workerId,\n error: updateError?.message || String(updateError),\n });\n }\n }\n\n // Queue step complete is notified from wrapHandlerForQueue (after append) so one DB update marks step + queue.\n\n console.log('[Worker] Job completed:', {\n jobId,\n workerId,\n output,\n });\n\n const successPayload: WebhookPayload = {\n jobId,\n workerId,\n status: 'success',\n output,\n metadata,\n };\n\n if (webhookUrl) {\n await sendWebhook(webhookUrl, successPayload);\n }\n } catch (error: any) {\n console.error('[Worker] Error processing SQS record:', {\n jobId: messageBody?.jobId ?? '(parse failed)',\n workerId: messageBody?.workerId ?? '(parse failed)',\n error: error?.message || String(error),\n stack: error?.stack,\n });\n throw error;\n }\n });\n\n await Promise.all(promises);\n };\n}\n","/**\n * MongoDB-backed job store for Lambda workers.\n * Updates jobs directly in MongoDB; never uses HTTP/origin URL.\n *\n * Env: MONGODB_WORKER_URI (or MONGODB_URI), MONGODB_WORKER_DB (or MONGODB_DB),\n * MONGODB_WORKER_JOBS_COLLECTION (default: worker_jobs).\n */\n\nimport { MongoClient, type Collection } from 'mongodb';\nimport type { JobStore, JobStoreUpdate } from './handler';\n\nconst uri = process.env.MONGODB_WORKER_URI || process.env.DATABASE_MONGODB_URI || process.env.MONGODB_URI;\nconst dbName =\n process.env.MONGODB_WORKER_DB ||\n process.env.MONGODB_DB ||\n 'worker';\nconst collectionName =\n process.env.MONGODB_WORKER_JOBS_COLLECTION || 'worker_jobs';\n\ntype InternalJobEntry = { jobId: string; workerId: string };\n\ntype Doc = {\n _id: string;\n jobId: string;\n workerId: string;\n status: 'queued' | 'running' | 'completed' | 'failed';\n input: any;\n output?: any;\n error?: { message: string; stack?: string; name?: string };\n metadata?: Record<string, any>;\n internalJobs?: InternalJobEntry[];\n createdAt: string;\n updatedAt: string;\n completedAt?: string;\n};\n\nlet clientPromise: Promise<MongoClient> | null = null;\n\nfunction getClient(): Promise<MongoClient> {\n if (!uri) {\n throw new Error(\n 'MongoDB URI required for job store. Set DATABASE_MONGODB_URI or MONGODB_URI.'\n );\n }\n if (!clientPromise) {\n clientPromise = new MongoClient(uri, {\n maxPoolSize: 10,\n minPoolSize: 0,\n serverSelectionTimeoutMS: 10_000,\n }).connect();\n }\n return clientPromise;\n}\n\nasync function getCollection(): Promise<Collection<Doc>> {\n const client = await getClient();\n return client.db(dbName).collection<Doc>(collectionName);\n}\n\n/**\n * Load a job by id (read-only). Used for idempotency check before processing.\n */\nexport async function getJobById(jobId: string): Promise<{\n jobId: string;\n workerId: string;\n status: 'queued' | 'running' | 'completed' | 'failed';\n input: any;\n output?: any;\n error?: { message: string; stack?: string };\n metadata?: Record<string, any>;\n internalJobs?: Array<{ jobId: string; workerId: string }>;\n createdAt: string;\n updatedAt: string;\n completedAt?: string;\n} | null> {\n try {\n const coll = await getCollection();\n const doc = await coll.findOne({ _id: jobId });\n if (!doc) return null;\n const { _id, ...r } = doc;\n return r as any;\n } catch (e: any) {\n console.error('[Worker] MongoDB getJobById failed:', {\n jobId,\n error: e?.message ?? String(e),\n });\n return null;\n }\n}\n\n/**\n * Create a JobStore that reads/writes directly to MongoDB.\n * Caller must ensure the job exists (upsert on first use).\n */\nexport function createMongoJobStore(\n workerId: string,\n jobId: string,\n input: any,\n metadata: Record<string, any>\n): JobStore {\n return {\n update: async (update: JobStoreUpdate): Promise<void> => {\n try {\n const coll = await getCollection();\n const now = new Date().toISOString();\n const existing = await coll.findOne({ _id: jobId });\n\n let metadataUpdate: Record<string, any> = { ...(existing?.metadata ?? {}) };\n if (update.metadata) {\n Object.assign(metadataUpdate, update.metadata);\n }\n if (update.progress !== undefined || update.progressMessage !== undefined) {\n metadataUpdate.progress = update.progress;\n metadataUpdate.progressMessage = update.progressMessage;\n }\n\n const set: Partial<Doc> = {\n updatedAt: now,\n metadata: metadataUpdate,\n };\n if (update.status !== undefined) {\n set.status = update.status;\n if (['completed', 'failed'].includes(update.status) && !existing?.completedAt) {\n set.completedAt = now;\n }\n }\n if (update.output !== undefined) set.output = update.output;\n if (update.error !== undefined) set.error = update.error;\n\n if (existing) {\n await coll.updateOne({ _id: jobId }, { $set: set });\n } else {\n const doc: Doc = {\n _id: jobId,\n jobId,\n workerId,\n status: (update.status as Doc['status']) ?? 'queued',\n input: input ?? {},\n output: update.output,\n error: update.error,\n metadata: metadataUpdate,\n createdAt: now,\n updatedAt: now,\n completedAt: set.completedAt,\n };\n if (doc.status === 'completed' || doc.status === 'failed') {\n doc.completedAt = doc.completedAt ?? now;\n }\n await coll.updateOne({ _id: jobId }, { $set: doc }, { upsert: true });\n }\n } catch (e: any) {\n console.error('[Worker] MongoDB job store update failed:', {\n jobId,\n workerId,\n error: e?.message ?? String(e),\n });\n }\n },\n get: async () => {\n try {\n const coll = await getCollection();\n const doc = await coll.findOne({ _id: jobId });\n if (!doc) return null;\n const { _id, ...r } = doc;\n return r as any;\n } catch (e: any) {\n console.error('[Worker] MongoDB job store get failed:', {\n jobId,\n workerId,\n error: e?.message ?? String(e),\n });\n return null;\n }\n },\n appendInternalJob: async (entry: { jobId: string; workerId: string }): Promise<void> => {\n try {\n const coll = await getCollection();\n await coll.updateOne(\n { _id: jobId },\n { $push: { internalJobs: entry } }\n );\n } catch (e: any) {\n console.error('[Worker] MongoDB job store appendInternalJob failed:', {\n jobId,\n workerId,\n error: e?.message ?? String(e),\n });\n }\n },\n getJob: async (otherJobId: string): Promise<{\n jobId: string;\n workerId: string;\n status: 'queued' | 'running' | 'completed' | 'failed';\n input: any;\n output?: any;\n error?: { message: string; stack?: string };\n metadata?: Record<string, any>;\n internalJobs?: Array<{ jobId: string; workerId: string }>;\n createdAt: string;\n updatedAt: string;\n completedAt?: string;\n } | null> => {\n try {\n const coll = await getCollection();\n const doc = await coll.findOne({ _id: otherJobId });\n if (!doc) return null;\n const { _id, ...r } = doc;\n return r as any;\n } catch (e: any) {\n console.error('[Worker] MongoDB job store getJob failed:', {\n otherJobId,\n error: e?.message ?? String(e),\n });\n return null;\n }\n },\n };\n}\n\n/**\n * Upsert initial job record in MongoDB (queued).\n * Call this when the Lambda starts processing a message.\n */\nexport async function upsertJob(\n jobId: string,\n workerId: string,\n input: any,\n metadata: Record<string, any>\n): Promise<void> {\n const coll = await getCollection();\n const now = new Date().toISOString();\n await coll.updateOne(\n { _id: jobId },\n {\n $set: {\n _id: jobId,\n jobId,\n workerId,\n status: 'queued',\n input: input ?? {},\n metadata: metadata ?? {},\n createdAt: now,\n updatedAt: now,\n },\n },\n { upsert: true }\n );\n}\n\nexport function isMongoJobStoreConfigured(): boolean {\n return Boolean(uri?.trim());\n}\n","import { Redis } from '@upstash/redis';\nimport type { JobStore, JobStoreUpdate, JobRecord } from './handler';\n\n// Canonical: WORKER_* first, then UPSTASH_* / REDIS_* / WORKFLOW_* fallbacks\nconst redisUrl =\n process.env.WORKER_UPSTASH_REDIS_REST_URL ||\n process.env.UPSTASH_REDIS_REST_URL ||\n process.env.UPSTASH_REDIS_URL;\nconst redisToken =\n process.env.WORKER_UPSTASH_REDIS_REST_TOKEN ||\n process.env.UPSTASH_REDIS_REST_TOKEN ||\n process.env.UPSTASH_REDIS_TOKEN;\nconst jobKeyPrefix =\n process.env.WORKER_UPSTASH_REDIS_JOBS_PREFIX ||\n process.env.UPSTASH_REDIS_KEY_PREFIX ||\n process.env.REDIS_WORKER_JOB_PREFIX ||\n 'worker:jobs:';\nconst defaultTtlSeconds = 60 * 60 * 24 * 7; // 7 days\nconst jobTtlSeconds =\n typeof process.env.WORKER_JOBS_TTL_SECONDS === 'string'\n ? parseInt(process.env.WORKER_JOBS_TTL_SECONDS, 10) || defaultTtlSeconds\n : typeof process.env.REDIS_WORKER_JOB_TTL_SECONDS === 'string'\n ? parseInt(process.env.REDIS_WORKER_JOB_TTL_SECONDS, 10) || defaultTtlSeconds\n : typeof process.env.WORKFLOW_JOBS_TTL_SECONDS === 'string'\n ? parseInt(process.env.WORKFLOW_JOBS_TTL_SECONDS, 10) || defaultTtlSeconds\n : defaultTtlSeconds;\n\nlet redisClient: Redis | null = null;\n\nfunction getRedis(): Redis {\n if (!redisUrl || !redisToken) {\n throw new Error(\n 'Upstash Redis configuration missing. Set WORKER_UPSTASH_REDIS_REST_URL and WORKER_UPSTASH_REDIS_REST_TOKEN (or UPSTASH_REDIS_REST_URL/UPSTASH_REDIS_REST_TOKEN).'\n );\n }\n if (!redisClient) {\n redisClient = new Redis({\n url: redisUrl,\n token: redisToken,\n });\n }\n return redisClient;\n}\n\nfunction jobKey(jobId: string): string {\n return `${jobKeyPrefix}${jobId}`;\n}\n\n/** Separate LIST key for internal job refs; each RPUSH is atomic so no race when appending multiple. */\nfunction internalListKey(jobId: string): string {\n return `${jobKeyPrefix}${jobId}:internal`;\n}\n\nexport function isRedisJobStoreConfigured(): boolean {\n return Boolean((redisUrl || '').trim() && (redisToken || '').trim());\n}\n\n/** Load a job by id (read-only). Used for idempotency check before processing. */\nexport async function loadJob(jobId: string): Promise<JobRecord | null> {\n const redis = getRedis();\n const key = jobKey(jobId);\n const data = await redis.hgetall<Record<string, string>>(key);\n if (!data || Object.keys(data).length === 0) return null;\n const parseJson = <T>(val?: string | null): T | undefined => {\n if (!val) return undefined;\n try {\n return JSON.parse(val) as T;\n } catch {\n return undefined;\n }\n };\n // Prefer atomic list key for internal jobs; fallback to hash field for old records\n const listKey = internalListKey(jobId);\n const listItems = await redis.lrange<string>(listKey, 0, -1);\n let internalJobs: Array<{ jobId: string; workerId: string }> | undefined;\n if (listItems && listItems.length > 0) {\n internalJobs = listItems.map((s) => {\n try {\n return JSON.parse(s) as { jobId: string; workerId: string };\n } catch {\n return null;\n }\n }).filter(Boolean) as Array<{ jobId: string; workerId: string }>;\n } else {\n internalJobs = parseJson<Array<{ jobId: string; workerId: string }>>(data.internalJobs);\n }\n const record: JobRecord = {\n jobId: data.jobId,\n workerId: data.workerId,\n status: (data.status as JobRecord['status']) || 'queued',\n input: parseJson<any>(data.input) ?? {},\n output: parseJson<any>(data.output),\n error: parseJson<any>(data.error),\n metadata: parseJson<Record<string, any>>(data.metadata) ?? {},\n internalJobs,\n createdAt: data.createdAt,\n updatedAt: data.updatedAt,\n completedAt: data.completedAt,\n };\n return record;\n}\n\nexport function createRedisJobStore(\n workerId: string,\n jobId: string,\n input: any,\n metadata: Record<string, any>\n): JobStore {\n return {\n update: async (update: JobStoreUpdate): Promise<void> => {\n const redis = getRedis();\n const key = jobKey(jobId);\n const now = new Date().toISOString();\n\n // Load existing to merge metadata/progress if needed\n const existing = await loadJob(jobId);\n const next: Partial<JobRecord> = {};\n\n // Start from existing metadata\n const mergedMeta: Record<string, any> = { ...(existing?.metadata ?? {}) };\n if (update.metadata) {\n Object.assign(mergedMeta, update.metadata);\n }\n if (update.progress !== undefined || update.progressMessage !== undefined) {\n mergedMeta.progress = update.progress;\n mergedMeta.progressMessage = update.progressMessage;\n }\n\n next.metadata = mergedMeta;\n if (update.status !== undefined) {\n next.status = update.error ? 'failed' : update.status;\n if ((update.status === 'completed' || update.status === 'failed') && !existing?.completedAt) {\n next.completedAt = now;\n }\n }\n if (update.output !== undefined) next.output = update.output;\n if (update.error !== undefined) next.error = update.error;\n\n const toSet: Record<string, string> = {};\n if (next.status) toSet['status'] = String(next.status);\n if (next.output !== undefined) toSet['output'] = JSON.stringify(next.output);\n if (next.error !== undefined) toSet['error'] = JSON.stringify(next.error);\n if (next.metadata !== undefined) toSet['metadata'] = JSON.stringify(next.metadata);\n if (next.completedAt) {\n toSet['completedAt'] = next.completedAt;\n }\n toSet['updatedAt'] = now;\n\n await redis.hset(key, toSet);\n if (jobTtlSeconds > 0) {\n await redis.expire(key, jobTtlSeconds);\n }\n },\n get: async () => {\n return loadJob(jobId);\n },\n appendInternalJob: async (entry) => {\n const redis = getRedis();\n const listKey = internalListKey(jobId);\n await redis.rpush(listKey, JSON.stringify(entry));\n const mainKey = jobKey(jobId);\n await redis.hset(mainKey, { updatedAt: new Date().toISOString() });\n if (jobTtlSeconds > 0) {\n await redis.expire(listKey, jobTtlSeconds);\n await redis.expire(mainKey, jobTtlSeconds);\n }\n },\n getJob: async (otherJobId: string) => {\n return loadJob(otherJobId);\n },\n };\n}\n\nexport async function upsertRedisJob(\n jobId: string,\n workerId: string,\n input: any,\n metadata: Record<string, any>\n): Promise<void> {\n const redis = getRedis();\n const key = jobKey(jobId);\n const now = new Date().toISOString();\n const doc: Partial<JobRecord> = {\n jobId,\n workerId,\n status: 'queued',\n input,\n metadata,\n createdAt: now,\n updatedAt: now,\n };\n const toSet: Record<string, string> = {\n jobId: jobId,\n workerId: workerId,\n status: doc.status!,\n input: JSON.stringify(doc.input ?? {}),\n metadata: JSON.stringify(doc.metadata ?? {}),\n createdAt: now,\n updatedAt: now,\n };\n await redis.hset(key, toSet);\n if (jobTtlSeconds > 0) {\n await redis.expire(key, jobTtlSeconds);\n }\n}\n\n","/**\n * Queue job store for worker queues (MongoDB or Upstash Redis).\n *\n * Mirrors the worker_jobs pattern but optimized for queues:\n * - MongoDB: collection `queue_jobs` (configurable via MONGODB_QUEUE_JOBS_COLLECTION)\n * - Upstash Redis: JSON blob per queue job with compact step entries\n *\n * This module is runtime-only (used by Lambda workers). Next.js APIs can read\n * the same collections/keys to show queue progress.\n */\n\nimport type { Redis } from '@upstash/redis';\nimport { Redis as UpstashRedis } from '@upstash/redis';\nimport { MongoClient, type Collection } from 'mongodb';\n\ntype QueueJobStep = {\n workerId: string;\n workerJobId: string;\n status: 'queued' | 'running' | 'completed' | 'failed';\n input?: unknown;\n output?: unknown;\n error?: { message: string };\n startedAt?: string;\n completedAt?: string;\n};\n\ntype QueueJobDoc = {\n _id: string; // queueJobId\n id: string;\n queueId: string;\n status: 'running' | 'completed' | 'failed' | 'partial';\n steps: QueueJobStep[];\n metadata?: Record<string, unknown>;\n createdAt: string;\n updatedAt: string;\n completedAt?: string;\n};\n\n\n// === Mongo backend (shares connection pattern with mongoJobStore) ===\n\nconst mongoUri = process.env.DATABASE_MONGODB_URI || process.env.MONGODB_URI;\nconst mongoDbName =\n process.env.DATABASE_MONGODB_DB ||\n process.env.MONGODB_DB ||\n 'mediamake';\nconst mongoQueueCollectionName =\n process.env.MONGODB_QUEUE_JOBS_COLLECTION || 'queue_jobs';\n\nlet mongoClientPromise: Promise<MongoClient> | null = null;\n\nasync function getMongoClient(): Promise<MongoClient> {\n if (!mongoUri) {\n throw new Error(\n 'MongoDB URI required for queue job store. Set DATABASE_MONGODB_URI or MONGODB_URI.'\n );\n }\n if (!mongoClientPromise) {\n mongoClientPromise = new MongoClient(mongoUri, {\n maxPoolSize: 10,\n minPoolSize: 0,\n serverSelectionTimeoutMS: 10_000,\n }).connect();\n }\n return mongoClientPromise;\n}\n\nasync function getMongoQueueCollection(): Promise<Collection<QueueJobDoc>> {\n const client = await getMongoClient();\n return client.db(mongoDbName).collection<QueueJobDoc>(mongoQueueCollectionName);\n}\n\n// === Redis backend (Upstash) ===\n\nconst redisUrl =\n process.env.WORKER_UPSTASH_REDIS_REST_URL ||\n process.env.UPSTASH_REDIS_REST_URL ||\n process.env.UPSTASH_REDIS_URL;\nconst redisToken =\n process.env.WORKER_UPSTASH_REDIS_REST_TOKEN ||\n process.env.UPSTASH_REDIS_REST_TOKEN ||\n process.env.UPSTASH_REDIS_TOKEN;\nconst queueKeyPrefix =\n process.env.WORKER_UPSTASH_REDIS_QUEUE_PREFIX ||\n process.env.UPSTASH_REDIS_QUEUE_PREFIX ||\n 'worker:queue-jobs:';\n\nconst defaultTtlSeconds = 60 * 60 * 24 * 7; // 7 days\nconst queueJobTtlSeconds =\n typeof process.env.WORKER_QUEUE_JOBS_TTL_SECONDS === 'string'\n ? parseInt(process.env.WORKER_QUEUE_JOBS_TTL_SECONDS, 10) || defaultTtlSeconds\n : typeof process.env.WORKER_JOBS_TTL_SECONDS === 'string'\n ? parseInt(process.env.WORKER_JOBS_TTL_SECONDS, 10) || defaultTtlSeconds\n : defaultTtlSeconds;\n\nlet redisClient: Redis | null = null;\n\nfunction getRedis(): Redis {\n if (!redisUrl || !redisToken) {\n throw new Error(\n 'Upstash Redis configuration missing for queue job store. Set WORKER_UPSTASH_REDIS_REST_URL and WORKER_UPSTASH_REDIS_REST_TOKEN (or UPSTASH_REDIS_REST_URL/UPSTASH_REDIS_REST_TOKEN).'\n );\n }\n if (!redisClient) {\n redisClient = new UpstashRedis({\n url: redisUrl,\n token: redisToken,\n });\n }\n return redisClient;\n}\n\nfunction queueKey(id: string): string {\n return `${queueKeyPrefix}${id}`;\n}\n\ntype QueueJobRecord = Omit<QueueJobDoc, '_id'>;\n\n/** Hash values from Upstash hgetall may be auto-parsed (array/object) or raw strings. */\nfunction stepsFromHash(val: unknown): QueueJobStep[] {\n if (Array.isArray(val)) return val as QueueJobStep[];\n if (typeof val === 'string') {\n try {\n const parsed = JSON.parse(val) as QueueJobStep[];\n return Array.isArray(parsed) ? parsed : [];\n } catch {\n return [];\n }\n }\n return [];\n}\n\nfunction metadataFromHash(val: unknown): Record<string, unknown> {\n if (val && typeof val === 'object' && !Array.isArray(val)) return val as Record<string, unknown>;\n if (typeof val === 'string') {\n try {\n const parsed = JSON.parse(val) as Record<string, unknown>;\n return parsed && typeof parsed === 'object' ? parsed : {};\n } catch {\n return {};\n }\n }\n return {};\n}\n\nasync function loadQueueJobRedis(queueJobId: string): Promise<QueueJobRecord | null> {\n const redis = getRedis();\n const key = queueKey(queueJobId);\n const data = await redis.hgetall(key);\n if (!data || typeof data !== 'object' || Object.keys(data).length === 0) return null;\n const d = data as Record<string, unknown>;\n const record: QueueJobRecord = {\n id: (d.id === undefined ? queueJobId : String(d.id)) as string,\n queueId: String(d.queueId ?? ''),\n status: (String(d.status ?? 'running') as QueueJobRecord['status']),\n steps: stepsFromHash(d.steps),\n metadata: metadataFromHash(d.metadata),\n createdAt: String(d.createdAt ?? new Date().toISOString()),\n updatedAt: String(d.updatedAt ?? new Date().toISOString()),\n completedAt: d.completedAt != null ? String(d.completedAt) : undefined,\n };\n return record;\n}\n\nasync function saveQueueJobRedis(record: QueueJobRecord): Promise<void> {\n const redis = getRedis();\n const key = queueKey(record.id);\n const now = new Date().toISOString();\n const toSet: Record<string, string> = {\n id: record.id,\n queueId: record.queueId,\n status: record.status,\n steps: JSON.stringify(record.steps || []),\n metadata: JSON.stringify(record.metadata || {}),\n createdAt: record.createdAt || now,\n updatedAt: record.updatedAt || now,\n };\n if (record.completedAt) {\n toSet.completedAt = record.completedAt;\n }\n await redis.hset(key, toSet);\n if (queueJobTtlSeconds > 0) {\n await redis.expire(key, queueJobTtlSeconds);\n }\n}\n\n// === Backend selection ===\n\nfunction getStoreType(): 'mongodb' | 'upstash-redis' {\n const t = (process.env.WORKER_DATABASE_TYPE || 'upstash-redis').toLowerCase();\n return t === 'mongodb' ? 'mongodb' : 'upstash-redis';\n}\n\nfunction preferMongo(): boolean {\n return getStoreType() === 'mongodb' && Boolean(mongoUri?.trim());\n}\n\nfunction preferRedis(): boolean {\n return getStoreType() !== 'mongodb' && Boolean((redisUrl || '').trim() && (redisToken || '').trim());\n}\n\n// === Public API used from handler.ts ===\n\nexport async function upsertInitialQueueJob(options: {\n queueJobId: string;\n queueId: string;\n firstWorkerId: string;\n firstWorkerJobId: string;\n metadata?: Record<string, any>;\n}): Promise<void> {\n const { queueJobId, queueId, firstWorkerId, firstWorkerJobId, metadata } = options;\n const now = new Date().toISOString();\n\n if (preferMongo()) {\n const coll = await getMongoQueueCollection();\n const existing = await coll.findOne({ _id: queueJobId });\n if (existing) {\n const steps = existing.steps ?? [];\n if (steps.length === 0) {\n steps.push({\n workerId: firstWorkerId,\n workerJobId: firstWorkerJobId,\n status: 'queued',\n });\n }\n await coll.updateOne(\n { _id: queueJobId },\n {\n $set: {\n steps,\n updatedAt: now,\n },\n }\n );\n } else {\n const doc: QueueJobDoc = {\n _id: queueJobId,\n id: queueJobId,\n queueId,\n status: 'running',\n steps: [\n {\n workerId: firstWorkerId,\n workerJobId: firstWorkerJobId,\n status: 'queued',\n },\n ],\n metadata: metadata ?? {},\n createdAt: now,\n updatedAt: now,\n };\n await coll.updateOne(\n { _id: queueJobId },\n { $set: doc },\n { upsert: true }\n );\n }\n return;\n }\n\n if (preferRedis()) {\n const existing = await loadQueueJobRedis(queueJobId);\n if (existing) {\n // Ensure we have at least one step\n if (!existing.steps || existing.steps.length === 0) {\n existing.steps = [\n {\n workerId: firstWorkerId,\n workerJobId: firstWorkerJobId,\n status: 'queued',\n },\n ];\n }\n existing.updatedAt = now;\n await saveQueueJobRedis(existing);\n } else {\n const record: QueueJobRecord = {\n id: queueJobId,\n queueId,\n status: 'running',\n steps: [\n {\n workerId: firstWorkerId,\n workerJobId: firstWorkerJobId,\n status: 'queued',\n },\n ],\n metadata: metadata ?? {},\n createdAt: now,\n updatedAt: now,\n };\n await saveQueueJobRedis(record);\n }\n }\n}\n\nexport async function updateQueueJobStepInStore(options: {\n queueJobId: string;\n queueId?: string;\n stepIndex: number;\n workerId: string;\n workerJobId: string;\n status: 'running' | 'completed' | 'failed';\n input?: unknown;\n output?: unknown;\n error?: { message: string };\n}): Promise<void> {\n const { queueJobId, stepIndex, status, input, output, error } = options;\n const now = new Date().toISOString();\n\n if (preferMongo()) {\n const coll = await getMongoQueueCollection();\n const existing = await coll.findOne({ _id: queueJobId });\n if (!existing) return;\n const step = existing.steps[stepIndex];\n if (!step) return;\n\n const mergedStep: QueueJobStep = {\n ...step,\n status,\n ...(input !== undefined && { input }),\n ...(output !== undefined && { output }),\n ...(error !== undefined && { error }),\n startedAt: step.startedAt ?? (status === 'running' ? now : step.startedAt),\n completedAt:\n step.completedAt ??\n (status === 'completed' || status === 'failed' ? now : step.completedAt),\n };\n\n const setDoc: Partial<QueueJobDoc> & { steps: QueueJobStep[] } = {\n steps: existing.steps,\n updatedAt: now,\n };\n setDoc.steps[stepIndex] = mergedStep;\n if (status === 'failed') {\n setDoc.status = 'failed';\n if (!existing.completedAt) setDoc.completedAt = now;\n } else if (status === 'completed' && stepIndex === existing.steps.length - 1) {\n setDoc.status = 'completed';\n if (!existing.completedAt) setDoc.completedAt = now;\n }\n\n await coll.updateOne(\n { _id: queueJobId },\n {\n $set: setDoc,\n }\n );\n return;\n }\n\n if (preferRedis()) {\n const existing = await loadQueueJobRedis(queueJobId);\n if (!existing) {\n // No queue job; nothing to update\n return;\n }\n const steps = existing.steps || [];\n const step = steps[stepIndex];\n if (!step) {\n return;\n }\n step.status = status;\n if (input !== undefined) step.input = input;\n if (output !== undefined) step.output = output;\n if (error !== undefined) step.error = error;\n if (status === 'running') {\n step.startedAt = step.startedAt ?? now;\n }\n if (status === 'completed' || status === 'failed') {\n step.completedAt = step.completedAt ?? now;\n }\n\n existing.steps = steps;\n existing.updatedAt = now;\n if (status === 'failed') {\n existing.status = 'failed';\n existing.completedAt = existing.completedAt ?? now;\n } else if (status === 'completed' && stepIndex === steps.length - 1) {\n existing.status = 'completed';\n existing.completedAt = existing.completedAt ?? now;\n }\n await saveQueueJobRedis(existing);\n }\n}\n\nexport async function appendQueueJobStepInStore(options: {\n queueJobId: string;\n queueId?: string;\n workerId: string;\n workerJobId: string;\n}): Promise<void> {\n const { queueJobId, workerId, workerJobId } = options;\n const now = new Date().toISOString();\n\n if (preferMongo()) {\n const coll = await getMongoQueueCollection();\n await coll.updateOne(\n { _id: queueJobId },\n {\n $push: {\n steps: {\n workerId,\n workerJobId,\n status: 'queued',\n } as QueueJobStep,\n },\n $set: { updatedAt: now },\n }\n );\n return;\n }\n\n if (preferRedis()) {\n const existing = await loadQueueJobRedis(queueJobId);\n if (!existing) return;\n const steps = existing.steps || [];\n steps.push({\n workerId,\n workerJobId,\n status: 'queued',\n });\n existing.steps = steps;\n existing.updatedAt = now;\n await saveQueueJobRedis(existing);\n }\n}\n\n/**\n * Load a queue job by ID (for mapping context: previous step outputs).\n * Used by wrapHandlerForQueue when invoking mapInputFromPrev with previousOutputs.\n */\nexport async function getQueueJob(queueJobId: string): Promise<{\n id: string;\n queueId: string;\n status: string;\n steps: Array<{ workerId: string; workerJobId: string; status: string; output?: unknown }>;\n} | null> {\n if (preferMongo()) {\n const coll = await getMongoQueueCollection();\n const doc = await coll.findOne({ _id: queueJobId });\n if (!doc) return null;\n return {\n id: doc.id ?? queueJobId,\n queueId: doc.queueId,\n status: doc.status,\n steps: (doc.steps ?? []).map((s: QueueJobStep) => ({\n workerId: s.workerId,\n workerJobId: s.workerJobId,\n status: s.status,\n output: s.output,\n })),\n };\n }\n if (preferRedis()) {\n const record = await loadQueueJobRedis(queueJobId);\n if (!record) return null;\n return {\n id: record.id,\n queueId: record.queueId,\n status: record.status,\n steps: (record.steps ?? []).map((s) => ({\n workerId: s.workerId,\n workerJobId: s.workerJobId,\n status: s.status,\n output: s.output,\n })),\n };\n }\n return null;\n}\n\n","/**\n * Workers-config client for resolving queue URLs from the workers-config API Lambda.\n */\n\nexport interface WorkersConfig {\n version: string;\n stage: string;\n region: string;\n workers: Record<\n string,\n {\n queueUrl: string;\n region: string;\n }\n >;\n}\n\nlet cachedConfig: WorkersConfig | null = null;\nlet cacheExpiry: number = 0;\nconst CACHE_TTL_MS = 5 * 60 * 1000; // 5 minutes\n\n/**\n * Fetches the workers configuration from the workers-config API.\n * Results are cached for 5 minutes to reduce API calls.\n *\n * @param apiUrl - The URL of the workers-config API endpoint\n * @param apiKey - Optional API key for authentication (sent as x-workers-config-key header)\n * @returns The workers configuration mapping worker IDs to queue URLs\n */\nexport async function getWorkersConfig(\n apiUrl: string,\n apiKey?: string\n): Promise<WorkersConfig> {\n const now = Date.now();\n\n // Return cached config if still valid\n if (cachedConfig && now < cacheExpiry) {\n return cachedConfig;\n }\n\n const headers: Record<string, string> = {\n 'Content-Type': 'application/json',\n };\n\n if (apiKey) {\n headers['x-workers-config-key'] = apiKey;\n }\n\n const response = await fetch(apiUrl, {\n method: 'GET',\n headers,\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to fetch workers config: ${response.status} ${response.statusText}`\n );\n }\n\n const config = (await response.json()) as WorkersConfig;\n cachedConfig = config;\n cacheExpiry = now + CACHE_TTL_MS;\n\n return config;\n}\n\n/**\n * Resolves the queue URL for a specific worker ID.\n * Throws an error if the worker ID is not found in the configuration.\n *\n * @param workerId - The ID of the worker\n * @param apiUrl - The URL of the workers-config API endpoint\n * @param apiKey - Optional API key for authentication\n * @returns The queue URL for the worker\n */\nexport async function resolveQueueUrl(\n workerId: string,\n apiUrl: string,\n apiKey?: string\n): Promise<string> {\n const config = await getWorkersConfig(apiUrl, apiKey);\n const worker = config.workers[workerId];\n\n if (!worker) {\n throw new Error(\n `Worker \"${workerId}\" not found in workers config. Available workers: ${Object.keys(config.workers).join(', ')}`\n );\n }\n\n return worker.queueUrl;\n}\n\n/**\n * Clears the cached workers configuration.\n * Useful for testing or when you need to force a refresh.\n */\nexport function clearWorkersConfigCache(): void {\n cachedConfig = null;\n cacheExpiry = 0;\n}\n","/**\n * Queue definition and context types for worker queues.\n *\n * These types are used at code-time by .queue.ts files and at runtime\n * by the client and generated registry/queue wrappers.\n */\n\nexport interface WorkerQueueStep {\n /** Worker ID for this step. Must match an existing worker id. */\n workerId: string;\n /**\n * Optional delay (in seconds) before this step is executed.\n * Implemented via SQS DelaySeconds (0–900).\n */\n delaySeconds?: number;\n /**\n * Optional name of a mapping function exported from the .queue.ts file.\n * The function is called with (initialInput, previousOutputs):\n * - initialInput: original input passed to dispatchQueue (always first, for best DX).\n * - previousOutputs: array of { stepIndex, workerId, output } for steps 0..current-1.\n * Use any prior step's output; the immediate previous step is previousOutputs[previousOutputs.length - 1]?.output.\n */\n mapInputFromPrev?: string;\n}\n\nexport interface WorkerQueueConfig<InitialInput = any, StepOutput = any> {\n /** Stable queue identifier, e.g. \"cost-usage\". */\n id: string;\n /** Ordered list of workers forming the queue. */\n steps: WorkerQueueStep[];\n /**\n * Optional schedule for the queue (cron or rate).\n * When set, the CLI generates a queue-starter Lambda triggered by this schedule.\n * Example: 'cron(0 3 * * ? *)' for daily at 03:00 UTC.\n */\n schedule?: string | { rate: string; enabled?: boolean; input?: Record<string, any> };\n // The generic parameters are reserved for future typing improvements and\n // are intentionally unused here (config is structural at runtime).\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _initialInputType?: InitialInput;\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _stepOutputType?: StepOutput;\n}\n\n/**\n * Queue execution context that is embedded into job input/metadata so\n * queue-aware wrappers can determine where they are in the queue.\n */\nexport interface WorkerQueueContext<InitialInput = any> {\n id: string;\n stepIndex: number;\n initialInput: InitialInput;\n /** Queue job ID (same as first worker's jobId) for tracking progress. */\n queueJobId?: string;\n}\n\n/**\n * Identity helper for defining worker queues in .queue.ts files.\n * This is primarily for type safety and CLI discovery.\n */\nexport function defineWorkerQueue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A,oBACb,MAAM,MAAM,QAAQ;AAAA,kBACtB;AAAA,kBACA,UAAU,QAAQ;AAAA,gBACpB,CAAC;AAAA,cACH,CAAC;AAAA,YACH,SAAS,cAAc;AACrB,sBAAQ,KAAK,6CAA6C,YAAY;AAAA,YACxE;AAAA,UACF;AACA,gBAAM;AAAA,QACR;AAAA,MACF;AAGA,aAAO,SAAS,IAAI,OAAO,aAAa,OAAO;AAAA,IACjD;AAAA,EACF;AAEA,SAAO;AACT;AASO,SAAS,uBACd,OACA;AACA,SAAO,oBAAoB,MAAM,SAAS,MAAM,YAAY;AAC9D;","names":["import_redis","import_mongodb","redisUrl","redisToken","defaultTtlSeconds","redisClient","getRedis","UpstashRedis","module","explicitPath","DEFAULT_POLL_INTERVAL_MS","DEFAULT_POLL_TIMEOUT_MS","input","options","response"]}