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