ai-workflows 0.0.2 → 1.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/LICENSE +21 -0
- package/README.md +31 -4
- package/dist/index.d.ts +7 -0
- package/dist/index.js +9 -0
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +9 -0
- package/package.json +63 -26
- package/bun.lockb +0 -0
- package/index.test.ts +0 -9
- package/index.ts +0 -81
- package/tsconfig.json +0 -48
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
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/README.md
CHANGED
|
@@ -1,18 +1,45 @@
|
|
|
1
|
+
[](https://badge.fury.io/js/ai-workflows)
|
|
2
|
+
[](https://opensource.org/licenses/MIT)
|
|
3
|
+
|
|
1
4
|
# ai-workflows
|
|
2
5
|
|
|
6
|
+
Composable AI Workflows & Durable Execution Framework
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
ai-workflows is a powerful framework for building event-driven, AI-powered workflows with durable execution guarantees. It provides both a JavaScript API and MDX-based workflow definitions for maximum flexibility.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- 🎯 **Event-Driven Architecture** - Build reactive workflows that respond to external events
|
|
15
|
+
- 🤖 **AI Functions** - Built-in MDX-based AI functions structured data generation and processing
|
|
16
|
+
- ⏰ **Smart Scheduling** - Schedule tasks with natural language time expressions
|
|
17
|
+
- 📝 **Workflow Definitions** - Design workflows by composing AI functions and events
|
|
18
|
+
- 💪 **Durable Execution** - Reliable workflow execution with state persistence
|
|
19
|
+
|
|
20
|
+
## Installation
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install ai-workflows
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### JavaScript API
|
|
29
|
+
|
|
3
30
|
```javascript
|
|
4
31
|
import { ai, Workflow } from 'ai-workflows'
|
|
5
32
|
|
|
6
33
|
const workflow = Workflow()
|
|
7
34
|
|
|
8
|
-
workflow.on('ticket.created', ticket => {
|
|
35
|
+
workflow.on('ticket.created', (ticket) => {
|
|
9
36
|
const summary = ai.summarize(ticket)
|
|
10
37
|
workflow.send('ticket.summarized', summary)
|
|
11
38
|
})
|
|
12
39
|
|
|
13
|
-
workflow.on('ticket.summarized', summary => ai.sentiment(summary).then('update.ticket'))
|
|
40
|
+
workflow.on('ticket.summarized', (summary) => ai.sentiment(summary).then('update.ticket'))
|
|
14
41
|
|
|
15
|
-
workflow.every('30 minutes during business hours', context => ai.reviewKPIs(context).then('slack.post'))
|
|
42
|
+
workflow.every('30 minutes during business hours', (context) => ai.reviewKPIs(context).then('slack.post'))
|
|
16
43
|
|
|
17
44
|
export default workflow
|
|
18
|
-
```
|
|
45
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
});
|
package/package.json
CHANGED
|
@@ -1,42 +1,79 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai-workflows",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "Composable AI Workflows & Durable Execution Framework",
|
|
6
|
+
"main": "dist/index.js",
|
|
5
7
|
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"ai-workflows": "bin/cli.js"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"bin"
|
|
15
|
+
],
|
|
6
16
|
"scripts": {
|
|
7
17
|
"build": "tsc",
|
|
8
|
-
"
|
|
9
|
-
"test": "
|
|
18
|
+
"test": "jest",
|
|
19
|
+
"test:watch": "jest --watch",
|
|
20
|
+
"lint": "eslint src",
|
|
21
|
+
"format": "prettier --write \"src/**/*.{ts,tsx,js,jsx}\" \"*.{md,mdx}\"",
|
|
22
|
+
"prepublishOnly": "pnpm run build"
|
|
10
23
|
},
|
|
24
|
+
"keywords": [
|
|
25
|
+
"ai",
|
|
26
|
+
"workflows",
|
|
27
|
+
"mdx",
|
|
28
|
+
"durable-execution"
|
|
29
|
+
],
|
|
30
|
+
"author": "AI Primitives",
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"homepage": "https://mdx.org.ai",
|
|
11
33
|
"repository": {
|
|
12
34
|
"type": "git",
|
|
13
|
-
"url": "git+https://github.com/
|
|
35
|
+
"url": "git+https://github.com/ai-primitives/ai-workflows.git"
|
|
14
36
|
},
|
|
15
|
-
"keywords": [],
|
|
16
|
-
"author": "Nathan Clevenger",
|
|
17
|
-
"license": "MIT",
|
|
18
37
|
"bugs": {
|
|
19
|
-
"url": "https://github.com/
|
|
38
|
+
"url": "https://github.com/ai-primitives/ai-workflows/issues"
|
|
20
39
|
},
|
|
21
|
-
"homepage": "https://github.com/nathanclevenger/ai-workflows#readme",
|
|
22
40
|
"devDependencies": {
|
|
23
|
-
"@
|
|
24
|
-
"
|
|
25
|
-
"
|
|
41
|
+
"@babel/core": "^7.26.0",
|
|
42
|
+
"@babel/preset-env": "^7.26.0",
|
|
43
|
+
"@eslint/js": "^9.17.0",
|
|
44
|
+
"@jest/globals": "^29.7.0",
|
|
45
|
+
"@semantic-release/commit-analyzer": "^13.0.0",
|
|
46
|
+
"@semantic-release/github": "^11.0.1",
|
|
47
|
+
"@semantic-release/npm": "^12.0.1",
|
|
48
|
+
"@semantic-release/release-notes-generator": "^14.0.1",
|
|
49
|
+
"@testing-library/jest-dom": "^6.4.2",
|
|
50
|
+
"@testing-library/react": "^16.1.0",
|
|
51
|
+
"@types/jest": "^29.5.14",
|
|
52
|
+
"@types/js-yaml": "^4.0.9",
|
|
53
|
+
"@types/node": "^22.10.2",
|
|
54
|
+
"@types/react": "^18.3.17",
|
|
55
|
+
"@types/react-dom": "^18.2.0",
|
|
56
|
+
"@typescript-eslint/eslint-plugin": "^8.18.0",
|
|
57
|
+
"@typescript-eslint/parser": "^8.18.0",
|
|
58
|
+
"babel-jest": "^29.7.0",
|
|
59
|
+
"eslint": "^9.17.0",
|
|
60
|
+
"identity-obj-proxy": "^3.0.0",
|
|
61
|
+
"jest": "^29.7.0",
|
|
62
|
+
"jest-environment-jsdom": "^29.7.0",
|
|
63
|
+
"prettier": "^3.4.2",
|
|
64
|
+
"semantic-release": "^24.2.0",
|
|
65
|
+
"ts-jest": "^29.2.5",
|
|
66
|
+
"typescript": "^5.7.2"
|
|
26
67
|
},
|
|
27
68
|
"dependencies": {
|
|
28
|
-
"ai": "^
|
|
69
|
+
"@ai-sdk/openai": "^1.0.8",
|
|
70
|
+
"ai": "^4.0.18",
|
|
29
71
|
"ai-functions": "^0.2.19",
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"semi": false,
|
|
37
|
-
"singleQuote": true,
|
|
38
|
-
"trailingComma": "es5",
|
|
39
|
-
"tabWidth": 2,
|
|
40
|
-
"printWidth": 120
|
|
72
|
+
"gray-matter": "^4.0.3",
|
|
73
|
+
"mdxld": "^0.1.1",
|
|
74
|
+
"react": "^18.3.1",
|
|
75
|
+
"react-dom": "^18.2.0",
|
|
76
|
+
"yaml": "^2.6.1",
|
|
77
|
+
"zod": "^3.24.1"
|
|
41
78
|
}
|
|
42
|
-
}
|
|
79
|
+
}
|
package/bun.lockb
DELETED
|
Binary file
|
package/index.test.ts
DELETED
package/index.ts
DELETED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { AI } from 'ai-functions'
|
|
2
|
-
import { AutoRouter } from 'itty-router'
|
|
3
|
-
|
|
4
|
-
export const ai: Record<string, any> = {}
|
|
5
|
-
|
|
6
|
-
export type WorkflowOptions = {
|
|
7
|
-
queue?: string
|
|
8
|
-
dql?: string
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
// export let send: (event: string | WorkflowEvent) => Promise<any>
|
|
12
|
-
|
|
13
|
-
// TODO: Add generic type for the event data
|
|
14
|
-
export const Workflow = (options: WorkflowOptions) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const { queue = 'workflows' } = options
|
|
18
|
-
|
|
19
|
-
const api = AutoRouter()
|
|
20
|
-
|
|
21
|
-
const events: Record<string, any> = {}
|
|
22
|
-
|
|
23
|
-
const send = (env: any) => async (event: string | WorkflowEvent) => {
|
|
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
|
-
}
|
|
28
|
-
return await (env[queue] as Queue<WorkflowEvent>).send(event)
|
|
29
|
-
}
|
|
30
|
-
throw new Error('Queue not found')
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
on: (event: string, handler: WorkflowEventHandler) => {
|
|
35
|
-
events[event] = handler
|
|
36
|
-
api.get('/' + event, async (req, env, ctx) => {
|
|
37
|
-
const data = Object.fromEntries(new URL(req.url).searchParams)
|
|
38
|
-
return send(env)({ event, data })
|
|
39
|
-
})
|
|
40
|
-
api.post('/' + event, async (req, env, ctx) => {
|
|
41
|
-
const data = await req.json()
|
|
42
|
-
return send(env)({ event, data })
|
|
43
|
-
})
|
|
44
|
-
},
|
|
45
|
-
fetch: api.fetch,
|
|
46
|
-
queue: (batch: MessageBatch<WorkflowEvent>, env: any, ctx: ExecutionContext) => {
|
|
47
|
-
batch.messages.map(async (message) => {
|
|
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')
|
|
65
|
-
})
|
|
66
|
-
},
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export type WorkflowEvent<T = any> = {
|
|
71
|
-
event: string
|
|
72
|
-
data: T
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export type WorkflowContext = {
|
|
76
|
-
ai: Record<string, any>
|
|
77
|
-
db: Record<string, any>
|
|
78
|
-
api: Record<string, any>
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export type WorkflowEventHandler = (event: WorkflowEvent, context: WorkflowContext) => Promise<any>
|
package/tsconfig.json
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
|
4
|
-
|
|
5
|
-
/* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
|
6
|
-
"target": "es2021",
|
|
7
|
-
/* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
8
|
-
"lib": ["es2021"],
|
|
9
|
-
/* Specify what JSX code is generated. */
|
|
10
|
-
"jsx": "react-jsx",
|
|
11
|
-
"jsxImportSource": "hono/jsx",
|
|
12
|
-
|
|
13
|
-
/* Specify what module code is generated. */
|
|
14
|
-
"module": "es2022",
|
|
15
|
-
/* Specify how TypeScript looks up a file from a given module specifier. */
|
|
16
|
-
"moduleResolution": "Bundler",
|
|
17
|
-
/* Specify type package names to be included without being referenced in a source file. */
|
|
18
|
-
"types": ["@cloudflare/workers-types/2023-07-01"],
|
|
19
|
-
/* Enable importing .json files */
|
|
20
|
-
"resolveJsonModule": true,
|
|
21
|
-
|
|
22
|
-
/* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
|
23
|
-
"allowJs": true,
|
|
24
|
-
/* Enable error reporting in type-checked JavaScript files. */
|
|
25
|
-
"checkJs": false,
|
|
26
|
-
|
|
27
|
-
/* Disable emitting files from a compilation. */
|
|
28
|
-
"noEmit": false,
|
|
29
|
-
|
|
30
|
-
/* Emit the compiled files to the ./dist folder */
|
|
31
|
-
"outDir": "./dist",
|
|
32
|
-
|
|
33
|
-
/* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
34
|
-
"isolatedModules": true,
|
|
35
|
-
/* Allow 'import x from y' when a module doesn't have a default export. */
|
|
36
|
-
"allowSyntheticDefaultImports": true,
|
|
37
|
-
/* Ensure that casing is correct in imports. */
|
|
38
|
-
"forceConsistentCasingInFileNames": true,
|
|
39
|
-
|
|
40
|
-
/* Enable all strict type-checking options. */
|
|
41
|
-
"strict": true,
|
|
42
|
-
|
|
43
|
-
/* Skip type checking all .d.ts files. */
|
|
44
|
-
"skipLibCheck": true
|
|
45
|
-
},
|
|
46
|
-
"exclude": ["test"],
|
|
47
|
-
"include": ["types.d.ts", "**/*.ts", "**/*.tsx"]
|
|
48
|
-
}
|