google.rs

  1use anyhow::{Context as _, Result, anyhow};
  2use collections::BTreeMap;
  3use credentials_provider::CredentialsProvider;
  4use editor::{Editor, EditorElement, EditorStyle};
  5use futures::{FutureExt, Stream, StreamExt, future::BoxFuture};
  6use google_ai::{
  7    FunctionDeclaration, GenerateContentResponse, GoogleModelMode, Part, SystemInstruction,
  8    ThinkingConfig, UsageMetadata,
  9};
 10use gpui::{
 11    AnyView, App, AsyncApp, Context, Entity, FontStyle, Subscription, Task, TextStyle, WhiteSpace,
 12};
 13use http_client::HttpClient;
 14use language_model::{
 15    AuthenticateError, ConfigurationViewTargetAgent, LanguageModelCompletionError,
 16    LanguageModelCompletionEvent, LanguageModelToolChoice, LanguageModelToolSchemaFormat,
 17    LanguageModelToolUse, LanguageModelToolUseId, MessageContent, StopReason,
 18};
 19use language_model::{
 20    LanguageModel, LanguageModelId, LanguageModelName, LanguageModelProvider,
 21    LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState,
 22    LanguageModelRequest, RateLimiter, Role,
 23};
 24use schemars::JsonSchema;
 25use serde::{Deserialize, Serialize};
 26pub use settings::GoogleAvailableModel as AvailableModel;
 27use settings::{Settings, SettingsStore};
 28use std::pin::Pin;
 29use std::sync::{
 30    Arc,
 31    atomic::{self, AtomicU64},
 32};
 33use strum::IntoEnumIterator;
 34use theme::ThemeSettings;
 35use ui::{Icon, IconName, List, Tooltip, prelude::*};
 36use util::ResultExt;
 37
 38use crate::AllLanguageModelSettings;
 39use crate::ui::InstructionListItem;
 40
 41use super::anthropic::ApiKey;
 42
 43const PROVIDER_ID: LanguageModelProviderId = language_model::GOOGLE_PROVIDER_ID;
 44const PROVIDER_NAME: LanguageModelProviderName = language_model::GOOGLE_PROVIDER_NAME;
 45
 46#[derive(Default, Clone, Debug, PartialEq)]
 47pub struct GoogleSettings {
 48    pub api_url: String,
 49    pub available_models: Vec<AvailableModel>,
 50}
 51
 52#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
 53#[serde(tag = "type", rename_all = "lowercase")]
 54pub enum ModelMode {
 55    #[default]
 56    Default,
 57    Thinking {
 58        /// The maximum number of tokens to use for reasoning. Must be lower than the model's `max_output_tokens`.
 59        budget_tokens: Option<u32>,
 60    },
 61}
 62
 63pub struct GoogleLanguageModelProvider {
 64    http_client: Arc<dyn HttpClient>,
 65    state: gpui::Entity<State>,
 66}
 67
 68pub struct State {
 69    api_key: Option<String>,
 70    api_key_from_env: bool,
 71    _subscription: Subscription,
 72}
 73
 74const GEMINI_API_KEY_VAR: &str = "GEMINI_API_KEY";
 75const GOOGLE_AI_API_KEY_VAR: &str = "GOOGLE_AI_API_KEY";
 76
 77impl State {
 78    fn is_authenticated(&self) -> bool {
 79        self.api_key.is_some()
 80    }
 81
 82    fn reset_api_key(&self, cx: &mut Context<Self>) -> Task<Result<()>> {
 83        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 84        let api_url = AllLanguageModelSettings::get_global(cx)
 85            .google
 86            .api_url
 87            .clone();
 88        cx.spawn(async move |this, cx| {
 89            credentials_provider
 90                .delete_credentials(&api_url, cx)
 91                .await
 92                .log_err();
 93            this.update(cx, |this, cx| {
 94                this.api_key = None;
 95                this.api_key_from_env = false;
 96                cx.notify();
 97            })
 98        })
 99    }
100
101    fn set_api_key(&mut self, api_key: String, cx: &mut Context<Self>) -> Task<Result<()>> {
102        let credentials_provider = <dyn CredentialsProvider>::global(cx);
103        let api_url = AllLanguageModelSettings::get_global(cx)
104            .google
105            .api_url
106            .clone();
107        cx.spawn(async move |this, cx| {
108            credentials_provider
109                .write_credentials(&api_url, "Bearer", api_key.as_bytes(), cx)
110                .await?;
111            this.update(cx, |this, cx| {
112                this.api_key = Some(api_key);
113                cx.notify();
114            })
115        })
116    }
117
118    fn authenticate(&self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
119        if self.is_authenticated() {
120            return Task::ready(Ok(()));
121        }
122
123        let credentials_provider = <dyn CredentialsProvider>::global(cx);
124        let api_url = AllLanguageModelSettings::get_global(cx)
125            .google
126            .api_url
127            .clone();
128
129        cx.spawn(async move |this, cx| {
130            let (api_key, from_env) = if let Ok(api_key) = std::env::var(GOOGLE_AI_API_KEY_VAR) {
131                (api_key, true)
132            } else if let Ok(api_key) = std::env::var(GEMINI_API_KEY_VAR) {
133                (api_key, true)
134            } else {
135                let (_, api_key) = credentials_provider
136                    .read_credentials(&api_url, cx)
137                    .await?
138                    .ok_or(AuthenticateError::CredentialsNotFound)?;
139                (
140                    String::from_utf8(api_key).context("invalid {PROVIDER_NAME} API key")?,
141                    false,
142                )
143            };
144
145            this.update(cx, |this, cx| {
146                this.api_key = Some(api_key);
147                this.api_key_from_env = from_env;
148                cx.notify();
149            })?;
150
151            Ok(())
152        })
153    }
154}
155
156impl GoogleLanguageModelProvider {
157    pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
158        let state = cx.new(|cx| State {
159            api_key: None,
160            api_key_from_env: false,
161            _subscription: cx.observe_global::<SettingsStore>(|_, cx| {
162                cx.notify();
163            }),
164        });
165
166        Self { http_client, state }
167    }
168
169    fn create_language_model(&self, model: google_ai::Model) -> Arc<dyn LanguageModel> {
170        Arc::new(GoogleLanguageModel {
171            id: LanguageModelId::from(model.id().to_string()),
172            model,
173            state: self.state.clone(),
174            http_client: self.http_client.clone(),
175            request_limiter: RateLimiter::new(4),
176        })
177    }
178
179    pub fn api_key(cx: &mut App) -> Task<Result<ApiKey>> {
180        let credentials_provider = <dyn CredentialsProvider>::global(cx);
181        let api_url = AllLanguageModelSettings::get_global(cx)
182            .google
183            .api_url
184            .clone();
185
186        if let Ok(key) = std::env::var(GEMINI_API_KEY_VAR) {
187            Task::ready(Ok(ApiKey {
188                key,
189                from_env: true,
190            }))
191        } else {
192            cx.spawn(async move |cx| {
193                let (_, api_key) = credentials_provider
194                    .read_credentials(&api_url, cx)
195                    .await?
196                    .ok_or(AuthenticateError::CredentialsNotFound)?;
197
198                Ok(ApiKey {
199                    key: String::from_utf8(api_key).context("invalid {PROVIDER_NAME} API key")?,
200                    from_env: false,
201                })
202            })
203        }
204    }
205}
206
207impl LanguageModelProviderState for GoogleLanguageModelProvider {
208    type ObservableEntity = State;
209
210    fn observable_entity(&self) -> Option<gpui::Entity<Self::ObservableEntity>> {
211        Some(self.state.clone())
212    }
213}
214
215impl LanguageModelProvider for GoogleLanguageModelProvider {
216    fn id(&self) -> LanguageModelProviderId {
217        PROVIDER_ID
218    }
219
220    fn name(&self) -> LanguageModelProviderName {
221        PROVIDER_NAME
222    }
223
224    fn icon(&self) -> IconName {
225        IconName::AiGoogle
226    }
227
228    fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
229        Some(self.create_language_model(google_ai::Model::default()))
230    }
231
232    fn default_fast_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
233        Some(self.create_language_model(google_ai::Model::default_fast()))
234    }
235
236    fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
237        let mut models = BTreeMap::default();
238
239        // Add base models from google_ai::Model::iter()
240        for model in google_ai::Model::iter() {
241            if !matches!(model, google_ai::Model::Custom { .. }) {
242                models.insert(model.id().to_string(), model);
243            }
244        }
245
246        // Override with available models from settings
247        for model in &AllLanguageModelSettings::get_global(cx)
248            .google
249            .available_models
250        {
251            models.insert(
252                model.name.clone(),
253                google_ai::Model::Custom {
254                    name: model.name.clone(),
255                    display_name: model.display_name.clone(),
256                    max_tokens: model.max_tokens,
257                    mode: model.mode.unwrap_or_default().into(),
258                },
259            );
260        }
261
262        models
263            .into_values()
264            .map(|model| {
265                Arc::new(GoogleLanguageModel {
266                    id: LanguageModelId::from(model.id().to_string()),
267                    model,
268                    state: self.state.clone(),
269                    http_client: self.http_client.clone(),
270                    request_limiter: RateLimiter::new(4),
271                }) as Arc<dyn LanguageModel>
272            })
273            .collect()
274    }
275
276    fn is_authenticated(&self, cx: &App) -> bool {
277        self.state.read(cx).is_authenticated()
278    }
279
280    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
281        self.state.update(cx, |state, cx| state.authenticate(cx))
282    }
283
284    fn configuration_view(
285        &self,
286        target_agent: language_model::ConfigurationViewTargetAgent,
287        window: &mut Window,
288        cx: &mut App,
289    ) -> AnyView {
290        cx.new(|cx| ConfigurationView::new(self.state.clone(), target_agent, window, cx))
291            .into()
292    }
293
294    fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
295        self.state.update(cx, |state, cx| state.reset_api_key(cx))
296    }
297}
298
299pub struct GoogleLanguageModel {
300    id: LanguageModelId,
301    model: google_ai::Model,
302    state: gpui::Entity<State>,
303    http_client: Arc<dyn HttpClient>,
304    request_limiter: RateLimiter,
305}
306
307impl GoogleLanguageModel {
308    fn stream_completion(
309        &self,
310        request: google_ai::GenerateContentRequest,
311        cx: &AsyncApp,
312    ) -> BoxFuture<
313        'static,
314        Result<futures::stream::BoxStream<'static, Result<GenerateContentResponse>>>,
315    > {
316        let http_client = self.http_client.clone();
317
318        let Ok((api_key, api_url)) = cx.read_entity(&self.state, |state, cx| {
319            let settings = &AllLanguageModelSettings::get_global(cx).google;
320            (state.api_key.clone(), settings.api_url.clone())
321        }) else {
322            return futures::future::ready(Err(anyhow!("App state dropped"))).boxed();
323        };
324
325        async move {
326            let api_key = api_key.context("Missing Google API key")?;
327            let request = google_ai::stream_generate_content(
328                http_client.as_ref(),
329                &api_url,
330                &api_key,
331                request,
332            );
333            request.await.context("failed to stream completion")
334        }
335        .boxed()
336    }
337}
338
339impl LanguageModel for GoogleLanguageModel {
340    fn id(&self) -> LanguageModelId {
341        self.id.clone()
342    }
343
344    fn name(&self) -> LanguageModelName {
345        LanguageModelName::from(self.model.display_name().to_string())
346    }
347
348    fn provider_id(&self) -> LanguageModelProviderId {
349        PROVIDER_ID
350    }
351
352    fn provider_name(&self) -> LanguageModelProviderName {
353        PROVIDER_NAME
354    }
355
356    fn supports_tools(&self) -> bool {
357        self.model.supports_tools()
358    }
359
360    fn supports_images(&self) -> bool {
361        self.model.supports_images()
362    }
363
364    fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
365        match choice {
366            LanguageModelToolChoice::Auto
367            | LanguageModelToolChoice::Any
368            | LanguageModelToolChoice::None => true,
369        }
370    }
371
372    fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
373        LanguageModelToolSchemaFormat::JsonSchemaSubset
374    }
375
376    fn telemetry_id(&self) -> String {
377        format!("google/{}", self.model.request_id())
378    }
379
380    fn max_token_count(&self) -> u64 {
381        self.model.max_token_count()
382    }
383
384    fn max_output_tokens(&self) -> Option<u64> {
385        self.model.max_output_tokens()
386    }
387
388    fn count_tokens(
389        &self,
390        request: LanguageModelRequest,
391        cx: &App,
392    ) -> BoxFuture<'static, Result<u64>> {
393        let model_id = self.model.request_id().to_string();
394        let request = into_google(request, model_id, self.model.mode());
395        let http_client = self.http_client.clone();
396        let api_key = self.state.read(cx).api_key.clone();
397
398        let settings = &AllLanguageModelSettings::get_global(cx).google;
399        let api_url = settings.api_url.clone();
400
401        async move {
402            let api_key = api_key.context("Missing Google API key")?;
403            let response = google_ai::count_tokens(
404                http_client.as_ref(),
405                &api_url,
406                &api_key,
407                google_ai::CountTokensRequest {
408                    generate_content_request: request,
409                },
410            )
411            .await?;
412            Ok(response.total_tokens)
413        }
414        .boxed()
415    }
416
417    fn stream_completion(
418        &self,
419        request: LanguageModelRequest,
420        cx: &AsyncApp,
421    ) -> BoxFuture<
422        'static,
423        Result<
424            futures::stream::BoxStream<
425                'static,
426                Result<LanguageModelCompletionEvent, LanguageModelCompletionError>,
427            >,
428            LanguageModelCompletionError,
429        >,
430    > {
431        let request = into_google(
432            request,
433            self.model.request_id().to_string(),
434            self.model.mode(),
435        );
436        let request = self.stream_completion(request, cx);
437        let future = self.request_limiter.stream(async move {
438            let response = request.await.map_err(LanguageModelCompletionError::from)?;
439            Ok(GoogleEventMapper::new().map_stream(response))
440        });
441        async move { Ok(future.await?.boxed()) }.boxed()
442    }
443}
444
445pub fn into_google(
446    mut request: LanguageModelRequest,
447    model_id: String,
448    mode: GoogleModelMode,
449) -> google_ai::GenerateContentRequest {
450    fn map_content(content: Vec<MessageContent>) -> Vec<Part> {
451        content
452            .into_iter()
453            .flat_map(|content| match content {
454                language_model::MessageContent::Text(text) => {
455                    if !text.is_empty() {
456                        vec![Part::TextPart(google_ai::TextPart { text })]
457                    } else {
458                        vec![]
459                    }
460                }
461                language_model::MessageContent::Thinking {
462                    text: _,
463                    signature: Some(signature),
464                } => {
465                    if !signature.is_empty() {
466                        vec![Part::ThoughtPart(google_ai::ThoughtPart {
467                            thought: true,
468                            thought_signature: signature,
469                        })]
470                    } else {
471                        vec![]
472                    }
473                }
474                language_model::MessageContent::Thinking { .. } => {
475                    vec![]
476                }
477                language_model::MessageContent::RedactedThinking(_) => vec![],
478                language_model::MessageContent::Image(image) => {
479                    vec![Part::InlineDataPart(google_ai::InlineDataPart {
480                        inline_data: google_ai::GenerativeContentBlob {
481                            mime_type: "image/png".to_string(),
482                            data: image.source.to_string(),
483                        },
484                    })]
485                }
486                language_model::MessageContent::ToolUse(tool_use) => {
487                    vec![Part::FunctionCallPart(google_ai::FunctionCallPart {
488                        function_call: google_ai::FunctionCall {
489                            name: tool_use.name.to_string(),
490                            args: tool_use.input,
491                        },
492                    })]
493                }
494                language_model::MessageContent::ToolResult(tool_result) => {
495                    match tool_result.content {
496                        language_model::LanguageModelToolResultContent::Text(text) => {
497                            vec![Part::FunctionResponsePart(
498                                google_ai::FunctionResponsePart {
499                                    function_response: google_ai::FunctionResponse {
500                                        name: tool_result.tool_name.to_string(),
501                                        // The API expects a valid JSON object
502                                        response: serde_json::json!({
503                                            "output": text
504                                        }),
505                                    },
506                                },
507                            )]
508                        }
509                        language_model::LanguageModelToolResultContent::Image(image) => {
510                            vec![
511                                Part::FunctionResponsePart(google_ai::FunctionResponsePart {
512                                    function_response: google_ai::FunctionResponse {
513                                        name: tool_result.tool_name.to_string(),
514                                        // The API expects a valid JSON object
515                                        response: serde_json::json!({
516                                            "output": "Tool responded with an image"
517                                        }),
518                                    },
519                                }),
520                                Part::InlineDataPart(google_ai::InlineDataPart {
521                                    inline_data: google_ai::GenerativeContentBlob {
522                                        mime_type: "image/png".to_string(),
523                                        data: image.source.to_string(),
524                                    },
525                                }),
526                            ]
527                        }
528                    }
529                }
530            })
531            .collect()
532    }
533
534    let system_instructions = if request
535        .messages
536        .first()
537        .is_some_and(|msg| matches!(msg.role, Role::System))
538    {
539        let message = request.messages.remove(0);
540        Some(SystemInstruction {
541            parts: map_content(message.content),
542        })
543    } else {
544        None
545    };
546
547    google_ai::GenerateContentRequest {
548        model: google_ai::ModelName { model_id },
549        system_instruction: system_instructions,
550        contents: request
551            .messages
552            .into_iter()
553            .filter_map(|message| {
554                let parts = map_content(message.content);
555                if parts.is_empty() {
556                    None
557                } else {
558                    Some(google_ai::Content {
559                        parts,
560                        role: match message.role {
561                            Role::User => google_ai::Role::User,
562                            Role::Assistant => google_ai::Role::Model,
563                            Role::System => google_ai::Role::User, // Google AI doesn't have a system role
564                        },
565                    })
566                }
567            })
568            .collect(),
569        generation_config: Some(google_ai::GenerationConfig {
570            candidate_count: Some(1),
571            stop_sequences: Some(request.stop),
572            max_output_tokens: None,
573            temperature: request.temperature.map(|t| t as f64).or(Some(1.0)),
574            thinking_config: match (request.thinking_allowed, mode) {
575                (true, GoogleModelMode::Thinking { budget_tokens }) => {
576                    budget_tokens.map(|thinking_budget| ThinkingConfig { thinking_budget })
577                }
578                _ => None,
579            },
580            top_p: None,
581            top_k: None,
582        }),
583        safety_settings: None,
584        tools: (!request.tools.is_empty()).then(|| {
585            vec![google_ai::Tool {
586                function_declarations: request
587                    .tools
588                    .into_iter()
589                    .map(|tool| FunctionDeclaration {
590                        name: tool.name,
591                        description: tool.description,
592                        parameters: tool.input_schema,
593                    })
594                    .collect(),
595            }]
596        }),
597        tool_config: request.tool_choice.map(|choice| google_ai::ToolConfig {
598            function_calling_config: google_ai::FunctionCallingConfig {
599                mode: match choice {
600                    LanguageModelToolChoice::Auto => google_ai::FunctionCallingMode::Auto,
601                    LanguageModelToolChoice::Any => google_ai::FunctionCallingMode::Any,
602                    LanguageModelToolChoice::None => google_ai::FunctionCallingMode::None,
603                },
604                allowed_function_names: None,
605            },
606        }),
607    }
608}
609
610pub struct GoogleEventMapper {
611    usage: UsageMetadata,
612    stop_reason: StopReason,
613}
614
615impl GoogleEventMapper {
616    pub fn new() -> Self {
617        Self {
618            usage: UsageMetadata::default(),
619            stop_reason: StopReason::EndTurn,
620        }
621    }
622
623    pub fn map_stream(
624        mut self,
625        events: Pin<Box<dyn Send + Stream<Item = Result<GenerateContentResponse>>>>,
626    ) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>
627    {
628        events
629            .map(Some)
630            .chain(futures::stream::once(async { None }))
631            .flat_map(move |event| {
632                futures::stream::iter(match event {
633                    Some(Ok(event)) => self.map_event(event),
634                    Some(Err(error)) => {
635                        vec![Err(LanguageModelCompletionError::from(error))]
636                    }
637                    None => vec![Ok(LanguageModelCompletionEvent::Stop(self.stop_reason))],
638                })
639            })
640    }
641
642    pub fn map_event(
643        &mut self,
644        event: GenerateContentResponse,
645    ) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
646        static TOOL_CALL_COUNTER: AtomicU64 = AtomicU64::new(0);
647
648        let mut events: Vec<_> = Vec::new();
649        let mut wants_to_use_tool = false;
650        if let Some(usage_metadata) = event.usage_metadata {
651            update_usage(&mut self.usage, &usage_metadata);
652            events.push(Ok(LanguageModelCompletionEvent::UsageUpdate(
653                convert_usage(&self.usage),
654            )))
655        }
656        if let Some(candidates) = event.candidates {
657            for candidate in candidates {
658                if let Some(finish_reason) = candidate.finish_reason.as_deref() {
659                    self.stop_reason = match finish_reason {
660                        "STOP" => StopReason::EndTurn,
661                        "MAX_TOKENS" => StopReason::MaxTokens,
662                        _ => {
663                            log::error!("Unexpected google finish_reason: {finish_reason}");
664                            StopReason::EndTurn
665                        }
666                    };
667                }
668                candidate
669                    .content
670                    .parts
671                    .into_iter()
672                    .for_each(|part| match part {
673                        Part::TextPart(text_part) => {
674                            events.push(Ok(LanguageModelCompletionEvent::Text(text_part.text)))
675                        }
676                        Part::InlineDataPart(_) => {}
677                        Part::FunctionCallPart(function_call_part) => {
678                            wants_to_use_tool = true;
679                            let name: Arc<str> = function_call_part.function_call.name.into();
680                            let next_tool_id =
681                                TOOL_CALL_COUNTER.fetch_add(1, atomic::Ordering::SeqCst);
682                            let id: LanguageModelToolUseId =
683                                format!("{}-{}", name, next_tool_id).into();
684
685                            events.push(Ok(LanguageModelCompletionEvent::ToolUse(
686                                LanguageModelToolUse {
687                                    id,
688                                    name,
689                                    is_input_complete: true,
690                                    raw_input: function_call_part.function_call.args.to_string(),
691                                    input: function_call_part.function_call.args,
692                                },
693                            )));
694                        }
695                        Part::FunctionResponsePart(_) => {}
696                        Part::ThoughtPart(part) => {
697                            events.push(Ok(LanguageModelCompletionEvent::Thinking {
698                                text: "(Encrypted thought)".to_string(), // TODO: Can we populate this from thought summaries?
699                                signature: Some(part.thought_signature),
700                            }));
701                        }
702                    });
703            }
704        }
705
706        // Even when Gemini wants to use a Tool, the API
707        // responds with `finish_reason: STOP`
708        if wants_to_use_tool {
709            self.stop_reason = StopReason::ToolUse;
710            events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::ToolUse)));
711        }
712        events
713    }
714}
715
716pub fn count_google_tokens(
717    request: LanguageModelRequest,
718    cx: &App,
719) -> BoxFuture<'static, Result<u64>> {
720    // We couldn't use the GoogleLanguageModelProvider to count tokens because the github copilot doesn't have the access to google_ai directly.
721    // So we have to use tokenizer from tiktoken_rs to count tokens.
722    cx.background_spawn(async move {
723        let messages = request
724            .messages
725            .into_iter()
726            .map(|message| tiktoken_rs::ChatCompletionRequestMessage {
727                role: match message.role {
728                    Role::User => "user".into(),
729                    Role::Assistant => "assistant".into(),
730                    Role::System => "system".into(),
731                },
732                content: Some(message.string_contents()),
733                name: None,
734                function_call: None,
735            })
736            .collect::<Vec<_>>();
737
738        // Tiktoken doesn't yet support these models, so we manually use the
739        // same tokenizer as GPT-4.
740        tiktoken_rs::num_tokens_from_messages("gpt-4", &messages).map(|tokens| tokens as u64)
741    })
742    .boxed()
743}
744
745fn update_usage(usage: &mut UsageMetadata, new: &UsageMetadata) {
746    if let Some(prompt_token_count) = new.prompt_token_count {
747        usage.prompt_token_count = Some(prompt_token_count);
748    }
749    if let Some(cached_content_token_count) = new.cached_content_token_count {
750        usage.cached_content_token_count = Some(cached_content_token_count);
751    }
752    if let Some(candidates_token_count) = new.candidates_token_count {
753        usage.candidates_token_count = Some(candidates_token_count);
754    }
755    if let Some(tool_use_prompt_token_count) = new.tool_use_prompt_token_count {
756        usage.tool_use_prompt_token_count = Some(tool_use_prompt_token_count);
757    }
758    if let Some(thoughts_token_count) = new.thoughts_token_count {
759        usage.thoughts_token_count = Some(thoughts_token_count);
760    }
761    if let Some(total_token_count) = new.total_token_count {
762        usage.total_token_count = Some(total_token_count);
763    }
764}
765
766fn convert_usage(usage: &UsageMetadata) -> language_model::TokenUsage {
767    let prompt_tokens = usage.prompt_token_count.unwrap_or(0);
768    let cached_tokens = usage.cached_content_token_count.unwrap_or(0);
769    let input_tokens = prompt_tokens - cached_tokens;
770    let output_tokens = usage.candidates_token_count.unwrap_or(0);
771
772    language_model::TokenUsage {
773        input_tokens,
774        output_tokens,
775        cache_read_input_tokens: cached_tokens,
776        cache_creation_input_tokens: 0,
777    }
778}
779
780struct ConfigurationView {
781    api_key_editor: Entity<Editor>,
782    state: gpui::Entity<State>,
783    target_agent: language_model::ConfigurationViewTargetAgent,
784    load_credentials_task: Option<Task<()>>,
785}
786
787impl ConfigurationView {
788    fn new(
789        state: gpui::Entity<State>,
790        target_agent: language_model::ConfigurationViewTargetAgent,
791        window: &mut Window,
792        cx: &mut Context<Self>,
793    ) -> Self {
794        cx.observe(&state, |_, _, cx| {
795            cx.notify();
796        })
797        .detach();
798
799        let load_credentials_task = Some(cx.spawn_in(window, {
800            let state = state.clone();
801            async move |this, cx| {
802                if let Some(task) = state
803                    .update(cx, |state, cx| state.authenticate(cx))
804                    .log_err()
805                {
806                    // We don't log an error, because "not signed in" is also an error.
807                    let _ = task.await;
808                }
809                this.update(cx, |this, cx| {
810                    this.load_credentials_task = None;
811                    cx.notify();
812                })
813                .log_err();
814            }
815        }));
816
817        Self {
818            api_key_editor: cx.new(|cx| {
819                let mut editor = Editor::single_line(window, cx);
820                editor.set_placeholder_text("AIzaSy...", window, cx);
821                editor
822            }),
823            target_agent,
824            state,
825            load_credentials_task,
826        }
827    }
828
829    fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
830        let api_key = self.api_key_editor.read(cx).text(cx);
831        if api_key.is_empty() {
832            return;
833        }
834
835        let state = self.state.clone();
836        cx.spawn_in(window, async move |_, cx| {
837            state
838                .update(cx, |state, cx| state.set_api_key(api_key, cx))?
839                .await
840        })
841        .detach_and_log_err(cx);
842
843        cx.notify();
844    }
845
846    fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
847        self.api_key_editor
848            .update(cx, |editor, cx| editor.set_text("", window, cx));
849
850        let state = self.state.clone();
851        cx.spawn_in(window, async move |_, cx| {
852            state.update(cx, |state, cx| state.reset_api_key(cx))?.await
853        })
854        .detach_and_log_err(cx);
855
856        cx.notify();
857    }
858
859    fn render_api_key_editor(&self, cx: &mut Context<Self>) -> impl IntoElement {
860        let settings = ThemeSettings::get_global(cx);
861        let text_style = TextStyle {
862            color: cx.theme().colors().text,
863            font_family: settings.ui_font.family.clone(),
864            font_features: settings.ui_font.features.clone(),
865            font_fallbacks: settings.ui_font.fallbacks.clone(),
866            font_size: rems(0.875).into(),
867            font_weight: settings.ui_font.weight,
868            font_style: FontStyle::Normal,
869            line_height: relative(1.3),
870            white_space: WhiteSpace::Normal,
871            ..Default::default()
872        };
873        EditorElement::new(
874            &self.api_key_editor,
875            EditorStyle {
876                background: cx.theme().colors().editor_background,
877                local_player: cx.theme().players().local(),
878                text: text_style,
879                ..Default::default()
880            },
881        )
882    }
883
884    fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
885        !self.state.read(cx).is_authenticated()
886    }
887}
888
889impl Render for ConfigurationView {
890    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
891        let env_var_set = self.state.read(cx).api_key_from_env;
892
893        if self.load_credentials_task.is_some() {
894            div().child(Label::new("Loading credentials...")).into_any()
895        } else if self.should_render_editor(cx) {
896            v_flex()
897                .size_full()
898                .on_action(cx.listener(Self::save_api_key))
899                .child(Label::new(format!("To use {}, you need to add an API key. Follow these steps:", match &self.target_agent {
900                    ConfigurationViewTargetAgent::ZedAgent => "Zed's agent with Google AI".into(),
901                    ConfigurationViewTargetAgent::Other(agent) => agent.clone(),
902                })))
903                .child(
904                    List::new()
905                        .child(InstructionListItem::new(
906                            "Create one by visiting",
907                            Some("Google AI's console"),
908                            Some("https://aistudio.google.com/app/apikey"),
909                        ))
910                        .child(InstructionListItem::text_only(
911                            "Paste your API key below and hit enter to start using the assistant",
912                        )),
913                )
914                .child(
915                    h_flex()
916                        .w_full()
917                        .my_2()
918                        .px_2()
919                        .py_1()
920                        .bg(cx.theme().colors().editor_background)
921                        .border_1()
922                        .border_color(cx.theme().colors().border)
923                        .rounded_sm()
924                        .child(self.render_api_key_editor(cx)),
925                )
926                .child(
927                    Label::new(
928                        format!("You can also assign the {GEMINI_API_KEY_VAR} environment variable and restart Zed."),
929                    )
930                    .size(LabelSize::Small).color(Color::Muted),
931                )
932                .into_any()
933        } else {
934            h_flex()
935                .mt_1()
936                .p_1()
937                .justify_between()
938                .rounded_md()
939                .border_1()
940                .border_color(cx.theme().colors().border)
941                .bg(cx.theme().colors().background)
942                .child(
943                    h_flex()
944                        .gap_1()
945                        .child(Icon::new(IconName::Check).color(Color::Success))
946                        .child(Label::new(if env_var_set {
947                            format!("API key set in {GEMINI_API_KEY_VAR} environment variable.")
948                        } else {
949                            "API key configured.".to_string()
950                        })),
951                )
952                .child(
953                    Button::new("reset-key", "Reset Key")
954                        .label_size(LabelSize::Small)
955                        .icon(Some(IconName::Trash))
956                        .icon_size(IconSize::Small)
957                        .icon_position(IconPosition::Start)
958                        .disabled(env_var_set)
959                        .when(env_var_set, |this| {
960                            this.tooltip(Tooltip::text(format!("To reset your API key, make sure {GEMINI_API_KEY_VAR} and {GOOGLE_AI_API_KEY_VAR} environment variables are unset.")))
961                        })
962                        .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx))),
963                )
964                .into_any()
965        }
966    }
967}