syntax = "proto3";

package anp.v1;

option go_package = "github.com/soraecho/anp/pkg/proto/anp/v1;anpv1";

// ANP 信封 - 统一消息格式
message ANPEnvelope {
  string message_id = 1;           // UUID v4
  string sender_did = 2;           // did:key:xxx
  string receiver_did = 3;         // 接收方 DID (点对点) 或群组 ID
  int64 timestamp = 4;             // Unix 毫秒
  bytes signature = 5;             // 发送方签名
  string topic = 6;                // Gossipsub 主题 (广播消息)

  // E2E 加密字段（可选，为空表示明文）
  bool encrypted = 15;             // 是否加密
  bytes eph_pubkey = 16;          // 发送方临时 Curve25519 公钥
  bytes nonce = 17;               // XSalsa20-Poly1305 nonce
  bytes encrypted_payload = 18;   // 加密后的序列化 payload（加密时使用）
  string payload_type = 19;        // 加密前的 payload 类型（agent/human/mail/webrtc/sys）

  oneof payload {
    AgentMessage agent_msg = 10;   // AI Agent 消息
    HumanMessage human_msg = 11;   // 人类即时消息
    MailMessage mail_msg = 12;     // 人类邮件
    WebRTCSignal webrtc_sig = 13;  // WebRTC 信令
    SystemMessage sys_msg = 14;    // 系统消息
  }
}

// AI Agent 消息
message AgentMessage {
  string conversation_id = 1;
  string prev_cid = 2;             // 上一条消息的 CID (对话链)
  oneof content {
    TaskOffer task_offer = 10;
    TaskResult task_result = 11;
    TaskNegotiation negotiation = 12;
    AgentCard card = 13;          // Agent Card 发布
    A2ATask a2a_task = 14;         // A2A 完整任务
    string text = 15;              // 文本内容
  }
}

// 任务提议
message TaskOffer {
  string task_id = 1;
  string task_type = 2;
  string description = 3;
  map<string, string> requirements = 4;
  int64 deadline = 5;
  int64 reward = 6;                // 可选激励
}

// 任务结果
message TaskResult {
  string task_id = 1;
  string status = 2;               // "success", "failed", "partial"
  oneof output {
    string text = 10;
    ContentRef content_ref = 11;   // 大结果存 IPFS
  }
  string error = 12;
}

// 人类即时消息
message HumanMessage {
  string conversation_id = 1;
  string prev_cid = 2;
  oneof content {
    TextContent text = 10;
    RichContent rich = 11;
    MediaContent media = 12;
  }
}

// 文本内容
message TextContent {
  string text = 1;
  string format = 2;               // "plain", "markdown"
}

// 富媒体内容
message RichContent {
  string content = 1;              // Markdown 或 HTML
  repeated ContentRef attachments = 2;
}

// 媒体内容
message MediaContent {
  ContentRef media = 1;
  string caption = 2;
}

// 邮件消息
message MailMessage {
  string subject = 1;
  string body = 2;                 // HTML 或纯文本
  string format = 3;               // "html", "plain"
  repeated ContentRef attachments = 4;
  repeated string reply_to = 5;    // 回复链 (CID 列表)
  Priority priority = 6;
}

enum Priority {
  PRIORITY_NORMAL = 0;
  PRIORITY_HIGH = 1;
  PRIORITY_LOW = 2;
}

// WebRTC 信令
message WebRTCSignal {
  string call_id = 1;
  SignalType type = 2;
  string sdp = 3;                  // SDP Offer/Answer
  string candidate = 4;            // ICE Candidate
}

enum SignalType {
  SIGNAL_TYPE_OFFER = 0;
  SIGNAL_TYPE_ANSWER = 1;
  SIGNAL_TYPE_ICE_CANDIDATE = 2;
  SIGNAL_TYPE_HANGUP = 3;
}

// 系统消息
message SystemMessage {
  SystemEventType event = 1;
  map<string, string> data = 2;
}

enum SystemEventType {
  EVENT_TYPE_UNKNOWN = 0;
  EVENT_TYPE_PEER_ONLINE = 1;
  EVENT_TYPE_PEER_OFFLINE = 2;
  EVENT_TYPE_GROUP_JOIN = 3;
  EVENT_TYPE_GROUP_LEAVE = 4;
  EVENT_TYPE_GROUP_UPDATE = 5;
}

// 内容引用 (IPFS CID)
message ContentRef {
  string cid = 1;                  // IPFS CID
  int64 size = 2;                  // 字节数
  string mime_type = 3;
  string name = 4;                 // 文件名
}

// Agent 身份信息
message AgentIdentity {
  string did = 1;                  // did:key:xxx
  string peer_id = 2;              // libp2p PeerID
  string name = 3;                 // 显示名称
  string description = 4;
  repeated string capabilities = 5; // 能力标签
  string endpoint = 6;             // 可选 HTTP 端点
  int64 updated_at = 7;
  bytes ca_signature = 8;          // CA 签名 (可选)
  bytes x25519_public_key = 9;    // X25519 公钥（用于 E2E 加密）
}

// === A2A Data Model ===

// A2A 消息 (通信轮次)
message A2AMessage {
  string role = 1;                // "user" | "agent"
  repeated A2APart parts = 2;
  map<string, string> metadata = 3;
  int64 timestamp = 4;
}

// A2A Part (最小内容单元)
message A2APart {
  oneof content {
    TextPart text_part = 1;
    FilePart file_part = 2;
    DataPart data_part = 3;
  }
}

// 文本 Part
message TextPart {
  string text = 1;
  string mime_type = 2;           // "text/plain" | "text/markdown"
}

// 文件 Part
message FilePart {
  string name = 1;
  string mime_type = 2;
  int64 size = 3;
  oneof source {
    string url = 4;
    ContentRef content_ref = 5;
  }
}

// 数据 Part (结构化)
message DataPart {
  string mime_type = 1;           // "application/json"
  bytes data = 2;
  string schema = 3;              // JSON Schema (optional)
}

// A2A Artifact (Agent 输出产物)
message A2AArtifact {
  string artifact_id = 1;
  string name = 2;
  string description = 3;
  repeated A2APart parts = 4;
  map<string, string> metadata = 5;
  int64 created_at = 6;
}

// A2A Task (完整任务状态)
message A2ATask {
  string task_id = 1;
  A2ATaskState state = 2;
  repeated A2AMessage history = 3;   // 对话历史
  repeated A2AArtifact artifacts = 4; // 输出产物
  string context_id = 5;            // 可选上下文
  int64 created_at = 6;
  int64 updated_at = 7;
}

// A2A Task State
enum A2ATaskState {
  A2A_TASK_STATE_UNKNOWN = 0;
  A2A_TASK_STATE_PENDING = 1;
  A2A_TASK_STATE_WORKING = 2;
  A2A_TASK_STATE_INPUT_REQUIRED = 3;  // 需要用户输入
  A2A_TASK_STATE_COMPLETED = 4;
  A2A_TASK_STATE_CANCELED = 5;
  A2A_TASK_STATE_FAILED = 6;
}

// === A2A 适配层 ===

// Agent Card (A2A 标准格式)
message AgentCard {
  // 基础信息
  string did = 1;
  string name = 2;
  string description = 3;
  string version = 4;              // Agent 版本
  
  // 能力声明
  repeated AgentSkill skills = 5;
  repeated string capabilities = 6; // 简化标签 (兼容旧版)
  
  // 端点信息
  AgentEndpoint endpoint = 7;
  
  // 认证信息
  AuthenticationInfo authentication = 8;
  
  // 元数据
  AgentMetadata metadata = 9;
  
  // 时间戳
  int64 created_at = 10;
  int64 updated_at = 11;
  
  // 签名
  bytes signature = 12;
}

// Agent 技能 (细粒度能力)
message AgentSkill {
  string id = 1;                   // skill-xxx
  string name = 2;
  string description = 3;
  repeated string tags = 4;        // 分类标签
  SkillInput input = 5;           // 输入要求
  SkillOutput output = 6;          // 输出格式
}

message SkillInput {
  string mime_type = 1;            // application/json
  string schema = 2;               // JSON Schema (可选)
  bool required = 3;
}

message SkillOutput {
  string mime_type = 1;
  string schema = 2;
}

// Agent 端点
message AgentEndpoint {
  string url = 1;                  // https://agent.example.com
  string transport = 2;            // "anp" | "https" | "ws"
  map<string, string> headers = 3; // 自定义请求头
}

// 认证信息
message AuthenticationInfo {
  string type = 1;                 // "none" | "bearer" | "basic" | "api-key"
  map<string, string> credentials = 2;
}

// Agent 元数据
message AgentMetadata {
  string author = 1;
  string homepage = 2;
  string license = 3;
  repeated string keywords = 4;
  map<string, string> extra = 5;
}

// 任务状态
message TaskStatus {
  string task_id = 1;
  TaskPhase phase = 2;             // 提议/接受/执行/完成
  string status = 3;               // "pending" | "running" | "completed" | "failed"
  int32 progress = 4;              // 0-100
  string message = 5;
  int64 started_at = 6;
  int64 completed_at = 7;
}

enum TaskPhase {
  TASK_PHASE_UNKNOWN = 0;
  TASK_PHASE_OFFERED = 1;         // 已提议
  TASK_PHASE_ACCEPTED = 2;       // 已接受
  TASK_PHASE_EXECUTING = 3;      // 执行中
  TASK_PHASE_COMPLETED = 4;      // 已完成
  TASK_PHASE_CANCELLED = 5;      // 已取消
}

// 任务协商消息
message TaskNegotiation {
  string task_id = 1;
  NegotiationType type = 2;
  oneof payload {
    TaskOffer offer = 10;
    TaskAccept accept = 11;
    TaskReject reject = 12;
    TaskUpdate update = 13;
    TaskCancel cancel = 14;
  }
}

enum NegotiationType {
  NEGOTIATION_TYPE_UNKNOWN = 0;
  NEGOTIATION_TYPE_OFFER = 1;
  NEGOTIATION_TYPE_ACCEPT = 2;
  NEGOTIATION_TYPE_REJECT = 3;
  NEGOTIATION_TYPE_UPDATE = 4;
  NEGOTIATION_TYPE_CANCEL = 5;
}

message TaskAccept {
  string task_id = 1;
  int64 accepted_at = 2;
  map<string, string> params = 3;  // 接受参数
}

message TaskReject {
  string task_id = 1;
  string reason = 2;
  int64 rejected_at = 3;
}

message TaskUpdate {
  string task_id = 1;
  int32 progress = 2;
  string message = 3;
  int64 updated_at = 4;
}

message TaskCancel {
  string task_id = 1;
  string reason = 2;
  string cancelled_by = 3;         // 取消方 DID
  int64 cancelled_at = 4;
}

// 群组信息
message GroupInfo {
  string group_id = 1;
  string name = 2;
  string creator_did = 3;
  repeated string member_dids = 4;
  string latest_msg_cid = 5;       // 最新消息 CID
  int64 created_at = 6;
  int64 updated_at = 7;
  GroupSettings settings = 8;
}

message GroupSettings {
  bool encrypted = 1;
  int32 max_members = 2;
  bool history_visible = 3;
}

// 对话节点 (IPLD DAG)
message ConversationNode {
  string cid = 1;                  // 本节点 CID (计算后填充)
  string conversation_id = 2;
  string sender_did = 3;
  int64 timestamp = 4;
  string prev_cid = 5;             // 上一节点 CID
  repeated string branch_cids = 6; // 分叉节点 (多 Agent 响应)
  oneof content {
    string text = 10;
    ContentRef content_ref = 11;
  }
  map<string, string> metadata = 20;
}
