47 if (!src) {
return nullptr; }
48 size_t len = std::strlen(src) + 1;
50 if (dst) { std::memcpy(dst, src, len); }
74static thread_local std::string s_last_error_cache;
75static thread_local char s_pre_create_error[512] =
"";
91 if (!handle) {
return s_pre_create_error; }
94 return s_last_error_cache.c_str();
156 auto* backend = h->
orchestrator->get_backend(tier_name);
158 throw std::runtime_error(
159 "no backend for tier: " + std::string(tier_name));
161 if (!backend->is_active()) {
162 throw std::runtime_error(
163 "model not active: " + std::string(tier_name));
182 if (handle ==
nullptr) {
185 entropic::log::init(spdlog::level::info);
189 if (engine ==
nullptr) {
198 static std::atomic<int> s_log_id_counter{0};
199 engine->
log_id = ++s_log_id_counter;
201 s_log->info(
"entropic_create() — v{} (log_id={})",
202 CONFIG_ENTROPIC_VERSION_STRING, engine->log_id);
255 return cached->second;
260 if (cfg && !cfg->allowed_tools.empty()) {
261 return cfg->allowed_tools;
277 const nlohmann::json& all_tools,
278 const std::vector<std::string>& allowed) {
279 std::vector<std::string> result;
280 for (
const auto& tool : all_tools) {
281 std::string name = tool.value(
"name",
"");
282 bool pass = allowed.empty()
283 || std::find(allowed.begin(), allowed.end(), name)
285 if (pass) { result.push_back(tool.dump()); }
304 if (!h || !h->server_manager || !h->orchestrator) {
return 1; }
306 std::string tier_name = tier ? tier :
"";
307 auto all_json = h->server_manager->list_tools();
308 auto all_tools = nlohmann::json::parse(all_json,
nullptr,
false);
311 auto* adapter = h->orchestrator->get_adapter(tier_name);
313 bool ok = all_tools.is_array() && !all_tools.empty()
314 && !tool_jsons.empty() && adapter !=
nullptr;
315 if (!ok) {
return 1; }
317 *result = strdup(adapter->format_tools(tool_jsons).c_str());
334 h->
engine->set_external_interrupt(
355 h->
engine->set_stream_observer(
374 h->
engine->set_queue_observer(
393 h->
engine->set_state_observer(
452 const std::filesystem::path& data_dir,
453 const std::string& shared_prefix) {
457 auto parsed = entropic::prompts::resolve_tier_identity_full(
458 tier, name, data_dir);
459 info.system_prompt = shared_prefix + parsed.body;
460 info.explicit_completion = !tier.auto_chain.has_value();
464 info.max_iterations_override =
465 parsed.frontmatter.max_iterations;
466 info.max_tool_calls_per_turn_override =
467 parsed.frontmatter.max_tool_calls_per_turn;
468 h->
engine->set_tier_info(name, info);
493 const char* parent_id,
const char* delegating_tier,
494 const char* target_tier,
const char* task,
int max_turns,
495 std::string& delegation_id, std::string& child_conversation_id,
498 if (sb ==
nullptr) {
return false; }
500 parent_id ? parent_id :
"",
501 delegating_tier ? delegating_tier :
"",
502 target_tier ? target_tier :
"",
504 max_turns, delegation_id, child_conversation_id);
520 const char* title, std::string& conversation_id,
523 if (sb ==
nullptr) {
return false; }
525 title ? title :
"session", std::nullopt, std::nullopt);
526 return !conversation_id.empty();
535 const char* delegation_id,
const char* status,
536 const char* summary,
void* user_data) {
538 if (sb ==
nullptr || delegation_id ==
nullptr) {
return false; }
539 std::optional<std::string> sum;
540 if (summary !=
nullptr) { sum = summary; }
542 status ? status :
"completed", sum);
551 const char* conversation_id,
const char* messages_json,
554 if (sb ==
nullptr || conversation_id ==
nullptr
555 || messages_json ==
nullptr) {
567 const char* conversation_id,
const char* messages_json,
570 if (sb ==
nullptr || conversation_id ==
nullptr
571 || messages_json ==
nullptr) {
588 const char* delegation_id, std::string& result_json,
591 bool ok = sb !=
nullptr && delegation_id !=
nullptr;
592 std::string del_json;
593 nlohmann::json del, conv;
594 std::string child_id, target, conv_json;
596 ok = sb->get_delegation_by_id(delegation_id, del_json);
599 del = nlohmann::json::parse(del_json,
nullptr,
false);
600 ok = del.is_object();
603 child_id = del.value(
"child_conversation_id", std::string{});
604 target = del.value(
"target_tier", std::string{});
605 ok = !child_id.empty() && !target.empty()
606 && sb->load_conversation(child_id, conv_json);
609 conv = nlohmann::json::parse(conv_json,
nullptr,
false);
610 ok = conv.is_object();
613 conv[
"target_tier"] = target;
614 conv[
"delegation_id"] = del.value(
"id", std::string{});
615 result_json = conv.dump();
654 h->
storage = std::make_unique<entropic::SqliteStorageBackend>(db_path);
655 if (h->
storage->initialize()) {
656 s_log->info(
"storage: {}", db_path.string());
658 s_log->warn(
"storage init failed, continuing without persistence");
690 std::unordered_set<std::string> targets;
692 for (
const auto& t : dests) { targets.insert(t); }
694 if (targets.empty()) {
695 for (
const auto& [name, tier] : config.
models.
tiers) {
697 targets.insert(name);
701 return {targets.begin(), targets.end()};
712 const std::filesystem::path& data_dir) {
714 ? std::filesystem::current_path()
720 h->
config.
mcp, tier_names, data_dir.string());
733 const std::filesystem::path& data_dir) {
734 std::string constitution, app_ctx;
735 entropic::prompts::load_constitution(
737 data_dir, constitution);
738 entropic::prompts::load_app_context(
742 if (!constitution.empty()) { prefix += constitution +
"\n\n"; }
743 if (!app_ctx.empty()) { prefix += app_ctx +
"\n\n"; }
764 const std::string& name,
773 h->
engine->set_relay_single_delegate(name);
786 const std::filesystem::path& data_dir) {
788 std::filesystem::path id_path;
789 if (tier.identity.has_value()) {
790 id_path = tier.identity.value();
791 }
else if (!tier.identity_disabled) {
792 id_path = data_dir /
"prompts" / (
"identity_" + name +
".md");
794 if (id_path.empty() || !std::filesystem::exists(id_path)) {
798 if (entropic::prompts::load_identity(id_path,
id).empty()) {
823 if (exec ==
nullptr) {
return nullptr; }
825 if (s.empty() || s ==
"[]") {
return nullptr; }
826 auto* out =
static_cast<char*
>(std::malloc(s.size() + 1));
827 if (out !=
nullptr) {
828 std::memcpy(out, s.data(), s.size());
829 out[s.size()] =
'\0';
850 h->
engine->build_directive_hooks());
853 const std::vector<entropic::ToolCall>& calls,
854 void* ud) -> std::vector<entropic::Message> {
856 ->process_tool_calls(ctx, calls);
860 tei.
free_fn = [](
char* p) { std::free(p); };
861 h->
engine->set_tool_executor(tei);
879 if (h ==
nullptr || h->validator ==
nullptr) {
return nullptr; }
885 v[
"verdict"] =
"passed";
break;
887 v[
"verdict"] =
"revised";
break;
889 v[
"verdict"] =
"rejected_reverted_length";
break;
891 v[
"verdict"] =
"rejected_max_revisions";
break;
893 v[
"verdict"] =
"skipped";
break;
895 v[
"verdict"] =
"paused_pending_consumer";
break;
897 v[
"verdict"] =
"passed_consumer_override";
break;
899 v[
"revisions_applied"] = r.revision_count;
903 v[
"attempt_n"] = r.attempt_n;
904 nlohmann::json violations = nlohmann::json::array();
905 for (
const auto& vi : r.final_critique.violations) {
906 violations.push_back({
908 {
"rule_id", vi.rule},
909 {
"rule_text", vi.rule},
910 {
"excerpt", vi.excerpt},
911 {
"quote", vi.excerpt},
912 {
"explanation", vi.explanation},
913 {
"severity",
"error"},
916 v[
"violations"] = violations;
917 return strdup(v.dump().c_str());
937 entropic::InferenceInterface& iface,
938 const std::string& constitution_text) {
939 entropic::HookInterface hook_iface;
942 const char* json,
char** out) ->
int {
944 ->fire_pre(pt, json, out);
947 const char* json,
char** out) {
949 ->fire_post(pt, json, out);
955 h->
engine->set_hooks(hook_iface);
965 && !constitution_text.empty()) {
966 h->
validator = std::make_unique<entropic::ConstitutionalValidator>(
968 h->
validator->attach(&hook_iface, &iface);
972 s_log->info(
"Constitutional validator attached (max_revisions={})",
988 j[
"log_level"] = h->config.log_level;
989 j[
"log_dir"] = h->config.log_dir.string();
990 j[
"ggml_logging"] = h->config.ggml_logging;
991 return strdup(j.dump().c_str());
1009 auto data_dir = entropic::config::resolve_data_dir(h->
config);
1010 std::string constitution, app_ctx;
1011 entropic::prompts::load_constitution(
1013 data_dir, constitution);
1014 entropic::prompts::load_app_context(
1017 std::string identity_body;
1020 identity_body = entropic::prompts::resolve_tier_identity(
1021 it->second, tier_name, data_dir);
1024 if (!constitution.empty()) { out += constitution +
"\n\n"; }
1025 if (!app_ctx.empty()) { out += app_ctx +
"\n\n"; }
1026 if (!identity_body.empty()) { out += identity_body; }
1032 tier_name.c_str(), &tp,
1034 if (rc == 0 && tp !=
nullptr) {
1062 nlohmann::json arr = nlohmann::json::array();
1063 for (
const auto& [name, _] : h->config.models.tiers) {
1064 nlohmann::json entry;
1065 entry[
"name"] = name;
1066 entry[
"assembled_prompt"] =
1068 arr.push_back(std::move(entry));
1070 return strdup(arr.dump().c_str());
1080 if (!h->server_manager) {
return strdup(
"[]"); }
1081 return strdup(h->server_manager->list_tools().c_str());
1104 if (!h || !h->engine) {
return strdup(
"[]"); }
1105 const auto& msgs = h->engine->get_messages();
1106 nlohmann::json arr = nlohmann::json::array();
1109 &&
static_cast<int>(msgs.size()) > max_entries) {
1110 start =
static_cast<int>(msgs.size()) - max_entries;
1112 for (
int i = start; i < static_cast<int>(msgs.size()); ++i) {
1113 const auto& m = msgs[
static_cast<size_t>(i)];
1114 std::string preview = m.content.size() > 200
1115 ? entropic::facade::utf8_safe_substr(m.content, 200) +
"..."
1119 {
"content_preview", preview},
1120 {
"token_count_est", m.content.size() / 4u}
1123 return strdup(arr.dump().c_str());
1141 if (!h || !h->orchestrator) {
1142 return strdup(
"{\"vram_total_bytes\":0,\"vram_budget_bytes\":0,"
1143 "\"vram_headroom_bytes\":0,\"backend\":\"unknown\","
1144 "\"residency\":[]}");
1146 return strdup(h->orchestrator->residency_snapshot_json().c_str());
1157 j[
"engine_state"] = h->
configured.load() ?
"configured" :
"init";
1158 j[
"default_tier"] = h->config.models.default_tier;
1160 nlohmann::json tiers = nlohmann::json::array();
1161 for (
const auto& [name, _] : h->config.models.tiers) {
1162 tiers.push_back(name);
1164 j[
"active_tiers"] = tiers;
1166 if (h->server_manager) {
1167 j[
"working_dir"] = h->server_manager->project_dir().string();
1168 j[
"registered_servers"] = h->server_manager->server_names();
1170 j[
"data_dir"] = entropic::config::resolve_data_dir(
1171 h->config).string();
1172 j[
"log_dir"] = h->config.log_dir.string();
1173 return strdup(j.dump().c_str());
1190 if (!h || !h->engine) {
return strdup(
"{}"); }
1191 auto m = h->engine->last_loop_metrics();
1193 j[
"iterations"] = m.iterations;
1194 j[
"tool_calls"] = m.tool_calls;
1195 j[
"tokens_used"] = m.tokens_used;
1196 j[
"errors"] = m.errors;
1197 j[
"duration_ms"] = m.duration_ms();
1199 nlohmann::json per_tier = nlohmann::json::object();
1200 for (
auto& [tier, tm] : h->engine->per_tier_metrics()) {
1202 {
"iterations", tm.iterations},
1203 {
"tool_calls", tm.tool_calls},
1204 {
"tokens_used", tm.tokens_used},
1205 {
"errors", tm.errors},
1206 {
"duration_ms", tm.duration_ms()},
1209 j[
"per_tier"] = per_tier;
1210 return strdup(j.dump().c_str());
1234 const char* query,
int max_results,
void* ud) {
1236 if (h ==
nullptr || !h->storage || query ==
nullptr) {
1240 if (!h->storage->search_delegations(query, max_results, out)) {
1243 return strdup(out.c_str());
1256 const char* delegation_id,
void* ud) {
1258 if (h ==
nullptr || !h->storage || delegation_id ==
nullptr) {
1263 delegation_id, out, h->storage.get())) {
1266 return strdup(out.c_str());
1284 if (es ==
nullptr) {
return; }
1300 es->set_state_provider(sp);
1301 s_log->info(
"State provider wired to entropic server");
1313 h->
validator->set_tier_rules(name, rules);
1349 ? std::filesystem::current_path()
1354 s_log->warn(
"External MCP bridge failed to start");
1406 h->
orchestrator = std::make_unique<entropic::ModelOrchestrator>();
1408 h->
last_error =
"orchestrator initialization failed";
1415 h->
orchestrator->load_grammars_from(data_dir /
"grammars");
1434 h->
mcp_auth = std::make_unique<entropic::MCPAuthorizationManager>();
1455 h->
engine = std::make_unique<entropic::AgentEngine>(
1485 h->
engine->set_system_prompt(
1486 entropic::prompts::assemble(h->
config, data_dir));
1505 auto data_dir = entropic::config::resolve_data_dir(h->
config);
1515 s_log->info(
"configure complete");
1528 const char* config_json) {
1529 if (!handle || !config_json) {
1538 auto err = entropic::config::load_config_from_string(
1542 s_log->error(
"configure: {}", err);
1558 const char* config_path) {
1559 if (!handle || !config_path) {
1568 auto err = entropic::config::load_config_from_file(
1572 s_log->error(
"configure_from_file: {}", err);
1586 entropic::log::register_handle_log(
1607 const char* project_dir) {
1615 if (project_dir && project_dir[0] !=
'\0') {
1616 entropic::log::setup_session(project_dir);
1620 entropic::log::register_handle_log(
1621 handle->
log_id, project_dir);
1626 std::filesystem::path proj_dir = (project_dir && project_dir[0] !=
'\0')
1628 auto err = entropic::config::load_layered(
1629 proj_dir,
"default_config.yaml",
1633 s_log->error(
"configure_dir: {}", err);
1645 handle->
engine->set_project_dir(std::filesystem::absolute(proj_dir));
1662 if (handle ==
nullptr) {
1665 s_log->info(
"entropic_destroy()");
1684 entropic::log::unregister_handle_log(handle->
log_id);
1698 return CONFIG_ENTROPIC_VERSION_STRING;
1717 if (!handle || !handle->
engine) {
return 0; }
1718 return handle->
engine->seconds_since_last_activity();
1729 return malloc(size);
1761 char** result_json) {
1769 auto result = handle->
engine->run_turn(input);
1771 facade_json::serialize_messages(result));
1780 }
catch (
const std::exception& e) {
1788 facade_json::serialize_messages(
1789 handle->
engine->get_messages()));
1791 *result_json =
nullptr;
1816 void (*on_token)(
const char* token,
size_t len,
void* user_data),
1827 int code = handle->
engine->run_streaming(
1828 input, on_token, user_data, cancel_flag);
1834 }
catch (
const std::exception& e) {
1836 s_log->error(
"run_streaming: {}", handle->
last_error);
1862 const char* messages_json,
1894 const char* messages_json,
1895 char** result_json) {
1899 auto result = handle->
engine->run_turn(std::move(msgs));
1901 facade_json::serialize_messages(result));
1926 const char* messages_json,
1927 char** result_json) {
1930 || !messages_json || !result_json || !handle->
engine) {
1935 }
catch (
const std::exception& e) {
1937 s_log->error(
"run_messages: {}", handle->
last_error);
1954 const char* messages_json,
1955 void (*on_token)(
const char* token,
size_t len,
void* user_data),
1961 int code = handle->
engine->run_streaming(
1962 std::move(msgs), on_token, user_data, cancel_flag);
1989 const char* messages_json,
1990 void (*on_token)(
const char* token,
size_t len,
void* user_data),
1995 || !messages_json || !on_token || !handle->
engine) {
2000 handle, messages_json, on_token, user_data, cancel_flag);
2001 }
catch (
const std::exception& e) {
2003 s_log->error(
"run_messages_streaming: {}", handle->
last_error);
2019 void (*observer)(
const char* token,
size_t len,
void* user_data),
2027 handle->
engine->set_stream_observer(observer, user_data);
2043 handle->
validator->set_auto_retry(enabled != 0);
2058 return handle->
validator->resume_retry();
2071 return handle->
validator->accept_last();
2085 handle->
validator->set_attempt_boundary_cb(cb, user_data);
2107 handle->
engine->set_delegation_callbacks(
2108 on_start, on_complete, user_data);
2131 void (*observer)(
int state,
void* user_data),
2135 handle->state_observer_data = user_data;
2143 handle->
engine->set_state_observer(observer, user_data);
2168 void (*start_cb)(
void* user_data),
2169 void (*end_cb)(
void* user_data),
2176 handle->
validator->set_critique_callbacks(
2177 start_cb, end_cb, user_data);
2193 handle->
engine->interrupt();
2216 }
else if (!message) {
2218 }
else if (!handle->
engine || !handle->
engine->is_running()) {
2220 }
else if (!handle->
engine->queue_user_message(message)) {
2236 ? handle->
engine->user_message_queue_depth() : 0;
2249 handle->
engine->clear_user_message_queue();
2267 void (*observer)(
const char*,
size_t,
void*),
2271 handle->queue_observer_data = user_data;
2273 handle->
engine->set_queue_observer(observer, user_data);
2290 handle->
engine->clear_conversation();
2308 facade_json::serialize_messages(handle->
engine->get_messages()));
2324 *count = handle->
engine->message_count();
2344 size_t* tokens_used,
2349 auto [used, max] = handle->
engine->context_usage(
2350 handle->
engine->get_messages());
2351 *tokens_used =
static_cast<size_t>(used);
2352 *capacity =
static_cast<size_t>(max);
2393 const char* adapter_name,
2394 const char* adapter_path,
2395 const char* base_model_path,
2399 if (rc !=
ENTROPIC_OK || !adapter_name || !adapter_path || !base_model_path) {
2406 throw std::runtime_error(
"no tier for model: "
2407 + std::string(base_model_path));
2411 if (!llama || !llama->llama_model_ptr()) {
2412 throw std::runtime_error(
"backend not ready for tier: " + tier);
2414 bool ok = handle->
orchestrator->adapter_manager().load(
2415 adapter_name, adapter_path, llama->llama_model_ptr(), scale);
2417 }
catch (
const std::exception& e) {
2436 const char* adapter_name)
2445 auto info = mgr.info(adapter_name);
2447 throw std::runtime_error(
"adapter not loaded: "
2448 + std::string(adapter_name));
2453 mgr.
unload(adapter_name, llama ? llama->llama_context_ptr() :
nullptr);
2455 }
catch (
const std::exception& e) {
2474 const char* adapter_name)
2485 if (!llama || !llama->llama_context_ptr()) {
2486 throw std::runtime_error(
"no active llama context for swap");
2488 bool ok = handle->
orchestrator->adapter_manager().swap(
2489 adapter_name, llama->llama_context_ptr());
2491 }
catch (
const std::exception& e) {
2509 const char* adapter_name)
2516 auto st = handle->
orchestrator->adapter_manager().state(adapter_name);
2517 return static_cast<int>(st);
2518 }
catch (
const std::exception& e) {
2520 s_log->error(
"adapter_state: {}", handle->
last_error);
2537 const char* adapter_name)
2544 auto ai = handle->
orchestrator->adapter_manager().info(adapter_name);
2546 facade_json::serialize_adapter_info(ai).c_str());
2547 }
catch (
const std::exception& e) {
2549 s_log->error(
"adapter_info: {}", handle->
last_error);
2570 auto adapters = handle->
orchestrator->adapter_manager().list_adapters();
2572 facade_json::serialize_adapter_list(adapters).c_str());
2573 }
catch (
const std::exception& e) {
2575 s_log->error(
"adapter_list: {}", handle->
last_error);
2597 const char* gbnf_content)
2606 .register_grammar(key, gbnf_content);
2607 s_log->info(
"grammar_register: key={} ok={}", key, ok);
2609 }
catch (
const std::exception& e) {
2611 s_log->error(
"grammar_register: {}", handle->
last_error);
2639 .register_from_file(key, path);
2640 s_log->info(
"grammar_register_file: key={} ok={}", key, ok);
2642 }
catch (
const std::exception& e) {
2644 s_log->error(
"grammar_register_file: {}", handle->
last_error);
2670 bool ok = handle->
orchestrator->grammar_registry().deregister(key);
2671 s_log->info(
"grammar_deregister: key={} ok={}", key, ok);
2673 }
catch (
const std::exception& e) {
2675 s_log->error(
"grammar_deregister: {}", handle->
last_error);
2699 auto content = handle->
orchestrator->grammar_registry().get(key);
2700 return content.empty() ? nullptr :
alloc_cstr(content.c_str());
2701 }
catch (
const std::exception& e) {
2703 s_log->error(
"grammar_get: {}", handle->
last_error);
2720 if (!gbnf_content) {
return alloc_cstr(
"null input"); }
2723 return err.empty() ? nullptr :
alloc_cstr(err.c_str());
2724 }
catch (
const std::exception& e) {
2746 auto entries = handle->
orchestrator->grammar_registry().list();
2747 nlohmann::json arr = nlohmann::json::array();
2748 for (
const auto& e : entries) {
2749 arr.push_back({{
"key", e.key},
2750 {
"source", e.source},
2751 {
"validated", e.validated},
2752 {
"error", e.error}});
2755 }
catch (
const std::exception& e) {
2757 s_log->error(
"grammar_list: {}", handle->
last_error);
2778 const char* profile_json)
2786 auto j = nlohmann::json::parse(profile_json);
2788 p.
name = j.value(
"name",
"");
2789 if (p.
name.empty()) {
throw std::invalid_argument(
"missing 'name'"); }
2790 p.
n_batch = j.value(
"n_batch", 512);
2795 .register_profile(p);
2796 s_log->info(
"profile_register: name={} ok={}", p.
name, ok);
2798 }
catch (
const std::exception& e) {
2800 s_log->error(
"profile_register: {}", handle->
last_error);
2826 bool ok = handle->
orchestrator->profile_registry().deregister(name);
2827 s_log->info(
"profile_deregister: name={} ok={}", name, ok);
2829 }
catch (
const std::exception& e) {
2831 s_log->error(
"profile_deregister: {}", handle->
last_error);
2857 auto p = handle->
orchestrator->profile_registry().get(name);
2860 j[
"n_batch"] = p.n_batch;
2861 j[
"n_threads"] = p.n_threads;
2862 j[
"n_threads_batch"] = p.n_threads_batch;
2863 j[
"description"] = p.description;
2865 }
catch (
const std::exception& e) {
2867 s_log->error(
"profile_get: {}", handle->
last_error);
2888 auto names = handle->
orchestrator->profile_registry().list();
2889 nlohmann::json arr = nlohmann::json(names);
2891 }
catch (
const std::exception& e) {
2893 s_log->error(
"profile_list: {}", handle->
last_error);
2914 const char* model_path)
2921 return handle->
orchestrator->throughput_tracker().tok_per_sec();
2922 }
catch (
const std::exception& e) {
2924 s_log->error(
"throughput_tok_per_sec: {}", handle->
last_error);
2942 const char* model_path)
2950 s_log->info(
"throughput_reset: data cleared");
2951 }
catch (
const std::exception& e) {
2953 s_log->error(
"throughput_reset: {}", handle->
last_error);
2968 const char* identity_name,
2969 const char* pattern,
2973 if (rc !=
ENTROPIC_OK || !identity_name || !pattern) {
2977 return handle->
mcp_auth->grant(identity_name, pattern, lvl);
2989 const char* identity_name,
2990 const char* pattern)
2993 if (rc !=
ENTROPIC_OK || !identity_name || !pattern) {
2996 return handle->
mcp_auth->revoke(identity_name, pattern);
3008 const char* identity_name,
3009 const char* tool_name,
3013 || !handle->
mcp_auth || !identity_name || !tool_name) {
3017 return handle->
mcp_auth->check_access(identity_name, tool_name, lvl) ? 1 : 0;
3029 const char* identity_name)
3032 || !handle->
mcp_auth || !identity_name) {
3036 auto keys = handle->
mcp_auth->list_keys(identity_name);
3037 nlohmann::json arr = nlohmann::json::array();
3038 for (
const auto& k : keys) {
3039 arr.push_back({{
"pattern", k.tool_pattern},
3040 {
"level",
static_cast<int>(k.level)}});
3043 }
catch (
const std::exception& e) {
3058 const char* granter,
3059 const char* grantee,
3060 const char* pattern,
3064 if (rc !=
ENTROPIC_OK || !granter || !grantee || !pattern) {
3068 return handle->
mcp_auth->grant_from(granter, grantee, pattern, lvl);
3084 auto json = handle->
mcp_auth->serialize_all();
3086 }
catch (
const std::exception& e) {
3107 bool ok = handle->
mcp_auth->deserialize_all(json);
3122 const char* config_json)
3129 auto j = nlohmann::json::parse(config_json);
3131 cfg.
name = j.value(
"name",
"");
3133 if (j.contains(
"focus") && j[
"focus"].is_array()) {
3134 cfg.
focus = j[
"focus"].get<std::vector<std::string>>();
3138 }
catch (
const std::exception& e) {
3154 const char* config_json)
3161 auto j = nlohmann::json::parse(config_json);
3165 if (j.contains(
"focus") && j[
"focus"].is_array()) {
3166 cfg.
focus = j[
"focus"].get<std::vector<std::string>>();
3170 }
catch (
const std::exception& e) {
3208 if (!cfg) {
throw std::runtime_error(
"identity not found"); }
3210 j[
"name"] = cfg->name;
3211 j[
"system_prompt"] = cfg->system_prompt;
3213 ?
"static" :
"dynamic";
3232 nlohmann::json arr(names);
3275 const char* model_id,
3276 const int32_t* tokens,
3281 if (rc !=
ENTROPIC_OK || !model_id || !tokens || !result || n_tokens < 2) {
3287 auto lr = backend->evaluate_logprobs(tokens, n_tokens);
3292 result->
logprobs =
static_cast<float*
>(
3293 malloc(
sizeof(
float) * lr.logprobs.size()));
3294 std::copy(lr.logprobs.begin(), lr.logprobs.end(),
3296 result->
tokens =
static_cast<int32_t*
>(
3297 malloc(
sizeof(int32_t) * lr.tokens.size()));
3298 std::copy(lr.tokens.begin(), lr.tokens.end(),
3301 }
catch (
const std::exception& e) {
3303 s_log->error(
"get_logprobs: {}", handle->
last_error);
3320 const char* model_id,
3321 const int32_t* tokens,
3326 if (rc !=
ENTROPIC_OK || !model_id || !tokens || !perplexity || n_tokens < 2) {
3332 *perplexity = backend->compute_perplexity(tokens, n_tokens);
3334 }
catch (
const std::exception& e) {
3336 s_log->error(
"compute_perplexity: {}", handle->
last_error);
3352 if (result ==
nullptr) {
3358 result->
tokens =
nullptr;
3376 const char* model_id)
3383 auto* backend = handle->
orchestrator->get_backend(model_id);
3384 return (backend && backend->supports(
3386 }
catch (
const std::exception& e) {
3388 s_log->error(
"model_has_vision: {}", handle->
last_error);
3408 handle->
validator->set_global_enabled(enabled);
3421 const char* identity_name,
3429 handle->
validator->set_identity_validation(identity_name, enabled);
3442 if (!handle || !handle->
validator) {
return nullptr; }
3444 auto result = handle->
validator->last_result();
3446 j[
"content"] = result.content;
3447 j[
"was_revised"] = result.was_revised;
3448 j[
"revision_count"] = result.revision_count;
3466 char** prompt_out) {
3467 if (handle ==
nullptr || prompt_out ==
nullptr) {
3471 static const char* prompt =
3472 "[SYSTEM DIRECTIVE: SELF-DIAGNOSIS]\n\n"
3473 "Analyze your recent actions and identify any issues. "
3474 "Follow these steps:\n\n"
3475 "1. Call entropic.diagnose to get a full engine state "
3477 "2. Review the tool call history for:\n"
3478 " - Repeated failures (same tool, same error)\n"
3479 " - Duplicate tool calls (circuit breaker risk)\n"
3480 " - Tool calls that returned errors\n"
3481 " - Unexpected state (wrong phase, wrong tier)\n"
3482 "3. Review your reasoning for:\n"
3483 " - Actions that didn't achieve the stated goal\n"
3484 " - Unnecessary tool calls\n"
3485 " - Missing context that led to errors\n"
3486 "4. Produce a structured assessment:\n"
3487 " - FINDINGS: What went wrong (be specific)\n"
3488 " - ROOT CAUSE: Why it went wrong\n"
3489 " - RECOMMENDATION: What to do differently\n\n"
3490 "Be honest and specific. The goal is accurate "
3491 "self-assessment, not self-defense.\n";
3519 char** diagnostic) {
3520 if (handle ==
nullptr || compatible ==
nullptr) {
3526 auto info = handle->
orchestrator->check_speculative_compat();
3527 *compatible = info.compatible ? 1 : 0;
3528 if (diagnostic !=
nullptr) {
3529 *diagnostic = info.compatible
3561 if (observer !=
nullptr) {
3562 fn = [observer, user_data](
3564 const std::string& tier_name,
3565 const std::string& model_path,
3575 handle->
orchestrator->set_residency_observer(std::move(fn));
3596 if (handle ==
nullptr || out_json ==
nullptr) {
3601 std::string snapshot =
3604 if (*out_json ==
nullptr) {
static std::string validate(const std::string &gbnf_content)
Validate a GBNF grammar string.
gh#59 (v2.3.1): RAII guard combining api_mutex + log scope.
Thread-safe hook registration and dispatch.
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.
void fire_info(entropic_hook_point_t point, const char *context_json)
Fire informational hooks (no modify, no cancel).
Concrete base class for inference backends (80% logic).
void unload()
Full unload (→ COLD).
LlamaCppBackend — common llama.cpp patterns (15% layer).
Multi-model lifecycle and routing orchestrator.
std::function< void(ResidencyEvent event, const std::string &tier_name, const std::string &model_path, size_t footprint)> ResidencyObserverFn
Residency observer callback type (internal C++ form).
void clear_all_prompt_caches()
Invalidate prompt/KV caches across every pooled backend.
ResidencyEvent
Residency observer event codes — mirror the C ABI enum entropic_residency_event_t exactly (LOADED=0,...
Manages MCP server instances and routes tool calls.
void interrupt_external_tools()
Abort in-flight tool calls across every external MCP client.
SQLite-based storage backend.
bool save_messages(const std::string &conversation_id, const std::string &messages_json)
Save messages to a conversation.
bool complete_delegation(const std::string &delegation_id, const std::string &status, const std::optional< std::string > &result_summary=std::nullopt)
Mark a delegation as completed or failed.
bool create_delegation(const std::string &parent_conversation_id, const std::string &delegating_tier, const std::string &target_tier, const std::string &task, int max_turns, std::string &delegation_id, std::string &child_conversation_id)
Create a delegation record with a child conversation.
bool save_snapshot(const std::string &conversation_id, const std::string &messages_json)
Save a pre-compaction snapshot of full conversation history.
std::string create_conversation(const std::string &title="New Conversation", const std::optional< std::string > &project_path=std::nullopt, const std::optional< std::string > &model_id=std::nullopt)
Create a new conversation.
std::string to_json(size_t count) const
Serialize recent entries to JSON array string.
std::string auto_discover_and_load()
Auto-discover and load bundled_models.yaml.
Private definition of the entropic_engine struct.
static entropic_error_t check_mcp_auth(entropic_handle_t h)
Check handle prerequisites for MCP auth APIs.
static void apply_identity_frontmatter(entropic_handle_t h, const std::string &name, const entropic::prompts::IdentityFrontmatter &fm)
Cache per-tier frontmatter fields (allowed_tools, validation_rules, relay).
entropic_error_t entropic_run_messages(entropic_handle_t handle, const char *messages_json, char **result_json)
Blocking multimodal agentic run (gh#37, v2.1.8).
entropic_error_t entropic_identity_count(entropic_handle_t handle, size_t *total, size_t *dynamic)
Get identity count (total and dynamic).
static bool si_create_delegation(const char *parent_id, const char *delegating_tier, const char *target_tier, const char *task, int max_turns, std::string &delegation_id, std::string &child_conversation_id, void *user_data)
Initialize persistence: storage + session logger.
static entropic_error_t reject_if_configured(entropic_handle_t h)
Post-parse config setup: subsystem construction + wiring.
static std::vector< std::string > resolve_allowed_tools(entropic_engine *h, const std::string &tier)
Post-parse config setup: load bundled models, set configured.
static void start_external_bridge(entropic_handle_t h)
Start the external MCP bridge if enabled in config.
static char * sp_search_delegations(const char *query, int max_results, void *ud)
State provider: search_delegations (gh#32, v2.1.6).
entropic_error_t entropic_grammar_deregister(entropic_handle_t handle, const char *key)
Remove a grammar from the registry.
entropic_error_t entropic_context_usage(entropic_handle_t handle, size_t *tokens_used, size_t *capacity)
Read current context-window pressure (gh#39, v2.1.8).
char * entropic_adapter_list(entropic_handle_t handle)
List all known adapters as a JSON array.
entropic_error_t entropic_validation_resume_retry(entropic_handle_t handle)
Resume a paused constitutional revision pass.
static char * sp_load_delegation_conversation(const char *delegation_id, void *ud)
State provider: load_delegation_conversation (gh#32, v2.1.6).
static entropic_error_t run_messages_stream_inner(entropic_handle_t handle, const char *messages_json, void(*on_token)(const char *token, size_t len, void *user_data), void *user_data, int *cancel_flag)
Streaming multimodal run (gh#37, v2.1.8).
static char * sp_get_metrics(void *ud)
State provider: get_metrics.
static char * sp_get_state(void *ud)
State provider: get_state (runtime environment).
static void rewire_observers(entropic_handle_t h)
Re-bind every pre-configure observer to the new engine.
entropic_error_t entropic_deserialize_mcp_keys(entropic_handle_t handle, const char *json)
Deserialize all identity key sets from JSON.
static char * sp_get_residency(void *ud)
State provider: get_residency — VRAM residency snapshot.
static std::string build_shared_prompt_prefix(entropic_handle_t h, const std::filesystem::path &data_dir)
Build shared system prompt prefix (constitution + app_context).
entropic_error_t entropic_create(entropic_handle_t *handle)
Create a new engine instance.
static void init_mcp_servers(entropic_handle_t h, const std::filesystem::path &data_dir)
Initialize MCP servers with resolved working directory.
entropic_error_t entropic_grammar_register(entropic_handle_t handle, const char *key, const char *gbnf_content)
Register a grammar by key with GBNF content.
entropic_error_t entropic_set_stream_observer(entropic_handle_t handle, void(*observer)(const char *token, size_t len, void *user_data), void *user_data)
Set a global stream observer callback.
entropic_error_t entropic_validation_set_auto_retry(entropic_handle_t handle, int enabled)
Toggle automatic constitutional revision.
entropic_error_t entropic_update_identity(entropic_handle_t handle, const char *name, const char *config_json)
Update an existing dynamic identity.
entropic_error_t entropic_set_residency_observer(entropic_handle_t handle, entropic_residency_observer_t observer, void *user_data)
Register a residency observer on the orchestrator.
entropic_error_t entropic_metrics_json(entropic_handle_t handle, char **out)
Get loop metrics as JSON (flat + per_tier).
entropic_error_t entropic_validation_set_identity(entropic_handle_t handle, const char *identity_name, bool enabled)
Set per-identity validation override.
entropic_error_t entropic_grant_mcp_key_from(entropic_handle_t handle, const char *granter, const char *grantee, const char *pattern, entropic_mcp_access_level_t level)
Grant a key from one identity to another.
void entropic_free(void *ptr)
Free memory allocated by the engine.
entropic_error_t entropic_run_streaming(entropic_handle_t handle, const char *input, void(*on_token)(const char *token, size_t len, void *user_data), void *user_data, int *cancel_flag)
Streaming generation — delegates entirely to engine.
double entropic_throughput_tok_per_sec(entropic_handle_t handle, const char *model_path)
Get EWMA throughput estimate in tokens per second.
char * entropic_profile_get(entropic_handle_t handle, const char *name)
Get a GPU resource profile by name as JSON.
entropic_error_t entropic_validation_accept_last(entropic_handle_t handle)
Accept the last paused attempt as the final answer.
static void populate_tier_info(entropic_handle_t h, const std::filesystem::path &data_dir, const std::string &shared_prefix)
Register per-tier ChildContextInfo with the engine.
static void init_persistence(entropic_handle_t h)
Initialize persistence: storage + session logger + StorageInterface.
void entropic_throughput_reset(entropic_handle_t handle, const char *model_path)
Reset throughput tracking data.
static char * sp_get_docs(const char *section, void *ud)
State provider: get_docs.
static bool si_save_conversation(const char *conversation_id, const char *messages_json, void *user_data)
StorageInterface bridge: save_conversation trampoline.
static void rewire_state_observer(entropic_handle_t h)
Propagate any pre-configure state observer to the new engine.
entropic_error_t entropic_set_critique_callbacks(entropic_handle_t handle, void(*start_cb)(void *user_data), void(*end_cb)(void *user_data), void *user_data)
Register critique start/end callbacks on the handle (gh#50, v2.1.12).
entropic_error_t entropic_queue_user_message(entropic_handle_t handle, const char *message)
Enqueue a follow-up user message while a run is in flight.
char * entropic_list_mcp_keys(entropic_handle_t handle, const char *identity_name)
List MCP keys for an identity as JSON array.
void entropic_destroy(entropic_handle_t handle)
Destroy an engine instance.
entropic_error_t entropic_adapter_load(entropic_handle_t handle, const char *adapter_name, const char *adapter_path, const char *base_model_path, float scale)
Load a LoRA adapter into RAM.
entropic_error_t entropic_validation_set_enabled(entropic_handle_t handle, bool enabled)
Enable or disable constitutional validation globally.
entropic_error_t entropic_configure_from_file(entropic_handle_t handle, const char *config_path)
Configure the engine from a YAML config file.
int entropic_adapter_state(entropic_handle_t handle, const char *adapter_name)
Query adapter lifecycle state.
static void rewire_critique_callbacks(entropic_handle_t h)
Propagate pre-configure critique callbacks to a newly- constructed ConstitutionalValidator (gh#50,...
int64_t entropic_seconds_since_last_activity(entropic_handle_t handle)
gh#35: idle-time accessor for host-side idle-exit policies.
entropic_error_t entropic_set_state_observer(entropic_handle_t handle, void(*observer)(int state, void *user_data), void *user_data)
Register a state-change observer on the handle.
static void init_engine_and_interfaces(entropic_handle_t h, const std::filesystem::path &data_dir)
Build the engine + inference interfaces (configure step 2).
static void wire_hooks_and_validator(entropic_handle_t h, entropic::InferenceInterface &iface, const std::string &constitution_text)
Wire hook dispatch and attach the constitutional validator.
entropic_error_t entropic_grant_mcp_key(entropic_handle_t handle, const char *identity_name, const char *pattern, entropic_mcp_access_level_t level)
Grant an MCP tool key to an identity.
static entropic::LoopConfig build_loop_config(entropic_handle_t h)
Build LoopConfig from parsed config.
static entropic_error_t check_orchestrator(entropic_handle_t h)
Check handle prerequisites for orchestrator APIs.
entropic_error_t entropic_context_count(entropic_handle_t handle, size_t *count)
Get conversation message count.
static void rewire_queue_observer(entropic_handle_t h)
Propagate any pre-configure queue observer to the new engine.
char * entropic_validation_last_result(entropic_handle_t handle)
Get last validation result as JSON.
static char * sp_get_validation(void *ud)
Return the validator's last verdict as JSON for ON_COMPLETE.
char * entropic_grammar_validate(const char *gbnf_content)
Validate a GBNF grammar string without registering.
char * entropic_profile_list(entropic_handle_t handle)
List all registered profile names as a JSON array.
entropic_error_t entropic_compute_perplexity(entropic_handle_t handle, const char *model_id, const int32_t *tokens, int n_tokens, float *perplexity)
Compute perplexity for a token sequence.
entropic_error_t entropic_grammar_register_file(entropic_handle_t handle, const char *key, const char *path)
Register a grammar from a GBNF file.
static char * sp_get_config(void *ud)
State provider: get_config.
entropic_error_t entropic_speculative_compat(entropic_handle_t handle, int *compatible, char **diagnostic)
Query speculative-decoding compatibility for the configured target/draft pair.
entropic_error_t entropic_destroy_identity(entropic_handle_t handle, const char *name)
Destroy a dynamic identity.
static entropic::InferenceBackend * require_active_backend(entropic_handle_t h, const char *tier_name)
Resolve tier name to an ACTIVE backend, or throw.
static char * sp_get_history(int max_entries, void *ud)
State provider: get_history — conversation context snapshot.
int entropic_check_mcp_key(entropic_handle_t handle, const char *identity_name, const char *tool_name, entropic_mcp_access_level_t level)
Check MCP key authorization for an identity.
entropic_error_t entropic_run(entropic_handle_t handle, const char *input, char **result_json)
Single-turn blocking agentic run.
static void rewire_stream_observer(entropic_handle_t h)
Propagate any pre-configure stream observer to the new engine.
char * entropic_adapter_info(entropic_handle_t handle, const char *adapter_name)
Get adapter info as JSON string.
static char * sp_get_tools(void *ud)
State provider: get_tools.
entropic_error_t entropic_configure(entropic_handle_t handle, const char *config_json)
Configure the engine from a JSON/YAML config string.
entropic_error_t entropic_profile_deregister(entropic_handle_t handle, const char *name)
Remove a GPU resource profile by name.
entropic_error_t entropic_set_queue_observer(entropic_handle_t handle, void(*observer)(const char *, size_t, void *), void *user_data)
Register the queue-consumption observer.
static std::vector< entropic::Message > parse_and_check_vision(entropic_handle_t handle, const char *messages_json, entropic_error_t &out_rc)
Parse messages_json and check vision-tier availability (gh#37/gh#41).
static char * tool_history_json_thunk(size_t count, void *ud)
Wire the ToolExecutor and attach it to the engine.
static std::string build_assembled_prompt_for_tier(entropic_engine *h, const std::string &tier_name)
Build the assembled system prompt the engine would send for a given tier (constitution + app_context ...
static bool si_complete_delegation(const char *delegation_id, const char *status, const char *summary, void *user_data)
StorageInterface bridge: complete_delegation trampoline.
entropic_error_t entropic_user_message_queue_depth(entropic_handle_t handle, size_t *count)
Snapshot the mid-gen queue depth.
static entropic_error_t init_orchestrator(entropic_handle_t h, const std::filesystem::path &data_dir)
Shared body of all entropic_configure* entry points.
char * entropic_grammar_get(entropic_handle_t handle, const char *key)
Get grammar GBNF content by key.
char * entropic_list_identities(entropic_handle_t handle)
List all identity names as JSON array.
entropic_error_t entropic_context_get(entropic_handle_t handle, char **messages_json)
Get conversation as JSON array.
static void wire_prompts_and_persistence(entropic_handle_t h, const std::filesystem::path &data_dir)
Assemble prompts + wire validation/persistence (config step 3).
static entropic_error_t configure_common(entropic_handle_t h)
Shared body of all entropic_configure* entry points.
entropic_error_t entropic_profile_register(entropic_handle_t handle, const char *profile_json)
Register a custom GPU resource profile from JSON.
char * entropic_serialize_mcp_keys(entropic_handle_t handle)
Serialize all identity key sets to JSON.
void entropic_free_logprob_result(entropic_logprob_result_t *result)
Free internal arrays of a logprob result.
entropic_error_t entropic_adapter_swap(entropic_handle_t handle, const char *adapter_name)
Swap active LoRA adapter.
static entropic_error_t run_messages_inner(entropic_handle_t handle, const char *messages_json, char **result_json)
Blocking multimodal run (gh#37, v2.1.8).
entropic_error_t entropic_set_delegation_callbacks(entropic_handle_t handle, ent_delegation_start_cb on_start, ent_delegation_complete_cb on_complete, void *user_data)
Register delegation start/complete callbacks (gh#29, v2.1.5).
entropic_error_t entropic_interrupt(entropic_handle_t handle)
Interrupt a running generation (thread-safe).
static std::vector< std::string > collect_delegatable_tiers(const entropic::ParsedConfig &config)
Collect valid delegation targets from handoff_rules.
static std::vector< std::string > filter_tools(const nlohmann::json &all_tools, const std::vector< std::string > &allowed)
Filter tool definitions by allowed list.
int entropic_model_has_vision(entropic_handle_t handle, const char *model_id)
Check if a model has vision (multimodal) capability.
entropic_error_t entropic_get_diagnostic_prompt(entropic_handle_t handle, char **prompt_out)
Get diagnostic prompt text for /diagnose command (stub).
static void wire_external_interrupt(entropic_handle_t h)
Wire engine interrupt propagation into MCP transports (P1-10).
static entropic::StorageInterface build_storage_iface(entropic::SqliteStorageBackend *sb)
Build a populated StorageInterface bound to sb.
static void cache_tier_allowed_tools(entropic_handle_t h, const std::filesystem::path &data_dir)
Cache per-tier frontmatter fields from identity files.
const char * entropic_last_error(entropic_handle_t handle)
Read the per-handle last_error under api_mutex.
entropic_error_t entropic_configure_dir(entropic_handle_t handle, const char *project_dir)
Configure using layered resolution (project dir).
entropic_error_t entropic_residency_snapshot(entropic_handle_t handle, char **out_json)
Return the engine's current residency-set snapshot as JSON.
entropic_error_t entropic_run_messages_streaming(entropic_handle_t handle, const char *messages_json, void(*on_token)(const char *token, size_t len, void *user_data), void *user_data, int *cancel_flag)
Streaming multimodal agentic run (gh#37, v2.1.8).
entropic_error_t entropic_context_clear(entropic_handle_t handle)
Clear conversation history.
static char * sp_get_identities(void *ud)
State provider: get_identities.
entropic_error_t entropic_create_identity(entropic_handle_t handle, const char *config_json)
Create a dynamic identity from JSON config.
entropic_error_t entropic_set_attempt_boundary_cb(entropic_handle_t handle, ent_validation_attempt_boundary_cb cb, void *user_data)
Register attempt-boundary callback on the validator.
int entropic_api_version(void)
Get the plugin API version number.
char * entropic_grammar_list(entropic_handle_t handle)
List all registered grammars as a JSON array.
static bool si_create_conversation(const char *title, std::string &conversation_id, void *user_data)
StorageInterface bridge: create_conversation trampoline.
const char * entropic_version(void)
Get the library version string.
static entropic_error_t check_identity(entropic_handle_t h)
Check handle prerequisites for identity manager APIs.
static void wire_state_provider(entropic_handle_t h)
Wire state provider to the EntropicServer.
static int facade_get_tool_prompt(const char *tier, char **result, void *user_data)
Build formatted tool prompt for a tier.
entropic_error_t entropic_get_logprobs(entropic_handle_t handle, const char *model_id, const int32_t *tokens, int n_tokens, entropic_logprob_result_t *result)
Evaluate per-token log-probabilities for a token sequence.
static void wire_tier_validation_rules(entropic_handle_t h)
Pass per-identity validation rules to the validator.
static void wire_tool_executor(entropic_handle_t h)
Wire the ToolExecutor and attach it to the engine.
entropic_error_t entropic_adapter_unload(entropic_handle_t handle, const char *adapter_name)
Unload a LoRA adapter.
static bool si_save_snapshot(const char *conversation_id, const char *messages_json, void *user_data)
StorageInterface bridge: save_snapshot trampoline.
void * entropic_alloc(size_t size)
Allocate memory using the engine's allocator.
entropic_error_t entropic_clear_user_message_queue(entropic_handle_t handle)
Drop all queued mid-gen user messages.
entropic_error_t entropic_revoke_mcp_key(entropic_handle_t handle, const char *identity_name, const char *pattern)
Revoke an MCP tool key from an identity.
char * entropic_get_identity_config(entropic_handle_t handle, const char *name)
Get identity config as JSON by name.
static char * alloc_cstr(const char *src)
Allocate a C string copy via the engine allocator.
static bool si_load_delegation_with_messages(const char *delegation_id, std::string &result_json, void *user_data)
StorageInterface bridge: load_delegation_with_messages.
Public C API for the Entropic inference engine.
ent_decision_t(* ent_delegation_start_cb)(const ent_delegation_request_t *req, void *user_data)
Callback fired before a delegation runs.
ent_decision_t(* ent_delegation_complete_cb)(const ent_delegation_result_t *res, void *user_data)
Callback fired after a delegation produces a patch.
void(* entropic_residency_observer_t)(entropic_residency_event_t event, const char *tier_name, const char *model_path, size_t footprint_bytes, void *user_data)
Residency observer callback.
entropic_residency_event_t
Reasons fired by entropic_residency_observer_t.
entropic_mcp_access_level_t
Access level enum for MCP authorization.
void(* ent_validation_attempt_boundary_cb)(int attempt_n, void *user_data)
Stream-side callback fired between constitutional revision passes.
Entropic MCP server — engine-level tools including introspection.
entropic_error_t
Error codes returned by all C API functions.
@ ENTROPIC_ERROR_NO_VISION_TIER
Image content present but no vision-capable tier (v2.1.8, gh#41)
@ ENTROPIC_ERROR_ADAPTER_SWAP_FAILED
Swap failed (e.g., base model not ACTIVE) (v1.9.2)
@ ENTROPIC_ERROR_CANCELLED
Operation cancelled via cancel token.
@ ENTROPIC_ERROR_ALREADY_EXISTS
Named resource already exists (v1.9.6)
@ ENTROPIC_ERROR_INTERNAL
Unexpected internal error (bug)
@ ENTROPIC_ERROR_GRAMMAR_NOT_FOUND
Grammar key not in registry (v1.9.3)
@ ENTROPIC_ERROR_INVALID_ARGUMENT
NULL pointer, empty string, out-of-range value.
@ ENTROPIC_ERROR_QUEUE_FULL
Mid-gen user-message queue at capacity (v2.1.10, gh#40)
@ ENTROPIC_ERROR_INVALID_HANDLE
NULL or destroyed handle (v1.8.9)
@ ENTROPIC_ERROR_OUT_OF_MEMORY
Allocation failed (system RAM or VRAM)
@ ENTROPIC_ERROR_EVAL_FAILED
Evaluation failed (llama_decode error) (v1.9.10)
@ ENTROPIC_ERROR_ADAPTER_LOAD_FAILED
LoRA file invalid or incompatible with base model (v1.9.2)
@ ENTROPIC_ERROR_PROFILE_NOT_FOUND
Profile name not in registry (v1.9.7)
@ ENTROPIC_ERROR_IO
File/network I/O error.
@ ENTROPIC_ERROR_GENERATE_FAILED
Generation failed (context overflow, model error)
@ ENTROPIC_ERROR_INVALID_CONFIG
Config validation failed (missing fields, bad values)
@ ENTROPIC_ERROR_INVALID_STATE
Operation not valid in current state (e.g., generate before activate)
@ ENTROPIC_ERROR_LOAD_FAILED
Model load failed (corrupt file, OOM, unsupported format)
entropic_hook_point_t
Hook points in the engine lifecycle.
Pure C interface contract for inference backends.
void entropic_inference_log_silence(void)
Silence all llama/ggml log output.
Factory for building InferenceInterface from a ModelOrchestrator.
JSON serialization helpers for the facade.
LlamaCppBackend — llama.cpp C API integration.
Config loader — YAML to C++ structs with validation.
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
Prompt manager — frontmatter parsing, identity loading, assembly.
Shared parser: messages-JSON wire format → vector<Message>.
@ VISION
Vision / multimodal input (v1.9.11)
bool any_message_has_images(const std::vector< Message > &messages)
Convenience: true if any message carries image content_parts.
@ passed_consumer_override
gh#30 (v2.1.5): consumer called accept_last() to override a paused rejection.
@ rejected_reverted_length
Revision gutted content >50%; original preserved.
@ passed
No violations, content unchanged.
@ revised
Violations found; revision applied.
@ paused_pending_consumer
gh#30 (v2.1.5): auto_retry disabled and a critique failed.
@ skipped
Validation did not run (skip_tiers / pure-tool-call / empty)
@ rejected_max_revisions
Revisions exhausted; last output returned as-is.
@ DYNAMIC
Created at runtime via API.
@ STATIC
Loaded from YAML frontmatter file at startup.
void destroy_orchestrator_interface(InterfaceContext *context)
Free a context returned by build_orchestrator_interface().
std::vector< Message > parse_messages_json(const char *json_str)
Parse a JSON array of messages into a vector of Message.
MCPAccessLevel
MCP tool access level for per-identity authorization.
@ COLD
Not loaded. No resources consumed.
InferenceInterface build_orchestrator_interface(ModelOrchestrator *orchestrator, const std::string &default_tier, InterfaceContext **out_context)
Build an InferenceInterface wired to an orchestrator.
ModelOrchestrator — multi-model lifecycle and routing.
Resolved tier information for building child delegation contexts.
int max_revisions
Max re-generation attempts (0 = critique only)
bool enabled
Global enable/disable (default OFF)
bool enabled
Enable external MCP.
Named GPU resource profile for controlling inference hardware knobs.
int n_threads_batch
CPU threads for batch processing (0 = use n_threads)
int n_batch
Batch size for prompt processing (1-2048)
std::string name
Profile name ("maximum", "balanced", "background", "minimal")
int n_threads
CPU threads for generation (0 = auto-detect)
std::string description
Human-readable description.
Full identity configuration.
std::string name
Unique identity name (e.g., "eng", "npc_blacksmith")
std::vector< std::string > focus
Classification focus keywords (min 1)
IdentityOrigin origin
How this identity was created.
std::string system_prompt
Full system prompt text (markdown body)
Configuration for the identity manager.
Configuration for the agentic loop.
int context_length
Context budget for compaction (v2.0.4)
bool stream_output
Stream vs batch generation.
bool auto_approve_tools
Skip tool approval (v1.8.5)
Mutable state carried through the agentic loop.
ExternalMCPConfig external
External MCP server config (Entropic-as-server)
std::string working_dir
Server working directory (empty = CWD) (v2.0.4)
std::unordered_map< std::string, TierConfig > tiers
Tier name → config.
std::string find_tier_by_path(const std::filesystem::path &model_path) const
Find tier name by model path.
std::string default_tier
Default tier name.
Full parsed configuration.
PermissionsConfig permissions
Tool permissions.
std::optional< std::filesystem::path > app_context
App context: nullopt = disabled by default.
CompactionConfig compaction
Auto-compaction settings.
RoutingConfig routing
Routing rules.
ModelsConfig models
Tiers + router.
ConstitutionalValidationConfig constitutional_validation
Constitutional validation pipeline settings.
std::filesystem::path log_dir
Session log directory (session.log + session_model.log).
MCPConfig mcp
MCP server settings.
bool console_logging
Emit engine spdlog output to the stderr console sink.
bool app_context_disabled
true if app_context explicitly disabled
std::optional< std::filesystem::path > constitution
Constitution: nullopt = bundled default, disabled = explicit false.
bool constitution_disabled
true if constitution explicitly disabled
std::filesystem::path config_dir
Config dir — base for bundled data discovery.
bool auto_approve
Skip confirmation prompts.
std::unordered_map< std::string, std::vector< std::string > > handoff_rules
Tier handoff rules.
Storage interface for conversation persistence.
bool(* create_conversation)(const char *title, std::string &conversation_id, void *user_data)
Create a root conversation row.
Identity frontmatter — full tier identity metadata.
std::vector< std::string > validation_rules
Per-identity constitutional rules (v2.0.6)
std::optional< std::vector< std::string > > allowed_tools
Tool filter.
bool relay_single_delegate
Skip re-synthesis when single delegate returns (v2.0.11)
Parsed identity file: frontmatter + body.
Engine handle struct — owns all subsystems.
std::unique_ptr< entropic::ConstitutionalValidator > validator
Constitutional validation.
std::unique_ptr< entropic::ToolExecutor > tool_executor
Tool dispatch.
void(* critique_end_cb)(void *)
Fires after the critique generate returns.
int log_id
gh#59 (v2.3.1): unique handle id for per-handle log routing via entropic::log::HandleAwareSink.
std::unordered_map< std::string, std::vector< std::string > > tier_validation_rules
Per-tier validation_rules from identity frontmatter (v2.0.6).
std::unique_ptr< entropic::SqliteStorageBackend > storage
SQLite persistence.
entropic::InferenceInterface inference_iface
Stable copy for validator lifetime.
std::unique_ptr< entropic::SessionLogger > session_logger
Model transcript log.
std::unordered_map< std::string, std::vector< std::string > > tier_allowed_tools
Per-tier allowed_tools from identity frontmatter.
std::atomic< bool > configured
True after configure()
std::unique_ptr< entropic::ExternalBridge > external_bridge
Unix socket MCP bridge.
void(* state_observer)(int, void *)
Observer for engine state transitions.
entropic::config::BundledModels bundled_models
Model registry.
void * stream_observer_data
Observer user_data.
std::unique_ptr< entropic::IdentityManager > identity_manager
Identity lifecycle.
std::string last_error
Per-handle error message.
std::unique_ptr< entropic::AgentEngine > engine
Agentic loop (owns conversation state)
void * critique_cb_data
Forwarded to both callbacks.
std::unique_ptr< entropic::MCPAuthorizationManager > mcp_auth
Per-identity tool auth.
std::unique_ptr< entropic::ModelOrchestrator > orchestrator
Model pool + routing.
entropic::ParsedConfig config
Parsed config.
void(* stream_observer)(const char *, size_t, void *)
Global stream observer — fires for all streaming output.
void(* queue_observer)(const char *, size_t, void *)
Observer fired when a queued mid-gen user message is consumed and seeded as the next turn.
std::unique_ptr< entropic::ServerManager > server_manager
MCP server lifecycle.
entropic::InterfaceContext * inference_iface_ctx
Per-handle owned context backing inference_iface.user_data.
void(* critique_start_cb)(void *)
Fires before the constitutional validator's critique generate begins.
entropic::HookRegistry hook_registry
Hook dispatch.
Per-token log-probability result.
float perplexity
exp(-mean(logprobs)) over the sequence.
int32_t * tokens
Input tokens echoed back (N values).
int n_logprobs
Number of logprob values (n_tokens - 1).
int n_tokens
Number of input tokens.
float * logprobs
Per-token log-probabilities (N-1 values).
float total_logprob
Sum of all logprob values.
Read-only engine state provider for introspection tools.
char *(* get_config)(void *user_data)
Get current engine configuration as JSON.
UTF-8-boundary-aware string truncation helper for the facade.