Skip to main content

blink_shared/
lib.rs

1//! Blink host/guest shared protocol definitions.
2
3use serde::{Deserialize, Serialize};
4
5/// Default OCI rootfs image for agent sandboxes.
6pub const DEFAULT_ROOTFS_IMAGE: &str = "alpine:3.20";
7
8pub const BLINK_MAGIC: u32 = 0x424C_494E;
9pub const PROTOCOL_VERSION: u8 = 1;
10pub const VHUB_PORT: u32 = 10000;
11pub const VMADDR_CID_HOST: u32 = 2;
12
13/// Guest path for agent memory and artifacts (persisted on session disk).
14pub const AGENT_MEMORY_DIR: &str = "/var/blink/memory";
15
16pub const HEADER_FORMAT: &str = "<I B B H I Q";
17pub const HEADER_SIZE: usize = 20;
18
19#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
20#[repr(u8)]
21pub enum MessageType {
22    Handshake = 0x01,
23    Heartbeat = 0x02,
24    Error = 0x03,
25    RpcRequest = 0x10,
26    RpcResponse = 0x11,
27    RpcError = 0x12,
28    /// Raw PTY byte stream (interactive terminal output/input).
29    StreamData = 0x20,
30    /// Resize PTY window: JSON `{"rows":N,"cols":N}`.
31    TtyResize = 0x21,
32    Stdout = 0x30,
33    Stderr = 0x31,
34}
35
36impl MessageType {
37    pub fn from_u8(value: u8) -> Option<Self> {
38        match value {
39            0x01 => Some(Self::Handshake),
40            0x02 => Some(Self::Heartbeat),
41            0x03 => Some(Self::Error),
42            0x10 => Some(Self::RpcRequest),
43            0x11 => Some(Self::RpcResponse),
44            0x12 => Some(Self::RpcError),
45            0x20 => Some(Self::StreamData),
46            0x21 => Some(Self::TtyResize),
47            0x30 => Some(Self::Stdout),
48            0x31 => Some(Self::Stderr),
49            _ => None,
50        }
51    }
52}
53
54#[derive(Clone, Debug, Serialize, Deserialize)]
55pub struct ExecutionPayload {
56    pub event: String,
57    pub stdout: String,
58    pub stderr: String,
59    pub exit_code: i32,
60}