Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AGENTS.md - opencode-dictation

OpenCode Desktop GUI plugin, macOS only, whisper.cpp only. No TUI part, no LLM.
OpenCode Desktop GUI plugin, macOS and Windows, whisper.cpp only. No TUI part, no LLM.

## Key invariants

Expand All @@ -14,6 +14,9 @@ OpenCode Desktop GUI plugin, macOS only, whisper.cpp only. No TUI part, no LLM.
`whisper-cli` work lives there.
- The Ipc id must start with the package name: `opencode-dictation.whisper`.
- Desktop's PATH is minimal, so `whisper-cli` is looked up in Homebrew dirs too.
On Windows the managed runtime dir (`%LOCALAPPDATA%\opencode-dictation\bin`,
auto-downloaded from the pinned ggml-org release and checksum-verified) is
checked first, and `whisper-cli.exe` is the binary name.
- Settings live in plugin storage (`Store.global`) and are edited in Settings →
Dictation. `opencode.jsonc` options are not read. The window passes model and
language on each Ipc call, and main validates them again.
Expand Down
25 changes: 14 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# opencode-dictation

Local voice dictation for the [OpenCode](https://opencode.ai) Desktop app on macOS.
Local voice dictation for the [OpenCode](https://opencode.ai) Desktop app on macOS and Windows.

![Dictating a message into the OpenCode prompt](assets/demo.gif)

Expand All @@ -11,33 +11,36 @@ Local voice dictation for the [OpenCode](https://opencode.ai) Desktop app on mac

## Requirements

- macOS, OpenCode Desktop 2.2.5 or later
- whisper.cpp:
- OpenCode Desktop 2.2.5 or later
- macOS: whisper.cpp:

```bash
brew install whisper-cpp
```

- Windows: nothing to install. The official whisper.cpp CPU build is downloaded, checksum-verified, and extracted on first dictation.

## Install

```bash
opencode plugin add github:qonto/opencode-dictation
```

Then restart Desktop.
Then restart Desktop. To load from a local checkout instead, put the repo's absolute path in the `plugins` list of `~/.config/opencode/opencode.jsonc`.

## Usage

Press `⌘D` to start recording, and again to stop. The transcript goes into the
Press `⌘D` (macOS) or `Ctrl+D` (Windows) to start recording, and again to stop. The transcript goes into the
prompt. Change the key in **Settings → Shortcuts**.

## Settings

Open **Settings → Dictation**.

| Setting | Default | What it does |
| ------------------ | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| Push-to-talk | Off | Hold the shortcut while you talk, and let go to stop. Off: press once to start, once to stop. |
| Send automatically | Off | Send the transcript as soon as it is ready. |
| Language | `en` | A whisper language code such as `fr`, or `auto` to detect it (adds a short delay). |
| Model | `large-v3-turbo-q5_0` | Any [whisper.cpp model](https://huggingface.co/ggerganov/whisper.cpp/tree/main), without `ggml-` and `.bin`. Downloads on first use. |
| Setting | Default | What it does |
| ------------------ | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Push-to-talk | Off | Hold the shortcut while you talk, and let go to stop. Off: press once to start, once to stop. |
| Send automatically | Off | Send the transcript as soon as it is ready. |
| Live dictation | Off | Transcribe while you speak; text updates as you talk. The final pass cleans up boundaries. |
| Language | `en` | A whisper language code such as `fr`, or `auto` to detect it (adds a short delay). |
| Model | `small.en` | Preset dropdown or Custom…: any [whisper.cpp model](https://huggingface.co/ggerganov/whisper.cpp/tree/main), without `ggml-` and `.bin`. Downloads on first use. |
11 changes: 9 additions & 2 deletions dist/gui-experimental.cjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "opencode-dictation",
"version": "0.3.1",
"version": "0.4.0",
"private": true,
"description": "Local voice dictation for OpenCode Desktop on macOS, with whisper.cpp.",
"description": "Local voice dictation for OpenCode Desktop on macOS and Windows, with whisper.cpp.",
"homepage": "https://github.com/qonto/opencode-dictation#readme",
"bugs": "https://github.com/qonto/opencode-dictation/issues",
"license": "MIT",
Expand Down
86 changes: 86 additions & 0 deletions src/chunks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Pure helpers for live dictation: no DOM, no process, node-testable.
// The window slices the recording tap into overlapping chunks and dedupes their
// transcripts against the running draft; both pieces live here so they can be
// unit tested without a browser.

// RMS above this counts as sound; shared by the meter and chunk gating. Silence
// with noise suppression stays well below.
export const SOUND_LEVEL = 0.01;

// New audio per chunk. Each chunk covers a HOP of fresh audio plus a WINDOW
// overall, so WINDOW_MS > HOP_MS and consecutive chunks overlap: whisper gets
// context across the cut and the dedupe below can repair the seam.
export const HOP_MS = 2000;
export const WINDOW_MS = 3000;

// whisper-cli only reads 16 kHz mono, while the tap records at the AudioContext
// rate (44.1 or 48 kHz on most machines). Linear interpolation is inaudibly
// different from a proper resampler for speech, and keeps this dependency-free.
export function resample(input: Float32Array, from: number, to: number): Float32Array {
const output = new Float32Array(Math.max(0, Math.floor((input.length * to) / from)));
const ratio = from / to;
for (let i = 0; i < output.length; i++) {
const position = i * ratio;
const index = Math.floor(position);
const next = Math.min(index + 1, input.length - 1);
output[i] = input[index] + (input[next] - input[index]) * (position - index);
}
return output;
}

// Words are compared case- and punctuation-insensitively, because the same
// spoken words come back from different chunks with different punctuation
// ("world." vs "world").
const normalizeWord = (word: string) => word.toLowerCase().replace(/[^\p{L}\p{N}]/gu, "");

const splitWords = (text: string) =>
text
.trim()
.split(/\s+/)
.filter((word) => word !== "");

// whisper repeats a phrase on trailing silence: "one more try, one more try,
// one more try" after a single "one more try". Collapse a run of 3+ identical
// consecutive phrases at the END of the chunk to one. The rule is deliberately
// conservative (3+ and tail only): a 2-word phrase repeated twice could be a
// real repetition by the speaker, and eating it would lose speech.
const MAX_PHRASE_WORDS = 6;

function collapseRepeatedPhrase(words: string[]): string[] {
const normalized = words.map(normalizeWord);
for (let size = 1; size <= MAX_PHRASE_WORDS && size * 3 <= normalized.length; size++) {
// Count how many times the last `size` words repeat back-to-back at the end.
let repeats = 1;
while (
normalized.length >= (repeats + 1) * size &&
normalized
.slice(normalized.length - (repeats + 1) * size, normalized.length - repeats * size)
.every((word, i) => word === normalized[normalized.length - size + i])
) {
repeats++;
}
if (repeats >= 3) return words.slice(0, words.length - (repeats - 1) * size);
}
return words;
}

// Consecutive chunks overlap by design, so the chunk head usually repeats the
// draft tail. Drop the longest suffix of the draft that equals a prefix of the
// chunk (after collapsing whisper's trailing-silence repeats) and return only
// the new words to append.
export function dedupeChunk(draft: string, chunk: string): string {
const words = collapseRepeatedPhrase(splitWords(chunk));
const draftWords = splitWords(draft);
for (let overlap = Math.min(draftWords.length, words.length); overlap >= 1; overlap--) {
const matches = [];
for (let i = 0; i < overlap; i++) {
const a = normalizeWord(draftWords[draftWords.length - overlap + i]);
const b = normalizeWord(words[i]);
// An empty normalized word (pure punctuation) never matches: two chunks
// that only share punctuation must not be glued together.
matches.push(a !== "" && a === b);
}
if (matches.every(Boolean)) return words.slice(overlap).join(" ");
}
return words.join(" ");
}
Loading