@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.
Files changed (85) hide show
  1. package/README.md +9 -4
  2. package/dist/cjs/core/Agent.d.ts +0 -5
  3. package/dist/cjs/core/Agent.d.ts.map +1 -1
  4. package/dist/cjs/core/Agent.js +75 -157
  5. package/dist/cjs/core/Agent.js.map +1 -1
  6. package/dist/cjs/core/ResponseEngine.js +2 -2
  7. package/dist/cjs/core/ResponseEngine.js.map +1 -1
  8. package/dist/cjs/core/Route.d.ts +6 -1
  9. package/dist/cjs/core/Route.d.ts.map +1 -1
  10. package/dist/cjs/core/Route.js +19 -1
  11. package/dist/cjs/core/Route.js.map +1 -1
  12. package/dist/cjs/core/RoutingEngine.d.ts +68 -2
  13. package/dist/cjs/core/RoutingEngine.d.ts.map +1 -1
  14. package/dist/cjs/core/RoutingEngine.js +416 -2
  15. package/dist/cjs/core/RoutingEngine.js.map +1 -1
  16. package/dist/cjs/core/State.d.ts +1 -2
  17. package/dist/cjs/core/State.d.ts.map +1 -1
  18. package/dist/cjs/core/State.js +5 -6
  19. package/dist/cjs/core/State.js.map +1 -1
  20. package/dist/cjs/core/Transition.d.ts +2 -2
  21. package/dist/cjs/core/Transition.d.ts.map +1 -1
  22. package/dist/cjs/core/Transition.js +3 -2
  23. package/dist/cjs/core/Transition.js.map +1 -1
  24. package/dist/cjs/types/route.d.ts +15 -4
  25. package/dist/cjs/types/route.d.ts.map +1 -1
  26. package/dist/cjs/utils/event.d.ts +6 -0
  27. package/dist/cjs/utils/event.d.ts.map +1 -0
  28. package/dist/cjs/utils/event.js +20 -0
  29. package/dist/cjs/utils/event.js.map +1 -0
  30. package/dist/core/Agent.d.ts +0 -5
  31. package/dist/core/Agent.d.ts.map +1 -1
  32. package/dist/core/Agent.js +74 -156
  33. package/dist/core/Agent.js.map +1 -1
  34. package/dist/core/ResponseEngine.js +2 -2
  35. package/dist/core/ResponseEngine.js.map +1 -1
  36. package/dist/core/Route.d.ts +6 -1
  37. package/dist/core/Route.d.ts.map +1 -1
  38. package/dist/core/Route.js +19 -1
  39. package/dist/core/Route.js.map +1 -1
  40. package/dist/core/RoutingEngine.d.ts +68 -2
  41. package/dist/core/RoutingEngine.d.ts.map +1 -1
  42. package/dist/core/RoutingEngine.js +416 -2
  43. package/dist/core/RoutingEngine.js.map +1 -1
  44. package/dist/core/State.d.ts +1 -2
  45. package/dist/core/State.d.ts.map +1 -1
  46. package/dist/core/State.js +5 -6
  47. package/dist/core/State.js.map +1 -1
  48. package/dist/core/Transition.d.ts +2 -2
  49. package/dist/core/Transition.d.ts.map +1 -1
  50. package/dist/core/Transition.js +3 -2
  51. package/dist/core/Transition.js.map +1 -1
  52. package/dist/types/route.d.ts +15 -4
  53. package/dist/types/route.d.ts.map +1 -1
  54. package/dist/utils/event.d.ts +6 -0
  55. package/dist/utils/event.d.ts.map +1 -0
  56. package/dist/utils/event.js +17 -0
  57. package/dist/utils/event.js.map +1 -0
  58. package/docs/ADAPTERS.md +1 -1
  59. package/docs/API_REFERENCE.md +15 -7
  60. package/docs/ARCHITECTURE.md +25 -5
  61. package/docs/CONSTRUCTOR_OPTIONS.md +2 -2
  62. package/docs/CONTEXT_MANAGEMENT.md +1 -1
  63. package/docs/GETTING_STARTED.md +1 -1
  64. package/docs/PERSISTENCE.md +3 -3
  65. package/examples/business-onboarding.ts +97 -70
  66. package/examples/company-qna-agent.ts +4 -4
  67. package/examples/custom-database-persistence.ts +2 -2
  68. package/examples/declarative-agent.ts +3 -3
  69. package/examples/extracted-data-modification.ts +1 -1
  70. package/examples/healthcare-agent.ts +9 -3
  71. package/examples/openai-agent.ts +1 -1
  72. package/examples/opensearch-persistence.ts +2 -2
  73. package/examples/persistent-onboarding.ts +18 -12
  74. package/examples/prisma-persistence.ts +3 -3
  75. package/examples/redis-persistence.ts +3 -3
  76. package/examples/travel-agent.ts +23 -4
  77. package/package.json +1 -1
  78. package/src/core/Agent.ts +78 -227
  79. package/src/core/ResponseEngine.ts +2 -2
  80. package/src/core/Route.ts +34 -3
  81. package/src/core/RoutingEngine.ts +663 -2
  82. package/src/core/State.ts +6 -13
  83. package/src/core/Transition.ts +6 -3
  84. package/src/types/route.ts +15 -5
  85. 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: TransitionSpec<TContext, TExtracted>,
165
- condition?: string
166
- ) => stateInstance.transitionTo(spec, condition),
158
+ transitionTo: (spec: TransitionSpec<TContext, TExtracted>) =>
159
+ stateInstance.transitionTo(spec),
167
160
  };
168
161
  }
169
162
 
@@ -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
- public readonly condition?: string
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
@@ -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 gatherSchema)
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
- gatherSchema?: StructuredSchema;
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 gatherSchema
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
+ }