ai-workflows 0.0.1 → 0.0.2
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/README.md +4 -0
- package/index.test.ts +9 -0
- package/index.ts +25 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,5 +10,9 @@ workflow.on('ticket.created', ticket => {
|
|
|
10
10
|
workflow.send('ticket.summarized', summary)
|
|
11
11
|
})
|
|
12
12
|
|
|
13
|
+
workflow.on('ticket.summarized', summary => ai.sentiment(summary).then('update.ticket'))
|
|
14
|
+
|
|
15
|
+
workflow.every('30 minutes during business hours', context => ai.reviewKPIs(context).then('slack.post'))
|
|
16
|
+
|
|
13
17
|
export default workflow
|
|
14
18
|
```
|
package/index.test.ts
ADDED
package/index.ts
CHANGED
|
@@ -4,9 +4,12 @@ import { AutoRouter } from 'itty-router'
|
|
|
4
4
|
export const ai: Record<string, any> = {}
|
|
5
5
|
|
|
6
6
|
export type WorkflowOptions = {
|
|
7
|
-
queue
|
|
7
|
+
queue?: string
|
|
8
|
+
dql?: string
|
|
8
9
|
}
|
|
9
10
|
|
|
11
|
+
// export let send: (event: string | WorkflowEvent) => Promise<any>
|
|
12
|
+
|
|
10
13
|
// TODO: Add generic type for the event data
|
|
11
14
|
export const Workflow = (options: WorkflowOptions) => {
|
|
12
15
|
|
|
@@ -17,8 +20,11 @@ export const Workflow = (options: WorkflowOptions) => {
|
|
|
17
20
|
|
|
18
21
|
const events: Record<string, any> = {}
|
|
19
22
|
|
|
20
|
-
const send = (env: any) => async (event: WorkflowEvent) => {
|
|
23
|
+
const send = (env: any) => async (event: string | WorkflowEvent) => {
|
|
21
24
|
if (env[queue] && env[queue].send) {
|
|
25
|
+
if (typeof event === 'string') {
|
|
26
|
+
return (data: any) => (env[queue] as Queue<WorkflowEvent>).send({ event, data })
|
|
27
|
+
}
|
|
22
28
|
return await (env[queue] as Queue<WorkflowEvent>).send(event)
|
|
23
29
|
}
|
|
24
30
|
throw new Error('Queue not found')
|
|
@@ -39,7 +45,23 @@ export const Workflow = (options: WorkflowOptions) => {
|
|
|
39
45
|
fetch: api.fetch,
|
|
40
46
|
queue: (batch: MessageBatch<WorkflowEvent>, env: any, ctx: ExecutionContext) => {
|
|
41
47
|
batch.messages.map(async (message) => {
|
|
42
|
-
|
|
48
|
+
const { event, data } = message.body
|
|
49
|
+
if (events[event]) {
|
|
50
|
+
try {
|
|
51
|
+
const results = await events[event](message.body, { ai, db: env.db, api: env.api, data, message, send: send(env) })
|
|
52
|
+
console.log(results)
|
|
53
|
+
message.ack()
|
|
54
|
+
// TODO: Try to support `workflow.on('ticket.summarized', summary => ai.sentiment(summary).then('update.ticket'))`
|
|
55
|
+
// if (typeof results === 'string') {
|
|
56
|
+
// const resultData = await results
|
|
57
|
+
// return send(env)({ event: results, data })
|
|
58
|
+
// }
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error(error)
|
|
61
|
+
message.retry()
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
throw new Error('Event not found')
|
|
43
65
|
})
|
|
44
66
|
},
|
|
45
67
|
}
|