Utils

March 10, 2026 · View on GitHub

Package utils provides shared utilities: backoff, notifier, sentence-boundary detection, and text/pattern aggregation for LLM streams.

Purpose

  • backoff: Exponential backoff for retries/reconnection (e.g. WebSocket).
  • notifier: One-shot signal: one goroutine waits, another notifies (Wait/Notify).
  • sentence: Sentence-end detection (e.g. .!?) for aggregating text.
  • patternaggregator: Delimiter-based aggregation (e.g. ``) to extract tagged content from LLM output (IVR, DTMF).
  • textaggregator: Interface and implementations to aggregate incremental text (e.g. LLM tokens) into segments (e.g. sentences).

Utility tree

graph TD
    Root["utils"] --> Backoff["backoff\nExponentialBackoff\nBackoffCap"]
    Root --> Notifier["notifier\nNotifier\nWait / Notify"]
    Root --> Sentence["sentence\nMatchEndOfSentence\nDefaultSentenceEnd"]
    Root --> PatternAgg["patternaggregator\nAggregator\nFeed / Flush\nMatch"]
    Root --> TextAgg["textaggregator\nAggregator interface\nSegment\nsentence impl"]

Exported symbols (root)

SymbolTypeDescription
BackoffCapconstMax duration (60s) for ExponentialBackoff
ExponentialBackoff(attempt)funcReturns 2^attempt seconds, capped at BackoffCap; attempt 1-based

Subpackages

PathExported symbolsDescription
notifierNotifier, New, Notify, WaitOne waiter unblocked per Notify; safe for concurrent use
sentenceDefaultSentenceEnd, MatchEndOfSentence(s, endChars)Reports if trimmed s ends with sentence-ending rune
patternaggregatorMatch, Aggregator, New(open, close), Feed, FlushEmits text segments and delimiter matches from stream
textaggregatorSegment, Aggregator (interface: Aggregate, Flush, Reset, HandleInterruption)Aggregate incremental text into segments; sentence implementation

Concurrency

  • backoff: Stateless; safe for concurrent use.
  • notifier: Notify and Wait are safe for concurrent use; mutex protects the channel.
  • sentence: Stateless; safe for concurrent use.
  • patternaggregator: Aggregator is not safe for concurrent use; one caller should Feed/Flush.
  • textaggregator: Implementations are typically single-threaded per aggregator instance.

Files

FileDescription
backoff.goBackoffCap, ExponentialBackoff
notifier/notifier.goNotifier, New, Notify, Wait
sentence/sentence.goDefaultSentenceEnd, MatchEndOfSentence
patternaggregator/patternaggregator.goMatch, Aggregator, New, Feed, Flush
textaggregator/textaggregator.goSegment, Aggregator interface
textaggregator/sentence.goSentence-based aggregator implementation

See also