Skip to main content

basic/
basic.rs

1use ai_cortex_sdk::{CortexClient, CortexConfig};
2
3/// Demonstrates the PAT-based SDK workflow: browse software -> download -> list devices.
4///
5/// PAT 由用户在 web 端创建(POST /api/access-tokens)后填入。不再需要 app_key/app_secret 或登录。
6#[tokio::main]
7async fn main() {
8    let server_url = "http://localhost:40404";
9    let pat = std::env::args()
10        .nth(1)
11        .unwrap_or_else(|| "REPLACE_WITH_YOUR_PAT".to_string());
12
13    // software_id:SDK 以哪个软件身份心跳/下载,需与用户持有的有效 auth_record.software_id 一致。
14    let software_id = "00000000-0000-0000-0000-000000000000".to_string();
15
16    let config = CortexConfig::new(server_url, pat, software_id).with_timeout(30);
17    let client = CortexClient::new(config);
18
19    // --- List Software ---
20    println!("=== Software Store ===");
21    match client.list_software().await {
22        Ok(items) => {
23            if items.is_empty() {
24                println!("No software available.");
25            }
26            for (i, item) in items.iter().enumerate() {
27                let version_str = item
28                    .latest_version
29                    .as_ref()
30                    .map(|v| format!("v{} ({} bytes)", v.version, v.file_size))
31                    .unwrap_or_else(|| "no version".to_string());
32                println!(
33                    "{}. {} — {}  [{}]",
34                    i + 1,
35                    item.software.name,
36                    item.software.description,
37                    version_str
38                );
39            }
40
41            // --- Download first software's latest version ---
42            if let Some(item) = items.first() {
43                if let Some(version) = &item.latest_version {
44                    println!("\n=== Download ===");
45                    match client.download(&version.id).await {
46                        Ok(info) => {
47                            println!(
48                                "Download: v{}, size={} bytes\n  URL: {}",
49                                info.version, info.file_size, info.download_url
50                            );
51                        }
52                        Err(e) => eprintln!("Download failed: {e}"),
53                    }
54                }
55            }
56        }
57        Err(e) => eprintln!("List software failed: {e}"),
58    }
59
60    // --- List Devices (PAT 所属用户的设备;心跳在后台自动绑定) ---
61    println!("\n=== My Devices ===");
62    match client.list_devices().await {
63        Ok(devices) => {
64            if devices.is_empty() {
65                println!("No bound devices yet (heartbeat may not have fired yet).");
66            }
67            for d in devices {
68                println!(
69                    "- id={}, fingerprint={}, last_active={}",
70                    d.id, d.fingerprint, d.last_active_time
71                );
72            }
73        }
74        Err(e) => eprintln!("List devices failed: {e}"),
75    }
76}