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                                    // Try to convert invalid (incomplete) JSON into
 718                                    // valid JSON that serde can accept, e.g. by closing
 719                                    // unclosed delimiters. This way, we can update the
 720                                    // UI with whatever has been streamed back so far.
 721                                    if let Ok(input) = serde_json::Value::from_str(
 722                                        &partial_json_fixer::fix_json(&tool_use.input_json),
 723                                    ) {
 724                                        return Some((
 725                                            vec![Ok(LanguageModelCompletionEvent::ToolUse(
 726                                                LanguageModelToolUse {
 727                                                    id: tool_use.id.clone().into(),
 728                                                    name: tool_use.name.clone().into(),
 729                                                    is_input_complete: false,
 730                                                    raw_input: tool_use.input_json.clone(),
 731                                                    input,
 732                                                },
 733                                            ))],
 734                                            state,
 735                                        ));
 736                                    }
 737                                }
 738                            }
 739                        },
 740                        Event::ContentBlockStop { index } => {
 741                            if let Some(tool_use) = state.tool_uses_by_index.remove(&index) {
 742                                let input_json = tool_use.input_json.trim();
 743
 744                                return Some((
 745                                    vec![maybe!({
 746                                        Ok(LanguageModelCompletionEvent::ToolUse(
 747                                            LanguageModelToolUse {
 748                                                id: tool_use.id.into(),
 749                                                name: tool_use.name.into(),
 750                                                is_input_complete: true,
 751                                                input: if input_json.is_empty() {
 752                                                    serde_json::Value::Object(
 753                                                        serde_json::Map::default(),
 754                                                    )
 755                                                } else {
 756                                                    serde_json::Value::from_str(
 757                                                        input_json
 758                                                    )
 759                                                    .map_err(|err| anyhow!("Error parsing tool call input JSON: {err:?} - JSON string was: {input_json:?}"))?
 760                                                },
 761                                                raw_input: tool_use.input_json.clone(),
 762                                            },
 763                                        ))
 764                                    })],
 765                                    state,
 766                                ));
 767                            }
 768                        }
 769                        Event::MessageStart { message } => {
 770                            update_usage(&mut state.usage, &message.usage);
 771                            return Some((
 772                                vec![
 773                                    Ok(LanguageModelCompletionEvent::UsageUpdate(convert_usage(
 774                                        &state.usage,
 775                                    ))),
 776                                    Ok(LanguageModelCompletionEvent::StartMessage {
 777                                        message_id: message.id,
 778                                    }),
 779                                ],
 780                                state,
 781                            ));
 782                        }
 783                        Event::MessageDelta { delta, usage } => {
 784                            update_usage(&mut state.usage, &usage);
 785                            if let Some(stop_reason) = delta.stop_reason.as_deref() {
 786                                state.stop_reason = match stop_reason {
 787                                    "end_turn" => StopReason::EndTurn,
 788                                    "max_tokens" => StopReason::MaxTokens,
 789                                    "tool_use" => StopReason::ToolUse,
 790                                    _ => {
 791                                        log::error!(
 792                                            "Unexpected anthropic stop_reason: {stop_reason}"
 793                                        );
 794                                        StopReason::EndTurn
 795                                    }
 796                                };
 797                            }
 798                            return Some((
 799                                vec![Ok(LanguageModelCompletionEvent::UsageUpdate(
 800                                    convert_usage(&state.usage),
 801                                ))],
 802                                state,
 803                            ));
 804                        }
 805                        Event::MessageStop => {
 806                            return Some((
 807                                vec![Ok(LanguageModelCompletionEvent::Stop(state.stop_reason))],
 808                                state,
 809                            ));
 810                        }
 811                        Event::Error { error } => {
 812                            return Some((
 813                                vec![Err(anyhow!(AnthropicError::ApiError(error)))],
 814                                state,
 815                            ));
 816                        }
 817                        _ => {}
 818                    },
 819                    Err(err) => {
 820                        return Some((vec![Err(anthropic_err_to_anyhow(err))], state));
 821                    }
 822                }
 823            }
 824
 825            None
 826        },
 827    )
 828    .flat_map(futures::stream::iter)
 829}
 830
 831pub fn anthropic_err_to_anyhow(err: AnthropicError) -> anyhow::Error {
 832    if let AnthropicError::ApiError(api_err) = &err {
 833        if let Some(tokens) = api_err.match_window_exceeded() {
 834            return anyhow!(LanguageModelKnownError::ContextWindowLimitExceeded { tokens });
 835        }
 836    }
 837
 838    anyhow!(err)
 839}
 840
 841/// Updates usage data by preferring counts from `new`.
 842fn update_usage(usage: &mut Usage, new: &Usage) {
 843    if let Some(input_tokens) = new.input_tokens {
 844        usage.input_tokens = Some(input_tokens);
 845    }
 846    if let Some(output_tokens) = new.output_tokens {
 847        usage.output_tokens = Some(output_tokens);
 848    }
 849    if let Some(cache_creation_input_tokens) = new.cache_creation_input_tokens {
 850        usage.cache_creation_input_tokens = Some(cache_creation_input_tokens);
 851    }
 852    if let Some(cache_read_input_tokens) = new.cache_read_input_tokens {
 853        usage.cache_read_input_tokens = Some(cache_read_input_tokens);
 854    }
 855}
 856
 857fn convert_usage(usage: &Usage) -> language_model::TokenUsage {
 858    language_model::TokenUsage {
 859        input_tokens: usage.input_tokens.unwrap_or(0),
 860        output_tokens: usage.output_tokens.unwrap_or(0),
 861        cache_creation_input_tokens: usage.cache_creation_input_tokens.unwrap_or(0),
 862        cache_read_input_tokens: usage.cache_read_input_tokens.unwrap_or(0),
 863    }
 864}
 865
 866struct ConfigurationView {
 867    api_key_editor: Entity<Editor>,
 868    state: gpui::Entity<State>,
 869    load_credentials_task: Option<Task<()>>,
 870}
 871
 872impl ConfigurationView {
 873    const PLACEHOLDER_TEXT: &'static str = "sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
 874
 875    fn new(state: gpui::Entity<State>, window: &mut Window, cx: &mut Context<Self>) -> Self {
 876        cx.observe(&state, |_, _, cx| {
 877            cx.notify();
 878        })
 879        .detach();
 880
 881        let load_credentials_task = Some(cx.spawn({
 882            let state = state.clone();
 883            async move |this, cx| {
 884                if let Some(task) = state
 885                    .update(cx, |state, cx| state.authenticate(cx))
 886                    .log_err()
 887                {
 888                    // We don't log an error, because "not signed in" is also an error.
 889                    let _ = task.await;
 890                }
 891                this.update(cx, |this, cx| {
 892                    this.load_credentials_task = None;
 893                    cx.notify();
 894                })
 895                .log_err();
 896            }
 897        }));
 898
 899        Self {
 900            api_key_editor: cx.new(|cx| {
 901                let mut editor = Editor::single_line(window, cx);
 902                editor.set_placeholder_text(Self::PLACEHOLDER_TEXT, cx);
 903                editor
 904            }),
 905            state,
 906            load_credentials_task,
 907        }
 908    }
 909
 910    fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
 911        let api_key = self.api_key_editor.read(cx).text(cx);
 912        if api_key.is_empty() {
 913            return;
 914        }
 915
 916        let state = self.state.clone();
 917        cx.spawn_in(window, async move |_, cx| {
 918            state
 919                .update(cx, |state, cx| state.set_api_key(api_key, cx))?
 920                .await
 921        })
 922        .detach_and_log_err(cx);
 923
 924        cx.notify();
 925    }
 926
 927    fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 928        self.api_key_editor
 929            .update(cx, |editor, cx| editor.set_text("", window, cx));
 930
 931        let state = self.state.clone();
 932        cx.spawn_in(window, async move |_, cx| {
 933            state.update(cx, |state, cx| state.reset_api_key(cx))?.await
 934        })
 935        .detach_and_log_err(cx);
 936
 937        cx.notify();
 938    }
 939
 940    fn render_api_key_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
 941        let settings = ThemeSettings::get_global(cx);
 942        let text_style = TextStyle {
 943            color: cx.theme().colors().text,
 944            font_family: settings.ui_font.family.clone(),
 945            font_features: settings.ui_font.features.clone(),
 946            font_fallbacks: settings.ui_font.fallbacks.clone(),
 947            font_size: rems(0.875).into(),
 948            font_weight: settings.ui_font.weight,
 949            font_style: FontStyle::Normal,
 950            line_height: relative(1.3),
 951            white_space: WhiteSpace::Normal,
 952            ..Default::default()
 953        };
 954        EditorElement::new(
 955            &self.api_key_editor,
 956            EditorStyle {
 957                background: cx.theme().colors().editor_background,
 958                local_player: cx.theme().players().local(),
 959                text: text_style,
 960                ..Default::default()
 961            },
 962        )
 963    }
 964
 965    fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
 966        !self.state.read(cx).is_authenticated()
 967    }
 968}
 969
 970impl Render for ConfigurationView {
 971    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 972        let env_var_set = self.state.read(cx).api_key_from_env;
 973
 974        if self.load_credentials_task.is_some() {
 975            div().child(Label::new("Loading credentials...")).into_any()
 976        } else if self.should_render_editor(cx) {
 977            v_flex()
 978                .size_full()
 979                .on_action(cx.listener(Self::save_api_key))
 980                .child(Label::new("To use Zed's assistant with Anthropic, you need to add an API key. Follow these steps:"))
 981                .child(
 982                    List::new()
 983                        .child(
 984                            InstructionListItem::new(
 985                                "Create one by visiting",
 986                                Some("Anthropic's settings"),
 987                                Some("https://console.anthropic.com/settings/keys")
 988                            )
 989                        )
 990                        .child(
 991                            InstructionListItem::text_only("Paste your API key below and hit enter to start using the assistant")
 992                        )
 993                )
 994                .child(
 995                    h_flex()
 996                        .w_full()
 997                        .my_2()
 998                        .px_2()
 999                        .py_1()
1000                        .bg(cx.theme().colors().editor_background)
1001                        .border_1()
1002                        .border_color(cx.theme().colors().border)
1003                        .rounded_sm()
1004                        .child(self.render_api_key_editor(cx)),
1005                )
1006                .child(
1007                    Label::new(
1008                        format!("You can also assign the {ANTHROPIC_API_KEY_VAR} environment variable and restart Zed."),
1009                    )
1010                    .size(LabelSize::Small)
1011                    .color(Color::Muted),
1012                )
1013                .into_any()
1014        } else {
1015            h_flex()
1016                .mt_1()
1017                .p_1()
1018                .justify_between()
1019                .rounded_md()
1020                .border_1()
1021                .border_color(cx.theme().colors().border)
1022                .bg(cx.theme().colors().background)
1023                .child(
1024                    h_flex()
1025                        .gap_1()
1026                        .child(Icon::new(IconName::Check).color(Color::Success))
1027                        .child(Label::new(if env_var_set {
1028                            format!("API key set in {ANTHROPIC_API_KEY_VAR} environment variable.")
1029                        } else {
1030                            "API key configured.".to_string()
1031                        })),
1032                )
1033                .child(
1034                    Button::new("reset-key", "Reset Key")
1035                        .label_size(LabelSize::Small)
1036                        .icon(Some(IconName::Trash))
1037                        .icon_size(IconSize::Small)
1038                        .icon_position(IconPosition::Start)
1039                        .disabled(env_var_set)
1040                        .when(env_var_set, |this| {
1041                            this.tooltip(Tooltip::text(format!("To reset your API key, unset the {ANTHROPIC_API_KEY_VAR} environment variable.")))
1042                        })
1043                        .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx))),
1044                )
1045                .into_any()
1046        }
1047    }
1048}