Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(call): auto-assign voice to macro user (#3191)

Will Hutchinsonfc6c659fbc3e
diff --git a/rust/cloud-storage/.sqlx/query-1135cf21b6f164a59956cffb56b929369dc09dc71c131ba722d84082a8e98f85.json b/rust/cloud-storage/.sqlx/query-1135cf21b6f164a59956cffb56b929369dc09dc71c131ba722d84082a8e98f85.json
deleted file mode 100644
index d78d22d89c..0000000000
--- a/rust/cloud-storage/.sqlx/query-1135cf21b6f164a59956cffb56b929369dc09dc71c131ba722d84082a8e98f85.json
+++ /dev/null
@@ -1,28 +0,0 @@
-{
- "db_name": "PostgreSQL",
- "query": "\n WITH per_speaker AS (\n SELECT\n u.macro_user_id,\n COUNT(*) AS total_segments,\n COUNT(t.voice_id) AS voiced_segments,\n COUNT(DISTINCT t.voice_id) AS distinct_voice_ids,\n (array_agg(DISTINCT t.voice_id) FILTER (WHERE t.voice_id IS NOT NULL))[1] AS voice_id,\n MIN(t.sequence_num) AS first_sequence_num\n FROM call_record_transcripts t\n JOIN \"User\" u\n ON u.id = t.speaker_id\n AND u.macro_user_id IS NOT NULL\n WHERE t.call_record_id = $1\n GROUP BY t.speaker_id, u.macro_user_id\n )\n SELECT macro_user_id AS \"macro_user_id!\", voice_id AS \"voice_id!\"\n FROM per_speaker\n WHERE total_segments = voiced_segments\n AND distinct_voice_ids = 1\n ORDER BY first_sequence_num ASC\n ",
- "describe": {
- "columns": [
- {
- "ordinal": 0,
- "name": "macro_user_id!",
- "type_info": "Uuid"
- },
- {
- "ordinal": 1,
- "name": "voice_id!",
- "type_info": "Uuid"
- }
- ],
- "parameters": {
- "Left": [
- "Uuid"
- ]
- },
- "nullable": [
- false,
- null
- ]
- },
- "hash": "1135cf21b6f164a59956cffb56b929369dc09dc71c131ba722d84082a8e98f85"
-}
diff --git a/rust/cloud-storage/.sqlx/query-609f0f7ffb41618435bfabd82f782666220cf9bf24918ba7c7b9fce1fcd5783e.json b/rust/cloud-storage/.sqlx/query-609f0f7ffb41618435bfabd82f782666220cf9bf24918ba7c7b9fce1fcd5783e.json
new file mode 100644
index 0000000000..aceea5d3e6
--- /dev/null
+++ b/rust/cloud-storage/.sqlx/query-609f0f7ffb41618435bfabd82f782666220cf9bf24918ba7c7b9fce1fcd5783e.json
@@ -0,0 +1,28 @@
+{
+ "db_name": "PostgreSQL",
+ "query": "\n WITH per_speaker AS (\n SELECT\n u.macro_user_id,\n COUNT(*) AS total_segments,\n COUNT(t.diarized_speaker_id) AS diarized_segments,\n COUNT(DISTINCT t.diarized_speaker_id) AS distinct_diarized_speaker_ids,\n ARRAY_AGG(DISTINCT t.voice_id) FILTER (WHERE t.voice_id IS NOT NULL) AS voice_ids,\n MIN(t.sequence_num) AS first_sequence_num\n FROM call_record_transcripts t\n JOIN \"User\" u\n ON u.id = t.speaker_id\n AND u.macro_user_id IS NOT NULL\n WHERE t.call_record_id = $1\n GROUP BY t.speaker_id, u.macro_user_id\n )\n SELECT macro_user_id AS \"macro_user_id!\", voices.voice_id AS \"voice_id!\"\n FROM per_speaker\n CROSS JOIN LATERAL UNNEST(voice_ids) AS voices(voice_id)\n WHERE total_segments = diarized_segments\n AND distinct_diarized_speaker_ids = 1\n ORDER BY first_sequence_num ASC, voices.voice_id ASC\n ",
+ "describe": {
+ "columns": [
+ {
+ "ordinal": 0,
+ "name": "macro_user_id!",
+ "type_info": "Uuid"
+ },
+ {
+ "ordinal": 1,
+ "name": "voice_id!",
+ "type_info": "Uuid"
+ }
+ ],
+ "parameters": {
+ "Left": [
+ "Uuid"
+ ]
+ },
+ "nullable": [
+ false,
+ null
+ ]
+ },
+ "hash": "609f0f7ffb41618435bfabd82f782666220cf9bf24918ba7c7b9fce1fcd5783e"
+}
diff --git a/rust/cloud-storage/call/src/domain/ports.rs b/rust/cloud-storage/call/src/domain/ports.rs
index 9cfa30db37..f456aa66d6 100644
--- a/rust/cloud-storage/call/src/domain/ports.rs
+++ b/rust/cloud-storage/call/src/domain/ports.rs
@@ -268,9 +268,10 @@ pub trait CallRepository: Send + Sync + 'static {
/// Stable `(macro_user_id, voice_id)` pairs inferred from a finished
/// call's archived transcripts. A speaker is returned only when every
- /// transcript row for that `speaker_id` has a non-NULL `voice_id` and all
- /// of those voice ids are the same. The `speaker_id` is resolved through
- /// the canonical `User` row to get the `macro_user.id` used by
+ /// transcript row for that `speaker_id` has the same non-NULL
+ /// `diarized_speaker_id`; all distinct non-NULL `voice_id`s observed on
+ /// those rows are returned. The `speaker_id` is resolved through the
+ /// canonical `User` row to get the `macro_user.id` used by
/// `macro_user_voice`.
fn get_stable_speaker_voices_for_call_record(
&self,
diff --git a/rust/cloud-storage/call/src/domain/service.rs b/rust/cloud-storage/call/src/domain/service.rs
index 3efec3ab28..c6b00a38f2 100644
--- a/rust/cloud-storage/call/src/domain/service.rs
+++ b/rust/cloud-storage/call/src/domain/service.rs
@@ -1355,10 +1355,11 @@ impl<
/// Fire-and-forget spawn of finished-call voice processing.
///
/// Called from `process_webhook_event` immediately after `archive_call`
- /// finalizes the `call_records` row. First, stable speaker voice ids are
- /// enrolled for the users who spoke them; then the regular voice matcher
- /// can use both pre-existing and newly-enrolled voices to populate speaker
- /// overrides. Errors are logged inside the spawned task.
+ /// finalizes the `call_records` row. First, voice ids for consistently
+ /// diarized speakers are enrolled for the users who spoke them; then the
+ /// regular voice matcher can use both pre-existing and newly-enrolled
+ /// voices to populate speaker overrides. Errors are logged inside the
+ /// spawned task.
fn spawn_process_voices_for_call(&self, call_record_id: Uuid) {
let repo = self.repo.clone();
let voice_repo = self.voice_repo.clone();
@@ -1377,10 +1378,11 @@ impl<
/// Enroll stable speaker voice ids observed in a freshly archived call.
///
-/// For each `speaker_id` in the call transcript, the repository returns a
-/// candidate only when every transcript row for that speaker has the same
-/// non-NULL `voice_id`. Those unambiguous pairs are linked to the resolved
-/// macro user in `macro_user_voice` via [`VoiceRepository::link_user_voice`].
+/// For each `speaker_id` in the call transcript, the repository returns
+/// candidates only when every transcript row for that speaker has the same
+/// non-NULL `diarized_speaker_id`. All distinct non-NULL `voice_id`s on those
+/// rows are linked to the resolved macro user in `macro_user_voice` via
+/// [`VoiceRepository::link_user_voice`].
async fn enroll_stable_speaker_voices_for_call_record<R: CallRepository, Vr: VoiceRepository>(
repo: &R,
voice_repo: &Vr,
diff --git a/rust/cloud-storage/call/src/domain/service/test.rs b/rust/cloud-storage/call/src/domain/service/test.rs
index 2013290f11..bb123b15df 100644
--- a/rust/cloud-storage/call/src/domain/service/test.rs
+++ b/rust/cloud-storage/call/src/domain/service/test.rs
@@ -128,7 +128,7 @@ fn exclude_voip_recipients_returns_empty_when_all_users_received_voip() {
fixtures(path = "../../../fixtures", scripts("call_repo")),
migrator = "MACRO_DB_MIGRATIONS"
)]
-async fn enroll_stable_speaker_voices_links_only_unambiguous_speakers(
+async fn enroll_stable_speaker_voices_links_all_voices_for_consistent_diarized_speakers(
pool: sqlx::Pool<sqlx::Postgres>,
) -> anyhow::Result<()> {
use crate::domain::models::TranscriptSegmentRequest;
@@ -151,7 +151,7 @@ async fn enroll_stable_speaker_voices_links_only_unambiguous_speakers(
let segments = [
("stable-a-1", user_a.as_ref(), Some("spk-a0"), voice_a),
- ("stable-a-2", user_a.as_ref(), Some("spk-a1"), voice_a),
+ ("stable-a-2", user_a.as_ref(), Some("spk-a0"), voice_b),
("ambiguous-b-1", user_b.as_ref(), Some("spk-b0"), voice_a),
("ambiguous-b-2", user_b.as_ref(), Some("spk-b1"), voice_b),
];
@@ -184,10 +184,11 @@ async fn enroll_stable_speaker_voices_links_only_unambiguous_speakers(
super::enroll_stable_speaker_voices_for_call_record(&call_repo, &voice_repo, call_record_id)
.await;
- assert_eq!(
- voice_repo.get_user_voices(&MACRO_USER_A).await?,
- vec![voice_a]
- );
+ let mut user_a_voices = voice_repo.get_user_voices(&MACRO_USER_A).await?;
+ user_a_voices.sort();
+ let mut expected = vec![voice_a, voice_b];
+ expected.sort();
+ assert_eq!(user_a_voices, expected);
assert!(voice_repo.get_user_voices(&MACRO_USER_B).await?.is_empty());
Ok(())
}
diff --git a/rust/cloud-storage/call/src/outbound/pg_call_repo.rs b/rust/cloud-storage/call/src/outbound/pg_call_repo.rs
index b67c7ed2f4..96d8e37e17 100644
--- a/rust/cloud-storage/call/src/outbound/pg_call_repo.rs
+++ b/rust/cloud-storage/call/src/outbound/pg_call_repo.rs
@@ -1444,8 +1444,9 @@ impl CallRepository for PgCallRepo {
///
/// `call_record_id` scopes the scan to a single call's archived transcript rows.
/// A speaker is returned only when every row for that `speaker_id` has the
- /// same non-NULL `voice_id`; ambiguous, missing, or unresolved speakers are
- /// skipped. The returned `macro_user_id` is the user's canonical
+ /// same non-NULL `diarized_speaker_id`; all distinct non-NULL `voice_id`s
+ /// on those rows are returned. Ambiguous, missing, or unresolved speakers
+ /// are skipped. The returned `macro_user_id` is the user's canonical
/// `macro_user.id`, suitable for linking to `voice_id` in `macro_user_voice`.
#[tracing::instrument(err, skip(self))]
async fn get_stable_speaker_voices_for_call_record(
@@ -1458,9 +1459,9 @@ impl CallRepository for PgCallRepo {
SELECT
u.macro_user_id,
COUNT(*) AS total_segments,
- COUNT(t.voice_id) AS voiced_segments,
- COUNT(DISTINCT t.voice_id) AS distinct_voice_ids,
- (array_agg(DISTINCT t.voice_id) FILTER (WHERE t.voice_id IS NOT NULL))[1] AS voice_id,
+ COUNT(t.diarized_speaker_id) AS diarized_segments,
+ COUNT(DISTINCT t.diarized_speaker_id) AS distinct_diarized_speaker_ids,
+ ARRAY_AGG(DISTINCT t.voice_id) FILTER (WHERE t.voice_id IS NOT NULL) AS voice_ids,
MIN(t.sequence_num) AS first_sequence_num
FROM call_record_transcripts t
JOIN "User" u
@@ -1469,11 +1470,12 @@ impl CallRepository for PgCallRepo {
WHERE t.call_record_id = $1
GROUP BY t.speaker_id, u.macro_user_id
)
- SELECT macro_user_id AS "macro_user_id!", voice_id AS "voice_id!"
+ SELECT macro_user_id AS "macro_user_id!", voices.voice_id AS "voice_id!"
FROM per_speaker
- WHERE total_segments = voiced_segments
- AND distinct_voice_ids = 1
- ORDER BY first_sequence_num ASC
+ CROSS JOIN LATERAL UNNEST(voice_ids) AS voices(voice_id)
+ WHERE total_segments = diarized_segments
+ AND distinct_diarized_speaker_ids = 1
+ ORDER BY first_sequence_num ASC, voices.voice_id ASC
"#,
call_record_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.rs
index eec60f6068..9e5105fafe 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
@@ -1089,7 +1089,7 @@ async fn archive_call_rolls_up_consecutive_same_speaker_transcripts(
fixtures(path = "../../../fixtures", scripts("call_repo")),
migrator = "MACRO_DB_MIGRATIONS"
)]
-async fn get_stable_speaker_voices_for_call_record_returns_only_unambiguous_speakers(
+async fn get_stable_speaker_voices_for_call_record_returns_all_voices_for_consistent_diarized_speakers(
pool: Pool<Postgres>,
) -> anyhow::Result<()> {
let repo = repo(pool.clone());
@@ -1103,10 +1103,12 @@ async fn get_stable_speaker_voices_for_call_record_returns_only_unambiguous_spea
insert_user_mapping(&pool, USER_C.deref(), MACRO_USER_C).await?;
let segments = [
- // USER_A is stable: every transcript row has the same non-null voice id.
+ // USER_A is stable: every row has the same non-null diarized speaker,
+ // so all non-null voice ids from those rows should be returned.
("stable-a-1", USER_A.as_ref(), Some("spk-a0"), Some(voice_a)),
- ("stable-a-2", USER_A.as_ref(), Some("spk-a1"), Some(voice_a)),
- // USER_B is ambiguous: more than one distinct voice id.
+ ("stable-a-2", USER_A.as_ref(), Some("spk-a0"), Some(voice_b)),
+ ("stable-a-3", USER_A.as_ref(), Some("spk-a0"), None),
+ // USER_B is ambiguous: more than one distinct diarized speaker id.
(
"ambiguous-b-1",
USER_B.as_ref(),
@@ -1119,15 +1121,15 @@ async fn get_stable_speaker_voices_for_call_record_returns_only_unambiguous_spea
Some("spk-b1"),
Some(voice_b),
),
- // USER_C is incomplete: at least one transcript row has no voice id.
+ // USER_C is incomplete: at least one transcript row has no diarized speaker id.
(
"missing-c-1",
USER_C.as_ref(),
Some("spk-c0"),
Some(voice_a),
),
- ("missing-c-2", USER_C.as_ref(), Some("spk-c0"), None),
- // Unknown speaker ids are ignored even if their voice id is stable.
+ ("missing-c-2", USER_C.as_ref(), None, Some(voice_b)),
+ // Unknown speaker ids are ignored even if their diarized speaker id is stable.
(
"unknown-speaker",
@@ -1160,11 +1162,14 @@ async fn get_stable_speaker_voices_for_call_record_returns_only_unambiguous_spea
let record_id = repo.archive_call(&CALL1).await?;
- let stable = repo
+ let mut stable = repo
.get_stable_speaker_voices_for_call_record(&record_id)
.await?;
+ stable.sort();
- assert_eq!(stable, vec![(MACRO_USER_A, voice_a)]);
+ let mut expected = vec![(MACRO_USER_A, voice_a), (MACRO_USER_A, voice_b)];
+ expected.sort();
+ assert_eq!(stable, expected);
Ok(())
}