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