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 "copilot_chat".into(),
384 ]),
385 ..Default::default()
386 }
387 .into()
388}
389
390impl Default for AssistantDefaultModel {
391 fn default() -> Self {
392 Self {
393 provider: "openai".to_string(),
394 model: "gpt-4".to_string(),
395 }
396 }
397}
398
399#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
400pub struct AssistantSettingsContentV1 {
401 /// Whether the Assistant is enabled.
402 ///
403 /// Default: true
404 enabled: Option<bool>,
405 /// Whether to show the assistant panel button in the status bar.
406 ///
407 /// Default: true
408 button: Option<bool>,
409 /// Where to dock the assistant.
410 ///
411 /// Default: right
412 dock: Option<AssistantDockPosition>,
413 /// Default width in pixels when the assistant is docked to the left or right.
414 ///
415 /// Default: 640
416 default_width: Option<f32>,
417 /// Default height in pixels when the assistant is docked to the bottom.
418 ///
419 /// Default: 320
420 default_height: Option<f32>,
421 /// The provider of the assistant service.
422 ///
423 /// This can be "openai", "anthropic", "ollama", "zed.dev"
424 /// each with their respective default models and configurations.
425 provider: Option<AssistantProviderContentV1>,
426}
427
428#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
429pub struct LegacyAssistantSettingsContent {
430 /// Whether to show the assistant panel button in the status bar.
431 ///
432 /// Default: true
433 pub button: Option<bool>,
434 /// Where to dock the assistant.
435 ///
436 /// Default: right
437 pub dock: Option<AssistantDockPosition>,
438 /// Default width in pixels when the assistant is docked to the left or right.
439 ///
440 /// Default: 640
441 pub default_width: Option<f32>,
442 /// Default height in pixels when the assistant is docked to the bottom.
443 ///
444 /// Default: 320
445 pub default_height: Option<f32>,
446 /// The default OpenAI model to use when creating new contexts.
447 ///
448 /// Default: gpt-4-1106-preview
449 pub default_open_ai_model: Option<OpenAiModel>,
450 /// OpenAI API base URL to use when creating new contexts.
451 ///
452 /// Default: https://api.openai.com/v1
453 pub openai_api_url: Option<String>,
454}
455
456impl Settings for AssistantSettings {
457 const KEY: Option<&'static str> = Some("assistant");
458
459 type FileContent = AssistantSettingsContent;
460
461 fn load(
462 sources: SettingsSources<Self::FileContent>,
463 _: &mut gpui::AppContext,
464 ) -> anyhow::Result<Self> {
465 let mut settings = AssistantSettings::default();
466
467 for value in sources.defaults_and_customizations() {
468 if value.is_version_outdated() {
469 settings.using_outdated_settings_version = true;
470 }
471
472 let value = value.upgrade();
473 merge(&mut settings.enabled, value.enabled);
474 merge(&mut settings.button, value.button);
475 merge(&mut settings.dock, value.dock);
476 merge(
477 &mut settings.default_width,
478 value.default_width.map(Into::into),
479 );
480 merge(
481 &mut settings.default_height,
482 value.default_height.map(Into::into),
483 );
484 merge(
485 &mut settings.default_model,
486 value.default_model.map(Into::into),
487 );
488 }
489
490 Ok(settings)
491 }
492}
493
494fn merge<T>(target: &mut T, value: Option<T>) {
495 if let Some(value) = value {
496 *target = value;
497 }
498}
499
500// #[cfg(test)]
501// mod tests {
502// use gpui::{AppContext, UpdateGlobal};
503// use settings::SettingsStore;
504
505// use super::*;
506
507// #[gpui::test]
508// fn test_deserialize_assistant_settings(cx: &mut AppContext) {
509// let store = settings::SettingsStore::test(cx);
510// cx.set_global(store);
511
512// // Settings default to gpt-4-turbo.
513// AssistantSettings::register(cx);
514// assert_eq!(
515// AssistantSettings::get_global(cx).provider,
516// AssistantProvider::OpenAi {
517// model: OpenAiModel::FourOmni,
518// api_url: open_ai::OPEN_AI_API_URL.into(),
519// low_speed_timeout_in_seconds: None,
520// available_models: Default::default(),
521// }
522// );
523
524// // Ensure backward-compatibility.
525// SettingsStore::update_global(cx, |store, cx| {
526// store
527// .set_user_settings(
528// r#"{
529// "assistant": {
530// "openai_api_url": "test-url",
531// }
532// }"#,
533// cx,
534// )
535// .unwrap();
536// });
537// assert_eq!(
538// AssistantSettings::get_global(cx).provider,
539// AssistantProvider::OpenAi {
540// model: OpenAiModel::FourOmni,
541// api_url: "test-url".into(),
542// low_speed_timeout_in_seconds: None,
543// available_models: Default::default(),
544// }
545// );
546// SettingsStore::update_global(cx, |store, cx| {
547// store
548// .set_user_settings(
549// r#"{
550// "assistant": {
551// "default_open_ai_model": "gpt-4-0613"
552// }
553// }"#,
554// cx,
555// )
556// .unwrap();
557// });
558// assert_eq!(
559// AssistantSettings::get_global(cx).provider,
560// AssistantProvider::OpenAi {
561// model: OpenAiModel::Four,
562// api_url: open_ai::OPEN_AI_API_URL.into(),
563// low_speed_timeout_in_seconds: None,
564// available_models: Default::default(),
565// }
566// );
567
568// // The new version supports setting a custom model when using zed.dev.
569// SettingsStore::update_global(cx, |store, cx| {
570// store
571// .set_user_settings(
572// r#"{
573// "assistant": {
574// "version": "1",
575// "provider": {
576// "name": "zed.dev",
577// "default_model": {
578// "custom": {
579// "name": "custom-provider"
580// }
581// }
582// }
583// }
584// }"#,
585// cx,
586// )
587// .unwrap();
588// });
589// assert_eq!(
590// AssistantSettings::get_global(cx).provider,
591// AssistantProvider::ZedDotDev {
592// model: CloudModel::Custom {
593// name: "custom-provider".into(),
594// max_tokens: None
595// }
596// }
597// );
598// }
599// }