-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoneshot.rs
More file actions
380 lines (348 loc) · 12.4 KB
/
Copy pathoneshot.rs
File metadata and controls
380 lines (348 loc) · 12.4 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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! Headless one-shot channel used by embedding hosts such as ALTAI CLI.
//!
//! The channel injects a single inbound prompt, captures the final outbound
//! assistant message, and either resumes approvals on the controlling TTY
//! (`/dev/tty`) or rejects them so non-TTY callers never hang or silently
//! approve.
use std::any::Any;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use async_trait::async_trait;
use tokio::sync::{mpsc, oneshot, watch};
use crate::bus::{BusMessage, InboundMessage, OutboundMessage, RunLifecycleEvent, RunOutcome};
use crate::channels::tty_prompt::{prompt_on_tty, tty_available};
use crate::channels::Channel;
use crate::utils::ContentPart;
pub const ONESHOT_CHANNEL_NAME: &str = "altai-cli";
/// Terminal result of a one-shot host run.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OneshotResult {
pub chat_id: String,
pub run_id: Option<String>,
pub outcome: OneshotOutcome,
pub final_text: Option<String>,
}
/// Why a one-shot run stopped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OneshotOutcome {
Completed,
Failed(String),
Cancelled,
TimedOut,
ApprovalRequired { detail: String },
ClarificationRequired { detail: String },
}
impl OneshotOutcome {
pub fn from_run_outcome(outcome: &RunOutcome) -> Self {
match outcome {
RunOutcome::Completed => Self::Completed,
RunOutcome::Cancelled => Self::Cancelled,
RunOutcome::Failed { failure, .. } => Self::Failed(format!("{failure:?}")),
RunOutcome::Stuck { reason } => Self::Failed(format!("stuck:{reason:?}")),
RunOutcome::BudgetExhausted { budget } => {
Self::Failed(format!("budget_exhausted:{budget:?}"))
}
}
}
}
#[derive(Debug, Default)]
struct OneshotState {
final_text: Option<String>,
run_id: Option<String>,
finished: bool,
}
/// Captures one-shot progress and completes when the run terminates.
///
/// Interactive approvals/clarifications resume via `/dev/tty` when available;
/// otherwise the run exits with an approval/clarification outcome.
pub struct OneshotChannel {
chat_id: String,
prompt: String,
attachments: Vec<ContentPart>,
files: Vec<PathBuf>,
state: Arc<Mutex<OneshotState>>,
result_tx: Mutex<Option<oneshot::Sender<OneshotResult>>>,
shutdown_tx: mpsc::UnboundedSender<()>,
observe_tx: Option<mpsc::UnboundedSender<BusMessage>>,
bus_tx: Mutex<Option<mpsc::Sender<BusMessage>>>,
started: watch::Sender<bool>,
}
impl OneshotChannel {
pub fn new(
chat_id: String,
prompt: String,
files: Vec<PathBuf>,
attachments: Vec<ContentPart>,
result_tx: oneshot::Sender<OneshotResult>,
shutdown_tx: mpsc::UnboundedSender<()>,
observe_tx: Option<mpsc::UnboundedSender<BusMessage>>,
) -> Self {
let (started, _) = watch::channel(false);
Self {
chat_id,
prompt,
attachments,
files,
state: Arc::new(Mutex::new(OneshotState::default())),
result_tx: Mutex::new(Some(result_tx)),
shutdown_tx,
observe_tx,
bus_tx: Mutex::new(None),
started,
}
}
fn observe(&self, msg: BusMessage) {
if let Some(tx) = &self.observe_tx {
let _ = tx.send(msg);
}
}
fn complete(&self, outcome: OneshotOutcome) {
let mut state = self.state.lock().expect("oneshot state");
if state.finished {
return;
}
state.finished = true;
let result = OneshotResult {
chat_id: self.chat_id.clone(),
run_id: state.run_id.clone(),
outcome,
final_text: state.final_text.clone(),
};
drop(state);
if let Some(tx) = self.result_tx.lock().expect("oneshot result").take() {
let _ = tx.send(result);
}
let _ = self.shutdown_tx.send(());
}
fn normalize_tty_reply(raw: &str) -> String {
let trimmed = raw.trim();
if trimmed.chars().count() == 1 {
if let Some(mapped) =
crate::channels::terminal_ui::approval_hotkey_reply(trimmed.chars().next().unwrap())
{
return mapped.to_string();
}
}
trimmed.to_string()
}
async fn resume_approval_on_tty(&self, detail: &str) -> Result<bool, String> {
let bus_tx = self
.bus_tx
.lock()
.expect("oneshot bus")
.clone()
.ok_or_else(|| "oneshot bus is not ready for approval resume".to_string())?;
let prompt = format!(
"\n[altai] Approval required\n{detail}\nChoices: approve / deny / always / abort (y/n/a/x)\n> "
);
let reply = tokio::task::spawn_blocking(move || prompt_on_tty(&prompt))
.await
.map_err(|error| format!("tty prompt join failed: {error}"))?
.map_err(|error| format!("tty prompt failed: {error}"))?;
let reply = Self::normalize_tty_reply(&reply);
if reply.eq_ignore_ascii_case("abort")
|| reply.eq_ignore_ascii_case("cancel")
|| reply.eq_ignore_ascii_case("quit")
{
self.complete(OneshotOutcome::Cancelled);
return Ok(true);
}
let inbound = InboundMessage {
channel: ONESHOT_CHANNEL_NAME.to_string(),
sender_id: "altai-cli".to_string(),
chat_id: self.chat_id.clone(),
thread_id: None,
content: reply,
attachments: Vec::new(),
metadata: HashMap::new(),
};
self.observe(BusMessage::Inbound(inbound.clone()));
bus_tx
.send(BusMessage::Inbound(inbound))
.await
.map_err(|error| format!("failed to enqueue tty approval reply: {error}"))?;
Ok(true)
}
/// Observe host bus traffic that is not delivered through [`Channel::send`].
pub fn observe_bus_message(&self, msg: &BusMessage) {
self.observe(msg.clone());
match msg {
BusMessage::RunLifecycle(RunLifecycleEvent::Started { run_id, chat_id })
if chat_id == &self.chat_id =>
{
let mut state = self.state.lock().expect("oneshot state");
state.run_id = Some(run_id.clone());
}
BusMessage::RunLifecycle(RunLifecycleEvent::Terminated {
chat_id,
run_id,
outcome,
}) if chat_id == &self.chat_id =>
{
{
let mut state = self.state.lock().expect("oneshot state");
state.run_id = Some(run_id.clone());
}
self.complete(OneshotOutcome::from_run_outcome(outcome));
}
BusMessage::Telemetry(crate::bus::TelemetryEvent::ShellPolicyDecision {
chat_id,
decision,
command_preview,
..
}) if chat_id == &self.chat_id && decision == "approval_requested" =>
{
// When a controlling TTY is available, wait for the clarification
// outbound (handled in `send`) so the user can approve interactively.
if tty_available() {
return;
}
self.complete(OneshotOutcome::ApprovalRequired {
detail: command_preview.clone(),
});
}
BusMessage::Outbound(out)
if out.channel == ONESHOT_CHANNEL_NAME && out.chat_id == self.chat_id =>
{
// Captured via Channel::send as well; keep observe path consistent.
}
_ => {}
}
}
fn attachment_note(files: &[PathBuf]) -> String {
if files.is_empty() {
return String::new();
}
let list = files
.iter()
.map(|path| path.display().to_string())
.collect::<Vec<_>>()
.join(", ");
format!("\n\n[Attached files: {list}]")
}
}
#[async_trait]
impl Channel for OneshotChannel {
fn name(&self) -> &str {
ONESHOT_CHANNEL_NAME
}
async fn start(&self, bus_tx: mpsc::Sender<BusMessage>) -> Result<(), String> {
*self.bus_tx.lock().expect("oneshot bus") = Some(bus_tx.clone());
let mut content = self.prompt.clone();
// Prefer real multimodal attachments; only keep a path note when loading failed.
if self.attachments.is_empty() {
content.push_str(&Self::attachment_note(&self.files));
}
let inbound = InboundMessage {
channel: ONESHOT_CHANNEL_NAME.to_string(),
sender_id: "altai-cli".to_string(),
chat_id: self.chat_id.clone(),
thread_id: None,
content,
attachments: self.attachments.clone(),
metadata: HashMap::new(),
};
self.observe(BusMessage::Inbound(inbound.clone()));
bus_tx
.send(BusMessage::Inbound(inbound))
.await
.map_err(|error| format!("failed to enqueue oneshot prompt: {error}"))?;
let _ = self.started.send(true);
Ok(())
}
async fn stop(&self) -> Result<(), String> {
Ok(())
}
async fn send(&self, msg: OutboundMessage) -> Result<(), String> {
self.observe(BusMessage::Outbound(msg.clone()));
if msg
.metadata
.get(crate::clarification::METADATA_CLARIFICATION)
.and_then(|value| value.as_bool())
== Some(true)
|| msg
.metadata
.get(crate::bus::METADATA_CLARIFICATION_TICKET_ID)
.is_some()
{
let detail = if let Some(edit) = msg.metadata.get("edit_diff") {
let file = edit
.get("file")
.and_then(|v| v.as_str())
.unwrap_or("(unknown)");
let truncated = edit
.get("truncated")
.and_then(|v| v.as_bool())
.unwrap_or(false);
let diff = edit.get("diff").and_then(|v| v.as_str()).unwrap_or("");
let mut detail = format!("edit approval required for {file}");
if truncated {
detail.push_str(" [diff truncated]");
}
if !diff.is_empty() {
detail.push('\n');
detail.push_str(diff);
} else if !msg.content.is_empty() {
detail.push('\n');
detail.push_str(&msg.content);
}
detail
} else {
msg.content.clone()
};
if tty_available() {
match self.resume_approval_on_tty(&detail).await {
Ok(true) => return Ok(()),
Ok(false) => {}
Err(error) => {
eprintln!("altai-cli: tty approval resume failed: {error}");
}
}
}
if msg.metadata.get("edit_diff").is_some() {
self.complete(OneshotOutcome::ApprovalRequired { detail });
} else {
self.complete(OneshotOutcome::ClarificationRequired { detail });
}
return Ok(());
}
if msg
.metadata
.get("isanagent_notification")
.and_then(|value| value.as_bool())
== Some(true)
{
return Ok(());
}
{
let mut state = self.state.lock().expect("oneshot state");
state.final_text = Some(msg.content);
}
Ok(())
}
fn as_any(&self) -> &dyn Any {
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn maps_completed_run_outcome() {
assert_eq!(
OneshotOutcome::from_run_outcome(&RunOutcome::Completed),
OneshotOutcome::Completed
);
assert_eq!(
OneshotOutcome::from_run_outcome(&RunOutcome::Cancelled),
OneshotOutcome::Cancelled
);
}
#[test]
fn normalizes_hotkey_replies() {
assert_eq!(OneshotChannel::normalize_tty_reply("y\n"), "approve");
assert_eq!(OneshotChannel::normalize_tty_reply("3"), "always");
assert_eq!(OneshotChannel::normalize_tty_reply("deny"), "deny");
}
}