@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,1240 @@
|
|
|
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.EngineMedia = exports.ENGINE_MEDIA = exports.withResolvedVoice = exports.keepResolvedVoice = void 0;
|
|
16
|
+
// The engine's call media: the shared call media (../../voice/call-voice) with the engine's own turns — clips (a CMS
|
|
17
|
+
// record, a URL, or TTS of the clip's text, then its tail) and LLM lines requested early and played later (the eager
|
|
18
|
+
// greeting) — and its reply source: one LLM request per turn on the call's LLM dialog, or one stream kept open for the
|
|
19
|
+
// call (`prompt.persistentStream`).
|
|
20
|
+
const tsyringe_1 = require("tsyringe");
|
|
21
|
+
const rxjs_1 = require("rxjs");
|
|
22
|
+
const decorators_1 = require("../../core/decorators");
|
|
23
|
+
const hooks_1 = require("../../core/hooks");
|
|
24
|
+
const tokens_1 = require("../../core/tokens");
|
|
25
|
+
const active_media_1 = require("../../dialog-events/active-media");
|
|
26
|
+
const lifecycle_1 = require("../../lifecycle/lifecycle");
|
|
27
|
+
const call_voice_1 = require("../../voice/call-voice");
|
|
28
|
+
const transfer_1 = require("../../voice/transfer");
|
|
29
|
+
const intent_interrupt_1 = require("../../voice/intent-interrupt");
|
|
30
|
+
const llm_tap_1 = require("../../call/llm-tap");
|
|
31
|
+
const call_1 = require("./call");
|
|
32
|
+
const match_1 = require("./match");
|
|
33
|
+
const variants_1 = require("./variants");
|
|
34
|
+
const resolvedVoices = new WeakMap();
|
|
35
|
+
/** Keep the call's resolved voice values (by its dialog context) for its media. */
|
|
36
|
+
function keepResolvedVoice(context, v) {
|
|
37
|
+
resolvedVoices.set(context, v);
|
|
38
|
+
}
|
|
39
|
+
exports.keepResolvedVoice = keepResolvedVoice;
|
|
40
|
+
/** The media settings with the call's resolved voice values. */
|
|
41
|
+
function withResolvedVoice(base, v) {
|
|
42
|
+
if (!v)
|
|
43
|
+
return base;
|
|
44
|
+
const tts = (p) => {
|
|
45
|
+
const cfg = { ...(p.ttsConfig ?? {}) };
|
|
46
|
+
if (v.voiceId != null)
|
|
47
|
+
cfg['voice_id'] = v.voiceId;
|
|
48
|
+
if (v.model != null)
|
|
49
|
+
cfg['model_id'] = v.model;
|
|
50
|
+
return { ...p, ttsConfig: cfg };
|
|
51
|
+
};
|
|
52
|
+
const session = base.ttsSession;
|
|
53
|
+
return {
|
|
54
|
+
...base,
|
|
55
|
+
play: { ...base.play, llm: tts(base.play.llm), greeting: tts(base.play.greeting) },
|
|
56
|
+
ttsSession: session
|
|
57
|
+
? { ...session, data: { ...session.data, ...(v.voiceId != null ? { voice_id: v.voiceId } : {}), ...(v.model != null ? { model_id: v.model } : {}) } }
|
|
58
|
+
: session,
|
|
59
|
+
asr: { ...base.asr, ...(v.asrVendor != null ? { vendor: v.asrVendor } : {}) },
|
|
60
|
+
audio: { ...base.audio, ...(v.ambientUrl != null ? { ambientLoopUrl: v.ambientUrl } : {}) },
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
exports.withResolvedVoice = withResolvedVoice;
|
|
64
|
+
const isHeld = (t) => typeof t === 'object' && 'held' in t;
|
|
65
|
+
exports.ENGINE_MEDIA = 'TEMPLATES_ENGINE_MEDIA';
|
|
66
|
+
const isPrefetched = (t) => typeof t === 'object' && 'prefetched' in t;
|
|
67
|
+
const isAfterPrefetch = (t) => typeof t === 'object' && 'after' in t;
|
|
68
|
+
let EngineMedia = class EngineMedia extends call_voice_1.CallVoice {
|
|
69
|
+
constructor(channel, logger, am, engineCall, lifecycle, settings) {
|
|
70
|
+
super(channel, logger, am, engineCall, lifecycle);
|
|
71
|
+
this.engineCall = engineCall;
|
|
72
|
+
this.settings = settings;
|
|
73
|
+
/** The reducer's answer to a tool call of the reply in flight (set by the ports). */
|
|
74
|
+
this.toolGate = null;
|
|
75
|
+
this.latched = new Set();
|
|
76
|
+
/** The next turn's reply carries a tool call latched on an empty reply asked again. */
|
|
77
|
+
this.latchNextTurn = false;
|
|
78
|
+
/** `voice.asrByReply`: the key the replies last read as, and the recognizer key the call listens with. */
|
|
79
|
+
this.replyKey = null;
|
|
80
|
+
this.listenKey = null;
|
|
81
|
+
this.refused = new Set();
|
|
82
|
+
this.cutLogged = new Set();
|
|
83
|
+
this.errored = new Set();
|
|
84
|
+
/** Text each turn sent to the LLM. */
|
|
85
|
+
this.sent = new Map();
|
|
86
|
+
this.heldTexts = new Map();
|
|
87
|
+
/** The transfer took the call over. */
|
|
88
|
+
this.transferring = false;
|
|
89
|
+
/** `scriptLeak.retry` was used; a reply with words resets it. */
|
|
90
|
+
this.leakRetried = false;
|
|
91
|
+
/** The agent queue ran dry (`turn.playedOut: { by: 'queueIdle' }`). */
|
|
92
|
+
this.queueEmptied = new rxjs_1.Subject();
|
|
93
|
+
this.transferPlayedOut = null;
|
|
94
|
+
this.dial = null;
|
|
95
|
+
this.bridgeTeardown = null;
|
|
96
|
+
this.prefetched = new Map();
|
|
97
|
+
this.persistentHandle = null;
|
|
98
|
+
/** The turn whose barge-in block ends when it drains (a clip or the greeting). */
|
|
99
|
+
this.blockingSeq = -1;
|
|
100
|
+
this.greetingAlias = 'greeting';
|
|
101
|
+
/** How the current greeting's stream ends, its fallback clip, whether the caller can cut it. */
|
|
102
|
+
this.greetingOpts = {};
|
|
103
|
+
this.greeting = null;
|
|
104
|
+
this.cutDrained = -1;
|
|
105
|
+
this.fillerTimer = null;
|
|
106
|
+
this.fillerSeq = -1;
|
|
107
|
+
/** Tool calls of replies already told to the conversation (`<turn>:<name>`). */
|
|
108
|
+
this.toolsSeen = new Set();
|
|
109
|
+
/** When the last turn-end sound played (0: never). */
|
|
110
|
+
this.voiceOf = null;
|
|
111
|
+
/** The caller's first speech run (`monologue` detector): from answer, its start, the last end, closed. */
|
|
112
|
+
this.run = { t0: Date.now(), start: null, lastEnd: null, closed: false, flagged: false };
|
|
113
|
+
this.intents = null;
|
|
114
|
+
this.vendorFinalInTranscript = settings.vendorFinalInTranscript === true;
|
|
115
|
+
// Who ended the call, for the facts after it (`call.endedBy`).
|
|
116
|
+
engineCall.whoEnded = () => (lifecycle.maxDurationHit ? 'timeout' : lifecycle.agentHangup ? 'agent' : 'caller');
|
|
117
|
+
// A `stream` reply filter hook's `clip` plays through here.
|
|
118
|
+
this.filterState['playClip'] = (play) => {
|
|
119
|
+
if (this.terminated || this.callEnding)
|
|
120
|
+
return;
|
|
121
|
+
const url = play.url != null ? (0, variants_1.withVoice)(play.url, this.voiceTag) : undefined;
|
|
122
|
+
if (url)
|
|
123
|
+
void this.channel.audio.play(url, { queue: this.agent.queues.tts, alias: play.alias ?? 'reply_clip' }).catch(() => undefined);
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/** `voice.asrByReply`: the reply names the caller's language; the recognizer follows it. */
|
|
127
|
+
replyProgress(textSoFar) {
|
|
128
|
+
const f = this.settings.asrByReply;
|
|
129
|
+
if (!f)
|
|
130
|
+
return;
|
|
131
|
+
const key = f.detect(textSoFar);
|
|
132
|
+
if (key == null)
|
|
133
|
+
return;
|
|
134
|
+
if (key !== this.replyKey) {
|
|
135
|
+
this.replyKey = key;
|
|
136
|
+
this.emit({ type: 'REPLY_ASR', key });
|
|
137
|
+
}
|
|
138
|
+
if (this.callEnding || this.terminated)
|
|
139
|
+
return;
|
|
140
|
+
const current = this.listenKey ?? this.agent.asr.defaultLanguage;
|
|
141
|
+
if (key === current || !this.agent.asr.languages[key])
|
|
142
|
+
return;
|
|
143
|
+
if (f.noReturnTo != null && key === f.noReturnTo && current !== f.noReturnTo)
|
|
144
|
+
return;
|
|
145
|
+
this.listenKey = key;
|
|
146
|
+
this.logger.log('asr_language_switch_requested', { from: current, to: key, replyLen: textSoFar.length });
|
|
147
|
+
void this.switchTo(key).catch((err) => {
|
|
148
|
+
this.listenKey = current;
|
|
149
|
+
this.logger.log('asr_language_switch_failed', { from: current, to: key, err: String(err) });
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/** The media settings with the call's voice variant (its voice and model on every TTS play). */
|
|
153
|
+
get agent() {
|
|
154
|
+
if (this.voiceOf)
|
|
155
|
+
return this.voiceOf.agent;
|
|
156
|
+
const agent = this.callAgent();
|
|
157
|
+
const st = this.settings.silenceText;
|
|
158
|
+
const made = this.voiceOf;
|
|
159
|
+
if (st != null && made) {
|
|
160
|
+
// The silence signal of the moment: the reply language's, with the call's variables.
|
|
161
|
+
const view = () => this.engineCall.view?.() ?? { language: this.settings.defaultLanguage, vars: {}, flags: {}, counters: {}, lines: [], agentLines: [], callerLines: [] };
|
|
162
|
+
const withSignal = Object.defineProperty({ ...agent }, 'silenceEllipsis', { get: () => (0, match_1.resolveText)(st, view()) ?? '', enumerable: true });
|
|
163
|
+
this.voiceOf = { variant: made.variant, agent: withSignal };
|
|
164
|
+
return withSignal;
|
|
165
|
+
}
|
|
166
|
+
return agent;
|
|
167
|
+
}
|
|
168
|
+
callAgent() {
|
|
169
|
+
const vars = this.engineCall.payload?.vars ?? null;
|
|
170
|
+
const variant = this.settings.variant?.(this.engineCall.callUuid, vars) ?? null;
|
|
171
|
+
const base = withResolvedVoice(this.settings.voice, this.engineCall.context ? resolvedVoices.get(this.engineCall.context) : undefined);
|
|
172
|
+
if (!variant) {
|
|
173
|
+
this.voiceOf = { variant: null, agent: base };
|
|
174
|
+
return base;
|
|
175
|
+
}
|
|
176
|
+
const tts = (p) => {
|
|
177
|
+
const cfg = {
|
|
178
|
+
...(p.ttsConfig ?? {}), voice_id: variant.voiceId, ...(variant.model ? { model_id: variant.model } : {}),
|
|
179
|
+
...(variant.settings ?? {}),
|
|
180
|
+
};
|
|
181
|
+
return { ...p, ...(variant.strategy ? { ttsStrategy: variant.strategy } : {}), ttsConfig: cfg };
|
|
182
|
+
};
|
|
183
|
+
const session = base.ttsSession;
|
|
184
|
+
const agent = {
|
|
185
|
+
...base,
|
|
186
|
+
play: { ...base.play, llm: tts(base.play.llm), greeting: tts(base.play.greeting) },
|
|
187
|
+
ttsSession: variant.session === false ? null : session ? { ...session, data: { ...session.data, voice_id: variant.voiceId, ...(variant.model ? { model_id: variant.model } : {}) } } : session,
|
|
188
|
+
};
|
|
189
|
+
this.voiceOf = { variant, agent };
|
|
190
|
+
return agent;
|
|
191
|
+
}
|
|
192
|
+
asrPick(language) {
|
|
193
|
+
const pick = this.settings.asrPick?.[language];
|
|
194
|
+
if (!pick)
|
|
195
|
+
return null;
|
|
196
|
+
const vars = (this.engineCall.payload?.vars) ?? {};
|
|
197
|
+
const p = pick(vars);
|
|
198
|
+
return p ? { ...(p.name ? { name: p.name } : {}), ...(p.data ? { data: { ...p.data } } : {}) } : null;
|
|
199
|
+
}
|
|
200
|
+
/** Speech starts and partials reach the conversation when it reads them (`turn.carryOn`). */
|
|
201
|
+
get speechEvents() {
|
|
202
|
+
return this.settings.speechEvents === true;
|
|
203
|
+
}
|
|
204
|
+
/** Tag of the call's voice variant (`null`: none). */
|
|
205
|
+
get voiceTag() {
|
|
206
|
+
void this.agent;
|
|
207
|
+
return this.voiceOf?.variant?.tag ?? null;
|
|
208
|
+
}
|
|
209
|
+
setup() {
|
|
210
|
+
return {
|
|
211
|
+
defaultLanguage: this.settings.defaultLanguage,
|
|
212
|
+
llmPayload: this.engineCall.setup.llmPayload,
|
|
213
|
+
...(this.settings.speechFilter ? { speechFilter: this.speechFilter(this.settings.speechFilter) } : {}),
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
speechFilter(f) {
|
|
217
|
+
return typeof f === 'function' ? f(this.engineCall.view ? this.engineCall.view() : null) : f;
|
|
218
|
+
}
|
|
219
|
+
/** A reply asked again after a cut is asked with the prompt variables as they are now (the cut may have changed them). */
|
|
220
|
+
abortedRetryLanguage(language) {
|
|
221
|
+
const now = this.settings.promptVarsNow;
|
|
222
|
+
if (!now || !language.payload)
|
|
223
|
+
return language;
|
|
224
|
+
return { ...language, payload: { ...language.payload, ...now() } };
|
|
225
|
+
}
|
|
226
|
+
/** `annotateCaller.perRequest`: a reply asked again after a cut is asked with the caller's words annotated anew. */
|
|
227
|
+
abortedRetryText(text, opts) {
|
|
228
|
+
const again = this.settings.annotateAgain;
|
|
229
|
+
if (!again || opts.trigger == null || opts.trigger === this.agent.silenceEllipsis)
|
|
230
|
+
return text;
|
|
231
|
+
return again(opts.trigger);
|
|
232
|
+
}
|
|
233
|
+
/** `turn.replyPlayed`: the agent's hook hears what the caller heard of a conversation reply (never the greeting). */
|
|
234
|
+
replyPlayed(text, heard, tag) {
|
|
235
|
+
if (tag === 'bootstrap' || this.terminated)
|
|
236
|
+
return;
|
|
237
|
+
if (this.settings.trackPlayed)
|
|
238
|
+
this.emit({ type: 'REPLY_PLAYED', text, heard });
|
|
239
|
+
const fn = this.settings.replyPlayed;
|
|
240
|
+
if (!fn)
|
|
241
|
+
return;
|
|
242
|
+
try {
|
|
243
|
+
fn(text, heard, { context: this.engineCall.context, logger: this.logger });
|
|
244
|
+
}
|
|
245
|
+
catch (err) {
|
|
246
|
+
this.logger.log('hook_failed', { name: 'replyPlayed', err: String(err) });
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
/** The agent file's prompt variables on the call's payload (no reply-language keys of their own). */
|
|
250
|
+
turnPayload(language) {
|
|
251
|
+
return { ...this.setup().llmPayload, ...(language.payload ?? {}) };
|
|
252
|
+
}
|
|
253
|
+
startBackground() {
|
|
254
|
+
const { channel, logger } = this;
|
|
255
|
+
try {
|
|
256
|
+
const sfx = this.settings.sfx;
|
|
257
|
+
if (sfx)
|
|
258
|
+
channel.audio.queue(sfx.queue).volume = sfx.volume;
|
|
259
|
+
const url = this.agent.audio.ambientLoopUrl;
|
|
260
|
+
if (url) {
|
|
261
|
+
void channel.audio.play(url, this.agent.play.ambient).catch((err) => logger.log('ambient play error', { err: String(err) }));
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
// A CMS record (when the agent file names one): played once its row is found.
|
|
265
|
+
void this.engineCall.ambient().then((row) => {
|
|
266
|
+
if (!row || this.terminated || this.callEnding)
|
|
267
|
+
return;
|
|
268
|
+
return channel.audio.play(row, this.agent.play.ambient);
|
|
269
|
+
}).catch((err) => logger.log('ambient play error', { err: String(err) }));
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
catch (err) {
|
|
273
|
+
logger.warn('engine: background audio failed', { error: String(err) });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
heldBack(seq) {
|
|
277
|
+
const take = (key) => {
|
|
278
|
+
const h = this.filterState[key];
|
|
279
|
+
delete this.filterState[key];
|
|
280
|
+
return h && h.seq === seq ? h.text : '';
|
|
281
|
+
};
|
|
282
|
+
return take('heldBack:review') + take('heldBack:cut');
|
|
283
|
+
}
|
|
284
|
+
partialHeard(text) {
|
|
285
|
+
const cfg = this.settings.intentInterrupt;
|
|
286
|
+
if (!cfg)
|
|
287
|
+
return;
|
|
288
|
+
this.intents ??= new intent_interrupt_1.IntentInterrupt(this.engineCall.platform, this.logger, cfg, () => ({ speaking: this.agentSpeaking, startedAt: this.playbackStartedAtMs }), (intent, partial) => this.forceCut('interrupt_intent', { matchedIntent: intent, partialText: partial }));
|
|
289
|
+
this.intents.onPartial(text);
|
|
290
|
+
}
|
|
291
|
+
teardown() {
|
|
292
|
+
this.intents?.dispose();
|
|
293
|
+
// The transfer's progress is what the row reports: a dial that resumes after the hangup does not change it.
|
|
294
|
+
this.engineCall.transferProgress.closed = true;
|
|
295
|
+
this.clearFiller();
|
|
296
|
+
this.dial?.dispose();
|
|
297
|
+
this.stopSfx();
|
|
298
|
+
super.teardown();
|
|
299
|
+
}
|
|
300
|
+
stopSfx() {
|
|
301
|
+
const sfx = this.settings.sfx;
|
|
302
|
+
if (sfx) {
|
|
303
|
+
try {
|
|
304
|
+
this.channel.audio.stop(sfx.queue);
|
|
305
|
+
}
|
|
306
|
+
catch { /* ignore */ }
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
get callEnding() {
|
|
310
|
+
return this.ending || this.transferring;
|
|
311
|
+
}
|
|
312
|
+
onCallDestroy() {
|
|
313
|
+
try {
|
|
314
|
+
this.bridgeTeardown?.();
|
|
315
|
+
}
|
|
316
|
+
catch { /* best effort */ }
|
|
317
|
+
this.bridgeTeardown = null;
|
|
318
|
+
for (const p of this.prefetched.values())
|
|
319
|
+
p.sub.unsubscribe();
|
|
320
|
+
this.prefetched.clear();
|
|
321
|
+
try {
|
|
322
|
+
this.persistentHandle?.disconnect();
|
|
323
|
+
}
|
|
324
|
+
catch { /* ignore */ }
|
|
325
|
+
this.persistentHandle = null;
|
|
326
|
+
super.onCallDestroy();
|
|
327
|
+
}
|
|
328
|
+
// ── replies ──────────────────────────────────────────────────────────────────
|
|
329
|
+
llmOptions(payload) {
|
|
330
|
+
return { agentUuid: this.engineCall.agentUuid, dialogUuid: this.engineCall.llmDialogUuid, payload };
|
|
331
|
+
}
|
|
332
|
+
/** Text of one LLM request of turn `seq` (its tool and error events read); unsubscribing cancels it. */
|
|
333
|
+
llmText(text, payload, seq) {
|
|
334
|
+
const hidden = this.settings.hiddenTexts;
|
|
335
|
+
const isHidden = hidden != null && (hidden.has(text) || (this.settings.silenceText != null && text === this.agent.silenceEllipsis));
|
|
336
|
+
const opts = hidden ? { ...this.llmOptions(payload), hidden: isHidden } : this.llmOptions(payload);
|
|
337
|
+
const llm = this.channel.llm;
|
|
338
|
+
const p = this.settings.persistent;
|
|
339
|
+
let chunks$;
|
|
340
|
+
if (p && typeof llm.makePersistentStream === 'function') {
|
|
341
|
+
chunks$ = new rxjs_1.Observable((subscriber) => {
|
|
342
|
+
this.persistentHandle ??= llm.makePersistentStream({ agentUuid: opts.agentUuid, dialogUuid: opts.dialogUuid });
|
|
343
|
+
const handle = this.persistentHandle.send(text, opts);
|
|
344
|
+
let done = false;
|
|
345
|
+
const sub = handle.stream$.subscribe({
|
|
346
|
+
next: (c) => subscriber.next(c),
|
|
347
|
+
error: (err) => { done = true; subscriber.error(err); },
|
|
348
|
+
complete: () => { done = true; subscriber.complete(); },
|
|
349
|
+
});
|
|
350
|
+
return () => {
|
|
351
|
+
sub.unsubscribe();
|
|
352
|
+
if (!done) {
|
|
353
|
+
try {
|
|
354
|
+
handle.abort();
|
|
355
|
+
}
|
|
356
|
+
catch { /* ignore */ }
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
else {
|
|
362
|
+
chunks$ = llm.stream(text, opts);
|
|
363
|
+
}
|
|
364
|
+
if (seq != null)
|
|
365
|
+
this.sent.set(seq, text);
|
|
366
|
+
return chunks$.pipe((0, rxjs_1.tap)((c) => { if (seq != null)
|
|
367
|
+
this.toolEvents(seq, c); }),
|
|
368
|
+
// finishReason closes the stream; the SSE socket otherwise stays open a few seconds after the last token.
|
|
369
|
+
(0, rxjs_1.takeWhile)((c) => c.finishReason == null, true), (0, rxjs_1.map)((c) => c.content ?? ''), (0, rxjs_1.filter)((c) => c.length > 0));
|
|
370
|
+
}
|
|
371
|
+
/** An empty reply whose tool call latched is asked again: the retry's reply carries the tool call (it is the transfer). */
|
|
372
|
+
emptyReplyAskedAgain(seq) {
|
|
373
|
+
if (!this.latched.has(seq))
|
|
374
|
+
return;
|
|
375
|
+
this.latched.delete(seq);
|
|
376
|
+
this.latchNextTurn = true;
|
|
377
|
+
this.logger.log('transfer_tool_carried_to_retry', { seq });
|
|
378
|
+
}
|
|
379
|
+
replyStream(turn, seq, payload) {
|
|
380
|
+
if (this.latchNextTurn) {
|
|
381
|
+
this.latchNextTurn = false;
|
|
382
|
+
this.latched.add(seq);
|
|
383
|
+
}
|
|
384
|
+
if (isPrefetched(turn))
|
|
385
|
+
return this.prefetchedStream(turn.prefetched, turn.heard, payload);
|
|
386
|
+
if (isAfterPrefetch(turn)) {
|
|
387
|
+
// The prefetched stream (still in the LLM's history) ends first, so the two requests do not overlap.
|
|
388
|
+
const p = this.prefetched.get(turn.after);
|
|
389
|
+
const reply$ = (0, rxjs_1.defer)(() => this.llmText(turn.heard, payload, seq));
|
|
390
|
+
if (!p)
|
|
391
|
+
return reply$;
|
|
392
|
+
const ended$ = p.stream$.pipe((0, rxjs_1.ignoreElements)(), (0, rxjs_1.catchError)(() => rxjs_1.EMPTY), (0, rxjs_1.takeUntil)((0, rxjs_1.timer)(turn.maxMs)));
|
|
393
|
+
return (0, rxjs_1.concat)(ended$, reply$);
|
|
394
|
+
}
|
|
395
|
+
if (isHeld(turn))
|
|
396
|
+
return this.heldReply(turn, seq, payload);
|
|
397
|
+
return this.llmText(typeof turn === 'string' ? turn : turn.heard, payload, seq);
|
|
398
|
+
}
|
|
399
|
+
/** Tool and error events of a chunk of turn `seq`: the transfer's tool latched or refused (by the reducer). */
|
|
400
|
+
toolEvents(seq, chunk) {
|
|
401
|
+
const names = (0, llm_tap_1.extractToolNames)(chunk);
|
|
402
|
+
if (!names.length)
|
|
403
|
+
return;
|
|
404
|
+
const err = this.settings.errorEvent;
|
|
405
|
+
if (err != null && names.includes(err))
|
|
406
|
+
this.errored.add(seq);
|
|
407
|
+
if (this.settings.toolRules && this.isCurrent(seq)) {
|
|
408
|
+
for (const name of names) {
|
|
409
|
+
const key = `${seq}:${name}`;
|
|
410
|
+
if (this.toolsSeen.has(key))
|
|
411
|
+
continue;
|
|
412
|
+
this.toolsSeen.add(key);
|
|
413
|
+
this.emit({ type: 'TOOL_CALL', name, m: this.snapshot() });
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (!this.isCurrent(seq) || !this.toolGate || this.latched.has(seq))
|
|
417
|
+
return;
|
|
418
|
+
const decision = this.toolGate(names);
|
|
419
|
+
if (decision === 'latched') {
|
|
420
|
+
this.latched.add(seq);
|
|
421
|
+
this.logger.log('transfer_tool_detected', { names });
|
|
422
|
+
}
|
|
423
|
+
else if (decision === 'refused') {
|
|
424
|
+
this.refused.add(seq);
|
|
425
|
+
this.logger.log('transfer_tool_refused_no_request', { names });
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
// ── the opening monologue ────────────────────────────────────────────────────
|
|
429
|
+
async onOnlineCall() {
|
|
430
|
+
this.run.t0 = Date.now();
|
|
431
|
+
await super.onOnlineCall();
|
|
432
|
+
}
|
|
433
|
+
speechStarted() {
|
|
434
|
+
const m = this.settings.monologue;
|
|
435
|
+
const r = this.run;
|
|
436
|
+
if (!m || r.closed)
|
|
437
|
+
return;
|
|
438
|
+
const t = Date.now() - r.t0;
|
|
439
|
+
if (r.start === null) {
|
|
440
|
+
if (t > m.startWithinMs)
|
|
441
|
+
this.closeRun('late_start', t);
|
|
442
|
+
else
|
|
443
|
+
r.start = t;
|
|
444
|
+
return;
|
|
445
|
+
}
|
|
446
|
+
if (r.lastEnd !== null && t - r.lastEnd > m.mergeGapMs) {
|
|
447
|
+
this.closeRun('gap', t);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
r.lastEnd = null;
|
|
451
|
+
}
|
|
452
|
+
speechEnded() {
|
|
453
|
+
const m = this.settings.monologue;
|
|
454
|
+
const r = this.run;
|
|
455
|
+
if (!m || r.closed || r.start === null)
|
|
456
|
+
return;
|
|
457
|
+
const t = Date.now() - r.t0;
|
|
458
|
+
r.lastEnd = t;
|
|
459
|
+
if (!r.flagged && t - r.start >= m.minSpanMs) {
|
|
460
|
+
r.flagged = true;
|
|
461
|
+
this.logger.log('opening_monologue_suspect', { startMs: r.start, spanMs: t - r.start });
|
|
462
|
+
this.emit({ type: 'FLAG', flag: m.flag, m: this.snapshot() });
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
closeRun(reason, atMs) {
|
|
466
|
+
const r = this.run;
|
|
467
|
+
r.closed = true;
|
|
468
|
+
this.logger.log('opening_speech_run', { reason, atMs, startMs: r.start, spanMs: r.start !== null && r.lastEnd !== null ? r.lastEnd - r.start : null, suspect: r.flagged });
|
|
469
|
+
}
|
|
470
|
+
// ── the slow-reply filler ────────────────────────────────────────────────────
|
|
471
|
+
replyRequested(seq, tag) {
|
|
472
|
+
this.clearFiller();
|
|
473
|
+
const f = this.settings.filler;
|
|
474
|
+
if (!f || tag === 'bootstrap' || !f.urls.length)
|
|
475
|
+
return;
|
|
476
|
+
this.fillerSeq = seq;
|
|
477
|
+
this.fillerTimer = setTimeout(() => {
|
|
478
|
+
this.fillerTimer = null;
|
|
479
|
+
if (!this.isCurrent(seq) || this.callEnding || this.terminated || this.llmWaitingSince === 0 || this.agentSpeaking)
|
|
480
|
+
return;
|
|
481
|
+
const url = (0, variants_1.withVoice)(f.urls[Math.floor(Math.random() * f.urls.length)], this.voiceTag);
|
|
482
|
+
this.logger.log('reply_filler_played', { tag, msAfterTurnStart: f.afterMs });
|
|
483
|
+
void this.channel.audio.play(url, { queue: this.agent.queues.tts, alias: f.alias }).catch(() => undefined);
|
|
484
|
+
}, f.afterMs);
|
|
485
|
+
}
|
|
486
|
+
replyStarted(seq) {
|
|
487
|
+
if (seq === this.fillerSeq)
|
|
488
|
+
this.clearFiller();
|
|
489
|
+
}
|
|
490
|
+
clearFiller() {
|
|
491
|
+
if (this.fillerTimer != null)
|
|
492
|
+
clearTimeout(this.fillerTimer);
|
|
493
|
+
this.fillerTimer = null;
|
|
494
|
+
}
|
|
495
|
+
/** The held turn's reply: its text once the reducer built it, a filler meanwhile. */
|
|
496
|
+
heldReply(turn, seq, payload) {
|
|
497
|
+
const textP = new Promise((resolve) => this.heldTexts.set(turn.held, resolve));
|
|
498
|
+
const text$ = (0, rxjs_1.from)(textP);
|
|
499
|
+
const reply$ = text$.pipe((0, rxjs_1.filter)(() => this.isCurrent(seq) && !this.terminated), (0, rxjs_1.switchMap)((text) => (text ? this.llmText(text, payload, seq) : rxjs_1.EMPTY)));
|
|
500
|
+
if (turn.filler == null)
|
|
501
|
+
return reply$;
|
|
502
|
+
const { text: filler, afterMs } = turn.filler;
|
|
503
|
+
return (0, rxjs_1.merge)((0, rxjs_1.timer)(afterMs).pipe((0, rxjs_1.map)(() => filler), (0, rxjs_1.takeUntil)(text$)), reply$);
|
|
504
|
+
}
|
|
505
|
+
/** The reducer built a held turn's text (empty: the turn is not asked). */
|
|
506
|
+
provideHeldText(key, text) {
|
|
507
|
+
const resolve = this.heldTexts.get(key);
|
|
508
|
+
this.heldTexts.delete(key);
|
|
509
|
+
resolve?.(text);
|
|
510
|
+
}
|
|
511
|
+
replyCut(seq, tag, soFar, next) {
|
|
512
|
+
const tr = this.settings.transfer ? this.settings.toolReply : undefined;
|
|
513
|
+
if (tr && this.latched.has(seq) && !this.cutLogged.has(seq)) {
|
|
514
|
+
// With the tool latched the agent's code says where the reply is over.
|
|
515
|
+
if (tr(soFar, next, false) === 'over') {
|
|
516
|
+
this.cutLogged.add(seq);
|
|
517
|
+
this.logger.log('reply_cut_with_tool', { tag, replyLen: soFar.length });
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
else if (tr && this.cutLogged.has(seq) && this.latched.has(seq))
|
|
522
|
+
return true;
|
|
523
|
+
if (!this.refused.has(seq))
|
|
524
|
+
return false;
|
|
525
|
+
if (!this.cutLogged.has(seq)) {
|
|
526
|
+
this.cutLogged.add(seq);
|
|
527
|
+
this.logger.log('reply_cut_after_refused_tool', { tag, replyLen: soFar.length });
|
|
528
|
+
}
|
|
529
|
+
return true;
|
|
530
|
+
}
|
|
531
|
+
replyExempt(seq) {
|
|
532
|
+
// A reply whose words the `sentences` filter took out is not an empty reply.
|
|
533
|
+
return this.refused.has(seq) || this.filterState['sentencesDropped'] === seq;
|
|
534
|
+
}
|
|
535
|
+
replyErrored(seq) {
|
|
536
|
+
return this.errored.has(seq);
|
|
537
|
+
}
|
|
538
|
+
sentText(turn, seq) {
|
|
539
|
+
return this.sent.get(seq) ?? (typeof turn === 'string' ? turn : turn.heard);
|
|
540
|
+
}
|
|
541
|
+
/** A refused tool call that took the whole reply: asked once more with the refused signal. */
|
|
542
|
+
// ── queue-idle drain (`turn.playedOut: { by: 'queueIdle' }`) ─────────────────────────────
|
|
543
|
+
watchQueue(ttsQueue) {
|
|
544
|
+
if (!this.settings.queueIdle)
|
|
545
|
+
return;
|
|
546
|
+
this.subs.push(ttsQueue.queueEmpty$.subscribe(() => {
|
|
547
|
+
this.pending = 0;
|
|
548
|
+
this.queueEmptied.next();
|
|
549
|
+
}));
|
|
550
|
+
}
|
|
551
|
+
closeCeilingFor(text) {
|
|
552
|
+
return this.settings.queueIdle ? this.drainCeilingFor(text) : undefined;
|
|
553
|
+
}
|
|
554
|
+
async awaitAfterFloor(text, ceilingMs, seq) {
|
|
555
|
+
if (!this.settings.queueIdle) {
|
|
556
|
+
await this.awaitTtsQueueDrain(ceilingMs);
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
const queueIdle = await this.awaitQueueIdle(this.settings.queueIdle, (0, call_voice_1.spokenLength)(text), ceilingMs, () => !this.isCurrent(seq));
|
|
560
|
+
this.logger.log('perf', { event: 'tts_queue_idle', queueIdle });
|
|
561
|
+
}
|
|
562
|
+
async awaitQueueIdle(q, spokenChars, ceilingMs, isCancelled) {
|
|
563
|
+
if (!this.queueEventsObserved)
|
|
564
|
+
return 'unobserved';
|
|
565
|
+
const deadline = Date.now() + ceilingMs;
|
|
566
|
+
const remaining = () => Math.max(0, deadline - Date.now());
|
|
567
|
+
if (this.turnFirstItemStartedAt == null) {
|
|
568
|
+
const signal = await this.nextQueueSignal({ started: true }, Math.min(q.startAllowanceMs, remaining()));
|
|
569
|
+
if (signal === 'terminated')
|
|
570
|
+
return 'terminated';
|
|
571
|
+
if (isCancelled())
|
|
572
|
+
return 'cancelled';
|
|
573
|
+
if (signal === 'timeout')
|
|
574
|
+
return 'no_playback';
|
|
575
|
+
}
|
|
576
|
+
const startedAt = this.turnFirstItemStartedAt ?? Date.now();
|
|
577
|
+
const idleNotBefore = startedAt + spokenChars * q.msPerChar;
|
|
578
|
+
for (;;) {
|
|
579
|
+
if (isCancelled())
|
|
580
|
+
return 'cancelled';
|
|
581
|
+
if (remaining() === 0)
|
|
582
|
+
return 'timeout';
|
|
583
|
+
if (this.pending > 0) {
|
|
584
|
+
const signal = await this.nextQueueSignal({ emptied: true }, remaining());
|
|
585
|
+
if (signal === 'terminated')
|
|
586
|
+
return 'terminated';
|
|
587
|
+
continue;
|
|
588
|
+
}
|
|
589
|
+
const holdMs = Math.max(q.settleMs, idleNotBefore - Date.now());
|
|
590
|
+
const signal = await this.nextQueueSignal({ started: true }, Math.min(holdMs, remaining()));
|
|
591
|
+
if (signal === 'terminated')
|
|
592
|
+
return 'terminated';
|
|
593
|
+
if (signal === 'started')
|
|
594
|
+
continue;
|
|
595
|
+
if (isCancelled())
|
|
596
|
+
return 'cancelled';
|
|
597
|
+
if (Date.now() < idleNotBefore)
|
|
598
|
+
continue;
|
|
599
|
+
return 'idle';
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
nextQueueSignal(watch, timeoutMs) {
|
|
603
|
+
const sources = [];
|
|
604
|
+
if (watch.started === true)
|
|
605
|
+
sources.push(this.itemStarted.pipe((0, rxjs_1.map)(() => 'started')));
|
|
606
|
+
if (watch.emptied === true)
|
|
607
|
+
sources.push(this.queueEmptied.pipe((0, rxjs_1.map)(() => 'emptied')));
|
|
608
|
+
sources.push(this.channel.events.terminated$.pipe((0, rxjs_1.map)(() => 'terminated')));
|
|
609
|
+
sources.push((0, rxjs_1.timer)(Math.max(0, timeoutMs)).pipe((0, rxjs_1.map)(() => 'timeout')));
|
|
610
|
+
// A source that completes without a value (a channel closed after the hangup) counts as the call's end.
|
|
611
|
+
return (0, rxjs_1.firstValueFrom)((0, rxjs_1.race)(sources), { defaultValue: 'terminated' });
|
|
612
|
+
}
|
|
613
|
+
/** `voice.dropLineAnswer`: a bare «yes» dropped as a filler is the answer when nothing plays and the line is not held. */
|
|
614
|
+
rescueDropped(text, reason) {
|
|
615
|
+
const answer = this.settings.dropLineAnswer;
|
|
616
|
+
if (reason !== 'filler' || answer == null || !answer.test(text) || this.agentSpeaking || this.timing.awaitingHandover)
|
|
617
|
+
return false;
|
|
618
|
+
this.logger.log('asr_filler_rescued_as_answer', { text });
|
|
619
|
+
this.emitFinal(text);
|
|
620
|
+
return true;
|
|
621
|
+
}
|
|
622
|
+
replyRetried(end) {
|
|
623
|
+
const note = this.settings.leakRetry;
|
|
624
|
+
if (note != null) {
|
|
625
|
+
// A reply that was only dropped sentences (the model's notes): asked again once with the note.
|
|
626
|
+
if (end.aggregated.trim().length === 0 && end.suppressed.length > 0) {
|
|
627
|
+
if (!this.leakRetried && !this.callEnding && typeof end.turn === 'string') {
|
|
628
|
+
this.leakRetried = true;
|
|
629
|
+
this.logger.log('agent_reply_leak_retry', { tag: end.tag, msAfterTurnStart: Date.now() - end.turnStart });
|
|
630
|
+
this.runLlmTurn(`${note}\n${end.turn}`, end.tag, end.language);
|
|
631
|
+
return true;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
else if (end.aggregated.trim().length > 0) {
|
|
635
|
+
this.leakRetried = false;
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
const g = this.greeting;
|
|
639
|
+
if (end.tag === 'bootstrap' && g && g.seq === end.seq && g.fallback && end.aggregated.trim().length === 0 && !this.callEnding) {
|
|
640
|
+
this.playGreetingFallback(end, g.fallback);
|
|
641
|
+
return true;
|
|
642
|
+
}
|
|
643
|
+
if (this.reviewed(end))
|
|
644
|
+
return true;
|
|
645
|
+
const clip = this.filterState['asClip'];
|
|
646
|
+
if (clip && clip.seq === end.seq) {
|
|
647
|
+
delete this.filterState['asClip'];
|
|
648
|
+
if (this.callEnding)
|
|
649
|
+
return true;
|
|
650
|
+
this.playReplyClip(clip, end);
|
|
651
|
+
return true;
|
|
652
|
+
}
|
|
653
|
+
const rv = this.filterState['replyVars'];
|
|
654
|
+
if (rv && rv.seq === end.seq) {
|
|
655
|
+
delete this.filterState['replyVars'];
|
|
656
|
+
if (end.aggregated.trim().length > 0)
|
|
657
|
+
this.emit({ type: 'REPLY_VARS', vars: rv.vars });
|
|
658
|
+
}
|
|
659
|
+
const vetoed = this.filterState['guardReplaced'];
|
|
660
|
+
if (vetoed && vetoed.seq === end.seq) {
|
|
661
|
+
delete this.filterState['guardReplaced'];
|
|
662
|
+
if (this.callEnding)
|
|
663
|
+
return true;
|
|
664
|
+
this.resetEmptyCounts();
|
|
665
|
+
this.logger.log('closing_vetoed', { tag: end.tag, replacement: vetoed.text, msAfterTurnStart: Date.now() - end.turnStart });
|
|
666
|
+
this.emit({ type: 'GUARD_REPLACED', text: vetoed.text, ...(vetoed.alias ? { alias: vetoed.alias } : {}), ...(vetoed.close ? { close: vetoed.close } : {}), m: this.snapshot() });
|
|
667
|
+
return true;
|
|
668
|
+
}
|
|
669
|
+
if (this.vetoed(end) || this.regeneratedEmpty(end))
|
|
670
|
+
return true;
|
|
671
|
+
const signal = this.settings.transfer?.refusedSignal;
|
|
672
|
+
if (!this.refused.has(end.seq) || end.opts?.refusedRetry || signal == null || end.aggregated.trim().length > 0 || this.callEnding)
|
|
673
|
+
return false;
|
|
674
|
+
this.logger.log('transfer_refused_turn_regenerated', { tag: end.tag, msAfterTurnStart: Date.now() - end.turnStart });
|
|
675
|
+
this.runLlmTurn(signal, end.tag, end.language, { keepTrigger: true, refusedRetry: true });
|
|
676
|
+
return true;
|
|
677
|
+
}
|
|
678
|
+
/** A regeneration of the turn that ended: its text, the turn's trigger kept, the reason carried. */
|
|
679
|
+
regenerate(end, text, reason) {
|
|
680
|
+
this.logger.log('reply_regenerated', { reason, tag: end.tag, msAfterTurnStart: Date.now() - end.turnStart, reply: end.aggregated.slice(0, 120) });
|
|
681
|
+
this.runLlmTurn(text, end.tag, end.language, { keepTrigger: true, regenerated: reason });
|
|
682
|
+
}
|
|
683
|
+
/** `cutAtSentence` and `turn.review`: a reply cut with nothing spoken, or dropped by the review (true: handled). */
|
|
684
|
+
reviewed(end) {
|
|
685
|
+
const cut = this.filterState['cutAtSentence'];
|
|
686
|
+
if (cut && cut.seq === end.seq) {
|
|
687
|
+
delete this.filterState['cutAtSentence'];
|
|
688
|
+
if (cut.cut) {
|
|
689
|
+
this.logger.log('reply_cut_at_sentence', { tag: end.tag, spokenLen: cut.spoken.length });
|
|
690
|
+
if (!cut.spoken.trim() && cut.regenerate && !end.opts?.regenerated && !this.latched.has(end.seq) && !this.callEnding) {
|
|
691
|
+
this.regenerate(end, cut.regenerate, 'cutAtSentence');
|
|
692
|
+
return true;
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
const review = this.filterState['review'];
|
|
697
|
+
if (!review || review.seq !== end.seq)
|
|
698
|
+
return false;
|
|
699
|
+
delete this.filterState['review'];
|
|
700
|
+
if (this.callEnding)
|
|
701
|
+
return false;
|
|
702
|
+
// Nothing of the reply was spoken: its tool call goes with it.
|
|
703
|
+
this.latched.delete(end.seq);
|
|
704
|
+
if ('regenerate' in review.verdict) {
|
|
705
|
+
this.regenerate(end, review.verdict.regenerate, 'review');
|
|
706
|
+
return true;
|
|
707
|
+
}
|
|
708
|
+
this.logger.log('reply_review_actions', { tag: end.tag, msAfterTurnStart: Date.now() - end.turnStart });
|
|
709
|
+
this.emit({ type: 'REVIEW_ACTIONS', actions: review.verdict.then, m: this.snapshot() });
|
|
710
|
+
return true;
|
|
711
|
+
}
|
|
712
|
+
/** `transfer.veto`: the tool latched on a reply that said no words, and the agent refuses it (true: asked again). */
|
|
713
|
+
vetoed(end) {
|
|
714
|
+
const veto = this.settings.transferVeto;
|
|
715
|
+
if (!veto || !this.latched.has(end.seq) || end.opts?.regenerated === 'veto' || this.callEnding)
|
|
716
|
+
return false;
|
|
717
|
+
if (/[\p{L}\p{N}]/u.test(end.aggregated))
|
|
718
|
+
return false;
|
|
719
|
+
const text = veto(this.lastTurnTriggerText ?? '');
|
|
720
|
+
if (text == null)
|
|
721
|
+
return false;
|
|
722
|
+
this.latched.delete(end.seq);
|
|
723
|
+
this.logger.log('transfer_vetoed', { tag: end.tag, callerText: this.lastTurnTriggerText });
|
|
724
|
+
this.regenerate(end, text, 'veto');
|
|
725
|
+
return true;
|
|
726
|
+
}
|
|
727
|
+
/** `turn.regenerateEmpty`: a first-round reply with no words and no transfer is asked again once (true: asked). */
|
|
728
|
+
regeneratedEmpty(end) {
|
|
729
|
+
const again = this.settings.regenerateEmpty;
|
|
730
|
+
if (!again || end.tag === 'bootstrap' || end.aggregated.trim().length > 0 || end.opts?.regenerated || this.latched.has(end.seq) || this.callEnding)
|
|
731
|
+
return false;
|
|
732
|
+
const text = again(this.lastTurnTriggerText ?? '');
|
|
733
|
+
if (text == null)
|
|
734
|
+
return false;
|
|
735
|
+
this.regenerate(end, text, 'empty');
|
|
736
|
+
return true;
|
|
737
|
+
}
|
|
738
|
+
// ── the transfer ─────────────────────────────────────────────────────────────
|
|
739
|
+
isTransferReply(text) {
|
|
740
|
+
const t = this.settings.transfer;
|
|
741
|
+
if (!t || this.transferring)
|
|
742
|
+
return false;
|
|
743
|
+
const tr = this.settings.toolReply;
|
|
744
|
+
if (tr && this.latched.has(this.turnSeq) && tr(text, null, this.agent.conversation.endsWithGoodbyeMarker(text)) === 'drop') {
|
|
745
|
+
// The agent's code says the reply is no transfer: the tool call is dropped.
|
|
746
|
+
this.latched.delete(this.turnSeq);
|
|
747
|
+
this.logger.log('transfer_tool_ignored', { replyTail: text.slice(-80) });
|
|
748
|
+
}
|
|
749
|
+
return this.latched.has(this.turnSeq) || (t.spoken?.(text) ?? false);
|
|
750
|
+
}
|
|
751
|
+
replyDoneLogFields(isTransfer) {
|
|
752
|
+
return this.settings.transfer ? { isTransfer } : {};
|
|
753
|
+
}
|
|
754
|
+
/** The reply is the transfer: the conversation is over; the reducer starts it (`runTransfer`). */
|
|
755
|
+
transferReply(end) {
|
|
756
|
+
const t = this.settings.transfer;
|
|
757
|
+
if (!t || this.callEnding)
|
|
758
|
+
return false;
|
|
759
|
+
this.transferring = true;
|
|
760
|
+
const via = this.latched.has(end.seq) ? 'tool' : 'spoken';
|
|
761
|
+
this.transferPlayedOut = t.phraseFloor
|
|
762
|
+
? this.awaitPlayedOut(end.aggregated, t.phraseFloor, { turnStart: end.turnStart, firstChunkAt: end.firstChunkAt }, end.seq)
|
|
763
|
+
: null;
|
|
764
|
+
this.logger.log('transfer started, conversation closed', { via });
|
|
765
|
+
this.emit({
|
|
766
|
+
type: 'TRANSFER_REPLY', via, spokeWords: /[\p{L}\p{N}]/u.test(end.aggregated), ...(/\S/u.test(end.aggregated) ? {} : { blank: true }),
|
|
767
|
+
...(this.lastTurnTriggerText != null ? { trigger: this.lastTurnTriggerText } : {}), m: this.snapshot(),
|
|
768
|
+
});
|
|
769
|
+
return true;
|
|
770
|
+
}
|
|
771
|
+
finalsIgnored(text) {
|
|
772
|
+
if (!this.transferring || !this.settings.transfer)
|
|
773
|
+
return false;
|
|
774
|
+
this.logger.log('asr result ignored — transfer', { text });
|
|
775
|
+
return true;
|
|
776
|
+
}
|
|
777
|
+
/** The transfer the reducer started: its phrase, the line played out, then the bridge or the dial. */
|
|
778
|
+
async runTransfer(run) {
|
|
779
|
+
const t = this.settings.transfer;
|
|
780
|
+
if (!t || this.terminated)
|
|
781
|
+
return;
|
|
782
|
+
this.transferring = true;
|
|
783
|
+
const playedOut = this.transferPlayedOut;
|
|
784
|
+
this.transferPlayedOut = null;
|
|
785
|
+
// Bridge: the silence windows stop while the transfer runs (a failure back to the line arms them again with the
|
|
786
|
+
// reaction's own line); the call clock runs until the operator answers.
|
|
787
|
+
if (run.mode === 'bridge')
|
|
788
|
+
this.timing.dispose();
|
|
789
|
+
// The transfer's own line (or clip) and how long it counts as playing, from when it was handed over.
|
|
790
|
+
let announced = null;
|
|
791
|
+
if (run.say) {
|
|
792
|
+
const at = Date.now();
|
|
793
|
+
try {
|
|
794
|
+
await this.channel.audio.say(run.say, { ...this.agent.play.llm, alias: run.alias, tts: this.tts ?? undefined });
|
|
795
|
+
}
|
|
796
|
+
catch { /* the transfer goes on */ }
|
|
797
|
+
if (t.textTranscript)
|
|
798
|
+
this.emit({ type: 'SPOKEN', text: run.say, unit: run.alias });
|
|
799
|
+
if (t.textTranscript && t.phraseFloor)
|
|
800
|
+
announced = { at, playMs: Math.max((0, call_voice_1.spokenLength)(run.say) * t.phraseFloor.msPerChar, t.phraseFloor.minMs) };
|
|
801
|
+
}
|
|
802
|
+
else if (run.clip) {
|
|
803
|
+
const at = Date.now();
|
|
804
|
+
let playMs = run.clip.playMs;
|
|
805
|
+
try {
|
|
806
|
+
const source = run.clip.record != null ? await this.engineCall.record(run.clip.record) : undefined;
|
|
807
|
+
const media = source ?? run.clip.url;
|
|
808
|
+
if (media == null)
|
|
809
|
+
throw new Error('no clip');
|
|
810
|
+
await this.channel.audio.play(media, { queue: this.agent.queues.tts, alias: run.alias });
|
|
811
|
+
}
|
|
812
|
+
catch (err) {
|
|
813
|
+
// The clip could not play: its text by TTS instead.
|
|
814
|
+
this.logger.log('transfer_clip_error', { err: String(err) });
|
|
815
|
+
try {
|
|
816
|
+
await this.channel.audio.say(run.clip.text, { ...this.agent.play.llm, alias: run.alias, tts: this.tts ?? undefined });
|
|
817
|
+
}
|
|
818
|
+
catch { /* the transfer goes on */ }
|
|
819
|
+
playMs = t.phraseFloor ? (0, call_voice_1.spokenLength)(run.clip.text) * t.phraseFloor.msPerChar : 0;
|
|
820
|
+
}
|
|
821
|
+
if (t.textTranscript && run.clip.text)
|
|
822
|
+
this.emit({ type: 'SPOKEN', text: run.clip.text, unit: run.alias });
|
|
823
|
+
announced = { at, playMs };
|
|
824
|
+
}
|
|
825
|
+
if (run.mode === 'bridge') {
|
|
826
|
+
if (playedOut)
|
|
827
|
+
await playedOut;
|
|
828
|
+
else if (announced) {
|
|
829
|
+
const floorMs = Math.min(announced.playMs, this.agent.lifecycle.ttsDrainTimeoutMs) - (Date.now() - announced.at);
|
|
830
|
+
if (floorMs > 0)
|
|
831
|
+
await new Promise((r) => setTimeout(r, floorMs));
|
|
832
|
+
}
|
|
833
|
+
await this.awaitTtsQueueDrain(this.agent.lifecycle.ttsDrainTimeoutMs);
|
|
834
|
+
if (t.graceMs > 0)
|
|
835
|
+
await new Promise((r) => setTimeout(r, t.graceMs));
|
|
836
|
+
if (this.terminated)
|
|
837
|
+
return;
|
|
838
|
+
try {
|
|
839
|
+
this.stopTts();
|
|
840
|
+
}
|
|
841
|
+
catch { /* closed */ }
|
|
842
|
+
try {
|
|
843
|
+
this.channel.audio.stop(this.agent.queues.ambient);
|
|
844
|
+
}
|
|
845
|
+
catch { /* closed */ }
|
|
846
|
+
this.stopSfx();
|
|
847
|
+
const progress = this.engineCall.transferProgress;
|
|
848
|
+
let outcome;
|
|
849
|
+
if (!run.target.did) {
|
|
850
|
+
this.logger.log('transfer_not_configured', { mode: run.mode });
|
|
851
|
+
outcome = 'not_configured';
|
|
852
|
+
}
|
|
853
|
+
else {
|
|
854
|
+
try {
|
|
855
|
+
const bridged = await (0, transfer_1.bridgeToOperator)(this.channel, this.logger, {
|
|
856
|
+
destination: run.target.did,
|
|
857
|
+
channel: run.target.channel ?? '',
|
|
858
|
+
protoAdditional: run.headers,
|
|
859
|
+
answerTimeoutMs: t.answerTimeoutMs,
|
|
860
|
+
onBeforeDial: () => {
|
|
861
|
+
if (progress.closed)
|
|
862
|
+
return;
|
|
863
|
+
progress.started = true;
|
|
864
|
+
progress.startedAt = new Date().toISOString();
|
|
865
|
+
},
|
|
866
|
+
onConnected: () => {
|
|
867
|
+
progress.completed = true;
|
|
868
|
+
this.lifecycle.cancelMaxDuration();
|
|
869
|
+
},
|
|
870
|
+
});
|
|
871
|
+
this.bridgeTeardown = bridged.teardown;
|
|
872
|
+
outcome = bridged.outcome;
|
|
873
|
+
}
|
|
874
|
+
catch (err) {
|
|
875
|
+
this.logger.log('transfer_bridge_error', { err: String(err) });
|
|
876
|
+
outcome = 'dial_failed';
|
|
877
|
+
}
|
|
878
|
+
}
|
|
879
|
+
if (outcome === 'connected') {
|
|
880
|
+
this.emit({ type: 'TRANSFER_BRIDGED' });
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
this.transferFailedBack(outcome);
|
|
884
|
+
return;
|
|
885
|
+
}
|
|
886
|
+
const dial = t.dial;
|
|
887
|
+
const grace = t.graceMs;
|
|
888
|
+
this.dial ??= new transfer_1.DialTransfer({
|
|
889
|
+
target: {
|
|
890
|
+
...(run.target.msisdn ? { msisdn: run.target.msisdn } : {}),
|
|
891
|
+
...(run.target.channel ? { channel: run.target.channel } : {}),
|
|
892
|
+
},
|
|
893
|
+
holdTimeoutMs: dial.holdTimeoutMs,
|
|
894
|
+
operatorAsr: (() => {
|
|
895
|
+
const v = this.engineCall.context ? resolvedVoices.get(this.engineCall.context)?.operatorVendor : undefined;
|
|
896
|
+
return v != null ? { ...dial.operatorAsr, vendor: v } : dial.operatorAsr;
|
|
897
|
+
})(),
|
|
898
|
+
queuePatterns: dial.queuePatterns,
|
|
899
|
+
whisper: run.whisper ?? '',
|
|
900
|
+
results: dial.results,
|
|
901
|
+
}, {
|
|
902
|
+
channel: this.channel,
|
|
903
|
+
logger: this.logger,
|
|
904
|
+
play: this.agent.play.llm,
|
|
905
|
+
terminated: this.terminatedP,
|
|
906
|
+
isTerminated: () => this.terminated,
|
|
907
|
+
setResult: (result) => this.emit({ type: 'RESULT', result }),
|
|
908
|
+
onBridged: () => {
|
|
909
|
+
this.stopTts();
|
|
910
|
+
try {
|
|
911
|
+
this.channel.audio.stop(this.agent.queues.ambient);
|
|
912
|
+
}
|
|
913
|
+
catch { /* ignore */ }
|
|
914
|
+
this.asrDisposed = true;
|
|
915
|
+
this.clearEngine();
|
|
916
|
+
this.lifecycle.cancelMaxDuration();
|
|
917
|
+
this.emit({ type: 'TRANSFER_BRIDGED' });
|
|
918
|
+
},
|
|
919
|
+
onFailed: (failure) => {
|
|
920
|
+
this.transferring = false;
|
|
921
|
+
this.emit({ type: 'TRANSFER_FAILED', failure, m: this.snapshot() });
|
|
922
|
+
},
|
|
923
|
+
});
|
|
924
|
+
const ready = (playedOut ?? Promise.resolve()).then(() => (grace > 0 ? new Promise((r) => setTimeout(r, grace)) : undefined));
|
|
925
|
+
await this.dial.run(ready);
|
|
926
|
+
}
|
|
927
|
+
/** The transfer's target could not be read: it fails as `not_configured` before anything of it ran. */
|
|
928
|
+
transferNotConfigured() {
|
|
929
|
+
this.transferPlayedOut = null;
|
|
930
|
+
this.logger.log('transfer_outcome', { outcome: 'not_configured' });
|
|
931
|
+
if (this.terminated)
|
|
932
|
+
return;
|
|
933
|
+
this.transferring = false;
|
|
934
|
+
this.emit({ type: 'TRANSFER_FAILED', failure: 'not_configured', m: this.snapshot() });
|
|
935
|
+
}
|
|
936
|
+
/**
|
|
937
|
+
* The transfer did not connect: the conversation gets the call back as it was (the background bed, caller lines
|
|
938
|
+
* read) and the reducer runs the agent's `onFailure` with the outcome. Nothing more is decided here.
|
|
939
|
+
*/
|
|
940
|
+
transferFailedBack(outcome) {
|
|
941
|
+
this.logger.log('transfer_outcome', { outcome });
|
|
942
|
+
if (this.terminated)
|
|
943
|
+
return;
|
|
944
|
+
this.transferring = false;
|
|
945
|
+
this.startBackground();
|
|
946
|
+
this.emit({ type: 'TRANSFER_FAILED', failure: outcome, m: this.snapshot() });
|
|
947
|
+
}
|
|
948
|
+
replyBlocksBargeIn(tag) {
|
|
949
|
+
return tag === 'bootstrap' ? true : undefined;
|
|
950
|
+
}
|
|
951
|
+
// ── an LLM line requested early (the eager greeting) ─────────────────────────
|
|
952
|
+
/** The LLM stream of `key` starts now and is kept, so it can play the moment its turn comes. */
|
|
953
|
+
prefetch(key, signal, language, opts = {}) {
|
|
954
|
+
if (this.prefetched.has(key) || this.terminated)
|
|
955
|
+
return;
|
|
956
|
+
const stream$ = (0, rxjs_1.defer)(() => this.greetingText(signal, this.turnPayload(language), opts)).pipe((0, rxjs_1.shareReplay)(Infinity));
|
|
957
|
+
const sub = stream$.subscribe({
|
|
958
|
+
error: (err) => this.logger.warn('engine: prefetched LLM stream failed', { key, error: String(err) }),
|
|
959
|
+
});
|
|
960
|
+
this.prefetched.set(key, { stream$, sub });
|
|
961
|
+
}
|
|
962
|
+
/** A greeting's LLM text: ended empty after `firstTokenMs` without a chunk, and on an error with `emptyOnError`. */
|
|
963
|
+
greetingText(signal, payload, opts) {
|
|
964
|
+
let text$ = this.llmText(signal, payload);
|
|
965
|
+
if (opts.firstTokenMs != null)
|
|
966
|
+
text$ = text$.pipe((0, rxjs_1.timeout)({ first: opts.firstTokenMs }));
|
|
967
|
+
if (opts.firstTokenMs == null && !opts.emptyOnError)
|
|
968
|
+
return text$;
|
|
969
|
+
return text$.pipe((0, rxjs_1.catchError)((err) => {
|
|
970
|
+
this.logger.log('greeting_stream_ended_empty', { firstTokenMs: opts.firstTokenMs ?? null, err: String(err) });
|
|
971
|
+
return rxjs_1.EMPTY;
|
|
972
|
+
}));
|
|
973
|
+
}
|
|
974
|
+
prefetchedStream(key, signal, payload) {
|
|
975
|
+
if (!this.prefetched.has(key)) {
|
|
976
|
+
const stream$ = (0, rxjs_1.defer)(() => this.greetingText(signal, payload, this.greetingOpts)).pipe((0, rxjs_1.shareReplay)(Infinity));
|
|
977
|
+
this.prefetched.set(key, { stream$, sub: stream$.subscribe({ error: () => undefined }) });
|
|
978
|
+
}
|
|
979
|
+
return this.prefetched.get(key).stream$;
|
|
980
|
+
}
|
|
981
|
+
/** Play the LLM line of `key` (requested now when it was not prefetched); it plays out whole. */
|
|
982
|
+
playLlm(key, signal, alias, language, opts = {}) {
|
|
983
|
+
this.greetingAlias = alias;
|
|
984
|
+
this.greetingOpts = opts;
|
|
985
|
+
const turn = { heard: signal, prefetched: key };
|
|
986
|
+
this.runLlmTurn(turn, 'bootstrap', language);
|
|
987
|
+
const seq = this.turnSeq;
|
|
988
|
+
this.greeting = { seq, fallback: opts.fallback ?? null, cuttable: opts.bargeInAfterMs != null };
|
|
989
|
+
if (opts.bargeInAfterMs != null) {
|
|
990
|
+
// The caller can cut the greeting once it has played this long.
|
|
991
|
+
setTimeout(() => { if (this.isCurrent(seq) && !this.terminated)
|
|
992
|
+
this.greetingOnWire = false; }, opts.bargeInAfterMs);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
/** The greeting streamed nothing: its fallback clip, which counts as playing at least its text's reply floor. */
|
|
996
|
+
playGreetingFallback(end, fb) {
|
|
997
|
+
const { play, text } = fb;
|
|
998
|
+
const seq = end.seq;
|
|
999
|
+
void (async () => {
|
|
1000
|
+
const at = Date.now();
|
|
1001
|
+
let spoken = true;
|
|
1002
|
+
try {
|
|
1003
|
+
const source = play.record != null ? await this.engineCall.record((0, variants_1.withVoice)(play.record, this.voiceTag)) : undefined;
|
|
1004
|
+
const media = source ?? (play.url != null ? (0, variants_1.withVoice)(play.url, this.voiceTag) : undefined);
|
|
1005
|
+
this.logger.log('greeting_fallback', { alias: play.alias, ms: Date.now() - end.turnStart });
|
|
1006
|
+
if (media)
|
|
1007
|
+
await this.channel.audio.play(media, { queue: this.agent.queues.tts, alias: play.alias });
|
|
1008
|
+
else
|
|
1009
|
+
await this.channel.audio.say(this.call.voice.respell?.phrase(text) ?? text, { ...this.agent.play.llm, alias: play.alias, tts: this.tts ?? undefined });
|
|
1010
|
+
}
|
|
1011
|
+
catch (err) {
|
|
1012
|
+
spoken = false;
|
|
1013
|
+
this.logger.warn('engine: greeting fallback failed', { error: String(err) });
|
|
1014
|
+
}
|
|
1015
|
+
if (!this.isCurrent(seq))
|
|
1016
|
+
return;
|
|
1017
|
+
if (spoken && text)
|
|
1018
|
+
this.emit({ type: 'SPOKEN', text, unit: play.alias });
|
|
1019
|
+
if (this.terminated)
|
|
1020
|
+
return;
|
|
1021
|
+
const msPerChar = this.replyFloor?.msPerChar ?? 0;
|
|
1022
|
+
const ceilingMs = this.agent.lifecycle.ttsDrainTimeoutMs;
|
|
1023
|
+
const floorMs = Math.min(Math.max((0, call_voice_1.spokenLength)(text) * msPerChar, play.minPlayMs), ceilingMs) - (Date.now() - at);
|
|
1024
|
+
if (floorMs > 0)
|
|
1025
|
+
await new Promise((r) => setTimeout(r, floorMs));
|
|
1026
|
+
if (!this.isCurrent(seq))
|
|
1027
|
+
return;
|
|
1028
|
+
await this.awaitTtsQueueDrain(Math.max(0, ceilingMs - Math.max(0, floorMs)));
|
|
1029
|
+
if (!this.isCurrent(seq))
|
|
1030
|
+
return;
|
|
1031
|
+
this.greetingOnWire = false;
|
|
1032
|
+
this.drained(seq);
|
|
1033
|
+
})().catch((err) => {
|
|
1034
|
+
this.turnEndFailed('greeting_fallback_end_failed', seq, err, { alias: play.alias }, () => {
|
|
1035
|
+
if (this.isCurrent(seq))
|
|
1036
|
+
this.greetingOnWire = false;
|
|
1037
|
+
});
|
|
1038
|
+
});
|
|
1039
|
+
}
|
|
1040
|
+
llmPlay(tag) {
|
|
1041
|
+
return tag === 'bootstrap' ? { ...this.agent.play.greeting, alias: this.greetingAlias } : this.agent.play.llm;
|
|
1042
|
+
}
|
|
1043
|
+
// ── clips ────────────────────────────────────────────────────────────────────
|
|
1044
|
+
/**
|
|
1045
|
+
* A clip: the CMS record when there is one, else the URL, else TTS of its text; it is not cut by
|
|
1046
|
+
* the caller unless `bargeIn`. The clip's turn drains `tailMs` after the clip was handed over (a record: played;
|
|
1047
|
+
* TTS: synthesized) — the silence window counts from there.
|
|
1048
|
+
*/
|
|
1049
|
+
playClip(play, text, items, queued = false) {
|
|
1050
|
+
if (this.terminated || this.callEnding)
|
|
1051
|
+
return;
|
|
1052
|
+
// An opening step's clip queues behind the step before it (a clip spoken by TTS may still be playing).
|
|
1053
|
+
const seq = this.beginTurn(!queued);
|
|
1054
|
+
this.greetingOnWire = !play.bargeIn;
|
|
1055
|
+
this.blockingSeq = play.bargeIn ? -1 : seq;
|
|
1056
|
+
this.agentSpeaking = true;
|
|
1057
|
+
const alias = play.alias;
|
|
1058
|
+
const tag = this.voiceTag;
|
|
1059
|
+
const release = () => {
|
|
1060
|
+
if (this.blockingSeq === seq && this.isCurrent(seq))
|
|
1061
|
+
this.greetingOnWire = false;
|
|
1062
|
+
};
|
|
1063
|
+
const failed = (err) => this.turnEndFailed('clip_end_failed', seq, err, { alias }, release);
|
|
1064
|
+
if (items) {
|
|
1065
|
+
this.playSequence(play, items, seq).catch(failed);
|
|
1066
|
+
return;
|
|
1067
|
+
}
|
|
1068
|
+
void (async () => {
|
|
1069
|
+
let spoken = true;
|
|
1070
|
+
let playedAt = Date.now();
|
|
1071
|
+
try {
|
|
1072
|
+
if (play.delayMs) {
|
|
1073
|
+
this.logger.log('clip_delay', { alias, delayMs: play.delayMs });
|
|
1074
|
+
await new Promise((r) => setTimeout(r, play.delayMs));
|
|
1075
|
+
if (!this.isCurrent(seq) || this.terminated || this.callEnding)
|
|
1076
|
+
return;
|
|
1077
|
+
}
|
|
1078
|
+
const source = play.record != null ? await this.engineCall.record((0, variants_1.withVoice)(play.record, tag)) : undefined;
|
|
1079
|
+
if (!this.isCurrent(seq) || this.terminated)
|
|
1080
|
+
return;
|
|
1081
|
+
playedAt = Date.now();
|
|
1082
|
+
const media = source ?? (play.url != null ? (0, variants_1.withVoice)(play.url, tag) : undefined);
|
|
1083
|
+
if (media) {
|
|
1084
|
+
// With `waitFinished` the clip counts as played when the queue says it finished (watched from the start).
|
|
1085
|
+
const finished = play.waitFinished ? this.itemFinished(alias) : null;
|
|
1086
|
+
await this.channel.audio.play(media, { queue: this.agent.queues.tts, alias });
|
|
1087
|
+
if (this.settings.clipTranscript && text)
|
|
1088
|
+
this.transcriptLine('agent', text);
|
|
1089
|
+
if (finished) {
|
|
1090
|
+
await finished;
|
|
1091
|
+
if (this.terminated)
|
|
1092
|
+
spoken = false;
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
else
|
|
1096
|
+
await this.channel.audio.say(this.call.voice.respell?.phrase(text) ?? text, { ...this.agent.play.llm, alias, tts: this.tts ?? undefined });
|
|
1097
|
+
}
|
|
1098
|
+
catch (err) {
|
|
1099
|
+
spoken = false;
|
|
1100
|
+
this.logger.warn('engine: clip failed', { name: alias, error: String(err) });
|
|
1101
|
+
}
|
|
1102
|
+
if (!this.isCurrent(seq))
|
|
1103
|
+
return;
|
|
1104
|
+
// A clip the caller hung up on is in the transcript as far as the platform played it.
|
|
1105
|
+
if (spoken)
|
|
1106
|
+
this.emit({ type: 'SPOKEN', text, unit: alias });
|
|
1107
|
+
if (this.terminated)
|
|
1108
|
+
return;
|
|
1109
|
+
const floor = play.minPlayMs - (Date.now() - playedAt);
|
|
1110
|
+
if (floor > 0)
|
|
1111
|
+
await new Promise((r) => setTimeout(r, floor));
|
|
1112
|
+
if (!this.isCurrent(seq))
|
|
1113
|
+
return;
|
|
1114
|
+
if (play.tailMs > 0)
|
|
1115
|
+
await new Promise((r) => setTimeout(r, play.tailMs));
|
|
1116
|
+
if (!this.isCurrent(seq))
|
|
1117
|
+
return;
|
|
1118
|
+
this.drained(seq);
|
|
1119
|
+
})().catch(failed);
|
|
1120
|
+
}
|
|
1121
|
+
/**
|
|
1122
|
+
* A sequence of records (`PlaySpec.records`): each looked up and played in turn (a missing one by TTS of its text),
|
|
1123
|
+
* `gapMs` between two of them; the turn drains `tailMs` after the last. A hangup stops it.
|
|
1124
|
+
*/
|
|
1125
|
+
async playSequence(play, items, seq) {
|
|
1126
|
+
const tag = this.voiceTag;
|
|
1127
|
+
for (const [i, item] of items.entries()) {
|
|
1128
|
+
if (!this.isCurrent(seq) || this.terminated)
|
|
1129
|
+
return;
|
|
1130
|
+
const alias = `${play.alias}:${item.record}`;
|
|
1131
|
+
let spoken = true;
|
|
1132
|
+
try {
|
|
1133
|
+
const source = await this.engineCall.record((0, variants_1.withVoice)(item.record, tag));
|
|
1134
|
+
if (!this.isCurrent(seq) || this.terminated)
|
|
1135
|
+
return;
|
|
1136
|
+
if (source) {
|
|
1137
|
+
const finished = play.waitFinished ? this.itemFinished(alias) : null;
|
|
1138
|
+
await this.channel.audio.play(source, { queue: this.agent.queues.tts, alias });
|
|
1139
|
+
if (this.settings.clipTranscript && item.text)
|
|
1140
|
+
this.transcriptLine('agent', item.text);
|
|
1141
|
+
if (finished) {
|
|
1142
|
+
await finished;
|
|
1143
|
+
if (this.terminated)
|
|
1144
|
+
spoken = false;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
else {
|
|
1148
|
+
await this.channel.audio.say(this.call.voice.respell?.phrase(item.text) ?? item.text, { ...this.agent.play.llm, alias, tts: this.tts ?? undefined });
|
|
1149
|
+
}
|
|
1150
|
+
}
|
|
1151
|
+
catch (err) {
|
|
1152
|
+
spoken = false;
|
|
1153
|
+
this.logger.warn('engine: clip failed', { name: alias, error: String(err) });
|
|
1154
|
+
}
|
|
1155
|
+
if (!this.isCurrent(seq))
|
|
1156
|
+
return;
|
|
1157
|
+
if (spoken && item.text)
|
|
1158
|
+
this.emit({ type: 'SPOKEN', text: item.text, unit: alias });
|
|
1159
|
+
if (this.terminated)
|
|
1160
|
+
return;
|
|
1161
|
+
if (i < items.length - 1 && play.gapMs > 0)
|
|
1162
|
+
await new Promise((r) => setTimeout(r, play.gapMs));
|
|
1163
|
+
}
|
|
1164
|
+
if (!this.isCurrent(seq) || this.terminated)
|
|
1165
|
+
return;
|
|
1166
|
+
if (play.tailMs > 0)
|
|
1167
|
+
await new Promise((r) => setTimeout(r, play.tailMs));
|
|
1168
|
+
if (!this.isCurrent(seq))
|
|
1169
|
+
return;
|
|
1170
|
+
this.drained(seq);
|
|
1171
|
+
}
|
|
1172
|
+
/** A reply that is the clip's text: the clip plays in its place; the reply counts as said. */
|
|
1173
|
+
playReplyClip(clip, end) {
|
|
1174
|
+
const alias = clip.play.alias ?? 'reply_clip';
|
|
1175
|
+
this.logger.log('reply_played_as_clip', { tag: end.tag, text: clip.text, msAfterTurnStart: Date.now() - end.turnStart });
|
|
1176
|
+
this.resetEmptyCounts();
|
|
1177
|
+
this.emit({ type: 'SPOKEN', text: clip.text, unit: alias });
|
|
1178
|
+
this.awaitPlayedOut(clip.text, this.replyFloor, { turnStart: end.turnStart, firstChunkAt: end.firstChunkAt }, end.seq).then(() => this.drained(end.seq), (err) => this.turnEndFailed('reply_clip_end_failed', end.seq, err, { tag: end.tag }));
|
|
1179
|
+
}
|
|
1180
|
+
/** The agent queue finished the item `alias` (or the call ended, or a minute passed). */
|
|
1181
|
+
itemFinished(alias) {
|
|
1182
|
+
const q = this.channel.audio.queue(this.agent.queues.tts);
|
|
1183
|
+
const done$ = q.itemFinished$.pipe((0, rxjs_1.filter)((a) => a === alias || a?.alias === alias));
|
|
1184
|
+
return (0, rxjs_1.firstValueFrom)((0, rxjs_1.race)(done$, this.channel.events.terminated$, (0, rxjs_1.timer)(60_000))).then(() => undefined, () => undefined);
|
|
1185
|
+
}
|
|
1186
|
+
/** A line of the platform's call transcript (the CMS log of the call). */
|
|
1187
|
+
transcriptLine(role, text) {
|
|
1188
|
+
try {
|
|
1189
|
+
this.logger.transcript?.(role, text);
|
|
1190
|
+
}
|
|
1191
|
+
catch (err) {
|
|
1192
|
+
this.logger.log('cms_transcript_write_failed', { role, err: String(err) });
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
/** No new line once the hangup started (the effects of one event may run after the next event's hangup). */
|
|
1196
|
+
runLlmTurn(...args) {
|
|
1197
|
+
if (this.callEnding)
|
|
1198
|
+
return;
|
|
1199
|
+
super.runLlmTurn(...args);
|
|
1200
|
+
}
|
|
1201
|
+
runStaticTurn(...args) {
|
|
1202
|
+
if (this.callEnding)
|
|
1203
|
+
return;
|
|
1204
|
+
super.runStaticTurn(...args);
|
|
1205
|
+
}
|
|
1206
|
+
drained(seq) {
|
|
1207
|
+
// A greeting the caller cut has drained already (at the cut).
|
|
1208
|
+
if (this.cutDrained === seq)
|
|
1209
|
+
return;
|
|
1210
|
+
if (this.blockingSeq === seq && this.isCurrent(seq))
|
|
1211
|
+
this.greetingOnWire = false;
|
|
1212
|
+
super.drained(seq);
|
|
1213
|
+
}
|
|
1214
|
+
/** A greeting the caller can cut (`bargeInAfterMs`) ends at the cut: the opening goes on at once. */
|
|
1215
|
+
turnCut(seq) {
|
|
1216
|
+
const g = this.greeting;
|
|
1217
|
+
if (!g || g.seq !== seq || !g.cuttable || !this.isCurrent(seq))
|
|
1218
|
+
return;
|
|
1219
|
+
this.logger.log('greeting_cut', { msAfterTurnStart: Date.now() - this.turnStartedAtMs });
|
|
1220
|
+
this.greetingOnWire = false;
|
|
1221
|
+
super.drained(seq);
|
|
1222
|
+
this.cutDrained = seq;
|
|
1223
|
+
}
|
|
1224
|
+
};
|
|
1225
|
+
exports.EngineMedia = EngineMedia;
|
|
1226
|
+
exports.EngineMedia = EngineMedia = __decorate([
|
|
1227
|
+
(0, decorators_1.Injectable)()
|
|
1228
|
+
// After LifecycleService (-50): the max-duration timer starts first.
|
|
1229
|
+
,
|
|
1230
|
+
(0, hooks_1.PhaseOrder)(-40),
|
|
1231
|
+
__param(0, (0, tsyringe_1.inject)(tokens_1.CHANNEL)),
|
|
1232
|
+
__param(1, (0, tsyringe_1.inject)(tokens_1.LOGGER)),
|
|
1233
|
+
__param(2, (0, tsyringe_1.inject)(active_media_1.ActiveMedia)),
|
|
1234
|
+
__param(3, (0, tsyringe_1.inject)(call_1.EngineCall)),
|
|
1235
|
+
__param(4, (0, tsyringe_1.inject)(lifecycle_1.LifecycleService)),
|
|
1236
|
+
__param(5, (0, tsyringe_1.inject)(exports.ENGINE_MEDIA)),
|
|
1237
|
+
__metadata("design:paramtypes", [Object, Object, active_media_1.ActiveMedia,
|
|
1238
|
+
call_1.EngineCall,
|
|
1239
|
+
lifecycle_1.LifecycleService, Object])
|
|
1240
|
+
], EngineMedia);
|