Repository navigation
feat: add voice dictation beta #5213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+1,383
−1
Closed
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1dbfe76
feat: add voice dictation beta
t3-code[bot] b5aa8b6
fix: simplify voice dictation providers
t3-code[bot] bf3ecf0
fix: split transcription input errors
t3-code[bot] dbbe349
fix: bound transcription response processing
t3-code[bot] ecbafb6
fix: follow effect recovery conventions
t3-code[bot] 14315f5
feat: improve voice dictation configuration and recording
maria-rcks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import { expect, it } from "@effect/vitest"; | ||
| import { describe, vi } from "vite-plus/test"; | ||
|
|
||
| import { forwardWhisperTranscription, resolveWhisperTranscriptionUrl } from "./transcription.ts"; | ||
|
|
||
| describe("resolveWhisperTranscriptionUrl", () => { | ||
| it("adds the OpenAI-compatible transcription path", () => { | ||
| expect(resolveWhisperTranscriptionUrl("http://127.0.0.1:8080/v1/")?.toString()).toBe( | ||
| "http://127.0.0.1:8080/v1/audio/transcriptions", | ||
| ); | ||
| expect( | ||
| resolveWhisperTranscriptionUrl( | ||
| "https://api.groq.com/openai/v1/audio/transcriptions", | ||
| )?.toString(), | ||
| ).toBe("https://api.groq.com/openai/v1/audio/transcriptions"); | ||
| }); | ||
|
|
||
| it("rejects non-http and credential-bearing URLs", () => { | ||
| expect(resolveWhisperTranscriptionUrl("file:///tmp/whisper")).toBeNull(); | ||
| expect(resolveWhisperTranscriptionUrl("https://user:pass@example.com/v1")).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("forwardWhisperTranscription", () => { | ||
| it("sends OpenAI-compatible multipart audio with BYOK authorization", async () => { | ||
| const fetchImpl = vi.fn(async (_input: string | URL | Request, init?: RequestInit) => { | ||
| expect(init?.headers).toEqual({ authorization: "Bearer groq-key" }); | ||
| expect(init?.body).toBeInstanceOf(FormData); | ||
| const form = init?.body as FormData; | ||
| expect(form.get("model")).toBe("whisper-large-v3-turbo"); | ||
| expect(form.get("file")).toBeInstanceOf(Blob); | ||
| return Response.json({ text: "hello from the microphone" }); | ||
| }); | ||
|
|
||
| await expect( | ||
| forwardWhisperTranscription( | ||
| { | ||
| audio: new Uint8Array([1, 2, 3]), | ||
| audioMimeType: "audio/webm;codecs=opus", | ||
| baseUrl: "https://api.groq.com/openai/v1", | ||
| model: "whisper-large-v3-turbo", | ||
| apiKey: "groq-key", | ||
| }, | ||
| fetchImpl, | ||
| ), | ||
| ).resolves.toEqual({ ok: true, text: "hello from the microphone" }); | ||
| }); | ||
|
|
||
| it("returns a safe provider error", async () => { | ||
| const fetchImpl = vi.fn(async () => | ||
| Response.json({ error: { message: "bad key" } }, { status: 401 }), | ||
| ); | ||
| await expect( | ||
| forwardWhisperTranscription( | ||
| { | ||
| audio: new Uint8Array([1]), | ||
| audioMimeType: "audio/webm", | ||
| baseUrl: "https://example.com/v1", | ||
| model: "whisper-1", | ||
| apiKey: "bad", | ||
| }, | ||
| fetchImpl, | ||
| ), | ||
| ).resolves.toEqual({ ok: false, status: 502, message: "bad key" }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| const TRANSCRIPTION_PATH = "/audio/transcriptions"; | ||
|
|
||
| export const MAX_TRANSCRIPTION_AUDIO_BYTES = 25 * 1024 * 1024; | ||
|
|
||
| export interface WhisperTranscriptionInput { | ||
| readonly audio: Uint8Array; | ||
| readonly audioMimeType: string; | ||
| readonly baseUrl: string; | ||
| readonly model: string; | ||
| readonly apiKey: string; | ||
| } | ||
|
|
||
| type TranscriptionFetch = (input: string | URL | Request, init?: RequestInit) => Promise<Response>; | ||
|
|
||
| export type WhisperTranscriptionResult = | ||
| | { readonly ok: true; readonly text: string } | ||
| | { readonly ok: false; readonly status: number; readonly message: string }; | ||
|
macroscopeapp[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| export function resolveWhisperTranscriptionUrl(baseUrl: string): URL | null { | ||
| try { | ||
| const url = new URL(baseUrl.trim()); | ||
| if (url.protocol !== "http:" && url.protocol !== "https:") return null; | ||
| if (url.username || url.password) return null; | ||
| url.search = ""; | ||
| url.hash = ""; | ||
| url.pathname = url.pathname.replace(/\/$/, ""); | ||
| if (!url.pathname.endsWith(TRANSCRIPTION_PATH)) { | ||
| url.pathname += TRANSCRIPTION_PATH; | ||
| } | ||
| return url; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function audioFileExtension(mimeType: string): string { | ||
| if (mimeType.includes("ogg")) return "ogg"; | ||
| if (mimeType.includes("mp4") || mimeType.includes("m4a")) return "m4a"; | ||
| if (mimeType.includes("wav")) return "wav"; | ||
| return "webm"; | ||
| } | ||
|
|
||
| export async function forwardWhisperTranscription( | ||
| input: WhisperTranscriptionInput, | ||
| fetchImpl: TranscriptionFetch = globalThis.fetch, | ||
|
macroscopeapp[bot] marked this conversation as resolved.
Outdated
|
||
| ): Promise<WhisperTranscriptionResult> { | ||
| const endpoint = resolveWhisperTranscriptionUrl(input.baseUrl); | ||
| if (!endpoint) { | ||
| return { ok: false, status: 400, message: "Enter a valid HTTP transcription endpoint." }; | ||
| } | ||
| if (!input.model.trim()) { | ||
| return { ok: false, status: 400, message: "Enter a transcription model." }; | ||
| } | ||
| if (input.audio.byteLength === 0) { | ||
| return { ok: false, status: 400, message: "The recording was empty." }; | ||
| } | ||
| if (input.audio.byteLength > MAX_TRANSCRIPTION_AUDIO_BYTES) { | ||
| return { ok: false, status: 413, message: "The recording exceeds the 25 MB limit." }; | ||
| } | ||
|
|
||
| const mimeType = input.audioMimeType.split(";", 1)[0]?.trim() || "audio/webm"; | ||
| const form = new FormData(); | ||
| form.set("model", input.model.trim()); | ||
| form.set( | ||
| "file", | ||
| new Blob([input.audio], { type: mimeType }), | ||
| `recording.${audioFileExtension(mimeType)}`, | ||
| ); | ||
|
|
||
| let response: Response; | ||
| try { | ||
| response = await fetchImpl(endpoint, { | ||
| method: "POST", | ||
| headers: input.apiKey ? { authorization: `Bearer ${input.apiKey}` } : undefined, | ||
| body: form, | ||
| signal: AbortSignal.timeout(120_000), | ||
| }); | ||
| } catch { | ||
| return { ok: false, status: 502, message: "Could not reach the transcription provider." }; | ||
| } | ||
|
|
||
| let payload: unknown; | ||
| try { | ||
| payload = await response.json(); | ||
| } catch { | ||
| return { ok: false, status: 502, message: "The transcription provider returned invalid JSON." }; | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| const providerMessage = | ||
| typeof payload === "object" && | ||
| payload !== null && | ||
| "error" in payload && | ||
| typeof payload.error === "object" && | ||
| payload.error !== null && | ||
| "message" in payload.error && | ||
| typeof payload.error.message === "string" | ||
| ? payload.error.message | ||
| : null; | ||
| return { | ||
| ok: false, | ||
| status: 502, | ||
| message: providerMessage?.slice(0, 300) || "The transcription provider rejected the request.", | ||
| }; | ||
|
macroscopeapp[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| const text = | ||
| typeof payload === "object" && payload !== null && "text" in payload ? payload.text : undefined; | ||
| if (typeof text !== "string") { | ||
| return { ok: false, status: 502, message: "The transcription response did not contain text." }; | ||
| } | ||
| return { ok: true, text: text.trim() }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.