1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/**
* AgentDB — C API
*
* Single-file embedded database for AI agents.
* SQL + Vector Search + Full-Text Search + Hybrid Queries + Memory Graphs.
*
* Memory contract:
* - All strings returned by agentdb_* functions are heap-allocated.
* Free them with agentdb_free_string(), never with free() or delete.
* - Input const char* pointers are borrowed for the duration of the call.
* - AgentDbHandle* is opaque. Close with agentdb_close().
*
* https://github.com/hvrcharon1/agentdb
*/
typedef struct AgentDbHandle AgentDbHandle;
/**
* Return the last error message as a UTF-8 C string, or NULL if none.
* The returned pointer must be freed with `agentdb_free_string`.
*/
char *;
/**
* Free a string previously returned by AgentDB.
*
* # Safety
* `ptr` must be a pointer previously returned by an `agentdb_*` function
* and must not have been freed already.
*/
void ;
/**
* Open or create an AgentDB database at `path`.
* Use `":memory:"` for an in-memory database.
*
* Returns an opaque handle on success, or NULL on failure.
* Check `agentdb_last_error()` on NULL.
*/
struct AgentDbHandle *;
/**
* Close and free an AgentDB handle.
*
* # Safety
* `handle` must be a valid pointer previously returned by `agentdb_open`
* and must not be used after this call.
*/
void ;
/**
* Execute a raw SQL statement (no parameters).
*
* Returns the number of rows affected, or -1 on error.
*/
int64_t ;
/**
* Query and return all rows as a JSON array string.
*
* Returns a heap-allocated JSON string — free with `agentdb_free_string`.
* Returns NULL on error; check `agentdb_last_error()`.
*/
char *;
/**
* Query with positional parameters and return all rows as a JSON array string.
*
* `params_json` is a JSON array of parameter values (e.g. `["alice", 42]`).
* Returns a heap-allocated JSON string — free with `agentdb_free_string`.
* Returns NULL on error; check `agentdb_last_error()`.
*/
char *;
/**
* Upsert a single vector into `collection` (created if absent).
*
* `id` — unique string identifier for this vector
* `vector` — pointer to `dim` f32 values
* `dim` — number of dimensions
* `metadata` — JSON string (may be NULL)
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Search a vector collection and return results as a JSON array.
*
* `query` — pointer to `dim` f32 query values
* `dim` — number of dimensions
* `top_k` — maximum results to return
* `filter_json`— MongoDB-style metadata filter JSON string (may be NULL)
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
* Returns NULL on error.
*/
char *;
/**
* Add or update a node in the memory graph.
*
* `id` — unique node identifier
* `kind` — node type label (e.g. "session", "concept")
* `data_json`— JSON metadata string (may be NULL)
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Add or update a directed weighted edge in the memory graph.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Traverse the memory graph from `node_id` and return results as JSON.
*
* `max_depth` — maximum hops from the anchor node
* `min_weight` — minimum edge weight to traverse (0.0 = all edges)
* `relation` — optional edge relation filter (NULL = all relations)
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Index a text document for full-text search.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Full-text search over a collection, returning results as JSON.
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Run a hybrid graph + vector query and return results as JSON.
*
* `anchor_node` — graph traversal start node id
* `embedding` — pointer to `dim` f32 query values
* `dim` — embedding dimensions
* `collection` — vector collection name
* `graph_depth` — max hops from anchor
* `top_k` — results to return
* `alpha` — 0.0 = pure graph, 1.0 = pure vector
* `filter_json` — optional MongoDB-style metadata filter JSON (may be NULL)
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Return database statistics as a JSON object.
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Create a new conversation.
*
* `id` — unique conversation identifier
* `title` — optional title (may be NULL)
* `metadata` — optional JSON string (may be NULL)
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Add a message to an existing conversation.
*
* Returns the new message ID as a heap-allocated string, or NULL on error.
* Free with `agentdb_free_string`.
*/
char *;
/**
* Get messages for a conversation as a JSON array.
*
* `limit` — maximum messages to return (0 = all).
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* List all conversations as a JSON array.
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Delete a conversation and all its messages.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Create a new workflow in `pending` status.
*
* `id` — unique workflow identifier
* `name` — human-readable workflow name
* `input` — optional JSON input (may be NULL)
* `metadata` — optional JSON metadata (may be NULL)
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Add a step to an existing workflow.
*
* Returns the step ID as a heap-allocated string, or NULL on error.
* Free with `agentdb_free_string`.
*/
char *;
/**
* Update a workflow step's status, output, and/or error.
*
* `status` — new status string ("running", "completed", "failed")
* `output` — optional JSON output (may be NULL)
* `error` — optional error message (may be NULL)
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Mark a workflow as completed with optional output.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Mark a workflow as failed with an optional error message.
*
* `error` — optional error string (may be NULL)
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Get a workflow and its steps as a JSON object.
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* List workflows as a JSON array, optionally filtered by status.
*
* `status_filter` — status to filter by (may be NULL for all).
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Record a new reasoning trace entry.
*
* Returns the trace ID as a heap-allocated string, or NULL on error.
* Free with `agentdb_free_string`.
*/
char *;
/**
* Get all traces for a session as a JSON array.
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Delete a single vector from a collection by ID.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Drop an entire vector collection and all its vectors.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Rebuild the HNSW index for a collection from stored vectors.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Get a graph node as a JSON object, or NULL if not found.
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Delete a graph node (and all its connected edges via CASCADE).
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Delete the directed edge from `src` to `dst` with the given `relation`.
*
* Returns 0 on success, -1 on error (including edge not found).
*/
int32_t ;
/**
* Delete a document from the FTS index.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Merge FTS index segments for faster queries (optimize).
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Register or update a tool definition.
*
* `name` — unique tool name
* `description` — human-readable description (may be NULL)
* `parameters_schema` — JSON Schema string (may be NULL)
* `version` — semver version string (may be NULL, defaults to "1.0.0")
*
* Returns the tool ID as a heap-allocated string, or NULL on error.
* Free with `agentdb_free_string`.
*/
char *;
/**
* List all registered tools as a JSON array.
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Log a tool call invocation.
*
* `session_id` — optional session context (may be NULL)
* `tool_name` — name of the tool called
* `arguments` — JSON arguments (may be NULL)
* `result` — JSON result (may be NULL)
* `error` — error message (may be NULL)
* `latency_ms` — execution time in milliseconds (-1 if unknown)
*
* Returns the tool call ID as a heap-allocated string, or NULL on error.
* Free with `agentdb_free_string`.
*/
char *;
/**
* Append an entry to the immutable audit log.
*
* `actor` — who performed the action (may be NULL)
* `action` — action type (e.g. "insert", "update", "delete")
* `table_name` — target table
* `record_id` — target record ID
* `old_value` — JSON of previous state (may be NULL)
* `new_value` — JSON of new state (may be NULL)
* `reason` — human-readable reason (may be NULL)
*
* Returns the audit entry ID, or NULL on error.
* Free with `agentdb_free_string`.
*/
char *;
/**
* Query recent audit log entries as a JSON array.
*
* `limit` — max entries to return (0 = default 100).
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Add an entry to the context window for a session.
*
* Returns the entry ID, or NULL on error.
* Free with `agentdb_free_string`.
*/
char *;
/**
* Build a token-budgeted context window for a session.
*
* Returns entries as a JSON array, filling up to `max_tokens`.
* Free with `agentdb_free_string`.
*/
char *;
/**
* Clear all context entries for a session.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Create a new version of a prompt template.
*
* `name` — template name (versions auto-increment per name)
* `template` — template body with {{placeholder}} syntax
* `model_hint`— suggested model (may be NULL)
* `max_tokens`— suggested max tokens (-1 if not set)
* `metadata` — optional JSON metadata (may be NULL)
*
* Returns the template ID, or NULL on error.
* Free with `agentdb_free_string`.
*/
char *;
/**
* Render a prompt template with variable substitution.
*
* `name` — template name (uses latest version)
* `vars_json` — JSON object of key-value pairs for {{placeholder}} substitution
*
* Returns the rendered string, or NULL on error.
* Free with `agentdb_free_string`.
*/
char *;
/**
* Tag a record with a privacy/classification label.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Remove a specific label from a record.
*
* Returns 0 on success, -1 on error.
*/
int32_t ;
/**
* Get all labels for a record as a JSON array.
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;
/**
* Check if a record has a specific label.
*
* Returns 1 if true, 0 if false, -1 on error.
*/
int32_t ;
/**
* Get a trace subtree as a JSON array.
*
* Returns heap-allocated JSON string — free with `agentdb_free_string`.
*/
char *;