kcode-k1-chat-thread-actions 0.3.0

Synchronous CurrentTime launch for K1 chat threads
Documentation
use serde_json::Value;

const INVALID_CURRENT_TIME_ARGUMENTS: &str = "invalid CurrentTime arguments";
const UNKNOWN_TOOL: &str = "unknown Ktool";

pub fn launch(name: &str, arguments: &str) -> Result<String, String> {
    match name {
        "CurrentTime" if is_empty_object(arguments) => {
            Ok(kcode_k1_ktool_current_time::current_time())
        }
        "CurrentTime" => Err(INVALID_CURRENT_TIME_ARGUMENTS.to_owned()),
        _ => Err(UNKNOWN_TOOL.to_owned()),
    }
}

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;