cargo-ai 0.3.0

Build lightweight AI agents with Cargo. Powered by Rust. Declared in JSON.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
//! Shared helpers for account command modules.
use crate::config::adder::set_account_tokens;
use crate::config::loader::load_config;
use crate::config::setup::config_path;
use crate::credentials::store;
use crate::infra_api;

/// Canonical Cargo-AI API base URL used by account command flows.
pub const INFRA_BASE_URL: &str = "https://api.cargo-ai.org";

/// In-memory account auth tokens loaded from local credential storage.
#[derive(Debug, Clone)]
pub struct AccountAuth {
    pub access_token: String,
    pub refresh_token: Option<String>,
}

/// Failure modes when attempting to refresh an expired access token.
#[derive(Debug)]
pub enum RefreshAccessError {
    MissingRefreshToken,
    RequestFailed(String),
    MissingRefreshedToken(serde_json::Value),
}

/// Loads the account access and refresh tokens from local credential storage with
/// user-facing validation errors.
pub fn load_account_auth() -> Result<AccountAuth, String> {
    let cfg = load_config().ok_or_else(|| {
        format!(
            "❌ No local config file found at '{}'. Run `cargo ai account register <email>` on this machine, or copy your config from another machine.",
            config_path().display()
        )
    })?;

    let acct = cfg.account.as_ref().ok_or_else(|| {
        "❌ No account found in config. You must confirm your account first.".to_string()
    })?;

    if let Some(account_tokens) = store::load_account_tokens()
        .map_err(|error| format!("❌ Failed to load account credentials: {error}"))?
    {
        return Ok(AccountAuth {
            access_token: account_tokens.access_token,
            refresh_token: account_tokens.refresh_token,
        });
    }

    // Backward-compatible fallback for legacy configs when migration has not run yet.
    let access_token = acct.access_token.as_ref().cloned().ok_or_else(|| {
        "❌ No access token found in credentials store or legacy config. Run `cargo ai account confirm <code>` first."
            .to_string()
    })?;

    Ok(AccountAuth {
        access_token,
        refresh_token: acct.refresh_token.clone(),
    })
}

/// Refreshes an expired access token by calling account status with refresh
/// token support and returns the retry token + optional expiry.
pub async fn refresh_access_token_for_retry(
    access_token: &str,
    refresh_token: Option<&str>,
) -> Result<(String, Option<i32>), RefreshAccessError> {
    let rt = refresh_token.ok_or(RefreshAccessError::MissingRefreshToken)?;

    let refresh_response =
        infra_api::account::status::fetch_status(INFRA_BASE_URL, access_token, Some(rt))
            .await
            .map_err(|e| RefreshAccessError::RequestFailed(format!("{e:?}")))?;

    let refreshed_access_token = refresh_response
        .get("session")
        .and_then(|s| s.get("access_token"))
        .and_then(|v| v.as_str())
        .filter(|s| !s.is_empty())
        .map(|s| s.to_string());

    let refreshed_expires_in: Option<i32> = refresh_response
        .get("session")
        .and_then(|s| s.get("expires_in_seconds"))
        .and_then(|v| v.as_i64())
        .and_then(|n| i32::try_from(n).ok());

    match refreshed_access_token {
        Some(token) => Ok((token, refreshed_expires_in)),
        None => Err(RefreshAccessError::MissingRefreshedToken(refresh_response)),
    }
}

/// Persists refreshed access token metadata when expiry is provided.
pub fn persist_refreshed_access_token(
    refreshed_access_token: &str,
    refresh_token: &str,
    refreshed_expires_in: Option<i32>,
) {
    if let Some(expires_in) = refreshed_expires_in {
        if let Err(e) = set_account_tokens(
            refreshed_access_token.to_string(),
            refresh_token.to_string(),
            expires_in,
        ) {
            eprintln!("⚠️ Failed to update account tokens in credential store: {e}");
        }
    }
}

/// Applies `--limit` output truncation to successful agents-list responses.
pub fn apply_agents_list_display_limit(
    response: &mut serde_json::Value,
    display_limit: Option<usize>,
) -> Option<(usize, usize)> {
    let limit = display_limit?;
    let response_type = response.get("type").and_then(|v| v.as_str());
    if response_type != Some("account_agents_list_succeeded") {
        return None;
    }

    let agents = response.get_mut("agents").and_then(|v| v.as_array_mut())?;
    let total = agents.len();
    if total <= limit {
        return None;
    }

    agents.truncate(limit);
    let shown = agents.len();

    if let Some(ui) = response.get_mut("ui") {
        if let Some(summary) = ui.get_mut("summary") {
            *summary = serde_json::json!(format!("Showing {shown} of {total} agents."));
        }

        if let Some(sections) = ui.get_mut("sections").and_then(|v| v.as_array_mut()) {
            for section in sections.iter_mut() {
                let is_list_section = section
                    .get("type")
                    .and_then(|v| v.as_str())
                    .map(|v| v == "list")
                    .unwrap_or(false);
                let is_kv_section = section
                    .get("type")
                    .and_then(|v| v.as_str())
                    .map(|v| v == "kv")
                    .unwrap_or(false);

                if is_list_section {
                    if let Some(items) = section.get_mut("items").and_then(|v| v.as_array_mut()) {
                        items.truncate(limit);
                    }
                }

                if is_kv_section {
                    if let Some(items) = section.get_mut("items").and_then(|v| v.as_array_mut()) {
                        for item in items.iter_mut() {
                            let is_count = item
                                .get("label")
                                .and_then(|v| v.as_str())
                                .map(|label| label.eq_ignore_ascii_case("count"))
                                .unwrap_or(false);

                            if is_count {
                                item["value"] = serde_json::json!(shown);
                            }
                        }
                    }
                }
            }

            sections.push(serde_json::json!({
                "type": "notice",
                "message": format!(
                    "Showing {shown} of {total} agents. Use --limit <N> or --all to adjust output."
                )
            }));
        }
    }

    Some((shown, total))
}

/// Applies `--limit` output truncation to successful projects-list responses.
pub fn apply_projects_list_display_limit(
    response: &mut serde_json::Value,
    display_limit: Option<usize>,
) -> Option<(usize, usize)> {
    let limit = display_limit?;
    let response_type = response.get("type").and_then(|v| v.as_str());
    if response_type != Some("account_projects_list_succeeded") {
        return None;
    }

    let projects = response
        .get_mut("projects")
        .and_then(|v| v.as_array_mut())?;
    let total = projects.len();
    if total <= limit {
        return None;
    }

    projects.truncate(limit);
    let shown = projects.len();

    if let Some(ui) = response.get_mut("ui") {
        if let Some(summary) = ui.get_mut("summary") {
            *summary = serde_json::json!(format!("Showing {shown} of {total} projects."));
        }

        if let Some(sections) = ui.get_mut("sections").and_then(|v| v.as_array_mut()) {
            for section in sections.iter_mut() {
                let is_list_section = section
                    .get("type")
                    .and_then(|v| v.as_str())
                    .map(|v| v == "list")
                    .unwrap_or(false);
                let is_kv_section = section
                    .get("type")
                    .and_then(|v| v.as_str())
                    .map(|v| v == "kv")
                    .unwrap_or(false);

                if is_list_section {
                    if let Some(items) = section.get_mut("items").and_then(|v| v.as_array_mut()) {
                        items.truncate(limit);
                    }
                }

                if is_kv_section {
                    if let Some(items) = section.get_mut("items").and_then(|v| v.as_array_mut()) {
                        for item in items.iter_mut() {
                            let is_count = item
                                .get("label")
                                .and_then(|v| v.as_str())
                                .map(|label| label.eq_ignore_ascii_case("count"))
                                .unwrap_or(false);

                            if is_count {
                                item["value"] = serde_json::json!(shown);
                            }
                        }
                    }
                }
            }

            sections.push(serde_json::json!({
                "type": "notice",
                "message": format!(
                    "Showing {shown} of {total} projects. Use --limit <N> or --all to adjust output."
                )
            }));
        }
    }

    Some((shown, total))
}

/// Fetches status for register-guard checks and retries once with refresh token
/// when the initial access token is expired.
pub async fn fetch_status_for_register_guard(
    access_token: &str,
    refresh_token: Option<&str>,
) -> serde_json::Value {
    let first_response =
        match infra_api::account::status::fetch_status(INFRA_BASE_URL, access_token, None).await {
            Ok(v) => v,
            Err(e) => {
                eprintln!("⚠️ Could not validate local session before register: {e:?}");
                return serde_json::Value::Null;
            }
        };

    let is_expired_error = first_response
        .get("type")
        .and_then(|v| v.as_str())
        .map(|t| t == "access_token_expired")
        .unwrap_or(false);

    if !is_expired_error {
        return first_response;
    }

    let rt = match refresh_token {
        Some(rt) => rt,
        None => return first_response,
    };

    match infra_api::account::status::fetch_status(INFRA_BASE_URL, access_token, Some(rt)).await {
        Ok(v) => v,
        Err(e) => {
            eprintln!("⚠️ Could not refresh local session before register: {e:?}");
            serde_json::Value::Null
        }
    }
}

/// Extracts the account email from a successful status response payload.
pub fn extract_status_account_email(status_response: &serde_json::Value) -> Option<String> {
    let is_success = status_response
        .get("status")
        .and_then(|v| v.as_str())
        .map(|s| s.eq_ignore_ascii_case("success"))
        .unwrap_or(false);

    if !is_success {
        return None;
    }

    status_response
        .get("account")
        .and_then(|v| v.get("email"))
        .and_then(|v| v.as_str())
        .map(|s| s.to_string())
}

#[cfg(test)]
mod tests {
    use super::{
        apply_agents_list_display_limit, apply_projects_list_display_limit,
        extract_status_account_email,
    };
    use serde_json::json;

    fn sample_agents_list_response() -> serde_json::Value {
        json!({
            "status": "success",
            "type": "account_agents_list_succeeded",
            "agents": [
                { "name": "agent-1" },
                { "name": "agent-2" },
                { "name": "agent-3" }
            ],
            "ui": {
                "summary": "Showing all agents.",
                "sections": [
                    {
                        "type": "list",
                        "items": [
                            { "name": "agent-1" },
                            { "name": "agent-2" },
                            { "name": "agent-3" }
                        ]
                    },
                    {
                        "type": "kv",
                        "items": [
                            { "label": "count", "value": 3 },
                            { "label": "owner", "value": "demo" }
                        ]
                    }
                ]
            }
        })
    }

    fn sample_projects_list_response() -> serde_json::Value {
        json!({
            "status": "success",
            "type": "account_projects_list_succeeded",
            "projects": [
                { "name": "project-1" },
                { "name": "project-2" },
                { "name": "project-3" }
            ],
            "ui": {
                "summary": "Showing all projects.",
                "sections": [
                    {
                        "type": "list",
                        "items": [
                            { "name": "project-1" },
                            { "name": "project-2" },
                            { "name": "project-3" }
                        ]
                    },
                    {
                        "type": "kv",
                        "items": [
                            { "label": "count", "value": 3 },
                            { "label": "owner", "value": "demo" }
                        ]
                    }
                ]
            }
        })
    }

    #[test]
    fn applies_display_limit_and_keeps_response_shape_consistent() {
        let mut response = sample_agents_list_response();
        let result = apply_agents_list_display_limit(&mut response, Some(2));

        assert_eq!(result, Some((2, 3)));
        assert_eq!(
            response
                .get("agents")
                .and_then(|v| v.as_array())
                .map(Vec::len),
            Some(2)
        );
        assert_eq!(
            response
                .get("ui")
                .and_then(|v| v.get("summary"))
                .and_then(|v| v.as_str()),
            Some("Showing 2 of 3 agents.")
        );

        let sections = response
            .get("ui")
            .and_then(|v| v.get("sections"))
            .and_then(|v| v.as_array())
            .expect("ui.sections should be present");

        let list_len = sections
            .iter()
            .find(|section| section.get("type").and_then(|v| v.as_str()) == Some("list"))
            .and_then(|section| section.get("items"))
            .and_then(|items| items.as_array())
            .map(Vec::len);
        assert_eq!(list_len, Some(2));

        let kv_count_value = sections
            .iter()
            .find(|section| section.get("type").and_then(|v| v.as_str()) == Some("kv"))
            .and_then(|section| section.get("items"))
            .and_then(|items| items.as_array())
            .and_then(|items| {
                items.iter().find(|item| {
                    item.get("label")
                        .and_then(|v| v.as_str())
                        .map(|label| label.eq_ignore_ascii_case("count"))
                        .unwrap_or(false)
                })
            })
            .and_then(|item| item.get("value"))
            .and_then(|v| v.as_i64());
        assert_eq!(kv_count_value, Some(2));

        let has_notice = sections.iter().any(|section| {
            section.get("type").and_then(|v| v.as_str()) == Some("notice")
                && section
                    .get("message")
                    .and_then(|v| v.as_str())
                    .map(|m| m.contains("Showing 2 of 3 agents"))
                    .unwrap_or(false)
        });
        assert!(has_notice);
    }

    #[test]
    fn applies_projects_display_limit_and_keeps_response_shape_consistent() {
        let mut response = sample_projects_list_response();

        let truncation = apply_projects_list_display_limit(&mut response, Some(2));

        assert_eq!(truncation, Some((2, 3)));
        assert_eq!(response["projects"].as_array().map(Vec::len), Some(2));
        assert_eq!(response["ui"]["summary"], "Showing 2 of 3 projects.");
        assert_eq!(
            response["ui"]["sections"][0]["items"]
                .as_array()
                .map(Vec::len),
            Some(2)
        );
        assert_eq!(response["ui"]["sections"][1]["items"][0]["value"], 2);
    }

    #[test]
    fn does_not_apply_limit_for_non_success_agents_list_shape() {
        let mut response = json!({
            "status": "error",
            "type": "account_agents_list_failed",
            "agents": [{ "name": "agent-1" }]
        });
        let original = response.clone();
        let result = apply_agents_list_display_limit(&mut response, Some(1));

        assert_eq!(result, None);
        assert_eq!(response, original);
    }

    #[test]
    fn extracts_status_email_on_success_shape() {
        let response = json!({
            "status": "success",
            "account": {
                "email": "jp@example.com"
            }
        });

        assert_eq!(
            extract_status_account_email(&response),
            Some("jp@example.com".to_string())
        );
    }

    #[test]
    fn returns_none_for_non_success_status_shape() {
        let response = json!({
            "status": "error",
            "account": {
                "email": "jp@example.com"
            }
        });

        assert_eq!(extract_status_account_email(&response), None);
    }
}