use std::hash::{Hash, Hasher};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
use schemars::JsonSchema;
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
#[schemars(rename = "agent.completions.request.ResponseFormat")]
pub enum ResponseFormat {
#[schemars(title = "Text")]
Text,
#[schemars(title = "JsonObject")]
JsonObject,
#[schemars(title = "JsonSchema")]
JsonSchema {
schema: IndexMap<String, serde_json::Value>,
},
#[schemars(title = "Grammar")]
Grammar { grammar: String },
#[schemars(title = "Python")]
Python,
#[schemars(title = "ToolCall")]
ToolCall {
name: String,
description: String,
schema: IndexMap<String, serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
#[schemars(extend("omitempty" = true))]
required: Option<bool>,
},
}
impl ResponseFormat {
pub fn tool_key(&self) -> String {
let json = serde_json::to_string(self).unwrap_or_default();
let mut h = std::collections::hash_map::DefaultHasher::new();
json.hash(&mut h);
format!("{:x}", h.finish())
}
}
pub type PerAgentResponseFormat = IndexMap<String, ResponseFormat>;
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(untagged)]
#[schemars(rename = "agent.completions.request.ResponseFormatParam")]
pub enum ResponseFormatParam {
#[schemars(title = "Single")]
Single(ResponseFormat),
#[schemars(title = "PerAgent")]
PerAgent(PerAgentResponseFormat),
}