@falai/agent 0.9.0-alpha-1 → 0.9.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 +34 -22
- package/dist/cjs/src/core/Agent.d.ts +77 -59
- package/dist/cjs/src/core/Agent.d.ts.map +1 -1
- package/dist/cjs/src/core/Agent.js +284 -1060
- package/dist/cjs/src/core/Agent.js.map +1 -1
- package/dist/cjs/src/core/PersistenceManager.d.ts.map +1 -1
- package/dist/cjs/src/core/PersistenceManager.js +48 -25
- package/dist/cjs/src/core/PersistenceManager.js.map +1 -1
- package/dist/cjs/src/core/PromptComposer.d.ts +1 -1
- package/dist/cjs/src/core/PromptComposer.d.ts.map +1 -1
- package/dist/cjs/src/core/PromptComposer.js.map +1 -1
- package/dist/cjs/src/core/ResponseEngine.d.ts +13 -12
- package/dist/cjs/src/core/ResponseEngine.d.ts.map +1 -1
- package/dist/cjs/src/core/ResponseEngine.js +4 -4
- package/dist/cjs/src/core/ResponseEngine.js.map +1 -1
- package/dist/cjs/src/core/ResponseModal.d.ts +205 -0
- package/dist/cjs/src/core/ResponseModal.d.ts.map +1 -0
- package/dist/cjs/src/core/ResponseModal.js +1328 -0
- package/dist/cjs/src/core/ResponseModal.js.map +1 -0
- package/dist/cjs/src/core/ResponsePipeline.d.ts +66 -38
- package/dist/cjs/src/core/ResponsePipeline.d.ts.map +1 -1
- package/dist/cjs/src/core/ResponsePipeline.js +72 -4
- package/dist/cjs/src/core/ResponsePipeline.js.map +1 -1
- package/dist/cjs/src/core/Route.d.ts +24 -5
- package/dist/cjs/src/core/Route.d.ts.map +1 -1
- package/dist/cjs/src/core/Route.js +45 -1
- package/dist/cjs/src/core/Route.js.map +1 -1
- package/dist/cjs/src/core/RoutingEngine.d.ts +31 -6
- package/dist/cjs/src/core/RoutingEngine.d.ts.map +1 -1
- package/dist/cjs/src/core/RoutingEngine.js +113 -9
- package/dist/cjs/src/core/RoutingEngine.js.map +1 -1
- package/dist/cjs/src/core/SessionManager.d.ts +14 -4
- package/dist/cjs/src/core/SessionManager.d.ts.map +1 -1
- package/dist/cjs/src/core/SessionManager.js +25 -5
- package/dist/cjs/src/core/SessionManager.js.map +1 -1
- package/dist/cjs/src/core/Step.d.ts +10 -10
- package/dist/cjs/src/core/Step.d.ts.map +1 -1
- package/dist/cjs/src/core/Step.js.map +1 -1
- package/dist/cjs/src/core/ToolExecutor.d.ts +4 -2
- package/dist/cjs/src/core/ToolExecutor.d.ts.map +1 -1
- package/dist/cjs/src/core/ToolExecutor.js +13 -3
- package/dist/cjs/src/core/ToolExecutor.js.map +1 -1
- package/dist/cjs/src/index.d.ts +3 -1
- package/dist/cjs/src/index.d.ts.map +1 -1
- package/dist/cjs/src/index.js +7 -1
- package/dist/cjs/src/index.js.map +1 -1
- package/dist/cjs/src/types/agent.d.ts +42 -21
- package/dist/cjs/src/types/agent.d.ts.map +1 -1
- package/dist/cjs/src/types/agent.js.map +1 -1
- package/dist/cjs/src/types/ai.d.ts +1 -1
- package/dist/cjs/src/types/ai.d.ts.map +1 -1
- package/dist/cjs/src/types/index.d.ts +1 -1
- package/dist/cjs/src/types/index.d.ts.map +1 -1
- package/dist/cjs/src/types/index.js.map +1 -1
- package/dist/cjs/src/types/persistence.d.ts +0 -1
- package/dist/cjs/src/types/persistence.d.ts.map +1 -1
- package/dist/cjs/src/types/route.d.ts +22 -16
- package/dist/cjs/src/types/route.d.ts.map +1 -1
- package/dist/cjs/src/types/session.d.ts +6 -11
- package/dist/cjs/src/types/session.d.ts.map +1 -1
- package/dist/cjs/src/types/tool.d.ts +12 -6
- package/dist/cjs/src/types/tool.d.ts.map +1 -1
- package/dist/cjs/src/utils/clone.d.ts.map +1 -1
- package/dist/cjs/src/utils/clone.js +0 -4
- package/dist/cjs/src/utils/clone.js.map +1 -1
- package/dist/cjs/src/utils/history.d.ts +30 -1
- package/dist/cjs/src/utils/history.d.ts.map +1 -1
- package/dist/cjs/src/utils/history.js +169 -23
- package/dist/cjs/src/utils/history.js.map +1 -1
- package/dist/cjs/src/utils/index.d.ts +1 -1
- package/dist/cjs/src/utils/index.d.ts.map +1 -1
- package/dist/cjs/src/utils/index.js +5 -1
- package/dist/cjs/src/utils/index.js.map +1 -1
- package/dist/cjs/src/utils/session.d.ts +2 -2
- package/dist/cjs/src/utils/session.d.ts.map +1 -1
- package/dist/cjs/src/utils/session.js +6 -26
- package/dist/cjs/src/utils/session.js.map +1 -1
- package/dist/src/core/Agent.d.ts +77 -59
- package/dist/src/core/Agent.d.ts.map +1 -1
- package/dist/src/core/Agent.js +285 -1061
- package/dist/src/core/Agent.js.map +1 -1
- package/dist/src/core/PersistenceManager.d.ts.map +1 -1
- package/dist/src/core/PersistenceManager.js +48 -25
- package/dist/src/core/PersistenceManager.js.map +1 -1
- package/dist/src/core/PromptComposer.d.ts +1 -1
- package/dist/src/core/PromptComposer.d.ts.map +1 -1
- package/dist/src/core/PromptComposer.js.map +1 -1
- package/dist/src/core/ResponseEngine.d.ts +13 -12
- package/dist/src/core/ResponseEngine.d.ts.map +1 -1
- package/dist/src/core/ResponseEngine.js +4 -4
- package/dist/src/core/ResponseEngine.js.map +1 -1
- package/dist/src/core/ResponseModal.d.ts +205 -0
- package/dist/src/core/ResponseModal.d.ts.map +1 -0
- package/dist/src/core/ResponseModal.js +1323 -0
- package/dist/src/core/ResponseModal.js.map +1 -0
- package/dist/src/core/ResponsePipeline.d.ts +66 -38
- package/dist/src/core/ResponsePipeline.d.ts.map +1 -1
- package/dist/src/core/ResponsePipeline.js +72 -4
- package/dist/src/core/ResponsePipeline.js.map +1 -1
- package/dist/src/core/Route.d.ts +24 -5
- package/dist/src/core/Route.d.ts.map +1 -1
- package/dist/src/core/Route.js +45 -1
- package/dist/src/core/Route.js.map +1 -1
- package/dist/src/core/RoutingEngine.d.ts +31 -6
- package/dist/src/core/RoutingEngine.d.ts.map +1 -1
- package/dist/src/core/RoutingEngine.js +113 -9
- package/dist/src/core/RoutingEngine.js.map +1 -1
- package/dist/src/core/SessionManager.d.ts +14 -4
- package/dist/src/core/SessionManager.d.ts.map +1 -1
- package/dist/src/core/SessionManager.js +25 -5
- package/dist/src/core/SessionManager.js.map +1 -1
- package/dist/src/core/Step.d.ts +10 -10
- package/dist/src/core/Step.d.ts.map +1 -1
- package/dist/src/core/Step.js.map +1 -1
- package/dist/src/core/ToolExecutor.d.ts +4 -2
- package/dist/src/core/ToolExecutor.d.ts.map +1 -1
- package/dist/src/core/ToolExecutor.js +13 -3
- package/dist/src/core/ToolExecutor.js.map +1 -1
- package/dist/src/index.d.ts +3 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/types/agent.d.ts +42 -21
- package/dist/src/types/agent.d.ts.map +1 -1
- package/dist/src/types/agent.js.map +1 -1
- package/dist/src/types/ai.d.ts +1 -1
- package/dist/src/types/ai.d.ts.map +1 -1
- package/dist/src/types/index.d.ts +1 -1
- package/dist/src/types/index.d.ts.map +1 -1
- package/dist/src/types/index.js.map +1 -1
- package/dist/src/types/persistence.d.ts +0 -1
- package/dist/src/types/persistence.d.ts.map +1 -1
- package/dist/src/types/route.d.ts +22 -16
- package/dist/src/types/route.d.ts.map +1 -1
- package/dist/src/types/session.d.ts +6 -11
- package/dist/src/types/session.d.ts.map +1 -1
- package/dist/src/types/tool.d.ts +12 -6
- package/dist/src/types/tool.d.ts.map +1 -1
- package/dist/src/utils/clone.d.ts.map +1 -1
- package/dist/src/utils/clone.js +0 -4
- package/dist/src/utils/clone.js.map +1 -1
- package/dist/src/utils/history.d.ts +30 -1
- package/dist/src/utils/history.d.ts.map +1 -1
- package/dist/src/utils/history.js +165 -23
- package/dist/src/utils/history.js.map +1 -1
- package/dist/src/utils/index.d.ts +1 -1
- package/dist/src/utils/index.d.ts.map +1 -1
- package/dist/src/utils/index.js +1 -1
- package/dist/src/utils/index.js.map +1 -1
- package/dist/src/utils/session.d.ts +2 -2
- package/dist/src/utils/session.d.ts.map +1 -1
- package/dist/src/utils/session.js +6 -26
- package/dist/src/utils/session.js.map +1 -1
- package/docs/README.md +5 -4
- package/docs/api/README.md +195 -4
- package/docs/api/overview.md +232 -13
- package/docs/core/agent/README.md +162 -17
- package/docs/core/agent/context-management.md +39 -15
- package/docs/core/agent/session-management.md +49 -16
- package/docs/core/ai-integration/prompt-composition.md +38 -14
- package/docs/core/ai-integration/response-processing.md +28 -17
- package/docs/core/conversation-flows/data-collection.md +103 -25
- package/docs/core/conversation-flows/route-dsl.md +45 -22
- package/docs/core/conversation-flows/routes.md +74 -18
- package/docs/core/conversation-flows/step-transitions.md +3 -3
- package/docs/core/conversation-flows/steps.md +39 -15
- package/docs/core/routing/intelligent-routing.md +18 -9
- package/docs/core/tools/tool-definition.md +8 -8
- package/docs/core/tools/tool-execution.md +26 -26
- package/docs/core/tools/tool-scoping.md +5 -5
- package/docs/guides/getting-started/README.md +54 -32
- package/docs/guides/migration/README.md +72 -0
- package/docs/guides/migration/response-modal-refactor.md +518 -0
- package/examples/advanced-patterns/knowledge-based-agent.ts +37 -28
- package/examples/advanced-patterns/persistent-onboarding.ts +70 -41
- package/examples/advanced-patterns/route-lifecycle-hooks.ts +28 -2
- package/examples/advanced-patterns/streaming-responses.ts +197 -119
- package/examples/ai-providers/anthropic-integration.ts +40 -33
- package/examples/ai-providers/openai-integration.ts +25 -25
- package/examples/conversation-flows/completion-transitions.ts +36 -32
- package/examples/core-concepts/basic-agent.ts +76 -78
- package/examples/core-concepts/modern-streaming-api.ts +309 -0
- package/examples/core-concepts/schema-driven-extraction.ts +20 -16
- package/examples/core-concepts/session-management.ts +65 -53
- package/examples/integrations/database-integration.ts +49 -34
- package/examples/integrations/healthcare-integration.ts +96 -91
- package/examples/integrations/search-integration.ts +79 -82
- package/examples/integrations/server-session-management.ts +25 -17
- package/examples/persistence/database-persistence.ts +61 -45
- package/examples/persistence/memory-sessions.ts +52 -63
- package/examples/persistence/redis-persistence.ts +81 -95
- package/examples/tools/basic-tools.ts +73 -62
- package/examples/tools/data-enrichment-tools.ts +52 -44
- package/package.json +1 -1
- package/src/core/Agent.ts +396 -1499
- package/src/core/PersistenceManager.ts +51 -27
- package/src/core/PromptComposer.ts +1 -1
- package/src/core/ResponseEngine.ts +21 -19
- package/src/core/ResponseModal.ts +1722 -0
- package/src/core/ResponsePipeline.ts +175 -60
- package/src/core/Route.ts +58 -6
- package/src/core/RoutingEngine.ts +174 -27
- package/src/core/SessionManager.ts +32 -8
- package/src/core/Step.ts +20 -12
- package/src/core/ToolExecutor.ts +19 -5
- package/src/index.ts +11 -0
- package/src/types/agent.ts +47 -23
- package/src/types/ai.ts +1 -1
- package/src/types/index.ts +2 -0
- package/src/types/persistence.ts +0 -1
- package/src/types/route.ts +22 -16
- package/src/types/session.ts +6 -12
- package/src/types/tool.ts +15 -9
- package/src/utils/clone.ts +6 -8
- package/src/utils/history.ts +190 -27
- package/src/utils/index.ts +4 -0
- package/src/utils/session.ts +6 -31
package/src/types/agent.ts
CHANGED
|
@@ -7,6 +7,7 @@ import type { Tool } from "./tool";
|
|
|
7
7
|
import type { RouteOptions } from "./route";
|
|
8
8
|
import type { PersistenceConfig } from "./persistence";
|
|
9
9
|
import type { SessionState } from "./session";
|
|
10
|
+
import type { StructuredSchema } from "./schema";
|
|
10
11
|
import { Template } from "./template";
|
|
11
12
|
|
|
12
13
|
/**
|
|
@@ -26,7 +27,7 @@ export enum CompositionMode {
|
|
|
26
27
|
/**
|
|
27
28
|
* Context lifecycle hooks for managing step persistence
|
|
28
29
|
*/
|
|
29
|
-
export interface ContextLifecycleHooks<TContext = unknown> {
|
|
30
|
+
export interface ContextLifecycleHooks<TContext = unknown, TData = unknown> {
|
|
30
31
|
/**
|
|
31
32
|
* Called before respond() to get fresh context
|
|
32
33
|
* Useful for loading context from a database or cache
|
|
@@ -47,13 +48,12 @@ export interface ContextLifecycleHooks<TContext = unknown> {
|
|
|
47
48
|
* Useful for validation, enrichment, or persistence of collected data
|
|
48
49
|
* Return modified collected data or the same data to keep it unchanged
|
|
49
50
|
*
|
|
50
|
-
* Note: This hook works with
|
|
51
|
-
* multiple routes with different extraction schemas). Use type guards or runtime
|
|
52
|
-
* checks if you need type-specific logic.
|
|
51
|
+
* Note: This hook now works with agent-level data collection (TData type)
|
|
53
52
|
*/
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
53
|
+
onDataUpdate?: (
|
|
54
|
+
data: Partial<TData>,
|
|
55
|
+
previousCollected: Partial<TData>
|
|
56
|
+
) => Partial<TData> | Promise<Partial<TData>>;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
/**
|
|
@@ -67,7 +67,7 @@ export type ContextProvider<TContext = unknown> = () =>
|
|
|
67
67
|
/**
|
|
68
68
|
* Options for creating an Agent
|
|
69
69
|
*/
|
|
70
|
-
export interface AgentOptions<TContext = unknown> {
|
|
70
|
+
export interface AgentOptions<TContext = unknown, TData = unknown> {
|
|
71
71
|
/** Display name of the agent */
|
|
72
72
|
name: string;
|
|
73
73
|
/** Detailed description of the agent's purpose and personality */
|
|
@@ -89,47 +89,51 @@ export interface AgentOptions<TContext = unknown> {
|
|
|
89
89
|
/** Context provider function for always-fresh context (alternative to static context) */
|
|
90
90
|
contextProvider?: ContextProvider<TContext>;
|
|
91
91
|
/** Lifecycle hooks for context management */
|
|
92
|
-
hooks?: ContextLifecycleHooks<TContext>;
|
|
92
|
+
hooks?: ContextLifecycleHooks<TContext, TData>;
|
|
93
93
|
/** AI provider strategy for generating responses */
|
|
94
94
|
provider: AiProvider;
|
|
95
95
|
/** Composition mode for response generation */
|
|
96
96
|
compositionMode?: CompositionMode;
|
|
97
97
|
/** Initial terms for domain glossary */
|
|
98
|
-
terms?: Term<TContext>[];
|
|
98
|
+
terms?: Term<TContext, TData>[];
|
|
99
99
|
/** Initial guidelines for agent behavior */
|
|
100
|
-
guidelines?: Guideline<TContext>[];
|
|
100
|
+
guidelines?: Guideline<TContext, TData>[];
|
|
101
101
|
/** Global tools available to all routes */
|
|
102
|
-
tools?: Tool<TContext, unknown[], unknown
|
|
102
|
+
tools?: Tool<TContext, TData, unknown[], unknown>[];
|
|
103
103
|
/** Initial routes (will be instantiated as Route objects) */
|
|
104
|
-
routes?: RouteOptions<TContext,
|
|
104
|
+
routes?: RouteOptions<TContext, TData>[];
|
|
105
105
|
/** Optional persistence configuration for auto-saving sessions and messages */
|
|
106
|
-
persistence?: PersistenceConfig
|
|
106
|
+
persistence?: PersistenceConfig<TData>;
|
|
107
107
|
/** Knowledge base containing any JSON structure the AI should know */
|
|
108
108
|
knowledgeBase?: Record<string, unknown>;
|
|
109
|
+
/** Agent-level data schema defining the complete data structure for collection */
|
|
110
|
+
schema?: StructuredSchema;
|
|
111
|
+
/** Initial data to pre-populate when creating the agent */
|
|
112
|
+
initialData?: Partial<TData>;
|
|
109
113
|
}
|
|
110
114
|
|
|
111
115
|
/**
|
|
112
116
|
* A term in the domain glossary
|
|
113
117
|
*/
|
|
114
|
-
export interface Term<TContext = unknown> {
|
|
118
|
+
export interface Term<TContext = unknown, TData = unknown> {
|
|
115
119
|
/** Name of the term */
|
|
116
|
-
name: Template<TContext>;
|
|
120
|
+
name: Template<TContext, TData>;
|
|
117
121
|
/** Description/definition of the term */
|
|
118
|
-
description: Template<TContext>;
|
|
122
|
+
description: Template<TContext, TData>;
|
|
119
123
|
/** Alternative names or synonyms */
|
|
120
|
-
synonyms?: Template<TContext>[];
|
|
124
|
+
synonyms?: Template<TContext, TData>[];
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
/**
|
|
124
128
|
* A behavioral guideline for the agent
|
|
125
129
|
*/
|
|
126
|
-
export interface Guideline<TContext = unknown> {
|
|
130
|
+
export interface Guideline<TContext = unknown, TData = unknown> {
|
|
127
131
|
/** Unique identifier */
|
|
128
132
|
id?: string;
|
|
129
133
|
/** Condition that triggers this guideline (optional for always-active guidelines) */
|
|
130
|
-
condition?: Template<TContext>;
|
|
134
|
+
condition?: Template<TContext, TData>;
|
|
131
135
|
/** Action the agent should take when the condition is met */
|
|
132
|
-
action: Template<TContext>;
|
|
136
|
+
action: Template<TContext, TData>;
|
|
133
137
|
/** Whether this guideline is currently enabled */
|
|
134
138
|
enabled?: boolean;
|
|
135
139
|
/** Tags for organizing and filtering guidelines */
|
|
@@ -141,9 +145,9 @@ export interface Guideline<TContext = unknown> {
|
|
|
141
145
|
/**
|
|
142
146
|
* Guideline match with rationale
|
|
143
147
|
*/
|
|
144
|
-
export interface GuidelineMatch<TContext = unknown> {
|
|
148
|
+
export interface GuidelineMatch<TContext = unknown, TData = unknown> {
|
|
145
149
|
/** The matched guideline */
|
|
146
|
-
guideline: Guideline<TContext>;
|
|
150
|
+
guideline: Guideline<TContext, TData>;
|
|
147
151
|
/** Explanation of why this guideline was matched */
|
|
148
152
|
rationale?: string;
|
|
149
153
|
}
|
|
@@ -169,4 +173,24 @@ export interface AgentResponseStreamChunk<TData = Record<string, unknown>> {
|
|
|
169
173
|
[key: string]: unknown;
|
|
170
174
|
};
|
|
171
175
|
structured?: AgentStructuredResponse;
|
|
176
|
+
error?: Error;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Validation error for data validation
|
|
181
|
+
*/
|
|
182
|
+
export interface ValidationError {
|
|
183
|
+
field: string;
|
|
184
|
+
value: unknown;
|
|
185
|
+
message: string;
|
|
186
|
+
schemaPath: string;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Result of data validation
|
|
191
|
+
*/
|
|
192
|
+
export interface ValidationResult {
|
|
193
|
+
valid: boolean;
|
|
194
|
+
errors: ValidationError[];
|
|
195
|
+
warnings: ValidationError[];
|
|
172
196
|
}
|
package/src/types/ai.ts
CHANGED
|
@@ -66,7 +66,7 @@ export interface GenerateMessageInput<TContext = unknown> {
|
|
|
66
66
|
/**
|
|
67
67
|
* Structured response from AI containing message and metadata
|
|
68
68
|
*/
|
|
69
|
-
export interface AgentStructuredResponse {
|
|
69
|
+
export interface AgentStructuredResponse extends Record<string, unknown> {
|
|
70
70
|
/** The actual message to send to the user */
|
|
71
71
|
message: string;
|
|
72
72
|
/** Route chosen by the agent (route title or null if no route) */
|
package/src/types/index.ts
CHANGED
package/src/types/persistence.ts
CHANGED
|
@@ -45,7 +45,6 @@ export type CreateSessionData<TData = Record<string, unknown>> = Omit<
|
|
|
45
45
|
*/
|
|
46
46
|
export interface CollectedStateData<TData = Record<string, unknown>> {
|
|
47
47
|
data: Partial<TData>;
|
|
48
|
-
dataByRoute: Record<string, Partial<TData>>;
|
|
49
48
|
routeHistory: SessionState<TData>["routeHistory"];
|
|
50
49
|
history?: SessionState<TData>["history"];
|
|
51
50
|
currentRouteTitle?: string;
|
package/src/types/route.ts
CHANGED
|
@@ -104,7 +104,7 @@ export interface RouteOptions<TContext = unknown, TData = unknown> {
|
|
|
104
104
|
/** Initial terms for the route's domain glossary */
|
|
105
105
|
terms?: Term<TContext>[];
|
|
106
106
|
/** Tools available in this route */
|
|
107
|
-
tools?: Tool<TContext, unknown[], unknown
|
|
107
|
+
tools?: Tool<TContext, TData, unknown[], unknown>[];
|
|
108
108
|
/** Absolute rules the agent must follow in this route */
|
|
109
109
|
rules?: Template<TContext, TData>[];
|
|
110
110
|
/** Absolute prohibitions the agent must never do in this route */
|
|
@@ -114,14 +114,20 @@ export interface RouteOptions<TContext = unknown, TData = unknown> {
|
|
|
114
114
|
/** Optional: structured response data for this route's message generation */
|
|
115
115
|
responseOutputSchema?: StructuredSchema;
|
|
116
116
|
/**
|
|
117
|
-
*
|
|
118
|
-
*
|
|
117
|
+
* Required fields for route completion - must be valid keys from agent's TData type
|
|
118
|
+
* Route is considered complete when all required fields are present in agent data
|
|
119
119
|
*/
|
|
120
|
-
|
|
120
|
+
requiredFields?: (keyof TData)[];
|
|
121
121
|
/**
|
|
122
|
-
*
|
|
122
|
+
* Optional fields that enhance the route but aren't required for completion
|
|
123
|
+
* Must be valid keys from agent's TData type
|
|
124
|
+
*/
|
|
125
|
+
optionalFields?: (keyof TData)[];
|
|
126
|
+
/**
|
|
127
|
+
* Initial data to pre-populate when entering this route
|
|
123
128
|
* Useful for restoring sessions or pre-filling known information
|
|
124
129
|
* Steps with skipIf conditions will be automatically bypassed if data is present
|
|
130
|
+
* Now refers to agent-level data
|
|
125
131
|
*/
|
|
126
132
|
initialData?: Partial<TData>;
|
|
127
133
|
/**
|
|
@@ -191,37 +197,37 @@ export interface StepOptions<TContext = unknown, TData = unknown> {
|
|
|
191
197
|
/** Transition to a chat state with this description */
|
|
192
198
|
prompt?: Template<TContext, TData>;
|
|
193
199
|
/** Tools available for AI to call in this step (by ID reference or inline definition) */
|
|
194
|
-
tools?: (string | Tool<TContext, unknown[], unknown
|
|
200
|
+
tools?: (string | Tool<TContext, TData, unknown[], unknown>)[];
|
|
195
201
|
/** Programmatic function or tool to run before AI responds */
|
|
196
202
|
prepare?:
|
|
197
203
|
| string
|
|
198
|
-
| Tool<TContext, unknown[], unknown
|
|
204
|
+
| Tool<TContext, TData, unknown[], unknown>
|
|
199
205
|
| ((context: TContext, data?: Partial<TData>) => void | Promise<void>);
|
|
200
206
|
/** Programmatic function or tool to run after AI responds */
|
|
201
207
|
finalize?:
|
|
202
208
|
| string
|
|
203
|
-
| Tool<TContext, unknown[], unknown
|
|
209
|
+
| Tool<TContext, TData, unknown[], unknown>
|
|
204
210
|
| ((context: TContext, data?: Partial<TData>) => void | Promise<void>);
|
|
205
211
|
/** Transition to a specific step or end marker */
|
|
206
212
|
step?: StepRef | symbol;
|
|
207
213
|
/**
|
|
208
|
-
*
|
|
209
|
-
* These should match keys in the
|
|
214
|
+
* Fields to collect from the conversation in this step
|
|
215
|
+
* These should match keys in the agent's TData schema
|
|
210
216
|
*/
|
|
211
|
-
collect?:
|
|
217
|
+
collect?: (keyof TData)[];
|
|
212
218
|
/**
|
|
213
|
-
*
|
|
219
|
+
* Function to determine if this step should be skipped
|
|
214
220
|
* If returns true, the step will be bypassed
|
|
215
|
-
* @param data - Currently collected data
|
|
221
|
+
* @param data - Currently collected agent-level data
|
|
216
222
|
* @returns true if step should be skipped, false otherwise
|
|
217
223
|
*/
|
|
218
224
|
skipIf?: (data: Partial<TData>) => boolean;
|
|
219
225
|
/**
|
|
220
|
-
*
|
|
226
|
+
* Required data fields that must be present before entering this step
|
|
221
227
|
* If any required field is missing, step cannot be entered
|
|
222
|
-
*
|
|
228
|
+
* Must be valid keys from agent's TData type
|
|
223
229
|
*/
|
|
224
|
-
requires?:
|
|
230
|
+
requires?: (keyof TData)[];
|
|
225
231
|
/**
|
|
226
232
|
* Optional condition for this transition
|
|
227
233
|
* Description of when this transition should be taken
|
package/src/types/session.ts
CHANGED
|
@@ -17,8 +17,8 @@ export interface PendingTransition {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
|
-
* Session
|
|
21
|
-
* and data collected
|
|
20
|
+
* Session state tracks the current position in the conversation flow
|
|
21
|
+
* and data collected at the agent level across all routes
|
|
22
22
|
*/
|
|
23
23
|
export interface SessionState<TData = unknown> {
|
|
24
24
|
/** Unique session identifier (useful for persistence) */
|
|
@@ -39,17 +39,11 @@ export interface SessionState<TData = unknown> {
|
|
|
39
39
|
};
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
|
-
*
|
|
43
|
-
*
|
|
42
|
+
* Agent-level data collected across all routes
|
|
43
|
+
* This is the single source of truth for all collected data
|
|
44
|
+
* Routes can access and contribute to this shared data structure
|
|
44
45
|
*/
|
|
45
|
-
data
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Data collected organized by route ID
|
|
49
|
-
* Persists data when switching between routes
|
|
50
|
-
* Allows resuming incomplete routes where they left off
|
|
51
|
-
*/
|
|
52
|
-
dataByRoute?: Record<string, Partial<TData>>;
|
|
46
|
+
data: Partial<TData>;
|
|
53
47
|
|
|
54
48
|
/** History of routes visited in this session */
|
|
55
49
|
routeHistory?: Array<{
|
package/src/types/tool.ts
CHANGED
|
@@ -12,11 +12,13 @@ export interface ToolContext<TContext = unknown, TData = unknown> {
|
|
|
12
12
|
context: TContext;
|
|
13
13
|
/** Update the agent's context (triggers lifecycle hooks if configured) */
|
|
14
14
|
updateContext: (updates: Partial<TContext>) => Promise<void>;
|
|
15
|
+
/** Update the agent's collected data (triggers lifecycle hooks if configured) */
|
|
16
|
+
updateData: (updates: Partial<TData>) => Promise<void>;
|
|
15
17
|
/** Current step reference (if in a route) */
|
|
16
18
|
step?: StepRef;
|
|
17
19
|
/** Interaction history */
|
|
18
20
|
history: Event[];
|
|
19
|
-
/**
|
|
21
|
+
/** Complete agent-level data collected so far */
|
|
20
22
|
data?: Partial<TData>;
|
|
21
23
|
/** Additional metadata */
|
|
22
24
|
metadata?: Record<string, unknown>;
|
|
@@ -31,11 +33,15 @@ export interface ToolResult<
|
|
|
31
33
|
TData = unknown
|
|
32
34
|
> {
|
|
33
35
|
/** The result data */
|
|
34
|
-
data
|
|
36
|
+
data?: TResultData;
|
|
35
37
|
/** Optional context update to be merged with current context */
|
|
36
38
|
contextUpdate?: Partial<TContext>;
|
|
37
|
-
/** Optional
|
|
39
|
+
/** Optional agent-level data update to be merged with collected data */
|
|
38
40
|
dataUpdate?: Partial<TData>;
|
|
41
|
+
/** Success indicator */
|
|
42
|
+
success?: boolean;
|
|
43
|
+
/** Error message if operation failed */
|
|
44
|
+
error?: string;
|
|
39
45
|
/** Optional metadata about the execution */
|
|
40
46
|
meta?: Record<string, unknown>;
|
|
41
47
|
}
|
|
@@ -45,9 +51,9 @@ export interface ToolResult<
|
|
|
45
51
|
*/
|
|
46
52
|
export type ToolHandler<
|
|
47
53
|
TContext,
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
54
|
+
TData = unknown,
|
|
55
|
+
TArgs extends unknown[] = unknown[],
|
|
56
|
+
TResult = unknown
|
|
51
57
|
> = (
|
|
52
58
|
context: ToolContext<TContext, TData>,
|
|
53
59
|
...args: TArgs
|
|
@@ -60,16 +66,16 @@ export type ToolHandler<
|
|
|
60
66
|
*/
|
|
61
67
|
export interface Tool<
|
|
62
68
|
TContext = unknown,
|
|
69
|
+
TData = unknown,
|
|
63
70
|
TArgs extends unknown[] = unknown[],
|
|
64
|
-
TResult = unknown
|
|
65
|
-
TData = unknown
|
|
71
|
+
TResult = unknown
|
|
66
72
|
> {
|
|
67
73
|
/** Tool identifier */
|
|
68
74
|
id: string;
|
|
69
75
|
/** Tool display name (shown to AI models) */
|
|
70
76
|
name?: string;
|
|
71
77
|
/** Tool handler function */
|
|
72
|
-
handler: ToolHandler<TContext, TArgs, TResult
|
|
78
|
+
handler: ToolHandler<TContext, TData, TArgs, TResult>;
|
|
73
79
|
/** Description of what the tool does (for AI discovery) */
|
|
74
80
|
description?: string;
|
|
75
81
|
/** Parameter schema or description */
|
package/src/utils/clone.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
|
|
2
2
|
/**
|
|
3
3
|
* Deep clone utility to avoid adding large dependencies like lodash.
|
|
4
4
|
* Handles objects, arrays, dates, and primitives.
|
|
@@ -11,21 +11,19 @@ export function cloneDeep<T>(obj: T): T {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
if (obj instanceof Date) {
|
|
14
|
-
|
|
15
|
-
return new Date(obj.getTime()) as any;
|
|
14
|
+
return new Date(obj.getTime()) as T;
|
|
16
15
|
}
|
|
17
16
|
|
|
18
17
|
if (Array.isArray(obj)) {
|
|
19
|
-
const arrCopy
|
|
18
|
+
const arrCopy: unknown[] = [];
|
|
20
19
|
for (let i = 0; i < obj.length; i++) {
|
|
21
|
-
|
|
20
|
+
|
|
22
21
|
arrCopy[i] = cloneDeep(obj[i]);
|
|
23
22
|
}
|
|
24
|
-
|
|
25
|
-
return arrCopy as any;
|
|
23
|
+
return arrCopy as T;
|
|
26
24
|
}
|
|
27
25
|
|
|
28
|
-
const objCopy
|
|
26
|
+
const objCopy: Record<string, unknown> = {};
|
|
29
27
|
for (const key in obj) {
|
|
30
28
|
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
31
29
|
objCopy[key] = cloneDeep((obj as Record<string, unknown>)[key]);
|