@microfox/ai-worker 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/README.md +185 -0
- package/dist/chunk-BJTO5JO5.mjs +11 -0
- package/dist/chunk-BJTO5JO5.mjs.map +1 -0
- package/dist/chunk-FQCZSXDI.mjs +83 -0
- package/dist/chunk-FQCZSXDI.mjs.map +1 -0
- package/dist/chunk-WVR4JVWK.mjs +285 -0
- package/dist/chunk-WVR4JVWK.mjs.map +1 -0
- package/dist/chunk-ZYYWZ3PR.mjs +50 -0
- package/dist/chunk-ZYYWZ3PR.mjs.map +1 -0
- package/dist/client.d.mts +64 -0
- package/dist/client.d.ts +64 -0
- package/dist/client.js +108 -0
- package/dist/client.js.map +1 -0
- package/dist/client.mjs +10 -0
- package/dist/client.mjs.map +1 -0
- package/dist/config.d.mts +38 -0
- package/dist/config.d.ts +38 -0
- package/dist/config.js +76 -0
- package/dist/config.js.map +1 -0
- package/dist/config.mjs +12 -0
- package/dist/config.mjs.map +1 -0
- package/dist/handler.d.mts +96 -0
- package/dist/handler.d.ts +96 -0
- package/dist/handler.js +311 -0
- package/dist/handler.js.map +1 -0
- package/dist/handler.mjs +8 -0
- package/dist/handler.mjs.map +1 -0
- package/dist/index.d.mts +236 -0
- package/dist/index.d.ts +236 -0
- package/dist/index.js +734 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +313 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +73 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import * as aws_lambda from 'aws-lambda';
|
|
2
|
+
import { DispatchOptions, DispatchResult } from './client.mjs';
|
|
3
|
+
export { SerializedContext, dispatch, dispatchLocal } from './client.mjs';
|
|
4
|
+
import { WorkerHandler } from './handler.mjs';
|
|
5
|
+
export { JobStore, JobStoreUpdate, SQSMessageBody, WebhookPayload, WorkerHandlerParams, createLambdaHandler } from './handler.mjs';
|
|
6
|
+
import { ZodType, z } from 'zod';
|
|
7
|
+
export { WorkersConfig, clearWorkersConfigCache, getWorkersConfig, resolveQueueUrl } from './config.mjs';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Schedule event configuration for a worker.
|
|
11
|
+
* Supports both simple rate/cron strings and full configuration objects.
|
|
12
|
+
*
|
|
13
|
+
* @example Simple rate/cron
|
|
14
|
+
* ```typescript
|
|
15
|
+
* schedule: 'rate(2 hours)'
|
|
16
|
+
* // or
|
|
17
|
+
* schedule: 'cron(0 12 * * ? *)'
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example Full configuration
|
|
21
|
+
* ```typescript
|
|
22
|
+
* schedule: {
|
|
23
|
+
* rate: 'rate(10 minutes)',
|
|
24
|
+
* enabled: true,
|
|
25
|
+
* input: { key1: 'value1' }
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Multiple schedules
|
|
30
|
+
* ```typescript
|
|
31
|
+
* schedule: [
|
|
32
|
+
* 'rate(2 hours)',
|
|
33
|
+
* { rate: 'cron(0 12 * * ? *)', enabled: false }
|
|
34
|
+
* ]
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
interface ScheduleEventConfig {
|
|
38
|
+
/**
|
|
39
|
+
* Schedule rate using either rate() or cron() syntax.
|
|
40
|
+
* Can be a string or array of strings for multiple schedules.
|
|
41
|
+
*
|
|
42
|
+
* @example 'rate(2 hours)' or 'cron(0 12 * * ? *)'
|
|
43
|
+
* @example ['cron(0 0/4 ? * MON-FRI *)', 'cron(0 2 ? * SAT-SUN *)']
|
|
44
|
+
*/
|
|
45
|
+
rate: string | string[];
|
|
46
|
+
/**
|
|
47
|
+
* Whether the schedule is enabled (default: true).
|
|
48
|
+
*/
|
|
49
|
+
enabled?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Input payload to pass to the function.
|
|
52
|
+
*/
|
|
53
|
+
input?: Record<string, any>;
|
|
54
|
+
/**
|
|
55
|
+
* JSONPath expression to select part of the event data as input.
|
|
56
|
+
*/
|
|
57
|
+
inputPath?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Input transformer configuration for custom input mapping.
|
|
60
|
+
*/
|
|
61
|
+
inputTransformer?: {
|
|
62
|
+
inputPathsMap?: Record<string, string>;
|
|
63
|
+
inputTemplate?: string;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Name of the schedule event.
|
|
67
|
+
*/
|
|
68
|
+
name?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Description of the schedule event.
|
|
71
|
+
*/
|
|
72
|
+
description?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Method to use: 'eventBus' (default) or 'scheduler'.
|
|
75
|
+
* Use 'scheduler' for higher limits (1M events vs 300).
|
|
76
|
+
*/
|
|
77
|
+
method?: 'eventBus' | 'scheduler';
|
|
78
|
+
/**
|
|
79
|
+
* Timezone for the schedule (only used with method: 'scheduler').
|
|
80
|
+
* @example 'America/New_York'
|
|
81
|
+
*/
|
|
82
|
+
timezone?: string;
|
|
83
|
+
}
|
|
84
|
+
type ScheduleConfig = string | ScheduleEventConfig | (string | ScheduleEventConfig)[];
|
|
85
|
+
/**
|
|
86
|
+
* Configuration for a worker's Lambda function deployment.
|
|
87
|
+
*
|
|
88
|
+
* **Best Practice**: Export this as a separate const from your worker file:
|
|
89
|
+
* ```typescript
|
|
90
|
+
* export const workerConfig: WorkerConfig = {
|
|
91
|
+
* timeout: 900,
|
|
92
|
+
* memorySize: 2048,
|
|
93
|
+
* layers: ['arn:aws:lambda:${aws:region}:${aws:accountId}:layer:ffmpeg:1'],
|
|
94
|
+
* schedule: 'rate(2 hours)',
|
|
95
|
+
* };
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* The CLI will automatically extract it from the export. You do not need to pass it to `createWorker()`.
|
|
99
|
+
*/
|
|
100
|
+
interface WorkerConfig {
|
|
101
|
+
/**
|
|
102
|
+
* Lambda function timeout in seconds (max 900).
|
|
103
|
+
*/
|
|
104
|
+
timeout?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Lambda function memory size in MB (128-10240).
|
|
107
|
+
*/
|
|
108
|
+
memorySize?: number;
|
|
109
|
+
/**
|
|
110
|
+
* Optional Lambda layers ARNs to attach to this worker function.
|
|
111
|
+
*
|
|
112
|
+
* This is primarily used by @microfox/ai-worker-cli when generating serverless.yml.
|
|
113
|
+
* Supports CloudFormation pseudo-parameters like ${aws:region} and ${aws:accountId}.
|
|
114
|
+
*
|
|
115
|
+
* Example:
|
|
116
|
+
* layers: ['arn:aws:lambda:${aws:region}:${aws:accountId}:layer:ffmpeg:1']
|
|
117
|
+
*/
|
|
118
|
+
layers?: string[];
|
|
119
|
+
/**
|
|
120
|
+
* Schedule events configuration for this worker.
|
|
121
|
+
* Allows multiple schedule events to be attached to the same function.
|
|
122
|
+
*
|
|
123
|
+
* @example Simple rate
|
|
124
|
+
* ```typescript
|
|
125
|
+
* schedule: 'rate(2 hours)'
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @example Multiple schedules
|
|
129
|
+
* ```typescript
|
|
130
|
+
* schedule: [
|
|
131
|
+
* 'rate(2 hours)',
|
|
132
|
+
* { rate: 'cron(0 12 * * ? *)', enabled: true, input: { key: 'value' } }
|
|
133
|
+
* ]
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @example Using scheduler method with timezone
|
|
137
|
+
* ```typescript
|
|
138
|
+
* schedule: {
|
|
139
|
+
* method: 'scheduler',
|
|
140
|
+
* rate: 'cron(0 0/4 ? * MON-FRI *)',
|
|
141
|
+
* timezone: 'America/New_York',
|
|
142
|
+
* input: { key1: 'value1' }
|
|
143
|
+
* }
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
schedule?: ScheduleConfig;
|
|
147
|
+
/**
|
|
148
|
+
* SQS queue settings for this worker (used by @microfox/ai-worker-cli when generating serverless.yml).
|
|
149
|
+
*
|
|
150
|
+
* Notes:
|
|
151
|
+
* - To effectively disable retries, set `maxReceiveCount: 1` (requires DLQ; the CLI will create one).
|
|
152
|
+
* - SQS does not support `maxReceiveCount: 0`.
|
|
153
|
+
* - `messageRetentionPeriod` is in seconds (max 1209600 = 14 days).
|
|
154
|
+
*/
|
|
155
|
+
sqs?: {
|
|
156
|
+
/**
|
|
157
|
+
* How many receives before sending to DLQ.
|
|
158
|
+
* Use 1 to avoid retries.
|
|
159
|
+
*/
|
|
160
|
+
maxReceiveCount?: number;
|
|
161
|
+
/**
|
|
162
|
+
* How long messages are retained in the main queue (seconds).
|
|
163
|
+
*/
|
|
164
|
+
messageRetentionPeriod?: number;
|
|
165
|
+
/**
|
|
166
|
+
* Visibility timeout for the main queue (seconds).
|
|
167
|
+
* If not set, CLI defaults to (worker timeout + 60s).
|
|
168
|
+
*/
|
|
169
|
+
visibilityTimeout?: number;
|
|
170
|
+
/**
|
|
171
|
+
* DLQ message retention period (seconds).
|
|
172
|
+
* Defaults to `messageRetentionPeriod` (or 14 days).
|
|
173
|
+
*/
|
|
174
|
+
deadLetterMessageRetentionPeriod?: number;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
interface WorkerAgentConfig<INPUT_SCHEMA extends ZodType<any>, OUTPUT> {
|
|
178
|
+
id: string;
|
|
179
|
+
inputSchema: INPUT_SCHEMA;
|
|
180
|
+
outputSchema: ZodType<OUTPUT>;
|
|
181
|
+
handler: WorkerHandler<z.infer<INPUT_SCHEMA>, OUTPUT>;
|
|
182
|
+
/**
|
|
183
|
+
* @deprecated Prefer exporting `workerConfig` as a separate const from your worker file.
|
|
184
|
+
* The CLI will automatically extract it from the export. This parameter is kept for backward compatibility.
|
|
185
|
+
*/
|
|
186
|
+
workerConfig?: WorkerConfig;
|
|
187
|
+
}
|
|
188
|
+
interface WorkerAgent<INPUT_SCHEMA extends ZodType<any>, OUTPUT> {
|
|
189
|
+
id: string;
|
|
190
|
+
dispatch: (input: z.input<INPUT_SCHEMA>, options: DispatchOptions) => Promise<DispatchResult>;
|
|
191
|
+
handler: WorkerHandler<z.infer<INPUT_SCHEMA>, OUTPUT>;
|
|
192
|
+
inputSchema: INPUT_SCHEMA;
|
|
193
|
+
outputSchema: ZodType<OUTPUT>;
|
|
194
|
+
workerConfig?: WorkerConfig;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Creates a worker agent that can be dispatched to SQS/Lambda.
|
|
198
|
+
*
|
|
199
|
+
* In development mode (NODE_ENV === 'development' and WORKERS_LOCAL_MODE !== 'false'),
|
|
200
|
+
* dispatch() will run the handler immediately in the same process.
|
|
201
|
+
*
|
|
202
|
+
* In production, dispatch() sends a message to SQS which triggers a Lambda function.
|
|
203
|
+
*
|
|
204
|
+
* @template INPUT_SCHEMA - The Zod schema type (e.g., `typeof InputSchema`).
|
|
205
|
+
* Used to derive both:
|
|
206
|
+
* - Pre-parse input type via `z.input<INPUT_SCHEMA>` for `dispatch()` (preserves optional fields)
|
|
207
|
+
* - Parsed input type via `z.infer<INPUT_SCHEMA>` for handler (defaults applied)
|
|
208
|
+
* @template OUTPUT - The output type returned by the handler. Use `z.infer<typeof OutputSchema>`.
|
|
209
|
+
*
|
|
210
|
+
* @param config - Worker agent configuration
|
|
211
|
+
* @returns A worker agent object with a dispatch method
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* const InputSchema = z.object({
|
|
216
|
+
* url: z.string().url(),
|
|
217
|
+
* timeout: z.number().optional().default(5000), // optional with default
|
|
218
|
+
* });
|
|
219
|
+
*
|
|
220
|
+
* export const worker = createWorker<typeof InputSchema, Output>({
|
|
221
|
+
* // dispatch() accepts { url: string, timeout?: number } (pre-parse, optional preserved)
|
|
222
|
+
* // handler receives { url: string, timeout: number } (parsed, default applied)
|
|
223
|
+
* });
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
declare function createWorker<INPUT_SCHEMA extends ZodType<any>, OUTPUT>(config: WorkerAgentConfig<INPUT_SCHEMA, OUTPUT>): WorkerAgent<INPUT_SCHEMA, OUTPUT>;
|
|
227
|
+
/**
|
|
228
|
+
* Creates a Lambda handler entrypoint for a worker agent.
|
|
229
|
+
* This is used by the deployment script to generate Lambda entrypoints.
|
|
230
|
+
*
|
|
231
|
+
* @param agent - The worker agent
|
|
232
|
+
* @returns A Lambda handler function
|
|
233
|
+
*/
|
|
234
|
+
declare function createLambdaEntrypoint<INPUT_SCHEMA extends ZodType<any>, OUTPUT>(agent: WorkerAgent<INPUT_SCHEMA, OUTPUT>): (event: aws_lambda.SQSEvent, context: aws_lambda.Context) => Promise<void>;
|
|
235
|
+
|
|
236
|
+
export { DispatchOptions, DispatchResult, type ScheduleConfig, type ScheduleEventConfig, type WorkerAgent, type WorkerAgentConfig, type WorkerConfig, WorkerHandler, createLambdaEntrypoint, createWorker };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import * as aws_lambda from 'aws-lambda';
|
|
2
|
+
import { DispatchOptions, DispatchResult } from './client.js';
|
|
3
|
+
export { SerializedContext, dispatch, dispatchLocal } from './client.js';
|
|
4
|
+
import { WorkerHandler } from './handler.js';
|
|
5
|
+
export { JobStore, JobStoreUpdate, SQSMessageBody, WebhookPayload, WorkerHandlerParams, createLambdaHandler } from './handler.js';
|
|
6
|
+
import { ZodType, z } from 'zod';
|
|
7
|
+
export { WorkersConfig, clearWorkersConfigCache, getWorkersConfig, resolveQueueUrl } from './config.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Schedule event configuration for a worker.
|
|
11
|
+
* Supports both simple rate/cron strings and full configuration objects.
|
|
12
|
+
*
|
|
13
|
+
* @example Simple rate/cron
|
|
14
|
+
* ```typescript
|
|
15
|
+
* schedule: 'rate(2 hours)'
|
|
16
|
+
* // or
|
|
17
|
+
* schedule: 'cron(0 12 * * ? *)'
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* @example Full configuration
|
|
21
|
+
* ```typescript
|
|
22
|
+
* schedule: {
|
|
23
|
+
* rate: 'rate(10 minutes)',
|
|
24
|
+
* enabled: true,
|
|
25
|
+
* input: { key1: 'value1' }
|
|
26
|
+
* }
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Multiple schedules
|
|
30
|
+
* ```typescript
|
|
31
|
+
* schedule: [
|
|
32
|
+
* 'rate(2 hours)',
|
|
33
|
+
* { rate: 'cron(0 12 * * ? *)', enabled: false }
|
|
34
|
+
* ]
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
interface ScheduleEventConfig {
|
|
38
|
+
/**
|
|
39
|
+
* Schedule rate using either rate() or cron() syntax.
|
|
40
|
+
* Can be a string or array of strings for multiple schedules.
|
|
41
|
+
*
|
|
42
|
+
* @example 'rate(2 hours)' or 'cron(0 12 * * ? *)'
|
|
43
|
+
* @example ['cron(0 0/4 ? * MON-FRI *)', 'cron(0 2 ? * SAT-SUN *)']
|
|
44
|
+
*/
|
|
45
|
+
rate: string | string[];
|
|
46
|
+
/**
|
|
47
|
+
* Whether the schedule is enabled (default: true).
|
|
48
|
+
*/
|
|
49
|
+
enabled?: boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Input payload to pass to the function.
|
|
52
|
+
*/
|
|
53
|
+
input?: Record<string, any>;
|
|
54
|
+
/**
|
|
55
|
+
* JSONPath expression to select part of the event data as input.
|
|
56
|
+
*/
|
|
57
|
+
inputPath?: string;
|
|
58
|
+
/**
|
|
59
|
+
* Input transformer configuration for custom input mapping.
|
|
60
|
+
*/
|
|
61
|
+
inputTransformer?: {
|
|
62
|
+
inputPathsMap?: Record<string, string>;
|
|
63
|
+
inputTemplate?: string;
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Name of the schedule event.
|
|
67
|
+
*/
|
|
68
|
+
name?: string;
|
|
69
|
+
/**
|
|
70
|
+
* Description of the schedule event.
|
|
71
|
+
*/
|
|
72
|
+
description?: string;
|
|
73
|
+
/**
|
|
74
|
+
* Method to use: 'eventBus' (default) or 'scheduler'.
|
|
75
|
+
* Use 'scheduler' for higher limits (1M events vs 300).
|
|
76
|
+
*/
|
|
77
|
+
method?: 'eventBus' | 'scheduler';
|
|
78
|
+
/**
|
|
79
|
+
* Timezone for the schedule (only used with method: 'scheduler').
|
|
80
|
+
* @example 'America/New_York'
|
|
81
|
+
*/
|
|
82
|
+
timezone?: string;
|
|
83
|
+
}
|
|
84
|
+
type ScheduleConfig = string | ScheduleEventConfig | (string | ScheduleEventConfig)[];
|
|
85
|
+
/**
|
|
86
|
+
* Configuration for a worker's Lambda function deployment.
|
|
87
|
+
*
|
|
88
|
+
* **Best Practice**: Export this as a separate const from your worker file:
|
|
89
|
+
* ```typescript
|
|
90
|
+
* export const workerConfig: WorkerConfig = {
|
|
91
|
+
* timeout: 900,
|
|
92
|
+
* memorySize: 2048,
|
|
93
|
+
* layers: ['arn:aws:lambda:${aws:region}:${aws:accountId}:layer:ffmpeg:1'],
|
|
94
|
+
* schedule: 'rate(2 hours)',
|
|
95
|
+
* };
|
|
96
|
+
* ```
|
|
97
|
+
*
|
|
98
|
+
* The CLI will automatically extract it from the export. You do not need to pass it to `createWorker()`.
|
|
99
|
+
*/
|
|
100
|
+
interface WorkerConfig {
|
|
101
|
+
/**
|
|
102
|
+
* Lambda function timeout in seconds (max 900).
|
|
103
|
+
*/
|
|
104
|
+
timeout?: number;
|
|
105
|
+
/**
|
|
106
|
+
* Lambda function memory size in MB (128-10240).
|
|
107
|
+
*/
|
|
108
|
+
memorySize?: number;
|
|
109
|
+
/**
|
|
110
|
+
* Optional Lambda layers ARNs to attach to this worker function.
|
|
111
|
+
*
|
|
112
|
+
* This is primarily used by @microfox/ai-worker-cli when generating serverless.yml.
|
|
113
|
+
* Supports CloudFormation pseudo-parameters like ${aws:region} and ${aws:accountId}.
|
|
114
|
+
*
|
|
115
|
+
* Example:
|
|
116
|
+
* layers: ['arn:aws:lambda:${aws:region}:${aws:accountId}:layer:ffmpeg:1']
|
|
117
|
+
*/
|
|
118
|
+
layers?: string[];
|
|
119
|
+
/**
|
|
120
|
+
* Schedule events configuration for this worker.
|
|
121
|
+
* Allows multiple schedule events to be attached to the same function.
|
|
122
|
+
*
|
|
123
|
+
* @example Simple rate
|
|
124
|
+
* ```typescript
|
|
125
|
+
* schedule: 'rate(2 hours)'
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @example Multiple schedules
|
|
129
|
+
* ```typescript
|
|
130
|
+
* schedule: [
|
|
131
|
+
* 'rate(2 hours)',
|
|
132
|
+
* { rate: 'cron(0 12 * * ? *)', enabled: true, input: { key: 'value' } }
|
|
133
|
+
* ]
|
|
134
|
+
* ```
|
|
135
|
+
*
|
|
136
|
+
* @example Using scheduler method with timezone
|
|
137
|
+
* ```typescript
|
|
138
|
+
* schedule: {
|
|
139
|
+
* method: 'scheduler',
|
|
140
|
+
* rate: 'cron(0 0/4 ? * MON-FRI *)',
|
|
141
|
+
* timezone: 'America/New_York',
|
|
142
|
+
* input: { key1: 'value1' }
|
|
143
|
+
* }
|
|
144
|
+
* ```
|
|
145
|
+
*/
|
|
146
|
+
schedule?: ScheduleConfig;
|
|
147
|
+
/**
|
|
148
|
+
* SQS queue settings for this worker (used by @microfox/ai-worker-cli when generating serverless.yml).
|
|
149
|
+
*
|
|
150
|
+
* Notes:
|
|
151
|
+
* - To effectively disable retries, set `maxReceiveCount: 1` (requires DLQ; the CLI will create one).
|
|
152
|
+
* - SQS does not support `maxReceiveCount: 0`.
|
|
153
|
+
* - `messageRetentionPeriod` is in seconds (max 1209600 = 14 days).
|
|
154
|
+
*/
|
|
155
|
+
sqs?: {
|
|
156
|
+
/**
|
|
157
|
+
* How many receives before sending to DLQ.
|
|
158
|
+
* Use 1 to avoid retries.
|
|
159
|
+
*/
|
|
160
|
+
maxReceiveCount?: number;
|
|
161
|
+
/**
|
|
162
|
+
* How long messages are retained in the main queue (seconds).
|
|
163
|
+
*/
|
|
164
|
+
messageRetentionPeriod?: number;
|
|
165
|
+
/**
|
|
166
|
+
* Visibility timeout for the main queue (seconds).
|
|
167
|
+
* If not set, CLI defaults to (worker timeout + 60s).
|
|
168
|
+
*/
|
|
169
|
+
visibilityTimeout?: number;
|
|
170
|
+
/**
|
|
171
|
+
* DLQ message retention period (seconds).
|
|
172
|
+
* Defaults to `messageRetentionPeriod` (or 14 days).
|
|
173
|
+
*/
|
|
174
|
+
deadLetterMessageRetentionPeriod?: number;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
interface WorkerAgentConfig<INPUT_SCHEMA extends ZodType<any>, OUTPUT> {
|
|
178
|
+
id: string;
|
|
179
|
+
inputSchema: INPUT_SCHEMA;
|
|
180
|
+
outputSchema: ZodType<OUTPUT>;
|
|
181
|
+
handler: WorkerHandler<z.infer<INPUT_SCHEMA>, OUTPUT>;
|
|
182
|
+
/**
|
|
183
|
+
* @deprecated Prefer exporting `workerConfig` as a separate const from your worker file.
|
|
184
|
+
* The CLI will automatically extract it from the export. This parameter is kept for backward compatibility.
|
|
185
|
+
*/
|
|
186
|
+
workerConfig?: WorkerConfig;
|
|
187
|
+
}
|
|
188
|
+
interface WorkerAgent<INPUT_SCHEMA extends ZodType<any>, OUTPUT> {
|
|
189
|
+
id: string;
|
|
190
|
+
dispatch: (input: z.input<INPUT_SCHEMA>, options: DispatchOptions) => Promise<DispatchResult>;
|
|
191
|
+
handler: WorkerHandler<z.infer<INPUT_SCHEMA>, OUTPUT>;
|
|
192
|
+
inputSchema: INPUT_SCHEMA;
|
|
193
|
+
outputSchema: ZodType<OUTPUT>;
|
|
194
|
+
workerConfig?: WorkerConfig;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Creates a worker agent that can be dispatched to SQS/Lambda.
|
|
198
|
+
*
|
|
199
|
+
* In development mode (NODE_ENV === 'development' and WORKERS_LOCAL_MODE !== 'false'),
|
|
200
|
+
* dispatch() will run the handler immediately in the same process.
|
|
201
|
+
*
|
|
202
|
+
* In production, dispatch() sends a message to SQS which triggers a Lambda function.
|
|
203
|
+
*
|
|
204
|
+
* @template INPUT_SCHEMA - The Zod schema type (e.g., `typeof InputSchema`).
|
|
205
|
+
* Used to derive both:
|
|
206
|
+
* - Pre-parse input type via `z.input<INPUT_SCHEMA>` for `dispatch()` (preserves optional fields)
|
|
207
|
+
* - Parsed input type via `z.infer<INPUT_SCHEMA>` for handler (defaults applied)
|
|
208
|
+
* @template OUTPUT - The output type returned by the handler. Use `z.infer<typeof OutputSchema>`.
|
|
209
|
+
*
|
|
210
|
+
* @param config - Worker agent configuration
|
|
211
|
+
* @returns A worker agent object with a dispatch method
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* ```typescript
|
|
215
|
+
* const InputSchema = z.object({
|
|
216
|
+
* url: z.string().url(),
|
|
217
|
+
* timeout: z.number().optional().default(5000), // optional with default
|
|
218
|
+
* });
|
|
219
|
+
*
|
|
220
|
+
* export const worker = createWorker<typeof InputSchema, Output>({
|
|
221
|
+
* // dispatch() accepts { url: string, timeout?: number } (pre-parse, optional preserved)
|
|
222
|
+
* // handler receives { url: string, timeout: number } (parsed, default applied)
|
|
223
|
+
* });
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
declare function createWorker<INPUT_SCHEMA extends ZodType<any>, OUTPUT>(config: WorkerAgentConfig<INPUT_SCHEMA, OUTPUT>): WorkerAgent<INPUT_SCHEMA, OUTPUT>;
|
|
227
|
+
/**
|
|
228
|
+
* Creates a Lambda handler entrypoint for a worker agent.
|
|
229
|
+
* This is used by the deployment script to generate Lambda entrypoints.
|
|
230
|
+
*
|
|
231
|
+
* @param agent - The worker agent
|
|
232
|
+
* @returns A Lambda handler function
|
|
233
|
+
*/
|
|
234
|
+
declare function createLambdaEntrypoint<INPUT_SCHEMA extends ZodType<any>, OUTPUT>(agent: WorkerAgent<INPUT_SCHEMA, OUTPUT>): (event: aws_lambda.SQSEvent, context: aws_lambda.Context) => Promise<void>;
|
|
235
|
+
|
|
236
|
+
export { DispatchOptions, DispatchResult, type ScheduleConfig, type ScheduleEventConfig, type WorkerAgent, type WorkerAgentConfig, type WorkerConfig, WorkerHandler, createLambdaEntrypoint, createWorker };
|