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