// Typed wrappers around the Tauri commands and events of the app shell. import { invoke } from '@tauri-apps/api/core' import { listen, type UnlistenFn } from '@tauri-apps/api/event' export type CallId = number export interface Settings { allowlist: string[] mic_device: string | null output_device: string | null language: string deepscript_model: string retention_days: number autostart: boolean start_after_secs: number end_after_secs: number } export type Service = 'deepscript' | 'anthropic' export interface DeviceInfo { id: string name: string } export interface Devices { inputs: DeviceInfo[] outputs: DeviceInfo[] } export interface DeviceLevels { mic: number out: number } export type Track = 'mic' | 'out' export type RecordingState = | { state: 'idle' } | { state: 'recording'; call_id: CallId; app: string; started_at: string } /** What the end-of-call prompt shows about a call. Times are RFC 3339. */ export interface CallBrief { id: CallId app: string started_at: string ended_at: string | null duration_secs: number } /** The call the end-of-call prompt shows, and how many wait behind it. */ export interface PendingPrompt extends CallBrief { more_waiting: number } export type CallStatus = 'recording' | 'awaiting_decision' | 'processing' | 'done' | 'failed' /** A row of the main window's call list. Times are RFC 3339. */ export interface CallListItem { id: CallId app: string started_at: string /** 0 while recording. */ duration_secs: number status: CallStatus interrupted: boolean /** The name the user gave the call, if any. */ title: string | null } /** A transcript line; `speaker_key` is "me" or "remote:". */ export interface SegmentView { speaker_key: string label: string start: number end: number text: string } /** A merged speaker: its lines now belong to `into_key`. */ export interface SpeakerMerge { from_key: string from_label: string into_key: string into_label: string } export interface CallDetail { call: CallListItem & { error: string | null; account_id: string | null } /** The transcript with speaker merges applied. */ segments: SegmentView[] /** Renamed speakers by key. */ speaker_names: Record merges: SpeakerMerge[] } export interface Account { id: string name: string } export interface Levels { mic: number out: number failed_tracks: Track[] } export const getSettings = () => invoke('get_settings') export const saveSettings = (settings: Settings) => invoke('save_settings', { settings }) export const setApiKey = (service: Service, key: string) => invoke('set_api_key', { service, key }) export const hasApiKey = (service: Service) => invoke('has_api_key', { service }) export const listDevices = () => invoke('list_devices') export const testDevices = () => invoke('test_devices') export const getRecordingState = () => invoke('get_recording_state') export const startManualRecording = () => invoke('start_manual_recording') export const stopRecording = () => invoke('stop_recording') export const discardCurrentRecording = () => invoke('discard_current_recording') export const getPendingPrompt = () => invoke('get_pending_prompt') export const getCallBrief = (id: CallId) => invoke('get_call_brief', { id }) export const transcribeCall = (id: CallId) => invoke('transcribe_call', { id }) export const retryCall = (id: CallId) => invoke('retry_call', { id }) export const discardCall = (id: CallId) => invoke('discard_call', { id }) export const listCalls = () => invoke('list_calls') export const getCall = (id: CallId) => invoke('get_call', { id }) /** An empty name restores the default label. */ export const renameSpeaker = (id: CallId, speakerKey: string, name: string) => invoke('rename_speaker', { id, speakerKey, name }) export const mergeSpeakers = (id: CallId, fromKey: string, intoKey: string) => invoke('merge_speakers', { id, fromKey, intoKey }) export const unmergeSpeaker = (id: CallId, fromKey: string) => invoke('unmerge_speaker', { id, fromKey }) /** `null` or an empty title restores the default heading. */ export const renameCall = (id: CallId, title: string | null) => invoke('rename_call', { id, title }) export const listAccounts = () => invoke('list_accounts') export const setAccount = (id: CallId, accountId: string | null) => invoke('set_account', { id, accountId }) export const onRecordingState = (handler: (state: RecordingState) => void): Promise => listen('recording-state', (event) => handler(event.payload)) export const onCallEnded = (handler: (id: CallId) => void): Promise => listen<{ id: CallId }>('call-ended', (event) => handler(event.payload.id)) /** The prompt queue changed (a call was decided elsewhere). */ export const onPromptChanged = (handler: () => void): Promise => listen('prompt-changed', () => handler()) /** A call's status changed (processing started, done or failed). */ export const onCallUpdated = (handler: (id: CallId) => void): Promise => listen<{ id: CallId }>('call-updated', (event) => handler(event.payload.id)) export const onLevels = (handler: (levels: Levels) => void): Promise => listen('levels', (event) => handler(event.payload)) /** Maps an rms level (0..1) to a meter fill (0..1) on a -60..0 dB scale. */ export function meterFill(level: number): number { if (level <= 0) return 0 const db = 20 * Math.log10(level) return Math.min(1, Math.max(0, (db + 60) / 60)) } /** Error text of a rejected command. */ export function errorText(err: unknown): string { return typeof err === 'string' ? err : err instanceof Error ? err.message : String(err) }