yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
tool_schema_builder.cc
Go to the documentation of this file.
2
3#include <fstream>
4
5#include "absl/status/status.h"
6#include "absl/strings/str_cat.h"
7#include "nlohmann/json.hpp"
9
10namespace yaze::cli {
11
12namespace {
13
14nlohmann::json BuildFunctionDeclaration(const ToolSpecification& spec) {
15 nlohmann::json function;
16 function["name"] = spec.name;
17 function["description"] = spec.description;
18 if (!spec.usage_notes.empty()) {
19 function["description"] = spec.description + " " + spec.usage_notes;
20 }
21
22 nlohmann::json parameters;
23 parameters["type"] = "object";
24
25 nlohmann::json properties = nlohmann::json::object();
26 nlohmann::json required = nlohmann::json::array();
27
28 for (const auto& arg : spec.arguments) {
29 nlohmann::json arg_schema;
30 arg_schema["type"] = "string";
31 arg_schema["description"] = arg.description;
32 if (!arg.example.empty()) {
33 arg_schema["example"] = arg.example;
34 }
35 properties[arg.name] = std::move(arg_schema);
36
37 if (arg.required) {
38 required.push_back(arg.name);
39 }
40 }
41
42 parameters["properties"] = std::move(properties);
43 if (!required.empty()) {
44 parameters["required"] = std::move(required);
45 }
46
47 function["parameters"] = std::move(parameters);
48 return function;
49}
50
51absl::StatusOr<nlohmann::json> ExtractFunctionDeclaration(
52 const nlohmann::json& schema_json) {
53 if (!schema_json.is_object()) {
54 return absl::InvalidArgumentError(
55 "Tool schema entries must be JSON objects");
56 }
57
58 if (schema_json.contains("function")) {
59 const auto& function = schema_json["function"];
60 if (!function.is_object()) {
61 return absl::InvalidArgumentError(
62 "OpenAI tool schema must contain an object-valued function field");
63 }
64 return function;
65 }
66
67 return schema_json;
68}
69
70} // namespace
71
73 const std::vector<ToolSpecification>& tool_specs) {
74 nlohmann::json functions = nlohmann::json::array();
75 for (const auto& spec : tool_specs) {
76 functions.push_back(BuildFunctionDeclaration(spec));
77 }
78 return functions;
79}
80
82 const PromptBuilder& prompt_builder) {
83 nlohmann::json function_declarations =
84 BuildFunctionDeclarations(prompt_builder.tool_specs());
85 if (!function_declarations.empty()) {
86 return function_declarations;
87 }
88
90}
91
93 const nlohmann::json& schema_json) {
94 if (schema_json.is_object() &&
95 schema_json.contains("function_declarations")) {
96 const auto& functions = schema_json["function_declarations"];
97 if (!functions.is_array()) {
98 return absl::InvalidArgumentError(
99 "function_declarations must be a JSON array");
100 }
101 return functions;
102 }
103
104 if (schema_json.is_array()) {
105 nlohmann::json functions = nlohmann::json::array();
106 for (const auto& entry : schema_json) {
107 auto function_or = ExtractFunctionDeclaration(entry);
108 if (!function_or.ok()) {
109 return function_or.status();
110 }
111 functions.push_back(std::move(function_or).value());
112 }
113 return functions;
114 }
115
116 auto function_or = ExtractFunctionDeclaration(schema_json);
117 if (!function_or.ok()) {
118 return function_or.status();
119 }
120
121 nlohmann::json functions = nlohmann::json::array();
122 functions.push_back(std::move(function_or).value());
123 return functions;
124}
125
126absl::StatusOr<nlohmann::json> ToolSchemaBuilder::ParseFunctionDeclarations(
127 std::string_view schema_json) {
128 if (schema_json.empty()) {
129 return nlohmann::json::array();
130 }
131
132 try {
133 return NormalizeFunctionDeclarations(nlohmann::json::parse(schema_json));
134 } catch (const nlohmann::json::exception& e) {
135 return absl::InvalidArgumentError(
136 absl::StrCat("Failed to parse tool schema JSON: ", e.what()));
137 }
138}
139
140absl::StatusOr<nlohmann::json>
142 const std::string& relative_path) {
143 auto schema_path_or = util::PlatformPaths::FindAsset(relative_path);
144 if (!schema_path_or.ok()) {
145 return schema_path_or.status();
146 }
147
148 std::ifstream file(schema_path_or->string());
149 if (!file.is_open()) {
150 return absl::NotFoundError(absl::StrCat(
151 "Failed to open tool schema asset: ", schema_path_or->string()));
152 }
153
154 try {
155 nlohmann::json schema_json;
156 file >> schema_json;
157 return NormalizeFunctionDeclarations(schema_json);
158 } catch (const nlohmann::json::exception& e) {
159 return absl::InvalidArgumentError(
160 absl::StrCat("Failed to parse tool schema asset: ", e.what()));
161 }
162}
163
165 const nlohmann::json& function_declarations) {
166 nlohmann::json tools = nlohmann::json::array();
167 for (const auto& function : function_declarations) {
168 tools.push_back({{"type", "function"}, {"function", function}});
169 }
170 return tools;
171}
172
174 const nlohmann::json& function_declarations) {
175 if (function_declarations.empty()) {
176 return nlohmann::json::array();
177 }
178
179 return nlohmann::json::array(
180 {{{"function_declarations", function_declarations}}});
181}
182
184 const nlohmann::json& function_declarations) {
185 nlohmann::json tools = nlohmann::json::array();
186 for (const auto& function : function_declarations) {
187 tools.push_back({{"name", function.value("name", "")},
188 {"description", function.value("description", "")},
189 {"input_schema",
190 function.value("parameters", nlohmann::json::object())}});
191 }
192 return tools;
193}
194
195} // namespace yaze::cli
const std::vector< ToolSpecification > & tool_specs() const
static nlohmann::json BuildGeminiTools(const nlohmann::json &function_declarations)
static nlohmann::json BuildFunctionDeclarations(const std::vector< ToolSpecification > &tool_specs)
static nlohmann::json BuildAnthropicTools(const nlohmann::json &function_declarations)
static absl::StatusOr< nlohmann::json > LoadFunctionDeclarationsFromAsset(const std::string &relative_path="agent/function_schemas.json")
static absl::StatusOr< nlohmann::json > ResolveFunctionDeclarations(const PromptBuilder &prompt_builder)
static nlohmann::json BuildOpenAITools(const nlohmann::json &function_declarations)
static absl::StatusOr< nlohmann::json > ParseFunctionDeclarations(std::string_view schema_json)
static absl::StatusOr< nlohmann::json > NormalizeFunctionDeclarations(const nlohmann::json &schema_json)
static absl::StatusOr< std::filesystem::path > FindAsset(const std::string &relative_path)
Find an asset file in multiple standard locations.
absl::StatusOr< nlohmann::json > ExtractFunctionDeclaration(const nlohmann::json &schema_json)
nlohmann::json BuildFunctionDeclaration(const ToolSpecification &spec)
Namespace for the command line interface.
std::vector< ToolArgument > arguments