use std::sync::Arc;
use std::time::Duration;
use serde_json::{Value, json};
use super::termination::holds;
use super::{ProceduralRun, Slot, beat_of};
use crate::api::PlanAnswer;
use crate::dialogue::graph::{GraphState, emit};
use crate::dialogue::window::Window;
use crate::error::EmbedError;
use crate::runtime;
use crate::shared::Shared;
use crate::types::DialogueId;
pub(crate) const PLAN_WAIT: Duration = Duration::from_secs(15);
pub(crate) struct Asked {
pub beat: Option<String>,
pub prefix: String,
pub open_topics: Vec<String>,
}
impl Asked {
pub fn anchor(&self) -> String {
self.beat.clone().unwrap_or_else(|| format!("{}_0", self.prefix))
}
}
pub(crate) enum Landed {
Planned(Value),
Kept(String),
Failed(EmbedError),
TimedOut,
}
impl ProceduralRun {
pub(super) fn next_plan(&mut self, graph: &GraphState, window: &Window) -> Option<Value> {
if graph.ended() || self.plans_off || self.asked_here || !matches!(self.slot, Slot::Idle) {
return None;
}
let reason = if self.exhausted(graph) {
"exhausted"
} else if self.crossed {
"crossed"
} else {
return None;
};
Some(self.ask(reason, graph, window))
}
pub(super) fn exhausted(&self, graph: &GraphState) -> bool {
let Some(beat) = self.frontier.beat(beat_of(&graph.current().id)) else { return false };
let sketch = |id: &str| self.frontier.beat(id).is_some_and(|b| b.sketch);
beat.sketch || (!beat.exits.is_empty() && beat.exits.iter().all(|exit| sketch(&exit.to)))
}
pub(super) fn ask(&mut self, reason: &str, graph: &GraphState, window: &Window) -> Value {
let prefix = format!("p{}", self.progress.next_prefix.max(1));
self.progress.next_prefix = self.progress.next_prefix.max(1).saturating_add(1);
self.progress.plans = self.progress.plans.saturating_add(1);
(self.asked_here, self.crossed) = (true, false);
let open_topics = self.open_topics(graph);
let mut body = json!({
"document": self.document,
"npc": self.npc,
"reason": reason,
"id_prefix": prefix,
"open_topics": open_topics,
"goals_met": self.goals_met(graph),
"visited": self.progress.visited,
"turns": self.progress.turns,
"beats": self.progress.beats,
"history": window.entries(),
});
let beat = (reason != "start").then(|| beat_of(&graph.current().id).to_owned());
if let Some(current) = &beat {
body["current"] = json!(current);
body["frontier"] = json!(self.frontier);
}
self.slot = Slot::InFlight(Asked { beat, prefix, open_topics });
body
}
fn open_topics(&self, graph: &GraphState) -> Vec<String> {
let admits = |npcs: &Option<Vec<String>>| npcs.as_ref().is_none_or(|ids| ids.contains(&self.npc));
let open = |when: &Option<String>| when.as_deref().is_none_or(|text| holds(graph, text));
self.doc.procedural.topics.iter().filter(|t| admits(&t.npcs) && open(&t.when)).map(|t| t.id.clone()).collect()
}
fn goals_met(&self, graph: &GraphState) -> Vec<String> {
self.doc.procedural.goals.iter().filter(|g| self.goal_holds(graph, &g.id)).map(|g| g.id.clone()).collect()
}
}
pub(crate) async fn run(shared: Arc<Shared>, dialogue: DialogueId, body: Value) {
let landed = match tokio::time::timeout(PLAN_WAIT, shared.api.plan_dialogue(body)).await {
Ok(Ok(PlanAnswer::Planned { fragment })) => Landed::Planned(fragment),
Ok(Ok(PlanAnswer::Kept { reason })) => Landed::Kept(reason),
Ok(Err(error)) => Landed::Failed(error),
Err(_) => Landed::TimedOut,
};
let Some((steps, next)) = super::load::land(&shared, dialogue, landed) else { return };
emit(&shared, dialogue, steps);
if let Some(body) = next {
spawn(&shared, dialogue, body);
}
}
pub(crate) fn spawn(shared: &Arc<Shared>, dialogue: DialogueId, body: Value) {
runtime::spawn(shared, run(Arc::clone(shared), dialogue, body));
}