@falai/agent 0.1.5 → 0.3.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 (49) hide show
  1. package/README.md +65 -4
  2. package/dist/cjs/core/Agent.d.ts +11 -0
  3. package/dist/cjs/core/Agent.d.ts.map +1 -1
  4. package/dist/cjs/core/Agent.js +44 -2
  5. package/dist/cjs/core/Agent.js.map +1 -1
  6. package/dist/cjs/core/State.d.ts +1 -1
  7. package/dist/cjs/core/State.d.ts.map +1 -1
  8. package/dist/cjs/core/State.js +4 -10
  9. package/dist/cjs/core/State.js.map +1 -1
  10. package/dist/cjs/index.d.ts +1 -1
  11. package/dist/cjs/index.d.ts.map +1 -1
  12. package/dist/cjs/index.js.map +1 -1
  13. package/dist/cjs/types/agent.d.ts +24 -0
  14. package/dist/cjs/types/agent.d.ts.map +1 -1
  15. package/dist/cjs/types/route.d.ts +4 -6
  16. package/dist/cjs/types/route.d.ts.map +1 -1
  17. package/dist/cjs/types/tool.d.ts +6 -2
  18. package/dist/cjs/types/tool.d.ts.map +1 -1
  19. package/dist/core/Agent.d.ts +11 -0
  20. package/dist/core/Agent.d.ts.map +1 -1
  21. package/dist/core/Agent.js +44 -2
  22. package/dist/core/Agent.js.map +1 -1
  23. package/dist/core/State.d.ts +1 -1
  24. package/dist/core/State.d.ts.map +1 -1
  25. package/dist/core/State.js +4 -10
  26. package/dist/core/State.js.map +1 -1
  27. package/dist/index.d.ts +1 -1
  28. package/dist/index.d.ts.map +1 -1
  29. package/dist/index.js.map +1 -1
  30. package/dist/types/agent.d.ts +24 -0
  31. package/dist/types/agent.d.ts.map +1 -1
  32. package/dist/types/route.d.ts +4 -6
  33. package/dist/types/route.d.ts.map +1 -1
  34. package/dist/types/tool.d.ts +6 -2
  35. package/dist/types/tool.d.ts.map +1 -1
  36. package/docs/API_REFERENCE.md +25 -1
  37. package/docs/CONTEXT_MANAGEMENT.md +447 -0
  38. package/docs/GETTING_STARTED.md +3 -3
  39. package/examples/healthcare-agent.ts +15 -15
  40. package/examples/openai-agent.ts +2 -2
  41. package/examples/persistent-onboarding.ts +464 -0
  42. package/examples/travel-agent.ts +17 -17
  43. package/package.json +1 -1
  44. package/src/core/Agent.ts +56 -2
  45. package/src/core/State.ts +6 -25
  46. package/src/index.ts +2 -0
  47. package/src/types/agent.ts +32 -0
  48. package/src/types/route.ts +7 -9
  49. package/src/types/tool.ts +6 -2
package/src/core/State.ts CHANGED
@@ -35,7 +35,7 @@ export class State<TContext = unknown> {
35
35
  *
36
36
  * @param spec - Transition specification (chatState, toolState, or direct state)
37
37
  * @param condition - Optional condition for this transition
38
- * @returns Object with target state that supports chaining
38
+ * @returns TransitionResult that supports chaining
39
39
  */
40
40
  transitionTo(
41
41
  spec: TransitionSpec<TContext>,
@@ -55,9 +55,7 @@ export class State<TContext = unknown> {
55
55
  this.transitions.push(endTransition);
56
56
 
57
57
  // Return a terminal state reference
58
- return {
59
- target: this.createTerminalRef(),
60
- };
58
+ return this.createTerminalRef();
61
59
  }
62
60
 
63
61
  // Handle direct state reference
@@ -69,9 +67,7 @@ export class State<TContext = unknown> {
69
67
  );
70
68
  this.transitions.push(transition);
71
69
 
72
- return {
73
- target: this.createStateRefWithTransition(spec.state),
74
- };
70
+ return this.createStateRefWithTransition(spec.state);
75
71
  }
76
72
 
77
73
  // Create new target state for chatState or toolState
@@ -81,12 +77,7 @@ export class State<TContext = unknown> {
81
77
 
82
78
  this.transitions.push(transition);
83
79
 
84
- return {
85
- target: this.createStateRefWithTransition(
86
- targetState.getRef(),
87
- targetState
88
- ),
89
- };
80
+ return this.createStateRefWithTransition(targetState.getRef(), targetState);
90
81
  }
91
82
 
92
83
  /**
@@ -126,12 +117,7 @@ export class State<TContext = unknown> {
126
117
  private createStateRefWithTransition(
127
118
  ref: StateRef,
128
119
  state?: State<TContext>
129
- ): StateRef & {
130
- transitionTo: (
131
- spec: TransitionSpec<TContext>,
132
- condition?: string
133
- ) => TransitionResult<TContext>;
134
- } {
120
+ ): TransitionResult<TContext> {
135
121
  const stateInstance = state || this;
136
122
 
137
123
  return {
@@ -144,12 +130,7 @@ export class State<TContext = unknown> {
144
130
  /**
145
131
  * Create a terminal state reference (for END_ROUTE)
146
132
  */
147
- private createTerminalRef(): StateRef & {
148
- transitionTo: (
149
- spec: TransitionSpec<TContext>,
150
- condition?: string
151
- ) => TransitionResult<TContext>;
152
- } {
133
+ private createTerminalRef(): TransitionResult<TContext> {
153
134
  const terminalRef: StateRef = {
154
135
  id: "END",
155
136
  routeId: this.routeId,
package/src/index.ts CHANGED
@@ -43,6 +43,8 @@ export type {
43
43
  Guideline,
44
44
  Capability,
45
45
  GuidelineMatch,
46
+ ContextLifecycleHooks,
47
+ ContextProvider,
46
48
  } from "./types/agent";
47
49
  export { CompositionMode } from "./types/agent";
48
50
 
@@ -25,6 +25,34 @@ export enum CompositionMode {
25
25
  */
26
26
  import type { ObservationOptions } from "./observation";
27
27
 
28
+ /**
29
+ * Context lifecycle hooks for managing state persistence
30
+ */
31
+ export interface ContextLifecycleHooks<TContext = unknown> {
32
+ /**
33
+ * Called before respond() to get fresh context
34
+ * Useful for loading context from a database or cache
35
+ */
36
+ beforeRespond?: (currentContext: TContext) => Promise<TContext> | TContext;
37
+
38
+ /**
39
+ * Called after context is updated via updateContext() or tool execution
40
+ * Useful for persisting context to a database or cache
41
+ */
42
+ onContextUpdate?: (
43
+ newContext: TContext,
44
+ previousContext: TContext
45
+ ) => Promise<void> | void;
46
+ }
47
+
48
+ /**
49
+ * Context provider function for always-fresh context
50
+ * Alternative to static context, useful for loading from external sources
51
+ */
52
+ export type ContextProvider<TContext = unknown> = () =>
53
+ | Promise<TContext>
54
+ | TContext;
55
+
28
56
  /**
29
57
  * Options for creating an Agent
30
58
  */
@@ -37,6 +65,10 @@ export interface AgentOptions<TContext = unknown> {
37
65
  goal?: string;
38
66
  /** Default context data available to the agent */
39
67
  context?: TContext;
68
+ /** Context provider function for always-fresh context (alternative to static context) */
69
+ contextProvider?: ContextProvider<TContext>;
70
+ /** Lifecycle hooks for context management */
71
+ hooks?: ContextLifecycleHooks<TContext>;
40
72
  /** AI provider strategy for generating responses */
41
73
  ai: AiProvider;
42
74
  /** Maximum number of processing iterations per request */
@@ -58,14 +58,12 @@ export interface TransitionSpec<TContext = unknown> {
58
58
 
59
59
  /**
60
60
  * Result of a transition operation
61
+ * Combines state reference with the ability to chain transitions
61
62
  */
62
- export interface TransitionResult<TContext = unknown> {
63
- /** The target state after transition */
64
- target: StateRef & {
65
- /** Allow chaining transitions */
66
- transitionTo: (
67
- spec: TransitionSpec<TContext>,
68
- condition?: string
69
- ) => TransitionResult<TContext>;
70
- };
63
+ export interface TransitionResult<TContext = unknown> extends StateRef {
64
+ /** Allow chaining transitions */
65
+ transitionTo: (
66
+ spec: TransitionSpec<TContext>,
67
+ condition?: string
68
+ ) => TransitionResult<TContext>;
71
69
  }
package/src/types/tool.ts CHANGED
@@ -10,6 +10,8 @@ import type { Event, StateRef } from "./index";
10
10
  export interface ToolContext<TContext = unknown> {
11
11
  /** The agent's context data */
12
12
  context: TContext;
13
+ /** Update the agent's context (triggers lifecycle hooks if configured) */
14
+ updateContext: (updates: Partial<TContext>) => Promise<void>;
13
15
  /** Current state reference (if in a route) */
14
16
  state?: StateRef;
15
17
  /** Interaction history */
@@ -21,9 +23,11 @@ export interface ToolContext<TContext = unknown> {
21
23
  /**
22
24
  * Result returned by a tool
23
25
  */
24
- export interface ToolResult<TData = unknown> {
26
+ export interface ToolResult<TData = unknown, TContext = unknown> {
25
27
  /** The result data */
26
28
  data: TData;
29
+ /** Optional context update to be merged with current context */
30
+ contextUpdate?: Partial<TContext>;
27
31
  /** Optional metadata about the execution */
28
32
  meta?: Record<string, unknown>;
29
33
  }
@@ -34,7 +38,7 @@ export interface ToolResult<TData = unknown> {
34
38
  export type ToolHandler<TContext, TArgs extends unknown[], TResult> = (
35
39
  context: ToolContext<TContext>,
36
40
  ...args: TArgs
37
- ) => Promise<ToolResult<TResult>> | ToolResult<TResult>;
41
+ ) => Promise<ToolResult<TResult, TContext>> | ToolResult<TResult, TContext>;
38
42
 
39
43
  /**
40
44
  * Reference to a defined tool