sparrow-cli 0.7.0

A local-first Rust agent cockpit — route, run, replay, rewind
Documentation
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// ─── Migration paths from competitor tools (§4) ───────────────────────────────
//
// Sparrow import <tool> — zero-friction migration.
// Reads the competitor's config directory, extracts everything we understand,
// and writes the Sparrow equivalents. Never overwrites user data without asking.

use std::path::{Path, PathBuf};

use crate::onboarding::claude_compat::{self, ClaudeSettings};

// ─── Migration result ──────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct MigrationResult {
    pub tool: String,
    pub agents: usize,
    pub commands: usize,
    pub skills: usize,
    pub config_entries: usize,
    pub api_keys: usize,
    pub mcp_servers: usize,
    pub summary: Vec<String>,
}

impl MigrationResult {
    fn new(tool: &str) -> Self {
        MigrationResult {
            tool: tool.to_string(),
            agents: 0,
            commands: 0,
            skills: 0,
            config_entries: 0,
            api_keys: 0,
            mcp_servers: 0,
            summary: Vec::new(),
        }
    }

    fn note(&mut self, msg: &str) {
        self.summary.push(msg.to_string());
    }
}

// ─── Target directories ────────────────────────────────────────────────────────

fn sparrow_dir() -> PathBuf {
    dirs::config_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join("sparrow")
}

fn sparrow_agents_dir() -> PathBuf {
    sparrow_dir().join("agents")
}

fn sparrow_commands_dir() -> PathBuf {
    sparrow_dir().join("commands")
}

fn sparrow_config_file() -> PathBuf {
    sparrow_dir().join("config.toml")
}

// ─── Public API ────────────────────────────────────────────────────────────────

pub struct Migration;

impl Migration {
    // ── Claude Code ─────────────────────────────────────────────────────────

    /// Import from Claude Code.
    ///
    /// Reads:
    ///   - `~/.claude/CLAUDE.md`          → user-level Sparrow instruction
    ///   - `~/.claude/commands/*.md`      → Sparrow slash commands
    ///   - `~/.claude/agents/*.md`        → Sparrow SOUL agents
    ///   - `~/.claude/settings.json`      → Sparrow config (permissions, env)
    ///   - `<cwd>/.claude/CLAUDE.md`      → project-level instruction
    ///   - `~/.claude/mcp.json` or `.mcp.json` → Sparrow MCP servers
    pub fn import_claude_code(path: &Path) -> anyhow::Result<MigrationResult> {
        let mut result = MigrationResult::new("claude-code");
        let home = dirs::home_dir().unwrap_or_default();
        let cwd = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());

        // Discover everything Claude Code has on disk
        let imported = claude_compat::discover(&home, &cwd);

        // ── CLAUDE.md → memory / instruction ───────────────────────────────
        if let Some(user_md) = &imported.user_memory {
            write_sparrow_instruction("claude-user.md", user_md)?;
            result.note("Imported ~/.claude/CLAUDE.md → user instruction");
            result.config_entries += 1;
        }
        if let Some(proj_md) = &imported.project_memory {
            write_sparrow_instruction("claude-project.md", proj_md)?;
            result.note("Imported .claude/CLAUDE.md → project instruction");
            result.config_entries += 1;
        }

        // ── Commands → Sparrow slash commands ──────────────────────────────
        let cmd_dir = sparrow_commands_dir();
        std::fs::create_dir_all(&cmd_dir)?;
        for cmd in &imported.commands {
            let dest = cmd_dir.join(format!("{}.md", cmd.name));
            if !dest.exists() {
                std::fs::write(&dest, &cmd.body)?;
                result.commands += 1;
            }
        }
        if result.commands > 0 {
            result.note(&format!(
                "Imported {} slash commands → {}",
                result.commands,
                cmd_dir.display()
            ));
        }

        // ── Agents → Sparrow SOUL agents ───────────────────────────────────
        let agents_dir = sparrow_agents_dir();
        std::fs::create_dir_all(&agents_dir)?;
        for agent in &imported.agents {
            let dest = agents_dir.join(format!("{}.soul.toml", agent.name));
            if !dest.exists() {
                let soul = agent_body_to_soul(&agent.name, &agent.body);
                std::fs::write(&dest, &soul)?;
                result.agents += 1;
            }
        }
        if result.agents > 0 {
            result.note(&format!(
                "Imported {} agents → {}",
                result.agents,
                agents_dir.display()
            ));
        }

        // ── settings.json → Sparrow config ─────────────────────────────────
        if let Some(settings) = &imported.settings {
            let merged = merge_claude_settings(settings)?;
            if merged > 0 {
                result.config_entries += merged;
                result.note(&format!(
                    "Merged {} settings entries into {}",
                    merged,
                    sparrow_config_file().display()
                ));
            }
        }

        // ── MCP servers ────────────────────────────────────────────────────
        let mcp_sources = [home.join(".claude").join("mcp.json"), cwd.join(".mcp.json")];
        for mcp_path in &mcp_sources {
            if mcp_path.exists() {
                result.mcp_servers += import_mcp_servers(mcp_path)?;
            }
        }
        if result.mcp_servers > 0 {
            result.note(&format!("Imported {} MCP servers", result.mcp_servers));
        }

        // ── API keys from env ──────────────────────────────────────────────
        if let Some(settings) = &imported.settings {
            result.api_keys += extract_api_keys_from_settings(settings)?;
        }
        if result.api_keys > 0 {
            result.note(&format!(
                "Detected {} API keys in settings",
                result.api_keys
            ));
            result.note("Run `sparrow auth add <provider>` to register them securely.");
        }

        if result.summary.is_empty() {
            result.note("No Claude Code configuration found.");
        }

        Ok(result)
    }

    // ── Codex ──────────────────────────────────────────────────────────────

    /// Import from OpenAI Codex CLI.
    ///
    /// Reads:
    ///   - `~/.codex/config.json` / `codex.yaml`      → provider/model config
    ///   - `AGENTS.md`                                  → agent instructions
    pub fn import_codex(path: &Path) -> anyhow::Result<MigrationResult> {
        let mut result = MigrationResult::new("codex");
        let cwd = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
        let home = dirs::home_dir().unwrap_or_default();

        // AGENTS.md → instruction
        for md_path in &[cwd.join("AGENTS.md"), home.join(".codex").join("AGENTS.md")] {
            if md_path.exists() {
                let content = std::fs::read_to_string(md_path)?;
                write_sparrow_instruction("codex-agents.md", &content)?;
                result.note("Imported AGENTS.md → instruction");
                result.config_entries += 1;
                break;
            }
        }

        // codex.yaml / codex.yml / ~/.codex/config.json → provider config
        let config_paths = [
            cwd.join("codex.yaml"),
            cwd.join("codex.yml"),
            home.join(".codex").join("config.json"),
        ];
        for cfg_path in &config_paths {
            if cfg_path.exists() {
                result.config_entries += import_codex_config(cfg_path)?;
                result.note(&format!(
                    "Imported config from {}",
                    cfg_path.file_name().unwrap_or_default().to_string_lossy()
                ));
                break;
            }
        }

        if result.summary.is_empty() {
            result.note("No Codex configuration found.");
        }

        Ok(result)
    }

    // ── OpenCode ───────────────────────────────────────────────────────────

    /// Import from OpenCode.
    ///
    /// Reads:
    ///   - `opencode.json`              → provider/models/routing config
    ///   - `~/.config/opencode/`        → user-level config
    pub fn import_opencode(path: &Path) -> anyhow::Result<MigrationResult> {
        let mut result = MigrationResult::new("opencode");
        let cwd = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
        let home = dirs::home_dir().unwrap_or_default();

        let config_paths = [
            cwd.join("opencode.json"),
            home.join(".config").join("opencode").join("config.json"),
        ];
        for cfg_path in &config_paths {
            if cfg_path.exists() {
                result.config_entries += import_opencode_config(cfg_path)?;
                result.note(&format!("Imported config from {}", cfg_path.display()));
                break;
            }
        }

        if result.summary.is_empty() {
            result.note("No OpenCode configuration found.");
        }

        Ok(result)
    }

    // ── OpenClaw (existing) ────────────────────────────────────────────────

    pub fn import_openclaw(path: &PathBuf) -> anyhow::Result<MigrationResult> {
        let mut result = MigrationResult::new("openclaw");

        let agents_dir = path.join("agents");
        if agents_dir.exists() {
            result.agents = std::fs::read_dir(&agents_dir)?
                .filter_map(|e| e.ok())
                .filter(|e| e.path().extension().map(|x| x == "md").unwrap_or(false))
                .count();
        }

        let skills_dir = path.join("skills");
        if skills_dir.exists() {
            result.skills = std::fs::read_dir(&skills_dir)?
                .filter_map(|e| e.ok())
                .filter(|e| e.path().is_dir())
                .count();
        }

        let cron_file = path.join("cron.json");
        if cron_file.exists() {
            if let Ok(content) = std::fs::read_to_string(&cron_file) {
                if let Ok(jobs) = serde_json::from_str::<Vec<serde_json::Value>>(&content) {
                    result.config_entries += jobs.len();
                }
            }
        }

        if result.agents > 0 {
            result.note(&format!("Found {} agents", result.agents));
        }
        if result.skills > 0 {
            result.note(&format!("Found {} skills", result.skills));
        }

        Ok(result)
    }

    // ── Hermes ─────────────────────────────────────────────────────────────

    pub fn import_hermes(path: &PathBuf) -> anyhow::Result<MigrationResult> {
        let mut result = MigrationResult::new("hermes");

        let agents_dir = path.join("agents");
        if agents_dir.exists() {
            result.agents = std::fs::read_dir(&agents_dir)?
                .filter_map(|e| e.ok())
                .filter(|e| e.path().extension().map(|x| x == "md").unwrap_or(false))
                .count();
        }

        let skills_dir = path.join("skills");
        if skills_dir.exists() {
            result.skills = std::fs::read_dir(&skills_dir)?
                .filter_map(|e| e.ok())
                .filter(|e| e.path().is_dir())
                .count();
        }

        Ok(result)
    }

    // ── Auto-detect ────────────────────────────────────────────────────────

    pub fn detect_installed() -> Vec<String> {
        let mut found = Vec::new();
        let home = dirs::home_dir().unwrap_or_default();

        let tools: Vec<(&str, PathBuf)> = vec![
            ("claude-code", home.join(".claude")),
            ("codex", home.join(".codex")),
            ("opencode", home.join(".config").join("opencode")),
            ("openclaw", home.join(".openclaw")),
            ("hermes", home.join(".hermes")),
        ];

        for (name, path) in tools {
            if path.exists() {
                found.push(name.to_string());
            }
        }
        found
    }
}

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

fn write_sparrow_instruction(name: &str, content: &str) -> anyhow::Result<()> {
    let dir = sparrow_dir().join("instructions");
    std::fs::create_dir_all(&dir)?;
    let dest = dir.join(name);
    if !dest.exists() {
        std::fs::write(&dest, content)?;
    }
    Ok(())
}

fn agent_body_to_soul(name: &str, body: &str) -> String {
    // Try YAML frontmatter first (Claude Code agent format)
    if let Some(rest) = body.strip_prefix("---") {
        if let Some(end) = rest.find("---") {
            let frontmatter = &rest[..end];
            let content = &rest[end + 3..].trim();
            return format!(
                "# Imported from Claude Code\n\
                 name = \"{}\"\n\
                 {}\n\
                 personality = \"\"\"\n{}\n\"\"\"\n",
                name, frontmatter, content
            );
        }
    }
    // Plain text: entire body is the personality
    format!(
        "# Imported from Claude Code\n\
         name = \"{}\"\n\
         role = \"assistant\"\n\
         personality = \"\"\"\n{}\n\"\"\"\n",
        name,
        body.lines().take(80).collect::<Vec<_>>().join("\n")
    )
}

fn merge_claude_settings(settings: &ClaudeSettings) -> anyhow::Result<usize> {
    let mut count = 0;

    // Only write a hint file; actual config merge is done interactively
    // to avoid breaking the user's existing Sparrow config.
    let hint_dir = sparrow_dir().join("imports");
    std::fs::create_dir_all(&hint_dir)?;
    let hint_path = hint_dir.join("claude-settings.json");
    if !hint_path.exists() {
        let pretty = serde_json::to_string_pretty(settings)?;
        std::fs::write(&hint_path, &pretty)?;
        count += 1;
    }

    // Also check for env vars in settings
    if let Some(env) = &settings.env {
        let env_path = hint_dir.join("claude-env.txt");
        if !env_path.exists() {
            if let Some(obj) = env.as_object() {
                let mut lines = Vec::new();
                for (k, v) in obj {
                    if let Some(val) = v.as_str() {
                        lines.push(format!("{}={}", k, val));
                    }
                }
                if !lines.is_empty() {
                    std::fs::write(&env_path, lines.join("\n") + "\n")?;
                    count += 1;
                }
            }
        }
    }

    Ok(count)
}

fn extract_api_keys_from_settings(settings: &ClaudeSettings) -> anyhow::Result<usize> {
    let env = match &settings.env {
        Some(e) => e,
        None => return Ok(0),
    };

    let obj = match env.as_object() {
        Some(o) => o,
        None => return Ok(0),
    };

    let key_patterns = [
        "ANTHROPIC_API_KEY",
        "OPENAI_API_KEY",
        "GROQ_API_KEY",
        "OPENROUTER_API_KEY",
        "GOOGLE_API_KEY",
        "NVIDIA_API_KEY",
        "DEEPSEEK_API_KEY",
    ];

    let mut found = 0;
    for pattern in &key_patterns {
        if obj.contains_key(*pattern) {
            found += 1;
        }
    }

    Ok(found)
}

fn import_mcp_servers(mcp_path: &Path) -> anyhow::Result<usize> {
    let content = std::fs::read_to_string(mcp_path)?;
    let config: serde_json::Value = serde_json::from_str(&content)?;

    let mcp_dir = sparrow_dir().join("mcp");
    std::fs::create_dir_all(&mcp_dir)?;

    let dest = mcp_dir.join(
        mcp_path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .replace(".json", "-imported.json"),
    );

    if !dest.exists() {
        let pretty = serde_json::to_string_pretty(&config)?;
        std::fs::write(&dest, &pretty)?;
    }

    // Count MCP servers
    let count = config
        .get("mcpServers")
        .and_then(|s| s.as_object())
        .map(|o| o.len())
        .unwrap_or(0);

    Ok(count)
}

fn import_codex_config(path: &Path) -> anyhow::Result<usize> {
    let content = std::fs::read_to_string(path)?;
    let config: serde_json::Value = serde_json::from_str(&content)?;

    // Extract model/provider info
    let import_dir = sparrow_dir().join("imports");
    std::fs::create_dir_all(&import_dir)?;

    let dest = import_dir.join("codex-config.json");
    if !dest.exists() {
        let pretty = serde_json::to_string_pretty(&config)?;
        std::fs::write(&dest, &pretty)?;
    }

    let entries = config.as_object().map(|o| o.len()).unwrap_or(0);

    Ok(entries)
}

fn import_opencode_config(path: &Path) -> anyhow::Result<usize> {
    let content = std::fs::read_to_string(path)?;
    let config: serde_json::Value = serde_json::from_str(&content)?;

    let import_dir = sparrow_dir().join("imports");
    std::fs::create_dir_all(&import_dir)?;

    let dest = import_dir.join("opencode-config.json");
    if !dest.exists() {
        let pretty = serde_json::to_string_pretty(&config)?;
        std::fs::write(&dest, &pretty)?;
    }

    let entries = config.as_object().map(|o| o.len()).unwrap_or(0);

    Ok(entries)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_agent_body_to_soul_with_frontmatter() {
        let body = "---\nname: planner\nrole: architect\n---\nYou plan carefully.";
        let soul = agent_body_to_soul("planner", body);
        assert!(soul.contains("name = \"planner\""));
        assert!(soul.contains("role: architect"));
        assert!(soul.contains("You plan carefully."));
    }

    #[test]
    fn test_agent_body_to_soul_plain_text() {
        let body = "Be concise. Return evidence.";
        let soul = agent_body_to_soul("helper", body);
        assert!(soul.contains("name = \"helper\""));
        assert!(soul.contains("role = \"assistant\""));
        assert!(soul.contains("Be concise."));
    }

    #[test]
    fn test_detect_installed_returns_vec() {
        let found = Migration::detect_installed();
        // May be empty on CI, but must not panic
        let _ = found.len();
    }

    #[test]
    fn test_migration_result_new() {
        let r = MigrationResult::new("test-tool");
        assert_eq!(r.tool, "test-tool");
        assert_eq!(r.agents, 0);
        assert!(r.summary.is_empty());
    }
}