2026-09-01 04:56:37 +00:00
|
|
|
<script lang="ts">
|
2026-09-11 00:48:11 +00:00
|
|
|
import {onDestroy, onMount} from "svelte"
|
2026-09-01 04:56:37 +00:00
|
|
|
import cx from "classnames"
|
2026-09-11 00:48:11 +00:00
|
|
|
import type {MaybeAsync} from "@welshman/lib"
|
2026-09-01 04:56:37 +00:00
|
|
|
import Microphone from "@assets/icons/microphone.svg?dataurl"
|
|
|
|
|
import Stop from "@assets/icons/record.svg?dataurl"
|
|
|
|
|
import Icon from "@lib/components/Icon.svelte"
|
|
|
|
|
import Button from "@lib/components/Button.svelte"
|
|
|
|
|
import Spinner from "@lib/components/Spinner.svelte"
|
|
|
|
|
import {errorMessage} from "@lib/util"
|
2026-09-09 14:43:55 +00:00
|
|
|
import OpenRouterEnable from "@app/components/OpenRouterEnable.svelte"
|
2026-09-11 00:48:11 +00:00
|
|
|
import {clearDictation, getDictation, startDictation} from "@app/dictation"
|
2026-09-01 04:56:37 +00:00
|
|
|
import {getSetting} from "@app/settings"
|
|
|
|
|
import {pushModal} from "@app/modal"
|
|
|
|
|
import {pushToast} from "@app/toast"
|
|
|
|
|
|
|
|
|
|
type Props = {
|
2026-09-11 00:48:11 +00:00
|
|
|
key: string
|
|
|
|
|
dictating?: boolean
|
2026-09-01 04:56:37 +00:00
|
|
|
onTranscript: (text: string) => MaybeAsync<void>
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 00:48:11 +00:00
|
|
|
let {key, dictating = $bindable(false), onTranscript}: Props = $props()
|
2026-09-01 04:56:37 +00:00
|
|
|
|
|
|
|
|
// Room noise idles just below this, so the pulse follows quiet speech too.
|
|
|
|
|
const onLevel = (level: number) => {
|
|
|
|
|
loud = level > 0.03
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const start = async () => {
|
|
|
|
|
if (getSetting("openrouter_key")) {
|
|
|
|
|
// Granting microphone access can sit on a permission prompt for a while, so hold the button
|
|
|
|
|
// until it resolves — a second click would open a stream nothing is left holding on to.
|
|
|
|
|
loading = true
|
|
|
|
|
|
|
|
|
|
try {
|
2026-09-11 00:48:11 +00:00
|
|
|
await startDictation(key, onLevel)
|
2026-09-01 04:56:37 +00:00
|
|
|
recording = true
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error)
|
|
|
|
|
pushToast({theme: "error", message: "Failed to access your microphone."})
|
|
|
|
|
} finally {
|
|
|
|
|
loading = false
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2026-09-09 14:43:55 +00:00
|
|
|
pushModal(OpenRouterEnable, {
|
|
|
|
|
feature: "Voice input",
|
|
|
|
|
subtitle: "Dictate your messages instead of typing them.",
|
|
|
|
|
})
|
2026-09-01 04:56:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 00:48:11 +00:00
|
|
|
const stop = () => {
|
|
|
|
|
getDictation(key)?.stop()
|
2026-09-01 04:56:37 +00:00
|
|
|
|
2026-09-11 00:48:11 +00:00
|
|
|
recording = false
|
|
|
|
|
loud = false
|
|
|
|
|
|
|
|
|
|
deliver()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deliver = async () => {
|
|
|
|
|
const dictation = getDictation(key)
|
|
|
|
|
|
|
|
|
|
if (dictation) {
|
2026-09-01 04:56:37 +00:00
|
|
|
loading = true
|
|
|
|
|
|
2026-09-11 00:48:11 +00:00
|
|
|
await dictation.finished
|
2026-09-01 04:56:37 +00:00
|
|
|
|
2026-09-11 00:48:11 +00:00
|
|
|
// A composer that has gone away has nowhere to put the transcript, so leave the dictation
|
|
|
|
|
// where it is for whichever one mounts next.
|
|
|
|
|
if (!destroyed) {
|
|
|
|
|
if (dictation.error) {
|
|
|
|
|
console.error(dictation.error)
|
|
|
|
|
pushToast({
|
|
|
|
|
theme: "error",
|
|
|
|
|
message: `Failed to transcribe: ${errorMessage(dictation.error)}`,
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
await onTranscript(dictation.transcript ?? "")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearDictation(key)
|
2026-09-01 04:56:37 +00:00
|
|
|
}
|
2026-09-11 00:48:11 +00:00
|
|
|
|
|
|
|
|
loading = false
|
2026-09-01 04:56:37 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const toggle = () => (recording ? stop() : start())
|
|
|
|
|
|
2026-09-11 00:48:11 +00:00
|
|
|
let destroyed = false
|
|
|
|
|
let recording = $state(false)
|
|
|
|
|
// Starts out in flight when a dictation is already waiting to be picked up, so that the composer
|
|
|
|
|
// keeps rendering this button until its transcript has been handed over.
|
|
|
|
|
let loading = $state(Boolean(getDictation(key)))
|
2026-09-01 04:56:37 +00:00
|
|
|
let loud = $state(false)
|
|
|
|
|
|
|
|
|
|
const buttonClass = $derived(
|
|
|
|
|
cx(
|
|
|
|
|
"button button-circle tip tip-left h-10 w-10 min-w-10",
|
|
|
|
|
recording ? "button-error" : "button-primary",
|
|
|
|
|
{"pulse-scale": loud},
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
2026-09-11 00:48:11 +00:00
|
|
|
$effect(() => {
|
|
|
|
|
dictating = recording || loading
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Pick up a dictation an earlier composer left running.
|
|
|
|
|
onMount(deliver)
|
|
|
|
|
|
2026-09-01 04:56:37 +00:00
|
|
|
onDestroy(() => {
|
2026-09-11 00:48:11 +00:00
|
|
|
destroyed = true
|
|
|
|
|
|
|
|
|
|
const dictation = getDictation(key)
|
|
|
|
|
|
|
|
|
|
if (dictation?.recording) {
|
|
|
|
|
dictation.stop()
|
|
|
|
|
}
|
2026-09-01 04:56:37 +00:00
|
|
|
})
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
class={buttonClass}
|
|
|
|
|
data-tip={recording ? "Stop and transcribe" : "Record a message"}
|
|
|
|
|
aria-label={recording ? "Stop recording" : "Start dictation"}
|
|
|
|
|
disabled={loading}
|
|
|
|
|
onclick={toggle}>
|
|
|
|
|
{#if loading}
|
|
|
|
|
<Spinner size="xs" />
|
|
|
|
|
{:else if recording}
|
|
|
|
|
<Icon icon={Stop} />
|
|
|
|
|
{:else}
|
|
|
|
|
<Icon icon={Microphone} />
|
|
|
|
|
{/if}
|
|
|
|
|
</Button>
|