kcode_k1_chat_thread_actions/
lib.rs1use kcode_k1_chat_chatend::{DispatchCall, DispatchOutcome};
2use serde_json::Value;
3
4const INVALID_CURRENT_TIME_ARGUMENTS: &str = "invalid CurrentTime arguments";
5const UNKNOWN_TOOL: &str = "unknown Ktool";
6
7pub fn dispatch(calls: &[DispatchCall]) -> Vec<DispatchOutcome> {
8 calls.iter().map(dispatch_call).collect()
9}
10
11fn dispatch_call(call: &DispatchCall) -> DispatchOutcome {
12 let result = match call.name.as_str() {
13 "CurrentTime" if is_empty_object(&call.arguments) => {
14 Ok(kcode_k1_ktool_current_time::current_time())
15 }
16 "CurrentTime" => Err(INVALID_CURRENT_TIME_ARGUMENTS.to_owned()),
17 _ => Err(UNKNOWN_TOOL.to_owned()),
18 };
19 DispatchOutcome::Terminal(result)
20}
21
22fn is_empty_object(arguments: &str) -> bool {
23 matches!(
24 serde_json::from_str::<Value>(arguments),
25 Ok(Value::Object(fields)) if fields.is_empty()
26 )
27}
28
29#[cfg(test)]
30mod tests;