@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
@@ -7,6 +7,7 @@ import type { Tool } from "./tool";
7
7
  import type { RouteOptions } from "./route";
8
8
  import type { PersistenceConfig } from "./persistence";
9
9
  import type { SessionState } from "./session";
10
+ import type { StructuredSchema } from "./schema";
10
11
  import { Template } from "./template";
11
12
 
12
13
  /**
@@ -26,7 +27,7 @@ export enum CompositionMode {
26
27
  /**
27
28
  * Context lifecycle hooks for managing step persistence
28
29
  */
29
- export interface ContextLifecycleHooks<TContext = unknown> {
30
+ export interface ContextLifecycleHooks<TContext = unknown, TData = unknown> {
30
31
  /**
31
32
  * Called before respond() to get fresh context
32
33
  * Useful for loading context from a database or cache
@@ -47,13 +48,12 @@ export interface ContextLifecycleHooks<TContext = unknown> {
47
48
  * Useful for validation, enrichment, or persistence of collected data
48
49
  * Return modified collected data or the same data to keep it unchanged
49
50
  *
50
- * Note: This hook works with ANY route's collected data (since an agent can have
51
- * multiple routes with different extraction schemas). Use type guards or runtime
52
- * checks if you need type-specific logic.
51
+ * Note: This hook now works with agent-level data collection (TData type)
53
52
  */
54
-
55
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
- onDataUpdate?: (data: any, previousCollected: any) => any;
53
+ onDataUpdate?: (
54
+ data: Partial<TData>,
55
+ previousCollected: Partial<TData>
56
+ ) => Partial<TData> | Promise<Partial<TData>>;
57
57
  }
58
58
 
59
59
  /**
@@ -67,7 +67,7 @@ export type ContextProvider<TContext = unknown> = () =>
67
67
  /**
68
68
  * Options for creating an Agent
69
69
  */
70
- export interface AgentOptions<TContext = unknown> {
70
+ export interface AgentOptions<TContext = unknown, TData = unknown> {
71
71
  /** Display name of the agent */
72
72
  name: string;
73
73
  /** Detailed description of the agent's purpose and personality */
@@ -89,47 +89,51 @@ export interface AgentOptions<TContext = unknown> {
89
89
  /** Context provider function for always-fresh context (alternative to static context) */
90
90
  contextProvider?: ContextProvider<TContext>;
91
91
  /** Lifecycle hooks for context management */
92
- hooks?: ContextLifecycleHooks<TContext>;
92
+ hooks?: ContextLifecycleHooks<TContext, TData>;
93
93
  /** AI provider strategy for generating responses */
94
94
  provider: AiProvider;
95
95
  /** Composition mode for response generation */
96
96
  compositionMode?: CompositionMode;
97
97
  /** Initial terms for domain glossary */
98
- terms?: Term<TContext>[];
98
+ terms?: Term<TContext, TData>[];
99
99
  /** Initial guidelines for agent behavior */
100
- guidelines?: Guideline<TContext>[];
100
+ guidelines?: Guideline<TContext, TData>[];
101
101
  /** Global tools available to all routes */
102
- tools?: Tool<TContext, unknown[], unknown, unknown>[];
102
+ tools?: Tool<TContext, TData, unknown[], unknown>[];
103
103
  /** Initial routes (will be instantiated as Route objects) */
104
- routes?: RouteOptions<TContext, unknown>[];
104
+ routes?: RouteOptions<TContext, TData>[];
105
105
  /** Optional persistence configuration for auto-saving sessions and messages */
106
- persistence?: PersistenceConfig;
106
+ persistence?: PersistenceConfig<TData>;
107
107
  /** Knowledge base containing any JSON structure the AI should know */
108
108
  knowledgeBase?: Record<string, unknown>;
109
+ /** Agent-level data schema defining the complete data structure for collection */
110
+ schema?: StructuredSchema;
111
+ /** Initial data to pre-populate when creating the agent */
112
+ initialData?: Partial<TData>;
109
113
  }
110
114
 
111
115
  /**
112
116
  * A term in the domain glossary
113
117
  */
114
- export interface Term<TContext = unknown> {
118
+ export interface Term<TContext = unknown, TData = unknown> {
115
119
  /** Name of the term */
116
- name: Template<TContext>;
120
+ name: Template<TContext, TData>;
117
121
  /** Description/definition of the term */
118
- description: Template<TContext>;
122
+ description: Template<TContext, TData>;
119
123
  /** Alternative names or synonyms */
120
- synonyms?: Template<TContext>[];
124
+ synonyms?: Template<TContext, TData>[];
121
125
  }
122
126
 
123
127
  /**
124
128
  * A behavioral guideline for the agent
125
129
  */
126
- export interface Guideline<TContext = unknown> {
130
+ export interface Guideline<TContext = unknown, TData = unknown> {
127
131
  /** Unique identifier */
128
132
  id?: string;
129
133
  /** Condition that triggers this guideline (optional for always-active guidelines) */
130
- condition?: Template<TContext>;
134
+ condition?: Template<TContext, TData>;
131
135
  /** Action the agent should take when the condition is met */
132
- action: Template<TContext>;
136
+ action: Template<TContext, TData>;
133
137
  /** Whether this guideline is currently enabled */
134
138
  enabled?: boolean;
135
139
  /** Tags for organizing and filtering guidelines */
@@ -141,9 +145,9 @@ export interface Guideline<TContext = unknown> {
141
145
  /**
142
146
  * Guideline match with rationale
143
147
  */
144
- export interface GuidelineMatch<TContext = unknown> {
148
+ export interface GuidelineMatch<TContext = unknown, TData = unknown> {
145
149
  /** The matched guideline */
146
- guideline: Guideline<TContext>;
150
+ guideline: Guideline<TContext, TData>;
147
151
  /** Explanation of why this guideline was matched */
148
152
  rationale?: string;
149
153
  }
@@ -169,4 +173,24 @@ export interface AgentResponseStreamChunk<TData = Record<string, unknown>> {
169
173
  [key: string]: unknown;
170
174
  };
171
175
  structured?: AgentStructuredResponse;
176
+ error?: Error;
177
+ }
178
+
179
+ /**
180
+ * Validation error for data validation
181
+ */
182
+ export interface ValidationError {
183
+ field: string;
184
+ value: unknown;
185
+ message: string;
186
+ schemaPath: string;
187
+ }
188
+
189
+ /**
190
+ * Result of data validation
191
+ */
192
+ export interface ValidationResult {
193
+ valid: boolean;
194
+ errors: ValidationError[];
195
+ warnings: ValidationError[];
172
196
  }
package/src/types/ai.ts CHANGED
@@ -66,7 +66,7 @@ export interface GenerateMessageInput<TContext = unknown> {
66
66
  /**
67
67
  * Structured response from AI containing message and metadata
68
68
  */
69
- export interface AgentStructuredResponse {
69
+ export interface AgentStructuredResponse extends Record<string, unknown> {
70
70
  /** The actual message to send to the user */
71
71
  message: string;
72
72
  /** Route chosen by the agent (route title or null if no route) */
@@ -12,6 +12,8 @@ export type {
12
12
  AgentResponse,
13
13
  ContextLifecycleHooks,
14
14
  ContextProvider,
15
+ ValidationError,
16
+ ValidationResult,
15
17
  } from "./agent";
16
18
  export { CompositionMode } from "./agent";
17
19
 
@@ -45,7 +45,6 @@ export type CreateSessionData<TData = Record<string, unknown>> = Omit<
45
45
  */
46
46
  export interface CollectedStateData<TData = Record<string, unknown>> {
47
47
  data: Partial<TData>;
48
- dataByRoute: Record<string, Partial<TData>>;
49
48
  routeHistory: SessionState<TData>["routeHistory"];
50
49
  history?: SessionState<TData>["history"];
51
50
  currentRouteTitle?: string;
@@ -104,7 +104,7 @@ export interface RouteOptions<TContext = unknown, TData = unknown> {
104
104
  /** Initial terms for the route's domain glossary */
105
105
  terms?: Term<TContext>[];
106
106
  /** Tools available in this route */
107
- tools?: Tool<TContext, unknown[], unknown, TData>[];
107
+ tools?: Tool<TContext, TData, unknown[], unknown>[];
108
108
  /** Absolute rules the agent must follow in this route */
109
109
  rules?: Template<TContext, TData>[];
110
110
  /** Absolute prohibitions the agent must never do in this route */
@@ -114,14 +114,20 @@ export interface RouteOptions<TContext = unknown, TData = unknown> {
114
114
  /** Optional: structured response data for this route's message generation */
115
115
  responseOutputSchema?: StructuredSchema;
116
116
  /**
117
- * NEW: Schema defining data to extract throughout this route
118
- * This creates a type-safe contract for what data the route collects
117
+ * Required fields for route completion - must be valid keys from agent's TData type
118
+ * Route is considered complete when all required fields are present in agent data
119
119
  */
120
- schema?: StructuredSchema;
120
+ requiredFields?: (keyof TData)[];
121
121
  /**
122
- * NEW: Initial data to pre-populate when entering this route
122
+ * Optional fields that enhance the route but aren't required for completion
123
+ * Must be valid keys from agent's TData type
124
+ */
125
+ optionalFields?: (keyof TData)[];
126
+ /**
127
+ * Initial data to pre-populate when entering this route
123
128
  * Useful for restoring sessions or pre-filling known information
124
129
  * Steps with skipIf conditions will be automatically bypassed if data is present
130
+ * Now refers to agent-level data
125
131
  */
126
132
  initialData?: Partial<TData>;
127
133
  /**
@@ -191,37 +197,37 @@ export interface StepOptions<TContext = unknown, TData = unknown> {
191
197
  /** Transition to a chat state with this description */
192
198
  prompt?: Template<TContext, TData>;
193
199
  /** Tools available for AI to call in this step (by ID reference or inline definition) */
194
- tools?: (string | Tool<TContext, unknown[], unknown, TData>)[];
200
+ tools?: (string | Tool<TContext, TData, unknown[], unknown>)[];
195
201
  /** Programmatic function or tool to run before AI responds */
196
202
  prepare?:
197
203
  | string
198
- | Tool<TContext, unknown[], unknown, TData>
204
+ | Tool<TContext, TData, unknown[], unknown>
199
205
  | ((context: TContext, data?: Partial<TData>) => void | Promise<void>);
200
206
  /** Programmatic function or tool to run after AI responds */
201
207
  finalize?:
202
208
  | string
203
- | Tool<TContext, unknown[], unknown, TData>
209
+ | Tool<TContext, TData, unknown[], unknown>
204
210
  | ((context: TContext, data?: Partial<TData>) => void | Promise<void>);
205
211
  /** Transition to a specific step or end marker */
206
212
  step?: StepRef | symbol;
207
213
  /**
208
- * NEW: Fields to collect from the conversation in this step
209
- * These should match keys in the route's schema
214
+ * Fields to collect from the conversation in this step
215
+ * These should match keys in the agent's TData schema
210
216
  */
211
- collect?: string[];
217
+ collect?: (keyof TData)[];
212
218
  /**
213
- * NEW: Function to determine if this step should be skipped
219
+ * Function to determine if this step should be skipped
214
220
  * If returns true, the step will be bypassed
215
- * @param data - Currently collected data
221
+ * @param data - Currently collected agent-level data
216
222
  * @returns true if step should be skipped, false otherwise
217
223
  */
218
224
  skipIf?: (data: Partial<TData>) => boolean;
219
225
  /**
220
- * NEW: Required data fields that must be present before entering this step
226
+ * Required data fields that must be present before entering this step
221
227
  * If any required field is missing, step cannot be entered
222
- * Uses string[] for developer-friendly usage (same as collect)
228
+ * Must be valid keys from agent's TData type
223
229
  */
224
- requires?: string[];
230
+ requires?: (keyof TData)[];
225
231
  /**
226
232
  * Optional condition for this transition
227
233
  * Description of when this transition should be taken
@@ -17,8 +17,8 @@ export interface PendingTransition {
17
17
  }
18
18
 
19
19
  /**
20
- * Session step tracks the current position in the conversation flow
21
- * and data collected during the route progression
20
+ * Session state tracks the current position in the conversation flow
21
+ * and data collected at the agent level across all routes
22
22
  */
23
23
  export interface SessionState<TData = unknown> {
24
24
  /** Unique session identifier (useful for persistence) */
@@ -39,17 +39,11 @@ export interface SessionState<TData = unknown> {
39
39
  };
40
40
 
41
41
  /**
42
- * Data collected during the current route
43
- * Convenience reference to dataByRoute[currentRoute.id]
42
+ * Agent-level data collected across all routes
43
+ * This is the single source of truth for all collected data
44
+ * Routes can access and contribute to this shared data structure
44
45
  */
45
- data?: Partial<TData>;
46
-
47
- /**
48
- * Data collected organized by route ID
49
- * Persists data when switching between routes
50
- * Allows resuming incomplete routes where they left off
51
- */
52
- dataByRoute?: Record<string, Partial<TData>>;
46
+ data: Partial<TData>;
53
47
 
54
48
  /** History of routes visited in this session */
55
49
  routeHistory?: Array<{
package/src/types/tool.ts CHANGED
@@ -12,11 +12,13 @@ export interface ToolContext<TContext = unknown, TData = unknown> {
12
12
  context: TContext;
13
13
  /** Update the agent's context (triggers lifecycle hooks if configured) */
14
14
  updateContext: (updates: Partial<TContext>) => Promise<void>;
15
+ /** Update the agent's collected data (triggers lifecycle hooks if configured) */
16
+ updateData: (updates: Partial<TData>) => Promise<void>;
15
17
  /** Current step reference (if in a route) */
16
18
  step?: StepRef;
17
19
  /** Interaction history */
18
20
  history: Event[];
19
- /** Data collected so far in the current route */
21
+ /** Complete agent-level data collected so far */
20
22
  data?: Partial<TData>;
21
23
  /** Additional metadata */
22
24
  metadata?: Record<string, unknown>;
@@ -31,11 +33,15 @@ export interface ToolResult<
31
33
  TData = unknown
32
34
  > {
33
35
  /** The result data */
34
- data: TResultData;
36
+ data?: TResultData;
35
37
  /** Optional context update to be merged with current context */
36
38
  contextUpdate?: Partial<TContext>;
37
- /** Optional collected data update to be merged with session step */
39
+ /** Optional agent-level data update to be merged with collected data */
38
40
  dataUpdate?: Partial<TData>;
41
+ /** Success indicator */
42
+ success?: boolean;
43
+ /** Error message if operation failed */
44
+ error?: string;
39
45
  /** Optional metadata about the execution */
40
46
  meta?: Record<string, unknown>;
41
47
  }
@@ -45,9 +51,9 @@ export interface ToolResult<
45
51
  */
46
52
  export type ToolHandler<
47
53
  TContext,
48
- TArgs extends unknown[],
49
- TResult,
50
- TData = unknown
54
+ TData = unknown,
55
+ TArgs extends unknown[] = unknown[],
56
+ TResult = unknown
51
57
  > = (
52
58
  context: ToolContext<TContext, TData>,
53
59
  ...args: TArgs
@@ -60,16 +66,16 @@ export type ToolHandler<
60
66
  */
61
67
  export interface Tool<
62
68
  TContext = unknown,
69
+ TData = unknown,
63
70
  TArgs extends unknown[] = unknown[],
64
- TResult = unknown,
65
- TData = unknown
71
+ TResult = unknown
66
72
  > {
67
73
  /** Tool identifier */
68
74
  id: string;
69
75
  /** Tool display name (shown to AI models) */
70
76
  name?: string;
71
77
  /** Tool handler function */
72
- handler: ToolHandler<TContext, TArgs, TResult, TData>;
78
+ handler: ToolHandler<TContext, TData, TArgs, TResult>;
73
79
  /** Description of what the tool does (for AI discovery) */
74
80
  description?: string;
75
81
  /** Parameter schema or description */
@@ -1,4 +1,4 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
1
+
2
2
  /**
3
3
  * Deep clone utility to avoid adding large dependencies like lodash.
4
4
  * Handles objects, arrays, dates, and primitives.
@@ -11,21 +11,19 @@ export function cloneDeep<T>(obj: T): T {
11
11
  }
12
12
 
13
13
  if (obj instanceof Date) {
14
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
15
- return new Date(obj.getTime()) as any;
14
+ return new Date(obj.getTime()) as T;
16
15
  }
17
16
 
18
17
  if (Array.isArray(obj)) {
19
- const arrCopy = [] as any[];
18
+ const arrCopy: unknown[] = [];
20
19
  for (let i = 0; i < obj.length; i++) {
21
- // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
20
+
22
21
  arrCopy[i] = cloneDeep(obj[i]);
23
22
  }
24
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return
25
- return arrCopy as any;
23
+ return arrCopy as T;
26
24
  }
27
25
 
28
- const objCopy = {} as { [key: string]: any };
26
+ const objCopy: Record<string, unknown> = {};
29
27
  for (const key in obj) {
30
28
  if (Object.prototype.hasOwnProperty.call(obj, key)) {
31
29
  objCopy[key] = cloneDeep((obj as Record<string, unknown>)[key]);