@microfox/ai-worker 1.0.1 → 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/CHANGELOG.md +14 -0
- package/README.md +19 -2
- package/dist/chunk-72XGFZCE.mjs +163 -0
- package/dist/chunk-72XGFZCE.mjs.map +1 -0
- package/dist/chunk-7LQNS2SG.mjs +797 -0
- package/dist/chunk-7LQNS2SG.mjs.map +1 -0
- package/dist/chunk-AOXGONGI.mjs +351 -0
- package/dist/chunk-AOXGONGI.mjs.map +1 -0
- package/dist/client-BqSJQ9mZ.d.mts +183 -0
- package/dist/client-BqSJQ9mZ.d.ts +183 -0
- package/dist/client.d.mts +2 -64
- package/dist/client.d.ts +2 -64
- package/dist/client.js +88 -4
- package/dist/client.js.map +1 -1
- package/dist/client.mjs +11 -3
- package/dist/handler.d.mts +113 -14
- package/dist/handler.d.ts +113 -14
- package/dist/handler.js +823 -6
- package/dist/handler.js.map +1 -1
- package/dist/handler.mjs +10 -3
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1059 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +167 -8
- package/dist/index.mjs.map +1 -1
- package/dist/queueJobStore.d.mts +53 -0
- package/dist/queueJobStore.d.ts +53 -0
- package/dist/queueJobStore.js +378 -0
- package/dist/queueJobStore.js.map +1 -0
- package/dist/queueJobStore.mjs +14 -0
- package/dist/queueJobStore.mjs.map +1 -0
- package/package.json +9 -2
- package/dist/chunk-FQCZSXDI.mjs +0 -83
- package/dist/chunk-FQCZSXDI.mjs.map +0 -1
- package/dist/chunk-WVR4JVWK.mjs +0 -285
- package/dist/chunk-WVR4JVWK.mjs.map +0 -1
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { ZodType, z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Queue definition and context types for worker queues.
|
|
5
|
+
*
|
|
6
|
+
* These types are used at code-time by .queue.ts files and at runtime
|
|
7
|
+
* by the client and generated registry/queue wrappers.
|
|
8
|
+
*/
|
|
9
|
+
interface WorkerQueueStep {
|
|
10
|
+
/** Worker ID for this step. Must match an existing worker id. */
|
|
11
|
+
workerId: string;
|
|
12
|
+
/**
|
|
13
|
+
* Optional delay (in seconds) before this step is executed.
|
|
14
|
+
* Implemented via SQS DelaySeconds (0–900).
|
|
15
|
+
*/
|
|
16
|
+
delaySeconds?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Optional name of a mapping function exported from the .queue.ts file.
|
|
19
|
+
* The function is called with (initialInput, previousOutputs):
|
|
20
|
+
* - initialInput: original input passed to dispatchQueue (always first, for best DX).
|
|
21
|
+
* - previousOutputs: array of { stepIndex, workerId, output } for steps 0..current-1.
|
|
22
|
+
* Use any prior step's output; the immediate previous step is previousOutputs[previousOutputs.length - 1]?.output.
|
|
23
|
+
*/
|
|
24
|
+
mapInputFromPrev?: string;
|
|
25
|
+
}
|
|
26
|
+
interface WorkerQueueConfig<InitialInput = any, StepOutput = any> {
|
|
27
|
+
/** Stable queue identifier, e.g. "cost-usage". */
|
|
28
|
+
id: string;
|
|
29
|
+
/** Ordered list of workers forming the queue. */
|
|
30
|
+
steps: WorkerQueueStep[];
|
|
31
|
+
/**
|
|
32
|
+
* Optional schedule for the queue (cron or rate).
|
|
33
|
+
* When set, the CLI generates a queue-starter Lambda triggered by this schedule.
|
|
34
|
+
* Example: 'cron(0 3 * * ? *)' for daily at 03:00 UTC.
|
|
35
|
+
*/
|
|
36
|
+
schedule?: string | {
|
|
37
|
+
rate: string;
|
|
38
|
+
enabled?: boolean;
|
|
39
|
+
input?: Record<string, any>;
|
|
40
|
+
};
|
|
41
|
+
_initialInputType?: InitialInput;
|
|
42
|
+
_stepOutputType?: StepOutput;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Queue execution context that is embedded into job input/metadata so
|
|
46
|
+
* queue-aware wrappers can determine where they are in the queue.
|
|
47
|
+
*/
|
|
48
|
+
interface WorkerQueueContext<InitialInput = any> {
|
|
49
|
+
id: string;
|
|
50
|
+
stepIndex: number;
|
|
51
|
+
initialInput: InitialInput;
|
|
52
|
+
/** Queue job ID (same as first worker's jobId) for tracking progress. */
|
|
53
|
+
queueJobId?: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Identity helper for defining worker queues in .queue.ts files.
|
|
57
|
+
* This is primarily for type safety and CLI discovery.
|
|
58
|
+
*/
|
|
59
|
+
declare function defineWorkerQueue<T extends WorkerQueueConfig>(config: T): T;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Client for dispatching background worker jobs.
|
|
63
|
+
*
|
|
64
|
+
* In production, dispatching happens via the workers HTTP API:
|
|
65
|
+
* POST /workers/trigger -> enqueues message to SQS on the workers service side
|
|
66
|
+
*
|
|
67
|
+
* This avoids requiring AWS credentials in your Next.js app.
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
interface WorkerQueueRegistry {
|
|
71
|
+
getQueueById(queueId: string): WorkerQueueConfig | undefined;
|
|
72
|
+
/** (initialInput, previousOutputs) for best DX: derive next input from original request and all prior step outputs. */
|
|
73
|
+
invokeMapInput?: (queueId: string, stepIndex: number, initialInput: unknown, previousOutputs: Array<{
|
|
74
|
+
stepIndex: number;
|
|
75
|
+
workerId: string;
|
|
76
|
+
output: unknown;
|
|
77
|
+
}>) => Promise<unknown> | unknown;
|
|
78
|
+
}
|
|
79
|
+
interface DispatchOptions {
|
|
80
|
+
/**
|
|
81
|
+
* Optional webhook callback URL to notify when the job finishes.
|
|
82
|
+
* Only called when provided. Default: no webhook (use job store / MongoDB only).
|
|
83
|
+
*/
|
|
84
|
+
webhookUrl?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Controls how dispatch executes.
|
|
87
|
+
* - "auto" (default): local inline execution in development unless WORKERS_LOCAL_MODE=false.
|
|
88
|
+
* - "local": force inline execution (no SQS).
|
|
89
|
+
* - "remote": force SQS/Lambda dispatch even in development.
|
|
90
|
+
*/
|
|
91
|
+
mode?: 'auto' | 'local' | 'remote';
|
|
92
|
+
jobId?: string;
|
|
93
|
+
metadata?: Record<string, any>;
|
|
94
|
+
/**
|
|
95
|
+
* In-memory queue registry for dispatchQueue. Required when using dispatchQueue.
|
|
96
|
+
* Pass a registry that imports from your .queue.ts definitions (works on Vercel/serverless).
|
|
97
|
+
*/
|
|
98
|
+
registry?: WorkerQueueRegistry;
|
|
99
|
+
/**
|
|
100
|
+
* Optional callback to create a queue job record before dispatching.
|
|
101
|
+
* Called with queueJobId (= first worker's jobId), queueId, and firstStep.
|
|
102
|
+
*/
|
|
103
|
+
onCreateQueueJob?: (params: {
|
|
104
|
+
queueJobId: string;
|
|
105
|
+
queueId: string;
|
|
106
|
+
firstStep: {
|
|
107
|
+
workerId: string;
|
|
108
|
+
workerJobId: string;
|
|
109
|
+
};
|
|
110
|
+
metadata?: Record<string, unknown>;
|
|
111
|
+
}) => Promise<void>;
|
|
112
|
+
}
|
|
113
|
+
interface DispatchResult {
|
|
114
|
+
messageId: string;
|
|
115
|
+
status: 'queued';
|
|
116
|
+
jobId: string;
|
|
117
|
+
}
|
|
118
|
+
interface DispatchQueueResult extends DispatchResult {
|
|
119
|
+
queueId: string;
|
|
120
|
+
}
|
|
121
|
+
interface SerializedContext {
|
|
122
|
+
requestId?: string;
|
|
123
|
+
userId?: string;
|
|
124
|
+
traceId?: string;
|
|
125
|
+
[key: string]: any;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Derives the full /workers/trigger URL from env.
|
|
129
|
+
* Exported for use by local dispatchWorker (worker-to-worker in dev).
|
|
130
|
+
* Server-side only; clients should use useWorkflowJob with your app's /api/workflows routes.
|
|
131
|
+
*
|
|
132
|
+
* Env vars:
|
|
133
|
+
* - WORKER_BASE_URL: base URL of the workers service (e.g. https://.../prod)
|
|
134
|
+
* - WORKERS_TRIGGER_API_URL / WORKERS_CONFIG_API_URL: legacy, still supported
|
|
135
|
+
*/
|
|
136
|
+
declare function getWorkersTriggerUrl(): string;
|
|
137
|
+
/**
|
|
138
|
+
* URL for the queue start endpoint (dispatch proxy). Use this so queue starts
|
|
139
|
+
* go through the queue handler Lambda for easier debugging (one log stream per queue).
|
|
140
|
+
*/
|
|
141
|
+
declare function getQueueStartUrl(queueId: string): string;
|
|
142
|
+
/**
|
|
143
|
+
* Dispatches a background worker job to SQS.
|
|
144
|
+
*
|
|
145
|
+
* @param workerId - The ID of the worker to dispatch
|
|
146
|
+
* @param input - The input data for the worker (will be validated against inputSchema)
|
|
147
|
+
* @param inputSchema - Zod schema for input validation
|
|
148
|
+
* @param options - Dispatch options including webhook URL
|
|
149
|
+
* @param ctx - Optional context object (only serializable parts will be sent)
|
|
150
|
+
* @returns Promise resolving to dispatch result with messageId and jobId
|
|
151
|
+
*/
|
|
152
|
+
declare function dispatch<INPUT_SCHEMA extends ZodType<any>>(workerId: string, input: z.input<INPUT_SCHEMA>, inputSchema: INPUT_SCHEMA, options: DispatchOptions, ctx?: any): Promise<DispatchResult>;
|
|
153
|
+
/**
|
|
154
|
+
* Dispatch a worker by ID without importing the worker module.
|
|
155
|
+
* Sends to the workers trigger API (WORKER_BASE_URL). No input schema validation at call site.
|
|
156
|
+
*
|
|
157
|
+
* @param workerId - The worker ID (e.g. 'echo', 'data-processor')
|
|
158
|
+
* @param input - Input payload (object or undefined)
|
|
159
|
+
* @param options - Optional jobId, webhookUrl, metadata
|
|
160
|
+
* @param ctx - Optional context (serializable parts sent in the request)
|
|
161
|
+
* @returns Promise resolving to { messageId, status: 'queued', jobId }
|
|
162
|
+
*/
|
|
163
|
+
declare function dispatchWorker(workerId: string, input?: Record<string, unknown>, options?: DispatchOptions, ctx?: any): Promise<DispatchResult>;
|
|
164
|
+
/**
|
|
165
|
+
* Local development mode: runs the handler immediately in the same process.
|
|
166
|
+
* This bypasses SQS and Lambda for faster iteration during development.
|
|
167
|
+
*
|
|
168
|
+
* @param handler - The worker handler function
|
|
169
|
+
* @param input - The input data
|
|
170
|
+
* @param ctx - The context object
|
|
171
|
+
* @returns The handler result
|
|
172
|
+
*/
|
|
173
|
+
declare function dispatchLocal<INPUT, OUTPUT>(handler: (params: {
|
|
174
|
+
input: INPUT;
|
|
175
|
+
ctx: any;
|
|
176
|
+
}) => Promise<OUTPUT>, input: INPUT, ctx?: any): Promise<OUTPUT>;
|
|
177
|
+
/**
|
|
178
|
+
* Dispatches a queue by ID. POSTs to the queue-start API; the queue-start handler creates the queue job.
|
|
179
|
+
* Pass the first worker's input directly (no registry required).
|
|
180
|
+
*/
|
|
181
|
+
declare function dispatchQueue<InitialInput = any>(queueId: string, initialInput?: InitialInput, options?: DispatchOptions, _ctx?: any): Promise<DispatchQueueResult>;
|
|
182
|
+
|
|
183
|
+
export { type DispatchOptions as D, type SerializedContext as S, type WorkerQueueRegistry as W, type DispatchResult as a, type DispatchQueueResult as b, getQueueStartUrl as c, dispatch as d, dispatchWorker as e, dispatchLocal as f, getWorkersTriggerUrl as g, dispatchQueue as h, type WorkerQueueStep as i, type WorkerQueueConfig as j, type WorkerQueueContext as k, defineWorkerQueue as l };
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { ZodType, z } from 'zod';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Queue definition and context types for worker queues.
|
|
5
|
+
*
|
|
6
|
+
* These types are used at code-time by .queue.ts files and at runtime
|
|
7
|
+
* by the client and generated registry/queue wrappers.
|
|
8
|
+
*/
|
|
9
|
+
interface WorkerQueueStep {
|
|
10
|
+
/** Worker ID for this step. Must match an existing worker id. */
|
|
11
|
+
workerId: string;
|
|
12
|
+
/**
|
|
13
|
+
* Optional delay (in seconds) before this step is executed.
|
|
14
|
+
* Implemented via SQS DelaySeconds (0–900).
|
|
15
|
+
*/
|
|
16
|
+
delaySeconds?: number;
|
|
17
|
+
/**
|
|
18
|
+
* Optional name of a mapping function exported from the .queue.ts file.
|
|
19
|
+
* The function is called with (initialInput, previousOutputs):
|
|
20
|
+
* - initialInput: original input passed to dispatchQueue (always first, for best DX).
|
|
21
|
+
* - previousOutputs: array of { stepIndex, workerId, output } for steps 0..current-1.
|
|
22
|
+
* Use any prior step's output; the immediate previous step is previousOutputs[previousOutputs.length - 1]?.output.
|
|
23
|
+
*/
|
|
24
|
+
mapInputFromPrev?: string;
|
|
25
|
+
}
|
|
26
|
+
interface WorkerQueueConfig<InitialInput = any, StepOutput = any> {
|
|
27
|
+
/** Stable queue identifier, e.g. "cost-usage". */
|
|
28
|
+
id: string;
|
|
29
|
+
/** Ordered list of workers forming the queue. */
|
|
30
|
+
steps: WorkerQueueStep[];
|
|
31
|
+
/**
|
|
32
|
+
* Optional schedule for the queue (cron or rate).
|
|
33
|
+
* When set, the CLI generates a queue-starter Lambda triggered by this schedule.
|
|
34
|
+
* Example: 'cron(0 3 * * ? *)' for daily at 03:00 UTC.
|
|
35
|
+
*/
|
|
36
|
+
schedule?: string | {
|
|
37
|
+
rate: string;
|
|
38
|
+
enabled?: boolean;
|
|
39
|
+
input?: Record<string, any>;
|
|
40
|
+
};
|
|
41
|
+
_initialInputType?: InitialInput;
|
|
42
|
+
_stepOutputType?: StepOutput;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Queue execution context that is embedded into job input/metadata so
|
|
46
|
+
* queue-aware wrappers can determine where they are in the queue.
|
|
47
|
+
*/
|
|
48
|
+
interface WorkerQueueContext<InitialInput = any> {
|
|
49
|
+
id: string;
|
|
50
|
+
stepIndex: number;
|
|
51
|
+
initialInput: InitialInput;
|
|
52
|
+
/** Queue job ID (same as first worker's jobId) for tracking progress. */
|
|
53
|
+
queueJobId?: string;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Identity helper for defining worker queues in .queue.ts files.
|
|
57
|
+
* This is primarily for type safety and CLI discovery.
|
|
58
|
+
*/
|
|
59
|
+
declare function defineWorkerQueue<T extends WorkerQueueConfig>(config: T): T;
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Client for dispatching background worker jobs.
|
|
63
|
+
*
|
|
64
|
+
* In production, dispatching happens via the workers HTTP API:
|
|
65
|
+
* POST /workers/trigger -> enqueues message to SQS on the workers service side
|
|
66
|
+
*
|
|
67
|
+
* This avoids requiring AWS credentials in your Next.js app.
|
|
68
|
+
*/
|
|
69
|
+
|
|
70
|
+
interface WorkerQueueRegistry {
|
|
71
|
+
getQueueById(queueId: string): WorkerQueueConfig | undefined;
|
|
72
|
+
/** (initialInput, previousOutputs) for best DX: derive next input from original request and all prior step outputs. */
|
|
73
|
+
invokeMapInput?: (queueId: string, stepIndex: number, initialInput: unknown, previousOutputs: Array<{
|
|
74
|
+
stepIndex: number;
|
|
75
|
+
workerId: string;
|
|
76
|
+
output: unknown;
|
|
77
|
+
}>) => Promise<unknown> | unknown;
|
|
78
|
+
}
|
|
79
|
+
interface DispatchOptions {
|
|
80
|
+
/**
|
|
81
|
+
* Optional webhook callback URL to notify when the job finishes.
|
|
82
|
+
* Only called when provided. Default: no webhook (use job store / MongoDB only).
|
|
83
|
+
*/
|
|
84
|
+
webhookUrl?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Controls how dispatch executes.
|
|
87
|
+
* - "auto" (default): local inline execution in development unless WORKERS_LOCAL_MODE=false.
|
|
88
|
+
* - "local": force inline execution (no SQS).
|
|
89
|
+
* - "remote": force SQS/Lambda dispatch even in development.
|
|
90
|
+
*/
|
|
91
|
+
mode?: 'auto' | 'local' | 'remote';
|
|
92
|
+
jobId?: string;
|
|
93
|
+
metadata?: Record<string, any>;
|
|
94
|
+
/**
|
|
95
|
+
* In-memory queue registry for dispatchQueue. Required when using dispatchQueue.
|
|
96
|
+
* Pass a registry that imports from your .queue.ts definitions (works on Vercel/serverless).
|
|
97
|
+
*/
|
|
98
|
+
registry?: WorkerQueueRegistry;
|
|
99
|
+
/**
|
|
100
|
+
* Optional callback to create a queue job record before dispatching.
|
|
101
|
+
* Called with queueJobId (= first worker's jobId), queueId, and firstStep.
|
|
102
|
+
*/
|
|
103
|
+
onCreateQueueJob?: (params: {
|
|
104
|
+
queueJobId: string;
|
|
105
|
+
queueId: string;
|
|
106
|
+
firstStep: {
|
|
107
|
+
workerId: string;
|
|
108
|
+
workerJobId: string;
|
|
109
|
+
};
|
|
110
|
+
metadata?: Record<string, unknown>;
|
|
111
|
+
}) => Promise<void>;
|
|
112
|
+
}
|
|
113
|
+
interface DispatchResult {
|
|
114
|
+
messageId: string;
|
|
115
|
+
status: 'queued';
|
|
116
|
+
jobId: string;
|
|
117
|
+
}
|
|
118
|
+
interface DispatchQueueResult extends DispatchResult {
|
|
119
|
+
queueId: string;
|
|
120
|
+
}
|
|
121
|
+
interface SerializedContext {
|
|
122
|
+
requestId?: string;
|
|
123
|
+
userId?: string;
|
|
124
|
+
traceId?: string;
|
|
125
|
+
[key: string]: any;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Derives the full /workers/trigger URL from env.
|
|
129
|
+
* Exported for use by local dispatchWorker (worker-to-worker in dev).
|
|
130
|
+
* Server-side only; clients should use useWorkflowJob with your app's /api/workflows routes.
|
|
131
|
+
*
|
|
132
|
+
* Env vars:
|
|
133
|
+
* - WORKER_BASE_URL: base URL of the workers service (e.g. https://.../prod)
|
|
134
|
+
* - WORKERS_TRIGGER_API_URL / WORKERS_CONFIG_API_URL: legacy, still supported
|
|
135
|
+
*/
|
|
136
|
+
declare function getWorkersTriggerUrl(): string;
|
|
137
|
+
/**
|
|
138
|
+
* URL for the queue start endpoint (dispatch proxy). Use this so queue starts
|
|
139
|
+
* go through the queue handler Lambda for easier debugging (one log stream per queue).
|
|
140
|
+
*/
|
|
141
|
+
declare function getQueueStartUrl(queueId: string): string;
|
|
142
|
+
/**
|
|
143
|
+
* Dispatches a background worker job to SQS.
|
|
144
|
+
*
|
|
145
|
+
* @param workerId - The ID of the worker to dispatch
|
|
146
|
+
* @param input - The input data for the worker (will be validated against inputSchema)
|
|
147
|
+
* @param inputSchema - Zod schema for input validation
|
|
148
|
+
* @param options - Dispatch options including webhook URL
|
|
149
|
+
* @param ctx - Optional context object (only serializable parts will be sent)
|
|
150
|
+
* @returns Promise resolving to dispatch result with messageId and jobId
|
|
151
|
+
*/
|
|
152
|
+
declare function dispatch<INPUT_SCHEMA extends ZodType<any>>(workerId: string, input: z.input<INPUT_SCHEMA>, inputSchema: INPUT_SCHEMA, options: DispatchOptions, ctx?: any): Promise<DispatchResult>;
|
|
153
|
+
/**
|
|
154
|
+
* Dispatch a worker by ID without importing the worker module.
|
|
155
|
+
* Sends to the workers trigger API (WORKER_BASE_URL). No input schema validation at call site.
|
|
156
|
+
*
|
|
157
|
+
* @param workerId - The worker ID (e.g. 'echo', 'data-processor')
|
|
158
|
+
* @param input - Input payload (object or undefined)
|
|
159
|
+
* @param options - Optional jobId, webhookUrl, metadata
|
|
160
|
+
* @param ctx - Optional context (serializable parts sent in the request)
|
|
161
|
+
* @returns Promise resolving to { messageId, status: 'queued', jobId }
|
|
162
|
+
*/
|
|
163
|
+
declare function dispatchWorker(workerId: string, input?: Record<string, unknown>, options?: DispatchOptions, ctx?: any): Promise<DispatchResult>;
|
|
164
|
+
/**
|
|
165
|
+
* Local development mode: runs the handler immediately in the same process.
|
|
166
|
+
* This bypasses SQS and Lambda for faster iteration during development.
|
|
167
|
+
*
|
|
168
|
+
* @param handler - The worker handler function
|
|
169
|
+
* @param input - The input data
|
|
170
|
+
* @param ctx - The context object
|
|
171
|
+
* @returns The handler result
|
|
172
|
+
*/
|
|
173
|
+
declare function dispatchLocal<INPUT, OUTPUT>(handler: (params: {
|
|
174
|
+
input: INPUT;
|
|
175
|
+
ctx: any;
|
|
176
|
+
}) => Promise<OUTPUT>, input: INPUT, ctx?: any): Promise<OUTPUT>;
|
|
177
|
+
/**
|
|
178
|
+
* Dispatches a queue by ID. POSTs to the queue-start API; the queue-start handler creates the queue job.
|
|
179
|
+
* Pass the first worker's input directly (no registry required).
|
|
180
|
+
*/
|
|
181
|
+
declare function dispatchQueue<InitialInput = any>(queueId: string, initialInput?: InitialInput, options?: DispatchOptions, _ctx?: any): Promise<DispatchQueueResult>;
|
|
182
|
+
|
|
183
|
+
export { type DispatchOptions as D, type SerializedContext as S, type WorkerQueueRegistry as W, type DispatchResult as a, type DispatchQueueResult as b, getQueueStartUrl as c, dispatch as d, dispatchWorker as e, dispatchLocal as f, getWorkersTriggerUrl as g, dispatchQueue as h, type WorkerQueueStep as i, type WorkerQueueConfig as j, type WorkerQueueContext as k, defineWorkerQueue as l };
|
package/dist/client.d.mts
CHANGED
|
@@ -1,64 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Client for dispatching background worker jobs.
|
|
5
|
-
*
|
|
6
|
-
* In production, dispatching happens via the workers HTTP API:
|
|
7
|
-
* POST /workers/trigger -> enqueues message to SQS on the workers service side
|
|
8
|
-
*
|
|
9
|
-
* This avoids requiring AWS credentials in your Next.js app.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
interface DispatchOptions {
|
|
13
|
-
/**
|
|
14
|
-
* Optional webhook callback URL to notify when the job finishes.
|
|
15
|
-
* Only called when provided. Default: no webhook (use job store / MongoDB only).
|
|
16
|
-
*/
|
|
17
|
-
webhookUrl?: string;
|
|
18
|
-
/**
|
|
19
|
-
* Controls how dispatch executes.
|
|
20
|
-
* - "auto" (default): local inline execution in development unless WORKERS_LOCAL_MODE=false.
|
|
21
|
-
* - "local": force inline execution (no SQS).
|
|
22
|
-
* - "remote": force SQS/Lambda dispatch even in development.
|
|
23
|
-
*/
|
|
24
|
-
mode?: 'auto' | 'local' | 'remote';
|
|
25
|
-
jobId?: string;
|
|
26
|
-
metadata?: Record<string, any>;
|
|
27
|
-
}
|
|
28
|
-
interface DispatchResult {
|
|
29
|
-
messageId: string;
|
|
30
|
-
status: 'queued';
|
|
31
|
-
jobId: string;
|
|
32
|
-
}
|
|
33
|
-
interface SerializedContext {
|
|
34
|
-
requestId?: string;
|
|
35
|
-
userId?: string;
|
|
36
|
-
traceId?: string;
|
|
37
|
-
[key: string]: any;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Dispatches a background worker job to SQS.
|
|
41
|
-
*
|
|
42
|
-
* @param workerId - The ID of the worker to dispatch
|
|
43
|
-
* @param input - The input data for the worker (will be validated against inputSchema)
|
|
44
|
-
* @param inputSchema - Zod schema for input validation
|
|
45
|
-
* @param options - Dispatch options including webhook URL
|
|
46
|
-
* @param ctx - Optional context object (only serializable parts will be sent)
|
|
47
|
-
* @returns Promise resolving to dispatch result with messageId and jobId
|
|
48
|
-
*/
|
|
49
|
-
declare function dispatch<INPUT_SCHEMA extends ZodType<any>>(workerId: string, input: z.input<INPUT_SCHEMA>, inputSchema: INPUT_SCHEMA, options: DispatchOptions, ctx?: any): Promise<DispatchResult>;
|
|
50
|
-
/**
|
|
51
|
-
* Local development mode: runs the handler immediately in the same process.
|
|
52
|
-
* This bypasses SQS and Lambda for faster iteration during development.
|
|
53
|
-
*
|
|
54
|
-
* @param handler - The worker handler function
|
|
55
|
-
* @param input - The input data
|
|
56
|
-
* @param ctx - The context object
|
|
57
|
-
* @returns The handler result
|
|
58
|
-
*/
|
|
59
|
-
declare function dispatchLocal<INPUT, OUTPUT>(handler: (params: {
|
|
60
|
-
input: INPUT;
|
|
61
|
-
ctx: any;
|
|
62
|
-
}) => Promise<OUTPUT>, input: INPUT, ctx?: any): Promise<OUTPUT>;
|
|
63
|
-
|
|
64
|
-
export { type DispatchOptions, type DispatchResult, type SerializedContext, dispatch, dispatchLocal };
|
|
1
|
+
import 'zod';
|
|
2
|
+
export { D as DispatchOptions, b as DispatchQueueResult, a as DispatchResult, S as SerializedContext, W as WorkerQueueRegistry, d as dispatch, f as dispatchLocal, h as dispatchQueue, e as dispatchWorker, c as getQueueStartUrl, g as getWorkersTriggerUrl } from './client-BqSJQ9mZ.mjs';
|
package/dist/client.d.ts
CHANGED
|
@@ -1,64 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Client for dispatching background worker jobs.
|
|
5
|
-
*
|
|
6
|
-
* In production, dispatching happens via the workers HTTP API:
|
|
7
|
-
* POST /workers/trigger -> enqueues message to SQS on the workers service side
|
|
8
|
-
*
|
|
9
|
-
* This avoids requiring AWS credentials in your Next.js app.
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
interface DispatchOptions {
|
|
13
|
-
/**
|
|
14
|
-
* Optional webhook callback URL to notify when the job finishes.
|
|
15
|
-
* Only called when provided. Default: no webhook (use job store / MongoDB only).
|
|
16
|
-
*/
|
|
17
|
-
webhookUrl?: string;
|
|
18
|
-
/**
|
|
19
|
-
* Controls how dispatch executes.
|
|
20
|
-
* - "auto" (default): local inline execution in development unless WORKERS_LOCAL_MODE=false.
|
|
21
|
-
* - "local": force inline execution (no SQS).
|
|
22
|
-
* - "remote": force SQS/Lambda dispatch even in development.
|
|
23
|
-
*/
|
|
24
|
-
mode?: 'auto' | 'local' | 'remote';
|
|
25
|
-
jobId?: string;
|
|
26
|
-
metadata?: Record<string, any>;
|
|
27
|
-
}
|
|
28
|
-
interface DispatchResult {
|
|
29
|
-
messageId: string;
|
|
30
|
-
status: 'queued';
|
|
31
|
-
jobId: string;
|
|
32
|
-
}
|
|
33
|
-
interface SerializedContext {
|
|
34
|
-
requestId?: string;
|
|
35
|
-
userId?: string;
|
|
36
|
-
traceId?: string;
|
|
37
|
-
[key: string]: any;
|
|
38
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Dispatches a background worker job to SQS.
|
|
41
|
-
*
|
|
42
|
-
* @param workerId - The ID of the worker to dispatch
|
|
43
|
-
* @param input - The input data for the worker (will be validated against inputSchema)
|
|
44
|
-
* @param inputSchema - Zod schema for input validation
|
|
45
|
-
* @param options - Dispatch options including webhook URL
|
|
46
|
-
* @param ctx - Optional context object (only serializable parts will be sent)
|
|
47
|
-
* @returns Promise resolving to dispatch result with messageId and jobId
|
|
48
|
-
*/
|
|
49
|
-
declare function dispatch<INPUT_SCHEMA extends ZodType<any>>(workerId: string, input: z.input<INPUT_SCHEMA>, inputSchema: INPUT_SCHEMA, options: DispatchOptions, ctx?: any): Promise<DispatchResult>;
|
|
50
|
-
/**
|
|
51
|
-
* Local development mode: runs the handler immediately in the same process.
|
|
52
|
-
* This bypasses SQS and Lambda for faster iteration during development.
|
|
53
|
-
*
|
|
54
|
-
* @param handler - The worker handler function
|
|
55
|
-
* @param input - The input data
|
|
56
|
-
* @param ctx - The context object
|
|
57
|
-
* @returns The handler result
|
|
58
|
-
*/
|
|
59
|
-
declare function dispatchLocal<INPUT, OUTPUT>(handler: (params: {
|
|
60
|
-
input: INPUT;
|
|
61
|
-
ctx: any;
|
|
62
|
-
}) => Promise<OUTPUT>, input: INPUT, ctx?: any): Promise<OUTPUT>;
|
|
63
|
-
|
|
64
|
-
export { type DispatchOptions, type DispatchResult, type SerializedContext, dispatch, dispatchLocal };
|
|
1
|
+
import 'zod';
|
|
2
|
+
export { D as DispatchOptions, b as DispatchQueueResult, a as DispatchResult, S as SerializedContext, W as WorkerQueueRegistry, d as dispatch, f as dispatchLocal, h as dispatchQueue, e as dispatchWorker, c as getQueueStartUrl, g as getWorkersTriggerUrl } from './client-BqSJQ9mZ.js';
|
package/dist/client.js
CHANGED
|
@@ -21,14 +21,18 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var client_exports = {};
|
|
22
22
|
__export(client_exports, {
|
|
23
23
|
dispatch: () => dispatch,
|
|
24
|
-
dispatchLocal: () => dispatchLocal
|
|
24
|
+
dispatchLocal: () => dispatchLocal,
|
|
25
|
+
dispatchQueue: () => dispatchQueue,
|
|
26
|
+
dispatchWorker: () => dispatchWorker,
|
|
27
|
+
getQueueStartUrl: () => getQueueStartUrl,
|
|
28
|
+
getWorkersTriggerUrl: () => getWorkersTriggerUrl
|
|
25
29
|
});
|
|
26
30
|
module.exports = __toCommonJS(client_exports);
|
|
27
31
|
function getWorkersTriggerUrl() {
|
|
28
|
-
const raw = process.env.WORKER_BASE_URL || process.env.
|
|
32
|
+
const raw = process.env.WORKER_BASE_URL || process.env.WORKERS_TRIGGER_API_URL || process.env.WORKERS_CONFIG_API_URL;
|
|
29
33
|
if (!raw) {
|
|
30
34
|
throw new Error(
|
|
31
|
-
"WORKER_BASE_URL
|
|
35
|
+
"WORKER_BASE_URL is required for background workers. Set it server-side only."
|
|
32
36
|
);
|
|
33
37
|
}
|
|
34
38
|
const url = new URL(raw);
|
|
@@ -40,6 +44,23 @@ function getWorkersTriggerUrl() {
|
|
|
40
44
|
url.pathname = `${basePath}/workers/trigger`.replace(/\/+$/, "");
|
|
41
45
|
return url.toString();
|
|
42
46
|
}
|
|
47
|
+
function getQueueStartUrl(queueId) {
|
|
48
|
+
const raw = process.env.WORKER_BASE_URL || process.env.WORKERS_TRIGGER_API_URL || process.env.WORKERS_CONFIG_API_URL;
|
|
49
|
+
if (!raw) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
"WORKER_BASE_URL is required for background workers. Set it server-side only."
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
const url = new URL(raw);
|
|
55
|
+
url.search = "";
|
|
56
|
+
url.hash = "";
|
|
57
|
+
const path = url.pathname || "";
|
|
58
|
+
url.pathname = path.replace(/\/?workers\/(trigger|config)\/?$/, "");
|
|
59
|
+
const basePath = url.pathname.replace(/\/+$/, "");
|
|
60
|
+
const safeSegment = encodeURIComponent(queueId);
|
|
61
|
+
url.pathname = `${basePath}/queues/${safeSegment}/start`.replace(/\/+$/, "");
|
|
62
|
+
return url.toString();
|
|
63
|
+
}
|
|
43
64
|
function serializeContext(ctx) {
|
|
44
65
|
const serialized = {};
|
|
45
66
|
if (ctx.requestId) {
|
|
@@ -97,12 +118,75 @@ async function dispatch(workerId, input, inputSchema, options, ctx) {
|
|
|
97
118
|
jobId
|
|
98
119
|
};
|
|
99
120
|
}
|
|
121
|
+
async function dispatchWorker(workerId, input, options = {}, ctx) {
|
|
122
|
+
const jobId = options.jobId || `job-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
123
|
+
const triggerUrl = getWorkersTriggerUrl();
|
|
124
|
+
const serializedContext = ctx ? serializeContext(ctx) : {};
|
|
125
|
+
const messageBody = {
|
|
126
|
+
workerId,
|
|
127
|
+
jobId,
|
|
128
|
+
input: input ?? {},
|
|
129
|
+
context: serializedContext,
|
|
130
|
+
webhookUrl: options.webhookUrl,
|
|
131
|
+
metadata: options.metadata || {},
|
|
132
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
133
|
+
};
|
|
134
|
+
const headers = { "Content-Type": "application/json" };
|
|
135
|
+
const triggerKey = process.env.WORKERS_TRIGGER_API_KEY;
|
|
136
|
+
if (triggerKey) headers["x-workers-trigger-key"] = triggerKey;
|
|
137
|
+
const response = await fetch(triggerUrl, {
|
|
138
|
+
method: "POST",
|
|
139
|
+
headers,
|
|
140
|
+
body: JSON.stringify({ workerId, body: messageBody })
|
|
141
|
+
});
|
|
142
|
+
if (!response.ok) {
|
|
143
|
+
const text = await response.text().catch(() => "");
|
|
144
|
+
throw new Error(
|
|
145
|
+
`Failed to trigger worker "${workerId}": ${response.status} ${response.statusText}${text ? ` - ${text}` : ""}`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
const data = await response.json().catch(() => ({}));
|
|
149
|
+
const messageId = data?.messageId ? String(data.messageId) : `trigger-${jobId}`;
|
|
150
|
+
return { messageId, status: "queued", jobId };
|
|
151
|
+
}
|
|
100
152
|
async function dispatchLocal(handler, input, ctx) {
|
|
101
153
|
return handler({ input, ctx: ctx || {} });
|
|
102
154
|
}
|
|
155
|
+
async function dispatchQueue(queueId, initialInput, options = {}, _ctx) {
|
|
156
|
+
const jobId = options.jobId || `job-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
157
|
+
const queueStartUrl = getQueueStartUrl(queueId);
|
|
158
|
+
const normalizedInput = initialInput !== null && typeof initialInput === "object" ? initialInput : { value: initialInput };
|
|
159
|
+
const headers = { "Content-Type": "application/json" };
|
|
160
|
+
const triggerKey = process.env.WORKERS_TRIGGER_API_KEY;
|
|
161
|
+
if (triggerKey) headers["x-workers-trigger-key"] = triggerKey;
|
|
162
|
+
const response = await fetch(queueStartUrl, {
|
|
163
|
+
method: "POST",
|
|
164
|
+
headers,
|
|
165
|
+
body: JSON.stringify({
|
|
166
|
+
input: normalizedInput,
|
|
167
|
+
initialInput: normalizedInput,
|
|
168
|
+
metadata: options.metadata ?? {},
|
|
169
|
+
jobId,
|
|
170
|
+
...options.webhookUrl ? { webhookUrl: options.webhookUrl } : {}
|
|
171
|
+
})
|
|
172
|
+
});
|
|
173
|
+
if (!response.ok) {
|
|
174
|
+
const text = await response.text().catch(() => "");
|
|
175
|
+
throw new Error(
|
|
176
|
+
`Failed to start queue "${queueId}": ${response.status} ${response.statusText}${text ? ` - ${text}` : ""}`
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
const data = await response.json().catch(() => ({}));
|
|
180
|
+
const messageId = data?.messageId ?? data?.jobId ?? `queue-${jobId}`;
|
|
181
|
+
return { queueId, messageId, status: "queued", jobId };
|
|
182
|
+
}
|
|
103
183
|
// Annotate the CommonJS export names for ESM import in node:
|
|
104
184
|
0 && (module.exports = {
|
|
105
185
|
dispatch,
|
|
106
|
-
dispatchLocal
|
|
186
|
+
dispatchLocal,
|
|
187
|
+
dispatchQueue,
|
|
188
|
+
dispatchWorker,
|
|
189
|
+
getQueueStartUrl,
|
|
190
|
+
getWorkersTriggerUrl
|
|
107
191
|
});
|
|
108
192
|
//# sourceMappingURL=client.js.map
|