@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
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* and integration with conversation flows.
|
|
7
7
|
*
|
|
8
8
|
* Key concepts:
|
|
9
|
-
* - Tool definition with Tool<TContext, TArgs, TResult
|
|
9
|
+
* - Tool definition with Tool<TContext, TData, TArgs, TResult> interface
|
|
10
10
|
* - Tool context and parameters
|
|
11
11
|
* - Tool execution in conversation flows
|
|
12
12
|
* - Error handling in tools
|
|
@@ -36,7 +36,7 @@ interface SearchData {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
// Example 1: Simple Calculator Tool
|
|
39
|
-
const calculatorTool: Tool<unknown, [], string
|
|
39
|
+
const calculatorTool: Tool<unknown, UnifiedToolData, [], string> = {
|
|
40
40
|
id: "calculator",
|
|
41
41
|
name: "Math Calculator",
|
|
42
42
|
description: "Evaluate mathematical expressions and return results",
|
|
@@ -51,8 +51,7 @@ const calculatorTool: Tool<unknown, [], string, CalculatorData> = {
|
|
|
51
51
|
required: ["expression"],
|
|
52
52
|
},
|
|
53
53
|
handler: ({ data }) => {
|
|
54
|
-
|
|
55
|
-
if (!calcData?.expression) {
|
|
54
|
+
if (!data?.expression) {
|
|
56
55
|
throw new Error("No expression provided");
|
|
57
56
|
}
|
|
58
57
|
|
|
@@ -60,22 +59,22 @@ const calculatorTool: Tool<unknown, [], string, CalculatorData> = {
|
|
|
60
59
|
// Simple expression evaluation (in production, use a safe math library)
|
|
61
60
|
// WARNING: eval is unsafe - use a proper math evaluation library in production
|
|
62
61
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
63
|
-
const result = eval(
|
|
62
|
+
const result = eval(data.expression);
|
|
64
63
|
|
|
65
64
|
if (typeof result !== "number" || isNaN(result)) {
|
|
66
65
|
throw new Error("Invalid calculation result");
|
|
67
66
|
}
|
|
68
67
|
|
|
69
68
|
return {
|
|
70
|
-
data: `The result of ${
|
|
69
|
+
data: `The result of ${data.expression} is ${result}`,
|
|
71
70
|
dataUpdate: {
|
|
72
71
|
result,
|
|
73
|
-
operation:
|
|
72
|
+
operation: data.expression,
|
|
74
73
|
},
|
|
75
74
|
};
|
|
76
75
|
} catch (error) {
|
|
77
76
|
throw new Error(
|
|
78
|
-
`Error calculating ${
|
|
77
|
+
`Error calculating ${data.expression}: ${
|
|
79
78
|
error instanceof Error ? error.message : "Unknown error"
|
|
80
79
|
}`
|
|
81
80
|
);
|
|
@@ -84,7 +83,7 @@ const calculatorTool: Tool<unknown, [], string, CalculatorData> = {
|
|
|
84
83
|
};
|
|
85
84
|
|
|
86
85
|
// Example 2: Weather Tool with External API Simulation
|
|
87
|
-
const weatherTool: Tool<unknown, [], string
|
|
86
|
+
const weatherTool: Tool<unknown, UnifiedToolData, [], string> = {
|
|
88
87
|
id: "get_weather",
|
|
89
88
|
name: "Weather Lookup",
|
|
90
89
|
description: "Get current weather and forecast for a location",
|
|
@@ -96,8 +95,7 @@ const weatherTool: Tool<unknown, [], string, WeatherData> = {
|
|
|
96
95
|
required: ["location"],
|
|
97
96
|
},
|
|
98
97
|
handler: async ({ data }) => {
|
|
99
|
-
|
|
100
|
-
if (!weatherData?.location) {
|
|
98
|
+
if (!data?.location) {
|
|
101
99
|
throw new Error("No location provided");
|
|
102
100
|
}
|
|
103
101
|
|
|
@@ -127,14 +125,14 @@ const weatherTool: Tool<unknown, [], string, WeatherData> = {
|
|
|
127
125
|
},
|
|
128
126
|
};
|
|
129
127
|
|
|
130
|
-
const weather = mockWeather[
|
|
128
|
+
const weather = mockWeather[data.location] || {
|
|
131
129
|
temp: 70,
|
|
132
130
|
condition: "Unknown",
|
|
133
131
|
forecast: "Weather data unavailable",
|
|
134
132
|
};
|
|
135
133
|
|
|
136
134
|
return {
|
|
137
|
-
data: `Weather in ${
|
|
135
|
+
data: `Weather in ${data.location}: ${weather.temp}°F and ${weather.condition}. ${weather.forecast}`,
|
|
138
136
|
dataUpdate: {
|
|
139
137
|
temperature: weather.temp,
|
|
140
138
|
condition: weather.condition,
|
|
@@ -145,7 +143,7 @@ const weatherTool: Tool<unknown, [], string, WeatherData> = {
|
|
|
145
143
|
};
|
|
146
144
|
|
|
147
145
|
// Example 3: Search Tool with Multiple Results
|
|
148
|
-
const searchTool: Tool<unknown, [], string
|
|
146
|
+
const searchTool: Tool<unknown, UnifiedToolData, [], string> = {
|
|
149
147
|
id: "web_search",
|
|
150
148
|
name: "Web Search",
|
|
151
149
|
description: "Search the web for information on a given query",
|
|
@@ -157,8 +155,7 @@ const searchTool: Tool<unknown, [], string, SearchData> = {
|
|
|
157
155
|
required: ["query"],
|
|
158
156
|
},
|
|
159
157
|
handler: async ({ data }) => {
|
|
160
|
-
|
|
161
|
-
if (!searchData?.query) {
|
|
158
|
+
if (!data?.query) {
|
|
162
159
|
throw new Error("No search query provided");
|
|
163
160
|
}
|
|
164
161
|
|
|
@@ -187,15 +184,15 @@ const searchTool: Tool<unknown, [], string, SearchData> = {
|
|
|
187
184
|
],
|
|
188
185
|
};
|
|
189
186
|
|
|
190
|
-
const results = mockResults[
|
|
191
|
-
`Search results for "${
|
|
187
|
+
const results = mockResults[data.query.toLowerCase()] || [
|
|
188
|
+
`Search results for "${data.query}"`,
|
|
192
189
|
"This is a simulated search result",
|
|
193
190
|
"In a real implementation, this would connect to a search API",
|
|
194
191
|
"Such as Google Custom Search, Bing Web Search, or Elasticsearch",
|
|
195
192
|
];
|
|
196
193
|
|
|
197
194
|
return {
|
|
198
|
-
data: `Search results for "${
|
|
195
|
+
data: `Search results for "${data.query}":\n${results
|
|
199
196
|
.map((r, i) => `${i + 1}. ${r}`)
|
|
200
197
|
.join("\n")}`,
|
|
201
198
|
dataUpdate: {
|
|
@@ -209,9 +206,9 @@ const searchTool: Tool<unknown, [], string, SearchData> = {
|
|
|
209
206
|
// Example 4: Tool that Modifies Context (Advanced)
|
|
210
207
|
const updatePreferencesTool: Tool<
|
|
211
208
|
{ preferences?: { theme: string; language: string } },
|
|
209
|
+
UnifiedToolData,
|
|
212
210
|
[],
|
|
213
|
-
string
|
|
214
|
-
{ theme?: string; language?: string }
|
|
211
|
+
string
|
|
215
212
|
> = {
|
|
216
213
|
id: "update_preferences",
|
|
217
214
|
name: "Update Preferences",
|
|
@@ -242,29 +239,54 @@ const updatePreferencesTool: Tool<
|
|
|
242
239
|
},
|
|
243
240
|
};
|
|
244
241
|
|
|
242
|
+
// Define unified data schema for all tool interactions
|
|
243
|
+
interface UnifiedToolData extends CalculatorData, WeatherData, SearchData {
|
|
244
|
+
theme?: string;
|
|
245
|
+
language?: string;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const unifiedToolSchema = {
|
|
249
|
+
type: "object",
|
|
250
|
+
properties: {
|
|
251
|
+
// Calculator fields
|
|
252
|
+
expression: { type: "string" },
|
|
253
|
+
result: { type: "number" },
|
|
254
|
+
operation: { type: "string" },
|
|
255
|
+
// Weather fields
|
|
256
|
+
location: { type: "string" },
|
|
257
|
+
temperature: { type: "number" },
|
|
258
|
+
condition: { type: "string" },
|
|
259
|
+
forecast: { type: "string" },
|
|
260
|
+
// Search fields
|
|
261
|
+
query: { type: "string" },
|
|
262
|
+
results: { type: "array", items: { type: "string" } },
|
|
263
|
+
source: { type: "string" },
|
|
264
|
+
// Preferences fields
|
|
265
|
+
theme: { type: "string", enum: ["light", "dark"] },
|
|
266
|
+
language: { type: "string", enum: ["en", "es", "fr"] },
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
|
|
245
270
|
// Create agent with tools
|
|
246
|
-
const agent = new Agent<{ preferences?: { theme: string; language: string } }>({
|
|
271
|
+
const agent = new Agent<{ preferences?: { theme: string; language: string } }, UnifiedToolData>({
|
|
247
272
|
name: "ToolBot",
|
|
248
273
|
description: "An agent demonstrating various tool capabilities",
|
|
249
274
|
provider: new GeminiProvider({
|
|
250
275
|
apiKey: process.env.GEMINI_API_KEY!,
|
|
251
276
|
model: "models/gemini-2.5-flash",
|
|
252
277
|
}),
|
|
278
|
+
// NEW: Agent-level schema
|
|
279
|
+
schema: unifiedToolSchema,
|
|
253
280
|
});
|
|
254
281
|
|
|
255
282
|
// Create routes that use different tools
|
|
256
|
-
agent.createRoute
|
|
283
|
+
agent.createRoute({
|
|
257
284
|
title: "Calculator",
|
|
258
285
|
description: "Mathematical calculations",
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
result: { type: "number" },
|
|
264
|
-
operation: { type: "string" },
|
|
265
|
-
},
|
|
266
|
-
required: ["expression"],
|
|
267
|
-
},
|
|
286
|
+
// NEW: Required fields for route completion
|
|
287
|
+
requiredFields: ["expression"],
|
|
288
|
+
// NEW: Optional fields that enhance the experience
|
|
289
|
+
optionalFields: ["result", "operation"],
|
|
268
290
|
steps: [
|
|
269
291
|
{
|
|
270
292
|
id: "get_expression",
|
|
@@ -283,19 +305,13 @@ agent.createRoute<CalculatorData>({
|
|
|
283
305
|
],
|
|
284
306
|
});
|
|
285
307
|
|
|
286
|
-
agent.createRoute
|
|
308
|
+
agent.createRoute({
|
|
287
309
|
title: "Weather",
|
|
288
310
|
description: "Weather information",
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
temperature: { type: "number" },
|
|
294
|
-
condition: { type: "string" },
|
|
295
|
-
forecast: { type: "string" },
|
|
296
|
-
},
|
|
297
|
-
required: ["location"],
|
|
298
|
-
},
|
|
311
|
+
// NEW: Required fields for route completion
|
|
312
|
+
requiredFields: ["location"],
|
|
313
|
+
// NEW: Optional fields that enhance the experience
|
|
314
|
+
optionalFields: ["temperature", "condition", "forecast"],
|
|
299
315
|
steps: [
|
|
300
316
|
{
|
|
301
317
|
id: "get_location",
|
|
@@ -314,18 +330,13 @@ agent.createRoute<WeatherData>({
|
|
|
314
330
|
],
|
|
315
331
|
});
|
|
316
332
|
|
|
317
|
-
agent.createRoute
|
|
333
|
+
agent.createRoute({
|
|
318
334
|
title: "Web Search",
|
|
319
335
|
description: "Information search",
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
results: { type: "array", items: { type: "string" } },
|
|
325
|
-
source: { type: "string" },
|
|
326
|
-
},
|
|
327
|
-
required: ["query"],
|
|
328
|
-
},
|
|
336
|
+
// NEW: Required fields for route completion
|
|
337
|
+
requiredFields: ["query"],
|
|
338
|
+
// NEW: Optional fields that enhance the experience
|
|
339
|
+
optionalFields: ["results", "source"],
|
|
329
340
|
steps: [
|
|
330
341
|
{
|
|
331
342
|
id: "get_query",
|
|
@@ -371,7 +382,7 @@ async function demonstrateBasicTools() {
|
|
|
371
382
|
console.log("Bot:", calcResponse.message);
|
|
372
383
|
console.log(
|
|
373
384
|
"Calculated result:",
|
|
374
|
-
|
|
385
|
+
calcResponse.session?.data?.result
|
|
375
386
|
);
|
|
376
387
|
console.log();
|
|
377
388
|
|
|
@@ -383,9 +394,9 @@ async function demonstrateBasicTools() {
|
|
|
383
394
|
});
|
|
384
395
|
console.log("Bot:", weatherResponse.message);
|
|
385
396
|
console.log("Weather data:", {
|
|
386
|
-
temperature: (weatherResponse.session?.data as Partial<
|
|
397
|
+
temperature: (weatherResponse.session?.data as Partial<UnifiedToolData>)
|
|
387
398
|
?.temperature,
|
|
388
|
-
condition: (weatherResponse.session?.data as Partial<
|
|
399
|
+
condition: (weatherResponse.session?.data as Partial<UnifiedToolData>)
|
|
389
400
|
?.condition,
|
|
390
401
|
});
|
|
391
402
|
console.log();
|
|
@@ -404,7 +415,7 @@ async function demonstrateBasicTools() {
|
|
|
404
415
|
console.log("Bot:", searchResponse.message);
|
|
405
416
|
console.log(
|
|
406
417
|
"Search results count:",
|
|
407
|
-
(searchResponse.session?.data as Partial<
|
|
418
|
+
(searchResponse.session?.data as Partial<UnifiedToolData>)?.results?.length
|
|
408
419
|
);
|
|
409
420
|
}
|
|
410
421
|
|
|
@@ -453,15 +464,15 @@ async function demonstrateToolDataFlow() {
|
|
|
453
464
|
console.log("1. Initial calculation:");
|
|
454
465
|
console.log(
|
|
455
466
|
" User input collected:",
|
|
456
|
-
(response1.session?.data as Partial<
|
|
467
|
+
(response1.session?.data as Partial<UnifiedToolData>)?.expression
|
|
457
468
|
);
|
|
458
469
|
console.log(
|
|
459
470
|
" Tool result:",
|
|
460
|
-
(response1.session?.data as Partial<
|
|
471
|
+
(response1.session?.data as Partial<UnifiedToolData>)?.result
|
|
461
472
|
);
|
|
462
473
|
console.log(
|
|
463
474
|
" Operation stored:",
|
|
464
|
-
(response1.session?.data as Partial<
|
|
475
|
+
(response1.session?.data as Partial<UnifiedToolData>)?.operation
|
|
465
476
|
);
|
|
466
477
|
|
|
467
478
|
// Follow up question using previous result
|
|
@@ -475,7 +486,7 @@ async function demonstrateToolDataFlow() {
|
|
|
475
486
|
console.log("\n2. Follow-up calculation:");
|
|
476
487
|
console.log(
|
|
477
488
|
" Previous result available:",
|
|
478
|
-
(response2.session?.data as Partial<
|
|
489
|
+
(response2.session?.data as Partial<UnifiedToolData>)?.result
|
|
479
490
|
);
|
|
480
491
|
console.log(" Bot response:", response2.message);
|
|
481
492
|
}
|
|
@@ -486,7 +497,7 @@ function demonstrateToolPatterns() {
|
|
|
486
497
|
|
|
487
498
|
console.log("1. Basic Tool Pattern:");
|
|
488
499
|
console.log(`
|
|
489
|
-
const myTool: Tool<ContextType, [], ResultType
|
|
500
|
+
const myTool: Tool<ContextType, DataType, [], ResultType> = {
|
|
490
501
|
id: "tool_name", // Unique identifier
|
|
491
502
|
description: "What this tool does", // AI uses this to decide when to call
|
|
492
503
|
parameters: { // JSON Schema for tool parameters
|
|
@@ -45,7 +45,7 @@ interface FlightData {
|
|
|
45
45
|
// ==============================================================================
|
|
46
46
|
|
|
47
47
|
// Tool 1: Convert city names to airport codes
|
|
48
|
-
const enrichDestinationTool: Tool<FlightBookingContext, [], void
|
|
48
|
+
const enrichDestinationTool: Tool<FlightBookingContext, FlightData, [], void> =
|
|
49
49
|
{
|
|
50
50
|
id: "enrich_destination",
|
|
51
51
|
name: "Destination Code Lookup",
|
|
@@ -84,7 +84,7 @@ const enrichDestinationTool: Tool<FlightBookingContext, [], void, FlightData> =
|
|
|
84
84
|
};
|
|
85
85
|
|
|
86
86
|
// Tool 2: Parse and validate dates
|
|
87
|
-
const validateDateTool: Tool<FlightBookingContext, [], void
|
|
87
|
+
const validateDateTool: Tool<FlightBookingContext, FlightData, [], void> = {
|
|
88
88
|
id: "validate_date",
|
|
89
89
|
name: "Date Parser & Validator",
|
|
90
90
|
description:
|
|
@@ -139,7 +139,7 @@ const validateDateTool: Tool<FlightBookingContext, [], void, FlightData> = {
|
|
|
139
139
|
};
|
|
140
140
|
|
|
141
141
|
// Tool 3: Search for flights (triggered by flag)
|
|
142
|
-
const searchFlightsTool: Tool<FlightBookingContext, [], void
|
|
142
|
+
const searchFlightsTool: Tool<FlightBookingContext, FlightData, [], void> = {
|
|
143
143
|
id: "search_flights",
|
|
144
144
|
name: "Flight Availability Search",
|
|
145
145
|
description: "Search for available flights based on collected data",
|
|
@@ -188,7 +188,7 @@ const searchFlightsTool: Tool<FlightBookingContext, [], void, FlightData> = {
|
|
|
188
188
|
};
|
|
189
189
|
|
|
190
190
|
// Tool 4: Book the flight
|
|
191
|
-
const bookFlightTool: Tool<FlightBookingContext, [], void
|
|
191
|
+
const bookFlightTool: Tool<FlightBookingContext, FlightData, [], void> = {
|
|
192
192
|
id: "book_flight",
|
|
193
193
|
name: "Flight Booking Processor",
|
|
194
194
|
description: "Finalize the flight booking",
|
|
@@ -252,7 +252,45 @@ function onDataUpdate(
|
|
|
252
252
|
// AGENT SETUP
|
|
253
253
|
// ==============================================================================
|
|
254
254
|
|
|
255
|
-
|
|
255
|
+
// Define flight booking schema
|
|
256
|
+
const flightBookingSchema = {
|
|
257
|
+
type: "object",
|
|
258
|
+
properties: {
|
|
259
|
+
destination: {
|
|
260
|
+
type: "string",
|
|
261
|
+
description: "City or airport the user wants to fly to",
|
|
262
|
+
},
|
|
263
|
+
destinationCode: {
|
|
264
|
+
type: "string",
|
|
265
|
+
description: "IATA airport code (enriched by tool)",
|
|
266
|
+
},
|
|
267
|
+
departureDate: {
|
|
268
|
+
type: "string",
|
|
269
|
+
description: "When the user wants to depart",
|
|
270
|
+
},
|
|
271
|
+
departureDateParsed: {
|
|
272
|
+
type: "string",
|
|
273
|
+
description: "Parsed ISO date (enriched by tool)",
|
|
274
|
+
},
|
|
275
|
+
passengers: {
|
|
276
|
+
type: "number",
|
|
277
|
+
minimum: 1,
|
|
278
|
+
maximum: 9,
|
|
279
|
+
},
|
|
280
|
+
cabinClass: {
|
|
281
|
+
type: "string",
|
|
282
|
+
enum: ["economy", "business", "first"],
|
|
283
|
+
default: "economy",
|
|
284
|
+
},
|
|
285
|
+
shouldSearchFlights: {
|
|
286
|
+
type: "boolean",
|
|
287
|
+
description: "Flag to trigger flight search",
|
|
288
|
+
},
|
|
289
|
+
},
|
|
290
|
+
required: ["destination", "departureDate", "passengers"],
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
const agent = new Agent<FlightBookingContext, FlightData>({
|
|
256
294
|
name: "Flight Booking Agent",
|
|
257
295
|
goal: "Help users book flights efficiently",
|
|
258
296
|
description: "I help you find and book flights",
|
|
@@ -261,55 +299,25 @@ const agent = new Agent<FlightBookingContext>({
|
|
|
261
299
|
model: "gpt-5o-mini",
|
|
262
300
|
}),
|
|
263
301
|
context: {},
|
|
302
|
+
// NEW: Agent-level schema
|
|
303
|
+
schema: flightBookingSchema,
|
|
264
304
|
hooks: {
|
|
265
305
|
onDataUpdate, // Validation & enrichment hook
|
|
266
306
|
},
|
|
267
307
|
});
|
|
268
308
|
|
|
269
309
|
// Define route with data extraction
|
|
270
|
-
const bookingRoute = agent.createRoute
|
|
310
|
+
const bookingRoute = agent.createRoute({
|
|
271
311
|
title: "Book Flight",
|
|
272
312
|
description: "Help user book a flight",
|
|
273
313
|
conditions: [
|
|
274
314
|
"User wants to book a flight",
|
|
275
315
|
"User mentions flying, traveling, or booking",
|
|
276
316
|
],
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
type: "string",
|
|
282
|
-
description: "City or airport the user wants to fly to",
|
|
283
|
-
},
|
|
284
|
-
destinationCode: {
|
|
285
|
-
type: "string",
|
|
286
|
-
description: "IATA airport code (enriched by tool)",
|
|
287
|
-
},
|
|
288
|
-
departureDate: {
|
|
289
|
-
type: "string",
|
|
290
|
-
description: "When the user wants to depart",
|
|
291
|
-
},
|
|
292
|
-
departureDateParsed: {
|
|
293
|
-
type: "string",
|
|
294
|
-
description: "Parsed ISO date (enriched by tool)",
|
|
295
|
-
},
|
|
296
|
-
passengers: {
|
|
297
|
-
type: "number",
|
|
298
|
-
minimum: 1,
|
|
299
|
-
maximum: 9,
|
|
300
|
-
},
|
|
301
|
-
cabinClass: {
|
|
302
|
-
type: "string",
|
|
303
|
-
enum: ["economy", "business", "first"],
|
|
304
|
-
default: "economy",
|
|
305
|
-
},
|
|
306
|
-
shouldSearchFlights: {
|
|
307
|
-
type: "boolean",
|
|
308
|
-
description: "Flag to trigger flight search",
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
required: ["destination", "departureDate", "passengers"],
|
|
312
|
-
},
|
|
317
|
+
// NEW: Required fields for route completion
|
|
318
|
+
requiredFields: ["destination", "departureDate", "passengers"],
|
|
319
|
+
// NEW: Optional fields that enhance the experience
|
|
320
|
+
optionalFields: ["destinationCode", "departureDateParsed", "cabinClass", "shouldSearchFlights"],
|
|
313
321
|
});
|
|
314
322
|
|
|
315
323
|
// Step 1: Collect destination
|
|
@@ -389,14 +397,14 @@ async function main() {
|
|
|
389
397
|
|
|
390
398
|
console.log("\n=== RESPONSE ===");
|
|
391
399
|
console.log("Message:", response.message);
|
|
392
|
-
console.log("Data:", agent.session.getData
|
|
400
|
+
console.log("Data:", agent.session.getData());
|
|
393
401
|
console.log("Context:", agent["context"]);
|
|
394
402
|
|
|
395
403
|
await agent.session.addMessage("assistant", response.message);
|
|
396
404
|
|
|
397
405
|
if (response.isRouteComplete) {
|
|
398
406
|
console.log("\n✅ Flight booking complete!");
|
|
399
|
-
await sendBookingConfirmation(agent.session.getData
|
|
407
|
+
await sendBookingConfirmation(agent.session.getData());
|
|
400
408
|
}
|
|
401
409
|
|
|
402
410
|
/*
|