Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(call): rollup transcripts (#3176)
Will Hutchinson664a43d31d90
diff --git a/rust/cloud-storage/.sqlx/query-07feb08ae2f8494b1a9043b878ac356af9e557d3b3d67a1a9cf2f07d292883c2.json b/rust/cloud-storage/.sqlx/query-07feb08ae2f8494b1a9043b878ac356af9e557d3b3d67a1a9cf2f07d292883c2.jsonnew file mode 100644index 0000000000..d7baab4a42--- /dev/null+++ b/rust/cloud-storage/.sqlx/query-07feb08ae2f8494b1a9043b878ac356af9e557d3b3d67a1a9cf2f07d292883c2.json@@ -0,0 +1,15 @@+{+ "db_name": "PostgreSQL",+ "query": "\n WITH ordered AS (\n SELECT\n segment_id,\n speaker_id,\n diarized_speaker_id,\n voice_id,\n content,\n started_at,\n ended_at,\n sequence_num,\n LAG(speaker_id) OVER w AS prev_speaker_id,\n LAG(diarized_speaker_id) OVER w AS prev_diarized_speaker_id,\n LAG(voice_id) OVER w AS prev_voice_id,\n LAG(ended_at) OVER w AS prev_ended_at\n FROM call_transcripts\n WHERE call_id = $2\n WINDOW w AS (ORDER BY sequence_num)\n ),\n marked AS (\n SELECT\n segment_id,\n speaker_id,\n diarized_speaker_id,\n voice_id,\n content,\n started_at,\n ended_at,\n sequence_num,\n CASE\n WHEN prev_speaker_id IS NOT NULL\n AND speaker_id = prev_speaker_id\n AND diarized_speaker_id IS NOT DISTINCT FROM prev_diarized_speaker_id\n AND voice_id IS NOT DISTINCT FROM prev_voice_id\n AND prev_ended_at IS NOT NULL\n AND started_at - prev_ended_at <= INTERVAL '5 seconds'\n THEN 0\n ELSE 1\n END AS is_new_group\n FROM ordered\n ),\n grouped AS (\n SELECT\n segment_id,\n speaker_id,\n diarized_speaker_id,\n voice_id,\n content,\n started_at,\n ended_at,\n sequence_num,\n SUM(is_new_group) OVER (ORDER BY sequence_num) AS group_id\n FROM marked\n )\n INSERT INTO call_record_transcripts (call_record_id, segment_id, speaker_id, diarized_speaker_id, voice_id, content, started_at, ended_at, sequence_num)\n SELECT\n $1,\n MIN(segment_id),\n MIN(speaker_id),\n MIN(diarized_speaker_id),\n -- voice_id is UUID (no MIN); all rows in a group share the same value via IS NOT DISTINCT FROM.\n (array_agg(voice_id ORDER BY sequence_num))[1],\n STRING_AGG(content, ' ' ORDER BY sequence_num),\n MIN(started_at),\n MAX(ended_at),\n MIN(sequence_num)\n FROM grouped\n GROUP BY group_id\n ",+ "describe": {+ "columns": [],+ "parameters": {+ "Left": [+ "Uuid",+ "Uuid"+ ]+ },+ "nullable": []+ },+ "hash": "07feb08ae2f8494b1a9043b878ac356af9e557d3b3d67a1a9cf2f07d292883c2"+}diff --git a/rust/cloud-storage/.sqlx/query-3652fd4b477c2b12eab280f53ad27d442aba1aa7c6485dedb96a9e104b567491.json b/rust/cloud-storage/.sqlx/query-3652fd4b477c2b12eab280f53ad27d442aba1aa7c6485dedb96a9e104b567491.jsondeleted file mode 100644index e60170d91c..0000000000--- a/rust/cloud-storage/.sqlx/query-3652fd4b477c2b12eab280f53ad27d442aba1aa7c6485dedb96a9e104b567491.json+++ /dev/null@@ -1,15 +0,0 @@-{- "db_name": "PostgreSQL",- "query": "\n INSERT INTO call_record_transcripts (call_record_id, segment_id, speaker_id, diarized_speaker_id, content, started_at, ended_at, sequence_num, voice_id)\n SELECT $1, segment_id, speaker_id, diarized_speaker_id, content, started_at, ended_at, sequence_num, voice_id\n FROM call_transcripts\n WHERE call_id = $2\n ",- "describe": {- "columns": [],- "parameters": {- "Left": [- "Uuid",- "Uuid"- ]- },- "nullable": []- },- "hash": "3652fd4b477c2b12eab280f53ad27d442aba1aa7c6485dedb96a9e104b567491"-}diff --git a/rust/cloud-storage/call/src/outbound/pg_call_repo.rs b/rust/cloud-storage/call/src/outbound/pg_call_repo.rsindex 99ee143f54..b8ac029884 100644--- a/rust/cloud-storage/call/src/outbound/pg_call_repo.rs+++ b/rust/cloud-storage/call/src/outbound/pg_call_repo.rs@@ -569,13 +569,79 @@ impl CallRepository for PgCallRepo {.execute(tx.as_mut()).await?;- // Copy transcripts to call_record_transcripts.+ // Copy transcripts to call_record_transcripts, rolling up consecutive+ // segments that share both speaker_id and diarized_speaker_id when the+ // gap between them (next.started_at - prev.ended_at) is <= 5 seconds.+ // voice_id must also match so the propagated value is unambiguous.sqlx::query!(r#"- INSERT INTO call_record_transcripts (call_record_id, segment_id, speaker_id, diarized_speaker_id, content, started_at, ended_at, sequence_num, voice_id)- SELECT $1, segment_id, speaker_id, diarized_speaker_id, content, started_at, ended_at, sequence_num, voice_id- FROM call_transcripts- WHERE call_id = $2+ WITH ordered AS (+ SELECT+ segment_id,+ speaker_id,+ diarized_speaker_id,+ voice_id,+ content,+ started_at,+ ended_at,+ sequence_num,+ LAG(speaker_id) OVER w AS prev_speaker_id,+ LAG(diarized_speaker_id) OVER w AS prev_diarized_speaker_id,+ LAG(voice_id) OVER w AS prev_voice_id,+ LAG(ended_at) OVER w AS prev_ended_at+ FROM call_transcripts+ WHERE call_id = $2+ WINDOW w AS (ORDER BY sequence_num)+ ),+ marked AS (+ SELECT+ segment_id,+ speaker_id,+ diarized_speaker_id,+ voice_id,+ content,+ started_at,+ ended_at,+ sequence_num,+ CASE+ WHEN prev_speaker_id IS NOT NULL+ AND speaker_id = prev_speaker_id+ AND diarized_speaker_id IS NOT DISTINCT FROM prev_diarized_speaker_id+ AND voice_id IS NOT DISTINCT FROM prev_voice_id+ AND prev_ended_at IS NOT NULL+ AND started_at - prev_ended_at <= INTERVAL '5 seconds'+ THEN 0+ ELSE 1+ END AS is_new_group+ FROM ordered+ ),+ grouped AS (+ SELECT+ segment_id,+ speaker_id,+ diarized_speaker_id,+ voice_id,+ content,+ started_at,+ ended_at,+ sequence_num,+ SUM(is_new_group) OVER (ORDER BY sequence_num) AS group_id+ FROM marked+ )+ INSERT INTO call_record_transcripts (call_record_id, segment_id, speaker_id, diarized_speaker_id, voice_id, content, started_at, ended_at, sequence_num)+ SELECT+ $1,+ MIN(segment_id),+ MIN(speaker_id),+ MIN(diarized_speaker_id),+ -- voice_id is UUID (no MIN); all rows in a group share the same value via IS NOT DISTINCT FROM.+ (array_agg(voice_id ORDER BY sequence_num))[1],+ STRING_AGG(content, ' ' ORDER BY sequence_num),+ MIN(started_at),+ MAX(ended_at),+ MIN(sequence_num)+ FROM grouped+ GROUP BY group_id"#,call_id,call_id,diff --git a/rust/cloud-storage/call/src/outbound/pg_call_repo/test.rs b/rust/cloud-storage/call/src/outbound/pg_call_repo/test.rsindex 3478a01a77..73811f4767 100644--- a/rust/cloud-storage/call/src/outbound/pg_call_repo/test.rs+++ b/rust/cloud-storage/call/src/outbound/pg_call_repo/test.rs@@ -6,7 +6,7 @@ use crate::domain::models::{};use crate::domain::ports::CallRepository;use crate::outbound::pg_call_repo::PgCallRepo;-use chrono::Utc;+use chrono::{Duration, SubsecRound, Utc};use filter_ast::Expr;use item_filters::ast::{LiteralTree, call::CallLiteral};use macro_db_migrator::MACRO_DB_MIGRATIONS;@@ -806,6 +806,129 @@ async fn archive_call_copies_transcripts(pool: Pool<Postgres>) -> anyhow::ResultOk(())}+// -- archive_call rolls up consecutive same-speaker transcripts --------------++#[sqlx::test(+ fixtures(path = "../../../fixtures", scripts("call_repo")),+ migrator = "MACRO_DB_MIGRATIONS"+)]+async fn archive_call_rolls_up_consecutive_same_speaker_transcripts(+ pool: Pool<Postgres>,+) -> anyhow::Result<()> {+ let repo = repo(pool.clone());+ // Truncate to microseconds to match Postgres TIMESTAMPTZ precision,+ // otherwise the round-trip drops sub-microsecond nanoseconds and the+ // timestamp comparison below is flaky.+ let t0 = Utc::now().trunc_subsecs(6);++ // Row 1: USER_A / spk-a0, ends at t0+3s.+ // Row 2: USER_A / spk-a0, starts at t0+5s (gap=2s) -> merges with row 1.+ // Row 3: USER_A / spk-a0, starts at t0+20s (gap=12s) -> new group.+ // Row 4: USER_B / spk-b0 -> new group (different speaker).+ // Row 5: USER_B / spk-b1 -> new group (different diarized_speaker_id).+ let segs = [+ (+ "seg-1",+ USER_A.to_string(),+ Some("spk-a0"),+ "hello",+ t0,+ t0 + Duration::seconds(3),+ ),+ (+ "seg-2",+ USER_A.to_string(),+ Some("spk-a0"),+ "world",+ t0 + Duration::seconds(5),+ t0 + Duration::seconds(8),+ ),+ (+ "seg-3",+ USER_A.to_string(),+ Some("spk-a0"),+ "distant",+ t0 + Duration::seconds(20),+ t0 + Duration::seconds(22),+ ),+ (+ "seg-4",+ USER_B.to_string(),+ Some("spk-b0"),+ "hey",+ t0 + Duration::seconds(22),+ t0 + Duration::seconds(24),+ ),+ (+ "seg-5",+ USER_B.to_string(),+ Some("spk-b1"),+ "other",+ t0 + Duration::seconds(24),+ t0 + Duration::seconds(26),+ ),+ ];+ for (segment_id, speaker_id, diar, content, started_at, ended_at) in segs {+ repo.create_transcript_segment(+ &CALL1,+ &TranscriptSegmentRequest {+ segment_id: segment_id.to_string(),+ speaker_id,+ diarized_speaker_id: diar.map(str::to_string),+ content: content.to_string(),+ started_at,+ ended_at: Some(ended_at),+ is_final: true,+ stream_started_at: None,+ embedding: None,+ },+ None,+ )+ .await?;+ }++ let record_id = repo.archive_call(&CALL1).await?;++ let rows = sqlx::query!(+ r#"+ SELECT speaker_id, diarized_speaker_id, content, started_at, ended_at+ FROM call_record_transcripts+ WHERE call_record_id = $1+ ORDER BY sequence_num ASC+ "#,+ record_id,+ )+ .fetch_all(&pool)+ .await?;++ // 5 segments collapse into 4 rolled-up rows.+ assert_eq!(rows.len(), 4);++ // Group 1: seg-1 + seg-2 merged.+ assert_eq!(rows[0].speaker_id, USER_A.as_ref());+ assert_eq!(rows[0].diarized_speaker_id.as_deref(), Some("spk-a0"));+ assert_eq!(rows[0].content, "hello world");+ assert_eq!(rows[0].started_at, t0);+ assert_eq!(rows[0].ended_at, Some(t0 + Duration::seconds(8)));++ // Group 2: seg-3 alone (gap from seg-2 was 12s).+ assert_eq!(rows[1].speaker_id, USER_A.as_ref());+ assert_eq!(rows[1].diarized_speaker_id.as_deref(), Some("spk-a0"));+ assert_eq!(rows[1].content, "distant");++ // Group 3: seg-4 alone (different speaker_id).+ assert_eq!(rows[2].speaker_id, USER_B.as_ref());+ assert_eq!(rows[2].diarized_speaker_id.as_deref(), Some("spk-b0"));+ assert_eq!(rows[2].content, "hey");++ // Group 4: seg-5 alone (different diarized_speaker_id).+ assert_eq!(rows[3].speaker_id, USER_B.as_ref());+ assert_eq!(rows[3].diarized_speaker_id.as_deref(), Some("spk-b1"));+ assert_eq!(rows[3].content, "other");++ Ok(())+}+// -- get_call_record_by_call_id ----------------------------------------------#[sqlx::test(