Skip to main content

ai_cortex_sdk/
config.rs

1use serde::Deserialize;
2
3/// SDK 配置。
4///
5/// 鉴权改为用户 PAT(`Authorization: Bearer actx_pat_...`),不再使用 app_key/app_secret HMAC。
6/// PAT 由用户在 web 端创建后填入配置。
7#[derive(Debug, Clone, Deserialize)]
8pub struct CortexConfig {
9    pub server_url: String,
10    /// 用户个人访问令牌(PAT),形如 `actx_pat_...`。
11    pub pat: String,
12    /// SDK 以哪个软件身份心跳/下载(设备按该软件的许可证 max_devices 校验上限)。
13    pub software_id: String,
14    #[serde(default = "default_timeout")]
15    pub timeout: u64,
16    /// 心跳上报间隔(秒)。0 = 关闭心跳;默认 60s。
17    #[serde(default = "default_heartbeat_interval")]
18    pub heartbeat_interval: u64,
19    /// 可选:钉扎的软件公钥(hex)。设置后,拉取到的离线许可证 public_key 必须与之匹配才信任。
20    /// 不设置则信任服务端在 issue 响应中下发的 public_key(依赖 TLS)。
21    #[serde(default)]
22    pub software_public_key: Option<String>,
23}
24
25fn default_timeout() -> u64 {
26    30
27}
28
29fn default_heartbeat_interval() -> u64 {
30    60
31}
32
33impl CortexConfig {
34    pub fn new(
35        server_url: impl Into<String>,
36        pat: impl Into<String>,
37        software_id: impl Into<String>,
38    ) -> Self {
39        Self {
40            server_url: server_url.into(),
41            pat: pat.into(),
42            software_id: software_id.into(),
43            timeout: 30,
44            heartbeat_interval: 60,
45            software_public_key: None,
46        }
47    }
48
49    pub fn with_timeout(mut self, timeout: u64) -> Self {
50        self.timeout = timeout;
51        self
52    }
53
54    /// 钉扎软件公钥(hex)。设置后离线许可证验签时要求下发公钥与之严格匹配。
55    pub fn with_software_public_key(mut self, pk: impl Into<String>) -> Self {
56        self.software_public_key = Some(pk.into());
57        self
58    }
59
60    /// 设置 SDK 心跳/下载所代表的软件 id。
61    pub fn with_software_id(mut self, software_id: impl Into<String>) -> Self {
62        self.software_id = software_id.into();
63        self
64    }
65
66    /// 设置心跳上报间隔(秒)。设为 0 可关闭心跳。
67    pub fn with_heartbeat_interval(mut self, secs: u64) -> Self {
68        self.heartbeat_interval = secs;
69        self
70    }
71
72    /// 关闭心跳上报。
73    pub fn without_heartbeat(mut self) -> Self {
74        self.heartbeat_interval = 0;
75        self
76    }
77}