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
package/src/types.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Types for ai-workflows
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Handler function with source code for remote execution
|
|
7
|
+
*/
|
|
8
|
+
export interface HandlerFunction<T = unknown> {
|
|
9
|
+
/** The actual function */
|
|
10
|
+
fn: (...args: any[]) => void | Promise<void>
|
|
11
|
+
/** Source code string for remote execution */
|
|
12
|
+
source: string
|
|
13
|
+
/** Handler name (for debugging) */
|
|
14
|
+
name?: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Event handler function type
|
|
19
|
+
* Can return void (for send) or a result (for do/try)
|
|
20
|
+
*/
|
|
21
|
+
export type EventHandler<T = unknown, R = unknown> = (
|
|
22
|
+
data: T,
|
|
23
|
+
$: WorkflowContext
|
|
24
|
+
) => R | void | Promise<R | void>
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Schedule handler function type
|
|
28
|
+
*/
|
|
29
|
+
export type ScheduleHandler = (
|
|
30
|
+
$: WorkflowContext
|
|
31
|
+
) => void | Promise<void>
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Workflow context ($) passed to handlers
|
|
35
|
+
*/
|
|
36
|
+
export interface WorkflowContext {
|
|
37
|
+
/**
|
|
38
|
+
* Send an event (fire and forget, durable)
|
|
39
|
+
* Confirms receipt but doesn't wait for result
|
|
40
|
+
*/
|
|
41
|
+
send: <T = unknown>(event: string, data: T) => Promise<void>
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Do an action (durable, waits for result)
|
|
45
|
+
* Retries on failure, stores result durably
|
|
46
|
+
*/
|
|
47
|
+
do: <TData = unknown, TResult = unknown>(event: string, data: TData) => Promise<TResult>
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Try an action (non-durable, waits for result)
|
|
51
|
+
* Simple execution without durability guarantees
|
|
52
|
+
*/
|
|
53
|
+
try: <TData = unknown, TResult = unknown>(event: string, data: TData) => Promise<TResult>
|
|
54
|
+
|
|
55
|
+
/** Register event handler ($.on.Noun.event) */
|
|
56
|
+
on: OnProxy
|
|
57
|
+
|
|
58
|
+
/** Register schedule handler ($.every.hour, $.every.Monday.at9am) */
|
|
59
|
+
every: EveryProxy
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Workflow state - read/write context data
|
|
63
|
+
* $.state.userId = '123'
|
|
64
|
+
* const id = $.state.userId
|
|
65
|
+
*/
|
|
66
|
+
state: Record<string, unknown>
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Get the full workflow state (context + history)
|
|
70
|
+
* Returns a copy to prevent mutation
|
|
71
|
+
*/
|
|
72
|
+
getState: () => WorkflowState
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Set a value in the context
|
|
76
|
+
* Alternative to $.state.key = value
|
|
77
|
+
*/
|
|
78
|
+
set: <T = unknown>(key: string, value: T) => void
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get a value from the context
|
|
82
|
+
* Alternative to $.state.key
|
|
83
|
+
*/
|
|
84
|
+
get: <T = unknown>(key: string) => T | undefined
|
|
85
|
+
|
|
86
|
+
/** Log message */
|
|
87
|
+
log: (message: string, data?: unknown) => void
|
|
88
|
+
|
|
89
|
+
/** Access to database (if connected) */
|
|
90
|
+
db?: DatabaseContext
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Database context for workflows
|
|
95
|
+
*/
|
|
96
|
+
export interface DatabaseContext {
|
|
97
|
+
/** Record an event (immutable) */
|
|
98
|
+
recordEvent: (event: string, data: unknown) => Promise<void>
|
|
99
|
+
|
|
100
|
+
/** Create an action (pending work) */
|
|
101
|
+
createAction: (action: ActionData) => Promise<void>
|
|
102
|
+
|
|
103
|
+
/** Complete an action */
|
|
104
|
+
completeAction: (id: string, result: unknown) => Promise<void>
|
|
105
|
+
|
|
106
|
+
/** Store an artifact */
|
|
107
|
+
storeArtifact: (artifact: ArtifactData) => Promise<void>
|
|
108
|
+
|
|
109
|
+
/** Get an artifact */
|
|
110
|
+
getArtifact: (key: string) => Promise<unknown | null>
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Action data for pending/active work
|
|
115
|
+
*/
|
|
116
|
+
export interface ActionData {
|
|
117
|
+
/** Actor performing the action (user, agent, system) */
|
|
118
|
+
actor: string
|
|
119
|
+
/** Object being acted upon */
|
|
120
|
+
object: string
|
|
121
|
+
/** Action being performed */
|
|
122
|
+
action: string
|
|
123
|
+
/** Current status */
|
|
124
|
+
status?: 'pending' | 'active' | 'completed' | 'failed'
|
|
125
|
+
/** Optional metadata */
|
|
126
|
+
metadata?: Record<string, unknown>
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Artifact data for cached compiled/parsed content
|
|
131
|
+
*/
|
|
132
|
+
export interface ArtifactData {
|
|
133
|
+
/** Unique key for the artifact */
|
|
134
|
+
key: string
|
|
135
|
+
/** Type of artifact (ast, types, esm, worker, html, markdown) */
|
|
136
|
+
type: 'ast' | 'types' | 'esm' | 'worker' | 'html' | 'markdown' | 'bundle' | string
|
|
137
|
+
/** Source hash (for cache invalidation) */
|
|
138
|
+
sourceHash: string
|
|
139
|
+
/** The artifact content */
|
|
140
|
+
content: unknown
|
|
141
|
+
/** Optional metadata */
|
|
142
|
+
metadata?: Record<string, unknown>
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Event proxy type for $.on.Noun.event pattern
|
|
147
|
+
*/
|
|
148
|
+
export type OnProxy = {
|
|
149
|
+
[noun: string]: {
|
|
150
|
+
[event: string]: (handler: EventHandler) => void
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Every proxy type for $.every patterns
|
|
156
|
+
*/
|
|
157
|
+
export type EveryProxy = {
|
|
158
|
+
(description: string, handler: ScheduleHandler): void
|
|
159
|
+
} & {
|
|
160
|
+
[key: string]: ((handler: ScheduleHandler) => void) | ((value: number) => (handler: ScheduleHandler) => void) | {
|
|
161
|
+
[timeKey: string]: (handler: ScheduleHandler) => void
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Workflow state
|
|
167
|
+
*/
|
|
168
|
+
export interface WorkflowState {
|
|
169
|
+
/** Current state name (for state machines) */
|
|
170
|
+
current?: string
|
|
171
|
+
/** Context data */
|
|
172
|
+
context: Record<string, unknown>
|
|
173
|
+
/** Execution history */
|
|
174
|
+
history: WorkflowHistoryEntry[]
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* History entry for workflow execution
|
|
179
|
+
*/
|
|
180
|
+
export interface WorkflowHistoryEntry {
|
|
181
|
+
timestamp: number
|
|
182
|
+
type: 'event' | 'schedule' | 'transition' | 'action'
|
|
183
|
+
name: string
|
|
184
|
+
data?: unknown
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Event registration with source
|
|
189
|
+
*/
|
|
190
|
+
export interface EventRegistration {
|
|
191
|
+
noun: string
|
|
192
|
+
event: string
|
|
193
|
+
handler: EventHandler
|
|
194
|
+
source: string
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Schedule registration with source
|
|
199
|
+
*/
|
|
200
|
+
export interface ScheduleRegistration {
|
|
201
|
+
interval: ScheduleInterval
|
|
202
|
+
handler: ScheduleHandler
|
|
203
|
+
source: string
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Schedule intervals
|
|
208
|
+
*/
|
|
209
|
+
export type ScheduleInterval =
|
|
210
|
+
| { type: 'second'; value?: number; natural?: string }
|
|
211
|
+
| { type: 'minute'; value?: number; natural?: string }
|
|
212
|
+
| { type: 'hour'; value?: number; natural?: string }
|
|
213
|
+
| { type: 'day'; value?: number; natural?: string }
|
|
214
|
+
| { type: 'week'; value?: number; natural?: string }
|
|
215
|
+
| { type: 'cron'; expression: string; natural?: string }
|
|
216
|
+
| { type: 'natural'; description: string }
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Workflow definition
|
|
220
|
+
*/
|
|
221
|
+
export interface WorkflowDefinition {
|
|
222
|
+
name: string
|
|
223
|
+
events: EventRegistration[]
|
|
224
|
+
schedules: ScheduleRegistration[]
|
|
225
|
+
initialContext?: Record<string, unknown>
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Parsed event name (Noun.event format)
|
|
230
|
+
*/
|
|
231
|
+
export interface ParsedEvent {
|
|
232
|
+
noun: string
|
|
233
|
+
event: string
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Workflow options
|
|
238
|
+
*/
|
|
239
|
+
export interface WorkflowOptions {
|
|
240
|
+
/** Initial context data */
|
|
241
|
+
context?: Record<string, unknown>
|
|
242
|
+
/** Database connection for persistence */
|
|
243
|
+
db?: DatabaseContext
|
|
244
|
+
}
|