| 1 | use std::collections::HashMap; |
| 2 | use std::sync::Arc; |
| 3 | |
| 4 | use chrono::{DateTime, Local, Utc}; |
| 5 | use gpui_kit::*; |
| 6 | use mcp::{AgentHub, UiEvent}; |
| 7 | use teams_core::CoreEvent; |
| 8 | use tokio::sync::mpsc::UnboundedReceiver; |
| 9 | |
| 10 | use super::source::AppSource; |
| 11 | use super::{AgentApi, ProposalEntry, ProposalStatus}; |
| 12 | use crate::backend::Engine; |
| 13 | use crate::runtime; |
| 14 | |
| 15 | const PREVIEW_CHARACTERS: usize = 200; |
| 16 | const NEW_MESSAGE_BATCH: usize = 50; |
| 17 | const CLOCK_SKEW_ALLOWANCE: chrono::TimeDelta = chrono::TimeDelta::minutes(2); |
| 18 | |
| 19 | pub fn drain_ui_events( |
| 20 | mut receiver: UnboundedReceiver<UiEvent>, |
| 21 | api: WeakEntity<AgentApi>, |
| 22 | cx: &mut App, |
| 23 | ) { |
| 24 | cx.spawn(async move |cx| { |
| 25 | while let Some(event) = receiver.recv().await { |
| 26 | if api |
| 27 | .update(cx, |api, cx| api.apply_ui_event(event, cx)) |
| 28 | .is_err() |
| 29 | { |
| 30 | return; |
| 31 | } |
| 32 | } |
| 33 | }) |
| 34 | .detach(); |
| 35 | } |
| 36 | |
| 37 | pub fn apply(proposals: &mut HashMap<String, ProposalEntry>, event: UiEvent) { |
| 38 | match event { |
| 39 | UiEvent::ProposalChanged(proposal) => { |
| 40 | proposals.insert( |
| 41 | proposal.conversation_id.clone(), |
| 42 | ProposalEntry { |
| 43 | proposal, |
| 44 | status: ProposalStatus::Pending, |
| 45 | }, |
| 46 | ); |
| 47 | } |
| 48 | UiEvent::ProposalStale(proposal) => { |
| 49 | proposals.insert( |
| 50 | proposal.conversation_id.clone(), |
| 51 | ProposalEntry { |
| 52 | proposal, |
| 53 | status: ProposalStatus::Stale { |
| 54 | answered_at: Local::now(), |
| 55 | }, |
| 56 | }, |
| 57 | ); |
| 58 | } |
| 59 | UiEvent::ProposalRemoved { conversation_id } => { |
| 60 | proposals.remove(&conversation_id); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub fn spawn_message_bridge(engine: Arc<Engine>, hub: Arc<AgentHub>, source: Arc<AppSource>) { |
| 66 | let started_at = Utc::now() - CLOCK_SKEW_ALLOWANCE; |
| 67 | drop(runtime::spawn(async move { |
| 68 | let mut receiver = engine.subscribe(); |
| 69 | let mut watermarks: HashMap<String, DateTime<Utc>> = HashMap::new(); |
| 70 | loop { |
| 71 | let conversation_id = match receiver.recv().await { |
| 72 | Ok(CoreEvent::MessagesChanged { conversation_id }) => conversation_id, |
| 73 | Ok(_) | Err(tokio::sync::broadcast::error::RecvError::Lagged(_)) => continue, |
| 74 | Err(tokio::sync::broadcast::error::RecvError::Closed) => return, |
| 75 | }; |
| 76 | let Ok(store) = mcp::TeamsSource::store(source.as_ref()) else { |
| 77 | continue; |
| 78 | }; |
| 79 | let watermark = watermarks |
| 80 | .entry(conversation_id.clone()) |
| 81 | .or_insert(started_at); |
| 82 | let my_id = mcp::TeamsSource::me_id(source.as_ref()); |
| 83 | loop { |
| 84 | let Ok(messages) = |
| 85 | store.messages_after(&conversation_id, *watermark, NEW_MESSAGE_BATCH) |
| 86 | else { |
| 87 | break; |
| 88 | }; |
| 89 | let batch_full = messages.len() == NEW_MESSAGE_BATCH; |
| 90 | for message in messages { |
| 91 | *watermark = (*watermark).max(message.created_at); |
| 92 | if message.deleted { |
| 93 | continue; |
| 94 | } |
| 95 | let sender_is_me = my_id.is_some() && message.sender_id == my_id; |
| 96 | let preview: String = store::plain_text(&message.body_html) |
| 97 | .chars() |
| 98 | .take(PREVIEW_CHARACTERS) |
| 99 | .collect(); |
| 100 | hub.message_arrived( |
| 101 | &conversation_id, |
| 102 | &message.message_id, |
| 103 | sender_is_me, |
| 104 | message.created_at, |
| 105 | message.sender_name, |
| 106 | &preview, |
| 107 | ); |
| 108 | } |
| 109 | if !batch_full { |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | })); |
| 115 | } |