agent-working-memory 0.6.1 → 0.7.1

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 (51) hide show
  1. package/README.md +27 -8
  2. package/dist/adapters/common.d.ts.map +1 -1
  3. package/dist/adapters/common.js +9 -1
  4. package/dist/adapters/common.js.map +1 -1
  5. package/dist/api/routes.d.ts.map +1 -1
  6. package/dist/api/routes.js +108 -10
  7. package/dist/api/routes.js.map +1 -1
  8. package/dist/cli.js +103 -103
  9. package/dist/core/auto-tagger.d.ts +29 -0
  10. package/dist/core/auto-tagger.d.ts.map +1 -0
  11. package/dist/core/auto-tagger.js +139 -0
  12. package/dist/core/auto-tagger.js.map +1 -0
  13. package/dist/core/hebbian.d.ts +25 -1
  14. package/dist/core/hebbian.d.ts.map +1 -1
  15. package/dist/core/hebbian.js +71 -3
  16. package/dist/core/hebbian.js.map +1 -1
  17. package/dist/core/query-expander.d.ts.map +1 -1
  18. package/dist/core/query-expander.js.map +1 -1
  19. package/dist/core/reranker.d.ts.map +1 -1
  20. package/dist/core/reranker.js.map +1 -1
  21. package/dist/engine/activation.d.ts +22 -4
  22. package/dist/engine/activation.d.ts.map +1 -1
  23. package/dist/engine/activation.js +136 -73
  24. package/dist/engine/activation.js.map +1 -1
  25. package/dist/engine/consolidation.d.ts +1 -0
  26. package/dist/engine/consolidation.d.ts.map +1 -1
  27. package/dist/engine/consolidation.js +149 -9
  28. package/dist/engine/consolidation.js.map +1 -1
  29. package/dist/index.js +1 -1
  30. package/dist/mcp.js +123 -84
  31. package/dist/mcp.js.map +1 -1
  32. package/dist/storage/sqlite.d.ts +17 -0
  33. package/dist/storage/sqlite.d.ts.map +1 -1
  34. package/dist/storage/sqlite.js +73 -0
  35. package/dist/storage/sqlite.js.map +1 -1
  36. package/dist/types/engram.d.ts +2 -0
  37. package/dist/types/engram.d.ts.map +1 -1
  38. package/package.json +1 -1
  39. package/src/adapters/common.ts +9 -1
  40. package/src/api/routes.ts +723 -600
  41. package/src/cli.ts +719 -719
  42. package/src/core/auto-tagger.ts +168 -0
  43. package/src/core/hebbian.ts +84 -3
  44. package/src/core/query-expander.ts +0 -1
  45. package/src/core/reranker.ts +0 -1
  46. package/src/engine/activation.ts +136 -70
  47. package/src/engine/consolidation.ts +165 -9
  48. package/src/index.ts +199 -199
  49. package/src/mcp.ts +1134 -1099
  50. package/src/storage/sqlite.ts +77 -0
  51. package/src/types/engram.ts +2 -0
@@ -329,6 +329,83 @@ export class EngramStore {
329
329
  return (this.db.prepare(query).all(...params) as any[]).map(r => this.rowToEngram(r));
330
330
  }
331
331
 
332
+ /**
333
+ * Get engrams across multiple agents (workspace-scoped recall).
334
+ * Used when workspace mode is enabled for hive memory sharing.
335
+ */
336
+ getEngramsByAgents(agentIds: string[], stage?: EngramStage, includeRetracted: boolean = false): Engram[] {
337
+ if (agentIds.length === 0) return [];
338
+ if (agentIds.length === 1) return this.getEngramsByAgent(agentIds[0], stage, includeRetracted);
339
+
340
+ const placeholders = agentIds.map(() => '?').join(',');
341
+ let query = `SELECT * FROM engrams WHERE agent_id IN (${placeholders})`;
342
+ const params: any[] = [...agentIds];
343
+
344
+ if (stage) {
345
+ query += ' AND stage = ?';
346
+ params.push(stage);
347
+ }
348
+ if (!includeRetracted) {
349
+ query += ' AND retracted = 0';
350
+ }
351
+
352
+ return (this.db.prepare(query).all(...params) as any[]).map(r => this.rowToEngram(r));
353
+ }
354
+
355
+ /**
356
+ * BM25 search across multiple agents (workspace-scoped).
357
+ */
358
+ searchBM25WithRankMultiAgent(agentIds: string[], query: string, limit: number = 10): { engram: Engram; bm25Score: number }[] {
359
+ if (agentIds.length === 0) return [];
360
+ if (agentIds.length === 1) return this.searchBM25WithRank(agentIds[0], query, limit);
361
+
362
+ const sanitized = query
363
+ .replace(/[^\w\s]/g, '')
364
+ .split(/\s+/)
365
+ .filter(w => w.length > 1)
366
+ .map(w => `"${w}"`)
367
+ .join(' OR ');
368
+
369
+ if (!sanitized) return [];
370
+
371
+ try {
372
+ const placeholders = agentIds.map(() => '?').join(',');
373
+ const rows = this.db.prepare(`
374
+ SELECT e.*, rank FROM engrams e
375
+ JOIN engrams_fts ON e.rowid = engrams_fts.rowid
376
+ WHERE engrams_fts MATCH ? AND e.agent_id IN (${placeholders}) AND e.retracted = 0
377
+ ORDER BY rank
378
+ LIMIT ?
379
+ `).all(sanitized, ...agentIds, limit) as any[];
380
+
381
+ return rows.map(r => ({
382
+ engram: this.rowToEngram(r),
383
+ bm25Score: Math.abs(r.rank ?? 0) / (1 + Math.abs(r.rank ?? 0)),
384
+ }));
385
+ } catch {
386
+ return [];
387
+ }
388
+ }
389
+
390
+ /**
391
+ * Get all distinct agent IDs that share a workspace (requires coord_agents table).
392
+ * Returns just the queried agentId if coordination tables don't exist.
393
+ */
394
+ getWorkspaceAgentIds(agentId: string, workspace: string): string[] {
395
+ try {
396
+ const rows = this.db.prepare(
397
+ `SELECT DISTINCT id FROM coord_agents WHERE workspace = ? AND status != 'dead'`
398
+ ).all(workspace) as Array<{ id: string }>;
399
+ const ids = rows.map(r => r.id);
400
+ // Ensure the querying agent is always included
401
+ if (!ids.includes(agentId)) ids.push(agentId);
402
+ return ids;
403
+ } catch {
404
+ // No coordination tables — fall back to single agent
405
+ return [agentId];
406
+ }
407
+ }
408
+
332
409
  /**
333
410
  * Touch an engram: increment access count, update last_accessed, and
334
411
  * nudge confidence upward. Each retrieval is weak evidence the memory
@@ -186,6 +186,8 @@ export interface ActivationQuery {
186
186
  internal?: boolean; // Skip access count increment, Hebbian update, and event logging (for system calls)
187
187
  memoryType?: MemoryType; // Filter by memory type (episodic, semantic, procedural)
188
188
  mode?: QueryMode; // Pipeline mode — 'auto' by default
189
+ workspace?: string; // Search across all agents in this workspace (hive mode). If unset, agent-scoped only.
190
+ bm25Only?: boolean; // Skip embedding — fast text-only retrieval for bulk/benchmark scenarios
189
191
  }
190
192
 
191
193
  /**