Skip to content

The open-source AI receptionist for businesses. Answers every call, books the appointment and hands over to a person when needed. You own the code and the data.

2.9 KB
Raw Blame
// When an outbound call's first sentence starts (LIN-873). The person who picks up usually greets first ("Restaurant
// Kant, guten Tag"): the assistant waits for the end of that greeting instead of talking over it, and starts by itself
// when nobody says anything. The person's greeting is kept in the conversation, but gets no reply of its own: the first
// sentence is the answer to it.

/** Nobody spoke this long after the pickup: the assistant starts. */
export const OPENING_SILENCE_MS = 2_000
/** Quiet this long after the person spoke, without an end of their turn: their greeting is over. */
export const OPENING_PAUSE_MS = 1_000
/** Longest the first sentence waits, also while the person keeps talking. */
export const OPENING_MAX_MS = 8_000

/** Why the first sentence started: nobody spoke, their turn ended, they went quiet, or the wait was at its longest. */
export type OpeningReason = 'silence' | 'turn' | 'pause' | 'longest'

export type OpeningGate = {
  /** The person started (true) or stopped (false) speaking. */
  speaking(on: boolean): void
  /** A transcript of the person's words came in. */
  heard(): void
  /**
   * The person's turn ended. True while the first sentence is still waiting: that turn (their greeting) starts no reply,
   * and the first sentence starts now. False afterwards: the turn is answered as usual.
   */
  takeTurn(): boolean
  /** Resolves once the first sentence may start. */
  ready: Promise<OpeningReason>
  /** Stops the timers: the call ended before the first sentence (`ready` then never resolves). */
  close(): void
}

/** The wait for the first sentence; its timers start now (at the pickup). */
export function openingGate(): OpeningGate {
  let done = false
  let spoke = false
  let speakingNow = false
  let pause: ReturnType<typeof setTimeout> | undefined
  let resolveReady: (reason: OpeningReason) => void = () => {}
  const ready = new Promise<OpeningReason>(resolve => (resolveReady = resolve))
  const finish = (reason: OpeningReason) => {
    if (done) return
    done = true
    clearTimeout(silence)
    clearTimeout(longest)
    clearTimeout(pause)
    resolveReady(reason)
  }
  const silence = setTimeout(() => !spoke && finish('silence'), OPENING_SILENCE_MS)
  const longest = setTimeout(() => finish('longest'), OPENING_MAX_MS)
  const restartPause = () => {
    clearTimeout(pause)
    pause = setTimeout(() => finish('pause'), OPENING_PAUSE_MS)
  }
  return {
    speaking(on) {
      if (done) return
      speakingNow = on
      if (on) {
        spoke = true
        clearTimeout(pause)
      } else if (spoke) restartPause()
    },
    heard() {
      if (done) return
      spoke = true
      if (!speakingNow) restartPause()
    },
    takeTurn() {
      if (done) return false
      finish('turn')
      return true
    },
    ready,
    close() {
      done = true
      clearTimeout(silence)
      clearTimeout(longest)
      clearTimeout(pause)
    },
  }
}