use std::sync::LazyLock;
use durable::{Doc, DocDefinition, DocSemantics, DocumentFork, DocumentHistory};
use serde_json::{Value, json};
fn conversation(history: DocumentHistory, fork: DocumentFork) -> DocSemantics {
DocSemantics::Conversation { history, fork }
}
fn define(definition: DocDefinition) -> Doc {
definition.define().expect("built-in document definitions are valid")
}
fn running(slots: Option<&Value>) -> bool {
slots
.and_then(Value::as_array)
.is_some_and(|slots| slots.iter().any(|slot| slot["status"] == "running"))
}
pub static LIVE: LazyLock<Doc> = LazyLock::new(|| {
define(
DocDefinition::new(
"ultron.live",
1,
conversation(DocumentHistory::Latest, DocumentFork::Initial),
|_| json!({}),
)
.checkpoint_when(|_, value, _, _| {
Ok(value.get("generation").is_none() && !running(value.get("tools")) && !running(value.get("nestedTools")))
}),
)
});
pub static INBOX: LazyLock<Doc> = LazyLock::new(|| {
define(
DocDefinition::new(
"ultron.inbox",
1,
conversation(DocumentHistory::Latest, DocumentFork::Initial),
|_| json!({ "items": [] }),
)
.checkpoint_when(|_, value, _, _| Ok(value["items"].as_array().is_none_or(|items| items.is_empty()))),
)
});
pub static USAGE: LazyLock<Doc> = LazyLock::new(|| {
define(
DocDefinition::new(
"ultron.usage",
1,
conversation(DocumentHistory::Latest, DocumentFork::Initial),
|_| json!({ "models": {}, "tools": {} }),
)
.checkpoint_when(|_, _, _, _| Ok(true)),
)
});
pub static AGENT: LazyLock<Doc> = LazyLock::new(|| {
define(
DocDefinition::new(
"ultron.agent",
1,
conversation(DocumentHistory::Rewindable, DocumentFork::AsOf),
|_| json!({}),
)
.checkpoint_when(|_, _, _, _| Ok(true)),
)
});
pub static PROVIDER: LazyLock<Doc> = LazyLock::new(|| {
define(
DocDefinition::new(
"ultron.provider",
1,
conversation(DocumentHistory::Latest, DocumentFork::Initial),
|_| json!({ "sessionId": jsrt::random::uuid_v7(jsrt::date::now_ms()) }),
)
.checkpoint_when(|_, _, _, _| Ok(true)),
)
});