@falai/agent 0.1.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 +516 -0
- package/dist/constants/index.d.ts +5 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +5 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/core/Agent.d.ts +98 -0
- package/dist/core/Agent.d.ts.map +1 -0
- package/dist/core/Agent.js +248 -0
- package/dist/core/Agent.js.map +1 -0
- package/dist/core/DomainRegistry.d.ts +26 -0
- package/dist/core/DomainRegistry.d.ts.map +1 -0
- package/dist/core/DomainRegistry.js +41 -0
- package/dist/core/DomainRegistry.js.map +1 -0
- package/dist/core/Events.d.ts +19 -0
- package/dist/core/Events.d.ts.map +1 -0
- package/dist/core/Events.js +79 -0
- package/dist/core/Events.js.map +1 -0
- package/dist/core/Observation.d.ts +24 -0
- package/dist/core/Observation.d.ts.map +1 -0
- package/dist/core/Observation.js +35 -0
- package/dist/core/Observation.js.map +1 -0
- package/dist/core/PromptBuilder.d.ts +121 -0
- package/dist/core/PromptBuilder.d.ts.map +1 -0
- package/dist/core/PromptBuilder.js +339 -0
- package/dist/core/PromptBuilder.js.map +1 -0
- package/dist/core/Route.d.ts +46 -0
- package/dist/core/Route.d.ts.map +1 -0
- package/dist/core/Route.js +113 -0
- package/dist/core/Route.js.map +1 -0
- package/dist/core/State.d.ts +50 -0
- package/dist/core/State.d.ts.map +1 -0
- package/dist/core/State.js +110 -0
- package/dist/core/State.js.map +1 -0
- package/dist/core/Tool.d.ts +31 -0
- package/dist/core/Tool.d.ts.map +1 -0
- package/dist/core/Tool.js +33 -0
- package/dist/core/Tool.js.map +1 -0
- package/dist/core/Transition.d.ts +32 -0
- package/dist/core/Transition.d.ts.map +1 -0
- package/dist/core/Transition.js +59 -0
- package/dist/core/Transition.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/GeminiProvider.d.ts +40 -0
- package/dist/providers/GeminiProvider.d.ts.map +1 -0
- package/dist/providers/GeminiProvider.js +126 -0
- package/dist/providers/GeminiProvider.js.map +1 -0
- package/dist/providers/OpenAIProvider.d.ts +42 -0
- package/dist/providers/OpenAIProvider.d.ts.map +1 -0
- package/dist/providers/OpenAIProvider.js +164 -0
- package/dist/providers/OpenAIProvider.js.map +1 -0
- package/dist/providers/OpenRouterProvider.d.ts +46 -0
- package/dist/providers/OpenRouterProvider.d.ts.map +1 -0
- package/dist/providers/OpenRouterProvider.js +171 -0
- package/dist/providers/OpenRouterProvider.js.map +1 -0
- package/dist/types/agent.d.ts +105 -0
- package/dist/types/agent.d.ts.map +1 -0
- package/dist/types/agent.js +18 -0
- package/dist/types/agent.js.map +1 -0
- package/dist/types/ai.d.ts +78 -0
- package/dist/types/ai.d.ts.map +1 -0
- package/dist/types/ai.js +5 -0
- package/dist/types/ai.js.map +1 -0
- package/dist/types/history.d.ts +112 -0
- package/dist/types/history.d.ts.map +1 -0
- package/dist/types/history.js +34 -0
- package/dist/types/history.js.map +1 -0
- package/dist/types/index.d.ts +14 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +7 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/observation.d.ts +25 -0
- package/dist/types/observation.d.ts.map +1 -0
- package/dist/types/observation.js +5 -0
- package/dist/types/observation.js.map +1 -0
- package/dist/types/prompt.d.ts +46 -0
- package/dist/types/prompt.d.ts.map +1 -0
- package/dist/types/prompt.js +16 -0
- package/dist/types/prompt.js.map +1 -0
- package/dist/types/route.d.ts +59 -0
- package/dist/types/route.d.ts.map +1 -0
- package/dist/types/route.js +5 -0
- package/dist/types/route.js.map +1 -0
- package/dist/types/tool.d.ts +46 -0
- package/dist/types/tool.d.ts.map +1 -0
- package/dist/types/tool.js +5 -0
- package/dist/types/tool.js.map +1 -0
- package/dist/utils/retry.d.ts +13 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +70 -0
- package/dist/utils/retry.js.map +1 -0
- package/docs/API_REFERENCE.md +517 -0
- package/docs/CONSTRUCTOR_OPTIONS.md +256 -0
- package/docs/CONTRIBUTING.md +481 -0
- package/docs/GETTING_STARTED.md +328 -0
- package/docs/PROVIDERS.md +472 -0
- package/docs/PUBLISHING.md +174 -0
- package/docs/README.md +68 -0
- package/docs/STRUCTURE.md +32 -0
- package/examples/declarative-agent.ts +217 -0
- package/examples/healthcare-agent.ts +283 -0
- package/examples/openai-agent.ts +167 -0
- package/examples/travel-agent.ts +342 -0
- package/package.json +73 -0
- package/src/constants/index.ts +5 -0
- package/src/core/Agent.ts +307 -0
- package/src/core/DomainRegistry.ts +50 -0
- package/src/core/Events.ts +101 -0
- package/src/core/Observation.ts +46 -0
- package/src/core/PromptBuilder.ts +511 -0
- package/src/core/Route.ts +136 -0
- package/src/core/State.ts +153 -0
- package/src/core/Tool.ts +54 -0
- package/src/core/Transition.ts +66 -0
- package/src/index.ts +83 -0
- package/src/providers/GeminiProvider.ts +220 -0
- package/src/providers/OpenAIProvider.ts +272 -0
- package/src/providers/OpenRouterProvider.ts +282 -0
- package/src/types/agent.ts +112 -0
- package/src/types/ai.ts +85 -0
- package/src/types/history.ts +125 -0
- package/src/types/index.ts +56 -0
- package/src/types/observation.ts +27 -0
- package/src/types/prompt.ts +49 -0
- package/src/types/route.ts +68 -0
- package/src/types/tool.ts +53 -0
- package/src/utils/retry.ts +96 -0
package/README.md
ADDED
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# 🤖 @falai/agent
|
|
4
|
+
|
|
5
|
+
### Build intelligent, conversational AI agents with TypeScript
|
|
6
|
+
|
|
7
|
+
**Standalone • Strongly-Typed • Production-Ready**
|
|
8
|
+
|
|
9
|
+
[](https://www.typescriptlang.org/)
|
|
10
|
+
[](./LICENSE)
|
|
11
|
+
[](https://bun.sh)
|
|
12
|
+
|
|
13
|
+
[Features](#-features) • [Installation](#-installation) • [Quick Start](#-quick-start) • [Documentation](#-documentation) • [Examples](#-examples)
|
|
14
|
+
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## 🌟 Features
|
|
20
|
+
|
|
21
|
+
<table>
|
|
22
|
+
<tr>
|
|
23
|
+
<td width="50%">
|
|
24
|
+
|
|
25
|
+
### 🎯 **Developer Experience**
|
|
26
|
+
|
|
27
|
+
- **Fully Type-Safe** - Generic `Agent<TContext>` with complete inference
|
|
28
|
+
- **Fluent API** - Chainable methods for elegant code
|
|
29
|
+
- **Modular Design** - Use what you need, when you need it
|
|
30
|
+
|
|
31
|
+
</td>
|
|
32
|
+
<td width="50%">
|
|
33
|
+
|
|
34
|
+
### 🚀 **Production Ready**
|
|
35
|
+
|
|
36
|
+
- **Robust Retry Logic** - Exponential backoff & backup models
|
|
37
|
+
- **AI Provider Strategy** - Pluggable backends (Gemini, OpenAI, OpenRouter)
|
|
38
|
+
- **Prompt Composition** - Sophisticated prompt building system
|
|
39
|
+
|
|
40
|
+
</td>
|
|
41
|
+
</tr>
|
|
42
|
+
<tr>
|
|
43
|
+
<td width="50%">
|
|
44
|
+
|
|
45
|
+
### 🛤️ **Conversation Flows**
|
|
46
|
+
|
|
47
|
+
- **Route DSL** - Declarative state machines for conversations
|
|
48
|
+
- **Smart Transitions** - Conditional flows with `transitionTo`
|
|
49
|
+
- **Disambiguation** - Observations for handling ambiguous intent
|
|
50
|
+
|
|
51
|
+
</td>
|
|
52
|
+
<td width="50%">
|
|
53
|
+
|
|
54
|
+
### 🔧 **Tools & Capabilities**
|
|
55
|
+
|
|
56
|
+
- **Type-Safe Tools** - Define tools with full type inference
|
|
57
|
+
- **Domain Registry** - Organize capabilities by domain
|
|
58
|
+
- **Context Awareness** - Tools receive typed context automatically
|
|
59
|
+
|
|
60
|
+
</td>
|
|
61
|
+
</tr>
|
|
62
|
+
</table>
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## 📦 Installation
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
# Using bun (recommended)
|
|
70
|
+
bun add @falai/agent
|
|
71
|
+
|
|
72
|
+
# Using npm
|
|
73
|
+
npm install @falai/agent
|
|
74
|
+
|
|
75
|
+
# Using yarn
|
|
76
|
+
yarn add @falai/agent
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
> **Requirements:** Node.js 18+ or Bun 1.0+
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## 🚀 Quick Start
|
|
84
|
+
|
|
85
|
+
Get up and running in 60 seconds:
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import {
|
|
89
|
+
Agent,
|
|
90
|
+
GeminiProvider,
|
|
91
|
+
OpenAIProvider, // or use OpenAI
|
|
92
|
+
createMessageEvent,
|
|
93
|
+
EventSource,
|
|
94
|
+
} from "@falai/agent";
|
|
95
|
+
|
|
96
|
+
// 1️⃣ Define your custom context
|
|
97
|
+
interface SupportContext {
|
|
98
|
+
userId: string;
|
|
99
|
+
userName: string;
|
|
100
|
+
tier: "free" | "premium";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 2️⃣ Create AI provider (choose one)
|
|
104
|
+
const ai = new GeminiProvider({
|
|
105
|
+
apiKey: process.env.GEMINI_API_KEY!,
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Or use OpenAI:
|
|
109
|
+
// const ai = new OpenAIProvider({
|
|
110
|
+
// apiKey: process.env.OPENAI_API_KEY!,
|
|
111
|
+
// model: "gpt-5", // Required - specify your model
|
|
112
|
+
// });
|
|
113
|
+
|
|
114
|
+
// 3️⃣ Initialize your agent - two ways!
|
|
115
|
+
|
|
116
|
+
// Option A: Declarative initialization (recommended for complex setups)
|
|
117
|
+
const agent = new Agent<SupportContext>({
|
|
118
|
+
name: "SupportBot",
|
|
119
|
+
description: "A helpful and empathetic customer support assistant",
|
|
120
|
+
goal: "Resolve customer issues efficiently while maintaining a positive experience",
|
|
121
|
+
ai,
|
|
122
|
+
context: {
|
|
123
|
+
userId: "usr_123",
|
|
124
|
+
userName: "Alex",
|
|
125
|
+
tier: "premium",
|
|
126
|
+
},
|
|
127
|
+
// Initialize with arrays
|
|
128
|
+
terms: [
|
|
129
|
+
{
|
|
130
|
+
name: "Premium Support",
|
|
131
|
+
description: "24/7 priority support with dedicated account manager",
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
guidelines: [
|
|
135
|
+
{
|
|
136
|
+
condition: "Customer asks about account issues",
|
|
137
|
+
action: "Prioritize account security and verify identity first",
|
|
138
|
+
tags: ["security", "account"],
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
routes: [
|
|
142
|
+
{
|
|
143
|
+
title: "Account Recovery",
|
|
144
|
+
description: "Help users regain access to their accounts",
|
|
145
|
+
conditions: ["User cannot access their account"],
|
|
146
|
+
},
|
|
147
|
+
],
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// Option B: Fluent chaining (great for dynamic additions)
|
|
151
|
+
agent
|
|
152
|
+
.createGuideline({
|
|
153
|
+
condition: "Customer is frustrated",
|
|
154
|
+
action: "Show extra empathy and offer immediate escalation",
|
|
155
|
+
tags: ["support", "escalation"],
|
|
156
|
+
})
|
|
157
|
+
.createTerm({
|
|
158
|
+
name: "SLA",
|
|
159
|
+
description: "Service Level Agreement - our response time commitment",
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// 4️⃣ Generate intelligent responses
|
|
163
|
+
const response = await agent.respond({
|
|
164
|
+
history: [
|
|
165
|
+
createMessageEvent(
|
|
166
|
+
EventSource.CUSTOMER,
|
|
167
|
+
"Alex",
|
|
168
|
+
"I can't access my dashboard"
|
|
169
|
+
),
|
|
170
|
+
],
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
console.log(response.message); // 🎉 AI-powered response ready!
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
---
|
|
177
|
+
|
|
178
|
+
## 📚 Documentation
|
|
179
|
+
|
|
180
|
+
### 📖 Guides
|
|
181
|
+
|
|
182
|
+
- **[Getting Started](./docs/GETTING_STARTED.md)** - Your first agent in 5 minutes
|
|
183
|
+
- **[Constructor Options](./docs/CONSTRUCTOR_OPTIONS.md)** - Declarative vs Fluent API patterns
|
|
184
|
+
- **[API Reference](./docs/API_REFERENCE.md)** - Complete API documentation
|
|
185
|
+
- **[Architecture](./docs/STRUCTURE.md)** - Package structure and design principles
|
|
186
|
+
|
|
187
|
+
### 💡 Key Concepts
|
|
188
|
+
|
|
189
|
+
### 💬 Working with Conversation History
|
|
190
|
+
|
|
191
|
+
Build rich conversation contexts:
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
import { EventSource, createMessageEvent, createToolEvent } from "@falai/agent";
|
|
195
|
+
|
|
196
|
+
const history = [
|
|
197
|
+
createMessageEvent(
|
|
198
|
+
EventSource.CUSTOMER,
|
|
199
|
+
"Alice",
|
|
200
|
+
"Book me a flight to Paris"
|
|
201
|
+
),
|
|
202
|
+
createMessageEvent(
|
|
203
|
+
EventSource.AI_AGENT,
|
|
204
|
+
"TravelBot",
|
|
205
|
+
"I'd love to help! When would you like to travel?"
|
|
206
|
+
),
|
|
207
|
+
createMessageEvent(EventSource.CUSTOMER, "Alice", "Next Friday"),
|
|
208
|
+
// Tool calls are tracked too
|
|
209
|
+
createToolEvent("search_flights", {
|
|
210
|
+
destination: "Paris",
|
|
211
|
+
date: "2025-10-20",
|
|
212
|
+
}),
|
|
213
|
+
];
|
|
214
|
+
|
|
215
|
+
const response = await agent.respond({ history });
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### 🔧 Defining Type-Safe Tools
|
|
219
|
+
|
|
220
|
+
Tools are first-class citizens:
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
import { defineTool } from "@falai/agent";
|
|
224
|
+
|
|
225
|
+
const fetchUserProfile = defineTool<
|
|
226
|
+
SupportContext,
|
|
227
|
+
[userId: string],
|
|
228
|
+
{ name: string; email: string; tier: string }
|
|
229
|
+
>(
|
|
230
|
+
"fetch_user_profile",
|
|
231
|
+
async ({ context }, userId) => {
|
|
232
|
+
// Full access to typed context
|
|
233
|
+
console.log(`Fetching for ${context.userName}`);
|
|
234
|
+
|
|
235
|
+
const profile = await db.users.findById(userId);
|
|
236
|
+
return { data: profile };
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
description: "Retrieves user profile information from the database",
|
|
240
|
+
}
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
// Use in guidelines
|
|
244
|
+
agent.createGuideline({
|
|
245
|
+
condition: "Customer asks about their account details",
|
|
246
|
+
action: "Fetch and present their profile information",
|
|
247
|
+
tools: [fetchUserProfile],
|
|
248
|
+
});
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### 🛤️ Creating Conversation Routes
|
|
252
|
+
|
|
253
|
+
Build sophisticated conversation flows - declaratively or programmatically:
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
import { END_ROUTE } from "@falai/agent";
|
|
257
|
+
|
|
258
|
+
// Option A: Declarative (in constructor)
|
|
259
|
+
const agent = new Agent({
|
|
260
|
+
name: "OnboardingBot",
|
|
261
|
+
ai: provider,
|
|
262
|
+
routes: [
|
|
263
|
+
{
|
|
264
|
+
title: "User Onboarding",
|
|
265
|
+
description: "Guide new users through account setup",
|
|
266
|
+
conditions: ["User is new and needs onboarding"],
|
|
267
|
+
guidelines: [
|
|
268
|
+
{
|
|
269
|
+
condition: "User provides invalid email",
|
|
270
|
+
action: "Politely ask for a valid email format",
|
|
271
|
+
tags: ["validation"],
|
|
272
|
+
},
|
|
273
|
+
],
|
|
274
|
+
},
|
|
275
|
+
],
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// Option B: Programmatic (build flows dynamically)
|
|
279
|
+
const onboardingRoute = agent.createRoute({
|
|
280
|
+
title: "User Onboarding",
|
|
281
|
+
description: "Guide new users through account setup",
|
|
282
|
+
conditions: ["User is new and needs onboarding"],
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// Build the flow
|
|
286
|
+
const askName = onboardingRoute.initialState.transitionTo({
|
|
287
|
+
chatState: "Ask for user's full name",
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
const askEmail = askName.target.transitionTo({
|
|
291
|
+
chatState: "Request email address",
|
|
292
|
+
condition: "User provided their name",
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
const confirmDetails = askEmail.target.transitionTo({
|
|
296
|
+
chatState: "Confirm all details before proceeding",
|
|
297
|
+
});
|
|
298
|
+
|
|
299
|
+
// Add guidelines dynamically (can also be in route options)
|
|
300
|
+
onboardingRoute.createGuideline({
|
|
301
|
+
condition: "User provides invalid email",
|
|
302
|
+
action: "Politely ask for a valid email format",
|
|
303
|
+
enabled: true,
|
|
304
|
+
tags: ["validation"],
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
// Happy path completion
|
|
308
|
+
confirmDetails.target.transitionTo({
|
|
309
|
+
chatState: "Welcome message and next steps",
|
|
310
|
+
condition: "User confirms details",
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
// End the route
|
|
314
|
+
confirmDetails.target.transitionTo({ state: END_ROUTE });
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
### 🔀 Disambiguation with Observations
|
|
318
|
+
|
|
319
|
+
Handle ambiguous user intent gracefully - declaratively or programmatically:
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
// Option A: Declarative (reference routes by title)
|
|
323
|
+
const agent = new Agent({
|
|
324
|
+
name: "HealthBot",
|
|
325
|
+
ai: provider,
|
|
326
|
+
routes: [
|
|
327
|
+
{ title: "Schedule Appointment", conditions: ["User wants to schedule"] },
|
|
328
|
+
{
|
|
329
|
+
title: "Reschedule Appointment",
|
|
330
|
+
conditions: ["User wants to reschedule"],
|
|
331
|
+
},
|
|
332
|
+
],
|
|
333
|
+
observations: [
|
|
334
|
+
{
|
|
335
|
+
description: "User mentions appointment but intent is unclear",
|
|
336
|
+
routeRefs: ["Schedule Appointment", "Reschedule Appointment"], // By title
|
|
337
|
+
},
|
|
338
|
+
],
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
// Option B: Programmatic
|
|
342
|
+
const scheduleRoute = agent.createRoute({
|
|
343
|
+
title: "Schedule Appointment",
|
|
344
|
+
conditions: ["User wants to schedule"],
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
const rescheduleRoute = agent.createRoute({
|
|
348
|
+
title: "Reschedule Appointment",
|
|
349
|
+
conditions: ["User wants to reschedule"],
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
const appointmentInquiry = agent.createObservation(
|
|
353
|
+
"User mentions appointment but intent is unclear"
|
|
354
|
+
);
|
|
355
|
+
|
|
356
|
+
// Agent will ask user to clarify between these routes
|
|
357
|
+
appointmentInquiry.disambiguate([scheduleRoute, rescheduleRoute]);
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### 🎨 Context Override
|
|
361
|
+
|
|
362
|
+
Dynamically update context per request:
|
|
363
|
+
|
|
364
|
+
```typescript
|
|
365
|
+
const response = await agent.respond({
|
|
366
|
+
history,
|
|
367
|
+
contextOverride: {
|
|
368
|
+
tier: "premium", // Temporarily upgrade user for this request
|
|
369
|
+
},
|
|
370
|
+
});
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
### 📖 Domain Glossary
|
|
374
|
+
|
|
375
|
+
Teach your agent business-specific language:
|
|
376
|
+
|
|
377
|
+
```typescript
|
|
378
|
+
agent
|
|
379
|
+
.createTerm({
|
|
380
|
+
name: "SLA",
|
|
381
|
+
description: "Service Level Agreement - our commitment to response times",
|
|
382
|
+
synonyms: ["service agreement", "support guarantee"],
|
|
383
|
+
})
|
|
384
|
+
.createTerm({
|
|
385
|
+
name: "Priority Support",
|
|
386
|
+
description: "Premium tier feature with <1hr response time",
|
|
387
|
+
synonyms: ["premium support", "fast track"],
|
|
388
|
+
});
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
### ⚙️ Advanced Configuration
|
|
392
|
+
|
|
393
|
+
Fine-tune AI provider behavior - works with both Gemini and OpenAI:
|
|
394
|
+
|
|
395
|
+
```typescript
|
|
396
|
+
// Gemini configuration
|
|
397
|
+
const geminiProvider = new GeminiProvider({
|
|
398
|
+
apiKey: process.env.GEMINI_API_KEY!,
|
|
399
|
+
model: "models/gemini-2.5-flash", // Primary model
|
|
400
|
+
backupModels: [
|
|
401
|
+
"models/gemini-2.5-pro", // Backup if primary fails
|
|
402
|
+
"models/gemini-2.0-flash",
|
|
403
|
+
],
|
|
404
|
+
retryConfig: {
|
|
405
|
+
timeout: 60000, // 60s timeout
|
|
406
|
+
retries: 3, // 3 attempts with exponential backoff
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
// OpenAI configuration
|
|
411
|
+
const openaiProvider = new OpenAIProvider({
|
|
412
|
+
apiKey: process.env.OPENAI_API_KEY!,
|
|
413
|
+
model: "gpt-5",
|
|
414
|
+
backupModels: ["gpt-5-mini", "gpt-5-nano"],
|
|
415
|
+
retryConfig: {
|
|
416
|
+
timeout: 60000,
|
|
417
|
+
retries: 3,
|
|
418
|
+
},
|
|
419
|
+
});
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
---
|
|
423
|
+
|
|
424
|
+
## 🎯 Examples
|
|
425
|
+
|
|
426
|
+
### 📋 [Declarative Agent](./examples/declarative-agent.ts)
|
|
427
|
+
|
|
428
|
+
**Comprehensive declarative configuration example:**
|
|
429
|
+
|
|
430
|
+
- 📦 Everything configured in constructor
|
|
431
|
+
- 📚 Terms, guidelines, capabilities, routes, observations
|
|
432
|
+
- 🔗 Route references by title in observations
|
|
433
|
+
- ➕ Dynamic additions after construction
|
|
434
|
+
|
|
435
|
+
### 🌍 [Travel Booking Agent](./examples/travel-agent.ts)
|
|
436
|
+
|
|
437
|
+
A complete travel agent implementation featuring:
|
|
438
|
+
|
|
439
|
+
- ✈️ Multi-step flight booking flow
|
|
440
|
+
- 🔄 Alternative options handling
|
|
441
|
+
- 🛠️ Real-world tool integration
|
|
442
|
+
- 📋 Status checking route
|
|
443
|
+
- 🎭 Edge case guidelines
|
|
444
|
+
|
|
445
|
+
### 🏥 [Healthcare Assistant](./examples/healthcare-agent.ts)
|
|
446
|
+
|
|
447
|
+
Healthcare-focused agent demonstrating:
|
|
448
|
+
|
|
449
|
+
- 🩺 Appointment scheduling with alternatives
|
|
450
|
+
- 🔬 Lab results retrieval
|
|
451
|
+
- 🤔 Observation-based disambiguation
|
|
452
|
+
- 🔐 Sensitive data handling
|
|
453
|
+
- ⚠️ Urgent case prioritization
|
|
454
|
+
|
|
455
|
+
### 🌐 [OpenAI Agent](./examples/openai-agent.ts)
|
|
456
|
+
|
|
457
|
+
Using OpenAI provider instead of Gemini:
|
|
458
|
+
|
|
459
|
+
- 🤖 OpenAI GPT-4o integration
|
|
460
|
+
- 🔄 Backup model configuration
|
|
461
|
+
- ⚙️ Custom retry settings
|
|
462
|
+
- 🌤️ Weather checking example
|
|
463
|
+
|
|
464
|
+
---
|
|
465
|
+
|
|
466
|
+
## 🏗️ Architecture
|
|
467
|
+
|
|
468
|
+
```
|
|
469
|
+
src/
|
|
470
|
+
├── types/ # Type definitions (strongly typed contracts)
|
|
471
|
+
├── core/ # Core framework (Agent, Route, State, Tools, etc.)
|
|
472
|
+
├── providers/ # AI providers (Gemini with retry/backup logic)
|
|
473
|
+
├── utils/ # Utilities (retry, timeout, helpers)
|
|
474
|
+
├── constants/ # Constants (END_ROUTE, symbols)
|
|
475
|
+
└── index.ts # Public API exports
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
**Design Principles:**
|
|
479
|
+
|
|
480
|
+
- **Modularity** - Clean separation of concerns
|
|
481
|
+
- **Type Safety** - TypeScript generics throughout
|
|
482
|
+
- **Extensibility** - Pluggable providers & tools
|
|
483
|
+
- **Developer Experience** - Fluent APIs & clear patterns
|
|
484
|
+
|
|
485
|
+
---
|
|
486
|
+
|
|
487
|
+
## 🤝 Contributing
|
|
488
|
+
|
|
489
|
+
We welcome contributions! See our [Contributing Guide](./docs/CONTRIBUTING.md) for details on:
|
|
490
|
+
|
|
491
|
+
- 🐛 Reporting bugs
|
|
492
|
+
- 💡 Suggesting features
|
|
493
|
+
- 📝 Improving documentation
|
|
494
|
+
- 🔨 Submitting pull requests
|
|
495
|
+
|
|
496
|
+
## 🎓 Inspired By
|
|
497
|
+
|
|
498
|
+
This framework draws inspiration from [**Parlant**](https://github.com/emcie-co/parlant) by Emcie Co., an excellent Python framework for conversational AI agents. We've adapted and enhanced these concepts for the TypeScript ecosystem with additional type safety and modern patterns.
|
|
499
|
+
|
|
500
|
+
---
|
|
501
|
+
|
|
502
|
+
## 📄 License
|
|
503
|
+
|
|
504
|
+
MIT © 2025
|
|
505
|
+
|
|
506
|
+
---
|
|
507
|
+
|
|
508
|
+
<div align="center">
|
|
509
|
+
|
|
510
|
+
**Made with ❤️ for the community**
|
|
511
|
+
|
|
512
|
+
[Report Bug](https://github.com/gusnips/falai/issues) • [Request Feature](https://github.com/gusnips/falai/issues) • [Contribute](https://github.com/gusnips/falai/pulls)
|
|
513
|
+
|
|
514
|
+
⭐ Star us on [GitHub](https://github.com/gusnips/falai) if this helped you build amazing agents!
|
|
515
|
+
|
|
516
|
+
</div>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/constants/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,eAAO,MAAM,SAAS,eAAsB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/constants/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,MAAM,CAAC,MAAM,SAAS,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core Agent implementation
|
|
3
|
+
*/
|
|
4
|
+
import type { AgentOptions, Term, Guideline, Capability } from "@/types/agent";
|
|
5
|
+
import type { Event, StateRef } from "@/types/index";
|
|
6
|
+
import type { RouteOptions } from "@/types/route";
|
|
7
|
+
import { Route } from "@/core/Route";
|
|
8
|
+
import { DomainRegistry } from "@/core/DomainRegistry";
|
|
9
|
+
import { Observation } from "@/core/Observation";
|
|
10
|
+
/**
|
|
11
|
+
* Main Agent class with generic context support
|
|
12
|
+
*/
|
|
13
|
+
export declare class Agent<TContext = unknown> {
|
|
14
|
+
private readonly options;
|
|
15
|
+
private terms;
|
|
16
|
+
private guidelines;
|
|
17
|
+
private capabilities;
|
|
18
|
+
private routes;
|
|
19
|
+
private observations;
|
|
20
|
+
private domainRegistry;
|
|
21
|
+
/**
|
|
22
|
+
* Dynamic domain property - populated via addDomain
|
|
23
|
+
*/
|
|
24
|
+
readonly domain: Record<string, Record<string, unknown>>;
|
|
25
|
+
constructor(options: AgentOptions<TContext>);
|
|
26
|
+
/**
|
|
27
|
+
* Get agent name
|
|
28
|
+
*/
|
|
29
|
+
get name(): string;
|
|
30
|
+
/**
|
|
31
|
+
* Get agent description
|
|
32
|
+
*/
|
|
33
|
+
get description(): string | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Get agent goal
|
|
36
|
+
*/
|
|
37
|
+
get goal(): string | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Create a new route (journey)
|
|
40
|
+
*/
|
|
41
|
+
createRoute(options: RouteOptions): Route;
|
|
42
|
+
/**
|
|
43
|
+
* Create a domain term for the glossary
|
|
44
|
+
*/
|
|
45
|
+
createTerm(term: Term): this;
|
|
46
|
+
/**
|
|
47
|
+
* Create a behavioral guideline
|
|
48
|
+
*/
|
|
49
|
+
createGuideline(guideline: Guideline): this;
|
|
50
|
+
/**
|
|
51
|
+
* Add a capability
|
|
52
|
+
*/
|
|
53
|
+
createCapability(capability: Capability): this;
|
|
54
|
+
/**
|
|
55
|
+
* Create an observation for disambiguation
|
|
56
|
+
*/
|
|
57
|
+
createObservation(description: string): Observation;
|
|
58
|
+
/**
|
|
59
|
+
* Add a domain with its tools/methods
|
|
60
|
+
*/
|
|
61
|
+
addDomain<TName extends string, TDomain extends Record<string, unknown>>(name: TName, domainObject: TDomain): void;
|
|
62
|
+
/**
|
|
63
|
+
* Generate a response based on history and context
|
|
64
|
+
*/
|
|
65
|
+
respond(params: {
|
|
66
|
+
history: Event[];
|
|
67
|
+
state?: StateRef;
|
|
68
|
+
contextOverride?: Partial<TContext>;
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
}): Promise<{
|
|
71
|
+
message: string;
|
|
72
|
+
}>;
|
|
73
|
+
/**
|
|
74
|
+
* Get all routes
|
|
75
|
+
*/
|
|
76
|
+
getRoutes(): Route[];
|
|
77
|
+
/**
|
|
78
|
+
* Get all terms
|
|
79
|
+
*/
|
|
80
|
+
getTerms(): Term[];
|
|
81
|
+
/**
|
|
82
|
+
* Get all guidelines
|
|
83
|
+
*/
|
|
84
|
+
getGuidelines(): Guideline[];
|
|
85
|
+
/**
|
|
86
|
+
* Get all capabilities
|
|
87
|
+
*/
|
|
88
|
+
getCapabilities(): Capability[];
|
|
89
|
+
/**
|
|
90
|
+
* Get all observations
|
|
91
|
+
*/
|
|
92
|
+
getObservations(): Observation[];
|
|
93
|
+
/**
|
|
94
|
+
* Get the domain registry
|
|
95
|
+
*/
|
|
96
|
+
getDomainRegistry(): DomainRegistry;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=Agent.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Agent.d.ts","sourceRoot":"","sources":["../../src/core/Agent.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,IAAI,EACJ,SAAS,EAET,UAAU,EACX,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD;;GAEG;AACH,qBAAa,KAAK,CAAC,QAAQ,GAAG,OAAO;IAavB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAZpC,OAAO,CAAC,KAAK,CAAc;IAC3B,OAAO,CAAC,UAAU,CAAmB;IACrC,OAAO,CAAC,YAAY,CAAoB;IACxC,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,YAAY,CAAqB;IACzC,OAAO,CAAC,cAAc,CAAwB;IAE9C;;OAEG;IACH,SAAgB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAM;gBAExC,OAAO,EAAE,YAAY,CAAC,QAAQ,CAAC;IAkD5D;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED;;OAEG;IACH,IAAI,WAAW,IAAI,MAAM,GAAG,SAAS,CAEpC;IAED;;OAEG;IACH,IAAI,IAAI,IAAI,MAAM,GAAG,SAAS,CAE7B;IAED;;OAEG;IACH,WAAW,CAAC,OAAO,EAAE,YAAY,GAAG,KAAK;IAMzC;;OAEG;IACH,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,IAAI;IAK5B;;OAEG;IACH,eAAe,CAAC,SAAS,EAAE,SAAS,GAAG,IAAI;IAU3C;;OAEG;IACH,gBAAgB,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI;IAS9C;;OAEG;IACH,iBAAiB,CAAC,WAAW,EAAE,MAAM,GAAG,WAAW;IAMnD;;OAEG;IACH,SAAS,CAAC,KAAK,SAAS,MAAM,EAAE,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACrE,IAAI,EAAE,KAAK,EACX,YAAY,EAAE,OAAO,GACpB,IAAI;IAMP;;OAEG;IACG,OAAO,CAAC,MAAM,EAAE;QACpB,OAAO,EAAE,KAAK,EAAE,CAAC;QACjB,KAAK,CAAC,EAAE,QAAQ,CAAC;QACjB,eAAe,CAAC,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;QACpC,MAAM,CAAC,EAAE,WAAW,CAAC;KACtB,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC;IAwFhC;;OAEG;IACH,SAAS,IAAI,KAAK,EAAE;IAIpB;;OAEG;IACH,QAAQ,IAAI,IAAI,EAAE;IAIlB;;OAEG;IACH,aAAa,IAAI,SAAS,EAAE;IAI5B;;OAEG;IACH,eAAe,IAAI,UAAU,EAAE;IAI/B;;OAEG;IACH,eAAe,IAAI,WAAW,EAAE;IAIhC;;OAEG;IACH,iBAAiB,IAAI,cAAc;CAGpC"}
|