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