@voctiv/agent-runtime 0.2.2 → 0.4.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 (628) hide show
  1. package/README.md +45 -20
  2. package/dist/barge-in/barge-in-module.d.ts +0 -1
  3. package/dist/barge-in/barge-in-module.js +0 -1
  4. package/dist/barge-in/barge-in-service.d.ts +1 -1
  5. package/dist/barge-in/barge-in-service.js +33 -2
  6. package/dist/barge-in/barge-in-strategy.d.ts +11 -9
  7. package/dist/barge-in/barge-in-strategy.js +39 -23
  8. package/dist/barge-in/echo-cancellation.d.ts +0 -1
  9. package/dist/barge-in/echo-cancellation.js +0 -1
  10. package/dist/barge-in/echo.d.ts +0 -1
  11. package/dist/barge-in/echo.js +0 -1
  12. package/dist/barge-in/lexicon.d.ts +0 -1
  13. package/dist/barge-in/lexicon.js +0 -1
  14. package/dist/barge-in/speech-ledger.d.ts +0 -2
  15. package/dist/barge-in/speech-ledger.js +3 -4
  16. package/dist/barge-in/word-classifier.d.ts +0 -1
  17. package/dist/barge-in/word-classifier.js +0 -1
  18. package/dist/call/asr-usage.d.ts +53 -0
  19. package/dist/call/asr-usage.js +42 -0
  20. package/dist/call/async.d.ts +2 -0
  21. package/dist/call/async.js +14 -0
  22. package/dist/call/env.d.ts +88 -0
  23. package/dist/call/env.js +294 -0
  24. package/dist/call/extraction/core.d.ts +99 -0
  25. package/dist/call/extraction/core.js +259 -0
  26. package/dist/call/extraction/defaults.d.ts +10 -0
  27. package/dist/call/extraction/defaults.js +25 -0
  28. package/dist/call/extraction/index.d.ts +7 -0
  29. package/dist/call/extraction/index.js +24 -0
  30. package/dist/call/extraction/orchestrator.d.ts +144 -0
  31. package/dist/call/extraction/orchestrator.js +309 -0
  32. package/dist/call/failed-leg.d.ts +43 -0
  33. package/dist/call/failed-leg.js +77 -0
  34. package/dist/call/http.d.ts +94 -0
  35. package/dist/call/http.js +145 -0
  36. package/dist/call/ledger.d.ts +18 -0
  37. package/dist/call/ledger.js +80 -0
  38. package/dist/call/llm-tap.d.ts +7 -0
  39. package/dist/call/llm-tap.js +41 -0
  40. package/dist/call/logging.d.ts +16 -0
  41. package/dist/call/logging.js +92 -0
  42. package/dist/call/outbound.d.ts +144 -0
  43. package/dist/call/outbound.js +404 -0
  44. package/dist/call/output.d.ts +108 -0
  45. package/dist/call/output.js +77 -0
  46. package/dist/call/plan.d.ts +119 -0
  47. package/dist/call/plan.js +2 -0
  48. package/dist/call/recall.d.ts +115 -0
  49. package/dist/call/recall.js +312 -0
  50. package/dist/call/records.d.ts +9 -0
  51. package/dist/call/records.js +71 -0
  52. package/dist/call/report.d.ts +65 -0
  53. package/dist/call/report.js +263 -0
  54. package/dist/call/sanitize.d.ts +16 -0
  55. package/dist/call/sanitize.js +72 -0
  56. package/dist/call/sip.d.ts +111 -0
  57. package/dist/call/sip.js +212 -0
  58. package/dist/call/voicemail.d.ts +159 -0
  59. package/dist/call/voicemail.js +246 -0
  60. package/dist/core/call-disposables.d.ts +2 -2
  61. package/dist/core/call-disposables.js +8 -3
  62. package/dist/core/call-ended-budget.d.ts +11 -0
  63. package/dist/core/call-ended-budget.js +31 -0
  64. package/dist/core/container.d.ts +1 -3
  65. package/dist/core/container.js +9 -49
  66. package/dist/core/create-agent-script.d.ts +6 -1
  67. package/dist/core/create-agent-script.js +161 -28
  68. package/dist/core/decorators.d.ts +0 -1
  69. package/dist/core/decorators.js +0 -1
  70. package/dist/core/define-module.d.ts +0 -1
  71. package/dist/core/define-module.js +0 -1
  72. package/dist/core/dump-policy.d.ts +0 -1
  73. package/dist/core/dump-policy.js +0 -1
  74. package/dist/core/hooks.d.ts +15 -4
  75. package/dist/core/hooks.js +0 -1
  76. package/dist/core/media-bootstrap.d.ts +19 -4
  77. package/dist/core/media-bootstrap.js +53 -13
  78. package/dist/core/open-timeout.d.ts +12 -0
  79. package/dist/core/open-timeout.js +48 -0
  80. package/dist/core/provide-media.d.ts +0 -8
  81. package/dist/core/provide-media.js +1 -22
  82. package/dist/core/runtime-events.d.ts +28 -0
  83. package/dist/{llm/llm-module.js → core/runtime-events.js} +19 -16
  84. package/dist/core/tokens.d.ts +0 -10
  85. package/dist/core/tokens.js +0 -1
  86. package/dist/core/types.d.ts +0 -1
  87. package/dist/core/types.js +0 -1
  88. package/dist/core/version.d.ts +2 -3
  89. package/dist/core/version.js +2 -3
  90. package/dist/dialog/dialog-definition.d.ts +0 -5
  91. package/dist/dialog/dialog-definition.js +1 -6
  92. package/dist/dialog/dialog-ports.d.ts +3 -1
  93. package/dist/dialog/dialog-ports.js +10 -2
  94. package/dist/dialog/online-call-host.d.ts +26 -4
  95. package/dist/dialog/online-call-host.js +94 -12
  96. package/dist/dialog/online-call-module.d.ts +5 -11
  97. package/dist/dialog/online-call-module.js +4 -36
  98. package/dist/dialog/turn-metrics.d.ts +46 -0
  99. package/dist/dialog/turn-metrics.js +141 -0
  100. package/dist/dialog-events/active-media.d.ts +50 -2
  101. package/dist/dialog-events/active-media.js +146 -34
  102. package/dist/dialog-events/event-source.d.ts +0 -1
  103. package/dist/dialog-events/event-source.js +0 -1
  104. package/dist/dialog-events/virtual-finals-module.d.ts +0 -1
  105. package/dist/dialog-events/virtual-finals-module.js +0 -1
  106. package/dist/dialog-events/virtual-finals.d.ts +2 -1
  107. package/dist/dialog-events/virtual-finals.js +1 -1
  108. package/dist/index.d.ts +1 -4
  109. package/dist/index.js +8 -26
  110. package/dist/lifecycle/lifecycle.d.ts +25 -3
  111. package/dist/lifecycle/lifecycle.js +92 -14
  112. package/dist/result/result-store.d.ts +0 -1
  113. package/dist/result/result-store.js +0 -1
  114. package/dist/templates/authoring.d.ts +222 -0
  115. package/dist/templates/authoring.js +2 -0
  116. package/dist/templates/builder/agent.d.ts +34 -0
  117. package/dist/templates/builder/agent.js +87 -0
  118. package/dist/templates/builder/blocks.d.ts +54 -0
  119. package/dist/templates/builder/blocks.js +92 -0
  120. package/dist/templates/builder/data-check.d.ts +2 -0
  121. package/dist/templates/builder/data-check.js +61 -0
  122. package/dist/templates/builder/draft.d.ts +73 -0
  123. package/dist/templates/builder/draft.js +142 -0
  124. package/dist/templates/builder/index.d.ts +8 -0
  125. package/dist/templates/builder/index.js +55 -0
  126. package/dist/templates/builder/presets.d.ts +34 -0
  127. package/dist/templates/builder/presets.js +45 -0
  128. package/dist/templates/builder/spec-of.d.ts +13 -0
  129. package/dist/templates/builder/spec-of.js +18 -0
  130. package/dist/templates/builder/steps.d.ts +25 -0
  131. package/dist/templates/builder/steps.js +27 -0
  132. package/dist/templates/business/adapter.d.ts +12 -0
  133. package/dist/templates/business/adapter.js +980 -0
  134. package/dist/templates/business/agent-integrations.d.ts +24 -0
  135. package/dist/templates/business/agent-integrations.js +233 -0
  136. package/dist/templates/business/extraction-rules-transcript.d.ts +158 -0
  137. package/dist/templates/business/extraction-rules-transcript.js +405 -0
  138. package/dist/templates/business/extraction-rules.d.ts +107 -0
  139. package/dist/templates/business/extraction-rules.js +267 -0
  140. package/dist/templates/business/index.d.ts +6 -0
  141. package/dist/{result → templates/business}/index.js +8 -2
  142. package/dist/templates/business/input.d.ts +56 -0
  143. package/dist/templates/business/input.js +299 -0
  144. package/dist/templates/business/integration-data.d.ts +87 -0
  145. package/dist/templates/business/integration-data.js +312 -0
  146. package/dist/templates/business/integrations.d.ts +76 -0
  147. package/dist/templates/business/integrations.js +535 -0
  148. package/dist/templates/business/legs-policy.d.ts +43 -0
  149. package/dist/templates/business/legs-policy.js +268 -0
  150. package/dist/templates/business/report-label.d.ts +7 -0
  151. package/dist/templates/business/report-label.js +29 -0
  152. package/dist/templates/business/report.d.ts +67 -0
  153. package/dist/templates/business/report.js +284 -0
  154. package/dist/templates/business/result-catalog.d.ts +142 -0
  155. package/dist/templates/business/result-catalog.js +208 -0
  156. package/dist/templates/business/result-rules.d.ts +105 -0
  157. package/dist/templates/business/result-rules.js +167 -0
  158. package/dist/templates/business/results.d.ts +73 -0
  159. package/dist/templates/business/results.js +183 -0
  160. package/dist/templates/business/rule-env.d.ts +18 -0
  161. package/dist/templates/business/rule-env.js +38 -0
  162. package/dist/templates/business/sources.d.ts +76 -0
  163. package/dist/templates/business/sources.js +239 -0
  164. package/dist/templates/business/test-presets.d.ts +10 -0
  165. package/dist/templates/business/test-presets.js +68 -0
  166. package/dist/templates/catalog-match/block.d.ts +32 -0
  167. package/dist/templates/catalog-match/block.js +95 -0
  168. package/dist/templates/catalog-match/catalog.d.ts +77 -0
  169. package/dist/templates/catalog-match/catalog.js +222 -0
  170. package/dist/templates/catalog-match/recognizer.d.ts +44 -0
  171. package/dist/templates/catalog-match/recognizer.js +206 -0
  172. package/dist/templates/catalog-match/reconcile.d.ts +4 -0
  173. package/dist/templates/catalog-match/reconcile.js +39 -0
  174. package/dist/templates/catalog-match/request.d.ts +57 -0
  175. package/dist/templates/catalog-match/request.js +139 -0
  176. package/dist/templates/catalog-match/types.d.ts +197 -0
  177. package/dist/templates/catalog-match/types.js +2 -0
  178. package/dist/templates/catalog-match/verify.d.ts +76 -0
  179. package/dist/templates/catalog-match/verify.js +220 -0
  180. package/dist/templates/decision-model/index.d.ts +52 -0
  181. package/dist/templates/decision-model/index.js +68 -0
  182. package/dist/templates/engine/asr-picks.d.ts +11 -0
  183. package/dist/templates/engine/asr-picks.js +44 -0
  184. package/dist/templates/engine/call.d.ts +104 -0
  185. package/dist/templates/engine/call.js +322 -0
  186. package/dist/templates/engine/config.d.ts +447 -0
  187. package/dist/templates/engine/config.js +493 -0
  188. package/dist/templates/engine/define.d.ts +1 -0
  189. package/dist/templates/engine/define.js +9 -0
  190. package/dist/templates/engine/dialog.d.ts +253 -0
  191. package/dist/templates/engine/dialog.js +167 -0
  192. package/dist/templates/engine/echo-numbers.d.ts +7 -0
  193. package/dist/templates/engine/echo-numbers.js +19 -0
  194. package/dist/templates/engine/expand.d.ts +6 -0
  195. package/dist/templates/engine/expand.js +125 -0
  196. package/dist/templates/engine/filters.d.ts +81 -0
  197. package/dist/templates/engine/filters.js +654 -0
  198. package/dist/templates/engine/hook-state.d.ts +18 -0
  199. package/dist/templates/engine/hook-state.js +100 -0
  200. package/dist/templates/engine/hooks.d.ts +51 -0
  201. package/dist/templates/engine/hooks.js +310 -0
  202. package/dist/templates/engine/index.d.ts +13 -0
  203. package/dist/templates/engine/index.js +30 -0
  204. package/dist/templates/engine/language-vars.d.ts +6 -0
  205. package/dist/templates/engine/language-vars.js +40 -0
  206. package/dist/templates/engine/language.d.ts +32 -0
  207. package/dist/templates/engine/language.js +155 -0
  208. package/dist/templates/engine/lookup.d.ts +20 -0
  209. package/dist/templates/engine/lookup.js +52 -0
  210. package/dist/templates/engine/match.d.ts +48 -0
  211. package/dist/templates/engine/match.js +253 -0
  212. package/dist/templates/engine/media.d.ts +428 -0
  213. package/dist/templates/engine/media.js +1240 -0
  214. package/dist/templates/engine/script.d.ts +11 -0
  215. package/dist/templates/engine/script.js +69 -0
  216. package/dist/templates/engine/turns/actions.d.ts +5 -0
  217. package/dist/templates/engine/turns/actions.js +128 -0
  218. package/dist/templates/engine/turns/carry.d.ts +15 -0
  219. package/dist/templates/engine/turns/carry.js +74 -0
  220. package/dist/templates/engine/turns/choices-offers.d.ts +21 -0
  221. package/dist/templates/engine/turns/choices-offers.js +143 -0
  222. package/dist/templates/engine/turns/choices.d.ts +22 -0
  223. package/dist/templates/engine/turns/choices.js +141 -0
  224. package/dist/templates/engine/turns/closing-guard.d.ts +73 -0
  225. package/dist/templates/engine/turns/closing-guard.js +162 -0
  226. package/dist/templates/engine/turns/closing.d.ts +10 -0
  227. package/dist/templates/engine/turns/closing.js +82 -0
  228. package/dist/templates/engine/turns/compile-forms.d.ts +24 -0
  229. package/dist/templates/engine/turns/compile-forms.js +68 -0
  230. package/dist/templates/engine/turns/context.d.ts +96 -0
  231. package/dist/templates/engine/turns/context.js +158 -0
  232. package/dist/templates/engine/turns/events.d.ts +409 -0
  233. package/dist/templates/engine/turns/events.js +5 -0
  234. package/dist/templates/engine/turns/hook-forms.d.ts +62 -0
  235. package/dist/templates/engine/turns/hook-forms.js +79 -0
  236. package/dist/templates/engine/turns/ladders.d.ts +42 -0
  237. package/dist/templates/engine/turns/ladders.js +119 -0
  238. package/dist/templates/engine/turns/language.d.ts +8 -0
  239. package/dist/templates/engine/turns/language.js +121 -0
  240. package/dist/templates/engine/turns/notes-dates.d.ts +9 -0
  241. package/dist/templates/engine/turns/notes-dates.js +42 -0
  242. package/dist/templates/engine/turns/notes-detect.d.ts +13 -0
  243. package/dist/templates/engine/turns/notes-detect.js +36 -0
  244. package/dist/templates/engine/turns/notes.d.ts +27 -0
  245. package/dist/templates/engine/turns/notes.js +176 -0
  246. package/dist/templates/engine/turns/opening.d.ts +34 -0
  247. package/dist/templates/engine/turns/opening.js +306 -0
  248. package/dist/templates/engine/turns/overtalk.d.ts +21 -0
  249. package/dist/templates/engine/turns/overtalk.js +247 -0
  250. package/dist/templates/engine/turns/pending.d.ts +9 -0
  251. package/dist/templates/engine/turns/pending.js +88 -0
  252. package/dist/templates/engine/turns/reply-rules.d.ts +184 -0
  253. package/dist/templates/engine/turns/reply-rules.js +335 -0
  254. package/dist/templates/engine/turns/rules.d.ts +20 -0
  255. package/dist/templates/engine/turns/rules.js +144 -0
  256. package/dist/templates/engine/turns/silence.d.ts +7 -0
  257. package/dist/templates/engine/turns/silence.js +127 -0
  258. package/dist/templates/engine/turns/state-spec.d.ts +305 -0
  259. package/dist/templates/engine/turns/state-spec.js +2 -0
  260. package/dist/templates/engine/turns/state.d.ts +38 -0
  261. package/dist/templates/engine/turns/state.js +101 -0
  262. package/dist/templates/engine/turns/tracking.d.ts +50 -0
  263. package/dist/templates/engine/turns/tracking.js +184 -0
  264. package/dist/templates/engine/turns/transfer.d.ts +19 -0
  265. package/dist/templates/engine/turns/transfer.js +93 -0
  266. package/dist/templates/engine/turns/turn.d.ts +17 -0
  267. package/dist/templates/engine/turns/turn.js +363 -0
  268. package/dist/templates/engine/turns/voicemail.d.ts +8 -0
  269. package/dist/templates/engine/turns/voicemail.js +101 -0
  270. package/dist/templates/engine/variants.d.ts +36 -0
  271. package/dist/templates/engine/variants.js +159 -0
  272. package/dist/templates/engine/voice-dialog.d.ts +24 -0
  273. package/dist/templates/engine/voice-dialog.js +157 -0
  274. package/dist/templates/engine/voice.d.ts +174 -0
  275. package/dist/templates/engine/voice.js +1337 -0
  276. package/dist/templates/expr/caller-after.d.ts +20 -0
  277. package/dist/templates/expr/caller-after.js +53 -0
  278. package/dist/templates/expr/check.d.ts +19 -0
  279. package/dist/templates/expr/check.js +314 -0
  280. package/dist/templates/expr/compiled.d.ts +8 -0
  281. package/dist/templates/expr/compiled.js +8 -0
  282. package/dist/templates/expr/context.d.ts +28 -0
  283. package/dist/templates/expr/context.js +55 -0
  284. package/dist/templates/expr/day-table.d.ts +4 -0
  285. package/dist/templates/expr/day-table.js +80 -0
  286. package/dist/templates/expr/evaluate.d.ts +26 -0
  287. package/dist/templates/expr/evaluate.js +885 -0
  288. package/dist/templates/expr/facts.d.ts +7 -0
  289. package/dist/templates/expr/facts.js +81 -0
  290. package/dist/templates/expr/index.d.ts +4 -0
  291. package/dist/templates/expr/index.js +11 -0
  292. package/dist/templates/expr/line-tally.d.ts +64 -0
  293. package/dist/templates/expr/line-tally.js +88 -0
  294. package/dist/templates/expr/matchers-state.d.ts +23 -0
  295. package/dist/templates/expr/matchers-state.js +37 -0
  296. package/dist/templates/expr/matchers.d.ts +12 -0
  297. package/dist/templates/expr/matchers.js +99 -0
  298. package/dist/templates/expr/readers.d.ts +14 -0
  299. package/dist/templates/expr/readers.js +351 -0
  300. package/dist/templates/expr/slots.d.ts +14 -0
  301. package/dist/templates/expr/slots.js +678 -0
  302. package/dist/templates/expr/template.d.ts +12 -0
  303. package/dist/templates/expr/template.js +76 -0
  304. package/dist/templates/expr/test-call.d.ts +8 -0
  305. package/dist/templates/expr/test-call.js +32 -0
  306. package/dist/templates/expr/transcript.d.ts +36 -0
  307. package/dist/templates/expr/transcript.js +394 -0
  308. package/dist/templates/expr/types.d.ts +1011 -0
  309. package/dist/templates/expr/types.js +2 -0
  310. package/dist/templates/expr/values.d.ts +42 -0
  311. package/dist/templates/expr/values.js +166 -0
  312. package/dist/templates/expr/voice-ops.d.ts +26 -0
  313. package/dist/templates/expr/voice-ops.js +85 -0
  314. package/dist/templates/helpers/decision.d.ts +55 -0
  315. package/dist/templates/helpers/decision.js +83 -0
  316. package/dist/templates/helpers/fuzzy.d.ts +9 -0
  317. package/dist/templates/helpers/fuzzy.js +44 -0
  318. package/dist/templates/helpers/index.d.ts +6 -0
  319. package/dist/templates/helpers/index.js +35 -0
  320. package/dist/templates/index.d.ts +9 -0
  321. package/dist/templates/index.js +68 -0
  322. package/dist/templates/integration-forms.d.ts +252 -0
  323. package/dist/templates/integration-forms.js +2 -0
  324. package/dist/templates/knowledge/block.d.ts +44 -0
  325. package/dist/templates/knowledge/block.js +199 -0
  326. package/dist/templates/knowledge/dense.d.ts +87 -0
  327. package/dist/templates/knowledge/dense.js +158 -0
  328. package/dist/templates/knowledge/flow.d.ts +328 -0
  329. package/dist/templates/knowledge/flow.js +509 -0
  330. package/dist/templates/knowledge/retriever.d.ts +75 -0
  331. package/dist/templates/knowledge/retriever.js +204 -0
  332. package/dist/templates/knowledge/service.d.ts +77 -0
  333. package/dist/templates/knowledge/service.js +316 -0
  334. package/dist/templates/knowledge/text.d.ts +31 -0
  335. package/dist/templates/knowledge/text.js +66 -0
  336. package/dist/templates/knowledge/types.d.ts +120 -0
  337. package/dist/templates/knowledge/types.js +2 -0
  338. package/dist/templates/locale.d.ts +259 -0
  339. package/dist/templates/locale.js +4 -0
  340. package/dist/templates/locales/en-lexicons.d.ts +9 -0
  341. package/dist/templates/locales/en-lexicons.js +101 -0
  342. package/dist/templates/locales/en.d.ts +132 -0
  343. package/dist/templates/locales/en.js +261 -0
  344. package/dist/templates/locales/es.d.ts +65 -0
  345. package/dist/templates/locales/es.js +208 -0
  346. package/dist/templates/locales/hi.d.ts +47 -0
  347. package/dist/templates/locales/hi.js +235 -0
  348. package/dist/templates/locales/id.d.ts +43 -0
  349. package/dist/templates/locales/id.js +127 -0
  350. package/dist/templates/locales/index.d.ts +34 -0
  351. package/dist/templates/locales/index.js +112 -0
  352. package/dist/templates/locales/kk-lexicons.d.ts +22 -0
  353. package/dist/templates/locales/kk-lexicons.js +89 -0
  354. package/dist/templates/locales/kk.d.ts +140 -0
  355. package/dist/templates/locales/kk.js +306 -0
  356. package/dist/templates/locales/kn.d.ts +29 -0
  357. package/dist/templates/locales/kn.js +22 -0
  358. package/dist/templates/locales/listen-only.d.ts +15 -0
  359. package/dist/templates/locales/listen-only.js +27 -0
  360. package/dist/templates/locales/ms-lexicons.d.ts +13 -0
  361. package/dist/templates/locales/ms-lexicons.js +113 -0
  362. package/dist/templates/locales/ms.d.ts +76 -0
  363. package/dist/templates/locales/ms.js +179 -0
  364. package/dist/templates/locales/parse.d.ts +86 -0
  365. package/dist/templates/locales/parse.js +461 -0
  366. package/dist/templates/locales/ru-lexicons.d.ts +27 -0
  367. package/dist/templates/locales/ru-lexicons.js +127 -0
  368. package/dist/templates/locales/ru-stem.d.ts +2 -0
  369. package/dist/templates/locales/ru-stem.js +53 -0
  370. package/dist/templates/locales/ru.d.ts +143 -0
  371. package/dist/templates/locales/ru.js +387 -0
  372. package/dist/templates/locales/shared.d.ts +122 -0
  373. package/dist/templates/locales/shared.js +318 -0
  374. package/dist/templates/locales/ta.d.ts +29 -0
  375. package/dist/templates/locales/ta.js +22 -0
  376. package/dist/templates/locales/te.d.ts +29 -0
  377. package/dist/templates/locales/te.js +22 -0
  378. package/dist/templates/locales/tl-lexicons.d.ts +2 -0
  379. package/dist/templates/locales/tl-lexicons.js +230 -0
  380. package/dist/templates/locales/tl.d.ts +59 -0
  381. package/dist/templates/locales/tl.js +82 -0
  382. package/dist/templates/locales/vi.d.ts +59 -0
  383. package/dist/templates/locales/vi.js +160 -0
  384. package/dist/templates/locales/word-regex.d.ts +13 -0
  385. package/dist/templates/locales/word-regex.js +43 -0
  386. package/dist/templates/locales/words-kk.d.ts +2 -0
  387. package/dist/templates/locales/words-kk.js +23 -0
  388. package/dist/templates/locales/words-ru.d.ts +4 -0
  389. package/dist/templates/locales/words-ru.js +92 -0
  390. package/dist/templates/spec.d.ts +2856 -0
  391. package/dist/templates/spec.js +2 -0
  392. package/dist/testing/fake-channel.d.ts +0 -1
  393. package/dist/testing/fake-channel.js +0 -1
  394. package/dist/testing/index.d.ts +24 -1
  395. package/dist/testing/index.js +117 -2
  396. package/dist/voice/call-voice.d.ts +581 -0
  397. package/dist/voice/call-voice.js +1399 -0
  398. package/dist/voice/intent-interrupt.d.ts +32 -0
  399. package/dist/voice/intent-interrupt.js +91 -0
  400. package/dist/voice/self-echo.d.ts +28 -0
  401. package/dist/voice/self-echo.js +135 -0
  402. package/dist/voice/sentence-tag.d.ts +5 -0
  403. package/dist/voice/sentence-tag.js +42 -0
  404. package/dist/voice/speech-leak.d.ts +74 -0
  405. package/dist/voice/speech-leak.js +216 -0
  406. package/dist/voice/transfer.d.ts +90 -0
  407. package/dist/voice/transfer.js +331 -0
  408. package/dist/voice/turn-timing.d.ts +293 -0
  409. package/dist/voice/turn-timing.js +567 -0
  410. package/docs/README.md +3 -6
  411. package/docs/framework/README.md +6 -10
  412. package/docs/framework/architecture.md +2 -2
  413. package/docs/framework/container-and-di.md +1 -2
  414. package/docs/framework/invariants.md +6 -6
  415. package/docs/framework/package-layout.md +16 -23
  416. package/docs/framework/testing.md +8 -6
  417. package/package.json +10 -54
  418. package/skill/SKILL.md +213 -0
  419. package/skill/references/agent-file.md +230 -0
  420. package/skill/references/blocks.md +246 -0
  421. package/skill/references/builder.md +163 -0
  422. package/skill/references/business.md +659 -0
  423. package/skill/references/conversation.md +586 -0
  424. package/skill/references/core.md +89 -0
  425. package/skill/references/examples.md +73 -0
  426. package/skill/references/expressions.md +342 -0
  427. package/skill/references/format.md +114 -0
  428. package/skill/references/locales.md +149 -0
  429. package/skill/references/migration.md +133 -0
  430. package/skill/references/new-agent.md +81 -0
  431. package/skill/references/porting.md +109 -0
  432. package/templates/agent/README.md +50 -0
  433. package/templates/agent/package.json +24 -0
  434. package/templates/agent/src/agent.spec.ts +51 -0
  435. package/templates/agent/src/agent.ts +268 -0
  436. package/templates/agent/src/entities.ts +44 -0
  437. package/templates/agent/src/prompt.sqrl +21 -0
  438. package/templates/agent/src/variants/fixed-opening.ts +144 -0
  439. package/templates/agent/tsconfig.build.json +4 -0
  440. package/templates/agent/tsconfig.json +18 -0
  441. package/dist/asr/index.d.ts +0 -3
  442. package/dist/asr/index.d.ts.map +0 -1
  443. package/dist/asr/index.js +0 -6
  444. package/dist/asr/index.js.map +0 -1
  445. package/dist/barge-in/barge-in-module.d.ts.map +0 -1
  446. package/dist/barge-in/barge-in-module.js.map +0 -1
  447. package/dist/barge-in/barge-in-service.d.ts.map +0 -1
  448. package/dist/barge-in/barge-in-service.js.map +0 -1
  449. package/dist/barge-in/barge-in-strategy.d.ts.map +0 -1
  450. package/dist/barge-in/barge-in-strategy.js.map +0 -1
  451. package/dist/barge-in/echo-cancellation.d.ts.map +0 -1
  452. package/dist/barge-in/echo-cancellation.js.map +0 -1
  453. package/dist/barge-in/echo.d.ts.map +0 -1
  454. package/dist/barge-in/echo.js.map +0 -1
  455. package/dist/barge-in/index.d.ts +0 -13
  456. package/dist/barge-in/index.d.ts.map +0 -1
  457. package/dist/barge-in/index.js +0 -28
  458. package/dist/barge-in/index.js.map +0 -1
  459. package/dist/barge-in/lexicon.d.ts.map +0 -1
  460. package/dist/barge-in/lexicon.js.map +0 -1
  461. package/dist/barge-in/lexicons/en.d.ts +0 -2
  462. package/dist/barge-in/lexicons/en.d.ts.map +0 -1
  463. package/dist/barge-in/lexicons/en.js +0 -36
  464. package/dist/barge-in/lexicons/en.js.map +0 -1
  465. package/dist/barge-in/lexicons/id.d.ts +0 -2
  466. package/dist/barge-in/lexicons/id.d.ts.map +0 -1
  467. package/dist/barge-in/lexicons/id.js +0 -39
  468. package/dist/barge-in/lexicons/id.js.map +0 -1
  469. package/dist/barge-in/lexicons/index.d.ts +0 -10
  470. package/dist/barge-in/lexicons/index.d.ts.map +0 -1
  471. package/dist/barge-in/lexicons/index.js +0 -11
  472. package/dist/barge-in/lexicons/index.js.map +0 -1
  473. package/dist/barge-in/lexicons/ru.d.ts +0 -2
  474. package/dist/barge-in/lexicons/ru.d.ts.map +0 -1
  475. package/dist/barge-in/lexicons/ru.js +0 -39
  476. package/dist/barge-in/lexicons/ru.js.map +0 -1
  477. package/dist/barge-in/speech-ledger.d.ts.map +0 -1
  478. package/dist/barge-in/speech-ledger.js.map +0 -1
  479. package/dist/barge-in/word-classifier.d.ts.map +0 -1
  480. package/dist/barge-in/word-classifier.js.map +0 -1
  481. package/dist/bridge/bridge.d.ts +0 -28
  482. package/dist/bridge/bridge.d.ts.map +0 -1
  483. package/dist/bridge/bridge.js +0 -117
  484. package/dist/bridge/bridge.js.map +0 -1
  485. package/dist/bridge/index.d.ts +0 -3
  486. package/dist/bridge/index.d.ts.map +0 -1
  487. package/dist/bridge/index.js +0 -8
  488. package/dist/bridge/index.js.map +0 -1
  489. package/dist/core/call-disposables.d.ts.map +0 -1
  490. package/dist/core/call-disposables.js.map +0 -1
  491. package/dist/core/container.d.ts.map +0 -1
  492. package/dist/core/container.js.map +0 -1
  493. package/dist/core/create-agent-script.d.ts.map +0 -1
  494. package/dist/core/create-agent-script.js.map +0 -1
  495. package/dist/core/decorators.d.ts.map +0 -1
  496. package/dist/core/decorators.js.map +0 -1
  497. package/dist/core/define-module.d.ts.map +0 -1
  498. package/dist/core/define-module.js.map +0 -1
  499. package/dist/core/dump-policy.d.ts.map +0 -1
  500. package/dist/core/dump-policy.js.map +0 -1
  501. package/dist/core/hooks.d.ts.map +0 -1
  502. package/dist/core/hooks.js.map +0 -1
  503. package/dist/core/index.d.ts +0 -16
  504. package/dist/core/index.d.ts.map +0 -1
  505. package/dist/core/index.js +0 -36
  506. package/dist/core/index.js.map +0 -1
  507. package/dist/core/media-bootstrap.d.ts.map +0 -1
  508. package/dist/core/media-bootstrap.js.map +0 -1
  509. package/dist/core/provide-media.d.ts.map +0 -1
  510. package/dist/core/provide-media.js.map +0 -1
  511. package/dist/core/tokens.d.ts.map +0 -1
  512. package/dist/core/tokens.js.map +0 -1
  513. package/dist/core/types.d.ts.map +0 -1
  514. package/dist/core/types.js.map +0 -1
  515. package/dist/core/version.d.ts.map +0 -1
  516. package/dist/core/version.js.map +0 -1
  517. package/dist/dialog/basic-ports.d.ts +0 -54
  518. package/dist/dialog/basic-ports.d.ts.map +0 -1
  519. package/dist/dialog/basic-ports.js +0 -206
  520. package/dist/dialog/basic-ports.js.map +0 -1
  521. package/dist/dialog/define-dialog.d.ts +0 -21
  522. package/dist/dialog/define-dialog.d.ts.map +0 -1
  523. package/dist/dialog/define-dialog.js +0 -21
  524. package/dist/dialog/define-dialog.js.map +0 -1
  525. package/dist/dialog/dialog-definition.d.ts.map +0 -1
  526. package/dist/dialog/dialog-definition.js.map +0 -1
  527. package/dist/dialog/dialog-ports.d.ts.map +0 -1
  528. package/dist/dialog/dialog-ports.js.map +0 -1
  529. package/dist/dialog/index.d.ts +0 -7
  530. package/dist/dialog/index.d.ts.map +0 -1
  531. package/dist/dialog/index.js +0 -23
  532. package/dist/dialog/index.js.map +0 -1
  533. package/dist/dialog/online-call-host.d.ts.map +0 -1
  534. package/dist/dialog/online-call-host.js.map +0 -1
  535. package/dist/dialog/online-call-module.d.ts.map +0 -1
  536. package/dist/dialog/online-call-module.js.map +0 -1
  537. package/dist/dialog-events/active-media.d.ts.map +0 -1
  538. package/dist/dialog-events/active-media.js.map +0 -1
  539. package/dist/dialog-events/event-source.d.ts.map +0 -1
  540. package/dist/dialog-events/event-source.js.map +0 -1
  541. package/dist/dialog-events/index.d.ts +0 -8
  542. package/dist/dialog-events/index.d.ts.map +0 -1
  543. package/dist/dialog-events/index.js +0 -24
  544. package/dist/dialog-events/index.js.map +0 -1
  545. package/dist/dialog-events/max-listen-source.d.ts +0 -27
  546. package/dist/dialog-events/max-listen-source.d.ts.map +0 -1
  547. package/dist/dialog-events/max-listen-source.js +0 -98
  548. package/dist/dialog-events/max-listen-source.js.map +0 -1
  549. package/dist/dialog-events/overtalk-source.d.ts +0 -61
  550. package/dist/dialog-events/overtalk-source.d.ts.map +0 -1
  551. package/dist/dialog-events/overtalk-source.js +0 -232
  552. package/dist/dialog-events/overtalk-source.js.map +0 -1
  553. package/dist/dialog-events/silence-source.d.ts +0 -32
  554. package/dist/dialog-events/silence-source.d.ts.map +0 -1
  555. package/dist/dialog-events/silence-source.js +0 -128
  556. package/dist/dialog-events/silence-source.js.map +0 -1
  557. package/dist/dialog-events/virtual-finals-module.d.ts.map +0 -1
  558. package/dist/dialog-events/virtual-finals-module.js.map +0 -1
  559. package/dist/dialog-events/virtual-finals.d.ts.map +0 -1
  560. package/dist/dialog-events/virtual-finals.js.map +0 -1
  561. package/dist/dtmf/dtmf.d.ts +0 -27
  562. package/dist/dtmf/dtmf.d.ts.map +0 -1
  563. package/dist/dtmf/dtmf.js +0 -87
  564. package/dist/dtmf/dtmf.js.map +0 -1
  565. package/dist/dtmf/index.d.ts +0 -3
  566. package/dist/dtmf/index.d.ts.map +0 -1
  567. package/dist/dtmf/index.js +0 -8
  568. package/dist/dtmf/index.js.map +0 -1
  569. package/dist/index.d.ts.map +0 -1
  570. package/dist/index.js.map +0 -1
  571. package/dist/lifecycle/index.d.ts +0 -3
  572. package/dist/lifecycle/index.d.ts.map +0 -1
  573. package/dist/lifecycle/index.js +0 -9
  574. package/dist/lifecycle/index.js.map +0 -1
  575. package/dist/lifecycle/lifecycle.d.ts.map +0 -1
  576. package/dist/lifecycle/lifecycle.js.map +0 -1
  577. package/dist/llm/index.d.ts +0 -7
  578. package/dist/llm/index.d.ts.map +0 -1
  579. package/dist/llm/index.js +0 -18
  580. package/dist/llm/index.js.map +0 -1
  581. package/dist/llm/llm-markers-module.d.ts +0 -15
  582. package/dist/llm/llm-markers-module.d.ts.map +0 -1
  583. package/dist/llm/llm-markers-module.js +0 -48
  584. package/dist/llm/llm-markers-module.js.map +0 -1
  585. package/dist/llm/llm-markers.d.ts +0 -36
  586. package/dist/llm/llm-markers.d.ts.map +0 -1
  587. package/dist/llm/llm-markers.js +0 -210
  588. package/dist/llm/llm-markers.js.map +0 -1
  589. package/dist/llm/llm-module.d.ts +0 -20
  590. package/dist/llm/llm-module.d.ts.map +0 -1
  591. package/dist/llm/llm-module.js.map +0 -1
  592. package/dist/match/index.d.ts +0 -3
  593. package/dist/match/index.d.ts.map +0 -1
  594. package/dist/match/index.js +0 -10
  595. package/dist/match/index.js.map +0 -1
  596. package/dist/match/match.d.ts +0 -33
  597. package/dist/match/match.d.ts.map +0 -1
  598. package/dist/match/match.js +0 -85
  599. package/dist/match/match.js.map +0 -1
  600. package/dist/outbound/index.d.ts +0 -3
  601. package/dist/outbound/index.d.ts.map +0 -1
  602. package/dist/outbound/index.js +0 -8
  603. package/dist/outbound/index.js.map +0 -1
  604. package/dist/outbound/outbound.d.ts +0 -40
  605. package/dist/outbound/outbound.d.ts.map +0 -1
  606. package/dist/outbound/outbound.js +0 -84
  607. package/dist/outbound/outbound.js.map +0 -1
  608. package/dist/result/index.d.ts +0 -2
  609. package/dist/result/index.d.ts.map +0 -1
  610. package/dist/result/index.js.map +0 -1
  611. package/dist/result/result-store.d.ts.map +0 -1
  612. package/dist/result/result-store.js.map +0 -1
  613. package/dist/testing/fake-channel.d.ts.map +0 -1
  614. package/dist/testing/fake-channel.js.map +0 -1
  615. package/dist/testing/index.d.ts.map +0 -1
  616. package/dist/testing/index.js.map +0 -1
  617. package/docs/agents/README.md +0 -41
  618. package/docs/agents/barge-in.md +0 -67
  619. package/docs/agents/dialog.md +0 -98
  620. package/docs/agents/getting-started.md +0 -142
  621. package/docs/agents/imports.md +0 -40
  622. package/docs/agents/media-and-ports.md +0 -82
  623. package/docs/agents/modules.md +0 -83
  624. package/docs/agents/phases.md +0 -83
  625. package/docs/framework/extending.md +0 -59
  626. package/docs/framework/idn-parity.md +0 -18
  627. package/examples/minimal-agent/README.md +0 -7
  628. package/examples/minimal-agent/agent.ts +0 -94
@@ -0,0 +1,509 @@
1
+ "use strict";
2
+ /**
3
+ * Question-answering dialog engine: the agent never generates text. Each caller utterance is
4
+ * answered with pre-built phrases chosen in up to three steps, cheapest first:
5
+ *
6
+ * 1. **Fast path** — a whole-utterance pattern for a social turn («hello», «thanks»,
7
+ * «operator», «repeat»). No network.
8
+ * 2. **Exact match** — the utterance equals (after normalization) a phrasing that only one
9
+ * answer card owns. No network.
10
+ * 3. **Decision** — the lexical retriever ({@link QnaRetriever}) shortlists the cards the
11
+ * utterance may be about, the conversation context pins a few more (the cards the bot just
12
+ * offered to choose between, the cards of the topic it just named, the neighbours of the
13
+ * card it just answered), and ONE `choice` request to the decision model picks among them
14
+ * and the dialog controls.
15
+ *
16
+ * The pick becomes a {@link TurnPlan}: the phrase ids to play, whether the call ends or goes
17
+ * to an operator, and the context the next turn pins. A confident card plays its answer; two
18
+ * close cards make the bot ask which one is meant; a broad topic plays its menu; anything
19
+ * unclear walks a ladder that ends with an operator (or a polite goodbye).
20
+ *
21
+ * Framework-free and I/O-free — `KnowledgeService` makes the request; nothing here changes
22
+ * until {@link QnaFlow.commit}, so a turn superseded while its decision was in flight leaves
23
+ * the flow exactly as it was.
24
+ */
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.QnaFlow = exports.QnaIndex = exports.QNA_OUTCOME_KEYS = exports.TOPIC_PHRASE_PREFIX = exports.CARD_PHRASE_PREFIX = exports.OTHER_OPTION = void 0;
27
+ const dense_1 = require("./dense");
28
+ const retriever_1 = require("./retriever");
29
+ exports.OTHER_OPTION = 'other';
30
+ const QUESTION_KEY = 'reply';
31
+ exports.CARD_PHRASE_PREFIX = 'card:';
32
+ exports.TOPIC_PHRASE_PREFIX = 'topic:';
33
+ /** Decision fields every call output carries (`KnowledgeService.outcomeEntities`). */
34
+ exports.QNA_OUTCOME_KEYS = [
35
+ 'decision_model',
36
+ 'decision_turns',
37
+ 'decision_errors',
38
+ 'decision_avg_ms',
39
+ 'qna_embedding_errors',
40
+ 'qna_turns',
41
+ 'qna_answers',
42
+ 'qna_cards',
43
+ 'qna_nodes',
44
+ 'qna_unclear_turns',
45
+ 'qna_not_found_turns',
46
+ 'qna_clarify_turns',
47
+ 'qna_fast_turns',
48
+ 'qna_exact_turns',
49
+ 'qna_operator_requested',
50
+ 'qna_path',
51
+ 'qna_last_unanswered',
52
+ ];
53
+ // ── Engine ──────────────────────────────────────────────────────────────────────
54
+ /** Per-process index of a catalog: the retrievers and the lookups the flow needs. Built
55
+ * once — the knowledge base is static — and shared by every call. Entry index: a card
56
+ * (`< cards.length`) or a topic (`cards.length + topic index`). */
57
+ class QnaIndex {
58
+ constructor(catalog, text) {
59
+ this.catalog = catalog;
60
+ this.text = text;
61
+ this.cardByKey = new Map();
62
+ this.topicByKey = new Map();
63
+ this.cardsByNode = new Map();
64
+ const started = Date.now();
65
+ // A criterion's «quoted» examples are caller phrasings too; the answer brings the
66
+ // vocabulary a caller may use (the words of the answer itself) at reduced weight.
67
+ const examples = (criterion) => [...criterion.matchAll(/«([^»]+)»/g)].map((m) => m[1]);
68
+ const entries = [
69
+ ...catalog.cards.map((c) => ({
70
+ phrases: [c.title, ...c.questions, ...examples(c.criterion)],
71
+ extra: [c.answer],
72
+ })),
73
+ ...catalog.topics.map((t) => ({ phrases: [t.title, ...t.phrases] })),
74
+ ];
75
+ this.retriever = retriever_1.QnaRetriever.build(entries, { ...retriever_1.DEFAULT_RETRIEVER_OPTIONS, ...catalog.retriever }, catalog.synonyms ?? [], text);
76
+ catalog.cards.forEach((c, i) => {
77
+ this.cardByKey.set(c.key, i);
78
+ const list = this.cardsByNode.get(c.node);
79
+ if (list == null)
80
+ this.cardsByNode.set(c.node, [i]);
81
+ else
82
+ list.push(i);
83
+ });
84
+ catalog.topics.forEach((t, i) => this.topicByKey.set(t.key, i));
85
+ const vectors = catalog.vectors;
86
+ if (vectors == null) {
87
+ this.dense = null;
88
+ this.denseOff = 'no vectors';
89
+ }
90
+ else if (vectors.kbVersion !== catalog.kbVersion) {
91
+ this.dense = null;
92
+ this.denseOff = `vectors of kb ${vectors.kbVersion}, catalog is ${catalog.kbVersion}`;
93
+ }
94
+ else {
95
+ const cardIndex = new Map(catalog.cards.map((c, i) => [c.id, i]));
96
+ const topicIndex = new Map(catalog.topics.map((t, i) => [
97
+ `topic:${t.id}`,
98
+ catalog.cards.length + i,
99
+ ]));
100
+ this.dense = new dense_1.DenseIndex(vectors, (owner) => cardIndex.get(owner) ?? topicIndex.get(owner));
101
+ this.denseOff = null;
102
+ }
103
+ this.buildMs = Date.now() - started;
104
+ }
105
+ /**
106
+ * Entries for `query`, best first: the lexical ranking fused with the dense one when a
107
+ * query vector is given. `accept` restricts both to some entries (a topic's cards).
108
+ */
109
+ rank(query, queryVector, accept) {
110
+ const depth = this.catalog.fusionDepth ?? 60;
111
+ const hits = this.retriever.search(query, depth, accept);
112
+ const lexical = new Map(hits.map((h) => [h.index, h.score]));
113
+ if (queryVector == null || this.dense == null) {
114
+ return { order: hits.map((h) => h.index), lexical, dense: new Map() };
115
+ }
116
+ const denseHits = this.dense
117
+ .search(queryVector, accept ? depth * 4 : depth)
118
+ .filter((h) => accept == null || accept(h.index))
119
+ .slice(0, depth);
120
+ const dense = new Map(denseHits.map((h) => [h.index, h.score]));
121
+ // The dense ranking leads; the lexical one breaks its ties and adds the exact terms
122
+ // (product names, numbers) embeddings blur.
123
+ const order = (0, dense_1.fuseRanks)([denseHits.map((h) => h.index), hits.map((h) => h.index)], [1, this.catalog.lexicalFusionWeight ?? 0.15]);
124
+ return { order, lexical, dense };
125
+ }
126
+ /** Card indices under tree node `nodeId` (the node itself or any descendant). */
127
+ cardsUnder(nodeId) {
128
+ const prefix = `${nodeId}.`;
129
+ const out = [];
130
+ for (const [node, list] of this.cardsByNode) {
131
+ if (node === nodeId || node.startsWith(prefix))
132
+ out.push(...list);
133
+ }
134
+ return out;
135
+ }
136
+ }
137
+ exports.QnaIndex = QnaIndex;
138
+ class QnaFlow {
139
+ constructor(index, textOf) {
140
+ this.index = index;
141
+ this.textOf = textOf;
142
+ this.unclearCount = 0;
143
+ this.notFoundCount = 0;
144
+ this.lastAgentPhraseIds = [];
145
+ this.focus = null;
146
+ this.dynamic = new Map();
147
+ this.turn = 0;
148
+ }
149
+ get catalog() {
150
+ return this.index.catalog;
151
+ }
152
+ /** Text of a per-turn phrase registered by a committed plan. */
153
+ dynamicText(id) {
154
+ return this.dynamic.get(id);
155
+ }
156
+ get currentFocus() {
157
+ return this.focus;
158
+ }
159
+ // ── Steps 1–2: no network ─────────────────────────────────────────────────────
160
+ /** The greeting, for the runtime's connection signal. */
161
+ planGreeting() {
162
+ return this.plan('greeting', this.catalog.greeting, { via: 'signal' });
163
+ }
164
+ /** A control matched by a whole-utterance pattern, or `null`. */
165
+ fastPlan(userText) {
166
+ const text = this.index.text.normalize(userText);
167
+ if (!text)
168
+ return null;
169
+ const choosing = this.focus?.kind === 'clarify' || this.focus?.kind === 'topic';
170
+ for (const [key, control] of Object.entries(this.catalog.controls)) {
171
+ if (choosing && control.noFastAfterChoice)
172
+ continue;
173
+ if (control.fastPatterns?.some((re) => re.test(text))) {
174
+ return this.controlPlan(key, control, 1, 'fast');
175
+ }
176
+ }
177
+ return null;
178
+ }
179
+ /** The answer whose phrasing the utterance repeats exactly, or `null`. */
180
+ exactPlan(userText) {
181
+ if (!this.catalog.exactMatch)
182
+ return null;
183
+ const entry = this.index.retriever.exact(userText);
184
+ if (entry == null || entry >= this.catalog.cards.length)
185
+ return null;
186
+ return this.answerPlan(entry, 1, 'exact', userText);
187
+ }
188
+ // ── Step 3: decision request ──────────────────────────────────────────────────
189
+ /**
190
+ * Candidates + state + question for one caller utterance. `queryVector` (the utterance's
191
+ * embedding, when dense retrieval is on) fuses the semantic ranking into the lexical one.
192
+ */
193
+ buildRequest(userText, queryVector = null) {
194
+ const { catalog, index } = this;
195
+ const nCards = catalog.cards.length;
196
+ const candidates = [];
197
+ const seen = new Set();
198
+ let topics = 0;
199
+ const ranked = index.rank(userText, queryVector);
200
+ const add = (entry, pinned) => {
201
+ if (candidates.length >= catalog.candidates)
202
+ return;
203
+ const isTopic = entry >= nCards;
204
+ const i = isTopic ? entry - nCards : entry;
205
+ const key = isTopic ? catalog.topics[i].key : catalog.cards[i].key;
206
+ if (seen.has(key))
207
+ return;
208
+ if (isTopic && topics >= catalog.maxTopicCandidates)
209
+ return;
210
+ seen.add(key);
211
+ if (isTopic)
212
+ topics += 1;
213
+ candidates.push({
214
+ key,
215
+ kind: isTopic ? 'topic' : 'card',
216
+ index: i,
217
+ score: ranked.lexical.get(entry) ?? null,
218
+ dense: ranked.dense.get(entry) ?? null,
219
+ pinned,
220
+ });
221
+ };
222
+ const focus = this.focus;
223
+ if (focus?.kind === 'clarify') {
224
+ for (const c of focus.cards)
225
+ add(c, true);
226
+ }
227
+ else if (focus?.kind === 'topic') {
228
+ const under = new Set(index.cardsUnder(catalog.topics[focus.topic].id));
229
+ const inTopic = index.rank(userText, queryVector, (e) => under.has(e));
230
+ for (const e of inTopic.order.slice(0, Math.ceil(catalog.candidates / 2))) {
231
+ add(e, true);
232
+ }
233
+ }
234
+ else if (focus?.kind === 'answered') {
235
+ const card = catalog.cards[focus.card];
236
+ const position = new Map(ranked.order.map((e, r) => [e, r]));
237
+ const siblings = (index.cardsByNode.get(card.node) ?? []).filter((c) => c !== focus.card);
238
+ // Best-ranked neighbours first; the answered card itself stays a candidate through
239
+ // the ranking («repeat the one about …» is a repeat of it, not a new question).
240
+ siblings.sort((a, b) => (position.get(a) ?? Infinity) - (position.get(b) ?? Infinity));
241
+ for (const c of siblings.slice(0, catalog.followUpCandidates))
242
+ add(c, true);
243
+ }
244
+ for (const e of ranked.order)
245
+ add(e, false);
246
+ const criteria = {};
247
+ for (const c of candidates) {
248
+ criteria[c.key] =
249
+ c.kind === 'card'
250
+ ? catalog.cards[c.index].criterion
251
+ : catalog.topics[c.index].criterion;
252
+ }
253
+ for (const [key, control] of Object.entries(catalog.controls)) {
254
+ if (!(key in criteria))
255
+ criteria[key] = control.criterion;
256
+ }
257
+ criteria[exports.OTHER_OPTION] = catalog.otherCriterion;
258
+ const lastSaid = this.lastAgentText();
259
+ const state = {
260
+ call: catalog.callContext,
261
+ bot_last_said: lastSaid.length > catalog.lastSaidMaxChars
262
+ ? `${lastSaid.slice(0, catalog.lastSaidMaxChars)}…`
263
+ : lastSaid,
264
+ customer_said: userText,
265
+ };
266
+ return {
267
+ userText,
268
+ state,
269
+ questions: {
270
+ [QUESTION_KEY]: {
271
+ type: 'choice',
272
+ instructions: catalog.instructions,
273
+ criteria,
274
+ },
275
+ },
276
+ candidates,
277
+ };
278
+ }
279
+ /** The answer to the question {@link buildRequest} built, or `undefined` when absent. */
280
+ pickAnswer(answers) {
281
+ return answers[QUESTION_KEY];
282
+ }
283
+ /** Turn a decision (or the lack of one) into a plan. `null` = undecided → unclear ladder. */
284
+ planFromAnswer(answer, request) {
285
+ if (answer == null)
286
+ return this.unclearPlan(null, 'none');
287
+ const probs = answer.probabilities ?? {};
288
+ const choice = answer.choice;
289
+ const p = probs[choice] ?? 0;
290
+ const control = this.catalog.controls[choice];
291
+ if (control != null) {
292
+ if (p < this.catalog.controlMinProbability) {
293
+ return this.unclearPlan(choice, 'decision', p);
294
+ }
295
+ return this.controlPlan(choice, control, p, 'decision');
296
+ }
297
+ const picked = request.candidates.find((c) => c.key === choice);
298
+ if (picked != null && p >= this.catalog.answerMinProbability) {
299
+ return picked.kind === 'card'
300
+ ? this.answerPlan(picked.index, p, 'decision', request.userText)
301
+ : this.topicPlan(picked.index, p);
302
+ }
303
+ // Not confident in one answer: two close cards → ask which one is meant.
304
+ const rankedCards = request.candidates
305
+ .filter((c) => c.kind === 'card')
306
+ .map((c) => ({ c, p: probs[c.key] ?? 0 }))
307
+ .sort((a, b) => b.p - a.p);
308
+ const [first, second] = rankedCards;
309
+ if (first != null &&
310
+ second != null &&
311
+ first.p + second.p >= this.catalog.clarifyMinProbability &&
312
+ second.p >= this.catalog.clarifyMinSecond) {
313
+ return this.clarifyPlan(first.c.index, second.c.index, choice, p);
314
+ }
315
+ return this.unclearPlan(choice, 'decision', p);
316
+ }
317
+ /** The unclear ladder without a decision — e.g. speech the recognizer did not catch. */
318
+ planUnclear(label) {
319
+ return this.unclearPlan(label, 'none');
320
+ }
321
+ /** Post-agent-turn silence #`count` (1-based). */
322
+ planSilence(count) {
323
+ const step = this.catalog.silence[count - 1];
324
+ if (step == null) {
325
+ return this.terminalPlan(this.catalog.silenceExhausted, 'silence');
326
+ }
327
+ return this.plan('silence', step, {
328
+ // Silence is not a misunderstanding — the ladders carry on where they were.
329
+ unclearCount: this.unclearCount,
330
+ notFoundCount: this.notFoundCount,
331
+ });
332
+ }
333
+ terminalPlan(terminal, kind = 'terminal') {
334
+ return this.plan(kind, terminal.say, { end: terminal.end });
335
+ }
336
+ /** Apply a plan that is about to play. */
337
+ commit(plan) {
338
+ this.turn += 1;
339
+ this.unclearCount = plan.unclearCount;
340
+ this.notFoundCount = plan.notFoundCount;
341
+ if (plan.focus !== undefined)
342
+ this.focus = plan.focus;
343
+ for (const [id, text] of Object.entries(plan.dynamicPhrases ?? {})) {
344
+ this.dynamic.set(id, text);
345
+ }
346
+ if (plan.phraseIds.length > 0)
347
+ this.lastAgentPhraseIds = plan.phraseIds;
348
+ }
349
+ // ── Plan builders ─────────────────────────────────────────────────────────────
350
+ answerPlan(card, p, via, userText) {
351
+ const { cards, anythingElse, transfer } = this.catalog;
352
+ const c = cards[card];
353
+ const answer = `${exports.CARD_PHRASE_PREFIX}${c.id}`;
354
+ const base = {
355
+ choice: c.key,
356
+ card: c,
357
+ probability: p,
358
+ via,
359
+ focus: { kind: 'answered', card },
360
+ };
361
+ if (!this.asksForOperator(userText)) {
362
+ return this.plan('answer', [answer, anythingElse], base);
363
+ }
364
+ // A question AND «put me through to an operator»: answer it, then hand over — or say plainly
365
+ // that the bot cannot, instead of ignoring the request.
366
+ return transfer.enabled
367
+ ? this.plan('answer', [answer, ...transfer.say], {
368
+ ...base,
369
+ transfer: true,
370
+ operatorRequested: true,
371
+ })
372
+ : this.plan('answer', [
373
+ answer,
374
+ ...(transfer.afterAnswer ?? transfer.unavailable),
375
+ anythingElse,
376
+ ], { ...base, operatorRequested: true });
377
+ }
378
+ asksForOperator(userText) {
379
+ const patterns = this.catalog.operatorRequestPatterns;
380
+ if (patterns == null || patterns.length === 0)
381
+ return false;
382
+ const text = this.index.text.normalize(userText);
383
+ return patterns.some((re) => re.test(text));
384
+ }
385
+ topicPlan(topic, p) {
386
+ const t = this.catalog.topics[topic];
387
+ return this.plan('topic', [`${exports.TOPIC_PHRASE_PREFIX}${t.id}`], {
388
+ choice: t.key,
389
+ probability: p,
390
+ via: 'decision',
391
+ focus: { kind: 'topic', topic },
392
+ });
393
+ }
394
+ clarifyPlan(a, b, choice, p) {
395
+ const { cards, clarifyTemplate } = this.catalog;
396
+ const id = `clarify:${this.turn + 1}`;
397
+ const text = clarifyTemplate
398
+ .replace('{a}', cards[a].title)
399
+ .replace('{b}', cards[b].title);
400
+ return this.plan('clarify', [id], {
401
+ choice,
402
+ probability: p,
403
+ via: 'decision',
404
+ focus: { kind: 'clarify', cards: [a, b] },
405
+ dynamicPhrases: { [id]: text },
406
+ });
407
+ }
408
+ controlPlan(key, control, p, via) {
409
+ const base = { choice: key, probability: p, via };
410
+ switch (control.kind) {
411
+ case 'repeat': {
412
+ const replay = this.lastAgentPhraseIds.length > 0
413
+ ? this.lastAgentPhraseIds
414
+ : this.catalog.greeting;
415
+ return this.plan('repeat', replay, {
416
+ ...base,
417
+ unclearCount: this.unclearCount,
418
+ notFoundCount: this.notFoundCount,
419
+ });
420
+ }
421
+ case 'goodbye':
422
+ return this.plan('control', control.say ?? [], {
423
+ ...base,
424
+ end: control.end ?? this.catalog.silenceExhausted.end,
425
+ });
426
+ case 'operator':
427
+ return this.transferPlan(base);
428
+ case 'not_found':
429
+ return this.notFoundPlan(base);
430
+ case 'listen':
431
+ case 'say':
432
+ return this.plan('control', control.say ?? [], {
433
+ ...base,
434
+ // A side remark does not resolve an open clarification or topic menu — unless
435
+ // the caller rejected what was offered.
436
+ ...(control.resetFocus ? { focus: null } : {}),
437
+ unclearCount: this.unclearCount,
438
+ notFoundCount: this.notFoundCount,
439
+ });
440
+ }
441
+ }
442
+ transferPlan(base) {
443
+ const { transfer } = this.catalog;
444
+ if (transfer.enabled) {
445
+ return this.plan('transfer', transfer.say, { ...base, transfer: true });
446
+ }
447
+ return this.plan('transfer', transfer.unavailable, {
448
+ ...base,
449
+ focus: null,
450
+ });
451
+ }
452
+ notFoundPlan(base) {
453
+ const n = this.notFoundCount + 1;
454
+ const step = this.catalog.notFound[n - 1];
455
+ if (step == null) {
456
+ return this.catalog.transfer.enabled
457
+ ? this.transferPlan({ ...base, notFoundCount: n })
458
+ : this.plan('not_found', this.catalog.notFoundExhausted.say, {
459
+ ...base,
460
+ end: this.catalog.notFoundExhausted.end,
461
+ notFoundCount: n,
462
+ });
463
+ }
464
+ return this.plan('not_found', step, {
465
+ ...base,
466
+ focus: null,
467
+ notFoundCount: n,
468
+ });
469
+ }
470
+ unclearPlan(choice, via, p = null) {
471
+ const n = this.unclearCount + 1;
472
+ const step = this.catalog.unclear[n - 1];
473
+ const base = {
474
+ choice,
475
+ probability: p,
476
+ via,
477
+ notFoundCount: this.notFoundCount,
478
+ };
479
+ if (step == null) {
480
+ return this.catalog.transfer.enabled
481
+ ? this.transferPlan({ ...base, unclearCount: n })
482
+ : this.plan('unclear', this.catalog.unclearExhausted.say, {
483
+ ...base,
484
+ end: this.catalog.unclearExhausted.end,
485
+ unclearCount: n,
486
+ });
487
+ }
488
+ return this.plan('unclear', step, { ...base, unclearCount: n });
489
+ }
490
+ plan(kind, phraseIds, extra = {}) {
491
+ return {
492
+ kind,
493
+ phraseIds,
494
+ end: null,
495
+ transfer: false,
496
+ choice: null,
497
+ card: null,
498
+ probability: null,
499
+ via: 'none',
500
+ unclearCount: 0,
501
+ notFoundCount: 0,
502
+ ...extra,
503
+ };
504
+ }
505
+ lastAgentText() {
506
+ return this.lastAgentPhraseIds.map((id) => this.textOf(id)).join(' ');
507
+ }
508
+ }
509
+ exports.QnaFlow = QnaFlow;
@@ -0,0 +1,75 @@
1
+ /**
2
+ * In-memory lexical retriever over the knowledge base — the candidate generator in front of
3
+ * the decision model. It only has to put the right answer among the top few (recall@K); the
4
+ * decision model picks among them.
5
+ *
6
+ * Every entry (answer card or topic) owns several phrases: its caller phrasings (full
7
+ * weight) and extra text such as the answer itself (reduced weight — it brings the
8
+ * vocabulary a caller may use without being a caller phrasing). Each phrase is a BM25
9
+ * document over two fields — word stems and character trigrams of the stems (robust to ASR
10
+ * misspellings and morphology). The query is expanded with domain synonyms («express» →
11
+ * the product name, a country → «international») at reduced weight. An entry scores as its best phrase
12
+ * plus a small bonus for its second best, so an entry that several phrasings support
13
+ * outranks one lucky phrasing.
14
+ *
15
+ * Built once per process; a query is a few postings scans — well under a millisecond at a
16
+ * few thousand phrases. I/O-free; runs in the script sandbox.
17
+ */
18
+ import { type RetrievalText } from './text';
19
+ /** One searchable unit: an answer card or a topic. */
20
+ export interface RetrievalEntry {
21
+ /** Caller phrasings (and the title) — full weight, eligible for exact match. */
22
+ readonly phrases: readonly string[];
23
+ /** Other text that describes the entry (answer, criterion) — `extraPhraseWeight`. */
24
+ readonly extra?: readonly string[];
25
+ }
26
+ export interface RetrievalHit {
27
+ /** Index of the entry in the array passed to {@link QnaRetriever.build}. */
28
+ readonly index: number;
29
+ readonly score: number;
30
+ /** The entry's best-matching phrase (for logs and offline evaluation). */
31
+ readonly phrase: string;
32
+ }
33
+ export interface RetrieverOptions {
34
+ /** BM25 term-frequency saturation. */
35
+ readonly k1: number;
36
+ /** BM25 length normalization. */
37
+ readonly b: number;
38
+ /** Weight of the trigram field against the stem field. */
39
+ readonly trigramWeight: number;
40
+ /** Share of an entry's second-best phrase score added to its best. */
41
+ readonly secondPhraseBonus: number;
42
+ /** Weight of an entry's `extra` text against its phrasings. */
43
+ readonly extraPhraseWeight: number;
44
+ /** Weight of a synonym-expanded query term against a word the caller said. */
45
+ readonly synonymWeight: number;
46
+ }
47
+ export declare const DEFAULT_RETRIEVER_OPTIONS: RetrieverOptions;
48
+ export declare class QnaRetriever {
49
+ readonly options: RetrieverOptions;
50
+ private readonly text;
51
+ private readonly stemField;
52
+ private readonly trigramField;
53
+ /** Phrase → owning entry. */
54
+ private readonly owner;
55
+ private readonly phraseText;
56
+ private readonly docWeight;
57
+ /** Stem key of a caller phrasing → entries that own such a phrasing. */
58
+ private readonly exactIndex;
59
+ /** Stem → stems of its synonyms. */
60
+ private readonly synonyms;
61
+ private readonly docScores;
62
+ private constructor();
63
+ static build(entries: readonly RetrievalEntry[], options: RetrieverOptions, synonymGroups: readonly (readonly string[])[], text: RetrievalText): QnaRetriever;
64
+ get phraseCount(): number;
65
+ /**
66
+ * Top `limit` entries for `query`, best first. `accept` (optional) restricts the result to
67
+ * the entries it returns `true` for — e.g. the cards under one topic.
68
+ */
69
+ search(query: string, limit: number, accept?: (index: number) => boolean): RetrievalHit[];
70
+ /**
71
+ * The single entry that owns a caller phrasing identical (after normalization and
72
+ * stemming) to `query`, or `null` when none does or several do.
73
+ */
74
+ exact(query: string): number | null;
75
+ }