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
// Dialogs that start something have an address (design principles, rule 20): `#contacts/new`, `#contacts/import`,
// `#outbound/new`, `#workspace/phone/add`, and the keyboard shortcuts as `?help` on the page they were opened on.
// A link or a reload opens the page with the dialog; nothing is sent before the person acts in it. Opening adds a
// history entry, so Back closes the dialog and stays on the page; Esc, Close, Cancel and Save do the same. Dialogs
// that confirm something (delete, leave, block) never get an address: a link must not open "Delete?".
import { useCallback, useEffect, useRef, useState } from 'react'
/** The dialogs with an address of their own, and the page each lies on (its address when the dialog is closed). */
export const DIALOG_PLACES = {
'contacts/new': 'contacts',
'contacts/import': 'contacts',
'outbound/new': 'outbound',
'workspace/phone/add': 'workspace/phone',
} as const
export type DialogPlace = keyof typeof DIALOG_PLACES
const pathOf = (hash: string) => hash.replace(/^#/, '').split('?')[0]!
/** The dialog a hash names, or null. */
export const dialogOf = (hash: string): DialogPlace | null =>
Object.hasOwn(DIALOG_PLACES, pathOf(hash)) ? (pathOf(hash) as DialogPlace) : null
/** The flag in a hash's query that says the keyboard shortcuts are open: `#calls?help`, `#calls?q=leak&help`. */
export const HELP_FLAG = 'help'
const flagsOf = (hash: string) => hash.split('?')[1]?.split('&').filter(Boolean) ?? []
export const hasFlag = (hash: string, flag: string) => flagsOf(hash).includes(flag)
export function withFlag(hash: string, flag: string, on: boolean): string {
const flags = flagsOf(hash).filter(f => f !== flag)
if (on) flags.push(flag)
return `#${pathOf(hash)}${flags.length ? `?${flags.join('&')}` : ''}`
}
/**
* The shared mechanics: `isOpen(hash)` says whether the address names the dialog; `opened(hash)` and `closed(hash)`
* give the address with and without it. `pushed`: this tab added the dialog's history entry, so closing is a step
* back; a dialog opened by a link or a reload has no entry of the page under it, and closing rewrites the address.
*/
function useAddress(
isOpen: (hash: string) => boolean,
opened: (hash: string) => string,
closed: (hash: string) => string,
allowed: boolean,
): [open: boolean, show: () => void, hide: () => void] {
const [open, setOpen] = useState(() => isOpen(window.location.hash))
const pushed = useRef(false)
const fns = useRef({ isOpen, opened, closed })
fns.current = { isOpen, opened, closed }
useEffect(() => {
const onHash = () => {
const now = fns.current.isOpen(window.location.hash)
if (!now) pushed.current = false
setOpen(now)
}
window.addEventListener('hashchange', onHash)
return () => window.removeEventListener('hashchange', onHash)
}, [])
const show = useCallback(() => {
if (fns.current.isOpen(window.location.hash)) return
pushed.current = true
setOpen(true)
window.location.hash = fns.current.opened(window.location.hash)
}, [])
const hide = useCallback(() => {
setOpen(false)
if (!fns.current.isOpen(window.location.hash)) return
if (pushed.current) {
pushed.current = false
window.history.back()
} else window.history.replaceState(null, '', fns.current.closed(window.location.hash))
}, [])
// No right for the dialog: the page, without it, and the address says so.
useEffect(() => {
if (!allowed && open) hide()
}, [allowed, open, hide])
return [open && allowed, show, hide]
}
/** A dialog with its own address. `allowed` false: it never opens, and a link to it shows the page. */
export function useDialogPlace(place: DialogPlace, allowed = true) {
return useAddress(
hash => pathOf(hash) === place,
() => `#${place}`,
() => `#${DIALOG_PLACES[place]}`,
allowed,
)
}
/** A dialog that opens over any page: a flag in the address's query (`?help`). */
export function useDialogFlag(flag: string) {
return useAddress(
hash => hasFlag(hash, flag),
hash => withFlag(hash || '#calls', flag, true),
hash => withFlag(hash, flag, false),
true,
)
}