192 lines
6.8 KiB
C++
192 lines
6.8 KiB
C++
// ADK: the strict YAML subset and Agent Config import / export in YAML.
|
|||
|
|
#include "framework.h"
|
||
|
|
#include "google/adk/caps.h"
|
||
|
|
#include "google/adk/error.h"
|
||
|
|
#include "google/adk/studio.h"
|
||
|
|
#include "google/adk/yaml.h"
|
||
|
|
|
||
|
|
using namespace molo::google::adk;
|
||
|
|
using molo::clients::body::Json;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
bool rejects(const std::string& text) {
|
||
|
|
try {
|
||
|
|
yaml::parse(text);
|
||
|
|
} catch (const AdkError& e) {
|
||
|
|
return e.code() == "invalid_argument";
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
TEST(adk_yaml, parses_maps_lists_scalars_and_block_strings) {
|
||
|
|
const char* doc =
|
||
|
|
"---\n"
|
||
|
|
"# comment\n"
|
||
|
|
"agent_class: LlmAgent # trailing\n"
|
||
|
|
"name: root\n"
|
||
|
|
"count: 3\n"
|
||
|
|
"ratio: 0.5\n"
|
||
|
|
"on: true\n"
|
||
|
|
"nothing: null\n"
|
||
|
|
"quoted: \"a: b\\n\\u00e9\"\n"
|
||
|
|
"single: 'it''s'\n"
|
||
|
|
"url: http://x.test/a#b\n"
|
||
|
|
"empty_list: []\n"
|
||
|
|
"empty_map: {}\n"
|
||
|
|
"instruction: |\n"
|
||
|
|
" line one\n"
|
||
|
|
"\n"
|
||
|
|
" indented\n"
|
||
|
|
" line three\n"
|
||
|
|
"folded: >-\n"
|
||
|
|
" a\n"
|
||
|
|
" b\n"
|
||
|
|
"\n"
|
||
|
|
" c\n"
|
||
|
|
"tools:\n"
|
||
|
|
" - name: lookup\n"
|
||
|
|
" args: {}\n"
|
||
|
|
" - name: other\n"
|
||
|
|
"sub_agents:\n"
|
||
|
|
"- name: kid\n"
|
||
|
|
" list:\n"
|
||
|
|
" - - x\n"
|
||
|
|
" - y\n"
|
||
|
|
" - 2\n";
|
||
|
|
Json v = yaml::parse(doc);
|
||
|
|
ASSERT_EQ(v.find("agent_class")->as_string(), "LlmAgent");
|
||
|
|
ASSERT_EQ(v.find("count")->as_int(), 3);
|
||
|
|
ASSERT_TRUE(v.find("on")->as_bool());
|
||
|
|
ASSERT_TRUE(v.find("nothing")->is_null());
|
||
|
|
ASSERT_EQ(v.find("quoted")->as_string(), "a: b\n\xC3\xA9");
|
||
|
|
ASSERT_EQ(v.find("single")->as_string(), "it's");
|
||
|
|
ASSERT_EQ(v.find("url")->as_string(), "http://x.test/a#b");
|
||
|
|
ASSERT_TRUE(v.find("empty_list")->as_array().empty());
|
||
|
|
ASSERT_EQ(v.find("instruction")->as_string(), "line one\n\n indented\nline three\n");
|
||
|
|
ASSERT_EQ(v.find("folded")->as_string(), "a b\nc");
|
||
|
|
ASSERT_EQ(v.find("tools")->as_array().size(), 2u);
|
||
|
|
const Json& kid = v.find("sub_agents")->as_array()[0];
|
||
|
|
ASSERT_EQ(kid.find("list")->as_array()[0].as_array()[1].as_string(), "y");
|
||
|
|
ASSERT_EQ(kid.find("list")->as_array()[1].as_int(), 2);
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(adk_yaml, rejects_unsafe_and_unsupported_input_loudly) {
|
||
|
|
ASSERT_TRUE(rejects("a: &x 1\n")); // anchor
|
||
|
|
ASSERT_TRUE(rejects("a: *x\n")); // alias
|
||
|
|
ASSERT_TRUE(rejects("a: !!str 1\n")); // tag
|
||
|
|
ASSERT_TRUE(rejects("a: [1, 2]\n")); // flow sequence
|
||
|
|
ASSERT_TRUE(rejects("a: {b: 1}\n")); // flow map
|
||
|
|
ASSERT_TRUE(rejects("? a\n: b\n")); // complex key
|
||
|
|
ASSERT_TRUE(rejects("<<: x\n")); // merge key
|
||
|
|
ASSERT_TRUE(rejects("a: 1\na: 2\n")); // duplicate key
|
||
|
|
ASSERT_TRUE(rejects("a:\n\tb: 1\n")); // tab indentation
|
||
|
|
ASSERT_TRUE(rejects("a: 1\n---\nb: 2\n")); // second document
|
||
|
|
ASSERT_TRUE(rejects("%YAML 1.2\na: 1\n")); // directive
|
||
|
|
ASSERT_TRUE(rejects("a: |2\n x\n")); // indentation indicator
|
||
|
|
ASSERT_TRUE(rejects("a: 'open\n")); // unterminated
|
||
|
|
ASSERT_TRUE(rejects("a: b: c\n")); // nested map on one line
|
||
|
|
ASSERT_TRUE(rejects("a: 1\n b: 2\n")); // bad indentation
|
||
|
|
ASSERT_TRUE(rejects(std::string("a: \x01\n")));
|
||
|
|
std::string deep;
|
||
|
|
for (int i = 0; i < AdkCaps::kMaxYamlDepth + 2; ++i)
|
||
|
|
deep += std::string(static_cast<std::size_t>(i) * 2, ' ') + "k:\n";
|
||
|
|
ASSERT_TRUE(rejects(deep));
|
||
|
|
std::string many;
|
||
|
|
for (std::size_t i = 0; i <= AdkCaps::kMaxYamlLines; ++i) many += "# x\n";
|
||
|
|
ASSERT_TRUE(rejects(many));
|
||
|
|
ASSERT_TRUE(rejects(std::string(AdkCaps::kMaxDefinitionBytes + 1, ' ')));
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(adk_yaml, dump_then_parse_is_identity) {
|
||
|
|
Json o = Json::object();
|
||
|
|
o.set("plain", Json("hello"));
|
||
|
|
o.set("looks_null", Json("null"));
|
||
|
|
o.set("looks_num", Json("12"));
|
||
|
|
o.set("colon", Json("a: b"));
|
||
|
|
o.set("hash", Json("a #b"));
|
||
|
|
o.set("lead", Json("- x"));
|
||
|
|
o.set("empty", Json(""));
|
||
|
|
o.set("quote", Json("say \"hi\"\t\\"));
|
||
|
|
o.set("multi", Json("l1\n l2\n\nl4\n"));
|
||
|
|
o.set("multi_strip", Json("l1\nl2"));
|
||
|
|
o.set("multi_keep", Json("l1\n\n\n"));
|
||
|
|
o.set("spaced_first", Json(" x\ny"));
|
||
|
|
o.set("num", Json(2.5));
|
||
|
|
o.set("int", Json(7));
|
||
|
|
o.set("t", Json(true));
|
||
|
|
o.set("n", Json());
|
||
|
|
o.set("e1", Json::array());
|
||
|
|
o.set("e2", Json::object());
|
||
|
|
Json nested = Json::array();
|
||
|
|
Json item = Json::object();
|
||
|
|
item.set("a", Json("x"));
|
||
|
|
Json inner = Json::array();
|
||
|
|
inner.push(Json("p"));
|
||
|
|
inner.push(Json::object());
|
||
|
|
item.set("b", inner);
|
||
|
|
nested.push(item);
|
||
|
|
Json arr_in_arr = Json::array();
|
||
|
|
arr_in_arr.push(Json(1));
|
||
|
|
arr_in_arr.push(Json("two"));
|
||
|
|
nested.push(arr_in_arr);
|
||
|
|
nested.push(Json("s"));
|
||
|
|
o.set("nested", nested);
|
||
|
|
o.set("key: odd", Json("v"));
|
||
|
|
std::string text = yaml::dump(o);
|
||
|
|
Json back = yaml::parse(text);
|
||
|
|
ASSERT_TRUE(o.equals(back));
|
||
|
|
ASSERT_EQ(yaml::dump(back), text);
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(adk_studio_yaml, imports_adk_agent_config_yaml_and_round_trips) {
|
||
|
|
ToolRegistry reg;
|
||
|
|
const char* doc =
|
||
|
|
"name: coordinator\n"
|
||
|
|
"model: gemini-3.5-flash\n"
|
||
|
|
"description: routes\n"
|
||
|
|
"instruction: |\n"
|
||
|
|
" Route the request.\n"
|
||
|
|
" Be brief.\n"
|
||
|
|
"tools:\n"
|
||
|
|
" - name: exit_loop\n"
|
||
|
|
"sub_agents:\n"
|
||
|
|
" - agent_class: google.adk.agents.LoopAgent\n"
|
||
|
|
" name: refine\n"
|
||
|
|
" max_iterations: 3\n"
|
||
|
|
" sub_agents:\n"
|
||
|
|
" - name: writer\n"
|
||
|
|
" instruction: Write.\n";
|
||
|
|
auto agent = AgentDefinition::from_text(doc, reg);
|
||
|
|
ASSERT_EQ(agent->name(), "coordinator");
|
||
|
|
ASSERT_TRUE(agent->find("writer") != nullptr);
|
||
|
|
std::string out = AgentDefinition::to_yaml(*agent);
|
||
|
|
auto again = AgentDefinition::from_text(out, reg);
|
||
|
|
ASSERT_EQ(AgentDefinition::to_yaml(*again), out);
|
||
|
|
ASSERT_EQ(AgentDefinition::to_json(*again).dump(), AgentDefinition::to_json(*agent).dump());
|
||
|
|
// JSON and YAML carry the same document.
|
||
|
|
auto from_json = AgentDefinition::from_text(AgentDefinition::to_json(*agent).dump(), reg);
|
||
|
|
ASSERT_EQ(AgentDefinition::to_yaml(*from_json), out);
|
||
|
|
}
|
||
|
|
|
||
|
|
TEST(adk_studio_yaml, rejects_fields_the_schema_or_runtime_does_not_have) {
|
||
|
|
ToolRegistry reg;
|
||
|
|
auto bad = [&](const std::string& t) {
|
||
|
|
try {
|
||
|
|
AgentDefinition::from_text(t, reg);
|
||
|
|
} catch (const AdkError&) {
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
};
|
||
|
|
ASSERT_TRUE(bad("name: a\ninstruction: x\nbogus: 1\n"));
|
||
|
|
ASSERT_TRUE(bad("name: a\ninstruction: x\nbefore_model_callbacks:\n - name: cb\n"));
|
||
|
|
ASSERT_TRUE(bad("name: a\n")); // instruction is required for LlmAgent
|
||
|
|
ASSERT_TRUE(bad("name: a\ninstruction: x\ntools:\n - name: exit_loop\n args:\n k: 1\n"));
|
||
|
|
ASSERT_TRUE(bad("name: a\ninstruction: x\nsub_agents:\n - config_path: ./b.yaml\n"));
|
||
|
|
ASSERT_TRUE(bad("name: a\ninstruction: &i x\n"));
|
||
|
|
ASSERT_TRUE(bad("agent_class: LoopAgent\nname: l\nmax_iterations: 99\n"));
|
||
|
|
ASSERT_TRUE(bad("agent_class: LoopAgent\nname: l\nmax_iterations: 1.5\n"));
|
||
|
|
ASSERT_TRUE(bad("agent_class: LoopAgent\nname: l\ninstruction: not allowed on a workflow agent\n"));
|
||
|
|
}
|