Skip to main content

opencode_sdk/types/
provider.rs

1//! Provider types for opencode_rs.
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6/// Response from the provider list endpoint.
7///
8/// Contains all available providers along with defaults and connection status.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct ProviderListResponse {
12    /// All available providers.
13    #[serde(default)]
14    pub all: Vec<Provider>,
15    /// Default model for each provider (provider_id -> model_id).
16    #[serde(default)]
17    pub default: HashMap<String, String>,
18    /// List of connected/authenticated provider IDs.
19    #[serde(default)]
20    pub connected: Vec<String>,
21}
22
23/// Provider source type.
24#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
25#[serde(rename_all = "lowercase")]
26pub enum ProviderSource {
27    /// From environment variable.
28    Env,
29    /// From config file.
30    Config,
31    /// Custom provider.
32    Custom,
33    /// From API.
34    Api,
35    /// Unknown source (forward compatibility).
36    #[serde(other)]
37    Unknown,
38}
39
40/// A provider configuration.
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub struct Provider {
44    /// Provider identifier.
45    pub id: String,
46    /// Provider display name.
47    pub name: String,
48    /// Source of this provider configuration.
49    #[serde(default, skip_serializing_if = "Option::is_none")]
50    pub source: Option<ProviderSource>,
51    /// Environment variable names for this provider.
52    #[serde(default)]
53    pub env: Vec<String>,
54    /// API key if set.
55    #[serde(default, skip_serializing_if = "Option::is_none")]
56    pub key: Option<String>,
57    /// Provider options.
58    #[serde(default)]
59    pub options: HashMap<String, serde_json::Value>,
60    /// Available models for this provider (keyed by model ID).
61    #[serde(default)]
62    pub models: HashMap<String, Model>,
63    /// Additional fields.
64    #[serde(flatten)]
65    pub extra: serde_json::Value,
66}
67
68/// A model available from a provider.
69#[derive(Debug, Clone, Serialize, Deserialize)]
70#[serde(rename_all = "camelCase")]
71pub struct Model {
72    /// Model identifier.
73    pub id: String,
74    /// Model display name.
75    #[serde(default, skip_serializing_if = "Option::is_none")]
76    pub name: Option<String>,
77    /// Model capabilities.
78    #[serde(default, skip_serializing_if = "Option::is_none")]
79    pub capabilities: Option<ModelCapabilities>,
80}
81
82/// Model capabilities.
83#[derive(Debug, Clone, Serialize, Deserialize)]
84#[serde(rename_all = "camelCase")]
85pub struct ModelCapabilities {
86    /// Whether the model supports tool use.
87    #[serde(default)]
88    pub tool_use: bool,
89    /// Whether the model supports vision/images.
90    #[serde(default)]
91    pub vision: bool,
92    /// Whether the model supports extended thinking.
93    #[serde(default)]
94    pub thinking: bool,
95}
96
97/// Provider authentication info.
98#[derive(Debug, Clone, Serialize, Deserialize)]
99#[serde(rename_all = "camelCase")]
100pub struct ProviderAuth {
101    /// Provider identifier.
102    pub provider_id: String,
103    /// Authentication method.
104    pub method: AuthMethod,
105    /// Whether auth is configured.
106    #[serde(default)]
107    pub configured: bool,
108}
109
110/// Authentication method for a provider.
111#[derive(Debug, Clone, Serialize, Deserialize)]
112#[serde(tag = "type", rename_all = "kebab-case")]
113pub enum AuthMethod {
114    /// API key authentication.
115    ApiKey {
116        /// Environment variable name for the key.
117        #[serde(default, skip_serializing_if = "Option::is_none")]
118        env_var: Option<String>,
119    },
120    /// OAuth authentication.
121    Oauth {
122        /// OAuth authorize URL.
123        #[serde(default, skip_serializing_if = "Option::is_none")]
124        authorize_url: Option<String>,
125    },
126    /// No authentication required.
127    None,
128    /// Unknown auth method (forward compatibility).
129    #[serde(other)]
130    Unknown,
131}
132
133/// Request to set authentication for a provider.
134#[derive(Debug, Clone, Serialize, Deserialize)]
135#[serde(rename_all = "camelCase")]
136pub struct SetAuthRequest {
137    /// The API key or token.
138    pub key: String,
139}
140
141/// OAuth authorize response.
142#[derive(Debug, Clone, Serialize, Deserialize)]
143#[serde(rename_all = "camelCase")]
144pub struct OAuthAuthorizeResponse {
145    /// The authorization URL to redirect to.
146    pub url: String,
147}
148
149/// OAuth callback request.
150#[derive(Debug, Clone, Serialize, Deserialize)]
151#[serde(rename_all = "camelCase")]
152pub struct OAuthCallbackRequest {
153    /// The authorization code.
154    pub code: String,
155    /// Optional state parameter.
156    #[serde(default, skip_serializing_if = "Option::is_none")]
157    pub state: Option<String>,
158}