The best way to control Claude Code, Codex and more from any computer or phone

main
ts 240 lines 9.07 KB
Raw
1 import { decodeSpeechPcmRequest } from "@t3tools/shared/speech";
2 import {
3 AuthFilesystemReadScope,
4 AuthOrchestrationOperateScope,
5 AuthSettingsWriteScope,
6 type AuthEnvironmentScope,
7 EnvironmentAuthenticatedPrincipal,
8 EnvironmentHttpApi,
9 type EnvironmentSpeechModels,
10 EnvironmentVoiceBodyLimit,
11 SPEECH_MAX_OPTIONS_BYTES,
12 } from "@t3tools/contracts";
13 import * as ByteSize from "effect/ByteSize";
14 import * as Effect from "effect/Effect";
15 import * as Layer from "effect/Layer";
16 import * as Option from "effect/Option";
17 import * as HttpServerRequest from "effect/http/HttpServerRequest";
18 import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
19
20 import {
21 annotateEnvironmentRequest,
22 failEnvironmentInternal,
23 failEnvironmentInvalidRequest,
24 requireEnvironmentScope,
25 } from "../auth/http.ts";
26 import * as SpeechPostProcessing from "./postProcessing.ts";
27 import * as CloudSpeech from "./CloudSpeech.ts";
28 import * as SpeechService from "./SpeechService.ts";
29
30 const MAX_TRANSCRIBE_BODY_BYTES = SpeechService.MAX_SPEECH_BYTES + SPEECH_MAX_OPTIONS_BYTES + 4;
31 const parseAudio = Option.liftThrowable(decodeSpeechPcmRequest);
32
33 const bodyLimit = Layer.succeed(EnvironmentVoiceBodyLimit, (effect) =>
34 Effect.gen(function* () {
35 const request = yield* HttpServerRequest.HttpServerRequest;
36 if (Number(request.headers["content-length"]) > MAX_TRANSCRIBE_BODY_BYTES)
37 return yield* failEnvironmentInvalidRequest("invalid_audio");
38 return yield* effect.pipe(
39 Effect.provideService(
40 HttpServerRequest.MaxBodySize,
41 ByteSize.bytes(MAX_TRANSCRIBE_BODY_BYTES),
42 ),
43 );
44 }),
45 );
46
47 const mapSpeechError = <A, R>(effect: Effect.Effect<A, SpeechService.SpeechError, R>) =>
48 effect.pipe(
49 Effect.catchTags({
50 SpeechInvalidAudioError: () => failEnvironmentInvalidRequest("invalid_audio"),
51 SpeechUnsupportedPlatformError: () => failEnvironmentInvalidRequest("speech_unavailable"),
52 SpeechBusyError: () => failEnvironmentInvalidRequest("speech_busy"),
53 SpeechDownloadCancelledError: () => failEnvironmentInvalidRequest("speech_busy"),
54 SpeechModelNotFoundError: () => failEnvironmentInvalidRequest("invalid_command"),
55 SpeechOperationError: (error) => failEnvironmentInternal("internal_error", error),
56 }),
57 );
58
59 // A linked model's origin is a path on this machine; only filesystem readers may see it.
60 // Untraced: it runs inside each handler's own span.
61 const withoutHostPaths = Effect.fnUntraced(function* <Fields extends EnvironmentSpeechModels>({
62 models,
63 ...fields
64 }: Fields) {
65 const session = yield* EnvironmentAuthenticatedPrincipal;
66 return {
67 ...fields,
68 models: session.scopes.has(AuthFilesystemReadScope)
69 ? models
70 : models.map(({ linkedFrom: _, ...model }) => model),
71 };
72 });
73
74 export const speechHttpApiLayer = HttpApiBuilder.group(
75 EnvironmentHttpApi,
76 "voice",
77 Effect.fnUntraced(function* (handlers) {
78 const speech = yield* SpeechService.SpeechService;
79 const cloud = yield* CloudSpeech.CloudSpeech;
80 const postProcessing = yield* SpeechPostProcessing.SpeechPostProcessing;
81 const guard = Effect.fnUntraced(function* (
82 endpoint: string,
83 scope: AuthEnvironmentScope = AuthOrchestrationOperateScope,
84 ) {
85 yield* annotateEnvironmentRequest(endpoint);
86 yield* requireEnvironmentScope(scope);
87 });
88 return handlers
89 .handle(
90 "cloudKeys",
91 Effect.fn("environment.voice.cloudKeys")(function* (args) {
92 yield* guard(args.endpoint.name);
93 return yield* cloud.configured.pipe(
94 Effect.catch((error) => failEnvironmentInternal("internal_error", error)),
95 );
96 }),
97 )
98 .handle(
99 "setCloudKey",
100 Effect.fn("environment.voice.setCloudKey")(function* (args) {
101 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
102 yield* cloud
103 .setKey(args.params.provider, args.payload.key)
104 .pipe(Effect.catch((error) => failEnvironmentInternal("internal_error", error)));
105 }),
106 )
107 .handle(
108 "cloudModels",
109 Effect.fn("environment.voice.cloudModels")(function* (args) {
110 yield* guard(args.endpoint.name);
111 return { models: yield* cloud.models };
112 }),
113 )
114 .handle(
115 "transcribeCloud",
116 Effect.fn("environment.voice.transcribeCloud")(function* (args) {
117 yield* guard(args.endpoint.name);
118 const decoded = parseAudio(args.payload);
119 if (Option.isNone(decoded)) return yield* failEnvironmentInvalidRequest("invalid_audio");
120 const text = yield* cloud
121 .transcribe(
122 args.params.provider,
123 decoded.value.pcm,
124 decoded.value.options,
125 args.query.model,
126 )
127 .pipe(
128 Effect.catchTags({
129 ServerSettingsError: (error) => failEnvironmentInternal("internal_error", error),
130 SpeechInvalidAudioError: () => failEnvironmentInvalidRequest("invalid_audio"),
131 }),
132 );
133 return { text };
134 }),
135 )
136 .handle(
137 "status",
138 Effect.fn("environment.voice.status")(function* (args) {
139 yield* guard(args.endpoint.name);
140 return yield* speech.status.pipe(mapSpeechError);
141 }),
142 )
143 .handle(
144 "prepareModel",
145 Effect.fn("environment.voice.prepareModel")(function* (args) {
146 yield* guard(args.endpoint.name);
147 yield* speech.prepareModel.pipe(mapSpeechError);
148 }),
149 )
150 .handle(
151 "models",
152 Effect.fn("environment.voice.models")(function* (args) {
153 yield* guard(args.endpoint.name);
154 return yield* speech
155 .models(args.query.benchmarks === "1")
156 .pipe(mapSpeechError, Effect.flatMap(withoutHostPaths));
157 }),
158 )
159 .handle(
160 "downloadModel",
161 Effect.fn("environment.voice.downloadModel")(function* (args) {
162 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
163 return yield* speech
164 .downloadModel(args.payload.modelId)
165 .pipe(mapSpeechError, Effect.flatMap(withoutHostPaths));
166 }),
167 )
168 .handle(
169 "selectModel",
170 Effect.fn("environment.voice.selectModel")(function* (args) {
171 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
172 return yield* speech
173 .selectModel(args.payload.modelId)
174 .pipe(mapSpeechError, Effect.flatMap(withoutHostPaths));
175 }),
176 )
177 .handle(
178 "cancelModelDownload",
179 Effect.fn("environment.voice.cancelModelDownload")(function* (args) {
180 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
181 return yield* speech
182 .cancelDownload(args.payload.modelId)
183 .pipe(mapSpeechError, Effect.flatMap(withoutHostPaths));
184 }),
185 )
186 .handle(
187 "removeModel",
188 Effect.fn("environment.voice.removeModel")(function* (args) {
189 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
190 return yield* speech
191 .removeModel(args.payload.modelId)
192 .pipe(mapSpeechError, Effect.flatMap(withoutHostPaths));
193 }),
194 )
195 .handle(
196 "linkModels",
197 Effect.fn("environment.voice.linkModels")(function* (args) {
198 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
199 // The request names a path on this machine and the result reveals what is there.
200 yield* requireEnvironmentScope(AuthFilesystemReadScope);
201 return yield* speech.linkModels(args.payload.path).pipe(mapSpeechError);
202 }),
203 )
204 .handle(
205 "updateAcceleration",
206 Effect.fn("environment.voice.updateAcceleration")(function* (args) {
207 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
208 return yield* speech.updateAcceleration(args.payload.acceleration).pipe(mapSpeechError);
209 }),
210 )
211 .handle(
212 "updateModelUnloadTimeout",
213 Effect.fn("environment.voice.updateModelUnloadTimeout")(function* (args) {
214 yield* guard(args.endpoint.name, AuthSettingsWriteScope);
215 return yield* speech.updateModelUnloadTimeout(args.payload.timeout).pipe(mapSpeechError);
216 }),
217 )
218 .handle(
219 "transcribe",
220 Effect.fn("environment.voice.transcribe")(function* (args) {
221 yield* guard(args.endpoint.name);
222 const decoded = parseAudio(args.payload);
223 if (Option.isNone(decoded)) return yield* failEnvironmentInvalidRequest("invalid_audio");
224 const text = yield* speech
225 .transcribe(decoded.value.pcm, decoded.value.options)
226 .pipe(mapSpeechError);
227 return { text };
228 }),
229 )
230 .handle(
231 "postProcess",
232 Effect.fn("environment.voice.postProcess")(function* (args) {
233 yield* guard(args.endpoint.name);
234 return yield* postProcessing
235 .process(args.payload)
236 .pipe(Effect.catch((error) => failEnvironmentInternal("internal_error", error)));
237 }),
238 );
239 }),
240 ).pipe(Layer.provide(bodyLimit), Layer.provide(SpeechPostProcessing.layer));