google.rs

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