autoagents_core/agent/executor/
event_helper.rs

1use crate::protocol::{ActorID, Event, SubmissionId};
2use autoagents_llm::chat::StreamChoice;
3use serde_json::Value;
4
5#[cfg(not(target_arch = "wasm32"))]
6use tokio::sync::mpsc;
7
8#[cfg(target_arch = "wasm32")]
9use futures::channel::mpsc;
10
11#[cfg(target_arch = "wasm32")]
12use futures::SinkExt;
13
14/// Helper for managing event emissions
15pub struct EventHelper;
16
17impl EventHelper {
18    /// Send an event if sender is available
19    pub async fn send(tx: &Option<mpsc::Sender<Event>>, event: Event) {
20        if let Some(tx) = tx {
21            #[cfg(not(target_arch = "wasm32"))]
22            //TODO: WASM Targets currently does not support event handling
23            let _ = tx.send(event).await;
24        }
25    }
26
27    /// Send task started event
28    pub async fn send_task_started(
29        tx: &Option<mpsc::Sender<Event>>,
30        sub_id: SubmissionId,
31        actor_id: ActorID,
32        actor_name: String,
33        task_description: String,
34    ) {
35        Self::send(
36            tx,
37            Event::TaskStarted {
38                sub_id,
39                actor_id,
40                actor_name,
41                task_description,
42            },
43        )
44        .await;
45    }
46
47    /// Send task started event
48    pub async fn send_task_completed(
49        tx: &Option<mpsc::Sender<Event>>,
50        sub_id: SubmissionId,
51        actor_id: ActorID,
52        actor_name: String,
53        result: String,
54    ) {
55        Self::send(
56            tx,
57            Event::TaskComplete {
58                sub_id,
59                result,
60                actor_id,
61                actor_name,
62            },
63        )
64        .await;
65    }
66
67    /// Send turn started event
68    pub async fn send_turn_started(
69        tx: &Option<mpsc::Sender<Event>>,
70        turn_number: usize,
71        max_turns: usize,
72    ) {
73        Self::send(
74            tx,
75            Event::TurnStarted {
76                turn_number,
77                max_turns,
78            },
79        )
80        .await;
81    }
82
83    /// Send turn completed event
84    pub async fn send_turn_completed(
85        tx: &Option<mpsc::Sender<Event>>,
86        turn_number: usize,
87        final_turn: bool,
88    ) {
89        Self::send(
90            tx,
91            Event::TurnCompleted {
92                turn_number,
93                final_turn,
94            },
95        )
96        .await;
97    }
98
99    /// Send stream chunk event
100    pub async fn send_stream_chunk(
101        tx: &Option<mpsc::Sender<Event>>,
102        sub_id: SubmissionId,
103        chunk: StreamChoice,
104    ) {
105        Self::send(tx, Event::StreamChunk { sub_id, chunk }).await;
106    }
107
108    /// Send stream tool call event
109    pub async fn send_stream_tool_call(
110        tx: &Option<mpsc::Sender<Event>>,
111        sub_id: SubmissionId,
112        tool_call: Value,
113    ) {
114        Self::send(tx, Event::StreamToolCall { sub_id, tool_call }).await;
115    }
116
117    /// Send stream complete event
118    pub async fn send_stream_complete(tx: &Option<mpsc::Sender<Event>>, sub_id: SubmissionId) {
119        Self::send(tx, Event::StreamComplete { sub_id }).await;
120    }
121}