/* * memory.h - shared declarations for plugin_memory. * * plugin_memory is a SCORPIOX BOT connector plugin that reimplements the * reference MCP "memory" knowledge graph in pure C99: entities with string * observations, relations between them, persisted as one JSONL file, exposed * both as nine MCP tools over stdio and as a JSON API behind a browser UI. * * The four translation units: * plugin_memory.c main(), --meta, --mcp loop, CGI dispatch, auth * graph.c storage: paths, load, atomic save, entity/relation store * tools.c the nine tools, shared by the MCP and the HTTP front end * api.c the CGI JSON actions and the HTML page * * MIT License. Copyright (c) 2026 SCORPIOX INC. */ #ifndef SX_PLUGIN_MEMORY_H #define SX_PLUGIN_MEMORY_H #include #include "yyjson.h" #define PLUGIN_SLUG "memory" #define PLUGIN_ID "plugin_memory" #define PLUGIN_VER "1.0.0" #define PLUGIN_NAME "memory" #define PLUGIN_DESC "Knowledge graph" #define MCP_PROTOCOL "2025-06-18" #define RESOURCE_URI "memory://knowledge-graph" #define SERVER_NAME "memory-server" /* ------------------------------------------------------------------ */ /* Limits */ /* */ /* Every cap is a deliberate, documented ceiling: a plugin runs inside */ /* the bot on someone's node, so a runaway agent must not be able to */ /* grow the graph until the node swaps. Each one has an env override so */ /* the tests can prove the truncation path on a tiny fixture. */ /* ------------------------------------------------------------------ */ #define MAX_ENTITIES_DEFAULT 20000 #define MAX_RELATIONS_DEFAULT 40000 #define MAX_OBS_PER_ENT_DEFAULT 2000 #define SX_NAME_MAX 1024 #define SX_TYPE_MAX 1024 #define OBS_TEXT_MAX 8000 #define PATH_SZ 1024 /* Suffix room: "%s.lock" and "%s.tmp." append to a full PATH_SZ path. */ #define SUFFIX_SZ 32 #define REQ_BODY_MAX (8 * 1024 * 1024) #define FILE_MAX (24 * 1024 * 1024) #define STDIN_LINE_MAX (8 * 1024 * 1024) #define UI_LIMIT_DEFAULT 1000 /* ------------------------------------------------------------------ */ /* Growable byte buffer (graph.c) */ /* ------------------------------------------------------------------ */ typedef struct { char *p; size_t n; size_t cap; } buf_t; void buf_init(buf_t *b); int buf_need(buf_t *b, size_t extra); void buf_add(buf_t *b, const char *s, size_t n); void buf_puts(buf_t *b, const char *s); void buf_putc(buf_t *b, int c); const char *buf_str(const buf_t *b); void buf_free(buf_t *b); /* ------------------------------------------------------------------ */ /* Small helpers (graph.c) */ /* ------------------------------------------------------------------ */ void str_copy(char *dst, size_t dsz, const char *src); int str_icontains(const char *hay, const char *needle); void json_str(buf_t *b, const char *s); int env_int(const char *name, int dflt, int lo, int hi); char *dup_or_null(const char *s); /* ------------------------------------------------------------------ */ /* The graph (graph.c) */ /* ------------------------------------------------------------------ */ typedef struct { char *name; char *etype; char **obs; size_t n_obs; int keep; /* scratch, used by the read tools */ } entity_t; typedef struct { char *from; char *to; char *rtype; } relation_t; typedef struct { entity_t *ent; size_t n_ent, cap_ent; relation_t *rel; size_t n_rel, cap_rel; char **bad; size_t n_bad, cap_bad; /* lines kept verbatim */ int truncated; } graph_t; extern graph_t G; extern char g_data_err[256]; int graph_load(void); /* 0 ok, -1 + g_data_err */ int graph_save(char *err, size_t errsz); void graph_lock(void); void graph_unlock(void); const char *graph_path(void); int graph_path_info(long long *size, int *exists); entity_t *ent_find(graph_t *g, const char *name); int ent_add(graph_t *g, const char *name, const char *etype, char **obs, size_t n_obs, char *err, size_t errsz); int rel_add(graph_t *g, const char *from, const char *to, const char *rtype, char *err, size_t errsz); void bad_add(graph_t *g, const char *line); int obs_push(entity_t *e, const char *text); void graph_clear(graph_t *g); int str_arr_from(yyjson_val *arr, char ***out, size_t *n_out, size_t cap); /* ------------------------------------------------------------------ */ /* Read options (tools.c) */ /* ------------------------------------------------------------------ */ typedef struct { int has_limit; /* observationLimit present (0 is a real value) */ int limit; int has_types; /* entityTypes present */ char **types; size_t n_types; int has_count; /* emit observationCount (set with has_limit) */ int max_entities;/* 0 = unlimited */ int truncated; } read_opt_t; void read_opt_init(read_opt_t *o); /* ------------------------------------------------------------------ */ /* Tool results (tools.c) */ /* ------------------------------------------------------------------ */ typedef struct { yyjson_mut_doc *md; /* owns root */ yyjson_mut_val *root; /* the result object */ char *notice; /* optional second text block (skipped, etc.) */ char *error; /* error text when is_error */ int is_error; } tool_out_t; void tool_out_init(tool_out_t *t); void tool_err(tool_out_t *t, const char *fmt, ...); void tool_free(tool_out_t *t); /* The nine tools. Each reads `args` (may be NULL) and fills `t`. */ void tool_create_entities(yyjson_val *args, tool_out_t *t); void tool_create_relations(yyjson_val *args, tool_out_t *t); void tool_add_observations(yyjson_val *args, tool_out_t *t); void tool_delete_entities(yyjson_val *args, tool_out_t *t); void tool_delete_observations(yyjson_val *args, tool_out_t *t); void tool_delete_relations(yyjson_val *args, tool_out_t *t); void tool_read_graph(yyjson_val *args, tool_out_t *t); void tool_search_nodes(yyjson_val *args, tool_out_t *t); void tool_open_nodes(yyjson_val *args, tool_out_t *t); /* The full tools/list array as JSON text (caller frees). */ char *tools_list_json(void); /* Dispatch by name; returns 0 when the name is one of the nine. */ int tool_dispatch(const char *name, yyjson_val *args, tool_out_t *out); /* Build the MCP result object for a tool call: content[] + isError * (+ structuredContent when the reference server emits one). */ char *tool_mcp_result(tool_out_t *t); /* Serialize the tool result for the HTTP API: {"ok":true,...} or * {"ok":false,"error":"..."}. Caller frees. */ char *tool_api_result(tool_out_t *t); #endif /* SX_PLUGIN_MEMORY_H */