use serde_json::{Value, json};
use vtcode_config::core::OpenAIHostedShellConfig;
use vtcode_config::types::ReasoningEffortLevel;
use crate::{error_display, provider::LLMError};
mod chat;
mod history;
mod model_policy;
mod responses;
pub(crate) use chat::build_chat_request;
pub(crate) use responses::build_responses_request;
pub(super) fn supports_prompt_cache_options(model: &str) -> bool {
vtcode_config::core::prompt_cache::supports_openai_explicit_prompt_cache(model)
}
pub(crate) struct ChatRequestContext<'a> {
pub model: &'a str,
pub is_native_openai: bool,
pub supports_tools: bool,
pub supports_parallel_tool_config: bool,
pub supports_temperature: bool,
pub prompt_cache_key: Option<&'a str>,
pub default_service_tier: Option<&'a str>,
pub include_structured_output: bool,
}
pub(crate) struct ResponsesRequestContext<'a> {
pub supports_tools: bool,
pub supports_allowed_tools: bool,
pub supports_parallel_tool_config: bool,
pub supports_temperature: bool,
pub supports_reasoning_effort: bool,
pub supported_reasoning_efforts: &'a [&'a str],
pub supports_reasoning: bool,
pub is_responses_api_model: bool,
pub include_max_output_tokens: bool,
pub include_output_types: bool,
pub include_sampling_parameters: bool,
pub force_response_store_false: bool,
pub include_assistant_phase: bool,
pub prompt_cache_key: Option<&'a str>,
pub include_prompt_cache_retention: bool,
pub prompt_cache_retention: Option<&'a str>,
pub supports_prompt_cache_options: bool,
pub prompt_cache_ttl: Option<vtcode_config::core::prompt_cache::PromptCacheTtl>,
pub stable_prefix_ttl: Option<vtcode_config::core::prompt_cache::PromptCacheTtl>,
pub include_explicit_cache_breakpoints: bool,
pub default_service_tier: Option<&'a str>,
pub default_response_store: Option<bool>,
pub default_responses_include: Option<&'a [String]>,
pub include_encrypted_reasoning: bool,
pub hosted_shell: Option<&'a OpenAIHostedShellConfig>,
pub include_structured_history_in_input: bool,
pub preserve_structured_history_on_replay: bool,
pub preserve_assistant_phase_on_replay: bool,
pub reasoning_context: Option<&'a str>,
pub safety_identifier: Option<&'a str>,
pub include_structured_output: bool,
}
pub(super) fn trimmed_non_empty(value: Option<&str>) -> Option<&str> {
value.map(str::trim).filter(|value| !value.is_empty())
}
struct JsonSchemaFormat<'a> {
name: &'a str,
schema: &'a Value,
strict: bool,
description: Option<&'a str>,
}
fn json_schema_format(output_format: &Value) -> Option<JsonSchemaFormat<'_>> {
if output_format.get("type").and_then(Value::as_str) != Some("json_schema") {
return None;
}
if let Some(nested) = output_format.get("json_schema").and_then(Value::as_object) {
return Some(JsonSchemaFormat {
name: nested.get("name").and_then(Value::as_str).unwrap_or("response"),
schema: nested.get("schema")?,
strict: nested.get("strict").and_then(Value::as_bool).unwrap_or(true),
description: nested.get("description").and_then(Value::as_str),
});
}
Some(JsonSchemaFormat {
name: output_format.get("name").and_then(Value::as_str).unwrap_or("response"),
schema: output_format.get("schema")?,
strict: output_format.get("strict").and_then(Value::as_bool).unwrap_or(true),
description: output_format.get("description").and_then(Value::as_str),
})
}
fn json_object_format(output_format: &Value) -> bool {
output_format.get("type").and_then(Value::as_str) == Some("json_object")
}
const UNSUPPORTED_STRICT_KEYWORDS: &[&str] = &[
"allOf",
"oneOf",
"not",
"dependentRequired",
"dependentSchemas",
"if",
"then",
"else",
"patternProperties",
];
fn invalid_schema(provider_name: &str, message: String) -> LLMError {
let formatted = error_display::format_llm_error(provider_name, &message);
LLMError::InvalidRequest { message: formatted, metadata: None }
}
pub(super) fn validate_output_format(output_format: Option<&Value>, provider_name: &str) -> Result<(), LLMError> {
let Some(format) = output_format else {
return Ok(());
};
if json_object_format(format) {
return Ok(());
}
let Some(schema) = json_schema_format(format) else {
return Ok(());
};
if !schema.strict {
return Ok(());
}
validate_strict_schema(schema.schema, "root", provider_name)
}
fn validate_schema_list(value: &Value, path: &str, provider_name: &str) -> Result<(), LLMError> {
match value {
Value::Array(schemas) => {
for (index, child) in schemas.iter().enumerate() {
validate_strict_schema(child, &format!("{path}[{index}]"), provider_name)?;
}
}
_ => validate_strict_schema(value, path, provider_name)?,
}
Ok(())
}
fn validate_strict_schema(schema: &Value, path: &str, provider_name: &str) -> Result<(), LLMError> {
let Value::Object(map) = schema else {
return Err(invalid_schema(
provider_name,
format!("Structured output schema at '{path}' must be a JSON object"),
));
};
if path == "root" {
if map.contains_key("anyOf") {
return Err(invalid_schema(
provider_name,
"Structured output root cannot use anyOf; it must be an object".to_string(),
));
}
if map.get("type").and_then(Value::as_str) != Some("object") {
return Err(invalid_schema(provider_name, "Structured output root must have type 'object'".to_string()));
}
}
for keyword in UNSUPPORTED_STRICT_KEYWORDS {
if map.contains_key(*keyword) {
return Err(invalid_schema(
provider_name,
format!("Keyword '{keyword}' is not supported in strict structured output schemas (path: {path})"),
));
}
}
let object_type = match map.get("type") {
Some(Value::String(schema_type)) => schema_type == "object",
Some(Value::Array(types)) => types.iter().any(|schema_type| schema_type.as_str() == Some("object")),
_ => false,
};
if object_type || map.contains_key("properties") || map.contains_key("required") {
if map.get("additionalProperties") != Some(&Value::Bool(false)) {
return Err(invalid_schema(
provider_name,
format!("Object at '{path}' must set additionalProperties to false for strict structured output"),
));
}
let required: Vec<&str> = map
.get("required")
.and_then(Value::as_array)
.map(|names| names.iter().filter_map(Value::as_str).collect())
.unwrap_or_default();
let properties = map.get("properties").and_then(Value::as_object);
if let Some(properties) = properties {
for name in properties.keys() {
if !required.contains(&name.as_str()) {
return Err(invalid_schema(
provider_name,
format!(
"Property '{name}' at '{path}' must be listed in required for strict structured output"
),
));
}
}
for name in &required {
if !properties.contains_key(*name) {
return Err(invalid_schema(
provider_name,
format!("Required property '{name}' at '{path}' is not declared in properties"),
));
}
}
} else if !required.is_empty() {
return Err(invalid_schema(
provider_name,
format!("Object at '{path}' declares required properties without a properties map"),
));
}
}
if let Some(properties) = map.get("properties").and_then(Value::as_object) {
for (name, child) in properties {
validate_strict_schema(child, &format!("{path}.properties.{name}"), provider_name)?;
}
}
if let Some(items) = map.get("items") {
validate_schema_list(items, &format!("{path}.items"), provider_name)?;
}
if let Some(prefix_items) = map.get("prefixItems") {
validate_schema_list(prefix_items, &format!("{path}.prefixItems"), provider_name)?;
}
if let Some(variants) = map.get("anyOf").and_then(Value::as_array) {
for (index, child) in variants.iter().enumerate() {
validate_strict_schema(child, &format!("{path}.anyOf[{index}]"), provider_name)?;
}
}
for key in ["$defs", "definitions"] {
if let Some(definitions) = map.get(key).and_then(Value::as_object) {
for (name, child) in definitions {
validate_strict_schema(child, &format!("{path}.{key}.{name}"), provider_name)?;
}
}
}
Ok(())
}
pub(super) fn chat_response_format(output_format: Option<&Value>) -> Option<Value> {
let format = output_format?;
if json_object_format(format) {
return Some(json!({"type": "json_object"}));
}
let schema = json_schema_format(format)?;
let mut nested = json!({
"name": schema.name,
"schema": schema.schema,
"strict": schema.strict,
});
if let Some(description) = schema.description {
nested["description"] = json!(description);
}
Some(json!({"type": "json_schema", "json_schema": nested}))
}
pub(super) fn responses_text_format(output_format: Option<&Value>) -> Option<Value> {
let format = output_format?;
if json_object_format(format) {
return Some(json!({"type": "json_object"}));
}
let schema = json_schema_format(format)?;
let mut value = json!({
"type": "json_schema",
"name": schema.name,
"schema": schema.schema,
"strict": schema.strict,
});
if let Some(description) = schema.description {
value["description"] = json!(description);
}
Some(value)
}
fn resolved_service_tier<'a>(model: &str, explicit: Option<&'a str>, default: Option<&'a str>) -> Option<&'a str> {
use vtcode_config::core::OpenAIServiceTier;
use vtcode_config::models::{Provider, ProviderModelSupport};
let value = trimmed_non_empty(explicit.or(default))?;
if !Provider::OpenAI.supports_service_tier(model) {
tracing::debug!(model, tier = value, "dropping service tier: model supports no service tier");
return None;
}
if let Some(tier) = OpenAIServiceTier::parse(value)
&& !Provider::OpenAI.supports_service_tier_value(model, tier)
{
tracing::debug!(model, tier = value, "dropping service tier: tier unsupported for model");
return None;
}
Some(value)
}
#[cfg(test)]
mod tests;