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 }),
258 AvailableProvider::Google => CloudModel::Google(google_ai::Model::Custom {
259 name: model.name.clone(),
260 max_tokens: model.max_tokens,
261 }),
262 };
263 models.insert(model.id().to_string(), model.clone());
264 }
265
266 models
267 .into_values()
268 .map(|model| {
269 Arc::new(CloudLanguageModel {
270 id: LanguageModelId::from(model.id().to_string()),
271 model,
272 llm_api_token: self.llm_api_token.clone(),
273 client: self.client.clone(),
274 request_limiter: RateLimiter::new(4),
275 }) as Arc<dyn LanguageModel>
276 })
277 .collect()
278 }
279
280 fn is_authenticated(&self, cx: &AppContext) -> bool {
281 !self.state.read(cx).is_signed_out()
282 }
283
284 fn authenticate(&self, _cx: &mut AppContext) -> Task<Result<()>> {
285 Task::ready(Ok(()))
286 }
287
288 fn configuration_view(&self, cx: &mut WindowContext) -> AnyView {
289 cx.new_view(|_cx| ConfigurationView {
290 state: self.state.clone(),
291 })
292 .into()
293 }
294
295 fn must_accept_terms(&self, cx: &AppContext) -> bool {
296 !self.state.read(cx).has_accepted_terms_of_service(cx)
297 }
298
299 fn render_accept_terms(&self, cx: &mut WindowContext) -> Option<AnyElement> {
300 let state = self.state.read(cx);
301
302 let terms = [(
303 "terms_of_service",
304 "Terms of Service",
305 "https://zed.dev/terms-of-service",
306 )]
307 .map(|(id, label, url)| {
308 Button::new(id, label)
309 .style(ButtonStyle::Subtle)
310 .icon(IconName::ExternalLink)
311 .icon_size(IconSize::XSmall)
312 .icon_color(Color::Muted)
313 .on_click(move |_, cx| cx.open_url(url))
314 });
315
316 if state.has_accepted_terms_of_service(cx) {
317 None
318 } else {
319 let disabled = state.accept_terms.is_some();
320 Some(
321 v_flex()
322 .gap_2()
323 .child(
324 v_flex()
325 .child(Label::new("Terms and Conditions").weight(FontWeight::MEDIUM))
326 .child(
327 Label::new(
328 "Please read and accept our terms and conditions to continue.",
329 )
330 .size(LabelSize::Small),
331 ),
332 )
333 .child(v_flex().gap_1().children(terms))
334 .child(
335 h_flex().justify_end().child(
336 Button::new("accept_terms", "I've read it and accept it")
337 .disabled(disabled)
338 .on_click({
339 let state = self.state.downgrade();
340 move |_, cx| {
341 state
342 .update(cx, |state, cx| {
343 state.accept_terms_of_service(cx)
344 })
345 .ok();
346 }
347 }),
348 ),
349 )
350 .into_any(),
351 )
352 }
353 }
354
355 fn reset_credentials(&self, _cx: &mut AppContext) -> Task<Result<()>> {
356 Task::ready(Ok(()))
357 }
358}
359
360pub struct CloudLanguageModel {
361 id: LanguageModelId,
362 model: CloudModel,
363 llm_api_token: LlmApiToken,
364 client: Arc<Client>,
365 request_limiter: RateLimiter,
366}
367
368#[derive(Clone, Default)]
369struct LlmApiToken(Arc<RwLock<Option<String>>>);
370
371impl CloudLanguageModel {
372 async fn perform_llm_completion(
373 client: Arc<Client>,
374 llm_api_token: LlmApiToken,
375 body: PerformCompletionParams,
376 ) -> Result<Response<AsyncBody>> {
377 let http_client = &client.http_client();
378
379 let mut token = llm_api_token.acquire(&client).await?;
380 let mut did_retry = false;
381
382 let response = loop {
383 let request = http_client::Request::builder()
384 .method(Method::POST)
385 .uri(http_client.build_zed_llm_url("/completion", &[])?.as_ref())
386 .header("Content-Type", "application/json")
387 .header("Authorization", format!("Bearer {token}"))
388 .body(serde_json::to_string(&body)?.into())?;
389 let mut response = http_client.send(request).await?;
390 if response.status().is_success() {
391 break response;
392 } else if !did_retry
393 && response
394 .headers()
395 .get(EXPIRED_LLM_TOKEN_HEADER_NAME)
396 .is_some()
397 {
398 did_retry = true;
399 token = llm_api_token.refresh(&client).await?;
400 } else {
401 let mut body = String::new();
402 response.body_mut().read_to_string(&mut body).await?;
403 break Err(anyhow!(
404 "cloud language model completion failed with status {}: {body}",
405 response.status()
406 ))?;
407 }
408 };
409
410 Ok(response)
411 }
412}
413
414impl LanguageModel for CloudLanguageModel {
415 fn id(&self) -> LanguageModelId {
416 self.id.clone()
417 }
418
419 fn name(&self) -> LanguageModelName {
420 LanguageModelName::from(self.model.display_name().to_string())
421 }
422
423 fn icon(&self) -> Option<IconName> {
424 self.model.icon()
425 }
426
427 fn provider_id(&self) -> LanguageModelProviderId {
428 LanguageModelProviderId(PROVIDER_ID.into())
429 }
430
431 fn provider_name(&self) -> LanguageModelProviderName {
432 LanguageModelProviderName(PROVIDER_NAME.into())
433 }
434
435 fn telemetry_id(&self) -> String {
436 format!("zed.dev/{}", self.model.id())
437 }
438
439 fn availability(&self) -> LanguageModelAvailability {
440 self.model.availability()
441 }
442
443 fn max_token_count(&self) -> usize {
444 self.model.max_token_count()
445 }
446
447 fn cache_configuration(&self) -> Option<LanguageModelCacheConfiguration> {
448 match &self.model {
449 CloudModel::Anthropic(model) => {
450 model
451 .cache_configuration()
452 .map(|cache| LanguageModelCacheConfiguration {
453 max_cache_anchors: cache.max_cache_anchors,
454 should_speculate: cache.should_speculate,
455 min_total_token: cache.min_total_token,
456 })
457 }
458 CloudModel::OpenAi(_) | CloudModel::Google(_) | CloudModel::Zed(_) => None,
459 }
460 }
461
462 fn count_tokens(
463 &self,
464 request: LanguageModelRequest,
465 cx: &AppContext,
466 ) -> BoxFuture<'static, Result<usize>> {
467 match self.model.clone() {
468 CloudModel::Anthropic(_) => count_anthropic_tokens(request, cx),
469 CloudModel::OpenAi(model) => count_open_ai_tokens(request, model, cx),
470 CloudModel::Google(model) => {
471 let client = self.client.clone();
472 let request = request.into_google(model.id().into());
473 let request = google_ai::CountTokensRequest {
474 contents: request.contents,
475 };
476 async move {
477 let request = serde_json::to_string(&request)?;
478 let response = client
479 .request(proto::CountLanguageModelTokens {
480 provider: proto::LanguageModelProvider::Google as i32,
481 request,
482 })
483 .await?;
484 Ok(response.token_count as usize)
485 }
486 .boxed()
487 }
488 CloudModel::Zed(_) => {
489 count_open_ai_tokens(request, open_ai::Model::ThreePointFiveTurbo, cx)
490 }
491 }
492 }
493
494 fn stream_completion(
495 &self,
496 request: LanguageModelRequest,
497 _cx: &AsyncAppContext,
498 ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
499 match &self.model {
500 CloudModel::Anthropic(model) => {
501 let request = request.into_anthropic(model.id().into(), model.max_output_tokens());
502 let client = self.client.clone();
503 let llm_api_token = self.llm_api_token.clone();
504 let future = self.request_limiter.stream(async move {
505 let response = Self::perform_llm_completion(
506 client.clone(),
507 llm_api_token,
508 PerformCompletionParams {
509 provider: client::LanguageModelProvider::Anthropic,
510 model: request.model.clone(),
511 provider_request: RawValue::from_string(serde_json::to_string(
512 &request,
513 )?)?,
514 },
515 )
516 .await?;
517 Ok(anthropic::extract_text_from_events(
518 response_lines(response).map_err(AnthropicError::Other),
519 ))
520 });
521 async move {
522 Ok(future
523 .await?
524 .map(|result| result.map_err(|err| anyhow!(err)))
525 .boxed())
526 }
527 .boxed()
528 }
529 CloudModel::OpenAi(model) => {
530 let client = self.client.clone();
531 let request = request.into_open_ai(model.id().into());
532 let llm_api_token = self.llm_api_token.clone();
533 let future = self.request_limiter.stream(async move {
534 let response = Self::perform_llm_completion(
535 client.clone(),
536 llm_api_token,
537 PerformCompletionParams {
538 provider: client::LanguageModelProvider::OpenAi,
539 model: request.model.clone(),
540 provider_request: RawValue::from_string(serde_json::to_string(
541 &request,
542 )?)?,
543 },
544 )
545 .await?;
546 Ok(open_ai::extract_text_from_events(response_lines(response)))
547 });
548 async move { Ok(future.await?.boxed()) }.boxed()
549 }
550 CloudModel::Google(model) => {
551 let client = self.client.clone();
552 let request = request.into_google(model.id().into());
553 let llm_api_token = self.llm_api_token.clone();
554 let future = self.request_limiter.stream(async move {
555 let response = Self::perform_llm_completion(
556 client.clone(),
557 llm_api_token,
558 PerformCompletionParams {
559 provider: client::LanguageModelProvider::Google,
560 model: request.model.clone(),
561 provider_request: RawValue::from_string(serde_json::to_string(
562 &request,
563 )?)?,
564 },
565 )
566 .await?;
567 Ok(google_ai::extract_text_from_events(response_lines(
568 response,
569 )))
570 });
571 async move { Ok(future.await?.boxed()) }.boxed()
572 }
573 CloudModel::Zed(model) => {
574 let client = self.client.clone();
575 let mut request = request.into_open_ai(model.id().into());
576 request.max_tokens = Some(4000);
577 let llm_api_token = self.llm_api_token.clone();
578 let future = self.request_limiter.stream(async move {
579 let response = Self::perform_llm_completion(
580 client.clone(),
581 llm_api_token,
582 PerformCompletionParams {
583 provider: client::LanguageModelProvider::Zed,
584 model: request.model.clone(),
585 provider_request: RawValue::from_string(serde_json::to_string(
586 &request,
587 )?)?,
588 },
589 )
590 .await?;
591 Ok(open_ai::extract_text_from_events(response_lines(response)))
592 });
593 async move { Ok(future.await?.boxed()) }.boxed()
594 }
595 }
596 }
597
598 fn use_any_tool(
599 &self,
600 request: LanguageModelRequest,
601 tool_name: String,
602 tool_description: String,
603 input_schema: serde_json::Value,
604 _cx: &AsyncAppContext,
605 ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
606 let client = self.client.clone();
607 let llm_api_token = self.llm_api_token.clone();
608
609 match &self.model {
610 CloudModel::Anthropic(model) => {
611 let mut request =
612 request.into_anthropic(model.tool_model_id().into(), model.max_output_tokens());
613 request.tool_choice = Some(anthropic::ToolChoice::Tool {
614 name: tool_name.clone(),
615 });
616 request.tools = vec![anthropic::Tool {
617 name: tool_name.clone(),
618 description: tool_description,
619 input_schema,
620 }];
621
622 self.request_limiter
623 .run(async move {
624 let response = Self::perform_llm_completion(
625 client.clone(),
626 llm_api_token,
627 PerformCompletionParams {
628 provider: client::LanguageModelProvider::Anthropic,
629 model: request.model.clone(),
630 provider_request: RawValue::from_string(serde_json::to_string(
631 &request,
632 )?)?,
633 },
634 )
635 .await?;
636
637 Ok(anthropic::extract_tool_args_from_events(
638 tool_name,
639 Box::pin(response_lines(response)),
640 )
641 .await?
642 .boxed())
643 })
644 .boxed()
645 }
646 CloudModel::OpenAi(model) => {
647 let mut request = request.into_open_ai(model.id().into());
648 request.tool_choice = Some(open_ai::ToolChoice::Other(
649 open_ai::ToolDefinition::Function {
650 function: open_ai::FunctionDefinition {
651 name: tool_name.clone(),
652 description: None,
653 parameters: None,
654 },
655 },
656 ));
657 request.tools = vec![open_ai::ToolDefinition::Function {
658 function: open_ai::FunctionDefinition {
659 name: tool_name.clone(),
660 description: Some(tool_description),
661 parameters: Some(input_schema),
662 },
663 }];
664
665 self.request_limiter
666 .run(async move {
667 let response = Self::perform_llm_completion(
668 client.clone(),
669 llm_api_token,
670 PerformCompletionParams {
671 provider: client::LanguageModelProvider::OpenAi,
672 model: request.model.clone(),
673 provider_request: RawValue::from_string(serde_json::to_string(
674 &request,
675 )?)?,
676 },
677 )
678 .await?;
679
680 Ok(open_ai::extract_tool_args_from_events(
681 tool_name,
682 Box::pin(response_lines(response)),
683 )
684 .await?
685 .boxed())
686 })
687 .boxed()
688 }
689 CloudModel::Google(_) => {
690 future::ready(Err(anyhow!("tool use not implemented for Google AI"))).boxed()
691 }
692 CloudModel::Zed(model) => {
693 // All Zed models are OpenAI-based at the time of writing.
694 let mut request = request.into_open_ai(model.id().into());
695 request.tool_choice = Some(open_ai::ToolChoice::Other(
696 open_ai::ToolDefinition::Function {
697 function: open_ai::FunctionDefinition {
698 name: tool_name.clone(),
699 description: None,
700 parameters: None,
701 },
702 },
703 ));
704 request.tools = vec![open_ai::ToolDefinition::Function {
705 function: open_ai::FunctionDefinition {
706 name: tool_name.clone(),
707 description: Some(tool_description),
708 parameters: Some(input_schema),
709 },
710 }];
711
712 self.request_limiter
713 .run(async move {
714 let response = Self::perform_llm_completion(
715 client.clone(),
716 llm_api_token,
717 PerformCompletionParams {
718 provider: client::LanguageModelProvider::Zed,
719 model: request.model.clone(),
720 provider_request: RawValue::from_string(serde_json::to_string(
721 &request,
722 )?)?,
723 },
724 )
725 .await?;
726
727 Ok(open_ai::extract_tool_args_from_events(
728 tool_name,
729 Box::pin(response_lines(response)),
730 )
731 .await?
732 .boxed())
733 })
734 .boxed()
735 }
736 }
737 }
738}
739
740fn response_lines<T: DeserializeOwned>(
741 response: Response<AsyncBody>,
742) -> impl Stream<Item = Result<T>> {
743 futures::stream::try_unfold(
744 (String::new(), BufReader::new(response.into_body())),
745 move |(mut line, mut body)| async {
746 match body.read_line(&mut line).await {
747 Ok(0) => Ok(None),
748 Ok(_) => {
749 let event: T = serde_json::from_str(&line)?;
750 line.clear();
751 Ok(Some((event, (line, body))))
752 }
753 Err(e) => Err(e.into()),
754 }
755 },
756 )
757}
758
759impl LlmApiToken {
760 async fn acquire(&self, client: &Arc<Client>) -> Result<String> {
761 let lock = self.0.upgradable_read().await;
762 if let Some(token) = lock.as_ref() {
763 Ok(token.to_string())
764 } else {
765 Self::fetch(RwLockUpgradableReadGuard::upgrade(lock).await, &client).await
766 }
767 }
768
769 async fn refresh(&self, client: &Arc<Client>) -> Result<String> {
770 Self::fetch(self.0.write().await, &client).await
771 }
772
773 async fn fetch<'a>(
774 mut lock: RwLockWriteGuard<'a, Option<String>>,
775 client: &Arc<Client>,
776 ) -> Result<String> {
777 let response = client.request(proto::GetLlmToken {}).await?;
778 *lock = Some(response.token.clone());
779 Ok(response.token.clone())
780 }
781}
782
783struct ConfigurationView {
784 state: gpui::Model<State>,
785}
786
787impl ConfigurationView {
788 fn authenticate(&mut self, cx: &mut ViewContext<Self>) {
789 self.state.update(cx, |state, cx| {
790 state.authenticate(cx).detach_and_log_err(cx);
791 });
792 cx.notify();
793 }
794}
795
796impl Render for ConfigurationView {
797 fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
798 const ZED_AI_URL: &str = "https://zed.dev/ai";
799 const ACCOUNT_SETTINGS_URL: &str = "https://zed.dev/account";
800
801 let is_connected = !self.state.read(cx).is_signed_out();
802 let plan = self.state.read(cx).user_store.read(cx).current_plan();
803 let must_accept_terms = !self.state.read(cx).has_accepted_terms_of_service(cx);
804
805 let is_pro = plan == Some(proto::Plan::ZedPro);
806
807 if is_connected {
808 v_flex()
809 .gap_3()
810 .max_w_4_5()
811 .when(must_accept_terms, |this| {
812 this.child(Label::new(
813 "You must accept the terms of service to use this provider.",
814 ))
815 })
816 .child(Label::new(
817 if is_pro {
818 "You have full access to Zed's hosted models from Anthropic, OpenAI, Google with faster speeds and higher limits through Zed Pro."
819 } else {
820 "You have basic access to models from Anthropic through the Zed AI Free plan."
821 }))
822 .children(if is_pro {
823 Some(
824 h_flex().child(
825 Button::new("manage_settings", "Manage Subscription")
826 .style(ButtonStyle::Filled)
827 .on_click(
828 cx.listener(|_, _, cx| cx.open_url(ACCOUNT_SETTINGS_URL)),
829 ),
830 ),
831 )
832 } else if cx.has_flag::<ZedPro>() {
833 Some(
834 h_flex()
835 .gap_2()
836 .child(
837 Button::new("learn_more", "Learn more")
838 .style(ButtonStyle::Subtle)
839 .on_click(cx.listener(|_, _, cx| cx.open_url(ZED_AI_URL))),
840 )
841 .child(
842 Button::new("upgrade", "Upgrade")
843 .style(ButtonStyle::Subtle)
844 .color(Color::Accent)
845 .on_click(
846 cx.listener(|_, _, cx| cx.open_url(ACCOUNT_SETTINGS_URL)),
847 ),
848 ),
849 )
850 } else {
851 None
852 })
853 } else {
854 v_flex()
855 .gap_6()
856 .child(Label::new("Use the zed.dev to access language models."))
857 .child(
858 v_flex()
859 .gap_2()
860 .child(
861 Button::new("sign_in", "Sign in")
862 .icon_color(Color::Muted)
863 .icon(IconName::Github)
864 .icon_position(IconPosition::Start)
865 .style(ButtonStyle::Filled)
866 .full_width()
867 .on_click(cx.listener(move |this, _, cx| this.authenticate(cx))),
868 )
869 .child(
870 div().flex().w_full().items_center().child(
871 Label::new("Sign in to enable collaboration.")
872 .color(Color::Muted)
873 .size(LabelSize::Small),
874 ),
875 ),
876 )
877 }
878 }
879}