Skip to content
Open
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
43 changes: 29 additions & 14 deletions src/cortex-engine/src/tools/handlers/create_agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl ToolHandler for CreateAgentHandler {

// Use context.cwd for project location to avoid relying on process cwd
let agents_dir = Self::get_agents_dir_with_cwd(location, &context.cwd)?;
let agent_path = agents_dir.join(format!("{}.toml", agent_name));
let agent_path = agents_dir.join(format!("{}.md", agent_name));

if agent_path.exists() {
return Err(CortexError::InvalidInput(format!(
Expand All @@ -139,18 +139,22 @@ impl ToolHandler for CreateAgentHandler {

std::fs::create_dir_all(&agents_dir)?;

let frontmatter = serde_yaml::to_string(&json!({
"name": agent_name,
"description": description,
}))
.map_err(|err| {
CortexError::Internal(format!("Failed to serialize agent frontmatter: {}", err))
})?;
let agent_content = format!(
r#"[agent]
name = "{}"
description = """
{}
"""
r#"---
{}---

You are a custom agent for the following purpose:

# Add custom configuration here
# [config]
# key = "value"
{}
"#,
agent_name, description
frontmatter, description
);

std::fs::write(&agent_path, agent_content)?;
Expand Down Expand Up @@ -180,6 +184,7 @@ description = """
#[cfg(test)]
mod tests {
use super::*;
use cortex_agents_ext::custom::CustomAgentLoader;
use serde_json::json;
use tempfile::TempDir;

Expand Down Expand Up @@ -211,16 +216,26 @@ mod tests {
let expected_name = CreateAgentHandler::sanitize_filename(description);
let agent_path = temp_dir
.path()
.join(format!(".cortex/agents/{}.toml", expected_name));
.join(format!(".cortex/agents/{}.md", expected_name));
assert!(
agent_path.exists(),
"Agent file should exist at {:?}",
agent_path
);

let content = std::fs::read_to_string(&agent_path).unwrap();
assert!(content.contains("name = \"process-and-transform-data-files\""));
assert!(content.contains("name: process-and-transform-data-files"));
assert!(content.contains("Process and transform data files"));

let loader = CustomAgentLoader::with_default_paths(Some(temp_dir.path()));
let agents = loader.load_all().await.unwrap();
let agent = agents
.iter()
.find(|agent| agent.name == "process-and-transform-data-files")
.expect("created agent should be discoverable by the standard loader");

assert_eq!(agent.description, description);
assert!(agent.prompt.contains(description));
}

#[tokio::test]
Expand Down Expand Up @@ -325,7 +340,7 @@ mod tests {
let expected_name = CreateAgentHandler::sanitize_filename(description);
let agent_path = temp_dir
.path()
.join(format!(".cortex/agents/{}.toml", expected_name));
.join(format!(".cortex/agents/{}.md", expected_name));
assert!(
agent_path.exists(),
"Agent file should exist at {:?}",
Expand All @@ -351,7 +366,7 @@ mod tests {

let agent_path = temp_dir
.path()
.join(".cortex/agents/duplicate-agent-test-case.toml");
.join(".cortex/agents/duplicate-agent-test-case.md");
assert!(
agent_path.exists(),
"First agent file should exist at {:?}",
Expand Down