diff --git a/apps/web/__tests__/unit/meetings-route.test.ts b/apps/web/__tests__/unit/meetings-route.test.ts index ff09558b16d..4ef838c3d06 100644 --- a/apps/web/__tests__/unit/meetings-route.test.ts +++ b/apps/web/__tests__/unit/meetings-route.test.ts @@ -554,6 +554,67 @@ describe("POST /transcribe", () => { expect(mocks.remove).toHaveBeenCalledWith("tr_1"); }); + it("asks for speaker labels in a per-user session and returns the letters", async () => { + signIn(); + mocks.submit.mockResolvedValue({ + id: "tr_s", + status: "completed", + text: "Hi there.", + language_code: "en", + words: [ + { text: "Hi", start: 100, end: 300, confidence: 0.9, speaker: "A" }, + { + text: "there.", + start: 320, + end: 700, + confidence: 0.9, + speaker: null, + }, + ], + }); + const response = await transcribe( + wav(16_000), + "?session=doc_1:system&seq=4", + ); + expect(response.status).toBe(200); + expect((await response.json()).words).toEqual([ + { text: "Hi", startMs: 100, endMs: 300, confidence: 0.9, speaker: "A" }, + { + text: "there.", + startMs: 320, + endMs: 700, + confidence: 0.9, + speaker: null, + }, + ]); + const params = mocks.submit.mock.calls[0]?.[0] as Record; + expect(params).toMatchObject({ speaker_labels: true, cap_speaker_seq: 4 }); + expect(String(params.cap_speaker_session)).toMatch(/^.+:doc_1:system$/); + expect(String(params.cap_speaker_session)).not.toBe("doc_1:system"); + }); + + it("sends no speaker fields without a session", async () => { + signIn(); + mocks.submit.mockResolvedValue({ + id: "tr_n", + status: "completed", + text: "", + language_code: "en", + words: [], + }); + await transcribe(wav(16_000)); + const params = mocks.submit.mock.calls[0]?.[0] as Record; + expect(params).not.toHaveProperty("speaker_labels"); + expect(params).not.toHaveProperty("cap_speaker_session"); + }); + + it("rejects a malformed speaker session", async () => { + signIn(); + const response = await transcribe(wav(16_000), "?session=a%20b"); + expect(response.status).toBe(400); + expect(mocks.submit).not.toHaveBeenCalled(); + }); + it("drops keyterms the provider rejects", async () => { signIn(); mocks.submit diff --git a/apps/web/__tests__/unit/meetings-schemas.test.ts b/apps/web/__tests__/unit/meetings-schemas.test.ts index dcf6c685019..407238d92b1 100644 --- a/apps/web/__tests__/unit/meetings-schemas.test.ts +++ b/apps/web/__tests__/unit/meetings-schemas.test.ts @@ -196,6 +196,34 @@ describe("transcribe query", () => { expect(parseTranscribeQuery({ language: "klingon" }).ok).toBe(false); }); + it("takes a speaker session and clip number for meeting clips", () => { + expect(parseTranscribeQuery({ session: "doc_8f2:mic", seq: "12" })).toEqual( + { + ok: true, + data: { + language: "auto", + keywords: [], + speakers: { session: "doc_8f2:mic", seq: 12 }, + }, + }, + ); + expect(parseTranscribeQuery({ session: "doc_8f2:system" })).toMatchObject({ + ok: true, + data: { speakers: { session: "doc_8f2:system", seq: 0 } }, + }); + }); + + it("rejects malformed speaker sessions and clip numbers", () => { + expect(parseTranscribeQuery({ seq: "3" })).toEqual({ + ok: false, + detail: "seq: needs a session", + }); + expect(parseTranscribeQuery({ session: "a b" }).ok).toBe(false); + expect(parseTranscribeQuery({ session: "x".repeat(101) }).ok).toBe(false); + expect(parseTranscribeQuery({ session: "s", seq: "-1" }).ok).toBe(false); + expect(parseTranscribeQuery({ session: "s", seq: "1.5" }).ok).toBe(false); + }); + it("rejects keywords in the URL without echoing them", () => { const result = parseTranscribeQuery({ keywordsQuery: SECRET }); expect(result).toEqual({ diff --git a/apps/web/app/api/desktop/[...route]/meetings.ts b/apps/web/app/api/desktop/[...route]/meetings.ts index 2f5ead886ff..85216463804 100644 --- a/apps/web/app/api/desktop/[...route]/meetings.ts +++ b/apps/web/app/api/desktop/[...route]/meetings.ts @@ -210,7 +210,14 @@ type TranscribeResult = { text: string; language: string | null; durationMs: number; - words: { text: string; startMs: number; endMs: number; confidence: number }[]; + words: { + text: string; + startMs: number; + endMs: number; + confidence: number; + /** "A", "B", ... per meeting session; only when the request named one. */ + speaker?: string | null; + }[]; }; const emptyTranscription = (durationMs: number): TranscribeResult => ({ @@ -234,6 +241,8 @@ app.post("/transcribe", async (c) => { language: c.req.query("language"), keywordsQuery: c.req.query("keywords"), keywordsHeader: c.req.header(MEETING_KEYWORDS_HEADER), + session: c.req.query("session"), + seq: c.req.query("seq"), }); if (!query.ok) { log({ outcome: "invalid_request", status: 400 }); @@ -298,6 +307,7 @@ app.post("/transcribe", async (c) => { query.data, clip.durationMs, signal, + user.id, ); log({ outcome: "ok", @@ -324,9 +334,21 @@ async function transcribeClip( query: TranscribeQuery, durationMs: number, signal: AbortSignal, + userId: string, ): Promise { const client = createAssemblyAIClient(); - const options = getAssemblyAITranscriptionOptions(query.language); + // Speaker labels that stay the same across a meeting's clips: the farm keeps + // one speaker state per session, namespaced by user so sessions never mix. + const options = { + ...getAssemblyAITranscriptionOptions(query.language), + ...(query.speakers + ? { + speaker_labels: true, + cap_speaker_session: `${userId}:${query.speakers.session}`, + cap_speaker_seq: query.speakers.seq, + } + : {}), + } as ReturnType; const audio = await untilAborted( client.files.upload( Buffer.from(wav.buffer, wav.byteOffset, wav.byteLength), @@ -382,6 +404,14 @@ async function transcribeClip( startMs: word.start, endMs: word.end, confidence: word.confidence, + ...(query.speakers + ? { + speaker: + typeof word.speaker === "string" && word.speaker + ? word.speaker + : null, + } + : {}), })), }; } diff --git a/apps/web/lib/meetings/schemas.ts b/apps/web/lib/meetings/schemas.ts index 176fd3533d7..125867a86e1 100644 --- a/apps/web/lib/meetings/schemas.ts +++ b/apps/web/lib/meetings/schemas.ts @@ -150,8 +150,14 @@ export function parseMeetingBody( export type TranscribeQuery = { language: AiGenerationLanguage; keywords: string[]; + /** Speaker labels for a meeting sent as clips: one session per meeting and + * track (opaque to the server) and the clip's number in it. */ + speakers?: { session: string; seq: number }; }; +const SPEAKER_SESSION = /^[A-Za-z0-9_.:-]{1,100}$/; +const SPEAKER_SEQ_MAX = 10_000_000; + function decodeKeywordsHeader(value: string | undefined): string[] { if (!value || value.length > MEETING_KEYWORDS_HEADER_MAX_CHARS) return []; const terms = new Set(); @@ -172,6 +178,8 @@ export function parseTranscribeQuery(input: { language?: string; keywordsQuery?: string; keywordsHeader?: string; + session?: string; + seq?: string; }): ParseResult { const language = input.language ?? AI_GENERATION_LANGUAGE_AUTO; if ( @@ -198,9 +206,29 @@ export function parseTranscribeQuery(input: { detail: `keywords: each term must be at most ${MEETING_KEYWORD_MAX_CHARS} characters`, }; + if (input.session === undefined && input.seq !== undefined) + return { ok: false, detail: "seq: needs a session" }; + if (input.session === undefined) + return { + ok: true, + data: { language: language as AiGenerationLanguage, keywords }, + }; + if (!SPEAKER_SESSION.test(input.session)) + return { + ok: false, + detail: "session: 1 to 100 letters, digits, '.', '_', ':' or '-'", + }; + const seq = input.seq === undefined ? 0 : Number(input.seq); + if (!Number.isInteger(seq) || seq < 0 || seq > SPEAKER_SEQ_MAX) + return { ok: false, detail: "seq: a whole number from 0" }; + return { ok: true, - data: { language: language as AiGenerationLanguage, keywords }, + data: { + language: language as AiGenerationLanguage, + keywords, + speakers: { session: input.session, seq }, + }, }; }