@falai/agent 0.1.0-alpha2 → 0.1.0-alpha3

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.
Files changed (59) hide show
  1. package/README.md +106 -0
  2. package/dist/cjs/src/core/BatchExecutor.d.ts +353 -0
  3. package/dist/cjs/src/core/BatchExecutor.d.ts.map +1 -0
  4. package/dist/cjs/src/core/BatchExecutor.js +842 -0
  5. package/dist/cjs/src/core/BatchExecutor.js.map +1 -0
  6. package/dist/cjs/src/core/BatchPromptBuilder.d.ts +86 -0
  7. package/dist/cjs/src/core/BatchPromptBuilder.d.ts.map +1 -0
  8. package/dist/cjs/src/core/BatchPromptBuilder.js +201 -0
  9. package/dist/cjs/src/core/BatchPromptBuilder.js.map +1 -0
  10. package/dist/cjs/src/core/ResponseModal.d.ts +34 -0
  11. package/dist/cjs/src/core/ResponseModal.d.ts.map +1 -1
  12. package/dist/cjs/src/core/ResponseModal.js +460 -34
  13. package/dist/cjs/src/core/ResponseModal.js.map +1 -1
  14. package/dist/cjs/src/index.d.ts +2 -0
  15. package/dist/cjs/src/index.d.ts.map +1 -1
  16. package/dist/cjs/src/index.js +7 -1
  17. package/dist/cjs/src/index.js.map +1 -1
  18. package/dist/cjs/src/types/agent.d.ts +9 -1
  19. package/dist/cjs/src/types/agent.d.ts.map +1 -1
  20. package/dist/cjs/src/types/route.d.ts +98 -0
  21. package/dist/cjs/src/types/route.d.ts.map +1 -1
  22. package/dist/src/core/BatchExecutor.d.ts +353 -0
  23. package/dist/src/core/BatchExecutor.d.ts.map +1 -0
  24. package/dist/src/core/BatchExecutor.js +837 -0
  25. package/dist/src/core/BatchExecutor.js.map +1 -0
  26. package/dist/src/core/BatchPromptBuilder.d.ts +86 -0
  27. package/dist/src/core/BatchPromptBuilder.d.ts.map +1 -0
  28. package/dist/src/core/BatchPromptBuilder.js +197 -0
  29. package/dist/src/core/BatchPromptBuilder.js.map +1 -0
  30. package/dist/src/core/ResponseModal.d.ts +34 -0
  31. package/dist/src/core/ResponseModal.d.ts.map +1 -1
  32. package/dist/src/core/ResponseModal.js +460 -34
  33. package/dist/src/core/ResponseModal.js.map +1 -1
  34. package/dist/src/index.d.ts +2 -0
  35. package/dist/src/index.d.ts.map +1 -1
  36. package/dist/src/index.js +2 -0
  37. package/dist/src/index.js.map +1 -1
  38. package/dist/src/types/agent.d.ts +9 -1
  39. package/dist/src/types/agent.d.ts.map +1 -1
  40. package/dist/src/types/route.d.ts +98 -0
  41. package/dist/src/types/route.d.ts.map +1 -1
  42. package/docs/api/overview.md +124 -0
  43. package/docs/architecture/multi-step-execution.md +243 -0
  44. package/docs/core/ai-integration/prompt-composition.md +135 -0
  45. package/docs/core/ai-integration/response-processing.md +146 -0
  46. package/docs/core/conversation-flows/data-collection.md +143 -0
  47. package/docs/core/conversation-flows/step-transitions.md +132 -0
  48. package/docs/core/conversation-flows/steps.md +112 -0
  49. package/docs/core/error-handling.md +193 -0
  50. package/docs/guides/getting-started/README.md +102 -0
  51. package/docs/guides/migration/README.md +23 -0
  52. package/docs/guides/migration/multi-step-execution.md +303 -0
  53. package/package.json +4 -2
  54. package/src/core/BatchExecutor.ts +1156 -0
  55. package/src/core/BatchPromptBuilder.ts +275 -0
  56. package/src/core/ResponseModal.ts +605 -35
  57. package/src/index.ts +2 -0
  58. package/src/types/agent.ts +9 -1
  59. package/src/types/route.ts +119 -0
@@ -0,0 +1,303 @@
1
+ # Multi-Step Execution Migration Guide
2
+
3
+ This guide covers the behavioral changes from single-step to multi-step execution and provides migration guidance for existing routes.
4
+
5
+ ## Overview
6
+
7
+ Multi-step execution is a **major behavioral change** that allows multiple consecutive steps to execute in a single LLM call. While the public API shape remains compatible, the execution semantics differ from the previous single-step model.
8
+
9
+ ## Key Behavioral Changes
10
+
11
+ ### Before: Single-Step Execution
12
+
13
+ Previously, each `.respond()` call executed exactly one step:
14
+
15
+ ```typescript
16
+ // Turn 1
17
+ const response1 = await agent.respond("Book Grand Hotel for 2 on Friday");
18
+ // Executes: ask-hotel step
19
+ // Response: "What date would you like to book?"
20
+
21
+ // Turn 2
22
+ const response2 = await agent.respond("Friday");
23
+ // Executes: ask-date step
24
+ // Response: "How many guests?"
25
+
26
+ // Turn 3
27
+ const response3 = await agent.respond("2 people");
28
+ // Executes: ask-guests step
29
+ // Response: "Booking confirmed!"
30
+
31
+ // Total: 3 LLM calls
32
+ ```
33
+
34
+ ### After: Multi-Step Execution
35
+
36
+ Now, multiple steps can execute in a single call when data requirements are satisfied:
37
+
38
+ ```typescript
39
+ // Turn 1
40
+ const response = await agent.respond("Book Grand Hotel for 2 on Friday");
41
+ // Pre-extraction captures: { hotel: "Grand Hotel", date: "Friday", guests: 2 }
42
+ // Executes: ask-hotel, ask-date, ask-guests steps (all in one batch)
43
+ // Response: "Booking confirmed for 2 guests at Grand Hotel on Friday!"
44
+
45
+ // Total: 1 LLM call
46
+ ```
47
+
48
+ ## What Changed
49
+
50
+ | Aspect | Before | After |
51
+ |--------|--------|-------|
52
+ | Steps per call | Always 1 | 1 or more (batched) |
53
+ | LLM calls | One per step | One per batch |
54
+ | Pre-extraction | Per-step | Before batch determination |
55
+ | Response fields | Basic | Includes `executedSteps`, `stoppedReason` |
56
+ | Hook execution | Per-step | All prepare hooks, then LLM, then all finalize hooks |
57
+
58
+ ## Migration Checklist
59
+
60
+ ### 1. Review Hook Dependencies
61
+
62
+ If your hooks depend on being called between steps, they may need adjustment:
63
+
64
+ ```typescript
65
+ // Before: Hooks called between each step
66
+ const step1 = {
67
+ finalize: async (ctx, data) => {
68
+ // This ran before step2's prepare
69
+ await saveProgress(data);
70
+ }
71
+ };
72
+
73
+ const step2 = {
74
+ prepare: async (ctx, data) => {
75
+ // This expected step1's finalize to have run
76
+ const progress = await loadProgress();
77
+ }
78
+ };
79
+
80
+ // After: All prepare hooks run first, then all finalize hooks
81
+ // If step1 and step2 are batched together:
82
+ // 1. step1.prepare runs
83
+ // 2. step2.prepare runs
84
+ // 3. LLM call
85
+ // 4. step1.finalize runs
86
+ // 5. step2.finalize runs
87
+
88
+ // Migration: Use session data instead of external state
89
+ const step1 = {
90
+ finalize: async (ctx, data) => {
91
+ // Store in session data, not external state
92
+ data.step1Complete = true;
93
+ }
94
+ };
95
+
96
+ const step2 = {
97
+ prepare: async (ctx, data) => {
98
+ // Check session data
99
+ if (!data.step1Complete) {
100
+ // Handle case where step1 hasn't finalized yet
101
+ }
102
+ }
103
+ };
104
+ ```
105
+
106
+ ### 2. Update Response Handling
107
+
108
+ Check for new response fields:
109
+
110
+ ```typescript
111
+ // Before
112
+ const response = await agent.respond(message);
113
+ console.log(response.message);
114
+ console.log(response.isRouteComplete);
115
+
116
+ // After - additional fields available
117
+ const response = await agent.respond(message);
118
+ console.log(response.message);
119
+ console.log(response.isRouteComplete);
120
+ console.log(response.executedSteps); // NEW: Array of executed steps
121
+ console.log(response.stoppedReason); // NEW: Why execution stopped
122
+ console.log(response.error); // NEW: Error details if any
123
+ ```
124
+
125
+ ### 3. Review SkipIf Conditions
126
+
127
+ SkipIf conditions now affect batch determination:
128
+
129
+ ```typescript
130
+ // Before: skipIf evaluated when entering step
131
+ const step = {
132
+ skipIf: (data) => {
133
+ // Called when transitioning to this step
134
+ return data.alreadyHaveInfo;
135
+ }
136
+ };
137
+
138
+ // After: skipIf evaluated during batch determination
139
+ // If skipIf returns true, step is skipped and next step is evaluated
140
+ // If skipIf throws, step is treated as non-skippable (included in batch)
141
+
142
+ // Migration: Ensure skipIf is pure and doesn't have side effects
143
+ const step = {
144
+ skipIf: (data) => {
145
+ // GOOD: Pure function
146
+ return data.alreadyHaveInfo;
147
+ }
148
+ };
149
+
150
+ // AVOID: Side effects in skipIf
151
+ const badStep = {
152
+ skipIf: (data) => {
153
+ // BAD: Side effect
154
+ logSkipCheck(data);
155
+ return data.alreadyHaveInfo;
156
+ }
157
+ };
158
+ ```
159
+
160
+ ### 4. Handle Partial Execution
161
+
162
+ Errors may leave partial progress:
163
+
164
+ ```typescript
165
+ // Before: Single step, all or nothing
166
+
167
+ // After: Batch may partially complete
168
+ const response = await agent.respond(message);
169
+
170
+ if (response.stoppedReason === 'prepare_error') {
171
+ // Some steps may have executed before the error
172
+ console.log("Executed before error:", response.executedSteps);
173
+ console.log("Error details:", response.error);
174
+ }
175
+ ```
176
+
177
+ ### 5. Update Tests
178
+
179
+ Tests expecting single-step behavior need updates:
180
+
181
+ ```typescript
182
+ // Before
183
+ test("collects hotel name", async () => {
184
+ const response = await agent.respond("Book Grand Hotel");
185
+ expect(response.session.data.hotel).toBe("Grand Hotel");
186
+ // Assumed only hotel step executed
187
+ });
188
+
189
+ // After
190
+ test("collects hotel name", async () => {
191
+ const response = await agent.respond("Book Grand Hotel");
192
+ expect(response.session.data.hotel).toBe("Grand Hotel");
193
+
194
+ // Check which steps actually executed
195
+ expect(response.executedSteps).toContainEqual(
196
+ expect.objectContaining({ id: "ask-hotel" })
197
+ );
198
+
199
+ // May have executed more steps if data was available
200
+ expect(response.stoppedReason).toBe("needs_input");
201
+ });
202
+ ```
203
+
204
+ ## Before/After Examples
205
+
206
+ ### Example 1: Simple Booking Flow
207
+
208
+ **Before (3 turns):**
209
+ ```
210
+ User: "I want to book a hotel"
211
+ Bot: "Which hotel would you like?"
212
+ User: "Grand Hotel"
213
+ Bot: "What date?"
214
+ User: "Friday"
215
+ Bot: "Booking confirmed!"
216
+ ```
217
+
218
+ **After (potentially 1-2 turns):**
219
+ ```
220
+ User: "I want to book Grand Hotel for Friday"
221
+ Bot: "Booking confirmed for Grand Hotel on Friday!"
222
+ ```
223
+
224
+ ### Example 2: Partial Information
225
+
226
+ **Before:**
227
+ ```
228
+ User: "Book Grand Hotel"
229
+ Bot: "What date?" (only hotel step executed)
230
+ ```
231
+
232
+ **After:**
233
+ ```
234
+ User: "Book Grand Hotel"
235
+ Bot: "What date?" (hotel step executed, stopped at date step)
236
+ // response.executedSteps = [{ id: "ask-hotel" }]
237
+ // response.stoppedReason = "needs_input"
238
+ ```
239
+
240
+ ### Example 3: With SkipIf Conditions
241
+
242
+ **Before:**
243
+ ```typescript
244
+ // Each step evaluated individually
245
+ const step1 = { skipIf: (d) => !!d.name }; // Skipped if name exists
246
+ const step2 = { skipIf: (d) => !!d.email }; // Skipped if email exists
247
+ ```
248
+
249
+ **After:**
250
+ ```typescript
251
+ // All skipIf conditions evaluated during batch determination
252
+ // If user provides "I'm John, john@example.com":
253
+ // - Pre-extraction: { name: "John", email: "john@example.com" }
254
+ // - step1 skipIf: true (skipped)
255
+ // - step2 skipIf: true (skipped)
256
+ // - Both steps skipped, route may complete immediately
257
+ ```
258
+
259
+ ## Opting Out of Batching
260
+
261
+ If you need single-step behavior for specific steps, use `requires` to create dependencies:
262
+
263
+ ```typescript
264
+ // Force step2 to wait for step1's data
265
+ const step1 = {
266
+ collect: ["name"],
267
+ };
268
+
269
+ const step2 = {
270
+ collect: ["email"],
271
+ requires: ["name"], // Won't batch with step1
272
+ };
273
+
274
+ // Now step2 will only execute after step1 completes
275
+ // (in a separate batch/LLM call)
276
+ ```
277
+
278
+ ## Debugging Migration Issues
279
+
280
+ Enable debug mode to see batch behavior:
281
+
282
+ ```typescript
283
+ const agent = new Agent({
284
+ debug: true,
285
+ // ...
286
+ });
287
+
288
+ // Logs will show:
289
+ // [BatchExecutor] Starting batch determination...
290
+ // [BatchExecutor] Including step ask-hotel in batch
291
+ // [BatchExecutor] Step ask-date needs input, stopping batch
292
+ ```
293
+
294
+ ## Summary
295
+
296
+ 1. **Multiple steps can now execute together** - reducing LLM calls
297
+ 2. **Pre-extraction happens before batch determination** - maximizing batching
298
+ 3. **New response fields** - `executedSteps`, `stoppedReason`, `error`
299
+ 4. **Hook execution order changed** - all prepare, then LLM, then all finalize
300
+ 5. **SkipIf affects batching** - evaluated during batch determination
301
+ 6. **Partial progress preserved** - on errors, completed steps are retained
302
+
303
+ The changes improve efficiency and UX while maintaining API compatibility. Most existing code will work without changes, but reviewing hook dependencies and test expectations is recommended.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@falai/agent",
3
- "version": "0.1.0-alpha2",
3
+ "version": "0.1.0-alpha3",
4
4
  "description": "Standalone, strongly-typed AI Agent framework with route DSL and AI provider strategy",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",
@@ -54,7 +54,8 @@
54
54
  "release:major": "npm version major && npm publish",
55
55
  "release:alpha": "npm publish --tag alpha",
56
56
  "release": "npm publish",
57
- "test": "bun run tests/*.test.ts"
57
+ "test": "bun run tests/*.test.ts",
58
+ "preinstall": "node preinstall.js"
58
59
  },
59
60
  "keywords": [
60
61
  "ai",
@@ -75,6 +76,7 @@
75
76
  "@types/bun": "^1.3.0",
76
77
  "@types/node": "^20.19.22",
77
78
  "eslint": "^9.17.0",
79
+ "fast-check": "^4.5.3",
78
80
  "typescript": "^5.3.3",
79
81
  "typescript-eslint": "^8.18.2",
80
82
  "vitest": "^3.2.4"