46 if (!is_valid_point(point)) {
50 auto idx =
static_cast<size_t>(point);
51 std::unique_lock lock(mutexes_[idx]);
53 HookEntry entry{callback, user_data, priority};
54 auto& vec = entries_[idx];
57 auto pos = std::lower_bound(
58 vec.begin(), vec.end(), entry,
60 return a.priority < b.priority;
62 vec.insert(pos, entry);
64 logger->info(
"Registered hook: point={} priority={} total={}",
65 static_cast<int>(point), priority, vec.size());
82 if (!is_valid_point(point)) {
86 auto idx =
static_cast<size_t>(point);
87 std::unique_lock lock(mutexes_[idx]);
89 auto& vec = entries_[idx];
90 auto it = std::find_if(vec.begin(), vec.end(),
91 [callback, user_data](
const HookEntry& e) {
92 return e.callback == callback
93 && e.user_data == user_data;
96 if (it != vec.end()) {
98 logger->info(
"Deregistered hook: point={}",
static_cast<int>(point));
110std::vector<HookEntry> HookRegistry::snapshot(
112 auto idx =
static_cast<size_t>(point);
113 std::shared_lock lock(mutexes_[idx]);
114 return entries_[idx];
128 const char* context_json,
132 if (!is_valid_point(point)) {
136 auto entries = snapshot(point);
137 logger->info(
"fire_pre: point={}, {} handler(s)",
138 static_cast<int>(point), entries.size());
139 char* accumulated =
nullptr;
142 for (
const auto& entry : entries) {
143 const char* input = accumulated ? accumulated : context_json;
144 char* modified =
nullptr;
148 rc = entry.callback(point, input, &modified, entry.user_data);
149 }
catch (
const std::exception& e) {
150 logger->warn(
"Pre-hook threw for point={}: {}",
151 static_cast<int>(point), e.what());
154 logger->warn(
"Pre-hook threw unknown exception for point={}",
155 static_cast<int>(point));
162 accumulated =
nullptr;
167 if (modified !=
nullptr) {
169 accumulated = modified;
173 *out_json = accumulated;
187 const char* context_json,
191 if (!is_valid_point(point)) {
195 auto entries = snapshot(point);
196 if (entries.empty()) {
200 char* accumulated =
nullptr;
202 for (
const auto& entry : entries) {
203 const char* input = accumulated ? accumulated : context_json;
204 char* modified =
nullptr;
207 entry.callback(point, input, &modified, entry.user_data);
208 }
catch (
const std::exception& e) {
209 logger->warn(
"Post-hook threw for point={}: {}",
210 static_cast<int>(point), e.what());
213 logger->warn(
"Post-hook threw unknown exception for point={}",
214 static_cast<int>(point));
218 if (modified !=
nullptr) {
220 accumulated = modified;
224 *out_json = accumulated;
236 const char* context_json) {
237 if (!is_valid_point(point)) {
241 auto entries = snapshot(point);
243 for (
const auto& entry : entries) {
244 char* modified =
nullptr;
246 entry.callback(point, context_json, &modified, entry.user_data);
247 }
catch (
const std::exception& e) {
248 logger->warn(
"Info hook threw for point={}: {}",
249 static_cast<int>(point), e.what());
251 logger->warn(
"Info hook threw unknown exception for point={}",
252 static_cast<int>(point));
267 if (!is_valid_point(point)) {
270 auto idx =
static_cast<size_t>(point);
271 std::shared_lock lock(mutexes_[idx]);
272 return entries_[idx].size();
entropic_error_t register_hook(entropic_hook_point_t point, entropic_hook_callback_t callback, void *user_data, int priority)
Register a hook callback at a hook point.
size_t hook_count(entropic_hook_point_t point) const
Get the number of registered hooks for a point.
int fire_pre(entropic_hook_point_t point, const char *context_json, char **out_json)
Fire pre-hooks.
void fire_post(entropic_hook_point_t point, const char *context_json, char **out_json)
Fire post-hooks.
entropic_error_t deregister_hook(entropic_hook_point_t point, entropic_hook_callback_t callback, void *user_data)
Deregister a hook callback.
void fire_info(entropic_hook_point_t point, const char *context_json)
Fire informational hooks (no modify, no cancel).
entropic_error_t
Error codes returned by all C API functions.
@ ENTROPIC_ERROR_INVALID_CONFIG
Config validation failed (missing fields, bad values)
Thread-safe hook registration and dispatch.
int(* entropic_hook_callback_t)(entropic_hook_point_t hook_point, const char *context_json, char **modified_json, void *user_data)
Hook callback function type.
entropic_hook_point_t
Hook points in the engine lifecycle.
@ ENTROPIC_HOOK_COUNT_
Sentinel — not a valid hook point.
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).
A single registered hook entry.