signalwire-agents 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 (245) hide show
  1. package/CLAUDE.md +112 -0
  2. package/README.md +417 -0
  3. package/dist/AgentBase.d.ts +481 -0
  4. package/dist/AgentBase.js +1155 -0
  5. package/dist/AgentBase.js.map +1 -0
  6. package/dist/AgentServer.d.ts +60 -0
  7. package/dist/AgentServer.js +128 -0
  8. package/dist/AgentServer.js.map +1 -0
  9. package/dist/AuthHandler.d.ts +56 -0
  10. package/dist/AuthHandler.js +119 -0
  11. package/dist/AuthHandler.js.map +1 -0
  12. package/dist/ConfigLoader.d.ts +68 -0
  13. package/dist/ConfigLoader.js +150 -0
  14. package/dist/ConfigLoader.js.map +1 -0
  15. package/dist/ContextBuilder.d.ts +431 -0
  16. package/dist/ContextBuilder.js +751 -0
  17. package/dist/ContextBuilder.js.map +1 -0
  18. package/dist/DataMap.d.ts +179 -0
  19. package/dist/DataMap.js +321 -0
  20. package/dist/DataMap.js.map +1 -0
  21. package/dist/Logger.d.ts +91 -0
  22. package/dist/Logger.js +191 -0
  23. package/dist/Logger.js.map +1 -0
  24. package/dist/PomBuilder.d.ts +144 -0
  25. package/dist/PomBuilder.js +249 -0
  26. package/dist/PomBuilder.js.map +1 -0
  27. package/dist/PromptManager.d.ts +85 -0
  28. package/dist/PromptManager.js +108 -0
  29. package/dist/PromptManager.js.map +1 -0
  30. package/dist/SchemaUtils.d.ts +42 -0
  31. package/dist/SchemaUtils.js +148 -0
  32. package/dist/SchemaUtils.js.map +1 -0
  33. package/dist/ServerlessAdapter.d.ts +100 -0
  34. package/dist/ServerlessAdapter.js +166 -0
  35. package/dist/ServerlessAdapter.js.map +1 -0
  36. package/dist/SessionManager.d.ts +86 -0
  37. package/dist/SessionManager.js +155 -0
  38. package/dist/SessionManager.js.map +1 -0
  39. package/dist/SslConfig.d.ts +77 -0
  40. package/dist/SslConfig.js +100 -0
  41. package/dist/SslConfig.js.map +1 -0
  42. package/dist/SwaigFunction.d.ts +88 -0
  43. package/dist/SwaigFunction.js +125 -0
  44. package/dist/SwaigFunction.js.map +1 -0
  45. package/dist/SwaigFunctionResult.d.ts +442 -0
  46. package/dist/SwaigFunctionResult.js +735 -0
  47. package/dist/SwaigFunctionResult.js.map +1 -0
  48. package/dist/SwmlBuilder.d.ts +37 -0
  49. package/dist/SwmlBuilder.js +55 -0
  50. package/dist/SwmlBuilder.js.map +1 -0
  51. package/dist/cli/agent-loader.d.ts +22 -0
  52. package/dist/cli/agent-loader.js +106 -0
  53. package/dist/cli/agent-loader.js.map +1 -0
  54. package/dist/cli/mock-data.d.ts +37 -0
  55. package/dist/cli/mock-data.js +79 -0
  56. package/dist/cli/mock-data.js.map +1 -0
  57. package/dist/cli/swaig-test.d.ts +32 -0
  58. package/dist/cli/swaig-test.js +405 -0
  59. package/dist/cli/swaig-test.js.map +1 -0
  60. package/dist/index.d.ts +36 -0
  61. package/dist/index.js +44 -0
  62. package/dist/index.js.map +1 -0
  63. package/dist/prefabs/ConciergeAgent.d.ts +64 -0
  64. package/dist/prefabs/ConciergeAgent.js +188 -0
  65. package/dist/prefabs/ConciergeAgent.js.map +1 -0
  66. package/dist/prefabs/FAQBotAgent.d.ts +68 -0
  67. package/dist/prefabs/FAQBotAgent.js +206 -0
  68. package/dist/prefabs/FAQBotAgent.js.map +1 -0
  69. package/dist/prefabs/InfoGathererAgent.d.ts +73 -0
  70. package/dist/prefabs/InfoGathererAgent.js +202 -0
  71. package/dist/prefabs/InfoGathererAgent.js.map +1 -0
  72. package/dist/prefabs/ReceptionistAgent.d.ts +65 -0
  73. package/dist/prefabs/ReceptionistAgent.js +205 -0
  74. package/dist/prefabs/ReceptionistAgent.js.map +1 -0
  75. package/dist/prefabs/SurveyAgent.d.ts +84 -0
  76. package/dist/prefabs/SurveyAgent.js +324 -0
  77. package/dist/prefabs/SurveyAgent.js.map +1 -0
  78. package/dist/prefabs/index.d.ts +13 -0
  79. package/dist/prefabs/index.js +14 -0
  80. package/dist/prefabs/index.js.map +1 -0
  81. package/dist/skills/SkillBase.d.ts +134 -0
  82. package/dist/skills/SkillBase.js +105 -0
  83. package/dist/skills/SkillBase.js.map +1 -0
  84. package/dist/skills/SkillManager.d.ts +83 -0
  85. package/dist/skills/SkillManager.js +159 -0
  86. package/dist/skills/SkillManager.js.map +1 -0
  87. package/dist/skills/SkillRegistry.d.ts +105 -0
  88. package/dist/skills/SkillRegistry.js +195 -0
  89. package/dist/skills/SkillRegistry.js.map +1 -0
  90. package/dist/skills/builtin/api_ninjas_trivia.d.ts +35 -0
  91. package/dist/skills/builtin/api_ninjas_trivia.js +161 -0
  92. package/dist/skills/builtin/api_ninjas_trivia.js.map +1 -0
  93. package/dist/skills/builtin/claude_skills.d.ts +34 -0
  94. package/dist/skills/builtin/claude_skills.js +156 -0
  95. package/dist/skills/builtin/claude_skills.js.map +1 -0
  96. package/dist/skills/builtin/custom_skills.d.ts +59 -0
  97. package/dist/skills/builtin/custom_skills.js +231 -0
  98. package/dist/skills/builtin/custom_skills.js.map +1 -0
  99. package/dist/skills/builtin/datasphere.d.ts +34 -0
  100. package/dist/skills/builtin/datasphere.js +163 -0
  101. package/dist/skills/builtin/datasphere.js.map +1 -0
  102. package/dist/skills/builtin/datasphere_serverless.d.ts +54 -0
  103. package/dist/skills/builtin/datasphere_serverless.js +155 -0
  104. package/dist/skills/builtin/datasphere_serverless.js.map +1 -0
  105. package/dist/skills/builtin/datetime.d.ts +31 -0
  106. package/dist/skills/builtin/datetime.js +95 -0
  107. package/dist/skills/builtin/datetime.js.map +1 -0
  108. package/dist/skills/builtin/google_maps.d.ts +33 -0
  109. package/dist/skills/builtin/google_maps.js +233 -0
  110. package/dist/skills/builtin/google_maps.js.map +1 -0
  111. package/dist/skills/builtin/index.d.ts +28 -0
  112. package/dist/skills/builtin/index.js +75 -0
  113. package/dist/skills/builtin/index.js.map +1 -0
  114. package/dist/skills/builtin/info_gatherer.d.ts +52 -0
  115. package/dist/skills/builtin/info_gatherer.js +237 -0
  116. package/dist/skills/builtin/info_gatherer.js.map +1 -0
  117. package/dist/skills/builtin/joke.d.ts +32 -0
  118. package/dist/skills/builtin/joke.js +142 -0
  119. package/dist/skills/builtin/joke.js.map +1 -0
  120. package/dist/skills/builtin/math.d.ts +33 -0
  121. package/dist/skills/builtin/math.js +120 -0
  122. package/dist/skills/builtin/math.js.map +1 -0
  123. package/dist/skills/builtin/mcp_gateway.d.ts +35 -0
  124. package/dist/skills/builtin/mcp_gateway.js +85 -0
  125. package/dist/skills/builtin/mcp_gateway.js.map +1 -0
  126. package/dist/skills/builtin/native_vector_search.d.ts +44 -0
  127. package/dist/skills/builtin/native_vector_search.js +248 -0
  128. package/dist/skills/builtin/native_vector_search.js.map +1 -0
  129. package/dist/skills/builtin/play_background_file.d.ts +34 -0
  130. package/dist/skills/builtin/play_background_file.js +135 -0
  131. package/dist/skills/builtin/play_background_file.js.map +1 -0
  132. package/dist/skills/builtin/spider.d.ts +34 -0
  133. package/dist/skills/builtin/spider.js +164 -0
  134. package/dist/skills/builtin/spider.js.map +1 -0
  135. package/dist/skills/builtin/swml_transfer.d.ts +44 -0
  136. package/dist/skills/builtin/swml_transfer.js +204 -0
  137. package/dist/skills/builtin/swml_transfer.js.map +1 -0
  138. package/dist/skills/builtin/weather_api.d.ts +34 -0
  139. package/dist/skills/builtin/weather_api.js +136 -0
  140. package/dist/skills/builtin/weather_api.js.map +1 -0
  141. package/dist/skills/builtin/web_search.d.ts +34 -0
  142. package/dist/skills/builtin/web_search.js +141 -0
  143. package/dist/skills/builtin/web_search.js.map +1 -0
  144. package/dist/skills/builtin/wikipedia_search.d.ts +34 -0
  145. package/dist/skills/builtin/wikipedia_search.js +166 -0
  146. package/dist/skills/builtin/wikipedia_search.js.map +1 -0
  147. package/dist/skills/index.d.ts +11 -0
  148. package/dist/skills/index.js +10 -0
  149. package/dist/skills/index.js.map +1 -0
  150. package/dist/types.d.ts +85 -0
  151. package/dist/types.js +5 -0
  152. package/dist/types.js.map +1 -0
  153. package/docs/agent-guide.md +869 -0
  154. package/docs/api-reference.md +2419 -0
  155. package/docs/architecture.md +843 -0
  156. package/docs/cli-guide.md +579 -0
  157. package/docs/configuration.md +422 -0
  158. package/docs/contexts-guide.md +787 -0
  159. package/docs/datamap-guide.md +1141 -0
  160. package/docs/prefabs-guide.md +830 -0
  161. package/docs/security.md +436 -0
  162. package/docs/serverless-guide.md +513 -0
  163. package/docs/skills-guide.md +845 -0
  164. package/docs/swaig-reference.md +1627 -0
  165. package/examples/call-flow.ts +60 -0
  166. package/examples/contexts-steps.ts +62 -0
  167. package/examples/datamap-tools.ts +52 -0
  168. package/examples/dynamic-config.ts +51 -0
  169. package/examples/multi-agent.ts +67 -0
  170. package/examples/pom-prompt.ts +68 -0
  171. package/examples/simple-agent.ts +33 -0
  172. package/package.json +35 -0
  173. package/src/AgentBase.ts +1279 -0
  174. package/src/AgentServer.ts +149 -0
  175. package/src/AuthHandler.ts +141 -0
  176. package/src/ConfigLoader.ts +165 -0
  177. package/src/ContextBuilder.ts +796 -0
  178. package/src/DataMap.ts +365 -0
  179. package/src/Logger.ts +197 -0
  180. package/src/PomBuilder.ts +298 -0
  181. package/src/PromptManager.ts +133 -0
  182. package/src/SchemaUtils.ts +170 -0
  183. package/src/ServerlessAdapter.ts +218 -0
  184. package/src/SessionManager.ts +161 -0
  185. package/src/SslConfig.ts +118 -0
  186. package/src/SwaigFunction.ts +168 -0
  187. package/src/SwaigFunctionResult.ts +864 -0
  188. package/src/SwmlBuilder.ts +62 -0
  189. package/src/cli/agent-loader.ts +122 -0
  190. package/src/cli/mock-data.ts +110 -0
  191. package/src/cli/swaig-test.ts +440 -0
  192. package/src/index.ts +99 -0
  193. package/src/prefabs/ConciergeAgent.ts +251 -0
  194. package/src/prefabs/FAQBotAgent.ts +263 -0
  195. package/src/prefabs/InfoGathererAgent.ts +276 -0
  196. package/src/prefabs/ReceptionistAgent.ts +277 -0
  197. package/src/prefabs/SurveyAgent.ts +413 -0
  198. package/src/prefabs/index.ts +23 -0
  199. package/src/skills/SkillBase.ts +184 -0
  200. package/src/skills/SkillManager.ts +177 -0
  201. package/src/skills/SkillRegistry.ts +231 -0
  202. package/src/skills/builtin/api_ninjas_trivia.ts +230 -0
  203. package/src/skills/builtin/claude_skills.ts +239 -0
  204. package/src/skills/builtin/custom_skills.ts +326 -0
  205. package/src/skills/builtin/datasphere.ts +225 -0
  206. package/src/skills/builtin/datasphere_serverless.ts +194 -0
  207. package/src/skills/builtin/datetime.ts +115 -0
  208. package/src/skills/builtin/google_maps.ts +347 -0
  209. package/src/skills/builtin/index.ts +79 -0
  210. package/src/skills/builtin/info_gatherer.ts +331 -0
  211. package/src/skills/builtin/joke.ts +180 -0
  212. package/src/skills/builtin/math.ts +150 -0
  213. package/src/skills/builtin/mcp_gateway.ts +99 -0
  214. package/src/skills/builtin/native_vector_search.ts +327 -0
  215. package/src/skills/builtin/play_background_file.ts +189 -0
  216. package/src/skills/builtin/spider.ts +232 -0
  217. package/src/skills/builtin/swml_transfer.ts +285 -0
  218. package/src/skills/builtin/weather_api.ts +193 -0
  219. package/src/skills/builtin/web_search.ts +212 -0
  220. package/src/skills/builtin/wikipedia_search.ts +248 -0
  221. package/src/skills/index.ts +12 -0
  222. package/src/types.ts +99 -0
  223. package/tests/AgentBase.test.ts +693 -0
  224. package/tests/AgentServer.test.ts +102 -0
  225. package/tests/AuthHandler.test.ts +121 -0
  226. package/tests/ConfigLoader.test.ts +121 -0
  227. package/tests/DataMap.test.ts +286 -0
  228. package/tests/Logger.test.ts +218 -0
  229. package/tests/PomBuilder.test.ts +164 -0
  230. package/tests/ProxyDetection.test.ts +185 -0
  231. package/tests/SchemaUtils.test.ts +106 -0
  232. package/tests/ServerlessAdapter.test.ts +200 -0
  233. package/tests/SessionManager.test.ts +139 -0
  234. package/tests/SslConfig.test.ts +97 -0
  235. package/tests/SwaigFunctionResult.test.ts +298 -0
  236. package/tests/cli/mock-data.test.ts +94 -0
  237. package/tests/cli/swaig-test.test.ts +96 -0
  238. package/tests/prefabs/prefabs.test.ts +854 -0
  239. package/tests/setup.ts +6 -0
  240. package/tests/skills/SkillBase.test.ts +118 -0
  241. package/tests/skills/SkillManager.test.ts +164 -0
  242. package/tests/skills/SkillRegistry.test.ts +124 -0
  243. package/tests/skills/builtin.test.ts +924 -0
  244. package/tsconfig.json +18 -0
  245. package/vitest.config.ts +9 -0
@@ -0,0 +1,751 @@
1
+ /**
2
+ * ContextBuilder - Contexts and Steps workflow system.
3
+ *
4
+ * Contexts contain ordered Steps, each with prompt text/POM sections,
5
+ * completion criteria, function restrictions, and navigation rules.
6
+ */
7
+ // ── GatherQuestion ──────────────────────────────────────────────────
8
+ /** Represents a single question within a gather operation. */
9
+ export class GatherQuestion {
10
+ /** Unique key used to store the answer. */
11
+ key;
12
+ /** The question text presented to the user. */
13
+ question;
14
+ /** Expected answer type (e.g., "string", "number"). */
15
+ type;
16
+ /** Whether the answer requires user confirmation. */
17
+ confirm;
18
+ /** Optional additional prompt context for this question. */
19
+ prompt;
20
+ /** Optional list of SWAIG function names available during this question. */
21
+ functions;
22
+ /**
23
+ * Creates a new GatherQuestion.
24
+ * @param opts - Question configuration including key, question text, and optional type/confirm/prompt/functions.
25
+ */
26
+ constructor(opts) {
27
+ this.key = opts.key;
28
+ this.question = opts.question;
29
+ this.type = opts.type ?? 'string';
30
+ this.confirm = opts.confirm ?? false;
31
+ this.prompt = opts.prompt;
32
+ this.functions = opts.functions;
33
+ }
34
+ /**
35
+ * Serializes this question to a plain object for SWML output.
36
+ * @returns A dictionary representation of this question.
37
+ */
38
+ toDict() {
39
+ const d = { key: this.key, question: this.question };
40
+ if (this.type !== 'string')
41
+ d['type'] = this.type;
42
+ if (this.confirm)
43
+ d['confirm'] = true;
44
+ if (this.prompt)
45
+ d['prompt'] = this.prompt;
46
+ if (this.functions)
47
+ d['functions'] = this.functions;
48
+ return d;
49
+ }
50
+ }
51
+ // ── GatherInfo ──────────────────────────────────────────────────────
52
+ /** Collects structured information from the user through a series of questions. */
53
+ export class GatherInfo {
54
+ questions = [];
55
+ outputKey;
56
+ completionAction;
57
+ prompt;
58
+ /**
59
+ * Creates a new GatherInfo.
60
+ * @param opts - Optional output key, completion action, and prompt configuration.
61
+ */
62
+ constructor(opts) {
63
+ this.outputKey = opts?.outputKey;
64
+ this.completionAction = opts?.completionAction;
65
+ this.prompt = opts?.prompt;
66
+ }
67
+ /**
68
+ * Adds a question to this gather operation.
69
+ * @param opts - Question configuration including key, question text, and optional type/confirm/prompt/functions.
70
+ * @returns This GatherInfo for chaining.
71
+ */
72
+ addQuestion(opts) {
73
+ this.questions.push(new GatherQuestion(opts));
74
+ return this;
75
+ }
76
+ /**
77
+ * Returns all questions in this gather operation.
78
+ * @returns The array of GatherQuestion instances.
79
+ */
80
+ getQuestions() {
81
+ return this.questions;
82
+ }
83
+ /**
84
+ * Serializes this gather operation to a plain object for SWML output.
85
+ * @returns A dictionary representation of the gather info and its questions.
86
+ */
87
+ toDict() {
88
+ if (!this.questions.length)
89
+ throw new Error('gather_info must have at least one question');
90
+ const d = { questions: this.questions.map((q) => q.toDict()) };
91
+ if (this.prompt)
92
+ d['prompt'] = this.prompt;
93
+ if (this.outputKey)
94
+ d['output_key'] = this.outputKey;
95
+ if (this.completionAction)
96
+ d['completion_action'] = this.completionAction;
97
+ return d;
98
+ }
99
+ }
100
+ /** A single step within a context, with prompt content, criteria, function restrictions, and navigation rules. */
101
+ export class Step {
102
+ /** The unique name identifying this step within its context. */
103
+ name;
104
+ text = null;
105
+ stepCriteria = null;
106
+ functions = null;
107
+ validSteps = null;
108
+ validContexts = null;
109
+ sections = [];
110
+ gatherInfo = null;
111
+ _end = false;
112
+ skipUserTurn = false;
113
+ _skipToNextStep = false;
114
+ resetSystemPrompt = null;
115
+ resetUserPrompt = null;
116
+ resetConsolidate = false;
117
+ resetFullReset = false;
118
+ /**
119
+ * Creates a new Step.
120
+ * @param name - The unique step name.
121
+ */
122
+ constructor(name) {
123
+ this.name = name;
124
+ }
125
+ /**
126
+ * Sets raw text content for this step, mutually exclusive with POM sections.
127
+ * @param text - The raw prompt text.
128
+ * @returns This step for chaining.
129
+ */
130
+ setText(text) {
131
+ if (this.sections.length)
132
+ throw new Error('Cannot use setText() when POM sections have been added.');
133
+ this.text = text;
134
+ return this;
135
+ }
136
+ /**
137
+ * Adds a POM section with a body, mutually exclusive with raw text.
138
+ * @param title - The section heading.
139
+ * @param body - The section body text.
140
+ * @returns This step for chaining.
141
+ */
142
+ addSection(title, body) {
143
+ if (this.text !== null)
144
+ throw new Error('Cannot add POM sections when setText() has been used.');
145
+ this.sections.push({ title, body });
146
+ return this;
147
+ }
148
+ /**
149
+ * Adds a POM section with bullet points, mutually exclusive with raw text.
150
+ * @param title - The section heading.
151
+ * @param bullets - The list of bullet point strings.
152
+ * @returns This step for chaining.
153
+ */
154
+ addBullets(title, bullets) {
155
+ if (this.text !== null)
156
+ throw new Error('Cannot add POM sections when setText() has been used.');
157
+ this.sections.push({ title, bullets });
158
+ return this;
159
+ }
160
+ /**
161
+ * Sets the criteria that must be met before this step is considered complete.
162
+ * @param criteria - A description of the completion criteria.
163
+ * @returns This step for chaining.
164
+ */
165
+ setStepCriteria(criteria) {
166
+ this.stepCriteria = criteria;
167
+ return this;
168
+ }
169
+ /**
170
+ * Restricts which SWAIG functions are available during this step.
171
+ * @param functions - A function name, list of names, or "*" for all.
172
+ * @returns This step for chaining.
173
+ */
174
+ setFunctions(functions) {
175
+ this.functions = functions;
176
+ return this;
177
+ }
178
+ /**
179
+ * Sets which steps the AI may navigate to from this step.
180
+ * @param steps - List of allowed step names.
181
+ * @returns This step for chaining.
182
+ */
183
+ setValidSteps(steps) {
184
+ this.validSteps = steps;
185
+ return this;
186
+ }
187
+ /**
188
+ * Sets which contexts the AI may navigate to from this step.
189
+ * @param contexts - List of allowed context names.
190
+ * @returns This step for chaining.
191
+ */
192
+ setValidContexts(contexts) {
193
+ this.validContexts = contexts;
194
+ return this;
195
+ }
196
+ /**
197
+ * Marks this step as an end step, terminating the conversation when reached.
198
+ * @param end - Whether this is an end step.
199
+ * @returns This step for chaining.
200
+ */
201
+ setEnd(end) {
202
+ this._end = end;
203
+ return this;
204
+ }
205
+ /**
206
+ * Sets whether to skip waiting for user input when entering this step.
207
+ * @param skip - Whether to skip the user turn.
208
+ * @returns This step for chaining.
209
+ */
210
+ setSkipUserTurn(skip) {
211
+ this.skipUserTurn = skip;
212
+ return this;
213
+ }
214
+ /**
215
+ * Sets whether to automatically advance to the next step after this one completes.
216
+ * @param skip - Whether to skip to the next step.
217
+ * @returns This step for chaining.
218
+ */
219
+ setSkipToNextStep(skip) {
220
+ this._skipToNextStep = skip;
221
+ return this;
222
+ }
223
+ /**
224
+ * Initializes a gather info operation on this step for collecting structured data.
225
+ * @param opts - Optional output key, completion action, and prompt configuration.
226
+ * @returns This step for chaining.
227
+ */
228
+ setGatherInfo(opts) {
229
+ this.gatherInfo = new GatherInfo(opts);
230
+ return this;
231
+ }
232
+ /**
233
+ * Adds a question to this step's gather info operation.
234
+ * @param opts - Question configuration including key, question text, and optional type/confirm/prompt/functions.
235
+ * @returns This step for chaining.
236
+ */
237
+ addGatherQuestion(opts) {
238
+ if (!this.gatherInfo)
239
+ throw new Error('Must call setGatherInfo() before addGatherQuestion()');
240
+ this.gatherInfo.addQuestion(opts);
241
+ return this;
242
+ }
243
+ /**
244
+ * Removes all POM sections and raw text from this step.
245
+ * @returns This step for chaining.
246
+ */
247
+ clearSections() {
248
+ this.sections = [];
249
+ this.text = null;
250
+ return this;
251
+ }
252
+ /**
253
+ * Sets the system prompt to use when resetting context at this step.
254
+ * @param systemPrompt - The replacement system prompt text.
255
+ * @returns This step for chaining.
256
+ */
257
+ setResetSystemPrompt(systemPrompt) {
258
+ this.resetSystemPrompt = systemPrompt;
259
+ return this;
260
+ }
261
+ /**
262
+ * Sets the user prompt to use when resetting context at this step.
263
+ * @param userPrompt - The replacement user prompt text.
264
+ * @returns This step for chaining.
265
+ */
266
+ setResetUserPrompt(userPrompt) {
267
+ this.resetUserPrompt = userPrompt;
268
+ return this;
269
+ }
270
+ /**
271
+ * Sets whether to consolidate conversation history when resetting at this step.
272
+ * @param consolidate - Whether to consolidate.
273
+ * @returns This step for chaining.
274
+ */
275
+ setResetConsolidate(consolidate) {
276
+ this.resetConsolidate = consolidate;
277
+ return this;
278
+ }
279
+ /**
280
+ * Sets whether to perform a full conversation reset at this step.
281
+ * @param fullReset - Whether to fully reset.
282
+ * @returns This step for chaining.
283
+ */
284
+ setResetFullReset(fullReset) {
285
+ this.resetFullReset = fullReset;
286
+ return this;
287
+ }
288
+ renderText() {
289
+ if (this.text !== null)
290
+ return this.text;
291
+ if (!this.sections.length)
292
+ throw new Error(`Step '${this.name}' has no text or POM sections defined`);
293
+ const parts = [];
294
+ for (const section of this.sections) {
295
+ parts.push(`## ${section.title}`);
296
+ if (section.bullets) {
297
+ for (const b of section.bullets)
298
+ parts.push(`- ${b}`);
299
+ }
300
+ else if (section.body) {
301
+ parts.push(section.body);
302
+ }
303
+ parts.push('');
304
+ }
305
+ return parts.join('\n').trim();
306
+ }
307
+ /**
308
+ * Serializes this step to a plain object for SWML output.
309
+ * @returns A dictionary representation of this step.
310
+ */
311
+ toDict() {
312
+ const d = { name: this.name, text: this.renderText() };
313
+ if (this.stepCriteria)
314
+ d['step_criteria'] = this.stepCriteria;
315
+ if (this.functions !== null)
316
+ d['functions'] = this.functions;
317
+ if (this.validSteps !== null)
318
+ d['valid_steps'] = this.validSteps;
319
+ if (this.validContexts !== null)
320
+ d['valid_contexts'] = this.validContexts;
321
+ if (this._end)
322
+ d['end'] = true;
323
+ if (this.skipUserTurn)
324
+ d['skip_user_turn'] = true;
325
+ if (this._skipToNextStep)
326
+ d['skip_to_next_step'] = true;
327
+ const reset = {};
328
+ if (this.resetSystemPrompt !== null)
329
+ reset['system_prompt'] = this.resetSystemPrompt;
330
+ if (this.resetUserPrompt !== null)
331
+ reset['user_prompt'] = this.resetUserPrompt;
332
+ if (this.resetConsolidate)
333
+ reset['consolidate'] = true;
334
+ if (this.resetFullReset)
335
+ reset['full_reset'] = true;
336
+ if (Object.keys(reset).length)
337
+ d['reset'] = reset;
338
+ if (this.gatherInfo)
339
+ d['gather_info'] = this.gatherInfo.toDict();
340
+ return d;
341
+ }
342
+ }
343
+ // ── Context ─────────────────────────────────────────────────────────
344
+ /** A named context containing ordered steps, prompt configuration, and navigation rules. */
345
+ export class Context {
346
+ /** The unique name identifying this context. */
347
+ name;
348
+ steps = new Map();
349
+ stepOrder = [];
350
+ _validContexts = null;
351
+ _validSteps = null;
352
+ _postPrompt = null;
353
+ _systemPrompt = null;
354
+ systemPromptSections = [];
355
+ _consolidate = false;
356
+ _fullReset = false;
357
+ _userPrompt = null;
358
+ _isolated = false;
359
+ promptText = null;
360
+ promptSections = [];
361
+ _enterFillers = null;
362
+ _exitFillers = null;
363
+ /**
364
+ * Creates a new Context.
365
+ * @param name - The unique context name.
366
+ */
367
+ constructor(name) {
368
+ this.name = name;
369
+ }
370
+ /**
371
+ * Adds a new step to this context.
372
+ * @param name - The unique step name within this context.
373
+ * @param opts - Optional shorthand for task text, bullets, criteria, functions, and valid steps.
374
+ * @returns The newly created Step for further configuration.
375
+ */
376
+ addStep(name, opts) {
377
+ if (this.steps.has(name))
378
+ throw new Error(`Step '${name}' already exists in context '${this.name}'`);
379
+ const step = new Step(name);
380
+ this.steps.set(name, step);
381
+ this.stepOrder.push(name);
382
+ if (opts?.task)
383
+ step.addSection('Task', opts.task);
384
+ if (opts?.bullets)
385
+ step.addBullets('Process', opts.bullets);
386
+ if (opts?.criteria)
387
+ step.setStepCriteria(opts.criteria);
388
+ if (opts?.functions)
389
+ step.setFunctions(opts.functions);
390
+ if (opts?.validSteps)
391
+ step.setValidSteps(opts.validSteps);
392
+ return step;
393
+ }
394
+ /**
395
+ * Returns a step by name.
396
+ * @param name - The step name to retrieve.
397
+ * @returns The matching Step, or undefined if not found.
398
+ */
399
+ getStep(name) {
400
+ return this.steps.get(name);
401
+ }
402
+ /**
403
+ * Removes a step from this context by name.
404
+ * @param name - The step name to remove.
405
+ * @returns This context for chaining.
406
+ */
407
+ removeStep(name) {
408
+ this.steps.delete(name);
409
+ this.stepOrder = this.stepOrder.filter((s) => s !== name);
410
+ return this;
411
+ }
412
+ /**
413
+ * Moves a step to a new position in the step order.
414
+ * @param name - The step name to move.
415
+ * @param position - The zero-based index to insert at.
416
+ * @returns This context for chaining.
417
+ */
418
+ moveStep(name, position) {
419
+ if (!this.steps.has(name))
420
+ throw new Error(`Step '${name}' not found in context '${this.name}'`);
421
+ this.stepOrder = this.stepOrder.filter((s) => s !== name);
422
+ this.stepOrder.splice(position, 0, name);
423
+ return this;
424
+ }
425
+ /**
426
+ * Sets which contexts the AI may navigate to from this context.
427
+ * @param contexts - List of allowed context names.
428
+ * @returns This context for chaining.
429
+ */
430
+ setValidContexts(contexts) {
431
+ this._validContexts = contexts;
432
+ return this;
433
+ }
434
+ /**
435
+ * Sets which steps the AI may navigate to from this context.
436
+ * @param steps - List of allowed step names.
437
+ * @returns This context for chaining.
438
+ */
439
+ setValidSteps(steps) {
440
+ this._validSteps = steps;
441
+ return this;
442
+ }
443
+ /**
444
+ * Sets the post-prompt text for this context.
445
+ * @param postPrompt - The post-prompt string.
446
+ * @returns This context for chaining.
447
+ */
448
+ setPostPrompt(postPrompt) {
449
+ this._postPrompt = postPrompt;
450
+ return this;
451
+ }
452
+ /**
453
+ * Sets raw system prompt text, mutually exclusive with system POM sections.
454
+ * @param systemPrompt - The system prompt string.
455
+ * @returns This context for chaining.
456
+ */
457
+ setSystemPrompt(systemPrompt) {
458
+ if (this.systemPromptSections.length)
459
+ throw new Error('Cannot use setSystemPrompt() when POM sections have been added.');
460
+ this._systemPrompt = systemPrompt;
461
+ return this;
462
+ }
463
+ /**
464
+ * Sets whether to consolidate conversation history when entering this context.
465
+ * @param consolidate - Whether to consolidate.
466
+ * @returns This context for chaining.
467
+ */
468
+ setConsolidate(consolidate) {
469
+ this._consolidate = consolidate;
470
+ return this;
471
+ }
472
+ /**
473
+ * Sets whether to fully reset conversation history when entering this context.
474
+ * @param fullReset - Whether to fully reset.
475
+ * @returns This context for chaining.
476
+ */
477
+ setFullReset(fullReset) {
478
+ this._fullReset = fullReset;
479
+ return this;
480
+ }
481
+ /**
482
+ * Sets the user prompt text for this context.
483
+ * @param userPrompt - The user prompt string.
484
+ * @returns This context for chaining.
485
+ */
486
+ setUserPrompt(userPrompt) {
487
+ this._userPrompt = userPrompt;
488
+ return this;
489
+ }
490
+ /**
491
+ * Sets whether this context is isolated from other contexts' conversation history.
492
+ * @param isolated - Whether to isolate.
493
+ * @returns This context for chaining.
494
+ */
495
+ setIsolated(isolated) {
496
+ this._isolated = isolated;
497
+ return this;
498
+ }
499
+ /**
500
+ * Adds a POM section to the system prompt, mutually exclusive with raw system prompt text.
501
+ * @param title - The section heading.
502
+ * @param body - The section body text.
503
+ * @returns This context for chaining.
504
+ */
505
+ addSystemSection(title, body) {
506
+ if (this._systemPrompt !== null)
507
+ throw new Error('Cannot add POM sections when setSystemPrompt() has been used.');
508
+ this.systemPromptSections.push({ title, body });
509
+ return this;
510
+ }
511
+ /**
512
+ * Adds a POM section with bullets to the system prompt, mutually exclusive with raw system prompt text.
513
+ * @param title - The section heading.
514
+ * @param bullets - The list of bullet point strings.
515
+ * @returns This context for chaining.
516
+ */
517
+ addSystemBullets(title, bullets) {
518
+ if (this._systemPrompt !== null)
519
+ throw new Error('Cannot add POM sections when setSystemPrompt() has been used.');
520
+ this.systemPromptSections.push({ title, bullets });
521
+ return this;
522
+ }
523
+ /**
524
+ * Sets raw prompt text for this context, mutually exclusive with POM prompt sections.
525
+ * @param prompt - The prompt string.
526
+ * @returns This context for chaining.
527
+ */
528
+ setPrompt(prompt) {
529
+ if (this.promptSections.length)
530
+ throw new Error('Cannot use setPrompt() when POM sections have been added.');
531
+ this.promptText = prompt;
532
+ return this;
533
+ }
534
+ /**
535
+ * Adds a POM section with a body to the context prompt, mutually exclusive with raw prompt text.
536
+ * @param title - The section heading.
537
+ * @param body - The section body text.
538
+ * @returns This context for chaining.
539
+ */
540
+ addSection(title, body) {
541
+ if (this.promptText !== null)
542
+ throw new Error('Cannot add POM sections when setPrompt() has been used.');
543
+ this.promptSections.push({ title, body });
544
+ return this;
545
+ }
546
+ /**
547
+ * Adds a POM section with bullets to the context prompt, mutually exclusive with raw prompt text.
548
+ * @param title - The section heading.
549
+ * @param bullets - The list of bullet point strings.
550
+ * @returns This context for chaining.
551
+ */
552
+ addBullets(title, bullets) {
553
+ if (this.promptText !== null)
554
+ throw new Error('Cannot add POM sections when setPrompt() has been used.');
555
+ this.promptSections.push({ title, bullets });
556
+ return this;
557
+ }
558
+ /**
559
+ * Sets filler phrases spoken when entering this context, keyed by language code.
560
+ * @param fillers - Map of language codes to arrays of filler phrases.
561
+ * @returns This context for chaining.
562
+ */
563
+ setEnterFillers(fillers) {
564
+ this._enterFillers = fillers;
565
+ return this;
566
+ }
567
+ /**
568
+ * Sets filler phrases spoken when exiting this context, keyed by language code.
569
+ * @param fillers - Map of language codes to arrays of filler phrases.
570
+ * @returns This context for chaining.
571
+ */
572
+ setExitFillers(fillers) {
573
+ this._exitFillers = fillers;
574
+ return this;
575
+ }
576
+ /**
577
+ * Adds enter filler phrases for a specific language.
578
+ * @param languageCode - The language code (e.g., "en-US").
579
+ * @param fillers - Array of filler phrases.
580
+ * @returns This context for chaining.
581
+ */
582
+ addEnterFiller(languageCode, fillers) {
583
+ if (!this._enterFillers)
584
+ this._enterFillers = {};
585
+ this._enterFillers[languageCode] = fillers;
586
+ return this;
587
+ }
588
+ /**
589
+ * Adds exit filler phrases for a specific language.
590
+ * @param languageCode - The language code (e.g., "en-US").
591
+ * @param fillers - Array of filler phrases.
592
+ * @returns This context for chaining.
593
+ */
594
+ addExitFiller(languageCode, fillers) {
595
+ if (!this._exitFillers)
596
+ this._exitFillers = {};
597
+ this._exitFillers[languageCode] = fillers;
598
+ return this;
599
+ }
600
+ /** @internal */
601
+ getSteps() {
602
+ return this.steps;
603
+ }
604
+ /** @internal */
605
+ getValidContexts() {
606
+ return this._validContexts;
607
+ }
608
+ renderPrompt() {
609
+ if (this.promptText !== null)
610
+ return this.promptText;
611
+ if (!this.promptSections.length)
612
+ return null;
613
+ return this.renderSections(this.promptSections);
614
+ }
615
+ renderSystemPrompt() {
616
+ if (this._systemPrompt !== null)
617
+ return this._systemPrompt;
618
+ if (!this.systemPromptSections.length)
619
+ return null;
620
+ return this.renderSections(this.systemPromptSections);
621
+ }
622
+ renderSections(sections) {
623
+ const parts = [];
624
+ for (const s of sections) {
625
+ parts.push(`## ${s.title}`);
626
+ if (s.bullets)
627
+ for (const b of s.bullets)
628
+ parts.push(`- ${b}`);
629
+ else if (s.body)
630
+ parts.push(s.body);
631
+ parts.push('');
632
+ }
633
+ return parts.join('\n').trim();
634
+ }
635
+ /**
636
+ * Serializes this context and all its steps to a plain object for SWML output.
637
+ * @returns A dictionary representation of this context.
638
+ */
639
+ toDict() {
640
+ if (!this.steps.size)
641
+ throw new Error(`Context '${this.name}' has no steps defined`);
642
+ const d = {
643
+ steps: this.stepOrder.map((name) => this.steps.get(name).toDict()),
644
+ };
645
+ if (this._validContexts !== null)
646
+ d['valid_contexts'] = this._validContexts;
647
+ if (this._validSteps !== null)
648
+ d['valid_steps'] = this._validSteps;
649
+ if (this._postPrompt !== null)
650
+ d['post_prompt'] = this._postPrompt;
651
+ const sp = this.renderSystemPrompt();
652
+ if (sp !== null)
653
+ d['system_prompt'] = sp;
654
+ if (this._consolidate)
655
+ d['consolidate'] = true;
656
+ if (this._fullReset)
657
+ d['full_reset'] = true;
658
+ if (this._userPrompt !== null)
659
+ d['user_prompt'] = this._userPrompt;
660
+ if (this._isolated)
661
+ d['isolated'] = true;
662
+ if (this.promptSections.length) {
663
+ d['pom'] = this.promptSections;
664
+ }
665
+ else if (this.promptText !== null) {
666
+ d['prompt'] = this.promptText;
667
+ }
668
+ if (this._enterFillers)
669
+ d['enter_fillers'] = this._enterFillers;
670
+ if (this._exitFillers)
671
+ d['exit_fillers'] = this._exitFillers;
672
+ return d;
673
+ }
674
+ }
675
+ // ── ContextBuilder ──────────────────────────────────────────────────
676
+ /** Builds and validates a collection of named contexts for multi-step AI workflows. */
677
+ export class ContextBuilder {
678
+ contexts = new Map();
679
+ contextOrder = [];
680
+ /**
681
+ * Adds a new named context to the builder.
682
+ * @param name - The unique context name.
683
+ * @returns The newly created Context for further configuration.
684
+ */
685
+ addContext(name) {
686
+ if (this.contexts.has(name))
687
+ throw new Error(`Context '${name}' already exists`);
688
+ const ctx = new Context(name);
689
+ this.contexts.set(name, ctx);
690
+ this.contextOrder.push(name);
691
+ return ctx;
692
+ }
693
+ /**
694
+ * Returns a context by name.
695
+ * @param name - The context name to retrieve.
696
+ * @returns The matching Context, or undefined if not found.
697
+ */
698
+ getContext(name) {
699
+ return this.contexts.get(name);
700
+ }
701
+ /**
702
+ * Validates that all contexts have steps, naming constraints are met, and cross-context references are valid.
703
+ * @throws Error if validation fails.
704
+ */
705
+ validate() {
706
+ if (!this.contexts.size)
707
+ throw new Error('At least one context must be defined');
708
+ if (this.contexts.size === 1) {
709
+ const name = [...this.contexts.keys()][0];
710
+ if (name !== 'default')
711
+ throw new Error("When using a single context, it must be named 'default'");
712
+ }
713
+ for (const [name, ctx] of this.contexts) {
714
+ if (!ctx.getSteps().size)
715
+ throw new Error(`Context '${name}' must have at least one step`);
716
+ }
717
+ // Validate context references
718
+ for (const [, ctx] of this.contexts) {
719
+ const vc = ctx.getValidContexts();
720
+ if (vc) {
721
+ for (const ref of vc) {
722
+ if (!this.contexts.has(ref)) {
723
+ throw new Error(`Context '${ctx.name}' references unknown context '${ref}'`);
724
+ }
725
+ }
726
+ }
727
+ }
728
+ }
729
+ /**
730
+ * Validates and serializes all contexts to a plain object for SWML output.
731
+ * @returns A dictionary mapping context names to their serialized representations.
732
+ */
733
+ toDict() {
734
+ this.validate();
735
+ const result = {};
736
+ for (const name of this.contextOrder) {
737
+ result[name] = this.contexts.get(name).toDict();
738
+ }
739
+ return result;
740
+ }
741
+ }
742
+ // ── Helper ──────────────────────────────────────────────────────────
743
+ /**
744
+ * Creates a standalone Context instance without a ContextBuilder.
745
+ * @param name - The context name (defaults to "default").
746
+ * @returns A new Context instance.
747
+ */
748
+ export function createSimpleContext(name = 'default') {
749
+ return new Context(name);
750
+ }
751
+ //# sourceMappingURL=ContextBuilder.js.map