import { ZodType, z } from 'zod'; import { d as WorkerQueueConfig, b as ChainContext, H as HitlResumeContext, L as LoopContext } from './queue-DaR2UuZi.mjs'; import './hitlConfig.mjs'; /** * Client for dispatching background worker jobs. * * In production, dispatching happens via the workers HTTP API: * POST /workers/trigger -> enqueues message to SQS on the workers service side * * This avoids requiring AWS credentials in your Next.js app. */ interface WorkerQueueRegistry { getQueueById(queueId: string): WorkerQueueConfig | undefined; getStepAt?(queueId: string, stepIndex: number): { workerId?: string; requiresApproval?: boolean; hasChain?: boolean; hasResume?: boolean; hasLoop?: boolean; hitl?: unknown; } | undefined; /** Build next-step input during normal chain advancement (no HITL). */ invokeChain?: (queueId: string, stepIndex: number, ctx: ChainContext) => Promise | unknown; /** Build domain input when a HITL step resumes after human approval. */ invokeResume?: (queueId: string, stepIndex: number, ctx: HitlResumeContext) => Promise | unknown; /** Evaluate whether a looping step should run again after its output. */ invokeLoop?: (queueId: string, stepIndex: number, ctx: LoopContext) => Promise | boolean; } interface DispatchOptions { /** * Optional webhook callback URL to notify when the job finishes. * Only called when provided. Default: no webhook (use job store / MongoDB only). */ webhookUrl?: string; /** * Controls how dispatch executes. * - "auto" (default): local inline execution in development unless WORKERS_LOCAL_MODE=false. * - "local": force inline execution (no SQS). * - "remote": force SQS/Lambda dispatch even in development. */ mode?: 'auto' | 'local' | 'remote'; jobId?: string; /** * The ID of the user who triggered this job. * Call getClientId() in your API route and pass the result here. * If omitted, userId is not stored and not logged. */ userId?: string; metadata?: Record; /** * In-memory queue registry for dispatchQueue. Required when using dispatchQueue. * Pass a registry that imports from your .queue.ts definitions (works on Vercel/serverless). */ registry?: WorkerQueueRegistry; /** * Optional callback to create a queue job record before dispatching. * Called with queueJobId (= first worker's jobId), queueId, and firstStep. */ onCreateQueueJob?: (params: { queueJobId: string; queueId: string; firstStep: { workerId: string; workerJobId: string; }; metadata?: Record; }) => Promise; /** * Maximum total tokens (input + output) allowed for this job. * The worker must call ctx.reportTokenUsage() after each LLM call. * Throws TokenBudgetExceededError when the limit is reached. * For queues, applies per-step unless overridden on the queue step config. */ maxTokens?: number; } interface DispatchResult { messageId: string; status: 'queued'; jobId: string; } interface DispatchQueueResult extends DispatchResult { queueId: string; } interface SerializedContext { requestId?: string; userId?: string; traceId?: string; [key: string]: any; } /** * Derives the full /workers/trigger URL from env. * Exported for use by local dispatchWorker (worker-to-worker in dev). * Server-side only; clients should use useWorkflowJob with your app's /api/workflows routes. * * Env vars: * - WORKER_BASE_URL: base URL of the workers service (e.g. https://.../prod) * - WORKERS_TRIGGER_API_URL / WORKERS_CONFIG_API_URL: legacy, still supported */ declare function getWorkersTriggerUrl(): string; /** * URL for the queue start endpoint (dispatch proxy). Use this so queue starts * go through the queue handler Lambda for easier debugging (one log stream per queue). */ declare function getQueueStartUrl(queueId: string): string; /** * Derives a stable worker API key from a projectId. * * This is the zero-config fallback used when no explicit `WORKERS_API_KEY` * (or legacy `WORKERS_TRIGGER_API_KEY` / `WORKERS_CONFIG_API_KEY`) is set. The * same formula is used by `@microfox/ai-worker-cli` at build time when it writes * the key into the deployed Lambda env, so both sides resolve the same value. * * The raw projectId is never sent as the header value — only this hash. */ declare function deriveWorkersApiKey(projectId: string): string; /** * Resolve the key to send on `x-workers-trigger-key` for /workers/trigger and * /queues/{id}/start. Precedence: explicit per-endpoint key → unified * `WORKERS_API_KEY` → projectId-derived (MICROFOX_PROJECT_ID). Returns undefined * when nothing resolves (the deployed endpoints are then public). */ declare function resolveWorkersTriggerKey(): string | undefined; /** * Resolve the key to send on `x-workers-config-key` for /workers/config. * Same precedence as {@link resolveWorkersTriggerKey} but with the config key. */ declare function resolveWorkersConfigKey(): string | undefined; /** * Dispatches a background worker job to SQS. * * @param workerId - The ID of the worker to dispatch * @param input - The input data for the worker (will be validated against inputSchema) * @param inputSchema - Zod schema for input validation * @param options - Dispatch options including webhook URL * @param ctx - Optional context object (only serializable parts will be sent) * @returns Promise resolving to dispatch result with messageId and jobId */ declare function dispatch>(workerId: string, input: z.input, inputSchema: INPUT_SCHEMA, options: DispatchOptions, ctx?: any): Promise; /** * Dispatch a worker by ID without importing the worker module. * Sends to the workers trigger API (WORKER_BASE_URL). No input schema validation at call site. * * @param workerId - The worker ID (e.g. 'echo', 'data-processor') * @param input - Input payload (object or undefined) * @param options - Optional jobId, webhookUrl, metadata * @param ctx - Optional context (serializable parts sent in the request) * @returns Promise resolving to { messageId, status: 'queued', jobId } */ declare function dispatchWorker(workerId: string, input?: Record, options?: DispatchOptions, ctx?: any): Promise; /** * Local development mode: runs the handler immediately in the same process. * This bypasses SQS and Lambda for faster iteration during development. * * @param handler - The worker handler function * @param input - The input data * @param ctx - The context object * @returns The handler result */ declare function dispatchLocal(handler: (params: { input: INPUT; ctx: any; }) => Promise, input: INPUT, ctx?: any): Promise; /** * Dispatches a queue by ID. POSTs to the queue-start API; the queue-start handler creates the queue job. * Pass the first worker's input directly (no registry required). */ declare function dispatchQueue(queueId: string, initialInput?: InitialInput, options?: DispatchOptions, _ctx?: any): Promise; export { type DispatchOptions, type DispatchQueueResult, type DispatchResult, type SerializedContext, type WorkerQueueRegistry, deriveWorkersApiKey, dispatch, dispatchLocal, dispatchQueue, dispatchWorker, getQueueStartUrl, getWorkersTriggerUrl, resolveWorkersConfigKey, resolveWorkersTriggerKey };