@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,517 @@
1
+ # API Reference
2
+
3
+ Complete API documentation for `@falai/agent`.
4
+
5
+ ---
6
+
7
+ ## Core Classes
8
+
9
+ ### `Agent<TContext>`
10
+
11
+ Main agent class for managing conversational AI.
12
+
13
+ #### Constructor
14
+
15
+ ```typescript
16
+ new Agent<TContext>(options: AgentOptions<TContext>)
17
+ ```
18
+
19
+ See [Constructor Options](./CONSTRUCTOR_OPTIONS.md) for full details.
20
+
21
+ #### Methods
22
+
23
+ ##### `createRoute(options: RouteOptions): Route`
24
+
25
+ Creates a new conversation route.
26
+
27
+ ##### `createTerm(term: Term): this`
28
+
29
+ Adds a domain glossary term. Returns `this` for chaining.
30
+
31
+ ##### `createGuideline(guideline: Guideline): this`
32
+
33
+ Adds a behavioral guideline. Returns `this` for chaining.
34
+
35
+ ##### `createCapability(capability: Capability): this`
36
+
37
+ Adds an agent capability. Returns `this` for chaining.
38
+
39
+ ##### `createObservation(description: string): Observation`
40
+
41
+ Creates an observation for disambiguation.
42
+
43
+ ##### `addDomain<TName, TDomain>(name: TName, domainObject: TDomain): this`
44
+
45
+ Registers a domain with tools/methods. Returns `this` for chaining.
46
+
47
+ ##### `respond(input: RespondInput<TContext>): Promise<RespondOutput>`
48
+
49
+ Generates an AI response based on conversation history.
50
+
51
+ ```typescript
52
+ interface RespondInput<TContext> {
53
+ history: Event[];
54
+ contextOverride?: Partial<TContext>;
55
+ }
56
+
57
+ interface RespondOutput {
58
+ message: string;
59
+ // ... additional fields
60
+ }
61
+ ```
62
+
63
+ #### Properties
64
+
65
+ ##### `name: string`
66
+
67
+ Agent's name (readonly).
68
+
69
+ ##### `description?: string`
70
+
71
+ Agent's description (readonly).
72
+
73
+ ##### `goal?: string`
74
+
75
+ Agent's goal (readonly).
76
+
77
+ ##### `domain: Record<string, Record<string, unknown>>`
78
+
79
+ Dynamic domain registry access.
80
+
81
+ ---
82
+
83
+ ### `Route`
84
+
85
+ Represents a conversation flow with states and transitions.
86
+
87
+ #### Constructor
88
+
89
+ ```typescript
90
+ new Route(options: RouteOptions)
91
+ ```
92
+
93
+ #### Methods
94
+
95
+ ##### `createGuideline(guideline: Guideline): this`
96
+
97
+ Adds a guideline specific to this route. Returns `this` for chaining.
98
+
99
+ ##### `getGuidelines(): Guideline[]`
100
+
101
+ Returns all guidelines for this route.
102
+
103
+ ##### `getRef(): RouteRef`
104
+
105
+ Returns a reference to this route.
106
+
107
+ ##### `toRef(): RouteRef`
108
+
109
+ Returns a reference including the route instance (for disambiguation).
110
+
111
+ ##### `describe(): string`
112
+
113
+ Generates a description of this route's structure.
114
+
115
+ #### Properties
116
+
117
+ ##### `id: string`
118
+
119
+ Unique route identifier (readonly).
120
+
121
+ ##### `title: string`
122
+
123
+ Route title (readonly).
124
+
125
+ ##### `description?: string`
126
+
127
+ Route description (readonly).
128
+
129
+ ##### `conditions: string[]`
130
+
131
+ Conditions that trigger this route (readonly).
132
+
133
+ ##### `initialState: State`
134
+
135
+ Starting state of the route (readonly).
136
+
137
+ ---
138
+
139
+ ### `State`
140
+
141
+ Represents a state within a conversation route.
142
+
143
+ #### Methods
144
+
145
+ ##### `transitionTo(spec: TransitionSpec, condition?: string): TransitionResult`
146
+
147
+ Creates a transition from this state.
148
+
149
+ ```typescript
150
+ interface TransitionSpec {
151
+ chatState?: string; // Transition to a chat interaction
152
+ toolState?: ToolRef; // Transition to execute a tool
153
+ state?: StateRef | symbol; // Transition to specific state or END_ROUTE
154
+ }
155
+ ```
156
+
157
+ ##### `addGuideline(guideline: Guideline): void`
158
+
159
+ Adds a guideline specific to this state.
160
+
161
+ #### Properties
162
+
163
+ ##### `id: string`
164
+
165
+ Unique state identifier (readonly).
166
+
167
+ ##### `routeId: string`
168
+
169
+ ID of the route this state belongs to (readonly).
170
+
171
+ ##### `description: string`
172
+
173
+ State description (readonly).
174
+
175
+ ---
176
+
177
+ ### `Observation`
178
+
179
+ Handles disambiguation between multiple routes.
180
+
181
+ #### Constructor
182
+
183
+ ```typescript
184
+ new Observation(options: ObservationOptions)
185
+ ```
186
+
187
+ #### Methods
188
+
189
+ ##### `disambiguate(routes: (Route | RouteRef)[]): this`
190
+
191
+ Sets routes this observation can disambiguate between. Returns `this` for chaining.
192
+
193
+ ##### `getRoutes(): RouteRef[]`
194
+
195
+ Returns routes associated with this observation.
196
+
197
+ #### Properties
198
+
199
+ ##### `id: string`
200
+
201
+ Unique identifier (readonly).
202
+
203
+ ##### `description: string`
204
+
205
+ Observation description (readonly).
206
+
207
+ ---
208
+
209
+ ### `GeminiProvider`
210
+
211
+ AI provider implementation for Google Gemini.
212
+
213
+ #### Constructor
214
+
215
+ ```typescript
216
+ new GeminiProvider(options: GeminiProviderOptions)
217
+
218
+ interface GeminiProviderOptions {
219
+ apiKey: string;
220
+ model?: string; // Default: "models/gemini-2.5-flash"
221
+ backupModels?: string[]; // Fallback models
222
+ retryConfig?: {
223
+ timeout?: number; // Default: 60000ms
224
+ retries?: number; // Default: 3
225
+ };
226
+ }
227
+ ```
228
+
229
+ #### Methods
230
+
231
+ ##### `generateMessage<TContext>(input: GenerateMessageInput<TContext>): Promise<GenerateMessageOutput>`
232
+
233
+ Generates a message with retry logic and backup models.
234
+
235
+ ---
236
+
237
+ ### `PromptBuilder`
238
+
239
+ Constructs prompts for AI generation.
240
+
241
+ #### Methods
242
+
243
+ ##### `addIdentity(name: string, description?: string, goal?: string): this`
244
+
245
+ Adds agent identity section.
246
+
247
+ ##### `addContext(variables: ContextVariable[]): this`
248
+
249
+ Adds context variables.
250
+
251
+ ##### `addGlossary(terms: Term[]): this`
252
+
253
+ Adds domain glossary terms.
254
+
255
+ ##### `addGuidelinesForMessageGeneration(guidelines: GuidelineMatch[]): this`
256
+
257
+ Adds guidelines section.
258
+
259
+ ##### `addCapabilitiesForMessageGeneration(capabilities: Capability[]): this`
260
+
261
+ Adds capabilities section.
262
+
263
+ ##### `addObservations(observations): this`
264
+
265
+ Adds observations for disambiguation.
266
+
267
+ ##### `addActiveRoutes(routes): this`
268
+
269
+ Adds active routes to the prompt.
270
+
271
+ ##### `build(): string`
272
+
273
+ Builds the final prompt string.
274
+
275
+ ---
276
+
277
+ ## Utility Functions
278
+
279
+ ### `defineTool<TContext, TArgs, TReturn>()`
280
+
281
+ Defines a type-safe tool.
282
+
283
+ ```typescript
284
+ defineTool<TContext, TArgs extends unknown[], TReturn>(
285
+ name: string,
286
+ handler: ToolHandler<TContext, TArgs, TReturn>,
287
+ options?: {
288
+ description?: string;
289
+ metadata?: Record<string, unknown>;
290
+ }
291
+ ): ToolRef<TContext, TArgs, TReturn>
292
+ ```
293
+
294
+ **Example:**
295
+
296
+ ```typescript
297
+ const getTool = defineTool<MyContext, [id: string], Data>(
298
+ "get_data",
299
+ async ({ context }, id) => {
300
+ return { data: await fetchData(id) };
301
+ },
302
+ { description: "Fetches data by ID" }
303
+ );
304
+ ```
305
+
306
+ ---
307
+
308
+ ### `createMessageEvent()`
309
+
310
+ Creates a message event for conversation history.
311
+
312
+ ```typescript
313
+ createMessageEvent(
314
+ source: EventSource,
315
+ name: string,
316
+ message: string
317
+ ): Event
318
+ ```
319
+
320
+ ---
321
+
322
+ ### `createToolEvent()`
323
+
324
+ Creates a tool execution event.
325
+
326
+ ```typescript
327
+ createToolEvent(
328
+ toolName: string,
329
+ data: unknown
330
+ ): Event
331
+ ```
332
+
333
+ ---
334
+
335
+ ### `adaptEvent()`
336
+
337
+ Adapts an event to the internal format.
338
+
339
+ ```typescript
340
+ adaptEvent(event: EmittedEvent): Event
341
+ ```
342
+
343
+ ---
344
+
345
+ ## Type Definitions
346
+
347
+ ### `Term`
348
+
349
+ ```typescript
350
+ interface Term {
351
+ name: string;
352
+ description: string;
353
+ synonyms?: string[];
354
+ }
355
+ ```
356
+
357
+ ---
358
+
359
+ ### `Guideline`
360
+
361
+ ```typescript
362
+ interface Guideline {
363
+ id?: string;
364
+ condition?: string;
365
+ action: string;
366
+ enabled?: boolean; // Default: true
367
+ tags?: string[];
368
+ tools?: ToolRef[];
369
+ metadata?: Record<string, unknown>;
370
+ }
371
+ ```
372
+
373
+ ---
374
+
375
+ ### `Capability`
376
+
377
+ ```typescript
378
+ interface Capability {
379
+ id?: string;
380
+ title: string;
381
+ description: string;
382
+ tools?: ToolRef[];
383
+ }
384
+ ```
385
+
386
+ ---
387
+
388
+ ### `Event`
389
+
390
+ ```typescript
391
+ interface Event {
392
+ kind: EventKind;
393
+ source: EventSource;
394
+ data: MessageEventData | ToolEventData | StatusEventData;
395
+ creation_utc: string;
396
+ }
397
+
398
+ enum EventKind {
399
+ MESSAGE = "message",
400
+ TOOL = "tool",
401
+ STATUS = "status",
402
+ }
403
+
404
+ enum EventSource {
405
+ CUSTOMER = "customer",
406
+ AI_AGENT = "agent",
407
+ SYSTEM = "system",
408
+ }
409
+ ```
410
+
411
+ ---
412
+
413
+ ### `ToolContext<TContext>`
414
+
415
+ ```typescript
416
+ interface ToolContext<TContext> {
417
+ context: TContext;
418
+ // Additional runtime context
419
+ }
420
+ ```
421
+
422
+ ---
423
+
424
+ ### `ToolResult<TReturn>`
425
+
426
+ ```typescript
427
+ interface ToolResult<TReturn> {
428
+ data: TReturn;
429
+ error?: string;
430
+ }
431
+ ```
432
+
433
+ ---
434
+
435
+ ## Constants
436
+
437
+ ### `END_ROUTE`
438
+
439
+ Symbol marking the end of a conversation route.
440
+
441
+ ```typescript
442
+ import { END_ROUTE } from "@falai/agent";
443
+
444
+ state.transitionTo({ state: END_ROUTE });
445
+ ```
446
+
447
+ ---
448
+
449
+ ## Enums
450
+
451
+ ### `CompositionMode`
452
+
453
+ ```typescript
454
+ enum CompositionMode {
455
+ FLUID = "fluid",
456
+ CANNED_FLUID = "canned_fluid",
457
+ CANNED_COMPOSITED = "composited_canned",
458
+ CANNED_STRICT = "strict_canned",
459
+ }
460
+ ```
461
+
462
+ ---
463
+
464
+ ### `BuiltInSection`
465
+
466
+ Prompt builder section identifiers.
467
+
468
+ ```typescript
469
+ enum BuiltInSection {
470
+ IDENTITY = "identity",
471
+ GLOSSARY = "glossary",
472
+ CONTEXT = "context",
473
+ GUIDELINES = "guidelines",
474
+ CAPABILITIES = "capabilities",
475
+ OBSERVATIONS = "observations",
476
+ ACTIVE_ROUTES = "active_routes",
477
+ }
478
+ ```
479
+
480
+ ---
481
+
482
+ ## Advanced Topics
483
+
484
+ ### Custom AI Providers
485
+
486
+ Implement the `AiProvider` interface:
487
+
488
+ ```typescript
489
+ interface AiProvider {
490
+ generateMessage<TContext = unknown>(
491
+ input: GenerateMessageInput<TContext>
492
+ ): Promise<GenerateMessageOutput>;
493
+ }
494
+ ```
495
+
496
+ ### Domain Registry
497
+
498
+ Organize tools by domain:
499
+
500
+ ```typescript
501
+ const paymentDomain = {
502
+ processPayment: async (amount: number) => {
503
+ /*...*/
504
+ },
505
+ refund: async (transactionId: string) => {
506
+ /*...*/
507
+ },
508
+ };
509
+
510
+ agent.addDomain("payment", paymentDomain);
511
+
512
+ // Access via agent.domain.payment.processPayment()
513
+ ```
514
+
515
+ ---
516
+
517
+ **Made with ❤️ for the community**