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 request = into_google(
 374            request,
 375            self.model.request_id().to_string(),
 376            self.model.mode(),
 377        );
 378        let request = self.stream_completion(request, cx);
 379        let future = self.request_limiter.stream(async move {
 380            let response = request.await.map_err(LanguageModelCompletionError::from)?;
 381            Ok(GoogleEventMapper::new().map_stream(response))
 382        });
 383        async move { Ok(future.await?.boxed()) }.boxed()
 384    }
 385}
 386
 387pub fn into_google(
 388    mut request: LanguageModelRequest,
 389    model_id: String,
 390    mode: GoogleModelMode,
 391) -> google_ai::GenerateContentRequest {
 392    fn map_content(content: Vec<MessageContent>) -> Vec<Part> {
 393        content
 394            .into_iter()
 395            .flat_map(|content| match content {
 396                language_model::MessageContent::Text(text) => {
 397                    if !text.is_empty() {
 398                        vec![Part::TextPart(google_ai::TextPart { text })]
 399                    } else {
 400                        vec![]
 401                    }
 402                }
 403                language_model::MessageContent::Thinking {
 404                    text: _,
 405                    signature: Some(signature),
 406                } => {
 407                    if !signature.is_empty() {
 408                        vec![Part::ThoughtPart(google_ai::ThoughtPart {
 409                            thought: true,
 410                            thought_signature: signature,
 411                        })]
 412                    } else {
 413                        vec![]
 414                    }
 415                }
 416                language_model::MessageContent::Thinking { .. } => {
 417                    vec![]
 418                }
 419                language_model::MessageContent::RedactedThinking(_) => vec![],
 420                language_model::MessageContent::Image(image) => {
 421                    vec![Part::InlineDataPart(google_ai::InlineDataPart {
 422                        inline_data: google_ai::GenerativeContentBlob {
 423                            mime_type: "image/png".to_string(),
 424                            data: image.source.to_string(),
 425                        },
 426                    })]
 427                }
 428                language_model::MessageContent::ToolUse(tool_use) => {
 429                    // Normalize empty string signatures to None
 430                    let thought_signature = tool_use.thought_signature.filter(|s| !s.is_empty());
 431
 432                    vec![Part::FunctionCallPart(google_ai::FunctionCallPart {
 433                        function_call: google_ai::FunctionCall {
 434                            name: tool_use.name.to_string(),
 435                            args: tool_use.input,
 436                        },
 437                        thought_signature,
 438                    })]
 439                }
 440                language_model::MessageContent::ToolResult(tool_result) => {
 441                    match tool_result.content {
 442                        language_model::LanguageModelToolResultContent::Text(text) => {
 443                            vec![Part::FunctionResponsePart(
 444                                google_ai::FunctionResponsePart {
 445                                    function_response: google_ai::FunctionResponse {
 446                                        name: tool_result.tool_name.to_string(),
 447                                        // The API expects a valid JSON object
 448                                        response: serde_json::json!({
 449                                            "output": text
 450                                        }),
 451                                    },
 452                                },
 453                            )]
 454                        }
 455                        language_model::LanguageModelToolResultContent::Image(image) => {
 456                            vec![
 457                                Part::FunctionResponsePart(google_ai::FunctionResponsePart {
 458                                    function_response: google_ai::FunctionResponse {
 459                                        name: tool_result.tool_name.to_string(),
 460                                        // The API expects a valid JSON object
 461                                        response: serde_json::json!({
 462                                            "output": "Tool responded with an image"
 463                                        }),
 464                                    },
 465                                }),
 466                                Part::InlineDataPart(google_ai::InlineDataPart {
 467                                    inline_data: google_ai::GenerativeContentBlob {
 468                                        mime_type: "image/png".to_string(),
 469                                        data: image.source.to_string(),
 470                                    },
 471                                }),
 472                            ]
 473                        }
 474                    }
 475                }
 476            })
 477            .collect()
 478    }
 479
 480    let system_instructions = if request
 481        .messages
 482        .first()
 483        .is_some_and(|msg| matches!(msg.role, Role::System))
 484    {
 485        let message = request.messages.remove(0);
 486        Some(SystemInstruction {
 487            parts: map_content(message.content),
 488        })
 489    } else {
 490        None
 491    };
 492
 493    google_ai::GenerateContentRequest {
 494        model: google_ai::ModelName { model_id },
 495        system_instruction: system_instructions,
 496        contents: request
 497            .messages
 498            .into_iter()
 499            .filter_map(|message| {
 500                let parts = map_content(message.content);
 501                if parts.is_empty() {
 502                    None
 503                } else {
 504                    Some(google_ai::Content {
 505                        parts,
 506                        role: match message.role {
 507                            Role::User => google_ai::Role::User,
 508                            Role::Assistant => google_ai::Role::Model,
 509                            Role::System => google_ai::Role::User, // Google AI doesn't have a system role
 510                        },
 511                    })
 512                }
 513            })
 514            .collect(),
 515        generation_config: Some(google_ai::GenerationConfig {
 516            candidate_count: Some(1),
 517            stop_sequences: Some(request.stop),
 518            max_output_tokens: None,
 519            temperature: request.temperature.map(|t| t as f64).or(Some(1.0)),
 520            thinking_config: match (request.thinking_allowed, mode) {
 521                (true, GoogleModelMode::Thinking { budget_tokens }) => {
 522                    budget_tokens.map(|thinking_budget| ThinkingConfig { thinking_budget })
 523                }
 524                _ => None,
 525            },
 526            top_p: None,
 527            top_k: None,
 528        }),
 529        safety_settings: None,
 530        tools: (!request.tools.is_empty()).then(|| {
 531            vec![google_ai::Tool {
 532                function_declarations: request
 533                    .tools
 534                    .into_iter()
 535                    .map(|tool| FunctionDeclaration {
 536                        name: tool.name,
 537                        description: tool.description,
 538                        parameters: tool.input_schema,
 539                    })
 540                    .collect(),
 541            }]
 542        }),
 543        tool_config: request.tool_choice.map(|choice| google_ai::ToolConfig {
 544            function_calling_config: google_ai::FunctionCallingConfig {
 545                mode: match choice {
 546                    LanguageModelToolChoice::Auto => google_ai::FunctionCallingMode::Auto,
 547                    LanguageModelToolChoice::Any => google_ai::FunctionCallingMode::Any,
 548                    LanguageModelToolChoice::None => google_ai::FunctionCallingMode::None,
 549                },
 550                allowed_function_names: None,
 551            },
 552        }),
 553    }
 554}
 555
 556pub struct GoogleEventMapper {
 557    usage: UsageMetadata,
 558    stop_reason: StopReason,
 559}
 560
 561impl GoogleEventMapper {
 562    pub fn new() -> Self {
 563        Self {
 564            usage: UsageMetadata::default(),
 565            stop_reason: StopReason::EndTurn,
 566        }
 567    }
 568
 569    pub fn map_stream(
 570        mut self,
 571        events: Pin<Box<dyn Send + Stream<Item = Result<GenerateContentResponse>>>>,
 572    ) -> impl Stream<Item = Result<LanguageModelCompletionEvent, LanguageModelCompletionError>>
 573    {
 574        events
 575            .map(Some)
 576            .chain(futures::stream::once(async { None }))
 577            .flat_map(move |event| {
 578                futures::stream::iter(match event {
 579                    Some(Ok(event)) => self.map_event(event),
 580                    Some(Err(error)) => {
 581                        vec![Err(LanguageModelCompletionError::from(error))]
 582                    }
 583                    None => vec![Ok(LanguageModelCompletionEvent::Stop(self.stop_reason))],
 584                })
 585            })
 586    }
 587
 588    pub fn map_event(
 589        &mut self,
 590        event: GenerateContentResponse,
 591    ) -> Vec<Result<LanguageModelCompletionEvent, LanguageModelCompletionError>> {
 592        static TOOL_CALL_COUNTER: AtomicU64 = AtomicU64::new(0);
 593
 594        let mut events: Vec<_> = Vec::new();
 595        let mut wants_to_use_tool = false;
 596        if let Some(usage_metadata) = event.usage_metadata {
 597            update_usage(&mut self.usage, &usage_metadata);
 598            events.push(Ok(LanguageModelCompletionEvent::UsageUpdate(
 599                convert_usage(&self.usage),
 600            )))
 601        }
 602
 603        if let Some(prompt_feedback) = event.prompt_feedback
 604            && let Some(block_reason) = prompt_feedback.block_reason.as_deref()
 605        {
 606            self.stop_reason = match block_reason {
 607                "SAFETY" | "OTHER" | "BLOCKLIST" | "PROHIBITED_CONTENT" | "IMAGE_SAFETY" => {
 608                    StopReason::Refusal
 609                }
 610                _ => {
 611                    log::error!("Unexpected Google block_reason: {block_reason}");
 612                    StopReason::Refusal
 613                }
 614            };
 615            events.push(Ok(LanguageModelCompletionEvent::Stop(self.stop_reason)));
 616
 617            return events;
 618        }
 619
 620        if let Some(candidates) = event.candidates {
 621            for candidate in candidates {
 622                if let Some(finish_reason) = candidate.finish_reason.as_deref() {
 623                    self.stop_reason = match finish_reason {
 624                        "STOP" => StopReason::EndTurn,
 625                        "MAX_TOKENS" => StopReason::MaxTokens,
 626                        _ => {
 627                            log::error!("Unexpected google finish_reason: {finish_reason}");
 628                            StopReason::EndTurn
 629                        }
 630                    };
 631                }
 632                candidate
 633                    .content
 634                    .parts
 635                    .into_iter()
 636                    .for_each(|part| match part {
 637                        Part::TextPart(text_part) => {
 638                            events.push(Ok(LanguageModelCompletionEvent::Text(text_part.text)))
 639                        }
 640                        Part::InlineDataPart(_) => {}
 641                        Part::FunctionCallPart(function_call_part) => {
 642                            wants_to_use_tool = true;
 643                            let name: Arc<str> = function_call_part.function_call.name.into();
 644                            let next_tool_id =
 645                                TOOL_CALL_COUNTER.fetch_add(1, atomic::Ordering::SeqCst);
 646                            let id: LanguageModelToolUseId =
 647                                format!("{}-{}", name, next_tool_id).into();
 648
 649                            // Normalize empty string signatures to None
 650                            let thought_signature = function_call_part
 651                                .thought_signature
 652                                .filter(|s| !s.is_empty());
 653
 654                            events.push(Ok(LanguageModelCompletionEvent::ToolUse(
 655                                LanguageModelToolUse {
 656                                    id,
 657                                    name,
 658                                    is_input_complete: true,
 659                                    raw_input: function_call_part.function_call.args.to_string(),
 660                                    input: function_call_part.function_call.args,
 661                                    thought_signature,
 662                                },
 663                            )));
 664                        }
 665                        Part::FunctionResponsePart(_) => {}
 666                        Part::ThoughtPart(part) => {
 667                            events.push(Ok(LanguageModelCompletionEvent::Thinking {
 668                                text: "(Encrypted thought)".to_string(), // TODO: Can we populate this from thought summaries?
 669                                signature: Some(part.thought_signature),
 670                            }));
 671                        }
 672                    });
 673            }
 674        }
 675
 676        // Even when Gemini wants to use a Tool, the API
 677        // responds with `finish_reason: STOP`
 678        if wants_to_use_tool {
 679            self.stop_reason = StopReason::ToolUse;
 680            events.push(Ok(LanguageModelCompletionEvent::Stop(StopReason::ToolUse)));
 681        }
 682        events
 683    }
 684}
 685
 686pub fn count_google_tokens(
 687    request: LanguageModelRequest,
 688    cx: &App,
 689) -> BoxFuture<'static, Result<u64>> {
 690    // We couldn't use the GoogleLanguageModelProvider to count tokens because the github copilot doesn't have the access to google_ai directly.
 691    // So we have to use tokenizer from tiktoken_rs to count tokens.
 692    cx.background_spawn(async move {
 693        let messages = request
 694            .messages
 695            .into_iter()
 696            .map(|message| tiktoken_rs::ChatCompletionRequestMessage {
 697                role: match message.role {
 698                    Role::User => "user".into(),
 699                    Role::Assistant => "assistant".into(),
 700                    Role::System => "system".into(),
 701                },
 702                content: Some(message.string_contents()),
 703                name: None,
 704                function_call: None,
 705            })
 706            .collect::<Vec<_>>();
 707
 708        // Tiktoken doesn't yet support these models, so we manually use the
 709        // same tokenizer as GPT-4.
 710        tiktoken_rs::num_tokens_from_messages("gpt-4", &messages).map(|tokens| tokens as u64)
 711    })
 712    .boxed()
 713}
 714
 715fn update_usage(usage: &mut UsageMetadata, new: &UsageMetadata) {
 716    if let Some(prompt_token_count) = new.prompt_token_count {
 717        usage.prompt_token_count = Some(prompt_token_count);
 718    }
 719    if let Some(cached_content_token_count) = new.cached_content_token_count {
 720        usage.cached_content_token_count = Some(cached_content_token_count);
 721    }
 722    if let Some(candidates_token_count) = new.candidates_token_count {
 723        usage.candidates_token_count = Some(candidates_token_count);
 724    }
 725    if let Some(tool_use_prompt_token_count) = new.tool_use_prompt_token_count {
 726        usage.tool_use_prompt_token_count = Some(tool_use_prompt_token_count);
 727    }
 728    if let Some(thoughts_token_count) = new.thoughts_token_count {
 729        usage.thoughts_token_count = Some(thoughts_token_count);
 730    }
 731    if let Some(total_token_count) = new.total_token_count {
 732        usage.total_token_count = Some(total_token_count);
 733    }
 734}
 735
 736fn convert_usage(usage: &UsageMetadata) -> language_model::TokenUsage {
 737    let prompt_tokens = usage.prompt_token_count.unwrap_or(0);
 738    let cached_tokens = usage.cached_content_token_count.unwrap_or(0);
 739    let input_tokens = prompt_tokens - cached_tokens;
 740    let output_tokens = usage.candidates_token_count.unwrap_or(0);
 741
 742    language_model::TokenUsage {
 743        input_tokens,
 744        output_tokens,
 745        cache_read_input_tokens: cached_tokens,
 746        cache_creation_input_tokens: 0,
 747    }
 748}
 749
 750struct ConfigurationView {
 751    api_key_editor: Entity<InputField>,
 752    state: Entity<State>,
 753    target_agent: language_model::ConfigurationViewTargetAgent,
 754    load_credentials_task: Option<Task<()>>,
 755}
 756
 757impl ConfigurationView {
 758    fn new(
 759        state: Entity<State>,
 760        target_agent: language_model::ConfigurationViewTargetAgent,
 761        window: &mut Window,
 762        cx: &mut Context<Self>,
 763    ) -> Self {
 764        cx.observe(&state, |_, _, cx| {
 765            cx.notify();
 766        })
 767        .detach();
 768
 769        let load_credentials_task = Some(cx.spawn_in(window, {
 770            let state = state.clone();
 771            async move |this, cx| {
 772                if let Some(task) = Some(state.update(cx, |state, cx| state.authenticate(cx))) {
 773                    // We don't log an error, because "not signed in" is also an error.
 774                    let _ = task.await;
 775                }
 776                this.update(cx, |this, cx| {
 777                    this.load_credentials_task = None;
 778                    cx.notify();
 779                })
 780                .log_err();
 781            }
 782        }));
 783
 784        Self {
 785            api_key_editor: cx.new(|cx| InputField::new(window, cx, "AIzaSy...")),
 786            target_agent,
 787            state,
 788            load_credentials_task,
 789        }
 790    }
 791
 792    fn save_api_key(&mut self, _: &menu::Confirm, window: &mut Window, cx: &mut Context<Self>) {
 793        let api_key = self.api_key_editor.read(cx).text(cx).trim().to_string();
 794        if api_key.is_empty() {
 795            return;
 796        }
 797
 798        // url changes can cause the editor to be displayed again
 799        self.api_key_editor
 800            .update(cx, |editor, cx| editor.set_text("", window, cx));
 801
 802        let state = self.state.clone();
 803        cx.spawn_in(window, async move |_, cx| {
 804            state
 805                .update(cx, |state, cx| state.set_api_key(Some(api_key), cx))
 806                .await
 807        })
 808        .detach_and_log_err(cx);
 809    }
 810
 811    fn reset_api_key(&mut self, window: &mut Window, cx: &mut Context<Self>) {
 812        self.api_key_editor
 813            .update(cx, |editor, cx| editor.set_text("", window, cx));
 814
 815        let state = self.state.clone();
 816        cx.spawn_in(window, async move |_, cx| {
 817            state
 818                .update(cx, |state, cx| state.set_api_key(None, cx))
 819                .await
 820        })
 821        .detach_and_log_err(cx);
 822    }
 823
 824    fn should_render_editor(&self, cx: &mut Context<Self>) -> bool {
 825        !self.state.read(cx).is_authenticated()
 826    }
 827}
 828
 829impl Render for ConfigurationView {
 830    fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
 831        let env_var_set = self.state.read(cx).api_key_state.is_from_env_var();
 832        let configured_card_label = if env_var_set {
 833            format!(
 834                "API key set in {} environment variable",
 835                API_KEY_ENV_VAR.name
 836            )
 837        } else {
 838            let api_url = GoogleLanguageModelProvider::api_url(cx);
 839            if api_url == google_ai::API_URL {
 840                "API key configured".to_string()
 841            } else {
 842                format!("API key configured for {}", api_url)
 843            }
 844        };
 845
 846        if self.load_credentials_task.is_some() {
 847            div()
 848                .child(Label::new("Loading credentials..."))
 849                .into_any_element()
 850        } else if self.should_render_editor(cx) {
 851            v_flex()
 852                .size_full()
 853                .on_action(cx.listener(Self::save_api_key))
 854                .child(Label::new(format!("To use {}, you need to add an API key. Follow these steps:", match &self.target_agent {
 855                    ConfigurationViewTargetAgent::ZedAgent => "Zed's agent with Google AI".into(),
 856                    ConfigurationViewTargetAgent::Other(agent) => agent.clone(),
 857                })))
 858                .child(
 859                    List::new()
 860                        .child(
 861                            ListBulletItem::new("")
 862                                .child(Label::new("Create one by visiting"))
 863                                .child(ButtonLink::new("Google AI's console", "https://aistudio.google.com/app/apikey"))
 864                        )
 865                        .child(
 866                            ListBulletItem::new("Paste your API key below and hit enter to start using the agent")
 867                        )
 868                )
 869                .child(self.api_key_editor.clone())
 870                .child(
 871                    Label::new(
 872                        format!("You can also set the {GEMINI_API_KEY_VAR_NAME} environment variable and restart Zed."),
 873                    )
 874                    .size(LabelSize::Small).color(Color::Muted),
 875                )
 876                .into_any_element()
 877        } else {
 878            ConfiguredApiCard::new(configured_card_label)
 879                .disabled(env_var_set)
 880                .on_click(cx.listener(|this, _, window, cx| this.reset_api_key(window, cx)))
 881                .when(env_var_set, |this| {
 882                    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."))
 883                })
 884                .into_any_element()
 885        }
 886    }
 887}
 888
 889#[cfg(test)]
 890mod tests {
 891    use super::*;
 892    use google_ai::{
 893        Content, FunctionCall, FunctionCallPart, GenerateContentCandidate, GenerateContentResponse,
 894        Part, Role as GoogleRole, TextPart,
 895    };
 896    use language_model::{LanguageModelToolUseId, MessageContent, Role};
 897    use serde_json::json;
 898
 899    #[test]
 900    fn test_function_call_with_signature_creates_tool_use_with_signature() {
 901        let mut mapper = GoogleEventMapper::new();
 902
 903        let response = GenerateContentResponse {
 904            candidates: Some(vec![GenerateContentCandidate {
 905                index: Some(0),
 906                content: Content {
 907                    parts: vec![Part::FunctionCallPart(FunctionCallPart {
 908                        function_call: FunctionCall {
 909                            name: "test_function".to_string(),
 910                            args: json!({"arg": "value"}),
 911                        },
 912                        thought_signature: Some("test_signature_123".to_string()),
 913                    })],
 914                    role: GoogleRole::Model,
 915                },
 916                finish_reason: None,
 917                finish_message: None,
 918                safety_ratings: None,
 919                citation_metadata: None,
 920            }]),
 921            prompt_feedback: None,
 922            usage_metadata: None,
 923        };
 924
 925        let events = mapper.map_event(response);
 926
 927        assert_eq!(events.len(), 2); // ToolUse event + Stop event
 928
 929        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
 930            assert_eq!(tool_use.name.as_ref(), "test_function");
 931            assert_eq!(
 932                tool_use.thought_signature.as_deref(),
 933                Some("test_signature_123")
 934            );
 935        } else {
 936            panic!("Expected ToolUse event");
 937        }
 938    }
 939
 940    #[test]
 941    fn test_function_call_without_signature_has_none() {
 942        let mut mapper = GoogleEventMapper::new();
 943
 944        let response = GenerateContentResponse {
 945            candidates: Some(vec![GenerateContentCandidate {
 946                index: Some(0),
 947                content: Content {
 948                    parts: vec![Part::FunctionCallPart(FunctionCallPart {
 949                        function_call: FunctionCall {
 950                            name: "test_function".to_string(),
 951                            args: json!({"arg": "value"}),
 952                        },
 953                        thought_signature: None,
 954                    })],
 955                    role: GoogleRole::Model,
 956                },
 957                finish_reason: None,
 958                finish_message: None,
 959                safety_ratings: None,
 960                citation_metadata: None,
 961            }]),
 962            prompt_feedback: None,
 963            usage_metadata: None,
 964        };
 965
 966        let events = mapper.map_event(response);
 967
 968        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
 969            assert_eq!(tool_use.thought_signature, None);
 970        } else {
 971            panic!("Expected ToolUse event");
 972        }
 973    }
 974
 975    #[test]
 976    fn test_empty_string_signature_normalized_to_none() {
 977        let mut mapper = GoogleEventMapper::new();
 978
 979        let response = GenerateContentResponse {
 980            candidates: Some(vec![GenerateContentCandidate {
 981                index: Some(0),
 982                content: Content {
 983                    parts: vec![Part::FunctionCallPart(FunctionCallPart {
 984                        function_call: FunctionCall {
 985                            name: "test_function".to_string(),
 986                            args: json!({"arg": "value"}),
 987                        },
 988                        thought_signature: Some("".to_string()),
 989                    })],
 990                    role: GoogleRole::Model,
 991                },
 992                finish_reason: None,
 993                finish_message: None,
 994                safety_ratings: None,
 995                citation_metadata: None,
 996            }]),
 997            prompt_feedback: None,
 998            usage_metadata: None,
 999        };
1000
1001        let events = mapper.map_event(response);
1002
1003        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
1004            assert_eq!(tool_use.thought_signature, None);
1005        } else {
1006            panic!("Expected ToolUse event");
1007        }
1008    }
1009
1010    #[test]
1011    fn test_parallel_function_calls_preserve_signatures() {
1012        let mut mapper = GoogleEventMapper::new();
1013
1014        let response = GenerateContentResponse {
1015            candidates: Some(vec![GenerateContentCandidate {
1016                index: Some(0),
1017                content: Content {
1018                    parts: vec![
1019                        Part::FunctionCallPart(FunctionCallPart {
1020                            function_call: FunctionCall {
1021                                name: "function_1".to_string(),
1022                                args: json!({"arg": "value1"}),
1023                            },
1024                            thought_signature: Some("signature_1".to_string()),
1025                        }),
1026                        Part::FunctionCallPart(FunctionCallPart {
1027                            function_call: FunctionCall {
1028                                name: "function_2".to_string(),
1029                                args: json!({"arg": "value2"}),
1030                            },
1031                            thought_signature: None,
1032                        }),
1033                    ],
1034                    role: GoogleRole::Model,
1035                },
1036                finish_reason: None,
1037                finish_message: None,
1038                safety_ratings: None,
1039                citation_metadata: None,
1040            }]),
1041            prompt_feedback: None,
1042            usage_metadata: None,
1043        };
1044
1045        let events = mapper.map_event(response);
1046
1047        assert_eq!(events.len(), 3); // 2 ToolUse events + Stop event
1048
1049        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
1050            assert_eq!(tool_use.name.as_ref(), "function_1");
1051            assert_eq!(tool_use.thought_signature.as_deref(), Some("signature_1"));
1052        } else {
1053            panic!("Expected ToolUse event for function_1");
1054        }
1055
1056        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[1] {
1057            assert_eq!(tool_use.name.as_ref(), "function_2");
1058            assert_eq!(tool_use.thought_signature, None);
1059        } else {
1060            panic!("Expected ToolUse event for function_2");
1061        }
1062    }
1063
1064    #[test]
1065    fn test_tool_use_with_signature_converts_to_function_call_part() {
1066        let tool_use = language_model::LanguageModelToolUse {
1067            id: LanguageModelToolUseId::from("test_id"),
1068            name: "test_function".into(),
1069            raw_input: json!({"arg": "value"}).to_string(),
1070            input: json!({"arg": "value"}),
1071            is_input_complete: true,
1072            thought_signature: Some("test_signature_456".to_string()),
1073        };
1074
1075        let request = super::into_google(
1076            LanguageModelRequest {
1077                messages: vec![language_model::LanguageModelRequestMessage {
1078                    role: Role::Assistant,
1079                    content: vec![MessageContent::ToolUse(tool_use)],
1080                    cache: false,
1081                    reasoning_details: None,
1082                }],
1083                ..Default::default()
1084            },
1085            "gemini-2.5-flash".to_string(),
1086            GoogleModelMode::Default,
1087        );
1088
1089        assert_eq!(request.contents[0].parts.len(), 1);
1090        if let Part::FunctionCallPart(fc_part) = &request.contents[0].parts[0] {
1091            assert_eq!(fc_part.function_call.name, "test_function");
1092            assert_eq!(
1093                fc_part.thought_signature.as_deref(),
1094                Some("test_signature_456")
1095            );
1096        } else {
1097            panic!("Expected FunctionCallPart");
1098        }
1099    }
1100
1101    #[test]
1102    fn test_tool_use_without_signature_omits_field() {
1103        let tool_use = language_model::LanguageModelToolUse {
1104            id: LanguageModelToolUseId::from("test_id"),
1105            name: "test_function".into(),
1106            raw_input: json!({"arg": "value"}).to_string(),
1107            input: json!({"arg": "value"}),
1108            is_input_complete: true,
1109            thought_signature: None,
1110        };
1111
1112        let request = super::into_google(
1113            LanguageModelRequest {
1114                messages: vec![language_model::LanguageModelRequestMessage {
1115                    role: Role::Assistant,
1116                    content: vec![MessageContent::ToolUse(tool_use)],
1117                    cache: false,
1118                    reasoning_details: None,
1119                }],
1120                ..Default::default()
1121            },
1122            "gemini-2.5-flash".to_string(),
1123            GoogleModelMode::Default,
1124        );
1125
1126        assert_eq!(request.contents[0].parts.len(), 1);
1127        if let Part::FunctionCallPart(fc_part) = &request.contents[0].parts[0] {
1128            assert_eq!(fc_part.thought_signature, None);
1129        } else {
1130            panic!("Expected FunctionCallPart");
1131        }
1132    }
1133
1134    #[test]
1135    fn test_empty_signature_in_tool_use_normalized_to_none() {
1136        let tool_use = language_model::LanguageModelToolUse {
1137            id: LanguageModelToolUseId::from("test_id"),
1138            name: "test_function".into(),
1139            raw_input: json!({"arg": "value"}).to_string(),
1140            input: json!({"arg": "value"}),
1141            is_input_complete: true,
1142            thought_signature: Some("".to_string()),
1143        };
1144
1145        let request = super::into_google(
1146            LanguageModelRequest {
1147                messages: vec![language_model::LanguageModelRequestMessage {
1148                    role: Role::Assistant,
1149                    content: vec![MessageContent::ToolUse(tool_use)],
1150                    cache: false,
1151                    reasoning_details: None,
1152                }],
1153                ..Default::default()
1154            },
1155            "gemini-2.5-flash".to_string(),
1156            GoogleModelMode::Default,
1157        );
1158
1159        if let Part::FunctionCallPart(fc_part) = &request.contents[0].parts[0] {
1160            assert_eq!(fc_part.thought_signature, None);
1161        } else {
1162            panic!("Expected FunctionCallPart");
1163        }
1164    }
1165
1166    #[test]
1167    fn test_round_trip_preserves_signature() {
1168        let mut mapper = GoogleEventMapper::new();
1169
1170        // Simulate receiving a response from Google with a signature
1171        let response = GenerateContentResponse {
1172            candidates: Some(vec![GenerateContentCandidate {
1173                index: Some(0),
1174                content: Content {
1175                    parts: vec![Part::FunctionCallPart(FunctionCallPart {
1176                        function_call: FunctionCall {
1177                            name: "test_function".to_string(),
1178                            args: json!({"arg": "value"}),
1179                        },
1180                        thought_signature: Some("round_trip_sig".to_string()),
1181                    })],
1182                    role: GoogleRole::Model,
1183                },
1184                finish_reason: None,
1185                finish_message: None,
1186                safety_ratings: None,
1187                citation_metadata: None,
1188            }]),
1189            prompt_feedback: None,
1190            usage_metadata: None,
1191        };
1192
1193        let events = mapper.map_event(response);
1194
1195        let tool_use = if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
1196            tool_use.clone()
1197        } else {
1198            panic!("Expected ToolUse event");
1199        };
1200
1201        // Convert back to Google format
1202        let request = super::into_google(
1203            LanguageModelRequest {
1204                messages: vec![language_model::LanguageModelRequestMessage {
1205                    role: Role::Assistant,
1206                    content: vec![MessageContent::ToolUse(tool_use)],
1207                    cache: false,
1208                    reasoning_details: None,
1209                }],
1210                ..Default::default()
1211            },
1212            "gemini-2.5-flash".to_string(),
1213            GoogleModelMode::Default,
1214        );
1215
1216        // Verify signature is preserved
1217        if let Part::FunctionCallPart(fc_part) = &request.contents[0].parts[0] {
1218            assert_eq!(fc_part.thought_signature.as_deref(), Some("round_trip_sig"));
1219        } else {
1220            panic!("Expected FunctionCallPart");
1221        }
1222    }
1223
1224    #[test]
1225    fn test_mixed_text_and_function_call_with_signature() {
1226        let mut mapper = GoogleEventMapper::new();
1227
1228        let response = GenerateContentResponse {
1229            candidates: Some(vec![GenerateContentCandidate {
1230                index: Some(0),
1231                content: Content {
1232                    parts: vec![
1233                        Part::TextPart(TextPart {
1234                            text: "I'll help with that.".to_string(),
1235                        }),
1236                        Part::FunctionCallPart(FunctionCallPart {
1237                            function_call: FunctionCall {
1238                                name: "helper_function".to_string(),
1239                                args: json!({"query": "help"}),
1240                            },
1241                            thought_signature: Some("mixed_sig".to_string()),
1242                        }),
1243                    ],
1244                    role: GoogleRole::Model,
1245                },
1246                finish_reason: None,
1247                finish_message: None,
1248                safety_ratings: None,
1249                citation_metadata: None,
1250            }]),
1251            prompt_feedback: None,
1252            usage_metadata: None,
1253        };
1254
1255        let events = mapper.map_event(response);
1256
1257        assert_eq!(events.len(), 3); // Text event + ToolUse event + Stop event
1258
1259        if let Ok(LanguageModelCompletionEvent::Text(text)) = &events[0] {
1260            assert_eq!(text, "I'll help with that.");
1261        } else {
1262            panic!("Expected Text event");
1263        }
1264
1265        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[1] {
1266            assert_eq!(tool_use.name.as_ref(), "helper_function");
1267            assert_eq!(tool_use.thought_signature.as_deref(), Some("mixed_sig"));
1268        } else {
1269            panic!("Expected ToolUse event");
1270        }
1271    }
1272
1273    #[test]
1274    fn test_special_characters_in_signature_preserved() {
1275        let mut mapper = GoogleEventMapper::new();
1276
1277        let signature_with_special_chars = "sig<>\"'&%$#@!{}[]".to_string();
1278
1279        let response = GenerateContentResponse {
1280            candidates: Some(vec![GenerateContentCandidate {
1281                index: Some(0),
1282                content: Content {
1283                    parts: vec![Part::FunctionCallPart(FunctionCallPart {
1284                        function_call: FunctionCall {
1285                            name: "test_function".to_string(),
1286                            args: json!({"arg": "value"}),
1287                        },
1288                        thought_signature: Some(signature_with_special_chars.clone()),
1289                    })],
1290                    role: GoogleRole::Model,
1291                },
1292                finish_reason: None,
1293                finish_message: None,
1294                safety_ratings: None,
1295                citation_metadata: None,
1296            }]),
1297            prompt_feedback: None,
1298            usage_metadata: None,
1299        };
1300
1301        let events = mapper.map_event(response);
1302
1303        if let Ok(LanguageModelCompletionEvent::ToolUse(tool_use)) = &events[0] {
1304            assert_eq!(
1305                tool_use.thought_signature.as_deref(),
1306                Some(signature_with_special_chars.as_str())
1307            );
1308        } else {
1309            panic!("Expected ToolUse event");
1310        }
1311    }
1312}