-
Notifications
You must be signed in to change notification settings - Fork 71
feat(cli): implement backend connections for CLI commands #148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,20 +1,87 @@ | ||||||
| //! `mofa agent start` command implementation | ||||||
|
|
||||||
| use crate::config::loader::ConfigLoader; | ||||||
| use crate::context::CliContext; | ||||||
| use colored::Colorize; | ||||||
|
|
||||||
| /// Execute the `mofa agent start` command | ||||||
| pub fn run(agent_id: &str, _config: Option<&std::path::Path>, daemon: bool) -> anyhow::Result<()> { | ||||||
| pub async fn run( | ||||||
| ctx: &CliContext, | ||||||
| agent_id: &str, | ||||||
| config_path: Option<&std::path::Path>, | ||||||
| daemon: bool, | ||||||
| ) -> anyhow::Result<()> { | ||||||
| println!("{} Starting agent: {}", "→".green(), agent_id.cyan()); | ||||||
|
|
||||||
| if daemon { | ||||||
| println!(" Mode: {}", "daemon".yellow()); | ||||||
| } | ||||||
|
|
||||||
| // TODO: Implement actual agent starting logic | ||||||
| // This would involve: | ||||||
| // 1. Loading agent configuration | ||||||
| // 2. Starting the agent process | ||||||
| // 3. Storing agent state/PID | ||||||
| // Check if agent is already registered | ||||||
| if ctx.agent_registry.contains(agent_id).await { | ||||||
| anyhow::bail!("Agent '{}' is already registered", agent_id); | ||||||
| } | ||||||
|
|
||||||
| // Load agent configuration | ||||||
| let agent_config = if let Some(path) = config_path { | ||||||
| println!(" Config: {}", path.display().to_string().cyan()); | ||||||
| let loader = ConfigLoader::new(); | ||||||
| let cli_config = loader.load(path)?; | ||||||
| println!(" Agent: {}", cli_config.agent.name.white()); | ||||||
|
|
||||||
| // Convert CLI AgentConfig to kernel AgentConfig | ||||||
| mofa_kernel::agent::config::AgentConfig::new(agent_id, &cli_config.agent.name) | ||||||
| } else { | ||||||
| // Try to auto-discover configuration | ||||||
| let loader = ConfigLoader::new(); | ||||||
| match loader.find_config() { | ||||||
| Some(found_path) => { | ||||||
| println!( | ||||||
| " Config: {} (auto-discovered)", | ||||||
| found_path.display().to_string().cyan() | ||||||
| ); | ||||||
| let cli_config = loader.load(&found_path)?; | ||||||
| println!(" Agent: {}", cli_config.agent.name.white()); | ||||||
| mofa_kernel::agent::config::AgentConfig::new(agent_id, &cli_config.agent.name) | ||||||
| } | ||||||
| None => { | ||||||
| println!( | ||||||
| " {} No config file found, using defaults", | ||||||
| "!".yellow() | ||||||
| ); | ||||||
| mofa_kernel::agent::config::AgentConfig::new(agent_id, agent_id) | ||||||
| } | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
| // Check if a matching factory type is available | ||||||
| let factory_types = ctx.agent_registry.list_factory_types().await; | ||||||
| if factory_types.is_empty() { | ||||||
| println!( | ||||||
| " {} No agent factories registered. Agent registered with config only.", | ||||||
| "!".yellow() | ||||||
| ); | ||||||
| println!(" Agent config stored for: {}", agent_config.name.cyan()); | ||||||
| } else { | ||||||
| // Try to create via factory | ||||||
| let type_id = factory_types.first().unwrap(); | ||||||
|
||||||
| let type_id = factory_types.first().unwrap(); | |
| let type_id = &factory_types[0]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,45 @@ | ||
| //! `mofa agent stop` command implementation | ||
|
|
||
| use crate::context::CliContext; | ||
| use colored::Colorize; | ||
|
|
||
| /// Execute the `mofa agent stop` command | ||
| pub fn run(agent_id: &str) -> anyhow::Result<()> { | ||
| pub async fn run(ctx: &CliContext, agent_id: &str) -> anyhow::Result<()> { | ||
| println!("{} Stopping agent: {}", "→".green(), agent_id.cyan()); | ||
|
|
||
| // TODO: Implement actual agent stopping logic | ||
| // This would involve: | ||
| // 1. Looking up the agent's PID/state | ||
| // 2. Sending a shutdown signal | ||
| // 3. Waiting for graceful shutdown | ||
| // Check if agent exists | ||
| if !ctx.agent_registry.contains(agent_id).await { | ||
| anyhow::bail!("Agent '{}' not found in registry", agent_id); | ||
| } | ||
|
|
||
| println!("{} Agent '{}' stopped", "✓".green(), agent_id); | ||
| // Attempt graceful shutdown via the agent instance | ||
| if let Some(agent) = ctx.agent_registry.get(agent_id).await { | ||
| let mut agent_guard = agent.write().await; | ||
| if let Err(e) = agent_guard.shutdown().await { | ||
| println!( | ||
| " {} Graceful shutdown failed: {}", | ||
| "!".yellow(), | ||
| e | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| // Unregister from the registry | ||
| let removed = ctx | ||
| .agent_registry | ||
| .unregister(agent_id) | ||
| .await | ||
| .map_err(|e| anyhow::anyhow!("Failed to unregister agent: {}", e))?; | ||
|
|
||
| if removed { | ||
| println!("{} Agent '{}' stopped and unregistered", "✓".green(), agent_id); | ||
| } else { | ||
| println!( | ||
| "{} Agent '{}' was not in the registry", | ||
| "!".yellow(), | ||
| agent_id | ||
| ); | ||
| } | ||
|
|
||
| Ok(()) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
daemonparameter is accepted but not implemented in the function body. It is only used for printing a message on line 17, but the actual daemon mode functionality (backgrounding the process, proper daemon setup, etc.) is not implemented. Consider either implementing daemon mode or removing this parameter until the feature is ready.