kowalski-cli 1.2.0

Kowalski CLI Interface: A Rust-based agent for interacting with Ollama models
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
use clap::Parser;
use kowalski_core::agent::Agent;
use kowalski_core::config::Config;
use kowalski_core::tools::ToolCall;
use log::info;
use serde_json::json;
use std::collections::HashMap;
use std::fs;
use std::io::{self, Write};
use std::sync::Arc;
use tokio::sync::RwLock;

use kowalski_core::memory::consolidation::{Consolidator, MemoryWeaver};

#[derive(Parser, Debug)]
#[clap(
    author,
    version,
    about = "Kowalski CLI — agents, memory, and MCP operators.",
    long_about = "Operators: `run`, `config check`, `db migrate`, `doctor`, `mcp ping`, `mcp tools`, `federation ping-notify` (with `--features postgres`) (see --help on each)."
)]
struct Cli {
    #[clap(subcommand)]
    command: Option<Commands>,

    /// Start in interactive mode
    #[clap(short, long)]
    interactive: bool,

    /// Path to a configuration file (.toml) to load an agent
    #[clap(short, long)]
    config: Option<String>,
}

#[derive(Parser, Debug)]
enum Commands {
    /// Create a new agent
    Create {
        /// Agent type (web, academic, code, data)
        agent_type: String,
        /// Optional system prompt
        #[clap(short, long)]
        prompt: Option<String>,
        /// Optional temperature
        #[clap(short, long)]
        temperature: Option<f32>,
        /// Optional agent name
        #[clap(short, long)]
        name: Option<String>,
        /// Optional configuration file
        #[clap(short, long)]
        config: Option<String>,
    },
    /// Chat with an agent
    Chat {
        /// Agent name or type
        agent: String,
        /// Optional system prompt
        #[clap(short, long)]
        prompt: Option<String>,
        /// Optional temperature
        #[clap(short, long)]
        temperature: Option<f32>,
        /// Optional model
        #[clap(short, long)]
        model: Option<String>,
    },
    /// List available agent types
    List,
    /// List active agents
    Agents,
    /// Consolidate memory - move from episodic history into semantic memory
    Consolidate {
        #[clap(long)]
        delete: bool,
    },
    /// Model Context Protocol helpers
    Mcp {
        #[clap(subcommand)]
        command: McpCommands,
    },
    /// Validate configuration TOML (and full Kowalski `Config` when possible)
    Config {
        #[clap(subcommand)]
        command: ConfigCommands,
    },
    /// Run SQL migrations for `sqlite:` or `postgres://` URLs
    Db {
        #[clap(subcommand)]
        command: DbCommands,
    },
    /// Print versions and probe local Ollama
    Doctor {
        /// Ollama base URL (default http://127.0.0.1:11434)
        #[clap(long)]
        ollama_url: Option<String>,
    },
    /// Interactive orchestrator REPL (`TemplateAgent` + `chat_with_tools`)
    Run {
        /// Config TOML (default ./config.toml)
        #[clap(short, long)]
        config: Option<String>,
    },
    /// Federation operators (Postgres `NOTIFY` smoke test when built with `--features postgres`)
    Federation {
        #[clap(subcommand)]
        command: FederationCommands,
    },
    /// Run extension commands discovered from PATH or local extension directory
    Extension {
        #[clap(subcommand)]
        command: ExtensionCommands,
    },
    /// Markdown-defined app agents (main + sub-agents in .md files)
    AgentApp {
        #[clap(subcommand)]
        command: AgentAppCommands,
    },
}

#[derive(Parser, Debug)]
enum ConfigCommands {
    /// Check that TOML parses and optionally matches core `Config`
    Check {
        /// Path to config.toml (default: config.toml)
        #[clap(default_value = "config.toml")]
        path: String,
    },
}

#[derive(Parser, Debug)]
enum DbCommands {
    /// Apply embedded migrations to the database URL (`memory.database_url` or `--url`)
    Migrate {
        /// SQL store URL (sqlite:… or postgres://…)
        #[clap(long)]
        url: Option<String>,
        /// Read memory.database_url from this TOML (ignored if --url is set)
        #[clap(short, long)]
        config: Option<String>,
    },
}

#[derive(Parser, Debug)]
enum FederationCommands {
    /// Send a Ping ACL via `pg_notify` on `kowalski_federation` (needs `memory.database_url` in config)
    PingNotify {
        /// Config TOML (default ./config.toml)
        #[clap(short, long)]
        config: Option<String>,
    },
}

#[derive(Parser, Debug)]
enum McpCommands {
    /// Run initialize + tools/list against each server in [mcp] (from config TOML)
    Ping {
        /// TOML file containing an [mcp] section (default: ./config.toml)
        #[clap(short, long)]
        config: Option<String>,
    },
    /// List tool names and descriptions per MCP server (same config as ping)
    Tools {
        /// TOML file containing an [mcp] section (default: ./config.toml)
        #[clap(short, long)]
        config: Option<String>,
    },
}

#[derive(Parser, Debug)]
enum ExtensionCommands {
    /// List available extensions (PATH `kowalski-ext-*` and local `.kowalski/extensions/*`)
    List,
    /// Run an extension by name, forwarding trailing arguments as-is
    Run {
        /// Extension name (for binary `kowalski-ext-<name>`)
        name: String,
        /// Arguments forwarded to extension command
        #[clap(trailing_var_arg = true, allow_hyphen_values = true)]
        args: Vec<String>,
    },
}

#[derive(Parser, Debug)]
enum AgentAppCommands {
    /// List horde pipeline and agents
    List {
        /// App dir (`horde.md` + `agents/`). Env `KOWALSKI_AGENT_APP_ROOT`, else dev default `examples/knowledge-compiler`.
        #[clap(short, long)]
        path: Option<String>,
    },
    /// Validate `horde.md` + `agents/*.md` (pipeline vs agent files)
    Validate {
        /// App dir (`horde.md` + `agents/`). Env `KOWALSKI_AGENT_APP_ROOT`, else dev default `examples/knowledge-compiler`.
        #[clap(short, long)]
        path: Option<String>,
    },
    /// Run pipeline sequentially (same steps as `horde.md` pipeline)
    Run {
        /// Source URL or text
        source: String,
        /// Optional question for query phase
        #[clap(short, long)]
        question: Option<String>,
        /// App dir (`horde.md` + `agents/`). Env `KOWALSKI_AGENT_APP_ROOT`, else dev default `examples/knowledge-compiler`.
        #[clap(short, long)]
        path: Option<String>,
        /// Kowalski API base URL (default: http://127.0.0.1:3456)
        #[clap(long)]
        api: Option<String>,
    },
    /// Delegate one orchestrated run via federation `/api/federation/delegate`
    Delegate {
        /// Required capability selector (e.g. `kc.run`)
        capability: String,
        /// Source URL or text
        source: String,
        /// Optional question for query phase
        #[clap(short, long)]
        question: Option<String>,
        /// Kowalski API base URL
        #[clap(long)]
        api: Option<String>,
    },
    /// Run a federated worker: either whole-app delegations (no `--role`) or a single pipeline
    /// step when `--role` matches a `kind` from the app’s `agents/*.md` (defined only by `--path`).
    Worker {
        /// Worker agent id
        agent_id: String,
        /// Directory containing `horde.md` and `agents/*.md` (defaults: env `KOWALSKI_AGENT_APP_ROOT`, else repo `examples/knowledge-compiler` for local dev only).
        #[clap(short, long)]
        path: Option<String>,
        /// Kowalski API base URL
        #[clap(long)]
        api: Option<String>,
        /// Federation topic (default: federation)
        #[clap(long)]
        topic: Option<String>,
        /// Pipeline step name from the app spec (`agents/*.md` → `kind`), e.g. the ingest step’s `kind`.
        /// Omit to accept whole-pipeline delegates for whatever capability the server sends.
        #[clap(long)]
        role: Option<String>,
        /// Override the registered capability; default derives from `--role` or from legacy whole-run mode.
        #[clap(long)]
        capability: Option<String>,
    },
    /// Print reproducible end-to-end federation proof-run checklist
    Proof {
        /// App dir (`horde.md` + `agents/`). Env `KOWALSKI_AGENT_APP_ROOT`, else dev default `examples/knowledge-compiler`.
        #[clap(short, long)]
        path: Option<String>,
        /// Kowalski API base URL
        #[clap(long)]
        api: Option<String>,
        /// Worker agent id
        #[clap(long)]
        agent_id: Option<String>,
        /// Capability to delegate
        #[clap(long)]
        capability: Option<String>,
        /// Source to delegate
        #[clap(long)]
        source: Option<String>,
        /// Question to delegate
        #[clap(long)]
        question: Option<String>,
    },
}

struct AgentManager {
    agents: Arc<RwLock<HashMap<String, Box<dyn Agent + Send + Sync>>>>,
    configs: Arc<RwLock<HashMap<String, Config>>>,
}

impl AgentManager {
    fn new() -> Self {
        Self {
            agents: Arc::new(RwLock::new(HashMap::new())),
            configs: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    async fn create_agent_from_config(
        &self,
        config_path: &str,
    ) -> Result<String, Box<dyn std::error::Error>> {
        use kowalski_cli::config::AgentConfig;
        use std::path::Path;

        let agent_config = AgentConfig::load_from_file(Path::new(config_path))
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error>)?;

        println!(
            "Loading agent '{}' of type '{}'...",
            agent_config.name, agent_config.agent_type
        );

        self.create_agent(
            agent_config.name.clone(),
            &agent_config.agent_type,
            agent_config.system_prompt.as_deref(),
            agent_config.temperature,
        )
        .await?;

        // If tools are specified, we might need a way to register them after creation
        // but for now create_agent uses default tools for each type.
        // In the future, we'll use tool_manager directly.

        Ok(agent_config.name)
    }

    async fn create_agent(
        &self,
        name: String,
        agent_type: &str,
        _prompt: Option<&str>,
        _temperature: Option<f32>,
    ) -> Result<(), Box<dyn std::error::Error>> {
        let config = Config::default();
        use kowalski_core::template::default::DefaultTemplate;
        let builder = DefaultTemplate::create_agent(vec![], None, Some(0.7)).await?;
        let mut template_agent = builder.build().await?;
        template_agent.base_mut().set_system_prompt(&format!(
            "Starting generic agent (was requested type: {})",
            agent_type
        ));

        let agent: Box<dyn Agent + Send + Sync> = Box::new(template_agent);
        self.agents.write().await.insert(name.clone(), agent);
        self.configs.write().await.insert(name, config);
        Ok(())
    }

    async fn get_agent_mut(
        &self,
        name: &str,
    ) -> Option<tokio::sync::RwLockWriteGuard<'_, HashMap<String, Box<dyn Agent + Send + Sync>>>>
    {
        let guard = self.agents.write().await;
        if guard.contains_key(name) {
            Some(guard)
        } else {
            None
        }
    }

    async fn get_config(&self, name: &str) -> Option<Config> {
        self.configs.read().await.get(name).cloned()
    }

    async fn list_agents(&self) -> Result<(), Box<dyn std::error::Error>> {
        let agents = self.agents.read().await;
        println!("Active agents:");
        for (name, _) in agents.iter() {
            println!("- {}", name);
        }
        Ok(())
    }
}

async fn run_mcp_ping(config_path: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
    use kowalski_cli::config::load_mcp_config_from_file;

    let path = kowalski_cli::ops::mcp_config_path(config_path);
    let mcp = load_mcp_config_from_file(&path)?;
    if mcp.servers.is_empty() {
        println!(
            "No MCP servers under [mcp] in {}. Add [[mcp.servers]] entries (see comments in config.toml).",
            path.display()
        );
        return Ok(());
    }

    println!(
        "MCP ping — {} ({} server(s))\n",
        path.display(),
        mcp.servers.len()
    );

    let results = kowalski_cli::ops::mcp_ping_results(&path).await?;
    for r in results {
        print!("  {} <{}> [{}] ... ", r.name, r.url, r.transport);
        io::stdout().flush()?;
        if r.ok {
            println!("OK — {} tool(s)", r.tool_count.unwrap_or(0));
        } else {
            let err = r.error.as_deref().unwrap_or("");
            if err.starts_with("tools/list:") {
                println!("partial — {}", err);
            } else {
                println!("FAILED — {}", err);
            }
        }
    }
    Ok(())
}

async fn run_mcp_tools(config_path: Option<&str>) -> Result<(), Box<dyn std::error::Error>> {
    use kowalski_cli::config::load_mcp_config_from_file;

    let path = kowalski_cli::ops::mcp_config_path(config_path);
    let mcp = load_mcp_config_from_file(&path)?;
    if mcp.servers.is_empty() {
        println!(
            "No MCP servers under [mcp] in {}. Add [[mcp.servers]] entries.",
            path.display()
        );
        return Ok(());
    }

    println!(
        "MCP tools — {} ({} server(s))\n",
        path.display(),
        mcp.servers.len()
    );

    for server in &mcp.servers {
        let loc = if server.url.trim().is_empty() {
            server.command.join(" ")
        } else {
            server.url.clone()
        };
        println!(
            "[{}] {} ({})",
            server.name,
            loc,
            match server.transport {
                kowalski_core::config::McpTransport::Http => "http",
                kowalski_core::config::McpTransport::Sse => "sse",
                kowalski_core::config::McpTransport::Stdio => "stdio",
            }
        );
        let tools_result = if matches!(server.transport, kowalski_core::config::McpTransport::Stdio)
        {
            match kowalski_core::McpStdioClient::connect(server).await {
                Ok(c) => c.list_tools().await,
                Err(e) => Err(e),
            }
        } else {
            match kowalski_core::mcp::McpClient::connect_server(server).await {
                Ok(client) => {
                    if let Some(sid) = client.session_id() {
                        println!("  Session: {}", sid);
                    }
                    client.list_tools().await
                }
                Err(e) => Err(e),
            }
        };
        match tools_result {
            Ok(tools) => {
                if tools.is_empty() {
                    println!("  (no tools reported)");
                }
                for t in &tools {
                    let desc = t.description.trim();
                    let short = if desc.len() > 120 {
                        format!("{}", &desc[..120])
                    } else {
                        desc.to_string()
                    };
                    println!("{}{}", t.name, short);
                }
            }
            Err(e) => println!("  FAILED: {}", e),
        }
        println!();
    }
    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
    let cli = Cli::parse();
    let manager = AgentManager::new();

    let mut active_agent_name = None;

    if let Some(config_path) = &cli.config {
        match manager.create_agent_from_config(config_path).await {
            Ok(name) => active_agent_name = Some(name),
            Err(e) => {
                eprintln!("Error loading config: {}", e);
                return Err(e);
            }
        }
    }

    if cli.interactive {
        println!("Starting Kowalski in interactive mode...");
        let agent_name = active_agent_name.unwrap_or_else(|| {
            // Fallback to default if no config provided
            "default".to_string()
        });

        if manager.get_agent_mut(&agent_name).await.is_none() {
            manager
                .create_agent(agent_name.clone(), "web", None, None)
                .await?;
        }

        let mut agents_guard = manager.get_agent_mut(&agent_name).await.unwrap();
        if let Some(agent) = agents_guard.remove(&agent_name) {
            let mut session = kowalski_cli::interactive::InteractiveSession::new(agent, "llama3");
            session.run().await?;
            return Ok(());
        }
    }

    match cli.command {
        Some(Commands::Create {
            agent_type,
            prompt,
            temperature,
            name,
            config,
        }) => {
            if let Some(config_path) = config {
                manager.create_agent_from_config(&config_path).await?;
            } else {
                let name = name.unwrap_or_else(|| format!("{}-agent", agent_type));
                manager
                    .create_agent(name, &agent_type, prompt.as_deref(), temperature)
                    .await?;
            }
        }
        Some(Commands::Chat { agent, .. }) => {
            let agents_guard = manager.get_agent_mut(&agent).await;
            if let Some(mut agents_guard) = agents_guard {
                if let Some(agent_ref) = agents_guard.get_mut(&agent) {
                    let config = manager
                        .get_config(&agent)
                        .await
                        .unwrap_or_else(Config::default);
                    let conv_id = agent_ref.start_conversation(&config.ollama.model);
                    println!(
                        "Chat session started with agent '{}'. Type /bye to end chat.",
                        agent
                    );
                    println!("Model in use: {}", config.ollama.model);
                    // Print registered tools
                    let tools = agent_ref.list_tools().await;
                    if !tools.is_empty() {
                        info!("Registered tools:");
                        for (name, desc) in tools {
                            info!("  - {}: {}", name, desc);
                        }
                    } else {
                        info!("No tools registered or tool listing not available.");
                    }

                    chat_loop(agent_ref, conv_id).await?;
                } else {
                    println!("Agent '{}' not found.", agent);
                }
            } else {
                println!("Agent '{}' not found.", agent);
            }
        }
        Some(Commands::List) => list_agents()?,
        Some(Commands::Agents) => manager.list_agents().await?,
        Some(Commands::Mcp { command }) => match command {
            McpCommands::Ping {
                config: config_path,
            } => {
                run_mcp_ping(config_path.as_deref()).await?;
            }
            McpCommands::Tools {
                config: config_path,
            } => {
                run_mcp_tools(config_path.as_deref()).await?;
            }
        },
        Some(Commands::Config { command }) => match command {
            ConfigCommands::Check { path } => {
                kowalski_cli::ops::run_config_check(std::path::Path::new(&path))?;
            }
        },
        Some(Commands::Db { command }) => match command {
            DbCommands::Migrate { url, config } => {
                kowalski_cli::ops::run_db_migrate(url, config).await?;
            }
        },
        Some(Commands::Doctor { ollama_url }) => {
            kowalski_cli::ops::run_doctor(ollama_url).await?;
        }
        Some(Commands::Run { config }) => {
            kowalski_cli::run_ops::run_orchestrator(config.as_deref()).await?;
        }
        Some(Commands::Federation { command }) => match command {
            FederationCommands::PingNotify { config } => {
                kowalski_cli::federation_ops::run_ping_notify(config.as_deref()).await?;
            }
        },
        Some(Commands::Extension { command }) => match command {
            ExtensionCommands::List => {
                let items = kowalski_cli::extension_ops::list_extensions()?;
                if items.is_empty() {
                    println!("No extensions found.");
                    println!(
                        "Install `kowalski-ext-<name>` in PATH or add `.kowalski/extensions/<name>/run`."
                    );
                } else {
                    println!("Available extensions:");
                    for name in items {
                        println!("- {}", name);
                    }
                }
            }
            ExtensionCommands::Run { name, args } => {
                kowalski_cli::extension_ops::run_extension(&name, &args)?;
            }
        },
        Some(Commands::AgentApp { command }) => match command {
            AgentAppCommands::List { path } => {
                kowalski_cli::agent_app_ops::list_agents(path.as_deref())?;
            }
            AgentAppCommands::Validate { path } => {
                kowalski_cli::agent_app_ops::validate(path.as_deref())?;
            }
            AgentAppCommands::Run {
                source,
                question,
                path,
                api,
            } => {
                let out = tokio::task::spawn_blocking(move || {
                    kowalski_cli::agent_app_ops::run(
                        path.as_deref(),
                        &source,
                        question.as_deref(),
                        api.as_deref(),
                    )
                    .map_err(|e| e.to_string())
                })
                .await?;
                if let Err(e) = out {
                    return Err(e.into());
                }
            }
            AgentAppCommands::Delegate {
                capability,
                source,
                question,
                api,
            } => {
                let out = tokio::task::spawn_blocking(move || {
                    kowalski_cli::agent_app_ops::federate_delegate(
                        api.as_deref(),
                        &capability,
                        &source,
                        question.as_deref(),
                    )
                    .map_err(|e| e.to_string())
                })
                .await?;
                if let Err(e) = out {
                    return Err(e.into());
                }
            }
            AgentAppCommands::Worker {
                agent_id,
                path,
                api,
                topic,
                role,
                capability,
            } => {
                let out = tokio::task::spawn_blocking(move || {
                    kowalski_cli::agent_app_ops::federate_worker(
                        path.as_deref(),
                        api.as_deref(),
                        &agent_id,
                        topic.as_deref(),
                        role.as_deref(),
                        capability.as_deref(),
                    )
                    .map_err(|e| e.to_string())
                })
                .await?;
                if let Err(e) = out {
                    return Err(e.into());
                }
            }
            AgentAppCommands::Proof {
                path,
                api,
                agent_id,
                capability,
                source,
                question,
            } => {
                let out = tokio::task::spawn_blocking(move || {
                    kowalski_cli::agent_app_ops::proof_check(
                        path.as_deref(),
                        api.as_deref(),
                        agent_id.as_deref(),
                        capability.as_deref(),
                        source.as_deref(),
                        question.as_deref(),
                    )
                    .map_err(|e| e.to_string())
                })
                .await?;
                if let Err(e) = out {
                    return Err(e.into());
                }
            }
        },
        Some(Commands::Consolidate { delete }) => {
            let config = Config::default();
            let ollama_model = &config.ollama.model;

            // Create LLM provider for consolidation
            let llm_provider: std::sync::Arc<dyn kowalski_core::llm::LLMProvider> =
                std::sync::Arc::new(kowalski_core::llm::OllamaProvider::new(
                    &config.ollama.host,
                    config.ollama.port,
                ));

            kowalski_core::db::run_memory_migrations_if_configured(&config).await?;

            let mut weaver = Consolidator::new(&config.memory, llm_provider, ollama_model).await?;
            weaver.run(delete).await?;
            println!("Memory consolidation complete.");
        }
        None => {
            // Enter REPL mode if no subcommand is provided
            println!("Kowalski CLI Interactive Mode. Type 'help' for commands.");
            repl(manager).await?;
        }
    }
    Ok(())
}

async fn chat_loop(
    agent: &mut Box<dyn Agent + Send + Sync>,
    mut conv_id: String,
) -> Result<(), Box<dyn std::error::Error>> {
    let agent_name = agent.name().to_lowercase();
    println!("Agent name: '{}'", agent_name);

    loop {
        print!("You: ");
        io::stdout().flush()?;
        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
        let input_trimmed = input.trim();

        if input_trimmed.eq_ignore_ascii_case("/bye") {
            println!("Goodbye!");
            break;
        }

        if input_trimmed.starts_with("/save") {
            let filename = input_trimmed.strip_prefix("/save").unwrap().trim();
            if filename.is_empty() {
                println!("Usage: /save <filename>");
            } else {
                match agent.export_conversation(&conv_id) {
                    Ok(json) => {
                        let _ = fs::create_dir_all("sessions");
                        let path = format!("sessions/{}.json", filename);
                        if let Err(e) = fs::write(&path, json) {
                            eprintln!("Failed to write session file: {}", e);
                        } else {
                            println!("Conversation saved to {}", path);
                        }
                    }
                    Err(e) => eprintln!("Failed to save conversation: {}", e),
                }
            }
            continue;
        }

        if input_trimmed.starts_with("/load") {
            let filename = input_trimmed.strip_prefix("/load").unwrap().trim();
            if filename.is_empty() {
                println!("Usage: /load <filename>");
            } else {
                let path = format!("sessions/{}.json", filename);
                match fs::read_to_string(&path) {
                    Ok(json) => match agent.import_conversation(&json) {
                        Ok(new_id) => {
                            conv_id = new_id;
                            println!("Conversation loaded. Current session ID: {}", conv_id);
                        }
                        Err(e) => eprintln!("Failed to import conversation: {}", e),
                    },
                    Err(e) => eprintln!("Failed to read session file: {}", e),
                }
            }
            continue;
        }

        // Always use tool-calling chat method
        info!("Using tool-calling chat method");
        match chat_with_tools(agent, &conv_id, &input).await {
            Ok(_) => {
                info!("Tool-calling chat completed successfully");
            }
            Err(e) => {
                eprintln!("Tool-calling chat failed: {}", e);
                // Optionally fallback to regular chat
                use_regular_chat(agent, &conv_id, &input).await?;
            }
        }
    }
    Ok(())
}

async fn chat_with_tools(
    agent: &mut Box<dyn Agent + Send + Sync>,
    conv_id: &str,
    input: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    // Use the agent's chat_with_tools method directly
    let _response = agent.chat_with_tools(conv_id, input).await?;
    // print!("{}", response); //this was already printed in chat_with_tools
    io::stdout().flush()?;
    Ok(())
}

async fn use_regular_chat(
    agent: &mut Box<dyn Agent + Send + Sync>,
    conv_id: &str,
    input: &str,
) -> Result<(), Box<dyn std::error::Error>> {
    // Regular chat for non-web agents
    agent.add_message(conv_id, "user", input).await;
    let response = agent.chat_with_history(conv_id, input.trim(), None).await?;
    println!("{}", response);
    io::stdout().flush()?;
    println!();
    agent.add_message(conv_id, "assistant", input).await;
    Ok(())
}

fn list_agents() -> Result<(), Box<dyn std::error::Error>> {
    println!("Available agent types:");
    println!("- web: Web research and information retrieval");
    println!("- academic: Academic research and paper analysis");
    println!("- code: Code analysis, refactoring, and documentation");
    println!("- data: Data analysis and processing");
    Ok(())
}

async fn repl(manager: AgentManager) -> Result<(), Box<dyn std::error::Error>> {
    loop {
        print!("kowalski> ");
        io::stdout().flush()?;
        let mut input = String::new();
        io::stdin().read_line(&mut input)?;
        let input = input.trim();
        if input.is_empty() {
            continue;
        }
        let mut parts = input.split_whitespace();
        let cmd = parts.next().unwrap_or("");
        match cmd {
            "exit" | "quit" | "bye" | "/bye" => {
                println!("Exiting Kowalski CLI.");
                break;
            }
            "help" => {
                println!("Commands:");
                println!("  create <type> [--name <name>]: Create an agent");
                println!("  chat <name>: Chat with an agent");
                println!("  list: List available agent types");
                println!("  agents: List active agents");
                println!("  bye | /bye : Exit the CLI");
                println!();
                println!("Operators (run outside this REPL):");
                println!("  kowalski-cli mcp ping [-c config.toml]   — health + tool count");
                println!("  kowalski-cli mcp tools [-c config.toml]  — list tools per server");
                println!("  kowalski-cli config check [config.toml]");
                println!("  kowalski-cli db migrate [--url] [-c config.toml]");
                println!("  kowalski-cli doctor [--ollama-url URL]");
                println!(
                    "  kowalski-cli federation ping-notify [-c config.toml]  — pg_notify smoke (needs --features postgres)"
                );
                println!("  kowalski-cli extension list");
                println!("  kowalski-cli extension run <name> [-- <args...>]");
                println!("  kowalski-cli agent-app <list|validate|run> [args]");
                println!(
                    "  kowalski  — /api/federation/registry, /api/federation/stream (SSE), /api/federation/delegate; with --features postgres + memory.database_url, LISTEN kowalski_federation → broker"
                );
            }
            "create" => {
                let agent_type = parts.next();
                let name = parts.next();
                if let Some(agent_type) = agent_type {
                    let agent_name = match name {
                        Some(n) => n.to_string(),
                        None => format!("{}-agent", agent_type),
                    };
                    manager
                        .create_agent(agent_name.clone(), agent_type, None, None)
                        .await?;
                    println!("Agent created successfully: {}", agent_name);
                } else {
                    println!("Usage: create <type> [name]");
                }
            }
            "chat" => {
                let name = parts.next();
                if let Some(name) = name {
                    let agents_guard = manager.get_agent_mut(name).await;
                    if let Some(mut agents_guard) = agents_guard {
                        if let Some(agent_ref) = agents_guard.get_mut(name) {
                            let config = manager
                                .get_config(name)
                                .await
                                .unwrap_or_else(Config::default);
                            let conv_id = agent_ref.start_conversation(&config.ollama.model);
                            info!(
                                "Chat session started with agent '{}'. Type /bye to end chat.",
                                name
                            );
                            info!("[DEBUG] Model in use: {}", config.ollama.model);
                            // Print registered tools
                            let tools = agent_ref.list_tools().await;
                            if !tools.is_empty() {
                                info!("[DEBUG] Registered tools:");
                                for (name, desc) in tools {
                                    info!("  - {}: {}", name, desc);
                                }
                            } else {
                                info!("[DEBUG] No tools registered or tool listing not available.");
                            }

                            chat_loop(agent_ref, conv_id.clone()).await?;
                        } else {
                            println!("Agent '{}' not found.", name);
                        }
                    } else {
                        println!("Agent '{}' not found.", name);
                    }
                } else {
                    println!("Usage: chat <name>");
                }
            }
            "list" => {
                list_agents()?;
            }
            "agents" => {
                manager.list_agents().await?;
            }
            _ => {
                println!(
                    "Unknown command: {}. Type 'help' for a list of commands.",
                    cmd
                );
            }
        }
    }
    Ok(())
}

#[allow(dead_code)]
fn rule_based_tool_call(user_input: &str) -> Option<ToolCall> {
    let input = user_input.to_lowercase();
    if input.contains("list")
        && input.contains("directory")
        && let Some(path) = input.split_whitespace().find(|w| w.starts_with('/'))
    {
        return Some(ToolCall {
            name: "fs_tool".to_string(),
            parameters: json!({ "task": "list_dir", "path": path }),
            reasoning: Some("Rule-based: user asked to list a directory".to_string()),
        });
    }
    if input.contains("first 10 lines")
        && input.contains(".csv")
        && let Some(path) = input.split_whitespace().find(|w| w.ends_with(".csv"))
    {
        return Some(ToolCall {
            name: "fs_tool".to_string(),
            parameters: json!({ "task": "get_file_first_lines", "path": path, "num_lines": 10 }),
            reasoning: Some("Rule-based: user asked for first 10 lines of a CSV".to_string()),
        });
    }
    // Add more rules as needed...
    None
}