use tau_proto::{CborValue, ToolCallId, ToolName, ToolUseState, ToolUseStatus};
use crate::tool_render::synthesize_fallback_display;
#[derive(Clone, Copy)]
pub(super) enum TerminalToolOutcome<'a> {
SuccessResult,
Error { canonical_message: &'a str },
Cancelled,
}
pub(super) struct BorrowedToolError<'a> {
pub(super) call_id: &'a ToolCallId,
pub(super) tool_name: &'a ToolName,
pub(super) message: &'a str,
pub(super) details: Option<&'a CborValue>,
pub(super) descriptor: Option<&'a ToolUseState>,
pub(super) originator_is_user: bool,
}
pub(super) fn normalize_terminal_tool_use_state(
mut descriptor: ToolUseState,
outcome: TerminalToolOutcome<'_>,
) -> ToolUseState {
match outcome {
TerminalToolOutcome::SuccessResult => {
if descriptor.status == ToolUseStatus::Warning {
if descriptor.status_text.trim().is_empty() {
descriptor.status_text = "warn".to_owned();
}
} else {
descriptor.status = ToolUseStatus::Success;
descriptor.status_text = "ok".to_owned();
}
}
TerminalToolOutcome::Error { canonical_message } => {
let retain_producer_label = descriptor.status == ToolUseStatus::Error
&& !descriptor.status_text.trim().is_empty();
descriptor.status = ToolUseStatus::Error;
if !retain_producer_label {
descriptor.status_text = if canonical_message.trim().is_empty() {
"err".to_owned()
} else {
let fallback =
synthesize_fallback_display("", Some(canonical_message)).status_text;
if fallback.trim().is_empty() {
"err".to_owned()
} else {
fallback
}
};
}
}
TerminalToolOutcome::Cancelled => {
descriptor.status = ToolUseStatus::Warning;
descriptor.status_text = "cancelled".to_owned();
}
}
descriptor
}