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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
//! Live flow progress for a Slack turn (#1838, #1988).
//!
//! One `ProgressCallback` that folds tool steps, narration and notices into
//! the turn's tool-group message. The follow-up tap and the background
//! resume both drive their turn through it, so a resumed answer shows the
//! same live steps as a message the user typed.
use super::SlackState;
use super::governor::GatedWrites;
use super::handler::sync_step_group;
use super::options_host::{drain_note_tasks, post_halted_answer};
use super::tool_group::{GroupEntry, GroupState};
use crate::brain::agent::ProgressEvent;
use slack_morphism::prelude::*;
use std::sync::Arc;
use tokio::sync::Mutex;
/// Shared state a turn's progress callback writes into.
pub(crate) struct FlowProgress {
pub(crate) cb: crate::brain::agent::ProgressCallback,
pub(crate) steps: Arc<Mutex<Vec<GroupEntry>>>,
pub(crate) intermediate_handles: Arc<std::sync::Mutex<Vec<tokio::task::JoinHandle<()>>>>,
/// Answers posted before the buttons (see `options_host`).
pub(crate) posted: Arc<Mutex<Vec<String>>>,
}
/// Build the progress callback for one turn.
///
/// `thread_ts` threads the group message into the conversation the turn
/// started in; `None` posts at channel root.
pub(crate) fn build_flow_progress(
client: Arc<SlackHyperClient>,
slack_state: Arc<SlackState>,
channel: SlackChannelId,
thread_ts: Option<SlackTs>,
turn_group_ts: Arc<Mutex<Option<SlackTs>>>,
bot_token: String,
) -> FlowProgress {
let steps: Arc<Mutex<Vec<GroupEntry>>> = Arc::new(Mutex::new(Vec::new()));
// Track IntermediateText spawns so the group is fully updated before the
// answer posts (handle_message #943 ordering).
let intermediate_handles: Arc<std::sync::Mutex<Vec<tokio::task::JoinHandle<()>>>> =
Arc::new(std::sync::Mutex::new(Vec::new()));
let posted: Arc<Mutex<Vec<String>>> = Arc::new(Mutex::new(Vec::new()));
let cb: crate::brain::agent::ProgressCallback = {
let tools = steps.clone();
let group_ts = turn_group_ts.clone();
let channel = channel.clone();
let client = client.clone();
let slack_state_grp = slack_state.clone();
let thread = thread_ts.clone();
let handles = intermediate_handles.clone();
let posted = posted.clone();
Arc::new(move |session_id, event| {
let tools = tools.clone();
let group_ts = group_ts.clone();
let token = SlackApiToken::new(SlackApiTokenValue::from(bot_token.clone()));
let channel = channel.clone();
let client = client.clone();
let slack_state_grp = slack_state_grp.clone();
match event {
ProgressEvent::ToolStarted {
tool_name,
tool_input,
..
} => {
let ctx = crate::utils::tool_context_hint(&tool_name, &tool_input);
let thread = thread.clone();
tokio::spawn(async move {
let session = client.open_session(&token);
let entries = {
let mut t = tools.lock().await;
t.push(GroupEntry::Tool {
name: tool_name,
context: ctx,
status: None,
task_id: None,
});
t.clone()
};
sync_step_group(
&session,
&slack_state_grp,
channel,
thread.clone(),
&group_ts,
entries,
)
.await;
});
}
ProgressEvent::ToolCompleted {
tool_name, success, ..
} => {
tokio::spawn(async move {
let session = client.open_session(&token);
let entries = {
let mut t = tools.lock().await;
if let Some(GroupEntry::Tool { status, .. }) =
t.iter_mut().rev().find(|e| {
matches!(
e,
GroupEntry::Tool { name, status: None, .. }
if *name == tool_name
)
})
{
*status = Some(success);
}
t.clone()
};
if let Some(ts) = group_ts.lock().await.as_ref() {
let group = slack_state_grp
.upsert_tool_group(
ts.to_string(),
GroupState::new(channel.clone(), entries),
)
.await;
let content = super::tool_group::render(&group, ts);
let upd = SlackApiChatUpdateRequest::new(channel, content, ts.clone());
if let Some(Err(e)) = session.update_chrome(&upd).await {
tracing::warn!(
"Slack tap: chat_update failed (tool group status, ts={ts}): {e}"
);
}
}
});
}
ProgressEvent::IntermediateText { text, .. } => {
// Same cleaning as handle_message's fold path: artifacts
// and media markers must not leak into the note.
let text = crate::utils::sanitize::sanitize_outbound(&text);
let (text_clean, _img) = crate::utils::extract_img_markers(&text);
let (text_clean, _vid) = crate::utils::extract_vid_markers(&text_clean);
if text_clean.trim().is_empty() {
return;
}
let thread = thread.clone();
let handle = tokio::spawn(async move {
let session = client.open_session(&token);
let text_fmt = crate::utils::slack_fmt::markdown_to_mrkdwn(&text_clean);
let entries = {
let mut t = tools.lock().await;
t.push(GroupEntry::Note(text_fmt));
t.clone()
};
sync_step_group(
&session,
&slack_state_grp,
channel,
thread.clone(),
&group_ts,
entries,
)
.await;
});
if let Ok(mut g) = handles.lock() {
g.push(handle);
}
}
ProgressEvent::DetachedTaskStarted { task_id, label } => {
// One row, updated in place (#2149). The start posts the
// block when the turn has none yet; the finish only ever
// rewrites, so a long task cannot leave two bubbles.
let thread = thread.clone();
tokio::spawn(async move {
let session = client.open_session(&token);
apply_detached(
&session,
&slack_state_grp,
channel,
thread,
&group_ts,
&tools,
super::tool_group::DetachedEvent::Started {
task_id: &task_id,
label: &label,
},
)
.await;
});
}
ProgressEvent::DetachedTaskFinished {
task_id, success, ..
} => {
let thread = thread.clone();
tokio::spawn(async move {
let session = client.open_session(&token);
apply_detached(
&session,
&slack_state_grp,
channel,
thread,
&group_ts,
&tools,
super::tool_group::DetachedEvent::Finished {
task_id: &task_id,
success,
},
)
.await;
});
}
ProgressEvent::SelfHealingAlert { message } => {
tokio::spawn(async move {
let session = client.open_session(&token);
let text =
format!("🔧 {}", crate::utils::sanitize::normalize_dashes(&message));
let req = SlackApiChatPostMessageRequest::new(
channel,
SlackMessageContent::new().with_text(text),
);
if let Err(e) = session.post(&req).await {
tracing::warn!(error = %e, "Slack tap: self-healing alert post failed");
}
});
}
ProgressEvent::RetryAttempt {
attempt,
max,
reason,
} => {
tokio::spawn(async move {
let session = client.open_session(&token);
let text = format!("⏳ Retry {attempt}/{max}: {reason}");
let req = SlackApiChatPostMessageRequest::new(
channel,
SlackMessageContent::new().with_text(text),
);
if let Err(e) = session.post(&req).await {
tracing::warn!(error = %e, "Slack tap: retry notice post failed");
}
});
}
ProgressEvent::ProviderSwitched {
to_name, to_model, ..
} => {
tokio::spawn(async move {
let session = client.open_session(&token);
let text = format!("🔄 Now using {to_name}/{to_model}");
let req = SlackApiChatPostMessageRequest::new(
channel,
SlackMessageContent::new().with_text(text),
);
if let Err(e) = session.post(&req).await {
tracing::warn!(error = %e, "Slack tap: provider switch post failed");
}
});
}
// Chained suggestions (#599): buttons under the tap's answer
// resolve their channel from the session route.
ProgressEvent::SuggestedOptions(options) => {
let raw_options: Vec<String> =
options.into_iter().map(|item| item.label).collect();
let handles = handles.clone();
let posted = posted.clone();
let thread = thread.clone();
tokio::spawn(async move {
// The answer the surface halted on posts before the
// buttons, out of the group (#2029).
drain_note_tasks(&handles).await;
let session = client.open_session(&token);
post_halted_answer(
&session,
&slack_state_grp,
channel,
thread,
&group_ts,
&tools,
&posted,
)
.await;
super::suggest_options::render_suggestions(
&slack_state_grp,
session_id,
raw_options,
)
.await;
});
}
_ => {}
}
})
};
FlowProgress {
cb,
steps,
intermediate_handles,
posted,
}
}
/// Apply one detached-task event to the turn's group message (#2149).
///
/// The decision comes from the pure [`super::tool_group::detached_render`], so
/// the "one message per task" contract is the same one the tests drive without
/// a bot. This function only performs what that decision says.
async fn apply_detached<'a>(
session: &SlackClientSession<'a, slack_morphism::hyper_tokio::SlackClientHyperHttpsConnector>,
slack_state: &Arc<SlackState>,
channel: SlackChannelId,
thread_ts: Option<SlackTs>,
group_ts: &Arc<Mutex<Option<SlackTs>>>,
tools: &Mutex<Vec<GroupEntry>>,
event: super::tool_group::DetachedEvent<'_>,
) {
let (action, entries) = {
let mut t = tools.lock().await;
let ts = group_ts.lock().await.as_ref().map(|ts| ts.to_string());
let action = super::tool_group::detached_render(&mut t, ts.as_deref(), event);
(action, t.clone())
};
match action {
super::tool_group::DetachedRender::Post => {
sync_step_group(session, slack_state, channel, thread_ts, group_ts, entries).await;
}
super::tool_group::DetachedRender::Edit(ts) => {
let ts = SlackTs::new(ts);
let group = slack_state
.upsert_tool_group(ts.to_string(), GroupState::new(channel.clone(), entries))
.await;
let content = super::tool_group::render(&group, &ts);
let upd = SlackApiChatUpdateRequest::new(channel, content, ts.clone());
if let Some(Err(e)) = session.update_chrome(&upd).await {
tracing::warn!("Slack: chat_update failed (detached row, ts={ts}): {e}");
}
}
super::tool_group::DetachedRender::Skip => {}
}
}