@falai/agent 0.6.8 → 0.7.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 (231) hide show
  1. package/README.md +62 -59
  2. package/dist/adapters/MemoryAdapter.js +2 -2
  3. package/dist/adapters/MemoryAdapter.js.map +1 -1
  4. package/dist/adapters/MongoAdapter.js +2 -2
  5. package/dist/adapters/MongoAdapter.js.map +1 -1
  6. package/dist/adapters/OpenSearchAdapter.js +7 -7
  7. package/dist/adapters/OpenSearchAdapter.js.map +1 -1
  8. package/dist/adapters/PostgreSQLAdapter.js +9 -9
  9. package/dist/adapters/PostgreSQLAdapter.js.map +1 -1
  10. package/dist/adapters/PrismaAdapter.js +3 -3
  11. package/dist/adapters/PrismaAdapter.js.map +1 -1
  12. package/dist/adapters/RedisAdapter.js +2 -2
  13. package/dist/adapters/RedisAdapter.js.map +1 -1
  14. package/dist/adapters/SQLiteAdapter.d.ts +3 -3
  15. package/dist/adapters/SQLiteAdapter.d.ts.map +1 -1
  16. package/dist/adapters/SQLiteAdapter.js +11 -11
  17. package/dist/adapters/SQLiteAdapter.js.map +1 -1
  18. package/dist/adapters/index.d.ts +1 -1
  19. package/dist/adapters/index.d.ts.map +1 -1
  20. package/dist/cjs/adapters/MemoryAdapter.js +2 -2
  21. package/dist/cjs/adapters/MemoryAdapter.js.map +1 -1
  22. package/dist/cjs/adapters/MongoAdapter.js +2 -2
  23. package/dist/cjs/adapters/MongoAdapter.js.map +1 -1
  24. package/dist/cjs/adapters/OpenSearchAdapter.js +7 -7
  25. package/dist/cjs/adapters/OpenSearchAdapter.js.map +1 -1
  26. package/dist/cjs/adapters/PostgreSQLAdapter.js +9 -9
  27. package/dist/cjs/adapters/PostgreSQLAdapter.js.map +1 -1
  28. package/dist/cjs/adapters/PrismaAdapter.js +3 -3
  29. package/dist/cjs/adapters/PrismaAdapter.js.map +1 -1
  30. package/dist/cjs/adapters/RedisAdapter.js +2 -2
  31. package/dist/cjs/adapters/RedisAdapter.js.map +1 -1
  32. package/dist/cjs/adapters/SQLiteAdapter.d.ts +3 -3
  33. package/dist/cjs/adapters/SQLiteAdapter.d.ts.map +1 -1
  34. package/dist/cjs/adapters/SQLiteAdapter.js +11 -11
  35. package/dist/cjs/adapters/SQLiteAdapter.js.map +1 -1
  36. package/dist/cjs/adapters/index.d.ts +1 -1
  37. package/dist/cjs/adapters/index.d.ts.map +1 -1
  38. package/dist/cjs/constants/index.d.ts +4 -4
  39. package/dist/cjs/constants/index.js +5 -5
  40. package/dist/cjs/core/Agent.d.ts +22 -22
  41. package/dist/cjs/core/Agent.d.ts.map +1 -1
  42. package/dist/cjs/core/Agent.js +160 -152
  43. package/dist/cjs/core/Agent.js.map +1 -1
  44. package/dist/cjs/core/Events.d.ts +6 -6
  45. package/dist/cjs/core/Events.d.ts.map +1 -1
  46. package/dist/cjs/core/PersistenceManager.d.ts +13 -13
  47. package/dist/cjs/core/PersistenceManager.d.ts.map +1 -1
  48. package/dist/cjs/core/PersistenceManager.js +24 -24
  49. package/dist/cjs/core/PersistenceManager.js.map +1 -1
  50. package/dist/cjs/core/ResponseEngine.d.ts +3 -8
  51. package/dist/cjs/core/ResponseEngine.d.ts.map +1 -1
  52. package/dist/cjs/core/ResponseEngine.js +8 -8
  53. package/dist/cjs/core/ResponseEngine.js.map +1 -1
  54. package/dist/cjs/core/Route.d.ts +17 -17
  55. package/dist/cjs/core/Route.d.ts.map +1 -1
  56. package/dist/cjs/core/Route.js +33 -33
  57. package/dist/cjs/core/Route.js.map +1 -1
  58. package/dist/cjs/core/RoutingEngine.d.ts +30 -30
  59. package/dist/cjs/core/RoutingEngine.d.ts.map +1 -1
  60. package/dist/cjs/core/RoutingEngine.js +192 -192
  61. package/dist/cjs/core/RoutingEngine.js.map +1 -1
  62. package/dist/cjs/core/Step.d.ts +72 -0
  63. package/dist/cjs/core/Step.d.ts.map +1 -0
  64. package/dist/cjs/core/Step.js +150 -0
  65. package/dist/cjs/core/Step.js.map +1 -0
  66. package/dist/cjs/core/ToolExecutor.d.ts +5 -5
  67. package/dist/cjs/core/ToolExecutor.d.ts.map +1 -1
  68. package/dist/cjs/core/ToolExecutor.js +8 -8
  69. package/dist/cjs/core/ToolExecutor.js.map +1 -1
  70. package/dist/cjs/core/Transition.d.ts +14 -14
  71. package/dist/cjs/core/Transition.d.ts.map +1 -1
  72. package/dist/cjs/core/Transition.js +48 -19
  73. package/dist/cjs/core/Transition.js.map +1 -1
  74. package/dist/cjs/index.d.ts +7 -7
  75. package/dist/cjs/index.d.ts.map +1 -1
  76. package/dist/cjs/index.js +8 -8
  77. package/dist/cjs/index.js.map +1 -1
  78. package/dist/cjs/types/agent.d.ts +8 -8
  79. package/dist/cjs/types/agent.d.ts.map +1 -1
  80. package/dist/cjs/types/ai.d.ts +2 -2
  81. package/dist/cjs/types/ai.d.ts.map +1 -1
  82. package/dist/cjs/types/history.d.ts +3 -3
  83. package/dist/cjs/types/history.d.ts.map +1 -1
  84. package/dist/cjs/types/index.d.ts +1 -1
  85. package/dist/cjs/types/index.d.ts.map +1 -1
  86. package/dist/cjs/types/persistence.d.ts +5 -5
  87. package/dist/cjs/types/persistence.d.ts.map +1 -1
  88. package/dist/cjs/types/route.d.ts +57 -52
  89. package/dist/cjs/types/route.d.ts.map +1 -1
  90. package/dist/cjs/types/session.d.ts +27 -27
  91. package/dist/cjs/types/session.d.ts.map +1 -1
  92. package/dist/cjs/types/session.js +48 -50
  93. package/dist/cjs/types/session.js.map +1 -1
  94. package/dist/cjs/types/tool.d.ts +13 -13
  95. package/dist/cjs/types/tool.d.ts.map +1 -1
  96. package/dist/cjs/utils/id.d.ts +8 -3
  97. package/dist/cjs/utils/id.d.ts.map +1 -1
  98. package/dist/cjs/utils/id.js +16 -7
  99. package/dist/cjs/utils/id.js.map +1 -1
  100. package/dist/constants/index.d.ts +4 -4
  101. package/dist/constants/index.js +4 -4
  102. package/dist/core/Agent.d.ts +22 -22
  103. package/dist/core/Agent.d.ts.map +1 -1
  104. package/dist/core/Agent.js +162 -154
  105. package/dist/core/Agent.js.map +1 -1
  106. package/dist/core/Events.d.ts +6 -6
  107. package/dist/core/Events.d.ts.map +1 -1
  108. package/dist/core/PersistenceManager.d.ts +13 -13
  109. package/dist/core/PersistenceManager.d.ts.map +1 -1
  110. package/dist/core/PersistenceManager.js +25 -25
  111. package/dist/core/PersistenceManager.js.map +1 -1
  112. package/dist/core/ResponseEngine.d.ts +3 -8
  113. package/dist/core/ResponseEngine.d.ts.map +1 -1
  114. package/dist/core/ResponseEngine.js +8 -8
  115. package/dist/core/ResponseEngine.js.map +1 -1
  116. package/dist/core/Route.d.ts +17 -17
  117. package/dist/core/Route.d.ts.map +1 -1
  118. package/dist/core/Route.js +33 -33
  119. package/dist/core/Route.js.map +1 -1
  120. package/dist/core/RoutingEngine.d.ts +30 -30
  121. package/dist/core/RoutingEngine.d.ts.map +1 -1
  122. package/dist/core/RoutingEngine.js +193 -193
  123. package/dist/core/RoutingEngine.js.map +1 -1
  124. package/dist/core/Step.d.ts +72 -0
  125. package/dist/core/Step.d.ts.map +1 -0
  126. package/dist/core/Step.js +146 -0
  127. package/dist/core/Step.js.map +1 -0
  128. package/dist/core/ToolExecutor.d.ts +5 -5
  129. package/dist/core/ToolExecutor.d.ts.map +1 -1
  130. package/dist/core/ToolExecutor.js +8 -8
  131. package/dist/core/ToolExecutor.js.map +1 -1
  132. package/dist/core/Transition.d.ts +14 -14
  133. package/dist/core/Transition.d.ts.map +1 -1
  134. package/dist/core/Transition.js +48 -19
  135. package/dist/core/Transition.js.map +1 -1
  136. package/dist/index.d.ts +7 -7
  137. package/dist/index.d.ts.map +1 -1
  138. package/dist/index.js +4 -4
  139. package/dist/index.js.map +1 -1
  140. package/dist/types/agent.d.ts +8 -8
  141. package/dist/types/agent.d.ts.map +1 -1
  142. package/dist/types/ai.d.ts +2 -2
  143. package/dist/types/ai.d.ts.map +1 -1
  144. package/dist/types/history.d.ts +3 -3
  145. package/dist/types/history.d.ts.map +1 -1
  146. package/dist/types/index.d.ts +1 -1
  147. package/dist/types/index.d.ts.map +1 -1
  148. package/dist/types/persistence.d.ts +5 -5
  149. package/dist/types/persistence.d.ts.map +1 -1
  150. package/dist/types/route.d.ts +57 -52
  151. package/dist/types/route.d.ts.map +1 -1
  152. package/dist/types/session.d.ts +27 -27
  153. package/dist/types/session.d.ts.map +1 -1
  154. package/dist/types/session.js +44 -46
  155. package/dist/types/session.js.map +1 -1
  156. package/dist/types/tool.d.ts +13 -13
  157. package/dist/types/tool.d.ts.map +1 -1
  158. package/dist/utils/id.d.ts +8 -3
  159. package/dist/utils/id.d.ts.map +1 -1
  160. package/dist/utils/id.js +14 -6
  161. package/dist/utils/id.js.map +1 -1
  162. package/docs/ADAPTERS.md +21 -21
  163. package/docs/AGENT.md +57 -55
  164. package/docs/API_REFERENCE.md +218 -220
  165. package/docs/ARCHITECTURE.md +99 -104
  166. package/docs/CONTEXT_MANAGEMENT.md +81 -88
  167. package/docs/DOCS.md +18 -18
  168. package/docs/DOMAINS.md +16 -16
  169. package/docs/EXAMPLES.md +43 -43
  170. package/docs/GETTING_STARTED.md +60 -63
  171. package/docs/PERSISTENCE.md +66 -70
  172. package/docs/PROVIDERS.md +2 -2
  173. package/docs/README.md +6 -6
  174. package/docs/ROUTES.md +218 -220
  175. package/docs/STEPS.md +883 -0
  176. package/examples/business-onboarding.ts +84 -81
  177. package/examples/company-qna-agent.ts +68 -67
  178. package/examples/custom-database-persistence.ts +87 -89
  179. package/examples/declarative-agent.ts +32 -32
  180. package/examples/domain-scoping.ts +18 -18
  181. package/examples/extracted-data-modification.ts +92 -97
  182. package/examples/healthcare-agent.ts +89 -91
  183. package/examples/openai-agent.ts +29 -32
  184. package/examples/opensearch-persistence.ts +43 -45
  185. package/examples/persistent-onboarding.ts +65 -66
  186. package/examples/prisma-persistence.ts +108 -112
  187. package/examples/prisma-schema.example.prisma +3 -3
  188. package/examples/redis-persistence.ts +67 -73
  189. package/examples/route-transitions.ts +71 -47
  190. package/examples/rules-prohibitions.ts +28 -28
  191. package/examples/streaming-agent.ts +24 -24
  192. package/examples/travel-agent.ts +94 -109
  193. package/package.json +1 -1
  194. package/src/adapters/MemoryAdapter.ts +3 -3
  195. package/src/adapters/MongoAdapter.ts +3 -3
  196. package/src/adapters/OpenSearchAdapter.ts +8 -8
  197. package/src/adapters/PostgreSQLAdapter.ts +10 -10
  198. package/src/adapters/PrismaAdapter.ts +4 -4
  199. package/src/adapters/RedisAdapter.ts +3 -3
  200. package/src/adapters/SQLiteAdapter.ts +15 -15
  201. package/src/adapters/index.ts +1 -1
  202. package/src/constants/index.ts +4 -4
  203. package/src/core/Agent.ts +210 -206
  204. package/src/core/Events.ts +12 -12
  205. package/src/core/PersistenceManager.ts +32 -36
  206. package/src/core/ResponseEngine.ts +11 -17
  207. package/src/core/Route.ts +55 -49
  208. package/src/core/RoutingEngine.ts +244 -252
  209. package/src/core/Step.ts +197 -0
  210. package/src/core/ToolExecutor.ts +11 -11
  211. package/src/core/Transition.ts +72 -26
  212. package/src/index.ts +8 -8
  213. package/src/types/agent.ts +8 -8
  214. package/src/types/ai.ts +2 -2
  215. package/src/types/history.ts +3 -3
  216. package/src/types/index.ts +1 -1
  217. package/src/types/persistence.ts +6 -6
  218. package/src/types/route.ts +77 -61
  219. package/src/types/session.ts +75 -78
  220. package/src/types/tool.ts +17 -17
  221. package/src/utils/id.ts +15 -6
  222. package/dist/cjs/core/State.d.ts +0 -72
  223. package/dist/cjs/core/State.d.ts.map +0 -1
  224. package/dist/cjs/core/State.js +0 -148
  225. package/dist/cjs/core/State.js.map +0 -1
  226. package/dist/core/State.d.ts +0 -72
  227. package/dist/core/State.d.ts.map +0 -1
  228. package/dist/core/State.js +0 -144
  229. package/dist/core/State.js.map +0 -1
  230. package/docs/STATES.md +0 -888
  231. package/src/core/State.ts +0 -212
@@ -2,7 +2,7 @@
2
2
  * Route/Journey DSL type definitions
3
3
  */
4
4
 
5
- import type { ToolRef } from "./tool";
5
+ import type { ToolRef, ToolResult } from "./tool";
6
6
  import type { StructuredSchema } from "./schema";
7
7
 
8
8
  /**
@@ -14,12 +14,12 @@ export interface RouteRef {
14
14
  }
15
15
 
16
16
  /**
17
- * Reference to a state within a route
17
+ * Reference to a step within a route
18
18
  */
19
- export interface StateRef {
20
- /** State identifier */
19
+ export interface StepRef {
20
+ /** Step identifier */
21
21
  id: string;
22
- /** Route this state belongs to */
22
+ /** Route this step belongs to */
23
23
  routeId: string;
24
24
  }
25
25
 
@@ -33,27 +33,31 @@ import type { Guideline } from "./agent";
33
33
  */
34
34
  export interface RouteTransitionConfig {
35
35
  /** Target route ID or title to transition to */
36
- transitionTo: string;
36
+ nextStep: string;
37
37
  /** Optional AI-evaluated condition for the transition */
38
38
  condition?: string;
39
39
  }
40
40
 
41
41
  /**
42
42
  * Function type for dynamic route completion transitions
43
- * @param session - Current session state with extracted data
43
+ * @param session - Current session step with collected data
44
44
  * @param context - Agent context
45
45
  * @returns Route ID/title to transition to, or transition config, or undefined to end
46
46
  */
47
- export type RouteCompletionHandler<TContext = unknown, TExtracted = unknown> = (
48
- session: { extracted?: Partial<TExtracted> },
47
+ export type RouteCompletionHandler<TContext = unknown, TData = unknown> = (
48
+ session: { data?: Partial<TData> },
49
49
  context?: TContext
50
- ) => string | RouteTransitionConfig | undefined | Promise<string | RouteTransitionConfig | undefined>;
50
+ ) =>
51
+ | string
52
+ | RouteTransitionConfig
53
+ | undefined
54
+ | Promise<string | RouteTransitionConfig | undefined>;
51
55
 
52
56
  /**
53
57
  * Options for creating a route
54
- * @template TExtracted - Type of data extracted throughout the route (inferred from extractionSchema)
58
+ * @template TData - Type of data collected throughout the route (inferred from schema)
55
59
  */
56
- export interface RouteOptions<TExtracted = unknown> {
60
+ export interface RouteOptions<TContext = unknown, TData = unknown> {
57
61
  /** Custom ID for the route (optional - will generate deterministic ID from title if not provided) */
58
62
  id?: string;
59
63
  /** Title of the route */
@@ -78,40 +82,40 @@ export interface RouteOptions<TExtracted = unknown> {
78
82
  * NEW: Schema defining data to extract throughout this route
79
83
  * This creates a type-safe contract for what data the route collects
80
84
  */
81
- extractionSchema?: StructuredSchema;
85
+ schema?: StructuredSchema;
82
86
  /**
83
87
  * NEW: Initial data to pre-populate when entering this route
84
88
  * Useful for restoring sessions or pre-filling known information
85
- * States with skipIf conditions will be automatically bypassed if data is present
89
+ * Steps with skipIf conditions will be automatically bypassed if data is present
86
90
  */
87
- initialData?: Partial<TExtracted>;
91
+ initialData?: Partial<TData>;
88
92
  /**
89
93
  * NEW: Sequential steps for simple linear flows
90
- * If provided, automatically chains the steps from initialState to END_STATE
91
- * For complex flows with branching, build the state machine manually instead
94
+ * If provided, automatically chains the steps from initialStep to END_ROUTE
95
+ * For complex flows with branching, build the step machine manually instead
92
96
  */
93
- steps?: TransitionSpec<unknown, TExtracted>[];
97
+ steps?: TransitionSpec<TContext, TData>[];
94
98
  /**
95
- * Configure the initial state (optional)
96
- * Accepts full TransitionSpec configuration (id, chatState, gather, skipIf, etc.)
97
- * Note: toolState and state properties are ignored for initial state
99
+ * Configure the initial step (optional)
100
+ * Accepts full TransitionSpec configuration (id, instructions, collect, skipIf, etc.)
101
+ * Note: tool and step properties are ignored for initial step
98
102
  */
99
- initialState?: Omit<
100
- TransitionSpec<unknown, TExtracted>,
101
- "toolState" | "state" | "condition"
103
+ initialStep?: Omit<
104
+ TransitionSpec<TContext, TData>,
105
+ "tool" | "step" | "condition"
102
106
  >;
103
107
  /**
104
- * Configure the end state (optional)
105
- * Defines what happens when the route completes (reaches END_STATE)
106
- * Can include chatState for completion message, toolState for final actions, etc.
107
- * Note: state, condition, skipIf properties are ignored for end state
108
+ * Configure the end step (optional)
109
+ * Defines what happens when the route completes (reaches END_ROUTE)
110
+ * Can include instructions for completion message, tool for final actions, etc.
111
+ * Note: step, condition, skipIf properties are ignored for end step
108
112
  */
109
- endState?: Omit<
110
- TransitionSpec<unknown, TExtracted>,
111
- "state" | "condition" | "skipIf"
113
+ endStep?: Omit<
114
+ TransitionSpec<TContext, TData>,
115
+ "step" | "condition" | "skipIf"
112
116
  >;
113
117
  /**
114
- * Optional transition when route completes (reaches END_STATE)
118
+ * Optional transition when route completes (reaches END_ROUTE)
115
119
  * Can be:
116
120
  * - String: Route ID or title to transition to
117
121
  * - Object: Transition config with optional AI-evaluated condition
@@ -124,51 +128,63 @@ export interface RouteOptions<TExtracted = unknown> {
124
128
  * @example
125
129
  * // With condition
126
130
  * onComplete: {
127
- * transitionTo: "feedback-collection",
131
+ * nextStep: "feedback-collection",
128
132
  * condition: "if booking succeeded"
129
133
  * }
130
134
  *
131
135
  * @example
132
136
  * // Dynamic function
133
137
  * onComplete: (session) => {
134
- * if (session.extracted?.success) return "feedback";
138
+ * if (session.data?.success) return "feedback";
135
139
  * return "error-recovery";
136
140
  * }
137
141
  */
138
- onComplete?: string | RouteTransitionConfig | RouteCompletionHandler<unknown, TExtracted>;
142
+ onComplete?:
143
+ | string
144
+ | RouteTransitionConfig
145
+ | RouteCompletionHandler<TContext, TData>;
139
146
  }
140
147
 
141
148
  /**
142
- * Specification for a state transition
149
+ * Inline tool handler for dynamic tool generation
143
150
  */
144
- export interface TransitionSpec<TContext = unknown, TExtracted = unknown> {
145
- /** Custom ID for this state (optional - will generate deterministic ID if not provided) */
151
+ export type InlineToolHandler<TContext = unknown, TData = unknown> = (
152
+ context: import("./tool").ToolContext<TContext, TData>
153
+ ) =>
154
+ | ToolResult<unknown, TContext, TData>
155
+ | Promise<ToolResult<unknown, TContext, TData>>;
156
+
157
+ /**
158
+ * Specification for a step transition
159
+ */
160
+ export interface TransitionSpec<TContext = unknown, TData = unknown> {
161
+ /** Custom ID for this step (optional - will generate deterministic ID if not provided) */
146
162
  id?: string;
147
163
  /** Transition to a chat state with this description */
148
- chatState?: string;
164
+ instructions?: string;
149
165
  /** Transition to execute a tool */
150
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
151
- toolState?: ToolRef<TContext, any[], any>;
152
- /** Transition to a specific state or end marker */
153
- state?: StateRef | symbol;
166
+ tool?: // eslint-disable-next-line @typescript-eslint/no-explicit-any
167
+ ToolRef<TContext, any[], any, TData> | InlineToolHandler<TContext, TData>;
168
+ /** Transition to a specific step or end marker */
169
+ step?: StepRef | symbol;
154
170
  /**
155
- * NEW: Fields to gather from the conversation in this state
156
- * These should match keys in the route's extractionSchema
171
+ * NEW: Fields to collect from the conversation in this step
172
+ * These should match keys in the route's schema
157
173
  */
158
- gather?: string[];
174
+ collect?: string[];
159
175
  /**
160
- * NEW: Function to determine if this state should be skipped
161
- * If returns true, the state will be bypassed
162
- * @param extracted - Currently extracted data
163
- * @returns true if state should be skipped, false otherwise
176
+ * NEW: Function to determine if this step should be skipped
177
+ * If returns true, the step will be bypassed
178
+ * @param data - Currently collected data
179
+ * @returns true if step should be skipped, false otherwise
164
180
  */
165
- skipIf?: (extracted: Partial<TExtracted>) => boolean;
181
+ skipIf?: (data: Partial<TData>) => boolean;
166
182
  /**
167
- * NEW: Required data fields that must be present before entering this state
168
- * If any required field is missing, state cannot be entered
169
- * Uses string[] for developer-friendly usage (same as gather)
183
+ * NEW: Required data fields that must be present before entering this step
184
+ * If any required field is missing, step cannot be entered
185
+ * Uses string[] for developer-friendly usage (same as collect)
170
186
  */
171
- requiredData?: string[];
187
+ requires?: string[];
172
188
  /**
173
189
  * Optional condition for this transition
174
190
  * Description of when this transition should be taken
@@ -178,12 +194,12 @@ export interface TransitionSpec<TContext = unknown, TExtracted = unknown> {
178
194
 
179
195
  /**
180
196
  * Result of a transition operation
181
- * Combines state reference with the ability to chain transitions
197
+ * Combines step reference with the ability to chain transitions
182
198
  */
183
- export interface TransitionResult<TContext = unknown, TExtracted = unknown>
184
- extends StateRef {
199
+ export interface TransitionResult<TContext = unknown, TData = unknown>
200
+ extends StepRef {
185
201
  /** Allow chaining transitions */
186
- transitionTo: (
187
- spec: TransitionSpec<TContext, TExtracted>
188
- ) => TransitionResult<TContext, TExtracted>;
202
+ nextStep: (
203
+ spec: TransitionSpec<TContext, TData>
204
+ ) => TransitionResult<TContext, TData>;
189
205
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Session state types for tracking conversation progress
2
+ * Session step types for tracking conversation progress
3
3
  */
4
4
 
5
5
  /**
@@ -15,10 +15,10 @@ export interface PendingTransition {
15
15
  }
16
16
 
17
17
  /**
18
- * Session state tracks the current position in the conversation flow
19
- * and data extracted during the route progression
18
+ * Session step tracks the current position in the conversation flow
19
+ * and data collected during the route progression
20
20
  */
21
- export interface SessionState<TExtracted = Record<string, unknown>> {
21
+ export interface SessionStep<TData = Record<string, unknown>> {
22
22
  /** Unique session identifier (useful for persistence) */
23
23
  id?: string;
24
24
 
@@ -29,25 +29,25 @@ export interface SessionState<TExtracted = Record<string, unknown>> {
29
29
  enteredAt: Date;
30
30
  };
31
31
 
32
- /** Current state within the route */
33
- currentState?: {
32
+ /** Current step within the route */
33
+ currentStep?: {
34
34
  id: string;
35
35
  description?: string;
36
36
  enteredAt: Date;
37
37
  };
38
38
 
39
39
  /**
40
- * Data extracted during the current route
41
- * This is a convenience reference to extractedByRoute[currentRoute.id]
40
+ * Data collected during the current route
41
+ * This is a convenience reference to dataByRoute[currentRoute.id]
42
42
  */
43
- extracted?: Partial<TExtracted>;
43
+ data?: Partial<TData>;
44
44
 
45
45
  /**
46
- * Extracted data organized by route ID
46
+ * Collected data organized by route ID
47
47
  * Preserves data when switching between routes
48
- * Format: { "routeId": { ...extractedData } }
48
+ * Format: { "routeId": { ...dataData } }
49
49
  */
50
- extractedByRoute?: Record<string, Partial<unknown>>;
50
+ dataByRoute?: Record<string, Partial<unknown>>;
51
51
 
52
52
  /** History of routes visited in this session */
53
53
  routeHistory: Array<{
@@ -76,14 +76,14 @@ export interface SessionState<TExtracted = Record<string, unknown>> {
76
76
  * @param sessionId - Optional session ID (e.g., from database)
77
77
  * @param metadata - Optional metadata to attach
78
78
  */
79
- export function createSession<TExtracted = Record<string, unknown>>(
79
+ export function createSession<TData = Record<string, unknown>>(
80
80
  sessionId?: string,
81
- metadata?: SessionState<TExtracted>["metadata"]
82
- ): SessionState<TExtracted> {
81
+ metadata?: SessionStep<TData>["metadata"]
82
+ ): SessionStep<TData> {
83
83
  return {
84
84
  id: sessionId,
85
- extracted: {},
86
- extractedByRoute: {},
85
+ data: {},
86
+ dataByRoute: {},
87
87
  routeHistory: [],
88
88
  metadata: {
89
89
  ...metadata,
@@ -95,21 +95,21 @@ export function createSession<TExtracted = Record<string, unknown>>(
95
95
 
96
96
  /**
97
97
  * Helper to update session with new route
98
- * Preserves extracted data per route in extractedByRoute map
98
+ * Preserves collected data per route in dataByRoute map
99
99
  */
100
- export function enterRoute<TExtracted = Record<string, unknown>>(
101
- session: SessionState<TExtracted>,
100
+ export function enterRoute<TData = Record<string, unknown>>(
101
+ session: SessionStep<TData>,
102
102
  routeId: string,
103
103
  routeTitle: string
104
- ): SessionState<TExtracted> {
105
- // Save current route's extracted data before switching
106
- const extractedByRoute = { ...session.extractedByRoute };
104
+ ): SessionStep<TData> {
105
+ // Save current route's collected data before switching
106
+ const dataByRoute = { ...session.dataByRoute };
107
107
  if (
108
108
  session.currentRoute &&
109
- session.extracted &&
110
- Object.keys(session.extracted).length > 0
109
+ session.data &&
110
+ Object.keys(session.data).length > 0
111
111
  ) {
112
- extractedByRoute[session.currentRoute.id] = session.extracted;
112
+ dataByRoute[session.currentRoute.id] = session.data;
113
113
  }
114
114
 
115
115
  // Exit current route if exists
@@ -123,8 +123,8 @@ export function enterRoute<TExtracted = Record<string, unknown>>(
123
123
  }
124
124
  }
125
125
 
126
- // Load extracted data for new route (if resuming) or start fresh
127
- const newExtracted = (extractedByRoute[routeId] as Partial<TExtracted>) || {};
126
+ // Load collected data for new route (if resuming) or start fresh
127
+ const newCollected = (dataByRoute[routeId] as Partial<TData>) || {};
128
128
 
129
129
  // Enter new route
130
130
  const now = new Date();
@@ -135,9 +135,9 @@ export function enterRoute<TExtracted = Record<string, unknown>>(
135
135
  title: routeTitle,
136
136
  enteredAt: now,
137
137
  },
138
- currentState: undefined,
139
- extracted: newExtracted, // Load route's data or start fresh
140
- extractedByRoute,
138
+ currentStep: undefined,
139
+ data: newCollected, // Load route's data or start fresh
140
+ dataByRoute,
141
141
  routeHistory: [
142
142
  ...routeHistory,
143
143
  {
@@ -154,18 +154,18 @@ export function enterRoute<TExtracted = Record<string, unknown>>(
154
154
  }
155
155
 
156
156
  /**
157
- * Helper to update session with new state
157
+ * Helper to update session with new step
158
158
  */
159
- export function enterState<TExtracted = Record<string, unknown>>(
160
- session: SessionState<TExtracted>,
161
- stateId: string,
162
- stateDescription?: string
163
- ): SessionState<TExtracted> {
159
+ export function enterStep<TData = Record<string, unknown>>(
160
+ session: SessionStep<TData>,
161
+ stepId: string,
162
+ stepDescription?: string
163
+ ): SessionStep<TData> {
164
164
  return {
165
165
  ...session,
166
- currentState: {
167
- id: stateId,
168
- description: stateDescription,
166
+ currentStep: {
167
+ id: stepId,
168
+ description: stepDescription,
169
169
  enteredAt: new Date(),
170
170
  },
171
171
  metadata: {
@@ -176,28 +176,28 @@ export function enterState<TExtracted = Record<string, unknown>>(
176
176
  }
177
177
 
178
178
  /**
179
- * Helper to merge extracted data into session
180
- * Updates both the extracted field and the extractedByRoute map
179
+ * Helper to merge collected data into session
180
+ * Updates both the data field and the dataByRoute map
181
181
  */
182
- export function mergeExtracted<TExtracted = Record<string, unknown>>(
183
- session: SessionState<TExtracted>,
184
- extracted: Partial<unknown>
185
- ): SessionState<TExtracted> {
186
- const newExtracted = {
187
- ...session.extracted,
188
- ...extracted,
189
- } as Partial<TExtracted>;
182
+ export function mergeCollected<TData = Record<string, unknown>>(
183
+ session: SessionStep<TData>,
184
+ data: Partial<unknown>
185
+ ): SessionStep<TData> {
186
+ const newCollected = {
187
+ ...session.data,
188
+ ...data,
189
+ } as Partial<TData>;
190
190
 
191
- // Also update the extractedByRoute map for the current route
192
- const extractedByRoute = { ...session.extractedByRoute };
191
+ // Also update the dataByRoute map for the current route
192
+ const dataByRoute = { ...session.dataByRoute };
193
193
  if (session.currentRoute) {
194
- extractedByRoute[session.currentRoute.id] = newExtracted;
194
+ dataByRoute[session.currentRoute.id] = newCollected;
195
195
  }
196
196
 
197
197
  return {
198
198
  ...session,
199
- extracted: newExtracted,
200
- extractedByRoute,
199
+ data: newCollected,
200
+ dataByRoute,
201
201
  metadata: {
202
202
  ...session.metadata,
203
203
  lastUpdatedAt: new Date(),
@@ -206,44 +206,44 @@ export function mergeExtracted<TExtracted = Record<string, unknown>>(
206
206
  }
207
207
 
208
208
  /**
209
- * Helper to convert SessionState to persistence-friendly format
209
+ * Helper to convert SessionStep to persistence-friendly format
210
210
  * Used when saving to database
211
211
  */
212
- export function sessionStateToData<TExtracted = Record<string, unknown>>(
213
- session: SessionState<TExtracted>
212
+ export function sessionStepToData<TData = Record<string, unknown>>(
213
+ session: SessionStep<TData>
214
214
  ): {
215
215
  currentRoute?: string;
216
- currentState?: string;
216
+ currentStep?: string;
217
217
  collectedData: Record<string, unknown>;
218
218
  } {
219
219
  return {
220
220
  currentRoute: session.currentRoute?.id,
221
- currentState: session.currentState?.id,
221
+ currentStep: session.currentStep?.id,
222
222
  collectedData: {
223
- extracted: session.extracted,
224
- extractedByRoute: session.extractedByRoute, // Include per-route data
223
+ data: session.data,
224
+ dataByRoute: session.dataByRoute, // Include per-route data
225
225
  routeHistory: session.routeHistory,
226
226
  currentRouteTitle: session.currentRoute?.title,
227
- currentStateDescription: session.currentState?.description,
227
+ currentStepDescription: session.currentStep?.description,
228
228
  metadata: session.metadata,
229
229
  },
230
230
  };
231
231
  }
232
232
 
233
233
  /**
234
- * Helper to convert database SessionData back to SessionState
234
+ * Helper to convert database SessionData back to SessionStep
235
235
  * Used when loading from database
236
236
  * @param sessionId - The database session ID
237
237
  * @param data - The database session data
238
238
  */
239
- export function sessionDataToState<TExtracted = Record<string, unknown>>(
239
+ export function sessionDataToStep<TData = Record<string, unknown>>(
240
240
  sessionId: string,
241
241
  data: {
242
242
  currentRoute?: string;
243
- currentState?: string;
243
+ currentStep?: string;
244
244
  collectedData?: Record<string, unknown>;
245
245
  }
246
- ): SessionState<TExtracted> {
246
+ ): SessionStep<TData> {
247
247
  const collectedData = data.collectedData || {};
248
248
 
249
249
  return {
@@ -256,22 +256,19 @@ export function sessionDataToState<TExtracted = Record<string, unknown>>(
256
256
  enteredAt: new Date(),
257
257
  }
258
258
  : undefined,
259
- currentState: data.currentState
259
+ currentStep: data.currentStep
260
260
  ? {
261
- id: data.currentState,
261
+ id: data.currentStep,
262
262
  description:
263
- (collectedData.currentStateDescription as string) || undefined,
263
+ (collectedData.currentStepDescription as string) || undefined,
264
264
  enteredAt: new Date(),
265
265
  }
266
266
  : undefined,
267
- extracted: (collectedData.extracted as Partial<TExtracted>) || {},
268
- extractedByRoute:
269
- (collectedData.extractedByRoute as Record<string, Partial<unknown>>) ||
270
- {}, // Restore per-route data
267
+ data: (collectedData.data as Partial<TData>) || {},
268
+ dataByRoute:
269
+ (collectedData.dataByRoute as Record<string, Partial<unknown>>) || {}, // Restore per-route data
271
270
  routeHistory:
272
- (collectedData.routeHistory as SessionState<TExtracted>["routeHistory"]) ||
273
- [],
274
- metadata:
275
- (collectedData.metadata as SessionState<TExtracted>["metadata"]) || {},
271
+ (collectedData.routeHistory as SessionStep<TData>["routeHistory"]) || [],
272
+ metadata: (collectedData.metadata as SessionStep<TData>["metadata"]) || {},
276
273
  };
277
274
  }
package/src/types/tool.ts CHANGED
@@ -2,22 +2,22 @@
2
2
  * Tool system type definitions
3
3
  */
4
4
 
5
- import type { Event, StateRef } from "./index";
5
+ import type { Event, StepRef } from "./index";
6
6
 
7
7
  /**
8
8
  * Context provided to tool handlers
9
9
  */
10
- export interface ToolContext<TContext = unknown, TExtracted = unknown> {
10
+ export interface ToolContext<TContext = unknown, TData = unknown> {
11
11
  /** The agent's context data */
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
- /** Current state reference (if in a route) */
16
- state?: StateRef;
15
+ /** Current step reference (if in a route) */
16
+ step?: StepRef;
17
17
  /** Interaction history */
18
18
  history: Event[];
19
- /** Data extracted so far in the current route */
20
- extracted?: Partial<TExtracted>;
19
+ /** Data collected so far in the current route */
20
+ data?: Partial<TData>;
21
21
  /** Additional metadata */
22
22
  metadata?: Record<string, unknown>;
23
23
  }
@@ -26,16 +26,16 @@ export interface ToolContext<TContext = unknown, TExtracted = unknown> {
26
26
  * Result returned by a tool
27
27
  */
28
28
  export interface ToolResult<
29
- TData = unknown,
29
+ TResultData = unknown,
30
30
  TContext = unknown,
31
- TExtracted = unknown
31
+ TData = unknown
32
32
  > {
33
33
  /** The result data */
34
- data: TData;
34
+ data: TResultData;
35
35
  /** Optional context update to be merged with current context */
36
36
  contextUpdate?: Partial<TContext>;
37
- /** Optional extracted data update to be merged with session state */
38
- extractedUpdate?: Partial<TExtracted>;
37
+ /** Optional collected data update to be merged with session step */
38
+ collectedUpdate?: Partial<TData>;
39
39
  /** Optional metadata about the execution */
40
40
  meta?: Record<string, unknown>;
41
41
  }
@@ -47,13 +47,13 @@ export type ToolHandler<
47
47
  TContext,
48
48
  TArgs extends unknown[],
49
49
  TResult,
50
- TExtracted = unknown
50
+ TData = unknown
51
51
  > = (
52
- context: ToolContext<TContext, TExtracted>,
52
+ context: ToolContext<TContext, TData>,
53
53
  ...args: TArgs
54
54
  ) =>
55
- | Promise<ToolResult<TResult, TContext, TExtracted>>
56
- | ToolResult<TResult, TContext, TExtracted>;
55
+ | Promise<ToolResult<TResult, TContext, TData>>
56
+ | ToolResult<TResult, TContext, TData>;
57
57
 
58
58
  /**
59
59
  * Reference to a defined tool
@@ -62,14 +62,14 @@ export interface ToolRef<
62
62
  TContext,
63
63
  TArgs extends unknown[],
64
64
  TResult,
65
- TExtracted = unknown
65
+ TData = unknown
66
66
  > {
67
67
  /** Tool identifier */
68
68
  id: string;
69
69
  /** Tool name */
70
70
  name: string;
71
71
  /** Tool handler function */
72
- handler: ToolHandler<TContext, TArgs, TResult, TExtracted>;
72
+ handler: ToolHandler<TContext, TArgs, TResult, TData>;
73
73
  /** Description of what the tool does */
74
74
  description?: string;
75
75
  /** Parameter schema or description */
package/src/utils/id.ts CHANGED
@@ -35,10 +35,10 @@ export function generateRouteId(title: string): string {
35
35
  }
36
36
 
37
37
  /**
38
- * Generate a deterministic state ID
39
- * Format: state_{sanitized_description}_{hash} or state_{routeId}_{index}
38
+ * Generate a deterministic step ID
39
+ * Format: step_{sanitized_description}_{hash} or step_{routeId}_{index}
40
40
  */
41
- export function generateStateId(
41
+ export function generateStepId(
42
42
  routeId: string,
43
43
  description?: string,
44
44
  index?: number
@@ -46,11 +46,11 @@ export function generateStateId(
46
46
  if (description) {
47
47
  const sanitized = sanitize(description);
48
48
  const hash = simpleHash(`${routeId}_${description}`);
49
- return `state_${sanitized}_${hash}`;
49
+ return `step_${sanitized}_${hash}`;
50
50
  }
51
- // Fallback for states without descriptions
51
+ // Fallback for steps without descriptions
52
52
  const suffix = index !== undefined ? index : simpleHash(routeId);
53
- return `state_${routeId}_${suffix}`;
53
+ return `step_${routeId}_${suffix}`;
54
54
  }
55
55
 
56
56
  /**
@@ -62,3 +62,12 @@ export function generateToolId(name: string): string {
62
62
  const hash = simpleHash(name);
63
63
  return `tool_${sanitized}_${hash}`;
64
64
  }
65
+
66
+ /**
67
+ * Generate a deterministic tool ID for inline tool handlers
68
+ * Format: tool_inline_{stepId}_{hash}
69
+ */
70
+ export function generateInlineToolId(stepId: string): string {
71
+ const hash = simpleHash(`${stepId}_inline_tool`);
72
+ return `tool_inline_${stepId}_${hash}`;
73
+ }