@miniaxolotl/grove 0.2.6 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/README.md +10 -108
  2. package/dist/bridge.js +30592 -0
  3. package/dist/bridge.js.map +7 -0
  4. package/dist/cli.js +32427 -0
  5. package/dist/cli.js.map +7 -0
  6. package/dist/hook.js +15682 -0
  7. package/dist/hook.js.map +7 -0
  8. package/dist/index.js +29223 -490
  9. package/dist/index.js.map +7 -1
  10. package/package.json +28 -20
  11. package/CHANGELOG.md +0 -7
  12. package/dist/config.d.ts +0 -27
  13. package/dist/config.d.ts.map +0 -1
  14. package/dist/config.js +0 -35
  15. package/dist/config.js.map +0 -1
  16. package/dist/index.d.ts +0 -2
  17. package/dist/index.d.ts.map +0 -1
  18. package/dist/repositories/entity.repository.d.ts +0 -44
  19. package/dist/repositories/entity.repository.d.ts.map +0 -1
  20. package/dist/repositories/entity.repository.js +0 -186
  21. package/dist/repositories/entity.repository.js.map +0 -1
  22. package/dist/repositories/memory.repository.d.ts +0 -59
  23. package/dist/repositories/memory.repository.d.ts.map +0 -1
  24. package/dist/repositories/memory.repository.js +0 -222
  25. package/dist/repositories/memory.repository.js.map +0 -1
  26. package/dist/repositories/relation.repository.d.ts +0 -38
  27. package/dist/repositories/relation.repository.d.ts.map +0 -1
  28. package/dist/repositories/relation.repository.js +0 -95
  29. package/dist/repositories/relation.repository.js.map +0 -1
  30. package/dist/services/embedding.d.ts +0 -4
  31. package/dist/services/embedding.d.ts.map +0 -1
  32. package/dist/services/embedding.js +0 -114
  33. package/dist/services/embedding.js.map +0 -1
  34. package/dist/services/qdrant.d.ts +0 -38
  35. package/dist/services/qdrant.d.ts.map +0 -1
  36. package/dist/services/qdrant.js +0 -117
  37. package/dist/services/qdrant.js.map +0 -1
  38. package/dist/services/reranking.d.ts +0 -6
  39. package/dist/services/reranking.d.ts.map +0 -1
  40. package/dist/services/reranking.js +0 -24
  41. package/dist/services/reranking.js.map +0 -1
  42. package/dist/utils/filter.d.ts +0 -10
  43. package/dist/utils/filter.d.ts.map +0 -1
  44. package/dist/utils/filter.js +0 -15
  45. package/dist/utils/filter.js.map +0 -1
  46. package/eslint.config.js +0 -1
  47. package/src/config.ts +0 -32
  48. package/src/index.ts +0 -594
  49. package/src/repositories/entity.repository.ts +0 -253
  50. package/src/repositories/memory.repository.ts +0 -316
  51. package/src/repositories/relation.repository.ts +0 -143
  52. package/src/services/embedding.ts +0 -150
  53. package/src/services/qdrant.ts +0 -201
  54. package/src/services/reranking.ts +0 -40
  55. package/src/utils/filter.ts +0 -20
  56. package/tsconfig.json +0 -10
package/src/index.ts DELETED
@@ -1,594 +0,0 @@
1
- import { FastMCP } from "fastmcp";
2
- import { z } from "zod";
3
- import { createServer } from "node:http";
4
- import { memoryRepository } from "./repositories/memory.repository.ts";
5
- import { entityRepository } from "./repositories/entity.repository.ts";
6
- import { relationRepository } from "./repositories/relation.repository.ts";
7
- import { config, hasRemoteReranking } from "./config.ts";
8
- import { rerankDocuments } from "./services/reranking.ts";
9
- import { warmup } from "./services/embedding.ts";
10
-
11
- let collectionsReady = false;
12
-
13
- const server = new FastMCP({
14
- name: "grove",
15
- version: "0.1.0",
16
- });
17
-
18
- function text(content: string) {
19
- return { content: [{ type: "text" as const, text: content }] };
20
- }
21
-
22
- function toolErr(err: unknown) {
23
- const message = err instanceof Error ? err.message : String(err);
24
- return text(`Error: ${message}`);
25
- }
26
-
27
- // ── Memory Tools ──────────────────────────────────────────────────────────────
28
-
29
- server.addTool({
30
- name: "memory_save",
31
- description: "Save a memory document to semantic storage",
32
- parameters: z.object({
33
- information: z.string(),
34
- metadata: z
35
- .object({
36
- source: z.string().optional(),
37
- project: z.string().optional(),
38
- tags: z.array(z.string()).optional(),
39
- importance: z.number().optional(),
40
- sessionId: z.string().optional(),
41
- })
42
- .optional(),
43
- }),
44
- execute: async (args) => {
45
- try {
46
- const result = await memoryRepository.save(args.information, {
47
- ...args.metadata,
48
- createdAt: new Date().toISOString(),
49
- });
50
- return text(`Saved memory: ${result.id}`);
51
- } catch (err) {
52
- return toolErr(err);
53
- }
54
- },
55
- });
56
-
57
- server.addTool({
58
- name: "memory_search",
59
- description: "Search memories by semantic similarity with optional reranking",
60
- parameters: z.object({
61
- query: z.string(),
62
- project: z.string().optional(),
63
- tags: z.array(z.string()).optional(),
64
- limit: z.number().optional().default(5),
65
- rerank: z.boolean().optional().default(hasRemoteReranking),
66
- rerankTopK: z.number().optional().default(20),
67
- }),
68
- execute: async (args) => {
69
- try {
70
- const limit = args.limit ?? 5;
71
- const rerankTopK = args.rerankTopK ?? 20;
72
-
73
- const results = await memoryRepository.search(args.query, {
74
- project: args.project,
75
- tags: args.tags,
76
- limit: args.rerank && hasRemoteReranking ? rerankTopK : limit,
77
- });
78
-
79
- if (args.rerank && hasRemoteReranking && results.length > limit) {
80
- const docs = results.map((r) => r.text);
81
- const reranked = await rerankDocuments(args.query, docs, limit);
82
- const rerankedResults = reranked
83
- .map((r) => ({ ...results[r.index], score: r.relevanceScore }))
84
- .filter((r) => r.text !== undefined);
85
-
86
- return text(JSON.stringify(rerankedResults, null, 2));
87
- }
88
-
89
- return text(JSON.stringify(results, null, 2));
90
- } catch (err) {
91
- return toolErr(err);
92
- }
93
- },
94
- });
95
-
96
- server.addTool({
97
- name: "memory_forget",
98
- description: "Delete memories by IDs or filter",
99
- parameters: z.object({
100
- ids: z.array(z.string()).optional(),
101
- project: z.string().optional(),
102
- tags: z.array(z.string()).optional(),
103
- }),
104
- execute: async (args) => {
105
- try {
106
- if (args.ids && args.ids.length > 0) {
107
- await memoryRepository.delete(args.ids);
108
- return text(`Deleted ${args.ids.length} memories`);
109
- }
110
- if (args.project || args.tags) {
111
- await memoryRepository.deleteByFilter({
112
- project: args.project,
113
- tags: args.tags,
114
- });
115
- return text("Deleted memories by filter");
116
- }
117
- return text("No IDs or filter provided");
118
- } catch (err) {
119
- return toolErr(err);
120
- }
121
- },
122
- });
123
-
124
- server.addTool({
125
- name: "memory_compact",
126
- description: "Compact memories by session or project, removing low-importance ones (importance < 0.6)",
127
- parameters: z.object({
128
- project: z.string().optional(),
129
- sessionId: z.string().optional(),
130
- importanceThreshold: z.number().optional().default(0.6),
131
- }),
132
- execute: async (args) => {
133
- try {
134
- const result = await memoryRepository.compact({
135
- project: args.project,
136
- sessionId: args.sessionId,
137
- importanceThreshold: args.importanceThreshold,
138
- });
139
- return text(`Compacted ${result.compacted} memories`);
140
- } catch (err) {
141
- return toolErr(err);
142
- }
143
- },
144
- });
145
-
146
- server.addTool({
147
- name: "memory_prune",
148
- description: "Delete memories below an importance threshold",
149
- parameters: z.object({
150
- threshold: z.number(),
151
- project: z.string().optional(),
152
- }),
153
- execute: async (args) => {
154
- try {
155
- const result = await memoryRepository.prune({
156
- threshold: args.threshold,
157
- project: args.project,
158
- });
159
- return text(`Pruned ${result.pruned} memories`);
160
- } catch (err) {
161
- return toolErr(err);
162
- }
163
- },
164
- });
165
-
166
- // ── Entity Tools ──────────────────────────────────────────────────────────────
167
-
168
- server.addTool({
169
- name: "entity_create",
170
- description: "Create an entity",
171
- parameters: z.object({
172
- name: z.string(),
173
- entityType: z.string(),
174
- observations: z.array(z.string()).optional().default([]),
175
- metadata: z.record(z.unknown()).optional(),
176
- }),
177
- execute: async (args) => {
178
- try {
179
- const result = await entityRepository.create(
180
- args.name,
181
- args.entityType,
182
- args.observations ?? [],
183
- args.metadata,
184
- );
185
- return text(`Created entity: ${result.name} (${result.id})`);
186
- } catch (err) {
187
- return toolErr(err);
188
- }
189
- },
190
- });
191
-
192
- server.addTool({
193
- name: "entity_search",
194
- description: "Find entities by name or type",
195
- parameters: z.object({
196
- query: z.string().optional(),
197
- entityType: z.string().optional(),
198
- limit: z.number().optional().default(10),
199
- }),
200
- execute: async (args) => {
201
- try {
202
- const results = await entityRepository.search(
203
- args.query,
204
- args.entityType,
205
- args.limit,
206
- );
207
- return text(JSON.stringify(results, null, 2));
208
- } catch (err) {
209
- return toolErr(err);
210
- }
211
- },
212
- });
213
-
214
- server.addTool({
215
- name: "entity_add_observations",
216
- description: "Add observations to an existing entity",
217
- parameters: z.object({
218
- name: z.string(),
219
- observations: z.array(z.string()),
220
- }),
221
- execute: async (args) => {
222
- try {
223
- const result = await entityRepository.addObservations(
224
- args.name,
225
- args.observations,
226
- );
227
- if (!result) return text(`Entity not found: ${args.name}`);
228
- return text(`Added observations to: ${result.name}`);
229
- } catch (err) {
230
- return toolErr(err);
231
- }
232
- },
233
- });
234
-
235
- server.addTool({
236
- name: "entity_get",
237
- description: "Get full entity details by name",
238
- parameters: z.object({
239
- name: z.string(),
240
- }),
241
- execute: async (args) => {
242
- try {
243
- const result = await entityRepository.get(args.name);
244
- if (!result) return text(`Entity not found: ${args.name}`);
245
- return text(JSON.stringify(result, null, 2));
246
- } catch (err) {
247
- return toolErr(err);
248
- }
249
- },
250
- });
251
-
252
- // ── Relation Tools ────────────────────────────────────────────────────────────
253
-
254
- server.addTool({
255
- name: "relation_create",
256
- description: "Create a relation between two entities",
257
- parameters: z.object({
258
- from: z.string(),
259
- relationType: z.string(),
260
- to: z.string(),
261
- metadata: z.record(z.unknown()).optional(),
262
- }),
263
- execute: async (args) => {
264
- try {
265
- const result = await relationRepository.create(
266
- args.from,
267
- args.relationType,
268
- args.to,
269
- args.metadata,
270
- );
271
- return text(
272
- `Created relation: ${args.from} --[${args.relationType}]--> ${args.to} (${result.id})`,
273
- );
274
- } catch (err) {
275
- return toolErr(err);
276
- }
277
- },
278
- });
279
-
280
- server.addTool({
281
- name: "relation_search",
282
- description: "Find relations by source, target, or type",
283
- parameters: z.object({
284
- from: z.string().optional(),
285
- to: z.string().optional(),
286
- relationType: z.string().optional(),
287
- limit: z.number().optional().default(20),
288
- }),
289
- execute: async (args) => {
290
- try {
291
- const results = await relationRepository.search(args, args.limit);
292
- return text(JSON.stringify(results, null, 2));
293
- } catch (err) {
294
- return toolErr(err);
295
- }
296
- },
297
- });
298
-
299
- server.addTool({
300
- name: "relation_delete",
301
- description: "Delete relations",
302
- parameters: z.object({
303
- from: z.string().optional(),
304
- to: z.string().optional(),
305
- relationType: z.string().optional(),
306
- }),
307
- execute: async (args) => {
308
- try {
309
- await relationRepository.delete(args);
310
- return text("Relations deleted");
311
- } catch (err) {
312
- return toolErr(err);
313
- }
314
- },
315
- });
316
-
317
- // ── Memory Update ─────────────────────────────────────────────────────────────
318
-
319
- server.addTool({
320
- name: "memory_update",
321
- description: "Update text or metadata of an existing memory by ID",
322
- parameters: z.object({
323
- id: z.string(),
324
- text: z.string().optional(),
325
- metadata: z
326
- .object({
327
- source: z.string().optional(),
328
- project: z.string().optional(),
329
- tags: z.array(z.string()).optional(),
330
- importance: z.number().optional(),
331
- sessionId: z.string().optional(),
332
- })
333
- .optional(),
334
- }),
335
- execute: async (args) => {
336
- try {
337
- const result = await memoryRepository.update(args.id, {
338
- text: args.text,
339
- metadata: args.metadata,
340
- });
341
- if (!result) return text(`Memory not found: ${args.id}`);
342
- return text(JSON.stringify(result, null, 2));
343
- } catch (err) {
344
- return toolErr(err);
345
- }
346
- },
347
- });
348
-
349
- // ── Memory Scroll / Stats ─────────────────────────────────────────────────────
350
-
351
- server.addTool({
352
- name: "memory_scroll",
353
- description: "Paginate through memories with cursor-based pagination",
354
- parameters: z.object({
355
- project: z.string().optional(),
356
- tags: z.array(z.string()).optional(),
357
- limit: z.number().optional().default(100),
358
- offset: z.string().optional(),
359
- }),
360
- execute: async (args) => {
361
- try {
362
- const { memories, offset } = await memoryRepository.scroll({
363
- project: args.project,
364
- tags: args.tags,
365
- limit: args.limit,
366
- offset: args.offset,
367
- });
368
- return text(JSON.stringify({ memories, nextOffset: offset }, null, 2));
369
- } catch (err) {
370
- return toolErr(err);
371
- }
372
- },
373
- });
374
-
375
- server.addTool({
376
- name: "memory_stats",
377
- description: "Get statistics about the memories collection",
378
- parameters: z.object({}),
379
- execute: async () => {
380
- try {
381
- const stats = await memoryRepository.stats();
382
- return text(JSON.stringify(stats, null, 2));
383
- } catch (err) {
384
- return toolErr(err);
385
- }
386
- },
387
- });
388
-
389
- // ── Entity / Relation Stats ───────────────────────────────────────────────────
390
-
391
- server.addTool({
392
- name: "entity_update",
393
- description: "Update an existing entity by ID",
394
- parameters: z.object({
395
- id: z.string(),
396
- name: z.string().optional(),
397
- entityType: z.string().optional(),
398
- metadata: z.record(z.unknown()).optional(),
399
- }),
400
- execute: async (args) => {
401
- try {
402
- const result = await entityRepository.update(args.id, {
403
- name: args.name,
404
- entityType: args.entityType,
405
- metadata: args.metadata,
406
- });
407
- if (!result) return text(`Entity not found: ${args.id}`);
408
- return text(JSON.stringify(result, null, 2));
409
- } catch (err) {
410
- return toolErr(err);
411
- }
412
- },
413
- });
414
-
415
- server.addTool({
416
- name: "entity_stats",
417
- description: "Get statistics about the entities collection",
418
- parameters: z.object({}),
419
- execute: async () => {
420
- try {
421
- const stats = await entityRepository.stats();
422
- return text(JSON.stringify(stats, null, 2));
423
- } catch (err) {
424
- return toolErr(err);
425
- }
426
- },
427
- });
428
-
429
- server.addTool({
430
- name: "relation_stats",
431
- description: "Get statistics about the relations collection",
432
- parameters: z.object({}),
433
- execute: async () => {
434
- try {
435
- const stats = await relationRepository.stats();
436
- return text(JSON.stringify(stats, null, 2));
437
- } catch (err) {
438
- return toolErr(err);
439
- }
440
- },
441
- });
442
-
443
- // ── List Tools ────────────────────────────────────────────────────────────────
444
-
445
- server.addTool({
446
- name: "memory_note",
447
- description: "Quick-save a note (lightweight memory with auto-tag 'note')",
448
- parameters: z.object({
449
- content: z.string(),
450
- project: z.string().optional(),
451
- tags: z.array(z.string()).optional(),
452
- }),
453
- execute: async (args) => {
454
- try {
455
- const tags = [...(args.tags ?? []), "note"];
456
- const result = await memoryRepository.save(args.content, {
457
- project: args.project,
458
- tags,
459
- createdAt: new Date().toISOString(),
460
- });
461
- return text(`Saved note: ${result.id}`);
462
- } catch (err) {
463
- return toolErr(err);
464
- }
465
- },
466
- });
467
-
468
- server.addTool({
469
- name: "entity_list",
470
- description:
471
- "List all entities, optionally filtered by type, with pagination",
472
- parameters: z.object({
473
- entityType: z.string().optional(),
474
- limit: z.number().optional().default(100),
475
- offset: z.string().optional(),
476
- }),
477
- execute: async (args) => {
478
- try {
479
- const { entities, offset } = await entityRepository.list(
480
- args.entityType,
481
- args.limit,
482
- args.offset,
483
- );
484
- return text(JSON.stringify({ entities, nextOffset: offset }, null, 2));
485
- } catch (err) {
486
- return toolErr(err);
487
- }
488
- },
489
- });
490
-
491
- server.addTool({
492
- name: "relation_list",
493
- description:
494
- "List all relations, optionally filtered by type, with pagination",
495
- parameters: z.object({
496
- relationType: z.string().optional(),
497
- limit: z.number().optional().default(100),
498
- offset: z.string().optional(),
499
- }),
500
- execute: async (args) => {
501
- try {
502
- const { relations, offset } = await relationRepository.list(
503
- args.relationType,
504
- args.limit,
505
- args.offset,
506
- );
507
- return text(JSON.stringify({ relations, nextOffset: offset }, null, 2));
508
- } catch (err) {
509
- return toolErr(err);
510
- }
511
- },
512
- });
513
-
514
- // ── Initialize collections on startup ────────────────────────────────────────
515
-
516
- async function init(retries = 10, delayMs = 3000) {
517
- for (let attempt = 1; attempt <= retries; attempt++) {
518
- try {
519
- await memoryRepository.init();
520
- await entityRepository.init();
521
- await relationRepository.init();
522
- collectionsReady = true;
523
- console.log("Collections initialized");
524
- try {
525
- await warmup();
526
- } catch (err) {
527
- console.warn("Embedding warmup failed (non-fatal):", err);
528
- }
529
- return;
530
- } catch (err) {
531
- if (attempt === retries) {
532
- console.error(`Failed to initialize collections after ${retries} attempts:`, err);
533
- process.exit(1);
534
- }
535
- console.error(`Collection init attempt ${attempt}/${retries} failed, retrying in ${delayMs / 1000}s...`);
536
- await new Promise((resolve) => setTimeout(resolve, delayMs));
537
- }
538
- }
539
- }
540
-
541
- init();
542
-
543
- if (config.transport === "http") {
544
- // Health check server (separate port)
545
- const healthPort = config.port + 1;
546
- createServer((req, res) => {
547
- if (req.url === "/ready") {
548
- if (collectionsReady) {
549
- res.writeHead(200, { "Content-Type": "application/json" });
550
- res.end(JSON.stringify({ status: "ready" }));
551
- } else {
552
- res.writeHead(503, { "Content-Type": "application/json" });
553
- res.end(JSON.stringify({ status: "initializing" }));
554
- }
555
- return;
556
- }
557
- if (req.url === "/health") {
558
- res.writeHead(200, { "Content-Type": "application/json" });
559
- res.end(JSON.stringify({ status: "healthy", uptime: process.uptime() }));
560
- return;
561
- }
562
- res.writeHead(404);
563
- res.end();
564
- }).listen(healthPort, () => {
565
- console.log(
566
- `[grove] Health check server on http://localhost:${healthPort}`,
567
- );
568
- });
569
-
570
- server.start({
571
- transportType: "httpStream",
572
- httpStream: { endpoint: "/mcp", port: config.port },
573
- });
574
- console.log(
575
- `[grove] HTTP server listening on http://localhost:${config.port}/mcp`,
576
- );
577
- } else {
578
- server.start({ transportType: "stdio" });
579
- }
580
-
581
- function shutdown(signal: string) {
582
- console.error(`[grove] Received ${signal}, shutting down...`);
583
- server.stop().then(() => {
584
- console.error("[grove] Server stopped");
585
- process.exit(0);
586
- });
587
- setTimeout(() => {
588
- console.error("[grove] Force exit after timeout");
589
- process.exit(1);
590
- }, 5000);
591
- }
592
-
593
- process.on("SIGTERM", () => shutdown("SIGTERM"));
594
- process.on("SIGINT", () => shutdown("SIGINT"));