mirror of
https://github.com/zed-industries/zed.git
synced 2026-11-19 06:59:22 +01:00
google_ai: Add Extra High thinking level for Gemini 4 Hydrogen (#67011)
## Summary Following #65572, Zed loads BYOK Gemini models dynamically from Google's `v1beta/models` catalog, while keeping model-specific thinking level rules in `google_ai` because the catalog endpoint only reports a boolean `thinking` flag. Google's newly released [Gemini 4 Hydrogen (`gemini-4-hydrogen`)](https://aistudio.google.com/docs/models/gemini-4-hydrogen) introduces a fourth thinking level (`xhigh` / `"XHIGH"`) and defaults to `high` rather than `medium` (while rejecting `minimal`). Without explicit support in `google_ai`, `Extra High` is omitted from Zed's thinking effort selector and Hydrogen defaults to `Medium`. This PR: - Adds `ThinkingLevel::XHigh` (`"Extra High"` / `"xhigh"`, serialized as `"XHIGH"` on `generateContent` requests). - Adds `supports_xhigh_thinking` so `Model::from_listed` exposes `[Low, Medium, High, XHigh]` on `gemini-4-hydrogen` while keeping other level-based models capped at `High` (ensuring BYOK `stream_completion` strips `"xhigh"` when switching threads to models that reject it). - Updates `default_thinking_level` to map `"gemini-4-hydrogen"` to `ThinkingLevel::High`. - Filters `request.thinking_effort` against `config.supported_effort_levels` on hosted Google requests in `language_models_cloud` before calling `into_google`, matching the BYOK provider so switching a thread from `xhigh` to a hosted Gemini model that doesn't accept it falls back to the model default instead of failing validation. ## Testing - `cargo test -p google_ai` (33 passed) - `cargo test -p language_models provider::google::tests` (1 passed) - `./script/clippy -p google_ai` - `cargo fmt -p google_ai -p language_models -p language_models_cloud -- --check` Reviewers with a Gemini API key (or Zed Cloud access) can select `Gemini 4 Hydrogen` in the Agent Panel and verify that thinking effort defaults to `High`, lists `Low`, `Medium`, `High`, and `Extra High`, and falls back gracefully when switching to `Gemini 4 Argon` or `Gemini 4 Carbon`. ## Self-review - [x] I've reviewed my diff for quality, security, reliability, and performance. - [x] UI changes follow the [checklist](https://zed.dev/docs/development/ui-checklist). - [x] Tests cover the new or changed behavior. Release Notes: - Added Extra High thinking effort support and High default thinking level for Gemini 4 Hydrogen (`gemini-4-hydrogen`). --------- Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>
This commit is contained in:
@@ -76,11 +76,21 @@ fn supports_minimal_thinking(model_id: &str) -> bool {
|
||||
)
|
||||
}
|
||||
|
||||
/// The most thinking a level-based model accepts. Only some models go past `HIGH`.
|
||||
fn highest_thinking_level(model_id: &str) -> ThinkingLevel {
|
||||
match model_id {
|
||||
"gemini-4-hydrogen" => ThinkingLevel::XHigh,
|
||||
_ => ThinkingLevel::High,
|
||||
}
|
||||
}
|
||||
|
||||
/// The thinking level Zed selects for a level-based model until the user picks another.
|
||||
fn default_thinking_level(model_id: &str) -> ThinkingLevel {
|
||||
match model_id {
|
||||
"gemini-3.5-flash-lite" => ThinkingLevel::Minimal,
|
||||
"gemini-3-flash-preview" | "gemini-3.1-pro-preview" => ThinkingLevel::High,
|
||||
"gemini-3-flash-preview" | "gemini-3.1-pro-preview" | "gemini-4-hydrogen" => {
|
||||
ThinkingLevel::High
|
||||
}
|
||||
_ => ThinkingLevel::Medium,
|
||||
}
|
||||
}
|
||||
@@ -663,13 +673,14 @@ pub struct ThinkingConfig {
|
||||
pub include_thoughts: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "UPPERCASE")]
|
||||
pub enum ThinkingLevel {
|
||||
Minimal,
|
||||
Low,
|
||||
Medium,
|
||||
High,
|
||||
XHigh,
|
||||
}
|
||||
|
||||
impl ThinkingLevel {
|
||||
@@ -679,6 +690,7 @@ impl ThinkingLevel {
|
||||
"low" => Some(Self::Low),
|
||||
"medium" => Some(Self::Medium),
|
||||
"high" => Some(Self::High),
|
||||
"xhigh" => Some(Self::XHigh),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -689,6 +701,7 @@ impl ThinkingLevel {
|
||||
Self::Low => "Low",
|
||||
Self::Medium => "Medium",
|
||||
Self::High => "High",
|
||||
Self::XHigh => "Extra High",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -698,6 +711,7 @@ impl ThinkingLevel {
|
||||
Self::Low => "low",
|
||||
Self::Medium => "medium",
|
||||
Self::High => "high",
|
||||
Self::XHigh => "xhigh",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -933,9 +947,10 @@ impl Model {
|
||||
ThinkingLevel::Low,
|
||||
ThinkingLevel::Medium,
|
||||
ThinkingLevel::High,
|
||||
ThinkingLevel::XHigh,
|
||||
]
|
||||
.into_iter()
|
||||
.skip_while(|level| *level != lowest)
|
||||
.filter(|level| (lowest..=highest_thinking_level(id)).contains(level))
|
||||
.collect(),
|
||||
Some(default_thinking_level(id)),
|
||||
),
|
||||
@@ -1066,6 +1081,54 @@ mod tests {
|
||||
assert_eq!(lowest_thinking_level("gemini-2.5-flash"), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_from_listed_thinking_levels_per_model() {
|
||||
let listed_entry = |id: &str| ListModelEntry {
|
||||
name: format!("models/{id}"),
|
||||
display_name: Some(id.to_string()),
|
||||
input_token_limit: Some(1_048_576),
|
||||
output_token_limit: Some(65_536),
|
||||
supported_generation_methods: vec!["generateContent".to_string()],
|
||||
thinking: true,
|
||||
};
|
||||
|
||||
for (id, expected_levels, expected_default) in [
|
||||
(
|
||||
"gemini-4-hydrogen",
|
||||
vec![
|
||||
ThinkingLevel::Low,
|
||||
ThinkingLevel::Medium,
|
||||
ThinkingLevel::High,
|
||||
ThinkingLevel::XHigh,
|
||||
],
|
||||
ThinkingLevel::High,
|
||||
),
|
||||
(
|
||||
"gemini-4-argon",
|
||||
vec![
|
||||
ThinkingLevel::Low,
|
||||
ThinkingLevel::Medium,
|
||||
ThinkingLevel::High,
|
||||
],
|
||||
ThinkingLevel::Medium,
|
||||
),
|
||||
(
|
||||
"gemini-3.6-flash",
|
||||
vec![
|
||||
ThinkingLevel::Minimal,
|
||||
ThinkingLevel::Low,
|
||||
ThinkingLevel::Medium,
|
||||
ThinkingLevel::High,
|
||||
],
|
||||
ThinkingLevel::Medium,
|
||||
),
|
||||
] {
|
||||
let model = Model::from_listed(listed_entry(id)).unwrap();
|
||||
assert_eq!(model.supported_thinking_levels, expected_levels, "{id}");
|
||||
assert_eq!(model.default_thinking_level, Some(expected_default), "{id}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_versioned_model() {
|
||||
assert!(is_versioned_model("gemini-3.1-pro-preview"));
|
||||
|
||||
@@ -445,3 +445,117 @@ fn available_model_to_google_model(available: &AvailableModel) -> google_ai::Mod
|
||||
default_thinking_level: None,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use credentials_provider::test_support::FakeCredentialsProvider;
|
||||
use futures::AsyncReadExt as _;
|
||||
use gpui::TestAppContext;
|
||||
use http_client::{AsyncBody, FakeHttpClient, Method, Response};
|
||||
use language_model::{LanguageModelRequestMessage, MessageContent, Role};
|
||||
use parking_lot::Mutex;
|
||||
use serde_json::json;
|
||||
|
||||
#[gpui::test]
|
||||
async fn catalog_models_drop_xhigh_effort_unless_supported(cx: &mut TestAppContext) {
|
||||
let thinking_configs: Arc<Mutex<Vec<(String, serde_json::Value)>>> = Arc::default();
|
||||
let http_client = FakeHttpClient::create({
|
||||
let thinking_configs = thinking_configs.clone();
|
||||
move |mut request| {
|
||||
let thinking_configs = thinking_configs.clone();
|
||||
async move {
|
||||
if request.method() == Method::GET {
|
||||
let models = ["gemini-4-hydrogen", "gemini-4-argon"].map(|id| {
|
||||
json!({
|
||||
"name": format!("models/{id}"),
|
||||
"displayName": id,
|
||||
"inputTokenLimit": 1_048_576,
|
||||
"outputTokenLimit": 65_536,
|
||||
"supportedGenerationMethods": ["generateContent"],
|
||||
"thinking": true
|
||||
})
|
||||
});
|
||||
let catalog = json!({ "models": models });
|
||||
return Ok(Response::builder()
|
||||
.status(200)
|
||||
.body(AsyncBody::from(catalog.to_string()))?);
|
||||
}
|
||||
|
||||
let mut body = String::new();
|
||||
request.body_mut().read_to_string(&mut body).await?;
|
||||
let body: serde_json::Value = serde_json::from_str(&body)?;
|
||||
thinking_configs.lock().push((
|
||||
request.uri().path().to_string(),
|
||||
body["generationConfig"]["thinkingConfig"].clone(),
|
||||
));
|
||||
Ok(Response::builder().status(200).body(AsyncBody::from(
|
||||
"data: {\"candidates\":[{\"content\":{\"role\":\"model\",\"parts\":[{\"text\":\"ok\"}]},\"finishReason\":\"STOP\"}]}\n\n",
|
||||
))?)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let provider = cx.update(|cx| {
|
||||
let settings_store = SettingsStore::test(cx);
|
||||
cx.set_global(settings_store);
|
||||
GoogleLanguageModelProvider::new(
|
||||
http_client,
|
||||
Arc::new(FakeCredentialsProvider::default()),
|
||||
cx,
|
||||
)
|
||||
});
|
||||
cx.update(|cx| provider.set_api_key(Some("test-key".to_string()), cx))
|
||||
.await
|
||||
.unwrap();
|
||||
cx.run_until_parked();
|
||||
|
||||
let models = cx.read(|cx| provider.provided_models(cx));
|
||||
let xhigh = models
|
||||
.iter()
|
||||
.find(|model| model.id.0.as_ref() == "gemini-4-hydrogen")
|
||||
.and_then(|model| model.supported_effort_levels.last())
|
||||
.unwrap()
|
||||
.clone();
|
||||
assert_eq!(xhigh.name.as_ref(), "Extra High");
|
||||
let async_cx = cx.to_async();
|
||||
for model_id in ["gemini-4-hydrogen", "gemini-4-argon"] {
|
||||
let model = models
|
||||
.iter()
|
||||
.find(|model| model.id.0.as_ref() == model_id)
|
||||
.unwrap();
|
||||
let request = LanguageModelRequest {
|
||||
messages: vec![LanguageModelRequestMessage {
|
||||
role: Role::User,
|
||||
content: vec![MessageContent::Text("Hello".to_string())],
|
||||
cache: false,
|
||||
reasoning_details: None,
|
||||
}],
|
||||
thinking_allowed: true,
|
||||
thinking_effort: Some(xhigh.value.to_string()),
|
||||
..Default::default()
|
||||
};
|
||||
let mut stream = provider
|
||||
.stream_completion(model, request, &async_cx)
|
||||
.await
|
||||
.unwrap();
|
||||
while let Some(event) = stream.next().await {
|
||||
event.unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
*thinking_configs.lock(),
|
||||
[
|
||||
(
|
||||
"/v1beta/models/gemini-4-hydrogen:streamGenerateContent".to_string(),
|
||||
json!({"thinkingLevel": "XHIGH", "includeThoughts": true}),
|
||||
),
|
||||
(
|
||||
"/v1beta/models/gemini-4-argon:streamGenerateContent".to_string(),
|
||||
json!({"includeThoughts": true}),
|
||||
),
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1192,6 +1192,18 @@ impl<TP: CloudLlmTokenProvider + 'static> CloudModelProvider<TP> {
|
||||
cloud_llm_client::LanguageModelProvider::Google => {
|
||||
let http_client = self.http_client.clone();
|
||||
let token_provider = self.token_provider.clone();
|
||||
let mut request = request;
|
||||
// A thread keeps its effort across model switches, and Gemini rejects levels a
|
||||
// model doesn't accept, including any level for thinking-budget models.
|
||||
let effort_supported = request.thinking_effort.as_deref().is_some_and(|effort| {
|
||||
config
|
||||
.supported_effort_levels
|
||||
.iter()
|
||||
.any(|level| level.value.eq_ignore_ascii_case(effort))
|
||||
});
|
||||
if !effort_supported {
|
||||
request.thinking_effort = None;
|
||||
}
|
||||
let mode = if config.supports_thinking {
|
||||
GoogleModelMode::Thinking {
|
||||
budget_tokens: None,
|
||||
@@ -2563,6 +2575,12 @@ mod tests {
|
||||
config.provider = cloud_llm_client::LanguageModelProvider::Google;
|
||||
config.id = cloud_llm_client::LanguageModelId("future-google-model".into());
|
||||
config.supports_thinking = true;
|
||||
config.supported_effort_levels = vec![cloud_llm_client::SupportedEffortLevel {
|
||||
name: Arc::from("Medium"),
|
||||
value: Arc::from("medium"),
|
||||
is_default: Some(true),
|
||||
supports_disabling_thinking: None,
|
||||
}];
|
||||
let (provider, model) = bind_test_model(
|
||||
FakeHttpClient::create(|mut request| async move {
|
||||
let mut body = String::new();
|
||||
@@ -2611,6 +2629,72 @@ mod tests {
|
||||
assert_eq!(summaries, ["Summary"]);
|
||||
}
|
||||
|
||||
#[gpui::test]
|
||||
async fn hosted_google_sends_only_effort_levels_the_model_lists(cx: &mut gpui::TestAppContext) {
|
||||
for (listed_efforts, expected_thinking_config) in [
|
||||
(
|
||||
["low", "medium", "high", "xhigh"].as_slice(),
|
||||
json!({"thinkingLevel": "XHIGH", "includeThoughts": true}),
|
||||
),
|
||||
(
|
||||
["low", "medium", "high"].as_slice(),
|
||||
json!({"includeThoughts": true}),
|
||||
),
|
||||
] {
|
||||
let thinking_config = Arc::new(Mutex::new(None));
|
||||
let http_client = FakeHttpClient::create({
|
||||
let thinking_config = thinking_config.clone();
|
||||
move |mut request| {
|
||||
let thinking_config = thinking_config.clone();
|
||||
async move {
|
||||
let mut body = String::new();
|
||||
request.body_mut().read_to_string(&mut body).await?;
|
||||
let body: serde_json::Value = serde_json::from_str(&body)?;
|
||||
*thinking_config.lock().unwrap() = Some(
|
||||
body["provider_request"]["generationConfig"]["thinkingConfig"].clone(),
|
||||
);
|
||||
Ok(Response::builder()
|
||||
.status(200)
|
||||
.header(SERVER_SUPPORTS_STATUS_MESSAGES_HEADER_NAME, "true")
|
||||
.body(AsyncBody::from(
|
||||
json!({"status": "stream_ended"}).to_string(),
|
||||
))?)
|
||||
}
|
||||
}
|
||||
});
|
||||
let mut config = cloud_test_config();
|
||||
config.provider = cloud_llm_client::LanguageModelProvider::Google;
|
||||
config.id = cloud_llm_client::LanguageModelId("future-google-model".into());
|
||||
config.supports_thinking = true;
|
||||
config.supported_effort_levels = listed_efforts
|
||||
.iter()
|
||||
.map(|value| cloud_llm_client::SupportedEffortLevel {
|
||||
name: Arc::from(*value),
|
||||
value: Arc::from(*value),
|
||||
is_default: None,
|
||||
supports_disabling_thinking: None,
|
||||
})
|
||||
.collect();
|
||||
let (provider, model) = bind_test_model(http_client, config);
|
||||
|
||||
let mut request = compact_test_request();
|
||||
request.thinking_allowed = true;
|
||||
request.thinking_effort = Some("xhigh".into());
|
||||
cx.update(|cx| provider.stream_completion(&model, request, cx))
|
||||
.await
|
||||
.unwrap()
|
||||
.try_collect::<Vec<_>>()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
thinking_config.lock().unwrap().take(),
|
||||
Some(expected_thinking_config),
|
||||
"listed efforts: {listed_efforts:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn cloud_google_test_model(
|
||||
events: Vec<serde_json::Value>,
|
||||
) -> (CloudModelProvider<TestTokenProvider>, LanguageModel) {
|
||||
|
||||
Reference in New Issue
Block a user