cloud.rs

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