use serde_json::{json, Value};
#[derive(Debug, Clone, PartialEq)]
pub struct ToolReply {
pub value: Value,
}
impl ToolReply {
pub fn ok(text: impl Into<String>) -> Self {
Self {
value: json!({ "ok": true, "text": text.into() }),
}
}
pub fn ok_json(value: Value) -> Self {
Self { value }
}
pub fn empty() -> Self {
Self { value: Value::Null }
}
pub fn as_value(&self) -> &Value {
&self.value
}
pub fn into_value(self) -> Value {
self.value
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ok_constructs_ok_text_shape() {
let r = ToolReply::ok("hello");
assert_eq!(r.value["ok"], true);
assert_eq!(r.value["text"], "hello");
}
#[test]
fn ok_json_passes_value_through() {
let r = ToolReply::ok_json(json!({"a": 1, "b": "two"}));
assert_eq!(r.value["a"], 1);
assert_eq!(r.value["b"], "two");
}
#[test]
fn empty_returns_null() {
let r = ToolReply::empty();
assert!(r.value.is_null());
}
#[test]
fn as_value_and_into_value_round_trip() {
let r = ToolReply::ok("x");
let snapshot = r.value.clone();
assert_eq!(r.as_value(), &snapshot);
assert_eq!(r.into_value(), snapshot);
}
}