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;
81 if (!db_path)
return nullptr;
84 }
catch (
const std::exception& e) {
85 logger->error(
"storage_create failed: {}", e.what());
103 }
catch (
const std::exception& e) {
104 logger->error(
"storage_initialize failed: {}", e.what());
134 const char* project_path,
135 const char* model_id) {
136 if (!storage || !title)
return nullptr;
141 }
catch (
const std::exception& e) {
142 logger->error(
"create_conversation failed: {}", e.what());
158 const char* conversation_id,
159 const char* messages_json) {
160 if (!storage || !conversation_id || !messages_json) {
164 return storage->impl.
save_messages(conversation_id, messages_json)
166 }
catch (
const std::exception& e) {
167 logger->error(
"save_conversation failed: {}", e.what());
183 const char* conversation_id,
184 char** result_json) {
185 if (!storage || !conversation_id || !result_json) {
191 conversation_id, json);
192 *result_json = found ?
dup_string(json) :
nullptr;
194 }
catch (
const std::exception& e) {
195 logger->error(
"load_conversation failed: {}", e.what());
212 int limit,
int offset,
213 char** result_json) {
214 if (!storage || !result_json) {
222 }
catch (
const std::exception& e) {
223 logger->error(
"list_conversations failed: {}", e.what());
240 const char* query,
int limit,
241 char** result_json) {
242 if (!storage || !query || !result_json) {
250 }
catch (
const std::exception& e) {
251 logger->error(
"search_conversations failed: {}", e.what());
266 const char* conversation_id) {
267 if (!storage || !conversation_id) {
273 }
catch (
const std::exception& e) {
274 logger->error(
"delete_conversation failed: {}", e.what());
296 const char* parent_conversation_id,
297 const char* delegating_tier,
298 const char* target_tier,
301 char** result_json) {
302 if (!storage || !parent_conversation_id || !delegating_tier ||
303 !target_tier || !task || !result_json) {
307 std::string del_id, child_id;
309 parent_conversation_id, delegating_tier,
310 target_tier, task, max_turns, del_id, child_id);
312 auto json =
"{\"delegation_id\":\"" + del_id +
313 "\",\"child_conversation_id\":\"" + child_id +
"\"}";
317 }
catch (
const std::exception& e) {
318 logger->error(
"create_delegation failed: {}", e.what());
335 const char* delegation_id,
337 const char* result_summary) {
338 if (!storage || !delegation_id || !status) {
343 delegation_id, status,
opt_str(result_summary))
345 }
catch (
const std::exception& e) {
346 logger->error(
"complete_delegation failed: {}", e.what());
362 const char* conversation_id,
363 char** result_json) {
364 if (!storage || !conversation_id || !result_json) {
372 }
catch (
const std::exception& e) {
373 logger->error(
"get_delegations failed: {}", e.what());
391 const char* conversation_id,
392 const char* messages_json) {
393 if (!storage || !conversation_id || !messages_json) {
397 return storage->impl.
save_snapshot(conversation_id, messages_json)
399 }
catch (
const std::exception& e) {
400 logger->error(
"save_snapshot failed: {}", e.what());
417 char** result_json) {
418 if (!storage || !result_json) {
426 }
catch (
const std::exception& e) {
427 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.
entropic_storage_backend(const char *path)
Construct with database path.