use crate::checkpoint::Checkpointer;
use crate::error::{GraphError, Result};
use crate::node::ExecutionConfig;
use crate::state::State;
use super::execution_log::ExecutionLog;
#[doc(hidden)]
pub async fn load_resume_point(
checkpointer: &dyn Checkpointer,
config: &ExecutionConfig,
) -> Result<Option<(State, ExecutionLog)>> {
let Some(checkpoint_id) = config.resume_from.as_deref() else {
return Ok(None);
};
let thread_id = &config.thread_id;
let checkpoint = checkpointer
.load_by_id(checkpoint_id)
.await?
.filter(|checkpoint| checkpoint.thread_id == *thread_id)
.ok_or_else(|| {
GraphError::CheckpointError(format!(
"cannot resume thread '{thread_id}': it has no checkpoint with id \
'{checkpoint_id}'. Pass an id that `Checkpointer::load` or \
`Checkpointer::list` returns for this thread"
))
})?;
let log = match checkpoint.metadata.get("execution_log") {
Some(value) => serde_json::from_value(value.clone()).map_err(|error| {
GraphError::CheckpointError(format!(
"checkpoint '{checkpoint_id}' holds an execution log that does not \
deserialize: {error}"
))
})?,
None => ExecutionLog::new(),
};
Ok(Some((checkpoint.state, log)))
}