use std::sync::Arc;
use adk_kernel::drivers::EffectClass;
use adk_kernel::{AgendaItem, AgendaState};
use async_trait::async_trait;
use chrono::{DateTime, Duration, FixedOffset, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use super::registry::{Entry, single_time};
use super::{Callers, ObjectKind, VerbDone, VerbError, VerbHandler, VerbScope, VerbSpec, words};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct ComingUpItem {
pub id: String,
pub title: String,
pub area: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub at: Option<DateTime<Utc>>,
pub at_text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ends: Option<DateTime<Utc>>,
pub choices: Vec<SnoozeChoice>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub why_not: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase")]
pub struct SnoozeChoice {
pub to: DateTime<FixedOffset>,
pub label: String,
pub at_text: String,
pub tomorrow: bool,
}
pub(crate) fn catalogue() -> Vec<Entry> {
let coming_up = VerbSpec::new(
"coming_up",
"Coming up",
"List what is coming up for the person: each item's id, title, when it wakes, and the \
two times it may be snoozed to. Reading it never asks the person.",
EffectClass::Read,
)
.with_objects([ObjectKind::ComingUp])
.with_callers(Callers::PersonAndAgents);
let snooze = VerbSpec::new(
"snooze",
words::SNOOZE,
words::SNOOZE_SOMETHING,
EffectClass::WriteLocal,
)
.with_objects([ObjectKind::ComingUp])
.with_args(json!({
"type": "object",
"properties": {
"item": {
"type": "string",
"description": "The id of the item, as coming_up lists it"
},
"to": {
"type": "string",
"format": "date-time",
"description": "When it should wake instead: exactly one of the two times coming_up lists for the item"
}
},
"required": ["item", "to"],
"additionalProperties": false
}))
.with_callers(Callers::PersonAndAgents)
.only_delays()
.reversible()
.with_voice_phrase("snooze");
vec![
Entry { spec: coming_up, handler: Arc::new(ComingUp) },
Entry { spec: snooze, handler: Arc::new(Snooze) },
]
}
impl VerbScope {
pub async fn coming_up(&self) -> Result<Vec<ComingUpItem>, VerbError> {
let mut out = Vec::new();
for item in self.items().await? {
out.push(self.coming_up_item(&item).await?);
}
out.sort_by(|a, b| match (a.at, b.at) {
(Some(a), Some(b)) => a.cmp(&b),
(Some(_), None) => std::cmp::Ordering::Less,
(None, Some(_)) => std::cmp::Ordering::Greater,
(None, None) => std::cmp::Ordering::Equal,
});
Ok(out)
}
async fn coming_up_item(&self, item: &AgendaItem) -> Result<ComingUpItem, VerbError> {
let at = match &item.state {
AgendaState::Scheduled { wake } => single_time(wake),
_ => None,
};
let why_not = match &item.state {
AgendaState::Running { .. } => Some(words::ALREADY_STARTED),
AgendaState::Scheduled { .. } if at.is_none() => Some(words::NOT_ONE_TIME),
AgendaState::Scheduled { .. } if self.started(item).await? => {
Some(words::ALREADY_STARTED)
}
AgendaState::Scheduled { .. } => None,
_ => Some(words::NOT_SCHEDULED),
};
let choices = match (at, why_not) {
(Some(at), None) => self.choices(at, item.expires_at),
_ => Vec::new(),
};
let why_not = why_not.map(str::to_string).or_else(|| match (at, item.expires_at) {
(Some(_), Some(ends)) if choices.is_empty() => {
Some(words::ends_at(&self.time_text(ends)))
}
_ => None,
});
Ok(ComingUpItem {
id: item.id.clone(),
title: item.title.clone(),
area: item.partition.to_string(),
at,
at_text: at.map(|at| self.time_text(at)).unwrap_or_default(),
ends: item.expires_at,
choices,
why_not,
})
}
fn choices(&self, at: DateTime<Utc>, ends: Option<DateTime<Utc>>) -> Vec<SnoozeChoice> {
[
(Duration::hours(1), words::ONE_HOUR_LATER, false),
(Duration::days(1), words::TOMORROW_SAME_TIME, true),
]
.into_iter()
.map(|(by, label, tomorrow)| (at + by, label, tomorrow))
.filter(|(to, _, _)| ends.is_none_or(|ends| *to <= ends))
.map(|(to, label, tomorrow)| SnoozeChoice {
to: to.with_timezone(&self.offset()),
label: label.to_string(),
at_text: self.time_text(to),
tomorrow,
})
.collect()
}
async fn snoozable(
&self,
id: &str,
to: DateTime<Utc>,
) -> Result<(AgendaItem, DateTime<Utc>), VerbError> {
let item = self.item(id).await?;
let wake = match &item.state {
AgendaState::Scheduled { wake } => wake,
AgendaState::Running { .. } => return Err(VerbError::refused(words::ALREADY_STARTED)),
AgendaState::Proposed | AgendaState::Approved => {
return Err(VerbError::refused(words::NOT_SCHEDULED));
}
_ => return Err(VerbError::refused(words::ENDED)),
};
let Some(at) = single_time(wake) else {
return Err(VerbError::refused(words::NOT_ONE_TIME));
};
if to != at + Duration::hours(1) && to != at + Duration::days(1) {
if to <= at {
return Err(VerbError::refused(words::IT_MOVED));
}
let times: Vec<String> = self
.choices(at, item.expires_at)
.into_iter()
.map(|choice| choice.at_text)
.collect();
return Err(VerbError::refused(match (times.is_empty(), item.expires_at) {
(true, Some(ends)) => words::ends_at(&self.time_text(ends)),
_ => words::only_moves_to(×),
}));
}
if let Some(ends) = item.expires_at.filter(|ends| to > *ends) {
return Err(VerbError::refused(words::ends_at(&self.time_text(ends))));
}
if self.started(&item).await? {
return Err(VerbError::refused(words::ALREADY_STARTED));
}
Ok((item, at))
}
}
pub fn find_by_title<'a>(
items: &'a [ComingUpItem],
title: &str,
) -> Result<&'a ComingUpItem, VerbError> {
let wanted = normalize(title);
let exact: Vec<&ComingUpItem> =
items.iter().filter(|item| normalize(&item.title) == wanted).collect();
match exact.as_slice() {
[only] => return Ok(only),
[] => {}
several => {
return Err(VerbError::Ambiguous {
said: title.to_string(),
matches: several.iter().map(|item| item.title.clone()).collect(),
});
}
}
let partial: Vec<&ComingUpItem> = items
.iter()
.filter(|item| !wanted.is_empty() && normalize(&item.title).contains(&wanted))
.collect();
if partial.len() > 1 {
return Err(VerbError::Ambiguous {
said: title.to_string(),
matches: partial.iter().map(|item| item.title.clone()).collect(),
});
}
Err(VerbError::not_found(format!("“{title}”")))
}
pub(crate) fn normalize(text: &str) -> String {
text.split(|c: char| !c.is_alphanumeric() && c != '\'')
.filter(|word| !word.is_empty())
.map(str::to_lowercase)
.collect::<Vec<_>>()
.join(" ")
}
fn snooze_args(args: &Value) -> Result<(String, DateTime<Utc>), VerbError> {
let item = args.get("item").and_then(Value::as_str).filter(|item| !item.is_empty());
let to = args.get("to").and_then(Value::as_str);
let (Some(item), Some(to)) = (item, to) else {
return Err(VerbError::invalid(
"snooze takes `item`, the id coming_up lists, and `to`, one of its two times",
));
};
let to = DateTime::parse_from_rfc3339(to)
.map_err(|_| VerbError::invalid(format!("`to` is not a time with its offset: {to}")))?
.with_timezone(&Utc);
Ok((item.to_string(), to))
}
struct ComingUp;
#[async_trait]
impl VerbHandler for ComingUp {
async fn describe(&self, _scope: &VerbScope, _args: &Value) -> Result<String, VerbError> {
Ok("List what is coming up".to_string())
}
async fn run(&self, scope: &VerbScope, _args: &Value) -> Result<VerbDone, VerbError> {
let items = scope.coming_up().await?;
let text = match items.len() {
0 => "Nothing is coming up".to_string(),
1 => "1 thing is coming up".to_string(),
count => format!("{count} things are coming up"),
};
let result = serde_json::to_value(&items).unwrap_or(Value::Null);
Ok(VerbDone::new("coming_up", text).with_result(json!({ "items": result })))
}
}
struct Snooze;
#[async_trait]
impl VerbHandler for Snooze {
async fn describe(&self, scope: &VerbScope, args: &Value) -> Result<String, VerbError> {
let (id, to) = snooze_args(args)?;
let (item, _) = scope.snoozable(&id, to).await?;
Ok(words::move_to(&item.title, &scope.time_text(to)))
}
async fn run(&self, scope: &VerbScope, args: &Value) -> Result<VerbDone, VerbError> {
let (id, to) = snooze_args(args)?;
let (item, from) = scope.snoozable(&id, to).await?;
scope.move_wake(&id, from, to).await?;
let at_text = scope.time_text(to);
Ok(VerbDone::new("snooze", words::moved_to(&item.title, &at_text))
.with_target(format!("agenda:{id}"), item.title.clone())
.with_result(json!({
"item": id,
"title": item.title,
"from": from,
"to": to.with_timezone(&scope.offset()),
"atText": at_text,
})))
}
}