@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/dist/core/State.d.ts
CHANGED
|
@@ -21,10 +21,9 @@ export declare class State<TContext = unknown, TExtracted = unknown> {
|
|
|
21
21
|
* Create a transition from this state to another
|
|
22
22
|
*
|
|
23
23
|
* @param spec - Transition specification (chatState, toolState, or direct state)
|
|
24
|
-
* @param condition - Optional condition for this transition
|
|
25
24
|
* @returns TransitionResult that supports chaining
|
|
26
25
|
*/
|
|
27
|
-
transitionTo(spec: TransitionSpec<TContext, TExtracted
|
|
26
|
+
transitionTo(spec: TransitionSpec<TContext, TExtracted>): TransitionResult<TContext, TExtracted>;
|
|
28
27
|
/**
|
|
29
28
|
* Add a guideline specific to this state
|
|
30
29
|
*/
|
package/dist/core/State.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"State.d.ts","sourceRoot":"","sources":["../../src/core/State.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EACV,QAAQ,EACR,cAAc,EACd,gBAAgB,EACjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAGhD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG1C;;GAEG;AACH,qBAAa,KAAK,CAAC,QAAQ,GAAG,OAAO,EAAE,UAAU,GAAG,OAAO;aASvC,OAAO,EAAE,MAAM;aACf,WAAW,CAAC,EAAE,MAAM;IATtC,SAAgB,EAAE,EAAE,MAAM,CAAC;IAC3B,OAAO,CAAC,WAAW,CAA0C;IAC7D,OAAO,CAAC,UAAU,CAAmB;IACrC,SAAgB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxC,SAAgB,MAAM,CAAC,EAAE,CAAC,SAAS,EAAE,OAAO,CAAC,UAAU,CAAC,KAAK,OAAO,CAAC;IACrE,SAAgB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;gBAGtB,OAAO,EAAE,MAAM,EACf,WAAW,CAAC,EAAE,MAAM,YAAA,EACpC,QAAQ,CAAC,EAAE,MAAM,EAC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zC;;OAEG;IACH,YAAY,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI;IAIxC;;OAEG;IACH,aAAa,IAAI,SAAS,EAAE;IAI5B;;OAEG;IACH,cAAc,IAAI,UAAU,CAAC,QAAQ,EAAE,UAAU,CAAC,EAAE;IAIpD;;OAEG;IACH,UAAU,CAAC,SAAS,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,OAAO;IAKnD;;OAEG;IACH,eAAe,CAAC,SAAS,EAAE,OAAO,CAAC,UAAU,CAAC,GAAG,OAAO;IAOxD;;OAEG;IACH,MAAM,IAAI,QAAQ;IAOlB;;OAEG;IACH,OAAO,CAAC,4BAA4B;IAapC;;OAEG;IACH,OAAO,CAAC,iBAAiB;CAa1B"}
|
package/dist/core/State.js
CHANGED
|
@@ -23,29 +23,28 @@ export class State {
|
|
|
23
23
|
* Create a transition from this state to another
|
|
24
24
|
*
|
|
25
25
|
* @param spec - Transition specification (chatState, toolState, or direct state)
|
|
26
|
-
* @param condition - Optional condition for this transition
|
|
27
26
|
* @returns TransitionResult that supports chaining
|
|
28
27
|
*/
|
|
29
|
-
transitionTo(spec
|
|
28
|
+
transitionTo(spec) {
|
|
30
29
|
// Handle END_ROUTE
|
|
31
30
|
if (spec.state &&
|
|
32
31
|
typeof spec.state === "symbol" &&
|
|
33
32
|
spec.state === END_ROUTE) {
|
|
34
|
-
const endTransition = new Transition(this.getRef(), { state: END_ROUTE
|
|
33
|
+
const endTransition = new Transition(this.getRef(), { state: END_ROUTE, condition: spec.condition });
|
|
35
34
|
this.transitions.push(endTransition);
|
|
36
35
|
// Return a terminal state reference
|
|
37
36
|
return this.createTerminalRef();
|
|
38
37
|
}
|
|
39
38
|
// Handle direct state reference
|
|
40
39
|
if (spec.state && typeof spec.state !== "symbol") {
|
|
41
|
-
const transition = new Transition(this.getRef(), spec
|
|
40
|
+
const transition = new Transition(this.getRef(), spec);
|
|
42
41
|
this.transitions.push(transition);
|
|
43
42
|
return this.createStateRefWithTransition(spec.state);
|
|
44
43
|
}
|
|
45
44
|
// Create new target state for chatState or toolState
|
|
46
45
|
const targetState = new State(this.routeId, spec.chatState, spec.id, // Use custom ID if provided
|
|
47
46
|
spec.gather, spec.skipIf, spec.requiredData);
|
|
48
|
-
const transition = new Transition(this.getRef(), spec
|
|
47
|
+
const transition = new Transition(this.getRef(), spec);
|
|
49
48
|
transition.setTarget(targetState);
|
|
50
49
|
this.transitions.push(transition);
|
|
51
50
|
return this.createStateRefWithTransition(targetState.getRef(), targetState);
|
|
@@ -100,7 +99,7 @@ export class State {
|
|
|
100
99
|
const stateInstance = state || this;
|
|
101
100
|
return {
|
|
102
101
|
...ref,
|
|
103
|
-
transitionTo: (spec
|
|
102
|
+
transitionTo: (spec) => stateInstance.transitionTo(spec),
|
|
104
103
|
};
|
|
105
104
|
}
|
|
106
105
|
/**
|
package/dist/core/State.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"State.js","sourceRoot":"","sources":["../../src/core/State.ts"],"names":[],"mappings":"AAAA;;GAEG;AASH,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C;;GAEG;AACH,MAAM,OAAO,KAAK;IAQhB,YACkB,OAAe,EACf,WAAoB,EACpC,QAAiB,EACjB,YAAuB,EACvB,MAAoD,EACpD,YAAuB;QALP,YAAO,GAAP,OAAO,CAAQ;QACf,gBAAW,GAAX,WAAW,CAAS;QAR9B,gBAAW,GAAuC,EAAE,CAAC;QACrD,eAAU,GAAgB,EAAE,CAAC;QAanC,kDAAkD;QAClD,IAAI,CAAC,EAAE,GAAG,QAAQ,IAAI,eAAe,CAAC,OAAO,EAAE,WAAW,CAAC,CAAC;QAC5D,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,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,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAChC,CAAC;IAED;;OAEG;IACH,eAAe,CAAC,SAA8B;QAC5C,IAAI,CAAC,IAAI,CAAC,YAAY,IAAI,IAAI,CAAC,YAAY,CAAC,MAAM,KAAK,CAAC;YAAE,OAAO,IAAI,CAAC;QACtE,OAAO,IAAI,CAAC,YAAY,CAAC,KAAK,CAC5B,CAAC,GAAG,EAAE,EAAE,CAAC,SAAS,CAAC,GAAuB,CAAC,KAAK,SAAS,CAC1D,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,OAAO;YACL,EAAE,EAAE,IAAI,CAAC,EAAE;YACX,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAC;IACJ,CAAC;IAED;;OAEG;IACK,4BAA4B,CAClC,GAAa,EACb,KAAmC;QAEnC,MAAM,aAAa,GAAG,KAAK,IAAI,IAAI,CAAC;QAEpC,OAAO;YACL,GAAG,GAAG;YACN,YAAY,EAAE,CAAC,IAA0C,EAAE,EAAE,CAC3D,aAAa,CAAC,YAAY,CAAC,IAAI,CAAC;SACnC,CAAC;IACJ,CAAC;IAED;;OAEG;IACK,iBAAiB;QACvB,MAAM,WAAW,GAAa;YAC5B,EAAE,EAAE,KAAK;YACT,OAAO,EAAE,IAAI,CAAC,OAAO;SACtB,CAAC;QAEF,OAAO;YACL,GAAG,WAAW;YACd,YAAY,EAAE,GAAG,EAAE;gBACjB,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;YAC5D,CAAC;SACF,CAAC;IACJ,CAAC;CACF"}
|
|
@@ -9,9 +9,9 @@ import type { State } from "./State";
|
|
|
9
9
|
export declare class Transition<TContext = unknown, TExtracted = unknown> {
|
|
10
10
|
readonly source: StateRef;
|
|
11
11
|
readonly spec: TransitionSpec<TContext, TExtracted>;
|
|
12
|
-
readonly condition?: string | undefined;
|
|
13
12
|
private target?;
|
|
14
|
-
|
|
13
|
+
readonly condition?: string;
|
|
14
|
+
constructor(source: StateRef, spec: TransitionSpec<TContext, TExtracted>);
|
|
15
15
|
/**
|
|
16
16
|
* Set the target state for this transition
|
|
17
17
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Transition.d.ts","sourceRoot":"","sources":["../../src/core/Transition.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC;;GAEG;AACH,qBAAa,UAAU,CAAC,QAAQ,GAAG,OAAO,EAAE,UAAU,GAAG,OAAO;
|
|
1
|
+
{"version":3,"file":"Transition.d.ts","sourceRoot":"","sources":["../../src/core/Transition.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAC/D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAErC;;GAEG;AACH,qBAAa,UAAU,CAAC,QAAQ,GAAG,OAAO,EAAE,UAAU,GAAG,OAAO;aAK5C,MAAM,EAAE,QAAQ;aAChB,IAAI,EAAE,cAAc,CAAC,QAAQ,EAAE,UAAU,CAAC;IAL5D,OAAO,CAAC,MAAM,CAAC,CAA8B;IAC7C,SAAgB,SAAS,CAAC,EAAE,MAAM,CAAC;gBAGjB,MAAM,EAAE,QAAQ,EAChB,IAAI,EAAE,cAAc,CAAC,QAAQ,EAAE,UAAU,CAAC;IAM5D;;OAEG;IACH,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,QAAQ,EAAE,UAAU,CAAC,GAAG,IAAI;IAInD;;OAEG;IACH,SAAS,IAAI,KAAK,CAAC,QAAQ,EAAE,UAAU,CAAC,GAAG,SAAS;IAIpD;;OAEG;IACH,YAAY,IAAI,OAAO;IAIvB;;OAEG;IACH,QAAQ,IAAI,MAAM;CAsBnB"}
|
package/dist/core/Transition.js
CHANGED
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
* Represents a transition from one state to another
|
|
6
6
|
*/
|
|
7
7
|
export class Transition {
|
|
8
|
-
constructor(source, spec
|
|
8
|
+
constructor(source, spec) {
|
|
9
9
|
this.source = source;
|
|
10
10
|
this.spec = spec;
|
|
11
|
-
|
|
11
|
+
// Extract condition from spec for convenience
|
|
12
|
+
this.condition = spec.condition;
|
|
12
13
|
}
|
|
13
14
|
/**
|
|
14
15
|
* Set the target state for this transition
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Transition.js","sourceRoot":"","sources":["../../src/core/Transition.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH;;GAEG;AACH,MAAM,OAAO,UAAU;
|
|
1
|
+
{"version":3,"file":"Transition.js","sourceRoot":"","sources":["../../src/core/Transition.ts"],"names":[],"mappings":"AAAA;;GAEG;AAKH;;GAEG;AACH,MAAM,OAAO,UAAU;IAIrB,YACkB,MAAgB,EAChB,IAA0C;QAD1C,WAAM,GAAN,MAAM,CAAU;QAChB,SAAI,GAAJ,IAAI,CAAsC;QAE1D,8CAA8C;QAC9C,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC;IAClC,CAAC;IAED;;OAEG;IACH,SAAS,CAAC,KAAkC;QAC1C,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED;;OAEG;IACH,SAAS;QACP,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,YAAY;QACV,OAAO,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,QAAQ;QACN,MAAM,KAAK,GAAa,EAAE,CAAC;QAE3B,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACxB,KAAK,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC;QAC/C,CAAC;QACD,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACxB,KAAK,CAAC,IAAI,CAAC,SAAS,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;QAClD,CAAC;QACD,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC;YACpB,IAAI,OAAO,IAAI,CAAC,IAAI,CAAC,KAAK,KAAK,QAAQ,EAAE,CAAC;gBACxC,KAAK,CAAC,IAAI,CAAC,kBAAkB,CAAC,CAAC;YACjC,CAAC;iBAAM,CAAC;gBACN,KAAK,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;YAC7C,CAAC;QACH,CAAC;QAED,MAAM,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpC,MAAM,aAAa,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,WAAW,IAAI,CAAC,SAAS,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;QAEzE,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,EAAE,QAAQ,UAAU,IAAI,aAAa,EAAE,CAAC;IAChE,CAAC;CACF"}
|
package/dist/types/route.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ export interface StateRef {
|
|
|
25
25
|
import type { Guideline } from "./agent";
|
|
26
26
|
/**
|
|
27
27
|
* Options for creating a route
|
|
28
|
-
* @template TExtracted - Type of data extracted throughout the route (inferred from
|
|
28
|
+
* @template TExtracted - Type of data extracted throughout the route (inferred from extractionSchema)
|
|
29
29
|
*/
|
|
30
30
|
export interface RouteOptions<TExtracted = unknown> {
|
|
31
31
|
/** Custom ID for the route (optional - will generate deterministic ID from title if not provided) */
|
|
@@ -52,13 +52,19 @@ export interface RouteOptions<TExtracted = unknown> {
|
|
|
52
52
|
* NEW: Schema defining data to extract throughout this route
|
|
53
53
|
* This creates a type-safe contract for what data the route collects
|
|
54
54
|
*/
|
|
55
|
-
|
|
55
|
+
extractionSchema?: StructuredSchema;
|
|
56
56
|
/**
|
|
57
57
|
* NEW: Initial data to pre-populate when entering this route
|
|
58
58
|
* Useful for restoring sessions or pre-filling known information
|
|
59
59
|
* States with skipIf conditions will be automatically bypassed if data is present
|
|
60
60
|
*/
|
|
61
61
|
initialData?: Partial<TExtracted>;
|
|
62
|
+
/**
|
|
63
|
+
* NEW: Sequential steps for simple linear flows
|
|
64
|
+
* If provided, automatically chains the steps from initialState to END_ROUTE
|
|
65
|
+
* For complex flows with branching, build the state machine manually instead
|
|
66
|
+
*/
|
|
67
|
+
steps?: TransitionSpec<unknown, TExtracted>[];
|
|
62
68
|
}
|
|
63
69
|
/**
|
|
64
70
|
* Specification for a state transition
|
|
@@ -74,7 +80,7 @@ export interface TransitionSpec<TContext = unknown, TExtracted = unknown> {
|
|
|
74
80
|
state?: StateRef | symbol;
|
|
75
81
|
/**
|
|
76
82
|
* NEW: Fields to gather from the conversation in this state
|
|
77
|
-
* These should match keys in the route's
|
|
83
|
+
* These should match keys in the route's extractionSchema
|
|
78
84
|
*/
|
|
79
85
|
gather?: string[];
|
|
80
86
|
/**
|
|
@@ -90,6 +96,11 @@ export interface TransitionSpec<TContext = unknown, TExtracted = unknown> {
|
|
|
90
96
|
* Uses string[] for developer-friendly usage (same as gather)
|
|
91
97
|
*/
|
|
92
98
|
requiredData?: string[];
|
|
99
|
+
/**
|
|
100
|
+
* Optional condition for this transition
|
|
101
|
+
* Description of when this transition should be taken
|
|
102
|
+
*/
|
|
103
|
+
condition?: string;
|
|
93
104
|
}
|
|
94
105
|
/**
|
|
95
106
|
* Result of a transition operation
|
|
@@ -97,6 +108,6 @@ export interface TransitionSpec<TContext = unknown, TExtracted = unknown> {
|
|
|
97
108
|
*/
|
|
98
109
|
export interface TransitionResult<TContext = unknown, TExtracted = unknown> extends StateRef {
|
|
99
110
|
/** Allow chaining transitions */
|
|
100
|
-
transitionTo: (spec: TransitionSpec<TContext, TExtracted
|
|
111
|
+
transitionTo: (spec: TransitionSpec<TContext, TExtracted>) => TransitionResult<TContext, TExtracted>;
|
|
101
112
|
}
|
|
102
113
|
//# sourceMappingURL=route.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"route.d.ts","sourceRoot":"","sources":["../../src/types/route.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAEjD;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,uBAAuB;IACvB,EAAE,EAAE,MAAM,CAAC;CACZ;AAED;;GAEG;AACH,MAAM,WAAW,QAAQ;IACvB,uBAAuB;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,kCAAkC;IAClC,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;GAEG;AACH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAC;AAEzC;;;GAGG;AACH,MAAM,WAAW,YAAY,CAAC,UAAU,GAAG,OAAO;IAChD,qGAAqG;IACrG,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,yBAAyB;IACzB,KAAK,EAAE,MAAM,CAAC;IACd,kDAAkD;IAClD,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,0CAA0C;IAC1C,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,wCAAwC;IACxC,UAAU,CAAC,EAAE,SAAS,EAAE,CAAC;IACzB,4EAA4E;IAC5E,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;IACnB,yDAAyD;IACzD,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;IACjB,kEAAkE;IAClE,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,4EAA4E;IAC5E,mBAAmB,CAAC,EAAE,gBAAgB,CAAC;IACvC,6EAA6E;IAC7E,oBAAoB,CAAC,EAAE,gBAA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@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { EventKind } from "../types/history";
|
|
2
|
+
/**
|
|
3
|
+
* Helper to extract last message from history
|
|
4
|
+
*/
|
|
5
|
+
export function getLastMessageFromHistory(history) {
|
|
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
|
+
}
|
|
17
|
+
//# sourceMappingURL=event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event.js","sourceRoot":"","sources":["../../src/utils/event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACzD;;GAEG;AACH,MAAM,UAAU,yBAAyB,CAAC,OAAgB;IACxD,KAAK,IAAI,CAAC,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7C,MAAM,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;QACzB,IAAI,KAAK,CAAC,IAAI,KAAK,SAAS,CAAC,OAAO,EAAE,CAAC;YACrC,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC,CAAC,SAAS,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE,CAAC;gBAC9C,SAAS;YACX,CAAC;YACD,OAAO,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC;QAC5B,CAAC;IACH,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
package/docs/ADAPTERS.md
CHANGED
|
@@ -102,7 +102,7 @@ const agent = new Agent({
|
|
|
102
102
|
// Create a route with data extraction
|
|
103
103
|
const route = agent.createRoute<YourDataType>({
|
|
104
104
|
title: "My Route",
|
|
105
|
-
|
|
105
|
+
extractionSchema: {
|
|
106
106
|
type: "object",
|
|
107
107
|
properties: {
|
|
108
108
|
field1: { type: "string" },
|
package/docs/API_REFERENCE.md
CHANGED
|
@@ -321,7 +321,7 @@ interface RouteOptions<TExtracted = unknown> {
|
|
|
321
321
|
prohibitions?: string[]; // Absolute prohibitions the agent MUST NEVER do in this route
|
|
322
322
|
|
|
323
323
|
// NEW: Schema-first data extraction
|
|
324
|
-
|
|
324
|
+
extractionSchema?: {
|
|
325
325
|
type: "object";
|
|
326
326
|
properties: Record<string, any>;
|
|
327
327
|
required?: string[];
|
|
@@ -395,7 +395,7 @@ Represents a state within a conversation route.
|
|
|
395
395
|
|
|
396
396
|
#### Methods
|
|
397
397
|
|
|
398
|
-
##### `transitionTo(spec: TransitionSpec
|
|
398
|
+
##### `transitionTo(spec: TransitionSpec): TransitionResult`
|
|
399
399
|
|
|
400
400
|
Creates a transition from this state and returns a chainable result.
|
|
401
401
|
|
|
@@ -413,18 +413,24 @@ interface TransitionSpec<TExtracted = unknown> {
|
|
|
413
413
|
|
|
414
414
|
// NEW: Prerequisites that must be met to enter this state
|
|
415
415
|
requiredData?: string[];
|
|
416
|
+
|
|
417
|
+
// Optional: AI-evaluated text condition for this transition
|
|
418
|
+
condition?: string;
|
|
416
419
|
}
|
|
417
420
|
|
|
418
421
|
interface TransitionResult<TExtracted = unknown> {
|
|
419
422
|
id: string; // State identifier
|
|
420
423
|
routeId: string; // Route identifier
|
|
421
424
|
transitionTo: (
|
|
422
|
-
spec: TransitionSpec<TExtracted
|
|
423
|
-
condition?: string
|
|
425
|
+
spec: TransitionSpec<TExtracted>
|
|
424
426
|
) => TransitionResult<TExtracted>;
|
|
425
427
|
}
|
|
426
428
|
```
|
|
427
429
|
|
|
430
|
+
**Parameters:**
|
|
431
|
+
|
|
432
|
+
- `spec`: The transition specification (see `TransitionSpec` above). Can include an optional `condition` property for AI-evaluated state selection guidance.
|
|
433
|
+
|
|
428
434
|
**Returns:** A `TransitionResult` that includes the target state's reference (`id`, `routeId`) and a `transitionTo` method for chaining additional transitions.
|
|
429
435
|
|
|
430
436
|
**Example:**
|
|
@@ -440,7 +446,7 @@ interface FlightData {
|
|
|
440
446
|
// Create a data-driven route
|
|
441
447
|
const flightRoute = agent.createRoute<FlightData>({
|
|
442
448
|
title: "Book Flight",
|
|
443
|
-
|
|
449
|
+
extractionSchema: {
|
|
444
450
|
type: "object",
|
|
445
451
|
properties: {
|
|
446
452
|
destination: { type: "string" },
|
|
@@ -451,11 +457,12 @@ const flightRoute = agent.createRoute<FlightData>({
|
|
|
451
457
|
},
|
|
452
458
|
});
|
|
453
459
|
|
|
454
|
-
// Approach 1: Step-by-step with data extraction
|
|
460
|
+
// Approach 1: Step-by-step with data extraction and text conditions
|
|
455
461
|
const askDestination = flightRoute.initialState.transitionTo({
|
|
456
462
|
chatState: "Ask where they want to fly",
|
|
457
463
|
gather: ["destination"],
|
|
458
464
|
skipIf: (extracted) => !!extracted.destination, // Skip if already have destination
|
|
465
|
+
condition: "Customer hasn't specified destination yet", // AI-evaluated condition
|
|
459
466
|
});
|
|
460
467
|
|
|
461
468
|
const askDates = askDestination.transitionTo({
|
|
@@ -463,6 +470,7 @@ const askDates = askDestination.transitionTo({
|
|
|
463
470
|
gather: ["departureDate"],
|
|
464
471
|
skipIf: (extracted) => !!extracted.departureDate,
|
|
465
472
|
requiredData: ["destination"], // Must have destination first
|
|
473
|
+
condition: "Destination confirmed, need travel dates",
|
|
466
474
|
});
|
|
467
475
|
|
|
468
476
|
const askPassengers = askDates.transitionTo({
|
|
@@ -1546,7 +1554,7 @@ interface SessionState<TExtracted = Record<string, unknown>> {
|
|
|
1546
1554
|
**Key Features:**
|
|
1547
1555
|
|
|
1548
1556
|
- **`id`** - Optional session identifier that persists across database operations
|
|
1549
|
-
- **`extracted`** - Type-safe data collected via `
|
|
1557
|
+
- **`extracted`** - Type-safe data collected via `extractionSchema`
|
|
1550
1558
|
- **`currentRoute`** / **`currentState`** - Track conversation position
|
|
1551
1559
|
- **`routeHistory`** - Full audit trail of route transitions
|
|
1552
1560
|
- **`metadata`** - Custom data (timestamps, user info, etc.)
|
package/docs/ARCHITECTURE.md
CHANGED
|
@@ -22,7 +22,7 @@ interface FlightData {
|
|
|
22
22
|
const route = agent.createRoute<FlightData>({
|
|
23
23
|
title: "Book Flight",
|
|
24
24
|
description: "Help user book a flight",
|
|
25
|
-
|
|
25
|
+
extractionSchema: {
|
|
26
26
|
type: "object",
|
|
27
27
|
properties: {
|
|
28
28
|
destination: { type: "string" },
|
|
@@ -106,9 +106,9 @@ const response = await agent.respond({
|
|
|
106
106
|
- **State Recovery** - Resume conversations from any point
|
|
107
107
|
- **Session Tracking** - Track conversations via session ID in database
|
|
108
108
|
|
|
109
|
-
### 3. 🔧 Code-Based State Logic
|
|
109
|
+
### 3. 🔧 Code-Based State Logic + AI-Driven Transitions
|
|
110
110
|
|
|
111
|
-
Use TypeScript functions
|
|
111
|
+
Use TypeScript functions for deterministic flow control AND text conditions for AI-driven state selection:
|
|
112
112
|
|
|
113
113
|
```typescript
|
|
114
114
|
// State with smart bypassing based on extracted data
|
|
@@ -117,6 +117,7 @@ const askDestination = route.initialState.transitionTo({
|
|
|
117
117
|
chatState: "Ask where they want to fly",
|
|
118
118
|
gather: ["destination"],
|
|
119
119
|
skipIf: (extracted) => !!extracted.destination, // Code-based condition!
|
|
120
|
+
condition: "Customer hasn't specified destination yet", // Text condition for AI
|
|
120
121
|
});
|
|
121
122
|
|
|
122
123
|
const askDate = askDestination.transitionTo({
|
|
@@ -125,6 +126,8 @@ const askDate = askDestination.transitionTo({
|
|
|
125
126
|
gather: ["departureDate"],
|
|
126
127
|
skipIf: (extracted) => !!extracted.departureDate,
|
|
127
128
|
requiredData: ["destination"], // Prerequisites
|
|
129
|
+
condition: "Destination confirmed, need travel dates now",
|
|
130
|
+
});
|
|
128
131
|
});
|
|
129
132
|
```
|
|
130
133
|
|
|
@@ -150,6 +153,23 @@ If you don't provide an ID, one is automatically generated from the route ID and
|
|
|
150
153
|
- **Type Safety** - Full TypeScript support for data validation
|
|
151
154
|
- **Custom IDs** - Easier tracking and debugging with meaningful state identifiers
|
|
152
155
|
|
|
156
|
+
**How State Transitions Work:**
|
|
157
|
+
|
|
158
|
+
1. **Code filters first**: `skipIf` and `requiredData` filter out invalid states deterministically
|
|
159
|
+
2. **AI selects best state**: From valid candidates, AI evaluates text conditions to choose optimal state
|
|
160
|
+
3. **Combined decision**: Single AI call handles both route selection AND state selection (no extra calls!)
|
|
161
|
+
4. **Completion detection**: When all states are skipped and `END_ROUTE` is reached, route is marked complete
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
// The AI sees:
|
|
165
|
+
// "Available states: askDate, confirmBooking
|
|
166
|
+
// - askDate: Destination confirmed, need travel dates now
|
|
167
|
+
// - confirmBooking: All required info collected, ready to book
|
|
168
|
+
// Current extracted: {destination: 'Paris', departureDate: '2025-01-15'}
|
|
169
|
+
//
|
|
170
|
+
// → AI selects 'confirmBooking' based on context"
|
|
171
|
+
```
|
|
172
|
+
|
|
153
173
|
### 4. 🛠️ Tools with Data Access
|
|
154
174
|
|
|
155
175
|
Tools execute with full context including extracted data:
|
|
@@ -350,7 +370,7 @@ interface BookingData {
|
|
|
350
370
|
|
|
351
371
|
const bookingRoute = agent.createRoute<BookingData>({
|
|
352
372
|
title: "Flight Booking",
|
|
353
|
-
|
|
373
|
+
extractionSchema: {
|
|
354
374
|
/* schema for all fields */
|
|
355
375
|
},
|
|
356
376
|
});
|
|
@@ -380,7 +400,7 @@ For simple question-answering without state:
|
|
|
380
400
|
const qnaRoute = agent.createRoute({
|
|
381
401
|
title: "Company Q&A",
|
|
382
402
|
conditions: ["User asks about company"],
|
|
383
|
-
// NO
|
|
403
|
+
// NO extractionSchema - stateless!
|
|
384
404
|
});
|
|
385
405
|
|
|
386
406
|
// Just use initial state
|
|
@@ -103,7 +103,7 @@ const agent = new Agent<FlightBookingContext>({
|
|
|
103
103
|
title: 'Book Flight',
|
|
104
104
|
description: 'Help user book a flight',
|
|
105
105
|
conditions: ['User wants to book a flight'],
|
|
106
|
-
|
|
106
|
+
extractionSchema: {
|
|
107
107
|
type: 'object',
|
|
108
108
|
properties: {
|
|
109
109
|
destination: { type: 'string' },
|
|
@@ -175,7 +175,7 @@ interface RouteOptions<TExtracted = unknown> {
|
|
|
175
175
|
prohibitions?: string[]; // Absolute prohibitions the agent MUST NEVER do
|
|
176
176
|
|
|
177
177
|
// NEW: Schema-first data extraction
|
|
178
|
-
|
|
178
|
+
extractionSchema?: {
|
|
179
179
|
type: "object";
|
|
180
180
|
properties: Record<string, any>;
|
|
181
181
|
required?: string[];
|
package/docs/GETTING_STARTED.md
CHANGED
|
@@ -86,7 +86,7 @@ const bookingRoute = agent.createRoute<FlightData>({
|
|
|
86
86
|
title: "Book Flight",
|
|
87
87
|
description: "Help user book a flight",
|
|
88
88
|
conditions: ["User wants to book a flight"],
|
|
89
|
-
|
|
89
|
+
extractionSchema: {
|
|
90
90
|
type: "object",
|
|
91
91
|
properties: {
|
|
92
92
|
destination: { type: "string" },
|
package/docs/PERSISTENCE.md
CHANGED
|
@@ -110,7 +110,7 @@ const agent = new Agent({
|
|
|
110
110
|
// Create a route with data extraction
|
|
111
111
|
const bookingRoute = agent.createRoute<BookingData>({
|
|
112
112
|
title: "Book Flight",
|
|
113
|
-
|
|
113
|
+
extractionSchema: {
|
|
114
114
|
type: "object",
|
|
115
115
|
properties: {
|
|
116
116
|
destination: { type: "string" },
|
|
@@ -273,7 +273,7 @@ await agent.updateContext({ preferences: { theme: "dark" } });
|
|
|
273
273
|
The new architecture automatically saves and loads `SessionState<TExtracted>` which includes:
|
|
274
274
|
|
|
275
275
|
- **Current route and state** - Track conversation progress
|
|
276
|
-
- **Extracted data** - All data gathered via `
|
|
276
|
+
- **Extracted data** - All data gathered via `extractionSchema` and `gather` fields
|
|
277
277
|
- **Route history** - History of route transitions
|
|
278
278
|
- **Metadata** - Session timestamps and custom data
|
|
279
279
|
|
|
@@ -533,7 +533,7 @@ await persistence.saveMessage({
|
|
|
533
533
|
|
|
534
534
|
1. ✅ **Use `createSessionWithState()`** - Get both database record and session state in one call
|
|
535
535
|
2. ✅ **Enable `autoSave: true`** - Automatically persist session state after each response
|
|
536
|
-
3. ✅ **Define extraction schemas** - Use `
|
|
536
|
+
3. ✅ **Define extraction schemas** - Use `extractionSchema` in routes for structured data collection
|
|
537
537
|
4. ✅ **Pass session state** - Always pass `session` parameter to `agent.respond()`
|
|
538
538
|
5. ✅ **Load session state** - Use `loadSessionState()` to resume conversations
|
|
539
539
|
6. ✅ **Store extracted data** - Leverage `collectedData.extracted` for user input tracking
|