1use crate::ui::InstructionListItem;
2use crate::AllLanguageModelSettings;
3use anthropic::{AnthropicError, ContentDelta, Event, ResponseContent, Usage};
4use anyhow::{anyhow, Context as _, Result};
5use collections::{BTreeMap, HashMap};
6use credentials_provider::CredentialsProvider;
7use editor::{Editor, EditorElement, EditorStyle};
8use futures::Stream;
9use futures::{future::BoxFuture, stream::BoxStream, FutureExt, StreamExt, TryStreamExt as _};
10use gpui::{
11 AnyView, App, AsyncApp, Context, Entity, FontStyle, Subscription, Task, TextStyle, WhiteSpace,
12};
13use http_client::HttpClient;
14use language_model::{
15 AuthenticateError, LanguageModel, LanguageModelCacheConfiguration, LanguageModelId,
16 LanguageModelName, LanguageModelProvider, LanguageModelProviderId, LanguageModelProviderName,
17 LanguageModelProviderState, LanguageModelRequest, MessageContent, RateLimiter, Role,
18};
19use language_model::{LanguageModelCompletionEvent, LanguageModelToolUse, StopReason};
20use schemars::JsonSchema;
21use serde::{Deserialize, Serialize};
22use settings::{Settings, SettingsStore};
23use std::pin::Pin;
24use std::str::FromStr;
25use std::sync::Arc;
26use strum::IntoEnumIterator;
27use theme::ThemeSettings;
28use ui::{prelude::*, Icon, IconName, List, Tooltip};
29use util::{maybe, ResultExt};
30
31const PROVIDER_ID: &str = language_model::ANTHROPIC_PROVIDER_ID;
32const PROVIDER_NAME: &str = "Anthropic";
33
34#[derive(Default, Clone, Debug, PartialEq)]
35pub struct AnthropicSettings {
36 pub api_url: String,
37 /// Extend Zed's list of Anthropic models.
38 pub available_models: Vec<AvailableModel>,
39 pub needs_setting_migration: bool,
40}
41
42#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
43pub struct AvailableModel {
44 /// The model's name in the Anthropic API. e.g. claude-3-5-sonnet-latest, claude-3-opus-20240229, etc
45 pub name: String,
46 /// The model's name in Zed's UI, such as in the model selector dropdown menu in the assistant panel.
47 pub display_name: Option<String>,
48 /// The model's context window size.
49 pub max_tokens: usize,
50 /// A model `name` to substitute when calling tools, in case the primary model doesn't support tool calling.
51 pub tool_override: Option<String>,
52 /// Configuration of Anthropic's caching API.
53 pub cache_configuration: Option<LanguageModelCacheConfiguration>,
54 pub max_output_tokens: Option<u32>,
55 pub default_temperature: Option<f32>,
56 #[serde(default)]
57 pub extra_beta_headers: Vec<String>,
58}
59
60pub struct AnthropicLanguageModelProvider {
61 http_client: Arc<dyn HttpClient>,
62 state: gpui::Entity<State>,
63}
64
65const ANTHROPIC_API_KEY_VAR: &str = "ANTHROPIC_API_KEY";
66
67pub struct State {
68 api_key: Option<String>,
69 api_key_from_env: bool,
70 _subscription: Subscription,
71}
72
73impl State {
74 fn reset_api_key(&self, cx: &mut Context<Self>) -> Task<Result<()>> {
75 let credentials_provider = <dyn CredentialsProvider>::global(cx);
76 let api_url = AllLanguageModelSettings::get_global(cx)
77 .anthropic
78 .api_url
79 .clone();
80 cx.spawn(async move |this, cx| {
81 credentials_provider
82 .delete_credentials(&api_url, &cx)
83 .await
84 .ok();
85 this.update(cx, |this, cx| {
86 this.api_key = None;
87 this.api_key_from_env = false;
88 cx.notify();
89 })
90 })
91 }
92
93 fn set_api_key(&mut self, api_key: String, cx: &mut Context<Self>) -> Task<Result<()>> {
94 let credentials_provider = <dyn CredentialsProvider>::global(cx);
95 let api_url = AllLanguageModelSettings::get_global(cx)
96 .anthropic
97 .api_url
98 .clone();
99 cx.spawn(async move |this, cx| {
100 credentials_provider
101 .write_credentials(&api_url, "Bearer", api_key.as_bytes(), &cx)
102 .await
103 .ok();
104
105 this.update(cx, |this, cx| {
106 this.api_key = Some(api_key);
107 cx.notify();
108 })
109 })
110 }
111
112 fn is_authenticated(&self) -> bool {
113 self.api_key.is_some()
114 }
115
116 fn authenticate(&self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
117 if self.is_authenticated() {
118 return Task::ready(Ok(()));
119 }
120
121 let credentials_provider = <dyn CredentialsProvider>::global(cx);
122 let api_url = AllLanguageModelSettings::get_global(cx)
123 .anthropic
124 .api_url
125 .clone();
126
127 cx.spawn(async move |this, cx| {
128 let (api_key, from_env) = if let Ok(api_key) = std::env::var(ANTHROPIC_API_KEY_VAR) {
129 (api_key, true)
130 } else {
131 let (_, api_key) = credentials_provider
132 .read_credentials(&api_url, &cx)
133 .await?
134 .ok_or(AuthenticateError::CredentialsNotFound)?;
135 (
136 String::from_utf8(api_key).context("invalid {PROVIDER_NAME} API key")?,
137 false,
138 )
139 };
140
141 this.update(cx, |this, cx| {
142 this.api_key = Some(api_key);
143 this.api_key_from_env = from_env;
144 cx.notify();
145 })?;
146
147 Ok(())
148 })
149 }
150}
151
152impl AnthropicLanguageModelProvider {
153 pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
154 let state = cx.new(|cx| State {
155 api_key: None,
156 api_key_from_env: false,
157 _subscription: cx.observe_global::<SettingsStore>(|_, cx| {
158 cx.notify();
159 }),
160 });
161
162 Self { http_client, state }
163 }
164}
165
166impl LanguageModelProviderState for AnthropicLanguageModelProvider {
167 type ObservableEntity = State;
168
169 fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>> {
170 Some(self.state.clone())
171 }
172}
173
174impl LanguageModelProvider for AnthropicLanguageModelProvider {
175 fn id(&self) -> LanguageModelProviderId {
176 LanguageModelProviderId(PROVIDER_ID.into())
177 }
178
179 fn name(&self) -> LanguageModelProviderName {
180 LanguageModelProviderName(PROVIDER_NAME.into())
181 }
182
183 fn icon(&self) -> IconName {
184 IconName::AiAnthropic
185 }
186
187 fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
188 let model = anthropic::Model::default();
189 Some(Arc::new(AnthropicModel {
190 id: LanguageModelId::from(model.id().to_string()),
191 model,
192 state: self.state.clone(),
193 http_client: self.http_client.clone(),
194 request_limiter: RateLimiter::new(4),
195 }))
196 }
197
198 fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
199 let mut models = BTreeMap::default();
200
201 // Add base models from anthropic::Model::iter()
202 for model in anthropic::Model::iter() {
203 if !matches!(model, anthropic::Model::Custom { .. }) {
204 models.insert(model.id().to_string(), model);
205 }
206 }
207
208 // Override with available models from settings
209 for model in AllLanguageModelSettings::get_global(cx)
210 .anthropic
211 .available_models
212 .iter()
213 {
214 models.insert(
215 model.name.clone(),
216 anthropic::Model::Custom {
217 name: model.name.clone(),
218 display_name: model.display_name.clone(),
219 max_tokens: model.max_tokens,
220 tool_override: model.tool_override.clone(),
221 cache_configuration: model.cache_configuration.as_ref().map(|config| {
222 anthropic::AnthropicModelCacheConfiguration {
223 max_cache_anchors: config.max_cache_anchors,
224 should_speculate: config.should_speculate,
225 min_total_token: config.min_total_token,
226 }
227 }),
228 max_output_tokens: model.max_output_tokens,
229 default_temperature: model.default_temperature,
230 extra_beta_headers: model.extra_beta_headers.clone(),
231 },
232 );
233 }
234
235 models
236 .into_values()
237 .map(|model| {
238 Arc::new(AnthropicModel {
239 id: LanguageModelId::from(model.id().to_string()),
240 model,
241 state: self.state.clone(),
242 http_client: self.http_client.clone(),
243 request_limiter: RateLimiter::new(4),
244 }) as Arc<dyn LanguageModel>
245 })
246 .collect()
247 }
248
249 fn is_authenticated(&self, cx: &App) -> bool {
250 self.state.read(cx).is_authenticated()
251 }
252
253 fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
254 self.state.update(cx, |state, cx| state.authenticate(cx))
255 }
256
257 fn configuration_view(&self, window: &mut Window, cx: &mut App) -> AnyView {
258 cx.new(|cx| ConfigurationView::new(self.state.clone(), window, cx))
259 .into()
260 }
261
262 fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
263 self.state.update(cx, |state, cx| state.reset_api_key(cx))
264 }
265}
266
267pub struct AnthropicModel {
268 id: LanguageModelId,
269 model: anthropic::Model,
270 state: gpui::Entity<State>,
271 http_client: Arc<dyn HttpClient>,
272 request_limiter: RateLimiter,
273}
274
275pub fn count_anthropic_tokens(
276 request: LanguageModelRequest,
277 cx: &App,
278) -> BoxFuture<'static, Result<usize>> {
279 cx.background_spawn(async move {
280 let messages = request.messages;
281 let mut tokens_from_images = 0;
282 let mut string_messages = Vec::with_capacity(messages.len());
283
284 for message in messages {
285 use language_model::MessageContent;
286
287 let mut string_contents = String::new();
288
289 for content in message.content {
290 match content {
291 MessageContent::Text(text) => {
292 string_contents.push_str(&text);
293 }
294 MessageContent::Image(image) => {
295 tokens_from_images += image.estimate_tokens();
296 }
297 MessageContent::ToolUse(_tool_use) => {
298 // TODO: Estimate token usage from tool uses.
299 }
300 MessageContent::ToolResult(tool_result) => {
301 string_contents.push_str(&tool_result.content);
302 }
303 }
304 }
305
306 if !string_contents.is_empty() {
307 string_messages.push(tiktoken_rs::ChatCompletionRequestMessage {
308 role: match message.role {
309 Role::User => "user".into(),
310 Role::Assistant => "assistant".into(),
311 Role::System => "system".into(),
312 },
313 content: Some(string_contents),
314 name: None,
315 function_call: None,
316 });
317 }
318 }
319
320 // Tiktoken doesn't yet support these models, so we manually use the
321 // same tokenizer as GPT-4.
322 tiktoken_rs::num_tokens_from_messages("gpt-4", &string_messages)
323 .map(|tokens| tokens + tokens_from_images)
324 })
325 .boxed()
326}
327
328impl AnthropicModel {
329 fn stream_completion(
330 &self,
331 request: anthropic::Request,
332 cx: &AsyncApp,
333 ) -> BoxFuture<'static, Result<BoxStream<'static, Result<anthropic::Event, AnthropicError>>>>
334 {
335 let http_client = self.http_client.clone();
336
337 let Ok((api_key, api_url)) = cx.read_entity(&self.state, |state, cx| {
338 let settings = &AllLanguageModelSettings::get_global(cx).anthropic;
339 (state.api_key.clone(), settings.api_url.clone())
340 }) else {
341 return futures::future::ready(Err(anyhow!("App state dropped"))).boxed();
342 };
343
344 async move {
345 let api_key = api_key.ok_or_else(|| anyhow!("Missing Anthropic API Key"))?;
346 let request =
347 anthropic::stream_completion(http_client.as_ref(), &api_url, &api_key, request);
348 request.await.context("failed to stream completion")
349 }
350 .boxed()
351 }
352}
353
354impl LanguageModel for AnthropicModel {
355 fn id(&self) -> LanguageModelId {
356 self.id.clone()
357 }
358
359 fn name(&self) -> LanguageModelName {
360 LanguageModelName::from(self.model.display_name().to_string())
361 }
362
363 fn provider_id(&self) -> LanguageModelProviderId {
364 LanguageModelProviderId(PROVIDER_ID.into())
365 }
366
367 fn provider_name(&self) -> LanguageModelProviderName {
368 LanguageModelProviderName(PROVIDER_NAME.into())
369 }
370
371 fn telemetry_id(&self) -> String {
372 format!("anthropic/{}", self.model.id())
373 }
374
375 fn api_key(&self, cx: &App) -> Option<String> {
376 self.state.read(cx).api_key.clone()
377 }
378
379 fn max_token_count(&self) -> usize {
380 self.model.max_token_count()
381 }
382
383 fn max_output_tokens(&self) -> Option<u32> {
384 Some(self.model.max_output_tokens())
385 }
386
387 fn count_tokens(
388 &self,
389 request: LanguageModelRequest,
390 cx: &App,
391 ) -> BoxFuture<'static, Result<usize>> {
392 count_anthropic_tokens(request, cx)
393 }
394
395 fn stream_completion(
396 &self,
397 request: LanguageModelRequest,
398 cx: &AsyncApp,
399 ) -> BoxFuture<'static, Result<BoxStream<'static, Result<LanguageModelCompletionEvent>>>> {
400 let request = into_anthropic(
401 request,
402 self.model.id().into(),
403 self.model.default_temperature(),
404 self.model.max_output_tokens(),
405 );
406 let request = self.stream_completion(request, cx);
407 let future = self.request_limiter.stream(async move {
408 let response = request.await.map_err(|err| anyhow!(err))?;
409 Ok(map_to_language_model_completion_events(response))
410 });
411 async move { Ok(future.await?.boxed()) }.boxed()
412 }
413
414 fn cache_configuration(&self) -> Option<LanguageModelCacheConfiguration> {
415 self.model
416 .cache_configuration()
417 .map(|config| LanguageModelCacheConfiguration {
418 max_cache_anchors: config.max_cache_anchors,
419 should_speculate: config.should_speculate,
420 min_total_token: config.min_total_token,
421 })
422 }
423
424 fn use_any_tool(
425 &self,
426 request: LanguageModelRequest,
427 tool_name: String,
428 tool_description: String,
429 input_schema: serde_json::Value,
430 cx: &AsyncApp,
431 ) -> BoxFuture<'static, Result<BoxStream<'static, Result<String>>>> {
432 let mut request = into_anthropic(
433 request,
434 self.model.tool_model_id().into(),
435 self.model.default_temperature(),
436 self.model.max_output_tokens(),
437 );
438 request.tool_choice = Some(anthropic::ToolChoice::Tool {
439 name: tool_name.clone(),
440 });
441 request.tools = vec![anthropic::Tool {
442 name: tool_name.clone(),
443 description: tool_description,
444 input_schema,
445 }];
446
447 let response = self.stream_completion(request, cx);
448 self.request_limiter
449 .run(async move {
450 let response = response.await?;
451 Ok(anthropic::extract_tool_args_from_events(
452 tool_name,
453 Box::pin(response.map_err(|e| anyhow!(e))),
454 )
455 .await?
456 .boxed())
457 })
458 .boxed()
459 }
460}
461
462pub fn into_anthropic(
463 request: LanguageModelRequest,
464 model: String,
465 default_temperature: f32,
466 max_output_tokens: u32,
467) -> anthropic::Request {
468 let mut new_messages: Vec<anthropic::Message> = Vec::new();
469 let mut system_message = String::new();
470
471 for message in request.messages {
472 if message.contents_empty() {
473 continue;
474 }
475
476 match message.role {
477 Role::User | Role::Assistant => {
478 let cache_control = if message.cache {
479 Some(anthropic::CacheControl {
480 cache_type: anthropic::CacheControlType::Ephemeral,
481 })
482 } else {
483 None
484 };
485 let anthropic_message_content: Vec<anthropic::RequestContent> = message
486 .content
487 .into_iter()
488 .filter_map(|content| match content {
489 MessageContent::Text(text) => {
490 if !text.is_empty() {
491 Some(anthropic::RequestContent::Text {
492 text,
493 cache_control,
494 })
495 } else {
496 None
497 }
498 }
499 MessageContent::Image(image) => Some(anthropic::RequestContent::Image {
500 source: anthropic::ImageSource {
501 source_type: "base64".to_string(),
502 media_type: "image/png".to_string(),
503 data: image.source.to_string(),
504 },
505 cache_control,
506 }),
507 MessageContent::ToolUse(tool_use) => {
508 Some(anthropic::RequestContent::ToolUse {
509 id: tool_use.id.to_string(),
510 name: tool_use.name.to_string(),
511 input: tool_use.input,
512 cache_control,
513 })
514 }
515 MessageContent::ToolResult(tool_result) => {
516 Some(anthropic::RequestContent::ToolResult {
517 tool_use_id: tool_result.tool_use_id.to_string(),
518 is_error: tool_result.is_error,
519 content: tool_result.content.to_string(),
520 cache_control,
521 })
522 }
523 })
524 .collect();
525 let anthropic_role = match message.role {
526 Role::User => anthropic::Role::User,
527 Role::Assistant => anthropic::Role::Assistant,
528 Role::System => unreachable!("System role should never occur here"),
529 };
530 if let Some(last_message) = new_messages.last_mut() {
531 if last_message.role == anthropic_role {
532 last_message.content.extend(anthropic_message_content);
533 continue;
534 }
535 }
536 new_messages.push(anthropic::Message {
537 role: anthropic_role,
538 content: anthropic_message_content,
539 });
540 }
541 Role::System => {
542 if !system_message.is_empty() {
543 system_message.push_str("\n\n");
544 }
545 system_message.push_str(&message.string_contents());
546 }
547 }
548 }
549
550 anthropic::Request {
551 model,
552 messages: new_messages,
553 max_tokens: max_output_tokens,
554 system: Some(system_message),
555 tools: request
556 .tools
557 .into_iter()
558 .map(|tool| anthropic::Tool {
559 name: tool.name,
560 description: tool.description,
561 input_schema: tool.input_schema,
562 })
563 .collect(),
564 tool_choice: None,
565 metadata: None,
566 stop_sequences: Vec::new(),
567 temperature: request.temperature.or(Some(default_temperature)),
568 top_k: None,
569 top_p: None,
570 }
571}
572
573pub fn map_to_language_model_completion_events(
574 events: Pin<Box<dyn Send + Stream<Item = Result<Event, AnthropicError>>>>,
575) -> impl Stream<Item = Result<LanguageModelCompletionEvent>> {
576 struct RawToolUse {
577 id: String,
578 name: String,
579 input_json: String,
580 }
581
582 struct State {
583 events: Pin<Box<dyn Send + Stream<Item = Result<Event, AnthropicError>>>>,
584 tool_uses_by_index: HashMap<usize, RawToolUse>,
585 usage: Usage,
586 stop_reason: StopReason,
587 }
588
589 futures::stream::unfold(
590 State {
591 events,
592 tool_uses_by_index: HashMap::default(),
593 usage: Usage::default(),
594 stop_reason: StopReason::EndTurn,
595 },
596 |mut state| async move {
597 while let Some(event) = state.events.next().await {
598 match event {
599 Ok(event) => match event {
600 Event::ContentBlockStart {
601 index,
602 content_block,
603 } => match content_block {
604 ResponseContent::Text { text } => {
605 return Some((
606 vec![Ok(LanguageModelCompletionEvent::Text(text))],
607 state,
608 ));
609 }
610 ResponseContent::ToolUse { id, name, .. } => {
611 state.tool_uses_by_index.insert(
612 index,
613 RawToolUse {
614 id,
615 name,
616 input_json: String::new(),
617 },
618 );
619 }
620 },
621 Event::ContentBlockDelta { index, delta } => match delta {
622 ContentDelta::TextDelta { text } => {
623 return Some((
624 vec![Ok(LanguageModelCompletionEvent::Text(text))],
625 state,
626 ));
627 }
628 ContentDelta::InputJsonDelta { partial_json } => {
629 if let Some(tool_use) = state.tool_uses_by_index.get_mut(&index) {
630 tool_use.input_json.push_str(&partial_json);
631 }
632 }
633 },
634 Event::ContentBlockStop { index } => {
635 if let Some(tool_use) = state.tool_uses_by_index.remove(&index) {
636 return Some((
637 vec![maybe!({
638 Ok(LanguageModelCompletionEvent::ToolUse(
639 LanguageModelToolUse {
640 id: tool_use.id.into(),
641 name: tool_use.name.into(),
642 input: if tool_use.input_json.is_empty() {
643 serde_json::Value::Object(
644 serde_json::Map::default(),
645 )
646 } else {
647 serde_json::Value::from_str(
648 &tool_use.input_json,
649 )
650 .map_err(|err| anyhow!(err))?
651 },
652 },
653 ))
654 })],
655 state,
656 ));
657 }
658 }
659 Event::MessageStart { message } => {
660 update_usage(&mut state.usage, &message.usage);
661 return Some((
662 vec![
663 Ok(LanguageModelCompletionEvent::StartMessage {
664 message_id: message.id,
665 }),
666 Ok(LanguageModelCompletionEvent::UsageUpdate(convert_usage(
667 &state.usage,
668 ))),
669 ],
670 state,
671 ));
672 }
673 Event::MessageDelta { delta, usage } => {
674 update_usage(&mut state.usage, &usage);
675 if let Some(stop_reason) = delta.stop_reason.as_deref() {
676 state.stop_reason = match stop_reason {
677 "end_turn" => StopReason::EndTurn,
678 "max_tokens" => StopReason::MaxTokens,
679 "tool_use" => StopReason::ToolUse,
680 _ => {
681 log::error!(
682 "Unexpected anthropic stop_reason: {stop_reason}"
683 );
684 StopReason::EndTurn
685 }
686 };
687 }
688 return Some((
689 vec![Ok(LanguageModelCompletionEvent::UsageUpdate(
690 convert_usage(&state.usage),
691 ))],
692 state,
693 ));
694 }
695 Event::MessageStop => {
696 return Some((
697 vec![Ok(LanguageModelCompletionEvent::Stop(state.stop_reason))],
698 state,
699 ));
700 }
701 Event::Error { error } => {
702 return Some((
703 vec![Err(anyhow!(AnthropicError::ApiError(error)))],
704 state,
705 ));
706 }
707 _ => {}
708 },
709 Err(err) => {
710 return Some((vec![Err(anyhow!(err))], state));
711 }
712 }
713 }
714
715 None
716 },
717 )
718 .flat_map(futures::stream::iter)
719}
720
721/// Updates usage data by preferring counts from `new`.
722fn update_usage(usage: &mut Usage, new: &Usage) {
723 if let Some(input_tokens) = new.input_tokens {
724 usage.input_tokens = Some(input_tokens);
725 }
726 if let Some(output_tokens) = new.output_tokens {
727 usage.output_tokens = Some(output_tokens);
728 }
729 if let Some(cache_creation_input_tokens) = new.cache_creation_input_tokens {
730 usage.cache_creation_input_tokens = Some(cache_creation_input_tokens);
731 }
732 if let Some(cache_read_input_tokens) = new.cache_read_input_tokens {
733 usage.cache_read_input_tokens = Some(cache_read_input_tokens);
734 }
735}
736
737fn convert_usage(usage: &Usage) -> language_model::TokenUsage {
738 language_model::TokenUsage {
739 input_tokens: usage.input_tokens.unwrap_or(0),
740 output_tokens: usage.output_tokens.unwrap_or(0),
741 cache_creation_input_tokens: usage.cache_creation_input_tokens.unwrap_or(0),
742 cache_read_input_tokens: usage.cache_read_input_tokens.unwrap_or(0),
743 }
744}
745
746struct ConfigurationView {
747 api_key_editor: Entity<Editor>,
748 state: gpui::Entity<State>,
749 load_credentials_task: Option<Task<()>>,
750}
751
752impl ConfigurationView {
753 const PLACEHOLDER_TEXT: &'static str = "sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
754
755 fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
756 cx.observe(&state, |_, _, cx| {
757 cx.notify();
758 })
759 .detach();
760
761 let load_credentials_task = Some(cx.spawn({
762 let state = state.clone();
763 async move |this, cx| {
764 if let Some(task) = state
765 .update(cx, |state, cx| state.authenticate(cx))
766 .log_err()
767 {
768 // We don't log an error, because "not signed in" is also an error.
769 let _ = task.await;
770 }
771 this.update(cx, |this, cx| {
772 this.load_credentials_task = None;
773 cx.notify();
774 })
775 .log_err();
776 }
777 }));
778
779 Self {
780 api_key_editor: cx.new(|cx| {
781 let mut editor = Editor::single_line(window, cx);
782 editor.set_placeholder_text(Self::PLACEHOLDER_TEXT, cx);
783 editor
784 }),
785 state,
786 load_credentials_task,
787 }
788 }
789
790 fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
791 let api_key = self.api_key_editor.read(cx).text(cx);
792 if api_key.is_empty() {
793 return;
794 }
795
796 let state = self.state.clone();
797 cx.spawn_in(window, async move |_, cx| {
798 state
799 .update(cx, |state, cx| state.set_api_key(api_key, cx))?
800 .await
801 })
802 .detach_and_log_err(cx);
803
804 cx.notify();
805 }
806
807 fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
808 self.api_key_editor
809 .update(cx, |editor, cx| editor.set_text("", window, cx));
810
811 let state = self.state.clone();
812 cx.spawn_in(window, async move |_, cx| {
813 state.update(cx, |state, cx| state.reset_api_key(cx))?.await
814 })
815 .detach_and_log_err(cx);
816
817 cx.notify();
818 }
819
820 fn render_api_key_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
821 let settings = ThemeSettings::get_global(cx);
822 let text_style = TextStyle {
823 color: cx.theme().colors().text,
824 font_family: settings.ui_font.family.clone(),
825 font_features: settings.ui_font.features.clone(),
826 font_fallbacks: settings.ui_font.fallbacks.clone(),
827 font_size: rems(0.875).into(),
828 font_weight: settings.ui_font.weight,
829 font_style: FontStyle::Normal,
830 line_height: relative(1.3),
831 white_space: WhiteSpace::Normal,
832 ..Default::default()
833 };
834 EditorElement::new(
835 &self.api_key_editor,
836 EditorStyle {
837 background: cx.theme().colors().editor_background,
838 local_player: cx.theme().players().local(),
839 text: text_style,
840 ..Default::default()
841 },
842 )
843 }
844
845 fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
846 !self.state.read(cx).is_authenticated()
847 }
848}
849
850impl Render for ConfigurationView {
851 fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
852 let env_var_set = self.state.read(cx).api_key_from_env;
853
854 if self.load_credentials_task.is_some() {
855 div().child(Label::new("Loading credentials...")).into_any()
856 } else if self.should_render_editor(cx) {
857 v_flex()
858 .size_full()
859 .on_action(cx.listener(Self::save_api_key))
860 .child(Label::new("To use Zed's assistant with Anthropic, you need to add an API key. Follow these steps:"))
861 .child(
862 List::new()
863 .child(
864 InstructionListItem::new(
865 "Create one by visiting",
866 Some("Anthropic's settings"),
867 Some("https://console.anthropic.com/settings/keys")
868 )
869 )
870 .child(
871 InstructionListItem::text_only("Paste your API key below and hit enter to start using the assistant")
872 )
873 )
874 .child(
875 h_flex()
876 .w_full()
877 .my_2()
878 .px_2()
879 .py_1()
880 .bg(cx.theme().colors().editor_background)
881 .border_1()
882 .border_color(cx.theme().colors().border_variant)
883 .rounded_sm()
884 .child(self.render_api_key_editor(cx)),
885 )
886 .child(
887 Label::new(
888 format!("You can also assign the {ANTHROPIC_API_KEY_VAR} environment variable and restart Zed."),
889 )
890 .size(LabelSize::Small)
891 .color(Color::Muted),
892 )
893 .into_any()
894 } else {
895 h_flex()
896 .size_full()
897 .justify_between()
898 .child(
899 h_flex()
900 .gap_1()
901 .child(Icon::new(IconName::Check).color(Color::Success))
902 .child(Label::new(if env_var_set {
903 format!("API key set in {ANTHROPIC_API_KEY_VAR} environment variable.")
904 } else {
905 "API key configured.".to_string()
906 })),
907 )
908 .child(
909 Button::new("reset-key", "Reset key")
910 .icon(Some(IconName::Trash))
911 .icon_size(IconSize::Small)
912 .icon_position(IconPosition::Start)
913 .disabled(env_var_set)
914 .when(env_var_set, |this| {
915 this.tooltip(Tooltip::text(format!("To reset your API key, unset the {ANTHROPIC_API_KEY_VAR} environment variable.")))
916 })
917 .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx))),
918 )
919 .into_any()
920 }
921 }
922}