use std::collections::HashMap;
use adk_core::{Event, RequestedCall, RequestedVia, RequestedViaKind};
use serde_json::Value;
use super::drawn::{DrawnSent, FormState, FormStatus, SentVia};
use crate::model::{DecidedAnswer, FeedEntry, ThreadItem};
use crate::words;
pub(crate) fn requested_via(event: &Event) -> Option<RequestedVia> {
RequestedCall::via_of(event)
}
pub(crate) fn sent(
via: &RequestedVia,
tool: &str,
call_id: &str,
event: &Event,
hh_mm: &str,
refused_by: Option<&str>,
decision_id: Option<String>,
) -> ThreadItem {
let sent_via = match via.kind {
RequestedViaKind::Form => SentVia::Form,
_ => SentVia::App,
};
ThreadItem::DrawnSent {
sent: DrawnSent {
surface_id: via.surface_id.clone(),
version: via.version,
action_id: via.action_id.clone(),
tool: tool.to_string(),
call_id: call_id.to_string(),
decision_id,
held: refused_by.is_none(),
at: event.timestamp,
text: match (refused_by, sent_via) {
(Some(agent), _) => words::cant_use_here(agent, tool),
(None, SentVia::Form) => words::sent_at(hh_mm),
(None, SentVia::App) => words::app_asked_at(hh_mm),
},
via: sent_via,
},
}
}
pub(crate) fn is_requested(call_id: Option<&String>) -> bool {
call_id.is_some_and(|id| RequestedCall::is_requested_id(id))
}
pub(crate) fn refused_on_resume(tool: &str, response: &Value) -> bool {
response
.get("error")
.and_then(Value::as_str)
.is_some_and(|error| error.starts_with(&format!("Tool '{tool}' denied by policy")))
}
pub fn form_states(
entries: &[FeedEntry],
decided: impl Fn(&str) -> Option<DecidedAnswer>,
) -> Vec<FormState> {
let mut sends: Vec<(&DrawnSent, String)> = Vec::new();
let mut results: HashMap<&str, (bool, &str, &str)> = HashMap::new();
for entry in entries {
for item in &entry.items {
match item {
ThreadItem::DrawnSent { sent } if sent.via == SentVia::App => {}
ThreadItem::DrawnSent { sent } => {
sends.retain(|(known, _)| {
(known.surface_id.as_str(), known.version)
!= (sent.surface_id.as_str(), sent.version)
});
sends.push((sent, entry.at_text.clone()));
}
ThreadItem::Result { call_id: Some(id), ok, text, .. } => {
results.insert(id.as_str(), (*ok, text.as_str(), entry.at_text.as_str()));
}
_ => {}
}
}
}
sends
.into_iter()
.map(|(sent, at_text)| {
let answer = sent.decision_id.as_deref().and_then(&decided);
let said_no = answer == Some(DecidedAnswer::No);
let (status, text) = match (sent.held, results.get(sent.call_id.as_str())) {
(false, _) => (FormStatus::Refused, sent.text.clone()),
(true, _) if answer == Some(DecidedAnswer::Ended) => {
(FormStatus::Ended, words::ENDED_WITHOUT_AN_ANSWER.to_string())
}
(true, Some((true, _, done_at))) => (FormStatus::Done, words::sent_done(done_at)),
(true, Some(_)) if said_no => (FormStatus::SaidNo, words::SENT_YOU_SAID_NO.into()),
(true, Some((false, result, _))) if *result != words::FAILED => {
(FormStatus::NotRun, (*result).to_string())
}
(true, Some(_)) => (FormStatus::Failed, words::SENT_FAILED.to_string()),
(true, None) => match answer {
None => (FormStatus::Waiting, words::sent_waiting(&at_text)),
Some(DecidedAnswer::No) => {
(FormStatus::SaidNo, words::SENT_YOU_SAID_NO.to_string())
}
Some(DecidedAnswer::Ended) => {
(FormStatus::Ended, words::ENDED_WITHOUT_AN_ANSWER.to_string())
}
Some(
DecidedAnswer::Yes
| DecidedAnswer::Edited
| DecidedAnswer::Replied
| DecidedAnswer::Unknown,
) => (FormStatus::Sent, words::sent_at(&at_text)),
},
};
FormState {
surface_id: sent.surface_id.clone(),
version: sent.version,
locked: status != FormStatus::Refused,
status,
text,
decision_id: sent.decision_id.clone(),
}
})
.collect()
}