DirectModel over GoogleClient with thought-signature replay, discovery check script, live verified against the real model (function calling, transfer, MCP). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
90 lines
3.0 KiB
C++
90 lines
3.0 KiB
C++
#include "google/adk/tool.h"
|
|
|
|
#include "google/adk/caps.h"
|
|
#include "google/adk/error.h"
|
|
|
|
namespace molo::google::adk {
|
|
|
|
using clients::body::Json;
|
|
|
|
bool valid_identifier(const std::string& name) {
|
|
if (name.empty() || name.size() > AdkCaps::kMaxNameChars) return false;
|
|
for (std::size_t i = 0; i < name.size(); ++i) {
|
|
char c = name[i];
|
|
bool alpha = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_';
|
|
bool digit = c >= '0' && c <= '9';
|
|
if (!(alpha || (digit && i > 0))) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
FunctionTool::FunctionTool(std::string name, std::string description,
|
|
Json schema, Fn fn)
|
|
: name_(std::move(name)),
|
|
description_(std::move(description)),
|
|
schema_(std::move(schema)),
|
|
fn_(std::move(fn)) {
|
|
if (!valid_identifier(name_))
|
|
throw AdkError::invalid("tool name must match [A-Za-z_][A-Za-z0-9_]*");
|
|
if (!fn_) throw AdkError::invalid("tool function is empty");
|
|
if (!schema_.is_null() && !schema_.is_object())
|
|
throw AdkError::invalid("tool schema must be an object or null");
|
|
}
|
|
|
|
FunctionDeclaration FunctionTool::declaration() const {
|
|
FunctionDeclaration d;
|
|
d.name = name_;
|
|
d.description = description_;
|
|
d.parameters = schema_;
|
|
return d;
|
|
}
|
|
|
|
namespace {
|
|
bool type_matches(const std::string& type, const Json& v) {
|
|
if (type == "string") return v.is_string();
|
|
if (type == "number") return v.is_number();
|
|
if (type == "integer")
|
|
return v.is_number() && v.as_number() == static_cast<double>(v.as_int());
|
|
if (type == "boolean") return v.is_bool();
|
|
if (type == "object") return v.is_object();
|
|
if (type == "array") return v.is_array();
|
|
return true; // Unknown type names are not enforced.
|
|
}
|
|
} // namespace
|
|
|
|
void FunctionTool::validate(const Json& schema, const Json& args) {
|
|
if (schema.is_null()) return;
|
|
if (!args.is_object()) throw AdkError::invalid("arguments must be an object");
|
|
if (const Json* req = schema.find("required"); req && req->is_array())
|
|
for (const Json& k : req->as_array())
|
|
if (k.is_string() && !args.has(k.as_string()))
|
|
throw AdkError::invalid("missing required argument: " + k.as_string());
|
|
const Json* props = schema.find("properties");
|
|
if (!props || !props->is_object()) return;
|
|
for (const auto& [key, def] : props->as_object()) {
|
|
const Json* v = args.find(key);
|
|
if (!v) continue;
|
|
const Json* t = def.find("type");
|
|
if (t && t->is_string() && !type_matches(t->as_string(), *v))
|
|
throw AdkError::invalid("argument " + key + " must be " + t->as_string());
|
|
}
|
|
}
|
|
|
|
Json FunctionTool::call(const Json& args, ToolContext& context) {
|
|
validate(schema_, args);
|
|
return fn_(args, context);
|
|
}
|
|
|
|
std::shared_ptr<Tool> make_exit_loop_tool() {
|
|
return std::make_shared<FunctionTool>(
|
|
"exit_loop", "Call when the task is done to stop the surrounding loop.",
|
|
Json(), [](const Json&, ToolContext& ctx) {
|
|
ctx.escalate = true;
|
|
Json out = Json::object();
|
|
out.set("ok", Json(true));
|
|
return out;
|
|
});
|
|
}
|
|
|
|
} // namespace molo::google::adk
|