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.
- package/.turbo/turbo-build.log +5 -0
- package/.turbo/turbo-test.log +104 -0
- package/README.md +285 -24
- package/dist/context.d.ts +26 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +84 -0
- package/dist/context.js.map +1 -0
- package/dist/every.d.ts +67 -0
- package/dist/every.d.ts.map +1 -0
- package/dist/every.js +268 -0
- package/dist/every.js.map +1 -0
- package/dist/index.d.ts +66 -5
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +70 -7
- package/dist/index.js.map +1 -0
- package/dist/on.d.ts +49 -0
- package/dist/on.d.ts.map +1 -0
- package/dist/on.js +80 -0
- package/dist/on.js.map +1 -0
- package/dist/send.d.ts +59 -0
- package/dist/send.d.ts.map +1 -0
- package/dist/send.js +112 -0
- package/dist/send.js.map +1 -0
- package/dist/types.d.ts +229 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/dist/workflow.d.ts +79 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +456 -0
- package/dist/workflow.js.map +1 -0
- package/package.json +24 -65
- package/src/context.ts +108 -0
- package/src/every.ts +299 -0
- package/src/index.ts +106 -0
- package/src/on.ts +100 -0
- package/src/send.ts +131 -0
- package/src/types.ts +244 -0
- package/src/workflow.ts +569 -0
- package/test/context.test.ts +151 -0
- package/test/every.test.ts +361 -0
- package/test/on.test.ts +100 -0
- package/test/send.test.ts +118 -0
- package/test/workflow.test.ts +288 -0
- package/tsconfig.json +9 -0
- package/vitest.config.ts +8 -0
- package/LICENSE +0 -21
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -9
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
|
|
2
|
+
import { Workflow, createTestContext, parseEvent } from '../src/workflow.js'
|
|
3
|
+
import { clearEventHandlers } from '../src/on.js'
|
|
4
|
+
import { clearScheduleHandlers } from '../src/every.js'
|
|
5
|
+
|
|
6
|
+
describe('Workflow - unified $ API', () => {
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
clearEventHandlers()
|
|
9
|
+
clearScheduleHandlers()
|
|
10
|
+
vi.useFakeTimers()
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
vi.useRealTimers()
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
describe('Workflow()', () => {
|
|
18
|
+
it('should create a workflow with $ context', () => {
|
|
19
|
+
const workflow = Workflow($ => {
|
|
20
|
+
// Just accessing $ to verify it works
|
|
21
|
+
expect($).toBeDefined()
|
|
22
|
+
expect($.on).toBeDefined()
|
|
23
|
+
expect($.every).toBeDefined()
|
|
24
|
+
expect($.send).toBeDefined()
|
|
25
|
+
expect($.log).toBeDefined()
|
|
26
|
+
expect($.get).toBeDefined()
|
|
27
|
+
expect($.set).toBeDefined()
|
|
28
|
+
expect($.getState).toBeDefined()
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
expect(workflow).toBeDefined()
|
|
32
|
+
expect(workflow.$).toBeDefined()
|
|
33
|
+
expect(workflow.send).toBeDefined()
|
|
34
|
+
expect(workflow.start).toBeDefined()
|
|
35
|
+
expect(workflow.stop).toBeDefined()
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('should capture event handlers registered via $.on', () => {
|
|
39
|
+
const workflow = Workflow($ => {
|
|
40
|
+
$.on.Customer.created(() => {})
|
|
41
|
+
$.on.Order.completed(() => {})
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
expect(workflow.definition.events).toHaveLength(2)
|
|
45
|
+
expect(workflow.definition.events[0]?.noun).toBe('Customer')
|
|
46
|
+
expect(workflow.definition.events[0]?.event).toBe('created')
|
|
47
|
+
expect(workflow.definition.events[1]?.noun).toBe('Order')
|
|
48
|
+
expect(workflow.definition.events[1]?.event).toBe('completed')
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('should capture schedule handlers registered via $.every', () => {
|
|
52
|
+
const workflow = Workflow($ => {
|
|
53
|
+
$.every.hour(() => {})
|
|
54
|
+
$.every.Monday.at9am(() => {})
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
expect(workflow.definition.schedules).toHaveLength(2)
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('should capture function source code', () => {
|
|
61
|
+
const workflow = Workflow($ => {
|
|
62
|
+
$.on.Test.event(async (data, ctx) => {
|
|
63
|
+
ctx.log('Test event', data)
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
// Source code is captured (variable names may be minified)
|
|
68
|
+
expect(workflow.definition.events[0]?.source).toBeDefined()
|
|
69
|
+
expect(workflow.definition.events[0]?.source.length).toBeGreaterThan(0)
|
|
70
|
+
expect(workflow.definition.events[0]?.source).toContain('Test event')
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
it('should deliver events to registered handlers', async () => {
|
|
74
|
+
const handler = vi.fn()
|
|
75
|
+
|
|
76
|
+
const workflow = Workflow($ => {
|
|
77
|
+
$.on.Customer.created(handler)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
await workflow.start()
|
|
81
|
+
await workflow.send('Customer.created', { id: '123', name: 'John' })
|
|
82
|
+
|
|
83
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
84
|
+
expect(handler).toHaveBeenCalledWith(
|
|
85
|
+
{ id: '123', name: 'John' },
|
|
86
|
+
expect.objectContaining({
|
|
87
|
+
send: expect.any(Function),
|
|
88
|
+
log: expect.any(Function),
|
|
89
|
+
})
|
|
90
|
+
)
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
it('should allow chained event sending from handlers', async () => {
|
|
94
|
+
const welcomeHandler = vi.fn()
|
|
95
|
+
|
|
96
|
+
const workflow = Workflow($ => {
|
|
97
|
+
$.on.Customer.created(async (customer, $) => {
|
|
98
|
+
await $.send('Email.welcome', { to: customer.email })
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
$.on.Email.welcome(welcomeHandler)
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
await workflow.start()
|
|
105
|
+
await workflow.send('Customer.created', { name: 'John', email: 'john@example.com' })
|
|
106
|
+
|
|
107
|
+
expect(welcomeHandler).toHaveBeenCalledWith(
|
|
108
|
+
{ to: 'john@example.com' },
|
|
109
|
+
expect.anything()
|
|
110
|
+
)
|
|
111
|
+
})
|
|
112
|
+
|
|
113
|
+
it('should track events in state history', async () => {
|
|
114
|
+
const workflow = Workflow($ => {
|
|
115
|
+
$.on.Test.event(() => {})
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
await workflow.start()
|
|
119
|
+
await workflow.send('Test.event', { data: 'test' })
|
|
120
|
+
|
|
121
|
+
expect(workflow.state.history).toHaveLength(1)
|
|
122
|
+
expect(workflow.state.history[0]).toMatchObject({
|
|
123
|
+
type: 'event',
|
|
124
|
+
name: 'Test.event',
|
|
125
|
+
data: { data: 'test' },
|
|
126
|
+
})
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
it('should trigger schedule handlers', async () => {
|
|
130
|
+
const handler = vi.fn()
|
|
131
|
+
|
|
132
|
+
const workflow = Workflow($ => {
|
|
133
|
+
$.every.seconds(1)(handler)
|
|
134
|
+
})
|
|
135
|
+
|
|
136
|
+
await workflow.start()
|
|
137
|
+
await vi.advanceTimersByTimeAsync(1000)
|
|
138
|
+
|
|
139
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
140
|
+
|
|
141
|
+
await vi.advanceTimersByTimeAsync(1000)
|
|
142
|
+
expect(handler).toHaveBeenCalledTimes(2)
|
|
143
|
+
|
|
144
|
+
await workflow.stop()
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('should stop schedule handlers on stop', async () => {
|
|
148
|
+
const handler = vi.fn()
|
|
149
|
+
|
|
150
|
+
const workflow = Workflow($ => {
|
|
151
|
+
$.every.seconds(1)(handler)
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
await workflow.start()
|
|
155
|
+
await vi.advanceTimersByTimeAsync(1000)
|
|
156
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
157
|
+
|
|
158
|
+
await workflow.stop()
|
|
159
|
+
await vi.advanceTimersByTimeAsync(5000)
|
|
160
|
+
expect(handler).toHaveBeenCalledTimes(1)
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('should support $.set and $.get for context data', async () => {
|
|
164
|
+
const workflow = Workflow($ => {
|
|
165
|
+
$.on.Test.set(async (data, $) => {
|
|
166
|
+
$.set('value', data.value)
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
$.on.Test.get(async (_, $) => {
|
|
170
|
+
const value = $.get<number>('value')
|
|
171
|
+
$.log('Got value', value)
|
|
172
|
+
})
|
|
173
|
+
})
|
|
174
|
+
|
|
175
|
+
await workflow.start()
|
|
176
|
+
await workflow.send('Test.set', { value: 42 })
|
|
177
|
+
|
|
178
|
+
expect(workflow.state.context.value).toBe(42)
|
|
179
|
+
expect(workflow.$.get('value')).toBe(42)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it('should use initial context from options', () => {
|
|
183
|
+
const workflow = Workflow(
|
|
184
|
+
$ => {},
|
|
185
|
+
{ context: { counter: 100 } }
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
expect(workflow.state.context.counter).toBe(100)
|
|
189
|
+
})
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
describe('parseEvent', () => {
|
|
193
|
+
it('should parse valid event strings', () => {
|
|
194
|
+
expect(parseEvent('Customer.created')).toEqual({
|
|
195
|
+
noun: 'Customer',
|
|
196
|
+
event: 'created',
|
|
197
|
+
})
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
it('should return null for invalid event strings', () => {
|
|
201
|
+
expect(parseEvent('invalid')).toBeNull()
|
|
202
|
+
expect(parseEvent('too.many.parts')).toBeNull()
|
|
203
|
+
expect(parseEvent('')).toBeNull()
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
describe('createTestContext', () => {
|
|
208
|
+
it('should create a $ context for testing', () => {
|
|
209
|
+
const $ = createTestContext()
|
|
210
|
+
|
|
211
|
+
expect($.send).toBeDefined()
|
|
212
|
+
expect($.on).toBeDefined()
|
|
213
|
+
expect($.every).toBeDefined()
|
|
214
|
+
expect($.log).toBeDefined()
|
|
215
|
+
expect($.get).toBeDefined()
|
|
216
|
+
expect($.set).toBeDefined()
|
|
217
|
+
expect($.getState).toBeDefined()
|
|
218
|
+
expect($.emittedEvents).toBeDefined()
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
it('should track emitted events', async () => {
|
|
222
|
+
const $ = createTestContext()
|
|
223
|
+
|
|
224
|
+
await $.send('Test.event1', { a: 1 })
|
|
225
|
+
await $.send('Test.event2', { b: 2 })
|
|
226
|
+
|
|
227
|
+
expect($.emittedEvents).toHaveLength(2)
|
|
228
|
+
expect($.emittedEvents[0]).toEqual({ event: 'Test.event1', data: { a: 1 } })
|
|
229
|
+
expect($.emittedEvents[1]).toEqual({ event: 'Test.event2', data: { b: 2 } })
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
it('should support get/set', () => {
|
|
233
|
+
const $ = createTestContext()
|
|
234
|
+
|
|
235
|
+
$.set('key', 'value')
|
|
236
|
+
expect($.get('key')).toBe('value')
|
|
237
|
+
})
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
describe('$.every patterns', () => {
|
|
241
|
+
it('should support $.every.hour', () => {
|
|
242
|
+
const workflow = Workflow($ => {
|
|
243
|
+
$.every.hour(() => {})
|
|
244
|
+
})
|
|
245
|
+
|
|
246
|
+
expect(workflow.definition.schedules[0]?.interval).toEqual({
|
|
247
|
+
type: 'cron',
|
|
248
|
+
expression: '0 * * * *',
|
|
249
|
+
natural: 'hour',
|
|
250
|
+
})
|
|
251
|
+
})
|
|
252
|
+
|
|
253
|
+
it('should support $.every.Monday.at9am', () => {
|
|
254
|
+
const workflow = Workflow($ => {
|
|
255
|
+
$.every.Monday.at9am(() => {})
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
expect(workflow.definition.schedules[0]?.interval).toEqual({
|
|
259
|
+
type: 'cron',
|
|
260
|
+
expression: '0 9 * * 1',
|
|
261
|
+
natural: 'Monday.at9am',
|
|
262
|
+
})
|
|
263
|
+
})
|
|
264
|
+
|
|
265
|
+
it('should support $.every.minutes(30)', () => {
|
|
266
|
+
const workflow = Workflow($ => {
|
|
267
|
+
$.every.minutes(30)(() => {})
|
|
268
|
+
})
|
|
269
|
+
|
|
270
|
+
expect(workflow.definition.schedules[0]?.interval).toEqual({
|
|
271
|
+
type: 'minute',
|
|
272
|
+
value: 30,
|
|
273
|
+
natural: '30 minutes',
|
|
274
|
+
})
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
it('should support $.every("natural language")', () => {
|
|
278
|
+
const workflow = Workflow($ => {
|
|
279
|
+
$.every('first Monday of the month', () => {})
|
|
280
|
+
})
|
|
281
|
+
|
|
282
|
+
expect(workflow.definition.schedules[0]?.interval).toEqual({
|
|
283
|
+
type: 'natural',
|
|
284
|
+
description: 'first Monday of the month',
|
|
285
|
+
})
|
|
286
|
+
})
|
|
287
|
+
})
|
|
288
|
+
})
|
package/tsconfig.json
ADDED
package/vitest.config.ts
ADDED
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 AI Primitives
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/index.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.test.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from '@jest/globals';
|
|
2
|
-
import { add } from './index.js';
|
|
3
|
-
describe('add', () => {
|
|
4
|
-
it('should add two numbers correctly', () => {
|
|
5
|
-
expect(add(2, 3)).toBe(5);
|
|
6
|
-
expect(add(-1, 1)).toBe(0);
|
|
7
|
-
expect(add(0, 0)).toBe(0);
|
|
8
|
-
});
|
|
9
|
-
});
|