Compare commits
1 Commits
01a4300bb3
...
feat/funct
| Author | SHA1 | Date | |
|---|---|---|---|
| 2f15b5298d |
@@ -151,6 +151,8 @@ impl Part {
|
|||||||
id: None,
|
id: None,
|
||||||
name: name.into(),
|
name: name.into(),
|
||||||
args,
|
args,
|
||||||
|
partial_args: None,
|
||||||
|
will_continue: None,
|
||||||
}),
|
}),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
@@ -166,6 +168,8 @@ impl Part {
|
|||||||
id: Some(id.into()),
|
id: Some(id.into()),
|
||||||
name: name.into(),
|
name: name.into(),
|
||||||
args,
|
args,
|
||||||
|
partial_args: None,
|
||||||
|
will_continue: None,
|
||||||
}),
|
}),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
@@ -229,14 +233,35 @@ impl Part {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
self.add_function_response_part(FunctionResponsePart::inline_data(mime_type, data))
|
self.add_function_response_part(FunctionResponsePart::inline_data(mime_type, data))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_will_continue(mut self, will_continue: bool) -> Self {
|
||||||
|
if let Some(ref mut call) = self.function_call {
|
||||||
|
call.will_continue = Some(will_continue);
|
||||||
|
}
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
pub fn with_partial_args(mut self, partial_args: Vec<Value>) -> Self {
|
||||||
|
if let Some(ref mut call) = self.function_call {
|
||||||
|
call.partial_args = Some(partial_args);
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct FunctionCallData {
|
pub struct FunctionCallData {
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub id: Option<String>,
|
pub id: Option<String>,
|
||||||
|
#[serde(default, skip_serializing_if = "String::is_empty")]
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
#[serde(default, skip_serializing_if = "Value::is_null")]
|
||||||
pub args: Value,
|
pub args: Value,
|
||||||
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
|
pub partial_args: Option<Vec<Value>>,
|
||||||
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||||
|
pub will_continue: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
@@ -345,6 +370,57 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn function_call_omits_unset_name_and_args_and_parses_missing_fields() {
|
||||||
|
// Deserializing without args (e.g. zero-argument function call)
|
||||||
|
let without_args: Part =
|
||||||
|
serde_json::from_value(json!({"functionCall": {"name": "f"}})).unwrap();
|
||||||
|
let fc = without_args.function_call.unwrap();
|
||||||
|
assert_eq!(fc.name, "f");
|
||||||
|
assert!(fc.args.is_null());
|
||||||
|
assert!(fc.partial_args.is_none());
|
||||||
|
assert!(fc.will_continue.is_none());
|
||||||
|
|
||||||
|
// Serializing when args is null omits args
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_value(&Part {
|
||||||
|
function_call: Some(fc),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
json!({"functionCall": {"name": "f"}})
|
||||||
|
);
|
||||||
|
|
||||||
|
// Deserializing partial chunk without name and args
|
||||||
|
let partial_chunk: Part = serde_json::from_value(json!({
|
||||||
|
"functionCall": {
|
||||||
|
"partialArgs": [{"key": "val"}],
|
||||||
|
"willContinue": true
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
.unwrap();
|
||||||
|
let fc_partial = partial_chunk.function_call.unwrap();
|
||||||
|
assert_eq!(fc_partial.name, "");
|
||||||
|
assert!(fc_partial.args.is_null());
|
||||||
|
assert_eq!(fc_partial.partial_args, Some(vec![json!({"key": "val"})]));
|
||||||
|
assert_eq!(fc_partial.will_continue, Some(true));
|
||||||
|
|
||||||
|
// Serializing partial chunk omits empty name and null args
|
||||||
|
assert_eq!(
|
||||||
|
serde_json::to_value(&Part {
|
||||||
|
function_call: Some(fc_partial),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.unwrap(),
|
||||||
|
json!({
|
||||||
|
"functionCall": {
|
||||||
|
"partialArgs": [{"key": "val"}],
|
||||||
|
"willContinue": true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn function_response_id_handling() {
|
fn function_response_id_handling() {
|
||||||
let without_id = Part::function_response("lookup", json!({"output": "ok"}));
|
let without_id = Part::function_response("lookup", json!({"output": "ok"}));
|
||||||
|
|||||||
Reference in New Issue
Block a user