@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
@@ -0,0 +1,197 @@
1
+ /**
2
+ * Step in the route DSL
3
+ */
4
+
5
+ import type { StepRef, TransitionSpec, TransitionResult } from "../types/route";
6
+ import type { Guideline } from "../types/agent";
7
+
8
+ import { END_ROUTE } from "../constants";
9
+ import { Transition } from "./Transition";
10
+ import { generateStepId } from "../utils/id";
11
+
12
+ /**
13
+ * Represents a step within a route
14
+ */
15
+ export class Step<TContext = unknown, TData = unknown> {
16
+ public readonly id: string;
17
+ private transitions: Transition<TContext, TData>[] = [];
18
+ private guidelines: Guideline[] = [];
19
+ public collectFields?: string[];
20
+ public skipIf?: (data: Partial<TData>) => boolean;
21
+ public requires?: string[];
22
+ public instructions?: string;
23
+
24
+ constructor(
25
+ public readonly routeId: string,
26
+ public description?: string,
27
+ customId?: string,
28
+ collectFields?: string[],
29
+ skipIf?: (data: Partial<TData>) => boolean,
30
+ requires?: string[],
31
+ instructions?: string
32
+ ) {
33
+ // Use provided ID or generate a deterministic one
34
+ this.id = customId || generateStepId(routeId, description);
35
+ this.collectFields = collectFields;
36
+ this.skipIf = skipIf;
37
+ this.requires = requires;
38
+ this.instructions = instructions;
39
+ }
40
+
41
+ /**
42
+ * Configure the step properties after creation
43
+ * Useful for overriding initial step configuration
44
+ */
45
+ configure(config: {
46
+ description?: string;
47
+ collectFields?: string[];
48
+ skipIf?: (data: Partial<TData>) => boolean;
49
+ requires?: string[];
50
+ instructions?: string;
51
+ }): this {
52
+ if (config.description !== undefined) {
53
+ this.description = config.description;
54
+ }
55
+ if (config.collectFields !== undefined) {
56
+ this.collectFields = config.collectFields;
57
+ }
58
+ if (config.skipIf !== undefined) {
59
+ this.skipIf = config.skipIf;
60
+ }
61
+ if (config.requires !== undefined) {
62
+ this.requires = config.requires;
63
+ }
64
+ if (config.instructions !== undefined) {
65
+ this.instructions = config.instructions;
66
+ }
67
+ return this;
68
+ }
69
+
70
+ /**
71
+ * Create a transition from this step to another
72
+ *
73
+ * @param spec - Transition specification (instructions, tool, or direct step)
74
+ * @returns TransitionResult that supports chaining
75
+ */
76
+ nextStep(
77
+ spec: TransitionSpec<TContext, TData>
78
+ ): TransitionResult<TContext, TData> {
79
+ // Handle END_ROUTE
80
+ if (spec.step && typeof spec.step === "symbol" && spec.step === END_ROUTE) {
81
+ const endTransition = new Transition<TContext, TData>(this.getRef(), {
82
+ step: END_ROUTE,
83
+ condition: spec.condition,
84
+ instructions: spec.instructions,
85
+ });
86
+ this.transitions.push(endTransition);
87
+
88
+ // Return a terminal step reference
89
+ return this.createTerminalRef();
90
+ }
91
+
92
+ // Handle direct step reference
93
+ if (spec.step && typeof spec.step !== "symbol") {
94
+ const transition = new Transition<TContext, TData>(this.getRef(), spec);
95
+ this.transitions.push(transition);
96
+
97
+ return this.createStepRefWithTransition(spec.step);
98
+ }
99
+
100
+ // Create new target step for instructions or tool
101
+ const targetStep = new Step<TContext, TData>(
102
+ this.routeId,
103
+ spec.instructions,
104
+ spec.id, // Use custom ID if provided
105
+ spec.collect,
106
+ spec.skipIf,
107
+ spec.requires,
108
+ spec.instructions
109
+ );
110
+ const transition = new Transition<TContext, TData>(this.getRef(), spec);
111
+ transition.setTarget(targetStep);
112
+
113
+ this.transitions.push(transition);
114
+
115
+ return this.createStepRefWithTransition(targetStep.getRef(), targetStep);
116
+ }
117
+
118
+ /**
119
+ * Add a guideline specific to this step
120
+ */
121
+ addGuideline(guideline: Guideline): void {
122
+ this.guidelines.push(guideline);
123
+ }
124
+
125
+ /**
126
+ * Get guidelines for this step
127
+ */
128
+ getGuidelines(): Guideline[] {
129
+ return [...this.guidelines];
130
+ }
131
+
132
+ /**
133
+ * Get all transitions from this step
134
+ */
135
+ getTransitions(): Transition<TContext, TData>[] {
136
+ return [...this.transitions];
137
+ }
138
+
139
+ /**
140
+ * Check if this step should be skipped based on collected data
141
+ */
142
+ shouldSkip(data: Partial<TData>): boolean {
143
+ if (!this.skipIf) return false;
144
+ return this.skipIf(data);
145
+ }
146
+
147
+ /**
148
+ * Check if this step has all required data to proceed
149
+ */
150
+ hasRequires(data: Partial<TData>): boolean {
151
+ if (!this.requires || this.requires.length === 0) return true;
152
+ return this.requires.every((key) => data[key as keyof TData] !== undefined);
153
+ }
154
+
155
+ /**
156
+ * Get step reference
157
+ */
158
+ getRef(): StepRef {
159
+ return {
160
+ id: this.id,
161
+ routeId: this.routeId,
162
+ };
163
+ }
164
+
165
+ /**
166
+ * Create a step reference with nextStep capability for chaining
167
+ */
168
+ private createStepRefWithTransition(
169
+ ref: StepRef,
170
+ step?: Step<TContext, TData>
171
+ ): TransitionResult<TContext, TData> {
172
+ const stepInstance = step || this;
173
+
174
+ return {
175
+ ...ref,
176
+ nextStep: (spec: TransitionSpec<TContext, TData>) =>
177
+ stepInstance.nextStep(spec),
178
+ };
179
+ }
180
+
181
+ /**
182
+ * Create a terminal step reference (for END_ROUTE)
183
+ */
184
+ private createTerminalRef(): TransitionResult<TContext, TData> {
185
+ const terminalRef: StepRef = {
186
+ id: "END",
187
+ routeId: this.routeId,
188
+ };
189
+
190
+ return {
191
+ ...terminalRef,
192
+ nextStep: () => {
193
+ throw new Error("Cannot transition from END_ROUTE step");
194
+ },
195
+ };
196
+ }
197
+ }
@@ -13,13 +13,13 @@ export interface ToolExecutionResult {
13
13
  success: boolean;
14
14
  data?: unknown;
15
15
  contextUpdate?: Record<string, unknown>;
16
- extractedUpdate?: Record<string, unknown>;
16
+ collectedUpdate?: Record<string, unknown>;
17
17
  error?: string;
18
18
  }
19
19
 
20
- export class ToolExecutor<TContext = unknown, TExtracted = unknown> {
20
+ export class ToolExecutor<TContext = unknown, TData = unknown> {
21
21
  /**
22
- * Execute a single tool with context and extracted data
22
+ * Execute a single tool with context and collected data
23
23
  * @param allowedDomains - Array of domain names allowed for this execution context (undefined = all domains allowed)
24
24
  */
25
25
  async executeTool(
@@ -27,7 +27,7 @@ export class ToolExecutor<TContext = unknown, TExtracted = unknown> {
27
27
  context: TContext,
28
28
  updateContext: (updates: Partial<TContext>) => Promise<void>,
29
29
  history: Event[],
30
- extracted?: Partial<TExtracted>,
30
+ data?: Partial<TData>,
31
31
  allowedDomains?: string[]
32
32
  ): Promise<ToolExecutionResult> {
33
33
  try {
@@ -47,15 +47,15 @@ export class ToolExecutor<TContext = unknown, TExtracted = unknown> {
47
47
  }
48
48
  }
49
49
 
50
- // Build tool context with extracted data
51
- const toolContext: ToolContext<TContext, TExtracted> = {
50
+ // Build tool context with collected data
51
+ const toolContext: ToolContext<TContext, TData> = {
52
52
  context,
53
53
  updateContext,
54
54
  history,
55
- extracted,
55
+ data,
56
56
  };
57
57
 
58
- // Execute tool (no arguments - tools read from context/extracted)
58
+ // Execute tool (no arguments - tools read from context/data)
59
59
  const result = await tool.handler(toolContext);
60
60
 
61
61
  // Return execution result
@@ -64,7 +64,7 @@ export class ToolExecutor<TContext = unknown, TExtracted = unknown> {
64
64
  success: true,
65
65
  data: result.data,
66
66
  contextUpdate: result.contextUpdate,
67
- extractedUpdate: result.extractedUpdate,
67
+ collectedUpdate: result.collectedUpdate,
68
68
  };
69
69
  } catch (error) {
70
70
  return {
@@ -84,7 +84,7 @@ export class ToolExecutor<TContext = unknown, TExtracted = unknown> {
84
84
  context: TContext,
85
85
  updateContext: (updates: Partial<TContext>) => Promise<void>,
86
86
  history: Event[],
87
- extracted?: Partial<TExtracted>,
87
+ data?: Partial<TData>,
88
88
  allowedDomains?: string[]
89
89
  ): Promise<ToolExecutionResult[]> {
90
90
  const results: ToolExecutionResult[] = [];
@@ -95,7 +95,7 @@ export class ToolExecutor<TContext = unknown, TExtracted = unknown> {
95
95
  context,
96
96
  updateContext,
97
97
  history,
98
- extracted,
98
+ data,
99
99
  allowedDomains
100
100
  );
101
101
  results.push(result);
@@ -1,44 +1,90 @@
1
1
  /**
2
- * Transition between states in the route DSL
2
+ * Transition between steps in the route DSL
3
3
  */
4
4
 
5
- import type { StateRef, TransitionSpec } from "../types/route";
6
- import type { State } from "./State";
5
+ import type {
6
+ StepRef,
7
+ TransitionSpec,
8
+ InlineToolHandler,
9
+ } from "../types/route";
10
+ import type { Step } from "./Step";
11
+ import type { ToolRef, ToolResult, ToolContext } from "../types/tool";
12
+ import { generateInlineToolId } from "../utils/id";
7
13
 
8
14
  /**
9
- * Represents a transition from one state to another
15
+ * Represents a transition from one step to another
10
16
  */
11
- export class Transition<TContext = unknown, TExtracted = unknown> {
12
- private target?: State<TContext, TExtracted>;
17
+ export class Transition<TContext = unknown, TData = unknown> {
18
+ private target?: Step<TContext, TData>;
13
19
  public readonly condition?: string;
14
20
 
15
21
  constructor(
16
- public readonly source: StateRef,
17
- public readonly spec: TransitionSpec<TContext, TExtracted>
22
+ public readonly source: StepRef,
23
+ public readonly spec: TransitionSpec<TContext, TData>
18
24
  ) {
19
25
  // Extract condition from spec for convenience
20
26
  this.condition = spec.condition;
21
- }
22
27
 
28
+ // Normalize tool if present
29
+ if (spec.tool) {
30
+ this.spec = {
31
+ ...spec,
32
+ tool: this.normalizeTool(spec.tool, source.id),
33
+ };
34
+ }
35
+ }
23
36
  /**
24
- * Set the target state for this transition
37
+ * Normalize tool - convert inline handler to ToolRef if needed
25
38
  */
26
- setTarget(state: State<TContext, TExtracted>): void {
27
- this.target = state;
39
+ private normalizeTool<TContext, TData>(
40
+ tool: // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ ToolRef<TContext, any[], any, TData> | InlineToolHandler<TContext, TData>,
42
+ stepId: string
43
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
44
+ ): ToolRef<TContext, any[], any, TData> {
45
+ // If it's already a ToolRef (has id and name properties), return as-is
46
+ if (typeof tool === "object" && "id" in tool && "name" in tool) {
47
+ return tool;
48
+ }
49
+
50
+ // Otherwise, it's an inline handler function - wrap it in a ToolRef
51
+ const inlineHandler = tool;
52
+ const toolId = generateInlineToolId(stepId);
53
+
54
+ return {
55
+ id: toolId,
56
+ name: toolId,
57
+
58
+ handler: async (
59
+ context: ToolContext<TContext, TData>
60
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
61
+ ): Promise<ToolResult<any, TContext, TData>> => {
62
+ const result = (await inlineHandler(context)) as ToolResult<
63
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
64
+ any,
65
+ TContext,
66
+ TData
67
+ >;
68
+ return {
69
+ data: result,
70
+ };
71
+ },
72
+ description: `Inline tool for step ${stepId}`,
73
+ };
28
74
  }
29
75
 
30
76
  /**
31
- * Get the target state
77
+ * Set the target step for this transition
32
78
  */
33
- getTarget(): State<TContext, TExtracted> | undefined {
34
- return this.target;
79
+ setTarget(step: Step<TContext, TData>): void {
80
+ this.target = step;
35
81
  }
36
82
 
37
83
  /**
38
- * Check if this transition has a condition
84
+ * Get the target step
39
85
  */
40
- hasCondition(): boolean {
41
- return !!this.condition;
86
+ getTarget(): Step<TContext, TData> | undefined {
87
+ return this.target;
42
88
  }
43
89
 
44
90
  /**
@@ -47,17 +93,17 @@ export class Transition<TContext = unknown, TExtracted = unknown> {
47
93
  describe(): string {
48
94
  const parts: string[] = [];
49
95
 
50
- if (this.spec.chatState) {
51
- parts.push(`chat: "${this.spec.chatState}"`);
96
+ if (this.spec.instructions) {
97
+ parts.push(`chat: "${this.spec.instructions}"`);
52
98
  }
53
- if (this.spec.toolState) {
54
- parts.push(`tool: ${this.spec.toolState.name}`);
99
+ if (this.spec.tool) {
100
+ parts.push(`tool: ${this.spec.tool.name}`);
55
101
  }
56
- if (this.spec.state) {
57
- if (typeof this.spec.state === "symbol") {
58
- parts.push("state: END_STATE");
102
+ if (this.spec.step) {
103
+ if (typeof this.spec.step === "symbol") {
104
+ parts.push("step: END_ROUTE");
59
105
  } else {
60
- parts.push(`state: ${this.spec.state.id}`);
106
+ parts.push(`step: ${this.spec.step.id}`);
61
107
  }
62
108
  }
63
109
 
package/src/index.ts CHANGED
@@ -7,7 +7,7 @@
7
7
  // Core
8
8
  export { Agent } from "./core/Agent";
9
9
  export { Route } from "./core/Route";
10
- export { State } from "./core/State";
10
+ export { Step } from "./core/Step";
11
11
  export { Transition } from "./core/Transition";
12
12
  export { defineTool } from "./core/Tool";
13
13
  export { DomainRegistry } from "./core/DomainRegistry";
@@ -51,7 +51,7 @@ export type {
51
51
  export { SQLiteAdapter } from "./adapters/SQLiteAdapter";
52
52
  export type {
53
53
  SqliteDatabase,
54
- SqliteStatement,
54
+ SqliteStepment,
55
55
  SQLiteAdapterOptions,
56
56
  } from "./adapters/SQLiteAdapter";
57
57
  export { MemoryAdapter } from "./adapters/MemoryAdapter";
@@ -62,10 +62,10 @@ export type {
62
62
  } from "./adapters/OpenSearchAdapter";
63
63
 
64
64
  // Constants
65
- export { END_STATE, END_STATE_ID } from "./constants";
65
+ export { END_ROUTE, END_ROUTE_ID } from "./constants";
66
66
 
67
67
  // Utils
68
- export { generateRouteId, generateStateId, generateToolId } from "./utils/id";
68
+ export { generateRouteId, generateStepId, generateToolId } from "./utils/id";
69
69
 
70
70
  // Types
71
71
  export type {
@@ -91,7 +91,7 @@ export { EventKind, EventSource } from "./types/history";
91
91
 
92
92
  export type {
93
93
  RouteRef,
94
- StateRef,
94
+ StepRef,
95
95
  RouteOptions,
96
96
  TransitionSpec,
97
97
  TransitionResult,
@@ -99,12 +99,12 @@ export type {
99
99
  RouteCompletionHandler,
100
100
  } from "./types/route";
101
101
 
102
- export type { SessionState, PendingTransition } from "./types/session";
102
+ export type { SessionStep, PendingTransition } from "./types/session";
103
103
  export {
104
104
  createSession,
105
105
  enterRoute,
106
- enterState,
107
- mergeExtracted,
106
+ enterStep,
107
+ mergeCollected,
108
108
  } from "./types/session";
109
109
 
110
110
  export type {
@@ -6,7 +6,7 @@ import type { AiProvider } from "./ai";
6
6
  import type { ToolRef } from "./tool";
7
7
  import type { RouteOptions } from "./route";
8
8
  import type { PersistenceConfig } from "./persistence";
9
- import type { SessionState } from "./session";
9
+ import type { SessionStep } from "./session";
10
10
 
11
11
  /**
12
12
  * Composition mode determines how the agent processes and structures responses
@@ -23,7 +23,7 @@ export enum CompositionMode {
23
23
  }
24
24
 
25
25
  /**
26
- * Context lifecycle hooks for managing state persistence
26
+ * Context lifecycle hooks for managing step persistence
27
27
  */
28
28
  export interface ContextLifecycleHooks<TContext = unknown> {
29
29
  /**
@@ -42,17 +42,17 @@ export interface ContextLifecycleHooks<TContext = unknown> {
42
42
  ) => Promise<void> | void;
43
43
 
44
44
  /**
45
- * Called after extracted data is updated (from AI response or tool execution)
46
- * Useful for validation, enrichment, or persistence of gathered data
47
- * Return modified extracted data or the same data to keep it unchanged
45
+ * Called after collected data is updated (from AI response or tool execution)
46
+ * Useful for validation, enrichment, or persistence of collected data
47
+ * Return modified collected data or the same data to keep it unchanged
48
48
  *
49
- * Note: This hook works with ANY route's extracted data (since an agent can have
49
+ * Note: This hook works with ANY route's collected data (since an agent can have
50
50
  * multiple routes with different extraction schemas). Use type guards or runtime
51
51
  * checks if you need type-specific logic.
52
52
  */
53
53
 
54
54
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
55
- onExtractedUpdate?: (extracted: any, previousExtracted: any) => any;
55
+ onDataUpdate?: (data: any, previousCollected: any) => any;
56
56
  }
57
57
 
58
58
  /**
@@ -80,7 +80,7 @@ export interface AgentOptions<TContext = unknown> {
80
80
  /** Default context data available to the agent */
81
81
  context?: TContext;
82
82
  /** Optional current session for convenience methods */
83
- session?: SessionState;
83
+ session?: SessionStep;
84
84
  /** Context provider function for always-fresh context (alternative to static context) */
85
85
  contextProvider?: ContextProvider<TContext>;
86
86
  /** Lifecycle hooks for context management */
package/src/types/ai.ts CHANGED
@@ -64,8 +64,8 @@ export interface AgentStructuredResponse {
64
64
  message: string;
65
65
  /** Route chosen by the agent (route title or null if no route) */
66
66
  route?: string | null;
67
- /** Current state within the route (state description or null) */
68
- state?: string | null;
67
+ /** Current step within the route (step description or null) */
68
+ step?: string | null;
69
69
  /** Tool calls the agent wants to execute */
70
70
  toolCalls?: Array<{
71
71
  /** Name of the tool to call */
@@ -58,9 +58,9 @@ export interface MessageEventData {
58
58
  session?: {
59
59
  routeId?: string;
60
60
  routeTitle?: string;
61
- stateId?: string;
62
- stateDescription?: string;
63
- extracted?: Record<string, unknown>;
61
+ stepId?: string;
62
+ stepDescription?: string;
63
+ data?: Record<string, unknown>;
64
64
  };
65
65
  }
66
66
 
@@ -28,7 +28,7 @@ export { EventKind, EventSource } from "./history";
28
28
  // Route types
29
29
  export type {
30
30
  RouteRef,
31
- StateRef,
31
+ StepRef,
32
32
  RouteOptions,
33
33
  TransitionSpec,
34
34
  TransitionResult,
@@ -24,7 +24,7 @@ export interface SessionData {
24
24
  agentName?: string;
25
25
  status: SessionStatus;
26
26
  currentRoute?: string;
27
- currentState?: string;
27
+ currentStep?: string;
28
28
  collectedData?: Record<string, unknown>;
29
29
  messageCount?: number;
30
30
  lastMessageAt?: Date;
@@ -43,7 +43,7 @@ export interface MessageData {
43
43
  role: MessageRole;
44
44
  content: string;
45
45
  route?: string;
46
- state?: string;
46
+ step?: string;
47
47
  toolCalls?: Array<{ toolName: string; arguments: Record<string, unknown> }>;
48
48
  event?: Event; // Optional: store full event data
49
49
  createdAt: Date;
@@ -102,12 +102,12 @@ export interface SessionRepository {
102
102
  ): Promise<SessionData | null>;
103
103
 
104
104
  /**
105
- * Update current route and state
105
+ * Update current route and step
106
106
  */
107
- updateRouteState(
107
+ updateRouteStep(
108
108
  id: string,
109
109
  route?: string,
110
- state?: string
110
+ step?: string
111
111
  ): Promise<SessionData | null>;
112
112
 
113
113
  /**
@@ -228,7 +228,7 @@ export interface SaveMessageOptions {
228
228
  role: MessageRole;
229
229
  content: string;
230
230
  route?: string;
231
- state?: string;
231
+ step?: string;
232
232
  toolCalls?: Array<{ toolName: string; arguments: Record<string, unknown> }>;
233
233
  event?: Event;
234
234
  }