|
| 1 | +import type { |
| 2 | + Provider, |
| 3 | + ProviderConfig, |
| 4 | + IngestOptions, |
| 5 | + IngestResult, |
| 6 | + SearchOptions, |
| 7 | + IndexingProgressCallback, |
| 8 | +} from "../../types/provider" |
| 9 | +import type { UnifiedSession } from "../../types/unified" |
| 10 | +import { logger } from "../../utils/logger" |
| 11 | +import { AGENT_MEMORY_PROMPTS } from "./prompts" |
| 12 | + |
| 13 | +/** |
| 14 | + * agent-memory provider for MemoryBench. |
| 15 | + * |
| 16 | + * Connects to a local agent-memory bench server (Python HTTP wrapper). |
| 17 | + * The server manages per-container SQLite databases with semantic embeddings |
| 18 | + * and graph-based memory relationships. |
| 19 | + * |
| 20 | + * Start the server: python -m agent_memory.bench_server --port 9876 |
| 21 | + */ |
| 22 | +export class AgentMemoryProvider implements Provider { |
| 23 | + name = "agent-memory" |
| 24 | + prompts = AGENT_MEMORY_PROMPTS |
| 25 | + concurrency = { |
| 26 | + default: 10, // Local, so moderate concurrency |
| 27 | + } |
| 28 | + private baseUrl: string = "http://127.0.0.1:9876" |
| 29 | + |
| 30 | + async initialize(config: ProviderConfig): Promise<void> { |
| 31 | + if (config.baseUrl) { |
| 32 | + this.baseUrl = config.baseUrl |
| 33 | + } |
| 34 | + |
| 35 | + // Health check |
| 36 | + try { |
| 37 | + const res = await fetch(`${this.baseUrl}/health`) |
| 38 | + if (!res.ok) throw new Error(`HTTP ${res.status}`) |
| 39 | + const data = await res.json() as { status: string } |
| 40 | + logger.info(`Connected to agent-memory bench server: ${data.status}`) |
| 41 | + } catch (e) { |
| 42 | + throw new Error( |
| 43 | + `Cannot connect to agent-memory bench server at ${this.baseUrl}. ` + |
| 44 | + `Start it with: python -m agent_memory.bench_server --port 9876\n` + |
| 45 | + `Error: ${e}` |
| 46 | + ) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + async ingest(sessions: UnifiedSession[], options: IngestOptions): Promise<IngestResult> { |
| 51 | + // Send sessions in batches to avoid overwhelming the server |
| 52 | + const batchSize = 5 |
| 53 | + const allDocIds: string[] = [] |
| 54 | + |
| 55 | + for (let i = 0; i < sessions.length; i += batchSize) { |
| 56 | + const batch = sessions.slice(i, i + batchSize) |
| 57 | + |
| 58 | + const res = await fetch(`${this.baseUrl}/ingest`, { |
| 59 | + method: "POST", |
| 60 | + headers: { "Content-Type": "application/json" }, |
| 61 | + body: JSON.stringify({ |
| 62 | + containerTag: options.containerTag, |
| 63 | + sessions: batch, |
| 64 | + }), |
| 65 | + }) |
| 66 | + |
| 67 | + if (!res.ok) { |
| 68 | + const text = await res.text() |
| 69 | + throw new Error(`Ingest failed: ${text}`) |
| 70 | + } |
| 71 | + |
| 72 | + const data = await res.json() as { documentIds: string[], count: number } |
| 73 | + allDocIds.push(...data.documentIds) |
| 74 | + |
| 75 | + if (i % 20 === 0 && i > 0) { |
| 76 | + logger.info(`Ingested ${i}/${sessions.length} sessions (${allDocIds.length} memories)`) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + logger.info(`Ingested ${sessions.length} sessions → ${allDocIds.length} memories`) |
| 81 | + return { documentIds: allDocIds } |
| 82 | + } |
| 83 | + |
| 84 | + async awaitIndexing( |
| 85 | + result: IngestResult, |
| 86 | + _containerTag: string, |
| 87 | + onProgress?: IndexingProgressCallback |
| 88 | + ): Promise<void> { |
| 89 | + // agent-memory indexes synchronously on ingest, no waiting needed |
| 90 | + const total = result.documentIds.length |
| 91 | + onProgress?.({ |
| 92 | + completedIds: result.documentIds, |
| 93 | + failedIds: [], |
| 94 | + total, |
| 95 | + }) |
| 96 | + } |
| 97 | + |
| 98 | + async search(query: string, options: SearchOptions): Promise<unknown[]> { |
| 99 | + const res = await fetch(`${this.baseUrl}/search`, { |
| 100 | + method: "POST", |
| 101 | + headers: { "Content-Type": "application/json" }, |
| 102 | + body: JSON.stringify({ |
| 103 | + containerTag: options.containerTag, |
| 104 | + query, |
| 105 | + limit: options.limit || 30, |
| 106 | + }), |
| 107 | + }) |
| 108 | + |
| 109 | + if (!res.ok) { |
| 110 | + const text = await res.text() |
| 111 | + throw new Error(`Search failed: ${text}`) |
| 112 | + } |
| 113 | + |
| 114 | + const data = await res.json() as { results: unknown[] } |
| 115 | + return data.results ?? [] |
| 116 | + } |
| 117 | + |
| 118 | + async clear(containerTag: string): Promise<void> { |
| 119 | + const res = await fetch(`${this.baseUrl}/clear`, { |
| 120 | + method: "POST", |
| 121 | + headers: { "Content-Type": "application/json" }, |
| 122 | + body: JSON.stringify({ containerTag }), |
| 123 | + }) |
| 124 | + |
| 125 | + if (!res.ok) { |
| 126 | + logger.warn(`Clear failed for ${containerTag}`) |
| 127 | + } else { |
| 128 | + logger.info(`Cleared memories for: ${containerTag}`) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +export default AgentMemoryProvider |
0 commit comments