assistant_settings.rs

  1use std::sync::Arc;
  2
  3use anthropic::Model as AnthropicModel;
  4use fs::Fs;
  5use gpui::{AppContext, Pixels};
  6use language_model::{settings::AllLanguageModelSettings, CloudModel, LanguageModel};
  7use ollama::Model as OllamaModel;
  8use open_ai::Model as OpenAiModel;
  9use schemars::{schema::Schema, JsonSchema};
 10use serde::{Deserialize, Serialize};
 11use settings::{update_settings_file, Settings, SettingsSources};
 12
 13#[derive(Copy, Clone, Default, Debug, Serialize, Deserialize, JsonSchema)]
 14#[serde(rename_all = "snake_case")]
 15pub enum AssistantDockPosition {
 16    Left,
 17    #[default]
 18    Right,
 19    Bottom,
 20}
 21
 22#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
 23#[serde(tag = "name", rename_all = "snake_case")]
 24pub enum AssistantProviderContentV1 {
 25    #[serde(rename = "zed.dev")]
 26    ZedDotDev { default_model: Option<CloudModel> },
 27    #[serde(rename = "openai")]
 28    OpenAi {
 29        default_model: Option<OpenAiModel>,
 30        api_url: Option<String>,
 31        low_speed_timeout_in_seconds: Option<u64>,
 32        available_models: Option<Vec<OpenAiModel>>,
 33    },
 34    #[serde(rename = "anthropic")]
 35    Anthropic {
 36        default_model: Option<AnthropicModel>,
 37        api_url: Option<String>,
 38        low_speed_timeout_in_seconds: Option<u64>,
 39    },
 40    #[serde(rename = "ollama")]
 41    Ollama {
 42        default_model: Option<OllamaModel>,
 43        api_url: Option<String>,
 44        low_speed_timeout_in_seconds: Option<u64>,
 45    },
 46}
 47
 48#[derive(Debug, Default)]
 49pub struct AssistantSettings {
 50    pub enabled: bool,
 51    pub button: bool,
 52    pub dock: AssistantDockPosition,
 53    pub default_width: Pixels,
 54    pub default_height: Pixels,
 55    pub default_model: AssistantDefaultModel,
 56    pub using_outdated_settings_version: bool,
 57}
 58
 59/// Assistant panel settings
 60#[derive(Clone, Serialize, Deserialize, Debug)]
 61#[serde(untagged)]
 62pub enum AssistantSettingsContent {
 63    Versioned(VersionedAssistantSettingsContent),
 64    Legacy(LegacyAssistantSettingsContent),
 65}
 66
 67impl JsonSchema for AssistantSettingsContent {
 68    fn schema_name() -> String {
 69        VersionedAssistantSettingsContent::schema_name()
 70    }
 71
 72    fn json_schema(gen: &mut schemars::gen::SchemaGenerator) -> Schema {
 73        VersionedAssistantSettingsContent::json_schema(gen)
 74    }
 75
 76    fn is_referenceable() -> bool {
 77        VersionedAssistantSettingsContent::is_referenceable()
 78    }
 79}
 80
 81impl Default for AssistantSettingsContent {
 82    fn default() -> Self {
 83        Self::Versioned(VersionedAssistantSettingsContent::default())
 84    }
 85}
 86
 87impl AssistantSettingsContent {
 88    pub fn is_version_outdated(&self) -> bool {
 89        match self {
 90            AssistantSettingsContent::Versioned(settings) => match settings {
 91                VersionedAssistantSettingsContent::V1(_) => true,
 92                VersionedAssistantSettingsContent::V2(_) => false,
 93            },
 94            AssistantSettingsContent::Legacy(_) => true,
 95        }
 96    }
 97
 98    pub fn update_file(&mut self, fs: Arc<dyn Fs>, cx: &AppContext) {
 99        if let AssistantSettingsContent::Versioned(settings) = self {
100            if let VersionedAssistantSettingsContent::V1(settings) = settings {
101                if let Some(provider) = settings.provider.clone() {
102                    match provider {
103                        AssistantProviderContentV1::Anthropic {
104                            api_url,
105                            low_speed_timeout_in_seconds,
106                            ..
107                        } => update_settings_file::<AllLanguageModelSettings>(
108                            fs,
109                            cx,
110                            move |content, _| {
111                                if content.anthropic.is_none() {
112                                    content.anthropic =
113                                        Some(language_model::settings::AnthropicSettingsContent {
114                                            api_url,
115                                            low_speed_timeout_in_seconds,
116                                            ..Default::default()
117                                        });
118                                }
119                            },
120                        ),
121                        AssistantProviderContentV1::Ollama {
122                            api_url,
123                            low_speed_timeout_in_seconds,
124                            ..
125                        } => update_settings_file::<AllLanguageModelSettings>(
126                            fs,
127                            cx,
128                            move |content, _| {
129                                if content.ollama.is_none() {
130                                    content.ollama =
131                                        Some(language_model::settings::OllamaSettingsContent {
132                                            api_url,
133                                            low_speed_timeout_in_seconds,
134                                        });
135                                }
136                            },
137                        ),
138                        AssistantProviderContentV1::OpenAi {
139                            api_url,
140                            low_speed_timeout_in_seconds,
141                            available_models,
142                            ..
143                        } => update_settings_file::<AllLanguageModelSettings>(
144                            fs,
145                            cx,
146                            move |content, _| {
147                                if content.openai.is_none() {
148                                    content.openai =
149                                        Some(language_model::settings::OpenAiSettingsContent {
150                                            api_url,
151                                            low_speed_timeout_in_seconds,
152                                            available_models,
153                                        });
154                                }
155                            },
156                        ),
157                        _ => {}
158                    }
159                }
160            }
161        }
162
163        *self = AssistantSettingsContent::Versioned(VersionedAssistantSettingsContent::V2(
164            self.upgrade(),
165        ));
166    }
167
168    fn upgrade(&self) -> AssistantSettingsContentV2 {
169        match self {
170            AssistantSettingsContent::Versioned(settings) => match settings {
171                VersionedAssistantSettingsContent::V1(settings) => AssistantSettingsContentV2 {
172                    enabled: settings.enabled,
173                    button: settings.button,
174                    dock: settings.dock,
175                    default_width: settings.default_width,
176                    default_height: settings.default_width,
177                    default_model: settings
178                        .provider
179                        .clone()
180                        .and_then(|provider| match provider {
181                            AssistantProviderContentV1::ZedDotDev { default_model } => {
182                                default_model.map(|model| AssistantDefaultModel {
183                                    provider: "zed.dev".to_string(),
184                                    model: model.id().to_string(),
185                                })
186                            }
187                            AssistantProviderContentV1::OpenAi { default_model, .. } => {
188                                default_model.map(|model| AssistantDefaultModel {
189                                    provider: "openai".to_string(),
190                                    model: model.id().to_string(),
191                                })
192                            }
193                            AssistantProviderContentV1::Anthropic { default_model, .. } => {
194                                default_model.map(|model| AssistantDefaultModel {
195                                    provider: "anthropic".to_string(),
196                                    model: model.id().to_string(),
197                                })
198                            }
199                            AssistantProviderContentV1::Ollama { default_model, .. } => {
200                                default_model.map(|model| AssistantDefaultModel {
201                                    provider: "ollama".to_string(),
202                                    model: model.id().to_string(),
203                                })
204                            }
205                        }),
206                },
207                VersionedAssistantSettingsContent::V2(settings) => settings.clone(),
208            },
209            AssistantSettingsContent::Legacy(settings) => AssistantSettingsContentV2 {
210                enabled: None,
211                button: settings.button,
212                dock: settings.dock,
213                default_width: settings.default_width,
214                default_height: settings.default_height,
215                default_model: Some(AssistantDefaultModel {
216                    provider: "openai".to_string(),
217                    model: settings
218                        .default_open_ai_model
219                        .clone()
220                        .unwrap_or_default()
221                        .id()
222                        .to_string(),
223                }),
224            },
225        }
226    }
227
228    pub fn set_dock(&mut self, dock: AssistantDockPosition) {
229        match self {
230            AssistantSettingsContent::Versioned(settings) => match settings {
231                VersionedAssistantSettingsContent::V1(settings) => {
232                    settings.dock = Some(dock);
233                }
234                VersionedAssistantSettingsContent::V2(settings) => {
235                    settings.dock = Some(dock);
236                }
237            },
238            AssistantSettingsContent::Legacy(settings) => {
239                settings.dock = Some(dock);
240            }
241        }
242    }
243
244    pub fn set_model(&mut self, language_model: Arc<dyn LanguageModel>) {
245        let model = language_model.id().0.to_string();
246        let provider = language_model.provider_id().0.to_string();
247
248        match self {
249            AssistantSettingsContent::Versioned(settings) => match settings {
250                VersionedAssistantSettingsContent::V1(settings) => match provider.as_ref() {
251                    "zed.dev" => {
252                        log::warn!("attempted to set zed.dev model on outdated settings");
253                    }
254                    "anthropic" => {
255                        let (api_url, low_speed_timeout_in_seconds) = match &settings.provider {
256                            Some(AssistantProviderContentV1::Anthropic {
257                                api_url,
258                                low_speed_timeout_in_seconds,
259                                ..
260                            }) => (api_url.clone(), *low_speed_timeout_in_seconds),
261                            _ => (None, None),
262                        };
263                        settings.provider = Some(AssistantProviderContentV1::Anthropic {
264                            default_model: AnthropicModel::from_id(&model).ok(),
265                            api_url,
266                            low_speed_timeout_in_seconds,
267                        });
268                    }
269                    "ollama" => {
270                        let (api_url, low_speed_timeout_in_seconds) = match &settings.provider {
271                            Some(AssistantProviderContentV1::Ollama {
272                                api_url,
273                                low_speed_timeout_in_seconds,
274                                ..
275                            }) => (api_url.clone(), *low_speed_timeout_in_seconds),
276                            _ => (None, None),
277                        };
278                        settings.provider = Some(AssistantProviderContentV1::Ollama {
279                            default_model: Some(ollama::Model::new(&model)),
280                            api_url,
281                            low_speed_timeout_in_seconds,
282                        });
283                    }
284                    "openai" => {
285                        let (api_url, low_speed_timeout_in_seconds, available_models) =
286                            match &settings.provider {
287                                Some(AssistantProviderContentV1::OpenAi {
288                                    api_url,
289                                    low_speed_timeout_in_seconds,
290                                    available_models,
291                                    ..
292                                }) => (
293                                    api_url.clone(),
294                                    *low_speed_timeout_in_seconds,
295                                    available_models.clone(),
296                                ),
297                                _ => (None, None, None),
298                            };
299                        settings.provider = Some(AssistantProviderContentV1::OpenAi {
300                            default_model: open_ai::Model::from_id(&model).ok(),
301                            api_url,
302                            low_speed_timeout_in_seconds,
303                            available_models,
304                        });
305                    }
306                    _ => {}
307                },
308                VersionedAssistantSettingsContent::V2(settings) => {
309                    settings.default_model = Some(AssistantDefaultModel { provider, model });
310                }
311            },
312            AssistantSettingsContent::Legacy(settings) => {
313                if let Ok(model) = open_ai::Model::from_id(&language_model.id().0) {
314                    settings.default_open_ai_model = Some(model);
315                }
316            }
317        }
318    }
319}
320
321#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
322#[serde(tag = "version")]
323pub enum VersionedAssistantSettingsContent {
324    #[serde(rename = "1")]
325    V1(AssistantSettingsContentV1),
326    #[serde(rename = "2")]
327    V2(AssistantSettingsContentV2),
328}
329
330impl Default for VersionedAssistantSettingsContent {
331    fn default() -> Self {
332        Self::V2(AssistantSettingsContentV2 {
333            enabled: None,
334            button: None,
335            dock: None,
336            default_width: None,
337            default_height: None,
338            default_model: None,
339        })
340    }
341}
342
343#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
344pub struct AssistantSettingsContentV2 {
345    /// Whether the Assistant is enabled.
346    ///
347    /// Default: true
348    enabled: Option<bool>,
349    /// Whether to show the assistant panel button in the status bar.
350    ///
351    /// Default: true
352    button: Option<bool>,
353    /// Where to dock the assistant.
354    ///
355    /// Default: right
356    dock: Option<AssistantDockPosition>,
357    /// Default width in pixels when the assistant is docked to the left or right.
358    ///
359    /// Default: 640
360    default_width: Option<f32>,
361    /// Default height in pixels when the assistant is docked to the bottom.
362    ///
363    /// Default: 320
364    default_height: Option<f32>,
365    /// The default model to use when creating new contexts.
366    default_model: Option<AssistantDefaultModel>,
367}
368
369#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, PartialEq)]
370pub struct AssistantDefaultModel {
371    #[schemars(schema_with = "providers_schema")]
372    pub provider: String,
373    pub model: String,
374}
375
376fn providers_schema(_: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
377    schemars::schema::SchemaObject {
378        enum_values: Some(vec![
379            "anthropic".into(),
380            "ollama".into(),
381            "openai".into(),
382            "zed.dev".into(),
383        ]),
384        ..Default::default()
385    }
386    .into()
387}
388
389impl Default for AssistantDefaultModel {
390    fn default() -> Self {
391        Self {
392            provider: "openai".to_string(),
393            model: "gpt-4".to_string(),
394        }
395    }
396}
397
398#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
399pub struct AssistantSettingsContentV1 {
400    /// Whether the Assistant is enabled.
401    ///
402    /// Default: true
403    enabled: Option<bool>,
404    /// Whether to show the assistant panel button in the status bar.
405    ///
406    /// Default: true
407    button: Option<bool>,
408    /// Where to dock the assistant.
409    ///
410    /// Default: right
411    dock: Option<AssistantDockPosition>,
412    /// Default width in pixels when the assistant is docked to the left or right.
413    ///
414    /// Default: 640
415    default_width: Option<f32>,
416    /// Default height in pixels when the assistant is docked to the bottom.
417    ///
418    /// Default: 320
419    default_height: Option<f32>,
420    /// The provider of the assistant service.
421    ///
422    /// This can either be the internal `zed.dev` service or an external `openai` service,
423    /// each with their respective default models and configurations.
424    provider: Option<AssistantProviderContentV1>,
425}
426
427#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
428pub struct LegacyAssistantSettingsContent {
429    /// Whether to show the assistant panel button in the status bar.
430    ///
431    /// Default: true
432    pub button: Option<bool>,
433    /// Where to dock the assistant.
434    ///
435    /// Default: right
436    pub dock: Option<AssistantDockPosition>,
437    /// Default width in pixels when the assistant is docked to the left or right.
438    ///
439    /// Default: 640
440    pub default_width: Option<f32>,
441    /// Default height in pixels when the assistant is docked to the bottom.
442    ///
443    /// Default: 320
444    pub default_height: Option<f32>,
445    /// The default OpenAI model to use when creating new contexts.
446    ///
447    /// Default: gpt-4-1106-preview
448    pub default_open_ai_model: Option<OpenAiModel>,
449    /// OpenAI API base URL to use when creating new contexts.
450    ///
451    /// Default: https://api.openai.com/v1
452    pub openai_api_url: Option<String>,
453}
454
455impl Settings for AssistantSettings {
456    const KEY: Option<&'static str> = Some("assistant");
457
458    type FileContent = AssistantSettingsContent;
459
460    fn load(
461        sources: SettingsSources<Self::FileContent>,
462        _: &mut gpui::AppContext,
463    ) -> anyhow::Result<Self> {
464        let mut settings = AssistantSettings::default();
465
466        for value in sources.defaults_and_customizations() {
467            if value.is_version_outdated() {
468                settings.using_outdated_settings_version = true;
469            }
470
471            let value = value.upgrade();
472            merge(&mut settings.enabled, value.enabled);
473            merge(&mut settings.button, value.button);
474            merge(&mut settings.dock, value.dock);
475            merge(
476                &mut settings.default_width,
477                value.default_width.map(Into::into),
478            );
479            merge(
480                &mut settings.default_height,
481                value.default_height.map(Into::into),
482            );
483            merge(
484                &mut settings.default_model,
485                value.default_model.map(Into::into),
486            );
487        }
488
489        Ok(settings)
490    }
491}
492
493fn merge<T>(target: &mut T, value: Option<T>) {
494    if let Some(value) = value {
495        *target = value;
496    }
497}
498
499// #[cfg(test)]
500// mod tests {
501//     use gpui::{AppContext, UpdateGlobal};
502//     use settings::SettingsStore;
503
504//     use super::*;
505
506//     #[gpui::test]
507//     fn test_deserialize_assistant_settings(cx: &mut AppContext) {
508//         let store = settings::SettingsStore::test(cx);
509//         cx.set_global(store);
510
511//         // Settings default to gpt-4-turbo.
512//         AssistantSettings::register(cx);
513//         assert_eq!(
514//             AssistantSettings::get_global(cx).provider,
515//             AssistantProvider::OpenAi {
516//                 model: OpenAiModel::FourOmni,
517//                 api_url: open_ai::OPEN_AI_API_URL.into(),
518//                 low_speed_timeout_in_seconds: None,
519//                 available_models: Default::default(),
520//             }
521//         );
522
523//         // Ensure backward-compatibility.
524//         SettingsStore::update_global(cx, |store, cx| {
525//             store
526//                 .set_user_settings(
527//                     r#"{
528//                         "assistant": {
529//                             "openai_api_url": "test-url",
530//                         }
531//                     }"#,
532//                     cx,
533//                 )
534//                 .unwrap();
535//         });
536//         assert_eq!(
537//             AssistantSettings::get_global(cx).provider,
538//             AssistantProvider::OpenAi {
539//                 model: OpenAiModel::FourOmni,
540//                 api_url: "test-url".into(),
541//                 low_speed_timeout_in_seconds: None,
542//                 available_models: Default::default(),
543//             }
544//         );
545//         SettingsStore::update_global(cx, |store, cx| {
546//             store
547//                 .set_user_settings(
548//                     r#"{
549//                         "assistant": {
550//                             "default_open_ai_model": "gpt-4-0613"
551//                         }
552//                     }"#,
553//                     cx,
554//                 )
555//                 .unwrap();
556//         });
557//         assert_eq!(
558//             AssistantSettings::get_global(cx).provider,
559//             AssistantProvider::OpenAi {
560//                 model: OpenAiModel::Four,
561//                 api_url: open_ai::OPEN_AI_API_URL.into(),
562//                 low_speed_timeout_in_seconds: None,
563//                 available_models: Default::default(),
564//             }
565//         );
566
567//         // The new version supports setting a custom model when using zed.dev.
568//         SettingsStore::update_global(cx, |store, cx| {
569//             store
570//                 .set_user_settings(
571//                     r#"{
572//                         "assistant": {
573//                             "version": "1",
574//                             "provider": {
575//                                 "name": "zed.dev",
576//                                 "default_model": {
577//                                     "custom": {
578//                                         "name": "custom-provider"
579//                                     }
580//                                 }
581//                             }
582//                         }
583//                     }"#,
584//                     cx,
585//                 )
586//                 .unwrap();
587//         });
588//         assert_eq!(
589//             AssistantSettings::get_global(cx).provider,
590//             AssistantProvider::ZedDotDev {
591//                 model: CloudModel::Custom {
592//                     name: "custom-provider".into(),
593//                     max_tokens: None
594//                 }
595//             }
596//         );
597//     }
598// }