use kcode_k1_chat_chatend::{DispatchCall, DispatchOutcome};
use serde_json::Value;
const INVALID_CURRENT_TIME_ARGUMENTS: &str = "invalid CurrentTime arguments";
const UNKNOWN_TOOL: &str = "unknown Ktool";
pub fn dispatch(calls: &[DispatchCall]) -> Vec<DispatchOutcome> {
calls.iter().map(dispatch_call).collect()
}
fn dispatch_call(call: &DispatchCall) -> DispatchOutcome {
let result = match call.name.as_str() {
"CurrentTime" if is_empty_object(&call.arguments) => {
Ok(kcode_k1_ktool_current_time::current_time())
}
"CurrentTime" => Err(INVALID_CURRENT_TIME_ARGUMENTS.to_owned()),
_ => Err(UNKNOWN_TOOL.to_owned()),
};
DispatchOutcome::Terminal(result)
}
fn is_empty_object(arguments: &str) -> bool {
matches!(
serde_json::from_str::<Value>(arguments),
Ok(Value::Object(fields)) if fields.is_empty()
)
}
#[cfg(test)]
mod tests;