@statelyai/flow 0.0.1
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/.vscode/launch.json +34 -0
- package/LICENSE +21 -0
- package/README.md +15 -0
- package/package.json +20 -0
- package/src/createFlow.test.ts +204 -0
- package/src/index.ts +62 -0
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"configurations": [
|
|
5
|
+
{
|
|
6
|
+
"type": "node",
|
|
7
|
+
"request": "launch",
|
|
8
|
+
"name": "Debug Current Test File",
|
|
9
|
+
"autoAttachChildProcesses": true,
|
|
10
|
+
"skipFiles": ["<node_internals>/**", "**/node_modules/**", "examples/**"],
|
|
11
|
+
"program": "${workspaceRoot}/node_modules/vitest/vitest.mjs",
|
|
12
|
+
"args": ["run", "${relativeFile}"],
|
|
13
|
+
"smartStep": true,
|
|
14
|
+
"console": "integratedTerminal"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"type": "node",
|
|
18
|
+
"request": "launch",
|
|
19
|
+
"name": "Debug Current File",
|
|
20
|
+
"program": "${file}",
|
|
21
|
+
"cwd": "${workspaceFolder}",
|
|
22
|
+
"runtimeExecutable": "${workspaceFolder}/node_modules/.bin/ts-node",
|
|
23
|
+
"runtimeArgs": ["--transpile-only", "-r", "dotenv/config"],
|
|
24
|
+
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
|
|
25
|
+
"sourceMaps": true,
|
|
26
|
+
"smartStep": true,
|
|
27
|
+
"resolveSourceMapLocations": [
|
|
28
|
+
"${workspaceFolder}/**",
|
|
29
|
+
"!**/node_modules/**"
|
|
30
|
+
],
|
|
31
|
+
"console": "integratedTerminal"
|
|
32
|
+
}
|
|
33
|
+
]
|
|
34
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Stately
|
|
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
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# AgentFlow
|
|
2
|
+
|
|
3
|
+
> **Work in progress** — a lightweight TypeScript library for defining composable, type-safe workflows with plain async functions.
|
|
4
|
+
|
|
5
|
+
**AgentFlow** lets you orchestrate multi-step flows by composing simple functions into declarative, testable flows. Think of it as a tiny state machine.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## ✨ Features
|
|
10
|
+
|
|
11
|
+
- ✅ **Composable steps** — each step is just an async function you can test in isolation
|
|
12
|
+
- ✅ **Declarative orchestration** — define what happens after each step completes
|
|
13
|
+
- ✅ **Type-safe transitions** — transitions between steps are fully inferred
|
|
14
|
+
- ✅ **Lightweight** — no runtime dependencies
|
|
15
|
+
- ✅ **Future integration** — designed to integrate naturally with [Vercel AI SDK](https://vercel.com/docs/ai/vercel-ai-sdk)
|
package/package.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@statelyai/flow",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "A lightweight TypeScript library for defining composable, type-safe workflows with plain async functions.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "tsup src/index.ts --format cjs,esm --dts",
|
|
8
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"author": "david@stately.ai",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"packageManager": "pnpm@10.11.1",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"ts-node": "^10.9.2",
|
|
16
|
+
"tsup": "^8.5.0",
|
|
17
|
+
"typescript": "^5.8.3",
|
|
18
|
+
"vitest": "^3.2.4"
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import { createFlow } from '.';
|
|
2
|
+
|
|
3
|
+
// Types for our email flow
|
|
4
|
+
type EmailData = {
|
|
5
|
+
subject: string;
|
|
6
|
+
bodyPrompt: string;
|
|
7
|
+
body: string;
|
|
8
|
+
recipient?: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
type RefinementRequest = {
|
|
12
|
+
emailData: EmailData;
|
|
13
|
+
feedback: string;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
// Email builder flow
|
|
17
|
+
const emailBuilderFlow = createFlow({
|
|
18
|
+
initial: 'askForSubject',
|
|
19
|
+
steps: {
|
|
20
|
+
askForSubject: {
|
|
21
|
+
task: async (initialData?: any) => {
|
|
22
|
+
// In a real app, this would be a UI prompt
|
|
23
|
+
console.log('📧 Email Builder Started');
|
|
24
|
+
console.log('Please enter the email subject:');
|
|
25
|
+
|
|
26
|
+
// Simulating user input - in real app this would be from UI
|
|
27
|
+
const subject = 'Meeting Follow-up - Q4 Planning';
|
|
28
|
+
console.log(`Subject: ${subject}`);
|
|
29
|
+
|
|
30
|
+
return subject;
|
|
31
|
+
},
|
|
32
|
+
onDone: (subject: string, steps) => {
|
|
33
|
+
return steps.askForBodyPrompt(subject);
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
askForBodyPrompt: {
|
|
37
|
+
task: async (subject: string) => {
|
|
38
|
+
console.log('\nPlease describe what you want in the email body:');
|
|
39
|
+
|
|
40
|
+
// Simulating user input
|
|
41
|
+
const bodyPrompt =
|
|
42
|
+
'Write a professional follow-up email for our Q4 planning meeting. Include key action items and next steps.';
|
|
43
|
+
console.log(`Body prompt: ${bodyPrompt}`);
|
|
44
|
+
|
|
45
|
+
return { subject, bodyPrompt };
|
|
46
|
+
},
|
|
47
|
+
onDone: (data: { subject: string; bodyPrompt: string }, steps) => {
|
|
48
|
+
return steps.generateBody(data);
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
generateBody: {
|
|
52
|
+
task: async (data: { subject: string; bodyPrompt: string }) => {
|
|
53
|
+
console.log('\n🤖 Generating email body...');
|
|
54
|
+
|
|
55
|
+
// Simulating AI generation
|
|
56
|
+
const generatedBody = `Dear Team,
|
|
57
|
+
|
|
58
|
+
Thank you for attending our Q4 planning meeting yesterday. I wanted to follow up on the key points we discussed and outline the next steps.
|
|
59
|
+
|
|
60
|
+
Key Action Items:
|
|
61
|
+
• Review and finalize budget allocations by Friday
|
|
62
|
+
• Schedule follow-up meetings with department heads
|
|
63
|
+
• Prepare presentation materials for the board meeting
|
|
64
|
+
|
|
65
|
+
Next Steps:
|
|
66
|
+
1. Complete individual department assessments
|
|
67
|
+
2. Submit resource requirements by EOD Wednesday
|
|
68
|
+
3. Schedule the next planning session for next week
|
|
69
|
+
|
|
70
|
+
Please let me know if you have any questions or need clarification on any of these items.
|
|
71
|
+
|
|
72
|
+
Best regards,
|
|
73
|
+
[Your name]`;
|
|
74
|
+
|
|
75
|
+
console.log('Generated body:');
|
|
76
|
+
console.log(generatedBody);
|
|
77
|
+
|
|
78
|
+
return { ...data, body: generatedBody };
|
|
79
|
+
},
|
|
80
|
+
onDone: (
|
|
81
|
+
emailData: { subject: string; bodyPrompt: string; body: string },
|
|
82
|
+
steps
|
|
83
|
+
) => {
|
|
84
|
+
return steps.askForRefinements(emailData);
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
askForRefinements: {
|
|
88
|
+
task: async (emailData: EmailData) => {
|
|
89
|
+
console.log('\nWould you like to refine the email? (yes/no):');
|
|
90
|
+
|
|
91
|
+
// Simulating user input - in real app this would be from UI
|
|
92
|
+
const wantsRefinement = true; // Change to false to skip refinements
|
|
93
|
+
console.log(`User response: ${wantsRefinement ? 'yes' : 'no'}`);
|
|
94
|
+
|
|
95
|
+
if (wantsRefinement) {
|
|
96
|
+
console.log('Please provide feedback for improvements:');
|
|
97
|
+
const feedback = 'Make it more concise and add a call-to-action';
|
|
98
|
+
console.log(`Feedback: ${feedback}`);
|
|
99
|
+
|
|
100
|
+
return { emailData, feedback };
|
|
101
|
+
} else {
|
|
102
|
+
return { emailData, feedback: null };
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
onDone: (
|
|
106
|
+
data: RefinementRequest | { emailData: EmailData; feedback: null },
|
|
107
|
+
steps
|
|
108
|
+
) => {
|
|
109
|
+
if (data.feedback) {
|
|
110
|
+
return steps.refineBody(data as RefinementRequest);
|
|
111
|
+
} else {
|
|
112
|
+
return steps.askForRecipient(data.emailData);
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
refineBody: {
|
|
117
|
+
task: async (data: RefinementRequest) => {
|
|
118
|
+
console.log('\n🤖 Refining email body based on feedback...');
|
|
119
|
+
|
|
120
|
+
// Simulating AI refinement
|
|
121
|
+
const refinedBody = `Dear Team,
|
|
122
|
+
|
|
123
|
+
Thank you for attending our Q4 planning meeting yesterday.
|
|
124
|
+
|
|
125
|
+
Key Action Items:
|
|
126
|
+
• Review and finalize budget allocations by Friday
|
|
127
|
+
• Schedule follow-up meetings with department heads
|
|
128
|
+
• Prepare presentation materials for the board meeting
|
|
129
|
+
|
|
130
|
+
Next Steps:
|
|
131
|
+
1. Complete individual department assessments
|
|
132
|
+
2. Submit resource requirements by EOD Wednesday
|
|
133
|
+
3. Schedule the next planning session for next week
|
|
134
|
+
|
|
135
|
+
Please confirm your availability for the follow-up meetings and submit your budget proposals by Friday.
|
|
136
|
+
|
|
137
|
+
Best regards,
|
|
138
|
+
[Your name]`;
|
|
139
|
+
|
|
140
|
+
console.log('Refined body:');
|
|
141
|
+
console.log(refinedBody);
|
|
142
|
+
|
|
143
|
+
return { ...data.emailData, body: refinedBody };
|
|
144
|
+
},
|
|
145
|
+
onDone: (emailData: EmailData, steps) => {
|
|
146
|
+
return steps.askForRefinements(emailData);
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
askForRecipient: {
|
|
150
|
+
task: async (emailData: EmailData) => {
|
|
151
|
+
console.log('\nPlease enter the recipient email address:');
|
|
152
|
+
|
|
153
|
+
// Simulating user input
|
|
154
|
+
const recipient = 'team@company.com';
|
|
155
|
+
console.log(`Recipient: ${recipient}`);
|
|
156
|
+
|
|
157
|
+
return { ...emailData, recipient };
|
|
158
|
+
},
|
|
159
|
+
onDone: (emailData: EmailData, steps) => {
|
|
160
|
+
return steps.sendEmail(emailData);
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
sendEmail: {
|
|
164
|
+
task: async (emailData: EmailData) => {
|
|
165
|
+
console.log('\n📤 Sending email...');
|
|
166
|
+
console.log('To:', emailData.recipient);
|
|
167
|
+
console.log('Subject:', emailData.subject);
|
|
168
|
+
console.log('Body:', emailData.body);
|
|
169
|
+
|
|
170
|
+
// Simulating email sending
|
|
171
|
+
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
172
|
+
|
|
173
|
+
const result = {
|
|
174
|
+
success: true,
|
|
175
|
+
messageId: 'msg_' + Date.now(),
|
|
176
|
+
sentAt: new Date().toISOString(),
|
|
177
|
+
emailData,
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
console.log('✅ Email sent successfully!');
|
|
181
|
+
console.log('Message ID:', result.messageId);
|
|
182
|
+
|
|
183
|
+
return result;
|
|
184
|
+
},
|
|
185
|
+
onDone: (result, steps) => {
|
|
186
|
+
// This won't be called since final: true, but required by type
|
|
187
|
+
return steps.sendEmail(result.emailData);
|
|
188
|
+
},
|
|
189
|
+
final: true,
|
|
190
|
+
},
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
// Example usage
|
|
195
|
+
console.log('🚀 Starting Email Builder Flow...\n');
|
|
196
|
+
emailBuilderFlow
|
|
197
|
+
.run(undefined)
|
|
198
|
+
.then((result) => {
|
|
199
|
+
console.log('\n🎉 Flow completed!');
|
|
200
|
+
console.log('Final result:', JSON.stringify(result, null, 2));
|
|
201
|
+
})
|
|
202
|
+
.catch((error) => {
|
|
203
|
+
console.error('❌ Flow failed:', error);
|
|
204
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
type StepTask<I, O> = (input: I) => Promise<O>;
|
|
2
|
+
|
|
3
|
+
type StepInstruction<S extends string, P> = { nextStep: S; params: P };
|
|
4
|
+
|
|
5
|
+
type FlowDefinition<
|
|
6
|
+
Steps extends Record<string, any>,
|
|
7
|
+
Initial extends keyof Steps
|
|
8
|
+
> = {
|
|
9
|
+
initial: Initial;
|
|
10
|
+
steps: {
|
|
11
|
+
[K in keyof Steps]: {
|
|
12
|
+
task: Steps[K];
|
|
13
|
+
onDone: (
|
|
14
|
+
result: Awaited<ReturnType<Steps[K]>>,
|
|
15
|
+
steps: {
|
|
16
|
+
[J in keyof Steps & string]: (
|
|
17
|
+
input: Parameters<Steps[J]>[0]
|
|
18
|
+
) => StepInstruction<J, Parameters<Steps[J]>[0]>;
|
|
19
|
+
}
|
|
20
|
+
) => StepInstruction<keyof Steps & string, any>;
|
|
21
|
+
final?: boolean;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export function createFlow<
|
|
27
|
+
Steps extends Record<string, StepTask<any, any>>,
|
|
28
|
+
Initial extends keyof Steps
|
|
29
|
+
>(config: FlowDefinition<Steps, Initial>) {
|
|
30
|
+
async function run(input: Parameters<Steps[Initial]>[0]): Promise<any> {
|
|
31
|
+
let currentStep: keyof Steps = config.initial;
|
|
32
|
+
let currentInput: any = input;
|
|
33
|
+
|
|
34
|
+
while (true) {
|
|
35
|
+
const { task, onDone, final } = config.steps[currentStep];
|
|
36
|
+
const result = await task(currentInput);
|
|
37
|
+
|
|
38
|
+
if (final) {
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const helpers = buildHelpers();
|
|
43
|
+
const next = onDone(result, helpers);
|
|
44
|
+
|
|
45
|
+
currentStep = next.nextStep;
|
|
46
|
+
currentInput = next.params;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function buildHelpers() {
|
|
51
|
+
const helpers: any = {};
|
|
52
|
+
for (const k in config.steps) {
|
|
53
|
+
helpers[k] = (input: any) => ({
|
|
54
|
+
nextStep: k,
|
|
55
|
+
params: input,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return helpers;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return { run };
|
|
62
|
+
}
|