use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::{
datatype::Value,
model::{LangModelOptions, ThinkingEffort},
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(skip_serializing_if = "Option::is_none")]
pub model_options: Option<LangModelOptions>,
#[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>,
}
pub(super) fn split_desc(desc: &str) -> (&str, &str) {
desc.split_once('/').unwrap_or((desc, ""))
}
impl AgentSpec {
pub fn new(model: impl Into<String>) -> Self {
Self {
model: model.into(),
instruction: None,
tools: Vec::new(),
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.extend(tools);
self
}
pub fn system_tools(mut self) -> Self {
self.tools.push(get_shell_tool_desc());
let org = split_desc(&self.model).0;
if org == "openai" {
self.tools.push(get_apply_patch_tool_desc());
} else {
self.tools.push(match org {
"google" => get_read_file_tool_desc(),
_ => get_read_tool_desc(),
});
self.tools.push(get_write_tool_desc());
self.tools.push(get_edit_tool_desc());
}
if !matches!(org, "deepseek-ai" | "moonshotai" | "zai-org" | "Qwen") {
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 thinking_effort(mut self, effort: ThinkingEffort) -> Self {
self.model_options
.get_or_insert_with(LangModelOptions::default)
.thinking_effort = Some(effort);
self
}
pub fn output_schema(mut self, schema: Value) -> Self {
self.model_options
.get_or_insert_with(LangModelOptions::default)
.output_schema = Some(schema);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
fn names(spec: &AgentSpec) -> Vec<&str> {
spec.tools.iter().map(|t| t.name.as_str()).collect()
}
#[test]
fn test_system_tools_follow_the_model() {
let gpt = AgentSpec::new("openai/gpt-6-luna").system_tools();
assert_eq!(names(&gpt), ["shell", "apply_patch", "imgread"]);
let gemini = AgentSpec::new("google").system_tools();
assert!(
names(&gemini).contains(&"read_file"),
"{:?}",
names(&gemini)
);
let claude = AgentSpec::new("anthropic/claude-sonnet-5-5").system_tools();
assert!(names(&claude).contains(&"read"), "{:?}", names(&claude));
assert!(names(&claude).contains(&"imgread"), "{:?}", names(&claude));
let deepseek = AgentSpec::new("deepseek-ai/deepseek-chat").system_tools();
assert!(!names(&deepseek).contains(&"imgread"));
let kimi = AgentSpec::new("moonshotai/Kimi-K2-Thinking").system_tools();
assert!(!names(&kimi).contains(&"imgread"));
}
#[test]
fn test_web_search_tool_empty_engines_keeps_field_none() {
let spec = AgentSpec::new("anthropic").web_search_tool(vec![]);
assert!(spec.web_search_engines.is_none());
assert_eq!(names(&spec), ["web_search"]);
}
#[test]
fn test_spec_roundtrip() {
let spec = AgentSpec::new("anthropic/claude-sonnet-5-5")
.instruction("Be brief.")
.thinking_effort(ThinkingEffort::Low)
.web_search_tool(vec![WebSearchEngineKind::Google]);
let json = serde_json::to_string(&spec).unwrap();
let back: AgentSpec = serde_json::from_str(&json).unwrap();
assert_eq!(back.model, "anthropic/claude-sonnet-5-5");
assert_eq!(
back.model_options.unwrap().thinking_effort,
Some(ThinkingEffort::Low)
);
assert_eq!(
back.web_search_engines.as_deref(),
Some([WebSearchEngineKind::Google].as_slice())
);
}
}