forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_tasks.rs
More file actions
159 lines (149 loc) · 4.67 KB
/
background_tasks.rs
File metadata and controls
159 lines (149 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use super::{
SessionAgents, SessionInterruptQueues, SwarmMember, fanout_session_event,
queue_soft_interrupt_for_session, session_event_fanout_sender,
};
use crate::message::{
format_background_task_notification_markdown, format_background_task_progress_markdown,
};
use crate::protocol::{NotificationType, ServerEvent};
use jcode_agent_runtime::SoftInterruptSource;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
async fn run_background_task_message_in_live_session_if_idle(
session_id: &str,
message: &str,
sessions: &SessionAgents,
swarm_members: &Arc<RwLock<HashMap<String, SwarmMember>>>,
) -> bool {
let agent = {
let guard = sessions.read().await;
guard.get(session_id).cloned()
};
let Some(agent) = agent else {
return false;
};
let has_live_attachments = {
let members = swarm_members.read().await;
members
.get(session_id)
.map(|member| !member.event_txs.is_empty() || !member.event_tx.is_closed())
.unwrap_or(false)
};
if !has_live_attachments {
return false;
}
let is_idle = match agent.try_lock() {
Ok(guard) => {
drop(guard);
true
}
Err(_) => false,
};
if !is_idle {
return false;
}
let session_id = session_id.to_string();
let message = message.to_string();
let event_tx = session_event_fanout_sender(session_id.clone(), Arc::clone(swarm_members));
tokio::spawn(async move {
if let Err(err) = super::client_lifecycle::process_message_streaming_mpsc(
agent,
&message,
vec![],
Some(
"A background task for this session just finished. Review the completion message and continue if useful."
.to_string(),
),
event_tx,
)
.await
{
crate::logging::error(&format!(
"Failed to run background task completion immediately for live session {}: {}",
session_id, err
));
}
});
true
}
pub(super) async fn dispatch_background_task_completion(
task: &crate::bus::BackgroundTaskCompleted,
sessions: &SessionAgents,
soft_interrupt_queues: &SessionInterruptQueues,
swarm_members: &Arc<RwLock<HashMap<String, SwarmMember>>>,
) {
let notification = format_background_task_notification_markdown(task);
if task.notify
&& fanout_session_event(
swarm_members,
&task.session_id,
ServerEvent::Notification {
from_session: "background_task".to_string(),
from_name: Some("background task".to_string()),
notification_type: NotificationType::Message {
scope: Some("background_task".to_string()),
channel: None,
},
message: notification.clone(),
},
)
.await
== 0
{
crate::logging::warn(&format!(
"Failed to notify attached clients for background task completion on session {}",
task.session_id
));
}
if task.wake
&& !run_background_task_message_in_live_session_if_idle(
&task.session_id,
¬ification,
sessions,
swarm_members,
)
.await
&& !queue_soft_interrupt_for_session(
&task.session_id,
notification.clone(),
false,
SoftInterruptSource::BackgroundTask,
soft_interrupt_queues,
sessions,
)
.await
{
crate::logging::warn(&format!(
"Failed to deliver background task completion to session {}",
task.session_id
));
}
}
pub(super) async fn dispatch_background_task_progress(
task: &crate::bus::BackgroundTaskProgressEvent,
swarm_members: &Arc<RwLock<HashMap<String, SwarmMember>>>,
) {
let notification = format_background_task_progress_markdown(task);
if fanout_session_event(
swarm_members,
&task.session_id,
ServerEvent::Notification {
from_session: "background_task".to_string(),
from_name: Some("background task".to_string()),
notification_type: NotificationType::Message {
scope: Some("background_task".to_string()),
channel: None,
},
message: notification,
},
)
.await
== 0
{
crate::logging::warn(&format!(
"Failed to notify attached clients for background task progress on session {}",
task.session_id
));
}
}