@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.
Files changed (129) hide show
  1. package/README.md +516 -0
  2. package/dist/constants/index.d.ts +5 -0
  3. package/dist/constants/index.d.ts.map +1 -0
  4. package/dist/constants/index.js +5 -0
  5. package/dist/constants/index.js.map +1 -0
  6. package/dist/core/Agent.d.ts +98 -0
  7. package/dist/core/Agent.d.ts.map +1 -0
  8. package/dist/core/Agent.js +248 -0
  9. package/dist/core/Agent.js.map +1 -0
  10. package/dist/core/DomainRegistry.d.ts +26 -0
  11. package/dist/core/DomainRegistry.d.ts.map +1 -0
  12. package/dist/core/DomainRegistry.js +41 -0
  13. package/dist/core/DomainRegistry.js.map +1 -0
  14. package/dist/core/Events.d.ts +19 -0
  15. package/dist/core/Events.d.ts.map +1 -0
  16. package/dist/core/Events.js +79 -0
  17. package/dist/core/Events.js.map +1 -0
  18. package/dist/core/Observation.d.ts +24 -0
  19. package/dist/core/Observation.d.ts.map +1 -0
  20. package/dist/core/Observation.js +35 -0
  21. package/dist/core/Observation.js.map +1 -0
  22. package/dist/core/PromptBuilder.d.ts +121 -0
  23. package/dist/core/PromptBuilder.d.ts.map +1 -0
  24. package/dist/core/PromptBuilder.js +339 -0
  25. package/dist/core/PromptBuilder.js.map +1 -0
  26. package/dist/core/Route.d.ts +46 -0
  27. package/dist/core/Route.d.ts.map +1 -0
  28. package/dist/core/Route.js +113 -0
  29. package/dist/core/Route.js.map +1 -0
  30. package/dist/core/State.d.ts +50 -0
  31. package/dist/core/State.d.ts.map +1 -0
  32. package/dist/core/State.js +110 -0
  33. package/dist/core/State.js.map +1 -0
  34. package/dist/core/Tool.d.ts +31 -0
  35. package/dist/core/Tool.d.ts.map +1 -0
  36. package/dist/core/Tool.js +33 -0
  37. package/dist/core/Tool.js.map +1 -0
  38. package/dist/core/Transition.d.ts +32 -0
  39. package/dist/core/Transition.d.ts.map +1 -0
  40. package/dist/core/Transition.js +59 -0
  41. package/dist/core/Transition.js.map +1 -0
  42. package/dist/index.d.ts +34 -0
  43. package/dist/index.d.ts.map +1 -0
  44. package/dist/index.js +26 -0
  45. package/dist/index.js.map +1 -0
  46. package/dist/providers/GeminiProvider.d.ts +40 -0
  47. package/dist/providers/GeminiProvider.d.ts.map +1 -0
  48. package/dist/providers/GeminiProvider.js +126 -0
  49. package/dist/providers/GeminiProvider.js.map +1 -0
  50. package/dist/providers/OpenAIProvider.d.ts +42 -0
  51. package/dist/providers/OpenAIProvider.d.ts.map +1 -0
  52. package/dist/providers/OpenAIProvider.js +164 -0
  53. package/dist/providers/OpenAIProvider.js.map +1 -0
  54. package/dist/providers/OpenRouterProvider.d.ts +46 -0
  55. package/dist/providers/OpenRouterProvider.d.ts.map +1 -0
  56. package/dist/providers/OpenRouterProvider.js +171 -0
  57. package/dist/providers/OpenRouterProvider.js.map +1 -0
  58. package/dist/types/agent.d.ts +105 -0
  59. package/dist/types/agent.d.ts.map +1 -0
  60. package/dist/types/agent.js +18 -0
  61. package/dist/types/agent.js.map +1 -0
  62. package/dist/types/ai.d.ts +78 -0
  63. package/dist/types/ai.d.ts.map +1 -0
  64. package/dist/types/ai.js +5 -0
  65. package/dist/types/ai.js.map +1 -0
  66. package/dist/types/history.d.ts +112 -0
  67. package/dist/types/history.d.ts.map +1 -0
  68. package/dist/types/history.js +34 -0
  69. package/dist/types/history.js.map +1 -0
  70. package/dist/types/index.d.ts +14 -0
  71. package/dist/types/index.d.ts.map +1 -0
  72. package/dist/types/index.js +7 -0
  73. package/dist/types/index.js.map +1 -0
  74. package/dist/types/observation.d.ts +25 -0
  75. package/dist/types/observation.d.ts.map +1 -0
  76. package/dist/types/observation.js +5 -0
  77. package/dist/types/observation.js.map +1 -0
  78. package/dist/types/prompt.d.ts +46 -0
  79. package/dist/types/prompt.d.ts.map +1 -0
  80. package/dist/types/prompt.js +16 -0
  81. package/dist/types/prompt.js.map +1 -0
  82. package/dist/types/route.d.ts +59 -0
  83. package/dist/types/route.d.ts.map +1 -0
  84. package/dist/types/route.js +5 -0
  85. package/dist/types/route.js.map +1 -0
  86. package/dist/types/tool.d.ts +46 -0
  87. package/dist/types/tool.d.ts.map +1 -0
  88. package/dist/types/tool.js +5 -0
  89. package/dist/types/tool.js.map +1 -0
  90. package/dist/utils/retry.d.ts +13 -0
  91. package/dist/utils/retry.d.ts.map +1 -0
  92. package/dist/utils/retry.js +70 -0
  93. package/dist/utils/retry.js.map +1 -0
  94. package/docs/API_REFERENCE.md +517 -0
  95. package/docs/CONSTRUCTOR_OPTIONS.md +256 -0
  96. package/docs/CONTRIBUTING.md +481 -0
  97. package/docs/GETTING_STARTED.md +328 -0
  98. package/docs/PROVIDERS.md +472 -0
  99. package/docs/PUBLISHING.md +174 -0
  100. package/docs/README.md +68 -0
  101. package/docs/STRUCTURE.md +32 -0
  102. package/examples/declarative-agent.ts +217 -0
  103. package/examples/healthcare-agent.ts +283 -0
  104. package/examples/openai-agent.ts +167 -0
  105. package/examples/travel-agent.ts +342 -0
  106. package/package.json +73 -0
  107. package/src/constants/index.ts +5 -0
  108. package/src/core/Agent.ts +307 -0
  109. package/src/core/DomainRegistry.ts +50 -0
  110. package/src/core/Events.ts +101 -0
  111. package/src/core/Observation.ts +46 -0
  112. package/src/core/PromptBuilder.ts +511 -0
  113. package/src/core/Route.ts +136 -0
  114. package/src/core/State.ts +153 -0
  115. package/src/core/Tool.ts +54 -0
  116. package/src/core/Transition.ts +66 -0
  117. package/src/index.ts +83 -0
  118. package/src/providers/GeminiProvider.ts +220 -0
  119. package/src/providers/OpenAIProvider.ts +272 -0
  120. package/src/providers/OpenRouterProvider.ts +282 -0
  121. package/src/types/agent.ts +112 -0
  122. package/src/types/ai.ts +85 -0
  123. package/src/types/history.ts +125 -0
  124. package/src/types/index.ts +56 -0
  125. package/src/types/observation.ts +27 -0
  126. package/src/types/prompt.ts +49 -0
  127. package/src/types/route.ts +68 -0
  128. package/src/types/tool.ts +53 -0
  129. package/src/utils/retry.ts +96 -0
@@ -0,0 +1,328 @@
1
+ # Getting Started
2
+
3
+ Complete guide to building your first AI agent with `@falai/agent`.
4
+
5
+ ---
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ # Using bun (recommended)
11
+ bun add @falai/agent
12
+
13
+ # Using npm
14
+ npm install @falai/agent
15
+
16
+ # Using yarn
17
+ yarn add @falai/agent
18
+ ```
19
+
20
+ **Requirements:** Node.js 18+ or Bun 1.0+
21
+
22
+ ---
23
+
24
+ ## Your First Agent (5 minutes)
25
+
26
+ ### 1. Set Up Your Environment
27
+
28
+ ```bash
29
+ # Create a new project
30
+ mkdir my-agent && cd my-agent
31
+ bun init -y
32
+
33
+ # Install dependencies
34
+ bun add @falai/agent
35
+
36
+ # Set your API key
37
+ echo "GEMINI_API_KEY=your_key_here" > .env
38
+ ```
39
+
40
+ ### 2. Create Your Agent
41
+
42
+ Create `index.ts`:
43
+
44
+ ```typescript
45
+ import {
46
+ Agent,
47
+ GeminiProvider,
48
+ createMessageEvent,
49
+ EventSource,
50
+ } from "@falai/agent";
51
+
52
+ // Define your context type
53
+ interface MyContext {
54
+ userId: string;
55
+ userName: string;
56
+ }
57
+
58
+ // Create AI provider
59
+ const ai = new GeminiProvider({
60
+ apiKey: process.env.GEMINI_API_KEY!,
61
+ });
62
+
63
+ // Create your agent
64
+ const agent = new Agent<MyContext>({
65
+ name: "MyBot",
66
+ description: "A helpful assistant",
67
+ ai,
68
+ context: {
69
+ userId: "user_123",
70
+ userName: "Alice",
71
+ },
72
+ });
73
+
74
+ // Add a guideline
75
+ agent.createGuideline({
76
+ condition: "User asks for help",
77
+ action: "Be friendly and offer specific assistance",
78
+ });
79
+
80
+ // Generate a response
81
+ const response = await agent.respond({
82
+ history: [
83
+ createMessageEvent(EventSource.CUSTOMER, "Alice", "Hi, I need help!"),
84
+ ],
85
+ });
86
+
87
+ console.log("Agent:", response.message);
88
+ ```
89
+
90
+ ### 3. Run It!
91
+
92
+ ```bash
93
+ bun run index.ts
94
+ ```
95
+
96
+ 🎉 **That's it!** You've created your first AI agent.
97
+
98
+ ---
99
+
100
+ ## Next Steps
101
+
102
+ ### Add a Domain Glossary
103
+
104
+ Help your agent understand your business:
105
+
106
+ ```typescript
107
+ agent
108
+ .createTerm({
109
+ name: "Premium Plan",
110
+ description: "Our top-tier subscription at $99/month",
111
+ synonyms: ["pro plan", "premium subscription"],
112
+ })
113
+ .createTerm({
114
+ name: "SLA",
115
+ description: "Service Level Agreement - our response time guarantee",
116
+ });
117
+ ```
118
+
119
+ ### Create Tools
120
+
121
+ Give your agent superpowers:
122
+
123
+ ```typescript
124
+ import { defineTool } from "@falai/agent";
125
+
126
+ const checkBalance = defineTool<MyContext, [accountId: string], number>(
127
+ "check_balance",
128
+ async ({ context }, accountId) => {
129
+ // Your logic here
130
+ const balance = await fetchBalance(accountId);
131
+ return { data: balance };
132
+ },
133
+ { description: "Checks account balance" }
134
+ );
135
+
136
+ agent.createGuideline({
137
+ condition: "User asks about their balance",
138
+ action: "Use the check_balance tool to retrieve current balance",
139
+ tools: [checkBalance],
140
+ });
141
+ ```
142
+
143
+ ### Build Conversation Routes
144
+
145
+ Create structured flows:
146
+
147
+ ```typescript
148
+ import { END_ROUTE } from "@falai/agent";
149
+
150
+ const onboardingRoute = agent.createRoute({
151
+ title: "User Onboarding",
152
+ description: "Guide new users through setup",
153
+ conditions: ["User is new and needs onboarding"],
154
+ });
155
+
156
+ // Build the flow
157
+ const step1 = onboardingRoute.initialState.transitionTo({
158
+ chatState: "Ask for user's name",
159
+ });
160
+
161
+ const step2 = step1.target.transitionTo({
162
+ chatState: "Ask for user's email",
163
+ });
164
+
165
+ const step3 = step2.target.transitionTo({
166
+ chatState: "Confirm details and welcome user",
167
+ });
168
+
169
+ // End the route
170
+ step3.target.transitionTo({ state: END_ROUTE });
171
+ ```
172
+
173
+ ### Handle Context Dynamically
174
+
175
+ Override context per request:
176
+
177
+ ```typescript
178
+ const response = await agent.respond({
179
+ history,
180
+ contextOverride: {
181
+ userName: "Bob", // Temporarily change user
182
+ },
183
+ });
184
+ ```
185
+
186
+ ---
187
+
188
+ ## Common Patterns
189
+
190
+ ### Declarative Configuration
191
+
192
+ Load everything from config:
193
+
194
+ ```typescript
195
+ const agent = new Agent({
196
+ name: "ConfiguredBot",
197
+ ai: provider,
198
+ terms: loadTermsFromFile(),
199
+ guidelines: loadGuidelinesFromDB(),
200
+ routes: [
201
+ {
202
+ title: "Support",
203
+ conditions: ["User needs support"],
204
+ guidelines: [
205
+ { condition: "Issue is urgent", action: "Escalate immediately" },
206
+ ],
207
+ },
208
+ ],
209
+ });
210
+ ```
211
+
212
+ ### Conditional Guidelines
213
+
214
+ Add guidelines based on runtime conditions:
215
+
216
+ ```typescript
217
+ if (user.isPremium) {
218
+ agent.createGuideline({
219
+ condition: "User asks about premium features",
220
+ action: "Provide detailed premium feature explanations",
221
+ tags: ["premium"],
222
+ });
223
+ }
224
+ ```
225
+
226
+ ### Multi-Turn Conversations
227
+
228
+ Build up conversation history:
229
+
230
+ ```typescript
231
+ const history: Event[] = [];
232
+
233
+ // Turn 1
234
+ history.push(createMessageEvent(EventSource.CUSTOMER, "User", "Hello"));
235
+ const response1 = await agent.respond({ history });
236
+ history.push(
237
+ createMessageEvent(EventSource.AI_AGENT, "Bot", response1.message)
238
+ );
239
+
240
+ // Turn 2
241
+ history.push(createMessageEvent(EventSource.CUSTOMER, "User", "Tell me more"));
242
+ const response2 = await agent.respond({ history });
243
+ history.push(
244
+ createMessageEvent(EventSource.AI_AGENT, "Bot", response2.message)
245
+ );
246
+ ```
247
+
248
+ ---
249
+
250
+ ## Best Practices
251
+
252
+ ### ✅ Do's
253
+
254
+ - **Use TypeScript** - Full type safety and IntelliSense
255
+ - **Define custom context** - Strongly typed context for your domain
256
+ - **Add glossary terms** - Help the agent understand your terminology
257
+ - **Use specific guidelines** - Clear conditions and actions
258
+ - **Enable/disable guidelines** - Use the `enabled` flag for A/B testing
259
+ - **Tag your guidelines** - Organize with tags for filtering
260
+
261
+ ### ❌ Don'ts
262
+
263
+ - **Don't use `any` for context** - Define proper types
264
+ - **Don't skip error handling** - Wrap agent calls in try/catch
265
+ - **Don't forget to set API key** - Use environment variables
266
+ - **Don't create overly complex routes** - Keep flows simple and clear
267
+ - **Don't ignore linter warnings** - Fix TypeScript errors
268
+
269
+ ---
270
+
271
+ ## Troubleshooting
272
+
273
+ ### "Cannot find module '@falai/agent'"
274
+
275
+ Make sure you've installed the package:
276
+
277
+ ```bash
278
+ bun add @falai/agent
279
+ ```
280
+
281
+ ### "API Key not found"
282
+
283
+ Set your environment variable:
284
+
285
+ ```bash
286
+ export GEMINI_API_KEY="your_key_here"
287
+ ```
288
+
289
+ ### TypeScript Errors
290
+
291
+ Ensure your `tsconfig.json` has:
292
+
293
+ ```json
294
+ {
295
+ "compilerOptions": {
296
+ "moduleResolution": "bundler",
297
+ "esModuleInterop": true,
298
+ "strict": true
299
+ }
300
+ }
301
+ ```
302
+
303
+ ### AI Response Timeout
304
+
305
+ Increase timeout in provider config:
306
+
307
+ ```typescript
308
+ new GeminiProvider({
309
+ apiKey: "...",
310
+ retryConfig: {
311
+ timeout: 120000, // 2 minutes
312
+ retries: 5,
313
+ },
314
+ });
315
+ ```
316
+
317
+ ---
318
+
319
+ ## What's Next?
320
+
321
+ - 📚 Read the [Constructor Options Guide](./CONSTRUCTOR_OPTIONS.md)
322
+ - 🔍 Check the [API Reference](./API_REFERENCE.md)
323
+ - 🏗️ Understand the [Architecture](./STRUCTURE.md)
324
+ - 🎯 Explore [Examples](../examples/)
325
+
326
+ ---
327
+
328
+ **Made with ❤️ for the community**