syntax = "proto3";

package ual;

// UAL 核心消息结构
message UALMessage {
  Header header = 1;
  oneof payload {
      Graph content = 2;
      Handshake handshake = 4;
  }
  Signature signature = 3;
}

message Header {
  string sender_id = 1;
  string receiver_id = 2;
  int64 timestamp = 3;
  string message_id = 4;
  string protocol_version = 5;
  // 语义哈希，用于压缩长指令
  string semantic_hash = 6;
  // 是否是 Delta 增量更新
  bool is_delta = 7;
  // Delta 压缩基准哈希 (上一帧的 semantic_hash)
  string parent_hash = 8;
  
  // 情感与优先级
  float urgency = 9; // 0.0 - 1.0 (0=Low, 1=Critical)
  enum Style {
    DEFAULT = 0;
    REQUEST = 1;
    COMMAND = 2;
    SUGGESTION = 3;
    ALERT = 4;
  }
  Style style = 10;
  
  // 环境共鸣层
  EnvironmentalFrame env_frame = 11;
}

message EnvironmentalFrame {
  string frame_id = 1; // e.g. "map_frame_01"
  repeated float origin = 2; // [x, y, z]
  repeated float orientation = 3; // [x, y, z, w] quaternion
  string unit = 4; // e.g. "meter"
  double timestamp = 5;
}

message Graph {
  repeated Node nodes = 1;
  repeated Edge edges = 2;
  // 全局上下文引用 ID (用于 KV Cache)
  string context_id = 3;
  // Delta 模式下：被移除的节点 ID 列表
  repeated string removed_node_ids = 4;
}

message Node {
  string id = 1; // 节点唯一标识
  
  // 语义 ID (来自 Universal Atlas)
  // 使用 uint32 存储 hex ID, e.g., 0x0A1
  uint32 semantic_id = 2;
  
  // 节点类型
  enum NodeType {
    UNKNOWN = 0;
    ENTITY = 1;    // 实体 (名词)
    ACTION = 2;    // 动作 (动词)
    PROPERTY = 3;  // 属性 (形容词/副词)
    LOGIC = 4;     // 逻辑谓词 (If/Then/Else)
    MODAL = 5;     // 模态 (Must/Should/Can)
    VALUE = 6;     // 具体数值
    DATA_REF = 7;  // 数据引用 (多模态)
  }
  NodeType type = 3;
  
  // 节点具体值 (如果是 VALUE 类型)
  oneof value {
    string str_val = 4;
    double num_val = 5;
    bool bool_val = 6;
    bytes blob_val = 7; // 原始数据或 Embeddings
    Vector embedding = 8;
  }
  
  // 动态扩展：如果 semantic_id 未知，提供描述子图
  Graph definition = 9;
}

message Edge {
  string source_id = 1;
  string target_id = 2;
  
  // 关系类型
  enum RelationType {
    DEPENDS_ON = 0; // 依赖
    NEXT = 1;       // 顺序 (A -> B means A then B)
    ATTRIBUTE = 2;  // 属性修饰
    ARGUMENT = 3;   // 参数 (Action -> Object)
    CONDITION = 4;  // 条件 (IfNode -> ActionNode)
    CONSEQUENCE = 5;// 结果 (IfNode -> ThenAction)
    ALTERNATIVE = 6;// 替代 (IfNode -> ElseAction)
    TEMPORAL = 7;   // 时序约束 (Action -> Time/Duration)
  }
  RelationType relation = 3;
  
  // 边权重 (用于概率图)
  float weight = 4;
}

message Vector {
    repeated float values = 1;
}

message Handshake {
    string agent_id = 1;
    repeated string capabilities = 2;
    string public_key = 3;
    repeated string active_namespaces = 4; // e.g. ["warehouse_v1"]
}

message Signature {
    string algorithm = 1;
    bytes signature = 2;
    string signer_id = 3;
}
