Skip to main content

adk_model/opencode/
config.rs

1use super::OpenCodeService;
2use crate::anthropic::{Effort, ThinkingMode};
3use crate::gemini::ThinkingConfig;
4use crate::openai::OpenAIReasoningEffort;
5use crate::retry::RetryConfig;
6
7/// Wire API accepted by an OpenCode model.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9#[non_exhaustive]
10pub enum OpenCodeApi {
11    /// OpenAI-compatible Chat Completions.
12    ChatCompletions,
13    /// OpenAI Responses.
14    Responses,
15    /// Anthropic Messages.
16    Messages,
17    /// Google Gemini GenerateContent.
18    GenerateContent,
19}
20
21/// Configuration for one OpenCode model and conversation.
22///
23/// Set the application's own user agent and reuse the session ID for all main
24/// and auxiliary requests in the same conversation. Credentials are omitted
25/// from the debug representation.
26///
27/// # Example
28///
29/// ```
30/// use adk_model::opencode::{OpenCodeConfig, OpenCodeService};
31/// let config = OpenCodeConfig::new(OpenCodeService::Go, "api-key", "deepseek-v4.1-flash")
32///     .with_user_agent("example-coding-agent/1.0")
33///     .with_session_id("conversation-42");
34/// ```
35#[derive(Clone)]
36pub struct OpenCodeConfig {
37    pub(super) service: OpenCodeService,
38    pub(super) gemini_thinking: Option<ThinkingConfig>,
39    pub(super) api_key: String,
40    pub(super) model: String,
41    pub(super) base_url: String,
42    pub(super) api: Option<OpenCodeApi>,
43    pub(super) user_agent: String,
44    pub(super) session_id: String,
45    pub(super) reasoning_effort: Option<OpenAIReasoningEffort>,
46    pub(super) thinking: Option<ThinkingMode>,
47    pub(super) anthropic_effort: Option<Effort>,
48    pub(super) retry: RetryConfig,
49    pub(super) timeouts: Option<crate::HttpTimeouts>,
50}
51
52impl std::fmt::Debug for OpenCodeConfig {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        f.debug_struct("OpenCodeConfig")
55            .field("service", &self.service)
56            .field("model", &self.model)
57            .field("api", &self.api)
58            .finish_non_exhaustive()
59    }
60}
61
62impl OpenCodeConfig {
63    /// Creates a config with the selected service's official URL and model routing.
64    pub fn new(
65        service: OpenCodeService,
66        api_key: impl Into<String>,
67        model: impl Into<String>,
68    ) -> Self {
69        Self {
70            service,
71            gemini_thinking: None,
72            api_key: api_key.into(),
73            model: model.into(),
74            base_url: service.base_url().into(),
75            api: None,
76            user_agent: String::new(),
77            session_id: String::new(),
78            reasoning_effort: None,
79            thinking: None,
80            anthropic_effort: None,
81            retry: RetryConfig::default(),
82            timeouts: None,
83        }
84    }
85
86    /// Sets the API explicitly, including for models added after this library release.
87    pub fn with_api(mut self, api: OpenCodeApi) -> Self {
88        self.api = Some(api);
89        self
90    }
91
92    /// Sets a proxy or test endpoint including its `/v1` suffix.
93    pub fn with_base_url(mut self, base_url: impl Into<String>) -> Self {
94        self.base_url = base_url.into();
95        self
96    }
97
98    /// Identifies the calling coding application, such as `my-coding-agent/1.0`.
99    pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
100        self.user_agent = user_agent.into();
101        self
102    }
103
104    /// Sets the stable conversation ID sent as `x-opencode-session`.
105    pub fn with_session_id(mut self, session_id: impl Into<String>) -> Self {
106        self.session_id = session_id.into();
107        self
108    }
109
110    /// Sets reasoning effort for Chat Completions or Responses models.
111    ///
112    /// Messages models use [`Self::with_anthropic_effort`] instead.
113    pub fn with_reasoning_effort(mut self, effort: OpenAIReasoningEffort) -> Self {
114        self.reasoning_effort = Some(effort);
115        self
116    }
117
118    /// Sets thinking for models using Anthropic Messages.
119    pub fn with_anthropic_thinking(mut self, thinking: ThinkingMode) -> Self {
120        self.thinking = Some(thinking);
121        self
122    }
123
124    /// Sets output effort for models using Anthropic Messages.
125    pub fn with_anthropic_effort(mut self, effort: Effort) -> Self {
126        self.anthropic_effort = Some(effort);
127        self
128    }
129
130    /// Sets thinking for Gemini GenerateContent models.
131    pub fn with_gemini_thinking(mut self, thinking: ThinkingConfig) -> Self {
132        self.gemini_thinking = Some(thinking);
133        self
134    }
135
136    /// Sets connect and byte-idle timeouts on the selected protocol client.
137    /// Existing SDK defaults remain unchanged when this option is unset.
138    ///
139    /// # Example
140    ///
141    /// ```
142    /// use adk_model::{HttpTimeouts, OpenCodeConfig, OpenCodeService};
143    /// use std::time::Duration;
144    /// let config = OpenCodeConfig::new(OpenCodeService::Go, "key", "model")
145    ///     .with_timeouts(HttpTimeouts::new(Duration::from_secs(30), Duration::from_secs(300)));
146    /// ```
147    pub fn with_timeouts(mut self, timeouts: crate::HttpTimeouts) -> Self {
148        self.timeouts = Some(timeouts);
149        self
150    }
151
152    /// Sets the underlying model client's retry policy.
153    pub fn with_retry_config(mut self, retry: RetryConfig) -> Self {
154        self.retry = retry;
155        self
156    }
157}