Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

http.ts

154 lines5.9 KB

1import { decodeSpeechPcmRequest } from "@t3tools/shared/speech";
2import {
3 AuthOrchestrationOperateScope,
4 AuthSettingsWriteScope,
5 type AuthEnvironmentScope,
6 EnvironmentHttpApi,
7 EnvironmentVoiceBodyLimit,
8 SPEECH_MAX_OPTIONS_BYTES,
9} from "@t3tools/contracts";
10import * as ByteSize from "effect/ByteSize";
11import * as Effect from "effect/Effect";
12import * as Layer from "effect/Layer";
13import * as Option from "effect/Option";
14import * as HttpServerRequest from "effect/http/HttpServerRequest";
15import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
16​
17import {
18 annotateEnvironmentRequest,
19 failEnvironmentInternal,
20 failEnvironmentInvalidRequest,
21 requireEnvironmentScope,
22} from "../auth/http.ts";
23import * as SpeechPostProcessing from "./postProcessing.ts";
24import * as SpeechService from "./SpeechService.ts";
25​
26const MAX_TRANSCRIBE_BODY_BYTES = SpeechService.MAX_SPEECH_BYTES + SPEECH_MAX_OPTIONS_BYTES + 4;
27const parseAudio = Option.liftThrowable(decodeSpeechPcmRequest);
28​
29const bodyLimit = Layer.succeed(EnvironmentVoiceBodyLimit, (effect) =>
30 Effect.gen(function* () {
31 const request = yield* HttpServerRequest.HttpServerRequest;
32 if (Number(request.headers["content-length"]) > MAX_TRANSCRIBE_BODY_BYTES)
33 return yield* failEnvironmentInvalidRequest("invalid_audio");
34 return yield* effect.pipe(
35 Effect.provideService(
36 HttpServerRequest.MaxBodySize,
37 ByteSize.bytes(MAX_TRANSCRIBE_BODY_BYTES),
38 ),
39 );
40 }),
41);
42​
43const mapSpeechError = <A, R>(effect: Effect.Effect<A, SpeechService.SpeechError, R>) =>
44 effect.pipe(
45 Effect.catchTags({
46 SpeechInvalidAudioError: () => failEnvironmentInvalidRequest("invalid_audio"),
47 SpeechUnsupportedPlatformError: () => failEnvironmentInvalidRequest("speech_unavailable"),
48 SpeechBusyError: () => failEnvironmentInvalidRequest("speech_busy"),
49 SpeechDownloadCancelledError: () => failEnvironmentInvalidRequest("speech_busy"),
50 SpeechModelNotFoundError: () => failEnvironmentInvalidRequest("invalid_command"),
51 SpeechOperationError: (error) => failEnvironmentInternal("internal_error", error),
52 }),
53 );
54​
55export const speechHttpApiLayer = HttpApiBuilder.group(
56 EnvironmentHttpApi,
57 "voice",
58 Effect.fnUntraced(function* (handlers) {
59 const speech = yield* SpeechService.SpeechService;
60 const postProcessing = yield* SpeechPostProcessing.SpeechPostProcessing;
61 const guard = Effect.fnUntraced(function* (
62 endpoint: string,
63 scope: AuthEnvironmentScope = AuthOrchestrationOperateScope,
64 ) {
65 yield* annotateEnvironmentRequest(endpoint);
66 yield* requireEnvironmentScope(scope);
67 });
68 return handlers
69 .handle(
70 "status",
71 Effect.fn("environment.voice.status")(function* (args) {
72 yield* guard(args.endpoint.name);
73 return yield* speech.status.pipe(mapSpeechError);
74 }),
75 )
76 .handle(
77 "prepareModel",
78 Effect.fn("environment.voice.prepareModel")(function* (args) {
79 yield* guard(args.endpoint.name);
80 yield* speech.prepareModel.pipe(mapSpeechError);
81 }),
82 )
83 .handle(
84 "models",
85 Effect.fn("environment.voice.models")(function* (args) {
86 yield* guard(args.endpoint.name);
87 return yield* speech.models(args.query.benchmarks === "1").pipe(mapSpeechError);
88 }),
89 )
90 .handle(
91 "downloadModel",
92 Effect.fn("environment.voice.downloadModel")(function* (args) {
93 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
94 return yield* speech.downloadModel(args.payload.modelId).pipe(mapSpeechError);
95 }),
96 )
97 .handle(
98 "selectModel",
99 Effect.fn("environment.voice.selectModel")(function* (args) {
100 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
101 return yield* speech.selectModel(args.payload.modelId).pipe(mapSpeechError);
102 }),
103 )
104 .handle(
105 "cancelModelDownload",
106 Effect.fn("environment.voice.cancelModelDownload")(function* (args) {
107 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
108 return yield* speech.cancelDownload(args.payload.modelId).pipe(mapSpeechError);
109 }),
110 )
111 .handle(
112 "removeModel",
113 Effect.fn("environment.voice.removeModel")(function* (args) {
114 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
115 return yield* speech.removeModel(args.payload.modelId).pipe(mapSpeechError);
116 }),
117 )
118 .handle(
119 "updateAcceleration",
120 Effect.fn("environment.voice.updateAcceleration")(function* (args) {
121 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
122 return yield* speech.updateAcceleration(args.payload.acceleration).pipe(mapSpeechError);
123 }),
124 )
125 .handle(
126 "updateModelUnloadTimeout",
127 Effect.fn("environment.voice.updateModelUnloadTimeout")(function* (args) {
128 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
129 return yield* speech.updateModelUnloadTimeout(args.payload.timeout).pipe(mapSpeechError);
130 }),
131 )
132 .handle(
133 "transcribe",
134 Effect.fn("environment.voice.transcribe")(function* (args) {
135 yield* guard(args.endpoint.name);
136 const decoded = parseAudio(args.payload);
137 if (Option.isNone(decoded)) return yield* failEnvironmentInvalidRequest("invalid_audio");
138 const text = yield* speech
139 .transcribe(decoded.value.pcm, decoded.value.options)
140 .pipe(mapSpeechError);
141 return { text };
142 }),
143 )
144 .handle(
145 "postProcess",
146 Effect.fn("environment.voice.postProcess")(function* (args) {
147 yield* guard(args.endpoint.name);
148 return yield* postProcessing
149 .process(args.payload)
150 .pipe(Effect.catch((error) => failEnvironmentInternal("internal_error", error)));
151 }),
152 );
153 }),
154).pipe(Layer.provide(bodyLimit), Layer.provide(SpeechPostProcessing.layer));