@falai/agent 0.2.0 → 0.3.10

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 (62) hide show
  1. package/README.md +160 -11
  2. package/dist/cjs/core/Agent.d.ts +12 -0
  3. package/dist/cjs/core/Agent.d.ts.map +1 -1
  4. package/dist/cjs/core/Agent.js +37 -1
  5. package/dist/cjs/core/Agent.js.map +1 -1
  6. package/dist/cjs/core/DomainRegistry.d.ts +10 -0
  7. package/dist/cjs/core/DomainRegistry.d.ts.map +1 -1
  8. package/dist/cjs/core/DomainRegistry.js +25 -0
  9. package/dist/cjs/core/DomainRegistry.js.map +1 -1
  10. package/dist/cjs/core/PromptBuilder.d.ts +9 -1
  11. package/dist/cjs/core/PromptBuilder.d.ts.map +1 -1
  12. package/dist/cjs/core/PromptBuilder.js +49 -2
  13. package/dist/cjs/core/PromptBuilder.js.map +1 -1
  14. package/dist/cjs/core/Route.d.ts +16 -0
  15. package/dist/cjs/core/Route.d.ts.map +1 -1
  16. package/dist/cjs/core/Route.js +22 -0
  17. package/dist/cjs/core/Route.js.map +1 -1
  18. package/dist/cjs/core/State.d.ts +1 -1
  19. package/dist/cjs/core/State.d.ts.map +1 -1
  20. package/dist/cjs/core/State.js +4 -10
  21. package/dist/cjs/core/State.js.map +1 -1
  22. package/dist/cjs/types/route.d.ts +10 -6
  23. package/dist/cjs/types/route.d.ts.map +1 -1
  24. package/dist/core/Agent.d.ts +12 -0
  25. package/dist/core/Agent.d.ts.map +1 -1
  26. package/dist/core/Agent.js +37 -1
  27. package/dist/core/Agent.js.map +1 -1
  28. package/dist/core/DomainRegistry.d.ts +10 -0
  29. package/dist/core/DomainRegistry.d.ts.map +1 -1
  30. package/dist/core/DomainRegistry.js +25 -0
  31. package/dist/core/DomainRegistry.js.map +1 -1
  32. package/dist/core/PromptBuilder.d.ts +9 -1
  33. package/dist/core/PromptBuilder.d.ts.map +1 -1
  34. package/dist/core/PromptBuilder.js +49 -2
  35. package/dist/core/PromptBuilder.js.map +1 -1
  36. package/dist/core/Route.d.ts +16 -0
  37. package/dist/core/Route.d.ts.map +1 -1
  38. package/dist/core/Route.js +22 -0
  39. package/dist/core/Route.js.map +1 -1
  40. package/dist/core/State.d.ts +1 -1
  41. package/dist/core/State.d.ts.map +1 -1
  42. package/dist/core/State.js +4 -10
  43. package/dist/core/State.js.map +1 -1
  44. package/dist/types/route.d.ts +10 -6
  45. package/dist/types/route.d.ts.map +1 -1
  46. package/docs/API_REFERENCE.md +124 -3
  47. package/docs/CONSTRUCTOR_OPTIONS.md +178 -37
  48. package/docs/GETTING_STARTED.md +13 -5
  49. package/examples/business-onboarding.ts +707 -0
  50. package/examples/domain-scoping.ts +266 -0
  51. package/examples/healthcare-agent.ts +15 -15
  52. package/examples/openai-agent.ts +2 -2
  53. package/examples/persistent-onboarding.ts +7 -7
  54. package/examples/rules-prohibitions.ts +258 -0
  55. package/examples/travel-agent.ts +17 -17
  56. package/package.json +1 -1
  57. package/src/core/Agent.ts +46 -1
  58. package/src/core/DomainRegistry.ts +30 -0
  59. package/src/core/PromptBuilder.ts +70 -3
  60. package/src/core/Route.ts +28 -0
  61. package/src/core/State.ts +6 -25
  62. package/src/types/route.ts +13 -9
@@ -162,20 +162,20 @@ async function createTravelAgent() {
162
162
  chatState: "Ask about the destination",
163
163
  });
164
164
 
165
- const t1 = t0.target.transitionTo({
165
+ const t1 = t0.transitionTo({
166
166
  chatState: "Ask about preferred travel dates",
167
167
  });
168
168
 
169
- const t2 = t1.target.transitionTo({
169
+ const t2 = t1.transitionTo({
170
170
  toolState: getAvailableFlights,
171
171
  });
172
172
 
173
- const t3 = t2.target.transitionTo({
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.target.transitionTo(
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.target.transitionTo(
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.target.transitionTo({
193
+ const t6 = t5.transitionTo({
194
194
  chatState: "Provide confirmation number and booking summary",
195
195
  });
196
196
 
197
- t6.target.transitionTo({ state: END_ROUTE });
197
+ t6.transitionTo({ state: END_ROUTE });
198
198
 
199
199
  // Alternative path: no flights work
200
- const t7 = t3.target.transitionTo(
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.target.transitionTo({
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.target.transitionTo(
212
+ t8.transitionTo(
213
213
  {
214
- state: t4.target,
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.target.transitionTo(
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.target.transitionTo({ state: END_ROUTE });
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.target.transitionTo({
256
+ const s1 = s0.transitionTo({
257
257
  toolState: getBookingStatus,
258
258
  });
259
259
 
260
- s1.target.transitionTo(
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.target.transitionTo(
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.target.transitionTo(
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@falai/agent",
3
- "version": "0.2.0",
3
+ "version": "0.3.10",
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",
package/src/core/Agent.ts CHANGED
@@ -295,17 +295,26 @@ export class Agent<TContext = unknown> {
295
295
  }
296
296
  }
297
297
 
298
- // Add active routes
298
+ // Add active routes with their rules and prohibitions
299
299
  if (this.routes.length > 0) {
300
300
  promptBuilder.addActiveRoutes(
301
301
  this.routes.map((r) => ({
302
302
  title: r.title,
303
303
  description: r.description,
304
304
  conditions: r.conditions,
305
+ domains: r.getDomains(),
306
+ rules: r.getRules(),
307
+ prohibitions: r.getProhibitions(),
305
308
  }))
306
309
  );
307
310
  }
308
311
 
312
+ // Add domains (tools) information if any domains are registered
313
+ const allDomains = this.domainRegistry.all();
314
+ if (Object.keys(allDomains).length > 0) {
315
+ promptBuilder.addDomains(allDomains);
316
+ }
317
+
309
318
  // Add JSON response schema instructions
310
319
  promptBuilder.addJsonResponseSchema();
311
320
 
@@ -414,4 +423,40 @@ export class Agent<TContext = unknown> {
414
423
  getDomainRegistry(): DomainRegistry {
415
424
  return this.domainRegistry;
416
425
  }
426
+
427
+ /**
428
+ * Get allowed domains for a specific route
429
+ * @param routeId - Route ID to check
430
+ * @returns Filtered domains object, or all domains if route has no restrictions
431
+ */
432
+ getDomainsForRoute(routeId: string): Record<string, Record<string, unknown>> {
433
+ const route = this.routes.find((r) => r.id === routeId);
434
+
435
+ if (!route) {
436
+ // Route not found, return all domains
437
+ return this.domainRegistry.all();
438
+ }
439
+
440
+ const allowedDomains = route.getDomains();
441
+ return this.domainRegistry.getFiltered(allowedDomains);
442
+ }
443
+
444
+ /**
445
+ * Get allowed domains for a specific route by title
446
+ * @param routeTitle - Route title to check
447
+ * @returns Filtered domains object, or all domains if route has no restrictions
448
+ */
449
+ getDomainsForRouteByTitle(
450
+ routeTitle: string
451
+ ): Record<string, Record<string, unknown>> {
452
+ const route = this.routes.find((r) => r.title === routeTitle);
453
+
454
+ if (!route) {
455
+ // Route not found, return all domains
456
+ return this.domainRegistry.all();
457
+ }
458
+
459
+ const allowedDomains = route.getDomains();
460
+ return this.domainRegistry.getFiltered(allowedDomains);
461
+ }
417
462
  }
@@ -47,4 +47,34 @@ export class DomainRegistry {
47
47
  }
48
48
  return result;
49
49
  }
50
+
51
+ /**
52
+ * Get filtered domains by names
53
+ * @param allowedNames - Array of domain names to include (undefined = all domains)
54
+ * @returns Object with only the specified domains
55
+ */
56
+ getFiltered(
57
+ allowedNames?: string[]
58
+ ): Record<string, Record<string, unknown>> {
59
+ // If no filter specified, return all domains
60
+ if (!allowedNames) {
61
+ return this.all();
62
+ }
63
+
64
+ const result: Record<string, Record<string, unknown>> = {};
65
+ for (const name of allowedNames) {
66
+ const domain = this.domains.get(name);
67
+ if (domain) {
68
+ result[name] = domain;
69
+ }
70
+ }
71
+ return result;
72
+ }
73
+
74
+ /**
75
+ * Get list of all registered domain names
76
+ */
77
+ getDomainNames(): string[] {
78
+ return Array.from(this.domains.keys());
79
+ }
50
80
  }
@@ -26,6 +26,7 @@ export enum BuiltInSection {
26
26
  OBSERVATIONS = "observations",
27
27
  CAPABILITIES = "capabilities",
28
28
  ACTIVE_ROUTES = "active_routes",
29
+ DOMAINS = "domains",
29
30
  }
30
31
 
31
32
  /**
@@ -431,7 +432,14 @@ When you detect any of these situations, consider which route would be most appr
431
432
  * Add active routes information
432
433
  */
433
434
  addActiveRoutes(
434
- routes: Array<{ title: string; description?: string; conditions: string[] }>
435
+ routes: Array<{
436
+ title: string;
437
+ description?: string;
438
+ conditions: string[];
439
+ domains?: string[];
440
+ rules?: string[];
441
+ prohibitions?: string[];
442
+ }>
435
443
  ): this {
436
444
  if (routes.length > 0) {
437
445
  const routesString = routes
@@ -441,7 +449,29 @@ When you detect any of these situations, consider which route would be most appr
441
449
  ? `\n Triggered when: ${route.conditions.join(" OR ")}`
442
450
  : "";
443
451
  const desc = route.description ? `\n ${route.description}` : "";
444
- return `${i + 1}) ${route.title}${desc}${conditions}`;
452
+
453
+ let domainInfo = "";
454
+ if (route.domains !== undefined) {
455
+ if (route.domains.length === 0) {
456
+ domainInfo = "\n Available tools: None (conversation only)";
457
+ } else {
458
+ domainInfo = `\n Available tools: ${route.domains.join(", ")}`;
459
+ }
460
+ }
461
+
462
+ let rulesInfo = "";
463
+ if (route.rules && route.rules.length > 0) {
464
+ const rulesList = route.rules.map((r, idx) => `${idx + 1}. ${r}`).join("; ");
465
+ rulesInfo = `\n RULES: ${rulesList}`;
466
+ }
467
+
468
+ let prohibitionsInfo = "";
469
+ if (route.prohibitions && route.prohibitions.length > 0) {
470
+ const prohibitionsList = route.prohibitions.map((p, idx) => `${idx + 1}. ${p}`).join("; ");
471
+ prohibitionsInfo = `\n PROHIBITIONS: ${prohibitionsList}`;
472
+ }
473
+
474
+ return `${i + 1}) ${route.title}${desc}${conditions}${domainInfo}${rulesInfo}${prohibitionsInfo}`;
445
475
  })
446
476
  .join("\n\n");
447
477
 
@@ -452,7 +482,11 @@ When you detect any of these situations, consider which route would be most appr
452
482
  {routes_string}
453
483
  ###
454
484
 
455
- These routes represent different paths the conversation can take. Choose the most appropriate route based on the user's needs.`,
485
+ These routes represent different paths the conversation can take. Choose the most appropriate route based on the user's needs.
486
+ IMPORTANT:
487
+ - If a route specifies available tools, you can ONLY call tools from those domains when following that route.
488
+ - If a route has RULES, you MUST follow them when you choose that route.
489
+ - If a route has PROHIBITIONS, you MUST NEVER do those things when you choose that route.`,
456
490
  { routes_string: routesString },
457
491
  SectionStatus.ACTIVE
458
492
  );
@@ -460,6 +494,39 @@ These routes represent different paths the conversation can take. Choose the mos
460
494
  return this;
461
495
  }
462
496
 
497
+ /**
498
+ * Add domains (tools) information
499
+ */
500
+ addDomains(
501
+ domains: Record<string, Record<string, unknown>>
502
+ ): this {
503
+ const domainNames = Object.keys(domains);
504
+ if (domainNames.length > 0) {
505
+ const domainsString = domainNames
506
+ .map((name, i) => {
507
+ const toolNames = Object.keys(domains[name]);
508
+ const tools = toolNames.join(", ");
509
+ return `${i + 1}) Domain "${name}": ${tools}`;
510
+ })
511
+ .join("\n");
512
+
513
+ this.addSection(
514
+ BuiltInSection.DOMAINS,
515
+ `Available tool domains:
516
+ ###
517
+ {domains_string}
518
+ ###
519
+
520
+ These are the tool domains registered in the system. Each domain contains specific tools/methods.
521
+ When calling tools, use the format: domain.toolName (e.g., "payment.processPayment").`,
522
+ { domains_string: domainsString },
523
+ SectionStatus.ACTIVE
524
+ );
525
+ }
526
+ return this;
527
+ }
528
+
529
+
463
530
  /**
464
531
  * Add JSON response schema instructions
465
532
  */
package/src/core/Route.ts CHANGED
@@ -16,6 +16,9 @@ export class Route<TContext = unknown> {
16
16
  public readonly title: string;
17
17
  public readonly description?: string;
18
18
  public readonly conditions: string[];
19
+ public readonly domains?: string[];
20
+ public readonly rules: string[];
21
+ public readonly prohibitions: string[];
19
22
  public readonly initialState: State<TContext>;
20
23
  private guidelines: Guideline[] = [];
21
24
 
@@ -25,6 +28,9 @@ export class Route<TContext = unknown> {
25
28
  this.title = options.title;
26
29
  this.description = options.description;
27
30
  this.conditions = options.conditions || [];
31
+ this.domains = options.domains;
32
+ this.rules = options.rules || ([] as string[]);
33
+ this.prohibitions = options.prohibitions || ([] as string[]);
28
34
  this.initialState = new State<TContext>(this.id, "Initial state");
29
35
 
30
36
  // Initialize guidelines from options
@@ -54,6 +60,28 @@ export class Route<TContext = unknown> {
54
60
  return [...this.guidelines];
55
61
  }
56
62
 
63
+ /**
64
+ * Get allowed domain names for this route
65
+ * @returns Array of domain names, or undefined if all domains are allowed
66
+ */
67
+ getDomains(): string[] | undefined {
68
+ return this.domains ? [...this.domains] : undefined;
69
+ }
70
+
71
+ /**
72
+ * Get rules for this route
73
+ */
74
+ getRules(): string[] {
75
+ return [...this.rules];
76
+ }
77
+
78
+ /**
79
+ * Get prohibitions for this route
80
+ */
81
+ getProhibitions(): string[] {
82
+ return [...this.prohibitions];
83
+ }
84
+
57
85
  /**
58
86
  * Get route reference
59
87
  */
package/src/core/State.ts CHANGED
@@ -35,7 +35,7 @@ export class State<TContext = unknown> {
35
35
  *
36
36
  * @param spec - Transition specification (chatState, toolState, or direct state)
37
37
  * @param condition - Optional condition for this transition
38
- * @returns Object with target state that supports chaining
38
+ * @returns TransitionResult that supports chaining
39
39
  */
40
40
  transitionTo(
41
41
  spec: TransitionSpec<TContext>,
@@ -55,9 +55,7 @@ export class State<TContext = unknown> {
55
55
  this.transitions.push(endTransition);
56
56
 
57
57
  // Return a terminal state reference
58
- return {
59
- target: this.createTerminalRef(),
60
- };
58
+ return this.createTerminalRef();
61
59
  }
62
60
 
63
61
  // Handle direct state reference
@@ -69,9 +67,7 @@ export class State<TContext = unknown> {
69
67
  );
70
68
  this.transitions.push(transition);
71
69
 
72
- return {
73
- target: this.createStateRefWithTransition(spec.state),
74
- };
70
+ return this.createStateRefWithTransition(spec.state);
75
71
  }
76
72
 
77
73
  // Create new target state for chatState or toolState
@@ -81,12 +77,7 @@ export class State<TContext = unknown> {
81
77
 
82
78
  this.transitions.push(transition);
83
79
 
84
- return {
85
- target: this.createStateRefWithTransition(
86
- targetState.getRef(),
87
- targetState
88
- ),
89
- };
80
+ return this.createStateRefWithTransition(targetState.getRef(), targetState);
90
81
  }
91
82
 
92
83
  /**
@@ -126,12 +117,7 @@ export class State<TContext = unknown> {
126
117
  private createStateRefWithTransition(
127
118
  ref: StateRef,
128
119
  state?: State<TContext>
129
- ): StateRef & {
130
- transitionTo: (
131
- spec: TransitionSpec<TContext>,
132
- condition?: string
133
- ) => TransitionResult<TContext>;
134
- } {
120
+ ): TransitionResult<TContext> {
135
121
  const stateInstance = state || this;
136
122
 
137
123
  return {
@@ -144,12 +130,7 @@ export class State<TContext = unknown> {
144
130
  /**
145
131
  * Create a terminal state reference (for END_ROUTE)
146
132
  */
147
- private createTerminalRef(): StateRef & {
148
- transitionTo: (
149
- spec: TransitionSpec<TContext>,
150
- condition?: string
151
- ) => TransitionResult<TContext>;
152
- } {
133
+ private createTerminalRef(): TransitionResult<TContext> {
153
134
  const terminalRef: StateRef = {
154
135
  id: "END",
155
136
  routeId: this.routeId,
@@ -41,6 +41,12 @@ export interface RouteOptions {
41
41
  conditions?: string[];
42
42
  /** Initial guidelines for this route */
43
43
  guidelines?: Guideline[];
44
+ /** Domain names that are allowed in this route (undefined = all domains) */
45
+ domains?: string[];
46
+ /** Absolute rules the agent must follow in this route */
47
+ rules?: string[];
48
+ /** Absolute prohibitions the agent must never do in this route */
49
+ prohibitions?: string[];
44
50
  }
45
51
 
46
52
  /**
@@ -58,14 +64,12 @@ export interface TransitionSpec<TContext = unknown> {
58
64
 
59
65
  /**
60
66
  * Result of a transition operation
67
+ * Combines state reference with the ability to chain transitions
61
68
  */
62
- export interface TransitionResult<TContext = unknown> {
63
- /** The target state after transition */
64
- target: StateRef & {
65
- /** Allow chaining transitions */
66
- transitionTo: (
67
- spec: TransitionSpec<TContext>,
68
- condition?: string
69
- ) => TransitionResult<TContext>;
70
- };
69
+ export interface TransitionResult<TContext = unknown> extends StateRef {
70
+ /** Allow chaining transitions */
71
+ transitionTo: (
72
+ spec: TransitionSpec<TContext>,
73
+ condition?: string
74
+ ) => TransitionResult<TContext>;
71
75
  }