feat/function-response-parts #1
@@ -151,6 +151,7 @@ impl Part {
|
||||
function_response: Some(FunctionResponseData {
|
||||
name: name.into(),
|
||||
response,
|
||||
parts: vec![],
|
||||
}),
|
||||
..Default::default()
|
||||
}
|
||||
@@ -167,6 +168,16 @@ pub struct FunctionCallData {
|
||||
pub struct FunctionResponseData {
|
||||
pub name: String,
|
||||
pub response: Value,
|
||||
/// Media returned by the function (multimodal function responses), sent as
|
||||
/// `functionResponse.parts`. Omitted when empty.
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub parts: Vec<FunctionResponsePart>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct FunctionResponsePart {
|
||||
pub inline_data: InlineData,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
@@ -182,3 +193,46 @@ pub struct FileData {
|
||||
pub mime_type: String,
|
||||
pub file_uri: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::{FunctionResponsePart, InlineData, Part};
|
||||
|
||||
#[test]
|
||||
fn function_response_omits_empty_parts() {
|
||||
let part = Part::function_response("lookup", json!({"output": "ok"}));
|
||||
assert_eq!(
|
||||
serde_json::to_value(&part).unwrap(),
|
||||
json!({"functionResponse": {"name": "lookup", "response": {"output": "ok"}}})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_response_serializes_inline_data_parts() {
|
||||
let mut part = Part::function_response("screenshot", json!({}));
|
||||
part.function_response.as_mut().unwrap().parts = vec![FunctionResponsePart {
|
||||
inline_data: InlineData {
|
||||
mime_type: "image/png".to_string(),
|
||||
data: "aGVsbG8=".to_string(),
|
||||
},
|
||||
}];
|
||||
assert_eq!(
|
||||
serde_json::to_value(&part).unwrap(),
|
||||
json!({"functionResponse": {
|
||||
"name": "screenshot",
|
||||
"response": {},
|
||||
"parts": [{"inlineData": {"mimeType": "image/png", "data": "aGVsbG8="}}]
|
||||
}})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn function_response_without_parts_deserializes() {
|
||||
let part: Part =
|
||||
serde_json::from_value(json!({"functionResponse": {"name": "lookup", "response": {}}}))
|
||||
.unwrap();
|
||||
assert!(part.function_response.unwrap().parts.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user