ai-cortex-sdk 0.1.1

Rust client SDK for AI Cortex server: PAT auth, device binding, software store, offline license (Ed25519), and auto-update (check-update pull + SSE push).
Documentation
use serde::{Deserialize, Serialize};

// --- Device models ---

/// 设备记录(user 维度:PAT 流程下设备绑定到用户)。
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceRecord {
    pub id: String,
    /// 旧 SDK/HMAC 流程的占位字段(PAT 流程下为空串)
    pub client_id: String,
    pub auth_app_id: String,
    /// PAT 所属用户
    pub user_id: String,
    pub fingerprint: String,
    pub device_info: String,
    pub last_active_time: i64,
    pub status: i32,
    pub created_time: i64,
    pub updated_time: i64,
}

// --- Software models ---

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SoftwareInfo {
    pub id: String,
    pub name: String,
    pub description: String,
    pub software_type: i32,
    pub icon: String,
    pub status: i32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SoftwareVersion {
    pub id: String,
    pub software_id: String,
    pub version: String,
    pub release_notes: String,
    pub download_url: String,
    pub file_size: i64,
    pub platform: String,
    pub status: i32,
    /// 是否强制更新(旧服务端无此列时默认 false,向后兼容)
    #[serde(default)]
    pub force_update: bool,
    /// 最低兼容版本(旧服务端无此列时默认空串,向后兼容)
    #[serde(default)]
    pub min_compatible: String,
    /// 发布通道(stable/beta/...,旧服务端无此列时默认空串,向后兼容)
    #[serde(default)]
    pub channel: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SoftwareStoreItem {
    pub software: SoftwareInfo,
    pub latest_version: Option<SoftwareVersion>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DownloadInfo {
    pub download_url: String,
    pub file_size: i64,
    pub version: String,
}

// --- Auto update models ---

/// 版本摘要(`sdk-check-update` 响应中的目标版本信息)
#[derive(Debug, Clone, Deserialize)]
pub struct VersionBrief {
    pub id: String,
    pub version: String,
    pub release_notes: String,
    pub download_url: String,
    pub file_size: i64,
    pub platform: String,
    pub channel: String,
    pub force_update: bool,
    pub min_compatible: String,
}

/// `sdk-check-update` 响应:是否有更新、是否强制、当前版本、目标版本摘要。
#[derive(Debug, Clone, Deserialize)]
pub struct UpdateInfo {
    pub has_update: bool,
    pub force_update: bool,
    pub current_version: String,
    pub target_version: Option<VersionBrief>,
}

/// SSE 事件载荷(`data:` JSON)。`id` 为服务端单调递增序号,用于 Last-Event-ID 重连。
#[derive(Debug, Clone, Deserialize)]
pub struct UpdateEvent {
    #[serde(default)]
    pub id: u64,
    pub software_id: String,
    pub version: String,
    pub platform: String,
    pub channel: String,
    pub force_update: bool,
}

// --- Generic API response wrapper ---
// The server wraps responses in ApiResult format: { "success": bool, "data": ..., "message": "...", "code": ... }

#[derive(Debug, Clone, Deserialize)]
pub struct ApiResponse<T> {
    pub success: bool,
    #[serde(default)]
    pub message: Option<String>,
    #[serde(default)]
    pub code: Option<i32>,
    pub data: Option<T>,
}

// --- Offline license models ---

/// 服务端下发的离线许可证文件(POST /api/offline-licenses/issue 响应 data)。
#[derive(Debug, Clone, Deserialize)]
pub struct OfflineLicenseFile {
    /// 被签名的原文(LicensePayload 的 JSON 字符串,snake_case)
    pub payload: String,
    /// Ed25519 签名(hex 64 字节)
    pub signature: String,
    /// Ed25519 公钥(hex 32 字节)
    pub public_key: String,
}

/// 解析后的离线许可证 payload。
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct OfflineLicensePayload {
    pub license_id: String,
    pub user_id: String,
    pub software_id: String,
    pub fingerprint: String,
    pub max_devices: i32,
    pub expire_time: i64,
    pub issued_at: i64,
}