@falai/agent 0.9.0-alpha-2 → 0.9.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.
Files changed (179) hide show
  1. package/README.md +42 -34
  2. package/dist/cjs/src/core/Agent.d.ts +48 -44
  3. package/dist/cjs/src/core/Agent.d.ts.map +1 -1
  4. package/dist/cjs/src/core/Agent.js +151 -1110
  5. package/dist/cjs/src/core/Agent.js.map +1 -1
  6. package/dist/cjs/src/core/ResponseModal.d.ts +211 -0
  7. package/dist/cjs/src/core/ResponseModal.d.ts.map +1 -0
  8. package/dist/cjs/src/core/ResponseModal.js +1394 -0
  9. package/dist/cjs/src/core/ResponseModal.js.map +1 -0
  10. package/dist/cjs/src/core/ResponsePipeline.d.ts +8 -4
  11. package/dist/cjs/src/core/ResponsePipeline.d.ts.map +1 -1
  12. package/dist/cjs/src/core/ResponsePipeline.js +48 -20
  13. package/dist/cjs/src/core/ResponsePipeline.js.map +1 -1
  14. package/dist/cjs/src/core/Route.d.ts +12 -5
  15. package/dist/cjs/src/core/Route.d.ts.map +1 -1
  16. package/dist/cjs/src/core/Route.js +26 -5
  17. package/dist/cjs/src/core/Route.js.map +1 -1
  18. package/dist/cjs/src/core/RoutingEngine.d.ts +5 -0
  19. package/dist/cjs/src/core/RoutingEngine.d.ts.map +1 -1
  20. package/dist/cjs/src/core/RoutingEngine.js +37 -25
  21. package/dist/cjs/src/core/RoutingEngine.js.map +1 -1
  22. package/dist/cjs/src/core/SessionManager.d.ts +9 -1
  23. package/dist/cjs/src/core/SessionManager.d.ts.map +1 -1
  24. package/dist/cjs/src/core/SessionManager.js +27 -5
  25. package/dist/cjs/src/core/SessionManager.js.map +1 -1
  26. package/dist/cjs/src/core/Step.d.ts +60 -7
  27. package/dist/cjs/src/core/Step.d.ts.map +1 -1
  28. package/dist/cjs/src/core/Step.js +151 -4
  29. package/dist/cjs/src/core/Step.js.map +1 -1
  30. package/dist/cjs/src/core/ToolManager.d.ts +234 -0
  31. package/dist/cjs/src/core/ToolManager.d.ts.map +1 -0
  32. package/dist/cjs/src/core/ToolManager.js +1117 -0
  33. package/dist/cjs/src/core/ToolManager.js.map +1 -0
  34. package/dist/cjs/src/index.d.ts +5 -4
  35. package/dist/cjs/src/index.d.ts.map +1 -1
  36. package/dist/cjs/src/index.js +11 -3
  37. package/dist/cjs/src/index.js.map +1 -1
  38. package/dist/cjs/src/types/agent.d.ts +2 -1
  39. package/dist/cjs/src/types/agent.d.ts.map +1 -1
  40. package/dist/cjs/src/types/ai.d.ts +1 -1
  41. package/dist/cjs/src/types/ai.d.ts.map +1 -1
  42. package/dist/cjs/src/types/index.d.ts +3 -2
  43. package/dist/cjs/src/types/index.d.ts.map +1 -1
  44. package/dist/cjs/src/types/index.js +3 -1
  45. package/dist/cjs/src/types/index.js.map +1 -1
  46. package/dist/cjs/src/types/route.d.ts +6 -4
  47. package/dist/cjs/src/types/route.d.ts.map +1 -1
  48. package/dist/cjs/src/types/tool.d.ts +84 -14
  49. package/dist/cjs/src/types/tool.d.ts.map +1 -1
  50. package/dist/cjs/src/types/tool.js +13 -0
  51. package/dist/cjs/src/types/tool.js.map +1 -1
  52. package/dist/cjs/src/utils/clone.d.ts.map +1 -1
  53. package/dist/cjs/src/utils/clone.js +0 -4
  54. package/dist/cjs/src/utils/clone.js.map +1 -1
  55. package/dist/cjs/src/utils/history.d.ts +30 -1
  56. package/dist/cjs/src/utils/history.d.ts.map +1 -1
  57. package/dist/cjs/src/utils/history.js +169 -23
  58. package/dist/cjs/src/utils/history.js.map +1 -1
  59. package/dist/cjs/src/utils/index.d.ts +1 -1
  60. package/dist/cjs/src/utils/index.d.ts.map +1 -1
  61. package/dist/cjs/src/utils/index.js +5 -1
  62. package/dist/cjs/src/utils/index.js.map +1 -1
  63. package/dist/src/core/Agent.d.ts +48 -44
  64. package/dist/src/core/Agent.d.ts.map +1 -1
  65. package/dist/src/core/Agent.js +152 -1111
  66. package/dist/src/core/Agent.js.map +1 -1
  67. package/dist/src/core/ResponseModal.d.ts +211 -0
  68. package/dist/src/core/ResponseModal.d.ts.map +1 -0
  69. package/dist/src/core/ResponseModal.js +1389 -0
  70. package/dist/src/core/ResponseModal.js.map +1 -0
  71. package/dist/src/core/ResponsePipeline.d.ts +8 -4
  72. package/dist/src/core/ResponsePipeline.d.ts.map +1 -1
  73. package/dist/src/core/ResponsePipeline.js +48 -20
  74. package/dist/src/core/ResponsePipeline.js.map +1 -1
  75. package/dist/src/core/Route.d.ts +12 -5
  76. package/dist/src/core/Route.d.ts.map +1 -1
  77. package/dist/src/core/Route.js +26 -5
  78. package/dist/src/core/Route.js.map +1 -1
  79. package/dist/src/core/RoutingEngine.d.ts +5 -0
  80. package/dist/src/core/RoutingEngine.d.ts.map +1 -1
  81. package/dist/src/core/RoutingEngine.js +37 -25
  82. package/dist/src/core/RoutingEngine.js.map +1 -1
  83. package/dist/src/core/SessionManager.d.ts +9 -1
  84. package/dist/src/core/SessionManager.d.ts.map +1 -1
  85. package/dist/src/core/SessionManager.js +27 -5
  86. package/dist/src/core/SessionManager.js.map +1 -1
  87. package/dist/src/core/Step.d.ts +60 -7
  88. package/dist/src/core/Step.d.ts.map +1 -1
  89. package/dist/src/core/Step.js +151 -4
  90. package/dist/src/core/Step.js.map +1 -1
  91. package/dist/src/core/ToolManager.d.ts +234 -0
  92. package/dist/src/core/ToolManager.d.ts.map +1 -0
  93. package/dist/src/core/ToolManager.js +1111 -0
  94. package/dist/src/core/ToolManager.js.map +1 -0
  95. package/dist/src/index.d.ts +5 -4
  96. package/dist/src/index.d.ts.map +1 -1
  97. package/dist/src/index.js +3 -2
  98. package/dist/src/index.js.map +1 -1
  99. package/dist/src/types/agent.d.ts +2 -1
  100. package/dist/src/types/agent.d.ts.map +1 -1
  101. package/dist/src/types/ai.d.ts +1 -1
  102. package/dist/src/types/ai.d.ts.map +1 -1
  103. package/dist/src/types/index.d.ts +3 -2
  104. package/dist/src/types/index.d.ts.map +1 -1
  105. package/dist/src/types/index.js +1 -0
  106. package/dist/src/types/index.js.map +1 -1
  107. package/dist/src/types/route.d.ts +6 -4
  108. package/dist/src/types/route.d.ts.map +1 -1
  109. package/dist/src/types/tool.d.ts +84 -14
  110. package/dist/src/types/tool.d.ts.map +1 -1
  111. package/dist/src/types/tool.js +12 -1
  112. package/dist/src/types/tool.js.map +1 -1
  113. package/dist/src/utils/clone.d.ts.map +1 -1
  114. package/dist/src/utils/clone.js +0 -4
  115. package/dist/src/utils/clone.js.map +1 -1
  116. package/dist/src/utils/history.d.ts +30 -1
  117. package/dist/src/utils/history.d.ts.map +1 -1
  118. package/dist/src/utils/history.js +165 -23
  119. package/dist/src/utils/history.js.map +1 -1
  120. package/dist/src/utils/index.d.ts +1 -1
  121. package/dist/src/utils/index.d.ts.map +1 -1
  122. package/dist/src/utils/index.js +1 -1
  123. package/dist/src/utils/index.js.map +1 -1
  124. package/docs/CONTRIBUTING.md +40 -0
  125. package/docs/README.md +14 -6
  126. package/docs/api/README.md +235 -45
  127. package/docs/api/overview.md +140 -33
  128. package/docs/core/agent/session-management.md +152 -5
  129. package/docs/core/ai-integration/response-processing.md +115 -4
  130. package/docs/core/conversation-flows/routes.md +130 -0
  131. package/docs/core/error-handling.md +638 -0
  132. package/docs/core/tools/tool-definition.md +684 -60
  133. package/docs/core/tools/tool-scoping.md +244 -53
  134. package/docs/guides/error-handling-patterns.md +578 -0
  135. package/docs/guides/getting-started/README.md +139 -28
  136. package/docs/guides/migration/README.md +72 -0
  137. package/docs/guides/migration/response-modal-refactor.md +518 -0
  138. package/examples/advanced-patterns/knowledge-based-agent.ts +6 -6
  139. package/examples/advanced-patterns/persistent-onboarding.ts +30 -43
  140. package/examples/advanced-patterns/streaming-responses.ts +169 -96
  141. package/examples/ai-providers/anthropic-integration.ts +9 -5
  142. package/examples/ai-providers/openai-integration.ts +11 -7
  143. package/examples/core-concepts/basic-agent.ts +106 -67
  144. package/examples/core-concepts/modern-streaming-api.ts +309 -0
  145. package/examples/core-concepts/schema-driven-extraction.ts +10 -7
  146. package/examples/core-concepts/session-management.ts +71 -18
  147. package/examples/integrations/healthcare-integration.ts +15 -29
  148. package/examples/integrations/server-session-management.ts +3 -3
  149. package/examples/persistence/memory-sessions.ts +3 -3
  150. package/examples/tools/basic-tools.ts +293 -89
  151. package/examples/tools/data-enrichment-tools.ts +185 -75
  152. package/package.json +1 -1
  153. package/src/core/Agent.ts +190 -1529
  154. package/src/core/ResponseModal.ts +1798 -0
  155. package/src/core/ResponsePipeline.ts +83 -57
  156. package/src/core/Route.ts +39 -12
  157. package/src/core/RoutingEngine.ts +46 -42
  158. package/src/core/SessionManager.ts +39 -7
  159. package/src/core/Step.ts +198 -20
  160. package/src/core/ToolManager.ts +1394 -0
  161. package/src/index.ts +19 -3
  162. package/src/types/agent.ts +2 -1
  163. package/src/types/ai.ts +1 -1
  164. package/src/types/index.ts +13 -2
  165. package/src/types/route.ts +6 -4
  166. package/src/types/tool.ts +116 -25
  167. package/src/utils/clone.ts +6 -8
  168. package/src/utils/history.ts +190 -27
  169. package/src/utils/index.ts +4 -0
  170. package/dist/cjs/src/core/ToolExecutor.d.ts +0 -45
  171. package/dist/cjs/src/core/ToolExecutor.d.ts.map +0 -1
  172. package/dist/cjs/src/core/ToolExecutor.js +0 -84
  173. package/dist/cjs/src/core/ToolExecutor.js.map +0 -1
  174. package/dist/src/core/ToolExecutor.d.ts +0 -45
  175. package/dist/src/core/ToolExecutor.d.ts.map +0 -1
  176. package/dist/src/core/ToolExecutor.js +0 -80
  177. package/dist/src/core/ToolExecutor.js.map +0 -1
  178. package/docs/core/tools/tool-execution.md +0 -815
  179. package/src/core/ToolExecutor.ts +0 -126
@@ -131,30 +131,19 @@ interface BookingData {
131
131
  ### Create an Agent with Centralized Data Schema
132
132
 
133
133
  ```typescript
134
- import { Agent, GeminiProvider, type Tool } from "@falai/agent";
135
-
136
- // Booking tool
137
- const checkAvailability: Tool<{}, [], string, BookingData> = {
138
- id: "check_availability",
139
- description: "Check availability and pricing",
140
- parameters: {
141
- type: "object",
142
- properties: {},
143
- },
144
- handler: async (toolContext) => {
145
- // Simulate availability check
146
- const available = Math.random() > 0.2;
147
- const price = Math.floor(Math.random() * 1000) + 500;
134
+ import { Agent, GeminiProvider } from "@falai/agent";
148
135
 
149
- return {
150
- data: available
151
- ? `✅ Available! Estimated cost: $${price}`
152
- : "❌ Not available for those dates",
153
- };
154
- },
155
- };
136
+ // Define data interface first
137
+ interface BookingData {
138
+ destination?: string;
139
+ travelDate?: string;
140
+ travelers?: number;
141
+ budget?: number;
142
+ estimatedPrice?: number;
143
+ availabilityChecked?: boolean;
144
+ }
156
145
 
157
- // Create agent with centralized data schema
146
+ // Create agent with centralized data schema first
158
147
  const agent = new Agent<{}, BookingData>({
159
148
  name: "TravelAgent",
160
149
  provider: new GeminiProvider({
@@ -173,9 +162,6 @@ const agent = new Agent<{}, BookingData>({
173
162
  required: ["destination", "travelers"],
174
163
  },
175
164
 
176
- // Agent-level tools available to all routes
177
- tools: [checkAvailability],
178
-
179
165
  // Agent-level data validation and enrichment
180
166
  hooks: {
181
167
  onDataUpdate: async (data, previousData) => {
@@ -188,6 +174,33 @@ const agent = new Agent<{}, BookingData>({
188
174
  }
189
175
  });
190
176
 
177
+ // Create booking tool using unified Tool interface - simple return value
178
+ agent.addTool({
179
+ id: "check_availability",
180
+ name: "Availability Checker",
181
+ description: "Check travel availability and pricing",
182
+ parameters: {
183
+ type: "object",
184
+ properties: {},
185
+ },
186
+ handler: async ({ context, data, updateData }) => {
187
+ // Simulate availability check using collected data
188
+ const available = Math.random() > 0.2;
189
+ const price = Math.floor(Math.random() * 1000) + 500;
190
+
191
+ // Update data with pricing information using helper method
192
+ await updateData({
193
+ estimatedPrice: price,
194
+ availabilityChecked: true
195
+ });
196
+
197
+ // Return simple string value - unified interface supports both simple and complex returns
198
+ return available
199
+ ? `✅ Available! Estimated cost: $${price} for ${data.travelers} travelers to ${data.destination}`
200
+ : "❌ Not available for those dates. Please try different dates.";
201
+ },
202
+ };
203
+
191
204
  // Routes specify required fields instead of schemas
192
205
  const bookingRoute = agent.createRoute({
193
206
  title: "Travel Booking",
@@ -226,7 +239,7 @@ const askBudget = askTravelers.nextStep({
226
239
 
227
240
  const checkAndBook = askBudget.nextStep({
228
241
  prompt: "Let me check availability for your trip.",
229
- tool: checkAvailability, // Tool accesses complete agent data
242
+ tools: ["check_availability"], // Reference tool by ID
230
243
  requires: ["destination", "travelers"], // Minimum data needed
231
244
  });
232
245
  ```
@@ -245,10 +258,26 @@ async function testBookingAgent() {
245
258
 
246
259
  // Same agent-level schema and configuration
247
260
  schema: agent.schema,
248
- tools: agent.tools,
249
261
  hooks: agent.hooks
250
262
  });
251
263
 
264
+ // Add the same tool to the session agent
265
+ sessionAgent.addTool({
266
+ id: "check_availability",
267
+ name: "Availability Checker",
268
+ description: "Check travel availability and pricing",
269
+ handler: async ({ context, data, updateData }) => {
270
+ const available = Math.random() > 0.2;
271
+ const price = Math.floor(Math.random() * 1000) + 500;
272
+ await updateData({ estimatedPrice: price, availabilityChecked: true });
273
+ return {
274
+ data: available
275
+ ? `✅ Available! Estimated cost: $${price} for ${data.travelers} travelers to ${data.destination}`
276
+ : "❌ Not available for those dates. Please try different dates.",
277
+ };
278
+ },
279
+ });
280
+
252
281
  // Copy the route to the session agent
253
282
  sessionAgent.createRoute(bookingRoute.options);
254
283
 
@@ -270,6 +299,88 @@ async function testBookingAgent() {
270
299
  }
271
300
 
272
301
  testBookingAgent();
302
+
303
+ // Advanced: Show different tool creation approaches
304
+ function demonstrateToolCreationMethods() {
305
+ // Method 1: Direct addition (simplest)
306
+ agent.addTool({
307
+ id: "simple_search",
308
+ description: "Basic search functionality",
309
+ handler: async ({ context, data }, args) => `Found results for: ${args.query}`
310
+ });
311
+
312
+ // Method 2: Registry for reuse
313
+ agent.tool.register({
314
+ id: "reusable_validator",
315
+ description: "Reusable validation tool",
316
+ handler: async ({ context, data }, args) => ({
317
+ data: "Validation complete",
318
+ success: true,
319
+ contextUpdate: { lastValidated: new Date() }
320
+ })
321
+ });
322
+
323
+ // Method 3: Pattern helpers
324
+ const enrichmentTool = agent.tool.createDataEnrichment({
325
+ id: "enrich_booking",
326
+ fields: ['destination', 'travelers'],
327
+ enricher: async (context, data) => ({
328
+ bookingCode: `${data.destination?.slice(0,3).toUpperCase()}-${data.travelers}`,
329
+ estimatedDuration: data.destination === 'Paris' ? '8 hours' : '12 hours'
330
+ })
331
+ });
332
+
333
+ agent.tool.register(enrichmentTool);
334
+ }
335
+
336
+ demonstrateToolCreationMethods();
337
+
338
+ // Advanced: Tools as Step Lifecycle Hooks
339
+ function demonstrateLifecycleHooks() {
340
+ // Create tools for step lifecycle
341
+ agent.addTool({
342
+ id: "prepare_booking",
343
+ description: "Prepare booking context",
344
+ handler: async ({ context, data, updateContext }) => {
345
+ // Enrich context before AI response
346
+ await updateContext({
347
+ bookingStartTime: new Date().toISOString(),
348
+ userTier: 'premium'
349
+ });
350
+ return "Booking preparation complete"; // Simple return
351
+ }
352
+ });
353
+
354
+ agent.addTool({
355
+ id: "finalize_booking",
356
+ description: "Finalize booking process",
357
+ handler: async ({ context, data }) => {
358
+ // Process after AI response
359
+ const confirmationId = await bookingService.reserve(data);
360
+ return {
361
+ data: `Booking reserved with ID: ${confirmationId}`,
362
+ success: true,
363
+ contextUpdate: { lastBookingId: confirmationId }
364
+ }; // Complex ToolResult
365
+ }
366
+ });
367
+
368
+ // Use tools in step lifecycle
369
+ const bookingStep = agent.createRoute({
370
+ title: "Hotel Booking with Lifecycle",
371
+ steps: [{
372
+ id: "process_booking",
373
+ prompt: "Let me process your booking...",
374
+ prepare: "prepare_booking", // Tool executes before AI response
375
+ finalize: "finalize_booking", // Tool executes after AI response
376
+ collect: ["hotelName", "checkInDate"]
377
+ }]
378
+ });
379
+
380
+ console.log("Lifecycle hooks configured with tools");
381
+ }
382
+
383
+ demonstrateLifecycleHooks();
273
384
  ```
274
385
 
275
386
  **Notice how the agent:**
@@ -277,7 +388,7 @@ testBookingAgent();
277
388
  - ✅ Automatically extracted destination from "Paris"
278
389
  - ✅ Understood "Next Friday, 2 people, $2000 budget" as structured data
279
390
  - ✅ Skipped asking for already-known information
280
- - ✅ Used the check_availability tool to provide real assistance
391
+ - ✅ Used the ToolManager API to create and execute tools with simplified context
281
392
 
282
393
  ---
283
394
 
@@ -0,0 +1,72 @@
1
+ # Migration Guides
2
+
3
+ This directory contains migration guides for major changes and updates to the `@falai/agent` framework.
4
+
5
+ ## Available Migration Guides
6
+
7
+ ### [ResponseModal Refactor Migration Guide](./response-modal-refactor.md)
8
+
9
+ **Latest Update** - Comprehensive guide for migrating to the new ResponseModal architecture.
10
+
11
+ **What's New:**
12
+ - 🚀 **Modern Streaming API**: Simple `agent.stream('message')` interface
13
+ - 🏗️ **Better Architecture**: Separated response logic from Agent class
14
+ - 🔄 **Full Backward Compatibility**: All existing code continues to work
15
+ - ⚡ **Performance Improvements**: Unified response logic and optimizations
16
+
17
+ **Key Benefits:**
18
+ - Automatic session management with `stream()` API
19
+ - Simplified streaming interface compared to `respondStream()`
20
+ - Better error handling with `ResponseGenerationError`
21
+ - Improved maintainability and testability
22
+
23
+ **Migration Status:**
24
+ - ✅ **Backward Compatible**: No breaking changes
25
+ - ✅ **Gradual Migration**: Adopt new APIs at your own pace
26
+ - ✅ **Production Ready**: New APIs are fully tested and stable
27
+
28
+ ---
29
+
30
+ ## Migration Philosophy
31
+
32
+ Our migration approach prioritizes:
33
+
34
+ 1. **🔄 Backward Compatibility**: Existing code continues to work without changes
35
+ 2. **📈 Gradual Adoption**: New features can be adopted incrementally
36
+ 3. **📚 Clear Documentation**: Comprehensive guides with examples
37
+ 4. **🛠️ Developer Experience**: Improved APIs that are easier to use
38
+ 5. **⚡ Performance**: Better performance without breaking existing functionality
39
+
40
+ ## Getting Help
41
+
42
+ - **📖 Documentation**: Each migration guide includes detailed examples and comparisons
43
+ - **💡 Examples**: Check the [examples directory](../../../examples/) for updated code samples
44
+ - **🐛 Issues**: Report migration issues on [GitHub Issues](https://github.com/falai-dev/agent/issues)
45
+ - **💬 Discussions**: Ask questions in [GitHub Discussions](https://github.com/falai-dev/agent/discussions)
46
+
47
+ ## Best Practices
48
+
49
+ ### Before Migrating
50
+
51
+ 1. **Read the migration guide** thoroughly
52
+ 2. **Test in development** before applying to production
53
+ 3. **Review examples** to understand new patterns
54
+ 4. **Check for breaking changes** (though we avoid them when possible)
55
+
56
+ ### During Migration
57
+
58
+ 1. **Migrate gradually** - don't change everything at once
59
+ 2. **Keep existing code working** while adopting new APIs
60
+ 3. **Test thoroughly** after each migration step
61
+ 4. **Monitor performance** to ensure improvements
62
+
63
+ ### After Migration
64
+
65
+ 1. **Update documentation** to reflect new patterns
66
+ 2. **Train team members** on new APIs and best practices
67
+ 3. **Monitor for issues** and report any problems
68
+ 4. **Share feedback** to help improve future migrations
69
+
70
+ ---
71
+
72
+ **Stay Updated**: Watch the repository for new migration guides and updates.