ricecoder_providers/models/
mod.rs

1//! Data models for providers
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::time::Duration;
6
7/// Information about an available model
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct ModelInfo {
10    /// Unique model identifier
11    pub id: String,
12    /// Human-readable model name
13    pub name: String,
14    /// Provider name
15    pub provider: String,
16    /// Maximum context window in tokens
17    pub context_window: usize,
18    /// Model capabilities
19    pub capabilities: Vec<Capability>,
20    /// Optional pricing information
21    pub pricing: Option<Pricing>,
22}
23
24/// Model capabilities
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
26pub enum Capability {
27    /// Chat completion capability
28    Chat,
29    /// Code generation capability
30    Code,
31    /// Vision/image understanding capability
32    Vision,
33    /// Function calling capability
34    FunctionCalling,
35    /// Streaming capability
36    Streaming,
37}
38
39/// Pricing information for a model
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct Pricing {
42    /// Cost per 1K input tokens (in USD)
43    pub input_per_1k_tokens: f64,
44    /// Cost per 1K output tokens (in USD)
45    pub output_per_1k_tokens: f64,
46}
47
48/// A chat message
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct Message {
51    /// Message role (user, assistant, system)
52    pub role: String,
53    /// Message content
54    pub content: String,
55}
56
57/// Chat completion request
58#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct ChatRequest {
60    /// Model to use
61    pub model: String,
62    /// Messages in the conversation
63    pub messages: Vec<Message>,
64    /// Temperature for sampling (0.0 to 2.0)
65    pub temperature: Option<f32>,
66    /// Maximum tokens to generate
67    pub max_tokens: Option<usize>,
68    /// Whether to stream the response
69    pub stream: bool,
70}
71
72/// Reason for chat completion finish
73#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
74pub enum FinishReason {
75    /// Model finished normally
76    Stop,
77    /// Maximum tokens reached
78    Length,
79    /// Model encountered an error
80    Error,
81}
82
83/// Token usage information
84#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct TokenUsage {
86    /// Number of tokens in the prompt
87    pub prompt_tokens: usize,
88    /// Number of tokens in the completion
89    pub completion_tokens: usize,
90    /// Total tokens used
91    pub total_tokens: usize,
92}
93
94/// Chat completion response
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct ChatResponse {
97    /// Generated content
98    pub content: String,
99    /// Model used
100    pub model: String,
101    /// Token usage
102    pub usage: TokenUsage,
103    /// Reason for completion
104    pub finish_reason: FinishReason,
105}
106
107/// Provider configuration
108#[derive(Debug, Clone, Serialize, Deserialize)]
109pub struct ProviderConfig {
110    /// Default provider and model settings
111    pub defaults: DefaultsConfig,
112    /// Per-provider settings
113    pub providers: HashMap<String, ProviderSettings>,
114}
115
116/// Default provider and model configuration
117#[derive(Debug, Clone, Serialize, Deserialize)]
118pub struct DefaultsConfig {
119    /// Default provider ID
120    pub provider: String,
121    /// Default model ID
122    pub model: String,
123    /// Per-command defaults (gen, refactor, review)
124    pub per_command: HashMap<String, String>,
125    /// Per-action defaults (analysis, generation)
126    pub per_action: HashMap<String, String>,
127}
128
129/// Settings for a specific provider
130#[derive(Debug, Clone, Default, Serialize, Deserialize)]
131pub struct ProviderSettings {
132    /// API key (can be overridden by environment variable)
133    pub api_key: Option<String>,
134    /// Base URL for the provider (for self-hosted or proxy)
135    pub base_url: Option<String>,
136    /// Request timeout
137    pub timeout: Option<Duration>,
138    /// Number of retries on failure
139    pub retry_count: Option<usize>,
140}
141
142/// API key configuration
143#[derive(Debug, Clone, Serialize, Deserialize)]
144pub struct ApiKeyConfig {
145    /// Environment variable name for the API key
146    pub env_var: String,
147    /// Whether to use system keyring for secure storage
148    pub secure_storage: bool,
149}