oxios 0.4.0

Oxios Agent OS — Agent Operating System powered by oxi-sdk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
//! `oxios update` — fetch binary and/or web UI from GitHub Releases.
//!
//! Release assets:
//!   - Binary: `oxios-macos-arm64` (macOS ARM64) with `.sha256` checksum
//!   - Web UI:  `web-dist.zip` (contains index.html + assets/)
//!
//! Installation paths:
//!   - Binary: `~/.cargo/bin/oxios` (overwrites current binary)
//!   - Web UI: `~/.oxios/web/dist/` (extracted over existing)

use anyhow::{Context, Result};
use console::style;
use sha2::Digest;
use std::io::Write;
use std::path::PathBuf;

/// Update oxios binary and/or web UI from GitHub Releases.
pub async fn run_update(
    web_only: bool,
    binary_only: bool,
    version: Option<&str>,
    dry_run: bool,
    yes: bool,
) -> Result<()> {
    let current = env!("CARGO_PKG_VERSION");

    // ── Determine what to update ────────────────────────────────────────────
    let update_binary = !web_only;
    let update_web = !binary_only;

    println!();
    println!(
        "  {} {}",
        style("⬡ Oxios Updater").bold(),
        style(format!("v{}", current)).dim()
    );
    println!("  {}", "".repeat(52));
    println!("  Current version:  {}", current);
    println!(
        "  Update binary:    {}",
        if update_binary { "yes" } else { "no" }
    );
    println!(
        "  Update web UI:   {}",
        if update_web { "yes" } else { "no" }
    );
    if let Some(v) = version {
        println!("  Target version:  {}", v);
    } else {
        println!("  Target version:  latest");
    }
    println!();

    // ── Fetch release info from GitHub ──────────────────────────────────────
    let owner = "a7garden";
    let repo = "oxios";
    let tag = version.map(|v| format!("v{}", v));

    let api_url = match &tag {
        Some(t) => format!(
            "https://api.github.com/repos/{}/{}/releases/tags/{}",
            owner, repo, t
        ),
        None => format!(
            "https://api.github.com/repos/{}/{}/releases/latest",
            owner, repo
        ),
    };

    println!("  Fetching release info from GitHub...");
    let client = reqwest::Client::builder()
        .user_agent("oxios/0.3")
        .build()
        .context("failed to create HTTP client")?;

    let resp = client
        .get(&api_url)
        .send()
        .await
        .context("failed to fetch release info (check network/GITHUB_TOKEN)")?;

    if !resp.status().is_success() {
        anyhow::bail!(
            "GitHub API error {}: {}",
            resp.status(),
            resp.text().await.unwrap_or_default()
        );
    }

    let release: serde_json::Value = resp.json().await.context("failed to parse release JSON")?;

    let tag_name = release["tag_name"]
        .as_str()
        .unwrap_or("unknown")
        .trim_start_matches('v');
    let html_url = release["html_url"].as_str().unwrap_or("");
    let body = release["body"].as_str().unwrap_or("No release notes.");

    println!(
        "  Latest release:  {} ({})",
        style(tag_name).green().bold(),
        html_url
    );
    println!();

    if tag_name == current && !dry_run && !yes {
        println!(
            "  {} Already on latest version ({}).",
            style("").green(),
            current
        );
        println!("  Use `--version X.Y.Z` to force a specific version.");
        return Ok(());
    }

    // ── Parse assets ─────────────────────────────────────────────────────────
    let assets: Vec<(String, String, u64)> = release["assets"]
        .as_array()
        .unwrap_or(&vec![])
        .iter()
        .filter_map(|a| {
            Some((
                a["name"].as_str()?.to_string(),
                a["browser_download_url"].as_str()?.to_string(),
                a["size"].as_u64()?,
            ))
        })
        .collect();

    let web_asset = assets.iter().find(|(name, _, _)| name == "web-dist.zip");
    let binary_asset = assets
        .iter()
        .find(|(name, _, _)| name == "oxios-macos-arm64");
    let checksum_asset = assets
        .iter()
        .find(|(name, _, _)| name == "oxios-macos-arm64.sha256");

    println!("  Available assets:");
    for (name, _, size) in &assets {
        println!("    {}  ({} bytes)", name, format_size(*size));
    }
    println!();

    // ── Dry run ──────────────────────────────────────────────────────────────
    if dry_run {
        println!("  {} Dry run — no changes made.\n", style("").yellow());
        if update_web {
            if let Some((name, _, size)) = web_asset {
                println!("  Would download: {} ({})", name, format_size(*size));
                println!("  Would extract to: ~/.oxios/web/dist/");
            } else {
                println!("  {} web-dist.zip not found in release.", style("").red());
            }
        }
        if update_binary {
            if let Some((name, _, size)) = binary_asset {
                println!("  Would download: {} ({})", name, format_size(*size));
                println!("  Would install to: ~/.cargo/bin/oxios");
            } else {
                println!(
                    "  {} oxios-macos-arm64 not found in release.",
                    style("").red()
                );
            }
        }
        return Ok(());
    }

    // ── Confirmation ─────────────────────────────────────────────────────────
    if !yes {
        println!("  {} Release notes:\n", style("📋").cyan());
        for line in body.lines().take(10) {
            println!("    {}", line);
        }
        if body.lines().count() > 10 {
            println!("    ... ({} more lines)", body.lines().count() - 10);
        }
        println!();

        print!("  Continue with update? [y/N] ");
        std::io::stdout().flush().ok();
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).ok();
        if !input.trim().eq_ignore_ascii_case("y") {
            println!("  Update cancelled.");
            return Ok(());
        }
    }

    // ── Download and install web UI ────────────────────────────────────────
    if update_web {
        if let Some((name, url, size)) = web_asset {
            print!("  Downloading {}... ", name);
            std::io::stdout().flush().ok();

            let bytes = download_file(&client, url, *size).await?;
            println!("done ({}).", format_size(bytes.len() as u64));

            let dest_dir = dest_web_dir()?;
            std::fs::create_dir_all(&dest_dir)
                .context(format!("failed to create {:?}", dest_dir))?;

            print!("  Extracting to {:?}... ", dest_dir);
            std::io::stdout().flush().ok();

            let cursor = std::io::Cursor::new(bytes);
            let mut archive = zip::ZipArchive::new(cursor).context("invalid zip file")?;

            for i in 0..archive.len() {
                let mut file = archive.by_index(i)?;
                let out_path = dest_dir.join(file.name());

                if file.name().ends_with('/') {
                    std::fs::create_dir_all(&out_path)?;
                } else {
                    if let Some(p) = out_path.parent() {
                        std::fs::create_dir_all(p)?;
                    }
                    let mut out_file = std::fs::File::create(&out_path)?;
                    std::io::copy(&mut file, &mut out_file)?;
                }
            }
            println!("done.");
            println!(
                "  {} Web UI updated to {} in {:?}.",
                style("").green(),
                tag_name,
                dest_dir
            );
        } else {
            println!(
                "  {} web-dist.zip not found — skipping.",
                style("").yellow()
            );
        }
    }

    // ── Download and install binary ─────────────────────────────────────────
    if update_binary {
        if let Some((name, url, size)) = binary_asset {
            let expected_checksum = if let Some((cs_name, cs_url, _)) = checksum_asset {
                print!("  Verifying checksum from {}... ", cs_name);
                std::io::stdout().flush().ok();
                let cs_bytes = download_file(&client, cs_url, 256).await?;
                String::from_utf8_lossy(&cs_bytes)
                    .split_whitespace()
                    .next()
                    .unwrap_or("")
                    .to_string()
            } else {
                String::new()
            };

            print!("  Downloading {}... ", name);
            std::io::stdout().flush().ok();

            let bytes = download_file(&client, url, *size).await?;
            println!("done ({}).", format_size(bytes.len() as u64));

            if !expected_checksum.is_empty() {
                let digest = sha2::Sha256::digest(&bytes);
                let actual = format!("{:x}", digest);
                if actual != expected_checksum {
                    anyhow::bail!(
                        "Checksum mismatch!\n  Expected: {}\n  Actual:   {}",
                        expected_checksum,
                        actual
                    );
                }
                println!("  {} Checksum verified.", style("").green());
            }

            let dest = dest_binary_path()?;
            if let Some(p) = dest.parent() {
                std::fs::create_dir_all(p)?;
            }
            std::fs::write(&dest, &bytes)
                .context(format!("failed to write binary to {:?}", dest))?;

            #[cfg(unix)]
            {
                use std::os::unix::fs::PermissionsExt;
                let mut perms = std::fs::metadata(&dest)?.permissions();
                perms.set_mode(0o755);
                std::fs::set_permissions(&dest, perms)?;
            }

            println!(
                "  {} Binary updated to {} at {:?}",
                style("").green(),
                tag_name,
                dest
            );
            println!();
            println!(
                "  {} Run `{}` to restart with the new binary.",
                style("").cyan(),
                style("oxios restart").bold()
            );
        } else {
            println!(
                "  {} oxios-macos-arm64 not found — skipping.",
                style("").yellow()
            );
        }
    }

    println!();
    Ok(())
}

/// Show changelog / release notes for a given version (or latest).
pub async fn run_changelog(version: Option<&str>) -> Result<()> {
    let owner = "a7garden";
    let repo = "oxios";
    let api_url = match version {
        Some(v) => format!(
            "https://api.github.com/repos/{}/{}/releases/tags/v{}",
            owner, repo, v
        ),
        None => format!(
            "https://api.github.com/repos/{}/{}/releases/latest",
            owner, repo
        ),
    };

    let client = reqwest::Client::builder()
        .user_agent("oxios/0.3")
        .build()
        .context("failed to create HTTP client")?;

    let resp = client
        .get(&api_url)
        .send()
        .await
        .context("failed to fetch release info")?;

    if !resp.status().is_success() {
        anyhow::bail!("Release not found: {}", resp.status());
    }

    let release: serde_json::Value = resp.json().await.context("failed to parse release JSON")?;
    let tag = release["tag_name"]
        .as_str()
        .unwrap_or("?")
        .trim_start_matches('v');
    let body = release["body"].as_str().unwrap_or("(no release notes)");
    let date = release["published_at"].as_str().unwrap_or("?");

    println!();
    println!(
        "  {} v{}  ({})",
        style("⬡ Oxios").bold(),
        style(tag).green().bold(),
        date
    );
    println!("  {}", "".repeat(55));
    println!();
    println!("{}", body);
    Ok(())
}

// ── Helpers ───────────────────────────────────────────────────────────────────

fn dest_web_dir() -> Result<PathBuf> {
    let home = dirs::home_dir().context("cannot determine home directory")?;
    Ok(home.join(".oxios").join("web").join("dist"))
}

fn dest_binary_path() -> Result<PathBuf> {
    let home = dirs::home_dir().context("cannot determine home directory")?;
    Ok(home.join(".cargo").join("bin").join("oxios"))
}

async fn download_file(
    client: &reqwest::Client,
    url: &str,
    _expected_size: u64,
) -> Result<Vec<u8>> {
    let resp = client
        .get(url)
        .send()
        .await
        .context("download request failed")?;

    if !resp.status().is_success() {
        anyhow::bail!("Download failed: {}", resp.status());
    }

    let bytes = resp.bytes().await.context("failed to read response body")?;

    Ok(bytes.to_vec())
}

fn format_size(bytes: u64) -> String {
    if bytes < 1024 {
        format!("{} B", bytes)
    } else if bytes < 1024 * 1024 {
        format!("{:.1} KB", bytes as f64 / 1024.0)
    } else {
        format!("{:.1} MB", bytes as f64 / (1024.0 * 1024.0))
    }
}