yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
tool_registry.cc
Go to the documentation of this file.
2
3#include <string>
4#include <vector>
5
6#include "absl/container/flat_hash_set.h"
7#include "absl/strings/str_cat.h"
10
11namespace yaze {
12namespace cli {
13namespace agent {
14
15namespace {
16
17std::vector<std::string> InferRequiredArgsFromUsage(
18 const std::vector<UsageParameter>& parameters) {
19 std::vector<std::string> required_args;
20 for (const UsageParameter& parameter : parameters) {
21 if (!parameter.positional && parameter.required && parameter.takes_value) {
22 required_args.push_back(parameter.name);
23 }
24 }
25 return required_args;
26}
27
28std::vector<std::string> InferFlagArgsFromUsage(
29 const std::vector<UsageParameter>& parameters) {
30 std::vector<std::string> flag_args;
31 for (const UsageParameter& parameter : parameters) {
32 if (!parameter.positional && !parameter.takes_value) {
33 flag_args.push_back(parameter.name);
34 }
35 }
36 return flag_args;
37}
38
39ToolAccess InferToolAccess(const std::string& tool_name) {
40 static const absl::flat_hash_set<std::string> kMutatingTools = {
41 "build-compile",
42 "build-configure",
43 "build-test",
44 "dungeon-import-custom-collision-json",
45 "dungeon-import-water-fill-json",
46 "dungeon-set-palette-color",
47 "dungeon-set-room-property",
48 "emulator-clear-breakpoint",
49 "emulator-pause",
50 "emulator-reset",
51 "emulator-run",
52 "emulator-set-breakpoint",
53 "emulator-step",
54 "emulator-write-memory",
55 "gui-click",
56 "gui-place-tile",
57 "gui-type",
58 "message-source-sync",
59 "overworld-set-tile",
60 "project-export",
61 "project-import",
62 "project-restore",
63 "project-snapshot",
64 "tools-extract-golden",
65 "tools-patch-v3",
66 };
67
68 return kMutatingTools.contains(tool_name) ? ToolAccess::kMutating
70}
71
73 if (def.access == ToolAccess::kReadOnly) {
74 def.access = InferToolAccess(def.name);
75 }
76 const std::vector<UsageParameter> parameters =
78 if (def.required_args.empty()) {
80 }
81 if (def.flag_args.empty()) {
82 def.flag_args = InferFlagArgsFromUsage(parameters);
83 }
84 return def;
85}
86
87} // namespace
88
90 static ToolRegistry instance;
91 instance.EnsureInitialized();
92 return instance;
93}
94
96 std::call_once(init_once_, [this]() {
97#ifndef __EMSCRIPTEN__
98 // The builtin tool set (tool_registration.cc) pulls in the full CLI
99 // command-handler layer, which is intentionally excluded from the minimal
100 // WASM agent library (see cli/agent.cmake). Skipping it here keeps the
101 // Emscripten link from requiring RegisterBuiltinAgentTools/tool_registration.cc.
103#else
104 (void)this;
105#endif
106 });
107}
108
110 HandlerFactory factory) {
111 ToolDefinition normalized = NormalizeDefinition(def);
112 const std::string tool_name = normalized.name;
113 tools_[tool_name] = {std::move(normalized), std::move(factory)};
114}
115
116std::vector<ToolDefinition> ToolRegistry::GetAllTools() const {
117 std::vector<ToolDefinition> defs;
118 defs.reserve(tools_.size());
119 for (const auto& [name, entry] : tools_) {
120 defs.push_back(entry.def);
121 }
122 return defs;
123}
124
125std::optional<ToolDefinition> ToolRegistry::GetToolDefinition(
126 const std::string& name) const {
127 auto it = tools_.find(name);
128 if (it != tools_.end()) {
129 return it->second.def;
130 }
131 return std::nullopt;
132}
133
134std::vector<ToolDefinition> ToolRegistry::GetToolsByCategory(
135 const std::string& category) const {
136 std::vector<ToolDefinition> defs;
137 for (const auto& [name, entry] : tools_) {
138 if (entry.def.category == category) {
139 defs.push_back(entry.def);
140 }
141 }
142 return defs;
143}
144
145absl::StatusOr<std::unique_ptr<resources::CommandHandler>>
146ToolRegistry::CreateHandler(const std::string& tool_name) const {
147 auto it = tools_.find(tool_name);
148 if (it == tools_.end()) {
149 return absl::NotFoundError(absl::StrCat("Tool not found: ", tool_name));
150 }
151 return it->second.factory();
152}
153
154} // namespace agent
155} // namespace cli
156} // namespace yaze
Centralized registry for all agent tools.
void RegisterTool(const ToolDefinition &def, HandlerFactory factory)
std::function< std::unique_ptr< resources::CommandHandler >()> HandlerFactory
std::optional< ToolDefinition > GetToolDefinition(const std::string &name) const
std::map< std::string, ToolEntry > tools_
static ToolRegistry & Get()
std::vector< ToolDefinition > GetToolsByCategory(const std::string &category) const
std::vector< ToolDefinition > GetAllTools() const
absl::StatusOr< std::unique_ptr< resources::CommandHandler > > CreateHandler(const std::string &tool_name) const
std::vector< std::string > InferRequiredArgsFromUsage(const std::vector< UsageParameter > &parameters)
ToolDefinition NormalizeDefinition(ToolDefinition def)
ToolAccess InferToolAccess(const std::string &tool_name)
std::vector< std::string > InferFlagArgsFromUsage(const std::vector< UsageParameter > &parameters)
void RegisterBuiltinAgentTools(ToolRegistry &registry)
std::vector< UsageParameter > ParseUsageParameters(absl::string_view usage)
Metadata describing a tool for the LLM.
std::vector< std::string > required_args
std::vector< std::string > flag_args