ai-workflows 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/.turbo/turbo-build.log +5 -0
  2. package/.turbo/turbo-test.log +104 -0
  3. package/README.md +285 -24
  4. package/dist/context.d.ts +26 -0
  5. package/dist/context.d.ts.map +1 -0
  6. package/dist/context.js +84 -0
  7. package/dist/context.js.map +1 -0
  8. package/dist/every.d.ts +67 -0
  9. package/dist/every.d.ts.map +1 -0
  10. package/dist/every.js +268 -0
  11. package/dist/every.js.map +1 -0
  12. package/dist/index.d.ts +66 -5
  13. package/dist/index.d.ts.map +1 -0
  14. package/dist/index.js +70 -7
  15. package/dist/index.js.map +1 -0
  16. package/dist/on.d.ts +49 -0
  17. package/dist/on.d.ts.map +1 -0
  18. package/dist/on.js +80 -0
  19. package/dist/on.js.map +1 -0
  20. package/dist/send.d.ts +59 -0
  21. package/dist/send.d.ts.map +1 -0
  22. package/dist/send.js +112 -0
  23. package/dist/send.js.map +1 -0
  24. package/dist/types.d.ts +229 -0
  25. package/dist/types.d.ts.map +1 -0
  26. package/dist/types.js +5 -0
  27. package/dist/types.js.map +1 -0
  28. package/dist/workflow.d.ts +79 -0
  29. package/dist/workflow.d.ts.map +1 -0
  30. package/dist/workflow.js +456 -0
  31. package/dist/workflow.js.map +1 -0
  32. package/package.json +24 -65
  33. package/src/context.ts +108 -0
  34. package/src/every.ts +299 -0
  35. package/src/index.ts +106 -0
  36. package/src/on.ts +100 -0
  37. package/src/send.ts +131 -0
  38. package/src/types.ts +244 -0
  39. package/src/workflow.ts +569 -0
  40. package/test/context.test.ts +151 -0
  41. package/test/every.test.ts +361 -0
  42. package/test/on.test.ts +100 -0
  43. package/test/send.test.ts +118 -0
  44. package/test/workflow.test.ts +288 -0
  45. package/tsconfig.json +9 -0
  46. package/vitest.config.ts +8 -0
  47. package/LICENSE +0 -21
  48. package/dist/index.test.d.ts +0 -1
  49. package/dist/index.test.js +0 -9
package/dist/every.js ADDED
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Schedule registration using natural language
3
+ *
4
+ * Usage:
5
+ * every.hour($ => { ... })
6
+ * every.Thursday.at8am($ => { ... })
7
+ * every.weekday.at9am($ => { ... })
8
+ * every('hour during business hours', $ => { ... })
9
+ * every('first Monday of the month at 9am', $ => { ... })
10
+ */
11
+ /**
12
+ * Registry of schedule handlers
13
+ */
14
+ const scheduleRegistry = [];
15
+ /**
16
+ * Get all registered schedule handlers
17
+ */
18
+ export function getScheduleHandlers() {
19
+ return [...scheduleRegistry];
20
+ }
21
+ /**
22
+ * Clear all registered schedule handlers
23
+ */
24
+ export function clearScheduleHandlers() {
25
+ scheduleRegistry.length = 0;
26
+ }
27
+ /**
28
+ * Register a schedule handler directly
29
+ */
30
+ export function registerScheduleHandler(interval, handler) {
31
+ scheduleRegistry.push({
32
+ interval,
33
+ handler,
34
+ source: handler.toString(),
35
+ });
36
+ }
37
+ /**
38
+ * Well-known cron patterns for common schedules
39
+ */
40
+ const KNOWN_PATTERNS = {
41
+ // Time units
42
+ 'second': '* * * * * *',
43
+ 'minute': '* * * * *',
44
+ 'hour': '0 * * * *',
45
+ 'day': '0 0 * * *',
46
+ 'week': '0 0 * * 0',
47
+ 'month': '0 0 1 * *',
48
+ 'year': '0 0 1 1 *',
49
+ // Days of week
50
+ 'Monday': '0 0 * * 1',
51
+ 'Tuesday': '0 0 * * 2',
52
+ 'Wednesday': '0 0 * * 3',
53
+ 'Thursday': '0 0 * * 4',
54
+ 'Friday': '0 0 * * 5',
55
+ 'Saturday': '0 0 * * 6',
56
+ 'Sunday': '0 0 * * 0',
57
+ // Common patterns
58
+ 'weekday': '0 0 * * 1-5',
59
+ 'weekend': '0 0 * * 0,6',
60
+ 'midnight': '0 0 * * *',
61
+ 'noon': '0 12 * * *',
62
+ };
63
+ /**
64
+ * Time suffixes for day-based schedules
65
+ */
66
+ const TIME_PATTERNS = {
67
+ 'at6am': { hour: 6, minute: 0 },
68
+ 'at7am': { hour: 7, minute: 0 },
69
+ 'at8am': { hour: 8, minute: 0 },
70
+ 'at9am': { hour: 9, minute: 0 },
71
+ 'at10am': { hour: 10, minute: 0 },
72
+ 'at11am': { hour: 11, minute: 0 },
73
+ 'at12pm': { hour: 12, minute: 0 },
74
+ 'atnoon': { hour: 12, minute: 0 },
75
+ 'at1pm': { hour: 13, minute: 0 },
76
+ 'at2pm': { hour: 14, minute: 0 },
77
+ 'at3pm': { hour: 15, minute: 0 },
78
+ 'at4pm': { hour: 16, minute: 0 },
79
+ 'at5pm': { hour: 17, minute: 0 },
80
+ 'at6pm': { hour: 18, minute: 0 },
81
+ 'at7pm': { hour: 19, minute: 0 },
82
+ 'at8pm': { hour: 20, minute: 0 },
83
+ 'at9pm': { hour: 21, minute: 0 },
84
+ 'atmidnight': { hour: 0, minute: 0 },
85
+ };
86
+ /**
87
+ * Parse a known pattern or return null
88
+ */
89
+ function parseKnownPattern(pattern) {
90
+ return KNOWN_PATTERNS[pattern] ?? null;
91
+ }
92
+ /**
93
+ * Combine a day pattern with a time pattern
94
+ */
95
+ function combineWithTime(baseCron, time) {
96
+ const parts = baseCron.split(' ');
97
+ parts[0] = String(time.minute);
98
+ parts[1] = String(time.hour);
99
+ return parts.join(' ');
100
+ }
101
+ /**
102
+ * AI-powered cron conversion (placeholder - will use ai-functions)
103
+ */
104
+ let cronConverter = null;
105
+ /**
106
+ * Set the AI cron converter function
107
+ */
108
+ export function setCronConverter(converter) {
109
+ cronConverter = converter;
110
+ }
111
+ /**
112
+ * Convert natural language to cron expression
113
+ */
114
+ export async function toCron(description) {
115
+ // First check known patterns
116
+ const known = parseKnownPattern(description);
117
+ if (known)
118
+ return known;
119
+ // If we have an AI converter, use it
120
+ if (cronConverter) {
121
+ return cronConverter(description);
122
+ }
123
+ // Otherwise, assume it's already a cron expression or throw
124
+ if (/^[\d\*\-\/\,\s]+$/.test(description)) {
125
+ return description;
126
+ }
127
+ throw new Error(`Unknown schedule pattern: "${description}". ` +
128
+ `Set up AI conversion with setCronConverter() for natural language schedules.`);
129
+ }
130
+ /**
131
+ * Create the `every` proxy
132
+ */
133
+ function createEveryProxy() {
134
+ const handler = {
135
+ get(_target, prop) {
136
+ // Check if it's a known pattern
137
+ const pattern = KNOWN_PATTERNS[prop];
138
+ if (pattern) {
139
+ // Return an object that can either be called directly or have time accessors
140
+ const result = (handlerFn) => {
141
+ registerScheduleHandler({ type: 'cron', expression: pattern, natural: prop }, handlerFn);
142
+ };
143
+ // Add time accessors
144
+ return new Proxy(result, {
145
+ get(_t, timeKey) {
146
+ const time = TIME_PATTERNS[timeKey];
147
+ if (time) {
148
+ const cron = combineWithTime(pattern, time);
149
+ return (handlerFn) => {
150
+ registerScheduleHandler({ type: 'cron', expression: cron, natural: `${prop}.${timeKey}` }, handlerFn);
151
+ };
152
+ }
153
+ return undefined;
154
+ },
155
+ apply(_t, _thisArg, args) {
156
+ registerScheduleHandler({ type: 'cron', expression: pattern, natural: prop }, args[0]);
157
+ }
158
+ });
159
+ }
160
+ // Check for plural time units (e.g., seconds(5), minutes(30))
161
+ const pluralUnits = {
162
+ seconds: 'second',
163
+ minutes: 'minute',
164
+ hours: 'hour',
165
+ days: 'day',
166
+ weeks: 'week',
167
+ };
168
+ if (pluralUnits[prop]) {
169
+ return (value) => (handlerFn) => {
170
+ registerScheduleHandler({ type: pluralUnits[prop], value, natural: `${value} ${prop}` }, handlerFn);
171
+ };
172
+ }
173
+ return undefined;
174
+ },
175
+ apply(_target, _thisArg, args) {
176
+ // Called as every('natural language description', handler)
177
+ const [description, handler] = args;
178
+ if (typeof description === 'string' && typeof handler === 'function') {
179
+ // Register with natural language - will be converted to cron at runtime
180
+ registerScheduleHandler({ type: 'natural', description }, handler);
181
+ }
182
+ }
183
+ };
184
+ return new Proxy(function () { }, handler);
185
+ }
186
+ /**
187
+ * The `every` function/object for registering scheduled handlers
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * import { every } from 'ai-workflows'
192
+ *
193
+ * // Simple intervals
194
+ * every.hour($ => $.log('Hourly task'))
195
+ * every.day($ => $.log('Daily task'))
196
+ *
197
+ * // Day + time combinations
198
+ * every.Monday.at9am($ => $.log('Monday morning standup'))
199
+ * every.weekday.at8am($ => $.log('Workday start'))
200
+ * every.Friday.at5pm($ => $.log('End of week report'))
201
+ *
202
+ * // Plural intervals with values
203
+ * every.minutes(30)($ => $.log('Every 30 minutes'))
204
+ * every.hours(4)($ => $.log('Every 4 hours'))
205
+ *
206
+ * // Natural language (requires AI converter)
207
+ * every('hour during business hours', $ => { ... })
208
+ * every('first Monday of the month at 9am', $ => { ... })
209
+ * every('every 15 minutes between 9am and 5pm on weekdays', $ => { ... })
210
+ * ```
211
+ */
212
+ export const every = createEveryProxy();
213
+ /**
214
+ * Convert interval to milliseconds (for simulation/testing)
215
+ */
216
+ export function intervalToMs(interval) {
217
+ switch (interval.type) {
218
+ case 'second':
219
+ return (interval.value ?? 1) * 1000;
220
+ case 'minute':
221
+ return (interval.value ?? 1) * 60 * 1000;
222
+ case 'hour':
223
+ return (interval.value ?? 1) * 60 * 60 * 1000;
224
+ case 'day':
225
+ return (interval.value ?? 1) * 24 * 60 * 60 * 1000;
226
+ case 'week':
227
+ return (interval.value ?? 1) * 7 * 24 * 60 * 60 * 1000;
228
+ case 'cron':
229
+ case 'natural':
230
+ // Cron/natural expressions need special handling
231
+ return 0;
232
+ }
233
+ }
234
+ /**
235
+ * Format interval for display
236
+ */
237
+ export function formatInterval(interval) {
238
+ if ('natural' in interval && interval.natural) {
239
+ return interval.natural;
240
+ }
241
+ switch (interval.type) {
242
+ case 'second':
243
+ return interval.value && interval.value > 1
244
+ ? `every ${interval.value} seconds`
245
+ : 'every second';
246
+ case 'minute':
247
+ return interval.value && interval.value > 1
248
+ ? `every ${interval.value} minutes`
249
+ : 'every minute';
250
+ case 'hour':
251
+ return interval.value && interval.value > 1
252
+ ? `every ${interval.value} hours`
253
+ : 'every hour';
254
+ case 'day':
255
+ return interval.value && interval.value > 1
256
+ ? `every ${interval.value} days`
257
+ : 'every day';
258
+ case 'week':
259
+ return interval.value && interval.value > 1
260
+ ? `every ${interval.value} weeks`
261
+ : 'every week';
262
+ case 'cron':
263
+ return `cron: ${interval.expression}`;
264
+ case 'natural':
265
+ return interval.description;
266
+ }
267
+ }
268
+ //# sourceMappingURL=every.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"every.js","sourceRoot":"","sources":["../src/every.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH;;GAEG;AACH,MAAM,gBAAgB,GAA2B,EAAE,CAAA;AAEnD;;GAEG;AACH,MAAM,UAAU,mBAAmB;IACjC,OAAO,CAAC,GAAG,gBAAgB,CAAC,CAAA;AAC9B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB;IACnC,gBAAgB,CAAC,MAAM,GAAG,CAAC,CAAA;AAC7B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB,CACrC,QAA0B,EAC1B,OAAwB;IAExB,gBAAgB,CAAC,IAAI,CAAC;QACpB,QAAQ;QACR,OAAO;QACP,MAAM,EAAE,OAAO,CAAC,QAAQ,EAAE;KAC3B,CAAC,CAAA;AACJ,CAAC;AAED;;GAEG;AACH,MAAM,cAAc,GAA2B;IAC7C,aAAa;IACb,QAAQ,EAAE,aAAa;IACvB,QAAQ,EAAE,WAAW;IACrB,MAAM,EAAE,WAAW;IACnB,KAAK,EAAE,WAAW;IAClB,MAAM,EAAE,WAAW;IACnB,OAAO,EAAE,WAAW;IACpB,MAAM,EAAE,WAAW;IAEnB,eAAe;IACf,QAAQ,EAAE,WAAW;IACrB,SAAS,EAAE,WAAW;IACtB,WAAW,EAAE,WAAW;IACxB,UAAU,EAAE,WAAW;IACvB,QAAQ,EAAE,WAAW;IACrB,UAAU,EAAE,WAAW;IACvB,QAAQ,EAAE,WAAW;IAErB,kBAAkB;IAClB,SAAS,EAAE,aAAa;IACxB,SAAS,EAAE,aAAa;IACxB,U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ClB,KAAK,KAAK;YACR,OAAO,QAAQ,CAAC,KAAK,IAAI,QAAQ,CAAC,KAAK,GAAG,CAAC;gBACzC,CAAC,CAAC,SAAS,QAAQ,CAAC,KAAK,OAAO;gBAChC,CAAC,CAAC,WAAW,CAAA;QACjB,KAAK,MAAM;YACT,OAAO,QAAQ,CAAC,KAAK,IAAI,QAAQ,CAAC,KAAK,GAAG,CAAC;gBACzC,CAAC,CAAC,SAAS,QAAQ,CAAC,KAAK,QAAQ;gBACjC,CAAC,CAAC,YAAY,CAAA;QAClB,KAAK,MAAM;YACT,OAAO,SAAS,QAAQ,CAAC,UAAU,EAAE,CAAA;QACvC,KAAK,SAAS;YACZ,OAAO,QAAQ,CAAC,WAAW,CAAA;IAC/B,CAAC;AACH,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,7 +1,68 @@
1
1
  /**
2
- * A placeholder function that adds two numbers
3
- * @param a First number
4
- * @param b Second number
5
- * @returns The sum of a and b
2
+ * ai-workflows - Event-driven workflows with $ context
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { Workflow } from 'ai-workflows'
7
+ *
8
+ * // Create a workflow with $ context
9
+ * const workflow = Workflow($ => {
10
+ * // Register event handlers
11
+ * $.on.Customer.created(async (customer, $) => {
12
+ * $.log('New customer:', customer.name)
13
+ * await $.send('Email.welcome', { to: customer.email })
14
+ * })
15
+ *
16
+ * $.on.Order.completed(async (order, $) => {
17
+ * $.log('Order completed:', order.id)
18
+ * })
19
+ *
20
+ * // Register scheduled tasks
21
+ * $.every.hour(async ($) => {
22
+ * $.log('Hourly check')
23
+ * })
24
+ *
25
+ * $.every.Monday.at9am(async ($) => {
26
+ * $.log('Weekly standup reminder')
27
+ * })
28
+ *
29
+ * $.every.minutes(30)(async ($) => {
30
+ * $.log('Every 30 minutes')
31
+ * })
32
+ *
33
+ * // Natural language scheduling
34
+ * $.every('first Monday of the month', async ($) => {
35
+ * $.log('Monthly report')
36
+ * })
37
+ * })
38
+ *
39
+ * // Start the workflow
40
+ * await workflow.start()
41
+ *
42
+ * // Emit events
43
+ * await workflow.send('Customer.created', { name: 'John', email: 'john@example.com' })
44
+ * ```
45
+ *
46
+ * @example
47
+ * // Alternative: Use standalone on/every for global registration
48
+ * ```ts
49
+ * import { on, every, send } from 'ai-workflows'
50
+ *
51
+ * on.Customer.created(async (customer, $) => {
52
+ * await $.send('Email.welcome', { to: customer.email })
53
+ * })
54
+ *
55
+ * every.hour(async ($) => {
56
+ * $.log('Hourly task')
57
+ * })
58
+ *
59
+ * await send('Customer.created', { name: 'John' })
60
+ * ```
6
61
  */
7
- export declare function add(a: number, b: number): number;
62
+ export { Workflow, createTestContext, parseEvent, type WorkflowInstance } from './workflow.js';
63
+ export { on, registerEventHandler, getEventHandlers, clearEventHandlers } from './on.js';
64
+ export { every, registerScheduleHandler, getScheduleHandlers, clearScheduleHandlers, setCronConverter, toCron, intervalToMs, formatInterval, } from './every.js';
65
+ export { send, getEventBus } from './send.js';
66
+ export { createWorkflowContext, createIsolatedContext } from './context.js';
67
+ export type { EventHandler, ScheduleHandler, WorkflowContext, WorkflowState, WorkflowHistoryEntry, EventRegistration, ScheduleRegistration, ScheduleInterval, WorkflowDefinition, WorkflowOptions, ParsedEvent, OnProxy, EveryProxy, HandlerFunction, DatabaseContext, ActionData, ArtifactData, } from './types.js';
68
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AAGH,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,UAAU,EAAE,KAAK,gBAAgB,EAAE,MAAM,eAAe,CAAA;AAG9F,OAAO,EAAE,EAAE,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAGxF,OAAO,EACL,KAAK,EACL,uBAAuB,EACvB,mBAAmB,EACnB,qBAAqB,EACrB,gBAAgB,EAChB,MAAM,EACN,YAAY,EACZ,cAAc,GACf,MAAM,YAAY,CAAA;AAGnB,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAG7C,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAA;AAG3E,YAAY,EACV,YAAY,EACZ,eAAe,EACf,eAAe,EACf,aAAa,EACb,oBAAoB,EACpB,iBAAiB,EACjB,oBAAoB,EACpB,gBAAgB,EAChB,kBAAkB,EAClB,eAAe,EACf,WAAW,EACX,OAAO,EACP,UAAU,EACV,eAAe,EACf,eAAe,EACf,UAAU,EACV,YAAY,GACb,MAAM,YAAY,CAAA"}
package/dist/index.js CHANGED
@@ -1,9 +1,72 @@
1
1
  /**
2
- * A placeholder function that adds two numbers
3
- * @param a First number
4
- * @param b Second number
5
- * @returns The sum of a and b
2
+ * ai-workflows - Event-driven workflows with $ context
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import { Workflow } from 'ai-workflows'
7
+ *
8
+ * // Create a workflow with $ context
9
+ * const workflow = Workflow($ => {
10
+ * // Register event handlers
11
+ * $.on.Customer.created(async (customer, $) => {
12
+ * $.log('New customer:', customer.name)
13
+ * await $.send('Email.welcome', { to: customer.email })
14
+ * })
15
+ *
16
+ * $.on.Order.completed(async (order, $) => {
17
+ * $.log('Order completed:', order.id)
18
+ * })
19
+ *
20
+ * // Register scheduled tasks
21
+ * $.every.hour(async ($) => {
22
+ * $.log('Hourly check')
23
+ * })
24
+ *
25
+ * $.every.Monday.at9am(async ($) => {
26
+ * $.log('Weekly standup reminder')
27
+ * })
28
+ *
29
+ * $.every.minutes(30)(async ($) => {
30
+ * $.log('Every 30 minutes')
31
+ * })
32
+ *
33
+ * // Natural language scheduling
34
+ * $.every('first Monday of the month', async ($) => {
35
+ * $.log('Monthly report')
36
+ * })
37
+ * })
38
+ *
39
+ * // Start the workflow
40
+ * await workflow.start()
41
+ *
42
+ * // Emit events
43
+ * await workflow.send('Customer.created', { name: 'John', email: 'john@example.com' })
44
+ * ```
45
+ *
46
+ * @example
47
+ * // Alternative: Use standalone on/every for global registration
48
+ * ```ts
49
+ * import { on, every, send } from 'ai-workflows'
50
+ *
51
+ * on.Customer.created(async (customer, $) => {
52
+ * await $.send('Email.welcome', { to: customer.email })
53
+ * })
54
+ *
55
+ * every.hour(async ($) => {
56
+ * $.log('Hourly task')
57
+ * })
58
+ *
59
+ * await send('Customer.created', { name: 'John' })
60
+ * ```
6
61
  */
7
- export function add(a, b) {
8
- return a + b;
9
- }
62
+ // Main Workflow API
63
+ export { Workflow, createTestContext, parseEvent } from './workflow.js';
64
+ // Standalone event handling (for global registration)
65
+ export { on, registerEventHandler, getEventHandlers, clearEventHandlers } from './on.js';
66
+ // Standalone scheduling (for global registration)
67
+ export { every, registerScheduleHandler, getScheduleHandlers, clearScheduleHandlers, setCronConverter, toCron, intervalToMs, formatInterval, } from './every.js';
68
+ // Event emission
69
+ export { send, getEventBus } from './send.js';
70
+ // Context
71
+ export { createWorkflowContext, createIsolatedContext } from './context.js';
72
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AAEH,oBAAoB;AACpB,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,UAAU,EAAyB,MAAM,eAAe,CAAA;AAE9F,sDAAsD;AACtD,OAAO,EAAE,EAAE,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAExF,kDAAkD;AAClD,OAAO,EACL,KAAK,EACL,uBAAuB,EACvB,mBAAmB,EACnB,qBAAqB,EACrB,gBAAgB,EAChB,MAAM,EACN,YAAY,EACZ,cAAc,GACf,MAAM,YAAY,CAAA;AAEnB,iBAAiB;AACjB,OAAO,EAAE,IAAI,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAE7C,UAAU;AACV,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAA"}
package/dist/on.d.ts ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Event handler registration using on.Noun.event syntax
3
+ *
4
+ * Usage:
5
+ * on.Customer.created(customer => { ... })
6
+ * on.Order.completed(order => { ... })
7
+ * on.Payment.failed(payment => { ... })
8
+ */
9
+ import type { EventHandler, EventRegistration } from './types.js';
10
+ /**
11
+ * Get all registered event handlers
12
+ */
13
+ export declare function getEventHandlers(): EventRegistration[];
14
+ /**
15
+ * Clear all registered event handlers
16
+ */
17
+ export declare function clearEventHandlers(): void;
18
+ /**
19
+ * Register an event handler directly
20
+ */
21
+ export declare function registerEventHandler(noun: string, event: string, handler: EventHandler): void;
22
+ /**
23
+ * Event proxy type for on.Noun.event pattern
24
+ */
25
+ type EventProxy = {
26
+ [noun: string]: {
27
+ [event: string]: (handler: EventHandler) => void;
28
+ };
29
+ };
30
+ /**
31
+ * The `on` object for registering event handlers
32
+ *
33
+ * @example
34
+ * ```ts
35
+ * import { on } from 'ai-workflows'
36
+ *
37
+ * on.Customer.created(async (customer, $) => {
38
+ * $.log('Customer created:', customer.name)
39
+ * await $.send('Email.welcome', { to: customer.email })
40
+ * })
41
+ *
42
+ * on.Order.completed(async (order, $) => {
43
+ * $.log('Order completed:', order.id)
44
+ * })
45
+ * ```
46
+ */
47
+ export declare const on: EventProxy;
48
+ export {};
49
+ //# sourceMappingURL=on.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"on.d.ts","sourceRoot":"","sources":["../src/on.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AAOjE;;GAEG;AACH,wBAAgB,gBAAgB,IAAI,iBAAiB,EAAE,CAEtD;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,YAAY,GACpB,IAAI,CAON;AAED;;GAEG;AACH,KAAK,UAAU,GAAG;IAChB,CAAC,IAAI,EAAE,MAAM,GAAG;QACd,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,OAAO,EAAE,YAAY,KAAK,IAAI,CAAA;KACjD,CAAA;CACF,CAAA;AA6BD;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,EAAE,YAAkB,CAAA"}
package/dist/on.js ADDED
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Event handler registration using on.Noun.event syntax
3
+ *
4
+ * Usage:
5
+ * on.Customer.created(customer => { ... })
6
+ * on.Order.completed(order => { ... })
7
+ * on.Payment.failed(payment => { ... })
8
+ */
9
+ /**
10
+ * Registry of event handlers
11
+ */
12
+ const eventRegistry = [];
13
+ /**
14
+ * Get all registered event handlers
15
+ */
16
+ export function getEventHandlers() {
17
+ return [...eventRegistry];
18
+ }
19
+ /**
20
+ * Clear all registered event handlers
21
+ */
22
+ export function clearEventHandlers() {
23
+ eventRegistry.length = 0;
24
+ }
25
+ /**
26
+ * Register an event handler directly
27
+ */
28
+ export function registerEventHandler(noun, event, handler) {
29
+ eventRegistry.push({
30
+ noun,
31
+ event,
32
+ handler,
33
+ source: handler.toString(),
34
+ });
35
+ }
36
+ /**
37
+ * Create the `on` proxy
38
+ *
39
+ * This creates a proxy that allows:
40
+ * on.Customer.created(handler)
41
+ * on.Order.shipped(handler)
42
+ *
43
+ * The first property access captures the noun (Customer, Order)
44
+ * The second property access captures the event (created, shipped)
45
+ * The function call registers the handler
46
+ */
47
+ function createOnProxy() {
48
+ return new Proxy({}, {
49
+ get(_target, noun) {
50
+ // Return a proxy for the event level
51
+ return new Proxy({}, {
52
+ get(_eventTarget, event) {
53
+ // Return a function that registers the handler
54
+ return (handler) => {
55
+ registerEventHandler(noun, event, handler);
56
+ };
57
+ }
58
+ });
59
+ }
60
+ });
61
+ }
62
+ /**
63
+ * The `on` object for registering event handlers
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * import { on } from 'ai-workflows'
68
+ *
69
+ * on.Customer.created(async (customer, $) => {
70
+ * $.log('Customer created:', customer.name)
71
+ * await $.send('Email.welcome', { to: customer.email })
72
+ * })
73
+ *
74
+ * on.Order.completed(async (order, $) => {
75
+ * $.log('Order completed:', order.id)
76
+ * })
77
+ * ```
78
+ */
79
+ export const on = createOnProxy();
80
+ //# sourceMappingURL=on.js.map
package/dist/on.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"on.js","sourceRoot":"","sources":["../src/on.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH;;GAEG;AACH,MAAM,aAAa,GAAwB,EAAE,CAAA;AAE7C;;GAEG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO,CAAC,GAAG,aAAa,CAAC,CAAA;AAC3B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB;IAChC,aAAa,CAAC,MAAM,GAAG,CAAC,CAAA;AAC1B,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,oBAAoB,CAClC,IAAY,EACZ,KAAa,EACb,OAAqB;IAErB,aAAa,CAAC,IAAI,CAAC;QACjB,IAAI;QACJ,KAAK;QACL,OAAO;QACP,MAAM,EAAE,OAAO,CAAC,QAAQ,EAAE;KAC3B,CAAC,CAAA;AACJ,CAAC;AAWD;;;;;;;;;;GAUG;AACH,SAAS,aAAa;IACpB,OAAO,IAAI,KAAK,CAAC,EAAgB,EAAE;QACjC,GAAG,CAAC,OAAO,EAAE,IAAY;YACvB,qCAAqC;YACrC,OAAO,IAAI,KAAK,CAAC,EAAE,EAAE;gBACnB,GAAG,CAAC,YAAY,EAAE,KAAa;oBAC7B,+CAA+C;oBAC/C,OAAO,CAAC,OAAqB,EAAE,EAAE;wBAC/B,oBAAoB,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,CAAA;oBAC5C,CAAC,CAAA;gBACH,CAAC;aACF,CAAC,CAAA;QACJ,CAAC;KACF,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,EAAE,GAAG,aAAa,EAAE,CAAA"}
package/dist/send.d.ts ADDED
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Event emission using send('Noun.event', data)
3
+ *
4
+ * Usage:
5
+ * send('Customer.created', customer)
6
+ * send('Order.completed', order)
7
+ */
8
+ import type { ParsedEvent } from './types.js';
9
+ /**
10
+ * Event bus for managing event delivery
11
+ */
12
+ declare class EventBus {
13
+ private pending;
14
+ private processing;
15
+ /**
16
+ * Emit an event
17
+ */
18
+ emit(event: string, data: unknown): Promise<void>;
19
+ /**
20
+ * Process pending events
21
+ */
22
+ private process;
23
+ /**
24
+ * Deliver an event to matching handlers
25
+ */
26
+ private deliver;
27
+ }
28
+ /**
29
+ * Parse event string into noun and event
30
+ */
31
+ export declare function parseEvent(event: string): ParsedEvent | null;
32
+ /**
33
+ * Send an event
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { send } from 'ai-workflows'
38
+ *
39
+ * // Emit a customer created event
40
+ * await send('Customer.created', {
41
+ * id: '123',
42
+ * name: 'John Doe',
43
+ * email: 'john@example.com'
44
+ * })
45
+ *
46
+ * // Emit an order completed event
47
+ * await send('Order.completed', {
48
+ * id: 'order-456',
49
+ * total: 99.99
50
+ * })
51
+ * ```
52
+ */
53
+ export declare function send<T = unknown>(event: string, data: T): Promise<void>;
54
+ /**
55
+ * Get the global event bus (for testing/internal use)
56
+ */
57
+ export declare function getEventBus(): EventBus;
58
+ export {};
59
+ //# sourceMappingURL=send.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"send.d.ts","sourceRoot":"","sources":["../src/send.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAI7C;;GAEG;AACH,cAAM,QAAQ;IACZ,OAAO,CAAC,OAAO,CAA8C;IAC7D,OAAO,CAAC,UAAU,CAAQ;IAE1B;;OAEG;IACG,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC;IAQvD;;OAEG;YACW,OAAO;IAWrB;;OAEG;YACW,OAAO;CA+BtB;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI,CAU5D;AAOD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAsB,IAAI,CAAC,CAAC,GAAG,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAE7E;AAED;;GAEG;AACH,wBAAgB,WAAW,IAAI,QAAQ,CAEtC"}