Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions crates/mofa-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ path = "src/main.rs"
# Core
mofa-sdk = { path = "../mofa-sdk", version = "0.1" }
mofa-kernel = { path = "../mofa-kernel", version = "0.1", features = ["config"] }
mofa-runtime = { path = "../mofa-runtime", version = "0.1" }
mofa-foundation = { path = "../mofa-foundation", version = "0.1" }
config.workspace = true
tokio = { workspace = true }
anyhow = { workspace = true }
async-trait = { workspace = true }
chrono = { workspace = true }

# CLI
clap = { version = "4", features = ["derive", "env"] }
Expand Down
4 changes: 4 additions & 0 deletions crates/mofa-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ pub enum AgentCommands {
#[arg(short, long)]
config: Option<PathBuf>,

/// Agent factory type (use `mofa agent status` to inspect available factories)
#[arg(long = "type")]
factory_type: Option<String>,

/// Run as daemon
#[arg(long)]
daemon: bool,
Expand Down
96 changes: 59 additions & 37 deletions crates/mofa-cli/src/commands/agent/list.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,76 @@
//! `mofa agent list` command implementation

use crate::context::CliContext;
use crate::output::Table;
use colored::Colorize;
use serde::Serialize;
use std::collections::BTreeMap;

/// Execute the `mofa agent list` command
pub fn run(running_only: bool, show_all: bool) -> anyhow::Result<()> {
pub async fn run(ctx: &CliContext, running_only: bool, _show_all: bool) -> anyhow::Result<()> {
println!("{} Listing agents", "→".green());
println!();

let agents_metadata = ctx.agent_registry.list().await;
let persisted_agents = ctx
.agent_store
.list()
.map_err(|e| anyhow::anyhow!("Failed to list persisted agents: {}", e))?;

if running_only {
println!(" Showing running agents only");
} else if show_all {
println!(" Showing all agents");
let mut merged: BTreeMap<String, AgentInfo> = BTreeMap::new();
for m in &agents_metadata {
let status = format!("{:?}", m.state);
let is_running = is_running_state(&status);
merged.insert(
m.id.clone(),
AgentInfo {
id: m.id.clone(),
name: m.name.clone(),
status,
is_running,
description: m.description.clone(),
},
);
}

println!();
for (_, entry) in persisted_agents {
merged.entry(entry.id.clone()).or_insert_with(|| {
let status = if is_running_state(&entry.state) {
format!("{} (persisted)", entry.state)
} else {
entry.state
};
AgentInfo {
id: entry.id,
name: entry.name,
status,
is_running: false,
description: entry.description,
}
});
}

// TODO: Implement actual agent listing from state store
// For now, show example output
if merged.is_empty() {
println!(" No agents registered.");
println!();
println!(
" Use {} to start an agent.",
"mofa agent start <agent_id>".cyan()
);
return Ok(());
}

let agents = vec![
AgentInfo {
id: "agent-001".to_string(),
name: "MyAgent".to_string(),
status: "running".to_string(),
uptime: Some("5m 32s".to_string()),
provider: Some("openai".to_string()),
model: Some("gpt-4o".to_string()),
},
AgentInfo {
id: "agent-002".to_string(),
name: "TestAgent".to_string(),
status: "stopped".to_string(),
uptime: None,
provider: None,
model: None,
},
];
let agents: Vec<AgentInfo> = merged.into_values().collect();

// Filter based on flags
let filtered: Vec<_> = if running_only {
agents
.iter()
.filter(|a| a.status == "running")
.cloned()
.collect()
agents.into_iter().filter(|a| a.is_running).collect()
} else {
agents
};

if filtered.is_empty() {
println!(" No agents found.");
println!(" No agents found matching criteria.");
return Ok(());
}

Expand All @@ -69,10 +89,12 @@ struct AgentInfo {
id: String,
name: String,
status: String,
#[serde(skip_serializing)]
is_running: bool,
#[serde(skip_serializing_if = "Option::is_none")]
uptime: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
provider: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
model: Option<String>,
description: Option<String>,
}

fn is_running_state(status: &str) -> bool {
status == "Running" || status == "Ready"
}
47 changes: 42 additions & 5 deletions crates/mofa-cli/src/commands/agent/restart.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,54 @@
//! `mofa agent restart` command implementation

use crate::context::CliContext;
use colored::Colorize;

/// Execute the `mofa agent restart` command
pub fn run(agent_id: &str, _config: Option<&std::path::Path>) -> anyhow::Result<()> {
pub async fn run(
ctx: &CliContext,
agent_id: &str,
config: Option<&std::path::Path>,
) -> anyhow::Result<()> {
println!("{} Restarting agent: {}", "→".green(), agent_id.cyan());

// TODO: Implement actual agent restart logic
// This would involve:
// 1. Stopping the agent
// 2. Starting it again with the same config
// Stop the agent if it's running
if ctx.agent_registry.contains(agent_id).await {
super::stop::run(ctx, agent_id).await?;
} else {
println!(" Agent was not running");
}

// Start it again
super::start::run(ctx, agent_id, config, None, false).await?;

println!("{} Agent '{}' restarted", "✓".green(), agent_id);

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;
use crate::commands::agent::{list, start, stop};
use crate::context::CliContext;
use tempfile::TempDir;

#[tokio::test]
async fn test_restart_chain_start_stop_restart_list() {
let temp = TempDir::new().unwrap();
let ctx = CliContext::with_temp_dir(temp.path()).await.unwrap();

start::run(&ctx, "chain-agent", None, None, false)
.await
.unwrap();
stop::run(&ctx, "chain-agent").await.unwrap();
run(&ctx, "chain-agent", None).await.unwrap();

assert!(ctx.agent_registry.contains("chain-agent").await);
let persisted = ctx.agent_store.get("chain-agent").unwrap().unwrap();
assert_eq!(persisted.state, "Running");

list::run(&ctx, false, false).await.unwrap();
list::run(&ctx, true, false).await.unwrap();
}
}
Loading