@voctiv/agent-runtime 0.2.2 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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 +0 -1
- 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,204 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* In-memory lexical retriever over the knowledge base — the candidate generator in front of
|
|
4
|
+
* the decision model. It only has to put the right answer among the top few (recall@K); the
|
|
5
|
+
* decision model picks among them.
|
|
6
|
+
*
|
|
7
|
+
* Every entry (answer card or topic) owns several phrases: its caller phrasings (full
|
|
8
|
+
* weight) and extra text such as the answer itself (reduced weight — it brings the
|
|
9
|
+
* vocabulary a caller may use without being a caller phrasing). Each phrase is a BM25
|
|
10
|
+
* document over two fields — word stems and character trigrams of the stems (robust to ASR
|
|
11
|
+
* misspellings and morphology). The query is expanded with domain synonyms («express» →
|
|
12
|
+
* the product name, a country → «international») at reduced weight. An entry scores as its best phrase
|
|
13
|
+
* plus a small bonus for its second best, so an entry that several phrasings support
|
|
14
|
+
* outranks one lucky phrasing.
|
|
15
|
+
*
|
|
16
|
+
* Built once per process; a query is a few postings scans — well under a millisecond at a
|
|
17
|
+
* few thousand phrases. I/O-free; runs in the script sandbox.
|
|
18
|
+
*/
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.QnaRetriever = exports.DEFAULT_RETRIEVER_OPTIONS = void 0;
|
|
21
|
+
const text_1 = require("./text");
|
|
22
|
+
exports.DEFAULT_RETRIEVER_OPTIONS = {
|
|
23
|
+
k1: 1.2,
|
|
24
|
+
b: 0.75,
|
|
25
|
+
trigramWeight: 0.3,
|
|
26
|
+
secondPhraseBonus: 0.2,
|
|
27
|
+
extraPhraseWeight: 0.4,
|
|
28
|
+
synonymWeight: 0.5,
|
|
29
|
+
};
|
|
30
|
+
/** One field (stems or trigrams) of the phrase collection. */
|
|
31
|
+
class Bm25Field {
|
|
32
|
+
constructor() {
|
|
33
|
+
this.postings = new Map();
|
|
34
|
+
this.lengths = [];
|
|
35
|
+
this.totalLength = 0;
|
|
36
|
+
}
|
|
37
|
+
add(doc, terms) {
|
|
38
|
+
this.lengths[doc] = terms.length;
|
|
39
|
+
this.totalLength += terms.length;
|
|
40
|
+
const tf = new Map();
|
|
41
|
+
for (const t of terms)
|
|
42
|
+
tf.set(t, (tf.get(t) ?? 0) + 1);
|
|
43
|
+
for (const [t, n] of tf) {
|
|
44
|
+
let list = this.postings.get(t);
|
|
45
|
+
if (list == null)
|
|
46
|
+
this.postings.set(t, (list = []));
|
|
47
|
+
// Flat [doc, tf, doc, tf, …] — half the allocations of tuples.
|
|
48
|
+
list.push(doc, n);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/** Add this field's BM25 score of `queryTerms` into `scores`, each term scaled by its
|
|
52
|
+
* weight and every document by `docWeight`. */
|
|
53
|
+
score(queryTerms, scores, touched, docWeight, opts) {
|
|
54
|
+
const n = this.lengths.length;
|
|
55
|
+
if (n === 0)
|
|
56
|
+
return;
|
|
57
|
+
const avgdl = this.totalLength / n || 1;
|
|
58
|
+
for (const [term, weight] of queryTerms) {
|
|
59
|
+
const list = this.postings.get(term);
|
|
60
|
+
if (list == null)
|
|
61
|
+
continue;
|
|
62
|
+
const df = list.length / 2;
|
|
63
|
+
const idf = Math.log(1 + (n - df + 0.5) / (df + 0.5));
|
|
64
|
+
for (let i = 0; i < list.length; i += 2) {
|
|
65
|
+
const doc = list[i];
|
|
66
|
+
const tf = list[i + 1];
|
|
67
|
+
const norm = opts.k1 * (1 - opts.b + (opts.b * this.lengths[doc]) / avgdl);
|
|
68
|
+
const s = (docWeight[doc] * weight * idf * tf * (opts.k1 + 1)) / (tf + norm);
|
|
69
|
+
if (scores[doc] === 0)
|
|
70
|
+
touched.push(doc);
|
|
71
|
+
scores[doc] += s;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
class QnaRetriever {
|
|
77
|
+
constructor(options, entries, synonymGroups, text) {
|
|
78
|
+
this.options = options;
|
|
79
|
+
this.text = text;
|
|
80
|
+
this.stemField = new Bm25Field();
|
|
81
|
+
this.trigramField = new Bm25Field();
|
|
82
|
+
/** Phrase → owning entry. */
|
|
83
|
+
this.owner = [];
|
|
84
|
+
this.phraseText = [];
|
|
85
|
+
/** Stem key of a caller phrasing → entries that own such a phrasing. */
|
|
86
|
+
this.exactIndex = new Map();
|
|
87
|
+
/** Stem → stems of its synonyms. */
|
|
88
|
+
this.synonyms = new Map();
|
|
89
|
+
const stems = (t) => text.stems(t);
|
|
90
|
+
for (const group of synonymGroups) {
|
|
91
|
+
const groupStems = [
|
|
92
|
+
...new Set(group.flatMap((w) => stems(w)).filter(Boolean)),
|
|
93
|
+
];
|
|
94
|
+
for (const s of groupStems) {
|
|
95
|
+
const others = groupStems.filter((o) => o !== s);
|
|
96
|
+
const cur = this.synonyms.get(s);
|
|
97
|
+
this.synonyms.set(s, cur ? [...new Set([...cur, ...others])] : others);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const weights = [];
|
|
101
|
+
let doc = 0;
|
|
102
|
+
const addDoc = (index, phrase, weight, exact) => {
|
|
103
|
+
const s = stems(phrase);
|
|
104
|
+
if (s.length === 0)
|
|
105
|
+
return;
|
|
106
|
+
this.stemField.add(doc, s);
|
|
107
|
+
this.trigramField.add(doc, (0, text_1.trigrams)(s));
|
|
108
|
+
this.owner[doc] = index;
|
|
109
|
+
this.phraseText[doc] = phrase;
|
|
110
|
+
weights[doc] = weight;
|
|
111
|
+
doc += 1;
|
|
112
|
+
if (!exact)
|
|
113
|
+
return;
|
|
114
|
+
const key = s.join(' ');
|
|
115
|
+
let set = this.exactIndex.get(key);
|
|
116
|
+
if (set == null)
|
|
117
|
+
this.exactIndex.set(key, (set = new Set()));
|
|
118
|
+
set.add(index);
|
|
119
|
+
};
|
|
120
|
+
entries.forEach((entry, index) => {
|
|
121
|
+
for (const p of entry.phrases)
|
|
122
|
+
addDoc(index, p, 1, true);
|
|
123
|
+
for (const p of entry.extra ?? []) {
|
|
124
|
+
addDoc(index, p, options.extraPhraseWeight, false);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
this.docWeight = Float64Array.from(weights);
|
|
128
|
+
this.docScores = new Float64Array(doc);
|
|
129
|
+
}
|
|
130
|
+
static build(entries, options, synonymGroups, text) {
|
|
131
|
+
return new QnaRetriever(options, entries, synonymGroups, text);
|
|
132
|
+
}
|
|
133
|
+
get phraseCount() {
|
|
134
|
+
return this.owner.length;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Top `limit` entries for `query`, best first. `accept` (optional) restricts the result to
|
|
138
|
+
* the entries it returns `true` for — e.g. the cards under one topic.
|
|
139
|
+
*/
|
|
140
|
+
search(query, limit, accept) {
|
|
141
|
+
const queryStems = this.text.stems(query);
|
|
142
|
+
if (queryStems.length === 0)
|
|
143
|
+
return [];
|
|
144
|
+
const stemTerms = new Map();
|
|
145
|
+
for (const s of queryStems)
|
|
146
|
+
stemTerms.set(s, 1);
|
|
147
|
+
for (const s of queryStems) {
|
|
148
|
+
for (const syn of this.synonyms.get(s) ?? []) {
|
|
149
|
+
if (!stemTerms.has(syn))
|
|
150
|
+
stemTerms.set(syn, this.options.synonymWeight);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const trigramTerms = new Map();
|
|
154
|
+
for (const t of (0, text_1.trigrams)(queryStems)) {
|
|
155
|
+
trigramTerms.set(t, this.options.trigramWeight);
|
|
156
|
+
}
|
|
157
|
+
const scores = this.docScores;
|
|
158
|
+
const touched = [];
|
|
159
|
+
const { docWeight, options } = this;
|
|
160
|
+
this.stemField.score(stemTerms, scores, touched, docWeight, options);
|
|
161
|
+
this.trigramField.score(trigramTerms, scores, touched, docWeight, options);
|
|
162
|
+
// Best and second-best phrase per entry.
|
|
163
|
+
const best = new Map();
|
|
164
|
+
for (const doc of touched) {
|
|
165
|
+
const s = scores[doc];
|
|
166
|
+
scores[doc] = 0;
|
|
167
|
+
const index = this.owner[doc];
|
|
168
|
+
if (accept != null && !accept(index))
|
|
169
|
+
continue;
|
|
170
|
+
const cur = best.get(index);
|
|
171
|
+
if (cur == null)
|
|
172
|
+
best.set(index, { s1: s, s2: 0, doc });
|
|
173
|
+
else if (s > cur.s1) {
|
|
174
|
+
cur.s2 = cur.s1;
|
|
175
|
+
cur.s1 = s;
|
|
176
|
+
cur.doc = doc;
|
|
177
|
+
}
|
|
178
|
+
else if (s > cur.s2)
|
|
179
|
+
cur.s2 = s;
|
|
180
|
+
}
|
|
181
|
+
const hits = [];
|
|
182
|
+
for (const [index, v] of best) {
|
|
183
|
+
hits.push({
|
|
184
|
+
index,
|
|
185
|
+
score: v.s1 + options.secondPhraseBonus * v.s2,
|
|
186
|
+
phrase: this.phraseText[v.doc],
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
hits.sort((a, b) => b.score - a.score);
|
|
190
|
+
return hits.slice(0, limit);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* The single entry that owns a caller phrasing identical (after normalization and
|
|
194
|
+
* stemming) to `query`, or `null` when none does or several do.
|
|
195
|
+
*/
|
|
196
|
+
exact(query) {
|
|
197
|
+
const set = this.exactIndex.get(this.text.exactKey(query));
|
|
198
|
+
if (set == null || set.size !== 1)
|
|
199
|
+
return null;
|
|
200
|
+
const [index] = set;
|
|
201
|
+
return index;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
exports.QnaRetriever = QnaRetriever;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { ScriptLogger } from '@voctiv/agent-sdk';
|
|
2
|
+
import type { DecisionClient } from '../decision-model';
|
|
3
|
+
import { type QnaIndex, type TurnPlan } from './flow';
|
|
4
|
+
/** Embedding settings of dense retrieval. */
|
|
5
|
+
export interface DenseSettings {
|
|
6
|
+
/** Workers AI model of the knowledge-base vectors. */
|
|
7
|
+
readonly model: string;
|
|
8
|
+
readonly timeoutMs: number;
|
|
9
|
+
/** Time limit of the call-start probe. Default 3 s. */
|
|
10
|
+
readonly probeTimeoutMs?: number;
|
|
11
|
+
/** A probe cosine below this turns dense retrieval off for the call. Default 0.97. */
|
|
12
|
+
readonly probeMinCosine?: number;
|
|
13
|
+
}
|
|
14
|
+
/** What the planner of a call is made of. */
|
|
15
|
+
export interface KnowledgeServiceDeps {
|
|
16
|
+
readonly logger: ScriptLogger;
|
|
17
|
+
readonly requestId: string | undefined;
|
|
18
|
+
readonly index: QnaIndex;
|
|
19
|
+
readonly decision: DecisionClient;
|
|
20
|
+
readonly dense: DenseSettings | undefined;
|
|
21
|
+
/** Results that only say how the call ended (a goodbye, silence): rewritten to answered / unanswered. */
|
|
22
|
+
readonly rewrite: ReadonlySet<string>;
|
|
23
|
+
}
|
|
24
|
+
export declare class KnowledgeService {
|
|
25
|
+
private readonly deps;
|
|
26
|
+
private readonly index;
|
|
27
|
+
private readonly flow;
|
|
28
|
+
private readonly logger;
|
|
29
|
+
private readonly decision;
|
|
30
|
+
/** Model name the backend reports, e.g. `jev-1.13.0`. */
|
|
31
|
+
private backendModel;
|
|
32
|
+
private readonly cardById;
|
|
33
|
+
private readonly topicById;
|
|
34
|
+
/** Whether the live embedding model matches the knowledge-base vectors (`probeDense`). */
|
|
35
|
+
private denseUsable;
|
|
36
|
+
private requests;
|
|
37
|
+
private errors;
|
|
38
|
+
private embeddingErrors;
|
|
39
|
+
private responses;
|
|
40
|
+
private totalMs;
|
|
41
|
+
/** Bumped on every commit — a request that sees it move was superseded mid-flight. */
|
|
42
|
+
private commits;
|
|
43
|
+
private turns;
|
|
44
|
+
private answers;
|
|
45
|
+
private unclearTurns;
|
|
46
|
+
private notFoundTurns;
|
|
47
|
+
private clarifyTurns;
|
|
48
|
+
private fastTurns;
|
|
49
|
+
private exactTurns;
|
|
50
|
+
private operatorRequested;
|
|
51
|
+
private lastUnanswered;
|
|
52
|
+
private readonly answeredCards;
|
|
53
|
+
private readonly path;
|
|
54
|
+
constructor(deps: KnowledgeServiceDeps);
|
|
55
|
+
private get prefix();
|
|
56
|
+
/** At call start: the decision model's credentials (and its warm-up), then the vectors' probe. */
|
|
57
|
+
init(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Embed the vectors' probe phrase live and compare it with the stored vector: a low cosine
|
|
60
|
+
* means the live model is not the one the knowledge base was embedded with, and dense
|
|
61
|
+
* retrieval stays off for this call. Runs while the greeting plays; a probe that fails
|
|
62
|
+
* outright leaves dense on — each turn falls back to lexical on its own failure.
|
|
63
|
+
*/
|
|
64
|
+
private probeDense;
|
|
65
|
+
decide(userText: string): Promise<TurnPlan>;
|
|
66
|
+
/** The utterance's embedding for dense retrieval, or `null` (no vectors, no credentials,
|
|
67
|
+
* or the request failed / timed out — the turn then retrieves lexically only). */
|
|
68
|
+
private embed;
|
|
69
|
+
private logDecision;
|
|
70
|
+
planSilence(count: number): TurnPlan;
|
|
71
|
+
planUnheardSpeech(): TurnPlan;
|
|
72
|
+
planBadConnection(): TurnPlan;
|
|
73
|
+
commit(plan: TurnPlan, userText: string | null): void;
|
|
74
|
+
phraseText(id: string): string;
|
|
75
|
+
resolveResult(current: string): string;
|
|
76
|
+
outcomeEntities(): Record<string, unknown>;
|
|
77
|
+
}
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KnowledgeService = void 0;
|
|
4
|
+
const decision_1 = require("../helpers/decision");
|
|
5
|
+
const dense_1 = require("./dense");
|
|
6
|
+
const flow_1 = require("./flow");
|
|
7
|
+
class KnowledgeService {
|
|
8
|
+
constructor(deps) {
|
|
9
|
+
this.deps = deps;
|
|
10
|
+
/** Model name the backend reports, e.g. `jev-1.13.0`. */
|
|
11
|
+
this.backendModel = null;
|
|
12
|
+
/** Whether the live embedding model matches the knowledge-base vectors (`probeDense`). */
|
|
13
|
+
this.denseUsable = Promise.resolve(true);
|
|
14
|
+
this.requests = 0;
|
|
15
|
+
this.errors = 0;
|
|
16
|
+
this.embeddingErrors = 0;
|
|
17
|
+
this.responses = 0;
|
|
18
|
+
this.totalMs = 0;
|
|
19
|
+
/** Bumped on every commit — a request that sees it move was superseded mid-flight. */
|
|
20
|
+
this.commits = 0;
|
|
21
|
+
this.turns = 0;
|
|
22
|
+
this.answers = 0;
|
|
23
|
+
this.unclearTurns = 0;
|
|
24
|
+
this.notFoundTurns = 0;
|
|
25
|
+
this.clarifyTurns = 0;
|
|
26
|
+
this.fastTurns = 0;
|
|
27
|
+
this.exactTurns = 0;
|
|
28
|
+
this.operatorRequested = false;
|
|
29
|
+
this.lastUnanswered = null;
|
|
30
|
+
this.answeredCards = [];
|
|
31
|
+
this.path = [];
|
|
32
|
+
const catalog = deps.index.catalog;
|
|
33
|
+
this.logger = deps.logger;
|
|
34
|
+
this.decision = deps.decision;
|
|
35
|
+
this.index = deps.index;
|
|
36
|
+
this.flow = new flow_1.QnaFlow(this.index, (id) => this.phraseText(id));
|
|
37
|
+
this.cardById = new Map(catalog.cards.map((c, i) => [c.id, i]));
|
|
38
|
+
this.topicById = new Map(catalog.topics.map((t, i) => [t.id, i]));
|
|
39
|
+
}
|
|
40
|
+
get prefix() {
|
|
41
|
+
return this.decision.spec.logPrefix;
|
|
42
|
+
}
|
|
43
|
+
/** At call start: the decision model's credentials (and its warm-up), then the vectors' probe. */
|
|
44
|
+
async init() {
|
|
45
|
+
const { catalog } = this.index;
|
|
46
|
+
await this.decision.init(() => ({
|
|
47
|
+
cards: catalog.cards.length,
|
|
48
|
+
topics: catalog.topics.length,
|
|
49
|
+
phrasesIndexed: this.index.retriever.phraseCount,
|
|
50
|
+
denseVectors: this.index.dense?.size ?? 0,
|
|
51
|
+
denseOff: this.index.denseOff,
|
|
52
|
+
indexBuildMs: this.index.buildMs,
|
|
53
|
+
}));
|
|
54
|
+
if (this.decision.creds != null && this.index.dense != null) {
|
|
55
|
+
this.denseUsable = this.probeDense();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Embed the vectors' probe phrase live and compare it with the stored vector: a low cosine
|
|
60
|
+
* means the live model is not the one the knowledge base was embedded with, and dense
|
|
61
|
+
* retrieval stays off for this call. Runs while the greeting plays; a probe that fails
|
|
62
|
+
* outright leaves dense on — each turn falls back to lexical on its own failure.
|
|
63
|
+
*/
|
|
64
|
+
async probeDense() {
|
|
65
|
+
const probe = this.index.catalog.vectors?.probe;
|
|
66
|
+
const dense = this.deps.dense;
|
|
67
|
+
if (probe == null || dense == null)
|
|
68
|
+
return true;
|
|
69
|
+
const startedAt = Date.now();
|
|
70
|
+
try {
|
|
71
|
+
const { vector } = await (0, dense_1.embedQuery)(this.logger, this.decision.creds, dense.model, probe.text, { timeoutMs: dense.probeTimeoutMs ?? 3_000, requestId: this.deps.requestId });
|
|
72
|
+
const cos = vector.length === probe.vector.length
|
|
73
|
+
? (0, dense_1.cosine)(vector, probe.vector)
|
|
74
|
+
: 0;
|
|
75
|
+
const ok = cos >= (dense.probeMinCosine ?? dense_1.DENSE_PROBE_MIN_COSINE);
|
|
76
|
+
this.logger.log('dense_probe', {
|
|
77
|
+
ok,
|
|
78
|
+
cosine: Number(cos.toFixed(4)),
|
|
79
|
+
dim: vector.length,
|
|
80
|
+
source: this.index.catalog.vectors?.source ?? null,
|
|
81
|
+
ms: Date.now() - startedAt,
|
|
82
|
+
});
|
|
83
|
+
return ok;
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
this.logger.log('dense_probe_failed', {
|
|
87
|
+
ms: Date.now() - startedAt,
|
|
88
|
+
err: String(err),
|
|
89
|
+
});
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
async decide(userText) {
|
|
94
|
+
const { flow } = this;
|
|
95
|
+
const startedAt = Date.now();
|
|
96
|
+
if (userText === this.index.catalog.connectionSignal) {
|
|
97
|
+
return flow.planGreeting();
|
|
98
|
+
}
|
|
99
|
+
const local = flow.fastPlan(userText) ?? flow.exactPlan(userText);
|
|
100
|
+
if (local != null) {
|
|
101
|
+
this.logDecision(userText, local, { totalMs: Date.now() - startedAt });
|
|
102
|
+
return local;
|
|
103
|
+
}
|
|
104
|
+
const creds = this.decision.creds;
|
|
105
|
+
if (creds == null) {
|
|
106
|
+
this.errors += 1;
|
|
107
|
+
this.logger.log(`${this.prefix}_skipped`, { reason: 'no credentials' });
|
|
108
|
+
return flow.planUnclear('no_credentials');
|
|
109
|
+
}
|
|
110
|
+
const embedding = await this.embed(userText);
|
|
111
|
+
const embeddingMs = Date.now() - startedAt;
|
|
112
|
+
const request = flow.buildRequest(userText, embedding);
|
|
113
|
+
const retrievalMs = Date.now() - startedAt;
|
|
114
|
+
const commitsAtRequest = this.commits;
|
|
115
|
+
this.requests += 1;
|
|
116
|
+
try {
|
|
117
|
+
const res = await (0, decision_1.runDecisionHedged)(this.logger, creds, this.decision.model, request.state, request.questions, {
|
|
118
|
+
timeoutMs: this.decision.spec.timeoutMs,
|
|
119
|
+
hedgeAfterMs: this.decision.spec.hedgeAfterMs,
|
|
120
|
+
requestId: this.deps.requestId,
|
|
121
|
+
});
|
|
122
|
+
this.backendModel = res.model;
|
|
123
|
+
this.responses += 1;
|
|
124
|
+
this.totalMs += res.ms;
|
|
125
|
+
const answer = flow.pickAnswer(res.answers) ?? null;
|
|
126
|
+
if (this.commits !== commitsAtRequest) {
|
|
127
|
+
// A newer turn committed while this request was in flight: the flow has moved on,
|
|
128
|
+
// so a plan built now would answer a context this request never saw. The turn
|
|
129
|
+
// service drops the plan anyway (superseded).
|
|
130
|
+
this.logger.log(`${this.prefix}_stale`, {
|
|
131
|
+
ms: res.ms,
|
|
132
|
+
userText,
|
|
133
|
+
choice: answer?.choice ?? null,
|
|
134
|
+
});
|
|
135
|
+
return flow.planUnclear('stale');
|
|
136
|
+
}
|
|
137
|
+
const plan = flow.planFromAnswer(answer, request);
|
|
138
|
+
const ranked = Object.entries(answer?.probabilities ?? {}).sort((a, b) => b[1] - a[1]);
|
|
139
|
+
this.logDecision(userText, plan, {
|
|
140
|
+
model: res.model,
|
|
141
|
+
ms: res.ms,
|
|
142
|
+
hedged: res.hedged,
|
|
143
|
+
dense: embedding != null,
|
|
144
|
+
embeddingMs: embedding != null ? embeddingMs : null,
|
|
145
|
+
retrievalMs,
|
|
146
|
+
totalMs: Date.now() - startedAt,
|
|
147
|
+
confidence: answer?.confidence ?? null,
|
|
148
|
+
runnerUp: ranked[1]
|
|
149
|
+
? `${ranked[1][0]}:${ranked[1][1].toFixed(3)}`
|
|
150
|
+
: null,
|
|
151
|
+
candidates: request.candidates.map((c) => `${c.key}${c.pinned ? '*' : ''}` +
|
|
152
|
+
`${c.score != null ? ` l${c.score.toFixed(1)}` : ''}` +
|
|
153
|
+
`${c.dense != null ? ` d${c.dense.toFixed(2)}` : ''}`),
|
|
154
|
+
});
|
|
155
|
+
return plan;
|
|
156
|
+
}
|
|
157
|
+
catch (err) {
|
|
158
|
+
this.errors += 1;
|
|
159
|
+
this.logger.log(`${this.prefix}_failed`, {
|
|
160
|
+
model: this.decision.model,
|
|
161
|
+
userText,
|
|
162
|
+
retrievalMs,
|
|
163
|
+
totalMs: Date.now() - startedAt,
|
|
164
|
+
err: String(err),
|
|
165
|
+
});
|
|
166
|
+
return flow.planUnclear('decision_failed');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/** The utterance's embedding for dense retrieval, or `null` (no vectors, no credentials,
|
|
170
|
+
* or the request failed / timed out — the turn then retrieves lexically only). */
|
|
171
|
+
async embed(userText) {
|
|
172
|
+
const dense = this.deps.dense;
|
|
173
|
+
if (this.index.dense == null || this.decision.creds == null || dense == null)
|
|
174
|
+
return null;
|
|
175
|
+
if (!(await this.denseUsable))
|
|
176
|
+
return null;
|
|
177
|
+
try {
|
|
178
|
+
const { vector } = await (0, dense_1.embedQuery)(this.logger, this.decision.creds, dense.model, userText, {
|
|
179
|
+
timeoutMs: dense.timeoutMs,
|
|
180
|
+
requestId: this.deps.requestId,
|
|
181
|
+
});
|
|
182
|
+
if (vector.length !== this.index.dense.dim) {
|
|
183
|
+
throw new Error(`embedding dim ${vector.length}, vectors ${this.index.dense.dim}`);
|
|
184
|
+
}
|
|
185
|
+
return vector;
|
|
186
|
+
}
|
|
187
|
+
catch (err) {
|
|
188
|
+
this.embeddingErrors += 1;
|
|
189
|
+
this.logger.log('embedding_failed', { userText, err: String(err) });
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
logDecision(userText, plan, extra) {
|
|
194
|
+
this.logger.log('qna_decision', {
|
|
195
|
+
via: plan.via,
|
|
196
|
+
userText,
|
|
197
|
+
plan: plan.kind,
|
|
198
|
+
choice: plan.choice,
|
|
199
|
+
probability: plan.probability != null ? Number(plan.probability.toFixed(3)) : null,
|
|
200
|
+
card: plan.card?.id ?? null,
|
|
201
|
+
phrases: plan.phraseIds,
|
|
202
|
+
end: plan.end,
|
|
203
|
+
transfer: plan.transfer,
|
|
204
|
+
...extra,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
planSilence(count) {
|
|
208
|
+
return this.flow.planSilence(count);
|
|
209
|
+
}
|
|
210
|
+
planUnheardSpeech() {
|
|
211
|
+
return this.flow.planUnclear('unheard_speech');
|
|
212
|
+
}
|
|
213
|
+
planBadConnection() {
|
|
214
|
+
return this.flow.terminalPlan(this.index.catalog.badConnection);
|
|
215
|
+
}
|
|
216
|
+
commit(plan, userText) {
|
|
217
|
+
this.commits += 1;
|
|
218
|
+
this.flow.commit(plan);
|
|
219
|
+
const callerTurn = userText != null && userText !== this.index.catalog.connectionSignal;
|
|
220
|
+
if (!callerTurn)
|
|
221
|
+
return;
|
|
222
|
+
this.turns += 1;
|
|
223
|
+
this.path.push(`${plan.kind}:${plan.choice ?? plan.via}`);
|
|
224
|
+
if (plan.via === 'fast')
|
|
225
|
+
this.fastTurns += 1;
|
|
226
|
+
if (plan.via === 'exact')
|
|
227
|
+
this.exactTurns += 1;
|
|
228
|
+
switch (plan.kind) {
|
|
229
|
+
case 'answer':
|
|
230
|
+
this.answers += 1;
|
|
231
|
+
if (plan.card != null)
|
|
232
|
+
this.answeredCards.push(plan.card.id);
|
|
233
|
+
this.lastUnanswered = null;
|
|
234
|
+
if (plan.operatorRequested)
|
|
235
|
+
this.operatorRequested = true;
|
|
236
|
+
break;
|
|
237
|
+
case 'unclear':
|
|
238
|
+
this.unclearTurns += 1;
|
|
239
|
+
this.lastUnanswered = userText;
|
|
240
|
+
break;
|
|
241
|
+
case 'not_found':
|
|
242
|
+
this.notFoundTurns += 1;
|
|
243
|
+
this.lastUnanswered = userText;
|
|
244
|
+
break;
|
|
245
|
+
case 'clarify':
|
|
246
|
+
this.clarifyTurns += 1;
|
|
247
|
+
break;
|
|
248
|
+
case 'transfer':
|
|
249
|
+
this.operatorRequested = true;
|
|
250
|
+
break;
|
|
251
|
+
default:
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
phraseText(id) {
|
|
256
|
+
const { catalog } = this.index;
|
|
257
|
+
const phrase = catalog.phrases[id];
|
|
258
|
+
if (phrase != null)
|
|
259
|
+
return phrase.text;
|
|
260
|
+
if (id.startsWith(flow_1.CARD_PHRASE_PREFIX)) {
|
|
261
|
+
const i = this.cardById.get(id.slice(flow_1.CARD_PHRASE_PREFIX.length));
|
|
262
|
+
if (i != null)
|
|
263
|
+
return catalog.cards[i].answer;
|
|
264
|
+
}
|
|
265
|
+
else if (id.startsWith(flow_1.TOPIC_PHRASE_PREFIX)) {
|
|
266
|
+
const i = this.topicById.get(id.slice(flow_1.TOPIC_PHRASE_PREFIX.length));
|
|
267
|
+
if (i != null)
|
|
268
|
+
return catalog.topics[i].menu;
|
|
269
|
+
}
|
|
270
|
+
else {
|
|
271
|
+
const dynamic = this.flow.dynamicText(id);
|
|
272
|
+
if (dynamic != null)
|
|
273
|
+
return dynamic;
|
|
274
|
+
}
|
|
275
|
+
this.logger.log('dialog_phrase_unknown', { id });
|
|
276
|
+
return '';
|
|
277
|
+
}
|
|
278
|
+
resolveResult(current) {
|
|
279
|
+
if (!this.deps.rewrite.has(current))
|
|
280
|
+
return current;
|
|
281
|
+
const { results } = this.index.catalog;
|
|
282
|
+
if (this.answers > 0)
|
|
283
|
+
return results.answered;
|
|
284
|
+
// The caller asked something (beyond greetings) and never got an answer.
|
|
285
|
+
if (this.unclearTurns + this.notFoundTurns + this.clarifyTurns > 0) {
|
|
286
|
+
return results.unanswered;
|
|
287
|
+
}
|
|
288
|
+
return current;
|
|
289
|
+
}
|
|
290
|
+
outcomeEntities() {
|
|
291
|
+
const nodes = [
|
|
292
|
+
...new Set(this.answeredCards.map((id) => id.split('/')[0])),
|
|
293
|
+
];
|
|
294
|
+
const entities = {
|
|
295
|
+
decision_model: this.backendModel ?? this.decision.model,
|
|
296
|
+
decision_turns: this.requests,
|
|
297
|
+
decision_errors: this.errors,
|
|
298
|
+
decision_avg_ms: this.responses > 0 ? Math.round(this.totalMs / this.responses) : null,
|
|
299
|
+
qna_embedding_errors: this.embeddingErrors,
|
|
300
|
+
qna_turns: this.turns,
|
|
301
|
+
qna_answers: this.answers,
|
|
302
|
+
qna_cards: this.answeredCards.join(' > '),
|
|
303
|
+
qna_nodes: nodes.join(', '),
|
|
304
|
+
qna_unclear_turns: this.unclearTurns,
|
|
305
|
+
qna_not_found_turns: this.notFoundTurns,
|
|
306
|
+
qna_clarify_turns: this.clarifyTurns,
|
|
307
|
+
qna_fast_turns: this.fastTurns,
|
|
308
|
+
qna_exact_turns: this.exactTurns,
|
|
309
|
+
qna_operator_requested: this.operatorRequested,
|
|
310
|
+
qna_path: this.path.join(' > '),
|
|
311
|
+
qna_last_unanswered: this.lastUnanswered,
|
|
312
|
+
};
|
|
313
|
+
return entities;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
exports.KnowledgeService = KnowledgeService;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text normalization for retrieval: the SAME functions index the knowledge base and read the caller's line, so the
|
|
3
|
+
* two always agree. Pipeline: lower case → the languages' fold → the agent's Latin-to-script word map → punctuation
|
|
4
|
+
* stripped → tokens → stopwords dropped → the languages' stem. Character trigrams of the stems give the index a second,
|
|
5
|
+
* fuzzy view that survives ASR misspellings. Pure; the language comes from locale packs (`LocalePack.words`).
|
|
6
|
+
*/
|
|
7
|
+
import type { LocaleWords } from '../locale';
|
|
8
|
+
export interface RetrievalTextOptions {
|
|
9
|
+
/** Packs of the languages that fold and stem (in order). */
|
|
10
|
+
readonly languages: readonly LocaleWords[];
|
|
11
|
+
/** Words dropped before stemming (as they stand after the fold). */
|
|
12
|
+
readonly stopwords: ReadonlySet<string>;
|
|
13
|
+
/** Latin words (lower case) an ASR engine or a card author writes for words of the agent's script. */
|
|
14
|
+
readonly transliterate?: Readonly<Record<string, string>>;
|
|
15
|
+
}
|
|
16
|
+
export declare class RetrievalText {
|
|
17
|
+
private readonly options;
|
|
18
|
+
private readonly stemCache;
|
|
19
|
+
private readonly folds;
|
|
20
|
+
private readonly stemmers;
|
|
21
|
+
constructor(options: RetrievalTextOptions);
|
|
22
|
+
/** Lower case, the languages' fold, Latin words mapped, punctuation stripped, spaces collapsed. */
|
|
23
|
+
normalize(text: string): string;
|
|
24
|
+
private stem;
|
|
25
|
+
/** Content stems of a text: normalized, stopwords dropped, stemmed. Order kept. */
|
|
26
|
+
stems(text: string): string[];
|
|
27
|
+
/** Canonical form for exact-match lookups: the stems joined by spaces. */
|
|
28
|
+
exactKey(text: string): string;
|
|
29
|
+
}
|
|
30
|
+
/** Character trigrams of every stem, with word-boundary marks (`#ab`, `abc`, …). */
|
|
31
|
+
export declare function trigrams(stemList: readonly string[]): string[];
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.trigrams = exports.RetrievalText = void 0;
|
|
4
|
+
/** Most stems remembered per text (the knowledge base repeats a few thousand words; caller text must not grow it forever). */
|
|
5
|
+
const STEM_CACHE_MAX = 50_000;
|
|
6
|
+
class RetrievalText {
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.options = options;
|
|
9
|
+
this.stemCache = new Map();
|
|
10
|
+
this.folds = options.languages.flatMap((l) => (l.fold ? [l.fold] : []));
|
|
11
|
+
this.stemmers = options.languages.flatMap((l) => (l.stem ? [l.stem] : []));
|
|
12
|
+
}
|
|
13
|
+
/** Lower case, the languages' fold, Latin words mapped, punctuation stripped, spaces collapsed. */
|
|
14
|
+
normalize(text) {
|
|
15
|
+
let lowered = text.toLowerCase();
|
|
16
|
+
for (const fold of this.folds)
|
|
17
|
+
lowered = fold(lowered);
|
|
18
|
+
const latin = this.options.transliterate ?? {};
|
|
19
|
+
return lowered
|
|
20
|
+
.replace(/[a-z]+/g, (w) => latin[w] ?? w)
|
|
21
|
+
.replace(/[^\p{L}\p{N}]+/gu, ' ')
|
|
22
|
+
.trim();
|
|
23
|
+
}
|
|
24
|
+
stem(token) {
|
|
25
|
+
let s = token;
|
|
26
|
+
for (const stem of this.stemmers)
|
|
27
|
+
s = stem(s);
|
|
28
|
+
return s;
|
|
29
|
+
}
|
|
30
|
+
/** Content stems of a text: normalized, stopwords dropped, stemmed. Order kept. */
|
|
31
|
+
stems(text) {
|
|
32
|
+
const out = [];
|
|
33
|
+
for (const token of this.normalize(text).split(' ')) {
|
|
34
|
+
if (!token || this.options.stopwords.has(token))
|
|
35
|
+
continue;
|
|
36
|
+
let s = this.stemCache.get(token);
|
|
37
|
+
if (s === undefined) {
|
|
38
|
+
s = this.stem(token);
|
|
39
|
+
if (this.stemCache.size < STEM_CACHE_MAX)
|
|
40
|
+
this.stemCache.set(token, s);
|
|
41
|
+
}
|
|
42
|
+
out.push(s);
|
|
43
|
+
}
|
|
44
|
+
return out;
|
|
45
|
+
}
|
|
46
|
+
/** Canonical form for exact-match lookups: the stems joined by spaces. */
|
|
47
|
+
exactKey(text) {
|
|
48
|
+
return this.stems(text).join(' ');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.RetrievalText = RetrievalText;
|
|
52
|
+
/** Character trigrams of every stem, with word-boundary marks (`#ab`, `abc`, …). */
|
|
53
|
+
function trigrams(stemList) {
|
|
54
|
+
const out = [];
|
|
55
|
+
for (const s of stemList) {
|
|
56
|
+
const w = `#${s}#`;
|
|
57
|
+
if (w.length <= 3) {
|
|
58
|
+
out.push(w);
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
for (let i = 0; i + 3 <= w.length; i += 1)
|
|
62
|
+
out.push(w.slice(i, i + 3));
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
exports.trigrams = trigrams;
|