Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 81 additions & 3 deletions apps/server/src/speech/stream.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
import { it } from "@effect/vitest";
import { expect } from "vite-plus/test";
import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";
import { HttpRouter, HttpServer, HttpServerRequest, HttpServerResponse } from "effect/http";
import { SpeechStreamEvent, DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS } from "@t3tools/contracts";
import type { SpeechService, SpeechStream } from "./SpeechService.ts";
import { runSpeechSocket } from "./stream.ts";
import {
AuthOrchestrationOperateScope,
AuthSessionId,
SpeechStreamEvent,
DEFAULT_SPEECH_TRANSCRIPTION_OPTIONS,
} from "@t3tools/contracts";
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
import * as SessionStore from "../auth/SessionStore.ts";
import { SpeechService, type SpeechStream } from "./SpeechService.ts";
import { runSpeechSocket, speechStreamRouteLayer } from "./stream.ts";
import type * as NetAddress from "effect/net/NetAddress";

const decodeEvent = Schema.decodeUnknownSync(Schema.fromJsonString(SpeechStreamEvent));
Expand Down Expand Up @@ -167,3 +175,73 @@ it.live("processes pipelined audio before the finish command", () => {
},
).pipe(Effect.scoped, Effect.provide(NodeHttpServer.layerTest));
});

it.live("stops in-flight work, closes the socket, and releases the stream on invalidation", () => {
const feeding = Promise.withResolvers<void>();
const released = Promise.withResolvers<boolean>();
let feedInterrupted = false;
const sessionId = AuthSessionId.make("speech-test");
const stream: SpeechStream = {
feed: () =>
Effect.sync(() => feeding.resolve()).pipe(
Effect.andThen(Effect.never),
Effect.onInterrupt(() => Effect.sync(() => (feedInterrupted = true))),
),
finish: Effect.never,
};
return Effect.gen(function* () {
const invalidated = yield* Deferred.make<void>();
yield* Layer.build(
HttpRouter.serve(speechStreamRouteLayer, {
disableLogger: true,
disableListenLog: true,
}).pipe(
Layer.provide(
Layer.mock(EnvironmentAuth.EnvironmentAuth, {
authenticateWebSocketUpgrade: () =>
Effect.succeed({
sessionId,
subject: "speech-test",
method: "bearer-access-token",
scopes: [AuthOrchestrationOperateScope],
proofKeyThumbprint: "thumbprint",
}),
}),
),
Layer.provide(
Layer.mock(SessionStore.SessionStore, {
cookieName: "test",
legacyCookieName: undefined,
awaitInvalidation: (id, proofKeyThumbprint) =>
id === sessionId && proofKeyThumbprint === "thumbprint"
? Deferred.await(invalidated)
: Effect.never,
}),
),
Layer.provide(
Layer.mock(SpeechService, {
startStream: () =>
Effect.acquireRelease(Effect.succeed(stream), () =>
Effect.sync(() => released.resolve(feedInterrupted)),
),
}),
),
),
);
const server = yield* HttpServer.HttpServer;
const origin = HttpServer.formatAddress(server.address).replace(/^http/, "ws");
const connection = client(`${origin}/ws/voice`);
yield* Effect.addFinalizer(() => Effect.sync(() => connection.socket.close()));
const closed = new Promise<void>((resolve) =>
connection.socket.addEventListener("close", () => resolve(), { once: true }),
);
expect(yield* Effect.promise(() => connection.next())).toEqual({ type: "ready" });
connection.socket.send(new Float32Array([0.25]));
yield* Effect.promise(() => feeding.promise);
yield* Deferred.succeed(invalidated, undefined);
const [, interruptedBeforeRelease] = yield* Effect.promise(() =>
Promise.all([closed, released.promise]),
);
expect(interruptedBeforeRelease).toBe(true);
}).pipe(Effect.scoped, Effect.provide(NodeHttpServer.layerTest));
});
9 changes: 8 additions & 1 deletion apps/server/src/speech/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as Scope from "effect/Scope";
import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/http";
import * as Socket from "effect/socket/Socket";
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
import * as SessionStore from "../auth/SessionStore.ts";
import * as SpeechService from "./SpeechService.ts";

const decodeCommand = Schema.decodeUnknownEffect(Schema.fromJsonString(SpeechStreamCommand));
Expand Down Expand Up @@ -80,6 +81,8 @@ export const runSpeechSocket = Effect.fn("speech.runSocket")(function* (
});
}
}).pipe(
// Stops the worker before the stream's slow cleanup, so it cannot send in the meantime.
Effect.scoped,
Effect.timeoutOrElse({
duration: "10 minutes",
// The socket read loop is already interrupted here; its writer can no longer send.
Expand All @@ -93,6 +96,7 @@ export const speechStreamRouteLayer = Layer.unwrap(
Effect.gen(function* () {
const auth = yield* EnvironmentAuth.EnvironmentAuth;
const speech = yield* SpeechService.SpeechService;
const sessions = yield* SessionStore.SessionStore;
return HttpRouter.add(
"GET",
SPEECH_STREAM_PATH,
Expand All @@ -105,7 +109,10 @@ export const speechStreamRouteLayer = Layer.unwrap(
if (!session.success.scopes.includes(AuthOrchestrationOperateScope))
return HttpServerResponse.empty({ status: 403 });
const socket = yield* request.upgrade;
yield* runSpeechSocket(socket, speech).pipe(Effect.scoped);
yield* Effect.raceFirst(
runSpeechSocket(socket, speech).pipe(Effect.scoped),
sessions.awaitInvalidation(session.success.sessionId, session.success.proofKeyThumbprint),
).pipe(Effect.ignore({ log: true }));
return HttpServerResponse.empty();
}),
);
Expand Down
Loading