@falai/agent 0.9.0 → 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 (145) hide show
  1. package/README.md +42 -34
  2. package/dist/cjs/src/core/Agent.d.ts +19 -5
  3. package/dist/cjs/src/core/Agent.d.ts.map +1 -1
  4. package/dist/cjs/src/core/Agent.js +79 -35
  5. package/dist/cjs/src/core/Agent.js.map +1 -1
  6. package/dist/cjs/src/core/ResponseModal.d.ts +9 -3
  7. package/dist/cjs/src/core/ResponseModal.d.ts.map +1 -1
  8. package/dist/cjs/src/core/ResponseModal.js +121 -55
  9. package/dist/cjs/src/core/ResponseModal.js.map +1 -1
  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 +47 -19
  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 +2 -3
  35. package/dist/cjs/src/index.d.ts.map +1 -1
  36. package/dist/cjs/src/index.js +5 -3
  37. package/dist/cjs/src/index.js.map +1 -1
  38. package/dist/cjs/src/types/agent.d.ts +1 -1
  39. package/dist/cjs/src/types/agent.d.ts.map +1 -1
  40. package/dist/cjs/src/types/index.d.ts +3 -2
  41. package/dist/cjs/src/types/index.d.ts.map +1 -1
  42. package/dist/cjs/src/types/index.js +3 -1
  43. package/dist/cjs/src/types/index.js.map +1 -1
  44. package/dist/cjs/src/types/route.d.ts +6 -4
  45. package/dist/cjs/src/types/route.d.ts.map +1 -1
  46. package/dist/cjs/src/types/tool.d.ts +84 -14
  47. package/dist/cjs/src/types/tool.d.ts.map +1 -1
  48. package/dist/cjs/src/types/tool.js +13 -0
  49. package/dist/cjs/src/types/tool.js.map +1 -1
  50. package/dist/src/core/Agent.d.ts +19 -5
  51. package/dist/src/core/Agent.d.ts.map +1 -1
  52. package/dist/src/core/Agent.js +79 -35
  53. package/dist/src/core/Agent.js.map +1 -1
  54. package/dist/src/core/ResponseModal.d.ts +9 -3
  55. package/dist/src/core/ResponseModal.d.ts.map +1 -1
  56. package/dist/src/core/ResponseModal.js +121 -55
  57. package/dist/src/core/ResponseModal.js.map +1 -1
  58. package/dist/src/core/ResponsePipeline.d.ts +8 -4
  59. package/dist/src/core/ResponsePipeline.d.ts.map +1 -1
  60. package/dist/src/core/ResponsePipeline.js +47 -19
  61. package/dist/src/core/ResponsePipeline.js.map +1 -1
  62. package/dist/src/core/Route.d.ts +12 -5
  63. package/dist/src/core/Route.d.ts.map +1 -1
  64. package/dist/src/core/Route.js +26 -5
  65. package/dist/src/core/Route.js.map +1 -1
  66. package/dist/src/core/RoutingEngine.d.ts +5 -0
  67. package/dist/src/core/RoutingEngine.d.ts.map +1 -1
  68. package/dist/src/core/RoutingEngine.js +37 -25
  69. package/dist/src/core/RoutingEngine.js.map +1 -1
  70. package/dist/src/core/SessionManager.d.ts +9 -1
  71. package/dist/src/core/SessionManager.d.ts.map +1 -1
  72. package/dist/src/core/SessionManager.js +27 -5
  73. package/dist/src/core/SessionManager.js.map +1 -1
  74. package/dist/src/core/Step.d.ts +60 -7
  75. package/dist/src/core/Step.d.ts.map +1 -1
  76. package/dist/src/core/Step.js +151 -4
  77. package/dist/src/core/Step.js.map +1 -1
  78. package/dist/src/core/ToolManager.d.ts +234 -0
  79. package/dist/src/core/ToolManager.d.ts.map +1 -0
  80. package/dist/src/core/ToolManager.js +1111 -0
  81. package/dist/src/core/ToolManager.js.map +1 -0
  82. package/dist/src/index.d.ts +2 -3
  83. package/dist/src/index.d.ts.map +1 -1
  84. package/dist/src/index.js +1 -1
  85. package/dist/src/index.js.map +1 -1
  86. package/dist/src/types/agent.d.ts +1 -1
  87. package/dist/src/types/agent.d.ts.map +1 -1
  88. package/dist/src/types/index.d.ts +3 -2
  89. package/dist/src/types/index.d.ts.map +1 -1
  90. package/dist/src/types/index.js +1 -0
  91. package/dist/src/types/index.js.map +1 -1
  92. package/dist/src/types/route.d.ts +6 -4
  93. package/dist/src/types/route.d.ts.map +1 -1
  94. package/dist/src/types/tool.d.ts +84 -14
  95. package/dist/src/types/tool.d.ts.map +1 -1
  96. package/dist/src/types/tool.js +12 -1
  97. package/dist/src/types/tool.js.map +1 -1
  98. package/docs/CONTRIBUTING.md +40 -0
  99. package/docs/README.md +12 -5
  100. package/docs/api/README.md +75 -45
  101. package/docs/api/overview.md +74 -32
  102. package/docs/core/agent/session-management.md +152 -5
  103. package/docs/core/ai-integration/response-processing.md +115 -4
  104. package/docs/core/conversation-flows/routes.md +130 -0
  105. package/docs/core/error-handling.md +638 -0
  106. package/docs/core/tools/tool-definition.md +684 -60
  107. package/docs/core/tools/tool-scoping.md +244 -53
  108. package/docs/guides/error-handling-patterns.md +578 -0
  109. package/docs/guides/getting-started/README.md +139 -28
  110. package/examples/advanced-patterns/knowledge-based-agent.ts +6 -6
  111. package/examples/advanced-patterns/persistent-onboarding.ts +30 -43
  112. package/examples/ai-providers/anthropic-integration.ts +9 -5
  113. package/examples/ai-providers/openai-integration.ts +11 -7
  114. package/examples/core-concepts/basic-agent.ts +106 -67
  115. package/examples/core-concepts/schema-driven-extraction.ts +10 -7
  116. package/examples/core-concepts/session-management.ts +71 -18
  117. package/examples/integrations/healthcare-integration.ts +15 -29
  118. package/examples/integrations/server-session-management.ts +3 -3
  119. package/examples/persistence/memory-sessions.ts +3 -3
  120. package/examples/tools/basic-tools.ts +293 -89
  121. package/examples/tools/data-enrichment-tools.ts +185 -75
  122. package/package.json +1 -1
  123. package/src/core/Agent.ts +98 -44
  124. package/src/core/ResponseModal.ts +148 -72
  125. package/src/core/ResponsePipeline.ts +82 -56
  126. package/src/core/Route.ts +39 -12
  127. package/src/core/RoutingEngine.ts +46 -42
  128. package/src/core/SessionManager.ts +39 -7
  129. package/src/core/Step.ts +198 -20
  130. package/src/core/ToolManager.ts +1394 -0
  131. package/src/index.ts +8 -3
  132. package/src/types/agent.ts +1 -1
  133. package/src/types/index.ts +13 -2
  134. package/src/types/route.ts +6 -4
  135. package/src/types/tool.ts +116 -25
  136. package/dist/cjs/src/core/ToolExecutor.d.ts +0 -45
  137. package/dist/cjs/src/core/ToolExecutor.d.ts.map +0 -1
  138. package/dist/cjs/src/core/ToolExecutor.js +0 -84
  139. package/dist/cjs/src/core/ToolExecutor.js.map +0 -1
  140. package/dist/src/core/ToolExecutor.d.ts +0 -45
  141. package/dist/src/core/ToolExecutor.d.ts.map +0 -1
  142. package/dist/src/core/ToolExecutor.js +0 -80
  143. package/dist/src/core/ToolExecutor.js.map +0 -1
  144. package/docs/core/tools/tool-execution.md +0 -815
  145. package/src/core/ToolExecutor.ts +0 -126
@@ -13,9 +13,10 @@
13
13
  import {
14
14
  Agent,
15
15
  GeminiProvider,
16
+ Tool,
17
+ ValidationError,
16
18
  type Term,
17
19
  type Guideline,
18
- type Tool,
19
20
  type RouteOptions,
20
21
  } from "../../src/index";
21
22
 
@@ -40,98 +41,71 @@ interface LabData {
40
41
  resultsNeeded: boolean;
41
42
  }
42
43
 
43
- // Define tools using the new Tool interface
44
- const getInsuranceProviders: Tool<HealthcareContext, HealthcareData, [], string[]> = {
44
+ // Define tools using unified Tool interface
45
+ const getInsuranceProvidersTool: Tool<HealthcareContext, HealthcareData> = {
45
46
  id: "healthcare_insurance_providers",
46
47
  description: "Retrieves list of accepted insurance providers",
47
48
  parameters: {
48
49
  type: "object",
49
50
  properties: {},
50
51
  },
51
- handler: () => {
52
+ handler: async (context, args) => {
52
53
  return {
53
- data: ["MegaCare Insurance", "HealthFirst", "WellnessPlus"],
54
+ data: "Available insurance providers: MegaCare Insurance, HealthFirst, WellnessPlus",
54
55
  };
55
56
  },
56
57
  };
57
58
 
58
- const getAvailableSlots: Tool<
59
- HealthcareContext,
60
- HealthcareData,
61
- [],
62
- { date: string; time: string }[]
63
- > = {
59
+ const getAvailableSlotsTool: Tool<HealthcareContext, HealthcareData> = {
64
60
  id: "healthcare_available_slots",
65
61
  description: "Gets available appointment slots",
66
62
  parameters: {
67
63
  type: "object",
68
64
  properties: {},
69
65
  },
70
- handler: () => {
66
+ handler: async (context, args) => {
71
67
  return {
72
- data: [
73
- { date: "2025-10-20", time: "10:00 AM" },
74
- { date: "2025-10-20", time: "2:00 PM" },
75
- { date: "2025-10-21", time: "1:00 PM" },
76
- ],
68
+ data: "Available slots: Oct 20 at 10:00 AM, Oct 20 at 2:00 PM, Oct 21 at 1:00 PM",
77
69
  };
78
70
  },
79
71
  };
80
72
 
81
- const getLabResults: Tool<
82
- HealthcareContext,
83
- HealthcareData,
84
- [],
85
- { report: string; status: string }
86
- > = {
73
+ const getLabResultsTool: Tool<HealthcareContext, HealthcareData> = {
87
74
  id: "healthcare_lab_results",
88
75
  description: "Retrieves patient lab results",
89
76
  parameters: {
90
77
  type: "object",
91
78
  properties: {},
92
79
  },
93
- handler: ({ context, data }) => {
80
+ handler: async (toolContext, args) => {
94
81
  // Tools can access collected data and context
95
- if (data?.testType) {
82
+ if (toolContext.data?.testType) {
96
83
  return {
97
- data: {
98
- report: `${data.testType} results for ${context.patientName}`,
99
- status: "All values within normal range",
100
- },
84
+ data: `${toolContext.data.testType} results for ${toolContext.context.patientName}: All values within normal range`,
101
85
  };
102
86
  }
103
87
 
104
88
  return {
105
- data: {
106
- report: `Lab results for ${context.patientName}`,
107
- status: "All values within normal range",
108
- },
89
+ data: `Lab results for ${toolContext.context.patientName}: All values within normal range`,
109
90
  };
110
91
  },
111
92
  };
112
93
 
113
- const scheduleAppointment: Tool<
114
- HealthcareContext,
115
- HealthcareData,
116
- [],
117
- { confirmation: string }
118
- > = {
94
+ const scheduleAppointmentTool: Tool<HealthcareContext, HealthcareData> = {
119
95
  id: "healthcare_schedule_appointment",
120
96
  description: "Schedules patient appointments",
121
97
  parameters: {
122
98
  type: "object",
123
99
  properties: {},
124
100
  },
125
- handler: ({ data }) => {
101
+ handler: async (context, args) => {
126
102
  // Tools access collected appointment data
127
- if (!data?.preferredDate || !data?.preferredTime) {
128
- return { data: { confirmation: "Please provide appointment details" } };
103
+ if (!context.data?.preferredDate || !context.data?.preferredTime) {
104
+ return { data: "Please provide appointment details" };
129
105
  }
130
106
 
131
107
  return {
132
- data: {
133
- confirmation: `Appointment scheduled for ${data.preferredDate} at ${data.preferredTime}`,
134
- },
108
+ data: `Appointment scheduled for ${context.data.preferredDate} at ${context.data.preferredTime}`,
135
109
  };
136
110
  },
137
111
  };
@@ -175,7 +149,7 @@ const guidelines: Guideline<HealthcareContext>[] = [
175
149
  },
176
150
  ];
177
151
 
178
- const routes: RouteOptions<HealthcareContext, HealthcareData>[] = [
152
+ const routes = [
179
153
  {
180
154
  id: "route_schedule_appointment",
181
155
  title: "Schedule Appointment",
@@ -221,27 +195,13 @@ const routes: RouteOptions<HealthcareContext, HealthcareData>[] = [
221
195
  id: "schedule_appointment",
222
196
  description: "Schedule the appointment",
223
197
  prompt: "I'll schedule your appointment now.",
224
- tools: [scheduleAppointment], // Use the scheduling tool
198
+ tools: ["healthcare_schedule_appointment"], // Reference by ID
225
199
  requires: ["preferredDate", "preferredTime"],
226
- prepare: getInsuranceProviders, // Use existing tool to prepare insurance info
227
- finalize: {
228
- // Inline tool to handle appointment finalization
229
- id: "finalize_appointment",
230
- description: "Complete the appointment booking process",
231
- parameters: { type: "object", properties: {} },
232
- handler: ({ context, data }) => {
233
- console.log(`✅ Appointment finalized for ${context.patientName}`);
234
- console.log(`📅 Details: ${JSON.stringify(data, null, 2)}`);
235
-
236
- // Could send confirmation email, update calendar, etc.
237
- return {
238
- data: `Appointment confirmed for ${context.patientName}`,
239
- };
240
- },
241
- },
200
+ prepare: "healthcare_insurance_providers", // Reference by ID
201
+ finalize: "finalize_appointment", // Reference by ID - will be registered later
242
202
  },
243
203
  ],
244
- tools: [getAvailableSlots], // Route-level tools available to all steps
204
+ tools: ["healthcare_available_slots"], // Reference by ID
245
205
  },
246
206
  {
247
207
  id: "route_check_lab_results",
@@ -260,7 +220,7 @@ const routes: RouteOptions<HealthcareContext, HealthcareData>[] = [
260
220
  tags: ["escalation"],
261
221
  },
262
222
  ],
263
- tools: [getLabResults], // Route-level tools
223
+ tools: ["healthcare_lab_results"], // Reference by ID
264
224
  },
265
225
  {
266
226
  title: "General Healthcare Questions",
@@ -334,8 +294,87 @@ const agent = new Agent<HealthcareContext, HealthcareData>({
334
294
  // Declarative initialization
335
295
  terms,
336
296
  guidelines,
337
- tools: [getInsuranceProviders], // Agent-level tools
338
- routes,
297
+ });
298
+
299
+ // Demonstrate different tool registration approaches
300
+
301
+ // Method 1: Register tools for ID-based reference in routes
302
+ agent.tool.registerMany([
303
+ getInsuranceProvidersTool,
304
+ getAvailableSlotsTool,
305
+ getLabResultsTool,
306
+ scheduleAppointmentTool,
307
+ ]);
308
+
309
+ // Method 2: Create and register specialized tools
310
+ const appointmentValidator = agent.tool.createValidation({
311
+ id: "validate_appointment",
312
+ fields: ["appointmentType", "preferredDate", "preferredTime"] as const,
313
+ validator: async (context, data) => {
314
+ const errors: ValidationError[] = [];
315
+ if (!data.appointmentType) errors.push({
316
+ field: "appointmentType",
317
+ value: data.appointmentType,
318
+ message: "Appointment type is required",
319
+ schemaPath: "appointmentType"
320
+ });
321
+ if (!data.preferredDate) errors.push({
322
+ field: "preferredDate",
323
+ value: data.preferredDate,
324
+ message: "Preferred date is required",
325
+ schemaPath: "preferredDate"
326
+ });
327
+ if (!data.preferredTime) errors.push({
328
+ field: "preferredTime",
329
+ value: data.preferredTime,
330
+ message: "Preferred time is required",
331
+ schemaPath: "preferredTime"
332
+ });
333
+
334
+ return {
335
+ valid: errors.length === 0,
336
+ errors,
337
+ warnings: [],
338
+ };
339
+ },
340
+ });
341
+
342
+ // Method 3: Create data enrichment tool
343
+ const patientDataEnricher = agent.tool.createDataEnrichment({
344
+ id: "enrich_patient_data",
345
+ fields: ["appointmentType", "symptoms"] as const,
346
+ enricher: async (context, data) => {
347
+ // Add urgency classification based on symptoms - return fields that exist in HealthcareData
348
+ const urgentKeywords = ["chest pain", "difficulty breathing", "severe", "emergency"];
349
+ const hasUrgentSymptoms = data.symptoms && urgentKeywords.some(keyword =>
350
+ data.symptoms!.toLowerCase().includes(keyword)
351
+ );
352
+
353
+ return {
354
+ urgency: hasUrgentSymptoms ? "high" : "medium", // This matches the urgency field in HealthcareData
355
+ };
356
+ },
357
+ });
358
+
359
+ // Method 4: Create tool using tool.create()
360
+ const finalizeAppointmentTool = agent.tool.create({
361
+ id: "finalize_appointment",
362
+ description: "Complete the appointment booking process",
363
+ parameters: { type: "object", properties: {} },
364
+ handler: async (context, args) => {
365
+ console.log(`✅ Appointment finalized for ${context.context.patientName}`);
366
+ console.log(`📅 Details: ${JSON.stringify(context.data, null, 2)}`);
367
+
368
+ // Could send confirmation email, update calendar, etc.
369
+ return {
370
+ data: `Appointment confirmed for ${context.context.patientName}`,
371
+ };
372
+ },
373
+ });
374
+
375
+ // Add routes after tools are registered
376
+ routes.forEach((route: any) => {
377
+ agent.createRoute(route);
339
378
  });
340
379
 
341
380
  // You can still add more dynamically after construction
@@ -23,22 +23,22 @@ interface UserProfileData {
23
23
  newsletterOptIn: boolean;
24
24
  }
25
25
 
26
- // Define a tool that uses the collected data
27
- const saveUserProfile: Tool<unknown, UserProfileData, [], string> = {
26
+ // Define a tool that uses the collected data - using unified Tool interface
27
+ const saveUserProfileTool: Tool<unknown, UserProfileData> = {
28
28
  id: "save_user_profile",
29
29
  description: "Save the collected user profile information",
30
30
  parameters: {
31
31
  type: "object",
32
32
  properties: {},
33
33
  },
34
- handler: ({ data }) => {
35
- console.log("Saving user profile:", data);
34
+ handler: async (context, args) => {
35
+ console.log("Saving user profile:", context.data);
36
36
 
37
37
  // Simulate saving to database
38
- console.log("Profile data:", data);
38
+ console.log("Profile data:", context.data);
39
39
 
40
40
  return {
41
- data: `Profile saved successfully! Welcome ${data?.name}!`,
41
+ data: `Profile saved successfully! Welcome ${context.data?.name}!`,
42
42
  };
43
43
  },
44
44
  };
@@ -99,6 +99,9 @@ const agent = new Agent<unknown, UserProfileData>({
99
99
  schema: userProfileSchema,
100
100
  });
101
101
 
102
+ // Add tool using unified interface
103
+ agent.addTool(saveUserProfileTool);
104
+
102
105
  // Create a route that collects profile information step by step
103
106
  agent.createRoute({
104
107
  title: "User Profile Collection",
@@ -163,7 +166,7 @@ agent.createRoute({
163
166
  description: "Save the collected profile",
164
167
  prompt:
165
168
  "Thanks for providing your information! Let me save your profile.",
166
- tools: [saveUserProfile],
169
+ tools: ["save_user_profile"], // Reference by ID
167
170
  requires: ["name", "email"],
168
171
  },
169
172
  ],
@@ -17,6 +17,7 @@ import {
17
17
  GeminiProvider,
18
18
  MemoryAdapter,
19
19
  type Tool,
20
+ ValidationError,
20
21
  } from "../../src/index";
21
22
 
22
23
  // Define data types for our multi-step process
@@ -36,23 +37,23 @@ interface OrderContext {
36
37
  }
37
38
 
38
39
  interface PaymentData {
39
- orderId: string;
40
+ orderId?: string; // Make this optional to match OrderData
40
41
  paymentMethod: "credit_card" | "paypal" | "bank_transfer";
41
42
  amount: number;
42
43
  confirmed: boolean;
43
44
  }
44
45
 
45
- // Tools for order processing
46
- const createOrder: Tool<unknown, UnifiedOrderData, [], string> = {
46
+ // Tools for order processing - using unified Tool interface
47
+ const createOrderTool: Tool<unknown, UnifiedOrderData> = {
47
48
  id: "create_order",
48
49
  description: "Create a new order from the collected order data",
49
50
  parameters: {
50
51
  type: "object",
51
52
  properties: {},
52
53
  },
53
- handler: ({ data }) => {
54
+ handler: (context, args) => {
54
55
  const orderId = `ORD-${Date.now()}`;
55
- console.log(`Creating order ${orderId} for ${data?.customerName}`);
56
+ console.log(`Creating order ${orderId} for ${context.data?.customerName}`);
56
57
 
57
58
  return {
58
59
  data: `Order ${orderId} created successfully!`,
@@ -63,22 +64,22 @@ const createOrder: Tool<unknown, UnifiedOrderData, [], string> = {
63
64
  },
64
65
  };
65
66
 
66
- const processPayment: Tool<unknown, UnifiedOrderData, [], string> = {
67
+ const processPaymentTool: Tool<unknown, UnifiedOrderData> = {
67
68
  id: "process_payment",
68
69
  description: "Process payment for an order",
69
70
  parameters: {
70
71
  type: "object",
71
72
  properties: {},
72
73
  },
73
- handler: ({ data }) => {
74
- console.log(`Processing payment for order ${data?.orderId}`);
74
+ handler: (context, args) => {
75
+ console.log(`Processing payment for order ${context.data?.orderId}`);
75
76
 
76
77
  // Simulate payment processing
77
78
  const success = Math.random() > 0.1; // 90% success rate
78
79
 
79
80
  if (success) {
80
81
  return {
81
- data: `Payment processed successfully! Order ${data?.orderId} is now confirmed.`,
82
+ data: `Payment processed successfully! Order ${context.data?.orderId} is now confirmed.`,
82
83
  dataUpdate: {
83
84
  confirmed: true,
84
85
  },
@@ -133,6 +134,58 @@ const agent = new Agent<unknown, UnifiedOrderData>({
133
134
  },
134
135
  });
135
136
 
137
+ // Demonstrate different tool registration patterns
138
+
139
+ // Method 1: Register tools for ID-based reference in steps
140
+ agent.tool.register(createOrderTool);
141
+ agent.tool.register(processPaymentTool);
142
+
143
+ // Method 2: Create specialized validation tool
144
+ const orderValidationTool = agent.tool.createValidation({
145
+ id: "validate_order",
146
+ fields: ["customerName", "productType", "budget"] as const,
147
+ validator: async (context, data) => {
148
+ const errors: ValidationError[] = [];
149
+ if (!data.customerName || data.customerName.length < 2) {
150
+ errors.push({
151
+ field: "customerName",
152
+ value: data.customerName,
153
+ message: "Customer name must be at least 2 characters",
154
+ schemaPath: "customerName"
155
+ });
156
+ }
157
+ if (!data.budget || data.budget < 100) {
158
+ errors.push({
159
+ field: "budget",
160
+ value: data.budget,
161
+ message: "Budget must be at least $100",
162
+ schemaPath: "budget"
163
+ });
164
+ }
165
+ return {
166
+ valid: errors.length === 0,
167
+ errors,
168
+ warnings: [],
169
+ };
170
+ },
171
+ });
172
+
173
+ // Method 3: Create data enrichment tool
174
+ const orderEnrichmentTool = agent.tool.createDataEnrichment({
175
+ id: "enrich_order",
176
+ fields: ["productType", "budget"] as const,
177
+ enricher: async (context, data) => {
178
+ // Enrich with fields that exist in UnifiedOrderData
179
+ const urgentDelivery = data.budget > 1000; // Premium orders get urgent delivery
180
+ const preferredColor = data.productType === "laptop" ? "silver" : "black";
181
+
182
+ return {
183
+ urgentDelivery,
184
+ preferredColor,
185
+ };
186
+ },
187
+ });
188
+
136
189
  // Order collection route with sequential steps
137
190
  agent.createRoute({
138
191
  title: "Product Order",
@@ -186,7 +239,7 @@ agent.createRoute({
186
239
  id: "create_order",
187
240
  description: "Create the order",
188
241
  prompt: "Great! Let me create your order.",
189
- tools: [createOrder],
242
+ tools: ["create_order"], // Reference by ID
190
243
  requires: ["customerName", "productType", "budget"],
191
244
  },
192
245
  ],
@@ -226,7 +279,7 @@ agent.createRoute({
226
279
  id: "process_payment",
227
280
  description: "Process the payment",
228
281
  prompt: "Processing your payment...",
229
- tools: [processPayment],
282
+ tools: ["process_payment"], // Reference by ID
230
283
  requires: ["orderId", "paymentMethod", "amount"],
231
284
  },
232
285
  ],
@@ -266,7 +319,7 @@ async function demonstrateSessionManagement() {
266
319
 
267
320
  console.log("Bot:", response1.message);
268
321
  console.log("Session ID:", sessionAgent.session.id);
269
- console.log("Session data:", JSON.stringify(sessionAgent.session.getData<UnifiedOrderData>(), null, 2));
322
+ console.log("Session data:", JSON.stringify(sessionAgent.session.getData(), null, 2));
270
323
  console.log("History length:", sessionAgent.session.getHistory().length);
271
324
  console.log();
272
325
 
@@ -277,7 +330,7 @@ async function demonstrateSessionManagement() {
277
330
  const response2 = await sessionAgent.chat("My budget is $1500");
278
331
 
279
332
  console.log("Bot:", response2.message);
280
- console.log("Session data:", JSON.stringify(sessionAgent.session.getData<UnifiedOrderData>(), null, 2));
333
+ console.log("Session data:", JSON.stringify(sessionAgent.session.getData(), null, 2));
281
334
  console.log("History length:", sessionAgent.session.getHistory().length);
282
335
  console.log();
283
336
 
@@ -288,7 +341,7 @@ async function demonstrateSessionManagement() {
288
341
  const response3 = await sessionAgent.chat("I want black color and urgent delivery please");
289
342
 
290
343
  console.log("Bot:", response3.message);
291
- console.log("Session data:", JSON.stringify(sessionAgent.session.getData<UnifiedOrderData>(), null, 2));
344
+ console.log("Session data:", JSON.stringify(sessionAgent.session.getData(), null, 2));
292
345
  console.log("Route complete:", response3.isRouteComplete);
293
346
  console.log("History length:", sessionAgent.session.getHistory().length);
294
347
  console.log();
@@ -300,7 +353,7 @@ async function demonstrateSessionManagement() {
300
353
  const response4 = await sessionAgent.chat("I'll pay with credit card, amount is $1599");
301
354
 
302
355
  console.log("Bot:", response4.message);
303
- console.log("Session data:", JSON.stringify(sessionAgent.session.getData<UnifiedOrderData>(), null, 2));
356
+ console.log("Session data:", JSON.stringify(sessionAgent.session.getData(), null, 2));
304
357
  console.log("Route complete:", response4.isRouteComplete);
305
358
  console.log("Final history length:", sessionAgent.session.getHistory().length);
306
359
  }
@@ -344,7 +397,7 @@ async function demonstrateSessionPersistence() {
344
397
 
345
398
  console.log("After first interaction:");
346
399
  console.log("🤖 Agent:", response1.message);
347
- console.log("Session data:", JSON.stringify(persistentAgent.session.getData<UnifiedOrderData>(), null, 2));
400
+ console.log("Session data:", JSON.stringify(persistentAgent.session.getData(), null, 2));
348
401
  console.log("History length:", persistentAgent.session.getHistory().length);
349
402
  console.log("Session automatically saved to persistence ✓");
350
403
 
@@ -379,14 +432,14 @@ async function demonstrateSessionPersistence() {
379
432
  console.log("Session automatically restored:");
380
433
  console.log("- Session ID:", restoredAgent.session.id);
381
434
  console.log("- History length:", restoredAgent.session.getHistory().length);
382
- console.log("- Restored data:", JSON.stringify(restoredAgent.session.getData<UnifiedOrderData>(), null, 2));
435
+ console.log("- Restored data:", JSON.stringify(restoredAgent.session.getData(), null, 2));
383
436
 
384
437
  // Continue conversation seamlessly
385
438
  const response2 = await restoredAgent.chat("Actually, make it urgent delivery");
386
439
 
387
440
  console.log("\nAfter continuing with restored session:");
388
441
  console.log("🤖 Agent:", response2.message);
389
- console.log("Session data:", JSON.stringify(restoredAgent.session.getData<UnifiedOrderData>(), null, 2));
442
+ console.log("Session data:", JSON.stringify(restoredAgent.session.getData(), null, 2));
390
443
  console.log("History length:", restoredAgent.session.getHistory().length);
391
444
  console.log("Session automatically saved again ✓");
392
445
  }
@@ -39,7 +39,7 @@ interface SatisfactionData {
39
39
 
40
40
  // Tools
41
41
 
42
- const getUpcomingSlots: Tool<HealthcareContext, HealthcareData, unknown[], unknown> = {
42
+ const getUpcomingSlots: Tool<HealthcareContext, HealthcareData> = {
43
43
  id: "get_upcoming_slots",
44
44
  name: "Available Appointment Slots",
45
45
  description: "Get upcoming appointment slots",
@@ -47,12 +47,12 @@ const getUpcomingSlots: Tool<HealthcareContext, HealthcareData, unknown[], unkno
47
47
  type: "object",
48
48
  properties: {},
49
49
  },
50
- handler: () => {
51
- return { data: ["Monday 10 AM", "Tuesday 2 PM", "Wednesday 1 PM"] };
50
+ handler: (context, args) => {
51
+ return { data: "Available slots: Monday 10 AM, Tuesday 2 PM, Wednesday 1 PM" };
52
52
  },
53
53
  };
54
54
 
55
- const getLaterSlots: Tool<HealthcareContext, HealthcareData, unknown[], unknown> = {
55
+ const getLaterSlots: Tool<HealthcareContext, HealthcareData> = {
56
56
  id: "get_later_slots",
57
57
  name: "Extended Appointment Slots",
58
58
  description: "Get later appointment slots",
@@ -60,17 +60,12 @@ const getLaterSlots: Tool<HealthcareContext, HealthcareData, unknown[], unknown>
60
60
  type: "object",
61
61
  properties: {},
62
62
  },
63
- handler: () => {
64
- return { data: ["November 3, 11:30 AM", "November 12, 3 PM"] };
63
+ handler: (context, args) => {
64
+ return { data: "Later slots: November 3, 11:30 AM, November 12, 3 PM" };
65
65
  },
66
66
  };
67
67
 
68
- const scheduleAppointment: Tool<
69
- HealthcareContext,
70
- HealthcareData,
71
- unknown[],
72
- unknown
73
- > = {
68
+ const scheduleAppointment: Tool<HealthcareContext, HealthcareData> = {
74
69
  id: "schedule_appointment",
75
70
  name: "Appointment Scheduler",
76
71
  description: "Schedule an appointment with a healthcare provider",
@@ -81,26 +76,21 @@ const scheduleAppointment: Tool<
81
76
  },
82
77
  required: ["datetime"],
83
78
  },
84
- handler: ({ data }) => {
85
- if (!data?.preferredDate || !data?.preferredTime) {
79
+ handler: (context, args) => {
80
+ if (!context.data?.preferredDate || !context.data?.preferredTime) {
86
81
  return {
87
82
  data: "Please specify preferred date and time for the appointment",
88
83
  };
89
84
  }
90
85
  return {
91
- data: `Appointment scheduled for ${data.preferredDate} at ${
92
- data.preferredTime
93
- } for ${data.appointmentReason || "consultation"}`,
86
+ data: `Appointment scheduled for ${context.data.preferredDate} at ${
87
+ context.data.preferredTime
88
+ } for ${context.data.appointmentReason || "consultation"}`,
94
89
  };
95
90
  },
96
91
  };
97
92
 
98
- const getLabResults: Tool<
99
- HealthcareContext,
100
- HealthcareData,
101
- unknown[],
102
- unknown
103
- > = {
93
+ const getLabResults: Tool<HealthcareContext, HealthcareData> = {
104
94
  id: "get_lab_results",
105
95
  name: "Lab Results Retriever",
106
96
  description: "Get lab test results",
@@ -108,13 +98,9 @@ const getLabResults: Tool<
108
98
  type: "object",
109
99
  properties: {},
110
100
  },
111
- handler: ({ context, data }) => {
101
+ handler: (context, args) => {
112
102
  return {
113
- data: {
114
- report: `Lab results for ${data?.testType || "general"} tests`,
115
- prognosis: "All tests are within the valid range",
116
- patientName: context.patientName,
117
- },
103
+ data: `Lab results for ${context.data?.testType || "general"} tests: All tests are within the valid range for ${context.context.patientName}`,
118
104
  };
119
105
  },
120
106
  };
@@ -31,15 +31,15 @@ interface BookingData {
31
31
  }
32
32
 
33
33
  // Booking confirmation tool
34
- const confirmBooking: Tool<unknown, BookingData, [], string> = {
34
+ const confirmBooking: Tool<unknown, BookingData> = {
35
35
  id: "confirm_booking",
36
36
  description: "Confirm the hotel booking with all details",
37
37
  parameters: {
38
38
  type: "object",
39
39
  properties: {},
40
40
  },
41
- handler: ({ data }) => {
42
- const bookingData = data as Partial<BookingData>;
41
+ handler: (context, args) => {
42
+ const bookingData = context.data;
43
43
  const bookingId = `BK-${Date.now()}`;
44
44
 
45
45
  console.log(`📋 Confirming booking ${bookingId} for ${bookingData.customerName}`);
@@ -38,15 +38,15 @@ interface SupportContext {
38
38
  }
39
39
 
40
40
  // Support tools
41
- const createTicket: Tool<unknown, SupportTicketData, [], string> = {
41
+ const createTicket: Tool<unknown, SupportTicketData> = {
42
42
  id: "create_support_ticket",
43
43
  description: "Create a new support ticket",
44
44
  parameters: {
45
45
  type: "object",
46
46
  properties: {},
47
47
  },
48
- handler: ({ data }) => {
49
- const ticketData = data as Partial<SupportTicketData>;
48
+ handler: (context, args) => {
49
+ const ticketData = context.data;
50
50
  const ticketId = `TICKET-${Date.now()}`;
51
51
  console.log(
52
52
  `Creating ticket ${ticketId} for ${ticketData?.category} issue`