talos-sync-types 0.1.6

Canonical data and errors for account-scoped Talos synchronization
Documentation
//! Shared facts projected from existing Talos domain objects.
//!
//! This module owns synchronization envelopes only. Canonical message, result,
//! configuration and error shapes are generated from their existing owners.

use serde::{Deserialize, Serialize};

use crate::{generated as wire, SyncResourceKey};

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SessionExecutionBinding {
    pub computer_id: String,
    pub agent_id: String,
    pub workspace_id: String,
    pub workspace_user_id: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "type",
    rename_all = "camelCase",
    rename_all_fields = "camelCase",
    deny_unknown_fields
)]
pub enum SyncEventKey {
    Agent {
        identity: wire::AgentTurnRef,
        sequence: u64,
    },
    Notice {
        peer_id: String,
        source_event_id: String,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SyncTurnMetadata {
    pub identity: wire::AgentTurnRef,
    pub coordinator_id: String,
    pub previous_turn_id: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SyncSessionSnapshot {
    pub session: wire::SessionInfo,
    pub conf: wire::SessionConf,
    pub environment: Option<wire::TalosSessionEnvironment>,
    pub channel_id: Option<String>,
    pub thread_id: Option<String>,
    pub binding: SessionExecutionBinding,
    pub tree_identity: wire::SessionTreeIdentity,
    pub memory_processed_through: Option<SyncEventKey>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SyncFileSnapshot {
    pub file: wire::FileInfo,
    pub storage_peer_id: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    content = "data",
    rename_all = "camelCase",
    deny_unknown_fields
)]
pub enum SyncResourceSnapshot {
    Computer(wire::NodeInfo),
    Project(wire::ProjectInfo),
    Session(Box<SyncSessionSnapshot>),
    File(SyncFileSnapshot),
}

impl SyncResourceSnapshot {
    pub fn resource(&self) -> SyncResourceKey {
        match self {
            Self::Computer(node) => SyncResourceKey::Computer {
                computer_id: node.node_id.clone(),
            },
            Self::Project(project) => SyncResourceKey::Project {
                project_id: project.project_id.clone(),
            },
            Self::Session(session) => SyncResourceKey::Session {
                session_id: session.session.id.clone(),
            },
            Self::File(file) => SyncResourceKey::File {
                file_id: file.file.file_id.clone(),
            },
        }
    }

    pub fn computer_id(&self) -> Option<&str> {
        match self {
            Self::Computer(node) => Some(&node.node_id),
            Self::Project(project) => Some(&project.node_id),
            Self::Session(session) => Some(&session.binding.computer_id),
            Self::File(_) => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SyncTimelineEntry {
    pub key: SyncEventKey,
    pub event: wire::TimelineEventPayload,
    pub persisted_at: i64,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SyncActiveTurnMetadata {
    pub identity: wire::AgentTurnRef,
    pub state: wire::ActiveTurnState,
    pub updated_at: i64,
    pub last_event: SyncEventKey,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
pub struct SyncAcceptedCommand {
    pub command: wire::IdempotentCommand,
    pub idempotency_key: String,
    pub request_id: String,
    pub request_hash: String,
    pub identity: wire::AgentTurnRef,
    pub message_id: String,
    pub status: wire::TurnCommandStatus,
    pub committed_event: SyncEventKey,
    pub expires_at: Option<i64>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "type",
    content = "data",
    rename_all = "camelCase",
    rename_all_fields = "camelCase",
    deny_unknown_fields
)]
pub enum SyncChange {
    Metadata {
        before: Option<Box<SyncResourceSnapshot>>,
        after: Box<SyncResourceSnapshot>,
    },
    Turn(SyncTurnMetadata),
    Event(SyncTimelineEntry),
    ActiveTurnMetadata(SyncActiveTurnMetadata),
    AcceptedCommand(SyncAcceptedCommand),
    Reset {
        history_generation: u64,
    },
    Delete,
}