@falai/agent 0.9.0-alpha-1 → 0.9.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.
Files changed (217) hide show
  1. package/README.md +34 -22
  2. package/dist/cjs/src/core/Agent.d.ts +77 -59
  3. package/dist/cjs/src/core/Agent.d.ts.map +1 -1
  4. package/dist/cjs/src/core/Agent.js +284 -1060
  5. package/dist/cjs/src/core/Agent.js.map +1 -1
  6. package/dist/cjs/src/core/PersistenceManager.d.ts.map +1 -1
  7. package/dist/cjs/src/core/PersistenceManager.js +48 -25
  8. package/dist/cjs/src/core/PersistenceManager.js.map +1 -1
  9. package/dist/cjs/src/core/PromptComposer.d.ts +1 -1
  10. package/dist/cjs/src/core/PromptComposer.d.ts.map +1 -1
  11. package/dist/cjs/src/core/PromptComposer.js.map +1 -1
  12. package/dist/cjs/src/core/ResponseEngine.d.ts +13 -12
  13. package/dist/cjs/src/core/ResponseEngine.d.ts.map +1 -1
  14. package/dist/cjs/src/core/ResponseEngine.js +4 -4
  15. package/dist/cjs/src/core/ResponseEngine.js.map +1 -1
  16. package/dist/cjs/src/core/ResponseModal.d.ts +205 -0
  17. package/dist/cjs/src/core/ResponseModal.d.ts.map +1 -0
  18. package/dist/cjs/src/core/ResponseModal.js +1328 -0
  19. package/dist/cjs/src/core/ResponseModal.js.map +1 -0
  20. package/dist/cjs/src/core/ResponsePipeline.d.ts +66 -38
  21. package/dist/cjs/src/core/ResponsePipeline.d.ts.map +1 -1
  22. package/dist/cjs/src/core/ResponsePipeline.js +72 -4
  23. package/dist/cjs/src/core/ResponsePipeline.js.map +1 -1
  24. package/dist/cjs/src/core/Route.d.ts +24 -5
  25. package/dist/cjs/src/core/Route.d.ts.map +1 -1
  26. package/dist/cjs/src/core/Route.js +45 -1
  27. package/dist/cjs/src/core/Route.js.map +1 -1
  28. package/dist/cjs/src/core/RoutingEngine.d.ts +31 -6
  29. package/dist/cjs/src/core/RoutingEngine.d.ts.map +1 -1
  30. package/dist/cjs/src/core/RoutingEngine.js +113 -9
  31. package/dist/cjs/src/core/RoutingEngine.js.map +1 -1
  32. package/dist/cjs/src/core/SessionManager.d.ts +14 -4
  33. package/dist/cjs/src/core/SessionManager.d.ts.map +1 -1
  34. package/dist/cjs/src/core/SessionManager.js +25 -5
  35. package/dist/cjs/src/core/SessionManager.js.map +1 -1
  36. package/dist/cjs/src/core/Step.d.ts +10 -10
  37. package/dist/cjs/src/core/Step.d.ts.map +1 -1
  38. package/dist/cjs/src/core/Step.js.map +1 -1
  39. package/dist/cjs/src/core/ToolExecutor.d.ts +4 -2
  40. package/dist/cjs/src/core/ToolExecutor.d.ts.map +1 -1
  41. package/dist/cjs/src/core/ToolExecutor.js +13 -3
  42. package/dist/cjs/src/core/ToolExecutor.js.map +1 -1
  43. package/dist/cjs/src/index.d.ts +3 -1
  44. package/dist/cjs/src/index.d.ts.map +1 -1
  45. package/dist/cjs/src/index.js +7 -1
  46. package/dist/cjs/src/index.js.map +1 -1
  47. package/dist/cjs/src/types/agent.d.ts +42 -21
  48. package/dist/cjs/src/types/agent.d.ts.map +1 -1
  49. package/dist/cjs/src/types/agent.js.map +1 -1
  50. package/dist/cjs/src/types/ai.d.ts +1 -1
  51. package/dist/cjs/src/types/ai.d.ts.map +1 -1
  52. package/dist/cjs/src/types/index.d.ts +1 -1
  53. package/dist/cjs/src/types/index.d.ts.map +1 -1
  54. package/dist/cjs/src/types/index.js.map +1 -1
  55. package/dist/cjs/src/types/persistence.d.ts +0 -1
  56. package/dist/cjs/src/types/persistence.d.ts.map +1 -1
  57. package/dist/cjs/src/types/route.d.ts +22 -16
  58. package/dist/cjs/src/types/route.d.ts.map +1 -1
  59. package/dist/cjs/src/types/session.d.ts +6 -11
  60. package/dist/cjs/src/types/session.d.ts.map +1 -1
  61. package/dist/cjs/src/types/tool.d.ts +12 -6
  62. package/dist/cjs/src/types/tool.d.ts.map +1 -1
  63. package/dist/cjs/src/utils/clone.d.ts.map +1 -1
  64. package/dist/cjs/src/utils/clone.js +0 -4
  65. package/dist/cjs/src/utils/clone.js.map +1 -1
  66. package/dist/cjs/src/utils/history.d.ts +30 -1
  67. package/dist/cjs/src/utils/history.d.ts.map +1 -1
  68. package/dist/cjs/src/utils/history.js +169 -23
  69. package/dist/cjs/src/utils/history.js.map +1 -1
  70. package/dist/cjs/src/utils/index.d.ts +1 -1
  71. package/dist/cjs/src/utils/index.d.ts.map +1 -1
  72. package/dist/cjs/src/utils/index.js +5 -1
  73. package/dist/cjs/src/utils/index.js.map +1 -1
  74. package/dist/cjs/src/utils/session.d.ts +2 -2
  75. package/dist/cjs/src/utils/session.d.ts.map +1 -1
  76. package/dist/cjs/src/utils/session.js +6 -26
  77. package/dist/cjs/src/utils/session.js.map +1 -1
  78. package/dist/src/core/Agent.d.ts +77 -59
  79. package/dist/src/core/Agent.d.ts.map +1 -1
  80. package/dist/src/core/Agent.js +285 -1061
  81. package/dist/src/core/Agent.js.map +1 -1
  82. package/dist/src/core/PersistenceManager.d.ts.map +1 -1
  83. package/dist/src/core/PersistenceManager.js +48 -25
  84. package/dist/src/core/PersistenceManager.js.map +1 -1
  85. package/dist/src/core/PromptComposer.d.ts +1 -1
  86. package/dist/src/core/PromptComposer.d.ts.map +1 -1
  87. package/dist/src/core/PromptComposer.js.map +1 -1
  88. package/dist/src/core/ResponseEngine.d.ts +13 -12
  89. package/dist/src/core/ResponseEngine.d.ts.map +1 -1
  90. package/dist/src/core/ResponseEngine.js +4 -4
  91. package/dist/src/core/ResponseEngine.js.map +1 -1
  92. package/dist/src/core/ResponseModal.d.ts +205 -0
  93. package/dist/src/core/ResponseModal.d.ts.map +1 -0
  94. package/dist/src/core/ResponseModal.js +1323 -0
  95. package/dist/src/core/ResponseModal.js.map +1 -0
  96. package/dist/src/core/ResponsePipeline.d.ts +66 -38
  97. package/dist/src/core/ResponsePipeline.d.ts.map +1 -1
  98. package/dist/src/core/ResponsePipeline.js +72 -4
  99. package/dist/src/core/ResponsePipeline.js.map +1 -1
  100. package/dist/src/core/Route.d.ts +24 -5
  101. package/dist/src/core/Route.d.ts.map +1 -1
  102. package/dist/src/core/Route.js +45 -1
  103. package/dist/src/core/Route.js.map +1 -1
  104. package/dist/src/core/RoutingEngine.d.ts +31 -6
  105. package/dist/src/core/RoutingEngine.d.ts.map +1 -1
  106. package/dist/src/core/RoutingEngine.js +113 -9
  107. package/dist/src/core/RoutingEngine.js.map +1 -1
  108. package/dist/src/core/SessionManager.d.ts +14 -4
  109. package/dist/src/core/SessionManager.d.ts.map +1 -1
  110. package/dist/src/core/SessionManager.js +25 -5
  111. package/dist/src/core/SessionManager.js.map +1 -1
  112. package/dist/src/core/Step.d.ts +10 -10
  113. package/dist/src/core/Step.d.ts.map +1 -1
  114. package/dist/src/core/Step.js.map +1 -1
  115. package/dist/src/core/ToolExecutor.d.ts +4 -2
  116. package/dist/src/core/ToolExecutor.d.ts.map +1 -1
  117. package/dist/src/core/ToolExecutor.js +13 -3
  118. package/dist/src/core/ToolExecutor.js.map +1 -1
  119. package/dist/src/index.d.ts +3 -1
  120. package/dist/src/index.d.ts.map +1 -1
  121. package/dist/src/index.js +2 -1
  122. package/dist/src/index.js.map +1 -1
  123. package/dist/src/types/agent.d.ts +42 -21
  124. package/dist/src/types/agent.d.ts.map +1 -1
  125. package/dist/src/types/agent.js.map +1 -1
  126. package/dist/src/types/ai.d.ts +1 -1
  127. package/dist/src/types/ai.d.ts.map +1 -1
  128. package/dist/src/types/index.d.ts +1 -1
  129. package/dist/src/types/index.d.ts.map +1 -1
  130. package/dist/src/types/index.js.map +1 -1
  131. package/dist/src/types/persistence.d.ts +0 -1
  132. package/dist/src/types/persistence.d.ts.map +1 -1
  133. package/dist/src/types/route.d.ts +22 -16
  134. package/dist/src/types/route.d.ts.map +1 -1
  135. package/dist/src/types/session.d.ts +6 -11
  136. package/dist/src/types/session.d.ts.map +1 -1
  137. package/dist/src/types/tool.d.ts +12 -6
  138. package/dist/src/types/tool.d.ts.map +1 -1
  139. package/dist/src/utils/clone.d.ts.map +1 -1
  140. package/dist/src/utils/clone.js +0 -4
  141. package/dist/src/utils/clone.js.map +1 -1
  142. package/dist/src/utils/history.d.ts +30 -1
  143. package/dist/src/utils/history.d.ts.map +1 -1
  144. package/dist/src/utils/history.js +165 -23
  145. package/dist/src/utils/history.js.map +1 -1
  146. package/dist/src/utils/index.d.ts +1 -1
  147. package/dist/src/utils/index.d.ts.map +1 -1
  148. package/dist/src/utils/index.js +1 -1
  149. package/dist/src/utils/index.js.map +1 -1
  150. package/dist/src/utils/session.d.ts +2 -2
  151. package/dist/src/utils/session.d.ts.map +1 -1
  152. package/dist/src/utils/session.js +6 -26
  153. package/dist/src/utils/session.js.map +1 -1
  154. package/docs/README.md +5 -4
  155. package/docs/api/README.md +195 -4
  156. package/docs/api/overview.md +232 -13
  157. package/docs/core/agent/README.md +162 -17
  158. package/docs/core/agent/context-management.md +39 -15
  159. package/docs/core/agent/session-management.md +49 -16
  160. package/docs/core/ai-integration/prompt-composition.md +38 -14
  161. package/docs/core/ai-integration/response-processing.md +28 -17
  162. package/docs/core/conversation-flows/data-collection.md +103 -25
  163. package/docs/core/conversation-flows/route-dsl.md +45 -22
  164. package/docs/core/conversation-flows/routes.md +74 -18
  165. package/docs/core/conversation-flows/step-transitions.md +3 -3
  166. package/docs/core/conversation-flows/steps.md +39 -15
  167. package/docs/core/routing/intelligent-routing.md +18 -9
  168. package/docs/core/tools/tool-definition.md +8 -8
  169. package/docs/core/tools/tool-execution.md +26 -26
  170. package/docs/core/tools/tool-scoping.md +5 -5
  171. package/docs/guides/getting-started/README.md +54 -32
  172. package/docs/guides/migration/README.md +72 -0
  173. package/docs/guides/migration/response-modal-refactor.md +518 -0
  174. package/examples/advanced-patterns/knowledge-based-agent.ts +37 -28
  175. package/examples/advanced-patterns/persistent-onboarding.ts +70 -41
  176. package/examples/advanced-patterns/route-lifecycle-hooks.ts +28 -2
  177. package/examples/advanced-patterns/streaming-responses.ts +197 -119
  178. package/examples/ai-providers/anthropic-integration.ts +40 -33
  179. package/examples/ai-providers/openai-integration.ts +25 -25
  180. package/examples/conversation-flows/completion-transitions.ts +36 -32
  181. package/examples/core-concepts/basic-agent.ts +76 -78
  182. package/examples/core-concepts/modern-streaming-api.ts +309 -0
  183. package/examples/core-concepts/schema-driven-extraction.ts +20 -16
  184. package/examples/core-concepts/session-management.ts +65 -53
  185. package/examples/integrations/database-integration.ts +49 -34
  186. package/examples/integrations/healthcare-integration.ts +96 -91
  187. package/examples/integrations/search-integration.ts +79 -82
  188. package/examples/integrations/server-session-management.ts +25 -17
  189. package/examples/persistence/database-persistence.ts +61 -45
  190. package/examples/persistence/memory-sessions.ts +52 -63
  191. package/examples/persistence/redis-persistence.ts +81 -95
  192. package/examples/tools/basic-tools.ts +73 -62
  193. package/examples/tools/data-enrichment-tools.ts +52 -44
  194. package/package.json +1 -1
  195. package/src/core/Agent.ts +396 -1499
  196. package/src/core/PersistenceManager.ts +51 -27
  197. package/src/core/PromptComposer.ts +1 -1
  198. package/src/core/ResponseEngine.ts +21 -19
  199. package/src/core/ResponseModal.ts +1722 -0
  200. package/src/core/ResponsePipeline.ts +175 -60
  201. package/src/core/Route.ts +58 -6
  202. package/src/core/RoutingEngine.ts +174 -27
  203. package/src/core/SessionManager.ts +32 -8
  204. package/src/core/Step.ts +20 -12
  205. package/src/core/ToolExecutor.ts +19 -5
  206. package/src/index.ts +11 -0
  207. package/src/types/agent.ts +47 -23
  208. package/src/types/ai.ts +1 -1
  209. package/src/types/index.ts +2 -0
  210. package/src/types/persistence.ts +0 -1
  211. package/src/types/route.ts +22 -16
  212. package/src/types/session.ts +6 -12
  213. package/src/types/tool.ts +15 -9
  214. package/src/utils/clone.ts +6 -8
  215. package/src/utils/history.ts +190 -27
  216. package/src/utils/index.ts +4 -0
  217. package/src/utils/session.ts +6 -31
@@ -6,7 +6,7 @@
6
6
  * and integration with conversation flows.
7
7
  *
8
8
  * Key concepts:
9
- * - Tool definition with Tool<TContext, TArgs, TResult, TData> interface
9
+ * - Tool definition with Tool<TContext, TData, TArgs, TResult> interface
10
10
  * - Tool context and parameters
11
11
  * - Tool execution in conversation flows
12
12
  * - Error handling in tools
@@ -36,7 +36,7 @@ interface SearchData {
36
36
  }
37
37
 
38
38
  // Example 1: Simple Calculator Tool
39
- const calculatorTool: Tool<unknown, [], string, CalculatorData> = {
39
+ const calculatorTool: Tool<unknown, UnifiedToolData, [], string> = {
40
40
  id: "calculator",
41
41
  name: "Math Calculator",
42
42
  description: "Evaluate mathematical expressions and return results",
@@ -51,8 +51,7 @@ const calculatorTool: Tool<unknown, [], string, CalculatorData> = {
51
51
  required: ["expression"],
52
52
  },
53
53
  handler: ({ data }) => {
54
- const calcData = data as Partial<CalculatorData>;
55
- if (!calcData?.expression) {
54
+ if (!data?.expression) {
56
55
  throw new Error("No expression provided");
57
56
  }
58
57
 
@@ -60,22 +59,22 @@ const calculatorTool: Tool<unknown, [], string, CalculatorData> = {
60
59
  // Simple expression evaluation (in production, use a safe math library)
61
60
  // WARNING: eval is unsafe - use a proper math evaluation library in production
62
61
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
63
- const result = eval(calcData.expression);
62
+ const result = eval(data.expression);
64
63
 
65
64
  if (typeof result !== "number" || isNaN(result)) {
66
65
  throw new Error("Invalid calculation result");
67
66
  }
68
67
 
69
68
  return {
70
- data: `The result of ${calcData.expression} is ${result}`,
69
+ data: `The result of ${data.expression} is ${result}`,
71
70
  dataUpdate: {
72
71
  result,
73
- operation: calcData.expression,
72
+ operation: data.expression,
74
73
  },
75
74
  };
76
75
  } catch (error) {
77
76
  throw new Error(
78
- `Error calculating ${calcData.expression}: ${
77
+ `Error calculating ${data.expression}: ${
79
78
  error instanceof Error ? error.message : "Unknown error"
80
79
  }`
81
80
  );
@@ -84,7 +83,7 @@ const calculatorTool: Tool<unknown, [], string, CalculatorData> = {
84
83
  };
85
84
 
86
85
  // Example 2: Weather Tool with External API Simulation
87
- const weatherTool: Tool<unknown, [], string, WeatherData> = {
86
+ const weatherTool: Tool<unknown, UnifiedToolData, [], string> = {
88
87
  id: "get_weather",
89
88
  name: "Weather Lookup",
90
89
  description: "Get current weather and forecast for a location",
@@ -96,8 +95,7 @@ const weatherTool: Tool<unknown, [], string, WeatherData> = {
96
95
  required: ["location"],
97
96
  },
98
97
  handler: async ({ data }) => {
99
- const weatherData = data as Partial<WeatherData>;
100
- if (!weatherData?.location) {
98
+ if (!data?.location) {
101
99
  throw new Error("No location provided");
102
100
  }
103
101
 
@@ -127,14 +125,14 @@ const weatherTool: Tool<unknown, [], string, WeatherData> = {
127
125
  },
128
126
  };
129
127
 
130
- const weather = mockWeather[weatherData.location] || {
128
+ const weather = mockWeather[data.location] || {
131
129
  temp: 70,
132
130
  condition: "Unknown",
133
131
  forecast: "Weather data unavailable",
134
132
  };
135
133
 
136
134
  return {
137
- data: `Weather in ${weatherData.location}: ${weather.temp}°F and ${weather.condition}. ${weather.forecast}`,
135
+ data: `Weather in ${data.location}: ${weather.temp}°F and ${weather.condition}. ${weather.forecast}`,
138
136
  dataUpdate: {
139
137
  temperature: weather.temp,
140
138
  condition: weather.condition,
@@ -145,7 +143,7 @@ const weatherTool: Tool<unknown, [], string, WeatherData> = {
145
143
  };
146
144
 
147
145
  // Example 3: Search Tool with Multiple Results
148
- const searchTool: Tool<unknown, [], string, SearchData> = {
146
+ const searchTool: Tool<unknown, UnifiedToolData, [], string> = {
149
147
  id: "web_search",
150
148
  name: "Web Search",
151
149
  description: "Search the web for information on a given query",
@@ -157,8 +155,7 @@ const searchTool: Tool<unknown, [], string, SearchData> = {
157
155
  required: ["query"],
158
156
  },
159
157
  handler: async ({ data }) => {
160
- const searchData = data as Partial<SearchData>;
161
- if (!searchData?.query) {
158
+ if (!data?.query) {
162
159
  throw new Error("No search query provided");
163
160
  }
164
161
 
@@ -187,15 +184,15 @@ const searchTool: Tool<unknown, [], string, SearchData> = {
187
184
  ],
188
185
  };
189
186
 
190
- const results = mockResults[searchData.query.toLowerCase()] || [
191
- `Search results for "${searchData.query}"`,
187
+ const results = mockResults[data.query.toLowerCase()] || [
188
+ `Search results for "${data.query}"`,
192
189
  "This is a simulated search result",
193
190
  "In a real implementation, this would connect to a search API",
194
191
  "Such as Google Custom Search, Bing Web Search, or Elasticsearch",
195
192
  ];
196
193
 
197
194
  return {
198
- data: `Search results for "${searchData.query}":\n${results
195
+ data: `Search results for "${data.query}":\n${results
199
196
  .map((r, i) => `${i + 1}. ${r}`)
200
197
  .join("\n")}`,
201
198
  dataUpdate: {
@@ -209,9 +206,9 @@ const searchTool: Tool<unknown, [], string, SearchData> = {
209
206
  // Example 4: Tool that Modifies Context (Advanced)
210
207
  const updatePreferencesTool: Tool<
211
208
  { preferences?: { theme: string; language: string } },
209
+ UnifiedToolData,
212
210
  [],
213
- string,
214
- { theme?: string; language?: string }
211
+ string
215
212
  > = {
216
213
  id: "update_preferences",
217
214
  name: "Update Preferences",
@@ -242,29 +239,54 @@ const updatePreferencesTool: Tool<
242
239
  },
243
240
  };
244
241
 
242
+ // Define unified data schema for all tool interactions
243
+ interface UnifiedToolData extends CalculatorData, WeatherData, SearchData {
244
+ theme?: string;
245
+ language?: string;
246
+ }
247
+
248
+ const unifiedToolSchema = {
249
+ type: "object",
250
+ properties: {
251
+ // Calculator fields
252
+ expression: { type: "string" },
253
+ result: { type: "number" },
254
+ operation: { type: "string" },
255
+ // Weather fields
256
+ location: { type: "string" },
257
+ temperature: { type: "number" },
258
+ condition: { type: "string" },
259
+ forecast: { type: "string" },
260
+ // Search fields
261
+ query: { type: "string" },
262
+ results: { type: "array", items: { type: "string" } },
263
+ source: { type: "string" },
264
+ // Preferences fields
265
+ theme: { type: "string", enum: ["light", "dark"] },
266
+ language: { type: "string", enum: ["en", "es", "fr"] },
267
+ },
268
+ };
269
+
245
270
  // Create agent with tools
246
- const agent = new Agent<{ preferences?: { theme: string; language: string } }>({
271
+ const agent = new Agent<{ preferences?: { theme: string; language: string } }, UnifiedToolData>({
247
272
  name: "ToolBot",
248
273
  description: "An agent demonstrating various tool capabilities",
249
274
  provider: new GeminiProvider({
250
275
  apiKey: process.env.GEMINI_API_KEY!,
251
276
  model: "models/gemini-2.5-flash",
252
277
  }),
278
+ // NEW: Agent-level schema
279
+ schema: unifiedToolSchema,
253
280
  });
254
281
 
255
282
  // Create routes that use different tools
256
- agent.createRoute<CalculatorData>({
283
+ agent.createRoute({
257
284
  title: "Calculator",
258
285
  description: "Mathematical calculations",
259
- schema: {
260
- type: "object",
261
- properties: {
262
- expression: { type: "string" },
263
- result: { type: "number" },
264
- operation: { type: "string" },
265
- },
266
- required: ["expression"],
267
- },
286
+ // NEW: Required fields for route completion
287
+ requiredFields: ["expression"],
288
+ // NEW: Optional fields that enhance the experience
289
+ optionalFields: ["result", "operation"],
268
290
  steps: [
269
291
  {
270
292
  id: "get_expression",
@@ -283,19 +305,13 @@ agent.createRoute<CalculatorData>({
283
305
  ],
284
306
  });
285
307
 
286
- agent.createRoute<WeatherData>({
308
+ agent.createRoute({
287
309
  title: "Weather",
288
310
  description: "Weather information",
289
- schema: {
290
- type: "object",
291
- properties: {
292
- location: { type: "string" },
293
- temperature: { type: "number" },
294
- condition: { type: "string" },
295
- forecast: { type: "string" },
296
- },
297
- required: ["location"],
298
- },
311
+ // NEW: Required fields for route completion
312
+ requiredFields: ["location"],
313
+ // NEW: Optional fields that enhance the experience
314
+ optionalFields: ["temperature", "condition", "forecast"],
299
315
  steps: [
300
316
  {
301
317
  id: "get_location",
@@ -314,18 +330,13 @@ agent.createRoute<WeatherData>({
314
330
  ],
315
331
  });
316
332
 
317
- agent.createRoute<SearchData>({
333
+ agent.createRoute({
318
334
  title: "Web Search",
319
335
  description: "Information search",
320
- schema: {
321
- type: "object",
322
- properties: {
323
- query: { type: "string" },
324
- results: { type: "array", items: { type: "string" } },
325
- source: { type: "string" },
326
- },
327
- required: ["query"],
328
- },
336
+ // NEW: Required fields for route completion
337
+ requiredFields: ["query"],
338
+ // NEW: Optional fields that enhance the experience
339
+ optionalFields: ["results", "source"],
329
340
  steps: [
330
341
  {
331
342
  id: "get_query",
@@ -371,7 +382,7 @@ async function demonstrateBasicTools() {
371
382
  console.log("Bot:", calcResponse.message);
372
383
  console.log(
373
384
  "Calculated result:",
374
- (calcResponse.session?.data as Partial<CalculatorData>)?.result
385
+ calcResponse.session?.data?.result
375
386
  );
376
387
  console.log();
377
388
 
@@ -383,9 +394,9 @@ async function demonstrateBasicTools() {
383
394
  });
384
395
  console.log("Bot:", weatherResponse.message);
385
396
  console.log("Weather data:", {
386
- temperature: (weatherResponse.session?.data as Partial<WeatherData>)
397
+ temperature: (weatherResponse.session?.data as Partial<UnifiedToolData>)
387
398
  ?.temperature,
388
- condition: (weatherResponse.session?.data as Partial<WeatherData>)
399
+ condition: (weatherResponse.session?.data as Partial<UnifiedToolData>)
389
400
  ?.condition,
390
401
  });
391
402
  console.log();
@@ -404,7 +415,7 @@ async function demonstrateBasicTools() {
404
415
  console.log("Bot:", searchResponse.message);
405
416
  console.log(
406
417
  "Search results count:",
407
- (searchResponse.session?.data as Partial<SearchData>)?.results?.length
418
+ (searchResponse.session?.data as Partial<UnifiedToolData>)?.results?.length
408
419
  );
409
420
  }
410
421
 
@@ -453,15 +464,15 @@ async function demonstrateToolDataFlow() {
453
464
  console.log("1. Initial calculation:");
454
465
  console.log(
455
466
  " User input collected:",
456
- (response1.session?.data as Partial<CalculatorData>)?.expression
467
+ (response1.session?.data as Partial<UnifiedToolData>)?.expression
457
468
  );
458
469
  console.log(
459
470
  " Tool result:",
460
- (response1.session?.data as Partial<CalculatorData>)?.result
471
+ (response1.session?.data as Partial<UnifiedToolData>)?.result
461
472
  );
462
473
  console.log(
463
474
  " Operation stored:",
464
- (response1.session?.data as Partial<CalculatorData>)?.operation
475
+ (response1.session?.data as Partial<UnifiedToolData>)?.operation
465
476
  );
466
477
 
467
478
  // Follow up question using previous result
@@ -475,7 +486,7 @@ async function demonstrateToolDataFlow() {
475
486
  console.log("\n2. Follow-up calculation:");
476
487
  console.log(
477
488
  " Previous result available:",
478
- (response2.session?.data as Partial<CalculatorData>)?.result
489
+ (response2.session?.data as Partial<UnifiedToolData>)?.result
479
490
  );
480
491
  console.log(" Bot response:", response2.message);
481
492
  }
@@ -486,7 +497,7 @@ function demonstrateToolPatterns() {
486
497
 
487
498
  console.log("1. Basic Tool Pattern:");
488
499
  console.log(`
489
- const myTool: Tool<ContextType, [], ResultType, DataType> = {
500
+ const myTool: Tool<ContextType, DataType, [], ResultType> = {
490
501
  id: "tool_name", // Unique identifier
491
502
  description: "What this tool does", // AI uses this to decide when to call
492
503
  parameters: { // JSON Schema for tool parameters
@@ -45,7 +45,7 @@ interface FlightData {
45
45
  // ==============================================================================
46
46
 
47
47
  // Tool 1: Convert city names to airport codes
48
- const enrichDestinationTool: Tool<FlightBookingContext, [], void, FlightData> =
48
+ const enrichDestinationTool: Tool<FlightBookingContext, FlightData, [], void> =
49
49
  {
50
50
  id: "enrich_destination",
51
51
  name: "Destination Code Lookup",
@@ -84,7 +84,7 @@ const enrichDestinationTool: Tool<FlightBookingContext, [], void, FlightData> =
84
84
  };
85
85
 
86
86
  // Tool 2: Parse and validate dates
87
- const validateDateTool: Tool<FlightBookingContext, [], void, FlightData> = {
87
+ const validateDateTool: Tool<FlightBookingContext, FlightData, [], void> = {
88
88
  id: "validate_date",
89
89
  name: "Date Parser & Validator",
90
90
  description:
@@ -139,7 +139,7 @@ const validateDateTool: Tool<FlightBookingContext, [], void, FlightData> = {
139
139
  };
140
140
 
141
141
  // Tool 3: Search for flights (triggered by flag)
142
- const searchFlightsTool: Tool<FlightBookingContext, [], void, FlightData> = {
142
+ const searchFlightsTool: Tool<FlightBookingContext, FlightData, [], void> = {
143
143
  id: "search_flights",
144
144
  name: "Flight Availability Search",
145
145
  description: "Search for available flights based on collected data",
@@ -188,7 +188,7 @@ const searchFlightsTool: Tool<FlightBookingContext, [], void, FlightData> = {
188
188
  };
189
189
 
190
190
  // Tool 4: Book the flight
191
- const bookFlightTool: Tool<FlightBookingContext, [], void, FlightData> = {
191
+ const bookFlightTool: Tool<FlightBookingContext, FlightData, [], void> = {
192
192
  id: "book_flight",
193
193
  name: "Flight Booking Processor",
194
194
  description: "Finalize the flight booking",
@@ -252,7 +252,45 @@ function onDataUpdate(
252
252
  // AGENT SETUP
253
253
  // ==============================================================================
254
254
 
255
- const agent = new Agent<FlightBookingContext>({
255
+ // Define flight booking schema
256
+ const flightBookingSchema = {
257
+ type: "object",
258
+ properties: {
259
+ destination: {
260
+ type: "string",
261
+ description: "City or airport the user wants to fly to",
262
+ },
263
+ destinationCode: {
264
+ type: "string",
265
+ description: "IATA airport code (enriched by tool)",
266
+ },
267
+ departureDate: {
268
+ type: "string",
269
+ description: "When the user wants to depart",
270
+ },
271
+ departureDateParsed: {
272
+ type: "string",
273
+ description: "Parsed ISO date (enriched by tool)",
274
+ },
275
+ passengers: {
276
+ type: "number",
277
+ minimum: 1,
278
+ maximum: 9,
279
+ },
280
+ cabinClass: {
281
+ type: "string",
282
+ enum: ["economy", "business", "first"],
283
+ default: "economy",
284
+ },
285
+ shouldSearchFlights: {
286
+ type: "boolean",
287
+ description: "Flag to trigger flight search",
288
+ },
289
+ },
290
+ required: ["destination", "departureDate", "passengers"],
291
+ };
292
+
293
+ const agent = new Agent<FlightBookingContext, FlightData>({
256
294
  name: "Flight Booking Agent",
257
295
  goal: "Help users book flights efficiently",
258
296
  description: "I help you find and book flights",
@@ -261,55 +299,25 @@ const agent = new Agent<FlightBookingContext>({
261
299
  model: "gpt-5o-mini",
262
300
  }),
263
301
  context: {},
302
+ // NEW: Agent-level schema
303
+ schema: flightBookingSchema,
264
304
  hooks: {
265
305
  onDataUpdate, // Validation & enrichment hook
266
306
  },
267
307
  });
268
308
 
269
309
  // Define route with data extraction
270
- const bookingRoute = agent.createRoute<FlightData>({
310
+ const bookingRoute = agent.createRoute({
271
311
  title: "Book Flight",
272
312
  description: "Help user book a flight",
273
313
  conditions: [
274
314
  "User wants to book a flight",
275
315
  "User mentions flying, traveling, or booking",
276
316
  ],
277
- schema: {
278
- type: "object",
279
- properties: {
280
- destination: {
281
- type: "string",
282
- description: "City or airport the user wants to fly to",
283
- },
284
- destinationCode: {
285
- type: "string",
286
- description: "IATA airport code (enriched by tool)",
287
- },
288
- departureDate: {
289
- type: "string",
290
- description: "When the user wants to depart",
291
- },
292
- departureDateParsed: {
293
- type: "string",
294
- description: "Parsed ISO date (enriched by tool)",
295
- },
296
- passengers: {
297
- type: "number",
298
- minimum: 1,
299
- maximum: 9,
300
- },
301
- cabinClass: {
302
- type: "string",
303
- enum: ["economy", "business", "first"],
304
- default: "economy",
305
- },
306
- shouldSearchFlights: {
307
- type: "boolean",
308
- description: "Flag to trigger flight search",
309
- },
310
- },
311
- required: ["destination", "departureDate", "passengers"],
312
- },
317
+ // NEW: Required fields for route completion
318
+ requiredFields: ["destination", "departureDate", "passengers"],
319
+ // NEW: Optional fields that enhance the experience
320
+ optionalFields: ["destinationCode", "departureDateParsed", "cabinClass", "shouldSearchFlights"],
313
321
  });
314
322
 
315
323
  // Step 1: Collect destination
@@ -389,14 +397,14 @@ async function main() {
389
397
 
390
398
  console.log("\n=== RESPONSE ===");
391
399
  console.log("Message:", response.message);
392
- console.log("Data:", agent.session.getData<FlightData>());
400
+ console.log("Data:", agent.session.getData());
393
401
  console.log("Context:", agent["context"]);
394
402
 
395
403
  await agent.session.addMessage("assistant", response.message);
396
404
 
397
405
  if (response.isRouteComplete) {
398
406
  console.log("\n✅ Flight booking complete!");
399
- await sendBookingConfirmation(agent.session.getData<FlightData>());
407
+ await sendBookingConfirmation(agent.session.getData());
400
408
  }
401
409
 
402
410
  /*
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@falai/agent",
3
- "version": "0.9.0-alpha-1",
3
+ "version": "0.9.0",
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",