cloud.rs

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