29std::string read_file_contents(
const std::filesystem::path& path) {
30 std::ifstream file(path);
31 if (!file.is_open()) {
34 std::ostringstream ss;
56 const std::filesystem::path& path) {
57 std::string content = read_file_contents(path);
58 if (content.empty()) {
59 logger->warn(
"Empty or unreadable grammar file: {}", path.string());
64 ge.
key = path.stem().string();
70 logger->warn(
"Bundled grammar '{}' has validation errors: {}",
82 const std::filesystem::path& grammar_dir)
84 if (!std::filesystem::is_directory(grammar_dir)) {
85 logger->warn(
"Grammar directory not found: {}",
86 grammar_dir.string());
91 for (
const auto&
entry : std::filesystem::directory_iterator(grammar_dir)) {
92 if (
entry.path().extension() !=
".gbnf") {
99 std::string key = ge->key;
100 std::lock_guard<std::mutex> lock(registry_mutex_);
101 grammars_[key] = std::move(*ge);
105 logger->info(
"Loaded {} bundled grammar(s)", count);
119 const std::string& key,
120 const std::string& gbnf_content,
121 const std::string& source)
123 std::lock_guard<std::mutex> lock(registry_mutex_);
124 if (grammars_.count(key) > 0) {
125 logger->warn(
"Grammar key '{}' already registered", key);
137 grammars_[key] = std::move(ge);
138 logger->info(
"Registered grammar '{}' (source={}, valid={})",
139 key, source, grammars_[key].validated);
152 const std::string& key,
153 const std::filesystem::path& path)
155 std::string content = read_file_contents(path);
156 if (content.empty()) {
157 logger->error(
"Cannot read grammar file: {}", path.string());
161 std::string resolved_key = key.empty()
162 ? path.stem().string() : key;
174 std::lock_guard<std::mutex> lock(registry_mutex_);
175 auto it = grammars_.find(key);
176 if (it == grammars_.end()) {
180 logger->info(
"Deregistered grammar '{}'", key);
192 std::lock_guard<std::mutex> lock(registry_mutex_);
193 auto it = grammars_.find(key);
194 if (it == grammars_.end()) {
197 return it->second.gbnf_content;
208 std::lock_guard<std::mutex> lock(registry_mutex_);
209 return grammars_.count(key) > 0;
220 std::lock_guard<std::mutex> lock(registry_mutex_);
221 auto it = grammars_.find(key);
222 if (it == grammars_.end()) {
235 std::lock_guard<std::mutex> lock(registry_mutex_);
236 std::vector<GrammarEntry> result;
237 result.reserve(grammars_.size());
238 for (
const auto& [k, e] : grammars_) {
243 meta.
error = e.error;
244 result.push_back(std::move(meta));
257 if (gbnf_content.empty()) {
258 return "empty grammar string";
264 llama_sampler* sampler = llama_sampler_init_grammar(
265 nullptr, gbnf_content.c_str(),
"root");
267 if (sampler ==
nullptr) {
268 return "GBNF parse failed";
271 llama_sampler_free(sampler);
282 std::lock_guard<std::mutex> lock(registry_mutex_);
283 return grammars_.size();
292 std::lock_guard<std::mutex> lock(registry_mutex_);
294 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.
static std::optional< GrammarEntry > build_grammar_entry(const std::filesystem::path &path)
Load all bundled grammars from a directory.
Metadata for a registered grammar.
std::string source
Origin: "bundled", "file", "runtime", "dynamic".
std::string key
Unique registry key (e.g., "compactor", "chess_executor")
bool validated
true if grammar has passed validation
std::string error
Non-empty if validation failed.
std::string gbnf_content
Raw GBNF grammar string.