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