anthropic.rs

  1use crate::AllLanguageModelSettings;
  2use crate::ui::InstructionListItem;
  3use anthropic::{AnthropicError, AnthropicModelMode, ContentDelta, Event, ResponseContent, Usage};
  4use anyhow::{Context as _, Result, anyhow};
  5use collections::{BTreeMap, HashMap};
  6use credentials_provider::CredentialsProvider;
  7use editor::{Editor, EditorElement, EditorStyle};
  8use futures::Stream;
  9use futures::{FutureExt, StreamExt, future::BoxFuture, stream::BoxStream};
 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::{Icon, IconName, List, Tooltip, prelude::*};
 29use util::{ResultExt, maybe};
 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 supports_tools(&self) -> bool {
404        true
405    }
406
407    fn telemetry_id(&self) -> String {
408        format!("anthropic/{}", self.model.id())
409    }
410
411    fn api_key(&self, cx: &App) -> Option<String> {
412        self.state.read(cx).api_key.clone()
413    }
414
415    fn max_token_count(&self) -> usize {
416        self.model.max_token_count()
417    }
418
419    fn max_output_tokens(&self) -> Option<u32> {
420        Some(self.model.max_output_tokens())
421    }
422
423    fn count_tokens(
424        &self,
425        request: LanguageModelRequest,
426        cx: &App,
427    ) -> BoxFuture<'static, Result<usize>> {
428        count_anthropic_tokens(request, cx)
429    }
430
431    fn stream_completion(
432        &self,
433        request: LanguageModelRequest,
434        cx: &AsyncApp,
435    ) -> BoxFuture<'static, Result<BoxStream<'static, Result<LanguageModelCompletionEvent>>>> {
436        let request = into_anthropic(
437            request,
438            self.model.request_id().into(),
439            self.model.default_temperature(),
440            self.model.max_output_tokens(),
441            self.model.mode(),
442        );
443        let request = self.stream_completion(request, cx);
444        let future = self.request_limiter.stream(async move {
445            let response = request.await.map_err(|err| anyhow!(err))?;
446            Ok(map_to_language_model_completion_events(response))
447        });
448        async move { Ok(future.await?.boxed()) }.boxed()
449    }
450
451    fn cache_configuration(&self) -> Option<LanguageModelCacheConfiguration> {
452        self.model
453            .cache_configuration()
454            .map(|config| LanguageModelCacheConfiguration {
455                max_cache_anchors: config.max_cache_anchors,
456                should_speculate: config.should_speculate,
457                min_total_token: config.min_total_token,
458            })
459    }
460}
461
462pub fn into_anthropic(
463    request: LanguageModelRequest,
464    model: String,
465    default_temperature: f32,
466    max_output_tokens: u32,
467    mode: AnthropicModelMode,
468) -> anthropic::Request {
469    let mut new_messages: Vec<anthropic::Message> = Vec::new();
470    let mut system_message = String::new();
471
472    for message in request.messages {
473        if message.contents_empty() {
474            continue;
475        }
476
477        match message.role {
478            Role::User | Role::Assistant => {
479                let cache_control = if message.cache {
480                    Some(anthropic::CacheControl {
481                        cache_type: anthropic::CacheControlType::Ephemeral,
482                    })
483                } else {
484                    None
485                };
486                let anthropic_message_content: Vec<anthropic::RequestContent> = message
487                    .content
488                    .into_iter()
489                    .filter_map(|content| match content {
490                        MessageContent::Text(text) => {
491                            if !text.is_empty() {
492                                Some(anthropic::RequestContent::Text {
493                                    text,
494                                    cache_control,
495                                })
496                            } else {
497                                None
498                            }
499                        }
500                        MessageContent::Image(image) => Some(anthropic::RequestContent::Image {
501                            source: anthropic::ImageSource {
502                                source_type: "base64".to_string(),
503                                media_type: "image/png".to_string(),
504                                data: image.source.to_string(),
505                            },
506                            cache_control,
507                        }),
508                        MessageContent::ToolUse(tool_use) => {
509                            Some(anthropic::RequestContent::ToolUse {
510                                id: tool_use.id.to_string(),
511                                name: tool_use.name.to_string(),
512                                input: tool_use.input,
513                                cache_control,
514                            })
515                        }
516                        MessageContent::ToolResult(tool_result) => {
517                            Some(anthropic::RequestContent::ToolResult {
518                                tool_use_id: tool_result.tool_use_id.to_string(),
519                                is_error: tool_result.is_error,
520                                content: tool_result.content.to_string(),
521                                cache_control,
522                            })
523                        }
524                    })
525                    .collect();
526                let anthropic_role = match message.role {
527                    Role::User => anthropic::Role::User,
528                    Role::Assistant => anthropic::Role::Assistant,
529                    Role::System => unreachable!("System role should never occur here"),
530                };
531                if let Some(last_message) = new_messages.last_mut() {
532                    if last_message.role == anthropic_role {
533                        last_message.content.extend(anthropic_message_content);
534                        continue;
535                    }
536                }
537                new_messages.push(anthropic::Message {
538                    role: anthropic_role,
539                    content: anthropic_message_content,
540                });
541            }
542            Role::System => {
543                if !system_message.is_empty() {
544                    system_message.push_str("\n\n");
545                }
546                system_message.push_str(&message.string_contents());
547            }
548        }
549    }
550
551    anthropic::Request {
552        model,
553        messages: new_messages,
554        max_tokens: max_output_tokens,
555        system: if system_message.is_empty() {
556            None
557        } else {
558            Some(anthropic::StringOrContents::String(system_message))
559        },
560        thinking: if let AnthropicModelMode::Thinking { budget_tokens } = mode {
561            Some(anthropic::Thinking::Enabled { budget_tokens })
562        } else {
563            None
564        },
565        tools: request
566            .tools
567            .into_iter()
568            .map(|tool| anthropic::Tool {
569                name: tool.name,
570                description: tool.description,
571                input_schema: tool.input_schema,
572            })
573            .collect(),
574        tool_choice: None,
575        metadata: None,
576        stop_sequences: Vec::new(),
577        temperature: request.temperature.or(Some(default_temperature)),
578        top_k: None,
579        top_p: None,
580    }
581}
582
583pub fn map_to_language_model_completion_events(
584    events: Pin<Box<dyn Send + Stream<Item = Result<Event, AnthropicError>>>>,
585) -> impl Stream<Item = Result<LanguageModelCompletionEvent>> {
586    struct RawToolUse {
587        id: String,
588        name: String,
589        input_json: String,
590    }
591
592    struct State {
593        events: Pin<Box<dyn Send + Stream<Item = Result<Event, AnthropicError>>>>,
594        tool_uses_by_index: HashMap<usize, RawToolUse>,
595        usage: Usage,
596        stop_reason: StopReason,
597    }
598
599    futures::stream::unfold(
600        State {
601            events,
602            tool_uses_by_index: HashMap::default(),
603            usage: Usage::default(),
604            stop_reason: StopReason::EndTurn,
605        },
606        |mut state| async move {
607            while let Some(event) = state.events.next().await {
608                match event {
609                    Ok(event) => match event {
610                        Event::ContentBlockStart {
611                            index,
612                            content_block,
613                        } => match content_block {
614                            ResponseContent::Text { text } => {
615                                return Some((
616                                    vec![Ok(LanguageModelCompletionEvent::Text(text))],
617                                    state,
618                                ));
619                            }
620                            ResponseContent::Thinking { thinking } => {
621                                return Some((
622                                    vec![Ok(LanguageModelCompletionEvent::Thinking(thinking))],
623                                    state,
624                                ));
625                            }
626                            ResponseContent::RedactedThinking { .. } => {
627                                // Redacted thinking is encrypted and not accessible to the user, see:
628                                // https://docs.anthropic.com/en/docs/build-with-claude/extended-thinking#suggestions-for-handling-redacted-thinking-in-production
629                            }
630                            ResponseContent::ToolUse { id, name, .. } => {
631                                state.tool_uses_by_index.insert(
632                                    index,
633                                    RawToolUse {
634                                        id,
635                                        name,
636                                        input_json: String::new(),
637                                    },
638                                );
639                            }
640                        },
641                        Event::ContentBlockDelta { index, delta } => match delta {
642                            ContentDelta::TextDelta { text } => {
643                                return Some((
644                                    vec![Ok(LanguageModelCompletionEvent::Text(text))],
645                                    state,
646                                ));
647                            }
648                            ContentDelta::ThinkingDelta { thinking } => {
649                                return Some((
650                                    vec![Ok(LanguageModelCompletionEvent::Thinking(thinking))],
651                                    state,
652                                ));
653                            }
654                            ContentDelta::SignatureDelta { .. } => {}
655                            ContentDelta::InputJsonDelta { partial_json } => {
656                                if let Some(tool_use) = state.tool_uses_by_index.get_mut(&index) {
657                                    tool_use.input_json.push_str(&partial_json);
658                                }
659                            }
660                        },
661                        Event::ContentBlockStop { index } => {
662                            if let Some(tool_use) = state.tool_uses_by_index.remove(&index) {
663                                return Some((
664                                    vec![maybe!({
665                                        Ok(LanguageModelCompletionEvent::ToolUse(
666                                            LanguageModelToolUse {
667                                                id: tool_use.id.into(),
668                                                name: tool_use.name.into(),
669                                                input: if tool_use.input_json.is_empty() {
670                                                    serde_json::Value::Object(
671                                                        serde_json::Map::default(),
672                                                    )
673                                                } else {
674                                                    serde_json::Value::from_str(
675                                                        &tool_use.input_json,
676                                                    )
677                                                    .map_err(|err| anyhow!(err))?
678                                                },
679                                            },
680                                        ))
681                                    })],
682                                    state,
683                                ));
684                            }
685                        }
686                        Event::MessageStart { message } => {
687                            update_usage(&mut state.usage, &message.usage);
688                            return Some((
689                                vec![
690                                    Ok(LanguageModelCompletionEvent::StartMessage {
691                                        message_id: message.id,
692                                    }),
693                                    Ok(LanguageModelCompletionEvent::UsageUpdate(convert_usage(
694                                        &state.usage,
695                                    ))),
696                                ],
697                                state,
698                            ));
699                        }
700                        Event::MessageDelta { delta, usage } => {
701                            update_usage(&mut state.usage, &usage);
702                            if let Some(stop_reason) = delta.stop_reason.as_deref() {
703                                state.stop_reason = match stop_reason {
704                                    "end_turn" => StopReason::EndTurn,
705                                    "max_tokens" => StopReason::MaxTokens,
706                                    "tool_use" => StopReason::ToolUse,
707                                    _ => {
708                                        log::error!(
709                                            "Unexpected anthropic stop_reason: {stop_reason}"
710                                        );
711                                        StopReason::EndTurn
712                                    }
713                                };
714                            }
715                            return Some((
716                                vec![Ok(LanguageModelCompletionEvent::UsageUpdate(
717                                    convert_usage(&state.usage),
718                                ))],
719                                state,
720                            ));
721                        }
722                        Event::MessageStop => {
723                            return Some((
724                                vec![Ok(LanguageModelCompletionEvent::Stop(state.stop_reason))],
725                                state,
726                            ));
727                        }
728                        Event::Error { error } => {
729                            return Some((
730                                vec![Err(anyhow!(AnthropicError::ApiError(error)))],
731                                state,
732                            ));
733                        }
734                        _ => {}
735                    },
736                    Err(err) => {
737                        return Some((vec![Err(anyhow!(err))], state));
738                    }
739                }
740            }
741
742            None
743        },
744    )
745    .flat_map(futures::stream::iter)
746}
747
748/// Updates usage data by preferring counts from `new`.
749fn update_usage(usage: &mut Usage, new: &Usage) {
750    if let Some(input_tokens) = new.input_tokens {
751        usage.input_tokens = Some(input_tokens);
752    }
753    if let Some(output_tokens) = new.output_tokens {
754        usage.output_tokens = Some(output_tokens);
755    }
756    if let Some(cache_creation_input_tokens) = new.cache_creation_input_tokens {
757        usage.cache_creation_input_tokens = Some(cache_creation_input_tokens);
758    }
759    if let Some(cache_read_input_tokens) = new.cache_read_input_tokens {
760        usage.cache_read_input_tokens = Some(cache_read_input_tokens);
761    }
762}
763
764fn convert_usage(usage: &Usage) -> language_model::TokenUsage {
765    language_model::TokenUsage {
766        input_tokens: usage.input_tokens.unwrap_or(0),
767        output_tokens: usage.output_tokens.unwrap_or(0),
768        cache_creation_input_tokens: usage.cache_creation_input_tokens.unwrap_or(0),
769        cache_read_input_tokens: usage.cache_read_input_tokens.unwrap_or(0),
770    }
771}
772
773struct ConfigurationView {
774    api_key_editor: Entity<Editor>,
775    state: gpui::Entity<State>,
776    load_credentials_task: Option<Task<()>>,
777}
778
779impl ConfigurationView {
780    const PLACEHOLDER_TEXT: &'static str = "sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
781
782    fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
783        cx.observe(&state, |_, _, cx| {
784            cx.notify();
785        })
786        .detach();
787
788        let load_credentials_task = Some(cx.spawn({
789            let state = state.clone();
790            async move |this, cx| {
791                if let Some(task) = state
792                    .update(cx, |state, cx| state.authenticate(cx))
793                    .log_err()
794                {
795                    // We don't log an error, because "not signed in" is also an error.
796                    let _ = task.await;
797                }
798                this.update(cx, |this, cx| {
799                    this.load_credentials_task = None;
800                    cx.notify();
801                })
802                .log_err();
803            }
804        }));
805
806        Self {
807            api_key_editor: cx.new(|cx| {
808                let mut editor = Editor::single_line(window, cx);
809                editor.set_placeholder_text(Self::PLACEHOLDER_TEXT, cx);
810                editor
811            }),
812            state,
813            load_credentials_task,
814        }
815    }
816
817    fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
818        let api_key = self.api_key_editor.read(cx).text(cx);
819        if api_key.is_empty() {
820            return;
821        }
822
823        let state = self.state.clone();
824        cx.spawn_in(window, async move |_, cx| {
825            state
826                .update(cx, |state, cx| state.set_api_key(api_key, cx))?
827                .await
828        })
829        .detach_and_log_err(cx);
830
831        cx.notify();
832    }
833
834    fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
835        self.api_key_editor
836            .update(cx, |editor, cx| editor.set_text("", window, cx));
837
838        let state = self.state.clone();
839        cx.spawn_in(window, async move |_, cx| {
840            state.update(cx, |state, cx| state.reset_api_key(cx))?.await
841        })
842        .detach_and_log_err(cx);
843
844        cx.notify();
845    }
846
847    fn render_api_key_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
848        let settings = ThemeSettings::get_global(cx);
849        let text_style = TextStyle {
850            color: cx.theme().colors().text,
851            font_family: settings.ui_font.family.clone(),
852            font_features: settings.ui_font.features.clone(),
853            font_fallbacks: settings.ui_font.fallbacks.clone(),
854            font_size: rems(0.875).into(),
855            font_weight: settings.ui_font.weight,
856            font_style: FontStyle::Normal,
857            line_height: relative(1.3),
858            white_space: WhiteSpace::Normal,
859            ..Default::default()
860        };
861        EditorElement::new(
862            &self.api_key_editor,
863            EditorStyle {
864                background: cx.theme().colors().editor_background,
865                local_player: cx.theme().players().local(),
866                text: text_style,
867                ..Default::default()
868            },
869        )
870    }
871
872    fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
873        !self.state.read(cx).is_authenticated()
874    }
875}
876
877impl Render for ConfigurationView {
878    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
879        let env_var_set = self.state.read(cx).api_key_from_env;
880
881        if self.load_credentials_task.is_some() {
882            div().child(Label::new("Loading credentials...")).into_any()
883        } else if self.should_render_editor(cx) {
884            v_flex()
885                .size_full()
886                .on_action(cx.listener(Self::save_api_key))
887                .child(Label::new("To use Zed's assistant with Anthropic, you need to add an API key. Follow these steps:"))
888                .child(
889                    List::new()
890                        .child(
891                            InstructionListItem::new(
892                                "Create one by visiting",
893                                Some("Anthropic's settings"),
894                                Some("https://console.anthropic.com/settings/keys")
895                            )
896                        )
897                        .child(
898                            InstructionListItem::text_only("Paste your API key below and hit enter to start using the assistant")
899                        )
900                )
901                .child(
902                    h_flex()
903                        .w_full()
904                        .my_2()
905                        .px_2()
906                        .py_1()
907                        .bg(cx.theme().colors().editor_background)
908                        .border_1()
909                        .border_color(cx.theme().colors().border_variant)
910                        .rounded_sm()
911                        .child(self.render_api_key_editor(cx)),
912                )
913                .child(
914                    Label::new(
915                        format!("You can also assign the {ANTHROPIC_API_KEY_VAR} environment variable and restart Zed."),
916                    )
917                    .size(LabelSize::Small)
918                    .color(Color::Muted),
919                )
920                .into_any()
921        } else {
922            h_flex()
923                .size_full()
924                .justify_between()
925                .child(
926                    h_flex()
927                        .gap_1()
928                        .child(Icon::new(IconName::Check).color(Color::Success))
929                        .child(Label::new(if env_var_set {
930                            format!("API key set in {ANTHROPIC_API_KEY_VAR} environment variable.")
931                        } else {
932                            "API key configured.".to_string()
933                        })),
934                )
935                .child(
936                    Button::new("reset-key", "Reset key")
937                        .icon(Some(IconName::Trash))
938                        .icon_size(IconSize::Small)
939                        .icon_position(IconPosition::Start)
940                        .disabled(env_var_set)
941                        .when(env_var_set, |this| {
942                            this.tooltip(Tooltip::text(format!("To reset your API key, unset the {ANTHROPIC_API_KEY_VAR} environment variable.")))
943                        })
944                        .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx))),
945                )
946                .into_any()
947        }
948    }
949}