use clap::Args;
#[derive(Args)]
pub struct ResponseFormatArgs {
#[arg(long, group = "response_format")]
pub response_format_json_object: bool,
#[arg(long, group = "response_format")]
pub response_format_json_schema_inline: Option<String>,
#[arg(long, group = "response_format")]
pub response_format_json_schema_python_inline: Option<String>,
#[arg(long, group = "response_format")]
pub response_format_json_schema_python_file: Option<std::path::PathBuf>,
#[arg(long, group = "response_format")]
pub response_format_grammar: Option<String>,
#[arg(long, group = "response_format")]
pub response_format_python: bool,
#[arg(long, group = "response_format", requires = "response_format_tool_call_description")]
pub response_format_tool_call_name: Option<String>,
#[arg(long)]
pub response_format_tool_call_description: Option<String>,
#[arg(long, group = "tool_call_schema")]
pub response_format_tool_call_schema_inline: Option<String>,
#[arg(long, group = "tool_call_schema")]
pub response_format_tool_call_schema_python_inline: Option<String>,
#[arg(long, group = "tool_call_schema")]
pub response_format_tool_call_schema_python_file: Option<std::path::PathBuf>,
#[arg(long)]
pub response_format_tool_call_required: bool,
}
impl ResponseFormatArgs {
pub fn resolve(self) -> Result<Option<objectiveai_sdk::agent::completions::request::ResponseFormat>, crate::error::Error> {
use objectiveai_sdk::agent::completions::request::ResponseFormat;
if self.response_format_json_object {
return Ok(Some(ResponseFormat::JsonObject));
}
if let Some(inline) = self.response_format_json_schema_inline {
let schema = {
let mut de = serde_json::Deserializer::from_str(&inline);
serde_path_to_error::deserialize(&mut de)
.map_err(crate::error::Error::InlineDeserialize)?
};
return Ok(Some(ResponseFormat::JsonSchema { schema }));
}
if let Some(code) = self.response_format_json_schema_python_inline {
let schema = crate::python::exec_code(&code)?;
return Ok(Some(ResponseFormat::JsonSchema { schema }));
}
if let Some(path) = self.response_format_json_schema_python_file {
let schema = crate::python::exec_file(&path)?;
return Ok(Some(ResponseFormat::JsonSchema { schema }));
}
if let Some(grammar) = self.response_format_grammar {
return Ok(Some(ResponseFormat::Grammar { grammar }));
}
if self.response_format_python {
return Ok(Some(ResponseFormat::Python));
}
if let Some(name) = self.response_format_tool_call_name {
let description = self.response_format_tool_call_description.unwrap_or_default();
let schema = if let Some(inline) = self.response_format_tool_call_schema_inline {
let mut de = serde_json::Deserializer::from_str(&inline);
serde_path_to_error::deserialize(&mut de)
.map_err(crate::error::Error::InlineDeserialize)?
} else if let Some(code) = self.response_format_tool_call_schema_python_inline {
crate::python::exec_code(&code)?
} else if let Some(path) = self.response_format_tool_call_schema_python_file {
crate::python::exec_file(&path)?
} else {
Default::default()
};
let required = if self.response_format_tool_call_required { Some(true) } else { None };
return Ok(Some(ResponseFormat::ToolCall { name, description, schema, required }));
}
Ok(None)
}
}