Fix gemini again (#5308)
Co-authored-by: Douwe Osinga <douwe@squareup.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use crate::conversation::message::{Message, MessageContent};
|
||||
use crate::model::ModelConfig;
|
||||
use crate::providers::formats::google as gemini_schema;
|
||||
use crate::providers::utils::{
|
||||
convert_image, detect_image_path, is_valid_function_name, load_image_file, safely_parse_json,
|
||||
sanitize_function_name, ImageFormat,
|
||||
@@ -276,9 +277,7 @@ pub fn format_tools(tools: &[Tool], model_name: &str) -> anyhow::Result<Vec<Valu
|
||||
}
|
||||
|
||||
let parameters = if is_gemini {
|
||||
let mut cleaned_schema = tool.input_schema.as_ref().clone();
|
||||
cleaned_schema.remove("$schema");
|
||||
json!(cleaned_schema)
|
||||
gemini_schema::process_map(tool.input_schema.as_ref(), None)
|
||||
} else {
|
||||
json!(tool.input_schema)
|
||||
};
|
||||
|
||||
@@ -154,8 +154,7 @@ pub fn format_tools(tools: &[Tool]) -> Vec<Value> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get the accepted keys for a given parent key in the JSON schema.
|
||||
fn get_accepted_keys(parent_key: Option<&str>) -> Vec<&str> {
|
||||
pub fn get_accepted_keys(parent_key: Option<&str>) -> Vec<&str> {
|
||||
match parent_key {
|
||||
Some("properties") => vec![
|
||||
"anyOf",
|
||||
@@ -178,7 +177,7 @@ fn get_accepted_keys(parent_key: Option<&str>) -> Vec<&str> {
|
||||
/// Process a JSON map to filter out unsupported attributes, mirroring the logic
|
||||
/// from the official Google Gemini CLI.
|
||||
/// See: https://github.com/google-gemini/gemini-cli/blob/8a6509ffeba271a8e7ccb83066a9a31a5d72a647/packages/core/src/tools/tool-registry.ts#L356
|
||||
fn process_map(map: &Map<String, Value>, parent_key: Option<&str>) -> Value {
|
||||
pub fn process_map(map: &Map<String, Value>, parent_key: Option<&str>) -> Value {
|
||||
let accepted_keys = get_accepted_keys(parent_key);
|
||||
let filtered_map: Map<String, Value> = map
|
||||
.iter()
|
||||
|
||||
@@ -92,29 +92,24 @@ pub fn map_http_error_to_provider_error(
|
||||
ProviderError::ContextLengthExceeded(payload_str)
|
||||
}
|
||||
StatusCode::BAD_REQUEST => {
|
||||
let mut error_msg = "Unknown error".to_string();
|
||||
let base_msg = format!("Request failed with status: {}", status);
|
||||
if let Some(payload) = &payload {
|
||||
let payload_str = payload.to_string();
|
||||
if check_context_length_exceeded(&payload_str) {
|
||||
ProviderError::ContextLengthExceeded(payload_str)
|
||||
} else {
|
||||
if let Some(error) = payload.get("error") {
|
||||
error_msg = error
|
||||
.get("message")
|
||||
ProviderError::RequestFailed(
|
||||
payload
|
||||
.get("error")
|
||||
.and_then(|e| e.get("message"))
|
||||
.or_else(|| payload.get("message"))
|
||||
.and_then(|m| m.as_str())
|
||||
.unwrap_or("Unknown error")
|
||||
.to_string();
|
||||
}
|
||||
ProviderError::RequestFailed(format!(
|
||||
"Request failed with status: {}. Message: {}",
|
||||
status, error_msg
|
||||
))
|
||||
.map(|msg| format!("{}. Message: {}", base_msg, msg))
|
||||
.unwrap_or(base_msg),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
ProviderError::RequestFailed(format!(
|
||||
"Request failed with status: {}. Message: {}",
|
||||
status, error_msg
|
||||
))
|
||||
ProviderError::RequestFailed(base_msg)
|
||||
}
|
||||
}
|
||||
StatusCode::TOO_MANY_REQUESTS => ProviderError::RateLimitExceeded {
|
||||
@@ -1101,7 +1096,7 @@ mod tests {
|
||||
StatusCode::BAD_REQUEST,
|
||||
None,
|
||||
ProviderError::RequestFailed(
|
||||
"Request failed with status: 400 Bad Request. Message: Unknown error".to_string(),
|
||||
"Request failed with status: 400 Bad Request".to_string(),
|
||||
),
|
||||
),
|
||||
// TOO_MANY_REQUESTS
|
||||
|
||||
Reference in New Issue
Block a user