From a2cff20aff444050f43aeff9d86fecabbbeaf2cb Mon Sep 17 00:00:00 2001 From: Andre Cipriani Bandarra Date: Mon, 26 Oct 2026 19:39:36 +0000 Subject: [PATCH] feat: omit None fields on Content, add optional id to function call/response, and add Part helpers (#11) --- src/types/common.rs | 203 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 202 insertions(+), 1 deletion(-) diff --git a/src/types/common.rs b/src/types/common.rs index 6b2c17d..55d748f 100644 --- a/src/types/common.rs +++ b/src/types/common.rs @@ -5,7 +5,9 @@ use serde_json::Value; #[derive(Clone, Default, Debug, Serialize, Deserialize)] pub struct Content { + #[serde(skip_serializing_if = "Option::is_none")] pub role: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub parts: Option>, } @@ -22,6 +24,13 @@ impl Content { pub fn builder() -> ContentBuilder { ContentBuilder::default() } + + pub fn system_prompt(prompt: impl Into) -> Self { + Content { + parts: Some(vec![Part::text(prompt)]), + role: None, + } + } } pub struct SystemPrompt { @@ -139,6 +148,22 @@ impl Part { pub fn function_call(name: impl Into, args: Value) -> Self { Part { function_call: Some(FunctionCallData { + id: None, + name: name.into(), + args, + }), + ..Default::default() + } + } + + pub fn function_call_with_id( + id: impl Into, + name: impl Into, + args: Value, + ) -> Self { + Part { + function_call: Some(FunctionCallData { + id: Some(id.into()), name: name.into(), args, }), @@ -149,6 +174,7 @@ impl Part { pub fn function_response(name: impl Into, response: Value) -> Self { Part { function_response: Some(FunctionResponseData { + id: None, name: name.into(), response, parts: vec![], @@ -156,16 +182,67 @@ impl Part { ..Default::default() } } + + pub fn function_response_with_id( + id: impl Into, + name: impl Into, + response: Value, + ) -> Self { + Part { + function_response: Some(FunctionResponseData { + id: Some(id.into()), + name: name.into(), + response, + parts: vec![], + }), + ..Default::default() + } + } + + pub fn with_thought_signature(mut self, signature: impl Into) -> Self { + self.thought_signature = Some(signature.into()); + self + } + + pub fn with_id(mut self, id: impl Into) -> Self { + let id = id.into(); + if let Some(ref mut call) = self.function_call { + call.id = Some(id.clone()); + } + if let Some(ref mut response) = self.function_response { + response.id = Some(id); + } + self + } + + pub fn add_function_response_part(mut self, part: FunctionResponsePart) -> Self { + if let Some(ref mut response) = self.function_response { + response.parts.push(part); + } + self + } + + pub fn add_function_response_inline_data( + self, + mime_type: impl Into, + data: impl Into, + ) -> Self { + self.add_function_response_part(FunctionResponsePart::inline_data(mime_type, data)) + } } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct FunctionCallData { + #[serde(skip_serializing_if = "Option::is_none")] + pub id: Option, pub name: String, pub args: Value, } #[derive(Clone, Debug, Serialize, Deserialize)] pub struct FunctionResponseData { + #[serde(skip_serializing_if = "Option::is_none")] + pub id: Option, pub name: String, pub response: Value, /// Media returned by the function (multimodal function responses), sent as @@ -180,6 +257,17 @@ pub struct FunctionResponsePart { pub inline_data: InlineData, } +impl FunctionResponsePart { + pub fn inline_data(mime_type: impl Into, data: impl Into) -> Self { + Self { + inline_data: InlineData { + mime_type: mime_type.into(), + data: data.into(), + }, + } + } +} + #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct InlineData { @@ -198,7 +286,120 @@ pub struct FileData { mod tests { use serde_json::json; - use super::{FunctionResponsePart, InlineData, Part}; + use super::{Content, FunctionResponsePart, InlineData, Part, Role}; + + #[test] + fn content_omits_unset_role_and_parts() { + let system = Content::system_prompt("You are a helpful assistant."); + assert_eq!( + serde_json::to_value(&system).unwrap(), + json!({ + "parts": [{"text": "You are a helpful assistant."}] + }) + ); + + let role_only = Content { + role: Some(Role::User), + parts: None, + }; + assert_eq!( + serde_json::to_value(&role_only).unwrap(), + json!({"role": "user"}) + ); + + let empty = Content::default(); + assert_eq!(serde_json::to_value(&empty).unwrap(), json!({})); + + let roundtrip: Content = serde_json::from_str(r#"{"parts": [{"text": "Hello"}]}"#).unwrap(); + assert!(roundtrip.role.is_none()); + assert_eq!(roundtrip.get_text(), Some("Hello".to_string())); + } + + #[test] + fn function_call_id_handling() { + let without_id = Part::function_call("lookup", json!({"q": "test"})); + assert_eq!( + serde_json::to_value(&without_id).unwrap(), + json!({"functionCall": {"name": "lookup", "args": {"q": "test"}}}) + ); + + let with_id = Part::function_call("lookup", json!({"q": "test"})).with_id("call-1"); + assert_eq!( + serde_json::to_value(&with_id).unwrap(), + json!({"functionCall": {"id": "call-1", "name": "lookup", "args": {"q": "test"}}}) + ); + + let with_id_ctor = Part::function_call_with_id("call-2", "lookup", json!({"q": "test"})); + assert_eq!( + serde_json::to_value(&with_id_ctor).unwrap(), + json!({"functionCall": {"id": "call-2", "name": "lookup", "args": {"q": "test"}}}) + ); + + let parsed: Part = serde_json::from_value(json!({ + "functionCall": {"id": "call-3", "name": "lookup", "args": {}} + })) + .unwrap(); + assert_eq!( + parsed.function_call.as_ref().unwrap().id.as_deref(), + Some("call-3") + ); + } + + #[test] + fn function_response_id_handling() { + let without_id = Part::function_response("lookup", json!({"output": "ok"})); + assert_eq!( + serde_json::to_value(&without_id).unwrap(), + json!({"functionResponse": {"name": "lookup", "response": {"output": "ok"}}}) + ); + + let with_id = Part::function_response("lookup", json!({"output": "ok"})).with_id("call-1"); + assert_eq!( + serde_json::to_value(&with_id).unwrap(), + json!({"functionResponse": {"id": "call-1", "name": "lookup", "response": {"output": "ok"}}}) + ); + + let with_id_ctor = + Part::function_response_with_id("call-2", "lookup", json!({"output": "ok"})); + assert_eq!( + serde_json::to_value(&with_id_ctor).unwrap(), + json!({"functionResponse": {"id": "call-2", "name": "lookup", "response": {"output": "ok"}}}) + ); + + let parsed: Part = serde_json::from_value(json!({ + "functionResponse": {"id": "call-3", "name": "lookup", "response": {}} + })) + .unwrap(); + assert_eq!( + parsed.function_response.as_ref().unwrap().id.as_deref(), + Some("call-3") + ); + } + + #[test] + fn part_builder_helpers() { + let part_sig = Part::function_call("fetch", json!({})).with_thought_signature("sig-abc"); + assert_eq!( + serde_json::to_value(&part_sig).unwrap(), + json!({ + "functionCall": {"name": "fetch", "args": {}}, + "thoughtSignature": "sig-abc" + }) + ); + + let part_media = Part::function_response("capture", json!({})) + .add_function_response_inline_data("image/jpeg", "base64data"); + assert_eq!( + serde_json::to_value(&part_media).unwrap(), + json!({ + "functionResponse": { + "name": "capture", + "response": {}, + "parts": [{"inlineData": {"mimeType": "image/jpeg", "data": "base64data"}}] + } + }) + ); + } #[test] fn function_response_omits_empty_parts() { -- 2.49.1