30 std::ifstream
file(path);
31 if (!
file.is_open()) {
34 std::ostringstream
ss;
56 const std::filesystem::path& path) {
58 if (content.empty()) {
59 logger->warn(
"Empty or unreadable grammar file: {}", path.string());
64 ge.
key = path.stem().string();
65 ge.gbnf_content = std::move(content);
66 ge.source =
"bundled";
70 logger->warn(
"Bundled grammar '{}' has validation errors: {}",
87 logger->warn(
"Grammar directory not found: {}",
94 if (
entry.path().extension() !=
".gbnf") {
101 std::string key =
ge->key;
102 std::lock_guard<std::mutex> lock(registry_mutex_);
103 grammars_[key] = std::move(*
ge);
107 logger->info(
"Loaded {} bundled grammar(s)",
count);
123 const std::string& key,
124 const std::string& gbnf_content,
125 const std::string& source)
127 std::lock_guard<std::mutex> lock(registry_mutex_);
128 if (grammars_.count(key) > 0) {
129 logger->warn(
"Grammar key '{}' already registered", key);
136 ge.gbnf_content = gbnf_content;
141 grammars_[key] = std::move(
ge);
142 logger->info(
"Registered grammar '{}' (source={}, valid={})",
143 key, source, grammars_[key].validated);
157 const std::string& key,
158 const std::filesystem::path& path)
161 if (content.empty()) {
162 logger->error(
"Cannot read grammar file: {}", path.string());
167 ? path.stem().string() : key;
179 std::lock_guard<std::mutex> lock(registry_mutex_);
180 auto it = grammars_.find(key);
181 if (
it == grammars_.end()) {
185 logger->info(
"Deregistered grammar '{}'", key);
197 std::lock_guard<std::mutex> lock(registry_mutex_);
198 auto it = grammars_.find(key);
199 if (
it == grammars_.end()) {
202 return it->second.gbnf_content;
213 std::lock_guard<std::mutex> lock(registry_mutex_);
214 return grammars_.count(key) > 0;
225 std::lock_guard<std::mutex> lock(registry_mutex_);
226 auto it = grammars_.find(key);
227 if (
it == grammars_.end()) {
240 std::lock_guard<std::mutex> lock(registry_mutex_);
241 std::vector<GrammarEntry> result;
242 result.reserve(grammars_.size());
243 for (
const auto& [
k,
e] : grammars_) {
246 meta.source =
e.source;
247 meta.validated =
e.validated;
248 meta.error =
e.error;
249 result.push_back(std::move(
meta));
264 if (gbnf_content.empty()) {
265 return "empty grammar string";
272 nullptr, gbnf_content.c_str(),
"root");
274 if (sampler ==
nullptr) {
275 return "GBNF parse failed";
289 std::lock_guard<std::mutex> lock(registry_mutex_);
290 return grammars_.size();
299 std::lock_guard<std::mutex> lock(registry_mutex_);
301 logger->info(
"Cleared all grammars");
size_t load_bundled(const std::filesystem::path &grammar_dir)
Load all bundled grammars from a directory.
size_t size() const
Number of registered grammars.
static std::string validate(const std::string &gbnf_content)
Validate a GBNF grammar string.
GrammarEntry entry(const std::string &key) const
Get full entry metadata for a grammar key.
bool deregister(const std::string &key)
Remove a grammar from the registry.
bool has(const std::string &key) const
Check if a grammar key exists in the registry.
std::vector< GrammarEntry > list() const
List all registered grammar keys.
void clear()
Remove all registered grammars.
bool register_from_file(const std::string &key, const std::filesystem::path &path)
Register a grammar from a file path.
std::string get(const std::string &key) const
Get GBNF content string for a grammar key.
bool register_grammar(const std::string &key, const std::string &gbnf_content, const std::string &source="runtime")
Register a grammar by key with GBNF content string.
GrammarRegistry — named grammar management and validation.
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Activate model on GPU (WARM → ACTIVE).
@ error
Tool server returned an error payload.
@ optional
optional: every escape asks the host's path approver
static std::optional< GrammarEntry > build_grammar_entry(const std::filesystem::path &path)
Load all bundled grammars from a directory.
@ count
Sentinel — MUST remain last.
Metadata for a registered grammar.
std::string key
Unique registry key (e.g., "compactor", "chess_executor")