@voctiv/agent-runtime 0.2.1 → 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.
- package/README.md +45 -20
- package/dist/barge-in/barge-in-module.d.ts +0 -1
- package/dist/barge-in/barge-in-module.js +0 -1
- package/dist/barge-in/barge-in-service.d.ts +1 -1
- package/dist/barge-in/barge-in-service.js +33 -2
- package/dist/barge-in/barge-in-strategy.d.ts +11 -9
- package/dist/barge-in/barge-in-strategy.js +39 -23
- package/dist/barge-in/echo-cancellation.d.ts +0 -1
- package/dist/barge-in/echo-cancellation.js +0 -1
- package/dist/barge-in/echo.d.ts +0 -1
- package/dist/barge-in/echo.js +0 -1
- package/dist/barge-in/lexicon.d.ts +0 -1
- package/dist/barge-in/lexicon.js +8 -2
- package/dist/barge-in/speech-ledger.d.ts +0 -2
- package/dist/barge-in/speech-ledger.js +3 -4
- package/dist/barge-in/word-classifier.d.ts +0 -1
- package/dist/barge-in/word-classifier.js +0 -1
- package/dist/call/asr-usage.d.ts +53 -0
- package/dist/call/asr-usage.js +42 -0
- package/dist/call/async.d.ts +2 -0
- package/dist/call/async.js +14 -0
- package/dist/call/env.d.ts +88 -0
- package/dist/call/env.js +294 -0
- package/dist/call/extraction/core.d.ts +99 -0
- package/dist/call/extraction/core.js +259 -0
- package/dist/call/extraction/defaults.d.ts +10 -0
- package/dist/call/extraction/defaults.js +25 -0
- package/dist/call/extraction/index.d.ts +7 -0
- package/dist/call/extraction/index.js +24 -0
- package/dist/call/extraction/orchestrator.d.ts +144 -0
- package/dist/call/extraction/orchestrator.js +309 -0
- package/dist/call/failed-leg.d.ts +43 -0
- package/dist/call/failed-leg.js +77 -0
- package/dist/call/http.d.ts +94 -0
- package/dist/call/http.js +145 -0
- package/dist/call/ledger.d.ts +18 -0
- package/dist/call/ledger.js +80 -0
- package/dist/call/llm-tap.d.ts +7 -0
- package/dist/call/llm-tap.js +41 -0
- package/dist/call/logging.d.ts +16 -0
- package/dist/call/logging.js +92 -0
- package/dist/call/outbound.d.ts +144 -0
- package/dist/call/outbound.js +404 -0
- package/dist/call/output.d.ts +108 -0
- package/dist/call/output.js +77 -0
- package/dist/call/plan.d.ts +119 -0
- package/dist/call/plan.js +2 -0
- package/dist/call/recall.d.ts +115 -0
- package/dist/call/recall.js +312 -0
- package/dist/call/records.d.ts +9 -0
- package/dist/call/records.js +71 -0
- package/dist/call/report.d.ts +65 -0
- package/dist/call/report.js +263 -0
- package/dist/call/sanitize.d.ts +16 -0
- package/dist/call/sanitize.js +72 -0
- package/dist/call/sip.d.ts +111 -0
- package/dist/call/sip.js +212 -0
- package/dist/call/voicemail.d.ts +159 -0
- package/dist/call/voicemail.js +246 -0
- package/dist/core/call-disposables.d.ts +2 -2
- package/dist/core/call-disposables.js +8 -3
- package/dist/core/call-ended-budget.d.ts +11 -0
- package/dist/core/call-ended-budget.js +31 -0
- package/dist/core/container.d.ts +1 -3
- package/dist/core/container.js +9 -49
- package/dist/core/create-agent-script.d.ts +6 -1
- package/dist/core/create-agent-script.js +161 -28
- package/dist/core/decorators.d.ts +0 -1
- package/dist/core/decorators.js +0 -1
- package/dist/core/define-module.d.ts +0 -1
- package/dist/core/define-module.js +0 -1
- package/dist/core/dump-policy.d.ts +0 -1
- package/dist/core/dump-policy.js +0 -1
- package/dist/core/hooks.d.ts +15 -4
- package/dist/core/hooks.js +0 -1
- package/dist/core/media-bootstrap.d.ts +19 -4
- package/dist/core/media-bootstrap.js +53 -13
- package/dist/core/open-timeout.d.ts +12 -0
- package/dist/core/open-timeout.js +48 -0
- package/dist/core/provide-media.d.ts +0 -8
- package/dist/core/provide-media.js +1 -22
- package/dist/core/runtime-events.d.ts +28 -0
- package/dist/{llm/llm-module.js → core/runtime-events.js} +19 -16
- package/dist/core/tokens.d.ts +0 -10
- package/dist/core/tokens.js +0 -1
- package/dist/core/types.d.ts +0 -1
- package/dist/core/types.js +0 -1
- package/dist/core/version.d.ts +2 -3
- package/dist/core/version.js +2 -3
- package/dist/dialog/dialog-definition.d.ts +0 -5
- package/dist/dialog/dialog-definition.js +1 -6
- package/dist/dialog/dialog-ports.d.ts +3 -1
- package/dist/dialog/dialog-ports.js +10 -2
- package/dist/dialog/online-call-host.d.ts +26 -4
- package/dist/dialog/online-call-host.js +94 -12
- package/dist/dialog/online-call-module.d.ts +5 -11
- package/dist/dialog/online-call-module.js +4 -36
- package/dist/dialog/turn-metrics.d.ts +46 -0
- package/dist/dialog/turn-metrics.js +141 -0
- package/dist/dialog-events/active-media.d.ts +50 -2
- package/dist/dialog-events/active-media.js +146 -34
- package/dist/dialog-events/event-source.d.ts +0 -1
- package/dist/dialog-events/event-source.js +0 -1
- package/dist/dialog-events/virtual-finals-module.d.ts +0 -1
- package/dist/dialog-events/virtual-finals-module.js +0 -1
- package/dist/dialog-events/virtual-finals.d.ts +2 -1
- package/dist/dialog-events/virtual-finals.js +1 -1
- package/dist/index.d.ts +1 -4
- package/dist/index.js +8 -26
- package/dist/lifecycle/lifecycle.d.ts +25 -3
- package/dist/lifecycle/lifecycle.js +92 -14
- package/dist/result/result-store.d.ts +0 -1
- package/dist/result/result-store.js +0 -1
- package/dist/templates/authoring.d.ts +222 -0
- package/dist/templates/authoring.js +2 -0
- package/dist/templates/builder/agent.d.ts +34 -0
- package/dist/templates/builder/agent.js +87 -0
- package/dist/templates/builder/blocks.d.ts +54 -0
- package/dist/templates/builder/blocks.js +92 -0
- package/dist/templates/builder/data-check.d.ts +2 -0
- package/dist/templates/builder/data-check.js +61 -0
- package/dist/templates/builder/draft.d.ts +73 -0
- package/dist/templates/builder/draft.js +142 -0
- package/dist/templates/builder/index.d.ts +8 -0
- package/dist/templates/builder/index.js +55 -0
- package/dist/templates/builder/presets.d.ts +34 -0
- package/dist/templates/builder/presets.js +45 -0
- package/dist/templates/builder/spec-of.d.ts +13 -0
- package/dist/templates/builder/spec-of.js +18 -0
- package/dist/templates/builder/steps.d.ts +25 -0
- package/dist/templates/builder/steps.js +27 -0
- package/dist/templates/business/adapter.d.ts +12 -0
- package/dist/templates/business/adapter.js +980 -0
- package/dist/templates/business/agent-integrations.d.ts +24 -0
- package/dist/templates/business/agent-integrations.js +233 -0
- package/dist/templates/business/extraction-rules-transcript.d.ts +158 -0
- package/dist/templates/business/extraction-rules-transcript.js +405 -0
- package/dist/templates/business/extraction-rules.d.ts +107 -0
- package/dist/templates/business/extraction-rules.js +267 -0
- package/dist/templates/business/index.d.ts +6 -0
- package/dist/{result → templates/business}/index.js +8 -2
- package/dist/templates/business/input.d.ts +56 -0
- package/dist/templates/business/input.js +299 -0
- package/dist/templates/business/integration-data.d.ts +87 -0
- package/dist/templates/business/integration-data.js +312 -0
- package/dist/templates/business/integrations.d.ts +76 -0
- package/dist/templates/business/integrations.js +535 -0
- package/dist/templates/business/legs-policy.d.ts +43 -0
- package/dist/templates/business/legs-policy.js +268 -0
- package/dist/templates/business/report-label.d.ts +7 -0
- package/dist/templates/business/report-label.js +29 -0
- package/dist/templates/business/report.d.ts +67 -0
- package/dist/templates/business/report.js +284 -0
- package/dist/templates/business/result-catalog.d.ts +142 -0
- package/dist/templates/business/result-catalog.js +208 -0
- package/dist/templates/business/result-rules.d.ts +105 -0
- package/dist/templates/business/result-rules.js +167 -0
- package/dist/templates/business/results.d.ts +73 -0
- package/dist/templates/business/results.js +183 -0
- package/dist/templates/business/rule-env.d.ts +18 -0
- package/dist/templates/business/rule-env.js +38 -0
- package/dist/templates/business/sources.d.ts +76 -0
- package/dist/templates/business/sources.js +239 -0
- package/dist/templates/business/test-presets.d.ts +10 -0
- package/dist/templates/business/test-presets.js +68 -0
- package/dist/templates/catalog-match/block.d.ts +32 -0
- package/dist/templates/catalog-match/block.js +95 -0
- package/dist/templates/catalog-match/catalog.d.ts +77 -0
- package/dist/templates/catalog-match/catalog.js +222 -0
- package/dist/templates/catalog-match/recognizer.d.ts +44 -0
- package/dist/templates/catalog-match/recognizer.js +206 -0
- package/dist/templates/catalog-match/reconcile.d.ts +4 -0
- package/dist/templates/catalog-match/reconcile.js +39 -0
- package/dist/templates/catalog-match/request.d.ts +57 -0
- package/dist/templates/catalog-match/request.js +139 -0
- package/dist/templates/catalog-match/types.d.ts +197 -0
- package/dist/templates/catalog-match/types.js +2 -0
- package/dist/templates/catalog-match/verify.d.ts +76 -0
- package/dist/templates/catalog-match/verify.js +220 -0
- package/dist/templates/decision-model/index.d.ts +52 -0
- package/dist/templates/decision-model/index.js +68 -0
- package/dist/templates/engine/asr-picks.d.ts +11 -0
- package/dist/templates/engine/asr-picks.js +44 -0
- package/dist/templates/engine/call.d.ts +104 -0
- package/dist/templates/engine/call.js +322 -0
- package/dist/templates/engine/config.d.ts +447 -0
- package/dist/templates/engine/config.js +493 -0
- package/dist/templates/engine/define.d.ts +1 -0
- package/dist/templates/engine/define.js +9 -0
- package/dist/templates/engine/dialog.d.ts +253 -0
- package/dist/templates/engine/dialog.js +167 -0
- package/dist/templates/engine/echo-numbers.d.ts +7 -0
- package/dist/templates/engine/echo-numbers.js +19 -0
- package/dist/templates/engine/expand.d.ts +6 -0
- package/dist/templates/engine/expand.js +125 -0
- package/dist/templates/engine/filters.d.ts +81 -0
- package/dist/templates/engine/filters.js +654 -0
- package/dist/templates/engine/hook-state.d.ts +18 -0
- package/dist/templates/engine/hook-state.js +100 -0
- package/dist/templates/engine/hooks.d.ts +51 -0
- package/dist/templates/engine/hooks.js +310 -0
- package/dist/templates/engine/index.d.ts +13 -0
- package/dist/templates/engine/index.js +30 -0
- package/dist/templates/engine/language-vars.d.ts +6 -0
- package/dist/templates/engine/language-vars.js +40 -0
- package/dist/templates/engine/language.d.ts +32 -0
- package/dist/templates/engine/language.js +155 -0
- package/dist/templates/engine/lookup.d.ts +20 -0
- package/dist/templates/engine/lookup.js +52 -0
- package/dist/templates/engine/match.d.ts +48 -0
- package/dist/templates/engine/match.js +253 -0
- package/dist/templates/engine/media.d.ts +428 -0
- package/dist/templates/engine/media.js +1240 -0
- package/dist/templates/engine/script.d.ts +11 -0
- package/dist/templates/engine/script.js +69 -0
- package/dist/templates/engine/turns/actions.d.ts +5 -0
- package/dist/templates/engine/turns/actions.js +128 -0
- package/dist/templates/engine/turns/carry.d.ts +15 -0
- package/dist/templates/engine/turns/carry.js +74 -0
- package/dist/templates/engine/turns/choices-offers.d.ts +21 -0
- package/dist/templates/engine/turns/choices-offers.js +143 -0
- package/dist/templates/engine/turns/choices.d.ts +22 -0
- package/dist/templates/engine/turns/choices.js +141 -0
- package/dist/templates/engine/turns/closing-guard.d.ts +73 -0
- package/dist/templates/engine/turns/closing-guard.js +162 -0
- package/dist/templates/engine/turns/closing.d.ts +10 -0
- package/dist/templates/engine/turns/closing.js +82 -0
- package/dist/templates/engine/turns/compile-forms.d.ts +24 -0
- package/dist/templates/engine/turns/compile-forms.js +68 -0
- package/dist/templates/engine/turns/context.d.ts +96 -0
- package/dist/templates/engine/turns/context.js +158 -0
- package/dist/templates/engine/turns/events.d.ts +409 -0
- package/dist/templates/engine/turns/events.js +5 -0
- package/dist/templates/engine/turns/hook-forms.d.ts +62 -0
- package/dist/templates/engine/turns/hook-forms.js +79 -0
- package/dist/templates/engine/turns/ladders.d.ts +42 -0
- package/dist/templates/engine/turns/ladders.js +119 -0
- package/dist/templates/engine/turns/language.d.ts +8 -0
- package/dist/templates/engine/turns/language.js +121 -0
- package/dist/templates/engine/turns/notes-dates.d.ts +9 -0
- package/dist/templates/engine/turns/notes-dates.js +42 -0
- package/dist/templates/engine/turns/notes-detect.d.ts +13 -0
- package/dist/templates/engine/turns/notes-detect.js +36 -0
- package/dist/templates/engine/turns/notes.d.ts +27 -0
- package/dist/templates/engine/turns/notes.js +176 -0
- package/dist/templates/engine/turns/opening.d.ts +34 -0
- package/dist/templates/engine/turns/opening.js +306 -0
- package/dist/templates/engine/turns/overtalk.d.ts +21 -0
- package/dist/templates/engine/turns/overtalk.js +247 -0
- package/dist/templates/engine/turns/pending.d.ts +9 -0
- package/dist/templates/engine/turns/pending.js +88 -0
- package/dist/templates/engine/turns/reply-rules.d.ts +184 -0
- package/dist/templates/engine/turns/reply-rules.js +335 -0
- package/dist/templates/engine/turns/rules.d.ts +20 -0
- package/dist/templates/engine/turns/rules.js +144 -0
- package/dist/templates/engine/turns/silence.d.ts +7 -0
- package/dist/templates/engine/turns/silence.js +127 -0
- package/dist/templates/engine/turns/state-spec.d.ts +305 -0
- package/dist/templates/engine/turns/state-spec.js +2 -0
- package/dist/templates/engine/turns/state.d.ts +38 -0
- package/dist/templates/engine/turns/state.js +101 -0
- package/dist/templates/engine/turns/tracking.d.ts +50 -0
- package/dist/templates/engine/turns/tracking.js +184 -0
- package/dist/templates/engine/turns/transfer.d.ts +19 -0
- package/dist/templates/engine/turns/transfer.js +93 -0
- package/dist/templates/engine/turns/turn.d.ts +17 -0
- package/dist/templates/engine/turns/turn.js +363 -0
- package/dist/templates/engine/turns/voicemail.d.ts +8 -0
- package/dist/templates/engine/turns/voicemail.js +101 -0
- package/dist/templates/engine/variants.d.ts +36 -0
- package/dist/templates/engine/variants.js +159 -0
- package/dist/templates/engine/voice-dialog.d.ts +24 -0
- package/dist/templates/engine/voice-dialog.js +157 -0
- package/dist/templates/engine/voice.d.ts +174 -0
- package/dist/templates/engine/voice.js +1337 -0
- package/dist/templates/expr/caller-after.d.ts +20 -0
- package/dist/templates/expr/caller-after.js +53 -0
- package/dist/templates/expr/check.d.ts +19 -0
- package/dist/templates/expr/check.js +314 -0
- package/dist/templates/expr/compiled.d.ts +8 -0
- package/dist/templates/expr/compiled.js +8 -0
- package/dist/templates/expr/context.d.ts +28 -0
- package/dist/templates/expr/context.js +55 -0
- package/dist/templates/expr/day-table.d.ts +4 -0
- package/dist/templates/expr/day-table.js +80 -0
- package/dist/templates/expr/evaluate.d.ts +26 -0
- package/dist/templates/expr/evaluate.js +885 -0
- package/dist/templates/expr/facts.d.ts +7 -0
- package/dist/templates/expr/facts.js +81 -0
- package/dist/templates/expr/index.d.ts +4 -0
- package/dist/templates/expr/index.js +11 -0
- package/dist/templates/expr/line-tally.d.ts +64 -0
- package/dist/templates/expr/line-tally.js +88 -0
- package/dist/templates/expr/matchers-state.d.ts +23 -0
- package/dist/templates/expr/matchers-state.js +37 -0
- package/dist/templates/expr/matchers.d.ts +12 -0
- package/dist/templates/expr/matchers.js +99 -0
- package/dist/templates/expr/readers.d.ts +14 -0
- package/dist/templates/expr/readers.js +351 -0
- package/dist/templates/expr/slots.d.ts +14 -0
- package/dist/templates/expr/slots.js +678 -0
- package/dist/templates/expr/template.d.ts +12 -0
- package/dist/templates/expr/template.js +76 -0
- package/dist/templates/expr/test-call.d.ts +8 -0
- package/dist/templates/expr/test-call.js +32 -0
- package/dist/templates/expr/transcript.d.ts +36 -0
- package/dist/templates/expr/transcript.js +394 -0
- package/dist/templates/expr/types.d.ts +1011 -0
- package/dist/templates/expr/types.js +2 -0
- package/dist/templates/expr/values.d.ts +42 -0
- package/dist/templates/expr/values.js +166 -0
- package/dist/templates/expr/voice-ops.d.ts +26 -0
- package/dist/templates/expr/voice-ops.js +85 -0
- package/dist/templates/helpers/decision.d.ts +55 -0
- package/dist/templates/helpers/decision.js +83 -0
- package/dist/templates/helpers/fuzzy.d.ts +9 -0
- package/dist/templates/helpers/fuzzy.js +44 -0
- package/dist/templates/helpers/index.d.ts +6 -0
- package/dist/templates/helpers/index.js +35 -0
- package/dist/templates/index.d.ts +9 -0
- package/dist/templates/index.js +68 -0
- package/dist/templates/integration-forms.d.ts +252 -0
- package/dist/templates/integration-forms.js +2 -0
- package/dist/templates/knowledge/block.d.ts +44 -0
- package/dist/templates/knowledge/block.js +199 -0
- package/dist/templates/knowledge/dense.d.ts +87 -0
- package/dist/templates/knowledge/dense.js +158 -0
- package/dist/templates/knowledge/flow.d.ts +328 -0
- package/dist/templates/knowledge/flow.js +509 -0
- package/dist/templates/knowledge/retriever.d.ts +75 -0
- package/dist/templates/knowledge/retriever.js +204 -0
- package/dist/templates/knowledge/service.d.ts +77 -0
- package/dist/templates/knowledge/service.js +316 -0
- package/dist/templates/knowledge/text.d.ts +31 -0
- package/dist/templates/knowledge/text.js +66 -0
- package/dist/templates/knowledge/types.d.ts +120 -0
- package/dist/templates/knowledge/types.js +2 -0
- package/dist/templates/locale.d.ts +259 -0
- package/dist/templates/locale.js +4 -0
- package/dist/templates/locales/en-lexicons.d.ts +9 -0
- package/dist/templates/locales/en-lexicons.js +101 -0
- package/dist/templates/locales/en.d.ts +132 -0
- package/dist/templates/locales/en.js +261 -0
- package/dist/templates/locales/es.d.ts +65 -0
- package/dist/templates/locales/es.js +208 -0
- package/dist/templates/locales/hi.d.ts +47 -0
- package/dist/templates/locales/hi.js +235 -0
- package/dist/templates/locales/id.d.ts +43 -0
- package/dist/templates/locales/id.js +127 -0
- package/dist/templates/locales/index.d.ts +34 -0
- package/dist/templates/locales/index.js +112 -0
- package/dist/templates/locales/kk-lexicons.d.ts +22 -0
- package/dist/templates/locales/kk-lexicons.js +89 -0
- package/dist/templates/locales/kk.d.ts +140 -0
- package/dist/templates/locales/kk.js +306 -0
- package/dist/templates/locales/kn.d.ts +29 -0
- package/dist/templates/locales/kn.js +22 -0
- package/dist/templates/locales/listen-only.d.ts +15 -0
- package/dist/templates/locales/listen-only.js +27 -0
- package/dist/templates/locales/ms-lexicons.d.ts +13 -0
- package/dist/templates/locales/ms-lexicons.js +113 -0
- package/dist/templates/locales/ms.d.ts +76 -0
- package/dist/templates/locales/ms.js +179 -0
- package/dist/templates/locales/parse.d.ts +86 -0
- package/dist/templates/locales/parse.js +461 -0
- package/dist/templates/locales/ru-lexicons.d.ts +27 -0
- package/dist/templates/locales/ru-lexicons.js +127 -0
- package/dist/templates/locales/ru-stem.d.ts +2 -0
- package/dist/templates/locales/ru-stem.js +53 -0
- package/dist/templates/locales/ru.d.ts +143 -0
- package/dist/templates/locales/ru.js +387 -0
- package/dist/templates/locales/shared.d.ts +122 -0
- package/dist/templates/locales/shared.js +318 -0
- package/dist/templates/locales/ta.d.ts +29 -0
- package/dist/templates/locales/ta.js +22 -0
- package/dist/templates/locales/te.d.ts +29 -0
- package/dist/templates/locales/te.js +22 -0
- package/dist/templates/locales/tl-lexicons.d.ts +2 -0
- package/dist/templates/locales/tl-lexicons.js +230 -0
- package/dist/templates/locales/tl.d.ts +59 -0
- package/dist/templates/locales/tl.js +82 -0
- package/dist/templates/locales/vi.d.ts +59 -0
- package/dist/templates/locales/vi.js +160 -0
- package/dist/templates/locales/word-regex.d.ts +13 -0
- package/dist/templates/locales/word-regex.js +43 -0
- package/dist/templates/locales/words-kk.d.ts +2 -0
- package/dist/templates/locales/words-kk.js +23 -0
- package/dist/templates/locales/words-ru.d.ts +4 -0
- package/dist/templates/locales/words-ru.js +92 -0
- package/dist/templates/spec.d.ts +2856 -0
- package/dist/templates/spec.js +2 -0
- package/dist/testing/fake-channel.d.ts +0 -1
- package/dist/testing/fake-channel.js +0 -1
- package/dist/testing/index.d.ts +24 -1
- package/dist/testing/index.js +117 -2
- package/dist/voice/call-voice.d.ts +581 -0
- package/dist/voice/call-voice.js +1399 -0
- package/dist/voice/intent-interrupt.d.ts +32 -0
- package/dist/voice/intent-interrupt.js +91 -0
- package/dist/voice/self-echo.d.ts +28 -0
- package/dist/voice/self-echo.js +135 -0
- package/dist/voice/sentence-tag.d.ts +5 -0
- package/dist/voice/sentence-tag.js +42 -0
- package/dist/voice/speech-leak.d.ts +74 -0
- package/dist/voice/speech-leak.js +216 -0
- package/dist/voice/transfer.d.ts +90 -0
- package/dist/voice/transfer.js +331 -0
- package/dist/voice/turn-timing.d.ts +293 -0
- package/dist/voice/turn-timing.js +567 -0
- package/docs/README.md +3 -6
- package/docs/framework/README.md +6 -10
- package/docs/framework/architecture.md +2 -2
- package/docs/framework/container-and-di.md +1 -2
- package/docs/framework/invariants.md +6 -6
- package/docs/framework/package-layout.md +16 -23
- package/docs/framework/testing.md +8 -6
- package/package.json +10 -54
- package/skill/SKILL.md +213 -0
- package/skill/references/agent-file.md +230 -0
- package/skill/references/blocks.md +246 -0
- package/skill/references/builder.md +163 -0
- package/skill/references/business.md +659 -0
- package/skill/references/conversation.md +586 -0
- package/skill/references/core.md +89 -0
- package/skill/references/examples.md +73 -0
- package/skill/references/expressions.md +342 -0
- package/skill/references/format.md +114 -0
- package/skill/references/locales.md +149 -0
- package/skill/references/migration.md +133 -0
- package/skill/references/new-agent.md +81 -0
- package/skill/references/porting.md +109 -0
- package/templates/agent/README.md +50 -0
- package/templates/agent/package.json +24 -0
- package/templates/agent/src/agent.spec.ts +51 -0
- package/templates/agent/src/agent.ts +268 -0
- package/templates/agent/src/entities.ts +44 -0
- package/templates/agent/src/prompt.sqrl +21 -0
- package/templates/agent/src/variants/fixed-opening.ts +144 -0
- package/templates/agent/tsconfig.build.json +4 -0
- package/templates/agent/tsconfig.json +18 -0
- package/dist/asr/index.d.ts +0 -3
- package/dist/asr/index.d.ts.map +0 -1
- package/dist/asr/index.js +0 -6
- package/dist/asr/index.js.map +0 -1
- package/dist/barge-in/barge-in-module.d.ts.map +0 -1
- package/dist/barge-in/barge-in-module.js.map +0 -1
- package/dist/barge-in/barge-in-service.d.ts.map +0 -1
- package/dist/barge-in/barge-in-service.js.map +0 -1
- package/dist/barge-in/barge-in-strategy.d.ts.map +0 -1
- package/dist/barge-in/barge-in-strategy.js.map +0 -1
- package/dist/barge-in/echo-cancellation.d.ts.map +0 -1
- package/dist/barge-in/echo-cancellation.js.map +0 -1
- package/dist/barge-in/echo.d.ts.map +0 -1
- package/dist/barge-in/echo.js.map +0 -1
- package/dist/barge-in/index.d.ts +0 -13
- package/dist/barge-in/index.d.ts.map +0 -1
- package/dist/barge-in/index.js +0 -28
- package/dist/barge-in/index.js.map +0 -1
- package/dist/barge-in/lexicon.d.ts.map +0 -1
- package/dist/barge-in/lexicon.js.map +0 -1
- package/dist/barge-in/lexicons/en.d.ts +0 -2
- package/dist/barge-in/lexicons/en.d.ts.map +0 -1
- package/dist/barge-in/lexicons/en.js +0 -36
- package/dist/barge-in/lexicons/en.js.map +0 -1
- package/dist/barge-in/lexicons/id.d.ts +0 -2
- package/dist/barge-in/lexicons/id.d.ts.map +0 -1
- package/dist/barge-in/lexicons/id.js +0 -39
- package/dist/barge-in/lexicons/id.js.map +0 -1
- package/dist/barge-in/lexicons/index.d.ts +0 -10
- package/dist/barge-in/lexicons/index.d.ts.map +0 -1
- package/dist/barge-in/lexicons/index.js +0 -11
- package/dist/barge-in/lexicons/index.js.map +0 -1
- package/dist/barge-in/lexicons/ru.d.ts +0 -2
- package/dist/barge-in/lexicons/ru.d.ts.map +0 -1
- package/dist/barge-in/lexicons/ru.js +0 -39
- package/dist/barge-in/lexicons/ru.js.map +0 -1
- package/dist/barge-in/speech-ledger.d.ts.map +0 -1
- package/dist/barge-in/speech-ledger.js.map +0 -1
- package/dist/barge-in/word-classifier.d.ts.map +0 -1
- package/dist/barge-in/word-classifier.js.map +0 -1
- package/dist/bridge/bridge.d.ts +0 -28
- package/dist/bridge/bridge.d.ts.map +0 -1
- package/dist/bridge/bridge.js +0 -117
- package/dist/bridge/bridge.js.map +0 -1
- package/dist/bridge/index.d.ts +0 -3
- package/dist/bridge/index.d.ts.map +0 -1
- package/dist/bridge/index.js +0 -8
- package/dist/bridge/index.js.map +0 -1
- package/dist/core/call-disposables.d.ts.map +0 -1
- package/dist/core/call-disposables.js.map +0 -1
- package/dist/core/container.d.ts.map +0 -1
- package/dist/core/container.js.map +0 -1
- package/dist/core/create-agent-script.d.ts.map +0 -1
- package/dist/core/create-agent-script.js.map +0 -1
- package/dist/core/decorators.d.ts.map +0 -1
- package/dist/core/decorators.js.map +0 -1
- package/dist/core/define-module.d.ts.map +0 -1
- package/dist/core/define-module.js.map +0 -1
- package/dist/core/dump-policy.d.ts.map +0 -1
- package/dist/core/dump-policy.js.map +0 -1
- package/dist/core/hooks.d.ts.map +0 -1
- package/dist/core/hooks.js.map +0 -1
- package/dist/core/index.d.ts +0 -16
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/index.js +0 -36
- package/dist/core/index.js.map +0 -1
- package/dist/core/media-bootstrap.d.ts.map +0 -1
- package/dist/core/media-bootstrap.js.map +0 -1
- package/dist/core/provide-media.d.ts.map +0 -1
- package/dist/core/provide-media.js.map +0 -1
- package/dist/core/tokens.d.ts.map +0 -1
- package/dist/core/tokens.js.map +0 -1
- package/dist/core/types.d.ts.map +0 -1
- package/dist/core/types.js.map +0 -1
- package/dist/core/version.d.ts.map +0 -1
- package/dist/core/version.js.map +0 -1
- package/dist/dialog/basic-ports.d.ts +0 -54
- package/dist/dialog/basic-ports.d.ts.map +0 -1
- package/dist/dialog/basic-ports.js +0 -206
- package/dist/dialog/basic-ports.js.map +0 -1
- package/dist/dialog/define-dialog.d.ts +0 -21
- package/dist/dialog/define-dialog.d.ts.map +0 -1
- package/dist/dialog/define-dialog.js +0 -21
- package/dist/dialog/define-dialog.js.map +0 -1
- package/dist/dialog/dialog-definition.d.ts.map +0 -1
- package/dist/dialog/dialog-definition.js.map +0 -1
- package/dist/dialog/dialog-ports.d.ts.map +0 -1
- package/dist/dialog/dialog-ports.js.map +0 -1
- package/dist/dialog/index.d.ts +0 -7
- package/dist/dialog/index.d.ts.map +0 -1
- package/dist/dialog/index.js +0 -23
- package/dist/dialog/index.js.map +0 -1
- package/dist/dialog/online-call-host.d.ts.map +0 -1
- package/dist/dialog/online-call-host.js.map +0 -1
- package/dist/dialog/online-call-module.d.ts.map +0 -1
- package/dist/dialog/online-call-module.js.map +0 -1
- package/dist/dialog-events/active-media.d.ts.map +0 -1
- package/dist/dialog-events/active-media.js.map +0 -1
- package/dist/dialog-events/event-source.d.ts.map +0 -1
- package/dist/dialog-events/event-source.js.map +0 -1
- package/dist/dialog-events/index.d.ts +0 -8
- package/dist/dialog-events/index.d.ts.map +0 -1
- package/dist/dialog-events/index.js +0 -24
- package/dist/dialog-events/index.js.map +0 -1
- package/dist/dialog-events/max-listen-source.d.ts +0 -27
- package/dist/dialog-events/max-listen-source.d.ts.map +0 -1
- package/dist/dialog-events/max-listen-source.js +0 -98
- package/dist/dialog-events/max-listen-source.js.map +0 -1
- package/dist/dialog-events/overtalk-source.d.ts +0 -61
- package/dist/dialog-events/overtalk-source.d.ts.map +0 -1
- package/dist/dialog-events/overtalk-source.js +0 -232
- package/dist/dialog-events/overtalk-source.js.map +0 -1
- package/dist/dialog-events/silence-source.d.ts +0 -32
- package/dist/dialog-events/silence-source.d.ts.map +0 -1
- package/dist/dialog-events/silence-source.js +0 -128
- package/dist/dialog-events/silence-source.js.map +0 -1
- package/dist/dialog-events/virtual-finals-module.d.ts.map +0 -1
- package/dist/dialog-events/virtual-finals-module.js.map +0 -1
- package/dist/dialog-events/virtual-finals.d.ts.map +0 -1
- package/dist/dialog-events/virtual-finals.js.map +0 -1
- package/dist/dtmf/dtmf.d.ts +0 -27
- package/dist/dtmf/dtmf.d.ts.map +0 -1
- package/dist/dtmf/dtmf.js +0 -87
- package/dist/dtmf/dtmf.js.map +0 -1
- package/dist/dtmf/index.d.ts +0 -3
- package/dist/dtmf/index.d.ts.map +0 -1
- package/dist/dtmf/index.js +0 -8
- package/dist/dtmf/index.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lifecycle/index.d.ts +0 -3
- package/dist/lifecycle/index.d.ts.map +0 -1
- package/dist/lifecycle/index.js +0 -9
- package/dist/lifecycle/index.js.map +0 -1
- package/dist/lifecycle/lifecycle.d.ts.map +0 -1
- package/dist/lifecycle/lifecycle.js.map +0 -1
- package/dist/llm/index.d.ts +0 -7
- package/dist/llm/index.d.ts.map +0 -1
- package/dist/llm/index.js +0 -18
- package/dist/llm/index.js.map +0 -1
- package/dist/llm/llm-markers-module.d.ts +0 -15
- package/dist/llm/llm-markers-module.d.ts.map +0 -1
- package/dist/llm/llm-markers-module.js +0 -48
- package/dist/llm/llm-markers-module.js.map +0 -1
- package/dist/llm/llm-markers.d.ts +0 -36
- package/dist/llm/llm-markers.d.ts.map +0 -1
- package/dist/llm/llm-markers.js +0 -210
- package/dist/llm/llm-markers.js.map +0 -1
- package/dist/llm/llm-module.d.ts +0 -20
- package/dist/llm/llm-module.d.ts.map +0 -1
- package/dist/llm/llm-module.js.map +0 -1
- package/dist/match/index.d.ts +0 -3
- package/dist/match/index.d.ts.map +0 -1
- package/dist/match/index.js +0 -10
- package/dist/match/index.js.map +0 -1
- package/dist/match/match.d.ts +0 -33
- package/dist/match/match.d.ts.map +0 -1
- package/dist/match/match.js +0 -85
- package/dist/match/match.js.map +0 -1
- package/dist/outbound/index.d.ts +0 -3
- package/dist/outbound/index.d.ts.map +0 -1
- package/dist/outbound/index.js +0 -8
- package/dist/outbound/index.js.map +0 -1
- package/dist/outbound/outbound.d.ts +0 -40
- package/dist/outbound/outbound.d.ts.map +0 -1
- package/dist/outbound/outbound.js +0 -84
- package/dist/outbound/outbound.js.map +0 -1
- package/dist/result/index.d.ts +0 -2
- package/dist/result/index.d.ts.map +0 -1
- package/dist/result/index.js.map +0 -1
- package/dist/result/result-store.d.ts.map +0 -1
- package/dist/result/result-store.js.map +0 -1
- package/dist/testing/fake-channel.d.ts.map +0 -1
- package/dist/testing/fake-channel.js.map +0 -1
- package/dist/testing/index.d.ts.map +0 -1
- package/dist/testing/index.js.map +0 -1
- package/docs/agents/README.md +0 -41
- package/docs/agents/barge-in.md +0 -67
- package/docs/agents/dialog.md +0 -98
- package/docs/agents/getting-started.md +0 -142
- package/docs/agents/imports.md +0 -40
- package/docs/agents/media-and-ports.md +0 -82
- package/docs/agents/modules.md +0 -83
- package/docs/agents/phases.md +0 -83
- package/docs/framework/extending.md +0 -59
- package/docs/framework/idn-parity.md +0 -18
- package/examples/minimal-agent/README.md +0 -7
- package/examples/minimal-agent/agent.ts +0 -94
|
@@ -0,0 +1,1337 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.defineVoiceAgent = exports.replyPlayedOf = exports.EngineVoicePorts = exports.EngineAnswerHooks = exports.DEFAULT_HOOK_TIMEOUT_MS = exports.debouncedVoicemail = exports.EngineVoicemailSource = exports.EngineVoiceSource = exports.EngineDialogDefinition = exports.mediaSettings = exports.resolveVoice = exports.sourcedVoiceOf = void 0;
|
|
16
|
+
// `defineVoiceAgent(spec, hooks?)` (internal, run by the builder's `.build()`): a whole voice agent from its agent file.
|
|
17
|
+
// The business part (input, extraction, results, report, integrations, recalls) runs through the business templates'
|
|
18
|
+
// adapter; the conversation is the engine's reducer (./dialog with ./voice-dialog) on the engine's call media
|
|
19
|
+
// (./media). This file builds the media settings from the agent file, maps media events to the reducer and its effects
|
|
20
|
+
// to the media, and assembles the script.
|
|
21
|
+
require("reflect-metadata");
|
|
22
|
+
const tsyringe_1 = require("tsyringe");
|
|
23
|
+
const rxjs_1 = require("rxjs");
|
|
24
|
+
const agent_sdk_1 = require("@voctiv/agent-sdk");
|
|
25
|
+
const decorators_1 = require("../../core/decorators");
|
|
26
|
+
const create_agent_script_1 = require("../../core/create-agent-script");
|
|
27
|
+
const define_module_1 = require("../../core/define-module");
|
|
28
|
+
const tokens_1 = require("../../core/tokens");
|
|
29
|
+
const hooks_1 = require("../../core/hooks");
|
|
30
|
+
const dump_policy_1 = require("../../core/dump-policy");
|
|
31
|
+
const barge_in_strategy_1 = require("../../barge-in/barge-in-strategy");
|
|
32
|
+
const barge_in_module_1 = require("../../barge-in/barge-in-module");
|
|
33
|
+
const online_call_module_1 = require("../../dialog/online-call-module");
|
|
34
|
+
const dialog_definition_1 = require("../../dialog/dialog-definition");
|
|
35
|
+
const dialog_ports_1 = require("../../dialog/dialog-ports");
|
|
36
|
+
const active_media_1 = require("../../dialog-events/active-media");
|
|
37
|
+
const virtual_finals_module_1 = require("../../dialog-events/virtual-finals-module");
|
|
38
|
+
const virtual_finals_1 = require("../../dialog-events/virtual-finals");
|
|
39
|
+
const lifecycle_1 = require("../../lifecycle/lifecycle");
|
|
40
|
+
const report_1 = require("../../call/report");
|
|
41
|
+
const env_1 = require("../../call/env");
|
|
42
|
+
const voicemail_1 = require("../../call/voicemail");
|
|
43
|
+
const call_voice_1 = require("../../voice/call-voice");
|
|
44
|
+
const adapter_1 = require("../business/adapter");
|
|
45
|
+
const sources_1 = require("../business/sources");
|
|
46
|
+
const integrations_1 = require("../business/integrations");
|
|
47
|
+
const sources_2 = require("../business/sources");
|
|
48
|
+
const config_1 = require("./config");
|
|
49
|
+
const call_1 = require("./call");
|
|
50
|
+
const hook_state_1 = require("./hook-state");
|
|
51
|
+
const dialog_1 = require("./dialog");
|
|
52
|
+
const filters_1 = require("./filters");
|
|
53
|
+
const expand_1 = require("./expand");
|
|
54
|
+
const hooks_2 = require("./hooks");
|
|
55
|
+
const match_1 = require("./match");
|
|
56
|
+
const media_1 = require("./media");
|
|
57
|
+
const variants_1 = require("./variants");
|
|
58
|
+
const asr_picks_1 = require("./asr-picks");
|
|
59
|
+
const script_1 = require("./script");
|
|
60
|
+
const lookup_1 = require("./lookup");
|
|
61
|
+
const sentence_tag_1 = require("../../voice/sentence-tag");
|
|
62
|
+
const compiled_1 = require("../expr/compiled");
|
|
63
|
+
const slots_1 = require("../expr/slots");
|
|
64
|
+
const state_1 = require("./turns/state");
|
|
65
|
+
const slots_2 = require("../expr/slots");
|
|
66
|
+
const locales_1 = require("../locales");
|
|
67
|
+
const echo_numbers_1 = require("./echo-numbers");
|
|
68
|
+
/** Defaults of the call media. */
|
|
69
|
+
const DEFAULT_SAY_TIMEOUT_MS = 30_000;
|
|
70
|
+
/** `transfer.answerTimeoutMs` (bridge): how long the operator leg rings. */
|
|
71
|
+
const DEFAULT_TRANSFER_ANSWER_TIMEOUT_MS = 30_000;
|
|
72
|
+
const DEFAULT_DRAIN = { msPerChar: 90, minMs: 20_000 };
|
|
73
|
+
const DEFAULT_MAX_UTTERANCE_MS = 120_000;
|
|
74
|
+
const DEFAULT_QUEUES = { tts: 1, ambient: 2, sfx: 0 };
|
|
75
|
+
const NO_AUDIO_WARN_MS = 5_000;
|
|
76
|
+
/** `turn.playedOut: queueIdle`: nothing queued for this long, and at least this many ms per spoken character. */
|
|
77
|
+
const PLAYED_OUT_SETTLE_MS = 500;
|
|
78
|
+
const PLAYED_OUT_MS_PER_CHAR = 45;
|
|
79
|
+
const UNDECIDED = 'undecided';
|
|
80
|
+
/** Silence window of a call without a conversation (an opening that ends the call): never reached. */
|
|
81
|
+
const NO_SILENCE_WINDOW_MS = 86_400_000;
|
|
82
|
+
/** `TtsSpec` fields the engine reads itself; the other keys go to the vendor as they are. */
|
|
83
|
+
const TTS_FIELDS = new Set(['vendor', 'name', 'voiceId', 'model', 'strategy', 'session', 'variants', 'variantBy', 'variantPick', 'variantVar', 'respell', 'respellScope', 'respellBy', 'presay', 'sentenceTag']);
|
|
84
|
+
const ENGINE_CONFIG = 'TEMPLATES_ENGINE_CONFIG';
|
|
85
|
+
const ENGINE_DETECTORS = 'TEMPLATES_ENGINE_DETECTORS';
|
|
86
|
+
const ENGINE_HOOKS = 'TEMPLATES_ENGINE_HOOKS';
|
|
87
|
+
const list = (x) => (x == null ? [] : Array.isArray(x) ? x : [x]);
|
|
88
|
+
/** A voice value as built once per agent: a value read from a source is its `default` here (the call reads it, see `sourcedVoiceOf`). */
|
|
89
|
+
function literal(v) {
|
|
90
|
+
if (v === undefined)
|
|
91
|
+
return undefined;
|
|
92
|
+
if ((0, sources_1.isSourced)(v))
|
|
93
|
+
return v.default;
|
|
94
|
+
return v;
|
|
95
|
+
}
|
|
96
|
+
/** Voice values of the agent file that are read from sources per call (none: `undefined`). */
|
|
97
|
+
function sourcedVoiceOf(file) {
|
|
98
|
+
const v = file.voice;
|
|
99
|
+
const first = v.asr[file.languages[0]];
|
|
100
|
+
const op = file.transfer?.humanDetect?.asr.vendor;
|
|
101
|
+
const out = {
|
|
102
|
+
...((0, sources_1.isSourced)(v.tts.voiceId) ? { voiceId: v.tts.voiceId } : {}),
|
|
103
|
+
...((0, sources_1.isSourced)(v.tts.model) ? { model: v.tts.model } : {}),
|
|
104
|
+
...((0, sources_1.isSourced)(first?.vendor) ? { asrVendor: first.vendor } : {}),
|
|
105
|
+
...((0, sources_1.isSourced)(v.ambient?.url) ? { ambientUrl: v.ambient.url } : {}),
|
|
106
|
+
...((0, sources_1.isSourced)(op) ? { operatorVendor: op } : {}),
|
|
107
|
+
};
|
|
108
|
+
return Object.keys(out).length ? out : undefined;
|
|
109
|
+
}
|
|
110
|
+
exports.sourcedVoiceOf = sourcedVoiceOf;
|
|
111
|
+
/** The call's values of the sourced voice fields (a source that fails or gives nothing: the field's `default`). */
|
|
112
|
+
async function resolveVoice(sv, scope, logger) {
|
|
113
|
+
const str = async (field, x) => {
|
|
114
|
+
if (x === undefined)
|
|
115
|
+
return undefined;
|
|
116
|
+
try {
|
|
117
|
+
const got = await (0, sources_2.resolveValue)(x, scope);
|
|
118
|
+
return got == null || got === '' ? literal(x) : String(got);
|
|
119
|
+
}
|
|
120
|
+
catch (err) {
|
|
121
|
+
logger?.log('voice_source_failed', { field, err: String(err) });
|
|
122
|
+
return literal(x);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const [voiceId, model, asrVendor, ambientUrl, operatorVendor] = await Promise.all([
|
|
126
|
+
str('voice.tts.voiceId', sv.voiceId), str('voice.tts.model', sv.model), str('voice.asr.vendor', sv.asrVendor),
|
|
127
|
+
str('voice.ambient.url', sv.ambientUrl), str('transfer.humanDetect.asr.vendor', sv.operatorVendor),
|
|
128
|
+
]);
|
|
129
|
+
return {
|
|
130
|
+
...(voiceId != null ? { voiceId } : {}), ...(model != null ? { model } : {}), ...(asrVendor != null ? { asrVendor } : {}),
|
|
131
|
+
...(ambientUrl != null ? { ambientUrl } : {}), ...(operatorVendor != null ? { operatorVendor } : {}),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
exports.resolveVoice = resolveVoice;
|
|
135
|
+
const same = (xs) => xs.every((x) => x === xs[0]);
|
|
136
|
+
/** Every clip the agent file can play by CMS record name (looked up once, at answer). */
|
|
137
|
+
function recordNames(spec) {
|
|
138
|
+
const out = [];
|
|
139
|
+
const addPlay = (p) => {
|
|
140
|
+
if (p?.record != null && !out.includes(p.record))
|
|
141
|
+
out.push(p.record);
|
|
142
|
+
for (const r of p?.recordSet ?? [])
|
|
143
|
+
if (!out.includes(r))
|
|
144
|
+
out.push(r);
|
|
145
|
+
};
|
|
146
|
+
const addActions = (a) => { for (const x of list(a))
|
|
147
|
+
if ('play' in x)
|
|
148
|
+
addPlay(x.play); };
|
|
149
|
+
for (const s of spec.opening) {
|
|
150
|
+
if (s.step === 'play')
|
|
151
|
+
addPlay(s);
|
|
152
|
+
if (s.step === 'listen')
|
|
153
|
+
for (const r of s.silence ?? [])
|
|
154
|
+
addActions(r.do);
|
|
155
|
+
}
|
|
156
|
+
for (const r of spec.silence.ladder)
|
|
157
|
+
addActions(r.do);
|
|
158
|
+
for (const r of spec.rules ?? [])
|
|
159
|
+
addActions(r.then);
|
|
160
|
+
return out;
|
|
161
|
+
}
|
|
162
|
+
function nluDetectors(spec) {
|
|
163
|
+
const out = [];
|
|
164
|
+
for (const [i, d] of (spec.voicemail?.detectors ?? []).entries()) {
|
|
165
|
+
if (d.by !== 'nlu')
|
|
166
|
+
continue;
|
|
167
|
+
const patterns = d.fallback ? (0, match_1.matcherAsPatterns)(d.fallback, spec.languages, `voicemail.detectors[${i}].fallback`) : [];
|
|
168
|
+
out.push({
|
|
169
|
+
onPartials: d.onPartials !== false,
|
|
170
|
+
debounce: d.timing === 'debounce',
|
|
171
|
+
fallbackFirst: d.fallbackFirst === true,
|
|
172
|
+
config: {
|
|
173
|
+
enabled: true,
|
|
174
|
+
intents: d.intents,
|
|
175
|
+
patterns,
|
|
176
|
+
...(d.minChars != null ? { minChars: d.minChars } : {}),
|
|
177
|
+
...(d.throttleMs != null ? { throttleMs: d.throttleMs } : {}),
|
|
178
|
+
useNeuroApi: false,
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
const floor = (f) => f ? { msPerChar: f.msPerChar, minMs: f.minMs, from: f.from, ...(f.staticFrom ? { staticFrom: f.staticFrom } : {}) } : null;
|
|
185
|
+
/** The transfer block as the media runs it. */
|
|
186
|
+
/** `voice.selfEcho` with `expandNumbers` made the `expandToken` of the first language's pack. */
|
|
187
|
+
function selfEchoOf(e, language) {
|
|
188
|
+
const { expandNumbers, ...rest } = e;
|
|
189
|
+
if (!expandNumbers || rest.expandToken)
|
|
190
|
+
return rest;
|
|
191
|
+
const pack = (0, locales_1.getLocale)(language);
|
|
192
|
+
if (!pack)
|
|
193
|
+
throw new Error(`[templates/engine] voice.selfEcho.expandNumbers: no locale pack "${language}"`);
|
|
194
|
+
return { ...rest, expandToken: (0, echo_numbers_1.numberExpander)(pack.format, expandNumbers === true ? {} : expandNumbers) };
|
|
195
|
+
}
|
|
196
|
+
function mediaTransfer(spec, languages) {
|
|
197
|
+
const t = spec.transfer;
|
|
198
|
+
if (!t)
|
|
199
|
+
return null;
|
|
200
|
+
const spoken = t.spoken ? (0, match_1.compileMatcher)(t.spoken, languages, 'transfer.spoken') : null;
|
|
201
|
+
const spokenView = { language: languages[0], vars: {}, flags: {}, counters: {}, lines: [], agentLines: [], callerLines: [] };
|
|
202
|
+
const h = t.humanDetect;
|
|
203
|
+
let dial = null;
|
|
204
|
+
if (t.mode === 'dialAndWhisper' && h) {
|
|
205
|
+
const queue = (0, match_1.matcherAsPatterns)(h.queue, languages, 'transfer.humanDetect.queue');
|
|
206
|
+
const r = t.results ?? { connected: 'bridge' };
|
|
207
|
+
dial = {
|
|
208
|
+
operatorAsr: {
|
|
209
|
+
...(h.asr.name ? { name: h.asr.name } : {}),
|
|
210
|
+
...(h.asr.vendor != null ? { vendor: literal(h.asr.vendor) } : {}),
|
|
211
|
+
language: h.asr.language,
|
|
212
|
+
},
|
|
213
|
+
queuePatterns: queue,
|
|
214
|
+
holdTimeoutMs: h.holdTimeoutMs,
|
|
215
|
+
results: { connected: r.connected, notAnswered: r.notAnswered ?? r.connected, customerDropped: r.customerDropped ?? r.connected },
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
return {
|
|
219
|
+
mode: t.mode,
|
|
220
|
+
spoken: spoken ? (text) => spoken(text, spokenView) : null,
|
|
221
|
+
...(t.requireRequest ? { refusedSignal: t.requireRequest.refusedSignal } : {}),
|
|
222
|
+
phraseFloor: t.phraseFloor ? { msPerChar: t.phraseFloor.msPerChar, minMs: t.phraseFloor.minMs, from: t.phraseFloor.from } : null,
|
|
223
|
+
graceMs: t.graceMs ?? 0,
|
|
224
|
+
answerTimeoutMs: t.answerTimeoutMs ?? DEFAULT_TRANSFER_ANSWER_TIMEOUT_MS,
|
|
225
|
+
dial,
|
|
226
|
+
...(t.text?.transcript ? { textTranscript: true } : {}),
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/** The `monologue` voicemail detector as the media runs it. */
|
|
230
|
+
function monologueOf(spec) {
|
|
231
|
+
const d = (spec.voicemail?.detectors ?? []).find((x) => x.by === 'monologue');
|
|
232
|
+
if (!d)
|
|
233
|
+
return null;
|
|
234
|
+
return { startWithinMs: d.startWithinMs ?? Number.MAX_SAFE_INTEGER, mergeGapMs: d.mergeGapMs ?? 0, minSpanMs: d.ms, flag: 'voicemail.monologue' };
|
|
235
|
+
}
|
|
236
|
+
/** `voice.asr.<lang>.pick` / `picks` as the media reads it: from the call's variables. */
|
|
237
|
+
function asrPicks(spec) {
|
|
238
|
+
const out = {};
|
|
239
|
+
for (const [key, a] of Object.entries(spec.voice.asr)) {
|
|
240
|
+
const fn = a ? (0, asr_picks_1.asrPickFn)(a, spec.languages[0]) : null;
|
|
241
|
+
if (fn)
|
|
242
|
+
out[key] = fn;
|
|
243
|
+
}
|
|
244
|
+
return out;
|
|
245
|
+
}
|
|
246
|
+
/** The media settings of the call from the agent file. */
|
|
247
|
+
function mediaSettings(file, cfg) {
|
|
248
|
+
const spec = (0, config_1.withConversation)(file);
|
|
249
|
+
const v = spec.voice;
|
|
250
|
+
const languages = spec.languages;
|
|
251
|
+
const tts = v.tts;
|
|
252
|
+
const turn = spec.turn ?? {};
|
|
253
|
+
const peakExcept = typeof v.minPeakVad === 'object' && v.minPeakVad.except != null ? (0, match_1.matcherAsPatterns)(v.minPeakVad.except, languages, 'voice.minPeakVad.except') : [];
|
|
254
|
+
const peak = typeof v.minPeakVad === 'number' ? { value: v.minPeakVad, over: 'segment' } : v.minPeakVad ? { value: v.minPeakVad.value, over: v.minPeakVad.over ?? 'segment' } : null;
|
|
255
|
+
const ttsConfig = {
|
|
256
|
+
voice_id: literal(tts.voiceId),
|
|
257
|
+
...(literal(tts.model) != null ? { model_id: literal(tts.model) } : {}),
|
|
258
|
+
};
|
|
259
|
+
for (const [k, x] of Object.entries(tts))
|
|
260
|
+
if (!TTS_FIELDS.has(k))
|
|
261
|
+
ttsConfig[k] = x;
|
|
262
|
+
const mixer = v.mixer ?? {};
|
|
263
|
+
const queues = { tts: mixer['tts'] ?? DEFAULT_QUEUES.tts, ambient: mixer['ambient'] ?? DEFAULT_QUEUES.ambient, ambientVolume: v.ambient?.volume ?? 0 };
|
|
264
|
+
const llmPlay = {
|
|
265
|
+
...(tts.name != null ? { name: tts.name } : {}),
|
|
266
|
+
ttsConfig,
|
|
267
|
+
queue: queues.tts,
|
|
268
|
+
cache: true,
|
|
269
|
+
alias: 'reply',
|
|
270
|
+
...(tts.strategy ? { ttsStrategy: tts.strategy } : {}),
|
|
271
|
+
...(tts.vendor ? { ttsVendor: tts.vendor } : {}),
|
|
272
|
+
};
|
|
273
|
+
// Recognizers: one per language, `undecided` while the pick is provisional.
|
|
274
|
+
const asrOf = (a, key) => {
|
|
275
|
+
if (!a)
|
|
276
|
+
throw new Error(`[templates/engine] voice.asr.${key} is required`);
|
|
277
|
+
return { name: a.name ?? '', language: a.language };
|
|
278
|
+
};
|
|
279
|
+
// An opening that ends the call may run without a recognizer (`voice.asr` empty).
|
|
280
|
+
const noAsr = (0, config_1.endsInOpening)(spec) && Object.keys(v.asr).length === 0;
|
|
281
|
+
const asrLanguages = {};
|
|
282
|
+
if (!noAsr)
|
|
283
|
+
for (const l of languages)
|
|
284
|
+
asrLanguages[l] = asrOf(v.asr[l], l);
|
|
285
|
+
// Recognizers the replies can switch to (`voice.asrByReply`).
|
|
286
|
+
if (v.asrByReply)
|
|
287
|
+
for (const k of Object.keys(v.asr))
|
|
288
|
+
if (k !== UNDECIDED && !asrLanguages[k])
|
|
289
|
+
asrLanguages[k] = asrOf(v.asr[k], k);
|
|
290
|
+
if (v.asr.undecided)
|
|
291
|
+
asrLanguages[UNDECIDED] = asrOf(v.asr.undecided, UNDECIDED);
|
|
292
|
+
const first = v.asr[languages[0]];
|
|
293
|
+
const vendor = literal(first?.vendor);
|
|
294
|
+
const firstData = first?.data;
|
|
295
|
+
// Silence windows: the ladder's, before the caller's first line, after speech without text, while held.
|
|
296
|
+
const sil = spec.silence;
|
|
297
|
+
const ladderMs = sil.ladder.map((r) => r.afterMs);
|
|
298
|
+
if (!ladderMs.length && !(0, config_1.endsInOpening)(spec))
|
|
299
|
+
throw new Error('[templates/engine] silence.ladder needs at least one rung');
|
|
300
|
+
const heldMs = (sil.held?.rungs ?? []).map((r) => r.afterMs);
|
|
301
|
+
const listen = cfg.voice.steps.find((s) => s.kind === 'listen');
|
|
302
|
+
const listenMs = listen && listen.kind === 'listen' ? listen.rungs.map((r) => r.afterMs) : [];
|
|
303
|
+
// The opening listen step waits its own window when it differs from `beforeFirstLineMs` (that one after the opening).
|
|
304
|
+
const ownListen = cfg.voice.afterOpeningWindow;
|
|
305
|
+
const listenHold = spec.opening.find((s) => s.step === 'listen')?.holdWhileSpeaking;
|
|
306
|
+
const beforeFirst = ownListen ? listenMs[0] : sil.beforeFirstLineMs ?? listenMs[0];
|
|
307
|
+
const swt = sil.speechWithoutText;
|
|
308
|
+
const eot = v.endOfTurn;
|
|
309
|
+
const drain = turn.drainCeiling ?? DEFAULT_DRAIN;
|
|
310
|
+
const interrupt = spec.overtalk?.interrupt;
|
|
311
|
+
const interruptPatterns = interrupt?.match ? (0, match_1.matcherAsPatterns)(interrupt.match, languages, 'overtalk.interrupt.match') : [];
|
|
312
|
+
const dropLine = v.dropLine != null ? (0, match_1.matcherAsPattern)(v.dropLine, languages, 'voice.dropLine') : undefined;
|
|
313
|
+
const holdOn = eot?.holdOn != null ? (0, match_1.matcherAsPattern)(eot.holdOn, languages, 'voice.endOfTurn.holdOn') : undefined;
|
|
314
|
+
const er = turn.emptyReply;
|
|
315
|
+
const policy = cfg.emptyExhausted != null;
|
|
316
|
+
const exhausted = er && !policy ? list(er.onExhausted) : [];
|
|
317
|
+
const hangsUp = exhausted.some((a) => 'hangup' in a);
|
|
318
|
+
const ambient = v.ambient?.url != null ? literal(v.ambient.url) : undefined;
|
|
319
|
+
// A silence signal by language or of the call is read at each use (`silenceText`); the one string otherwise.
|
|
320
|
+
const plainSignal = typeof spec.prompt.silenceSignal === 'string' ? spec.prompt.silenceSignal : null;
|
|
321
|
+
const signal = plainSignal ?? (0, match_1.resolveText)(spec.prompt.silenceSignal, { language: languages[0], vars: {}, flags: {}, counters: {}, lines: [], agentLines: [], callerLines: [] }) ?? '';
|
|
322
|
+
const goodbye = cfg.goodbye;
|
|
323
|
+
const goodbyeView = { language: languages[0], vars: {}, flags: {}, counters: {} };
|
|
324
|
+
const ps = spec.prompt.persistentStream;
|
|
325
|
+
const leak = (turn.filters ?? []).find((f) => f.filter === 'scriptLeak');
|
|
326
|
+
const voice = {
|
|
327
|
+
logicVersion: spec.id.version,
|
|
328
|
+
queues,
|
|
329
|
+
audio: { ...(ambient ? { ambientLoopUrl: ambient } : {}) },
|
|
330
|
+
play: { llm: llmPlay, greeting: { ...llmPlay, alias: 'greeting' }, ambient: { queue: queues.ambient, alias: 'ambient', loop: true } },
|
|
331
|
+
ttsSession: tts.session ? { name: tts.name ?? '', data: ttsConfig } : null,
|
|
332
|
+
...(typeof tts.session === 'object' && tts.session.openTimeoutMs > 0 ? { ttsSessionOpenTimeoutMs: tts.session.openTimeoutMs } : {}),
|
|
333
|
+
...(listen && listen.kind === 'listen' && listen.voiceAnswer ? { voiceAnswer: listen.voiceAnswer } : {}),
|
|
334
|
+
...(v.selfEcho ? { selfEcho: selfEchoOf(v.selfEcho, languages[0]) } : {}),
|
|
335
|
+
turnTiming: {
|
|
336
|
+
maxListenMs: eot?.maxUtteranceMs ?? DEFAULT_MAX_UTTERANCE_MS,
|
|
337
|
+
...(sil.speechLatchMaxMs != null ? { staleSpeechCeilingMs: sil.speechLatchMaxMs } : {}),
|
|
338
|
+
maxUserSilenceMs: ladderMs[0] ?? NO_SILENCE_WINDOW_MS,
|
|
339
|
+
...(beforeFirst != null ? { openingUserSilenceMs: beforeFirst } : {}),
|
|
340
|
+
...(ownListen ? { afterOpeningUserSilenceMs: sil.beforeFirstLineMs } : {}),
|
|
341
|
+
...(listenHold ? { openingHold: { pollMs: listenHold.pollMs, maxMs: listenHold.maxMs } } : {}),
|
|
342
|
+
...(swt ? { unheardUserSilenceMs: swt.afterMs, maxUnheardTurns: swt.excused ?? 0, ...(swt.minSpeechMs ? { minUnheardSpeechMs: swt.minSpeechMs } : {}) } : {}),
|
|
343
|
+
...(heldMs.length ? { handoverUserSilenceMs: heldMs[0], maxHandoverTurns: heldMs.length } : {}),
|
|
344
|
+
// Rungs with windows of their own: the n-th silence waits the n-th rung's `afterMs`.
|
|
345
|
+
...(!same(ladderMs) ? { silenceLadderMs: ladderMs } : {}),
|
|
346
|
+
...(!same(heldMs) ? { handoverLadderMs: heldMs } : {}),
|
|
347
|
+
...(!same(listenMs) && beforeFirst === listenMs[0] ? { openingLadderMs: listenMs } : {}),
|
|
348
|
+
...(sil.afterDroppedLineMs != null ? { droppedUserSilenceMs: sil.afterDroppedLineMs } : {}),
|
|
349
|
+
...(sil.noWordsMs != null ? { maxNoWordsMs: sil.noWordsMs } : {}),
|
|
350
|
+
...(sil.noProgress ? { noProgressMs: sil.noProgress.ms } : {}),
|
|
351
|
+
...(sil.maxAwaitMs != null ? { maxAwaitMs: sil.maxAwaitMs } : {}),
|
|
352
|
+
maxSilenceTurns: Math.max(0, ladderMs.length - 1),
|
|
353
|
+
},
|
|
354
|
+
asr: {
|
|
355
|
+
languages: asrLanguages,
|
|
356
|
+
defaultLanguage: languages[0],
|
|
357
|
+
...(v.asr.undecided ? { detectionLanguage: UNDECIDED } : {}),
|
|
358
|
+
...(vendor ? { vendor } : {}),
|
|
359
|
+
...(firstData ? { data: firstData } : {}),
|
|
360
|
+
vad: v.vad,
|
|
361
|
+
smartTurn: eot?.smartTurn ?? {},
|
|
362
|
+
noAudioWarnMs: NO_AUDIO_WARN_MS,
|
|
363
|
+
...(sil.deadAudio ? { deadAudioMs: sil.deadAudio.ms } : {}),
|
|
364
|
+
resultCorrections: (v.corrections ?? []).map((c) => ({ pattern: c.match, replacement: c.replace })),
|
|
365
|
+
},
|
|
366
|
+
lifecycle: {
|
|
367
|
+
sayTimeoutMs: turn.sayTimeoutMs ?? DEFAULT_SAY_TIMEOUT_MS,
|
|
368
|
+
ttsDrainTimeoutMs: drain.minMs,
|
|
369
|
+
ttsDrainMsPerChar: drain.msPerChar,
|
|
370
|
+
rtpTailGraceMs: spec.closing.hangupGraceMs ?? 0,
|
|
371
|
+
},
|
|
372
|
+
connectionErrorSignal: spec.prompt.errorSignal ?? '',
|
|
373
|
+
silenceEllipsis: signal,
|
|
374
|
+
emptyReplyRetries: er && er.max > 1 ? 1 : 0,
|
|
375
|
+
...(policy && er ? { emptyReply: { retry: er.retry, max: er.max, abortedRetries: turn.abortedRetries ?? 0, probes: er.probes?.length ?? 0 } } : {}),
|
|
376
|
+
...(hangsUp ? { hangupOnSecondEmptyReply: true } : {}),
|
|
377
|
+
...(spec.closing.emptyGoodbye === 'hangupNow' ? { emptyGoodbyeHangsUpNow: true } : {}),
|
|
378
|
+
conversation: { endsWithGoodbyeMarker: (t) => goodbye(t, goodbyeView) },
|
|
379
|
+
replyFloor: floor(turn.drainFloor),
|
|
380
|
+
...(spec.closing.openUntilFloor ? { goodbyeOpenUntilFloor: true } : {}),
|
|
381
|
+
goodbyeFloor: floor(spec.closing.drainFloor),
|
|
382
|
+
...(spec.closing.reopenOnSpeech ? { reopenOnSpeech: { runGapMs: spec.closing.reopenOnSpeech.runGapMs ?? 0 } } : {}),
|
|
383
|
+
};
|
|
384
|
+
return {
|
|
385
|
+
voice,
|
|
386
|
+
...(mixer['sfxVolume'] != null ? { sfx: { queue: mixer['sfx'] ?? DEFAULT_QUEUES.sfx, volume: mixer['sfxVolume'] } } : {}),
|
|
387
|
+
persistent: ps ? {} : null,
|
|
388
|
+
...(spec.prompt.hiddenSignals ? { hiddenTexts: signalTexts(spec) } : {}),
|
|
389
|
+
...(leak
|
|
390
|
+
? {
|
|
391
|
+
speechFilter: typeof leak.allow === 'function'
|
|
392
|
+
? (call) => ({ allowedLatinWords: [...(call ? leak.allow(call) : [])] })
|
|
393
|
+
: { allowedLatinWords: [...(leak.allow ?? [])] },
|
|
394
|
+
...(leak.retry != null ? { leakRetry: leak.retry } : {}),
|
|
395
|
+
}
|
|
396
|
+
: {}),
|
|
397
|
+
defaultLanguage: languages[0],
|
|
398
|
+
...(v.dropLineAnswer ? { dropLineAnswer: v.dropLineAnswer } : {}),
|
|
399
|
+
...(turn.playedOut?.by === 'queueIdle'
|
|
400
|
+
? { queueIdle: { startAllowanceMs: turn.playedOut.startAllowanceMs, settleMs: PLAYED_OUT_SETTLE_MS, msPerChar: PLAYED_OUT_MS_PER_CHAR } }
|
|
401
|
+
: {}),
|
|
402
|
+
...(er?.errorEvent != null ? { errorEvent: er.errorEvent.toLowerCase() } : {}),
|
|
403
|
+
transfer: mediaTransfer(spec, languages),
|
|
404
|
+
...(v.filler ? { filler: { urls: v.filler.urls, afterMs: v.filler.afterMs, alias: 'filler' } } : {}),
|
|
405
|
+
...(sourcedVoiceOf(file) ? { sourcedVoice: sourcedVoiceOf(file) } : {}),
|
|
406
|
+
...(plainSignal == null ? { silenceText: spec.prompt.silenceSignal } : {}),
|
|
407
|
+
...(v.clipTranscript ? { clipTranscript: true } : {}),
|
|
408
|
+
...(tts.variants?.length ? { variant: (callUuid, vars) => (0, variants_1.callVoiceVariant)(tts, callUuid, vars) } : {}),
|
|
409
|
+
...(Object.values(v.asr).some((a) => a?.pick || a?.picks?.length) ? { asrPick: asrPicks(spec) } : {}),
|
|
410
|
+
...(v.asrByReply ? { asrByReply: (0, script_1.replyAsrDetector)(v.asrByReply) } : {}),
|
|
411
|
+
...(turn.carryOn || (spec.rules ?? []).some((r) => r.on === 'callerPartial') ? { speechEvents: true } : {}),
|
|
412
|
+
...((spec.rules ?? []).some((r) => r.on === 'toolCall') ? { toolRules: true } : {}),
|
|
413
|
+
...(v.endOfTurn?.vendorFinalInTranscript ? { vendorFinalInTranscript: true } : {}),
|
|
414
|
+
...(interrupt?.intents ? { intentInterrupt: { intents: interrupt.intents.intents, minChars: interrupt.intents.minChars, throttleMs: interrupt.intents.throttleMs } } : {}),
|
|
415
|
+
monologue: monologueOf(spec),
|
|
416
|
+
bargeIn: {
|
|
417
|
+
enabled: interrupt != null,
|
|
418
|
+
interruptPatterns,
|
|
419
|
+
partialMinChars: interrupt?.minChars ?? 0,
|
|
420
|
+
minVadProbability: interrupt?.minVad ?? 0,
|
|
421
|
+
lengthTriggersInterrupt: interrupt?.byLength === true,
|
|
422
|
+
whileSilent: interrupt?.whileSilent === true,
|
|
423
|
+
countLetters: interrupt?.minCharsOf === 'letters',
|
|
424
|
+
},
|
|
425
|
+
eot: {
|
|
426
|
+
finalizeDebounceMs: eot?.finalizeDebounceMs ?? 0,
|
|
427
|
+
...(eot?.quietWindowMs != null ? { quietWindowMs: eot.quietWindowMs } : {}),
|
|
428
|
+
...(holdOn ? { holdOn } : {}),
|
|
429
|
+
...(dropLine ? { dropLine } : {}),
|
|
430
|
+
...(peak ? { minPeakVad: peak.value, ...(peak.over === 'line' ? { minPeakVadOver: 'line' } : {}) } : {}),
|
|
431
|
+
...(typeof v.minPeakVad === 'object' ? { minPeakVadExcept: peakExcept } : {}),
|
|
432
|
+
},
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
exports.mediaSettings = mediaSettings;
|
|
436
|
+
/**
|
|
437
|
+
* The call's dialog: the engine reducer with this call's variables; keeps the last state. It owns the commit of the
|
|
438
|
+
* hooks' state around the pure reduce: before an event, a snapshot of what the hooks keep (`HookApi.state`) goes into
|
|
439
|
+
* the state the reducer gets; after it, what agent code wrote to `call.hookState` in the reduce goes back to the hooks.
|
|
440
|
+
*/
|
|
441
|
+
let EngineDialogDefinition = class EngineDialogDefinition {
|
|
442
|
+
constructor(cfg, call) {
|
|
443
|
+
const vars = (call.payload?.vars) ?? {};
|
|
444
|
+
this.hookStore = (0, sources_1.hookStateOf)(call.context);
|
|
445
|
+
this.dialog = (0, dialog_1.createEngineDialog)(cfg, { vars, attempt: (0, env_1.readAttempt)(call.context), memory: (0, sources_1.readMemory)(call.context), hookState: this.hookStore, startedAt: () => call.startedAt });
|
|
446
|
+
// `input.lookup`: still running (the eager greeting waits for it), or in already with the call's result.
|
|
447
|
+
const lookup = (0, lookup_1.lookupOf)(call.context);
|
|
448
|
+
this.initial = lookup == null ? this.dialog.initial
|
|
449
|
+
: lookup.done === undefined ? { ...this.dialog.initial, lookupPending: true }
|
|
450
|
+
: lookup.done?.result ? { ...this.dialog.initial, result: lookup.done.result } : this.dialog.initial;
|
|
451
|
+
this.state = this.initial;
|
|
452
|
+
}
|
|
453
|
+
reduce(s, e) {
|
|
454
|
+
const before = (0, hook_state_1.snapshotHookState)(this.hookStore);
|
|
455
|
+
const t = this.dialog.reduce({ ...s, hookState: before }, e);
|
|
456
|
+
(0, hook_state_1.commitHookState)(this.hookStore, before, t.state.hookState ?? before);
|
|
457
|
+
this.state = t.state;
|
|
458
|
+
return t;
|
|
459
|
+
}
|
|
460
|
+
/** The call as agent code outside the reducer sees it (hooks, filters, the media): the hooks' state as it is now. */
|
|
461
|
+
view() {
|
|
462
|
+
return this.dialog.view(this.live());
|
|
463
|
+
}
|
|
464
|
+
/** The reducer's answer to a tool call of the reply in flight. */
|
|
465
|
+
toolDecision(names) {
|
|
466
|
+
return this.dialog.toolDecision(this.live(), names);
|
|
467
|
+
}
|
|
468
|
+
live() {
|
|
469
|
+
return { ...this.state, hookState: this.hookStore };
|
|
470
|
+
}
|
|
471
|
+
};
|
|
472
|
+
exports.EngineDialogDefinition = EngineDialogDefinition;
|
|
473
|
+
exports.EngineDialogDefinition = EngineDialogDefinition = __decorate([
|
|
474
|
+
(0, decorators_1.Injectable)(),
|
|
475
|
+
__param(0, (0, tsyringe_1.inject)(ENGINE_CONFIG)),
|
|
476
|
+
__param(1, (0, tsyringe_1.inject)(call_1.EngineCall)),
|
|
477
|
+
__metadata("design:paramtypes", [Object, call_1.EngineCall])
|
|
478
|
+
], EngineDialogDefinition);
|
|
479
|
+
const snapshot = (m) => m;
|
|
480
|
+
/** Media events as the reducer's events (and the ports' own: NLU answers, filler timers). */
|
|
481
|
+
let EngineVoiceSource = class EngineVoiceSource {
|
|
482
|
+
constructor(media, call) {
|
|
483
|
+
this.media = media;
|
|
484
|
+
this.call = call;
|
|
485
|
+
this.name = 'engine-voice';
|
|
486
|
+
this.own = new rxjs_1.Subject();
|
|
487
|
+
}
|
|
488
|
+
listen() {
|
|
489
|
+
const lookup = (0, lookup_1.lookupOf)(this.call.context);
|
|
490
|
+
// Also when it came in before this subscription: the conversation may have started waiting for it.
|
|
491
|
+
const lookup$ = lookup
|
|
492
|
+
? (0, rxjs_1.from)(lookup.promise).pipe((0, rxjs_1.filter)((o) => o != null), (0, rxjs_1.map)((o) => ({ type: 'INPUT_UPDATED', vars: o.vars, result: o.result })))
|
|
493
|
+
: rxjs_1.EMPTY;
|
|
494
|
+
const media$ = this.media.events$.pipe((0, rxjs_1.map)((e) => {
|
|
495
|
+
switch (e.type) {
|
|
496
|
+
case 'EMPTY_EXHAUSTED': return { type: 'EMPTY_EXHAUSTED', errored: e.errored, ...(e.probe != null ? { probe: e.probe } : {}), m: snapshot(e.m) };
|
|
497
|
+
case 'GUARD_REPLACED': return { type: 'GUARD_REPLACED', text: e.text, ...(e.alias ? { alias: e.alias } : {}), ...(e.close ? { close: e.close } : {}), m: snapshot(e.m) };
|
|
498
|
+
case 'REPLY_VARS': return { type: 'REPLY_VARS', vars: e.vars };
|
|
499
|
+
case 'FLAG': return { type: 'FLAG', flag: e.flag, m: snapshot(e.m) };
|
|
500
|
+
case 'TOOL_CALL': return { type: 'TOOL_CALL', name: e.name, m: snapshot(e.m) };
|
|
501
|
+
case 'REPLY_ASR': return { type: 'REPLY_ASR', key: e.key };
|
|
502
|
+
case 'TRANSFER_REPLY': return { type: 'TRANSFER_REPLY', via: e.via, spokeWords: e.spokeWords, ...(e.blank ? { blank: true } : {}), ...(e.trigger != null ? { trigger: e.trigger } : {}), m: snapshot(e.m) };
|
|
503
|
+
case 'TRANSFER_BRIDGED': return { type: 'TRANSFER_BRIDGED' };
|
|
504
|
+
case 'TRANSFER_FAILED': return { type: 'TRANSFER_FAILED', failure: e.failure, m: snapshot(e.m) };
|
|
505
|
+
case 'REVIEW_ACTIONS': return { type: 'REVIEW_ACTIONS', actions: e.actions, m: snapshot(e.m) };
|
|
506
|
+
case 'REPLY_PLAYED': return { type: 'REPLY_PLAYED', text: e.text, heard: e.heard };
|
|
507
|
+
case 'FINAL': return { type: 'CALLER_FINAL', text: e.text, m: snapshot(e.m) };
|
|
508
|
+
case 'FINAL_REWORDED': return { type: 'CALLER_FINAL_REWORDED', text: e.text, virtual: e.virtual };
|
|
509
|
+
case 'DROPPED': return { type: 'CALLER_DROPPED', text: e.text, reason: e.reason, m: snapshot(e.m) };
|
|
510
|
+
case 'SPEECH_START': return { type: 'CALLER_SPEECH_START', m: snapshot(e.m) };
|
|
511
|
+
case 'PARTIAL': return e.isFinal ? null : { type: 'CALLER_PARTIAL', text: e.text, m: snapshot(e.m) };
|
|
512
|
+
case 'DRAINED': return { type: 'AGENT_DRAINED', m: snapshot(e.m) };
|
|
513
|
+
case 'GOODBYE_STARTED': return { type: 'GOODBYE_STARTED', m: snapshot(e.m) };
|
|
514
|
+
case 'SPOKEN': return { type: 'AGENT_SPOKEN', text: e.text, unit: e.unit ?? null };
|
|
515
|
+
case 'RESULT': return { type: 'CALL_RESULT', result: e.result };
|
|
516
|
+
case 'SILENCE': return { type: 'SILENCE_TIMEOUT', timeout: e.timeout, m: snapshot(e.m) };
|
|
517
|
+
case 'AUDIO_DEAD': return { type: 'AUDIO_DEAD', m: snapshot(e.m) };
|
|
518
|
+
case 'NO_PROGRESS': return { type: 'NO_PROGRESS', m: snapshot(e.m) };
|
|
519
|
+
case 'VOICE_ANSWER': return { type: 'VOICE_ANSWER', m: snapshot(e.m) };
|
|
520
|
+
case 'REPLAY_DUE': return { type: 'REPLAY_DUE', m: snapshot(e.m) };
|
|
521
|
+
default: return null;
|
|
522
|
+
}
|
|
523
|
+
}));
|
|
524
|
+
return (0, rxjs_1.merge)(media$.pipe((0, rxjs_1.filter)((e) => e != null)), this.own, lookup$);
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
exports.EngineVoiceSource = EngineVoiceSource;
|
|
528
|
+
exports.EngineVoiceSource = EngineVoiceSource = __decorate([
|
|
529
|
+
(0, decorators_1.Injectable)(),
|
|
530
|
+
__param(0, (0, tsyringe_1.inject)(media_1.EngineMedia)),
|
|
531
|
+
__param(1, (0, tsyringe_1.inject)(call_1.EngineCall)),
|
|
532
|
+
__metadata("design:paramtypes", [media_1.EngineMedia, call_1.EngineCall])
|
|
533
|
+
], EngineVoiceSource);
|
|
534
|
+
/**
|
|
535
|
+
* NLU voicemail detectors on caller partials and finals (`VOICEMAIL { text }`), while detection is on. It reads a final
|
|
536
|
+
* before the conversation does (listed first), so a final its fallback patterns match is voicemail, not a turn.
|
|
537
|
+
*/
|
|
538
|
+
let EngineVoicemailSource = class EngineVoicemailSource {
|
|
539
|
+
constructor(detectors, asr$, media, platform, logger, cfg, dialog) {
|
|
540
|
+
this.detectors = detectors;
|
|
541
|
+
this.asr$ = asr$;
|
|
542
|
+
this.media = media;
|
|
543
|
+
this.platform = platform;
|
|
544
|
+
this.logger = logger;
|
|
545
|
+
this.cfg = cfg;
|
|
546
|
+
this.dialog = dialog;
|
|
547
|
+
this.name = 'engine-voicemail';
|
|
548
|
+
}
|
|
549
|
+
/** Detection runs until the first caller turn (`scope: 'beforeFirstTurn'`) or the whole call. */
|
|
550
|
+
active() {
|
|
551
|
+
const st = this.dialog.state;
|
|
552
|
+
if (st.phase === 'closing')
|
|
553
|
+
return false;
|
|
554
|
+
const when = this.cfg.voicemail?.when;
|
|
555
|
+
if (when && !when(this.dialog.view()))
|
|
556
|
+
return false;
|
|
557
|
+
return this.cfg.voicemail?.scope === 'wholeCall' || st.callerTurns === 0;
|
|
558
|
+
}
|
|
559
|
+
listen() {
|
|
560
|
+
if (!this.detectors.length || !this.cfg.voicemail)
|
|
561
|
+
return rxjs_1.EMPTY;
|
|
562
|
+
return (0, rxjs_1.merge)(...this.detectors.map((d) => this.detector(d)));
|
|
563
|
+
}
|
|
564
|
+
debounced(d) {
|
|
565
|
+
return debouncedVoicemail(d, {
|
|
566
|
+
platform: this.platform,
|
|
567
|
+
logger: this.logger,
|
|
568
|
+
partial$: this.asr$.pipe((0, rxjs_1.switchMap)((asr) => (asr ? asr.partial$ : rxjs_1.EMPTY)), (0, rxjs_1.map)((p) => p.text ?? '')),
|
|
569
|
+
speechStart$: this.asr$.pipe((0, rxjs_1.switchMap)((asr) => (asr?.speechStart$ ? asr.speechStart$ : rxjs_1.EMPTY))),
|
|
570
|
+
active: () => this.active(),
|
|
571
|
+
setGate: (gate) => this.media.setFinalGate(gate),
|
|
572
|
+
onPartialHit: (text) => { if (this.cfg.voicemail?.transcriptLine)
|
|
573
|
+
this.media.transcriptLine('user', text); },
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
detector(d) {
|
|
577
|
+
if (d.debounce)
|
|
578
|
+
return this.debounced(d);
|
|
579
|
+
return new rxjs_1.Observable((sub) => {
|
|
580
|
+
const detector = new voicemail_1.VoicemailDetector(this.platform, this.logger, d.config, (text, source) => {
|
|
581
|
+
// A line caught on a partial never reaches the platform's own call transcript: written there (`transcriptLine`).
|
|
582
|
+
if (source === 'partial' && this.cfg.voicemail?.transcriptLine)
|
|
583
|
+
this.media.transcriptLine('user', text);
|
|
584
|
+
sub.next({ type: 'VOICEMAIL', text });
|
|
585
|
+
}, d.fallbackFirst ? 'first' : 'fallback');
|
|
586
|
+
const subs = [
|
|
587
|
+
...(d.onPartials
|
|
588
|
+
? [this.asr$.pipe((0, rxjs_1.switchMap)((asr) => (asr ? asr.partial$ : rxjs_1.EMPTY))).subscribe((p) => {
|
|
589
|
+
if (this.active())
|
|
590
|
+
detector.onPartial(p.text ?? '');
|
|
591
|
+
})]
|
|
592
|
+
: []),
|
|
593
|
+
this.media.events$.subscribe((e) => {
|
|
594
|
+
if (e.type === 'FINAL' && this.active())
|
|
595
|
+
detector.onFinal(e.text, 'vendor');
|
|
596
|
+
}),
|
|
597
|
+
];
|
|
598
|
+
return () => {
|
|
599
|
+
for (const s of subs)
|
|
600
|
+
s.unsubscribe();
|
|
601
|
+
detector.dispose();
|
|
602
|
+
};
|
|
603
|
+
});
|
|
604
|
+
}
|
|
605
|
+
};
|
|
606
|
+
exports.EngineVoicemailSource = EngineVoicemailSource;
|
|
607
|
+
exports.EngineVoicemailSource = EngineVoicemailSource = __decorate([
|
|
608
|
+
(0, decorators_1.Injectable)(),
|
|
609
|
+
__param(0, (0, tsyringe_1.inject)(ENGINE_DETECTORS)),
|
|
610
|
+
__param(1, (0, tsyringe_1.inject)(active_media_1.ACTIVE_ASR$)),
|
|
611
|
+
__param(2, (0, tsyringe_1.inject)(media_1.EngineMedia)),
|
|
612
|
+
__param(3, (0, tsyringe_1.inject)(tokens_1.PLATFORM)),
|
|
613
|
+
__param(4, (0, tsyringe_1.inject)(tokens_1.LOGGER)),
|
|
614
|
+
__param(5, (0, tsyringe_1.inject)(ENGINE_CONFIG)),
|
|
615
|
+
__param(6, (0, tsyringe_1.inject)(dialog_definition_1.DIALOG_STATE)),
|
|
616
|
+
__metadata("design:paramtypes", [Array, rxjs_1.Observable,
|
|
617
|
+
media_1.EngineMedia, Object, Object, Object, EngineDialogDefinition])
|
|
618
|
+
], EngineVoicemailSource);
|
|
619
|
+
/**
|
|
620
|
+
* The `'debounce'` timing of an NLU voicemail detector: a partial is checked `throttleMs` after the last one, one
|
|
621
|
+
* request at a time; every final is held until its own NLU answer (the media's final gate).
|
|
622
|
+
*/
|
|
623
|
+
function debouncedVoicemail(d, io) {
|
|
624
|
+
const { config } = d;
|
|
625
|
+
const intents = [...(config.intents ?? [])];
|
|
626
|
+
const minChars = config.minChars ?? 0;
|
|
627
|
+
const waitMs = config.throttleMs ?? 0;
|
|
628
|
+
return new rxjs_1.Observable((sub) => {
|
|
629
|
+
let hit = false;
|
|
630
|
+
let disposed = false;
|
|
631
|
+
let pending = '';
|
|
632
|
+
let timer = null;
|
|
633
|
+
let inFlight = false;
|
|
634
|
+
const ask = async (text, source) => {
|
|
635
|
+
if (!io.platform?.nlu) {
|
|
636
|
+
io.logger.log('voicemail_nlu_skipped', { reason: 'no_platform_nlu', source });
|
|
637
|
+
return false;
|
|
638
|
+
}
|
|
639
|
+
try {
|
|
640
|
+
const result = await io.platform.nlu.extract(text, {
|
|
641
|
+
intents, use_neuro_api: config.useNeuroApi ?? false, ...(config.language != null ? { language: config.language } : {}), context: null,
|
|
642
|
+
});
|
|
643
|
+
const detected = intents.some((i) => result.has_intent(i));
|
|
644
|
+
io.logger.log('voicemail_nlu_check', { detected, textLen: text.length, source });
|
|
645
|
+
return detected;
|
|
646
|
+
}
|
|
647
|
+
catch (err) {
|
|
648
|
+
io.logger.log('voicemail_nlu_error', { err: String(err), source });
|
|
649
|
+
return false;
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
const fire = (text) => {
|
|
653
|
+
if (hit || disposed)
|
|
654
|
+
return;
|
|
655
|
+
hit = true;
|
|
656
|
+
sub.next({ type: 'VOICEMAIL', text });
|
|
657
|
+
};
|
|
658
|
+
const clear = () => {
|
|
659
|
+
if (timer != null)
|
|
660
|
+
clearTimeout(timer);
|
|
661
|
+
timer = null;
|
|
662
|
+
};
|
|
663
|
+
const candidate = (text) => text.length >= minChars && /[\p{L}\p{N}]/u.test(text);
|
|
664
|
+
const schedule = () => {
|
|
665
|
+
clear();
|
|
666
|
+
timer = setTimeout(() => void run(), waitMs);
|
|
667
|
+
};
|
|
668
|
+
const run = async () => {
|
|
669
|
+
timer = null;
|
|
670
|
+
if (disposed || hit || !io.active())
|
|
671
|
+
return;
|
|
672
|
+
const text = pending.trim();
|
|
673
|
+
if (!candidate(text))
|
|
674
|
+
return;
|
|
675
|
+
if (inFlight) {
|
|
676
|
+
schedule();
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
inFlight = true;
|
|
680
|
+
try {
|
|
681
|
+
if ((await ask(text, 'partial')) && io.active()) {
|
|
682
|
+
io.onPartialHit(text);
|
|
683
|
+
fire(text);
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
finally {
|
|
687
|
+
inFlight = false;
|
|
688
|
+
}
|
|
689
|
+
};
|
|
690
|
+
const subs = [
|
|
691
|
+
io.speechStart$.subscribe(() => {
|
|
692
|
+
clear();
|
|
693
|
+
pending = '';
|
|
694
|
+
}),
|
|
695
|
+
...(d.onPartials
|
|
696
|
+
? [io.partial$.subscribe((p) => {
|
|
697
|
+
const text = p.trim();
|
|
698
|
+
if (disposed || hit || !io.active() || !candidate(text))
|
|
699
|
+
return;
|
|
700
|
+
pending = text;
|
|
701
|
+
schedule();
|
|
702
|
+
})]
|
|
703
|
+
: []),
|
|
704
|
+
];
|
|
705
|
+
io.setGate(async (text) => {
|
|
706
|
+
if (hit || disposed || !io.active())
|
|
707
|
+
return hit;
|
|
708
|
+
const detected = await ask(text.trim(), 'final');
|
|
709
|
+
if (detected)
|
|
710
|
+
fire(text);
|
|
711
|
+
return detected;
|
|
712
|
+
});
|
|
713
|
+
return () => {
|
|
714
|
+
disposed = true;
|
|
715
|
+
clear();
|
|
716
|
+
io.setGate(null);
|
|
717
|
+
for (const s of subs)
|
|
718
|
+
s.unsubscribe();
|
|
719
|
+
};
|
|
720
|
+
});
|
|
721
|
+
}
|
|
722
|
+
exports.debouncedVoicemail = debouncedVoicemail;
|
|
723
|
+
/** Default time an asynchronous rule / step hook gets to answer. */
|
|
724
|
+
exports.DEFAULT_HOOK_TIMEOUT_MS = 10_000;
|
|
725
|
+
/** `phase: 'answer'` hook steps: started when the live call is answered, before the media (nothing waits for them). */
|
|
726
|
+
let EngineAnswerHooks = class EngineAnswerHooks {
|
|
727
|
+
constructor(context, logger, call, agentHooks) {
|
|
728
|
+
this.context = context;
|
|
729
|
+
this.logger = logger;
|
|
730
|
+
this.call = call;
|
|
731
|
+
this.agentHooks = agentHooks;
|
|
732
|
+
}
|
|
733
|
+
onOnlineCall() {
|
|
734
|
+
const all = this.agentHooks.integrations;
|
|
735
|
+
if (!all?.some((i) => i.phase === 'answer'))
|
|
736
|
+
return;
|
|
737
|
+
const view = this.call.view?.() ?? this.agentHooks.emptyView;
|
|
738
|
+
(0, integrations_1.startAnswerHooks)(all, { context: this.context, channel: this.call.channel, logger: this.logger, call: view, hooks: this.agentHooks.hooks, all });
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
exports.EngineAnswerHooks = EngineAnswerHooks;
|
|
742
|
+
exports.EngineAnswerHooks = EngineAnswerHooks = __decorate([
|
|
743
|
+
(0, decorators_1.Injectable)(),
|
|
744
|
+
(0, hooks_1.PhaseOrder)(-290),
|
|
745
|
+
__param(0, (0, tsyringe_1.inject)(tokens_1.CONTEXT)),
|
|
746
|
+
__param(1, (0, tsyringe_1.inject)(tokens_1.LOGGER)),
|
|
747
|
+
__param(2, (0, tsyringe_1.inject)(call_1.EngineCall)),
|
|
748
|
+
__param(3, (0, tsyringe_1.inject)(ENGINE_HOOKS)),
|
|
749
|
+
__metadata("design:paramtypes", [Object, Object, call_1.EngineCall, Object])
|
|
750
|
+
], EngineAnswerHooks);
|
|
751
|
+
/** The reducer's effects on the media. */
|
|
752
|
+
let EngineVoicePorts = class EngineVoicePorts extends dialog_ports_1.DialogPorts {
|
|
753
|
+
constructor(media, source, platform, logger, dialog, call, agentHooks) {
|
|
754
|
+
super();
|
|
755
|
+
this.media = media;
|
|
756
|
+
this.source = source;
|
|
757
|
+
this.platform = platform;
|
|
758
|
+
this.logger = logger;
|
|
759
|
+
this.dialog = dialog;
|
|
760
|
+
this.call = call;
|
|
761
|
+
this.agentHooks = agentHooks;
|
|
762
|
+
this.hookApiOf = null;
|
|
763
|
+
media.toolGate = (names) => dialog.toolDecision(names);
|
|
764
|
+
call.view = () => dialog.view();
|
|
765
|
+
}
|
|
766
|
+
api() {
|
|
767
|
+
if (this.hookApiOf)
|
|
768
|
+
return this.hookApiOf;
|
|
769
|
+
const { context } = this.call;
|
|
770
|
+
this.hookApiOf = (0, integrations_1.hookApi)({ context, logger: this.logger, hooks: this.agentHooks.hooks, all: this.agentHooks.integrations ?? [] }, this.dialog.view());
|
|
771
|
+
return this.hookApiOf;
|
|
772
|
+
}
|
|
773
|
+
/** A rule's hook on the caller's line: its actions at once, or later (the turn is held meanwhile). */
|
|
774
|
+
runHook(key, name, line) {
|
|
775
|
+
const own = this.source.own;
|
|
776
|
+
const hook = this.agentHooks.hooks[name];
|
|
777
|
+
const invalid = (reason) => this.logger.log('hook_result_invalid', { name, kind: 'action', reason });
|
|
778
|
+
const result = (actions) => own.next({ type: 'HOOK_RESULT', key, actions: (0, hooks_2.hookActions)(actions, this.agentHooks.transfer, invalid), m: snapshot(this.media.snapshot()) });
|
|
779
|
+
if (!hook) {
|
|
780
|
+
this.logger.log('hook_missing', { name });
|
|
781
|
+
result([]);
|
|
782
|
+
return;
|
|
783
|
+
}
|
|
784
|
+
let out;
|
|
785
|
+
try {
|
|
786
|
+
out = hook(this.dialog.view(), line, this.api());
|
|
787
|
+
}
|
|
788
|
+
catch (err) {
|
|
789
|
+
this.logger.log('hook_failed', { name, err: String(err) });
|
|
790
|
+
result([]);
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
if (out != null && typeof out.then === 'function') {
|
|
794
|
+
own.next({ type: 'HOOK_WAITING', key, m: snapshot(this.media.snapshot()) });
|
|
795
|
+
// One answer: the hook's, or none once it ran out of time (a late answer is dropped).
|
|
796
|
+
let settled = false;
|
|
797
|
+
const limitMs = this.agentHooks.timeoutMs ?? exports.DEFAULT_HOOK_TIMEOUT_MS;
|
|
798
|
+
const timer = limitMs > 0
|
|
799
|
+
? setTimeout(() => {
|
|
800
|
+
if (settled)
|
|
801
|
+
return;
|
|
802
|
+
settled = true;
|
|
803
|
+
this.logger.log('hook_failed', { name, err: `timed out after ${limitMs}ms` });
|
|
804
|
+
result([]);
|
|
805
|
+
}, limitMs)
|
|
806
|
+
: undefined;
|
|
807
|
+
out.then((actions) => {
|
|
808
|
+
if (settled)
|
|
809
|
+
return;
|
|
810
|
+
settled = true;
|
|
811
|
+
clearTimeout(timer);
|
|
812
|
+
result(actions);
|
|
813
|
+
}, (err) => {
|
|
814
|
+
if (settled)
|
|
815
|
+
return;
|
|
816
|
+
settled = true;
|
|
817
|
+
clearTimeout(timer);
|
|
818
|
+
this.logger.log('hook_failed', { name, err: String(err) });
|
|
819
|
+
result([]);
|
|
820
|
+
});
|
|
821
|
+
return;
|
|
822
|
+
}
|
|
823
|
+
result(out);
|
|
824
|
+
}
|
|
825
|
+
/** The transfer the reducer started, its target's sources read. */
|
|
826
|
+
async transfer(e) {
|
|
827
|
+
const scope = { context: this.call.context, channel: this.call.channel };
|
|
828
|
+
const str = async (v) => {
|
|
829
|
+
const x = await (0, sources_2.resolveValue)(v, scope);
|
|
830
|
+
return x == null || x === '' ? undefined : String(x);
|
|
831
|
+
};
|
|
832
|
+
let did;
|
|
833
|
+
let msisdn;
|
|
834
|
+
try {
|
|
835
|
+
[did, msisdn] = await Promise.all([str(e.target.did), str(e.target.msisdn)]);
|
|
836
|
+
}
|
|
837
|
+
catch (err) {
|
|
838
|
+
// A target whose source fails: the transfer fails back to the conversation like a target that is not set.
|
|
839
|
+
this.logger.log('transfer_target_failed', { err: String(err) });
|
|
840
|
+
this.media.transferNotConfigured();
|
|
841
|
+
return;
|
|
842
|
+
}
|
|
843
|
+
await this.media.runTransfer({
|
|
844
|
+
mode: e.mode, say: e.say, alias: e.alias, whisper: e.whisper, headers: e.headers, ...(e.clip ? { clip: e.clip } : {}),
|
|
845
|
+
target: {
|
|
846
|
+
...(did ? { did } : {}), ...(msisdn ? { msisdn } : {}), ...(e.target.channel ? { channel: e.target.channel } : {}),
|
|
847
|
+
},
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
async run(effect) {
|
|
851
|
+
const e = effect;
|
|
852
|
+
const { media } = this;
|
|
853
|
+
const lang = (x) => ({ replyLanguage: x.replyLanguage, provisional: x.provisional, ...(x.payload ? { payload: x.payload } : {}) });
|
|
854
|
+
switch (e?.type) {
|
|
855
|
+
case 'log':
|
|
856
|
+
this.logger.log(e.message, e.data);
|
|
857
|
+
return;
|
|
858
|
+
case 'staticTurn':
|
|
859
|
+
media.runStaticTurn(e.text, e.alias, e.opts);
|
|
860
|
+
return;
|
|
861
|
+
case 'pendingTurn':
|
|
862
|
+
media.startPendingTurn();
|
|
863
|
+
return;
|
|
864
|
+
case 'presay':
|
|
865
|
+
media.presay(e.texts);
|
|
866
|
+
return;
|
|
867
|
+
case 'llmTurn':
|
|
868
|
+
media.runLlmTurn(e.held ? { heard: e.text, held: e.held.key, filler: e.held.filler }
|
|
869
|
+
: e.after ? { heard: e.text, after: e.after.key, maxMs: e.after.maxMs } : e.text, 'turn', lang(e), {
|
|
870
|
+
...(e.trigger != null ? { trigger: e.trigger } : {}), ...(e.retry != null ? { retryText: e.retry } : {}),
|
|
871
|
+
});
|
|
872
|
+
return;
|
|
873
|
+
case 'heldText':
|
|
874
|
+
media.provideHeldText(e.key, e.text);
|
|
875
|
+
return;
|
|
876
|
+
case 'runHook':
|
|
877
|
+
this.runHook(e.key, e.name, e.line);
|
|
878
|
+
return;
|
|
879
|
+
case 'transfer':
|
|
880
|
+
void this.transfer(e).catch((err) => this.logger.log('transfer_error', { err: String(err) }));
|
|
881
|
+
return;
|
|
882
|
+
case 'prefetchLlm':
|
|
883
|
+
media.prefetch(e.key, e.text, lang(e), { ...(e.firstTokenMs != null ? { firstTokenMs: e.firstTokenMs } : {}), ...(e.emptyOnError ? { emptyOnError: true } : {}) });
|
|
884
|
+
return;
|
|
885
|
+
case 'playLlm':
|
|
886
|
+
media.playLlm(e.key, e.text, e.alias, lang(e), {
|
|
887
|
+
...(e.firstTokenMs != null ? { firstTokenMs: e.firstTokenMs } : {}), ...(e.emptyOnError ? { emptyOnError: true } : {}),
|
|
888
|
+
...(e.fallback ? { fallback: e.fallback } : {}), ...(e.bargeInAfterMs != null ? { bargeInAfterMs: e.bargeInAfterMs } : {}),
|
|
889
|
+
});
|
|
890
|
+
return;
|
|
891
|
+
case 'playClip':
|
|
892
|
+
media.playClip(e.play, e.text, e.items, e.queued === true);
|
|
893
|
+
return;
|
|
894
|
+
case 'hangupNow':
|
|
895
|
+
// Hung up at once: a line still in flight is not said.
|
|
896
|
+
if (e.tailMs === 0)
|
|
897
|
+
media.cutReply();
|
|
898
|
+
void media.hangupAfterPlayback(e.reason, Promise.resolve(), undefined, e.tailMs);
|
|
899
|
+
return;
|
|
900
|
+
case 'reopen':
|
|
901
|
+
media.cancelHangup(e.reason);
|
|
902
|
+
return;
|
|
903
|
+
case 'timing':
|
|
904
|
+
media.timingOp(e.op, e.value);
|
|
905
|
+
return;
|
|
906
|
+
case 'armReplay':
|
|
907
|
+
media.armReplay(e.ms);
|
|
908
|
+
return;
|
|
909
|
+
case 'voiceAnswer':
|
|
910
|
+
media.voiceAnswerOp(e.op);
|
|
911
|
+
return;
|
|
912
|
+
case 'clearReplay':
|
|
913
|
+
media.clearReplay();
|
|
914
|
+
return;
|
|
915
|
+
case 'cutReply':
|
|
916
|
+
media.cutReply();
|
|
917
|
+
return;
|
|
918
|
+
case 'switchAsr':
|
|
919
|
+
void media.switchTo(e.language).catch((err) => {
|
|
920
|
+
this.logger.log('asr switch error', { language: e.language, err: String(err) });
|
|
921
|
+
});
|
|
922
|
+
return;
|
|
923
|
+
case 'classify':
|
|
924
|
+
this.classify(e.key, e.text, e.intents, e.fillerAfterMs, e.nlu);
|
|
925
|
+
return;
|
|
926
|
+
case 'warmUpNlu': {
|
|
927
|
+
const nlu = this.platform.nlu;
|
|
928
|
+
if (!nlu)
|
|
929
|
+
return;
|
|
930
|
+
const startedAt = Date.now();
|
|
931
|
+
const opts = { intents: [...e.intents], context: null, ...(e.language ? { language: e.language } : {}) };
|
|
932
|
+
void nlu.extract(e.text, opts).then(() => this.logger.log('rule_nlu_warmup', { ms: Date.now() - startedAt }), (err) => this.logger.log('rule_nlu_warmup', { ms: Date.now() - startedAt, err: String(err) }));
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
default:
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
/** NLU of an asynchronous rule, off the effect chain; the answer (and a filler timer) come back as events. */
|
|
940
|
+
classify(key, text, intents, fillerAfterMs, opts) {
|
|
941
|
+
let settled = false;
|
|
942
|
+
const timer = fillerAfterMs != null
|
|
943
|
+
? setTimeout(() => { if (!settled)
|
|
944
|
+
this.source.own.next({ type: 'WAITING_FILLER', key, m: snapshot(this.media.snapshot()) }); }, fillerAfterMs)
|
|
945
|
+
: null;
|
|
946
|
+
// No answer within `timeoutMs` is no match.
|
|
947
|
+
const timeout = opts?.timeoutMs != null
|
|
948
|
+
? setTimeout(() => {
|
|
949
|
+
if (settled)
|
|
950
|
+
return;
|
|
951
|
+
this.logger.log('rule_nlu_timeout', { key, timeoutMs: opts.timeoutMs });
|
|
952
|
+
done([]);
|
|
953
|
+
}, opts.timeoutMs)
|
|
954
|
+
: null;
|
|
955
|
+
const done = (found) => {
|
|
956
|
+
if (settled)
|
|
957
|
+
return;
|
|
958
|
+
settled = true;
|
|
959
|
+
if (timer)
|
|
960
|
+
clearTimeout(timer);
|
|
961
|
+
if (timeout)
|
|
962
|
+
clearTimeout(timeout);
|
|
963
|
+
this.source.own.next({ type: 'CLASSIFIED', key, intents: found, m: snapshot(this.media.snapshot()) });
|
|
964
|
+
};
|
|
965
|
+
if (opts?.normalized) {
|
|
966
|
+
text = (0, match_1.normalizeLine)(text.normalize('NFC'));
|
|
967
|
+
if (!text) {
|
|
968
|
+
done([]);
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
const nlu = this.platform.nlu;
|
|
973
|
+
if (!nlu) {
|
|
974
|
+
this.logger.log('rule_nlu_unavailable', { key });
|
|
975
|
+
done(null);
|
|
976
|
+
return;
|
|
977
|
+
}
|
|
978
|
+
nlu.extract(text, { intents: [...intents], context: null, ...(opts?.language ? { language: opts.language } : {}) }).then((r) => done(intents.filter((i) => r.has_intent(i))), (err) => {
|
|
979
|
+
this.logger.log('rule_nlu_error', { key, err: String(err) });
|
|
980
|
+
done(null);
|
|
981
|
+
});
|
|
982
|
+
}
|
|
983
|
+
};
|
|
984
|
+
exports.EngineVoicePorts = EngineVoicePorts;
|
|
985
|
+
exports.EngineVoicePorts = EngineVoicePorts = __decorate([
|
|
986
|
+
(0, decorators_1.Injectable)(),
|
|
987
|
+
__param(0, (0, tsyringe_1.inject)(media_1.EngineMedia)),
|
|
988
|
+
__param(1, (0, tsyringe_1.inject)(EngineVoiceSource)),
|
|
989
|
+
__param(2, (0, tsyringe_1.inject)(tokens_1.PLATFORM)),
|
|
990
|
+
__param(3, (0, tsyringe_1.inject)(tokens_1.LOGGER)),
|
|
991
|
+
__param(4, (0, tsyringe_1.inject)(dialog_definition_1.DIALOG_STATE)),
|
|
992
|
+
__param(5, (0, tsyringe_1.inject)(call_1.EngineCall)),
|
|
993
|
+
__param(6, (0, tsyringe_1.inject)(ENGINE_HOOKS)),
|
|
994
|
+
__metadata("design:paramtypes", [media_1.EngineMedia,
|
|
995
|
+
EngineVoiceSource, Object, Object, EngineDialogDefinition,
|
|
996
|
+
call_1.EngineCall, Object])
|
|
997
|
+
], EngineVoicePorts);
|
|
998
|
+
/** `closing.guard` as the reply filters run it. */
|
|
999
|
+
function replyGuard(spec, hooks, invalid) {
|
|
1000
|
+
const g = spec.closing.guard;
|
|
1001
|
+
if (!g)
|
|
1002
|
+
return undefined;
|
|
1003
|
+
const lines = (x) => (call) => (typeof x === 'function' ? x(call) : x ?? []);
|
|
1004
|
+
const name = (0, slots_1.hookName)(g.replace) ?? '';
|
|
1005
|
+
const hook = hooks?.[name];
|
|
1006
|
+
if (!hook)
|
|
1007
|
+
throw new Error(`[templates/engine] closing.guard.replace: no hook "${name}"`);
|
|
1008
|
+
const replace = (call, line) => (0, hooks_2.hookText)(hook(call, line, { integration: async () => undefined, log: () => undefined }), (reason) => invalid(name, 'closingGuard', reason));
|
|
1009
|
+
return { closings: lines((0, compiled_1.noData)(g.closings)), openings: lines((0, compiled_1.noData)(g.openings)), holdChars: g.holdChars ?? 60, ...(g.leading ? { leading: g.leading } : {}), replace };
|
|
1010
|
+
}
|
|
1011
|
+
/** `turn.replyPlayed`: the agent's hook as the media calls it (with the call's view and the hook API). */
|
|
1012
|
+
/** @internal exported for tests. */
|
|
1013
|
+
function replyPlayedOf(spec, hooks, view) {
|
|
1014
|
+
const rp = spec.turn?.replyPlayed;
|
|
1015
|
+
if (!rp)
|
|
1016
|
+
return undefined;
|
|
1017
|
+
const hook = hooks?.[rp.hook];
|
|
1018
|
+
if (!hook)
|
|
1019
|
+
throw new Error(`[templates/engine] turn.replyPlayed: no hook "${rp.hook}"`);
|
|
1020
|
+
return (text, heard, { context, logger }) => {
|
|
1021
|
+
const call = view();
|
|
1022
|
+
const out = hook(call, { text, heard }, (0, integrations_1.hookApi)({ context, logger, hooks: hooks ?? {}, all: spec.integrations ?? [] }, call));
|
|
1023
|
+
if (out != null && typeof out.then === 'function') {
|
|
1024
|
+
out.then(undefined, (err) => logger.log('hook_failed', { name: rp.hook, err: String(err) }));
|
|
1025
|
+
}
|
|
1026
|
+
};
|
|
1027
|
+
}
|
|
1028
|
+
exports.replyPlayedOf = replyPlayedOf;
|
|
1029
|
+
/** The agent's own signals sent to the LLM (`prompt.hiddenSignals`). */
|
|
1030
|
+
function signalTexts(spec) {
|
|
1031
|
+
const out = new Set();
|
|
1032
|
+
const sig = spec.prompt.silenceSignal;
|
|
1033
|
+
if (typeof sig === 'string')
|
|
1034
|
+
out.add(sig);
|
|
1035
|
+
else if (sig != null && typeof sig === 'object')
|
|
1036
|
+
for (const x of Object.values(sig))
|
|
1037
|
+
if (typeof x === 'string')
|
|
1038
|
+
out.add(x);
|
|
1039
|
+
for (const st of spec.opening)
|
|
1040
|
+
if (st.step === 'llmGreeting')
|
|
1041
|
+
out.add(st.signal);
|
|
1042
|
+
for (const rung of [...(spec.silence.ladder ?? []), ...(spec.silence.held?.rungs ?? [])]) {
|
|
1043
|
+
for (const a of Array.isArray(rung.do) ? rung.do : rung.do ? [rung.do] : []) {
|
|
1044
|
+
if (a && typeof a === 'object' && 'llm' in a && typeof a.llm === 'string')
|
|
1045
|
+
out.add(a.llm);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
return out;
|
|
1049
|
+
}
|
|
1050
|
+
/** The voice-only respelling: of every line, or only of the conversation's LLM replies (`respellScope`). */
|
|
1051
|
+
function respellOptions(respell, scope, by = 'chunk') {
|
|
1052
|
+
const stream = by === 'word' ? (0, filters_1.respellByWord)(respell) : (t$) => t$.pipe((0, rxjs_1.map)(respell));
|
|
1053
|
+
if (scope === 'all')
|
|
1054
|
+
return { phrase: respell, reply: (t$) => stream(t$) };
|
|
1055
|
+
return { phrase: (text) => text, reply: (t$, tag) => (tag === 'bootstrap' ? t$ : stream(t$)) };
|
|
1056
|
+
}
|
|
1057
|
+
const NO_API = { integration: async () => undefined, log: () => undefined };
|
|
1058
|
+
/** `turn.review` as the filters run it: the agent's hook at the start of a turn and on the held text. */
|
|
1059
|
+
function reviewGate(spec, hooks, invalid) {
|
|
1060
|
+
const name = (0, slots_1.hookName)(spec.turn?.review);
|
|
1061
|
+
if (name == null)
|
|
1062
|
+
return undefined;
|
|
1063
|
+
const hook = hooks?.[name];
|
|
1064
|
+
if (!hook)
|
|
1065
|
+
throw new Error(`[templates/engine] turn.review: no hook "${name}"`);
|
|
1066
|
+
const bad = (reason) => invalid(name, 'review', reason);
|
|
1067
|
+
const transfer = spec.transfer != null;
|
|
1068
|
+
return {
|
|
1069
|
+
start: (view, t) => (0, hooks_2.reviewStart)(hook(view, { stage: 'start', ...t }, NO_API), bad),
|
|
1070
|
+
judge: (view, t) => (0, hooks_2.reviewVerdict)(hook(view, { stage: 'held', ...t }, NO_API), transfer, bad),
|
|
1071
|
+
};
|
|
1072
|
+
}
|
|
1073
|
+
/**
|
|
1074
|
+
* The engine's media hooks of the agent: reply filters and respelling. Compiled once; the filters of one call read
|
|
1075
|
+
* that call's view, keep its scratch and report to its log.
|
|
1076
|
+
*/
|
|
1077
|
+
function voiceHooks(spec, hooks) {
|
|
1078
|
+
const respell = (0, filters_1.respellText)(spec.voice.tts);
|
|
1079
|
+
const ignore = () => undefined;
|
|
1080
|
+
const filters = (0, filters_1.compileReplyFilters)(spec.turn?.filters ?? [], spec.languages, hooks, replyGuard(spec, hooks, ignore) != null, reviewGate(spec, hooks, ignore) != null, spec.transfer != null);
|
|
1081
|
+
return (view, invalid) => callVoiceHooks(spec, respell, filters?.({ view, state: {}, invalid }, replyGuard(spec, hooks, invalid), reviewGate(spec, hooks, invalid)) ?? null);
|
|
1082
|
+
}
|
|
1083
|
+
/** The media hooks of one call: its reply filters (`op`) and the agent's respelling. */
|
|
1084
|
+
function callVoiceHooks(spec, respell, op) {
|
|
1085
|
+
return {
|
|
1086
|
+
...(op
|
|
1087
|
+
? {
|
|
1088
|
+
replyFilter: (turn) => {
|
|
1089
|
+
const seq = turn.seq ?? -1;
|
|
1090
|
+
return {
|
|
1091
|
+
stream: op({
|
|
1092
|
+
seq, tag: turn.id, state: turn.state, ...(turn.text != null ? { trigger: turn.text } : {}), ...(turn.silence ? { silence: true } : {}),
|
|
1093
|
+
...(turn.request != null ? { request: turn.request } : {}), ...(turn.regenerated != null ? { regenerated: turn.regenerated } : {}),
|
|
1094
|
+
}),
|
|
1095
|
+
};
|
|
1096
|
+
},
|
|
1097
|
+
}
|
|
1098
|
+
: {}),
|
|
1099
|
+
...(respell ? { respell: respellOptions(respell, spec.voice.tts.respellScope ?? 'all', spec.voice.tts.respellBy) } : {}),
|
|
1100
|
+
...(spec.voice.tts.sentenceTag ? { respell: withSentenceTag(spec.voice.tts.sentenceTag, respell ? respellOptions(respell, spec.voice.tts.respellScope ?? 'all', spec.voice.tts.respellBy) : null) } : {}),
|
|
1101
|
+
};
|
|
1102
|
+
}
|
|
1103
|
+
/** `voice.tts.sentenceTag` after the respelling: the tag in front of every sentence of a line and of a reply. */
|
|
1104
|
+
function withSentenceTag(tag, inner) {
|
|
1105
|
+
return {
|
|
1106
|
+
phrase: (text) => (0, sentence_tag_1.tagText)(inner ? inner.phrase(text) : text, tag),
|
|
1107
|
+
reply: (t$, kind) => (inner ? inner.reply(t$, kind) : t$).pipe((0, sentence_tag_1.tagSentences)(tag)),
|
|
1108
|
+
};
|
|
1109
|
+
}
|
|
1110
|
+
/**
|
|
1111
|
+
* The whole agent from its agent file (internal: an agent calls `.build()` of its builder, which calls this). Throws at
|
|
1112
|
+
* build time when the file uses a block the engine does not run (the message names the field).
|
|
1113
|
+
*/
|
|
1114
|
+
function defineVoiceAgent(agentFile, agentHooks = agentFile.hooks, engineOptions = {}) {
|
|
1115
|
+
// The data forms of the agent file (conditions, expressions) become the functions of their slots; data in hook
|
|
1116
|
+
// slots becomes generated hooks. Then blocks with their own hooks (`knowledgeAnswer`, `catalogMatch`) are wired in.
|
|
1117
|
+
const compiled = (0, slots_2.compileDataForms)(agentFile, agentHooks);
|
|
1118
|
+
const { file, hooks } = (0, expand_1.withBlocks)(compiled.spec, compiled.hooks);
|
|
1119
|
+
// Every hook the agent file names must be there (an agent file typed `AgentSpec<typeof hooks>` is checked by the
|
|
1120
|
+
// compiler already).
|
|
1121
|
+
(0, hooks_2.checkHookNames)(file, hooks);
|
|
1122
|
+
const spec = (0, config_1.withConversation)(file);
|
|
1123
|
+
// Built once (data forms, matchers, the business plan); the parts that read a call's state are built per call
|
|
1124
|
+
// (`callScript`), so calls run from one built script at once never see each other's view, scratch or log.
|
|
1125
|
+
// A hook's answer of the wrong shape is logged on the call that got it and ignored. The reducer's own hooks
|
|
1126
|
+
// (`overtalk`) run inside one call's `reduce`: they report to the call reducing now.
|
|
1127
|
+
let reducing = null;
|
|
1128
|
+
const reducerInvalid = (name, kind, reason) => reducing?.log('hook_result_invalid', { name, kind, reason });
|
|
1129
|
+
const business = (0, adapter_1.callPlanFromSpec)(spec, hooks);
|
|
1130
|
+
const baseRule = business.resultRule;
|
|
1131
|
+
const resultRuleOf = (view) => (baseRule
|
|
1132
|
+
? (at, facts) => {
|
|
1133
|
+
// The rules read the state the call's hooks keep (`results.fromFacts` see `call.hookState`): inside the reducer
|
|
1134
|
+
// its snapshot, after the call the hooks' state itself.
|
|
1135
|
+
const hookState = facts.hookState ?? view()?.hookState;
|
|
1136
|
+
return baseRule(at, { ...facts, ...(hookState ? { hookState } : {}) });
|
|
1137
|
+
}
|
|
1138
|
+
: undefined);
|
|
1139
|
+
const cfg = (0, config_1.engineDialogConfig)(spec, resultRuleOf(() => null), hooks, reducerInvalid);
|
|
1140
|
+
// Checked here so a bad goodbye matcher fails at build time.
|
|
1141
|
+
(0, match_1.compileMatcher)(spec.closing.goodbye, spec.languages, 'closing.goodbye');
|
|
1142
|
+
const media = mediaSettings(spec, cfg);
|
|
1143
|
+
const names = spec.prompt.languageNames ?? {};
|
|
1144
|
+
const engine = {
|
|
1145
|
+
records: recordNames(spec),
|
|
1146
|
+
...(spec.voice.recordFlag ? { recordFlag: spec.voice.recordFlag } : {}),
|
|
1147
|
+
...(spec.voice.ambient?.record ? { ambientRecord: spec.voice.ambient.record } : {}),
|
|
1148
|
+
llmDialogPerCall: (spec.platform.llmDialogPerCall ?? true) !== false,
|
|
1149
|
+
...(spec.extraction?.namesDialog ? { extractionNamesDialog: true } : {}),
|
|
1150
|
+
fromInvite: spec.platform.outboundMode === 'from_invite' || spec.platform.followLeg === 'script',
|
|
1151
|
+
languageName: (code) => names[code] ?? code,
|
|
1152
|
+
...(spec.voice.tts.variants?.length
|
|
1153
|
+
? { voiceTag: (callUuid, vars) => (0, variants_1.callVoiceVariant)(spec.voice.tts, callUuid, vars)?.tag ?? null }
|
|
1154
|
+
: {}),
|
|
1155
|
+
...((spec.report.service || undefined)?.callStatus === 'signals' ? { callStatusFromSignals: true } : {}),
|
|
1156
|
+
...((spec.report.service || undefined)?.source === 'script' ? { scriptServiceFields: true } : {}),
|
|
1157
|
+
...(cfg.transfer
|
|
1158
|
+
? { transfer: { skipExtraction: cfg.transfer.whileBridged.skipExtraction, entities: cfg.transfer.entities ?? {}, ...(spec.transfer?.extract?.length ? { extract: spec.transfer.extract } : {}) } }
|
|
1159
|
+
: {}),
|
|
1160
|
+
};
|
|
1161
|
+
const fallbackView = { language: spec.languages[0], vars: {}, flags: {}, counters: {}, lines: [], agentLines: [], callerLines: [] };
|
|
1162
|
+
const voiceOf = voiceHooks(spec, hooks);
|
|
1163
|
+
const annotate = cfg.voice.annotatePerRequest ? cfg.voice.annotateCaller : undefined;
|
|
1164
|
+
const rt = cfg.voice.state;
|
|
1165
|
+
const barge = media.bargeIn;
|
|
1166
|
+
const eot = media.eot;
|
|
1167
|
+
const empty = spec.turn?.regenerateEmpty;
|
|
1168
|
+
const vetoName = (0, slots_1.hookName)(spec.transfer?.veto);
|
|
1169
|
+
const vetoHook = vetoName != null ? hooks?.[vetoName] : undefined;
|
|
1170
|
+
if (vetoName != null && !vetoHook)
|
|
1171
|
+
throw new Error(`[templates/engine] transfer.veto: no hook "${vetoName}"`);
|
|
1172
|
+
const toolName = (0, slots_1.hookName)(spec.transfer?.toolReply);
|
|
1173
|
+
const toolHook = toolName != null ? hooks?.[toolName] : undefined;
|
|
1174
|
+
if (toolName != null && !toolHook)
|
|
1175
|
+
throw new Error(`[templates/engine] transfer.toolReply: no hook "${toolName}"`);
|
|
1176
|
+
const detectors = nluDetectors(spec);
|
|
1177
|
+
/** The script of one call: its dialog, and the media hooks and result rule that read that dialog's view. */
|
|
1178
|
+
const callScript = () => {
|
|
1179
|
+
// The instance the host drives is the one the reply filters read (the container may build another).
|
|
1180
|
+
let current = null;
|
|
1181
|
+
let callLogger = null;
|
|
1182
|
+
const invalid = (name, kind, reason) => callLogger?.log('hook_result_invalid', { name, kind, reason });
|
|
1183
|
+
const viewNow = () => current?.view() ?? fallbackView;
|
|
1184
|
+
const resultRule = resultRuleOf(() => current?.view() ?? null);
|
|
1185
|
+
const plan = { ...business, voice: voiceOf(viewNow, invalid) };
|
|
1186
|
+
const played = replyPlayedOf(spec, hooks, viewNow);
|
|
1187
|
+
// Verdicts of agent code the media asks for at the end of a reply.
|
|
1188
|
+
const engineMedia = {
|
|
1189
|
+
...media,
|
|
1190
|
+
...(played ? { replyPlayed: played } : {}),
|
|
1191
|
+
...(annotate != null ? { annotateAgain: (line) => (0, match_1.resolveText)(annotate, viewNow(), line) ?? line } : {}),
|
|
1192
|
+
...(annotate == null && cfg.voice.annotatePerRequest && rt?.notes
|
|
1193
|
+
? { annotateAgain: (line) => (0, state_1.noteAgain)(rt, viewNow(), current?.state ?? null, line) }
|
|
1194
|
+
: {}),
|
|
1195
|
+
...((0, state_1.tracksPlayed)(rt) ? { trackPlayed: true } : {}),
|
|
1196
|
+
...(cfg.voice.promptVars ? { promptVarsNow: () => cfg.voice.promptVars(viewNow()) } : {}),
|
|
1197
|
+
...(empty ? { regenerateEmpty: (trigger) => (0, match_1.resolveText)(empty.signal, viewNow(), trigger) } : {}),
|
|
1198
|
+
...(vetoHook
|
|
1199
|
+
? {
|
|
1200
|
+
transferVeto: (trigger) => (0, hooks_2.hookText)(vetoHook(viewNow(), trigger, NO_API), (reason) => invalid(vetoName, 'transferVeto', reason)),
|
|
1201
|
+
}
|
|
1202
|
+
: {}),
|
|
1203
|
+
...(toolHook
|
|
1204
|
+
? {
|
|
1205
|
+
toolReply: (text, next, goodbye) => (0, hooks_2.toolReplyVerdict)(toolHook(viewNow(), { text, next, goodbye }, NO_API), (reason) => invalid(toolName, 'toolReply', reason)),
|
|
1206
|
+
}
|
|
1207
|
+
: {}),
|
|
1208
|
+
};
|
|
1209
|
+
let CallDialog = class CallDialog extends EngineDialogDefinition {
|
|
1210
|
+
constructor(c, call) {
|
|
1211
|
+
super(c, call);
|
|
1212
|
+
current = this;
|
|
1213
|
+
callLogger = call.logger;
|
|
1214
|
+
}
|
|
1215
|
+
reduce(s, e) {
|
|
1216
|
+
current = this;
|
|
1217
|
+
const outer = reducing;
|
|
1218
|
+
reducing = callLogger;
|
|
1219
|
+
try {
|
|
1220
|
+
return super.reduce(s, e);
|
|
1221
|
+
}
|
|
1222
|
+
finally {
|
|
1223
|
+
reducing = outer;
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
};
|
|
1227
|
+
CallDialog = __decorate([
|
|
1228
|
+
(0, decorators_1.Injectable)(),
|
|
1229
|
+
__param(0, (0, tsyringe_1.inject)(ENGINE_CONFIG)),
|
|
1230
|
+
__param(1, (0, tsyringe_1.inject)(call_1.EngineCall)),
|
|
1231
|
+
__metadata("design:paramtypes", [Object, call_1.EngineCall])
|
|
1232
|
+
], CallDialog);
|
|
1233
|
+
let EngineCallModule = class EngineCallModule {
|
|
1234
|
+
};
|
|
1235
|
+
EngineCallModule = __decorate([
|
|
1236
|
+
(0, decorators_1.Module)({
|
|
1237
|
+
providers: [
|
|
1238
|
+
{ provide: call_1.ENGINE_PLAN, useValue: plan },
|
|
1239
|
+
{ provide: call_1.ENGINE_AGENT, useValue: resultRule ? { ...engine, resultRule } : engine },
|
|
1240
|
+
{ provide: ENGINE_CONFIG, useValue: resultRule ? { ...cfg, resultRule } : cfg },
|
|
1241
|
+
{ provide: media_1.ENGINE_MEDIA, useValue: engineMedia },
|
|
1242
|
+
{ provide: ENGINE_DETECTORS, useValue: detectors },
|
|
1243
|
+
{ provide: ENGINE_HOOKS, useValue: { hooks: hooks ?? {}, integrations: spec.integrations, emptyView: fallbackView, transfer: spec.transfer != null, ...(engineOptions.hookTimeoutMs != null ? { timeoutMs: engineOptions.hookTimeoutMs } : {}) } },
|
|
1244
|
+
call_1.EngineCall,
|
|
1245
|
+
EngineAnswerHooks,
|
|
1246
|
+
media_1.EngineMedia,
|
|
1247
|
+
EngineVoiceSource,
|
|
1248
|
+
report_1.CallReport,
|
|
1249
|
+
{ provide: report_1.REPORTED_CALL, useExisting: call_1.EngineCall },
|
|
1250
|
+
{ provide: active_media_1.ASR_RESULT_FILTERS, useValue: { fillerPattern: eot.dropLine, minPeakVad: eot.minPeakVad, minPeakVadExcept: eot.minPeakVadExcept, ...(eot.minPeakVadOver ? { minPeakVadOver: eot.minPeakVadOver } : {}) } },
|
|
1251
|
+
// The pre-call pass returns its row too (`report.headlessRow: 'failedDial'`).
|
|
1252
|
+
...(spec.report.headlessRow === 'failedDial' ? [{ provide: dump_policy_1.DUMP_POLICY, useValue: 'always' }] : []),
|
|
1253
|
+
],
|
|
1254
|
+
})
|
|
1255
|
+
], EngineCallModule);
|
|
1256
|
+
let EngineAgentModule = class EngineAgentModule {
|
|
1257
|
+
};
|
|
1258
|
+
EngineAgentModule = __decorate([
|
|
1259
|
+
(0, decorators_1.AgentModule)({
|
|
1260
|
+
imports: [
|
|
1261
|
+
EngineCallModule,
|
|
1262
|
+
lifecycle_1.LifecycleModule.forRoot({
|
|
1263
|
+
// The max-duration timer hangs up at once; lines hang up through the media (after their drain and grace).
|
|
1264
|
+
hangup: lifecycle_1.HangupStrategy.immediate({ rtpTailMs: 0 }),
|
|
1265
|
+
maxDurationMs: spec.platform.maxCallMs,
|
|
1266
|
+
...(spec.platform.maxCallGraceMs ? { maxDurationTailMs: spec.platform.maxCallGraceMs } : {}),
|
|
1267
|
+
ringingTimeoutMs: plan.ringingTimeoutMs,
|
|
1268
|
+
}),
|
|
1269
|
+
barge_in_module_1.BargeInModule.forRoot({
|
|
1270
|
+
bindings: [{
|
|
1271
|
+
asr: call_voice_1.VOICE_ASR,
|
|
1272
|
+
queues: [media.voice.queues.tts],
|
|
1273
|
+
strategy: barge.enabled
|
|
1274
|
+
? barge_in_strategy_1.BargeIn.selective({
|
|
1275
|
+
interruptPatterns: [...barge.interruptPatterns],
|
|
1276
|
+
minVadProbability: barge.minVadProbability,
|
|
1277
|
+
partialMinChars: barge.partialMinChars,
|
|
1278
|
+
...(barge.lengthTriggersInterrupt ? { lengthTriggersInterrupt: true } : {}),
|
|
1279
|
+
...(barge.whileSilent ? { stopWhenSilent: true } : {}),
|
|
1280
|
+
...(barge.countLetters ? { countLetters: true } : {}),
|
|
1281
|
+
mode: 'stop',
|
|
1282
|
+
})
|
|
1283
|
+
: barge_in_strategy_1.BargeIn.off(),
|
|
1284
|
+
}],
|
|
1285
|
+
}),
|
|
1286
|
+
virtual_finals_module_1.VirtualFinalsModule.forRoot({
|
|
1287
|
+
strategy: virtual_finals_1.VirtualFinalsStrategy.debounced({
|
|
1288
|
+
debounceMs: eot.finalizeDebounceMs,
|
|
1289
|
+
quietWindowMs: eot.quietWindowMs,
|
|
1290
|
+
skipWhen: eot.holdOn,
|
|
1291
|
+
}),
|
|
1292
|
+
}),
|
|
1293
|
+
],
|
|
1294
|
+
phases: {
|
|
1295
|
+
onlineCall: online_call_module_1.OnlineCallModule.forRoot({
|
|
1296
|
+
state: CallDialog,
|
|
1297
|
+
ports: EngineVoicePorts,
|
|
1298
|
+
startEvent: true,
|
|
1299
|
+
sources: [(0, define_module_1.defineEventModule)({ sources: [EngineVoicemailSource, EngineVoiceSource] })],
|
|
1300
|
+
}),
|
|
1301
|
+
},
|
|
1302
|
+
})
|
|
1303
|
+
], EngineAgentModule);
|
|
1304
|
+
return (0, create_agent_script_1.createAgentScript)(EngineAgentModule);
|
|
1305
|
+
};
|
|
1306
|
+
// Built once here too, so a block the engine cannot wire fails at build time.
|
|
1307
|
+
callScript();
|
|
1308
|
+
const script = (ctx) => callScript()(ctx);
|
|
1309
|
+
const plan = business;
|
|
1310
|
+
const run = (0, agent_sdk_1.defineScript)(async (ctx) => {
|
|
1311
|
+
const { context, channel, logger, platform } = ctx;
|
|
1312
|
+
const payload = await plan.openPass(ctx);
|
|
1313
|
+
const cancelled = await plan.gatePass(ctx, payload);
|
|
1314
|
+
if (cancelled)
|
|
1315
|
+
return cancelled;
|
|
1316
|
+
// Voice values read from sources: this call's, before its media starts.
|
|
1317
|
+
if (media.sourcedVoice && !context.headless)
|
|
1318
|
+
(0, media_1.keepResolvedVoice)(context, await resolveVoice(media.sourcedVoice, { context, channel }, logger));
|
|
1319
|
+
if (spec.input.lookup && !context.headless) {
|
|
1320
|
+
// Started now, not awaited: the lookup runs while the opening's first clip plays.
|
|
1321
|
+
(0, lookup_1.startLookup)(spec, hooks, plan.callVars, context, channel, logger, payload);
|
|
1322
|
+
}
|
|
1323
|
+
// A leg that never connected is reported by its own `after_call_failed` pass (`platform.failedLegPass`).
|
|
1324
|
+
if (!plan.onHeadless)
|
|
1325
|
+
return script(ctx);
|
|
1326
|
+
if (plan.scheduleOutbound)
|
|
1327
|
+
await plan.scheduleOutbound({ context, logger, platform, channel }, payload);
|
|
1328
|
+
if (context.headless)
|
|
1329
|
+
return (await plan.onHeadless({ context, logger, platform }, payload)) ?? undefined;
|
|
1330
|
+
const result = await script(ctx);
|
|
1331
|
+
// A live pass that wrote no row (a leg left to `after_call_failed`) returns nothing.
|
|
1332
|
+
const out = result?.output;
|
|
1333
|
+
return out && Object.keys(out).length > 0 ? result : undefined;
|
|
1334
|
+
});
|
|
1335
|
+
return plan.outboundCallMode ? (0, agent_sdk_1.defineScript)(run, { outboundCallMode: plan.outboundCallMode }) : run;
|
|
1336
|
+
}
|
|
1337
|
+
exports.defineVoiceAgent = defineVoiceAgent;
|