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
Closed
Changes from 4 commits
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
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
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,50 @@ | ||
| import { expect, it } from "@effect/vitest"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Stream from "effect/Stream"; | ||
| import { describe } from "vite-plus/test"; | ||
|
|
||
| import { | ||
| MAX_TRANSCRIPTION_AUDIO_BYTES, | ||
| readTranscriptionAudio, | ||
| resolveTranscriptionProvider, | ||
| transcriptionProviderConfig, | ||
| } from "./transcription.ts"; | ||
|
|
||
| describe("transcription providers", () => { | ||
| it("uses a fixed OpenAI endpoint and model", () => { | ||
| expect(resolveTranscriptionProvider("openai")).toBe("openai"); | ||
| expect(transcriptionProviderConfig("openai")).toEqual({ | ||
| endpoint: "https://api.openai.com/v1/audio/transcriptions", | ||
| model: "gpt-4o-mini-transcribe", | ||
| }); | ||
| }); | ||
|
|
||
| it("uses a fixed Groq endpoint and rejects custom providers", () => { | ||
| expect(resolveTranscriptionProvider("groq")).toBe("groq"); | ||
| expect(transcriptionProviderConfig("groq")).toEqual({ | ||
| endpoint: "https://api.groq.com/openai/v1/audio/transcriptions", | ||
| model: "whisper-large-v3-turbo", | ||
| }); | ||
| expect(resolveTranscriptionProvider("http://127.0.0.1:8080/v1")).toBeNull(); | ||
| }); | ||
| }); | ||
|
|
||
| describe("readTranscriptionAudio", () => { | ||
| it.effect("combines streamed audio chunks", () => | ||
| Effect.gen(function* () { | ||
| const audio = yield* readTranscriptionAudio( | ||
| Stream.make(new Uint8Array([1, 2]), new Uint8Array([3, 4])), | ||
| ); | ||
| expect(Array.from(audio)).toEqual([1, 2, 3, 4]); | ||
| }), | ||
| ); | ||
|
|
||
| it.effect("stops when streamed audio exceeds the limit", () => | ||
| Effect.gen(function* () { | ||
| const error = yield* readTranscriptionAudio( | ||
| Stream.make(new Uint8Array(MAX_TRANSCRIPTION_AUDIO_BYTES), new Uint8Array([1])), | ||
| ).pipe(Effect.flip); | ||
| expect(error._tag).toBe("TranscriptionAudioTooLargeError"); | ||
| }), | ||
| ); | ||
| }); |
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,203 @@ | ||
| import type { VoiceTranscriptionProvider } from "@t3tools/contracts"; | ||
| import * as Effect from "effect/Effect"; | ||
| import * as Schema from "effect/Schema"; | ||
| import * as Stream from "effect/Stream"; | ||
| import { HttpClient, HttpClientRequest, HttpClientResponse } from "effect/unstable/http"; | ||
|
|
||
| export const MAX_TRANSCRIPTION_AUDIO_BYTES = 25 * 1024 * 1024; | ||
|
|
||
| const PROVIDERS = { | ||
| openai: { | ||
| endpoint: "https://api.openai.com/v1/audio/transcriptions", | ||
| model: "gpt-4o-mini-transcribe", | ||
| }, | ||
| groq: { | ||
| endpoint: "https://api.groq.com/openai/v1/audio/transcriptions", | ||
| model: "whisper-large-v3-turbo", | ||
| }, | ||
| } as const satisfies Record<VoiceTranscriptionProvider, { endpoint: string; model: string }>; | ||
|
|
||
| export interface VoiceTranscriptionInput { | ||
| readonly audio: Uint8Array; | ||
| readonly audioMimeType: string; | ||
| readonly provider: VoiceTranscriptionProvider; | ||
| readonly apiKey: string; | ||
| } | ||
|
|
||
| export class TranscriptionAudioTooLargeError extends Schema.TaggedErrorClass<TranscriptionAudioTooLargeError>()( | ||
| "TranscriptionAudioTooLargeError", | ||
| { receivedBytes: Schema.Number }, | ||
| ) { | ||
| override get message(): string { | ||
| return "The recording exceeds the 25 MB limit."; | ||
| } | ||
| } | ||
|
|
||
| export class TranscriptionBodyReadError extends Schema.TaggedErrorClass<TranscriptionBodyReadError>()( | ||
| "TranscriptionBodyReadError", | ||
| { cause: Schema.Defect() }, | ||
| ) { | ||
| override get message(): string { | ||
| return "Could not read the recording."; | ||
| } | ||
| } | ||
|
|
||
| export class TranscriptionEmptyAudioError extends Schema.TaggedErrorClass<TranscriptionEmptyAudioError>()( | ||
| "TranscriptionEmptyAudioError", | ||
| {}, | ||
| ) { | ||
| override get message(): string { | ||
| return "The recording was empty."; | ||
| } | ||
| } | ||
|
|
||
| export class TranscriptionProviderUnsupportedError extends Schema.TaggedErrorClass<TranscriptionProviderUnsupportedError>()( | ||
| "TranscriptionProviderUnsupportedError", | ||
| {}, | ||
| ) { | ||
| override get message(): string { | ||
| return "Select OpenAI or Groq for transcription."; | ||
| } | ||
| } | ||
|
|
||
| export class TranscriptionApiKeyMissingError extends Schema.TaggedErrorClass<TranscriptionApiKeyMissingError>()( | ||
| "TranscriptionApiKeyMissingError", | ||
| {}, | ||
| ) { | ||
| override get message(): string { | ||
| return "Add an API key for the selected transcription provider."; | ||
| } | ||
| } | ||
|
|
||
| export class TranscriptionRequestError extends Schema.TaggedErrorClass<TranscriptionRequestError>()( | ||
| "TranscriptionRequestError", | ||
| { | ||
| provider: Schema.String, | ||
| cause: Schema.Defect(), | ||
| }, | ||
| ) { | ||
| override get message(): string { | ||
| return "Could not reach the transcription provider."; | ||
| } | ||
| } | ||
|
|
||
| export class TranscriptionProviderError extends Schema.TaggedErrorClass<TranscriptionProviderError>()( | ||
| "TranscriptionProviderError", | ||
| { | ||
| provider: Schema.String, | ||
| providerStatus: Schema.Int, | ||
| }, | ||
| ) { | ||
| override get message(): string { | ||
| return "The transcription provider rejected the request. Check the provider and API key."; | ||
| } | ||
| } | ||
|
|
||
| export class TranscriptionResponseError extends Schema.TaggedErrorClass<TranscriptionResponseError>()( | ||
| "TranscriptionResponseError", | ||
| { | ||
| provider: Schema.String, | ||
| cause: Schema.Defect(), | ||
| }, | ||
| ) { | ||
| override get message(): string { | ||
| return "The transcription provider returned an invalid response."; | ||
| } | ||
| } | ||
|
|
||
| const TranscriptionResponse = Schema.Struct({ text: Schema.String }); | ||
|
|
||
| export function resolveTranscriptionProvider(provider: string): VoiceTranscriptionProvider | null { | ||
| return provider === "openai" || provider === "groq" ? provider : null; | ||
| } | ||
|
|
||
| export function transcriptionProviderConfig(provider: VoiceTranscriptionProvider) { | ||
| return PROVIDERS[provider]; | ||
| } | ||
|
|
||
| export const readTranscriptionAudio = <E, R>(stream: Stream.Stream<Uint8Array, E, R>) => | ||
| stream.pipe( | ||
| Stream.mapError((cause) => new TranscriptionBodyReadError({ cause })), | ||
| Stream.runFoldEffect( | ||
| () => ({ chunks: [] as Uint8Array[], size: 0 }), | ||
| (accumulator, chunk) => { | ||
| const size = accumulator.size + chunk.byteLength; | ||
| if (size > MAX_TRANSCRIPTION_AUDIO_BYTES) { | ||
| return Effect.fail(new TranscriptionAudioTooLargeError({ receivedBytes: size })); | ||
| } | ||
| accumulator.chunks.push(chunk); | ||
| return Effect.succeed({ chunks: accumulator.chunks, size }); | ||
| }, | ||
| ), | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
| Effect.map(({ chunks, size }) => { | ||
| const audio = new Uint8Array(size); | ||
| let offset = 0; | ||
| for (const chunk of chunks) { | ||
| audio.set(chunk, offset); | ||
| offset += chunk.byteLength; | ||
| } | ||
| return audio; | ||
| }), | ||
| ); | ||
|
|
||
| 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 const forwardVoiceTranscription = Effect.fn("voiceTranscription.forward")(function* ( | ||
| input: VoiceTranscriptionInput, | ||
| ) { | ||
| if (input.audio.byteLength === 0) { | ||
| return yield* new TranscriptionEmptyAudioError(); | ||
| } | ||
| if (input.audio.byteLength > MAX_TRANSCRIPTION_AUDIO_BYTES) { | ||
| return yield* new TranscriptionAudioTooLargeError({ | ||
| receivedBytes: input.audio.byteLength, | ||
| }); | ||
| } | ||
| const apiKey = input.apiKey.trim(); | ||
| if (!apiKey) { | ||
| return yield* new TranscriptionApiKeyMissingError(); | ||
| } | ||
|
|
||
| const providerConfig = transcriptionProviderConfig(input.provider); | ||
| const mimeType = input.audioMimeType.split(";", 1)[0]?.trim() || "audio/webm"; | ||
| const form = new FormData(); | ||
| form.set("model", providerConfig.model); | ||
| form.set( | ||
| "file", | ||
| new Blob([input.audio], { type: mimeType }), | ||
| `recording.${audioFileExtension(mimeType)}`, | ||
| ); | ||
|
|
||
| const httpClient = yield* HttpClient.HttpClient; | ||
|
macroscopeapp[bot] marked this conversation as resolved.
|
||
| const payload = yield* HttpClientRequest.post(providerConfig.endpoint).pipe( | ||
| HttpClientRequest.bearerToken(apiKey), | ||
| HttpClientRequest.bodyFormData(form), | ||
| httpClient.execute, | ||
| Effect.mapError((cause) => new TranscriptionRequestError({ provider: input.provider, cause })), | ||
| Effect.flatMap((response) => | ||
| Effect.gen(function* () { | ||
| if (response.status < 200 || response.status >= 300) { | ||
| return yield* new TranscriptionProviderError({ | ||
| provider: input.provider, | ||
| providerStatus: response.status, | ||
| }); | ||
| } | ||
| return yield* HttpClientResponse.schemaBodyJson(TranscriptionResponse)(response).pipe( | ||
| Effect.mapError( | ||
| (cause) => new TranscriptionResponseError({ provider: input.provider, cause }), | ||
| ), | ||
| ); | ||
| }), | ||
| ), | ||
| Effect.timeout("2 minutes"), | ||
| Effect.catchTag("TimeoutError", (cause) => | ||
| Effect.fail(new TranscriptionRequestError({ provider: input.provider, cause })), | ||
| ), | ||
|
macroscopeapp[bot] marked this conversation as resolved.
Outdated
|
||
| ); | ||
| return payload.text.trim(); | ||
| }); | ||
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.