| 1 | /* |
| 2 | * memory.h - shared declarations for plugin_memory. |
| 3 | * |
| 4 | * plugin_memory is a SCORPIOX BOT connector plugin that reimplements the |
| 5 | * reference MCP "memory" knowledge graph in pure C99: entities with string |
| 6 | * observations, relations between them, persisted as one JSONL file, exposed |
| 7 | * both as nine MCP tools over stdio and as a JSON API behind a browser UI. |
| 8 | * |
| 9 | * The four translation units: |
| 10 | * plugin_memory.c main(), --meta, --mcp loop, CGI dispatch, auth |
| 11 | * graph.c storage: paths, load, atomic save, entity/relation store |
| 12 | * tools.c the nine tools, shared by the MCP and the HTTP front end |
| 13 | * api.c the CGI JSON actions and the HTML page |
| 14 | * |
| 15 | * MIT License. Copyright (c) 2026 SCORPIOX INC. |
| 16 | */ |
| 17 | #ifndef SX_PLUGIN_MEMORY_H |
| 18 | #define SX_PLUGIN_MEMORY_H |
| 19 | |
| 20 | #include <stddef.h> |
| 21 | #include "yyjson.h" |
| 22 | |
| 23 | #define PLUGIN_SLUG "memory" |
| 24 | #define PLUGIN_ID "plugin_memory" |
| 25 | #define PLUGIN_VER "1.0.0" |
| 26 | #define PLUGIN_NAME "memory" |
| 27 | #define PLUGIN_DESC "Knowledge graph" |
| 28 | |
| 29 | #define MCP_PROTOCOL "2025-06-18" |
| 30 | #define RESOURCE_URI "memory://knowledge-graph" |
| 31 | #define SERVER_NAME "memory-server" |
| 32 | |
| 33 | /* ------------------------------------------------------------------ */ |
| 34 | /* Limits */ |
| 35 | /* */ |
| 36 | /* Every cap is a deliberate, documented ceiling: a plugin runs inside */ |
| 37 | /* the bot on someone's node, so a runaway agent must not be able to */ |
| 38 | /* grow the graph until the node swaps. Each one has an env override so */ |
| 39 | /* the tests can prove the truncation path on a tiny fixture. */ |
| 40 | /* ------------------------------------------------------------------ */ |
| 41 | |
| 42 | #define MAX_ENTITIES_DEFAULT 20000 |
| 43 | #define MAX_RELATIONS_DEFAULT 40000 |
| 44 | #define MAX_OBS_PER_ENT_DEFAULT 2000 |
| 45 | #define SX_NAME_MAX 1024 |
| 46 | #define SX_TYPE_MAX 1024 |
| 47 | #define OBS_TEXT_MAX 8000 |
| 48 | #define PATH_SZ 1024 |
| 49 | /* Suffix room: "%s.lock" and "%s.tmp.<pid>" append to a full PATH_SZ path. */ |
| 50 | #define SUFFIX_SZ 32 |
| 51 | #define REQ_BODY_MAX (8 * 1024 * 1024) |
| 52 | #define FILE_MAX (24 * 1024 * 1024) |
| 53 | #define STDIN_LINE_MAX (8 * 1024 * 1024) |
| 54 | #define UI_LIMIT_DEFAULT 1000 |
| 55 | |
| 56 | /* ------------------------------------------------------------------ */ |
| 57 | /* Growable byte buffer (graph.c) */ |
| 58 | /* ------------------------------------------------------------------ */ |
| 59 | |
| 60 | typedef struct { |
| 61 | char *p; |
| 62 | size_t n; |
| 63 | size_t cap; |
| 64 | } buf_t; |
| 65 | |
| 66 | void buf_init(buf_t *b); |
| 67 | int buf_need(buf_t *b, size_t extra); |
| 68 | void buf_add(buf_t *b, const char *s, size_t n); |
| 69 | void buf_puts(buf_t *b, const char *s); |
| 70 | void buf_putc(buf_t *b, int c); |
| 71 | const char *buf_str(const buf_t *b); |
| 72 | void buf_free(buf_t *b); |
| 73 | |
| 74 | /* ------------------------------------------------------------------ */ |
| 75 | /* Small helpers (graph.c) */ |
| 76 | /* ------------------------------------------------------------------ */ |
| 77 | |
| 78 | void str_copy(char *dst, size_t dsz, const char *src); |
| 79 | int str_icontains(const char *hay, const char *needle); |
| 80 | void json_str(buf_t *b, const char *s); |
| 81 | int env_int(const char *name, int dflt, int lo, int hi); |
| 82 | char *dup_or_null(const char *s); |
| 83 | |
| 84 | /* ------------------------------------------------------------------ */ |
| 85 | /* The graph (graph.c) */ |
| 86 | /* ------------------------------------------------------------------ */ |
| 87 | |
| 88 | typedef struct { |
| 89 | char *name; |
| 90 | char *etype; |
| 91 | char **obs; |
| 92 | size_t n_obs; |
| 93 | int keep; /* scratch, used by the read tools */ |
| 94 | } entity_t; |
| 95 | |
| 96 | typedef struct { |
| 97 | char *from; |
| 98 | char *to; |
| 99 | char *rtype; |
| 100 | } relation_t; |
| 101 | |
| 102 | typedef struct { |
| 103 | entity_t *ent; size_t n_ent, cap_ent; |
| 104 | relation_t *rel; size_t n_rel, cap_rel; |
| 105 | char **bad; size_t n_bad, cap_bad; /* lines kept verbatim */ |
| 106 | int truncated; |
| 107 | } graph_t; |
| 108 | |
| 109 | extern graph_t G; |
| 110 | extern char g_data_err[256]; |
| 111 | |
| 112 | int graph_load(void); /* 0 ok, -1 + g_data_err */ |
| 113 | int graph_save(char *err, size_t errsz); |
| 114 | void graph_lock(void); |
| 115 | void graph_unlock(void); |
| 116 | const char *graph_path(void); |
| 117 | int graph_path_info(long long *size, int *exists); |
| 118 | entity_t *ent_find(graph_t *g, const char *name); |
| 119 | int ent_add(graph_t *g, const char *name, const char *etype, |
| 120 | char **obs, size_t n_obs, char *err, size_t errsz); |
| 121 | int rel_add(graph_t *g, const char *from, const char *to, |
| 122 | const char *rtype, char *err, size_t errsz); |
| 123 | void bad_add(graph_t *g, const char *line); |
| 124 | int obs_push(entity_t *e, const char *text); |
| 125 | void graph_clear(graph_t *g); |
| 126 | int str_arr_from(yyjson_val *arr, char ***out, size_t *n_out, size_t cap); |
| 127 | |
| 128 | /* ------------------------------------------------------------------ */ |
| 129 | /* Read options (tools.c) */ |
| 130 | /* ------------------------------------------------------------------ */ |
| 131 | |
| 132 | typedef struct { |
| 133 | int has_limit; /* observationLimit present (0 is a real value) */ |
| 134 | int limit; |
| 135 | int has_types; /* entityTypes present */ |
| 136 | char **types; |
| 137 | size_t n_types; |
| 138 | int has_count; /* emit observationCount (set with has_limit) */ |
| 139 | int max_entities;/* 0 = unlimited */ |
| 140 | int truncated; |
| 141 | } read_opt_t; |
| 142 | |
| 143 | void read_opt_init(read_opt_t *o); |
| 144 | |
| 145 | /* ------------------------------------------------------------------ */ |
| 146 | /* Tool results (tools.c) */ |
| 147 | /* ------------------------------------------------------------------ */ |
| 148 | |
| 149 | typedef struct { |
| 150 | yyjson_mut_doc *md; /* owns root */ |
| 151 | yyjson_mut_val *root; /* the result object */ |
| 152 | char *notice; /* optional second text block (skipped, etc.) */ |
| 153 | char *error; /* error text when is_error */ |
| 154 | int is_error; |
| 155 | } tool_out_t; |
| 156 | |
| 157 | void tool_out_init(tool_out_t *t); |
| 158 | void tool_err(tool_out_t *t, const char *fmt, ...); |
| 159 | void tool_free(tool_out_t *t); |
| 160 | |
| 161 | /* The nine tools. Each reads `args` (may be NULL) and fills `t`. */ |
| 162 | void tool_create_entities(yyjson_val *args, tool_out_t *t); |
| 163 | void tool_create_relations(yyjson_val *args, tool_out_t *t); |
| 164 | void tool_add_observations(yyjson_val *args, tool_out_t *t); |
| 165 | void tool_delete_entities(yyjson_val *args, tool_out_t *t); |
| 166 | void tool_delete_observations(yyjson_val *args, tool_out_t *t); |
| 167 | void tool_delete_relations(yyjson_val *args, tool_out_t *t); |
| 168 | void tool_read_graph(yyjson_val *args, tool_out_t *t); |
| 169 | void tool_search_nodes(yyjson_val *args, tool_out_t *t); |
| 170 | void tool_open_nodes(yyjson_val *args, tool_out_t *t); |
| 171 | |
| 172 | /* The full tools/list array as JSON text (caller frees). */ |
| 173 | char *tools_list_json(void); |
| 174 | /* Dispatch by name; returns 0 when the name is one of the nine. */ |
| 175 | int tool_dispatch(const char *name, yyjson_val *args, tool_out_t *out); |
| 176 | |
| 177 | /* Build the MCP result object for a tool call: content[] + isError |
| 178 | * (+ structuredContent when the reference server emits one). */ |
| 179 | char *tool_mcp_result(tool_out_t *t); |
| 180 | |
| 181 | /* Serialize the tool result for the HTTP API: {"ok":true,...} or |
| 182 | * {"ok":false,"error":"..."}. Caller frees. */ |
| 183 | char *tool_api_result(tool_out_t *t); |
| 184 | |
| 185 | #endif /* SX_PLUGIN_MEMORY_H */ |