@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.
- package/README.md +65 -4
- package/dist/cjs/core/Agent.d.ts +11 -0
- package/dist/cjs/core/Agent.d.ts.map +1 -1
- package/dist/cjs/core/Agent.js +44 -2
- package/dist/cjs/core/Agent.js.map +1 -1
- package/dist/cjs/core/State.d.ts +1 -1
- package/dist/cjs/core/State.d.ts.map +1 -1
- package/dist/cjs/core/State.js +4 -10
- package/dist/cjs/core/State.js.map +1 -1
- package/dist/cjs/index.d.ts +1 -1
- package/dist/cjs/index.d.ts.map +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/agent.d.ts +24 -0
- package/dist/cjs/types/agent.d.ts.map +1 -1
- package/dist/cjs/types/route.d.ts +4 -6
- package/dist/cjs/types/route.d.ts.map +1 -1
- package/dist/cjs/types/tool.d.ts +6 -2
- package/dist/cjs/types/tool.d.ts.map +1 -1
- package/dist/core/Agent.d.ts +11 -0
- package/dist/core/Agent.d.ts.map +1 -1
- package/dist/core/Agent.js +44 -2
- package/dist/core/Agent.js.map +1 -1
- package/dist/core/State.d.ts +1 -1
- package/dist/core/State.d.ts.map +1 -1
- package/dist/core/State.js +4 -10
- package/dist/core/State.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/types/agent.d.ts +24 -0
- package/dist/types/agent.d.ts.map +1 -1
- package/dist/types/route.d.ts +4 -6
- package/dist/types/route.d.ts.map +1 -1
- package/dist/types/tool.d.ts +6 -2
- package/dist/types/tool.d.ts.map +1 -1
- package/docs/API_REFERENCE.md +25 -1
- package/docs/CONTEXT_MANAGEMENT.md +447 -0
- package/docs/GETTING_STARTED.md +3 -3
- package/examples/healthcare-agent.ts +15 -15
- package/examples/openai-agent.ts +2 -2
- package/examples/persistent-onboarding.ts +464 -0
- package/examples/travel-agent.ts +17 -17
- package/package.json +1 -1
- package/src/core/Agent.ts +56 -2
- package/src/core/State.ts +6 -25
- package/src/index.ts +2 -0
- package/src/types/agent.ts +32 -0
- package/src/types/route.ts +7 -9
- package/src/types/tool.ts +6 -2
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Persistent multi-turn onboarding agent example
|
|
3
|
+
* Demonstrates context lifecycle management for stateful conversations
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
Agent,
|
|
8
|
+
defineTool,
|
|
9
|
+
GeminiProvider,
|
|
10
|
+
END_ROUTE,
|
|
11
|
+
EventSource,
|
|
12
|
+
createMessageEvent,
|
|
13
|
+
type ContextLifecycleHooks,
|
|
14
|
+
} from "../src/index";
|
|
15
|
+
|
|
16
|
+
// ============================================================================
|
|
17
|
+
// DATABASE SIMULATION
|
|
18
|
+
// ============================================================================
|
|
19
|
+
|
|
20
|
+
interface SessionData {
|
|
21
|
+
sessionId: string;
|
|
22
|
+
userId: string;
|
|
23
|
+
collectedData: {
|
|
24
|
+
businessName?: string;
|
|
25
|
+
businessDescription?: string;
|
|
26
|
+
industry?: string;
|
|
27
|
+
contactEmail?: string;
|
|
28
|
+
};
|
|
29
|
+
completedSteps: string[];
|
|
30
|
+
lastUpdated: Date;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Simple in-memory database simulation
|
|
34
|
+
const database = new Map<string, SessionData>();
|
|
35
|
+
|
|
36
|
+
const db = {
|
|
37
|
+
sessions: {
|
|
38
|
+
async findById(sessionId: string): Promise<SessionData | undefined> {
|
|
39
|
+
return database.get(sessionId);
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
async update(
|
|
43
|
+
sessionId: string,
|
|
44
|
+
updates: Partial<SessionData>
|
|
45
|
+
): Promise<void> {
|
|
46
|
+
const existing = database.get(sessionId);
|
|
47
|
+
if (existing) {
|
|
48
|
+
database.set(sessionId, {
|
|
49
|
+
...existing,
|
|
50
|
+
...updates,
|
|
51
|
+
lastUpdated: new Date(),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
async create(sessionData: SessionData): Promise<void> {
|
|
57
|
+
database.set(sessionData.sessionId, sessionData);
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// ============================================================================
|
|
63
|
+
// CONTEXT TYPE
|
|
64
|
+
// ============================================================================
|
|
65
|
+
|
|
66
|
+
interface OnboardingContext {
|
|
67
|
+
sessionId: string;
|
|
68
|
+
userId: string;
|
|
69
|
+
userName?: string;
|
|
70
|
+
collectedData: {
|
|
71
|
+
businessName?: string;
|
|
72
|
+
businessDescription?: string;
|
|
73
|
+
industry?: string;
|
|
74
|
+
contactEmail?: string;
|
|
75
|
+
};
|
|
76
|
+
completedSteps: string[];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// ============================================================================
|
|
80
|
+
// AGENT FACTORY WITH LIFECYCLE HOOKS
|
|
81
|
+
// ============================================================================
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Creates an onboarding agent with persistent context management
|
|
85
|
+
*
|
|
86
|
+
* PATTERN 1: Factory + Lifecycle Hooks
|
|
87
|
+
* - Load fresh context from database before each response
|
|
88
|
+
* - Persist context updates automatically after changes
|
|
89
|
+
*/
|
|
90
|
+
async function createPersistentOnboardingAgent(sessionId: string) {
|
|
91
|
+
// Load session from database
|
|
92
|
+
const session = await db.sessions.findById(sessionId);
|
|
93
|
+
|
|
94
|
+
if (!session) {
|
|
95
|
+
throw new Error(`Session ${sessionId} not found`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Define lifecycle hooks for automatic persistence
|
|
99
|
+
const hooks: ContextLifecycleHooks<OnboardingContext> = {
|
|
100
|
+
// Called before respond() - load fresh context from database
|
|
101
|
+
beforeRespond: async (currentContext) => {
|
|
102
|
+
console.log("🔄 Loading fresh context from database...");
|
|
103
|
+
const freshSession = await db.sessions.findById(sessionId);
|
|
104
|
+
|
|
105
|
+
if (!freshSession) {
|
|
106
|
+
return currentContext; // Fallback to current
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
sessionId: freshSession.sessionId,
|
|
111
|
+
userId: freshSession.userId,
|
|
112
|
+
collectedData: freshSession.collectedData,
|
|
113
|
+
completedSteps: freshSession.completedSteps,
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
// Called after context updates - persist to database
|
|
118
|
+
onContextUpdate: async (newContext) => {
|
|
119
|
+
console.log("💾 Persisting context update to database...");
|
|
120
|
+
await db.sessions.update(sessionId, {
|
|
121
|
+
collectedData: newContext.collectedData,
|
|
122
|
+
completedSteps: newContext.completedSteps,
|
|
123
|
+
});
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
const provider = new GeminiProvider({
|
|
128
|
+
apiKey: process.env.GEMINI_API_KEY || "test-key",
|
|
129
|
+
model: "models/gemini-2.5-flash",
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
const agent = new Agent<OnboardingContext>({
|
|
133
|
+
name: "OnboardingBot",
|
|
134
|
+
description: "A friendly assistant that helps businesses get started",
|
|
135
|
+
goal: "Collect business information efficiently while being conversational",
|
|
136
|
+
ai: provider,
|
|
137
|
+
context: {
|
|
138
|
+
sessionId: session.sessionId,
|
|
139
|
+
userId: session.userId,
|
|
140
|
+
collectedData: session.collectedData,
|
|
141
|
+
completedSteps: session.completedSteps,
|
|
142
|
+
},
|
|
143
|
+
hooks, // Enable lifecycle hooks for persistence
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
// ============================================================================
|
|
147
|
+
// TOOLS (with context updates)
|
|
148
|
+
// ============================================================================
|
|
149
|
+
|
|
150
|
+
// OPTION 1: Using contextUpdate in return value
|
|
151
|
+
const saveBusinessInfo = defineTool<
|
|
152
|
+
OnboardingContext,
|
|
153
|
+
[name: string, description: string],
|
|
154
|
+
boolean
|
|
155
|
+
>(
|
|
156
|
+
"save_business_info",
|
|
157
|
+
async (toolContext, name, description) => {
|
|
158
|
+
console.log(`📝 Saving business info: ${name}`);
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
data: true,
|
|
162
|
+
// Context update is automatically persisted via onContextUpdate hook
|
|
163
|
+
contextUpdate: {
|
|
164
|
+
collectedData: {
|
|
165
|
+
...toolContext.context.collectedData,
|
|
166
|
+
businessName: name,
|
|
167
|
+
businessDescription: description,
|
|
168
|
+
},
|
|
169
|
+
completedSteps: [
|
|
170
|
+
...toolContext.context.completedSteps,
|
|
171
|
+
"business_info",
|
|
172
|
+
],
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
},
|
|
176
|
+
{
|
|
177
|
+
description: "Save business name and description",
|
|
178
|
+
}
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
// OPTION 2: Using updateContext method directly
|
|
182
|
+
const saveIndustry = defineTool<
|
|
183
|
+
OnboardingContext,
|
|
184
|
+
[industry: string],
|
|
185
|
+
boolean
|
|
186
|
+
>(
|
|
187
|
+
"save_industry",
|
|
188
|
+
async (toolContext, industry) => {
|
|
189
|
+
console.log(`🏭 Saving industry: ${industry}`);
|
|
190
|
+
|
|
191
|
+
// Direct context update (triggers onContextUpdate hook)
|
|
192
|
+
await toolContext.updateContext({
|
|
193
|
+
collectedData: {
|
|
194
|
+
...toolContext.context.collectedData,
|
|
195
|
+
industry,
|
|
196
|
+
},
|
|
197
|
+
completedSteps: [...toolContext.context.completedSteps, "industry"],
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
return { data: true };
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
description: "Save business industry",
|
|
204
|
+
}
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
const saveContactEmail = defineTool<
|
|
208
|
+
OnboardingContext,
|
|
209
|
+
[email: string],
|
|
210
|
+
boolean
|
|
211
|
+
>(
|
|
212
|
+
"save_contact_email",
|
|
213
|
+
async (toolContext, email) => {
|
|
214
|
+
console.log(`📧 Saving contact email: ${email}`);
|
|
215
|
+
|
|
216
|
+
await toolContext.updateContext({
|
|
217
|
+
collectedData: {
|
|
218
|
+
...toolContext.context.collectedData,
|
|
219
|
+
contactEmail: email,
|
|
220
|
+
},
|
|
221
|
+
completedSteps: [...toolContext.context.completedSteps, "contact"],
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
return { data: true };
|
|
225
|
+
},
|
|
226
|
+
{
|
|
227
|
+
description: "Save contact email",
|
|
228
|
+
}
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
// ============================================================================
|
|
232
|
+
// ONBOARDING ROUTE
|
|
233
|
+
// ============================================================================
|
|
234
|
+
|
|
235
|
+
const onboardingRoute = agent.createRoute({
|
|
236
|
+
title: "Business Onboarding",
|
|
237
|
+
description: "Guide user through business information collection",
|
|
238
|
+
conditions: ["User is onboarding their business"],
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// Step 1: Collect business info
|
|
242
|
+
const askBusinessInfo = onboardingRoute.initialState.transitionTo({
|
|
243
|
+
chatState: "Ask for business name and a brief description",
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
const saveBusinessStep = askBusinessInfo.transitionTo({
|
|
247
|
+
toolState: saveBusinessInfo,
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// Step 2: Collect industry
|
|
251
|
+
const askIndustry = saveBusinessStep.transitionTo({
|
|
252
|
+
chatState: "Ask what industry the business operates in",
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const saveIndustryStep = askIndustry.transitionTo({
|
|
256
|
+
toolState: saveIndustry,
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// Step 3: Collect contact
|
|
260
|
+
const askContact = saveIndustryStep.transitionTo({
|
|
261
|
+
chatState: "Ask for their contact email",
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
const saveContactStep = askContact.transitionTo({
|
|
265
|
+
toolState: saveContactEmail,
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
// Step 4: Confirmation
|
|
269
|
+
const confirm = saveContactStep.transitionTo({
|
|
270
|
+
chatState: "Summarize all collected information and ask for confirmation",
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
confirm.transitionTo({ state: END_ROUTE });
|
|
274
|
+
|
|
275
|
+
// Guidelines
|
|
276
|
+
onboardingRoute.createGuideline({
|
|
277
|
+
condition: "User provides invalid email format",
|
|
278
|
+
action: "Politely ask for a valid email address",
|
|
279
|
+
tags: ["validation"],
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
onboardingRoute.createGuideline({
|
|
283
|
+
condition: "User wants to skip a step",
|
|
284
|
+
action: "Explain why the information is important but allow them to skip",
|
|
285
|
+
tags: ["flexibility"],
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
agent.createGuideline({
|
|
289
|
+
condition: "User asks to start over",
|
|
290
|
+
action:
|
|
291
|
+
"Confirm they want to clear their progress, then restart the onboarding",
|
|
292
|
+
tags: ["reset"],
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
return agent;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// ============================================================================
|
|
299
|
+
// ALTERNATIVE PATTERN: CONTEXT PROVIDER
|
|
300
|
+
// ============================================================================
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Creates an onboarding agent using the contextProvider pattern
|
|
304
|
+
*
|
|
305
|
+
* PATTERN 2: Context Provider (Always Fresh)
|
|
306
|
+
* - Context is fetched fresh on every respond() call
|
|
307
|
+
* - No need for beforeRespond hook
|
|
308
|
+
* - Still use onContextUpdate for persistence
|
|
309
|
+
*/
|
|
310
|
+
async function createOnboardingAgentWithProvider(sessionId: string) {
|
|
311
|
+
const provider = new GeminiProvider({
|
|
312
|
+
apiKey: process.env.GEMINI_API_KEY || "test-key",
|
|
313
|
+
model: "models/gemini-2.5-flash",
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
const agent = new Agent<OnboardingContext>({
|
|
317
|
+
name: "OnboardingBot",
|
|
318
|
+
description: "A friendly assistant that helps businesses get started",
|
|
319
|
+
ai: provider,
|
|
320
|
+
|
|
321
|
+
// Context is always fetched fresh from database
|
|
322
|
+
contextProvider: async () => {
|
|
323
|
+
const session = await db.sessions.findById(sessionId);
|
|
324
|
+
if (!session) {
|
|
325
|
+
throw new Error(`Session ${sessionId} not found`);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
return {
|
|
329
|
+
sessionId: session.sessionId,
|
|
330
|
+
userId: session.userId,
|
|
331
|
+
collectedData: session.collectedData,
|
|
332
|
+
completedSteps: session.completedSteps,
|
|
333
|
+
};
|
|
334
|
+
},
|
|
335
|
+
|
|
336
|
+
// Still persist updates
|
|
337
|
+
hooks: {
|
|
338
|
+
onContextUpdate: async (newContext) => {
|
|
339
|
+
await db.sessions.update(sessionId, {
|
|
340
|
+
collectedData: newContext.collectedData,
|
|
341
|
+
completedSteps: newContext.completedSteps,
|
|
342
|
+
});
|
|
343
|
+
},
|
|
344
|
+
},
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// ... rest of agent setup (same as above)
|
|
348
|
+
|
|
349
|
+
return agent;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// ============================================================================
|
|
353
|
+
// USAGE EXAMPLE
|
|
354
|
+
// ============================================================================
|
|
355
|
+
|
|
356
|
+
async function main() {
|
|
357
|
+
const sessionId = "session_123";
|
|
358
|
+
const userId = "user_456";
|
|
359
|
+
|
|
360
|
+
// Initialize session in database
|
|
361
|
+
await db.sessions.create({
|
|
362
|
+
sessionId,
|
|
363
|
+
userId,
|
|
364
|
+
collectedData: {},
|
|
365
|
+
completedSteps: [],
|
|
366
|
+
lastUpdated: new Date(),
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
console.log("=== MULTI-TURN CONVERSATION SIMULATION ===\n");
|
|
370
|
+
|
|
371
|
+
// Turn 1: Start onboarding
|
|
372
|
+
console.log("📱 Turn 1: User starts onboarding");
|
|
373
|
+
const agent1 = await createPersistentOnboardingAgent(sessionId);
|
|
374
|
+
const response1 = await agent1.respond({
|
|
375
|
+
history: [
|
|
376
|
+
createMessageEvent(
|
|
377
|
+
EventSource.CUSTOMER,
|
|
378
|
+
"Alice",
|
|
379
|
+
"Hi, I want to onboard my business"
|
|
380
|
+
),
|
|
381
|
+
],
|
|
382
|
+
});
|
|
383
|
+
console.log("🤖 Bot:", response1.message);
|
|
384
|
+
console.log("📊 Context after turn 1:", agent1["context"]);
|
|
385
|
+
console.log();
|
|
386
|
+
|
|
387
|
+
// Turn 2: User provides business info
|
|
388
|
+
// NOTE: We create a NEW agent instance - context is loaded from database
|
|
389
|
+
console.log("📱 Turn 2: User provides business info");
|
|
390
|
+
const agent2 = await createPersistentOnboardingAgent(sessionId);
|
|
391
|
+
const response2 = await agent2.respond({
|
|
392
|
+
history: [
|
|
393
|
+
createMessageEvent(
|
|
394
|
+
EventSource.CUSTOMER,
|
|
395
|
+
"Alice",
|
|
396
|
+
"My business is called 'TechFlow' and we build AI-powered workflow automation tools"
|
|
397
|
+
),
|
|
398
|
+
],
|
|
399
|
+
});
|
|
400
|
+
console.log("🤖 Bot:", response2.message);
|
|
401
|
+
console.log("📊 Context after turn 2:", agent2["context"]);
|
|
402
|
+
console.log();
|
|
403
|
+
|
|
404
|
+
// Turn 3: User provides industry
|
|
405
|
+
console.log("📱 Turn 3: User provides industry");
|
|
406
|
+
const agent3 = await createPersistentOnboardingAgent(sessionId);
|
|
407
|
+
const response3 = await agent3.respond({
|
|
408
|
+
history: [
|
|
409
|
+
createMessageEvent(
|
|
410
|
+
EventSource.CUSTOMER,
|
|
411
|
+
"Alice",
|
|
412
|
+
"We're in the SaaS industry"
|
|
413
|
+
),
|
|
414
|
+
],
|
|
415
|
+
});
|
|
416
|
+
console.log("🤖 Bot:", response3.message);
|
|
417
|
+
console.log("📊 Context after turn 3:", agent3["context"]);
|
|
418
|
+
console.log();
|
|
419
|
+
|
|
420
|
+
// Verify persistence
|
|
421
|
+
console.log("=== PERSISTENCE VERIFICATION ===");
|
|
422
|
+
const finalSession = await db.sessions.findById(sessionId);
|
|
423
|
+
console.log(
|
|
424
|
+
"💾 Final persisted session:",
|
|
425
|
+
JSON.stringify(finalSession, null, 2)
|
|
426
|
+
);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// ============================================================================
|
|
430
|
+
// KEY PATTERNS DEMONSTRATED
|
|
431
|
+
// ============================================================================
|
|
432
|
+
|
|
433
|
+
/*
|
|
434
|
+
* ✅ PATTERN 1: Lifecycle Hooks (Recommended for most cases)
|
|
435
|
+
* - beforeRespond: Load fresh context before each response
|
|
436
|
+
* - onContextUpdate: Persist context after updates
|
|
437
|
+
* - Works with both static context and updates
|
|
438
|
+
*
|
|
439
|
+
* ✅ PATTERN 2: Context Provider (For always-fresh context)
|
|
440
|
+
* - contextProvider: Function that returns fresh context
|
|
441
|
+
* - onContextUpdate: Still needed for persistence
|
|
442
|
+
* - Best when context is always loaded from external source
|
|
443
|
+
*
|
|
444
|
+
* ✅ PATTERN 3: Tool Context Updates (Two options)
|
|
445
|
+
* - Option A: Return { data, contextUpdate } in tool result
|
|
446
|
+
* - Option B: Call toolContext.updateContext() directly
|
|
447
|
+
* - Both trigger onContextUpdate hook automatically
|
|
448
|
+
*
|
|
449
|
+
* ✅ PATTERN 4: Explicit Updates
|
|
450
|
+
* - agent.updateContext() for manual updates
|
|
451
|
+
* - Triggers onContextUpdate hook
|
|
452
|
+
* - Useful outside of tool execution
|
|
453
|
+
*
|
|
454
|
+
* ❌ ANTI-PATTERN: Caching Agents
|
|
455
|
+
* - DON'T cache agent instances across requests
|
|
456
|
+
* - DO recreate agents with fresh context
|
|
457
|
+
* - Context gets stale if agent is cached
|
|
458
|
+
*/
|
|
459
|
+
|
|
460
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
461
|
+
main().catch(console.error);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
export { createPersistentOnboardingAgent, createOnboardingAgentWithProvider };
|
package/examples/travel-agent.ts
CHANGED
|
@@ -162,20 +162,20 @@ async function createTravelAgent() {
|
|
|
162
162
|
chatState: "Ask about the destination",
|
|
163
163
|
});
|
|
164
164
|
|
|
165
|
-
const t1 = t0.
|
|
165
|
+
const t1 = t0.transitionTo({
|
|
166
166
|
chatState: "Ask about preferred travel dates",
|
|
167
167
|
});
|
|
168
168
|
|
|
169
|
-
const t2 = t1.
|
|
169
|
+
const t2 = t1.transitionTo({
|
|
170
170
|
toolState: getAvailableFlights,
|
|
171
171
|
});
|
|
172
172
|
|
|
173
|
-
const t3 = t2.
|
|
173
|
+
const t3 = t2.transitionTo({
|
|
174
174
|
chatState: "Present available flights and ask which one works for them",
|
|
175
175
|
});
|
|
176
176
|
|
|
177
177
|
// Happy path: customer selects a flight
|
|
178
|
-
const t4 = t3.
|
|
178
|
+
const t4 = t3.transitionTo(
|
|
179
179
|
{
|
|
180
180
|
chatState:
|
|
181
181
|
"Collect passenger information and confirm booking details before proceeding",
|
|
@@ -183,41 +183,41 @@ async function createTravelAgent() {
|
|
|
183
183
|
"The customer selects a flight"
|
|
184
184
|
);
|
|
185
185
|
|
|
186
|
-
const t5 = t4.
|
|
186
|
+
const t5 = t4.transitionTo(
|
|
187
187
|
{
|
|
188
188
|
toolState: bookFlight,
|
|
189
189
|
},
|
|
190
190
|
"The customer confirms the booking details"
|
|
191
191
|
);
|
|
192
192
|
|
|
193
|
-
const t6 = t5.
|
|
193
|
+
const t6 = t5.transitionTo({
|
|
194
194
|
chatState: "Provide confirmation number and booking summary",
|
|
195
195
|
});
|
|
196
196
|
|
|
197
|
-
t6.
|
|
197
|
+
t6.transitionTo({ state: END_ROUTE });
|
|
198
198
|
|
|
199
199
|
// Alternative path: no flights work
|
|
200
|
-
const t7 = t3.
|
|
200
|
+
const t7 = t3.transitionTo(
|
|
201
201
|
{
|
|
202
202
|
toolState: getAlternativeFlights,
|
|
203
203
|
},
|
|
204
204
|
"None of the flights work for the customer"
|
|
205
205
|
);
|
|
206
206
|
|
|
207
|
-
const t8 = t7.
|
|
207
|
+
const t8 = t7.transitionTo({
|
|
208
208
|
chatState: "Present alternative flights and ask if any work",
|
|
209
209
|
});
|
|
210
210
|
|
|
211
211
|
// Link back to happy path
|
|
212
|
-
t8.
|
|
212
|
+
t8.transitionTo(
|
|
213
213
|
{
|
|
214
|
-
state: t4
|
|
214
|
+
state: t4,
|
|
215
215
|
},
|
|
216
216
|
"The customer selects a flight"
|
|
217
217
|
);
|
|
218
218
|
|
|
219
219
|
// No alternative flights work either
|
|
220
|
-
const t9 = t8.
|
|
220
|
+
const t9 = t8.transitionTo(
|
|
221
221
|
{
|
|
222
222
|
chatState:
|
|
223
223
|
"Suggest calling our office or visiting our website for more options",
|
|
@@ -225,7 +225,7 @@ async function createTravelAgent() {
|
|
|
225
225
|
"None of the alternative flights work either"
|
|
226
226
|
);
|
|
227
227
|
|
|
228
|
-
t9.
|
|
228
|
+
t9.transitionTo({ state: END_ROUTE });
|
|
229
229
|
|
|
230
230
|
// Add route-specific guidelines
|
|
231
231
|
flightBookingRoute.createGuideline({
|
|
@@ -253,11 +253,11 @@ async function createTravelAgent() {
|
|
|
253
253
|
chatState: "Ask for the confirmation number or booking reference",
|
|
254
254
|
});
|
|
255
255
|
|
|
256
|
-
const s1 = s0.
|
|
256
|
+
const s1 = s0.transitionTo({
|
|
257
257
|
toolState: getBookingStatus,
|
|
258
258
|
});
|
|
259
259
|
|
|
260
|
-
s1.
|
|
260
|
+
s1.transitionTo(
|
|
261
261
|
{
|
|
262
262
|
chatState:
|
|
263
263
|
"Tell the customer that the booking could not be found and ask them to verify the confirmation number or call the office",
|
|
@@ -265,7 +265,7 @@ async function createTravelAgent() {
|
|
|
265
265
|
"The booking could not be found"
|
|
266
266
|
);
|
|
267
267
|
|
|
268
|
-
s1.
|
|
268
|
+
s1.transitionTo(
|
|
269
269
|
{
|
|
270
270
|
chatState:
|
|
271
271
|
"Provide the booking details and confirm everything is in order",
|
|
@@ -273,7 +273,7 @@ async function createTravelAgent() {
|
|
|
273
273
|
"The booking is confirmed and all details are correct"
|
|
274
274
|
);
|
|
275
275
|
|
|
276
|
-
s1.
|
|
276
|
+
s1.transitionTo(
|
|
277
277
|
{
|
|
278
278
|
chatState:
|
|
279
279
|
"Present the booking information and mention any issues or pending actions required",
|
package/package.json
CHANGED
package/src/core/Agent.ts
CHANGED
|
@@ -27,6 +27,7 @@ export class Agent<TContext = unknown> {
|
|
|
27
27
|
private routes: Route<TContext>[] = [];
|
|
28
28
|
private observations: Observation[] = [];
|
|
29
29
|
private domainRegistry = new DomainRegistry();
|
|
30
|
+
private context: TContext | undefined;
|
|
30
31
|
|
|
31
32
|
/**
|
|
32
33
|
* Dynamic domain property - populated via addDomain
|
|
@@ -39,6 +40,16 @@ export class Agent<TContext = unknown> {
|
|
|
39
40
|
this.options.maxEngineIterations = 1;
|
|
40
41
|
}
|
|
41
42
|
|
|
43
|
+
// Validate context configuration
|
|
44
|
+
if (options.context !== undefined && options.contextProvider) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
"Cannot provide both 'context' and 'contextProvider'. Choose one."
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// Initialize context if provided
|
|
51
|
+
this.context = options.context;
|
|
52
|
+
|
|
42
53
|
// Initialize from options
|
|
43
54
|
if (options.terms) {
|
|
44
55
|
this.terms = [...options.terms];
|
|
@@ -167,6 +178,39 @@ export class Agent<TContext = unknown> {
|
|
|
167
178
|
this.domain[name] = domainObject;
|
|
168
179
|
}
|
|
169
180
|
|
|
181
|
+
/**
|
|
182
|
+
* Update the agent's context
|
|
183
|
+
* Triggers the onContextUpdate lifecycle hook if configured
|
|
184
|
+
*/
|
|
185
|
+
async updateContext(updates: Partial<TContext>): Promise<void> {
|
|
186
|
+
const previousContext = this.context;
|
|
187
|
+
|
|
188
|
+
// Merge updates with current context
|
|
189
|
+
this.context = {
|
|
190
|
+
...(this.context as Record<string, unknown>),
|
|
191
|
+
...(updates as Record<string, unknown>),
|
|
192
|
+
} as TContext;
|
|
193
|
+
|
|
194
|
+
// Trigger lifecycle hook if configured
|
|
195
|
+
if (this.options.hooks?.onContextUpdate && previousContext !== undefined) {
|
|
196
|
+
await this.options.hooks.onContextUpdate(this.context, previousContext);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Get current context (fetches from provider if configured)
|
|
202
|
+
* @internal
|
|
203
|
+
*/
|
|
204
|
+
private async getContext(): Promise<TContext | undefined> {
|
|
205
|
+
// If context provider is configured, use it to fetch fresh context
|
|
206
|
+
if (this.options.contextProvider) {
|
|
207
|
+
return await this.options.contextProvider();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Otherwise return the stored context
|
|
211
|
+
return this.context;
|
|
212
|
+
}
|
|
213
|
+
|
|
170
214
|
/**
|
|
171
215
|
* Generate a response based on history and context
|
|
172
216
|
*/
|
|
@@ -183,9 +227,19 @@ export class Agent<TContext = unknown> {
|
|
|
183
227
|
}> {
|
|
184
228
|
const { history, contextOverride, signal } = params;
|
|
185
229
|
|
|
186
|
-
//
|
|
230
|
+
// Get current context (may fetch from provider)
|
|
231
|
+
let currentContext = await this.getContext();
|
|
232
|
+
|
|
233
|
+
// Call beforeRespond hook if configured
|
|
234
|
+
if (this.options.hooks?.beforeRespond && currentContext !== undefined) {
|
|
235
|
+
currentContext = await this.options.hooks.beforeRespond(currentContext);
|
|
236
|
+
// Update stored context with the result from beforeRespond
|
|
237
|
+
this.context = currentContext;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Merge context with override
|
|
187
241
|
const effectiveContext = {
|
|
188
|
-
...(
|
|
242
|
+
...(currentContext as Record<string, unknown>),
|
|
189
243
|
...(contextOverride as Record<string, unknown>),
|
|
190
244
|
} as TContext;
|
|
191
245
|
|