diff --git a/docs/screenshot.png b/docs/screenshot.png index 1a12261..aedf883 100644 Binary files a/docs/screenshot.png and b/docs/screenshot.png differ diff --git a/src/main/index.ts b/src/main/index.ts index 398a7b9..b494d96 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1,11 +1,12 @@ -import { app, BrowserWindow, ipcMain, nativeImage, session, shell } from 'electron' +import { app, BrowserWindow, ipcMain, nativeImage, nativeTheme, session, shell } from 'electron' import { mkdir, writeFile } from 'node:fs/promises' import { join } from 'node:path' import { tmpdir } from 'node:os' import { electronApp, optimizer, is } from '@electron-toolkit/utils' import { createPage, deletePage, getPage, listPages, savePage } from './pages' import { registerUpdater } from './updater' -import { broadcastStatus, ensureModel, pcmFromIpc, transcribePcm } from './whisper' +import { broadcastStatus, pcmFromIpc, transcribePcm, warmUp } from './whisper' +import { log, logPath } from './log' import type { Page } from '../shared/types' if (process.env.VOICE_PAGES_SMOKE === '1' || process.env.VOICE_PAGES_SCREENSHOT === '1') { @@ -26,7 +27,10 @@ function createWindow(): void { show: false, autoHideMenuBar: true, title: 'Lyra', - backgroundColor: '#f4efe6', + backgroundColor: nativeTheme.shouldUseDarkColors ? '#18181b' : '#fafafa', + ...(process.platform === 'darwin' + ? { titleBarStyle: 'hiddenInset' as const, trafficLightPosition: { x: 16, y: 18 } } + : {}), ...(icon.isEmpty() ? {} : { icon }), webPreferences: { preload: join(__dirname, '../preload/index.js'), @@ -75,10 +79,13 @@ function registerIpc(): void { savePage(page) ) ipcMain.handle('pages:delete', (_event, id: string) => deletePage(id)) - ipcMain.handle('whisper:ensureModel', () => ensureModel()) - ipcMain.handle('whisper:transcribe', async (_event, payload: unknown) => { - return transcribePcm(pcmFromIpc(payload)) + ipcMain.on('log:write', (_event, scope: unknown, message: unknown, data: unknown) => { + log(String(scope), String(message), data) }) + ipcMain.handle('whisper:warmUp', () => warmUp()) + ipcMain.handle('whisper:transcribe', (_event, payload: unknown, prompt: unknown) => + transcribePcm(pcmFromIpc(payload), typeof prompt === 'string' ? prompt : '') + ) } async function seedScreenshotPages(): Promise { @@ -164,6 +171,7 @@ async function runUiSmoke(win: BrowserWindow): Promise { app.whenReady().then(() => { electronApp.setAppUserModelId('com.voicepages.app') + log('app', `started ${app.getVersion()}`, { log: logPath() }) const icon = appIcon() if (process.platform === 'darwin' && !icon.isEmpty()) { diff --git a/src/main/log.ts b/src/main/log.ts new file mode 100644 index 0000000..86fccad --- /dev/null +++ b/src/main/log.ts @@ -0,0 +1,53 @@ +import { app } from 'electron' +import { appendFileSync, mkdirSync, renameSync, statSync } from 'node:fs' +import { join } from 'node:path' + +/** + * A tiny line logger for diagnosing dictation. Lines go to the terminal and to + * `/logs/lyra.log`, which is rotated once it passes a few megabytes. + */ + +const MAX_BYTES = 5 * 1024 * 1024 + +let file: string | null = null + +function logFile(): string | null { + if (file) return file + try { + const dir = join(app.getPath('userData'), 'logs') + mkdirSync(dir, { recursive: true }) + file = join(dir, 'lyra.log') + if ((statSync(file, { throwIfNoEntry: false })?.size ?? 0) > MAX_BYTES) { + renameSync(file, join(dir, 'lyra.old.log')) + } + } catch { + file = null + } + return file +} + +export function logPath(): string | null { + return logFile() +} + +function format(data: unknown): string { + if (data === undefined) return '' + if (data instanceof Error) return ` ${data.stack ?? data.message}` + try { + return ` ${JSON.stringify(data)}` + } catch { + return ` ${String(data)}` + } +} + +export function log(scope: string, message: string, data?: unknown): void { + const line = `${new Date().toISOString()} [${scope}] ${message}${format(data)}` + console.log(line) + const path = logFile() + if (!path) return + try { + appendFileSync(path, `${line}\n`) + } catch { + // Logging must never break dictation. + } +} diff --git a/src/main/whisper.ts b/src/main/whisper.ts index bce1e05..e50052c 100644 --- a/src/main/whisper.ts +++ b/src/main/whisper.ts @@ -1,13 +1,13 @@ import { app, BrowserWindow } from 'electron' import { createRequire } from 'node:module' import { createWriteStream } from 'node:fs' -import { mkdir, rename, stat, unlink, writeFile } from 'node:fs/promises' -import { arch, platform, tmpdir } from 'node:os' +import { mkdir, rename, stat, unlink } from 'node:fs/promises' +import { arch, platform } from 'node:os' import { join } from 'node:path' import { promisify } from 'node:util' -import { randomUUID } from 'node:crypto' import { finished } from 'node:stream/promises' -import type { WhisperStatus } from '../shared/types' +import type { TranscriptWord, WhisperStatus } from '../shared/types' +import { log } from './log' const MODEL_NAME = 'ggml-base.en.bin' const MODEL_URL = @@ -128,107 +128,143 @@ export function pcmFromIpc(payload: unknown): Float32Array { return copy } -function extractText(result: { transcription?: unknown } | string[] | string[][]): string { - const value = - result && typeof result === 'object' && 'transcription' in result - ? result.transcription - : result - if (value == null) return '' - if (typeof value === 'string') return cleanTranscript(value) - if (!Array.isArray(value)) return cleanTranscript(String(value)) - return cleanTranscript( - value - .map((row) => (Array.isArray(row) ? String(row[row.length - 1] ?? '') : String(row))) - .join(' ') - ) -} - -function cleanTranscript(text: string): string { - const cleaned = text - .replace(/\([^)]*\)/g, ' ') - .replace(/\[[^\]]*\]/g, ' ') - .replace(/\s+/g, ' ') - .trim() - if (!cleaned) return '' - const lower = cleaned.toLowerCase().replace(/[.?!,]+$/g, '') - const junk = new Set(['you', 'thank you', 'thanks for watching', 'bye', 'okay', 'ok', '...', '.']) - return junk.has(lower) ? '' : cleaned -} - -function encodeWav16k(pcm: Float32Array): Buffer { - const data = Buffer.alloc(pcm.length * 2) - for (let i = 0; i < pcm.length; i++) { - const sample = Math.max(-1, Math.min(1, pcm[i] ?? 0)) - data.writeInt16LE(Math.round(sample * 32767), i * 2) - } +const HALLUCINATIONS = new Set(['you', 'thank you', 'thanks for watching', 'bye', '.', '...']) + +function seconds(stamp: unknown): number { + const match = /^(\d+):(\d+):(\d+)[,.](\d+)$/.exec(String(stamp)) + if (!match) return 0 + const [, h, m, s, ms] = match.map(Number) + return h * 3600 + m * 60 + s + ms / 1000 +} - const header = Buffer.alloc(44) - header.write('RIFF', 0) - header.writeUInt32LE(36 + data.length, 4) - header.write('WAVE', 8) - header.write('fmt ', 12) - header.writeUInt32LE(16, 16) - header.writeUInt16LE(1, 20) - header.writeUInt16LE(1, 22) - header.writeUInt32LE(16000, 24) - header.writeUInt32LE(32000, 28) - header.writeUInt16LE(2, 32) - header.writeUInt16LE(16, 34) - header.write('data', 36) - header.writeUInt32LE(data.length, 40) - return Buffer.concat([header, data]) -} - -function pcmStats(pcm: Float32Array): { min: number; max: number; rms: number } { - let min = Infinity - let max = -Infinity - let sum = 0 - for (let i = 0; i < pcm.length; i++) { - const value = pcm[i] ?? 0 - if (value < min) min = value - if (value > max) max = value - sum += value * value +/** + * With `max_len: 1` Whisper returns one row per token: [start, end, text]. + * Tokens that start with a space begin a new word; the rest (punctuation, + * word pieces) attach to the previous word. Bracketed annotations such as + * [BLANK_AUDIO] or (music) are dropped. + */ +function wordsFromTokens(result: { transcription?: unknown }): TranscriptWord[] { + const rows = Array.isArray(result?.transcription) ? result.transcription : [] + const words: TranscriptWord[] = [] + let bracketDepth = 0 + + for (const row of rows) { + if (!Array.isArray(row) || row.length < 3) continue + const text = String(row[2] ?? '') + const start = seconds(row[0]) + const end = seconds(row[1]) + const opens = (text.match(/[[(]/g) ?? []).length + const closes = (text.match(/[\])]/g) ?? []).length + const inside = bracketDepth > 0 || opens > 0 + bracketDepth = Math.max(0, bracketDepth + opens - closes) + if (inside || !text.trim()) continue + + const last = words.at(-1) + if (last && !/^\s/.test(text)) { + last.text += text + last.end = end + } else { + words.push({ text: ` ${text.trim()}`, start, end }) + } } - return { min, max, rms: Math.sqrt(sum / Math.max(1, pcm.length)) } + // Whisper marks speech that runs past the end of the buffer with "..." — not dictation. + return words + .map((word) => ({ ...word, text: word.text.replace(/(?:\.\.\.|…)+/g, '') })) + .filter((word) => word.text.trim()) } -export async function transcribePcm(pcm: Float32Array): Promise { - if (pcm.length < 6400) return '' +function normalize(text: string): string { + return text.toLowerCase().replace(/[^a-z0-9 ]/g, '').replace(/\s+/g, ' ').trim() +} - await ensureModel() - const stats = pcmStats(pcm) - console.log( - `pcm: n=${pcm.length} min=${stats.min.toFixed(3)} max=${stats.max.toFixed(3)} rms=${stats.rms.toFixed(3)}` - ) +/** Drops the stock phrases Whisper invents on silence, and echoes of its own prompt. */ +function isHallucination(words: TranscriptWord[], prompt: string): boolean { + const text = normalize(words.map((word) => word.text).join('')) + if (!text || HALLUCINATIONS.has(text)) return true + return text.length > 12 && normalize(prompt).includes(text) +} + +const BASE_OPTIONS = { + language: 'en', + translate: false, + no_timestamps: false, + // One row per token, so the renderer gets word-level timestamps. + max_len: 1, + no_prints: true +} + +let useGpu = true +// The native addon shares one whisper context, so calls must never overlap. +let queue: Promise = Promise.resolve() - const wavPath = join(tmpdir(), `voice-pages-${randomUUID()}.wav`) - await writeFile(wavPath, encodeWav16k(pcm)) +function runSerial(task: () => Promise): Promise { + const next = queue.then(task, task) + queue = next.catch(() => undefined) + return next +} +async function runWhisper(pcm: Float32Array, prompt: string): Promise { const transcribe = loadTranscribe() - const options = { - model: modelPath(), - fname_inp: wavPath, - language: 'en', - translate: false, - no_timestamps: true, - no_prints: true, - temperature: 0 + const options = { ...BASE_OPTIONS, model: modelPath(), pcmf32: pcm, prompt } + try { + return wordsFromTokens(await transcribe({ ...options, use_gpu: useGpu })) + } catch (gpuError) { + if (!useGpu) throw gpuError + log('whisper', 'GPU transcription failed, falling back to CPU', gpuError) + useGpu = false + return wordsFromTokens(await transcribe({ ...options, use_gpu: false })) } +} + +let warmed: Promise | null = null + +/** Loads the model into memory ahead of the first real utterance (~3s cold start). */ +export function warmUp(): Promise { + warmed ??= ensureModel() + .then(() => runSerial(() => runWhisper(new Float32Array(16000), ''))) + .then(() => undefined) + .catch((error) => { + warmed = null + throw error + }) + return warmed +} + +/** Transcribes 16 kHz mono PCM into words with timestamps relative to the start of `pcm`. */ +export async function transcribePcm(pcm: Float32Array, prompt = ''): Promise { + if (pcm.length < 4800) return [] + await ensureModel() + const queuedAt = Date.now() + let startedAt = 0 + // whisper.cpp can't be cancelled, so a hung call blocks every later one; make that visible. + const watchdog = setInterval(() => { + log('whisper', 'transcription is taking unusually long', { + waitedMs: Date.now() - queuedAt, + runningMs: startedAt ? Date.now() - startedAt : 0, + audioSec: +(pcm.length / 16000).toFixed(2) + }) + }, 10_000) - broadcastStatus({ state: 'transcribing' }) try { - let result: { transcription?: unknown } - try { - result = await transcribe({ ...options, use_gpu: true }) - } catch (gpuError) { - console.warn('GPU transcription failed, retrying on CPU', gpuError) - result = await transcribe({ ...options, use_gpu: false }) - } - const text = extractText(result) - console.log(`whisper: ${JSON.stringify(text)}`) - return text + const words = await runSerial(() => { + startedAt = Date.now() + return runWhisper(pcm, prompt) + }) + const text = words.map((word) => word.text).join('').trim() + const dropped = isHallucination(words, prompt) + log('whisper', dropped ? 'dropped as hallucination' : 'transcribed', { + audioSec: +(pcm.length / 16000).toFixed(2), + queuedMs: startedAt - queuedAt, + runMs: Date.now() - startedAt, + gpu: useGpu, + promptChars: prompt.length, + text + }) + return dropped ? [] : words + } catch (error) { + log('whisper', 'transcription failed', error) + throw error } finally { - await unlink(wavPath).catch(() => undefined) - broadcastStatus({ state: 'idle' }) + clearInterval(watchdog) } } diff --git a/src/preload/index.ts b/src/preload/index.ts index c71759c..1cfc3cd 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -8,8 +8,10 @@ const api: VoicePagesApi = { savePage: (page) => ipcRenderer.invoke('pages:save', page), deletePage: (id) => ipcRenderer.invoke('pages:delete', id), copyText: (text) => clipboard.writeText(text), - ensureModel: () => ipcRenderer.invoke('whisper:ensureModel'), - transcribe: (pcm) => ipcRenderer.invoke('whisper:transcribe', pcm), + platform: process.platform, + warmUp: () => ipcRenderer.invoke('whisper:warmUp'), + transcribe: (pcm, prompt) => ipcRenderer.invoke('whisper:transcribe', pcm, prompt), + log: (scope, message, data) => ipcRenderer.send('log:write', scope, message, data), onStatus: (callback) => { const listener = (_event: Electron.IpcRendererEvent, status: WhisperStatus): void => { callback(status) diff --git a/src/renderer/src/App.tsx b/src/renderer/src/App.tsx index e6de8de..a502d0c 100644 --- a/src/renderer/src/App.tsx +++ b/src/renderer/src/App.tsx @@ -1,504 +1,192 @@ -import { useCallback, useEffect, useMemo, useRef, useState } from 'react' -import { startCapture, type CaptureHandle } from './audio/capture' -import lyraIcon from './assets/lyra-icon.png' -import type { Page, PageSummary, UpdateStatus, WhisperStatus } from '../../shared/types' - -function formatUpdated(value: string): string { - const date = new Date(value) - if (Number.isNaN(date.getTime())) return '' - return date.toLocaleString(undefined, { - month: 'short', - day: 'numeric', - hour: 'numeric', - minute: '2-digit' - }) -} - -function wordCount(text: string): number { - const words = text.trim().match(/\S+/g) - return words ? words.length : 0 -} - -function statusLabel( - status: WhisperStatus, - recording: boolean, - micLevel: number, - micName: string -): string { - if (status.state === 'downloading') { - if (!status.total) return 'Downloading Whisper model…' - const pct = Math.round((status.received / status.total) * 100) - return `Downloading Whisper model… ${pct}%` - } - if (status.state === 'error') return status.message - if (recording) { - if (micLevel < 0.04) { - return `${micName || 'Mic'} is silent — talk louder, or enable it in System Settings → Microphone` - } - return `Listening on ${micName || 'mic'} · edit anytime` - } - if (status.state === 'transcribing') return 'Transcribing…' - return 'Idle · everything stays on this computer' -} - -function updateChipLabel(status: UpdateStatus): string | null { - if (status.state === 'available') return 'Update' - if (status.state === 'downloading') { - return status.percent > 0 ? `Updating… ${status.percent}%` : 'Updating…' - } - if (status.state === 'ready') return 'Restart' - if (status.state === 'error') return 'Update failed' - return null -} - -function appendTranscript(body: string, chunk: string): string { - const text = chunk.trim() - if (!text) return body - if (!body.trim()) return text - const needsSpace = !/\s$/.test(body) - const prefix = needsSpace ? `${body} ` : body - const next = /[.!?]$/.test(body.trim()) ? text.replace(/^./, (ch) => ch.toUpperCase()) : text - return `${prefix}${next}` -} - -function pcmBytes(pcm: Float32Array): Uint8Array { - return new Uint8Array(pcm.buffer, pcm.byteOffset, pcm.byteLength).slice() +import { useCallback, useEffect, useRef, useState } from 'react' +import type { PageSummary, UpdateStatus, WhisperStatus } from '../../shared/types' +import { DictationDock } from './components/DictationDock' +import { Editor } from './components/Editor' +import { CheckIcon, CopyIcon, PlusIcon } from './components/Icons' +import { Sidebar } from './components/Sidebar' +import { useDictation } from './hooks/useDictation' +import { usePages } from './hooks/usePages' +import { readingMinutes, relativeTime, wordCount } from './lib/format' +import { appendTranscript, promptFor } from './text/transcript' + +function toMarkdown(title: string, body: string): string { + return title && title !== 'Untitled' ? `# ${title}\n\n${body}` : body } export default function App(): React.JSX.Element { - const [pages, setPages] = useState([]) - const [current, setCurrent] = useState(null) + const { pages, current, currentRef, saveState, edit, open, create, remove } = usePages() const [status, setStatus] = useState({ state: 'idle' }) - const [recording, setRecording] = useState(false) - const [micLevel, setMicLevel] = useState(0) - const [micName, setMicName] = useState('') - const [error, setError] = useState(null) - const [copied, setCopied] = useState(false) const [update, setUpdate] = useState({ state: 'idle' }) - const copiedTimer = useRef | null>(null) - const captureRef = useRef(null) - const currentRef = useRef(null) - const committedRef = useRef('') - const liveRef = useRef('') - const editorRef = useRef(null) - const saveTimer = useRef | null>(null) - const transcribeBusy = useRef(false) - const queuedAudio = useRef<{ pcm: Float32Array; final: boolean } | null>(null) - - currentRef.current = current - - const refreshList = useCallback(async (preferredId?: string) => { - const list = await window.api.listPages() - setPages(list) - const keepId = preferredId ?? currentRef.current?.id - if (keepId && list.some((page) => page.id === keepId)) return - if (!list[0]) { - committedRef.current = '' - liveRef.current = '' - setCurrent(null) - return - } - const page = await window.api.getPage(list[0].id) - committedRef.current = page.body - liveRef.current = '' - setCurrent(page) - setError(null) - }, []) + const [copied, setCopied] = useState(false) + const titleInput = useRef(null) + const scroller = useRef(null) useEffect(() => { - void refreshList() const stopStatus = window.api.onStatus(setStatus) const stopUpdate = window.api.onUpdateStatus(setUpdate) return () => { stopStatus() stopUpdate() } - }, [refreshList]) - - const persist = useCallback((page: Page, immediate = false) => { - const write = async (): Promise => { - const saved = await window.api.savePage({ - id: page.id, - title: page.title, - body: page.body - }) - setCurrent((prev) => - prev && prev.id === saved.id ? { ...prev, updatedAt: saved.updatedAt } : prev - ) - await refreshList(page.id) - } + }, []) - if (saveTimer.current) clearTimeout(saveTimer.current) - if (immediate) { - void write() - return - } - saveTimer.current = setTimeout(() => { - void write() - }, 300) - }, [refreshList]) + const onCommit = useCallback( + (text: string) => { + const page = currentRef.current + if (!page) return + const view = scroller.current + // Keep new words in view, unless the writer has scrolled up to reread. + const follow = !view || view.scrollHeight - view.scrollTop - view.clientHeight < 160 + edit({ body: appendTranscript(page.body, text) }) + if (follow) { + requestAnimationFrame(() => view?.scrollTo({ top: view.scrollHeight, behavior: 'smooth' })) + } + }, + [currentRef, edit] + ) - const showBody = (committed: string, live: string, immediate = false): void => { + const getContext = useCallback(() => { const page = currentRef.current - if (!page) return - committedRef.current = committed - liveRef.current = live - const body = appendTranscript(committed, live) - const editor = editorRef.current - const stickToEnd = - !!editor && - editor.selectionStart === editor.value.length && - editor.selectionEnd === editor.value.length - const next = { ...page, body } - currentRef.current = next - setCurrent(next) - persist(next, immediate) - if (stickToEnd) { - requestAnimationFrame(() => { - const el = editorRef.current - if (!el) return - const end = el.value.length - el.setSelectionRange(end, end) - }) - } - } + return page ? promptFor(page.title, page.body) : '' + }, [currentRef]) - const drainAudio = async (): Promise => { - if (transcribeBusy.current) return - transcribeBusy.current = true - try { - while (queuedAudio.current) { - const job = queuedAudio.current - queuedAudio.current = null - const text = await window.api.transcribe(pcmBytes(job.pcm)) - if (job.final) { - const committed = text - ? appendTranscript(committedRef.current, text) - : committedRef.current - showBody(committed, '', true) - } else if (text) { - showBody(committedRef.current, text) - } - } - } catch (caught) { - const message = caught instanceof Error ? caught.message : 'Transcription failed' - setError(message) - setStatus({ state: 'error', message }) - } finally { - transcribeBusy.current = false - if (queuedAudio.current) { - void drainAudio() - } - } - } + const dictation = useDictation({ getContext, onCommit }) + const { recording, stop, start } = dictation - const queueAudio = (pcm: Float32Array, final: boolean): void => { - const prev = queuedAudio.current - if (prev?.final && !final) return - queuedAudio.current = { pcm, final: Boolean(prev?.final) || final } - void drainAudio() - } + const toggleDictation = useCallback(() => { + if (recording) void stop() + else if (currentRef.current) void start() + }, [recording, stop, start, currentRef]) const openPage = async (id: string): Promise => { - if (recording) await stopRecording() - const page = await window.api.getPage(id) - committedRef.current = page.body - liveRef.current = '' - setCurrent(page) - setError(null) - } - - const onNewPage = async (): Promise => { - if (recording) await stopRecording() - const page = await window.api.createPage() - committedRef.current = page.body - liveRef.current = '' - setCurrent(page) - await refreshList(page.id) + await stop() + await open(id) } - const onDelete = async (id: string, title: string): Promise => { - const ok = window.confirm(`Delete “${title || 'Untitled'}”? This cannot be undone.`) - if (!ok) return - if (current?.id === id && recording) { - await captureRef.current?.stop() - captureRef.current = null - setRecording(false) - setMicLevel(0) - setMicName('') - } - await window.api.deletePage(id) - await refreshList() - } + const newPage = useCallback(async () => { + await stop() + await create() + requestAnimationFrame(() => titleInput.current?.select()) + }, [stop, create]) - const updateTitle = (title: string): void => { - setCurrent((prev) => { - if (!prev) return prev - const next = { ...prev, title } - persist(next) - return next - }) - } - - const onBodyChange = (value: string): void => { - committedRef.current = value - liveRef.current = '' - if (queuedAudio.current && !queuedAudio.current.final) { - queuedAudio.current = null - } - setCurrent((prev) => { - if (!prev) return prev - const next = { ...prev, body: value } - persist(next) - return next - }) + const deletePage = async (page: PageSummary): Promise => { + if (!window.confirm(`Delete “${page.title || 'Untitled'}”? This cannot be undone.`)) return + if (page.id === current?.id) await stop() + await remove(page.id) } - const onCopy = (): void => { + const copy = (): void => { if (!current?.body) return - try { - window.api.copyText(current.body) - setCopied(true) - setError(null) - if (copiedTimer.current) clearTimeout(copiedTimer.current) - copiedTimer.current = setTimeout(() => setCopied(false), 1500) - } catch (err) { - setError(err instanceof Error ? err.message : 'Could not copy to the clipboard.') - } - } - - const stopRecording = async (): Promise => { - const handle = captureRef.current - captureRef.current = null - setRecording(false) - setMicLevel(0) - setMicName('') - await handle?.stop() - await drainAudio() - if (liveRef.current) { - showBody(appendTranscript(committedRef.current, liveRef.current), '', true) - } + window.api.copyText(toMarkdown(current.title, current.body)) + setCopied(true) } - const startRecording = async (): Promise => { - if (!current) { - setError('Create a page before recording.') - return - } - setError(null) - committedRef.current = current.body - liveRef.current = '' - try { - const stream = await navigator.mediaDevices.getUserMedia({ - audio: { - channelCount: 1, - echoCancellation: false, - noiseSuppression: false, - autoGainControl: true - }, - video: false - }) - const track = stream.getAudioTracks()[0] - if (!track || track.readyState !== 'live') { - throw new Error( - 'The microphone did not start. Check System Settings → Privacy & Security → Microphone.' - ) - } - setMicName(track.label || 'Microphone') - await window.api.ensureModel() - const handle = await startCapture( - { - onPartial: (pcm) => queueAudio(pcm, false), - onFinal: (pcm) => queueAudio(pcm, true) - }, - { stream, onLevel: setMicLevel } - ) - captureRef.current = handle - setRecording(true) - } catch (caught) { - const message = caught instanceof Error ? caught.message : 'Could not start recording' - setError(message) - setStatus({ state: 'error', message }) - } - } - - const toggleRecord = async (): Promise => { - if (recording) { - await stopRecording() - return - } - await startRecording() - } + useEffect(() => { + if (!copied) return + const timer = setTimeout(() => setCopied(false), 1500) + return () => clearTimeout(timer) + }, [copied]) useEffect(() => { - return () => { - void captureRef.current?.stop() - if (saveTimer.current) clearTimeout(saveTimer.current) - if (copiedTimer.current) clearTimeout(copiedTimer.current) + const onKey = (event: KeyboardEvent): void => { + if (!(event.metaKey || event.ctrlKey)) return + const key = event.key.toLowerCase() + if (key === 'd' && event.shiftKey) { + event.preventDefault() + toggleDictation() + } else if (key === 'n' && !event.shiftKey) { + event.preventDefault() + void newPage() + } } - }, []) - - const downloadPct = useMemo(() => { - if (status.state !== 'downloading' || !status.total) return 0 - return Math.min(100, Math.round((status.received / status.total) * 100)) - }, [status]) + window.addEventListener('keydown', onKey) + return () => window.removeEventListener('keydown', onKey) + }, [toggleDictation, newPage]) - const updateLabel = updateChipLabel(update) - - const onUpdateChip = (): void => { - if (update.state === 'downloading') return - if (update.state === 'ready') { - void window.api.installUpdate() - return - } - void window.api.startUpdate() - } + const words = current ? wordCount(current.body) : 0 return ( -
- +
+ void openPage(id)} + onCreate={() => void newPage()} + onDelete={(page) => void deletePage(page)} + />
- {status.state === 'downloading' ? ( +
+ {current ? ( + <> + + {saveState === 'saving' + ? 'Saving…' + : `Saved ${relativeTime(current.updatedAt).toLowerCase()}`} + + + + ) : null} +
+ + {status.state === 'downloading' && status.total ? ( ) : null} {current ? ( <> -
- updateTitle(event.target.value)} - placeholder="Untitled" - /> -
- {recording ? ( -
- -
- ) : null} - - -
-
-
-