54 auto* p =
static_cast<char*
>(std::malloc(s.size() + 1));
55 if (p) std::memcpy(p, s.c_str(), s.size() + 1);
66static std::optional<std::string>
opt_str(
const char* s) {
67 return s ? std::optional<std::string>(s) : std::nullopt;
83 if (!db_path)
return nullptr;
86 }
catch (
const std::exception& e) {
87 logger->error(
"storage_create failed: {}", e.what());
107 }
catch (
const std::exception& e) {
108 logger->error(
"storage_initialize failed: {}", e.what());
140 const char* project_path,
141 const char* model_id) {
142 if (!storage || !title)
return nullptr;
147 }
catch (
const std::exception& e) {
148 logger->error(
"create_conversation failed: {}", e.what());
166 const char* conversation_id,
167 const char* messages_json) {
168 if (!storage || !conversation_id || !messages_json) {
172 return storage->impl.
save_messages(conversation_id, messages_json)
174 }
catch (
const std::exception& e) {
175 logger->error(
"save_conversation failed: {}", e.what());
193 const char* conversation_id,
194 char** result_json) {
195 if (!storage || !conversation_id || !result_json) {
201 conversation_id, json);
202 *result_json = found ?
dup_string(json) :
nullptr;
204 }
catch (
const std::exception& e) {
205 logger->error(
"load_conversation failed: {}", e.what());
224 int limit,
int offset,
225 char** result_json) {
226 if (!storage || !result_json) {
234 }
catch (
const std::exception& e) {
235 logger->error(
"list_conversations failed: {}", e.what());
254 const char* query,
int limit,
255 char** result_json) {
256 if (!storage || !query || !result_json) {
264 }
catch (
const std::exception& e) {
265 logger->error(
"search_conversations failed: {}", e.what());
283 const char* conversation_id) {
284 if (!storage || !conversation_id) {
290 }
catch (
const std::exception& e) {
291 logger->error(
"delete_conversation failed: {}", e.what());
315 const char* parent_conversation_id,
316 const char* delegating_tier,
317 const char* target_tier,
320 char** result_json) {
321 if (!storage || !parent_conversation_id || !delegating_tier ||
322 !target_tier || !task || !result_json) {
326 std::string del_id, child_id;
328 parent_conversation_id, delegating_tier,
329 target_tier, task, max_turns, del_id, child_id);
331 auto json =
"{\"delegation_id\":\"" + del_id +
332 "\",\"child_conversation_id\":\"" + child_id +
"\"}";
336 }
catch (
const std::exception& e) {
337 logger->error(
"create_delegation failed: {}", e.what());
356 const char* delegation_id,
358 const char* result_summary) {
359 if (!storage || !delegation_id || !status) {
364 delegation_id, status,
opt_str(result_summary))
366 }
catch (
const std::exception& e) {
367 logger->error(
"complete_delegation failed: {}", e.what());
385 const char* conversation_id,
386 char** result_json) {
387 if (!storage || !conversation_id || !result_json) {
395 }
catch (
const std::exception& e) {
396 logger->error(
"get_delegations failed: {}", e.what());
416 const char* conversation_id,
417 const char* messages_json) {
418 if (!storage || !conversation_id || !messages_json) {
422 return storage->impl.
save_snapshot(conversation_id, messages_json)
424 }
catch (
const std::exception& e) {
425 logger->error(
"save_snapshot failed: {}", e.what());
444 char** result_json) {
445 if (!storage || !result_json) {
453 }
catch (
const std::exception& e) {
454 logger->error(
"get_stats failed: {}", e.what());
entropic_error_t entropic_storage_get_stats(entropic_storage_backend_t storage, char **result_json)
Get statistics.
entropic_error_t entropic_storage_list_conversations(entropic_storage_backend_t storage, int limit, int offset, char **result_json)
List conversations.
entropic_error_t entropic_storage_load_conversation(entropic_storage_backend_t storage, const char *conversation_id, char **result_json)
Load a conversation.
entropic_error_t entropic_storage_create_delegation(entropic_storage_backend_t storage, const char *parent_conversation_id, const char *delegating_tier, const char *target_tier, const char *task, int max_turns, char **result_json)
Create a delegation.
entropic_error_t entropic_storage_save_snapshot(entropic_storage_backend_t storage, const char *conversation_id, const char *messages_json)
Save a compaction snapshot.
entropic_storage_backend_t entropic_storage_create(const char *db_path)
Create a storage backend.
void entropic_storage_destroy(entropic_storage_backend_t storage)
Destroy storage.
entropic_error_t entropic_storage_search_conversations(entropic_storage_backend_t storage, const char *query, int limit, char **result_json)
Search conversations.
static std::optional< std::string > opt_str(const char *s)
Convert optional C string to std::optional<std::string>.
entropic_error_t entropic_storage_initialize(entropic_storage_backend_t storage)
Initialize storage.
static char * dup_string(const std::string &s)
Duplicate a std::string to a malloc'd C string.
char * entropic_storage_create_conversation(entropic_storage_backend_t storage, const char *title, const char *project_path, const char *model_id)
Create a conversation.
entropic_error_t entropic_storage_complete_delegation(entropic_storage_backend_t storage, const char *delegation_id, const char *status, const char *result_summary)
Complete a delegation.
entropic_error_t entropic_storage_delete_conversation(entropic_storage_backend_t storage, const char *conversation_id)
Delete a conversation.
entropic_error_t entropic_storage_get_delegations(entropic_storage_backend_t storage, const char *conversation_id, char **result_json)
Get delegations for a conversation.
entropic_error_t entropic_storage_save_conversation(entropic_storage_backend_t storage, const char *conversation_id, const char *messages_json)
Save messages to a conversation.
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 search_conversations(const std::string &query, int limit, std::string &result_json)
Full-text search across conversations.
bool get_delegations(const std::string &conversation_id, std::string &result_json)
Get delegations for a parent 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.
bool load_conversation(const std::string &conversation_id, std::string &result_json)
Load a conversation with messages.
bool get_stats(std::string &result_json)
Get storage statistics.
bool delete_conversation(const std::string &conversation_id)
Delete a conversation and all associated records.
bool list_conversations(int limit, int offset, std::string &result_json)
List conversations with pagination.
bool initialize()
Initialize storage (open database, run migrations).
entropic_error_t
Error codes returned by all C API functions.
@ ENTROPIC_ERROR_INVALID_ARGUMENT
NULL pointer, empty string, out-of-range value.
@ ENTROPIC_ERROR_IO
File/network I/O error.
Pure C interface contract for storage backends.
spdlog initialization and logger access.
ENTROPIC_EXPORT std::shared_ptr< spdlog::logger > get(const std::string &name)
Get or create a named logger.
SqliteStorageBackend — conversation persistence via SQLite.
@dg_internal The opaque handle points directly to a SqliteStorageBackend.
entropic_storage_backend(const char *path)
Construct with database path.