@falai/agent 0.4.1 → 0.5.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.
Files changed (285) hide show
  1. package/README.md +21 -74
  2. package/dist/cjs/core/Agent.d.ts +22 -29
  3. package/dist/cjs/core/Agent.d.ts.map +1 -1
  4. package/dist/cjs/core/Agent.js +464 -291
  5. package/dist/cjs/core/Agent.js.map +1 -1
  6. package/dist/cjs/core/Events.d.ts +10 -1
  7. package/dist/cjs/core/Events.d.ts.map +1 -1
  8. package/dist/cjs/core/Events.js +3 -2
  9. package/dist/cjs/core/Events.js.map +1 -1
  10. package/dist/cjs/core/PersistenceManager.d.ts +19 -0
  11. package/dist/cjs/core/PersistenceManager.d.ts.map +1 -1
  12. package/dist/cjs/core/PersistenceManager.js +55 -0
  13. package/dist/cjs/core/PersistenceManager.js.map +1 -1
  14. package/dist/cjs/core/PromptComposer.d.ts +24 -0
  15. package/dist/cjs/core/PromptComposer.d.ts.map +1 -0
  16. package/dist/cjs/core/PromptComposer.js +127 -0
  17. package/dist/cjs/core/PromptComposer.js.map +1 -0
  18. package/dist/cjs/core/ResponseEngine.d.ts +19 -0
  19. package/dist/cjs/core/ResponseEngine.d.ts.map +1 -0
  20. package/dist/cjs/core/ResponseEngine.js +51 -0
  21. package/dist/cjs/core/ResponseEngine.js.map +1 -0
  22. package/dist/cjs/core/Route.d.ts +18 -12
  23. package/dist/cjs/core/Route.d.ts.map +1 -1
  24. package/dist/cjs/core/Route.js +15 -9
  25. package/dist/cjs/core/Route.js.map +1 -1
  26. package/dist/cjs/core/RoutingEngine.d.ts +38 -0
  27. package/dist/cjs/core/RoutingEngine.d.ts.map +1 -0
  28. package/dist/cjs/core/RoutingEngine.js +110 -0
  29. package/dist/cjs/core/RoutingEngine.js.map +1 -0
  30. package/dist/cjs/core/State.d.ts +15 -4
  31. package/dist/cjs/core/State.d.ts.map +1 -1
  32. package/dist/cjs/core/State.js +21 -2
  33. package/dist/cjs/core/State.js.map +1 -1
  34. package/dist/cjs/core/ToolExecutor.d.ts +29 -0
  35. package/dist/cjs/core/ToolExecutor.d.ts.map +1 -0
  36. package/dist/cjs/core/ToolExecutor.js +73 -0
  37. package/dist/cjs/core/ToolExecutor.js.map +1 -0
  38. package/dist/cjs/core/Transition.d.ts +5 -5
  39. package/dist/cjs/core/Transition.d.ts.map +1 -1
  40. package/dist/cjs/core/Transition.js.map +1 -1
  41. package/dist/cjs/index.d.ts +6 -8
  42. package/dist/cjs/index.d.ts.map +1 -1
  43. package/dist/cjs/index.js +8 -10
  44. package/dist/cjs/index.js.map +1 -1
  45. package/dist/cjs/providers/AnthropicProvider.d.ts.map +1 -1
  46. package/dist/cjs/providers/AnthropicProvider.js +10 -13
  47. package/dist/cjs/providers/AnthropicProvider.js.map +1 -1
  48. package/dist/cjs/providers/GeminiProvider.d.ts.map +1 -1
  49. package/dist/cjs/providers/GeminiProvider.js +12 -8
  50. package/dist/cjs/providers/GeminiProvider.js.map +1 -1
  51. package/dist/cjs/providers/OpenAIProvider.d.ts.map +1 -1
  52. package/dist/cjs/providers/OpenAIProvider.js +10 -53
  53. package/dist/cjs/providers/OpenAIProvider.js.map +1 -1
  54. package/dist/cjs/providers/OpenRouterProvider.d.ts.map +1 -1
  55. package/dist/cjs/providers/OpenRouterProvider.js +10 -53
  56. package/dist/cjs/providers/OpenRouterProvider.js.map +1 -1
  57. package/dist/cjs/types/agent.d.ts +13 -12
  58. package/dist/cjs/types/agent.d.ts.map +1 -1
  59. package/dist/cjs/types/ai.d.ts +8 -2
  60. package/dist/cjs/types/ai.d.ts.map +1 -1
  61. package/dist/cjs/types/history.d.ts +8 -0
  62. package/dist/cjs/types/history.d.ts.map +1 -1
  63. package/dist/cjs/types/index.d.ts +0 -3
  64. package/dist/cjs/types/index.d.ts.map +1 -1
  65. package/dist/cjs/types/index.js +1 -3
  66. package/dist/cjs/types/index.js.map +1 -1
  67. package/dist/cjs/types/route.d.ts +39 -4
  68. package/dist/cjs/types/route.d.ts.map +1 -1
  69. package/dist/cjs/types/routing.d.ts +16 -0
  70. package/dist/cjs/types/routing.d.ts.map +1 -0
  71. package/dist/cjs/types/routing.js +3 -0
  72. package/dist/cjs/types/routing.js.map +1 -0
  73. package/dist/cjs/types/schema.d.ts +22 -0
  74. package/dist/cjs/types/schema.d.ts.map +1 -0
  75. package/dist/cjs/types/schema.js +3 -0
  76. package/dist/cjs/types/schema.js.map +1 -0
  77. package/dist/cjs/types/session.d.ts +77 -0
  78. package/dist/cjs/types/session.d.ts.map +1 -0
  79. package/dist/cjs/types/session.js +146 -0
  80. package/dist/cjs/types/session.js.map +1 -0
  81. package/dist/cjs/types/tool.d.ts +11 -5
  82. package/dist/cjs/types/tool.d.ts.map +1 -1
  83. package/dist/cjs/utils/id.d.ts +0 -5
  84. package/dist/cjs/utils/id.d.ts.map +1 -1
  85. package/dist/cjs/utils/id.js +0 -10
  86. package/dist/cjs/utils/id.js.map +1 -1
  87. package/dist/cjs/utils/schema.d.ts +17 -0
  88. package/dist/cjs/utils/schema.d.ts.map +1 -0
  89. package/dist/cjs/utils/schema.js +32 -0
  90. package/dist/cjs/utils/schema.js.map +1 -0
  91. package/dist/core/Agent.d.ts +22 -29
  92. package/dist/core/Agent.d.ts.map +1 -1
  93. package/dist/core/Agent.js +464 -291
  94. package/dist/core/Agent.js.map +1 -1
  95. package/dist/core/Events.d.ts +10 -1
  96. package/dist/core/Events.d.ts.map +1 -1
  97. package/dist/core/Events.js +3 -2
  98. package/dist/core/Events.js.map +1 -1
  99. package/dist/core/PersistenceManager.d.ts +19 -0
  100. package/dist/core/PersistenceManager.d.ts.map +1 -1
  101. package/dist/core/PersistenceManager.js +55 -0
  102. package/dist/core/PersistenceManager.js.map +1 -1
  103. package/dist/core/PromptComposer.d.ts +24 -0
  104. package/dist/core/PromptComposer.d.ts.map +1 -0
  105. package/dist/core/PromptComposer.js +123 -0
  106. package/dist/core/PromptComposer.js.map +1 -0
  107. package/dist/core/ResponseEngine.d.ts +19 -0
  108. package/dist/core/ResponseEngine.d.ts.map +1 -0
  109. package/dist/core/ResponseEngine.js +47 -0
  110. package/dist/core/ResponseEngine.js.map +1 -0
  111. package/dist/core/Route.d.ts +18 -12
  112. package/dist/core/Route.d.ts.map +1 -1
  113. package/dist/core/Route.js +15 -9
  114. package/dist/core/Route.js.map +1 -1
  115. package/dist/core/RoutingEngine.d.ts +38 -0
  116. package/dist/core/RoutingEngine.d.ts.map +1 -0
  117. package/dist/core/RoutingEngine.js +106 -0
  118. package/dist/core/RoutingEngine.js.map +1 -0
  119. package/dist/core/State.d.ts +15 -4
  120. package/dist/core/State.d.ts.map +1 -1
  121. package/dist/core/State.js +21 -2
  122. package/dist/core/State.js.map +1 -1
  123. package/dist/core/ToolExecutor.d.ts +29 -0
  124. package/dist/core/ToolExecutor.d.ts.map +1 -0
  125. package/dist/core/ToolExecutor.js +69 -0
  126. package/dist/core/ToolExecutor.js.map +1 -0
  127. package/dist/core/Transition.d.ts +5 -5
  128. package/dist/core/Transition.d.ts.map +1 -1
  129. package/dist/core/Transition.js.map +1 -1
  130. package/dist/index.d.ts +6 -8
  131. package/dist/index.d.ts.map +1 -1
  132. package/dist/index.js +3 -5
  133. package/dist/index.js.map +1 -1
  134. package/dist/providers/AnthropicProvider.d.ts.map +1 -1
  135. package/dist/providers/AnthropicProvider.js +10 -13
  136. package/dist/providers/AnthropicProvider.js.map +1 -1
  137. package/dist/providers/GeminiProvider.d.ts.map +1 -1
  138. package/dist/providers/GeminiProvider.js +12 -8
  139. package/dist/providers/GeminiProvider.js.map +1 -1
  140. package/dist/providers/OpenAIProvider.d.ts.map +1 -1
  141. package/dist/providers/OpenAIProvider.js +10 -53
  142. package/dist/providers/OpenAIProvider.js.map +1 -1
  143. package/dist/providers/OpenRouterProvider.d.ts.map +1 -1
  144. package/dist/providers/OpenRouterProvider.js +10 -53
  145. package/dist/providers/OpenRouterProvider.js.map +1 -1
  146. package/dist/types/agent.d.ts +13 -12
  147. package/dist/types/agent.d.ts.map +1 -1
  148. package/dist/types/ai.d.ts +8 -2
  149. package/dist/types/ai.d.ts.map +1 -1
  150. package/dist/types/history.d.ts +8 -0
  151. package/dist/types/history.d.ts.map +1 -1
  152. package/dist/types/index.d.ts +0 -3
  153. package/dist/types/index.d.ts.map +1 -1
  154. package/dist/types/index.js +0 -1
  155. package/dist/types/index.js.map +1 -1
  156. package/dist/types/route.d.ts +39 -4
  157. package/dist/types/route.d.ts.map +1 -1
  158. package/dist/types/routing.d.ts +16 -0
  159. package/dist/types/routing.d.ts.map +1 -0
  160. package/dist/types/routing.js +2 -0
  161. package/dist/types/routing.js.map +1 -0
  162. package/dist/types/schema.d.ts +22 -0
  163. package/dist/types/schema.d.ts.map +1 -0
  164. package/dist/types/schema.js +2 -0
  165. package/dist/types/schema.js.map +1 -0
  166. package/dist/types/session.d.ts +77 -0
  167. package/dist/types/session.d.ts.map +1 -0
  168. package/dist/types/session.js +138 -0
  169. package/dist/types/session.js.map +1 -0
  170. package/dist/types/tool.d.ts +11 -5
  171. package/dist/types/tool.d.ts.map +1 -1
  172. package/dist/utils/id.d.ts +0 -5
  173. package/dist/utils/id.d.ts.map +1 -1
  174. package/dist/utils/id.js +0 -9
  175. package/dist/utils/id.js.map +1 -1
  176. package/dist/utils/schema.d.ts +17 -0
  177. package/dist/utils/schema.d.ts.map +1 -0
  178. package/dist/utils/schema.js +27 -0
  179. package/dist/utils/schema.js.map +1 -0
  180. package/docs/ADAPTERS.md +83 -3
  181. package/docs/API_REFERENCE.md +329 -104
  182. package/docs/ARCHITECTURE.md +284 -286
  183. package/docs/CONSTRUCTOR_OPTIONS.md +192 -135
  184. package/docs/CONTEXT_MANAGEMENT.md +311 -28
  185. package/docs/CONTRIBUTING.md +1 -1
  186. package/docs/DOMAINS.md +61 -0
  187. package/docs/GETTING_STARTED.md +177 -88
  188. package/docs/PERSISTENCE.md +170 -23
  189. package/docs/README.md +7 -10
  190. package/examples/business-onboarding.ts +21 -9
  191. package/examples/company-qna-agent.ts +508 -0
  192. package/examples/declarative-agent.ts +143 -26
  193. package/examples/domain-scoping.ts +31 -10
  194. package/examples/extracted-data-modification.ts +415 -0
  195. package/examples/healthcare-agent.ts +194 -90
  196. package/examples/openai-agent.ts +67 -25
  197. package/examples/opensearch-persistence.ts +455 -151
  198. package/examples/persistent-onboarding.ts +162 -96
  199. package/examples/prisma-persistence.ts +371 -125
  200. package/examples/redis-persistence.ts +393 -23
  201. package/examples/rules-prohibitions.ts +32 -11
  202. package/examples/streaming-agent.ts +61 -13
  203. package/examples/travel-agent.ts +266 -133
  204. package/package.json +1 -1
  205. package/src/core/Agent.ts +668 -356
  206. package/src/core/Events.ts +12 -2
  207. package/src/core/PersistenceManager.ts +81 -0
  208. package/src/core/PromptComposer.ts +143 -0
  209. package/src/core/ResponseEngine.ts +82 -0
  210. package/src/core/Route.ts +32 -17
  211. package/src/core/RoutingEngine.ts +165 -0
  212. package/src/core/State.ts +55 -15
  213. package/src/core/ToolExecutor.ts +117 -0
  214. package/src/core/Transition.ts +5 -5
  215. package/src/index.ts +12 -21
  216. package/src/providers/AnthropicProvider.ts +10 -13
  217. package/src/providers/GeminiProvider.ts +12 -8
  218. package/src/providers/OpenAIProvider.ts +10 -56
  219. package/src/providers/OpenRouterProvider.ts +10 -56
  220. package/src/types/agent.ts +16 -13
  221. package/src/types/ai.ts +6 -2
  222. package/src/types/history.ts +8 -0
  223. package/src/types/index.ts +0 -11
  224. package/src/types/route.ts +41 -5
  225. package/src/types/routing.ts +18 -0
  226. package/src/types/schema.ts +23 -0
  227. package/src/types/session.ts +219 -0
  228. package/src/types/tool.ts +29 -7
  229. package/src/utils/id.ts +0 -10
  230. package/src/utils/schema.ts +32 -0
  231. package/dist/cjs/core/ConditionEvaluator.d.ts +0 -72
  232. package/dist/cjs/core/ConditionEvaluator.d.ts.map +0 -1
  233. package/dist/cjs/core/ConditionEvaluator.js +0 -272
  234. package/dist/cjs/core/ConditionEvaluator.js.map +0 -1
  235. package/dist/cjs/core/Observation.d.ts +0 -24
  236. package/dist/cjs/core/Observation.d.ts.map +0 -1
  237. package/dist/cjs/core/Observation.js +0 -39
  238. package/dist/cjs/core/Observation.js.map +0 -1
  239. package/dist/cjs/core/PreparationEngine.d.ts +0 -116
  240. package/dist/cjs/core/PreparationEngine.d.ts.map +0 -1
  241. package/dist/cjs/core/PreparationEngine.js +0 -353
  242. package/dist/cjs/core/PreparationEngine.js.map +0 -1
  243. package/dist/cjs/core/PromptBuilder.d.ts +0 -136
  244. package/dist/cjs/core/PromptBuilder.d.ts.map +0 -1
  245. package/dist/cjs/core/PromptBuilder.js +0 -421
  246. package/dist/cjs/core/PromptBuilder.js.map +0 -1
  247. package/dist/cjs/types/observation.d.ts +0 -27
  248. package/dist/cjs/types/observation.d.ts.map +0 -1
  249. package/dist/cjs/types/observation.js +0 -6
  250. package/dist/cjs/types/observation.js.map +0 -1
  251. package/dist/cjs/types/prompt.d.ts +0 -46
  252. package/dist/cjs/types/prompt.d.ts.map +0 -1
  253. package/dist/cjs/types/prompt.js +0 -19
  254. package/dist/cjs/types/prompt.js.map +0 -1
  255. package/dist/core/ConditionEvaluator.d.ts +0 -72
  256. package/dist/core/ConditionEvaluator.d.ts.map +0 -1
  257. package/dist/core/ConditionEvaluator.js +0 -268
  258. package/dist/core/ConditionEvaluator.js.map +0 -1
  259. package/dist/core/Observation.d.ts +0 -24
  260. package/dist/core/Observation.d.ts.map +0 -1
  261. package/dist/core/Observation.js +0 -35
  262. package/dist/core/Observation.js.map +0 -1
  263. package/dist/core/PreparationEngine.d.ts +0 -116
  264. package/dist/core/PreparationEngine.d.ts.map +0 -1
  265. package/dist/core/PreparationEngine.js +0 -349
  266. package/dist/core/PreparationEngine.js.map +0 -1
  267. package/dist/core/PromptBuilder.d.ts +0 -136
  268. package/dist/core/PromptBuilder.d.ts.map +0 -1
  269. package/dist/core/PromptBuilder.js +0 -417
  270. package/dist/core/PromptBuilder.js.map +0 -1
  271. package/dist/types/observation.d.ts +0 -27
  272. package/dist/types/observation.d.ts.map +0 -1
  273. package/dist/types/observation.js +0 -5
  274. package/dist/types/observation.js.map +0 -1
  275. package/dist/types/prompt.d.ts +0 -46
  276. package/dist/types/prompt.d.ts.map +0 -1
  277. package/dist/types/prompt.js +0 -16
  278. package/dist/types/prompt.js.map +0 -1
  279. package/docs/STRUCTURE.md +0 -58
  280. package/src/core/ConditionEvaluator.ts +0 -381
  281. package/src/core/Observation.ts +0 -47
  282. package/src/core/PreparationEngine.ts +0 -561
  283. package/src/core/PromptBuilder.ts +0 -617
  284. package/src/types/observation.ts +0 -29
  285. package/src/types/prompt.ts +0 -49
@@ -1,617 +0,0 @@
1
- /**
2
- * Prompt construction and management
3
- */
4
-
5
- import type { Event, EmittedEvent, MessageEventData } from "../types/history";
6
- import { EventKind, EventSource } from "../types/history";
7
- import type { Term, Capability, GuidelineMatch } from "../types/agent";
8
- import type { PromptSection, ContextVariableValue } from "../types/prompt";
9
- import { SectionStatus } from "../types/prompt";
10
-
11
- import { adaptEvent } from "./Events";
12
-
13
- /**
14
- * Built-in section identifiers
15
- */
16
- export enum BuiltInSection {
17
- AGENT_IDENTITY = "agent_identity",
18
- CUSTOMER_IDENTITY = "customer_identity",
19
- INTERACTION_HISTORY = "interaction_history",
20
- CONTEXT_VARIABLES = "context_variables",
21
- GLOSSARY = "glossary",
22
- GUIDELINE_DESCRIPTIONS = "guideline_descriptions",
23
- GUIDELINES = "guidelines",
24
- STAGED_EVENTS = "staged_events",
25
- ROUTES = "routes",
26
- OBSERVATIONS = "observations",
27
- CAPABILITIES = "capabilities",
28
- ACTIVE_ROUTES = "active_routes",
29
- DOMAINS = "domains",
30
- }
31
-
32
- /**
33
- * Customer/user information
34
- */
35
- export interface Customer {
36
- name: string;
37
- id?: string;
38
- }
39
-
40
- /**
41
- * Agent information for prompt building
42
- */
43
- export interface AgentInfo {
44
- name: string;
45
- description?: string;
46
- }
47
-
48
- /**
49
- * Builds prompts from composable sections
50
- */
51
- export class PromptBuilder {
52
- private sections: Map<string | BuiltInSection, PromptSection> = new Map();
53
- private onBuild?: (prompt: string) => void;
54
- private cachedResults = new Set<string>();
55
-
56
- constructor(onBuild?: (prompt: string) => void) {
57
- this.onBuild = onBuild;
58
- }
59
-
60
- /**
61
- * Build the final prompt from all sections
62
- */
63
- build(): string {
64
- const parts: string[] = [];
65
-
66
- for (const section of this.sections.values()) {
67
- try {
68
- const formatted = this.formatTemplate(section.template, section.props);
69
- parts.push(formatted);
70
- } catch (error) {
71
- throw new Error(
72
- `Error formatting section: ${
73
- error instanceof Error ? error.message : "Unknown error"
74
- }`
75
- );
76
- }
77
- }
78
-
79
- const prompt = parts.join("\n\n").trim();
80
-
81
- if (this.onBuild && !this.cachedResults.has(prompt)) {
82
- this.onBuild(prompt);
83
- this.cachedResults.add(prompt);
84
- }
85
-
86
- return prompt;
87
- }
88
-
89
- /**
90
- * Add a section to the prompt
91
- */
92
- addSection(
93
- name: string | BuiltInSection,
94
- template: string,
95
- props: Record<string, unknown> = {},
96
- status?: SectionStatus
97
- ): this {
98
- if (this.sections.has(name)) {
99
- throw new Error(`Section '${String(name)}' already exists`);
100
- }
101
-
102
- this.sections.set(name, {
103
- template,
104
- props,
105
- status,
106
- });
107
-
108
- return this;
109
- }
110
-
111
- /**
112
- * Edit an existing section
113
- */
114
- editSection(
115
- name: string | BuiltInSection,
116
- editor: (section: PromptSection) => PromptSection
117
- ): this {
118
- const section = this.sections.get(name);
119
- if (section) {
120
- this.sections.set(name, editor(section));
121
- }
122
- return this;
123
- }
124
-
125
- /**
126
- * Get section status
127
- */
128
- sectionStatus(name: string | BuiltInSection): SectionStatus {
129
- const section = this.sections.get(name);
130
- return section?.status || SectionStatus.NONE;
131
- }
132
-
133
- /**
134
- * Add agent identity section
135
- */
136
- addAgentIdentity(agent: AgentInfo): this {
137
- if (agent.description) {
138
- this.addSection(
139
- BuiltInSection.AGENT_IDENTITY,
140
- `You are an AI agent named {agent_name}.
141
-
142
- The following is a description of your background and personality: ###
143
- {agent_description}
144
- ###`,
145
- {
146
- agent_name: agent.name,
147
- agent_description: agent.description,
148
- },
149
- SectionStatus.ACTIVE
150
- );
151
- }
152
- return this;
153
- }
154
-
155
- /**
156
- * Add customer identity section
157
- */
158
- addCustomerIdentity(customer: Customer): this {
159
- this.addSection(
160
- BuiltInSection.CUSTOMER_IDENTITY,
161
- `The user you're interacting with is called {customer_name}.`,
162
- { customer_name: customer.name },
163
- SectionStatus.ACTIVE
164
- );
165
- return this;
166
- }
167
-
168
- /**
169
- * Add interaction history section
170
- */
171
- addInteractionHistory(
172
- events: Event[],
173
- stagedEvents: EmittedEvent[] = []
174
- ): this {
175
- if (events.length === 0 && stagedEvents.length === 0) {
176
- this.addSection(
177
- BuiltInSection.INTERACTION_HISTORY,
178
- `Your interaction with the user has just began, and no events have been recorded yet.
179
- Proceed with your task accordingly.`,
180
- {},
181
- SectionStatus.PASSIVE
182
- );
183
- } else {
184
- const interactionEvents = this.gatherInteractionEvents(
185
- events,
186
- stagedEvents
187
- );
188
- this.addSection(
189
- BuiltInSection.INTERACTION_HISTORY,
190
- `The following is a list of events describing a back-and-forth
191
- interaction between you and a user: ###
192
- {interaction_events}
193
- ###`,
194
- { interaction_events: interactionEvents.join("\n") },
195
- SectionStatus.ACTIVE
196
- );
197
- }
198
- return this;
199
- }
200
-
201
- /**
202
- * Add interaction history with special handling for message generation
203
- */
204
- addInteractionHistoryForMessageGeneration(
205
- events: Event[],
206
- stagedEvents: EmittedEvent[] = []
207
- ): this {
208
- if (events.length === 0 && stagedEvents.length === 0) {
209
- return this.addInteractionHistory(events, stagedEvents);
210
- }
211
-
212
- const interactionEvents = this.gatherInteractionEvents(
213
- events,
214
- stagedEvents
215
- );
216
- const lastEventNote = this.lastAgentMessageNote(events);
217
-
218
- let template = `The following is a list of events describing a back-and-forth
219
- interaction between you and a user: ###
220
- {interaction_events}
221
- ###`;
222
-
223
- const props: Record<string, unknown> = {
224
- interaction_events: interactionEvents.join("\n"),
225
- };
226
-
227
- if (lastEventNote) {
228
- template += "\n\n{last_event_note}";
229
- props.last_event_note = lastEventNote;
230
- }
231
-
232
- this.addSection(
233
- BuiltInSection.INTERACTION_HISTORY,
234
- template,
235
- props,
236
- SectionStatus.ACTIVE
237
- );
238
-
239
- return this;
240
- }
241
-
242
- /**
243
- * Add context variables section
244
- */
245
- addContextVariables(variables: ContextVariableValue[]): this {
246
- if (variables.length > 0) {
247
- const contextValues = this.contextVariablesToJson(variables);
248
- this.addSection(
249
- BuiltInSection.CONTEXT_VARIABLES,
250
- `The following is information that you're given about the user and context of the interaction: ###
251
- {context_values}
252
- ###`,
253
- { context_values: contextValues },
254
- SectionStatus.ACTIVE
255
- );
256
- }
257
- return this;
258
- }
259
-
260
- /**
261
- * Add glossary section
262
- */
263
- addGlossary(terms: Term[]): this {
264
- if (terms.length > 0) {
265
- const termsString = terms
266
- .map((t, i) => {
267
- const synonyms = t.synonyms?.length
268
- ? ` (synonyms: ${t.synonyms.join(", ")})`
269
- : "";
270
- return `${i + 1}) ${t.name}${synonyms}: ${t.description}`;
271
- })
272
- .join("\n");
273
-
274
- this.addSection(
275
- BuiltInSection.GLOSSARY,
276
- `The following is a glossary of the business.
277
- Understanding these terms, as they apply to the business, is critical for your task.
278
- When encountering any of these terms, prioritize the interpretation provided here over any definitions you may already know.
279
- Please be tolerant of possible typos by the user with regards to these terms,
280
- and let the user know if/when you assume they meant a term by their typo: ###
281
- {terms_string}
282
- ###`,
283
- { terms_string: termsString },
284
- SectionStatus.ACTIVE
285
- );
286
- }
287
- return this;
288
- }
289
-
290
- /**
291
- * Add staged tool events section
292
- */
293
- addStagedToolEvents(events: EmittedEvent[]): this {
294
- const toolEvents = events.filter((e) => e.kind === EventKind.TOOL);
295
-
296
- if (toolEvents.length > 0) {
297
- const stagedEventsAsDict = toolEvents.map((e) => adaptEvent(e));
298
-
299
- this.addSection(
300
- BuiltInSection.STAGED_EVENTS,
301
- `Here are the most recent staged events for your reference.
302
- They represent interactions with external tools that perform actions or provide information.
303
- Prioritize their data over any other sources and use their details to complete your task: ###
304
- {staged_events_as_dict}
305
- ###`,
306
- { staged_events_as_dict: stagedEventsAsDict.join("\n") },
307
- SectionStatus.ACTIVE
308
- );
309
- }
310
- return this;
311
- }
312
-
313
- /**
314
- * Add guidelines for message generation
315
- */
316
- addGuidelinesForMessageGeneration(guidelines: GuidelineMatch[]): this {
317
- if (guidelines.length === 0) {
318
- this.addSection(
319
- BuiltInSection.GUIDELINE_DESCRIPTIONS,
320
- `In formulating your reply, you are normally required to follow a number of behavioral guidelines.
321
- However, in this case, no special behavioral guidelines were provided. Therefore, when generating revisions,
322
- you don't need to specifically double-check if you followed or broke any guidelines.`,
323
- {},
324
- SectionStatus.PASSIVE
325
- );
326
- return this;
327
- }
328
-
329
- const guidelineList = guidelines
330
- .map((g, i) => {
331
- const num = i + 1;
332
- let text = g.guideline.condition
333
- ? `Guideline #${num}) When ${g.guideline.condition}, then ${g.guideline.action}`
334
- : `Guideline #${num}) ${g.guideline.action}`;
335
-
336
- if (g.rationale) {
337
- text += `\n - Rationale: ${g.rationale}`;
338
- }
339
-
340
- return text;
341
- })
342
- .join("\n");
343
-
344
- this.addSection(
345
- BuiltInSection.GUIDELINE_DESCRIPTIONS,
346
- `When crafting your reply, you must follow the behavioral guidelines provided below, which have been identified as relevant to the current state of the interaction.
347
-
348
- - **Guidelines**:
349
- {guideline_list}
350
-
351
- You may choose not to follow a guideline only in the following cases:
352
- - It conflicts with a previous customer request.
353
- - It is clearly inappropriate given the current context of the conversation.
354
- - It lacks sufficient context or data to apply reliably.
355
- - It conflicts with an insight.
356
-
357
- In all other situations, you are expected to adhere to the guidelines.
358
- These guidelines have already been pre-filtered based on the interaction's context and other considerations outside your scope.`,
359
- { guideline_list: guidelineList },
360
- SectionStatus.ACTIVE
361
- );
362
-
363
- return this;
364
- }
365
-
366
- /**
367
- * Add capabilities section for message generation
368
- */
369
- addCapabilitiesForMessageGeneration(capabilities: Capability[]): this {
370
- if (capabilities.length > 0) {
371
- const capabilitiesString = capabilities
372
- .map(
373
- (c, i) => `Supported Capability ${i + 1}: ${c.title}
374
- ${c.description}`
375
- )
376
- .join("\n\n");
377
-
378
- this.addSection(
379
- BuiltInSection.CAPABILITIES,
380
- `Below are the capabilities available to you as an agent.
381
- You may inform the customer that you can assist them using these capabilities.
382
- If you choose to use any of them, additional details will be provided in your next response.
383
- Always prefer adhering to guidelines, before offering capabilities - only offer capabilities if you have no other instruction that's relevant for the current stage of the interaction.
384
- Be proactive and offer the most relevant capabilities—but only if they are likely to move the conversation forward.
385
- If multiple capabilities are appropriate, aim to present them all to the customer.
386
- If none of the capabilities address the current request of the customer - DO NOT MENTION THEM.
387
- ###
388
- {capabilities_string}
389
- ###`,
390
- { capabilities_string: capabilitiesString },
391
- SectionStatus.ACTIVE
392
- );
393
- }
394
- return this;
395
- }
396
-
397
- /**
398
- * Add observations for disambiguation
399
- */
400
- addObservations(
401
- observations: Array<{
402
- description: string;
403
- routes: Array<{ title: string }>;
404
- }>
405
- ): this {
406
- if (observations.length > 0) {
407
- const observationsString = observations
408
- .map((obs, i) => {
409
- const routeTitles = obs.routes.map((r) => `"${r.title}"`).join(", ");
410
- return `${i + 1}) "${
411
- obs.description
412
- }" → Can lead to routes: ${routeTitles}`;
413
- })
414
- .join("\n");
415
-
416
- this.addSection(
417
- BuiltInSection.OBSERVATIONS,
418
- `The following observations may help you understand the user's intent and choose the appropriate response path:
419
- ###
420
- {observations_string}
421
- ###
422
-
423
- When you detect any of these situations, consider which route would be most appropriate based on the user's actual need.`,
424
- { observations_string: observationsString },
425
- SectionStatus.ACTIVE
426
- );
427
- }
428
- return this;
429
- }
430
-
431
- /**
432
- * Add active routes information
433
- */
434
- addActiveRoutes(
435
- routes: Array<{
436
- title: string;
437
- description?: string;
438
- conditions: string[];
439
- domains?: string[];
440
- rules?: string[];
441
- prohibitions?: string[];
442
- }>
443
- ): this {
444
- if (routes.length > 0) {
445
- const routesString = routes
446
- .map((route, i) => {
447
- const conditions =
448
- route.conditions.length > 0
449
- ? `\n Triggered when: ${route.conditions.join(" OR ")}`
450
- : "";
451
- const desc = route.description ? `\n ${route.description}` : "";
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
465
- .map((r, idx) => `${idx + 1}. ${r}`)
466
- .join("; ");
467
- rulesInfo = `\n RULES: ${rulesList}`;
468
- }
469
-
470
- let prohibitionsInfo = "";
471
- if (route.prohibitions && route.prohibitions.length > 0) {
472
- const prohibitionsList = route.prohibitions
473
- .map((p, idx) => `${idx + 1}. ${p}`)
474
- .join("; ");
475
- prohibitionsInfo = `\n PROHIBITIONS: ${prohibitionsList}`;
476
- }
477
-
478
- return `${i + 1}) ${
479
- route.title
480
- }${desc}${conditions}${domainInfo}${rulesInfo}${prohibitionsInfo}`;
481
- })
482
- .join("\n\n");
483
-
484
- this.addSection(
485
- BuiltInSection.ACTIVE_ROUTES,
486
- `Available conversation routes:
487
- ###
488
- {routes_string}
489
- ###
490
-
491
- These routes represent different paths the conversation can take. Choose the most appropriate route based on the user's needs.
492
- IMPORTANT:
493
- - If a route specifies available tools, you can ONLY call tools from those domains when following that route.
494
- - If a route has RULES, you MUST follow them when you choose that route.
495
- - If a route has PROHIBITIONS, you MUST NEVER do those things when you choose that route.`,
496
- { routes_string: routesString },
497
- SectionStatus.ACTIVE
498
- );
499
- }
500
- return this;
501
- }
502
-
503
- /**
504
- * Add domains (tools) information
505
- */
506
- addDomains(domains: Record<string, Record<string, unknown>>): this {
507
- const domainNames = Object.keys(domains);
508
- if (domainNames.length > 0) {
509
- const domainsString = domainNames
510
- .map((name, i) => {
511
- const toolNames = Object.keys(domains[name]);
512
- const tools = toolNames.join(", ");
513
- return `${i + 1}) Domain "${name}": ${tools}`;
514
- })
515
- .join("\n");
516
-
517
- this.addSection(
518
- BuiltInSection.DOMAINS,
519
- `Available tool domains:
520
- ###
521
- {domains_string}
522
- ###
523
-
524
- These are the tool domains registered in the system. Each domain contains specific tools/methods.
525
- When calling tools, use the format: domain.toolName (e.g., "payment.processPayment").`,
526
- { domains_string: domainsString },
527
- SectionStatus.ACTIVE
528
- );
529
- }
530
- return this;
531
- }
532
-
533
- /**
534
- * Add JSON response schema instructions
535
- *
536
- * NOTE: toolCalls are NOT included. Tools execute automatically based on
537
- * state transitions and guideline matching, NOT based on AI decisions.
538
- */
539
- addJsonResponseSchema(): this {
540
- const schema = {
541
- message: "The actual message to send to the user",
542
- route: "The title of the route you chose (or null if no specific route)",
543
- state:
544
- "The current state within the chosen route (or null if not in a route)",
545
- reasoning: "Optional: Your internal reasoning for this response",
546
- };
547
-
548
- this.addSection(
549
- "json_response_format",
550
- `IMPORTANT: You must respond with valid JSON in the following format:
551
-
552
- \`\`\`json
553
- ${JSON.stringify(schema, null, 2)}
554
- \`\`\`
555
-
556
- Instructions:
557
- - "message": The actual message to send to the user (required)
558
- - "route": If you chose a specific conversation route, provide its exact title. If not in a route, use null.
559
- - "state": The current state within the chosen route. If not in a route or at initial state, use null.
560
- - "reasoning": Optional field for your internal thinking process.
561
-
562
- Your entire response must be valid JSON. Do not include any text before or after the JSON object.`,
563
- {},
564
- SectionStatus.ACTIVE
565
- );
566
- return this;
567
- }
568
-
569
- // Helper methods
570
-
571
- private formatTemplate(
572
- template: string,
573
- props: Record<string, unknown>
574
- ): string {
575
- return template.replace(/\{(\w+)\}/g, (match, key: string) => {
576
- if (key in props) {
577
- return String(props[key]);
578
- }
579
- return match;
580
- });
581
- }
582
-
583
- private gatherInteractionEvents(
584
- events: Event[],
585
- stagedEvents: EmittedEvent[]
586
- ): string[] {
587
- const combined = [...events, ...stagedEvents];
588
- return combined
589
- .filter((e) => e.kind !== EventKind.STATUS)
590
- .map((e) => adaptEvent(e));
591
- }
592
-
593
- private lastAgentMessageNote(events: Event[]): string | null {
594
- const lastMessageEvent = [...events]
595
- .reverse()
596
- .find((e) => e.kind === EventKind.MESSAGE);
597
-
598
- if (!lastMessageEvent || lastMessageEvent.source !== EventSource.AI_AGENT) {
599
- return null;
600
- }
601
-
602
- const lastMessage = (lastMessageEvent.data as MessageEventData).message;
603
- return `IMPORTANT: Please note that the last message was sent by you, the AI agent (likely as a preamble). Your last message was: ###
604
- ${lastMessage}
605
- ###
606
-
607
- You must keep that in mind when responding to the user, to continue the last message naturally (without repeating anything similar in your last message - make sure you don't repeat something like this in your next message - it was already said!).`;
608
- }
609
-
610
- private contextVariablesToJson(variables: ContextVariableValue[]): string {
611
- const obj: Record<string, unknown> = {};
612
- for (const v of variables) {
613
- obj[v.variable.name] = v.value;
614
- }
615
- return JSON.stringify(obj, null, 2);
616
- }
617
- }
@@ -1,29 +0,0 @@
1
- /**
2
- * Observation types for disambiguation
3
- */
4
-
5
- import type { RouteRef } from "./route";
6
-
7
- /**
8
- * An observation that can disambiguate between multiple routes
9
- */
10
- export interface Observation {
11
- /** Unique identifier */
12
- id: string;
13
- /** The observation description */
14
- description: string;
15
- /** Routes this observation can disambiguate between */
16
- routes?: RouteRef[];
17
- }
18
-
19
- /**
20
- * Options for creating an observation
21
- */
22
- export interface ObservationOptions {
23
- /** Custom ID for the observation (optional - will generate deterministic ID from description if not provided) */
24
- id?: string;
25
- /** The observation description */
26
- description: string;
27
- /** Route IDs or titles to disambiguate between (can be set later with disambiguate()) */
28
- routeRefs?: string[];
29
- }
@@ -1,49 +0,0 @@
1
- /**
2
- * Prompt building types
3
- */
4
-
5
- /**
6
- * Status of a prompt section
7
- */
8
- export enum SectionStatus {
9
- /** Section has active information */
10
- ACTIVE = "active",
11
- /** Section is inactive but may have empty-state text */
12
- PASSIVE = "passive",
13
- /** Section is not included */
14
- NONE = "none",
15
- }
16
-
17
- /**
18
- * A section within a prompt
19
- */
20
- export interface PromptSection {
21
- /** Template string (may contain placeholders) */
22
- template: string;
23
- /** Properties to fill template placeholders */
24
- props: Record<string, unknown>;
25
- /** Status of this section */
26
- status?: SectionStatus;
27
- }
28
-
29
- /**
30
- * Context variable with value
31
- */
32
- export interface ContextVariable {
33
- /** Variable name */
34
- name: string;
35
- /** Variable description */
36
- description?: string;
37
- /** Variable type */
38
- type?: string;
39
- }
40
-
41
- /**
42
- * Value for a context variable
43
- */
44
- export interface ContextVariableValue {
45
- /** The variable definition */
46
- variable: ContextVariable;
47
- /** The current value */
48
- value: unknown;
49
- }