Skip to content
Open
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
42 changes: 37 additions & 5 deletions src/cortex-cli/src/agent_cmd/handlers/show.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
//! Handler for the `agent show` command.

use anyhow::Result;
use anyhow::{Result, bail};

use crate::agent_cmd::cli::ShowArgs;
use crate::agent_cmd::loader::load_all_agents;
use crate::agent_cmd::utils::format_color_preview;

fn agent_not_found_message(name: &str) -> String {
format!("Agent '{}' not found", name)
}

fn agent_not_found_json(name: &str) -> serde_json::Value {
serde_json::json!({
"error": agent_not_found_message(name)
})
}

/// Show agent details command.
pub async fn run_show(args: ShowArgs) -> Result<()> {
let agents = load_all_agents()?;

let agent = agents
.iter()
.find(|a| a.name == args.name)
.ok_or_else(|| anyhow::anyhow!("Agent '{}' not found", args.name))?;
let agent = match agents.iter().find(|a| a.name == args.name) {
Some(agent) => agent,
None => {
let error_message = agent_not_found_message(&args.name);
if args.json {
println!(
"{}",
serde_json::to_string_pretty(&agent_not_found_json(&args.name))?
);
}
bail!("{}", error_message);
}
};

// Warn if the agent is hidden
if agent.hidden {
Expand Down Expand Up @@ -153,3 +172,16 @@ pub async fn run_show(args: ShowArgs) -> Result<()> {

Ok(())
}

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

#[test]
fn test_agent_not_found_json_error() {
let error = agent_not_found_json("nonexistent");

assert_eq!(error["error"], "Agent 'nonexistent' not found");
serde_json::to_string(&error).expect("error should serialize as JSON");
}
}
5 changes: 2 additions & 3 deletions src/cortex-cli/src/agent_cmd/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
#[cfg(test)]
mod tests {
use crate::agent_cmd::cli::{CopyArgs, ExportArgs};
use crate::agent_cmd::loader::{
load_builtin_agents, parse_frontmatter, read_file_with_encoding,
};
use crate::agent_cmd::loader::{load_builtin_agents, parse_frontmatter};
use crate::agent_cmd::types::AgentMode;
use crate::utils::file::read_file_with_encoding;

#[test]
fn test_read_file_with_utf8() {
Expand Down