use chord_delta::Op;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{Map, Value};
pub type JsonObject = Map<String, Value>;
pub type Cursor = JsonObject;
macro_rules! id_types {
($($(#[$doc:meta])* $name:ident),* $(,)?) => {$(
$(#[$doc])*
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct $name(pub u64);
)*};
}
id_types!(
ConversationId,
EntryId,
TaskId,
SubmissionId,
DocumentId,
Seq,
);
pub const ROOT_CONVERSATION_ID: ConversationId = ConversationId(1);
pub const MAX_SAFE_ID: u64 = (1 << 53) - 1;
fn present<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Option<Value>, D::Error> {
Value::deserialize(deserializer).map(Some)
}
fn is_false(value: &bool) -> bool {
!value
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ConversationRecord {
pub id: ConversationId,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub parent: Option<ConversationParent>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub owner: Option<ConversationOwner>,
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ConversationParent {
pub conversation_id: ConversationId,
pub at: EntryId,
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ConversationOwner {
pub conversation_id: ConversationId,
pub task_id: TaskId,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct EntryRecord {
pub id: EntryId,
pub conversation_id: ConversationId,
pub kind: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub model: Option<Vec<Value>>,
#[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")]
pub data: Option<Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub head: Option<EntryId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub edits: Option<Vec<Value>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub by_task_id: Option<TaskId>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum TaskStatus {
Pending,
Running,
Waiting,
Completing,
Terminal,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum JoinPolicy {
FailFast,
AllSettled,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "camelCase", deny_unknown_fields)]
pub enum TaskState {
Pending {
checkpoint: Value,
},
Running {
checkpoint: Value,
},
Waiting {
checkpoint: Value,
on: Vec<TaskId>,
policy: JoinPolicy,
},
Completing {
outcome: TaskOutcome,
},
Terminal {
outcome: TaskOutcome,
},
}
impl TaskState {
pub fn status(&self) -> TaskStatus {
match self {
TaskState::Pending { .. } => TaskStatus::Pending,
TaskState::Running { .. } => TaskStatus::Running,
TaskState::Waiting { .. } => TaskStatus::Waiting,
TaskState::Completing { .. } => TaskStatus::Completing,
TaskState::Terminal { .. } => TaskStatus::Terminal,
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "camelCase", deny_unknown_fields)]
pub enum TaskOutcome {
Completed {
result: Value,
},
Failed {
error: TaskOutcomeError,
#[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")]
result: Option<Value>,
},
Aborted {
#[serde(default, skip_serializing_if = "Option::is_none")]
reason: Option<String>,
#[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")]
result: Option<Value>,
},
Orphaned {
reason: String,
},
Faulted {
error: TaskOutcomeError,
},
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TaskOutcomeError {
pub message: String,
#[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")]
pub detail: Option<Value>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum AbortReason {
Restart,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct TaskRecord {
pub id: TaskId,
pub conversation_id: ConversationId,
pub kind: String,
pub version: u64,
pub input: Value,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub owner: Option<TaskId>,
pub background: bool,
pub abort_requested: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub abort_reason: Option<AbortReason>,
#[serde(default, skip_serializing_if = "is_false")]
pub abandon_on_restart: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub started_at: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ended_at: Option<u64>,
pub state: TaskState,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub memos: Option<JsonObject>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SubmissionType {
Input,
Write,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SubmissionStatus {
Queued,
Placed,
Done,
Unanswered,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SubmissionRecord {
pub id: SubmissionId,
pub conversation_id: ConversationId,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub request_id: Option<String>,
#[serde(rename = "type")]
pub kind: SubmissionType,
pub status: SubmissionStatus,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub entry: Option<EntryId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub answer: Option<EntryId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
#[serde(default, deserialize_with = "present", skip_serializing_if = "Option::is_none")]
pub detail: Option<Value>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(
tag = "kind",
rename_all = "camelCase",
rename_all_fields = "camelCase",
deny_unknown_fields
)]
pub enum DocumentScope {
Session,
Conversation { conversation_id: ConversationId },
Task { task_id: TaskId },
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum DocumentHistory {
Latest,
Rewindable,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum DocumentFork {
AsOf,
Current,
Initial,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct DocumentCreate {
pub id: DocumentId,
pub kind: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub key: Option<String>,
pub scope: DocumentScope,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub history: Option<DocumentHistory>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub fork: Option<DocumentFork>,
}
impl DocumentCreate {
pub fn is_current_only(&self) -> bool {
!matches!(self.scope, DocumentScope::Conversation { .. }) || self.history == Some(DocumentHistory::Latest)
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DocumentRecord {
#[serde(flatten)]
pub identity: DocumentCreate,
pub created_at: Seq,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub retired_at: Option<Seq>,
}
impl DocumentRecord {
pub fn is_alive_at(&self, at: DocumentPoint) -> bool {
match at {
DocumentPoint::Current => self.retired_at.is_none(),
DocumentPoint::At(seq) => self.created_at <= seq && self.retired_at.is_none_or(|retired| seq < retired),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "camelCase", deny_unknown_fields)]
pub enum DocumentContent {
Base {
version: u64,
value: JsonObject,
},
Delta {
version: u64,
#[serde(serialize_with = "ops_out", deserialize_with = "ops_in")]
ops: Vec<Op>,
},
}
impl DocumentContent {
pub fn version(&self) -> u64 {
match self {
DocumentContent::Base { version, .. } | DocumentContent::Delta { version, .. } => *version,
}
}
}
fn ops_out<S: Serializer>(ops: &[Op], serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_seq(ops.iter().map(Op::to_json))
}
fn ops_in<'de, D: Deserializer<'de>>(deserializer: D) -> Result<Vec<Op>, D::Error> {
Vec::<Value>::deserialize(deserializer)?
.iter()
.map(|op| Op::from_json(op).map_err(serde::de::Error::custom))
.collect()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DocumentPoint {
Current,
At(Seq),
}
impl Serialize for DocumentPoint {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
match self {
DocumentPoint::Current => serializer.serialize_str("current"),
DocumentPoint::At(seq) => seq.serialize(serializer),
}
}
}
impl<'de> Deserialize<'de> for DocumentPoint {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
match Value::deserialize(deserializer)? {
Value::String(text) if text == "current" => Ok(DocumentPoint::Current),
Value::Number(number) if number.as_u64().is_some() => {
Ok(DocumentPoint::At(Seq(number.as_u64().unwrap_or(0))))
}
other => Err(serde::de::Error::custom(format!("invalid document point: {other}"))),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DocumentCopySource {
pub id: DocumentId,
pub at: DocumentPoint,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", deny_unknown_fields)]
#[expect(
clippy::large_enum_variant,
reason = "writes are short-lived batch items; boxing adds an allocation each"
)]
pub enum StorageWrite {
#[serde(rename = "conversation")]
Conversation { value: ConversationRecord },
#[serde(rename = "entry")]
Entry { value: EntryRecord },
#[serde(rename = "task")]
Task { value: TaskRecord },
#[serde(rename = "submission")]
Submission { value: SubmissionRecord },
#[serde(rename = "document.create")]
DocumentCreate {
record: DocumentCreate,
content: DocumentContent,
},
#[serde(rename = "document.copy")]
DocumentCopy {
record: DocumentCreate,
source: DocumentCopySource,
},
#[serde(rename = "document.change")]
DocumentChange { id: DocumentId, content: DocumentContent },
#[serde(rename = "document.retire")]
DocumentRetire { id: DocumentId },
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ScanOrder {
Ascending,
Descending,
}
impl ScanOrder {
pub fn as_str(self) -> &'static str {
match self {
ScanOrder::Ascending => "ascending",
ScanOrder::Descending => "descending",
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct ConversationQuery {
pub owner_conversation_id: Option<ConversationId>,
pub owner_task_id: Option<TaskId>,
pub order: Option<ScanOrder>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct EntryQuery {
pub conversation_id: ConversationId,
#[serde(default)]
pub min_entry_id: Option<EntryId>,
#[serde(default)]
pub max_entry_id: Option<EntryId>,
#[serde(default)]
pub order: Option<ScanOrder>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct TaskQuery {
pub conversation_id: Option<ConversationId>,
pub kind: Option<String>,
pub status: Option<TaskStatus>,
pub abort_requested: Option<bool>,
pub background: Option<bool>,
pub order: Option<ScanOrder>,
}
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SubmissionQuery {
pub conversation_id: Option<ConversationId>,
pub status: Option<SubmissionStatus>,
pub order: Option<ScanOrder>,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DocumentAddress {
pub kind: String,
pub scope: DocumentScope,
#[serde(default)]
pub key: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DocumentQuery {
pub scope: DocumentScope,
pub at: DocumentPoint,
#[serde(default)]
pub kind: Option<String>,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct Page<T> {
pub items: Vec<T>,
#[serde(skip_serializing_if = "Option::is_none")]
pub next: Option<Cursor>,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StoredEntry {
pub entry: EntryRecord,
pub commit_seq: Seq,
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct StoredDocument {
pub record: DocumentRecord,
pub version: u64,
pub value: JsonObject,
pub deltas_since_base: usize,
}