Skip to content
Merged
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
61 changes: 61 additions & 0 deletions apps/web/__tests__/unit/meetings-route.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>;
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<string, unknown>;
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
Expand Down
28 changes: 28 additions & 0 deletions apps/web/__tests__/unit/meetings-schemas.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
34 changes: 32 additions & 2 deletions apps/web/app/api/desktop/[...route]/meetings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => ({
Expand All @@ -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 });
Expand Down Expand Up @@ -298,6 +307,7 @@ app.post("/transcribe", async (c) => {
query.data,
clip.durationMs,
signal,
user.id,
);
log({
outcome: "ok",
Expand All @@ -324,9 +334,21 @@ async function transcribeClip(
query: TranscribeQuery,
durationMs: number,
signal: AbortSignal,
userId: string,
): Promise<TranscribeResult> {
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<typeof getAssemblyAITranscriptionOptions>;
const audio = await untilAborted(
client.files.upload(
Buffer.from(wav.buffer, wav.byteOffset, wav.byteLength),
Expand Down Expand Up @@ -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,
}
: {}),
})),
};
}
Expand Down
30 changes: 29 additions & 1 deletion apps/web/lib/meetings/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,14 @@ export function parseMeetingBody<T>(
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<string>();
Expand All @@ -172,6 +178,8 @@ export function parseTranscribeQuery(input: {
language?: string;
keywordsQuery?: string;
keywordsHeader?: string;
session?: string;
seq?: string;
}): ParseResult<TranscribeQuery> {
const language = input.language ?? AI_GENERATION_LANGUAGE_AUTO;
if (
Expand All @@ -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 },
},
};
}

Expand Down
Loading