google.rs

   1use anyhow::{Context as _, Result};
   2use collections::BTreeMap;
   3use credentials_provider::CredentialsProvider;
   4use futures::{FutureExt, Stream, StreamExt, future::BoxFuture};
   5use google_ai::{
   6    FunctionDeclaration, GenerateContentResponse, GoogleModelMode, Part, SystemInstruction,
   7    ThinkingConfig, UsageMetadata,
   8};
   9use gpui::{AnyView, App, AsyncApp, Context, Entity, SharedString, Task, Window};
  10use http_client::HttpClient;
  11use language_model::{
  12    AuthenticateError, ConfigurationViewTargetAgent, EnvVar, LanguageModelCompletionError,
  13    LanguageModelCompletionEvent, LanguageModelToolChoice, LanguageModelToolSchemaFormat,
  14    LanguageModelToolUse, LanguageModelToolUseId, MessageContent, StopReason,
  15};
  16use language_model::{
  17    IconOrSvg, LanguageModel, LanguageModelId, LanguageModelName, LanguageModelProvider,
  18    LanguageModelProviderId, LanguageModelProviderName, LanguageModelProviderState,
  19    LanguageModelRequest, RateLimiter, Role,
  20};
  21use schemars::JsonSchema;
  22use serde::{Deserialize, Serialize};
  23pub use settings::GoogleAvailableModel as AvailableModel;
  24use settings::{Settings, SettingsStore};
  25use std::pin::Pin;
  26use std::sync::{
  27    Arc, LazyLock,
  28    atomic::{self, AtomicU64},
  29};
  30use strum::IntoEnumIterator;
  31use ui::{ButtonLink, ConfiguredApiCard, List, ListBulletItem, prelude::*};
  32use ui_input::InputField;
  33use util::ResultExt;
  34
  35use language_model::{ApiKey, ApiKeyState};
  36
  37const PROVIDER_ID: LanguageModelProviderId = language_model::GOOGLE_PROVIDER_ID;
  38const PROVIDER_NAME: LanguageModelProviderName = language_model::GOOGLE_PROVIDER_NAME;
  39
  40#[derive(Default, Clone, Debug, PartialEq)]
  41pub struct GoogleSettings {
  42    pub api_url: String,
  43    pub available_models: Vec<AvailableModel>,
  44}
  45
  46#[derive(Clone, Copy, Debug, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
  47#[serde(tag = "type", rename_all = "lowercase")]
  48pub enum ModelMode {
  49    #[default]
  50    Default,
  51    Thinking {
  52        /// The maximum number of tokens to use for reasoning. Must be lower than the model's `max_output_tokens`.
  53        budget_tokens: Option<u32>,
  54    },
  55}
  56
  57pub struct GoogleLanguageModelProvider {
  58    http_client: Arc<dyn HttpClient>,
  59    state: Entity<State>,
  60}
  61
  62pub struct State {
  63    api_key_state: ApiKeyState,
  64}
  65
  66const GEMINI_API_KEY_VAR_NAME: &str = "GEMINI_API_KEY";
  67const GOOGLE_AI_API_KEY_VAR_NAME: &str = "GOOGLE_AI_API_KEY";
  68
  69static API_KEY_ENV_VAR: LazyLock<EnvVar> = LazyLock::new(|| {
  70    // Try GEMINI_API_KEY first as primary, fallback to GOOGLE_AI_API_KEY
  71    EnvVar::new(GEMINI_API_KEY_VAR_NAME.into()).or(EnvVar::new(GOOGLE_AI_API_KEY_VAR_NAME.into()))
  72});
  73
  74impl State {
  75    fn is_authenticated(&self) -> bool {
  76        self.api_key_state.has_key()
  77    }
  78
  79    fn set_api_key(&mut self, api_key: Option<String>, cx: &mut Context<Self>) -> Task<Result<()>> {
  80        let api_url = GoogleLanguageModelProvider::api_url(cx);
  81        self.api_key_state
  82            .store(api_url, api_key, |this| &mut this.api_key_state, cx)
  83    }
  84
  85    fn authenticate(&mut self, cx: &mut Context<Self>) -> Task<Result<(), AuthenticateError>> {
  86        let api_url = GoogleLanguageModelProvider::api_url(cx);
  87        self.api_key_state
  88            .load_if_needed(api_url, |this| &mut this.api_key_state, cx)
  89    }
  90}
  91
  92impl GoogleLanguageModelProvider {
  93    pub fn new(http_client: Arc<dyn HttpClient>, cx: &mut App) -> Self {
  94        let state = cx.new(|cx| {
  95            cx.observe_global::<SettingsStore>(|this: &mut State, cx| {
  96                let api_url = Self::api_url(cx);
  97                this.api_key_state
  98                    .handle_url_change(api_url, |this| &mut this.api_key_state, cx);
  99                cx.notify();
 100            })
 101            .detach();
 102            State {
 103                api_key_state: ApiKeyState::new(Self::api_url(cx), (*API_KEY_ENV_VAR).clone()),
 104            }
 105        });
 106
 107        Self { http_client, state }
 108    }
 109
 110    fn create_language_model(&self, model: google_ai::Model) -> Arc<dyn LanguageModel> {
 111        Arc::new(GoogleLanguageModel {
 112            id: LanguageModelId::from(model.id().to_string()),
 113            model,
 114            state: self.state.clone(),
 115            http_client: self.http_client.clone(),
 116            request_limiter: RateLimiter::new(4),
 117        })
 118    }
 119
 120    pub fn api_key_for_gemini_cli(cx: &mut App) -> Task<Result<String>> {
 121        if let Some(key) = API_KEY_ENV_VAR.value.clone() {
 122            return Task::ready(Ok(key));
 123        }
 124        let credentials_provider = <dyn CredentialsProvider>::global(cx);
 125        let api_url = Self::api_url(cx).to_string();
 126        cx.spawn(async move |cx| {
 127            Ok(
 128                ApiKey::load_from_system_keychain(&api_url, credentials_provider.as_ref(), cx)
 129                    .await?
 130                    .key()
 131                    .to_string(),
 132            )
 133        })
 134    }
 135
 136    fn settings(cx: &App) -> &GoogleSettings {
 137        &crate::AllLanguageModelSettings::get_global(cx).google
 138    }
 139
 140    fn api_url(cx: &App) -> SharedString {
 141        let api_url = &Self::settings(cx).api_url;
 142        if api_url.is_empty() {
 143            google_ai::API_URL.into()
 144        } else {
 145            SharedString::new(api_url.as_str())
 146        }
 147    }
 148}
 149
 150impl LanguageModelProviderState for GoogleLanguageModelProvider {
 151    type ObservableEntity = State;
 152
 153    fn observable_entity(&self) -> Option<Entity<Self::ObservableEntity>> {
 154        Some(self.state.clone())
 155    }
 156}
 157
 158impl LanguageModelProvider for GoogleLanguageModelProvider {
 159    fn id(&self) -> LanguageModelProviderId {
 160        PROVIDER_ID
 161    }
 162
 163    fn name(&self) -> LanguageModelProviderName {
 164        PROVIDER_NAME
 165    }
 166
 167    fn icon(&self) -> IconOrSvg {
 168        IconOrSvg::Icon(IconName::AiGoogle)
 169    }
 170
 171    fn default_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
 172        Some(self.create_language_model(google_ai::Model::default()))
 173    }
 174
 175    fn default_fast_model(&self, _cx: &App) -> Option<Arc<dyn LanguageModel>> {
 176        Some(self.create_language_model(google_ai::Model::default_fast()))
 177    }
 178
 179    fn provided_models(&self, cx: &App) -> Vec<Arc<dyn LanguageModel>> {
 180        let mut models = BTreeMap::default();
 181
 182        // Add base models from google_ai::Model::iter()
 183        for model in google_ai::Model::iter() {
 184            if !matches!(model, google_ai::Model::Custom { .. }) {
 185                models.insert(model.id().to_string(), model);
 186            }
 187        }
 188
 189        // Override with available models from settings
 190        for model in &GoogleLanguageModelProvider::settings(cx).available_models {
 191            models.insert(
 192                model.name.clone(),
 193                google_ai::Model::Custom {
 194                    name: model.name.clone(),
 195                    display_name: model.display_name.clone(),
 196                    max_tokens: model.max_tokens,
 197                    mode: model.mode.unwrap_or_default(),
 198                },
 199            );
 200        }
 201
 202        models
 203            .into_values()
 204            .map(|model| {
 205                Arc::new(GoogleLanguageModel {
 206                    id: LanguageModelId::from(model.id().to_string()),
 207                    model,
 208                    state: self.state.clone(),
 209                    http_client: self.http_client.clone(),
 210                    request_limiter: RateLimiter::new(4),
 211                }) as Arc<dyn LanguageModel>
 212            })
 213            .collect()
 214    }
 215
 216    fn is_authenticated(&self, cx: &App) -> bool {
 217        self.state.read(cx).is_authenticated()
 218    }
 219
 220    fn authenticate(&self, cx: &mut App) -> Task<Result<(), AuthenticateError>> {
 221        self.state.update(cx, |state, cx| state.authenticate(cx))
 222    }
 223
 224    fn configuration_view(
 225        &self,
 226        target_agent: language_model::ConfigurationViewTargetAgent,
 227        window: &mut Window,
 228        cx: &mut App,
 229    ) -> AnyView {
 230        cx.new(|cx| ConfigurationView::new(self.state.clone(), target_agent, window, cx))
 231            .into()
 232    }
 233
 234    fn reset_credentials(&self, cx: &mut App) -> Task<Result<()>> {
 235        self.state
 236            .update(cx, |state, cx| state.set_api_key(None, cx))
 237    }
 238}
 239
 240pub struct GoogleLanguageModel {
 241    id: LanguageModelId,
 242    model: google_ai::Model,
 243    state: Entity<State>,
 244    http_client: Arc<dyn HttpClient>,
 245    request_limiter: RateLimiter,
 246}
 247
 248impl GoogleLanguageModel {
 249    fn stream_completion(
 250        &self,
 251        request: google_ai::GenerateContentRequest,
 252        cx: &AsyncApp,
 253    ) -> BoxFuture<
 254        'static,
 255        Result<futures::stream::BoxStream<'static, Result<GenerateContentResponse>>>,
 256    > {
 257        let http_client = self.http_client.clone();
 258
 259        let (api_key, api_url) = self.state.read_with(cx, |state, cx| {
 260            let api_url = GoogleLanguageModelProvider::api_url(cx);
 261            (state.api_key_state.key(&api_url), api_url)
 262        });
 263
 264        async move {
 265            let api_key = api_key.context("Missing Google API key")?;
 266            let request = google_ai::stream_generate_content(
 267                http_client.as_ref(),
 268                &api_url,
 269                &api_key,
 270                request,
 271            );
 272            request.await.context("failed to stream completion")
 273        }
 274        .boxed()
 275    }
 276}
 277
 278impl LanguageModel for GoogleLanguageModel {
 279    fn id(&self) -> LanguageModelId {
 280        self.id.clone()
 281    }
 282
 283    fn name(&self) -> LanguageModelName {
 284        LanguageModelName::from(self.model.display_name().to_string())
 285    }
 286
 287    fn provider_id(&self) -> LanguageModelProviderId {
 288        PROVIDER_ID
 289    }
 290
 291    fn provider_name(&self) -> LanguageModelProviderName {
 292        PROVIDER_NAME
 293    }
 294
 295    fn supports_tools(&self) -> bool {
 296        self.model.supports_tools()
 297    }
 298
 299    fn supports_images(&self) -> bool {
 300        self.model.supports_images()
 301    }
 302
 303    fn supports_tool_choice(&self, choice: LanguageModelToolChoice) -> bool {
 304        match choice {
 305            LanguageModelToolChoice::Auto
 306            | LanguageModelToolChoice::Any
 307            | LanguageModelToolChoice::None => true,
 308        }
 309    }
 310
 311    fn tool_input_format(&self) -> LanguageModelToolSchemaFormat {
 312        LanguageModelToolSchemaFormat::JsonSchemaSubset
 313    }
 314
 315    fn telemetry_id(&self) -> String {
 316        format!("google/{}", self.model.request_id())
 317    }
 318
 319    fn max_token_count(&self) -> u64 {
 320        self.model.max_token_count()
 321    }
 322
 323    fn max_output_tokens(&self) -> Option<u64> {
 324        self.model.max_output_tokens()
 325    }
 326
 327    fn count_tokens(
 328        &self,
 329        request: LanguageModelRequest,
 330        cx: &App,
 331    ) -> BoxFuture<'static, Result<u64>> {
 332        let model_id = self.model.request_id().to_string();
 333        let request = into_google(request, model_id, self.model.mode());
 334        let http_client = self.http_client.clone();
 335        let api_url = GoogleLanguageModelProvider::api_url(cx);
 336        let api_key = self.state.read(cx).api_key_state.key(&api_url);
 337
 338        async move {
 339            let Some(api_key) = api_key else {
 340                return Err(LanguageModelCompletionError::NoApiKey {
 341                    provider: PROVIDER_NAME,
 342                }
 343                .into());
 344            };
 345            let response = google_ai::count_tokens(
 346                http_client.as_ref(),
 347                &api_url,
 348                &api_key,
 349                google_ai::CountTokensRequest {
 350                    generate_content_request: request,
 351                },
 352            )
 353            .await?;
 354            Ok(response.total_tokens)
 355        }
 356        .boxed()
 357    }
 358
 359    fn stream_completion(
 360        &self,
 361        request: LanguageModelRequest,
 362        cx: &AsyncApp,
 363    ) -> BoxFuture<
 364        'static,
 365        Result<
 366            futures::stream::BoxStream<
 367                'static,
 368                Result<LanguageModelCompletionEvent, LanguageModelCompletionError>,
 369            >,
 370            LanguageModelCompletionError,
 371        >,
 372    > {
 373        let bypass_rate_limit = request.bypass_rate_limit;
 374        let request = into_google(
 375            request,
 376            self.model.request_id().to_string(),
 377            self.model.mode(),
 378        );
 379        let request = self.stream_completion(request, cx);
 380        let future = self.request_limiter.stream_with_bypass(
 381            async move {
 382                let response = request.await.map_err(LanguageModelCompletionError::from)?;
 383                Ok(GoogleEventMapper::new().map_stream(response))
 384            },
 385            bypass_rate_limit,
 386        );
 387        async move { Ok(future.await?.boxed()) }.boxed()
 388    }
 389}
 390
 391pub fn into_google(
 392    mut request: LanguageModelRequest,
 393    model_id: String,
 394    mode: GoogleModelMode,
 395) -> google_ai::GenerateContentRequest {
 396    fn map_content(content: Vec<MessageContent>) -> Vec<Part> {
 397        content
 398            .into_iter()
 399            .flat_map(|content| match content {
 400                language_model::MessageContent::Text(text) => {
 401                    if !text.is_empty() {
 402                        vec![Part::TextPart(google_ai::TextPart { text })]
 403                    } else {
 404                        vec![]
 405                    }
 406                }
 407                language_model::MessageContent::Thinking {
 408                    text: _,
 409                    signature: Some(signature),
 410                } => {
 411                    if !signature.is_empty() {
 412                        vec![Part::ThoughtPart(google_ai::ThoughtPart {
 413                            thought: true,
 414                            thought_signature: signature,
 415                        })]
 416                    } else {
 417                        vec![]
 418                    }
 419                }
 420                language_model::MessageContent::Thinking { .. } => {
 421                    vec![]
 422                }
 423                language_model::MessageContent::RedactedThinking(_) => vec![],
 424                language_model::MessageContent::Image(image) => {
 425                    vec![Part::InlineDataPart(google_ai::InlineDataPart {
 426                        inline_data: google_ai::GenerativeContentBlob {
 427                            mime_type: "image/png".to_string(),
 428                            data: image.source.to_string(),
 429                        },
 430                    })]
 431                }
 432                language_model::MessageContent::ToolUse(tool_use) => {
 433                    // Normalize empty string signatures to None
 434                    let thought_signature = tool_use.thought_signature.filter(|s| !s.is_empty());
 435
 436                    vec![Part::FunctionCallPart(google_ai::FunctionCallPart {
 437                        function_call: google_ai::FunctionCall {
 438                            name: tool_use.name.to_string(),
 439                            args: tool_use.input,
 440                        },
 441                        thought_signature,
 442                    })]
 443                }
 444                language_model::MessageContent::ToolResult(tool_result) => {
 445                    match tool_result.content {
 446                        language_model::LanguageModelToolResultContent::Text(text) => {
 447                            vec![Part::FunctionResponsePart(
 448                                google_ai::FunctionResponsePart {
 449                                    function_response: google_ai::FunctionResponse {
 450                                        name: tool_result.tool_name.to_string(),
 451                                        // The API expects a valid JSON object
 452                                        response: serde_json::json!({
 453                                            "output": text
 454                                        }),
 455                                    },
 456                                },
 457                            )]
 458                        }
 459                        language_model::LanguageModelToolResultContent::Image(image) => {
 460                            vec![
 461                                Part::FunctionResponsePart(google_ai::FunctionResponsePart {
 462                                    function_response: google_ai::FunctionResponse {
 463                                        name: tool_result.tool_name.to_string(),
 464                                        // The API expects a valid JSON object
 465                                        response: serde_json::json!({
 466                                            "output": "Tool responded with an image"
 467                                        }),
 468                                    },
 469                                }),
 470                                Part::InlineDataPart(google_ai::InlineDataPart {
 471                                    inline_data: google_ai::GenerativeContentBlob {
 472                                        mime_type: "image/png".to_string(),
 473                                        data: image.source.to_string(),
 474                                    },
 475                                }),
 476                            ]
 477                        }
 478                    }
 479                }
 480            })
 481            .collect()
 482    }
 483
 484    let system_instructions = if request
 485        .messages
 486        .first()
 487        .is_some_and(|msg| matches!(msg.role, Role::System))
 488    {
 489        let message = request.messages.remove(0);
 490        Some(SystemInstruction {
 491            parts: map_content(message.content),
 492        })
 493    } else {
 494        None
 495    };
 496
 497    google_ai::GenerateContentRequest {
 498        model: google_ai::ModelName { model_id },
 499        system_instruction: system_instructions,
 500        contents: request
 501            .messages
 502            .into_iter()
 503            .filter_map(|message| {
 504                let parts = map_content(message.content);
 505                if parts.is_empty() {
 506                    None
 507                } else {
 508                    Some(google_ai::Content {
 509                        parts,
 510                        role: match message.role {
 511                            Role::User => google_ai::Role::User,
 512                            Role::Assistant => google_ai::Role::Model,
 513                            Role::System => google_ai::Role::User, // Google AI doesn't have a system role
 514                        },
 515                    })
 516                }
 517            })
 518            .collect(),
 519        generation_config: Some(google_ai::GenerationConfig {
 520            candidate_count: Some(1),
 521            stop_sequences: Some(request.stop),
 522            max_output_tokens: None,
 523            temperature: request.temperature.map(|t| t as f64).or(Some(1.0)),
 524            thinking_config: match (request.thinking_allowed, mode) {
 525                (true, GoogleModelMode::Thinking { budget_tokens }) => {
 526                    budget_tokens.map(|thinking_budget| ThinkingConfig { thinking_budget })
 527                }
 528                _ => None,
 529            },
 530            top_p: None,
 531            top_k: None,
 532        }),
 533        safety_settings: None,
 534        tools: (!request.tools.is_empty()).then(|| {
 535            vec![google_ai::Tool {
 536                function_declarations: request
 537                    .tools
 538                    .into_iter()
 539                    .map(|tool| FunctionDeclaration {
 540                        name: tool.name,
 541                        description: tool.description,
 542                        parameters: tool.input_schema,
 543                    })
 544                    .collect(),
 545            }]
 546        }),
 547        tool_config: request.tool_choice.map(|choice| google_ai::ToolConfig {
 548            function_calling_config: google_ai::FunctionCallingConfig {
 549                mode: match choice {
 550                    LanguageModelToolChoice::Auto => google_ai::FunctionCallingMode::Auto,
 551                    LanguageModelToolChoice::Any => google_ai::FunctionCallingMode::Any,
 552                    LanguageModelToolChoice::None => google_ai::FunctionCallingMode::None,
 553                },
 554                allowed_function_names: None,
 555            },
 556        }),
 557    }
 558}
 559
 560pub struct GoogleEventMapper {
 561    usage: UsageMetadata,
 562    stop_reason: StopReason,
 563}
 564
 565impl GoogleEventMapper {
 566    pub fn new() -> Self {
 567        Self {
 568            usage: UsageMetadata::default(),
 569            stop_reason: StopReason::EndTurn,
 570        }
 571    }
 572
 573    pub fn map_stream(
 574        mut self,
 575        events: Pin<Box<dyn Send + Stream<Item = Result<GenerateContentResponse>>>>,
 576    ) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>
 577    {
 578        events
 579            .map(Some)
 580            .chain(futures::stream::once(async { None }))
 581            .flat_map(move |event| {
 582                futures::stream::iter(match event {
 583                    Some(Ok(event)) => self.map_event(event),
 584                    Some(Err(error)) => {
 585                        vec![Err(LanguageModelCompletionError::from(error))]
 586                    }
 587                    None => vec![Ok(LanguageModelCompletionEvent::Stop(self.stop_reason))],
 588                })
 589            })
 590    }
 591
 592    pub fn map_event(
 593        &mut self,
 594        event: GenerateContentResponse,
 595    ) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
 596        static TOOL_CALL_COUNTER: AtomicU64 = AtomicU64::new(0);
 597
 598        let mut events: Vec<_> = Vec::new();
 599        let mut wants_to_use_tool = false;
 600        if let Some(usage_metadata) = event.usage_metadata {
 601            update_usage(&mut self.usage, &usage_metadata);
 602            events.push(Ok(LanguageModelCompletionEvent::UsageUpdate(
 603                convert_usage(&self.usage),
 604            )))
 605        }
 606
 607        if let Some(prompt_feedback) = event.prompt_feedback
 608            && let Some(block_reason) = prompt_feedback.block_reason.as_deref()
 609        {
 610            self.stop_reason = match block_reason {
 611                "SAFETY" | "OTHER" | "BLOCKLIST" | "PROHIBITED_CONTENT" | "IMAGE_SAFETY" => {
 612                    StopReason::Refusal
 613                }
 614                _ => {
 615                    log::error!("Unexpected Google block_reason: {block_reason}");
 616                    StopReason::Refusal
 617                }
 618            };
 619            events.push(Ok(LanguageModelCompletionEvent::Stop(self.stop_reason)));
 620
 621            return events;
 622        }
 623
 624        if let Some(candidates) = event.candidates {
 625            for candidate in candidates {
 626                if let Some(finish_reason) = candidate.finish_reason.as_deref() {
 627                    self.stop_reason = match finish_reason {
 628                        "STOP" => StopReason::EndTurn,
 629                        "MAX_TOKENS" => StopReason::MaxTokens,
 630                        _ => {
 631                            log::error!("Unexpected google finish_reason: {finish_reason}");
 632                            StopReason::EndTurn
 633                        }
 634                    };
 635                }
 636                candidate
 637                    .content
 638                    .parts
 639                    .into_iter()
 640                    .for_each(|part| match part {
 641                        Part::TextPart(text_part) => {
 642                            events.push(Ok(LanguageModelCompletionEvent::Text(text_part.text)))
 643                        }
 644                        Part::InlineDataPart(_) => {}
 645                        Part::FunctionCallPart(function_call_part) => {
 646                            wants_to_use_tool = true;
 647                            let name: Arc<str> = function_call_part.function_call.name.into();
 648                            let next_tool_id =
 649                                TOOL_CALL_COUNTER.fetch_add(1, atomic::Ordering::SeqCst);
 650                            let id: LanguageModelToolUseId =
 651                                format!("{}-{}", name, next_tool_id).into();
 652
 653                            // Normalize empty string signatures to None
 654                            let thought_signature = function_call_part
 655                                .thought_signature
 656                                .filter(|s| !s.is_empty());
 657
 658                            events.push(Ok(LanguageModelCompletionEvent::ToolUse(
 659                                LanguageModelToolUse {
 660                                    id,
 661                                    name,
 662                                    is_input_complete: true,
 663                                    raw_input: function_call_part.function_call.args.to_string(),
 664                                    input: function_call_part.function_call.args,
 665                                    thought_signature,
 666                                },
 667                            )));
 668                        }
 669                        Part::FunctionResponsePart(_) => {}
 670                        Part::ThoughtPart(part) => {
 671                            events.push(Ok(LanguageModelCompletionEvent::Thinking {
 672                                text: "(Encrypted thought)".to_string(), // TODO: Can we populate this from thought summaries?
 673                                signature: Some(part.thought_signature),
 674                            }));
 675                        }
 676                    });
 677            }
 678        }
 679
 680        // Even when Gemini wants to use a Tool, the API
 681        // responds with `finish_reason: STOP`
 682        if wants_to_use_tool {
 683            self.stop_reason = StopReason::ToolUse;
 684            events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::ToolUse)));
 685        }
 686        events
 687    }
 688}
 689
 690pub fn count_google_tokens(
 691    request: LanguageModelRequest,
 692    cx: &App,
 693) -> BoxFuture<'static, Result<u64>> {
 694    // We couldn't use the GoogleLanguageModelProvider to count tokens because the github copilot doesn't have the access to google_ai directly.
 695    // So we have to use tokenizer from tiktoken_rs to count tokens.
 696    cx.background_spawn(async move {
 697        let messages = request
 698            .messages
 699            .into_iter()
 700            .map(|message| tiktoken_rs::ChatCompletionRequestMessage {
 701                role: match message.role {
 702                    Role::User => "user".into(),
 703                    Role::Assistant => "assistant".into(),
 704                    Role::System => "system".into(),
 705                },
 706                content: Some(message.string_contents()),
 707                name: None,
 708                function_call: None,
 709            })
 710            .collect::<Vec<_>>();
 711
 712        // Tiktoken doesn't yet support these models, so we manually use the
 713        // same tokenizer as GPT-4.
 714        tiktoken_rs::num_tokens_from_messages("gpt-4", &messages).map(|tokens| tokens as u64)
 715    })
 716    .boxed()
 717}
 718
 719fn update_usage(usage: &mut UsageMetadata, new: &UsageMetadata) {
 720    if let Some(prompt_token_count) = new.prompt_token_count {
 721        usage.prompt_token_count = Some(prompt_token_count);
 722    }
 723    if let Some(cached_content_token_count) = new.cached_content_token_count {
 724        usage.cached_content_token_count = Some(cached_content_token_count);
 725    }
 726    if let Some(candidates_token_count) = new.candidates_token_count {
 727        usage.candidates_token_count = Some(candidates_token_count);
 728    }
 729    if let Some(tool_use_prompt_token_count) = new.tool_use_prompt_token_count {
 730        usage.tool_use_prompt_token_count = Some(tool_use_prompt_token_count);
 731    }
 732    if let Some(thoughts_token_count) = new.thoughts_token_count {
 733        usage.thoughts_token_count = Some(thoughts_token_count);
 734    }
 735    if let Some(total_token_count) = new.total_token_count {
 736        usage.total_token_count = Some(total_token_count);
 737    }
 738}
 739
 740fn convert_usage(usage: &UsageMetadata) -> language_model::TokenUsage {
 741    let prompt_tokens = usage.prompt_token_count.unwrap_or(0);
 742    let cached_tokens = usage.cached_content_token_count.unwrap_or(0);
 743    let input_tokens = prompt_tokens - cached_tokens;
 744    let output_tokens = usage.candidates_token_count.unwrap_or(0);
 745
 746    language_model::TokenUsage {
 747        input_tokens,
 748        output_tokens,
 749        cache_read_input_tokens: cached_tokens,
 750        cache_creation_input_tokens: 0,
 751    }
 752}
 753
 754struct ConfigurationView {
 755    api_key_editor: Entity<InputField>,
 756    state: Entity<State>,
 757    target_agent: language_model::ConfigurationViewTargetAgent,
 758    load_credentials_task: Option<Task<()>>,
 759}
 760
 761impl ConfigurationView {
 762    fn new(
 763        state: Entity<State>,
 764        target_agent: language_model::ConfigurationViewTargetAgent,
 765        window: &mut Window,
 766        cx: &mut Context<Self>,
 767    ) -> Self {
 768        cx.observe(&state, |_, _, cx| {
 769            cx.notify();
 770        })
 771        .detach();
 772
 773        let load_credentials_task = Some(cx.spawn_in(window, {
 774            let state = state.clone();
 775            async move |this, cx| {
 776                if let Some(task) = Some(state.update(cx, |state, cx| state.authenticate(cx))) {
 777                    // We don't log an error, because "not signed in" is also an error.
 778                    let _ = task.await;
 779                }
 780                this.update(cx, |this, cx| {
 781                    this.load_credentials_task = None;
 782                    cx.notify();
 783                })
 784                .log_err();
 785            }
 786        }));
 787
 788        Self {
 789            api_key_editor: cx.new(|cx| InputField::new(window, cx, "AIzaSy...")),
 790            target_agent,
 791            state,
 792            load_credentials_task,
 793        }
 794    }
 795
 796    fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
 797        let api_key = self.api_key_editor.read(cx).text(cx).trim().to_string();
 798        if api_key.is_empty() {
 799            return;
 800        }
 801
 802        // url changes can cause the editor to be displayed again
 803        self.api_key_editor
 804            .update(cx, |editor, cx| editor.set_text("", window, cx));
 805
 806        let state = self.state.clone();
 807        cx.spawn_in(window, async move |_, cx| {
 808            state
 809                .update(cx, |state, cx| state.set_api_key(Some(api_key), cx))
 810                .await
 811        })
 812        .detach_and_log_err(cx);
 813    }
 814
 815    fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 816        self.api_key_editor
 817            .update(cx, |editor, cx| editor.set_text("", window, cx));
 818
 819        let state = self.state.clone();
 820        cx.spawn_in(window, async move |_, cx| {
 821            state
 822                .update(cx, |state, cx| state.set_api_key(None, cx))
 823                .await
 824        })
 825        .detach_and_log_err(cx);
 826    }
 827
 828    fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
 829        !self.state.read(cx).is_authenticated()
 830    }
 831}
 832
 833impl Render for ConfigurationView {
 834    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 835        let env_var_set = self.state.read(cx).api_key_state.is_from_env_var();
 836        let configured_card_label = if env_var_set {
 837            format!(
 838                "API key set in {} environment variable",
 839                API_KEY_ENV_VAR.name
 840            )
 841        } else {
 842            let api_url = GoogleLanguageModelProvider::api_url(cx);
 843            if api_url == google_ai::API_URL {
 844                "API key configured".to_string()
 845            } else {
 846                format!("API key configured for {}", api_url)
 847            }
 848        };
 849
 850        if self.load_credentials_task.is_some() {
 851            div()
 852                .child(Label::new("Loading credentials..."))
 853                .into_any_element()
 854        } else if self.should_render_editor(cx) {
 855            v_flex()
 856                .size_full()
 857                .on_action(cx.listener(Self::save_api_key))
 858                .child(Label::new(format!("To use {}, you need to add an API key. Follow these steps:", match &self.target_agent {
 859                    ConfigurationViewTargetAgent::ZedAgent => "Zed's agent with Google AI".into(),
 860                    ConfigurationViewTargetAgent::Other(agent) => agent.clone(),
 861                })))
 862                .child(
 863                    List::new()
 864                        .child(
 865                            ListBulletItem::new("")
 866                                .child(Label::new("Create one by visiting"))
 867                                .child(ButtonLink::new("Google AI's console", "https://aistudio.google.com/app/apikey"))
 868                        )
 869                        .child(
 870                            ListBulletItem::new("Paste your API key below and hit enter to start using the agent")
 871                        )
 872                )
 873                .child(self.api_key_editor.clone())
 874                .child(
 875                    Label::new(
 876                        format!("You can also set the {GEMINI_API_KEY_VAR_NAME} environment variable and restart Zed."),
 877                    )
 878                    .size(LabelSize::Small).color(Color::Muted),
 879                )
 880                .into_any_element()
 881        } else {
 882            ConfiguredApiCard::new(configured_card_label)
 883                .disabled(env_var_set)
 884                .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx)))
 885                .when(env_var_set, |this| {
 886                    this.tooltip_label(format!("To reset your API key, make sure {GEMINI_API_KEY_VAR_NAME} and {GOOGLE_AI_API_KEY_VAR_NAME} environment variables are unset."))
 887                })
 888                .into_any_element()
 889        }
 890    }
 891}
 892
 893#[cfg(test)]
 894mod tests {
 895    use super::*;
 896    use google_ai::{
 897        Content, FunctionCall, FunctionCallPart, GenerateContentCandidate, GenerateContentResponse,
 898        Part, Role as GoogleRole, TextPart,
 899    };
 900    use language_model::{LanguageModelToolUseId, MessageContent, Role};
 901    use serde_json::json;
 902
 903    #[test]
 904    fn test_function_call_with_signature_creates_tool_use_with_signature() {
 905        let mut mapper = GoogleEventMapper::new();
 906
 907        let response = GenerateContentResponse {
 908            candidates: Some(vec![GenerateContentCandidate {
 909                index: Some(0),
 910                content: Content {
 911                    parts: vec![Part::FunctionCallPart(FunctionCallPart {
 912                        function_call: FunctionCall {
 913                            name: "test_function".to_string(),
 914                            args: json!({"arg": "value"}),
 915                        },
 916                        thought_signature: Some("test_signature_123".to_string()),
 917                    })],
 918                    role: GoogleRole::Model,
 919                },
 920                finish_reason: None,
 921                finish_message: None,
 922                safety_ratings: None,
 923                citation_metadata: None,
 924            }]),
 925            prompt_feedback: None,
 926            usage_metadata: None,
 927        };
 928
 929        let events = mapper.map_event(response);
 930
 931        assert_eq!(events.len(), 2); // ToolUse event + Stop event
 932
 933        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
 934            assert_eq!(tool_use.name.as_ref(), "test_function");
 935            assert_eq!(
 936                tool_use.thought_signature.as_deref(),
 937                Some("test_signature_123")
 938            );
 939        } else {
 940            panic!("Expected ToolUse event");
 941        }
 942    }
 943
 944    #[test]
 945    fn test_function_call_without_signature_has_none() {
 946        let mut mapper = GoogleEventMapper::new();
 947
 948        let response = GenerateContentResponse {
 949            candidates: Some(vec![GenerateContentCandidate {
 950                index: Some(0),
 951                content: Content {
 952                    parts: vec![Part::FunctionCallPart(FunctionCallPart {
 953                        function_call: FunctionCall {
 954                            name: "test_function".to_string(),
 955                            args: json!({"arg": "value"}),
 956                        },
 957                        thought_signature: None,
 958                    })],
 959                    role: GoogleRole::Model,
 960                },
 961                finish_reason: None,
 962                finish_message: None,
 963                safety_ratings: None,
 964                citation_metadata: None,
 965            }]),
 966            prompt_feedback: None,
 967            usage_metadata: None,
 968        };
 969
 970        let events = mapper.map_event(response);
 971
 972        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
 973            assert_eq!(tool_use.thought_signature, None);
 974        } else {
 975            panic!("Expected ToolUse event");
 976        }
 977    }
 978
 979    #[test]
 980    fn test_empty_string_signature_normalized_to_none() {
 981        let mut mapper = GoogleEventMapper::new();
 982
 983        let response = GenerateContentResponse {
 984            candidates: Some(vec![GenerateContentCandidate {
 985                index: Some(0),
 986                content: Content {
 987                    parts: vec![Part::FunctionCallPart(FunctionCallPart {
 988                        function_call: FunctionCall {
 989                            name: "test_function".to_string(),
 990                            args: json!({"arg": "value"}),
 991                        },
 992                        thought_signature: Some("".to_string()),
 993                    })],
 994                    role: GoogleRole::Model,
 995                },
 996                finish_reason: None,
 997                finish_message: None,
 998                safety_ratings: None,
 999                citation_metadata: None,
1000            }]),
1001            prompt_feedback: None,
1002            usage_metadata: None,
1003        };
1004
1005        let events = mapper.map_event(response);
1006
1007        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
1008            assert_eq!(tool_use.thought_signature, None);
1009        } else {
1010            panic!("Expected ToolUse event");
1011        }
1012    }
1013
1014    #[test]
1015    fn test_parallel_function_calls_preserve_signatures() {
1016        let mut mapper = GoogleEventMapper::new();
1017
1018        let response = GenerateContentResponse {
1019            candidates: Some(vec![GenerateContentCandidate {
1020                index: Some(0),
1021                content: Content {
1022                    parts: vec![
1023                        Part::FunctionCallPart(FunctionCallPart {
1024                            function_call: FunctionCall {
1025                                name: "function_1".to_string(),
1026                                args: json!({"arg": "value1"}),
1027                            },
1028                            thought_signature: Some("signature_1".to_string()),
1029                        }),
1030                        Part::FunctionCallPart(FunctionCallPart {
1031                            function_call: FunctionCall {
1032                                name: "function_2".to_string(),
1033                                args: json!({"arg": "value2"}),
1034                            },
1035                            thought_signature: None,
1036                        }),
1037                    ],
1038                    role: GoogleRole::Model,
1039                },
1040                finish_reason: None,
1041                finish_message: None,
1042                safety_ratings: None,
1043                citation_metadata: None,
1044            }]),
1045            prompt_feedback: None,
1046            usage_metadata: None,
1047        };
1048
1049        let events = mapper.map_event(response);
1050
1051        assert_eq!(events.len(), 3); // 2 ToolUse events + Stop event
1052
1053        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
1054            assert_eq!(tool_use.name.as_ref(), "function_1");
1055            assert_eq!(tool_use.thought_signature.as_deref(), Some("signature_1"));
1056        } else {
1057            panic!("Expected ToolUse event for function_1");
1058        }
1059
1060        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[1] {
1061            assert_eq!(tool_use.name.as_ref(), "function_2");
1062            assert_eq!(tool_use.thought_signature, None);
1063        } else {
1064            panic!("Expected ToolUse event for function_2");
1065        }
1066    }
1067
1068    #[test]
1069    fn test_tool_use_with_signature_converts_to_function_call_part() {
1070        let tool_use = language_model::LanguageModelToolUse {
1071            id: LanguageModelToolUseId::from("test_id"),
1072            name: "test_function".into(),
1073            raw_input: json!({"arg": "value"}).to_string(),
1074            input: json!({"arg": "value"}),
1075            is_input_complete: true,
1076            thought_signature: Some("test_signature_456".to_string()),
1077        };
1078
1079        let request = super::into_google(
1080            LanguageModelRequest {
1081                messages: vec![language_model::LanguageModelRequestMessage {
1082                    role: Role::Assistant,
1083                    content: vec![MessageContent::ToolUse(tool_use)],
1084                    cache: false,
1085                    reasoning_details: None,
1086                }],
1087                ..Default::default()
1088            },
1089            "gemini-2.5-flash".to_string(),
1090            GoogleModelMode::Default,
1091        );
1092
1093        assert_eq!(request.contents[0].parts.len(), 1);
1094        if let Part::FunctionCallPart(fc_part) = &request.contents[0].parts[0] {
1095            assert_eq!(fc_part.function_call.name, "test_function");
1096            assert_eq!(
1097                fc_part.thought_signature.as_deref(),
1098                Some("test_signature_456")
1099            );
1100        } else {
1101            panic!("Expected FunctionCallPart");
1102        }
1103    }
1104
1105    #[test]
1106    fn test_tool_use_without_signature_omits_field() {
1107        let tool_use = language_model::LanguageModelToolUse {
1108            id: LanguageModelToolUseId::from("test_id"),
1109            name: "test_function".into(),
1110            raw_input: json!({"arg": "value"}).to_string(),
1111            input: json!({"arg": "value"}),
1112            is_input_complete: true,
1113            thought_signature: None,
1114        };
1115
1116        let request = super::into_google(
1117            LanguageModelRequest {
1118                messages: vec![language_model::LanguageModelRequestMessage {
1119                    role: Role::Assistant,
1120                    content: vec![MessageContent::ToolUse(tool_use)],
1121                    cache: false,
1122                    reasoning_details: None,
1123                }],
1124                ..Default::default()
1125            },
1126            "gemini-2.5-flash".to_string(),
1127            GoogleModelMode::Default,
1128        );
1129
1130        assert_eq!(request.contents[0].parts.len(), 1);
1131        if let Part::FunctionCallPart(fc_part) = &request.contents[0].parts[0] {
1132            assert_eq!(fc_part.thought_signature, None);
1133        } else {
1134            panic!("Expected FunctionCallPart");
1135        }
1136    }
1137
1138    #[test]
1139    fn test_empty_signature_in_tool_use_normalized_to_none() {
1140        let tool_use = language_model::LanguageModelToolUse {
1141            id: LanguageModelToolUseId::from("test_id"),
1142            name: "test_function".into(),
1143            raw_input: json!({"arg": "value"}).to_string(),
1144            input: json!({"arg": "value"}),
1145            is_input_complete: true,
1146            thought_signature: Some("".to_string()),
1147        };
1148
1149        let request = super::into_google(
1150            LanguageModelRequest {
1151                messages: vec![language_model::LanguageModelRequestMessage {
1152                    role: Role::Assistant,
1153                    content: vec![MessageContent::ToolUse(tool_use)],
1154                    cache: false,
1155                    reasoning_details: None,
1156                }],
1157                ..Default::default()
1158            },
1159            "gemini-2.5-flash".to_string(),
1160            GoogleModelMode::Default,
1161        );
1162
1163        if let Part::FunctionCallPart(fc_part) = &request.contents[0].parts[0] {
1164            assert_eq!(fc_part.thought_signature, None);
1165        } else {
1166            panic!("Expected FunctionCallPart");
1167        }
1168    }
1169
1170    #[test]
1171    fn test_round_trip_preserves_signature() {
1172        let mut mapper = GoogleEventMapper::new();
1173
1174        // Simulate receiving a response from Google with a signature
1175        let response = GenerateContentResponse {
1176            candidates: Some(vec![GenerateContentCandidate {
1177                index: Some(0),
1178                content: Content {
1179                    parts: vec![Part::FunctionCallPart(FunctionCallPart {
1180                        function_call: FunctionCall {
1181                            name: "test_function".to_string(),
1182                            args: json!({"arg": "value"}),
1183                        },
1184                        thought_signature: Some("round_trip_sig".to_string()),
1185                    })],
1186                    role: GoogleRole::Model,
1187                },
1188                finish_reason: None,
1189                finish_message: None,
1190                safety_ratings: None,
1191                citation_metadata: None,
1192            }]),
1193            prompt_feedback: None,
1194            usage_metadata: None,
1195        };
1196
1197        let events = mapper.map_event(response);
1198
1199        let tool_use = if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
1200            tool_use.clone()
1201        } else {
1202            panic!("Expected ToolUse event");
1203        };
1204
1205        // Convert back to Google format
1206        let request = super::into_google(
1207            LanguageModelRequest {
1208                messages: vec![language_model::LanguageModelRequestMessage {
1209                    role: Role::Assistant,
1210                    content: vec![MessageContent::ToolUse(tool_use)],
1211                    cache: false,
1212                    reasoning_details: None,
1213                }],
1214                ..Default::default()
1215            },
1216            "gemini-2.5-flash".to_string(),
1217            GoogleModelMode::Default,
1218        );
1219
1220        // Verify signature is preserved
1221        if let Part::FunctionCallPart(fc_part) = &request.contents[0].parts[0] {
1222            assert_eq!(fc_part.thought_signature.as_deref(), Some("round_trip_sig"));
1223        } else {
1224            panic!("Expected FunctionCallPart");
1225        }
1226    }
1227
1228    #[test]
1229    fn test_mixed_text_and_function_call_with_signature() {
1230        let mut mapper = GoogleEventMapper::new();
1231
1232        let response = GenerateContentResponse {
1233            candidates: Some(vec![GenerateContentCandidate {
1234                index: Some(0),
1235                content: Content {
1236                    parts: vec![
1237                        Part::TextPart(TextPart {
1238                            text: "I'll help with that.".to_string(),
1239                        }),
1240                        Part::FunctionCallPart(FunctionCallPart {
1241                            function_call: FunctionCall {
1242                                name: "helper_function".to_string(),
1243                                args: json!({"query": "help"}),
1244                            },
1245                            thought_signature: Some("mixed_sig".to_string()),
1246                        }),
1247                    ],
1248                    role: GoogleRole::Model,
1249                },
1250                finish_reason: None,
1251                finish_message: None,
1252                safety_ratings: None,
1253                citation_metadata: None,
1254            }]),
1255            prompt_feedback: None,
1256            usage_metadata: None,
1257        };
1258
1259        let events = mapper.map_event(response);
1260
1261        assert_eq!(events.len(), 3); // Text event + ToolUse event + Stop event
1262
1263        if let Ok(LanguageModelCompletionEvent::Text(text)) = &events[0] {
1264            assert_eq!(text, "I'll help with that.");
1265        } else {
1266            panic!("Expected Text event");
1267        }
1268
1269        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[1] {
1270            assert_eq!(tool_use.name.as_ref(), "helper_function");
1271            assert_eq!(tool_use.thought_signature.as_deref(), Some("mixed_sig"));
1272        } else {
1273            panic!("Expected ToolUse event");
1274        }
1275    }
1276
1277    #[test]
1278    fn test_special_characters_in_signature_preserved() {
1279        let mut mapper = GoogleEventMapper::new();
1280
1281        let signature_with_special_chars = "sig<>\"'&%$#@!{}[]".to_string();
1282
1283        let response = GenerateContentResponse {
1284            candidates: Some(vec![GenerateContentCandidate {
1285                index: Some(0),
1286                content: Content {
1287                    parts: vec![Part::FunctionCallPart(FunctionCallPart {
1288                        function_call: FunctionCall {
1289                            name: "test_function".to_string(),
1290                            args: json!({"arg": "value"}),
1291                        },
1292                        thought_signature: Some(signature_with_special_chars.clone()),
1293                    })],
1294                    role: GoogleRole::Model,
1295                },
1296                finish_reason: None,
1297                finish_message: None,
1298                safety_ratings: None,
1299                citation_metadata: None,
1300            }]),
1301            prompt_feedback: None,
1302            usage_metadata: None,
1303        };
1304
1305        let events = mapper.map_event(response);
1306
1307        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
1308            assert_eq!(
1309                tool_use.thought_signature.as_deref(),
1310                Some(signature_with_special_chars.as_str())
1311            );
1312        } else {
1313            panic!("Expected ToolUse event");
1314        }
1315    }
1316}