The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { |
| 2 | DEFAULT_SPEECH_POST_PROCESSING_PROMPT, |
| 3 | type EnvironmentSpeechPostProcessingRequest, |
| 4 | } from "@t3tools/contracts"; |
| 5 | import * as Context from "effect/Context"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as FileSystem from "effect/FileSystem"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import { resolveSpeechPostProcessingModelSelection } from "@t3tools/shared/serverSettings"; |
| 10 | |
| 11 | import * as ProviderRegistry from "../provider/ProviderRegistry.ts"; |
| 12 | import * as ServerSettings from "../serverSettings.ts"; |
| 13 | import * as TextGeneration from "../textGeneration/TextGeneration.ts"; |
| 14 | import { buildTranscriptionPostProcessingPrompt } from "../textGeneration/TranscriptionPostProcessing.ts"; |
| 15 | import { applySpeechAliases } from "./customWords.ts"; |
| 16 | import { SpeechOperationError } from "./SpeechService.ts"; |
| 17 | |
| 18 | export class SpeechPostProcessing extends Context.Service< |
| 19 | SpeechPostProcessing, |
| 20 | { |
| 21 | readonly process: ( |
| 22 | input: EnvironmentSpeechPostProcessingRequest, |
| 23 | ) => Effect.Effect<{ readonly text: string }, SpeechOperationError>; |
| 24 | } |
| 25 | >()("t3/speech/postProcessing/SpeechPostProcessing") {} |
| 26 | |
| 27 | const make = Effect.gen(function* () { |
| 28 | const settingsService = yield* ServerSettings.ServerSettingsService; |
| 29 | const fileSystem = yield* FileSystem.FileSystem; |
| 30 | const textGeneration = yield* TextGeneration.TextGeneration; |
| 31 | const providerRegistry = yield* ProviderRegistry.ProviderRegistry; |
| 32 | return SpeechPostProcessing.of({ |
| 33 | process: Effect.fn("SpeechPostProcessing.process")( |
| 34 | function* (input) { |
| 35 | if (input.transcript.trim().length === 0) return { text: input.transcript }; |
| 36 | const settings = yield* settingsService.getSettings; |
| 37 | const providers = yield* providerRegistry.getProviders; |
| 38 | const { mode, customInstructions } = input.options.speechPostProcessingPrompt; |
| 39 | const instructions = |
| 40 | (mode === "custom" ? customInstructions.trim() : "") || |
| 41 | DEFAULT_SPEECH_POST_PROCESSING_PROMPT; |
| 42 | // Providers run in a working directory; cleanup must not read a checkout. |
| 43 | const cwd = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-voice-cleanup-" }); |
| 44 | const generated = yield* textGeneration.generateTranscriptionPostProcessing({ |
| 45 | cwd, |
| 46 | prompt: buildTranscriptionPostProcessingPrompt( |
| 47 | instructions, |
| 48 | input.transcript, |
| 49 | input.draft, |
| 50 | input.options.speechCorrectionWord, |
| 51 | ), |
| 52 | modelSelection: resolveSpeechPostProcessingModelSelection(settings, providers), |
| 53 | }); |
| 54 | const text = generated.transcription.trim(); |
| 55 | return { |
| 56 | text: |
| 57 | text.length > 0 |
| 58 | ? applySpeechAliases(text, input.options.speechCustomWords) |
| 59 | : input.transcript, |
| 60 | }; |
| 61 | }, |
| 62 | Effect.scoped, |
| 63 | Effect.mapError((cause) => new SpeechOperationError({ operation: "post-processing", cause })), |
| 64 | ), |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | export const layer = Layer.effect(SpeechPostProcessing, make); |