@falai/agent 0.6.9 → 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/core/Step.ts
ADDED
|
@@ -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
|
+
}
|
package/src/core/ToolExecutor.ts
CHANGED
|
@@ -13,13 +13,13 @@ export interface ToolExecutionResult {
|
|
|
13
13
|
success: boolean;
|
|
14
14
|
data?: unknown;
|
|
15
15
|
contextUpdate?: Record<string, unknown>;
|
|
16
|
-
|
|
16
|
+
collectedUpdate?: Record<string, unknown>;
|
|
17
17
|
error?: string;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
export class ToolExecutor<TContext = unknown,
|
|
20
|
+
export class ToolExecutor<TContext = unknown, TData = unknown> {
|
|
21
21
|
/**
|
|
22
|
-
* Execute a single tool with context and
|
|
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
|
-
|
|
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
|
|
51
|
-
const toolContext: ToolContext<TContext,
|
|
50
|
+
// Build tool context with collected data
|
|
51
|
+
const toolContext: ToolContext<TContext, TData> = {
|
|
52
52
|
context,
|
|
53
53
|
updateContext,
|
|
54
54
|
history,
|
|
55
|
-
|
|
55
|
+
data,
|
|
56
56
|
};
|
|
57
57
|
|
|
58
|
-
// Execute tool (no arguments - tools read from context/
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
98
|
+
data,
|
|
99
99
|
allowedDomains
|
|
100
100
|
);
|
|
101
101
|
results.push(result);
|
package/src/core/Transition.ts
CHANGED
|
@@ -1,44 +1,90 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Transition between
|
|
2
|
+
* Transition between steps in the route DSL
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import type {
|
|
6
|
-
|
|
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
|
|
15
|
+
* Represents a transition from one step to another
|
|
10
16
|
*/
|
|
11
|
-
export class Transition<TContext = unknown,
|
|
12
|
-
private target?:
|
|
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:
|
|
17
|
-
public readonly spec: TransitionSpec<TContext,
|
|
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
|
-
*
|
|
37
|
+
* Normalize tool - convert inline handler to ToolRef if needed
|
|
25
38
|
*/
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
*
|
|
77
|
+
* Set the target step for this transition
|
|
32
78
|
*/
|
|
33
|
-
|
|
34
|
-
|
|
79
|
+
setTarget(step: Step<TContext, TData>): void {
|
|
80
|
+
this.target = step;
|
|
35
81
|
}
|
|
36
82
|
|
|
37
83
|
/**
|
|
38
|
-
*
|
|
84
|
+
* Get the target step
|
|
39
85
|
*/
|
|
40
|
-
|
|
41
|
-
return
|
|
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.
|
|
51
|
-
parts.push(`chat: "${this.spec.
|
|
96
|
+
if (this.spec.instructions) {
|
|
97
|
+
parts.push(`chat: "${this.spec.instructions}"`);
|
|
52
98
|
}
|
|
53
|
-
if (this.spec.
|
|
54
|
-
parts.push(`tool: ${this.spec.
|
|
99
|
+
if (this.spec.tool) {
|
|
100
|
+
parts.push(`tool: ${this.spec.tool.name}`);
|
|
55
101
|
}
|
|
56
|
-
if (this.spec.
|
|
57
|
-
if (typeof this.spec.
|
|
58
|
-
parts.push("
|
|
102
|
+
if (this.spec.step) {
|
|
103
|
+
if (typeof this.spec.step === "symbol") {
|
|
104
|
+
parts.push("step: END_ROUTE");
|
|
59
105
|
} else {
|
|
60
|
-
parts.push(`
|
|
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 {
|
|
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
|
-
|
|
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 {
|
|
65
|
+
export { END_ROUTE, END_ROUTE_ID } from "./constants";
|
|
66
66
|
|
|
67
67
|
// Utils
|
|
68
|
-
export { generateRouteId,
|
|
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
|
-
|
|
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 {
|
|
102
|
+
export type { SessionStep, PendingTransition } from "./types/session";
|
|
103
103
|
export {
|
|
104
104
|
createSession,
|
|
105
105
|
enterRoute,
|
|
106
|
-
|
|
107
|
-
|
|
106
|
+
enterStep,
|
|
107
|
+
mergeCollected,
|
|
108
108
|
} from "./types/session";
|
|
109
109
|
|
|
110
110
|
export type {
|
package/src/types/agent.ts
CHANGED
|
@@ -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 {
|
|
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
|
|
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
|
|
46
|
-
* Useful for validation, enrichment, or persistence of
|
|
47
|
-
* Return modified
|
|
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
|
|
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
|
-
|
|
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?:
|
|
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
|
|
68
|
-
|
|
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 */
|
package/src/types/history.ts
CHANGED
|
@@ -58,9 +58,9 @@ export interface MessageEventData {
|
|
|
58
58
|
session?: {
|
|
59
59
|
routeId?: string;
|
|
60
60
|
routeTitle?: string;
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
stepId?: string;
|
|
62
|
+
stepDescription?: string;
|
|
63
|
+
data?: Record<string, unknown>;
|
|
64
64
|
};
|
|
65
65
|
}
|
|
66
66
|
|
package/src/types/index.ts
CHANGED
package/src/types/persistence.ts
CHANGED
|
@@ -24,7 +24,7 @@ export interface SessionData {
|
|
|
24
24
|
agentName?: string;
|
|
25
25
|
status: SessionStatus;
|
|
26
26
|
currentRoute?: string;
|
|
27
|
-
|
|
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
|
-
|
|
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
|
|
105
|
+
* Update current route and step
|
|
106
106
|
*/
|
|
107
|
-
|
|
107
|
+
updateRouteStep(
|
|
108
108
|
id: string,
|
|
109
109
|
route?: string,
|
|
110
|
-
|
|
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
|
-
|
|
231
|
+
step?: string;
|
|
232
232
|
toolCalls?: Array<{ toolName: string; arguments: Record<string, unknown> }>;
|
|
233
233
|
event?: Event;
|
|
234
234
|
}
|