@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.
- package/README.md +62 -59
- package/dist/adapters/MemoryAdapter.js +2 -2
- package/dist/adapters/MemoryAdapter.js.map +1 -1
- package/dist/adapters/MongoAdapter.js +2 -2
- package/dist/adapters/MongoAdapter.js.map +1 -1
- package/dist/adapters/OpenSearchAdapter.js +7 -7
- package/dist/adapters/OpenSearchAdapter.js.map +1 -1
- package/dist/adapters/PostgreSQLAdapter.js +9 -9
- package/dist/adapters/PostgreSQLAdapter.js.map +1 -1
- package/dist/adapters/PrismaAdapter.js +3 -3
- package/dist/adapters/PrismaAdapter.js.map +1 -1
- package/dist/adapters/RedisAdapter.js +2 -2
- package/dist/adapters/RedisAdapter.js.map +1 -1
- package/dist/adapters/SQLiteAdapter.d.ts +3 -3
- package/dist/adapters/SQLiteAdapter.d.ts.map +1 -1
- package/dist/adapters/SQLiteAdapter.js +11 -11
- package/dist/adapters/SQLiteAdapter.js.map +1 -1
- package/dist/adapters/index.d.ts +1 -1
- package/dist/adapters/index.d.ts.map +1 -1
- package/dist/cjs/adapters/MemoryAdapter.js +2 -2
- package/dist/cjs/adapters/MemoryAdapter.js.map +1 -1
- package/dist/cjs/adapters/MongoAdapter.js +2 -2
- package/dist/cjs/adapters/MongoAdapter.js.map +1 -1
- package/dist/cjs/adapters/OpenSearchAdapter.js +7 -7
- package/dist/cjs/adapters/OpenSearchAdapter.js.map +1 -1
- package/dist/cjs/adapters/PostgreSQLAdapter.js +9 -9
- package/dist/cjs/adapters/PostgreSQLAdapter.js.map +1 -1
- package/dist/cjs/adapters/PrismaAdapter.js +3 -3
- package/dist/cjs/adapters/PrismaAdapter.js.map +1 -1
- package/dist/cjs/adapters/RedisAdapter.js +2 -2
- package/dist/cjs/adapters/RedisAdapter.js.map +1 -1
- package/dist/cjs/adapters/SQLiteAdapter.d.ts +3 -3
- package/dist/cjs/adapters/SQLiteAdapter.d.ts.map +1 -1
- package/dist/cjs/adapters/SQLiteAdapter.js +11 -11
- package/dist/cjs/adapters/SQLiteAdapter.js.map +1 -1
- package/dist/cjs/adapters/index.d.ts +1 -1
- package/dist/cjs/adapters/index.d.ts.map +1 -1
- package/dist/cjs/constants/index.d.ts +4 -4
- package/dist/cjs/constants/index.js +5 -5
- package/dist/cjs/core/Agent.d.ts +22 -22
- package/dist/cjs/core/Agent.d.ts.map +1 -1
- package/dist/cjs/core/Agent.js +160 -152
- package/dist/cjs/core/Agent.js.map +1 -1
- package/dist/cjs/core/Events.d.ts +6 -6
- package/dist/cjs/core/Events.d.ts.map +1 -1
- package/dist/cjs/core/PersistenceManager.d.ts +13 -13
- package/dist/cjs/core/PersistenceManager.d.ts.map +1 -1
- package/dist/cjs/core/PersistenceManager.js +24 -24
- package/dist/cjs/core/PersistenceManager.js.map +1 -1
- package/dist/cjs/core/ResponseEngine.d.ts +3 -8
- package/dist/cjs/core/ResponseEngine.d.ts.map +1 -1
- package/dist/cjs/core/ResponseEngine.js +8 -8
- package/dist/cjs/core/ResponseEngine.js.map +1 -1
- package/dist/cjs/core/Route.d.ts +17 -17
- package/dist/cjs/core/Route.d.ts.map +1 -1
- package/dist/cjs/core/Route.js +33 -33
- package/dist/cjs/core/Route.js.map +1 -1
- package/dist/cjs/core/RoutingEngine.d.ts +30 -30
- package/dist/cjs/core/RoutingEngine.d.ts.map +1 -1
- package/dist/cjs/core/RoutingEngine.js +192 -192
- package/dist/cjs/core/RoutingEngine.js.map +1 -1
- package/dist/cjs/core/Step.d.ts +72 -0
- package/dist/cjs/core/Step.d.ts.map +1 -0
- package/dist/cjs/core/Step.js +150 -0
- package/dist/cjs/core/Step.js.map +1 -0
- package/dist/cjs/core/ToolExecutor.d.ts +5 -5
- package/dist/cjs/core/ToolExecutor.d.ts.map +1 -1
- package/dist/cjs/core/ToolExecutor.js +8 -8
- package/dist/cjs/core/ToolExecutor.js.map +1 -1
- package/dist/cjs/core/Transition.d.ts +14 -14
- package/dist/cjs/core/Transition.d.ts.map +1 -1
- package/dist/cjs/core/Transition.js +48 -19
- package/dist/cjs/core/Transition.js.map +1 -1
- package/dist/cjs/index.d.ts +7 -7
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js +8 -8
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/agent.d.ts +8 -8
- package/dist/cjs/types/agent.d.ts.map +1 -1
- package/dist/cjs/types/ai.d.ts +2 -2
- package/dist/cjs/types/ai.d.ts.map +1 -1
- package/dist/cjs/types/history.d.ts +3 -3
- package/dist/cjs/types/history.d.ts.map +1 -1
- package/dist/cjs/types/index.d.ts +1 -1
- package/dist/cjs/types/index.d.ts.map +1 -1
- package/dist/cjs/types/persistence.d.ts +5 -5
- package/dist/cjs/types/persistence.d.ts.map +1 -1
- package/dist/cjs/types/route.d.ts +57 -52
- package/dist/cjs/types/route.d.ts.map +1 -1
- package/dist/cjs/types/session.d.ts +27 -27
- package/dist/cjs/types/session.d.ts.map +1 -1
- package/dist/cjs/types/session.js +48 -50
- package/dist/cjs/types/session.js.map +1 -1
- package/dist/cjs/types/tool.d.ts +13 -13
- package/dist/cjs/types/tool.d.ts.map +1 -1
- package/dist/cjs/utils/id.d.ts +8 -3
- package/dist/cjs/utils/id.d.ts.map +1 -1
- package/dist/cjs/utils/id.js +16 -7
- package/dist/cjs/utils/id.js.map +1 -1
- package/dist/constants/index.d.ts +4 -4
- package/dist/constants/index.js +4 -4
- package/dist/core/Agent.d.ts +22 -22
- package/dist/core/Agent.d.ts.map +1 -1
- package/dist/core/Agent.js +162 -154
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/Events.d.ts +6 -6
- package/dist/core/Events.d.ts.map +1 -1
- package/dist/core/PersistenceManager.d.ts +13 -13
- package/dist/core/PersistenceManager.d.ts.map +1 -1
- package/dist/core/PersistenceManager.js +25 -25
- package/dist/core/PersistenceManager.js.map +1 -1
- package/dist/core/ResponseEngine.d.ts +3 -8
- package/dist/core/ResponseEngine.d.ts.map +1 -1
- package/dist/core/ResponseEngine.js +8 -8
- package/dist/core/ResponseEngine.js.map +1 -1
- package/dist/core/Route.d.ts +17 -17
- package/dist/core/Route.d.ts.map +1 -1
- package/dist/core/Route.js +33 -33
- package/dist/core/Route.js.map +1 -1
- package/dist/core/RoutingEngine.d.ts +30 -30
- package/dist/core/RoutingEngine.d.ts.map +1 -1
- package/dist/core/RoutingEngine.js +193 -193
- package/dist/core/RoutingEngine.js.map +1 -1
- package/dist/core/Step.d.ts +72 -0
- package/dist/core/Step.d.ts.map +1 -0
- package/dist/core/Step.js +146 -0
- package/dist/core/Step.js.map +1 -0
- package/dist/core/ToolExecutor.d.ts +5 -5
- package/dist/core/ToolExecutor.d.ts.map +1 -1
- package/dist/core/ToolExecutor.js +8 -8
- package/dist/core/ToolExecutor.js.map +1 -1
- package/dist/core/Transition.d.ts +14 -14
- package/dist/core/Transition.d.ts.map +1 -1
- package/dist/core/Transition.js +48 -19
- package/dist/core/Transition.js.map +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/dist/types/agent.d.ts +8 -8
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/ai.d.ts +2 -2
- package/dist/types/ai.d.ts.map +1 -1
- package/dist/types/history.d.ts +3 -3
- package/dist/types/history.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/persistence.d.ts +5 -5
- package/dist/types/persistence.d.ts.map +1 -1
- package/dist/types/route.d.ts +57 -52
- package/dist/types/route.d.ts.map +1 -1
- package/dist/types/session.d.ts +27 -27
- package/dist/types/session.d.ts.map +1 -1
- package/dist/types/session.js +44 -46
- package/dist/types/session.js.map +1 -1
- package/dist/types/tool.d.ts +13 -13
- package/dist/types/tool.d.ts.map +1 -1
- package/dist/utils/id.d.ts +8 -3
- package/dist/utils/id.d.ts.map +1 -1
- package/dist/utils/id.js +14 -6
- package/dist/utils/id.js.map +1 -1
- package/docs/ADAPTERS.md +21 -21
- package/docs/AGENT.md +57 -55
- package/docs/API_REFERENCE.md +218 -220
- package/docs/ARCHITECTURE.md +99 -104
- package/docs/CONTEXT_MANAGEMENT.md +81 -88
- package/docs/DOCS.md +18 -18
- package/docs/DOMAINS.md +16 -16
- package/docs/EXAMPLES.md +43 -43
- package/docs/GETTING_STARTED.md +60 -63
- package/docs/PERSISTENCE.md +66 -70
- package/docs/PROVIDERS.md +2 -2
- package/docs/README.md +6 -6
- package/docs/ROUTES.md +218 -220
- package/docs/STEPS.md +883 -0
- package/examples/business-onboarding.ts +84 -81
- package/examples/company-qna-agent.ts +68 -67
- package/examples/custom-database-persistence.ts +87 -89
- package/examples/declarative-agent.ts +32 -32
- package/examples/domain-scoping.ts +18 -18
- package/examples/extracted-data-modification.ts +92 -97
- package/examples/healthcare-agent.ts +89 -91
- package/examples/openai-agent.ts +29 -32
- package/examples/opensearch-persistence.ts +43 -45
- package/examples/persistent-onboarding.ts +65 -66
- package/examples/prisma-persistence.ts +108 -112
- package/examples/prisma-schema.example.prisma +3 -3
- package/examples/redis-persistence.ts +67 -73
- package/examples/route-transitions.ts +71 -47
- package/examples/rules-prohibitions.ts +28 -28
- package/examples/streaming-agent.ts +24 -24
- package/examples/travel-agent.ts +94 -109
- package/package.json +1 -1
- package/src/adapters/MemoryAdapter.ts +3 -3
- package/src/adapters/MongoAdapter.ts +3 -3
- package/src/adapters/OpenSearchAdapter.ts +8 -8
- package/src/adapters/PostgreSQLAdapter.ts +10 -10
- package/src/adapters/PrismaAdapter.ts +4 -4
- package/src/adapters/RedisAdapter.ts +3 -3
- package/src/adapters/SQLiteAdapter.ts +15 -15
- package/src/adapters/index.ts +1 -1
- package/src/constants/index.ts +4 -4
- package/src/core/Agent.ts +210 -206
- package/src/core/Events.ts +12 -12
- package/src/core/PersistenceManager.ts +32 -36
- package/src/core/ResponseEngine.ts +11 -17
- package/src/core/Route.ts +55 -49
- package/src/core/RoutingEngine.ts +244 -252
- package/src/core/Step.ts +197 -0
- package/src/core/ToolExecutor.ts +11 -11
- package/src/core/Transition.ts +72 -26
- package/src/index.ts +8 -8
- package/src/types/agent.ts +8 -8
- package/src/types/ai.ts +2 -2
- package/src/types/history.ts +3 -3
- package/src/types/index.ts +1 -1
- package/src/types/persistence.ts +6 -6
- package/src/types/route.ts +77 -61
- package/src/types/session.ts +75 -78
- package/src/types/tool.ts +17 -17
- package/src/utils/id.ts +15 -6
- package/dist/cjs/core/State.d.ts +0 -72
- package/dist/cjs/core/State.d.ts.map +0 -1
- package/dist/cjs/core/State.js +0 -148
- package/dist/cjs/core/State.js.map +0 -1
- package/dist/core/State.d.ts +0 -72
- package/dist/core/State.d.ts.map +0 -1
- package/dist/core/State.js +0 -144
- package/dist/core/State.js.map +0 -1
- package/docs/STATES.md +0 -888
- package/src/core/State.ts +0 -212
package/src/types/route.ts
CHANGED
|
@@ -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
|
|
17
|
+
* Reference to a step within a route
|
|
18
18
|
*/
|
|
19
|
-
export interface
|
|
20
|
-
/**
|
|
19
|
+
export interface StepRef {
|
|
20
|
+
/** Step identifier */
|
|
21
21
|
id: string;
|
|
22
|
-
/** Route this
|
|
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
|
-
|
|
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
|
|
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,
|
|
48
|
-
session: {
|
|
47
|
+
export type RouteCompletionHandler<TContext = unknown, TData = unknown> = (
|
|
48
|
+
session: { data?: Partial<TData> },
|
|
49
49
|
context?: TContext
|
|
50
|
-
) =>
|
|
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
|
|
58
|
+
* @template TData - Type of data collected throughout the route (inferred from schema)
|
|
55
59
|
*/
|
|
56
|
-
export interface RouteOptions<
|
|
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
|
-
|
|
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
|
-
*
|
|
89
|
+
* Steps with skipIf conditions will be automatically bypassed if data is present
|
|
86
90
|
*/
|
|
87
|
-
initialData?: Partial<
|
|
91
|
+
initialData?: Partial<TData>;
|
|
88
92
|
/**
|
|
89
93
|
* NEW: Sequential steps for simple linear flows
|
|
90
|
-
* If provided, automatically chains the steps from
|
|
91
|
-
* For complex flows with branching, build the
|
|
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<
|
|
97
|
+
steps?: TransitionSpec<TContext, TData>[];
|
|
94
98
|
/**
|
|
95
|
-
* Configure the initial
|
|
96
|
-
* Accepts full TransitionSpec configuration (id,
|
|
97
|
-
* Note:
|
|
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
|
-
|
|
100
|
-
TransitionSpec<
|
|
101
|
-
"
|
|
103
|
+
initialStep?: Omit<
|
|
104
|
+
TransitionSpec<TContext, TData>,
|
|
105
|
+
"tool" | "step" | "condition"
|
|
102
106
|
>;
|
|
103
107
|
/**
|
|
104
|
-
* Configure the end
|
|
105
|
-
* Defines what happens when the route completes (reaches
|
|
106
|
-
* Can include
|
|
107
|
-
* Note:
|
|
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
|
-
|
|
110
|
-
TransitionSpec<
|
|
111
|
-
"
|
|
113
|
+
endStep?: Omit<
|
|
114
|
+
TransitionSpec<TContext, TData>,
|
|
115
|
+
"step" | "condition" | "skipIf"
|
|
112
116
|
>;
|
|
113
117
|
/**
|
|
114
|
-
* Optional transition when route completes (reaches
|
|
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
|
-
*
|
|
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.
|
|
138
|
+
* if (session.data?.success) return "feedback";
|
|
135
139
|
* return "error-recovery";
|
|
136
140
|
* }
|
|
137
141
|
*/
|
|
138
|
-
onComplete?:
|
|
142
|
+
onComplete?:
|
|
143
|
+
| string
|
|
144
|
+
| RouteTransitionConfig
|
|
145
|
+
| RouteCompletionHandler<TContext, TData>;
|
|
139
146
|
}
|
|
140
147
|
|
|
141
148
|
/**
|
|
142
|
-
*
|
|
149
|
+
* Inline tool handler for dynamic tool generation
|
|
143
150
|
*/
|
|
144
|
-
export
|
|
145
|
-
|
|
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
|
-
|
|
164
|
+
instructions?: string;
|
|
149
165
|
/** Transition to execute a tool */
|
|
150
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
151
|
-
|
|
152
|
-
/** Transition to a specific
|
|
153
|
-
|
|
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
|
|
156
|
-
* These should match keys in the route's
|
|
171
|
+
* NEW: Fields to collect from the conversation in this step
|
|
172
|
+
* These should match keys in the route's schema
|
|
157
173
|
*/
|
|
158
|
-
|
|
174
|
+
collect?: string[];
|
|
159
175
|
/**
|
|
160
|
-
* NEW: Function to determine if this
|
|
161
|
-
* If returns true, the
|
|
162
|
-
* @param
|
|
163
|
-
* @returns true if
|
|
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?: (
|
|
181
|
+
skipIf?: (data: Partial<TData>) => boolean;
|
|
166
182
|
/**
|
|
167
|
-
* NEW: Required data fields that must be present before entering this
|
|
168
|
-
* If any required field is missing,
|
|
169
|
-
* Uses string[] for developer-friendly usage (same as
|
|
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
|
-
|
|
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
|
|
197
|
+
* Combines step reference with the ability to chain transitions
|
|
182
198
|
*/
|
|
183
|
-
export interface TransitionResult<TContext = unknown,
|
|
184
|
-
extends
|
|
199
|
+
export interface TransitionResult<TContext = unknown, TData = unknown>
|
|
200
|
+
extends StepRef {
|
|
185
201
|
/** Allow chaining transitions */
|
|
186
|
-
|
|
187
|
-
spec: TransitionSpec<TContext,
|
|
188
|
-
) => TransitionResult<TContext,
|
|
202
|
+
nextStep: (
|
|
203
|
+
spec: TransitionSpec<TContext, TData>
|
|
204
|
+
) => TransitionResult<TContext, TData>;
|
|
189
205
|
}
|
package/src/types/session.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Session
|
|
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
|
|
19
|
-
* and data
|
|
18
|
+
* Session step tracks the current position in the conversation flow
|
|
19
|
+
* and data collected during the route progression
|
|
20
20
|
*/
|
|
21
|
-
export interface
|
|
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
|
|
33
|
-
|
|
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
|
|
41
|
-
* This is a convenience reference to
|
|
40
|
+
* Data collected during the current route
|
|
41
|
+
* This is a convenience reference to dataByRoute[currentRoute.id]
|
|
42
42
|
*/
|
|
43
|
-
|
|
43
|
+
data?: Partial<TData>;
|
|
44
44
|
|
|
45
45
|
/**
|
|
46
|
-
*
|
|
46
|
+
* Collected data organized by route ID
|
|
47
47
|
* Preserves data when switching between routes
|
|
48
|
-
* Format: { "routeId": { ...
|
|
48
|
+
* Format: { "routeId": { ...dataData } }
|
|
49
49
|
*/
|
|
50
|
-
|
|
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<
|
|
79
|
+
export function createSession<TData = Record<string, unknown>>(
|
|
80
80
|
sessionId?: string,
|
|
81
|
-
metadata?:
|
|
82
|
-
):
|
|
81
|
+
metadata?: SessionStep<TData>["metadata"]
|
|
82
|
+
): SessionStep<TData> {
|
|
83
83
|
return {
|
|
84
84
|
id: sessionId,
|
|
85
|
-
|
|
86
|
-
|
|
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
|
|
98
|
+
* Preserves collected data per route in dataByRoute map
|
|
99
99
|
*/
|
|
100
|
-
export function enterRoute<
|
|
101
|
-
session:
|
|
100
|
+
export function enterRoute<TData = Record<string, unknown>>(
|
|
101
|
+
session: SessionStep<TData>,
|
|
102
102
|
routeId: string,
|
|
103
103
|
routeTitle: string
|
|
104
|
-
):
|
|
105
|
-
// Save current route's
|
|
106
|
-
const
|
|
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.
|
|
110
|
-
Object.keys(session.
|
|
109
|
+
session.data &&
|
|
110
|
+
Object.keys(session.data).length > 0
|
|
111
111
|
) {
|
|
112
|
-
|
|
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
|
|
127
|
-
const
|
|
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
|
-
|
|
139
|
-
|
|
140
|
-
|
|
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
|
|
157
|
+
* Helper to update session with new step
|
|
158
158
|
*/
|
|
159
|
-
export function
|
|
160
|
-
session:
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
):
|
|
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
|
-
|
|
167
|
-
id:
|
|
168
|
-
description:
|
|
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
|
|
180
|
-
* Updates both the
|
|
179
|
+
* Helper to merge collected data into session
|
|
180
|
+
* Updates both the data field and the dataByRoute map
|
|
181
181
|
*/
|
|
182
|
-
export function
|
|
183
|
-
session:
|
|
184
|
-
|
|
185
|
-
):
|
|
186
|
-
const
|
|
187
|
-
...session.
|
|
188
|
-
...
|
|
189
|
-
} as Partial<
|
|
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
|
|
192
|
-
const
|
|
191
|
+
// Also update the dataByRoute map for the current route
|
|
192
|
+
const dataByRoute = { ...session.dataByRoute };
|
|
193
193
|
if (session.currentRoute) {
|
|
194
|
-
|
|
194
|
+
dataByRoute[session.currentRoute.id] = newCollected;
|
|
195
195
|
}
|
|
196
196
|
|
|
197
197
|
return {
|
|
198
198
|
...session,
|
|
199
|
-
|
|
200
|
-
|
|
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
|
|
209
|
+
* Helper to convert SessionStep to persistence-friendly format
|
|
210
210
|
* Used when saving to database
|
|
211
211
|
*/
|
|
212
|
-
export function
|
|
213
|
-
session:
|
|
212
|
+
export function sessionStepToData<TData = Record<string, unknown>>(
|
|
213
|
+
session: SessionStep<TData>
|
|
214
214
|
): {
|
|
215
215
|
currentRoute?: string;
|
|
216
|
-
|
|
216
|
+
currentStep?: string;
|
|
217
217
|
collectedData: Record<string, unknown>;
|
|
218
218
|
} {
|
|
219
219
|
return {
|
|
220
220
|
currentRoute: session.currentRoute?.id,
|
|
221
|
-
|
|
221
|
+
currentStep: session.currentStep?.id,
|
|
222
222
|
collectedData: {
|
|
223
|
-
|
|
224
|
-
|
|
223
|
+
data: session.data,
|
|
224
|
+
dataByRoute: session.dataByRoute, // Include per-route data
|
|
225
225
|
routeHistory: session.routeHistory,
|
|
226
226
|
currentRouteTitle: session.currentRoute?.title,
|
|
227
|
-
|
|
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
|
|
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
|
|
239
|
+
export function sessionDataToStep<TData = Record<string, unknown>>(
|
|
240
240
|
sessionId: string,
|
|
241
241
|
data: {
|
|
242
242
|
currentRoute?: string;
|
|
243
|
-
|
|
243
|
+
currentStep?: string;
|
|
244
244
|
collectedData?: Record<string, unknown>;
|
|
245
245
|
}
|
|
246
|
-
):
|
|
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
|
-
|
|
259
|
+
currentStep: data.currentStep
|
|
260
260
|
? {
|
|
261
|
-
id: data.
|
|
261
|
+
id: data.currentStep,
|
|
262
262
|
description:
|
|
263
|
-
(collectedData.
|
|
263
|
+
(collectedData.currentStepDescription as string) || undefined,
|
|
264
264
|
enteredAt: new Date(),
|
|
265
265
|
}
|
|
266
266
|
: undefined,
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
(collectedData.
|
|
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
|
|
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,
|
|
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,
|
|
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
|
|
16
|
-
|
|
15
|
+
/** Current step reference (if in a route) */
|
|
16
|
+
step?: StepRef;
|
|
17
17
|
/** Interaction history */
|
|
18
18
|
history: Event[];
|
|
19
|
-
/** Data
|
|
20
|
-
|
|
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
|
-
|
|
29
|
+
TResultData = unknown,
|
|
30
30
|
TContext = unknown,
|
|
31
|
-
|
|
31
|
+
TData = unknown
|
|
32
32
|
> {
|
|
33
33
|
/** The result data */
|
|
34
|
-
data:
|
|
34
|
+
data: TResultData;
|
|
35
35
|
/** Optional context update to be merged with current context */
|
|
36
36
|
contextUpdate?: Partial<TContext>;
|
|
37
|
-
/** Optional
|
|
38
|
-
|
|
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
|
-
|
|
50
|
+
TData = unknown
|
|
51
51
|
> = (
|
|
52
|
-
context: ToolContext<TContext,
|
|
52
|
+
context: ToolContext<TContext, TData>,
|
|
53
53
|
...args: TArgs
|
|
54
54
|
) =>
|
|
55
|
-
| Promise<ToolResult<TResult, TContext,
|
|
56
|
-
| ToolResult<TResult, TContext,
|
|
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
|
-
|
|
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,
|
|
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
|
|
39
|
-
* Format:
|
|
38
|
+
* Generate a deterministic step ID
|
|
39
|
+
* Format: step_{sanitized_description}_{hash} or step_{routeId}_{index}
|
|
40
40
|
*/
|
|
41
|
-
export function
|
|
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 `
|
|
49
|
+
return `step_${sanitized}_${hash}`;
|
|
50
50
|
}
|
|
51
|
-
// Fallback for
|
|
51
|
+
// Fallback for steps without descriptions
|
|
52
52
|
const suffix = index !== undefined ? index : simpleHash(routeId);
|
|
53
|
-
return `
|
|
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
|
+
}
|