Skip to main content

stakpak_shared/models/integrations/
openai.rs

1//! OpenAI provider configuration and chat message types
2//!
3//! This module contains:
4//! - Configuration types for OpenAI provider
5//! - OpenAI model enums with pricing info
6//! - Chat message types used throughout the TUI
7//! - Tool call types for agent interactions
8//!
9//! Note: Low-level API request/response types are in `libs/ai/src/providers/openai/`.
10
11use crate::models::llm::{
12    GenerationDelta, LLMMessage, LLMMessageContent, LLMMessageImageSource, LLMMessageTypedContent,
13    LLMTokenUsage, LLMTool,
14};
15use crate::models::model_pricing::{ContextAware, ContextPricingTier, ModelContextInfo};
16use serde::{Deserialize, Serialize};
17use serde_json::{Value, json};
18use uuid::Uuid;
19
20// =============================================================================
21// Provider Configuration
22// =============================================================================
23
24/// Configuration for OpenAI provider
25#[derive(Serialize, Deserialize, Clone, Debug, Default, PartialEq)]
26pub struct OpenAIConfig {
27    pub api_endpoint: Option<String>,
28    pub api_key: Option<String>,
29}
30
31impl OpenAIConfig {
32    /// Create config with API key
33    pub fn with_api_key(api_key: impl Into<String>) -> Self {
34        Self {
35            api_key: Some(api_key.into()),
36            api_endpoint: None,
37        }
38    }
39
40    /// Create config from ProviderAuth (only supports API key for OpenAI)
41    pub fn from_provider_auth(auth: &crate::models::auth::ProviderAuth) -> Option<Self> {
42        match auth {
43            crate::models::auth::ProviderAuth::Api { key } => Some(Self::with_api_key(key)),
44            crate::models::auth::ProviderAuth::OAuth { .. } => None, // OpenAI doesn't support OAuth
45        }
46    }
47
48    /// Merge with credentials from ProviderAuth, preserving existing endpoint
49    pub fn with_provider_auth(mut self, auth: &crate::models::auth::ProviderAuth) -> Option<Self> {
50        match auth {
51            crate::models::auth::ProviderAuth::Api { key } => {
52                self.api_key = Some(key.clone());
53                Some(self)
54            }
55            crate::models::auth::ProviderAuth::OAuth { .. } => None, // OpenAI doesn't support OAuth
56        }
57    }
58}
59
60// =============================================================================
61// Model Definitions
62// =============================================================================
63
64/// OpenAI model identifiers
65#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default)]
66pub enum OpenAIModel {
67    // Reasoning Models
68    #[serde(rename = "o3-2025-04-16")]
69    O3,
70    #[serde(rename = "o4-mini-2025-04-16")]
71    O4Mini,
72
73    #[default]
74    #[serde(rename = "gpt-5-2025-08-07")]
75    GPT5,
76    #[serde(rename = "gpt-5.1-2025-11-13")]
77    GPT51,
78    #[serde(rename = "gpt-5-mini-2025-08-07")]
79    GPT5Mini,
80    #[serde(rename = "gpt-5-nano-2025-08-07")]
81    GPT5Nano,
82
83    Custom(String),
84}
85
86impl OpenAIModel {
87    pub fn from_string(s: &str) -> Result<Self, String> {
88        serde_json::from_value(serde_json::Value::String(s.to_string()))
89            .map_err(|_| "Failed to deserialize OpenAI model".to_string())
90    }
91
92    /// Default smart model for OpenAI
93    pub const DEFAULT_SMART_MODEL: OpenAIModel = OpenAIModel::GPT5;
94
95    /// Default eco model for OpenAI
96    pub const DEFAULT_ECO_MODEL: OpenAIModel = OpenAIModel::GPT5Mini;
97
98    /// Default recovery model for OpenAI
99    pub const DEFAULT_RECOVERY_MODEL: OpenAIModel = OpenAIModel::GPT5Mini;
100
101    /// Get default smart model as string
102    pub fn default_smart_model() -> String {
103        Self::DEFAULT_SMART_MODEL.to_string()
104    }
105
106    /// Get default eco model as string
107    pub fn default_eco_model() -> String {
108        Self::DEFAULT_ECO_MODEL.to_string()
109    }
110
111    /// Get default recovery model as string
112    pub fn default_recovery_model() -> String {
113        Self::DEFAULT_RECOVERY_MODEL.to_string()
114    }
115}
116
117impl ContextAware for OpenAIModel {
118    fn context_info(&self) -> ModelContextInfo {
119        let model_name = self.to_string();
120
121        if model_name.starts_with("o3") {
122            return ModelContextInfo {
123                max_tokens: 200_000,
124                pricing_tiers: vec![ContextPricingTier {
125                    label: "Standard".to_string(),
126                    input_cost_per_million: 2.0,
127                    output_cost_per_million: 8.0,
128                    upper_bound: None,
129                }],
130                approach_warning_threshold: 0.8,
131            };
132        }
133
134        if model_name.starts_with("o4-mini") {
135            return ModelContextInfo {
136                max_tokens: 200_000,
137                pricing_tiers: vec![ContextPricingTier {
138                    label: "Standard".to_string(),
139                    input_cost_per_million: 1.10,
140                    output_cost_per_million: 4.40,
141                    upper_bound: None,
142                }],
143                approach_warning_threshold: 0.8,
144            };
145        }
146
147        if model_name.starts_with("gpt-5-mini") {
148            return ModelContextInfo {
149                max_tokens: 400_000,
150                pricing_tiers: vec![ContextPricingTier {
151                    label: "Standard".to_string(),
152                    input_cost_per_million: 0.25,
153                    output_cost_per_million: 2.0,
154                    upper_bound: None,
155                }],
156                approach_warning_threshold: 0.8,
157            };
158        }
159
160        if model_name.starts_with("gpt-5-nano") {
161            return ModelContextInfo {
162                max_tokens: 400_000,
163                pricing_tiers: vec![ContextPricingTier {
164                    label: "Standard".to_string(),
165                    input_cost_per_million: 0.05,
166                    output_cost_per_million: 0.40,
167                    upper_bound: None,
168                }],
169                approach_warning_threshold: 0.8,
170            };
171        }
172
173        if model_name.starts_with("gpt-5") {
174            return ModelContextInfo {
175                max_tokens: 400_000,
176                pricing_tiers: vec![ContextPricingTier {
177                    label: "Standard".to_string(),
178                    input_cost_per_million: 1.25,
179                    output_cost_per_million: 10.0,
180                    upper_bound: None,
181                }],
182                approach_warning_threshold: 0.8,
183            };
184        }
185
186        ModelContextInfo::default()
187    }
188
189    fn model_name(&self) -> String {
190        match self {
191            OpenAIModel::O3 => "O3".to_string(),
192            OpenAIModel::O4Mini => "O4-mini".to_string(),
193            OpenAIModel::GPT5 => "GPT-5".to_string(),
194            OpenAIModel::GPT51 => "GPT-5.1".to_string(),
195            OpenAIModel::GPT5Mini => "GPT-5 Mini".to_string(),
196            OpenAIModel::GPT5Nano => "GPT-5 Nano".to_string(),
197            OpenAIModel::Custom(name) => format!("Custom ({})", name),
198        }
199    }
200}
201
202impl std::fmt::Display for OpenAIModel {
203    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
204        match self {
205            OpenAIModel::O3 => write!(f, "o3-2025-04-16"),
206            OpenAIModel::O4Mini => write!(f, "o4-mini-2025-04-16"),
207            OpenAIModel::GPT5Nano => write!(f, "gpt-5-nano-2025-08-07"),
208            OpenAIModel::GPT5Mini => write!(f, "gpt-5-mini-2025-08-07"),
209            OpenAIModel::GPT5 => write!(f, "gpt-5-2025-08-07"),
210            OpenAIModel::GPT51 => write!(f, "gpt-5.1-2025-11-13"),
211            OpenAIModel::Custom(model_name) => write!(f, "{}", model_name),
212        }
213    }
214}
215
216// =============================================================================
217// Message Types (used by TUI)
218// =============================================================================
219
220/// Message role
221#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
222#[serde(rename_all = "lowercase")]
223pub enum Role {
224    System,
225    Developer,
226    User,
227    #[default]
228    Assistant,
229    Tool,
230}
231
232impl std::fmt::Display for Role {
233    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234        match self {
235            Role::System => write!(f, "system"),
236            Role::Developer => write!(f, "developer"),
237            Role::User => write!(f, "user"),
238            Role::Assistant => write!(f, "assistant"),
239            Role::Tool => write!(f, "tool"),
240        }
241    }
242}
243
244/// Model info for tracking which model generated a message
245#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
246pub struct ModelInfo {
247    /// Provider name (e.g., "anthropic", "openai")
248    pub provider: String,
249    /// Model identifier (e.g., "claude-sonnet-4-20250514", "gpt-4")
250    pub id: String,
251}
252
253/// Chat message
254#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Default)]
255pub struct ChatMessage {
256    pub role: Role,
257    pub content: Option<MessageContent>,
258    #[serde(skip_serializing_if = "Option::is_none")]
259    pub name: Option<String>,
260    #[serde(skip_serializing_if = "Option::is_none")]
261    pub tool_calls: Option<Vec<ToolCall>>,
262    #[serde(skip_serializing_if = "Option::is_none")]
263    pub tool_call_id: Option<String>,
264    #[serde(skip_serializing_if = "Option::is_none")]
265    pub usage: Option<LLMTokenUsage>,
266
267    // === Extended fields for session tracking ===
268    /// Unique message identifier
269    #[serde(skip_serializing_if = "Option::is_none")]
270    pub id: Option<String>,
271    /// Model that generated this message (for assistant messages)
272    #[serde(skip_serializing_if = "Option::is_none")]
273    pub model: Option<ModelInfo>,
274    /// Cost in dollars for this message
275    #[serde(skip_serializing_if = "Option::is_none")]
276    pub cost: Option<f64>,
277    /// Why the model stopped: "stop", "tool_calls", "length", "error"
278    #[serde(skip_serializing_if = "Option::is_none")]
279    pub finish_reason: Option<String>,
280    /// Unix timestamp (ms) when message was created/sent
281    #[serde(skip_serializing_if = "Option::is_none")]
282    pub created_at: Option<i64>,
283    /// Unix timestamp (ms) when assistant finished generating
284    #[serde(skip_serializing_if = "Option::is_none")]
285    pub completed_at: Option<i64>,
286    /// Plugin extensibility - unstructured metadata
287    #[serde(skip_serializing_if = "Option::is_none")]
288    pub metadata: Option<serde_json::Value>,
289}
290
291impl ChatMessage {
292    pub fn last_server_message(messages: &[ChatMessage]) -> Option<&ChatMessage> {
293        messages
294            .iter()
295            .rev()
296            .find(|message| message.role != Role::User && message.role != Role::Tool)
297    }
298
299    pub fn to_xml(&self) -> String {
300        match &self.content {
301            Some(MessageContent::String(s)) => {
302                format!("<message role=\"{}\">{}</message>", self.role, s)
303            }
304            Some(MessageContent::Array(parts)) => parts
305                .iter()
306                .map(|part| {
307                    format!(
308                        "<message role=\"{}\" type=\"{}\">{}</message>",
309                        self.role,
310                        part.r#type,
311                        part.text.clone().unwrap_or_default()
312                    )
313                })
314                .collect::<Vec<String>>()
315                .join("\n"),
316            None => String::new(),
317        }
318    }
319}
320
321/// Message content (string or array of parts)
322#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
323#[serde(untagged)]
324pub enum MessageContent {
325    String(String),
326    Array(Vec<ContentPart>),
327}
328
329impl MessageContent {
330    pub fn inject_checkpoint_id(&self, checkpoint_id: Uuid) -> Self {
331        match self {
332            MessageContent::String(s) => MessageContent::String(format!(
333                "<checkpoint_id>{checkpoint_id}</checkpoint_id>\n{s}"
334            )),
335            MessageContent::Array(parts) => MessageContent::Array(
336                std::iter::once(ContentPart {
337                    r#type: "text".to_string(),
338                    text: Some(format!("<checkpoint_id>{checkpoint_id}</checkpoint_id>")),
339                    image_url: None,
340                })
341                .chain(parts.iter().cloned())
342                .collect(),
343            ),
344        }
345    }
346
347    pub fn extract_checkpoint_id(&self) -> Option<Uuid> {
348        match self {
349            MessageContent::String(s) => s
350                .rfind("<checkpoint_id>")
351                .and_then(|start| {
352                    s[start..]
353                        .find("</checkpoint_id>")
354                        .map(|end| (start + "<checkpoint_id>".len(), start + end))
355                })
356                .and_then(|(start, end)| Uuid::parse_str(&s[start..end]).ok()),
357            MessageContent::Array(parts) => parts.iter().rev().find_map(|part| {
358                part.text.as_deref().and_then(|text| {
359                    text.rfind("<checkpoint_id>")
360                        .and_then(|start| {
361                            text[start..]
362                                .find("</checkpoint_id>")
363                                .map(|end| (start + "<checkpoint_id>".len(), start + end))
364                        })
365                        .and_then(|(start, end)| Uuid::parse_str(&text[start..end]).ok())
366                })
367            }),
368        }
369    }
370}
371
372impl std::fmt::Display for MessageContent {
373    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
374        match self {
375            MessageContent::String(s) => write!(f, "{s}"),
376            MessageContent::Array(parts) => {
377                let text_parts: Vec<String> =
378                    parts.iter().filter_map(|part| part.text.clone()).collect();
379                write!(f, "{}", text_parts.join("\n"))
380            }
381        }
382    }
383}
384
385impl Default for MessageContent {
386    fn default() -> Self {
387        MessageContent::String(String::new())
388    }
389}
390
391/// Content part (text or image)
392#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
393pub struct ContentPart {
394    pub r#type: String,
395    #[serde(skip_serializing_if = "Option::is_none")]
396    pub text: Option<String>,
397    #[serde(skip_serializing_if = "Option::is_none")]
398    pub image_url: Option<ImageUrl>,
399}
400
401/// Image URL with optional detail level
402#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
403pub struct ImageUrl {
404    pub url: String,
405    #[serde(skip_serializing_if = "Option::is_none")]
406    pub detail: Option<String>,
407}
408
409// =============================================================================
410// Tool Types (used by TUI)
411// =============================================================================
412
413/// Tool definition
414#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
415pub struct Tool {
416    pub r#type: String,
417    pub function: FunctionDefinition,
418}
419
420/// Function definition for tools
421#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
422pub struct FunctionDefinition {
423    pub name: String,
424    pub description: Option<String>,
425    pub parameters: serde_json::Value,
426}
427
428impl From<Tool> for LLMTool {
429    fn from(tool: Tool) -> Self {
430        LLMTool {
431            name: tool.function.name,
432            description: tool.function.description.unwrap_or_default(),
433            input_schema: tool.function.parameters,
434        }
435    }
436}
437
438/// Tool choice configuration
439#[derive(Debug, Clone, PartialEq)]
440pub enum ToolChoice {
441    Auto,
442    Required,
443    Object(ToolChoiceObject),
444}
445
446impl Serialize for ToolChoice {
447    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
448    where
449        S: serde::Serializer,
450    {
451        match self {
452            ToolChoice::Auto => serializer.serialize_str("auto"),
453            ToolChoice::Required => serializer.serialize_str("required"),
454            ToolChoice::Object(obj) => obj.serialize(serializer),
455        }
456    }
457}
458
459impl<'de> Deserialize<'de> for ToolChoice {
460    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
461    where
462        D: serde::Deserializer<'de>,
463    {
464        struct ToolChoiceVisitor;
465
466        impl<'de> serde::de::Visitor<'de> for ToolChoiceVisitor {
467            type Value = ToolChoice;
468
469            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
470                formatter.write_str("string or object")
471            }
472
473            fn visit_str<E>(self, value: &str) -> Result<ToolChoice, E>
474            where
475                E: serde::de::Error,
476            {
477                match value {
478                    "auto" => Ok(ToolChoice::Auto),
479                    "required" => Ok(ToolChoice::Required),
480                    _ => Err(serde::de::Error::unknown_variant(
481                        value,
482                        &["auto", "required"],
483                    )),
484                }
485            }
486
487            fn visit_map<M>(self, map: M) -> Result<ToolChoice, M::Error>
488            where
489                M: serde::de::MapAccess<'de>,
490            {
491                let obj = ToolChoiceObject::deserialize(
492                    serde::de::value::MapAccessDeserializer::new(map),
493                )?;
494                Ok(ToolChoice::Object(obj))
495            }
496        }
497
498        deserializer.deserialize_any(ToolChoiceVisitor)
499    }
500}
501
502#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
503pub struct ToolChoiceObject {
504    pub r#type: String,
505    pub function: FunctionChoice,
506}
507
508#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
509pub struct FunctionChoice {
510    pub name: String,
511}
512
513/// Tool call from assistant
514#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
515pub struct ToolCall {
516    pub id: String,
517    pub r#type: String,
518    pub function: FunctionCall,
519    /// Opaque provider-specific metadata (e.g., Gemini thought_signature)
520    #[serde(skip_serializing_if = "Option::is_none")]
521    pub metadata: Option<serde_json::Value>,
522}
523
524/// Function call details
525#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
526pub struct FunctionCall {
527    pub name: String,
528    pub arguments: String,
529}
530
531/// Tool call result status
532#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
533pub enum ToolCallResultStatus {
534    Success,
535    Error,
536    Cancelled,
537}
538
539/// Tool call result
540#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
541pub struct ToolCallResult {
542    pub call: ToolCall,
543    pub result: String,
544    pub status: ToolCallResultStatus,
545}
546
547/// Tool call result progress update
548#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
549pub struct ToolCallResultProgress {
550    pub id: Uuid,
551    pub message: String,
552    /// Type of progress update for specialized handling
553    #[serde(skip_serializing_if = "Option::is_none")]
554    pub progress_type: Option<ProgressType>,
555    /// Structured task updates for task wait progress
556    #[serde(skip_serializing_if = "Option::is_none")]
557    pub task_updates: Option<Vec<TaskUpdate>>,
558    /// Overall progress percentage (0.0 - 100.0)
559    #[serde(skip_serializing_if = "Option::is_none")]
560    pub progress: Option<f64>,
561}
562
563/// Type of progress update
564#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
565pub enum ProgressType {
566    /// Command output streaming
567    CommandOutput,
568    /// Task wait progress with structured updates
569    TaskWait,
570    /// Generic progress
571    Generic,
572}
573
574/// Structured task status update
575#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
576pub struct TaskUpdate {
577    pub task_id: String,
578    pub status: String,
579    #[serde(skip_serializing_if = "Option::is_none")]
580    pub description: Option<String>,
581    #[serde(skip_serializing_if = "Option::is_none")]
582    pub duration_secs: Option<f64>,
583    #[serde(skip_serializing_if = "Option::is_none")]
584    pub output_preview: Option<String>,
585    /// Whether this is a target task being waited on
586    #[serde(default)]
587    pub is_target: bool,
588    /// Pause information for paused subagent tasks
589    #[serde(skip_serializing_if = "Option::is_none")]
590    pub pause_info: Option<TaskPauseInfo>,
591}
592
593/// Pause information for subagent tasks awaiting approval
594#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
595pub struct TaskPauseInfo {
596    /// The agent's message before pausing
597    #[serde(skip_serializing_if = "Option::is_none")]
598    pub agent_message: Option<String>,
599    /// Pending tool calls awaiting approval
600    #[serde(skip_serializing_if = "Option::is_none")]
601    pub pending_tool_calls: Option<Vec<crate::models::async_manifest::PendingToolCall>>,
602}
603
604// =============================================================================
605// Chat Completion Types (used by TUI)
606// =============================================================================
607
608/// Chat completion request
609#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
610pub struct ChatCompletionRequest {
611    pub model: String,
612    pub messages: Vec<ChatMessage>,
613    #[serde(skip_serializing_if = "Option::is_none")]
614    pub frequency_penalty: Option<f32>,
615    #[serde(skip_serializing_if = "Option::is_none")]
616    pub logit_bias: Option<serde_json::Value>,
617    #[serde(skip_serializing_if = "Option::is_none")]
618    pub logprobs: Option<bool>,
619    #[serde(skip_serializing_if = "Option::is_none")]
620    pub max_tokens: Option<u32>,
621    #[serde(skip_serializing_if = "Option::is_none")]
622    pub n: Option<u32>,
623    #[serde(skip_serializing_if = "Option::is_none")]
624    pub presence_penalty: Option<f32>,
625    #[serde(skip_serializing_if = "Option::is_none")]
626    pub response_format: Option<ResponseFormat>,
627    #[serde(skip_serializing_if = "Option::is_none")]
628    pub seed: Option<i64>,
629    #[serde(skip_serializing_if = "Option::is_none")]
630    pub stop: Option<StopSequence>,
631    #[serde(skip_serializing_if = "Option::is_none")]
632    pub stream: Option<bool>,
633    #[serde(skip_serializing_if = "Option::is_none")]
634    pub temperature: Option<f32>,
635    #[serde(skip_serializing_if = "Option::is_none")]
636    pub top_p: Option<f32>,
637    #[serde(skip_serializing_if = "Option::is_none")]
638    pub tools: Option<Vec<Tool>>,
639    #[serde(skip_serializing_if = "Option::is_none")]
640    pub tool_choice: Option<ToolChoice>,
641    #[serde(skip_serializing_if = "Option::is_none")]
642    pub user: Option<String>,
643    #[serde(skip_serializing_if = "Option::is_none")]
644    pub context: Option<ChatCompletionContext>,
645}
646
647impl ChatCompletionRequest {
648    pub fn new(
649        model: String,
650        messages: Vec<ChatMessage>,
651        tools: Option<Vec<Tool>>,
652        stream: Option<bool>,
653    ) -> Self {
654        Self {
655            model,
656            messages,
657            frequency_penalty: None,
658            logit_bias: None,
659            logprobs: None,
660            max_tokens: None,
661            n: None,
662            presence_penalty: None,
663            response_format: None,
664            seed: None,
665            stop: None,
666            stream,
667            temperature: None,
668            top_p: None,
669            tools,
670            tool_choice: None,
671            user: None,
672            context: None,
673        }
674    }
675}
676
677#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
678pub struct ChatCompletionContext {
679    pub scratchpad: Option<Value>,
680}
681
682#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
683pub struct ResponseFormat {
684    pub r#type: String,
685}
686
687#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
688#[serde(untagged)]
689pub enum StopSequence {
690    String(String),
691    Array(Vec<String>),
692}
693
694/// Chat completion response
695#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
696pub struct ChatCompletionResponse {
697    pub id: String,
698    pub object: String,
699    pub created: u64,
700    pub model: String,
701    pub choices: Vec<ChatCompletionChoice>,
702    pub usage: LLMTokenUsage,
703    #[serde(skip_serializing_if = "Option::is_none")]
704    pub system_fingerprint: Option<String>,
705    pub metadata: Option<serde_json::Value>,
706}
707
708#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
709pub struct ChatCompletionChoice {
710    pub index: usize,
711    pub message: ChatMessage,
712    pub logprobs: Option<LogProbs>,
713    pub finish_reason: FinishReason,
714}
715
716#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
717#[serde(rename_all = "snake_case")]
718pub enum FinishReason {
719    Stop,
720    Length,
721    ContentFilter,
722    ToolCalls,
723}
724
725#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
726pub struct LogProbs {
727    pub content: Option<Vec<LogProbContent>>,
728}
729
730#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
731pub struct LogProbContent {
732    pub token: String,
733    pub logprob: f32,
734    pub bytes: Option<Vec<u8>>,
735    pub top_logprobs: Option<Vec<TokenLogprob>>,
736}
737
738#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
739pub struct TokenLogprob {
740    pub token: String,
741    pub logprob: f32,
742    pub bytes: Option<Vec<u8>>,
743}
744
745// =============================================================================
746// Streaming Types
747// =============================================================================
748
749#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
750pub struct ChatCompletionStreamResponse {
751    pub id: String,
752    pub object: String,
753    pub created: u64,
754    pub model: String,
755    pub choices: Vec<ChatCompletionStreamChoice>,
756    pub usage: Option<LLMTokenUsage>,
757    pub metadata: Option<serde_json::Value>,
758}
759
760#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
761pub struct ChatCompletionStreamChoice {
762    pub index: usize,
763    pub delta: ChatMessageDelta,
764    pub finish_reason: Option<FinishReason>,
765}
766
767#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
768pub struct ChatMessageDelta {
769    #[serde(skip_serializing_if = "Option::is_none")]
770    pub role: Option<Role>,
771    #[serde(skip_serializing_if = "Option::is_none")]
772    pub content: Option<String>,
773    #[serde(skip_serializing_if = "Option::is_none")]
774    pub tool_calls: Option<Vec<ToolCallDelta>>,
775}
776
777#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
778pub struct ToolCallDelta {
779    pub index: usize,
780    pub id: Option<String>,
781    pub r#type: Option<String>,
782    pub function: Option<FunctionCallDelta>,
783    /// Opaque provider-specific metadata (e.g., Gemini thought_signature)
784    #[serde(skip_serializing_if = "Option::is_none")]
785    pub metadata: Option<serde_json::Value>,
786}
787
788#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
789pub struct FunctionCallDelta {
790    pub name: Option<String>,
791    pub arguments: Option<String>,
792}
793
794// =============================================================================
795// Conversions
796// =============================================================================
797
798impl From<LLMMessage> for ChatMessage {
799    fn from(llm_message: LLMMessage) -> Self {
800        let role = match llm_message.role.as_str() {
801            "system" => Role::System,
802            "user" => Role::User,
803            "assistant" => Role::Assistant,
804            "tool" => Role::Tool,
805            "developer" => Role::Developer,
806            _ => Role::User,
807        };
808
809        let (content, tool_calls) = match llm_message.content {
810            LLMMessageContent::String(text) => (Some(MessageContent::String(text)), None),
811            LLMMessageContent::List(items) => {
812                let mut text_parts = Vec::new();
813                let mut tool_call_parts = Vec::new();
814
815                for item in items {
816                    match item {
817                        LLMMessageTypedContent::Text { text } => {
818                            text_parts.push(ContentPart {
819                                r#type: "text".to_string(),
820                                text: Some(text),
821                                image_url: None,
822                            });
823                        }
824                        LLMMessageTypedContent::ToolCall {
825                            id,
826                            name,
827                            args,
828                            metadata,
829                        } => {
830                            tool_call_parts.push(ToolCall {
831                                id,
832                                r#type: "function".to_string(),
833                                function: FunctionCall {
834                                    name,
835                                    arguments: args.to_string(),
836                                },
837                                metadata,
838                            });
839                        }
840                        LLMMessageTypedContent::ToolResult { content, .. } => {
841                            text_parts.push(ContentPart {
842                                r#type: "text".to_string(),
843                                text: Some(content),
844                                image_url: None,
845                            });
846                        }
847                        LLMMessageTypedContent::Image { source } => {
848                            text_parts.push(ContentPart {
849                                r#type: "image_url".to_string(),
850                                text: None,
851                                image_url: Some(ImageUrl {
852                                    url: format!(
853                                        "data:{};base64,{}",
854                                        source.media_type, source.data
855                                    ),
856                                    detail: None,
857                                }),
858                            });
859                        }
860                    }
861                }
862
863                let content = if !text_parts.is_empty() {
864                    Some(MessageContent::Array(text_parts))
865                } else {
866                    None
867                };
868
869                let tool_calls = if !tool_call_parts.is_empty() {
870                    Some(tool_call_parts)
871                } else {
872                    None
873                };
874
875                (content, tool_calls)
876            }
877        };
878
879        ChatMessage {
880            role,
881            content,
882            name: None,
883            tool_calls,
884            tool_call_id: None,
885            usage: None,
886            ..Default::default()
887        }
888    }
889}
890
891impl From<ChatMessage> for LLMMessage {
892    fn from(chat_message: ChatMessage) -> Self {
893        let mut content_parts = Vec::new();
894
895        match chat_message.content {
896            Some(MessageContent::String(s)) => {
897                if !s.is_empty() {
898                    content_parts.push(LLMMessageTypedContent::Text { text: s });
899                }
900            }
901            Some(MessageContent::Array(parts)) => {
902                for part in parts {
903                    if let Some(text) = part.text {
904                        content_parts.push(LLMMessageTypedContent::Text { text });
905                    } else if let Some(image_url) = part.image_url {
906                        let (media_type, data) = if image_url.url.starts_with("data:") {
907                            let parts: Vec<&str> = image_url.url.splitn(2, ',').collect();
908                            if parts.len() == 2 {
909                                let meta = parts[0];
910                                let data = parts[1];
911                                let media_type = meta
912                                    .trim_start_matches("data:")
913                                    .trim_end_matches(";base64")
914                                    .to_string();
915                                (media_type, data.to_string())
916                            } else {
917                                ("image/jpeg".to_string(), image_url.url)
918                            }
919                        } else {
920                            ("image/jpeg".to_string(), image_url.url)
921                        };
922
923                        content_parts.push(LLMMessageTypedContent::Image {
924                            source: LLMMessageImageSource {
925                                r#type: "base64".to_string(),
926                                media_type,
927                                data,
928                            },
929                        });
930                    }
931                }
932            }
933            None => {}
934        }
935
936        if let Some(tool_calls) = chat_message.tool_calls {
937            for tool_call in tool_calls {
938                let args = serde_json::from_str(&tool_call.function.arguments).unwrap_or(json!({}));
939                content_parts.push(LLMMessageTypedContent::ToolCall {
940                    id: tool_call.id,
941                    name: tool_call.function.name,
942                    args,
943                    metadata: tool_call.metadata,
944                });
945            }
946        }
947
948        // Handle tool result messages: when role is Tool and tool_call_id is present,
949        // convert the content to a ToolResult content part. This is the generic
950        // intermediate representation - each provider's conversion layer handles
951        // the specifics (e.g., Anthropic converts to user role with tool_result blocks)
952        if chat_message.role == Role::Tool
953            && let Some(tool_call_id) = chat_message.tool_call_id
954        {
955            // Extract content as string for the tool result
956            let content_str = content_parts
957                .iter()
958                .filter_map(|p| match p {
959                    LLMMessageTypedContent::Text { text } => Some(text.clone()),
960                    _ => None,
961                })
962                .collect::<Vec<_>>()
963                .join("\n");
964
965            // Replace content with a single ToolResult
966            content_parts = vec![LLMMessageTypedContent::ToolResult {
967                tool_use_id: tool_call_id,
968                content: content_str,
969            }];
970        }
971
972        LLMMessage {
973            role: chat_message.role.to_string(),
974            content: if content_parts.is_empty() {
975                LLMMessageContent::String(String::new())
976            } else if content_parts.len() == 1 {
977                match &content_parts[0] {
978                    LLMMessageTypedContent::Text { text } => {
979                        LLMMessageContent::String(text.clone())
980                    }
981                    _ => LLMMessageContent::List(content_parts),
982                }
983            } else {
984                LLMMessageContent::List(content_parts)
985            },
986        }
987    }
988}
989
990impl From<GenerationDelta> for ChatMessageDelta {
991    fn from(delta: GenerationDelta) -> Self {
992        match delta {
993            GenerationDelta::Content { content } => ChatMessageDelta {
994                role: Some(Role::Assistant),
995                content: Some(content),
996                tool_calls: None,
997            },
998            GenerationDelta::Thinking { thinking: _ } => ChatMessageDelta {
999                role: Some(Role::Assistant),
1000                content: None,
1001                tool_calls: None,
1002            },
1003            GenerationDelta::ToolUse { tool_use } => ChatMessageDelta {
1004                role: Some(Role::Assistant),
1005                content: None,
1006                tool_calls: Some(vec![ToolCallDelta {
1007                    index: tool_use.index,
1008                    id: tool_use.id,
1009                    r#type: Some("function".to_string()),
1010                    function: Some(FunctionCallDelta {
1011                        name: tool_use.name,
1012                        arguments: tool_use.input,
1013                    }),
1014                    metadata: tool_use.metadata,
1015                }]),
1016            },
1017            _ => ChatMessageDelta {
1018                role: Some(Role::Assistant),
1019                content: None,
1020                tool_calls: None,
1021            },
1022        }
1023    }
1024}
1025
1026#[cfg(test)]
1027mod tests {
1028    use super::*;
1029
1030    #[test]
1031    fn test_serialize_basic_request() {
1032        let request = ChatCompletionRequest {
1033            model: "gpt-4".to_string(),
1034            messages: vec![
1035                ChatMessage {
1036                    role: Role::System,
1037                    content: Some(MessageContent::String(
1038                        "You are a helpful assistant.".to_string(),
1039                    )),
1040                    name: None,
1041                    tool_calls: None,
1042                    tool_call_id: None,
1043                    usage: None,
1044                    ..Default::default()
1045                },
1046                ChatMessage {
1047                    role: Role::User,
1048                    content: Some(MessageContent::String("Hello!".to_string())),
1049                    name: None,
1050                    tool_calls: None,
1051                    tool_call_id: None,
1052                    usage: None,
1053                    ..Default::default()
1054                },
1055            ],
1056            frequency_penalty: None,
1057            logit_bias: None,
1058            logprobs: None,
1059            max_tokens: Some(100),
1060            n: None,
1061            presence_penalty: None,
1062            response_format: None,
1063            seed: None,
1064            stop: None,
1065            stream: None,
1066            temperature: Some(0.7),
1067            top_p: None,
1068            tools: None,
1069            tool_choice: None,
1070            user: None,
1071            context: None,
1072        };
1073
1074        let json = serde_json::to_string(&request).unwrap();
1075        assert!(json.contains("\"model\":\"gpt-4\""));
1076        assert!(json.contains("\"messages\":["));
1077        assert!(json.contains("\"role\":\"system\""));
1078    }
1079
1080    #[test]
1081    fn test_llm_message_to_chat_message() {
1082        let llm_message = LLMMessage {
1083            role: "user".to_string(),
1084            content: LLMMessageContent::String("Hello, world!".to_string()),
1085        };
1086
1087        let chat_message = ChatMessage::from(llm_message);
1088        assert_eq!(chat_message.role, Role::User);
1089        match &chat_message.content {
1090            Some(MessageContent::String(text)) => assert_eq!(text, "Hello, world!"),
1091            _ => panic!("Expected string content"),
1092        }
1093    }
1094
1095    #[test]
1096    fn test_chat_message_to_llm_message_tool_result() {
1097        // Test that Tool role messages with tool_call_id are converted to ToolResult content
1098        // This is critical for Anthropic compatibility - the provider layer converts
1099        // role="tool" to role="user" with tool_result content blocks
1100        let chat_message = ChatMessage {
1101            role: Role::Tool,
1102            content: Some(MessageContent::String("Tool execution result".to_string())),
1103            name: None,
1104            tool_calls: None,
1105            tool_call_id: Some("toolu_01Abc123".to_string()),
1106            usage: None,
1107            ..Default::default()
1108        };
1109
1110        let llm_message: LLMMessage = chat_message.into();
1111
1112        // Role should be preserved as "tool" - provider layer handles conversion
1113        assert_eq!(llm_message.role, "tool");
1114
1115        // Content should be a ToolResult with the tool_call_id
1116        match &llm_message.content {
1117            LLMMessageContent::List(parts) => {
1118                assert_eq!(parts.len(), 1, "Should have exactly one content part");
1119                match &parts[0] {
1120                    LLMMessageTypedContent::ToolResult {
1121                        tool_use_id,
1122                        content,
1123                    } => {
1124                        assert_eq!(tool_use_id, "toolu_01Abc123");
1125                        assert_eq!(content, "Tool execution result");
1126                    }
1127                    _ => panic!("Expected ToolResult content part, got {:?}", parts[0]),
1128                }
1129            }
1130            _ => panic!(
1131                "Expected List content with ToolResult, got {:?}",
1132                llm_message.content
1133            ),
1134        }
1135    }
1136
1137    #[test]
1138    fn test_chat_message_to_llm_message_tool_result_empty_content() {
1139        // Test tool result with empty content
1140        let chat_message = ChatMessage {
1141            role: Role::Tool,
1142            content: None,
1143            name: None,
1144            tool_calls: None,
1145            tool_call_id: Some("toolu_02Xyz789".to_string()),
1146            usage: None,
1147            ..Default::default()
1148        };
1149
1150        let llm_message: LLMMessage = chat_message.into();
1151
1152        assert_eq!(llm_message.role, "tool");
1153        match &llm_message.content {
1154            LLMMessageContent::List(parts) => {
1155                assert_eq!(parts.len(), 1);
1156                match &parts[0] {
1157                    LLMMessageTypedContent::ToolResult {
1158                        tool_use_id,
1159                        content,
1160                    } => {
1161                        assert_eq!(tool_use_id, "toolu_02Xyz789");
1162                        assert_eq!(content, ""); // Empty content
1163                    }
1164                    _ => panic!("Expected ToolResult content part"),
1165                }
1166            }
1167            _ => panic!("Expected List content with ToolResult"),
1168        }
1169    }
1170
1171    #[test]
1172    fn test_chat_message_to_llm_message_assistant_with_tool_calls() {
1173        // Test that assistant messages with tool_calls are converted correctly
1174        let chat_message = ChatMessage {
1175            role: Role::Assistant,
1176            content: Some(MessageContent::String(
1177                "I'll help you with that.".to_string(),
1178            )),
1179            name: None,
1180            tool_calls: Some(vec![ToolCall {
1181                id: "call_abc123".to_string(),
1182                r#type: "function".to_string(),
1183                function: FunctionCall {
1184                    name: "get_weather".to_string(),
1185                    arguments: r#"{"location": "Paris"}"#.to_string(),
1186                },
1187                metadata: None,
1188            }]),
1189            tool_call_id: None,
1190            usage: None,
1191            ..Default::default()
1192        };
1193
1194        let llm_message: LLMMessage = chat_message.into();
1195
1196        assert_eq!(llm_message.role, "assistant");
1197        match &llm_message.content {
1198            LLMMessageContent::List(parts) => {
1199                assert_eq!(parts.len(), 2, "Should have text and tool call");
1200
1201                // First part should be text
1202                match &parts[0] {
1203                    LLMMessageTypedContent::Text { text } => {
1204                        assert_eq!(text, "I'll help you with that.");
1205                    }
1206                    _ => panic!("Expected Text content part first"),
1207                }
1208
1209                // Second part should be tool call
1210                match &parts[1] {
1211                    LLMMessageTypedContent::ToolCall { id, name, args, .. } => {
1212                        assert_eq!(id, "call_abc123");
1213                        assert_eq!(name, "get_weather");
1214                        assert_eq!(args["location"], "Paris");
1215                    }
1216                    _ => panic!("Expected ToolCall content part second"),
1217                }
1218            }
1219            _ => panic!("Expected List content"),
1220        }
1221    }
1222}