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

http.ts

203 lines7.8 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 CloudSpeech from "./CloudSpeech.ts";
25import * as SpeechService from "./SpeechService.ts";
26​
27const MAX_TRANSCRIBE_BODY_BYTES = SpeechService.MAX_SPEECH_BYTES + SPEECH_MAX_OPTIONS_BYTES + 4;
28const parseAudio = Option.liftThrowable(decodeSpeechPcmRequest);
29​
30const bodyLimit = Layer.succeed(EnvironmentVoiceBodyLimit, (effect) =>
31 Effect.gen(function* () {
32 const request = yield* HttpServerRequest.HttpServerRequest;
33 if (Number(request.headers["content-length"]) > MAX_TRANSCRIBE_BODY_BYTES)
34 return yield* failEnvironmentInvalidRequest("invalid_audio");
35 return yield* effect.pipe(
36 Effect.provideService(
37 HttpServerRequest.MaxBodySize,
38 ByteSize.bytes(MAX_TRANSCRIBE_BODY_BYTES),
39 ),
40 );
41 }),
42);
43​
44const mapSpeechError = <A, R>(effect: Effect.Effect<A, SpeechService.SpeechError, R>) =>
45 effect.pipe(
46 Effect.catchTags({
47 SpeechInvalidAudioError: () => failEnvironmentInvalidRequest("invalid_audio"),
48 SpeechUnsupportedPlatformError: () => failEnvironmentInvalidRequest("speech_unavailable"),
49 SpeechBusyError: () => failEnvironmentInvalidRequest("speech_busy"),
50 SpeechDownloadCancelledError: () => failEnvironmentInvalidRequest("speech_busy"),
51 SpeechModelNotFoundError: () => failEnvironmentInvalidRequest("invalid_command"),
52 SpeechOperationError: (error) => failEnvironmentInternal("internal_error", error),
53 }),
54 );
55​
56export const speechHttpApiLayer = HttpApiBuilder.group(
57 EnvironmentHttpApi,
58 "voice",
59 Effect.fnUntraced(function* (handlers) {
60 const speech = yield* SpeechService.SpeechService;
61 const cloud = yield* CloudSpeech.CloudSpeech;
62 const postProcessing = yield* SpeechPostProcessing.SpeechPostProcessing;
63 const guard = Effect.fnUntraced(function* (
64 endpoint: string,
65 scope: AuthEnvironmentScope = AuthOrchestrationOperateScope,
66 ) {
67 yield* annotateEnvironmentRequest(endpoint);
68 yield* requireEnvironmentScope(scope);
69 });
70 return handlers
71 .handle(
72 "cloudKeys",
73 Effect.fn("environment.voice.cloudKeys")(function* (args) {
74 yield* guard(args.endpoint.name);
75 return yield* cloud.configured.pipe(
76 Effect.catch((error) => failEnvironmentInternal("internal_error", error)),
77 );
78 }),
79 )
80 .handle(
81 "setCloudKey",
82 Effect.fn("environment.voice.setCloudKey")(function* (args) {
83 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
84 yield* cloud
85 .setKey(args.params.provider, args.payload.key)
86 .pipe(Effect.catch((error) => failEnvironmentInternal("internal_error", error)));
87 }),
88 )
89 .handle(
90 "cloudModels",
91 Effect.fn("environment.voice.cloudModels")(function* (args) {
92 yield* guard(args.endpoint.name);
93 return { models: yield* cloud.models };
94 }),
95 )
96 .handle(
97 "transcribeCloud",
98 Effect.fn("environment.voice.transcribeCloud")(function* (args) {
99 yield* guard(args.endpoint.name);
100 const decoded = parseAudio(args.payload);
101 if (Option.isNone(decoded)) return yield* failEnvironmentInvalidRequest("invalid_audio");
102 const text = yield* cloud
103 .transcribe(
104 args.params.provider,
105 decoded.value.pcm,
106 decoded.value.options,
107 args.query.model,
108 )
109 .pipe(
110 Effect.catchTags({
111 ServerSettingsError: (error) => failEnvironmentInternal("internal_error", error),
112 SpeechInvalidAudioError: () => failEnvironmentInvalidRequest("invalid_audio"),
113 }),
114 );
115 return { text };
116 }),
117 )
118 .handle(
119 "status",
120 Effect.fn("environment.voice.status")(function* (args) {
121 yield* guard(args.endpoint.name);
122 return yield* speech.status.pipe(mapSpeechError);
123 }),
124 )
125 .handle(
126 "prepareModel",
127 Effect.fn("environment.voice.prepareModel")(function* (args) {
128 yield* guard(args.endpoint.name);
129 yield* speech.prepareModel.pipe(mapSpeechError);
130 }),
131 )
132 .handle(
133 "models",
134 Effect.fn("environment.voice.models")(function* (args) {
135 yield* guard(args.endpoint.name);
136 return yield* speech.models(args.query.benchmarks === "1").pipe(mapSpeechError);
137 }),
138 )
139 .handle(
140 "downloadModel",
141 Effect.fn("environment.voice.downloadModel")(function* (args) {
142 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
143 return yield* speech.downloadModel(args.payload.modelId).pipe(mapSpeechError);
144 }),
145 )
146 .handle(
147 "selectModel",
148 Effect.fn("environment.voice.selectModel")(function* (args) {
149 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
150 return yield* speech.selectModel(args.payload.modelId).pipe(mapSpeechError);
151 }),
152 )
153 .handle(
154 "cancelModelDownload",
155 Effect.fn("environment.voice.cancelModelDownload")(function* (args) {
156 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
157 return yield* speech.cancelDownload(args.payload.modelId).pipe(mapSpeechError);
158 }),
159 )
160 .handle(
161 "removeModel",
162 Effect.fn("environment.voice.removeModel")(function* (args) {
163 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
164 return yield* speech.removeModel(args.payload.modelId).pipe(mapSpeechError);
165 }),
166 )
167 .handle(
168 "updateAcceleration",
169 Effect.fn("environment.voice.updateAcceleration")(function* (args) {
170 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
171 return yield* speech.updateAcceleration(args.payload.acceleration).pipe(mapSpeechError);
172 }),
173 )
174 .handle(
175 "updateModelUnloadTimeout",
176 Effect.fn("environment.voice.updateModelUnloadTimeout")(function* (args) {
177 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
178 return yield* speech.updateModelUnloadTimeout(args.payload.timeout).pipe(mapSpeechError);
179 }),
180 )
181 .handle(
182 "transcribe",
183 Effect.fn("environment.voice.transcribe")(function* (args) {
184 yield* guard(args.endpoint.name);
185 const decoded = parseAudio(args.payload);
186 if (Option.isNone(decoded)) return yield* failEnvironmentInvalidRequest("invalid_audio");
187 const text = yield* speech
188 .transcribe(decoded.value.pcm, decoded.value.options)
189 .pipe(mapSpeechError);
190 return { text };
191 }),
192 )
193 .handle(
194 "postProcess",
195 Effect.fn("environment.voice.postProcess")(function* (args) {
196 yield* guard(args.endpoint.name);
197 return yield* postProcessing
198 .process(args.payload)
199 .pipe(Effect.catch((error) => failEnvironmentInternal("internal_error", error)));
200 }),
201 );
202 }),
203).pipe(Layer.provide(bodyLimit), Layer.provide(SpeechPostProcessing.layer));