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.

fix(calls): livekit agent webhook identity (#6291)

Will Hutchinson95f3e9fac6df
diff --git a/Cargo.lock b/Cargo.lock
index c61b0648ed..fc2081278c 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3123,6 +3123,7 @@ dependencies = [
"async-trait",
"aws-sdk-s3",
"axum",
+ "base64 0.22.1",
"channels",
"chrono",
"cloudfront_sign",
@@ -3153,6 +3154,7 @@ dependencies = [
"schemars 1.2.1",
"serde",
"serde_json",
+ "sha2 0.10.9",
"sqlx",
"subtle",
"thiserror 2.0.18",
diff --git a/crates/call/Cargo.toml b/crates/call/Cargo.toml
index 0b74b2d546..1d6cfe12b6 100644
--- a/crates/call/Cargo.toml
+++ b/crates/call/Cargo.toml
@@ -90,7 +90,9 @@ workspace-hack = { version = "0.1", path = "../workspace-hack" }
subtle = { workspace = true }
[dev-dependencies]
+base64 = { workspace = true }
macro_db_migrator = { path = "../macro_db_migrator" }
mockall = { workspace = true }
+sha2 = { workspace = true }
sqlx = { workspace = true }
tokio = { workspace = true }
diff --git a/crates/call/src/outbound/livekit_rtc_client.rs b/crates/call/src/outbound/livekit_rtc_client.rs
index bccb72f227..f505d556ee 100644
--- a/crates/call/src/outbound/livekit_rtc_client.rs
+++ b/crates/call/src/outbound/livekit_rtc_client.rs
@@ -314,20 +314,18 @@ impl CallRtcClient for LivekitRtcClient {
event: event.event,
id: event.id,
room_name: event.room.map(|r| r.name),
- participant_identity: event
- .participant
- .and_then(|p| {
- // The transcription agent joins with its agent name as the
- // identity, which is not a MacroUserId — short-circuit so
- // join/leave events for the agent don't fail parsing.
- if Some(p.identity.as_str()) == self.transcription_agent_name.as_deref() {
+ participant_identity: event.participant.and_then(|p| {
+ match MacroUserIdStr::parse_from_str(&p.identity) {
+ Ok(id) => Some(id.into_owned()),
+ Err(_) => {
+ tracing::debug!(
+ identity = %p.identity,
+ "skipping non-user LiveKit participant identity"
+ );
None
- } else {
- Some(MacroUserIdStr::parse_from_str(&p.identity).map(CowLike::into_owned))
}
- })
- .transpose()
- .map_err(anyhow::Error::from)?,
+ }
+ }),
egress_id,
file_url,
created_at: event.created_at,
diff --git a/crates/call/src/outbound/livekit_rtc_client/test.rs b/crates/call/src/outbound/livekit_rtc_client/test.rs
index d5aa13bb60..793bf88167 100644
--- a/crates/call/src/outbound/livekit_rtc_client/test.rs
+++ b/crates/call/src/outbound/livekit_rtc_client/test.rs
@@ -1,9 +1,16 @@
+use base64::Engine;
+use livekit_api::access_token::AccessToken;
use livekit_api::services::{ServiceError, TwirpError, TwirpErrorCode};
use macro_user_id::user_id::MacroUserIdStr;
+use sha2::{Digest, Sha256};
use super::*;
+use crate::domain::models::CallError;
use crate::domain::ports::CallRtcClient as _;
+const API_KEY: &str = "test-api-key";
+const API_SECRET: &str = "test-api-secret-test-api-secret-test";
+
fn twirp(code: &str, msg: &str) -> ServiceError {
ServiceError::Twirp(TwirpError::Twirp(TwirpErrorCode {
code: code.to_string(),
@@ -12,14 +19,47 @@ fn twirp(code: &str, msg: &str) -> ServiceError {
}
fn client() -> LivekitRtcClient {
+ LivekitRtcClient::new("wss://lk.example", API_KEY, API_SECRET, None)
+}
+
+fn client_with_transcription_agent(agent_name: &str) -> LivekitRtcClient {
LivekitRtcClient::new(
"wss://lk.example",
- "test-api-key",
- "test-api-secret-test-api-secret-test",
- None,
+ API_KEY,
+ API_SECRET,
+ Some(agent_name.to_owned()),
)
}
+fn sign_webhook(body: &str) -> String {
+ let digest = Sha256::digest(body.as_bytes());
+ let sha256 = base64::engine::general_purpose::STANDARD.encode(digest);
+ AccessToken::with_api_key(API_KEY, API_SECRET)
+ .with_sha256(&sha256)
+ .to_jwt()
+ .expect("webhook jwt")
+}
+
+fn participant_joined_body(identity: &str) -> String {
+ serde_json::json!({
+ "event": "participant_joined",
+ "id": "EV_test",
+ "createdAt": 1,
+ "room": { "name": "room-1" },
+ "participant": { "identity": identity }
+ })
+ .to_string()
+}
+
+fn receive_participant_joined(
+ client: &LivekitRtcClient,
+ identity: &str,
+) -> Result<crate::domain::models::CallWebhookEvent, CallError> {
+ let body = participant_joined_body(identity);
+ let token = sign_webhook(&body);
+ client.receive_webhook(&body, &token)
+}
+
#[tokio::test]
async fn verify_access_token_round_trips_identity_and_room() {
let client = client();
@@ -79,3 +119,41 @@ fn remove_participant_unavailable_still_fails() {
.to_string();
assert_eq!(message, "twirp error: twirp error: unavailable: overloaded");
}
+
+#[test]
+fn receive_webhook_accepts_livekit_agent_participant_identity() {
+ let event = receive_participant_joined(
+ &client_with_transcription_agent("macro-transcriber"),
+ "agent-AJ_CNFob6rwkHxK",
+ )
+ .expect("agent join must not fail webhook ingest");
+
+ assert_eq!(event.event, "participant_joined");
+ assert_eq!(event.room_name.as_deref(), Some("room-1"));
+ assert_eq!(event.participant_identity, None);
+}
+
+#[test]
+fn receive_webhook_parses_macro_user_participant_identity() {
+ let event = receive_participant_joined(&client(), "macro|[email protected]")
+ .expect("user join must parse identity");
+
+ assert_eq!(
+ event
+ .participant_identity
+ .as_ref()
+ .map(|identity| identity.as_ref()),
+ Some("macro|[email protected]")
+ );
+}
+
+#[test]
+fn receive_webhook_skips_configured_transcription_agent_name() {
+ let event = receive_participant_joined(
+ &client_with_transcription_agent("macro-transcriber"),
+ "macro-transcriber",
+ )
+ .expect("configured agent name must not fail webhook ingest");
+
+ assert_eq!(event.participant_identity, None);
+}