@falai/agent 0.6.0 → 0.6.1

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/docs/ADAPTERS.md CHANGED
@@ -4,6 +4,18 @@ All adapters follow the **provider pattern** - no dependencies required in the p
4
4
 
5
5
  **NEW**: All adapters now support the new **Session State** pattern with automatic persistence of extracted data, current route/state, and conversation progress!
6
6
 
7
+ ### 🎯 Available Adapters
8
+
9
+ | Adapter | Use Case | Install |
10
+ | --------------------- | ---------------------------------- | -------------------------------------------- |
11
+ | **PrismaAdapter** | Type-safe ORM with migrations | `npm install @prisma/client` |
12
+ | **RedisAdapter** | Fast in-memory for real-time apps | `npm install ioredis` |
13
+ | **MongoAdapter** | Flexible document storage | `npm install mongodb` |
14
+ | **PostgreSQLAdapter** | Raw SQL with auto table creation | `npm install pg` |
15
+ | **SQLiteAdapter** | Lightweight local database | `npm install better-sqlite3` |
16
+ | **OpenSearchAdapter** | Full-text search & analytics | `npm install @opensearch-project/opensearch` |
17
+ | **MemoryAdapter** | Testing & development (no install) | Built-in (no dependencies) ✨ |
18
+
7
19
  ## ✅ Implemented Adapters
8
20
 
9
21
  ### 1. **PrismaAdapter**
@@ -0,0 +1,419 @@
1
+ # Examples
2
+
3
+ This directory contains production-ready examples demonstrating all features of `@falai/agent`.
4
+
5
+ ## 🚀 Getting Started Examples
6
+
7
+ ### 📋 [Declarative Agent](../examples/declarative-agent.ts)
8
+
9
+ **Perfect for:** Learning the full configuration API
10
+
11
+ Comprehensive example showing declarative agent configuration:
12
+
13
+ - ✅ Full constructor-based setup
14
+ - ✅ Terms, guidelines, capabilities, routes defined upfront
15
+ - ✅ Session state management with data extraction
16
+ - ✅ Custom IDs for routes, states, and tools
17
+ - ✅ Dynamic additions after construction
18
+
19
+ **Key concepts:** Declarative configuration, session state, data extraction schemas
20
+
21
+ ```typescript
22
+ const agent = new Agent({
23
+ name: "HealthBot",
24
+ ai: provider,
25
+ terms: [...],
26
+ guidelines: [...],
27
+ routes: [{
28
+ extractionSchema: { /* JSON Schema */ }
29
+ }]
30
+ });
31
+ ```
32
+
33
+ ---
34
+
35
+ ## 🏢 Real-World Applications
36
+
37
+ ### 🏢 [Business Onboarding](../examples/business-onboarding.ts)
38
+
39
+ **Perfect for:** Building complex multi-step workflows
40
+
41
+ Production-ready business onboarding with advanced patterns:
42
+
43
+ - ✅ Multi-step data collection flow
44
+ - ✅ Branching logic (physical vs online business)
45
+ - ✅ Tools with `contextUpdate` for automatic state management
46
+ - ✅ Both step-by-step and fluent chaining approaches
47
+ - ✅ Lifecycle hooks for persistence
48
+ - ✅ Dynamic route creation based on collected data
49
+
50
+ **Key concepts:** Complex flows, branching logic, context updates, lifecycle hooks
51
+
52
+ ```typescript
53
+ // Branching based on business type
54
+ const askPhysicalLocation = askLocation.transitionTo({
55
+ chatState: "Get physical store address",
56
+ condition: "User has a physical store",
57
+ });
58
+
59
+ const askOnlineLocation = askLocation.transitionTo({
60
+ chatState: "Get website and online support hours",
61
+ condition: "User does not have a physical store",
62
+ });
63
+ ```
64
+
65
+ ### ✈️ [Travel Agent](../examples/travel-agent.ts)
66
+
67
+ **Perfect for:** Multi-route systems with session state
68
+
69
+ Complete travel booking system featuring:
70
+
71
+ - ✅ Multi-step flight booking flow
72
+ - ✅ Data extraction with JSON Schema
73
+ - ✅ Session state tracking across turns
74
+ - ✅ Tools with data access via `extracted` context
75
+ - ✅ Alternative flow handling (booking vs status check)
76
+ - ✅ Route-specific guidelines
77
+
78
+ **Key concepts:** Session state, data extraction, multiple routes, tool data access
79
+
80
+ ```typescript
81
+ const searchFlights = defineTool(
82
+ "search_flights",
83
+ async ({ context, extracted }) => {
84
+ // Tool has access to extracted booking data
85
+ if (!extracted?.destination || !extracted?.departureDate) {
86
+ return { data: [] };
87
+ }
88
+ // Use extracted data to search
89
+ const flights = await searchAPI(extracted);
90
+ return { data: flights };
91
+ }
92
+ );
93
+ ```
94
+
95
+ ### 🏥 [Healthcare Assistant](../examples/healthcare-agent.ts)
96
+
97
+ **Perfect for:** Sensitive data handling and compliance
98
+
99
+ Healthcare-focused agent demonstrating:
100
+
101
+ - ✅ Appointment scheduling with validation
102
+ - ✅ Lab results retrieval
103
+ - ✅ Route-based disambiguation with conditions
104
+ - ✅ Sensitive data handling best practices
105
+ - ✅ Urgent case prioritization
106
+ - ✅ HIPAA-style security patterns
107
+
108
+ **Key concepts:** Data security, route disambiguation, validation, compliance
109
+
110
+ ---
111
+
112
+ ## ⚡ Advanced Features
113
+
114
+ ### ⚡ [Streaming Responses](../examples/streaming-agent.ts)
115
+
116
+ **Perfect for:** Real-time UX and better perceived performance
117
+
118
+ Real-time streaming responses:
119
+
120
+ - ✅ Stream responses from all providers (Anthropic, OpenAI, Gemini, OpenRouter)
121
+ - ✅ Real-time text generation with `respondStream`
122
+ - ✅ Cancellable streams with AbortSignal
123
+ - ✅ Access route, state, and tool information in final chunk
124
+ - ✅ 5 comprehensive examples covering different use cases
125
+
126
+ **Key concepts:** Streaming, real-time UX, cancellation
127
+
128
+ ```typescript
129
+ for await (const chunk of agent.respondStream({ history })) {
130
+ process.stdout.write(chunk.delta);
131
+
132
+ if (chunk.done) {
133
+ console.log("Route:", chunk.route?.title);
134
+ console.log("Extracted:", chunk.extracted);
135
+ }
136
+ }
137
+ ```
138
+
139
+ ### 🔐 [Domain Scoping](../examples/domain-scoping.ts)
140
+
141
+ **Perfect for:** Security-conscious applications
142
+
143
+ Control tool access per route for security:
144
+
145
+ - ✅ Organize tools into security domains
146
+ - ✅ Restrict which tools each route can use
147
+ - ✅ Prevent unauthorized tool calls
148
+ - ✅ Improve AI performance by reducing decision space
149
+ - ✅ Clear documentation of route capabilities
150
+
151
+ **Key concepts:** Security, tool isolation, domain organization
152
+
153
+ ```typescript
154
+ agent.addDomain("payment", { processPayment, refund });
155
+ agent.addDomain("user", { updateProfile, sendEmail });
156
+
157
+ // Checkout route can ONLY use payment tools
158
+ agent.createRoute({
159
+ title: "Checkout",
160
+ domains: ["payment"], // ← Security boundary
161
+ });
162
+ ```
163
+
164
+ ### 📜 [Rules & Prohibitions](../examples/rules-prohibitions.ts)
165
+
166
+ **Perfect for:** Multi-channel bots with different styles
167
+
168
+ Control agent behavior and communication style per route:
169
+
170
+ - ✅ Define absolute rules the agent must follow
171
+ - ✅ Set prohibitions for what agent must never do
172
+ - ✅ Different communication styles per route
173
+ - ✅ Perfect for multi-channel bots (WhatsApp, email, chat)
174
+ - ✅ Automatic enforcement without manual checking
175
+
176
+ **Key concepts:** Behavior control, tone management, channel-specific styling
177
+
178
+ ```typescript
179
+ agent.createRoute({
180
+ title: "WhatsApp Support",
181
+ rules: ["Keep messages under 2 lines", "Use max 1 emoji"],
182
+ prohibitions: ["Never send long paragraphs", "Don't over-explain"],
183
+ });
184
+
185
+ agent.createRoute({
186
+ title: "Email Support",
187
+ rules: ["Use professional tone", "Include clear next steps"],
188
+ prohibitions: ["No emojis", "Avoid slang"],
189
+ });
190
+ ```
191
+
192
+ ---
193
+
194
+ ## 💾 Persistence Examples
195
+
196
+ ### 💾 [Prisma Persistence](../examples/prisma-persistence.ts)
197
+
198
+ **Perfect for:** Production apps with relational databases
199
+
200
+ Auto-save sessions and messages with Prisma ORM:
201
+
202
+ - ✅ Provider pattern - simple as `new PrismaAdapter({ prisma })`
203
+ - ✅ Automatic session and message persistence
204
+ - ✅ Seamless lifecycle hook integration
205
+ - ✅ Type-safe database operations
206
+ - ✅ 3-step setup guide
207
+
208
+ **Key concepts:** Database persistence, Prisma ORM, auto-save
209
+
210
+ ```typescript
211
+ const agent = new Agent({
212
+ persistence: {
213
+ adapter: new PrismaAdapter({ prisma }),
214
+ autoSave: true,
215
+ userId: "user_123",
216
+ },
217
+ });
218
+ ```
219
+
220
+ ### ⚡ [Redis Persistence](../examples/redis-persistence.ts)
221
+
222
+ **Perfect for:** High-throughput real-time applications
223
+
224
+ Fast, in-memory persistence:
225
+
226
+ - ✅ Lightning-fast session storage
227
+ - ✅ Configurable TTLs for auto-cleanup
228
+ - ✅ Custom key prefixes
229
+ - ✅ Perfect for real-time chat applications
230
+ - ✅ Simple setup with ioredis
231
+
232
+ **Key concepts:** In-memory persistence, Redis, TTL management
233
+
234
+ ### 🔍 [OpenSearch Persistence](../examples/opensearch-persistence.ts)
235
+
236
+ **Perfect for:** Analytics and full-text search requirements
237
+
238
+ Full-text search and analytics-powered persistence:
239
+
240
+ - ✅ Built-in full-text search across all messages
241
+ - ✅ Powerful aggregations and analytics
242
+ - ✅ Compatible with Elasticsearch 7.x
243
+ - ✅ AWS OpenSearch Service ready
244
+ - ✅ Index management and optimization
245
+
246
+ **Key concepts:** Search, analytics, OpenSearch, Elasticsearch
247
+
248
+ ### 🗄️ [Custom Database Integration](../examples/custom-database-persistence.ts)
249
+
250
+ **Perfect for:** Integrating with existing database schemas
251
+
252
+ Manual session state management for existing schemas:
253
+
254
+ - ✅ Full control over database operations
255
+ - ✅ Works with any database (no adapter needed)
256
+ - ✅ Manual session state save/restore
257
+ - ✅ Perfect for integrating with existing schemas
258
+ - ✅ Complete example with validation hooks
259
+
260
+ **Key concepts:** Custom persistence, existing schemas, manual control
261
+
262
+ ---
263
+
264
+ ## 🔧 Context & State Management
265
+
266
+ ### 💾 [Persistent Onboarding Agent](../examples/persistent-onboarding.ts)
267
+
268
+ **Perfect for:** Multi-turn conversations with persistence
269
+
270
+ Multi-turn conversation with state persistence:
271
+
272
+ - ✅ Context lifecycle hooks for database integration
273
+ - ✅ Automatic persistence on context updates
274
+ - ✅ Factory pattern for agent creation
275
+ - ✅ Two approaches: lifecycle hooks vs context provider
276
+ - ✅ Complete onboarding flow across multiple turns
277
+
278
+ **Key concepts:** Context lifecycle, multi-turn conversations, factory pattern
279
+
280
+ ```typescript
281
+ const agent = new Agent({
282
+ hooks: {
283
+ beforeRespond: async (context) => {
284
+ return await database.loadContext(sessionId);
285
+ },
286
+ onContextUpdate: async (newContext) => {
287
+ await database.saveContext(sessionId, newContext);
288
+ },
289
+ },
290
+ });
291
+ ```
292
+
293
+ ### 🔄 [Extracted Data Modification](../examples/extracted-data-modification.ts)
294
+
295
+ **Perfect for:** Data validation and enrichment
296
+
297
+ Tools that validate and enrich extracted data:
298
+
299
+ - ✅ Tools can modify extracted data with `extractedUpdate`
300
+ - ✅ Data validation and enrichment patterns
301
+ - ✅ Flag-based conditional execution
302
+ - ✅ Error handling and data correction
303
+ - ✅ Multi-step data refinement
304
+
305
+ **Key concepts:** Data validation, enrichment, extractedUpdate, flags
306
+
307
+ ```typescript
308
+ const validateEmail = defineTool(
309
+ "validate_email",
310
+ async ({ extracted }) => {
311
+ const isValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(extracted.email);
312
+ return {
313
+ data: isValid,
314
+ extractedUpdate: {
315
+ emailValid: isValid, // Enrich extracted data
316
+ },
317
+ };
318
+ }
319
+ );
320
+ ```
321
+
322
+ ---
323
+
324
+ ## 🤖 Provider Examples
325
+
326
+ ### 🌐 [OpenAI Agent](../examples/openai-agent.ts)
327
+
328
+ **Perfect for:** Using GPT models
329
+
330
+ GPT-5 integration with backup models:
331
+
332
+ - ✅ OpenAI provider configuration
333
+ - ✅ Backup model fallback
334
+ - ✅ Retry configuration
335
+ - ✅ Weather checking example
336
+
337
+ **Key concepts:** OpenAI integration, model fallback
338
+
339
+ ### 🌐 Multiple Providers
340
+
341
+ See how different AI providers work:
342
+
343
+ - **[OpenAI Agent](../examples/openai-agent.ts)** - GPT-5 integration
344
+ - **[Healthcare Agent](../examples/healthcare-agent.ts)** - Claude 3.5 Sonnet (Anthropic)
345
+ - **[Travel Agent](../examples/travel-agent.ts)** - OpenRouter with backup models
346
+ - All examples include backup model configuration and retry settings
347
+
348
+ ---
349
+
350
+ ## 📚 Additional Examples
351
+
352
+ ### 📊 [Company Q&A Agent](../examples/company-qna-agent.ts)
353
+
354
+ **Perfect for:** Stateless question-answering systems
355
+
356
+ Simple Q&A agent with knowledge base:
357
+
358
+ - ✅ Stateless routes (no data extraction)
359
+ - ✅ Knowledge base integration
360
+ - ✅ Simple request-response pattern
361
+ - ✅ Perfect for FAQ bots
362
+
363
+ **Key concepts:** Stateless routing, Q&A patterns
364
+
365
+ ---
366
+
367
+ ## 🎯 How to Use These Examples
368
+
369
+ ### Running Examples
370
+
371
+ ```bash
372
+ # Install dependencies
373
+ bun install
374
+
375
+ # Set up environment variables
376
+ echo "GEMINI_API_KEY=your_key" > .env
377
+
378
+ # Run an example
379
+ bun examples/travel-agent.ts
380
+ ```
381
+
382
+ ### Learning Path
383
+
384
+ 1. **Start here:** [Declarative Agent](../examples/declarative-agent.ts) - Learn the basics
385
+ 2. **Simple flow:** [Travel Agent](../examples/travel-agent.ts) - Session state & extraction
386
+ 3. **Complex flow:** [Business Onboarding](../examples/business-onboarding.ts) - Branching & lifecycle
387
+ 4. **Add persistence:** [Prisma Persistence](../examples/prisma-persistence.ts) - Database integration
388
+ 5. **Add security:** [Domain Scoping](../examples/domain-scoping.ts) - Tool isolation
389
+
390
+ ### Quick Reference
391
+
392
+ | Example | Best For | Key Features |
393
+ |---------|----------|--------------|
394
+ | Declarative Agent | Learning basics | Full API coverage |
395
+ | Travel Agent | Session state | Multi-turn conversations |
396
+ | Business Onboarding | Complex flows | Branching, lifecycle hooks |
397
+ | Healthcare Agent | Security | Data validation, compliance |
398
+ | Streaming Agent | Real-time UX | Streaming responses |
399
+ | Domain Scoping | Security | Tool isolation |
400
+ | Prisma Persistence | Production | Database integration |
401
+
402
+ ---
403
+
404
+ ## 💡 Tips
405
+
406
+ **For Production:**
407
+ - Use [Prisma Persistence](../examples/prisma-persistence.ts) for relational data
408
+ - Use [Redis Persistence](../examples/redis-persistence.ts) for high-throughput
409
+ - Implement [Domain Scoping](../examples/domain-scoping.ts) for security
410
+ - Add [Rules & Prohibitions](../examples/rules-prohibitions.ts) for brand consistency
411
+
412
+ **For Development:**
413
+ - Start with [Declarative Agent](../examples/declarative-agent.ts)
414
+ - Use [Streaming Agent](../examples/streaming-agent.ts) for better UX
415
+ - Check [Custom Database Integration](../examples/custom-database-persistence.ts) for existing schemas
416
+
417
+ ---
418
+
419
+ **Need help?** Check the [full documentation](./README.md) or [open an issue](https://github.com/gusnips/falai/issues).
@@ -612,10 +612,12 @@ async function createBusinessOnboardingAgent(
612
612
  .transitionTo({
613
613
  id: "ask_improve",
614
614
  chatState: "Is there anything we could improve?",
615
+ condition: "User wants to provide feedback",
615
616
  })
616
617
  .transitionTo({
617
618
  id: "thank_you",
618
619
  chatState: "Thank you for your feedback! It helps us improve. 🙏",
620
+ condition: "User provided feedback",
619
621
  })
620
622
  .transitionTo({ state: END_ROUTE });
621
623
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@falai/agent",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
4
4
  "description": "Standalone, strongly-typed AI Agent framework with route DSL and AI provider strategy",
5
5
  "type": "module",
6
6
  "main": "./dist/cjs/index.js",