use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
agent::AgentCard,
lang_model::{LangModelOptions, model_family},
tool::{
ToolDesc, WebSearchEngineKind,
r#impl::{
get_apply_patch_tool_desc, get_edit_tool_desc, get_imgread_tool_desc,
get_read_file_tool_desc, get_read_tool_desc, get_shell_tool_desc,
get_web_fetch_tool_desc, get_web_search_tool_desc, get_write_tool_desc,
},
},
};
#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub struct AgentSpec {
pub model: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub instruction: Option<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub tools: Vec<ToolDesc>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub subagents: Vec<AgentSpec>,
#[serde(skip_serializing_if = "Option::is_none")]
pub model_options: Option<LangModelOptions>,
#[serde(skip_serializing_if = "Option::is_none")]
pub card: Option<AgentCard>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub web_search_engines: Option<Vec<WebSearchEngineKind>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub skills: Vec<String>,
}
impl AgentSpec {
pub fn new(model: impl Into<String>) -> Self {
Self {
model: model.into(),
instruction: None,
tools: Vec::new(),
subagents: Vec::new(),
card: None,
model_options: None,
web_search_engines: None,
skills: Vec::new(),
}
}
pub fn instruction(mut self, inst: impl Into<String>) -> Self {
self.instruction = Some(inst.into());
self
}
pub fn skill(mut self, dir: impl Into<String>) -> Self {
self.skills.push(dir.into());
self
}
pub fn tool(mut self, tool: ToolDesc) -> Self {
self.tools.push(tool);
self
}
pub fn tools(mut self, tools: impl IntoIterator<Item = ToolDesc>) -> Self {
self.tools.append(&mut tools.into_iter().collect());
self
}
pub fn system_tools(mut self) -> Self {
self.tools.push(get_shell_tool_desc());
let (family, name) = model_family(&self.model);
if family == "openai" {
self.tools.push(get_apply_patch_tool_desc());
} else {
self.tools.push(match family {
"anthropic" | "deepseek" | "moonshotai" | "z-ai" => get_read_tool_desc(),
"google" | "qwen" => get_read_file_tool_desc(),
_ => get_read_tool_desc(),
});
self.tools.push(get_write_tool_desc());
self.tools.push(get_edit_tool_desc());
}
let text_only = match family {
"deepseek" => true,
"moonshotai" => {
name.starts_with("kimi-k2-")
|| (name.starts_with("moonshot-v1-") && !name.contains("vision"))
}
_ => false,
};
if !text_only {
self.tools.push(get_imgread_tool_desc());
}
self
}
pub fn shell_tool(mut self) -> Self {
self.tools.push(get_shell_tool_desc());
self
}
pub fn web_search_tool(mut self, engines: Vec<WebSearchEngineKind>) -> Self {
self.tools.push(get_web_search_tool_desc());
if !engines.is_empty() {
self.web_search_engines = Some(engines);
}
self
}
pub fn web_fetch_tool(mut self) -> Self {
self.tools.push(get_web_fetch_tool_desc());
self
}
pub fn subagent(mut self, spec: AgentSpec) -> Self {
self.subagents.push(spec);
self
}
pub fn subagents(mut self, specs: impl IntoIterator<Item = AgentSpec>) -> Self {
self.subagents = specs.into_iter().collect();
self
}
pub fn card(mut self, card: AgentCard) -> Self {
self.card = Some(card);
self
}
pub fn max_tokens(mut self, max_tokens: u64) -> Self {
self.model_options
.get_or_insert_with(LangModelOptions::new)
.max_tokens = Some(max_tokens);
self
}
pub fn temperature(mut self, temperature: f64) -> Self {
self.model_options
.get_or_insert_with(LangModelOptions::new)
.temperature = Some(temperature);
self
}
pub fn top_p(mut self, top_p: f64) -> Self {
self.model_options
.get_or_insert_with(LangModelOptions::new)
.top_p = Some(top_p);
self
}
pub fn top_k(mut self, top_k: u64) -> Self {
self.model_options
.get_or_insert_with(LangModelOptions::new)
.top_k = Some(top_k);
self
}
pub fn response_format(mut self, fmt: crate::lang_model::ResponseFormat) -> Self {
self.model_options
.get_or_insert_with(LangModelOptions::new)
.response_format = Some(fmt);
self
}
pub fn reasoning(mut self, effort: crate::lang_model::ReasoningEffort) -> Self {
self.model_options
.get_or_insert_with(LangModelOptions::new)
.reasoning = Some(effort);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_web_search_tool_empty_engines_keeps_field_none() {
let spec = AgentSpec::new("openai/gpt-4o-mini").web_search_tool(vec![]);
assert!(
spec.web_search_engines.is_none(),
"empty engines should leave web_search_engines as None"
);
assert_eq!(spec.tools.len(), 1);
assert_eq!(spec.tools[0].name, "web_search");
}
#[test]
fn test_web_search_tool_with_engines_stores_field() {
let spec = AgentSpec::new("openai/gpt-4o-mini").web_search_tool(vec![
WebSearchEngineKind::Google,
WebSearchEngineKind::Brave,
]);
assert_eq!(
spec.web_search_engines.as_deref(),
Some(vec![WebSearchEngineKind::Google, WebSearchEngineKind::Brave].as_slice())
);
assert_eq!(spec.tools.len(), 1);
}
#[test]
fn test_web_search_engines_omitted_from_serialisation_when_none() {
let spec = AgentSpec::new("openai/gpt-4o-mini").web_search_tool(vec![]);
let json = serde_json::to_string(&spec).unwrap();
assert!(
!json.contains("web_search_engines"),
"web_search_engines should be absent when None: {json}"
);
}
#[test]
fn test_web_search_engines_roundtrip() {
let spec = AgentSpec::new("openai/gpt-4o-mini").web_search_tool(vec![
WebSearchEngineKind::Google,
WebSearchEngineKind::Yahoo,
]);
let json = serde_json::to_string(&spec).unwrap();
let back: AgentSpec = serde_json::from_str(&json).unwrap();
assert_eq!(
back.web_search_engines.as_deref(),
Some(vec![WebSearchEngineKind::Google, WebSearchEngineKind::Yahoo].as_slice())
);
}
}