use std::fmt::Write as _;
use anyhow::Result;
use plannotator_tui_review::{CodeAnnotation, NoteScope, agent_feedback, pull_agent_feedback};
use super::{Mode, Screen};
use crate::archive::{self, AnnotationRecord, ReviewTarget, Submission, Target};
use crate::delivery::{Clipboard, Delivery as _, DeliveryError};
use crate::ui::send_button::{LastSend, SendState};
pub(super) const MAX_SEND_BYTES: usize = 256 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct PickThen {
all: bool,
close: bool,
}
#[derive(Debug)]
pub(super) enum Undo {
Finish(Vec<String>),
Removed(usize, Box<CodeAnnotation>),
}
impl Screen {
pub(super) fn feedback(&self, notes: &[&CodeAnnotation]) -> String {
if let Some(pull) = &self.capture.pull {
let clone = pull.clone.as_ref().map(|root| root.display().to_string());
return pull_agent_feedback(&pull.view, clone.as_deref(), notes);
}
let diff = self.capture.scope.describe(self.capture.commit_subject.as_deref());
agent_feedback(&self.root().display().to_string(), &diff, notes)
}
pub(super) fn send(&mut self, all: bool) {
self.send_asking(PickThen { all, close: false });
}
pub(super) fn send_picked(&mut self, then: PickThen) {
self.deliver_notes(then.all);
if then.close {
self.quit = !self.has_unsent();
}
}
fn send_asking(&mut self, then: PickThen) -> bool {
let empty =
if then.all { self.store.annotations().is_empty() } else { self.store.pending().is_empty() };
if !empty && let Some(picker) = self.reach.picker_for_send(self.delivery.as_ref(), &self.real_root())
{
self.picker = picker;
self.pick_then = Some(then);
self.mode = Mode::PickTarget;
return true;
}
self.deliver_notes(then.all);
false
}
fn deliver_notes(&mut self, all: bool) {
let notes: Vec<&CodeAnnotation> =
if all { self.store.annotations().iter().collect() } else { self.store.pending() };
if notes.is_empty() {
self.status = Some(if all { "nothing to send" } else { "nothing new to send" }.into());
return;
}
let text = self.feedback(¬es);
if text.len() > MAX_SEND_BYTES {
self.status = Some(format!(
"the feedback is {} KiB, over the {} KiB one send carries; send fewer notes",
text.len().div_ceil(1024),
MAX_SEND_BYTES / 1024
));
return;
}
let ids: Vec<String> = notes.iter().map(|n| n.id.clone()).collect();
let records = notes.iter().map(|n| record(n)).collect();
let target = self.delivery.describe();
let retry = if all { 'R' } else { 'E' };
match self.delivery.deliver(&text) {
Ok(()) => {
self.archive(&text, records, ids.len());
let saved = self.store.record_delivery(&target, &ids);
self.last_send = LastSend::to(self.delivery.is_agent());
let verb = if self.delivery.is_agent() { "sent" } else { "copied" };
let mut status = format!("{verb} {} note(s) → {target}", ids.len());
if let Some(notice) = self.delivery.notice() {
let _ = write!(status, " · {notice}");
}
if let Err(err) = saved {
let _ = write!(status, "; could not save sent status: {err} · next send may repeat it");
}
self.status = Some(status);
self.sync_markers();
self.derive_send_state();
}
Err(error) => {
let copied = error.wants_copy() && self.clipboard && Clipboard.deliver(&text).is_ok();
self.status = Some(error.status(&target, copied, retry));
self.derive_send_state();
if let DeliveryError::Blocked(msg) = error {
self.send_state = SendState::Blocked(msg);
}
}
}
}
fn archive(&self, text: &str, annotations: Vec<AnnotationRecord>, count: usize) {
if !archive::enabled(|key| std::env::var(key).ok(), &self.data_dir) {
return;
}
let pull = self.capture.pull.as_ref();
let review = ReviewTarget {
vcs_type: "git",
diff_type: self.capture.scope.diff_type(),
base: pull
.map(|p| p.view.base_sha.clone())
.or_else(|| self.capture.scope.base().map(str::to_owned)),
git_ref: pull.map(|p| format!("PR #{}", p.view.number)),
pr: pull.map(|p| archive::PrTarget {
provider: "github",
repo: p.view.repo.slug(),
number: p.view.number,
}),
cwd: self.root().display().to_string(),
changed_files: self.capture.files.len(),
patch_bytes: self.capture.patch_bytes(),
};
let origin = self.delivery.agent_host().map(|host| archive::origin_label(host).to_owned());
archive::append(&Submission {
data_dir: &self.data_dir,
project: &self.project,
surface: "review",
decision: "feedback",
origin,
target: Target::review(review),
feedback: text,
annotations,
count,
images: 0,
now_ms: None,
});
}
pub(super) fn has_unsent(&self) -> bool {
self.delivery.is_agent() && !self.store.pending().is_empty()
}
pub(super) fn request_quit(&mut self) {
self.count_unsent_elsewhere();
if self.has_unsent() || !self.switched.unsent.is_empty() || !self.switched.gone.is_empty() {
self.mode = Mode::ConfirmQuit;
self.told_of_gone();
} else {
self.quit = true;
}
}
pub(super) fn send_and_quit(&mut self) {
if !self.store.pending().is_empty() && self.send_asking(PickThen { all: false, close: true }) {
return;
}
self.quit = !self.has_unsent();
}
pub(super) fn finish(&mut self) {
let ids = match self.store.archive_sent() {
Ok(ids) => ids,
Err(err) => {
self.status = Some(format!("could not finish: {err}"));
return;
}
};
self.status = Some(if ids.is_empty() {
"no sent notes to archive".to_owned()
} else {
format!("archived {} note(s)", ids.len())
});
if !ids.is_empty() {
self.undo.push(Undo::Finish(ids));
}
self.after_note_change();
}
pub(super) fn undo_finish(&mut self) -> Result<()> {
match self.undo.pop() {
None => self.status = Some("nothing to undo".into()),
Some(Undo::Finish(ids)) => {
let restored = self.store.restore_archived(&ids)?;
self.place_notes()?;
self.status = Some(format!("restored {restored} note(s)"));
self.after_note_change();
}
Some(Undo::Removed(index, note)) => {
let id = note.id.clone();
self.status = Some(if self.store.put_back(index, *note)? {
"note restored".to_owned()
} else {
"that note is back already".to_owned()
});
self.after_note_change();
self.select_note(&id);
}
}
Ok(())
}
pub(super) fn undo_label(&self) -> &'static str {
match self.undo.last() {
Some(Undo::Removed(..)) => "Undo remove",
_ => "Undo finish",
}
}
pub(super) fn send_label(&self) -> String {
let count = self.store.pending().len();
let asks = self.reach.asks(self.delivery.as_ref());
let verb = if self.delivery.is_agent() || asks { "Send" } else { "Copy" };
let to = if self.delivery.is_agent() {
format!(" ▸ {}", self.delivery.describe())
} else if asks {
" ▸ choose agent".to_owned()
} else {
String::new()
};
let key = match self.keys.hint(crate::keys::Action::Send) {
key if key.is_empty() => String::new(),
key => format!(" ({key})"),
};
let copied = match self.last_send {
LastSend::Clipboard => true,
LastSend::Agent => false,
LastSend::None => {
self.store.deliveries().last().is_some_and(|d| d.target == Clipboard.describe())
}
};
match &self.send_state {
SendState::Blocked(_) => format!("{} at a dialog · retry{key}", self.delivery.describe()),
SendState::Sent if copied => "Copied ✓".to_owned(),
SendState::Sent if self.delivery.is_agent() => format!("Sent ✓{to}"),
SendState::Sent => "Sent ✓".to_owned(),
SendState::Ready => format!("{verb} {count} new{to}{key}"),
}
}
}
fn record(note: &CodeAnnotation) -> AnnotationRecord {
let line = note.scope == NoteScope::Line;
AnnotationRecord {
id: Some(note.id.clone()),
kind: Some(
serde_json::to_value(note.kind)
.ok()
.and_then(|v| v.as_str().map(str::to_owned))
.unwrap_or_default(),
),
text: note.text.clone().filter(|t| !t.is_empty()),
original_text: note.original_code.clone().or_else(|| note.anchor_text.clone()),
file: Some(note.file_path.clone()),
line_start: line.then_some(note.line_start),
line_end: line.then_some(note.line_end),
side: line.then(|| note.side.as_str().to_owned()),
scope: Some(
serde_json::to_value(note.scope)
.ok()
.and_then(|v| v.as_str().map(str::to_owned))
.unwrap_or_default(),
),
author: note.author_name.clone(),
}
}