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.
TypeScript ai-receptionistanswering-serviceappointment-bookinglivekitlocal-firstmcpopen-sourceself-hostedsiptelephonytypescriptvoice-agentvoice-ai
import type { TranscriptSegment } from '@linedesk/db/types'
import { expect, test } from 'vitest'
import {
applyChunk,
FINAL_LINE_TTL_MS,
finishLine,
type LiveLines,
pendingLines,
pruneLines,
rowsOf,
speakerOf,
} from './liveTranscript.ts'
const row = (speaker: TranscriptSegment['speaker'], text: string, n = 0) =>
({
id: `r${n}-${text}`,
call_id: 'c',
speaker,
text,
at_ms: n,
tool_event: null,
version: n,
}) as unknown as TranscriptSegment
const chunk = (
id: string,
speaker: 'caller' | 'agent' | 'unknown',
text: string,
extra: Partial<Parameters<typeof applyChunk>[1]> = {},
) => ({
id,
speaker,
text,
mode: speaker === 'agent' ? ('append' as const) : ('replace' as const),
final: false,
now: 1000,
rowsBefore: 0,
...extra,
})
test("the caller's recognised speech replaces the line, the assistant's words add to it", () => {
let lines: LiveLines = []
lines = applyChunk(lines, chunk('u1', 'caller', 'my sink'))
lines = applyChunk(lines, chunk('u1', 'caller', 'my sink is leaking'))
lines = applyChunk(lines, chunk('a1', 'agent', 'Sorry to '))
lines = applyChunk(lines, chunk('a1', 'agent', 'hear that.'))
expect(lines.map(l => [l.speaker, l.text])).toEqual([
['caller', 'my sink is leaking'],
['agent', 'Sorry to hear that.'],
])
})
test('a line disappears once its transcript row has synced in', () => {
const lines = applyChunk([], chunk('u1', 'caller', 'My sink is leaking', { final: true }))
expect(pendingLines(lines, [], 1000)).toHaveLength(1)
expect(pendingLines(lines, [row('caller', 'My sink is leaking.')], 1000)).toHaveLength(0)
})
test('an interrupted reply matches the row of the full reply', () => {
const lines = finishLine(applyChunk([], chunk('a1', 'agent', 'We can come by on')), 'a1', 1000)
expect(pendingLines(lines, [row('agent', 'We can come by on Tuesday at nine.')], 1000)).toHaveLength(0)
})
test('an earlier row with the same words does not hide a new line', () => {
const rows = [row('caller', 'Yes', 1)]
const lines = applyChunk([], chunk('u2', 'caller', 'yes', { rowsBefore: 1 }))
expect(pendingLines(lines, rows, 1000)).toHaveLength(1)
expect(pendingLines(lines, [...rows, row('caller', 'Yes.', 2)], 1000)).toHaveLength(0)
})
test('a finished line without a row is dropped after a while; one still spoken stays', () => {
let lines = applyChunk([], chunk('u1', 'caller', 'hello', { final: true, now: 0 }))
lines = applyChunk(lines, chunk('a1', 'agent', 'Hi there', { now: 0 }))
expect(pendingLines(lines, [], FINAL_LINE_TTL_MS - 1)).toHaveLength(2)
expect(pendingLines(lines, [], FINAL_LINE_TTL_MS).map(l => l.id)).toEqual(['a1'])
expect(pruneLines(lines, [], FINAL_LINE_TTL_MS).map(l => l.id)).toEqual(['a1'])
})
test('empty text is never shown', () => {
expect(pendingLines(applyChunk([], chunk('u1', 'caller', ' ')), [], 0)).toHaveLength(0)
})
// A listened-to call: the agent (kind AGENT, its own microphone TR_agent) and the SIP caller (TR_caller).
const room = (callerIdentity: string) => ({
remoteParticipants: new Map([
['agent-1', { isAgent: true, trackPublications: new Map([['TR_agent', {}]]) }],
[callerIdentity, { isAgent: false, trackPublications: new Map([['TR_caller', {}]]) }],
]),
})
const about = (track?: string, final = false) => ({
'lk.transcription_final': String(final),
...(track ? { 'lk.transcribed_track_id': track } : {}),
})
test("the caller's speech sent in the caller's name is the caller's, while it is recognised and once final", () => {
// Inbound (sip_+1503…) and outbound (outbound-callee) callers alike; with or without the track known yet.
for (const identity of ['sip_+15035550147', 'outbound-callee']) {
expect(speakerOf(about('TR_caller'), { identity }, room(identity))).toBe('caller')
expect(speakerOf(about('TR_caller', true), { identity }, room(identity))).toBe('caller')
expect(speakerOf(about(), { identity }, room(identity))).toBe('caller')
}
})
test("the assistant's own speech is the assistant's, with or without its track", () => {
const r = room('outbound-callee')
expect(speakerOf(about('TR_agent'), { identity: 'agent-1' }, r)).toBe('agent')
expect(speakerOf(about(), { identity: 'agent-1' }, r)).toBe('agent')
})
test("a stream the agent sends about the caller's track is the caller's", () => {
expect(speakerOf(about('TR_caller'), { identity: 'agent-1' }, room('outbound-callee'))).toBe('caller')
})
test('a sender not in the room is unknown, never guessed', () => {
expect(speakerOf(about('TR_caller'), { identity: 'someone' }, room('outbound-callee'))).toBe('unknown')
})
test("an unknown speaker's line counts and gives way to either side's row", () => {
const rows = [row('caller', 'Hi', 1), row('agent', 'Hello, Brightwater Plumbing', 2)]
expect(rowsOf(rows, 'unknown')).toBe(2)
const lines = applyChunk([], chunk('u', 'unknown', 'My boiler is leaking', { rowsBefore: 2 }))
expect(pendingLines(lines, rows, 1000).map(l => l.speaker)).toEqual(['unknown'])
const synced = [...rows, row('caller', 'My boiler is leaking.', 3)]
expect(pendingLines(lines, synced, 1000)).toEqual([])
})