use serde_json::{Map, Value};
#[must_use]
pub fn request_to_chat(body: &Value, model: &str) -> Value {
let Some(obj) = body.as_object() else {
return body.clone();
};
let mut out = Map::new();
out.insert("model".to_owned(), Value::String(model.to_owned()));
let mut messages: Vec<Value> = Vec::new();
if let Some(si) = obj.get("systemInstruction")
&& let Some(text) = parts_to_text(si)
&& !text.is_empty()
{
messages.push(serde_json::json!({"role": "system", "content": text}));
}
if let Some(Value::Array(contents)) = obj.get("contents") {
for (turn, c) in contents.iter().enumerate() {
messages.extend(content_to_messages(c, turn));
}
}
out.insert("messages".to_owned(), Value::Array(messages));
if let Some(Value::Object(gen_cfg)) = obj.get("generationConfig") {
if let Some(v) = gen_cfg.get("maxOutputTokens") {
out.insert("max_tokens".to_owned(), v.clone());
}
if let Some(v) = gen_cfg.get("temperature") {
out.insert("temperature".to_owned(), v.clone());
}
if let Some(v) = gen_cfg.get("topP") {
out.insert("top_p".to_owned(), v.clone());
}
}
if let Some(Value::Array(tools)) = obj.get("tools") {
let converted: Vec<Value> = tools
.iter()
.flat_map(|t| {
t.get("functionDeclarations")
.and_then(Value::as_array)
.map_or(vec![], |fds| fds.iter().filter_map(fd_to_chat).collect())
})
.collect();
if !converted.is_empty() {
out.insert("tools".to_owned(), Value::Array(converted));
}
}
Value::Object(out)
}
fn content_to_messages(content: &Value, turn: usize) -> Vec<Value> {
let Some(obj) = content.as_object() else {
return vec![];
};
let role = obj.get("role").and_then(Value::as_str).unwrap_or("user");
let chat_role = if role == "model" { "assistant" } else { role };
let Some(parts) = obj.get("parts").and_then(Value::as_array) else {
return vec![];
};
let calls: Vec<Value> = parts
.iter()
.filter_map(|p| p.as_object()?.get("functionCall")?.as_object())
.enumerate()
.filter_map(|(idx, fc)| {
let name = fc.get("name")?.clone();
let args = fc
.get("args")
.cloned()
.unwrap_or_else(|| Value::Object(Map::new()));
Some(serde_json::json!({
"id": format!("call_{turn}_{idx}"),
"type": "function",
"function": { "name": name, "arguments": args.to_string() },
}))
})
.collect();
if !calls.is_empty() {
let text = text_from_parts(parts);
let content_val = if text.is_empty() {
Value::Null
} else {
Value::String(text)
};
return vec![serde_json::json!({
"role": "assistant",
"content": content_val,
"tool_calls": calls,
})];
}
let tool_msgs: Vec<Value> = parts
.iter()
.filter_map(|p| p.as_object()?.get("functionResponse")?.as_object())
.enumerate()
.filter_map(|(idx, fr)| {
fr.get("name").and_then(Value::as_str)?;
let output = fr
.get("response")
.cloned()
.unwrap_or_else(|| Value::Object(Map::new()))
.to_string();
Some(serde_json::json!({
"role": "tool",
"tool_call_id": format!("call_{}_{idx}", turn.saturating_sub(1)),
"content": output,
}))
})
.collect();
if !tool_msgs.is_empty() {
return tool_msgs;
}
let text = text_from_parts(parts);
vec![serde_json::json!({ "role": chat_role, "content": text })]
}
fn text_from_parts(parts: &[Value]) -> String {
parts
.iter()
.filter_map(|p| p.get("text").and_then(Value::as_str))
.collect::<Vec<_>>()
.join("")
}
fn parts_to_text(v: &Value) -> Option<String> {
Some(text_from_parts(v.get("parts").and_then(Value::as_array)?))
}
fn fd_to_chat(fd: &Value) -> Option<Value> {
let mut f = Map::new();
f.insert("name".to_owned(), fd.get("name")?.clone());
if let Some(d) = fd.get("description") {
f.insert("description".to_owned(), d.clone());
}
if let Some(p) = fd.get("parameters") {
f.insert("parameters".to_owned(), p.clone());
}
Some(serde_json::json!({"type": "function", "function": Value::Object(f)}))
}
fn function_pairing_is_exact(contents: Option<&Value>) -> bool {
let Some(Value::Array(contents)) = contents else {
return true;
};
let names = |c: &Value, key: &str| -> Vec<String> {
c.get("parts")
.and_then(Value::as_array)
.map(|ps| {
ps.iter()
.filter_map(|p| p.get(key)?.get("name")?.as_str().map(str::to_owned))
.collect()
})
.unwrap_or_default()
};
let mut pending: Vec<String> = Vec::new();
for c in contents {
let responses = names(c, "functionResponse");
if !responses.is_empty() {
let all_parts = c.get("parts").and_then(Value::as_array).map_or(0, Vec::len);
if responses != pending || all_parts != responses.len() {
return false;
}
pending.clear();
continue;
}
if !pending.is_empty() {
return false; }
pending = names(c, "functionCall");
}
pending.is_empty()
}
#[must_use]
pub fn has_untranslatable_content(body: &Value) -> bool {
let Some(obj) = body.as_object() else {
return false;
};
if obj.keys().any(|k| {
!matches!(
k.as_str(),
"contents" | "systemInstruction" | "generationConfig" | "tools"
)
}) {
return true;
}
let parts_array = |v: &Value| v.get("parts").is_some_and(Value::is_array);
if obj.get("contents").is_some_and(|c| !c.is_array())
|| obj
.get("systemInstruction")
.is_some_and(|si| !parts_array(si))
|| obj.get("tools").is_some_and(|t| {
!t.as_array().is_some_and(|ts| {
ts.iter()
.all(|t| t.get("functionDeclarations").is_some_and(Value::is_array))
})
})
{
return true;
}
if !function_pairing_is_exact(obj.get("contents")) {
return true;
}
if obj.contains_key("cachedContent")
|| obj.contains_key("toolConfig")
|| obj.contains_key("safetySettings")
{
return true;
}
if let Some(Value::Array(tools)) = obj.get("tools") {
for tool in tools {
let is_only_fn_decls = tool
.as_object()
.is_some_and(|m| m.keys().all(|k| k == "functionDeclarations"));
if !is_only_fn_decls {
return true;
}
}
}
if let Some(si) = obj.get("systemInstruction")
&& let Some(Value::Array(parts)) = si.get("parts")
{
for p in parts {
let has_only_text = p
.as_object()
.is_some_and(|m| m.len() == 1 && m.contains_key("text"));
if !has_only_text {
return true;
}
}
}
if let Some(Value::Object(gen_cfg)) = obj.get("generationConfig") {
for k in gen_cfg.keys() {
if !matches!(k.as_str(), "maxOutputTokens" | "temperature" | "topP") {
return true;
}
}
}
let Some(Value::Array(contents)) = obj.get("contents") else {
return false;
};
contents.iter().any(|content| {
let Some(content_obj) = content.as_object() else {
return true;
};
let Some(Value::Array(parts)) = content_obj.get("parts") else {
return true;
};
parts.iter().any(|part| {
let Some(part_obj) = part.as_object() else {
return true;
};
part_obj.is_empty()
|| part_obj
.keys()
.any(|k| !matches!(k.as_str(), "text" | "functionCall" | "functionResponse"))
})
})
}
#[must_use]
pub fn response_from_chat(chat: &Value) -> Value {
let finish_reason = chat
.pointer("/choices/0/finish_reason")
.and_then(Value::as_str)
.map(chat_finish_reason_to_gemini)
.unwrap_or("STOP");
let mut parts: Vec<Value> = Vec::new();
let text = chat
.pointer("/choices/0/message/content")
.and_then(Value::as_str)
.unwrap_or("");
if !text.is_empty() {
parts.push(serde_json::json!({"text": text}));
}
if let Some(calls) = chat
.pointer("/choices/0/message/tool_calls")
.and_then(Value::as_array)
{
for tc in calls {
let name = tc
.pointer("/function/name")
.and_then(Value::as_str)
.unwrap_or("");
let args_str = tc
.pointer("/function/arguments")
.and_then(Value::as_str)
.unwrap_or("{}");
let args: Value =
serde_json::from_str(args_str).unwrap_or_else(|_| Value::Object(Map::new()));
parts.push(serde_json::json!({
"functionCall": { "name": name, "args": args }
}));
}
}
if parts.is_empty() {
parts.push(serde_json::json!({"text": ""}));
}
let in_tok = chat
.pointer("/usage/prompt_tokens")
.and_then(Value::as_u64)
.unwrap_or(0);
let out_tok = chat
.pointer("/usage/completion_tokens")
.and_then(Value::as_u64)
.unwrap_or(0);
serde_json::json!({
"candidates": [{
"content": { "role": "model", "parts": parts },
"finishReason": finish_reason,
}],
"usageMetadata": {
"promptTokenCount": in_tok,
"candidatesTokenCount": out_tok,
"totalTokenCount": in_tok + out_tok,
},
})
}
fn chat_finish_reason_to_gemini(reason: &str) -> &'static str {
match reason {
"length" => "MAX_TOKENS",
_ => "STOP",
}
}
#[must_use]
pub fn sse_from_response(resp: &Value) -> String {
format!("data: {resp}\n\n")
}
#[must_use]
pub fn sse_error_response(message: &str) -> String {
let resp = serde_json::json!({
"error": { "code": 500, "message": message, "status": "INTERNAL" }
});
format!("data: {resp}\n\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plain_text_conversation_translates() {
let body = serde_json::json!({
"contents": [
{ "role": "user", "parts": [{ "text": "hello" }] },
{ "role": "model", "parts": [{ "text": "hi there" }] },
{ "role": "user", "parts": [{ "text": "tell me more" }] },
],
"generationConfig": { "maxOutputTokens": 1024, "temperature": 0.5 },
});
let out = request_to_chat(&body, "gemini-2.0-flash");
assert_eq!(out["model"], "gemini-2.0-flash");
let msgs = out["messages"].as_array().unwrap();
assert_eq!(msgs.len(), 3);
assert_eq!(msgs[0]["role"], "user");
assert_eq!(msgs[0]["content"], "hello");
assert_eq!(msgs[1]["role"], "assistant");
assert_eq!(msgs[1]["content"], "hi there");
assert_eq!(msgs[2]["role"], "user");
assert_eq!(out["max_tokens"], 1024);
assert_eq!(out["temperature"], 0.5);
}
#[test]
fn system_instruction_becomes_system_message() {
let body = serde_json::json!({
"systemInstruction": { "parts": [{ "text": "be terse" }] },
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
});
let out = request_to_chat(&body, "m");
let msgs = out["messages"].as_array().unwrap();
assert_eq!(msgs[0]["role"], "system");
assert_eq!(msgs[0]["content"], "be terse");
assert_eq!(msgs[1]["role"], "user");
}
#[test]
fn function_declarations_become_chat_tools() {
let body = serde_json::json!({
"contents": [{ "role": "user", "parts": [{ "text": "weather?" }] }],
"tools": [{ "functionDeclarations": [{
"name": "get_weather",
"description": "look up weather",
"parameters": { "type": "object", "properties": { "city": { "type": "string" } } },
}] }],
});
let out = request_to_chat(&body, "m");
let tools = out["tools"].as_array().unwrap();
assert_eq!(tools.len(), 1);
assert_eq!(tools[0]["type"], "function");
assert_eq!(tools[0]["function"]["name"], "get_weather");
assert_eq!(tools[0]["function"]["description"], "look up weather");
}
#[test]
fn function_call_in_request_becomes_tool_calls() {
let body = serde_json::json!({
"contents": [
{ "role": "user", "parts": [{ "text": "weather in Paris?" }] },
{ "role": "model", "parts": [{ "functionCall": {
"name": "get_weather", "args": { "city": "Paris" }
} }] },
],
});
let out = request_to_chat(&body, "m");
let msgs = out["messages"].as_array().unwrap();
assert_eq!(msgs[1]["role"], "assistant");
let calls = msgs[1]["tool_calls"].as_array().unwrap();
assert_eq!(calls.len(), 1);
assert_eq!(calls[0]["function"]["name"], "get_weather");
}
#[test]
fn function_response_becomes_tool_message() {
let body = serde_json::json!({
"contents": [{
"role": "user",
"parts": [{ "functionResponse": {
"name": "get_weather",
"response": { "temperature": "22C" }
} }],
}],
});
let out = request_to_chat(&body, "m");
let msgs = out["messages"].as_array().unwrap();
assert_eq!(msgs[0]["role"], "tool");
assert_eq!(msgs[0]["tool_call_id"], "call_0_0");
assert!(msgs[0]["content"].as_str().is_some());
}
#[test]
fn parallel_function_responses_each_become_own_tool_message() {
let body = serde_json::json!({
"contents": [{
"role": "user",
"parts": [
{ "functionResponse": { "name": "lookup", "response": { "a": 1 } } },
{ "functionResponse": { "name": "lookup", "response": { "b": 2 } } },
],
}],
});
let out = request_to_chat(&body, "m");
let msgs = out["messages"].as_array().unwrap();
assert_eq!(
msgs.len(),
2,
"must emit one message per functionResponse part"
);
assert_eq!(msgs[0]["role"], "tool");
assert_eq!(msgs[0]["tool_call_id"], "call_0_0");
assert_eq!(msgs[1]["role"], "tool");
assert_eq!(msgs[1]["tool_call_id"], "call_0_1");
}
#[test]
fn parallel_function_calls_get_positional_ids() {
let body = serde_json::json!({
"contents": [{
"role": "model",
"parts": [
{ "functionCall": { "name": "search", "args": { "q": "foo" } } },
{ "functionCall": { "name": "search", "args": { "q": "bar" } } },
],
}],
});
let out = request_to_chat(&body, "m");
let msgs = out["messages"].as_array().unwrap();
assert_eq!(msgs.len(), 1);
let calls = msgs[0]["tool_calls"].as_array().unwrap();
assert_eq!(calls.len(), 2);
assert_ne!(
calls[0]["id"], calls[1]["id"],
"parallel calls must have distinct IDs"
);
assert_eq!(calls[0]["id"], "call_0_0");
assert_eq!(calls[1]["id"], "call_0_1");
}
#[test]
fn text_response_produces_candidates_shape() {
let chat = serde_json::json!({
"id": "c", "model": "m",
"choices": [{ "finish_reason": "stop", "message": { "content": "answer" } }],
"usage": { "prompt_tokens": 10, "completion_tokens": 5 },
});
let out = response_from_chat(&chat);
assert_eq!(
out["candidates"][0]["content"]["parts"][0]["text"],
"answer"
);
assert_eq!(out["candidates"][0]["content"]["role"], "model");
assert_eq!(out["candidates"][0]["finishReason"], "STOP");
assert_eq!(out["usageMetadata"]["promptTokenCount"], 10);
assert_eq!(out["usageMetadata"]["candidatesTokenCount"], 5);
assert_eq!(out["usageMetadata"]["totalTokenCount"], 15);
}
#[test]
fn length_finish_reason_maps_to_max_tokens() {
let chat = serde_json::json!({
"choices": [{ "finish_reason": "length", "message": { "content": "cut off" } }],
});
let out = response_from_chat(&chat);
assert_eq!(out["candidates"][0]["finishReason"], "MAX_TOKENS");
}
#[test]
fn tool_calls_in_response_become_function_call_parts() {
let chat = serde_json::json!({
"choices": [{ "finish_reason": "tool_calls", "message": {
"content": null,
"tool_calls": [{
"id": "call_1", "type": "function",
"function": { "name": "get_weather", "arguments": "{\"city\":\"Paris\"}" },
}],
}}],
"usage": { "prompt_tokens": 8, "completion_tokens": 4 },
});
let out = response_from_chat(&chat);
let parts = out["candidates"][0]["content"]["parts"].as_array().unwrap();
assert_eq!(parts[0]["functionCall"]["name"], "get_weather");
assert_eq!(parts[0]["functionCall"]["args"]["city"], "Paris");
assert_eq!(out["candidates"][0]["finishReason"], "STOP");
}
#[test]
fn empty_response_still_has_a_parts_array() {
let chat = serde_json::json!({
"choices": [{ "message": { "content": null } }],
});
let out = response_from_chat(&chat);
assert!(out["candidates"][0]["content"]["parts"].is_array());
}
#[test]
fn plain_text_is_translatable() {
assert!(!has_untranslatable_content(&serde_json::json!({
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
}
#[test]
fn inline_data_is_not_translatable() {
assert!(has_untranslatable_content(&serde_json::json!({
"contents": [{ "role": "user", "parts": [
{ "inlineData": { "mimeType": "image/png", "data": "AAAA" } }
]}],
})));
}
#[test]
fn executable_code_is_not_translatable() {
assert!(has_untranslatable_content(&serde_json::json!({
"contents": [{ "role": "user", "parts": [
{ "executableCode": { "language": "python", "code": "print(1)" } }
]}],
})));
}
#[test]
fn function_call_and_response_are_translatable() {
assert!(!has_untranslatable_content(&serde_json::json!({
"contents": [
{ "role": "model", "parts": [{ "functionCall": { "name": "f", "args": {} } }] },
{ "role": "user", "parts": [{ "functionResponse": { "name": "f", "response": {} } }] },
],
})));
}
#[test]
fn function_call_with_thought_signature_is_untranslatable() {
assert!(has_untranslatable_content(&serde_json::json!({
"contents": [{
"role": "model",
"parts": [{
"functionCall": { "name": "f", "args": {} },
"thoughtSignature": "Aef8xyz=="
}],
}],
})));
}
#[test]
fn a_translatable_part_with_any_extra_field_is_untranslatable() {
for part in [
serde_json::json!({ "text": "hi", "inlineData": { "mimeType": "image/png", "data": "AA==" } }),
serde_json::json!({ "text": "pondering", "thought": true }),
serde_json::json!({ "text": "ok", "thoughtSignature": "sig" }),
serde_json::json!({}),
] {
assert!(
has_untranslatable_content(&serde_json::json!({
"contents": [{ "role": "user", "parts": [part.clone()] }]
})),
"must not translate {part}"
);
}
assert!(!has_untranslatable_content(&serde_json::json!({
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }]
})));
}
#[test]
fn tool_call_ids_pair_across_turns_when_text_precedes_the_call() {
let body = serde_json::json!({ "contents": [
{ "role": "user", "parts": [{ "text": "list files" }] },
{ "role": "model", "parts": [
{ "text": "Let me look." },
{ "functionCall": { "name": "ls", "args": {} } },
{ "functionCall": { "name": "ls", "args": { "path": "src" } } }
] },
{ "role": "user", "parts": [
{ "functionResponse": { "name": "ls", "response": { "out": "a" } } },
{ "functionResponse": { "name": "ls", "response": { "out": "b" } } }
] }
] });
assert!(!has_untranslatable_content(&body));
let chat = request_to_chat(&body, "gemini-2.5-flash");
let msgs = chat["messages"].as_array().unwrap();
let call_ids: Vec<&str> = msgs
.iter()
.flat_map(|m| m["tool_calls"].as_array().into_iter().flatten())
.map(|c| c["id"].as_str().unwrap())
.collect();
let result_ids: Vec<&str> = msgs
.iter()
.filter(|m| m["role"] == "tool")
.map(|m| m["tool_call_id"].as_str().unwrap())
.collect();
assert_eq!(call_ids.len(), 2);
assert_eq!(call_ids, result_ids);
assert_ne!(call_ids[0], call_ids[1]);
}
#[test]
fn tool_call_ids_are_unique_across_turns() {
let call = serde_json::json!({ "functionCall": { "name": "ls", "args": {} } });
let resp = serde_json::json!({ "functionResponse": { "name": "ls", "response": {} } });
let body = serde_json::json!({ "contents": [
{ "role": "user", "parts": [{ "text": "go" }] },
{ "role": "model", "parts": [call.clone()] },
{ "role": "user", "parts": [resp.clone()] },
{ "role": "model", "parts": [call] },
{ "role": "user", "parts": [resp] }
] });
assert!(!has_untranslatable_content(&body));
let chat = request_to_chat(&body, "gemini-2.5-flash");
let msgs = chat["messages"].as_array().unwrap();
let call_ids: Vec<&str> = msgs
.iter()
.flat_map(|m| m["tool_calls"].as_array().into_iter().flatten())
.map(|c| c["id"].as_str().unwrap())
.collect();
let result_ids: Vec<&str> = msgs
.iter()
.filter(|m| m["role"] == "tool")
.map(|m| m["tool_call_id"].as_str().unwrap())
.collect();
assert_eq!(call_ids.len(), 2);
assert_ne!(call_ids[0], call_ids[1]);
assert_eq!(call_ids, result_ids);
}
#[test]
fn malformed_fields_take_passthrough_instead_of_being_dropped() {
let user = serde_json::json!([{ "role": "user", "parts": [{ "text": "hi" }] }]);
for body in [
serde_json::json!({ "contents": user, "systemInstruction": "be terse" }),
serde_json::json!({ "contents": user, "systemInstruction": { "text": "be terse" } }),
serde_json::json!({ "contents": user, "systemInstruction": { "parts": "be terse" } }),
serde_json::json!({ "contents": { "role": "user" } }),
serde_json::json!({ "contents": user, "tools": { "functionDeclarations": [] } }),
serde_json::json!({ "contents": user, "tools": [{ "functionDeclarations": {} }] }),
] {
assert!(
has_untranslatable_content(&body),
"must not translate {body}"
);
}
assert!(!has_untranslatable_content(&serde_json::json!({
"contents": user,
"systemInstruction": { "parts": [{ "text": "be terse" }] },
"tools": [{ "functionDeclarations": [{ "name": "ls", "parameters": { "type": "object" } }] }]
})));
}
#[test]
fn ambiguous_function_pairing_and_unknown_fields_are_untranslatable() {
let call = |n: &str| serde_json::json!({ "functionCall": { "name": n, "args": {} } });
let resp =
|n: &str| serde_json::json!({ "functionResponse": { "name": n, "response": {} } });
let turns = |model: Vec<Value>, user: Vec<Value>| {
serde_json::json!({ "contents": [
{ "role": "user", "parts": [{ "text": "go" }] },
{ "role": "model", "parts": model },
{ "role": "user", "parts": user }
] })
};
for body in [
turns(vec![call("a"), call("b")], vec![resp("b"), resp("a")]),
turns(vec![call("a"), call("b")], vec![resp("a")]),
turns(
vec![call("a")],
vec![resp("a"), serde_json::json!({ "text": "also" })],
),
turns(vec![serde_json::json!({ "text": "hi" })], vec![resp("a")]),
serde_json::json!({ "contents": [
{ "role": "user", "parts": [{ "text": "go" }] },
{ "role": "model", "parts": [call("a")] }
] }),
serde_json::json!({ "contents": [{ "role": "user", "parts": [{ "text": "hi" }] }], "labels": { "a": "b" } }),
] {
assert!(
has_untranslatable_content(&body),
"must not translate {body}"
);
}
assert!(!has_untranslatable_content(&turns(
vec![call("a")],
vec![resp("a")]
)));
}
#[test]
fn no_contents_is_not_flagged() {
assert!(!has_untranslatable_content(&serde_json::json!({
"generationConfig": { "maxOutputTokens": 128 },
})));
}
#[test]
fn cached_content_is_not_translatable() {
assert!(has_untranslatable_content(&serde_json::json!({
"cachedContent": "projects/p/cachedContents/c",
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
}
#[test]
fn tool_config_is_not_translatable() {
assert!(has_untranslatable_content(&serde_json::json!({
"toolConfig": { "functionCallingConfig": { "mode": "ANY" } },
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
}
#[test]
fn safety_settings_are_not_translatable() {
assert!(has_untranslatable_content(&serde_json::json!({
"safetySettings": [{ "category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_NONE" }],
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
}
#[test]
fn non_function_declaration_tool_is_not_translatable() {
assert!(has_untranslatable_content(&serde_json::json!({
"tools": [{ "googleSearch": {} }],
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
assert!(has_untranslatable_content(&serde_json::json!({
"tools": [{ "functionDeclarations": [{ "name": "f" }], "googleSearch": {} }],
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
assert!(!has_untranslatable_content(&serde_json::json!({
"tools": [{ "functionDeclarations": [{ "name": "f" }] }],
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
}
#[test]
fn system_instruction_with_non_text_part_is_not_translatable() {
assert!(has_untranslatable_content(&serde_json::json!({
"systemInstruction": { "parts": [{ "inlineData": { "mimeType": "image/png", "data": "AA==" } }] },
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
assert!(!has_untranslatable_content(&serde_json::json!({
"systemInstruction": { "parts": [{ "text": "be helpful" }] },
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
}
#[test]
fn unmapped_generation_config_field_is_not_translatable() {
assert!(has_untranslatable_content(&serde_json::json!({
"generationConfig": { "responseMimeType": "application/json" },
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
assert!(has_untranslatable_content(&serde_json::json!({
"generationConfig": { "maxOutputTokens": 512, "topK": 40 },
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
assert!(!has_untranslatable_content(&serde_json::json!({
"generationConfig": { "maxOutputTokens": 512, "temperature": 0.7, "topP": 0.9 },
"contents": [{ "role": "user", "parts": [{ "text": "hi" }] }],
})));
}
#[test]
fn sse_from_response_is_parseable_data_line() {
let resp = serde_json::json!({ "candidates": [{}], "usageMetadata": {} });
let sse = sse_from_response(&resp);
assert!(sse.starts_with("data: "));
assert!(sse.ends_with("\n\n"));
let json_str = sse.strip_prefix("data: ").unwrap().trim_end();
serde_json::from_str::<Value>(json_str).expect("SSE data must be valid JSON");
}
#[test]
fn sse_error_response_has_error_field() {
let sse = sse_error_response("all rungs failed");
let json_str = sse.strip_prefix("data: ").unwrap().trim_end();
let v: Value = serde_json::from_str(json_str).unwrap();
assert_eq!(v["error"]["message"], "all rungs failed");
}
#[test]
fn gemini_key_only_populated_on_gemini_dialect() {
use crate::provider::Auth;
use axum::http::HeaderMap;
use firstpass_core::Dialect;
let mut h = HeaderMap::new();
h.insert("x-goog-api-key", "AIza-google-key".parse().unwrap());
let auth = Auth::from_headers(&h, Dialect::Gemini);
assert_eq!(auth.gemini_key.as_deref(), Some("AIza-google-key"));
assert!(
auth.anthropic_key.is_none(),
"Google key must not go to Anthropic"
);
assert_ne!(
auth.openai_key.as_deref(),
Some("AIza-google-key"),
"Google key must not go to OpenAI path"
);
let auth_ant = Auth::from_headers(&h, Dialect::Anthropic);
assert!(
auth_ant.gemini_key.is_none(),
"Gemini key must not be set on Anthropic path"
);
let auth_oai = Auth::from_headers(&h, Dialect::Openai);
assert!(
auth_oai.gemini_key.is_none(),
"Gemini key must not be set on OpenAI path"
);
}
}