autoagents_core/agent/executor/
event_helper.rs1use 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
14pub struct EventHelper;
16
17impl EventHelper {
18 pub async fn send(tx: &Option<mpsc::Sender<Event>>, event: Event) {
20 if let Some(tx) = tx {
21 #[cfg(not(target_arch = "wasm32"))]
22 let _ = tx.send(event).await;
23 }
24 }
25
26 pub async fn send_task_started(
28 tx: &Option<mpsc::Sender<Event>>,
29 sub_id: SubmissionId,
30 actor_id: ActorID,
31 task_description: String,
32 ) {
33 Self::send(
34 tx,
35 Event::TaskStarted {
36 sub_id,
37 actor_id,
38 task_description,
39 },
40 )
41 .await;
42 }
43
44 pub async fn send_task_completed(
46 tx: &Option<mpsc::Sender<Event>>,
47 sub_id: SubmissionId,
48 result: String,
49 ) {
50 Self::send(tx, Event::TaskComplete { sub_id, result }).await;
51 }
52
53 pub async fn send_turn_started(
55 tx: &Option<mpsc::Sender<Event>>,
56 turn_number: usize,
57 max_turns: usize,
58 ) {
59 Self::send(
60 tx,
61 Event::TurnStarted {
62 turn_number,
63 max_turns,
64 },
65 )
66 .await;
67 }
68
69 pub async fn send_turn_completed(
71 tx: &Option<mpsc::Sender<Event>>,
72 turn_number: usize,
73 final_turn: bool,
74 ) {
75 Self::send(
76 tx,
77 Event::TurnCompleted {
78 turn_number,
79 final_turn,
80 },
81 )
82 .await;
83 }
84
85 pub async fn send_stream_chunk(
87 tx: &Option<mpsc::Sender<Event>>,
88 sub_id: SubmissionId,
89 chunk: StreamChoice,
90 ) {
91 Self::send(tx, Event::StreamChunk { sub_id, chunk }).await;
92 }
93
94 pub async fn send_stream_tool_call(
96 tx: &Option<mpsc::Sender<Event>>,
97 sub_id: SubmissionId,
98 tool_call: Value,
99 ) {
100 Self::send(tx, Event::StreamToolCall { sub_id, tool_call }).await;
101 }
102
103 pub async fn send_stream_complete(tx: &Option<mpsc::Sender<Event>>, sub_id: SubmissionId) {
105 Self::send(tx, Event::StreamComplete { sub_id }).await;
106 }
107}