import { decodeSpeechPcmRequest } from "@t3tools/shared/speech";
import {
AuthFilesystemReadScope,
AuthOrchestrationOperateScope,
AuthSettingsWriteScope,
type AuthEnvironmentScope,
EnvironmentAuthenticatedPrincipal,
EnvironmentHttpApi,
type EnvironmentSpeechModels,
EnvironmentVoiceBodyLimit,
SPEECH_MAX_OPTIONS_BYTES,
} from "@t3tools/contracts";
import * as ByteSize from "effect/ByteSize";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as HttpServerRequest from "effect/http/HttpServerRequest";
import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
import {
annotateEnvironmentRequest,
failEnvironmentInternal,
failEnvironmentInvalidRequest,
requireEnvironmentScope,
} from "../auth/http.ts";
import * as SpeechPostProcessing from "./postProcessing.ts";
import * as CloudSpeech from "./CloudSpeech.ts";
import * as SpeechService from "./SpeechService.ts";
import * as SpeechSynthesisService from "./SpeechSynthesisService.ts";
import { SpeechSynthesisError } from "@t3tools/contracts";
const MAX_TRANSCRIBE_BODY_BYTES = SpeechService.MAX_SPEECH_BYTES + SPEECH_MAX_OPTIONS_BYTES + 4;
const parseAudio = Option.liftThrowable(decodeSpeechPcmRequest);
const bodyLimit = Layer.succeed(EnvironmentVoiceBodyLimit, (effect) =>
Effect.gen(function* () {
const request = yield* HttpServerRequest.HttpServerRequest;
if (Number(request.headers["content-length"]) > MAX_TRANSCRIBE_BODY_BYTES)
return yield* failEnvironmentInvalidRequest("invalid_audio");
return yield* effect.pipe(
Effect.provideService(
HttpServerRequest.MaxBodySize,
ByteSize.bytes(MAX_TRANSCRIBE_BODY_BYTES),
),
);
}),
);
const mapSpeechError = (effect: Effect.Effect) =>
effect.pipe(
Effect.catchTags({
SpeechInvalidAudioError: () => failEnvironmentInvalidRequest("invalid_audio"),
SpeechUnsupportedPlatformError: () => failEnvironmentInvalidRequest("speech_unavailable"),
SpeechBusyError: () => failEnvironmentInvalidRequest("speech_busy"),
SpeechDownloadCancelledError: () => failEnvironmentInvalidRequest("speech_busy"),
SpeechModelNotFoundError: () => failEnvironmentInvalidRequest("invalid_command"),
SpeechOperationError: (error) => failEnvironmentInternal("internal_error", error),
}),
);
// A linked model's origin is a path on this machine; only filesystem readers may see it.
// Untraced: it runs inside each handler's own span.
const withoutHostPaths = Effect.fnUntraced(function* (result: EnvironmentSpeechModels) {
const session = yield* EnvironmentAuthenticatedPrincipal;
if (session.scopes.has(AuthFilesystemReadScope)) return result;
return { models: result.models.map(({ linkedFrom: _, ...model }) => model) };
});
const mapSynthesisError = (
effect: Effect.Effect<
A,
SpeechSynthesisError | SpeechSynthesisService.SpeechSynthesisOperationError,
R
>,
) =>
effect.pipe(
Effect.catchTags({
SpeechSynthesisOperationError: (error) =>
Effect.logWarning("Speech synthesis failed", error).pipe(
Effect.andThen(Effect.fail(new SpeechSynthesisError({ reason: "unavailable" }))),
),
}),
);
export const speechHttpApiLayer = HttpApiBuilder.group(
EnvironmentHttpApi,
"voice",
Effect.fnUntraced(function* (handlers) {
const speech = yield* SpeechService.SpeechService;
const synthesis = yield* SpeechSynthesisService.SpeechSynthesisService;
const cloud = yield* CloudSpeech.CloudSpeech;
const postProcessing = yield* SpeechPostProcessing.SpeechPostProcessing;
const guard = Effect.fnUntraced(function* (
endpoint: string,
scope: AuthEnvironmentScope = AuthOrchestrationOperateScope,
) {
yield* annotateEnvironmentRequest(endpoint);
yield* requireEnvironmentScope(scope);
});
return handlers
.handle(
"elevenLabsVoices",
Effect.fn("environment.voice.elevenLabsVoices")(function* (args) {
yield* guard(args.endpoint.name);
return yield* synthesis.elevenLabsVoices(args.payload.pageToken).pipe(mapSynthesisError);
}),
)
.handle(
"synthesisVoices",
Effect.fn("environment.voice.synthesisVoices")(function* (args) {
yield* guard(args.endpoint.name);
return yield* synthesis.voices.pipe(mapSynthesisError);
}),
)
.handle(
"installSynthesisRuntime",
Effect.fn("environment.voice.installSynthesisRuntime")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* synthesis.installRuntime.pipe(mapSynthesisError);
}),
)
.handle(
"cancelSynthesisRuntime",
Effect.fn("environment.voice.cancelSynthesisRuntime")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* synthesis.cancelRuntime.pipe(mapSynthesisError);
}),
)
.handle(
"removeSynthesisRuntime",
Effect.fn("environment.voice.removeSynthesisRuntime")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* synthesis.removeRuntime.pipe(mapSynthesisError);
}),
)
.handle(
"downloadSynthesisVoice",
Effect.fn("environment.voice.downloadSynthesisVoice")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* synthesis.downloadVoice(args.payload.voiceId).pipe(mapSynthesisError);
}),
)
.handle(
"cancelSynthesisVoiceDownload",
Effect.fn("environment.voice.cancelSynthesisVoiceDownload")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* synthesis.cancelVoiceDownload(args.payload.voiceId).pipe(mapSynthesisError);
}),
)
.handle(
"selectSynthesisVoice",
Effect.fn("environment.voice.selectSynthesisVoice")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* synthesis.selectVoice(args.payload.voiceId).pipe(mapSynthesisError);
}),
)
.handle(
"removeSynthesisVoice",
Effect.fn("environment.voice.removeSynthesisVoice")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* synthesis.removeVoice(args.payload.voiceId).pipe(mapSynthesisError);
}),
)
.handle(
"synthesize",
Effect.fn("environment.voice.synthesize")(function* (args) {
yield* guard(args.endpoint.name);
return yield* synthesis.synthesize(args.payload).pipe(mapSynthesisError);
}),
)
.handle(
"cloudKeys",
Effect.fn("environment.voice.cloudKeys")(function* (args) {
yield* guard(args.endpoint.name);
return yield* cloud.configured.pipe(
Effect.catch((error) => failEnvironmentInternal("internal_error", error)),
);
}),
)
.handle(
"setCloudKey",
Effect.fn("environment.voice.setCloudKey")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
yield* cloud
.setKey(args.params.provider, args.payload.key)
.pipe(Effect.catch((error) => failEnvironmentInternal("internal_error", error)));
}),
)
.handle(
"cloudModels",
Effect.fn("environment.voice.cloudModels")(function* (args) {
yield* guard(args.endpoint.name);
return { models: yield* cloud.models };
}),
)
.handle(
"transcribeCloud",
Effect.fn("environment.voice.transcribeCloud")(function* (args) {
yield* guard(args.endpoint.name);
const decoded = parseAudio(args.payload);
if (Option.isNone(decoded)) return yield* failEnvironmentInvalidRequest("invalid_audio");
const text = yield* cloud
.transcribe(
args.params.provider,
decoded.value.pcm,
decoded.value.options,
args.query.model,
)
.pipe(
Effect.catchTags({
ServerSettingsError: (error) => failEnvironmentInternal("internal_error", error),
SpeechInvalidAudioError: () => failEnvironmentInvalidRequest("invalid_audio"),
}),
);
return { text };
}),
)
.handle(
"status",
Effect.fn("environment.voice.status")(function* (args) {
yield* guard(args.endpoint.name);
return yield* speech.status.pipe(mapSpeechError);
}),
)
.handle(
"prepareModel",
Effect.fn("environment.voice.prepareModel")(function* (args) {
yield* guard(args.endpoint.name);
yield* speech.prepareModel.pipe(mapSpeechError);
}),
)
.handle(
"models",
Effect.fn("environment.voice.models")(function* (args) {
yield* guard(args.endpoint.name);
return yield* speech
.models(args.query.benchmarks === "1")
.pipe(mapSpeechError, Effect.flatMap(withoutHostPaths));
}),
)
.handle(
"downloadModel",
Effect.fn("environment.voice.downloadModel")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* speech
.downloadModel(args.payload.modelId)
.pipe(mapSpeechError, Effect.flatMap(withoutHostPaths));
}),
)
.handle(
"selectModel",
Effect.fn("environment.voice.selectModel")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* speech
.selectModel(args.payload.modelId)
.pipe(mapSpeechError, Effect.flatMap(withoutHostPaths));
}),
)
.handle(
"cancelModelDownload",
Effect.fn("environment.voice.cancelModelDownload")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* speech
.cancelDownload(args.payload.modelId)
.pipe(mapSpeechError, Effect.flatMap(withoutHostPaths));
}),
)
.handle(
"removeModel",
Effect.fn("environment.voice.removeModel")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* speech
.removeModel(args.payload.modelId)
.pipe(mapSpeechError, Effect.flatMap(withoutHostPaths));
}),
)
.handle(
"linkModels",
Effect.fn("environment.voice.linkModels")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
// The request names a path on this machine and the result reveals what is there.
yield* requireEnvironmentScope(AuthFilesystemReadScope);
return yield* speech.linkModels(args.payload.path).pipe(mapSpeechError);
}),
)
.handle(
"updateAcceleration",
Effect.fn("environment.voice.updateAcceleration")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* speech.updateAcceleration(args.payload.acceleration).pipe(mapSpeechError);
}),
)
.handle(
"updateModelUnloadTimeout",
Effect.fn("environment.voice.updateModelUnloadTimeout")(function* (args) {
yield* guard(args.endpoint.name, AuthSettingsWriteScope);
return yield* speech.updateModelUnloadTimeout(args.payload.timeout).pipe(mapSpeechError);
}),
)
.handle(
"transcribe",
Effect.fn("environment.voice.transcribe")(function* (args) {
yield* guard(args.endpoint.name);
const decoded = parseAudio(args.payload);
if (Option.isNone(decoded)) return yield* failEnvironmentInvalidRequest("invalid_audio");
const text = yield* speech
.transcribe(decoded.value.pcm, decoded.value.options)
.pipe(mapSpeechError);
return { text };
}),
)
.handle(
"postProcess",
Effect.fn("environment.voice.postProcess")(function* (args) {
yield* guard(args.endpoint.name);
return yield* postProcessing
.process(args.payload)
.pipe(Effect.catch((error) => failEnvironmentInternal("internal_error", error)));
}),
);
}),
).pipe(Layer.provide(bodyLimit), Layer.provide(SpeechPostProcessing.layer));