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 { encodeSpeechPcmRequest } from "@t3tools/shared/speech"; |
| 2 | // @effect-diagnostics nodeBuiltinImport:off globalFetch:off globalFetchInEffect:off - real HTTP streaming boundary test. |
| 3 | import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; |
| 4 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 5 | import { it as effectIt } from "@effect/vitest"; |
| 6 | import { expect, vi } from "vite-plus/test"; |
| 7 | import { |
| 8 | AuthSessionId, |
| 9 | AuthOrchestrationOperateScope, |
| 10 | AuthSettingsWriteScope, |
| 11 | type AuthEnvironmentScope, |
| 12 | CloudSpeechError, |
| 13 | DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, |
| 14 | SPEECH_MAX_OPTIONS_BYTES, |
| 15 | DEFAULT_SERVER_SETTINGS, |
| 16 | EnvironmentAuthenticatedAuth, |
| 17 | EnvironmentAuthenticatedPrincipal, |
| 18 | type EnvironmentSpeechModel, |
| 19 | EnvironmentVoiceHttpApi, |
| 20 | } from "@t3tools/contracts"; |
| 21 | import * as Effect from "effect/Effect"; |
| 22 | import * as Layer from "effect/Layer"; |
| 23 | import * as Redacted from "effect/Redacted"; |
| 24 | import * as Schema from "effect/Schema"; |
| 25 | import * as HttpRouter from "effect/http/HttpRouter"; |
| 26 | import * as HttpServer from "effect/http/HttpServer"; |
| 27 | import * as HttpApi from "effect/http-api/HttpApi"; |
| 28 | import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder"; |
| 29 | import type * as NetAddress from "effect/net/NetAddress"; |
| 30 | import * as SpeechService from "./SpeechService.ts"; |
| 31 | import * as CloudSpeech from "./CloudSpeech.ts"; |
| 32 | import * as ServerSettings from "../serverSettings.ts"; |
| 33 | import * as TextGeneration from "../textGeneration/TextGeneration.ts"; |
| 34 | import * as ProviderRegistry from "../provider/ProviderRegistry.ts"; |
| 35 | import { speechHttpApiLayer } from "./http.ts"; |
| 36 | |
| 37 | const decodeSessionId = Schema.decodeUnknownSync(AuthSessionId); |
| 38 | |
| 39 | const principal = (...scopes: AuthEnvironmentScope[]) => |
| 40 | Layer.succeed(EnvironmentAuthenticatedAuth, (effect) => |
| 41 | effect.pipe( |
| 42 | Effect.provideService(EnvironmentAuthenticatedPrincipal, { |
| 43 | sessionId: decodeSessionId("voice-test"), |
| 44 | subject: "test", |
| 45 | method: "bearer-access-token", |
| 46 | scopes: new Set(scopes), |
| 47 | }), |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | const dependencies = Layer.mergeAll( |
| 52 | Layer.mock(CloudSpeech.CloudSpeech)({}), |
| 53 | Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }), |
| 54 | Layer.mock(ServerSettings.ServerSettingsService)({ |
| 55 | getSettings: Effect.succeed(DEFAULT_SERVER_SETTINGS), |
| 56 | }), |
| 57 | Layer.succeed( |
| 58 | TextGeneration.TextGeneration, |
| 59 | TextGeneration.TextGeneration.of({ |
| 60 | generateCommitMessage: () => Effect.die("not used"), |
| 61 | generatePrContent: () => Effect.die("not used"), |
| 62 | generateBranchName: () => Effect.die("not used"), |
| 63 | generateThreadTitle: () => Effect.die("not used"), |
| 64 | generateReviewGuide: () => Effect.die("not used"), |
| 65 | generateTranscriptionPostProcessing: () => Effect.die("not used"), |
| 66 | }), |
| 67 | ), |
| 68 | ); |
| 69 | |
| 70 | effectIt.live("rejects oversized chunked audio without invoking transcription", () => |
| 71 | Effect.gen(function* () { |
| 72 | const transcribe = vi.fn(() => Effect.succeed("unexpected")); |
| 73 | const routes = HttpApiBuilder.layer( |
| 74 | HttpApi.make("environment").add(EnvironmentVoiceHttpApi), |
| 75 | ).pipe( |
| 76 | Layer.provide(speechHttpApiLayer), |
| 77 | Layer.provide(principal(AuthOrchestrationOperateScope)), |
| 78 | Layer.provide( |
| 79 | Layer.succeed(SpeechService.SpeechService, { |
| 80 | status: Effect.succeed({ supported: false as const, reason: "test" }), |
| 81 | prepareModel: Effect.void, |
| 82 | models: () => Effect.succeed({ models: [] }), |
| 83 | downloadModel: () => Effect.succeed({ models: [], cancelled: false }), |
| 84 | linkModels: () => Effect.succeed({ models: [] }), |
| 85 | selectModel: () => Effect.succeed({ models: [] }), |
| 86 | cancelDownload: () => Effect.succeed({ models: [] }), |
| 87 | updateAcceleration: () => Effect.succeed({ supported: false as const, reason: "test" }), |
| 88 | updateModelUnloadTimeout: () => |
| 89 | Effect.succeed({ supported: false as const, reason: "test" }), |
| 90 | transcribe, |
| 91 | startStream: () => Effect.die("not used"), |
| 92 | removeModel: () => Effect.succeed({ models: [] }), |
| 93 | }), |
| 94 | ), |
| 95 | ); |
| 96 | yield* Effect.gen(function* () { |
| 97 | yield* Layer.build(HttpRouter.serve(routes, { disableLogger: true, disableListenLog: true })); |
| 98 | const server = yield* HttpServer.HttpServer; |
| 99 | const address = server.address as NetAddress.InetAddress; |
| 100 | const host = address._tag === "InetAddressV6" ? "[::1]" : "127.0.0.1"; |
| 101 | const url = `http://${host}:${address.port}/api/voice/transcribe`; |
| 102 | const select = yield* Effect.promise(() => |
| 103 | fetch(new URL("models/select", url), { |
| 104 | method: "POST", |
| 105 | headers: { "content-type": "application/json" }, |
| 106 | body: JSON.stringify({ modelId: "test" }), |
| 107 | }), |
| 108 | ); |
| 109 | expect(select.status).toBe(403); |
| 110 | const changeKey = yield* Effect.promise(() => |
| 111 | fetch(new URL("cloud/xai/key", url), { |
| 112 | method: "POST", |
| 113 | headers: { "content-type": "application/json" }, |
| 114 | body: JSON.stringify({ key: "fixture" }), |
| 115 | }), |
| 116 | ); |
| 117 | expect(changeKey.status).toBe(403); |
| 118 | yield* Effect.promise(() => |
| 119 | expect( |
| 120 | fetch(url, { |
| 121 | method: "POST", |
| 122 | headers: { "content-type": "application/octet-stream" }, |
| 123 | body: new ReadableStream({ |
| 124 | start(controller) { |
| 125 | controller.enqueue( |
| 126 | new Uint8Array(SpeechService.MAX_SPEECH_BYTES + SPEECH_MAX_OPTIONS_BYTES + 4), |
| 127 | ); |
| 128 | controller.enqueue(new Uint8Array(4)); |
| 129 | controller.close(); |
| 130 | }, |
| 131 | }), |
| 132 | duplex: "half", |
| 133 | }), |
| 134 | ).resolves.toMatchObject({ status: 413 }), |
| 135 | ); |
| 136 | expect(transcribe).not.toHaveBeenCalled(); |
| 137 | }).pipe(Effect.scoped, Effect.provide(Layer.mergeAll(NodeHttpServer.layerTest, dependencies))); |
| 138 | }), |
| 139 | ); |
| 140 | |
| 141 | effectIt.effect( |
| 142 | "keeps keys write-only and returns provider failures without an auth challenge", |
| 143 | () => |
| 144 | Effect.gen(function* () { |
| 145 | let stored = false; |
| 146 | const reference = { provider: "work", reference: "op://Work/Voice/key" }; |
| 147 | const save = vi.fn(); |
| 148 | const transcribe = vi.fn(); |
| 149 | const cloud = Layer.mock(CloudSpeech.CloudSpeech)({ |
| 150 | configured: Effect.sync(() => ({ openai: stored, xai: false })), |
| 151 | models: Effect.succeed([{ id: "acme/transcribe-1", name: "Acme Transcribe" }]), |
| 152 | setKey: (provider, key) => |
| 153 | Effect.sync(() => { |
| 154 | save(provider, key); |
| 155 | stored = |
| 156 | provider === "openai" && |
| 157 | Redacted.isRedacted(key) && |
| 158 | Redacted.value(key) === "fixture"; |
| 159 | }), |
| 160 | transcribe: (provider, _pcm, _options, model) => { |
| 161 | transcribe(provider, model); |
| 162 | return Effect.fail(new CloudSpeechError({ provider, reason: "rejected-key" })); |
| 163 | }, |
| 164 | }); |
| 165 | const linkedModel: EnvironmentSpeechModel = { |
| 166 | id: "fixture/model", |
| 167 | name: "Fixture", |
| 168 | description: "", |
| 169 | size: 1, |
| 170 | languages: ["en"], |
| 171 | accuracy: 0, |
| 172 | speed: 0, |
| 173 | recommended: false, |
| 174 | supportsStreaming: false, |
| 175 | supportsLanguageDetection: false, |
| 176 | supportsTranslation: false, |
| 177 | active: true, |
| 178 | state: "installed", |
| 179 | linkedFrom: "/home/someone/models/fixture.gguf", |
| 180 | }; |
| 181 | const { handler, dispose } = HttpRouter.toWebHandler( |
| 182 | HttpApiBuilder.layer(HttpApi.make("environment").add(EnvironmentVoiceHttpApi)).pipe( |
| 183 | Layer.provide(speechHttpApiLayer), |
| 184 | Layer.provide(principal(AuthOrchestrationOperateScope, AuthSettingsWriteScope)), |
| 185 | Layer.provide( |
| 186 | Layer.mock(SpeechService.SpeechService)({ |
| 187 | models: () => Effect.succeed({ models: [linkedModel] }), |
| 188 | downloadModel: () => Effect.succeed({ models: [linkedModel], cancelled: true }), |
| 189 | }), |
| 190 | ), |
| 191 | Layer.provide(cloud), |
| 192 | Layer.provide(dependencies), |
| 193 | Layer.provide(HttpServer.layerServices), |
| 194 | Layer.provide(NodeServices.layer), |
| 195 | ), |
| 196 | { disableLogger: true }, |
| 197 | ); |
| 198 | yield* Effect.addFinalizer(() => Effect.promise(dispose)); |
| 199 | const context = yield* Layer.build(NodeServices.layer); |
| 200 | const call = (path: string, init?: RequestInit) => |
| 201 | Effect.promise(() => |
| 202 | handler(new Request(`http://localhost/api/voice/cloud/${path}`, init), context), |
| 203 | ); |
| 204 | const saved = yield* call("openai/key", { |
| 205 | method: "POST", |
| 206 | headers: { "content-type": "application/json" }, |
| 207 | body: JSON.stringify({ key: "fixture" }), |
| 208 | }); |
| 209 | expect(saved.status).toBe(204); |
| 210 | expect(yield* Effect.promise(() => saved.text())).toBe(""); |
| 211 | const keys = yield* call("keys"); |
| 212 | expect(yield* Effect.promise(() => keys.json())).toEqual({ |
| 213 | openai: true, |
| 214 | xai: false, |
| 215 | }); |
| 216 | const setReference = (key: unknown) => |
| 217 | call("xai/key", { |
| 218 | method: "POST", |
| 219 | headers: { "content-type": "application/json" }, |
| 220 | body: JSON.stringify({ key }), |
| 221 | }); |
| 222 | expect((yield* setReference(reference)).status).toBe(204); |
| 223 | expect(save).toHaveBeenLastCalledWith("xai", reference); |
| 224 | expect((yield* setReference(reference.reference)).status).toBe(400); |
| 225 | expect(save).toHaveBeenCalledTimes(2); |
| 226 | const failed = yield* call("openai/transcribe", { |
| 227 | method: "POST", |
| 228 | headers: { "content-type": "application/octet-stream" }, |
| 229 | body: encodeSpeechPcmRequest(new Uint8Array(4), DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS), |
| 230 | }); |
| 231 | expect(failed.status).toBe(422); |
| 232 | expect(yield* Effect.promise(() => failed.json())).toEqual({ |
| 233 | _tag: "CloudSpeechError", |
| 234 | provider: "openai", |
| 235 | reason: "rejected-key", |
| 236 | }); |
| 237 | expect(transcribe).toHaveBeenLastCalledWith("openai", undefined); |
| 238 | const catalog = yield* call("openrouter/models"); |
| 239 | expect(yield* Effect.promise(() => catalog.json())).toEqual({ |
| 240 | models: [{ id: "acme/transcribe-1", name: "Acme Transcribe" }], |
| 241 | }); |
| 242 | const audio = { |
| 243 | method: "POST", |
| 244 | headers: { "content-type": "application/octet-stream" }, |
| 245 | body: encodeSpeechPcmRequest(new Uint8Array(4), DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS), |
| 246 | }; |
| 247 | expect((yield* call("openrouter/transcribe?model=openrouter%2Fauto", audio)).status).toBe( |
| 248 | 400, |
| 249 | ); |
| 250 | expect((yield* call("openrouter/transcribe?model=acme%2Ftranscribe-1", audio)).status).toBe( |
| 251 | 422, |
| 252 | ); |
| 253 | expect(transcribe).toHaveBeenLastCalledWith("openrouter", "acme/transcribe-1"); |
| 254 | // Naming a path on the environment needs filesystem access, not only settings access. |
| 255 | const link = yield* Effect.promise(() => |
| 256 | handler( |
| 257 | new Request("http://localhost/api/voice/models/link", { |
| 258 | method: "POST", |
| 259 | headers: { "content-type": "application/json" }, |
| 260 | body: JSON.stringify({ path: "/models" }), |
| 261 | }), |
| 262 | context, |
| 263 | ), |
| 264 | ); |
| 265 | expect(link.status).toBe(403); |
| 266 | const listed = yield* Effect.promise(() => |
| 267 | handler(new Request("http://localhost/api/voice/models"), context), |
| 268 | ); |
| 269 | expect(yield* Effect.promise(() => listed.text())).not.toContain("/home/someone"); |
| 270 | const downloaded = yield* Effect.promise(() => |
| 271 | handler( |
| 272 | new Request("http://localhost/api/voice/models/download", { |
| 273 | method: "POST", |
| 274 | headers: { "content-type": "application/json" }, |
| 275 | body: JSON.stringify({ modelId: linkedModel.id }), |
| 276 | }), |
| 277 | context, |
| 278 | ), |
| 279 | ); |
| 280 | expect(downloaded.status).toBe(200); |
| 281 | const { linkedFrom: _, ...publicModel } = linkedModel; |
| 282 | expect(yield* Effect.promise(() => downloaded.json())).toEqual({ |
| 283 | models: [publicModel], |
| 284 | cancelled: true, |
| 285 | }); |
| 286 | }), |
| 287 | ); |
| 288 | |
| 289 | effectIt.effect.each([ |
| 290 | { |
| 291 | error: new SpeechService.SpeechInvalidAudioError({ byteLength: 3, message: "invalid PCM" }), |
| 292 | reason: "invalid_audio", |
| 293 | }, |
| 294 | { |
| 295 | error: new SpeechService.SpeechUnsupportedPlatformError({ |
| 296 | platform: "win32", |
| 297 | architecture: "arm64", |
| 298 | }), |
| 299 | reason: "speech_unavailable", |
| 300 | }, |
| 301 | { |
| 302 | malformedOptions: true, |
| 303 | error: new SpeechService.SpeechInvalidAudioError({ byteLength: 3, message: "invalid PCM" }), |
| 304 | reason: "invalid_audio", |
| 305 | }, |
| 306 | { |
| 307 | oversized: true, |
| 308 | error: new SpeechService.SpeechInvalidAudioError({ byteLength: 3, message: "invalid PCM" }), |
| 309 | reason: "invalid_audio", |
| 310 | }, |
| 311 | { |
| 312 | error: new SpeechService.SpeechBusyError({ operation: "transcription" }), |
| 313 | reason: "speech_busy", |
| 314 | }, |
| 315 | ])( |
| 316 | "returns a typed client error for $reason instead of HTTP 500", |
| 317 | ({ error, reason, ...options }) => |
| 318 | Effect.gen(function* () { |
| 319 | // Only authentication and the native service are replaced; exercise the actual HTTP handlers and codecs. |
| 320 | const transcribe = vi.fn(() => Effect.fail(error)); |
| 321 | const service = Layer.succeed(SpeechService.SpeechService, { |
| 322 | status: Effect.succeed({ supported: false as const, reason: "test" }), |
| 323 | prepareModel: Effect.void, |
| 324 | models: () => Effect.succeed({ models: [] }), |
| 325 | downloadModel: () => Effect.fail(error), |
| 326 | linkModels: () => Effect.fail(error), |
| 327 | selectModel: () => Effect.fail(error), |
| 328 | cancelDownload: () => Effect.fail(error), |
| 329 | updateAcceleration: () => Effect.fail(error), |
| 330 | updateModelUnloadTimeout: () => Effect.fail(error), |
| 331 | transcribe, |
| 332 | startStream: () => Effect.fail(error), |
| 333 | removeModel: () => Effect.fail(error), |
| 334 | }); |
| 335 | const api = HttpApi.make("environment").add(EnvironmentVoiceHttpApi); |
| 336 | const { handler, dispose } = HttpRouter.toWebHandler( |
| 337 | HttpApiBuilder.layer(api).pipe( |
| 338 | Layer.provide(speechHttpApiLayer), |
| 339 | Layer.provide(principal(AuthOrchestrationOperateScope)), |
| 340 | Layer.provide(service), |
| 341 | Layer.provide(HttpServer.layerServices), |
| 342 | Layer.provide(dependencies), |
| 343 | Layer.provide(NodeServices.layer), |
| 344 | ), |
| 345 | { disableLogger: true }, |
| 346 | ); |
| 347 | yield* Effect.addFinalizer(() => Effect.promise(dispose)); |
| 348 | const request = new Request("http://localhost/api/voice/transcribe", { |
| 349 | method: "POST", |
| 350 | headers: { "content-type": "application/octet-stream" }, |
| 351 | body: |
| 352 | "malformedOptions" in options |
| 353 | ? new Uint8Array([255, 255, 255, 255]) |
| 354 | : encodeSpeechPcmRequest(new Uint8Array(3), DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS), |
| 355 | }); |
| 356 | const decode = vi.spyOn(request, "arrayBuffer"); |
| 357 | if ("oversized" in options) |
| 358 | request.headers.set( |
| 359 | "content-length", |
| 360 | String(SpeechService.MAX_SPEECH_BYTES + SPEECH_MAX_OPTIONS_BYTES + 5), |
| 361 | ); |
| 362 | const context = yield* Layer.build(Layer.mergeAll(dependencies, NodeServices.layer)); |
| 363 | const response = yield* Effect.promise(() => handler(request, context)); |
| 364 | if ("oversized" in options) expect(decode).not.toHaveBeenCalled(); |
| 365 | if ("malformedOptions" in options || "oversized" in options) { |
| 366 | expect(transcribe).not.toHaveBeenCalled(); |
| 367 | } else { |
| 368 | expect(transcribe).toHaveBeenCalledWith( |
| 369 | new Uint8Array(3), |
| 370 | DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS, |
| 371 | ); |
| 372 | } |
| 373 | expect(response.status).toBe(400); |
| 374 | expect(yield* Effect.promise(() => response.json())).toMatchObject({ |
| 375 | code: "invalid_request", |
| 376 | reason, |
| 377 | }); |
| 378 | }), |
| 379 | ); |