language_model.rs

  1use collections::HashMap;
  2use schemars::JsonSchema;
  3use serde::{Deserialize, Serialize};
  4
  5use std::sync::Arc;
  6
  7#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
  8pub struct AllLanguageModelSettingsContent {
  9    pub anthropic: Option<AnthropicSettingsContent>,
 10    pub bedrock: Option<AmazonBedrockSettingsContent>,
 11    pub deepseek: Option<DeepseekSettingsContent>,
 12    pub google: Option<GoogleSettingsContent>,
 13    pub lmstudio: Option<LmStudioSettingsContent>,
 14    pub mistral: Option<MistralSettingsContent>,
 15    pub ollama: Option<OllamaSettingsContent>,
 16    pub open_router: Option<OpenRouterSettingsContent>,
 17    pub openai: Option<OpenAiSettingsContent>,
 18    pub openai_compatible: Option<HashMap<Arc<str>, OpenAiCompatibleSettingsContent>>,
 19    pub vercel: Option<VercelSettingsContent>,
 20    pub x_ai: Option<XAiSettingsContent>,
 21    #[serde(rename = "zed.dev")]
 22    pub zed_dot_dev: Option<ZedDotDevSettingsContent>,
 23}
 24
 25#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
 26pub struct AnthropicSettingsContent {
 27    pub api_url: Option<String>,
 28    pub available_models: Option<Vec<AnthropicAvailableModel>>,
 29}
 30
 31#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 32pub struct AnthropicAvailableModel {
 33    /// The model's name in the Anthropic API. e.g. claude-3-5-sonnet-latest, claude-3-opus-20240229, etc
 34    pub name: String,
 35    /// The model's name in Zed's UI, such as in the model selector dropdown menu in the assistant panel.
 36    pub display_name: Option<String>,
 37    /// The model's context window size.
 38    pub max_tokens: u64,
 39    /// A model `name` to substitute when calling tools, in case the primary model doesn't support tool calling.
 40    pub tool_override: Option<String>,
 41    /// Configuration of Anthropic's caching API.
 42    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
 43    pub max_output_tokens: Option<u64>,
 44    pub default_temperature: Option<f32>,
 45    #[serde(default)]
 46    pub extra_beta_headers: Vec<String>,
 47    /// The model's mode (e.g. thinking)
 48    pub mode: Option<ModelMode>,
 49}
 50
 51#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
 52pub struct AmazonBedrockSettingsContent {
 53    pub available_models: Option<Vec<BedrockAvailableModel>>,
 54    pub endpoint_url: Option<String>,
 55    pub region: Option<String>,
 56    pub profile: Option<String>,
 57    pub authentication_method: Option<BedrockAuthMethodContent>,
 58}
 59
 60#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 61pub struct BedrockAvailableModel {
 62    pub name: String,
 63    pub display_name: Option<String>,
 64    pub max_tokens: u64,
 65    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
 66    pub max_output_tokens: Option<u64>,
 67    pub default_temperature: Option<f32>,
 68    pub mode: Option<ModelMode>,
 69}
 70
 71#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 72pub enum BedrockAuthMethodContent {
 73    #[serde(rename = "named_profile")]
 74    NamedProfile,
 75    #[serde(rename = "sso")]
 76    SingleSignOn,
 77    /// IMDSv2, PodIdentity, env vars, etc.
 78    #[serde(rename = "default")]
 79    Automatic,
 80}
 81
 82#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
 83pub struct OllamaSettingsContent {
 84    pub api_url: Option<String>,
 85    pub available_models: Option<Vec<OllamaAvailableModel>>,
 86}
 87
 88#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 89pub struct OllamaAvailableModel {
 90    /// The model name in the Ollama API (e.g. "llama3.2:latest")
 91    pub name: String,
 92    /// The model's name in Zed's UI, such as in the model selector dropdown menu in the assistant panel.
 93    pub display_name: Option<String>,
 94    /// The Context Length parameter to the model (aka num_ctx or n_ctx)
 95    pub max_tokens: u64,
 96    /// The number of seconds to keep the connection open after the last request
 97    pub keep_alive: Option<KeepAlive>,
 98    /// Whether the model supports tools
 99    pub supports_tools: Option<bool>,
100    /// Whether the model supports vision
101    pub supports_images: Option<bool>,
102    /// Whether to enable think mode
103    pub supports_thinking: Option<bool>,
104}
105
106#[derive(Clone, Serialize, Deserialize, Debug, Eq, PartialEq, JsonSchema)]
107#[serde(untagged)]
108pub enum KeepAlive {
109    /// Keep model alive for N seconds
110    Seconds(isize),
111    /// Keep model alive for a fixed duration. Accepts durations like "5m", "10m", "1h", "1d", etc.
112    Duration(String),
113}
114
115impl KeepAlive {
116    /// Keep model alive until a new model is loaded or until Ollama shuts down
117    pub fn indefinite() -> Self {
118        Self::Seconds(-1)
119    }
120}
121
122impl Default for KeepAlive {
123    fn default() -> Self {
124        Self::indefinite()
125    }
126}
127
128#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
129pub struct LmStudioSettingsContent {
130    pub api_url: Option<String>,
131    pub available_models: Option<Vec<LmStudioAvailableModel>>,
132}
133
134#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
135pub struct LmStudioAvailableModel {
136    pub name: String,
137    pub display_name: Option<String>,
138    pub max_tokens: u64,
139    pub supports_tool_calls: bool,
140    pub supports_images: bool,
141}
142
143#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
144pub struct DeepseekSettingsContent {
145    pub api_url: Option<String>,
146    pub available_models: Option<Vec<DeepseekAvailableModel>>,
147}
148
149#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
150pub struct DeepseekAvailableModel {
151    pub name: String,
152    pub display_name: Option<String>,
153    pub max_tokens: u64,
154    pub max_output_tokens: Option<u64>,
155}
156
157#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
158pub struct MistralSettingsContent {
159    pub api_url: Option<String>,
160    pub available_models: Option<Vec<MistralAvailableModel>>,
161}
162
163#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
164pub struct MistralAvailableModel {
165    pub name: String,
166    pub display_name: Option<String>,
167    pub max_tokens: u64,
168    pub max_output_tokens: Option<u64>,
169    pub max_completion_tokens: Option<u64>,
170    pub supports_tools: Option<bool>,
171    pub supports_images: Option<bool>,
172    pub supports_thinking: Option<bool>,
173}
174
175#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
176pub struct OpenAiSettingsContent {
177    pub api_url: Option<String>,
178    pub available_models: Option<Vec<OpenAiAvailableModel>>,
179}
180
181#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
182pub struct OpenAiAvailableModel {
183    pub name: String,
184    pub display_name: Option<String>,
185    pub max_tokens: u64,
186    pub max_output_tokens: Option<u64>,
187    pub max_completion_tokens: Option<u64>,
188    pub reasoning_effort: Option<OpenAiReasoningEffort>,
189}
190
191#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, JsonSchema)]
192#[serde(rename_all = "lowercase")]
193pub enum OpenAiReasoningEffort {
194    Minimal,
195    Low,
196    Medium,
197    High,
198}
199
200#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
201pub struct OpenAiCompatibleSettingsContent {
202    pub api_url: String,
203    pub available_models: Vec<OpenAiCompatibleAvailableModel>,
204}
205
206#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
207pub struct OpenAiCompatibleAvailableModel {
208    pub name: String,
209    pub display_name: Option<String>,
210    pub max_tokens: u64,
211    pub max_output_tokens: Option<u64>,
212    pub max_completion_tokens: Option<u64>,
213    #[serde(default)]
214    pub capabilities: OpenAiCompatibleModelCapabilities,
215}
216
217#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
218pub struct OpenAiCompatibleModelCapabilities {
219    pub tools: bool,
220    pub images: bool,
221    pub parallel_tool_calls: bool,
222    pub prompt_cache_key: bool,
223}
224
225impl Default for OpenAiCompatibleModelCapabilities {
226    fn default() -> Self {
227        Self {
228            tools: true,
229            images: false,
230            parallel_tool_calls: false,
231            prompt_cache_key: false,
232        }
233    }
234}
235
236#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
237pub struct VercelSettingsContent {
238    pub api_url: Option<String>,
239    pub available_models: Option<Vec<VercelAvailableModel>>,
240}
241
242#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
243pub struct VercelAvailableModel {
244    pub name: String,
245    pub display_name: Option<String>,
246    pub max_tokens: u64,
247    pub max_output_tokens: Option<u64>,
248    pub max_completion_tokens: Option<u64>,
249}
250
251#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
252pub struct GoogleSettingsContent {
253    pub api_url: Option<String>,
254    pub available_models: Option<Vec<GoogleAvailableModel>>,
255}
256
257#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
258pub struct GoogleAvailableModel {
259    pub name: String,
260    pub display_name: Option<String>,
261    pub max_tokens: u64,
262    pub mode: Option<ModelMode>,
263}
264
265#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
266pub struct XAiSettingsContent {
267    pub api_url: Option<String>,
268    pub available_models: Option<Vec<XaiAvailableModel>>,
269}
270
271#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
272pub struct XaiAvailableModel {
273    pub name: String,
274    pub display_name: Option<String>,
275    pub max_tokens: u64,
276    pub max_output_tokens: Option<u64>,
277    pub max_completion_tokens: Option<u64>,
278}
279
280#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
281pub struct ZedDotDevSettingsContent {
282    pub available_models: Option<Vec<ZedDotDevAvailableModel>>,
283}
284
285#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
286pub struct ZedDotDevAvailableModel {
287    /// The provider of the language model.
288    pub provider: ZedDotDevAvailableProvider,
289    /// The model's name in the provider's API. e.g. claude-3-5-sonnet-20240620
290    pub name: String,
291    /// The name displayed in the UI, such as in the assistant panel model dropdown menu.
292    pub display_name: Option<String>,
293    /// The size of the context window, indicating the maximum number of tokens the model can process.
294    pub max_tokens: usize,
295    /// The maximum number of output tokens allowed by the model.
296    pub max_output_tokens: Option<u64>,
297    /// The maximum number of completion tokens allowed by the model (o1-* only)
298    pub max_completion_tokens: Option<u64>,
299    /// Override this model with a different Anthropic model for tool calls.
300    pub tool_override: Option<String>,
301    /// Indicates whether this custom model supports caching.
302    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
303    /// The default temperature to use for this model.
304    pub default_temperature: Option<f32>,
305    /// Any extra beta headers to provide when using the model.
306    #[serde(default)]
307    pub extra_beta_headers: Vec<String>,
308    /// The model's mode (e.g. thinking)
309    pub mode: Option<ModelMode>,
310}
311
312#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
313#[serde(rename_all = "lowercase")]
314pub enum ZedDotDevAvailableProvider {
315    Anthropic,
316    OpenAi,
317    Google,
318}
319
320#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, JsonSchema)]
321pub struct OpenRouterSettingsContent {
322    pub api_url: Option<String>,
323    pub available_models: Option<Vec<OpenRouterAvailableModel>>,
324}
325
326#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
327pub struct OpenRouterAvailableModel {
328    pub name: String,
329    pub display_name: Option<String>,
330    pub max_tokens: u64,
331    pub max_output_tokens: Option<u64>,
332    pub max_completion_tokens: Option<u64>,
333    pub supports_tools: Option<bool>,
334    pub supports_images: Option<bool>,
335    pub mode: Option<ModelMode>,
336    pub provider: Option<OpenRouterProvider>,
337}
338
339#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
340pub struct OpenRouterProvider {
341    #[serde(skip_serializing_if = "Option::is_none")]
342    order: Option<Vec<String>>,
343    #[serde(default = "default_true")]
344    allow_fallbacks: bool,
345    #[serde(default)]
346    require_parameters: bool,
347    #[serde(default)]
348    data_collection: DataCollection,
349    #[serde(skip_serializing_if = "Option::is_none")]
350    only: Option<Vec<String>>,
351    #[serde(skip_serializing_if = "Option::is_none")]
352    ignore: Option<Vec<String>>,
353    #[serde(skip_serializing_if = "Option::is_none")]
354    quantizations: Option<Vec<String>>,
355    #[serde(skip_serializing_if = "Option::is_none")]
356    sort: Option<String>,
357}
358
359#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
360#[serde(rename_all = "lowercase")]
361pub enum DataCollection {
362    Allow,
363    Disallow,
364}
365
366impl Default for DataCollection {
367    fn default() -> Self {
368        Self::Allow
369    }
370}
371
372fn default_true() -> bool {
373    true
374}
375
376/// Configuration for caching language model messages.
377#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
378pub struct LanguageModelCacheConfiguration {
379    pub max_cache_anchors: usize,
380    pub should_speculate: bool,
381    pub min_total_token: u64,
382}
383
384#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
385#[serde(tag = "type", rename_all = "lowercase")]
386pub enum ModelMode {
387    #[default]
388    Default,
389    Thinking {
390        /// The maximum number of tokens to use for reasoning. Must be lower than the model's `max_output_tokens`.
391        budget_tokens: Option<u32>,
392    },
393}