cloud.rs

  1use super::open_ai::count_open_ai_tokens;
  2use crate::{
  3    settings::AllLanguageModelSettings, CloudModel, LanguageModel, LanguageModelCacheConfiguration,
  4    LanguageModelId, LanguageModelName, LanguageModelProviderId, LanguageModelProviderName,
  5    LanguageModelProviderState, LanguageModelRequest, RateLimiter, ZedModel,
  6};
  7use anthropic::AnthropicError;
  8use anyhow::{anyhow, Result};
  9use client::{Client, PerformCompletionParams, UserStore, EXPIRED_LLM_TOKEN_HEADER_NAME};
 10use collections::BTreeMap;
 11use feature_flags::{FeatureFlagAppExt, LlmClosedBeta, ZedPro};
 12use futures::{
 13    future::BoxFuture, stream::BoxStream, AsyncBufReadExt, FutureExt, Stream, StreamExt,
 14    TryStreamExt as _,
 15};
 16use gpui::{
 17    AnyElement, AnyView, AppContext, AsyncAppContext, FontWeight, Model, ModelContext,
 18    Subscription, Task,
 19};
 20use http_client::{AsyncBody, HttpClient, Method, Response};
 21use schemars::JsonSchema;
 22use serde::{de::DeserializeOwned, Deserialize, Serialize};
 23use serde_json::value::RawValue;
 24use settings::{Settings, SettingsStore};
 25use smol::{
 26    io::{AsyncReadExt, BufReader},
 27    lock::{RwLock, RwLockUpgradableReadGuard, RwLockWriteGuard},
 28};
 29use std::{
 30    future,
 31    sync::{Arc, LazyLock},
 32};
 33use strum::IntoEnumIterator;
 34use ui::prelude::*;
 35
 36use crate::{LanguageModelAvailability, LanguageModelProvider};
 37
 38use super::anthropic::count_anthropic_tokens;
 39
 40pub const PROVIDER_ID: &str = "zed.dev";
 41pub const PROVIDER_NAME: &str = "Zed";
 42
 43const ZED_CLOUD_PROVIDER_ADDITIONAL_MODELS_JSON: Option<&str> =
 44    option_env!("ZED_CLOUD_PROVIDER_ADDITIONAL_MODELS_JSON");
 45
 46fn zed_cloud_provider_additional_models() -> &'static [AvailableModel] {
 47    static ADDITIONAL_MODELS: LazyLock<Vec<AvailableModel>> = LazyLock::new(|| {
 48        ZED_CLOUD_PROVIDER_ADDITIONAL_MODELS_JSON
 49            .map(|json| serde_json::from_str(json).unwrap())
 50            .unwrap_or(Vec::new())
 51    });
 52    ADDITIONAL_MODELS.as_slice()
 53}
 54
 55#[derive(Default, Clone, Debug, PartialEq)]
 56pub struct ZedDotDevSettings {
 57    pub available_models: Vec<AvailableModel>,
 58}
 59
 60#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 61#[serde(rename_all = "lowercase")]
 62pub enum AvailableProvider {
 63    Anthropic,
 64    OpenAi,
 65    Google,
 66}
 67
 68#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
 69pub struct AvailableModel {
 70    /// The provider of the language model.
 71    pub provider: AvailableProvider,
 72    /// The model's name in the provider's API. e.g. claude-3-5-sonnet-20240620
 73    pub name: String,
 74    /// The name displayed in the UI, such as in the assistant panel model dropdown menu.
 75    pub display_name: Option<String>,
 76    /// The size of the context window, indicating the maximum number of tokens the model can process.
 77    pub max_tokens: usize,
 78    /// The maximum number of output tokens allowed by the model.
 79    pub max_output_tokens: Option<u32>,
 80    /// Override this model with a different Anthropic model for tool calls.
 81    pub tool_override: Option<String>,
 82    /// Indicates whether this custom model supports caching.
 83    pub cache_configuration: Option<LanguageModelCacheConfiguration>,
 84}
 85
 86pub struct CloudLanguageModelProvider {
 87    client: Arc<Client>,
 88    llm_api_token: LlmApiToken,
 89    state: gpui::Model<State>,
 90    _maintain_client_status: Task<()>,
 91}
 92
 93pub struct State {
 94    client: Arc<Client>,
 95    user_store: Model<UserStore>,
 96    status: client::Status,
 97    accept_terms: Option<Task<Result<()>>>,
 98    _subscription: Subscription,
 99}
100
101impl State {
102    fn is_signed_out(&self) -> bool {
103        self.status.is_signed_out()
104    }
105
106    fn authenticate(&self, cx: &mut ModelContext<Self>) -> Task<Result<()>> {
107        let client = self.client.clone();
108        cx.spawn(move |this, mut cx| async move {
109            client.authenticate_and_connect(true, &cx).await?;
110            this.update(&mut cx, |_, cx| cx.notify())
111        })
112    }
113
114    fn has_accepted_terms_of_service(&self, cx: &AppContext) -> bool {
115        self.user_store
116            .read(cx)
117            .current_user_has_accepted_terms()
118            .unwrap_or(false)
119    }
120
121    fn accept_terms_of_service(&mut self, cx: &mut ModelContext<Self>) {
122        let user_store = self.user_store.clone();
123        self.accept_terms = Some(cx.spawn(move |this, mut cx| async move {
124            let _ = user_store
125                .update(&mut cx, |store, cx| store.accept_terms_of_service(cx))?
126                .await;
127            this.update(&mut cx, |this, cx| {
128                this.accept_terms = None;
129                cx.notify()
130            })
131        }));
132    }
133}
134
135impl CloudLanguageModelProvider {
136    pub fn new(user_store: Model<UserStore>, client: Arc<Client>, cx: &mut AppContext) -> Self {
137        let mut status_rx = client.status();
138        let status = *status_rx.borrow();
139
140        let state = cx.new_model(|cx| State {
141            client: client.clone(),
142            user_store,
143            status,
144            accept_terms: None,
145            _subscription: cx.observe_global::<SettingsStore>(|_, cx| {
146                cx.notify();
147            }),
148        });
149
150        let state_ref = state.downgrade();
151        let maintain_client_status = cx.spawn(|mut cx| async move {
152            while let Some(status) = status_rx.next().await {
153                if let Some(this) = state_ref.upgrade() {
154                    _ = this.update(&mut cx, |this, cx| {
155                        if this.status != status {
156                            this.status = status;
157                            cx.notify();
158                        }
159                    });
160                } else {
161                    break;
162                }
163            }
164        });
165
166        Self {
167            client,
168            state,
169            llm_api_token: LlmApiToken::default(),
170            _maintain_client_status: maintain_client_status,
171        }
172    }
173}
174
175impl LanguageModelProviderState for CloudLanguageModelProvider {
176    type ObservableEntity = State;
177
178    fn observable_entity(&self) -> Option<gpui::Model<Self::ObservableEntity>> {
179        Some(self.state.clone())
180    }
181}
182
183impl LanguageModelProvider for CloudLanguageModelProvider {
184    fn id(&self) -> LanguageModelProviderId {
185        LanguageModelProviderId(PROVIDER_ID.into())
186    }
187
188    fn name(&self) -> LanguageModelProviderName {
189        LanguageModelProviderName(PROVIDER_NAME.into())
190    }
191
192    fn icon(&self) -> IconName {
193        IconName::AiZed
194    }
195
196    fn provided_models(&self, cx: &AppContext) -> Vec<Arc<dyn LanguageModel>> {
197        let mut models = BTreeMap::default();
198
199        if cx.is_staff() {
200            for model in anthropic::Model::iter() {
201                if !matches!(model, anthropic::Model::Custom { .. }) {
202                    models.insert(model.id().to_string(), CloudModel::Anthropic(model));
203                }
204            }
205            for model in open_ai::Model::iter() {
206                if !matches!(model, open_ai::Model::Custom { .. }) {
207                    models.insert(model.id().to_string(), CloudModel::OpenAi(model));
208                }
209            }
210            for model in google_ai::Model::iter() {
211                if !matches!(model, google_ai::Model::Custom { .. }) {
212                    models.insert(model.id().to_string(), CloudModel::Google(model));
213                }
214            }
215            for model in ZedModel::iter() {
216                models.insert(model.id().to_string(), CloudModel::Zed(model));
217            }
218        } else {
219            models.insert(
220                anthropic::Model::Claude3_5Sonnet.id().to_string(),
221                CloudModel::Anthropic(anthropic::Model::Claude3_5Sonnet),
222            );
223        }
224
225        let llm_closed_beta_models = if cx.has_flag::<LlmClosedBeta>() {
226            zed_cloud_provider_additional_models()
227        } else {
228            &[]
229        };
230
231        // Override with available models from settings
232        for model in AllLanguageModelSettings::get_global(cx)
233            .zed_dot_dev
234            .available_models
235            .iter()
236            .chain(llm_closed_beta_models)
237            .cloned()
238        {
239            let model = match model.provider {
240                AvailableProvider::Anthropic => CloudModel::Anthropic(anthropic::Model::Custom {
241                    name: model.name.clone(),
242                    display_name: model.display_name.clone(),
243                    max_tokens: model.max_tokens,
244                    tool_override: model.tool_override.clone(),
245                    cache_configuration: model.cache_configuration.as_ref().map(|config| {
246                        anthropic::AnthropicModelCacheConfiguration {
247                            max_cache_anchors: config.max_cache_anchors,
248                            should_speculate: config.should_speculate,
249                            min_total_token: config.min_total_token,
250                        }
251                    }),
252                    max_output_tokens: model.max_output_tokens,
253                }),
254                AvailableProvider::OpenAi => CloudModel::OpenAi(open_ai::Model::Custom {
255                    name: model.name.clone(),
256                    max_tokens: model.max_tokens,
257                    max_output_tokens: model.max_output_tokens,
258                }),
259                AvailableProvider::Google => CloudModel::Google(google_ai::Model::Custom {
260                    name: model.name.clone(),
261                    max_tokens: model.max_tokens,
262                }),
263            };
264            models.insert(model.id().to_string(), model.clone());
265        }
266
267        models
268            .into_values()
269            .map(|model| {
270                Arc::new(CloudLanguageModel {
271                    id: LanguageModelId::from(model.id().to_string()),
272                    model,
273                    llm_api_token: self.llm_api_token.clone(),
274                    client: self.client.clone(),
275                    request_limiter: RateLimiter::new(4),
276                }) as Arc<dyn LanguageModel>
277            })
278            .collect()
279    }
280
281    fn is_authenticated(&self, cx: &AppContext) -> bool {
282        !self.state.read(cx).is_signed_out()
283    }
284
285    fn authenticate(&self, _cx: &mut AppContext) -> Task<Result<()>> {
286        Task::ready(Ok(()))
287    }
288
289    fn configuration_view(&self, cx: &mut WindowContext) -> AnyView {
290        cx.new_view(|_cx| ConfigurationView {
291            state: self.state.clone(),
292        })
293        .into()
294    }
295
296    fn must_accept_terms(&self, cx: &AppContext) -> bool {
297        !self.state.read(cx).has_accepted_terms_of_service(cx)
298    }
299
300    fn render_accept_terms(&self, cx: &mut WindowContext) -> Option<AnyElement> {
301        let state = self.state.read(cx);
302
303        let terms = [(
304            "terms_of_service",
305            "Terms of Service",
306            "https://zed.dev/terms-of-service",
307        )]
308        .map(|(id, label, url)| {
309            Button::new(id, label)
310                .style(ButtonStyle::Subtle)
311                .icon(IconName::ExternalLink)
312                .icon_size(IconSize::XSmall)
313                .icon_color(Color::Muted)
314                .on_click(move |_, cx| cx.open_url(url))
315        });
316
317        if state.has_accepted_terms_of_service(cx) {
318            None
319        } else {
320            let disabled = state.accept_terms.is_some();
321            Some(
322                v_flex()
323                    .gap_2()
324                    .child(
325                        v_flex()
326                            .child(Label::new("Terms and Conditions").weight(FontWeight::MEDIUM))
327                            .child(
328                                Label::new(
329                                    "Please read and accept our terms and conditions to continue.",
330                                )
331                                .size(LabelSize::Small),
332                            ),
333                    )
334                    .child(v_flex().gap_1().children(terms))
335                    .child(
336                        h_flex().justify_end().child(
337                            Button::new("accept_terms", "I've read it and accept it")
338                                .disabled(disabled)
339                                .on_click({
340                                    let state = self.state.downgrade();
341                                    move |_, cx| {
342                                        state
343                                            .update(cx, |state, cx| {
344                                                state.accept_terms_of_service(cx)
345                                            })
346                                            .ok();
347                                    }
348                                }),
349                        ),
350                    )
351                    .into_any(),
352            )
353        }
354    }
355
356    fn reset_credentials(&self, _cx: &mut AppContext) -> Task<Result<()>> {
357        Task::ready(Ok(()))
358    }
359}
360
361pub struct CloudLanguageModel {
362    id: LanguageModelId,
363    model: CloudModel,
364    llm_api_token: LlmApiToken,
365    client: Arc<Client>,
366    request_limiter: RateLimiter,
367}
368
369#[derive(Clone, Default)]
370struct LlmApiToken(Arc<RwLock<Option<String>>>);
371
372impl CloudLanguageModel {
373    async fn perform_llm_completion(
374        client: Arc<Client>,
375        llm_api_token: LlmApiToken,
376        body: PerformCompletionParams,
377    ) -> Result<Response<AsyncBody>> {
378        let http_client = &client.http_client();
379
380        let mut token = llm_api_token.acquire(&client).await?;
381        let mut did_retry = false;
382
383        let response = loop {
384            let request = http_client::Request::builder()
385                .method(Method::POST)
386                .uri(http_client.build_zed_llm_url("/completion", &[])?.as_ref())
387                .header("Content-Type", "application/json")
388                .header("Authorization", format!("Bearer {token}"))
389                .body(serde_json::to_string(&body)?.into())?;
390            let mut response = http_client.send(request).await?;
391            if response.status().is_success() {
392                break response;
393            } else if !did_retry
394                && response
395                    .headers()
396                    .get(EXPIRED_LLM_TOKEN_HEADER_NAME)
397                    .is_some()
398            {
399                did_retry = true;
400                token = llm_api_token.refresh(&client).await?;
401            } else {
402                let mut body = String::new();
403                response.body_mut().read_to_string(&mut body).await?;
404                break Err(anyhow!(
405                    "cloud language model completion failed with status {}: {body}",
406                    response.status()
407                ))?;
408            }
409        };
410
411        Ok(response)
412    }
413}
414
415impl LanguageModel for CloudLanguageModel {
416    fn id(&self) -> LanguageModelId {
417        self.id.clone()
418    }
419
420    fn name(&self) -> LanguageModelName {
421        LanguageModelName::from(self.model.display_name().to_string())
422    }
423
424    fn icon(&self) -> Option<IconName> {
425        self.model.icon()
426    }
427
428    fn provider_id(&self) -> LanguageModelProviderId {
429        LanguageModelProviderId(PROVIDER_ID.into())
430    }
431
432    fn provider_name(&self) -> LanguageModelProviderName {
433        LanguageModelProviderName(PROVIDER_NAME.into())
434    }
435
436    fn telemetry_id(&self) -> String {
437        format!("zed.dev/{}", self.model.id())
438    }
439
440    fn availability(&self) -> LanguageModelAvailability {
441        self.model.availability()
442    }
443
444    fn max_token_count(&self) -> usize {
445        self.model.max_token_count()
446    }
447
448    fn count_tokens(
449        &self,
450        request: LanguageModelRequest,
451        cx: &AppContext,
452    ) -> BoxFuture<'static, Result<usize>> {
453        match self.model.clone() {
454            CloudModel::Anthropic(_) => count_anthropic_tokens(request, cx),
455            CloudModel::OpenAi(model) => count_open_ai_tokens(request, model, cx),
456            CloudModel::Google(model) => {
457                let client = self.client.clone();
458                let request = request.into_google(model.id().into());
459                let request = google_ai::CountTokensRequest {
460                    contents: request.contents,
461                };
462                async move {
463                    let request = serde_json::to_string(&request)?;
464                    let response = client
465                        .request(proto::CountLanguageModelTokens {
466                            provider: proto::LanguageModelProvider::Google as i32,
467                            request,
468                        })
469                        .await?;
470                    Ok(response.token_count as usize)
471                }
472                .boxed()
473            }
474            CloudModel::Zed(_) => {
475                count_open_ai_tokens(request, open_ai::Model::ThreePointFiveTurbo, cx)
476            }
477        }
478    }
479
480    fn stream_completion(
481        &self,
482        request: LanguageModelRequest,
483        _cx: &AsyncAppContext,
484    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
485        match &self.model {
486            CloudModel::Anthropic(model) => {
487                let request = request.into_anthropic(model.id().into(), model.max_output_tokens());
488                let client = self.client.clone();
489                let llm_api_token = self.llm_api_token.clone();
490                let future = self.request_limiter.stream(async move {
491                    let response = Self::perform_llm_completion(
492                        client.clone(),
493                        llm_api_token,
494                        PerformCompletionParams {
495                            provider: client::LanguageModelProvider::Anthropic,
496                            model: request.model.clone(),
497                            provider_request: RawValue::from_string(serde_json::to_string(
498                                &request,
499                            )?)?,
500                        },
501                    )
502                    .await?;
503                    Ok(anthropic::extract_text_from_events(
504                        response_lines(response).map_err(AnthropicError::Other),
505                    ))
506                });
507                async move {
508                    Ok(future
509                        .await?
510                        .map(|result| result.map_err(|err| anyhow!(err)))
511                        .boxed())
512                }
513                .boxed()
514            }
515            CloudModel::OpenAi(model) => {
516                let client = self.client.clone();
517                let request = request.into_open_ai(model.id().into(), model.max_output_tokens());
518                let llm_api_token = self.llm_api_token.clone();
519                let future = self.request_limiter.stream(async move {
520                    let response = Self::perform_llm_completion(
521                        client.clone(),
522                        llm_api_token,
523                        PerformCompletionParams {
524                            provider: client::LanguageModelProvider::OpenAi,
525                            model: request.model.clone(),
526                            provider_request: RawValue::from_string(serde_json::to_string(
527                                &request,
528                            )?)?,
529                        },
530                    )
531                    .await?;
532                    Ok(open_ai::extract_text_from_events(response_lines(response)))
533                });
534                async move { Ok(future.await?.boxed()) }.boxed()
535            }
536            CloudModel::Google(model) => {
537                let client = self.client.clone();
538                let request = request.into_google(model.id().into());
539                let llm_api_token = self.llm_api_token.clone();
540                let future = self.request_limiter.stream(async move {
541                    let response = Self::perform_llm_completion(
542                        client.clone(),
543                        llm_api_token,
544                        PerformCompletionParams {
545                            provider: client::LanguageModelProvider::Google,
546                            model: request.model.clone(),
547                            provider_request: RawValue::from_string(serde_json::to_string(
548                                &request,
549                            )?)?,
550                        },
551                    )
552                    .await?;
553                    Ok(google_ai::extract_text_from_events(response_lines(
554                        response,
555                    )))
556                });
557                async move { Ok(future.await?.boxed()) }.boxed()
558            }
559            CloudModel::Zed(model) => {
560                let client = self.client.clone();
561                let mut request = request.into_open_ai(model.id().into(), None);
562                request.max_tokens = Some(4000);
563                let llm_api_token = self.llm_api_token.clone();
564                let future = self.request_limiter.stream(async move {
565                    let response = Self::perform_llm_completion(
566                        client.clone(),
567                        llm_api_token,
568                        PerformCompletionParams {
569                            provider: client::LanguageModelProvider::Zed,
570                            model: request.model.clone(),
571                            provider_request: RawValue::from_string(serde_json::to_string(
572                                &request,
573                            )?)?,
574                        },
575                    )
576                    .await?;
577                    Ok(open_ai::extract_text_from_events(response_lines(response)))
578                });
579                async move { Ok(future.await?.boxed()) }.boxed()
580            }
581        }
582    }
583
584    fn use_any_tool(
585        &self,
586        request: LanguageModelRequest,
587        tool_name: String,
588        tool_description: String,
589        input_schema: serde_json::Value,
590        _cx: &AsyncAppContext,
591    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
592        let client = self.client.clone();
593        let llm_api_token = self.llm_api_token.clone();
594
595        match &self.model {
596            CloudModel::Anthropic(model) => {
597                let mut request =
598                    request.into_anthropic(model.tool_model_id().into(), model.max_output_tokens());
599                request.tool_choice = Some(anthropic::ToolChoice::Tool {
600                    name: tool_name.clone(),
601                });
602                request.tools = vec![anthropic::Tool {
603                    name: tool_name.clone(),
604                    description: tool_description,
605                    input_schema,
606                }];
607
608                self.request_limiter
609                    .run(async move {
610                        let response = Self::perform_llm_completion(
611                            client.clone(),
612                            llm_api_token,
613                            PerformCompletionParams {
614                                provider: client::LanguageModelProvider::Anthropic,
615                                model: request.model.clone(),
616                                provider_request: RawValue::from_string(serde_json::to_string(
617                                    &request,
618                                )?)?,
619                            },
620                        )
621                        .await?;
622
623                        Ok(anthropic::extract_tool_args_from_events(
624                            tool_name,
625                            Box::pin(response_lines(response)),
626                        )
627                        .await?
628                        .boxed())
629                    })
630                    .boxed()
631            }
632            CloudModel::OpenAi(model) => {
633                let mut request =
634                    request.into_open_ai(model.id().into(), model.max_output_tokens());
635                request.tool_choice = Some(open_ai::ToolChoice::Other(
636                    open_ai::ToolDefinition::Function {
637                        function: open_ai::FunctionDefinition {
638                            name: tool_name.clone(),
639                            description: None,
640                            parameters: None,
641                        },
642                    },
643                ));
644                request.tools = vec![open_ai::ToolDefinition::Function {
645                    function: open_ai::FunctionDefinition {
646                        name: tool_name.clone(),
647                        description: Some(tool_description),
648                        parameters: Some(input_schema),
649                    },
650                }];
651
652                self.request_limiter
653                    .run(async move {
654                        let response = Self::perform_llm_completion(
655                            client.clone(),
656                            llm_api_token,
657                            PerformCompletionParams {
658                                provider: client::LanguageModelProvider::OpenAi,
659                                model: request.model.clone(),
660                                provider_request: RawValue::from_string(serde_json::to_string(
661                                    &request,
662                                )?)?,
663                            },
664                        )
665                        .await?;
666
667                        Ok(open_ai::extract_tool_args_from_events(
668                            tool_name,
669                            Box::pin(response_lines(response)),
670                        )
671                        .await?
672                        .boxed())
673                    })
674                    .boxed()
675            }
676            CloudModel::Google(_) => {
677                future::ready(Err(anyhow!("tool use not implemented for Google AI"))).boxed()
678            }
679            CloudModel::Zed(model) => {
680                // All Zed models are OpenAI-based at the time of writing.
681                let mut request = request.into_open_ai(model.id().into(), None);
682                request.tool_choice = Some(open_ai::ToolChoice::Other(
683                    open_ai::ToolDefinition::Function {
684                        function: open_ai::FunctionDefinition {
685                            name: tool_name.clone(),
686                            description: None,
687                            parameters: None,
688                        },
689                    },
690                ));
691                request.tools = vec![open_ai::ToolDefinition::Function {
692                    function: open_ai::FunctionDefinition {
693                        name: tool_name.clone(),
694                        description: Some(tool_description),
695                        parameters: Some(input_schema),
696                    },
697                }];
698
699                self.request_limiter
700                    .run(async move {
701                        let response = Self::perform_llm_completion(
702                            client.clone(),
703                            llm_api_token,
704                            PerformCompletionParams {
705                                provider: client::LanguageModelProvider::Zed,
706                                model: request.model.clone(),
707                                provider_request: RawValue::from_string(serde_json::to_string(
708                                    &request,
709                                )?)?,
710                            },
711                        )
712                        .await?;
713
714                        Ok(open_ai::extract_tool_args_from_events(
715                            tool_name,
716                            Box::pin(response_lines(response)),
717                        )
718                        .await?
719                        .boxed())
720                    })
721                    .boxed()
722            }
723        }
724    }
725}
726
727fn response_lines<T: DeserializeOwned>(
728    response: Response<AsyncBody>,
729) -> impl Stream<Item = Result<T>> {
730    futures::stream::try_unfold(
731        (String::new(), BufReader::new(response.into_body())),
732        move |(mut line, mut body)| async {
733            match body.read_line(&mut line).await {
734                Ok(0) => Ok(None),
735                Ok(_) => {
736                    let event: T = serde_json::from_str(&line)?;
737                    line.clear();
738                    Ok(Some((event, (line, body))))
739                }
740                Err(e) => Err(e.into()),
741            }
742        },
743    )
744}
745
746impl LlmApiToken {
747    async fn acquire(&self, client: &Arc<Client>) -> Result<String> {
748        let lock = self.0.upgradable_read().await;
749        if let Some(token) = lock.as_ref() {
750            Ok(token.to_string())
751        } else {
752            Self::fetch(RwLockUpgradableReadGuard::upgrade(lock).await, &client).await
753        }
754    }
755
756    async fn refresh(&self, client: &Arc<Client>) -> Result<String> {
757        Self::fetch(self.0.write().await, &client).await
758    }
759
760    async fn fetch<'a>(
761        mut lock: RwLockWriteGuard<'a, Option<String>>,
762        client: &Arc<Client>,
763    ) -> Result<String> {
764        let response = client.request(proto::GetLlmToken {}).await?;
765        *lock = Some(response.token.clone());
766        Ok(response.token.clone())
767    }
768}
769
770struct ConfigurationView {
771    state: gpui::Model<State>,
772}
773
774impl ConfigurationView {
775    fn authenticate(&mut self, cx: &mut ViewContext<Self>) {
776        self.state.update(cx, |state, cx| {
777            state.authenticate(cx).detach_and_log_err(cx);
778        });
779        cx.notify();
780    }
781}
782
783impl Render for ConfigurationView {
784    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
785        const ZED_AI_URL: &str = "https://zed.dev/ai";
786        const ACCOUNT_SETTINGS_URL: &str = "https://zed.dev/account";
787
788        let is_connected = !self.state.read(cx).is_signed_out();
789        let plan = self.state.read(cx).user_store.read(cx).current_plan();
790        let must_accept_terms = !self.state.read(cx).has_accepted_terms_of_service(cx);
791
792        let is_pro = plan == Some(proto::Plan::ZedPro);
793
794        if is_connected {
795            v_flex()
796                .gap_3()
797                .max_w_4_5()
798                .when(must_accept_terms, |this| {
799                    this.child(Label::new(
800                        "You must accept the terms of service to use this provider.",
801                    ))
802                })
803                .child(Label::new(
804                    if is_pro {
805                        "You have full access to Zed's hosted models from Anthropic, OpenAI, Google with faster speeds and higher limits through Zed Pro."
806                    } else {
807                        "You have basic access to models from Anthropic through the Zed AI Free plan."
808                    }))
809                .children(if is_pro {
810                    Some(
811                        h_flex().child(
812                            Button::new("manage_settings", "Manage Subscription")
813                                .style(ButtonStyle::Filled)
814                                .on_click(
815                                    cx.listener(|_, _, cx| cx.open_url(ACCOUNT_SETTINGS_URL)),
816                                ),
817                        ),
818                    )
819                } else if cx.has_flag::<ZedPro>() {
820                    Some(
821                        h_flex()
822                            .gap_2()
823                            .child(
824                                Button::new("learn_more", "Learn more")
825                                    .style(ButtonStyle::Subtle)
826                                    .on_click(cx.listener(|_, _, cx| cx.open_url(ZED_AI_URL))),
827                            )
828                            .child(
829                                Button::new("upgrade", "Upgrade")
830                                    .style(ButtonStyle::Subtle)
831                                    .color(Color::Accent)
832                                    .on_click(
833                                        cx.listener(|_, _, cx| cx.open_url(ACCOUNT_SETTINGS_URL)),
834                                    ),
835                            ),
836                    )
837                } else {
838                    None
839                })
840        } else {
841            v_flex()
842                .gap_6()
843                .child(Label::new("Use the zed.dev to access language models."))
844                .child(
845                    v_flex()
846                        .gap_2()
847                        .child(
848                            Button::new("sign_in", "Sign in")
849                                .icon_color(Color::Muted)
850                                .icon(IconName::Github)
851                                .icon_position(IconPosition::Start)
852                                .style(ButtonStyle::Filled)
853                                .full_width()
854                                .on_click(cx.listener(move |this, _, cx| this.authenticate(cx))),
855                        )
856                        .child(
857                            div().flex().w_full().items_center().child(
858                                Label::new("Sign in to enable collaboration.")
859                                    .color(Color::Muted)
860                                    .size(LabelSize::Small),
861                            ),
862                        ),
863                )
864        }
865    }
866}