@falai/agent 0.5.4 → 0.6.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 +9 -4
- package/dist/cjs/core/Agent.d.ts +0 -5
- package/dist/cjs/core/Agent.d.ts.map +1 -1
- package/dist/cjs/core/Agent.js +75 -157
- package/dist/cjs/core/Agent.js.map +1 -1
- package/dist/cjs/core/ResponseEngine.js +2 -2
- package/dist/cjs/core/ResponseEngine.js.map +1 -1
- package/dist/cjs/core/Route.d.ts +6 -1
- package/dist/cjs/core/Route.d.ts.map +1 -1
- package/dist/cjs/core/Route.js +19 -1
- package/dist/cjs/core/Route.js.map +1 -1
- package/dist/cjs/core/RoutingEngine.d.ts +68 -2
- package/dist/cjs/core/RoutingEngine.d.ts.map +1 -1
- package/dist/cjs/core/RoutingEngine.js +416 -2
- package/dist/cjs/core/RoutingEngine.js.map +1 -1
- package/dist/cjs/core/State.d.ts +1 -2
- package/dist/cjs/core/State.d.ts.map +1 -1
- package/dist/cjs/core/State.js +5 -6
- package/dist/cjs/core/State.js.map +1 -1
- package/dist/cjs/core/Transition.d.ts +2 -2
- package/dist/cjs/core/Transition.d.ts.map +1 -1
- package/dist/cjs/core/Transition.js +3 -2
- package/dist/cjs/core/Transition.js.map +1 -1
- package/dist/cjs/types/route.d.ts +15 -4
- package/dist/cjs/types/route.d.ts.map +1 -1
- package/dist/cjs/utils/event.d.ts +6 -0
- package/dist/cjs/utils/event.d.ts.map +1 -0
- package/dist/cjs/utils/event.js +20 -0
- package/dist/cjs/utils/event.js.map +1 -0
- package/dist/core/Agent.d.ts +0 -5
- package/dist/core/Agent.d.ts.map +1 -1
- package/dist/core/Agent.js +74 -156
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/ResponseEngine.js +2 -2
- package/dist/core/ResponseEngine.js.map +1 -1
- package/dist/core/Route.d.ts +6 -1
- package/dist/core/Route.d.ts.map +1 -1
- package/dist/core/Route.js +19 -1
- package/dist/core/Route.js.map +1 -1
- package/dist/core/RoutingEngine.d.ts +68 -2
- package/dist/core/RoutingEngine.d.ts.map +1 -1
- package/dist/core/RoutingEngine.js +416 -2
- package/dist/core/RoutingEngine.js.map +1 -1
- package/dist/core/State.d.ts +1 -2
- package/dist/core/State.d.ts.map +1 -1
- package/dist/core/State.js +5 -6
- package/dist/core/State.js.map +1 -1
- package/dist/core/Transition.d.ts +2 -2
- package/dist/core/Transition.d.ts.map +1 -1
- package/dist/core/Transition.js +3 -2
- package/dist/core/Transition.js.map +1 -1
- package/dist/types/route.d.ts +15 -4
- package/dist/types/route.d.ts.map +1 -1
- package/dist/utils/event.d.ts +6 -0
- package/dist/utils/event.d.ts.map +1 -0
- package/dist/utils/event.js +17 -0
- package/dist/utils/event.js.map +1 -0
- package/docs/ADAPTERS.md +1 -1
- package/docs/API_REFERENCE.md +15 -7
- package/docs/ARCHITECTURE.md +25 -5
- package/docs/CONSTRUCTOR_OPTIONS.md +2 -2
- package/docs/CONTEXT_MANAGEMENT.md +1 -1
- package/docs/GETTING_STARTED.md +1 -1
- package/docs/PERSISTENCE.md +3 -3
- package/examples/business-onboarding.ts +97 -70
- package/examples/company-qna-agent.ts +4 -4
- package/examples/custom-database-persistence.ts +2 -2
- package/examples/declarative-agent.ts +3 -3
- package/examples/extracted-data-modification.ts +1 -1
- package/examples/healthcare-agent.ts +9 -3
- package/examples/openai-agent.ts +1 -1
- package/examples/opensearch-persistence.ts +2 -2
- package/examples/persistent-onboarding.ts +18 -12
- package/examples/prisma-persistence.ts +3 -3
- package/examples/redis-persistence.ts +3 -3
- package/examples/travel-agent.ts +23 -4
- package/package.json +1 -1
- package/src/core/Agent.ts +78 -227
- package/src/core/ResponseEngine.ts +2 -2
- package/src/core/Route.ts +34 -3
- package/src/core/RoutingEngine.ts +663 -2
- package/src/core/State.ts +6 -13
- package/src/core/Transition.ts +6 -3
- package/src/types/route.ts +15 -5
- package/src/utils/event.ts +16 -0
package/src/core/State.ts
CHANGED
|
@@ -43,12 +43,10 @@ export class State<TContext = unknown, TExtracted = unknown> {
|
|
|
43
43
|
* Create a transition from this state to another
|
|
44
44
|
*
|
|
45
45
|
* @param spec - Transition specification (chatState, toolState, or direct state)
|
|
46
|
-
* @param condition - Optional condition for this transition
|
|
47
46
|
* @returns TransitionResult that supports chaining
|
|
48
47
|
*/
|
|
49
48
|
transitionTo(
|
|
50
|
-
spec: TransitionSpec<TContext, TExtracted
|
|
51
|
-
condition?: string
|
|
49
|
+
spec: TransitionSpec<TContext, TExtracted>
|
|
52
50
|
): TransitionResult<TContext, TExtracted> {
|
|
53
51
|
// Handle END_ROUTE
|
|
54
52
|
if (
|
|
@@ -58,8 +56,7 @@ export class State<TContext = unknown, TExtracted = unknown> {
|
|
|
58
56
|
) {
|
|
59
57
|
const endTransition = new Transition<TContext, TExtracted>(
|
|
60
58
|
this.getRef(),
|
|
61
|
-
{ state: END_ROUTE }
|
|
62
|
-
condition
|
|
59
|
+
{ state: END_ROUTE, condition: spec.condition }
|
|
63
60
|
);
|
|
64
61
|
this.transitions.push(endTransition);
|
|
65
62
|
|
|
@@ -71,8 +68,7 @@ export class State<TContext = unknown, TExtracted = unknown> {
|
|
|
71
68
|
if (spec.state && typeof spec.state !== "symbol") {
|
|
72
69
|
const transition = new Transition<TContext, TExtracted>(
|
|
73
70
|
this.getRef(),
|
|
74
|
-
spec
|
|
75
|
-
condition
|
|
71
|
+
spec
|
|
76
72
|
);
|
|
77
73
|
this.transitions.push(transition);
|
|
78
74
|
|
|
@@ -90,8 +86,7 @@ export class State<TContext = unknown, TExtracted = unknown> {
|
|
|
90
86
|
);
|
|
91
87
|
const transition = new Transition<TContext, TExtracted>(
|
|
92
88
|
this.getRef(),
|
|
93
|
-
spec
|
|
94
|
-
condition
|
|
89
|
+
spec
|
|
95
90
|
);
|
|
96
91
|
transition.setTarget(targetState);
|
|
97
92
|
|
|
@@ -160,10 +155,8 @@ export class State<TContext = unknown, TExtracted = unknown> {
|
|
|
160
155
|
|
|
161
156
|
return {
|
|
162
157
|
...ref,
|
|
163
|
-
transitionTo: (
|
|
164
|
-
spec
|
|
165
|
-
condition?: string
|
|
166
|
-
) => stateInstance.transitionTo(spec, condition),
|
|
158
|
+
transitionTo: (spec: TransitionSpec<TContext, TExtracted>) =>
|
|
159
|
+
stateInstance.transitionTo(spec),
|
|
167
160
|
};
|
|
168
161
|
}
|
|
169
162
|
|
package/src/core/Transition.ts
CHANGED
|
@@ -10,12 +10,15 @@ import type { State } from "./State";
|
|
|
10
10
|
*/
|
|
11
11
|
export class Transition<TContext = unknown, TExtracted = unknown> {
|
|
12
12
|
private target?: State<TContext, TExtracted>;
|
|
13
|
+
public readonly condition?: string;
|
|
13
14
|
|
|
14
15
|
constructor(
|
|
15
16
|
public readonly source: StateRef,
|
|
16
|
-
public readonly spec: TransitionSpec<TContext, TExtracted
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
public readonly spec: TransitionSpec<TContext, TExtracted>
|
|
18
|
+
) {
|
|
19
|
+
// Extract condition from spec for convenience
|
|
20
|
+
this.condition = spec.condition;
|
|
21
|
+
}
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
24
|
* Set the target state for this transition
|
package/src/types/route.ts
CHANGED
|
@@ -30,7 +30,7 @@ import type { Guideline } from "./agent";
|
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* Options for creating a route
|
|
33
|
-
* @template TExtracted - Type of data extracted throughout the route (inferred from
|
|
33
|
+
* @template TExtracted - Type of data extracted throughout the route (inferred from extractionSchema)
|
|
34
34
|
*/
|
|
35
35
|
export interface RouteOptions<TExtracted = unknown> {
|
|
36
36
|
/** Custom ID for the route (optional - will generate deterministic ID from title if not provided) */
|
|
@@ -57,13 +57,19 @@ export interface RouteOptions<TExtracted = unknown> {
|
|
|
57
57
|
* NEW: Schema defining data to extract throughout this route
|
|
58
58
|
* This creates a type-safe contract for what data the route collects
|
|
59
59
|
*/
|
|
60
|
-
|
|
60
|
+
extractionSchema?: StructuredSchema;
|
|
61
61
|
/**
|
|
62
62
|
* NEW: Initial data to pre-populate when entering this route
|
|
63
63
|
* Useful for restoring sessions or pre-filling known information
|
|
64
64
|
* States with skipIf conditions will be automatically bypassed if data is present
|
|
65
65
|
*/
|
|
66
66
|
initialData?: Partial<TExtracted>;
|
|
67
|
+
/**
|
|
68
|
+
* NEW: Sequential steps for simple linear flows
|
|
69
|
+
* If provided, automatically chains the steps from initialState to END_ROUTE
|
|
70
|
+
* For complex flows with branching, build the state machine manually instead
|
|
71
|
+
*/
|
|
72
|
+
steps?: TransitionSpec<unknown, TExtracted>[];
|
|
67
73
|
}
|
|
68
74
|
|
|
69
75
|
/**
|
|
@@ -81,7 +87,7 @@ export interface TransitionSpec<TContext = unknown, TExtracted = unknown> {
|
|
|
81
87
|
state?: StateRef | symbol;
|
|
82
88
|
/**
|
|
83
89
|
* NEW: Fields to gather from the conversation in this state
|
|
84
|
-
* These should match keys in the route's
|
|
90
|
+
* These should match keys in the route's extractionSchema
|
|
85
91
|
*/
|
|
86
92
|
gather?: string[];
|
|
87
93
|
/**
|
|
@@ -97,6 +103,11 @@ export interface TransitionSpec<TContext = unknown, TExtracted = unknown> {
|
|
|
97
103
|
* Uses string[] for developer-friendly usage (same as gather)
|
|
98
104
|
*/
|
|
99
105
|
requiredData?: string[];
|
|
106
|
+
/**
|
|
107
|
+
* Optional condition for this transition
|
|
108
|
+
* Description of when this transition should be taken
|
|
109
|
+
*/
|
|
110
|
+
condition?: string;
|
|
100
111
|
}
|
|
101
112
|
|
|
102
113
|
/**
|
|
@@ -107,7 +118,6 @@ export interface TransitionResult<TContext = unknown, TExtracted = unknown>
|
|
|
107
118
|
extends StateRef {
|
|
108
119
|
/** Allow chaining transitions */
|
|
109
120
|
transitionTo: (
|
|
110
|
-
spec: TransitionSpec<TContext, TExtracted
|
|
111
|
-
condition?: string
|
|
121
|
+
spec: TransitionSpec<TContext, TExtracted>
|
|
112
122
|
) => TransitionResult<TContext, TExtracted>;
|
|
113
123
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Event, EventKind } from "../types/history";
|
|
2
|
+
/**
|
|
3
|
+
* Helper to extract last message from history
|
|
4
|
+
*/
|
|
5
|
+
export function getLastMessageFromHistory(history: Event[]): string {
|
|
6
|
+
for (let i = history.length - 1; i >= 0; i--) {
|
|
7
|
+
const event = history[i];
|
|
8
|
+
if (event.kind === EventKind.MESSAGE) {
|
|
9
|
+
if (!event.data || !("message" in event.data)) {
|
|
10
|
+
continue;
|
|
11
|
+
}
|
|
12
|
+
return event.data.message;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return "";
|
|
16
|
+
}
|