use super::*;
use crate::provider::{
ContentPart, FinishReason, LLMError, LLMRequest, LLMResponse, LLMStream, LLMStreamEvent, Message, MessageContent,
MessageRole, ToolCall, ToolChoice, ToolDefinition, Usage,
};
use anyhow::Result;
use async_stream::try_stream;
use async_trait::async_trait;
use futures::StreamExt;
use hashbrown::HashMap;
use reqwest::Client as HttpClient;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use vtcode_config::TimeoutsConfig;
use vtcode_config::constants::{env_vars, models, urls};
use vtcode_config::core::{AnthropicConfig, ModelConfig, PromptCachingConfig};
use crate::providers::common::{
assistant_interleaved_history_text, collect_history_system_directives, extract_reasoning_text_from_detail_values,
extract_reasoning_text_from_serialized_details, is_minimax_m2_model, merge_system_prompt_with_history_directives,
override_base_url, parse_client_prompt_common, resolve_model, serialize_reasoning_detail_values,
};
use crate::providers::error_handling::{format_network_error, format_parse_error};
use crate::providers::local_readiness::{invalidate_readiness_cache, resolve_local_model};
use crate::providers::local_server::LocalProvider;
#[derive(Debug, Serialize)]
pub(super) struct OllamaChatRequest {
pub(super) model: String,
pub(super) messages: Vec<OllamaChatMessage>,
pub(super) stream: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) format: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) options: Option<OllamaChatOptions>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) tools: Option<Vec<ToolDefinition>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) think: Option<Value>,
}
#[derive(Debug, Serialize)]
pub(super) struct OllamaChatMessage {
pub(super) role: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) content: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) thinking: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) images: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) tool_calls: Option<Vec<OllamaToolCall>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) tool_call_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) tool_name: Option<String>,
}
#[derive(Debug, Serialize)]
pub(super) struct OllamaChatOptions {
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) temperature: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) num_predict: Option<u32>,
}
#[derive(Debug, Serialize)]
pub(super) struct OllamaToolCall {
#[serde(rename = "type")]
pub(super) call_type: String,
pub(super) function: OllamaToolFunctionCall,
}
#[derive(Debug, Serialize)]
pub(super) struct OllamaToolFunctionCall {
pub(super) name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) arguments: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(super) index: Option<u32>,
}
#[derive(Debug, Deserialize)]
pub(super) struct OllamaChatResponse {
pub(super) message: Option<OllamaResponseMessage>,
#[serde(default)]
pub(super) done: bool,
#[serde(default)]
pub(super) done_reason: Option<String>,
#[serde(default)]
pub(super) prompt_eval_count: Option<u32>,
#[serde(default)]
pub(super) eval_count: Option<u32>,
#[serde(default)]
pub(super) error: Option<String>,
}
#[derive(Debug, Deserialize)]
pub(super) struct OllamaResponseMessage {
#[serde(default)]
#[expect(
dead_code,
reason = "Intentional compatibility, platform, test, or API-shape suppression."
)]
pub(super) role: Option<String>,
#[serde(default)]
pub(super) content: Option<String>,
#[serde(default)]
pub(super) thinking: Option<String>,
#[serde(default)]
pub(super) reasoning_details: Option<Vec<Value>>,
#[serde(default)]
pub(super) tool_calls: Option<Vec<OllamaResponseToolCall>>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub(super) struct OllamaResponseToolCall {
#[serde(default)]
#[serde(rename = "type")]
pub(super) call_type: Option<String>,
#[serde(default)]
pub(super) function: Option<OllamaResponseFunctionCall>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub(super) struct OllamaResponseFunctionCall {
#[serde(default)]
pub(super) name: Option<String>,
#[serde(default)]
pub(super) arguments: Option<Value>,
#[serde(default)]
pub(super) index: Option<u32>,
}
#[derive(Debug, Deserialize)]
pub(super) struct OllamaErrorResponse {
pub(super) error: Option<String>,
}
pub(super) fn parse_stream_chunk(line: &str) -> Result<OllamaChatResponse, LLMError> {
serde_json::from_str::<OllamaChatResponse>(line).map_err(|err| LLMError::Provider {
message: format!("Failed to parse Ollama stream chunk: {err}"),
metadata: None,
})
}