darwin-agents 0.4.6 → 0.4.7
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.
- package/CHANGELOG.md +224 -0
- package/README.md +64 -1
- package/dist/examples/closed-loop-feedback.d.ts +100 -0
- package/dist/examples/closed-loop-feedback.d.ts.map +1 -0
- package/dist/examples/closed-loop-feedback.js +199 -0
- package/dist/examples/closed-loop-feedback.js.map +1 -0
- package/dist/examples/custom-agent.d.ts +10 -0
- package/dist/examples/custom-agent.d.ts.map +1 -0
- package/dist/examples/custom-agent.js +98 -0
- package/dist/examples/custom-agent.js.map +1 -0
- package/dist/examples/mcp-memory-bridge.d.ts +183 -0
- package/dist/examples/mcp-memory-bridge.d.ts.map +1 -0
- package/dist/examples/mcp-memory-bridge.js +542 -0
- package/dist/examples/mcp-memory-bridge.js.map +1 -0
- package/dist/examples/memory-darwin-integration.d.ts +63 -0
- package/dist/examples/memory-darwin-integration.d.ts.map +1 -0
- package/dist/examples/memory-darwin-integration.js +123 -0
- package/dist/examples/memory-darwin-integration.js.map +1 -0
- package/dist/examples/staleness-monitor.d.ts +45 -0
- package/dist/examples/staleness-monitor.d.ts.map +1 -0
- package/dist/examples/staleness-monitor.js +127 -0
- package/dist/examples/staleness-monitor.js.map +1 -0
- package/dist/src/agents/analyst.d.ts.map +1 -0
- package/dist/src/agents/analyst.js.map +1 -0
- package/dist/src/agents/blog-writer.d.ts.map +1 -0
- package/dist/src/agents/blog-writer.js.map +1 -0
- package/dist/src/agents/critic.d.ts.map +1 -0
- package/dist/src/agents/critic.js.map +1 -0
- package/dist/src/agents/index.d.ts.map +1 -0
- package/dist/src/agents/index.js.map +1 -0
- package/dist/src/agents/investigator-critic.d.ts.map +1 -0
- package/dist/src/agents/investigator-critic.js.map +1 -0
- package/dist/src/agents/investigator.d.ts.map +1 -0
- package/dist/src/agents/investigator.js.map +1 -0
- package/dist/src/agents/marketing.d.ts.map +1 -0
- package/dist/src/agents/marketing.js.map +1 -0
- package/dist/src/agents/researcher.d.ts.map +1 -0
- package/dist/src/agents/researcher.js.map +1 -0
- package/dist/src/agents/writer.d.ts.map +1 -0
- package/dist/src/agents/writer.js.map +1 -0
- package/dist/src/cli/create.d.ts.map +1 -0
- package/dist/src/cli/create.js.map +1 -0
- package/dist/src/cli/evolve.d.ts.map +1 -0
- package/dist/src/cli/evolve.js.map +1 -0
- package/dist/src/cli/index.d.ts.map +1 -0
- package/dist/src/cli/index.js.map +1 -0
- package/dist/src/cli/init.d.ts.map +1 -0
- package/dist/src/cli/init.js.map +1 -0
- package/dist/src/cli/run.d.ts.map +1 -0
- package/dist/src/cli/run.js.map +1 -0
- package/dist/src/cli/status.d.ts.map +1 -0
- package/dist/src/cli/status.js.map +1 -0
- package/dist/src/core/agent.d.ts.map +1 -0
- package/dist/src/core/agent.js.map +1 -0
- package/dist/src/core/runner.d.ts.map +1 -0
- package/dist/src/core/runner.js.map +1 -0
- package/dist/src/evolution/loop.d.ts.map +1 -0
- package/dist/src/evolution/loop.js.map +1 -0
- package/dist/src/evolution/multi-critic.d.ts.map +1 -0
- package/dist/src/evolution/multi-critic.js.map +1 -0
- package/dist/src/evolution/notifications.d.ts.map +1 -0
- package/dist/src/evolution/notifications.js.map +1 -0
- package/dist/src/evolution/optimizer.d.ts.map +1 -0
- package/dist/src/evolution/optimizer.js.map +1 -0
- package/dist/src/evolution/patterns.d.ts.map +1 -0
- package/dist/src/evolution/patterns.js.map +1 -0
- package/dist/src/evolution/safety.d.ts.map +1 -0
- package/dist/src/evolution/safety.js.map +1 -0
- package/dist/src/evolution/tracker.d.ts.map +1 -0
- package/dist/src/evolution/tracker.js.map +1 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/memory/index.d.ts.map +1 -0
- package/dist/src/memory/index.js.map +1 -0
- package/dist/src/memory/postgres-memory.d.ts.map +1 -0
- package/dist/src/memory/postgres-memory.js.map +1 -0
- package/dist/src/memory/sqlite-memory.d.ts.map +1 -0
- package/dist/src/memory/sqlite-memory.js.map +1 -0
- package/dist/src/providers/anthropic.d.ts.map +1 -0
- package/dist/src/providers/anthropic.js.map +1 -0
- package/dist/src/providers/claude-cli.d.ts.map +1 -0
- package/dist/src/providers/claude-cli.js.map +1 -0
- package/dist/src/providers/index.d.ts.map +1 -0
- package/dist/src/providers/index.js.map +1 -0
- package/dist/src/providers/ollama.d.ts.map +1 -0
- package/dist/src/providers/ollama.js.map +1 -0
- package/dist/src/providers/openai.d.ts.map +1 -0
- package/dist/src/providers/openai.js.map +1 -0
- package/dist/src/providers/types.d.ts.map +1 -0
- package/dist/src/providers/types.js.map +1 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js.map +1 -0
- package/package.json +19 -1
- package/dist/agents/analyst.d.ts.map +0 -1
- package/dist/agents/analyst.js.map +0 -1
- package/dist/agents/blog-writer.d.ts.map +0 -1
- package/dist/agents/blog-writer.js.map +0 -1
- package/dist/agents/critic.d.ts.map +0 -1
- package/dist/agents/critic.js.map +0 -1
- package/dist/agents/index.d.ts.map +0 -1
- package/dist/agents/index.js.map +0 -1
- package/dist/agents/investigator-critic.d.ts.map +0 -1
- package/dist/agents/investigator-critic.js.map +0 -1
- package/dist/agents/investigator.d.ts.map +0 -1
- package/dist/agents/investigator.js.map +0 -1
- package/dist/agents/marketing.d.ts.map +0 -1
- package/dist/agents/marketing.js.map +0 -1
- package/dist/agents/researcher.d.ts.map +0 -1
- package/dist/agents/researcher.js.map +0 -1
- package/dist/agents/writer.d.ts.map +0 -1
- package/dist/agents/writer.js.map +0 -1
- package/dist/cli/create.d.ts.map +0 -1
- package/dist/cli/create.js.map +0 -1
- package/dist/cli/evolve.d.ts.map +0 -1
- package/dist/cli/evolve.js.map +0 -1
- package/dist/cli/index.d.ts.map +0 -1
- package/dist/cli/index.js.map +0 -1
- package/dist/cli/init.d.ts.map +0 -1
- package/dist/cli/init.js.map +0 -1
- package/dist/cli/run.d.ts.map +0 -1
- package/dist/cli/run.js.map +0 -1
- package/dist/cli/status.d.ts.map +0 -1
- package/dist/cli/status.js.map +0 -1
- package/dist/core/agent.d.ts.map +0 -1
- package/dist/core/agent.js.map +0 -1
- package/dist/core/runner.d.ts.map +0 -1
- package/dist/core/runner.js.map +0 -1
- package/dist/evolution/loop.d.ts.map +0 -1
- package/dist/evolution/loop.js.map +0 -1
- package/dist/evolution/multi-critic.d.ts.map +0 -1
- package/dist/evolution/multi-critic.js.map +0 -1
- package/dist/evolution/notifications.d.ts.map +0 -1
- package/dist/evolution/notifications.js.map +0 -1
- package/dist/evolution/optimizer.d.ts.map +0 -1
- package/dist/evolution/optimizer.js.map +0 -1
- package/dist/evolution/patterns.d.ts.map +0 -1
- package/dist/evolution/patterns.js.map +0 -1
- package/dist/evolution/safety.d.ts.map +0 -1
- package/dist/evolution/safety.js.map +0 -1
- package/dist/evolution/tracker.d.ts.map +0 -1
- package/dist/evolution/tracker.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/memory/index.d.ts.map +0 -1
- package/dist/memory/index.js.map +0 -1
- package/dist/memory/postgres-memory.d.ts.map +0 -1
- package/dist/memory/postgres-memory.js.map +0 -1
- package/dist/memory/sqlite-memory.d.ts.map +0 -1
- package/dist/memory/sqlite-memory.js.map +0 -1
- package/dist/providers/anthropic.d.ts.map +0 -1
- package/dist/providers/anthropic.js.map +0 -1
- package/dist/providers/claude-cli.d.ts.map +0 -1
- package/dist/providers/claude-cli.js.map +0 -1
- package/dist/providers/index.d.ts.map +0 -1
- package/dist/providers/index.js.map +0 -1
- package/dist/providers/ollama.d.ts.map +0 -1
- package/dist/providers/ollama.js.map +0 -1
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js.map +0 -1
- package/dist/providers/types.d.ts.map +0 -1
- package/dist/providers/types.js.map +0 -1
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js.map +0 -1
- /package/dist/{agents → src/agents}/analyst.d.ts +0 -0
- /package/dist/{agents → src/agents}/analyst.js +0 -0
- /package/dist/{agents → src/agents}/blog-writer.d.ts +0 -0
- /package/dist/{agents → src/agents}/blog-writer.js +0 -0
- /package/dist/{agents → src/agents}/critic.d.ts +0 -0
- /package/dist/{agents → src/agents}/critic.js +0 -0
- /package/dist/{agents → src/agents}/index.d.ts +0 -0
- /package/dist/{agents → src/agents}/index.js +0 -0
- /package/dist/{agents → src/agents}/investigator-critic.d.ts +0 -0
- /package/dist/{agents → src/agents}/investigator-critic.js +0 -0
- /package/dist/{agents → src/agents}/investigator.d.ts +0 -0
- /package/dist/{agents → src/agents}/investigator.js +0 -0
- /package/dist/{agents → src/agents}/marketing.d.ts +0 -0
- /package/dist/{agents → src/agents}/marketing.js +0 -0
- /package/dist/{agents → src/agents}/researcher.d.ts +0 -0
- /package/dist/{agents → src/agents}/researcher.js +0 -0
- /package/dist/{agents → src/agents}/writer.d.ts +0 -0
- /package/dist/{agents → src/agents}/writer.js +0 -0
- /package/dist/{cli → src/cli}/create.d.ts +0 -0
- /package/dist/{cli → src/cli}/create.js +0 -0
- /package/dist/{cli → src/cli}/evolve.d.ts +0 -0
- /package/dist/{cli → src/cli}/evolve.js +0 -0
- /package/dist/{cli → src/cli}/index.d.ts +0 -0
- /package/dist/{cli → src/cli}/index.js +0 -0
- /package/dist/{cli → src/cli}/init.d.ts +0 -0
- /package/dist/{cli → src/cli}/init.js +0 -0
- /package/dist/{cli → src/cli}/run.d.ts +0 -0
- /package/dist/{cli → src/cli}/run.js +0 -0
- /package/dist/{cli → src/cli}/status.d.ts +0 -0
- /package/dist/{cli → src/cli}/status.js +0 -0
- /package/dist/{core → src/core}/agent.d.ts +0 -0
- /package/dist/{core → src/core}/agent.js +0 -0
- /package/dist/{core → src/core}/runner.d.ts +0 -0
- /package/dist/{core → src/core}/runner.js +0 -0
- /package/dist/{evolution → src/evolution}/loop.d.ts +0 -0
- /package/dist/{evolution → src/evolution}/loop.js +0 -0
- /package/dist/{evolution → src/evolution}/multi-critic.d.ts +0 -0
- /package/dist/{evolution → src/evolution}/multi-critic.js +0 -0
- /package/dist/{evolution → src/evolution}/notifications.d.ts +0 -0
- /package/dist/{evolution → src/evolution}/notifications.js +0 -0
- /package/dist/{evolution → src/evolution}/optimizer.d.ts +0 -0
- /package/dist/{evolution → src/evolution}/optimizer.js +0 -0
- /package/dist/{evolution → src/evolution}/patterns.d.ts +0 -0
- /package/dist/{evolution → src/evolution}/patterns.js +0 -0
- /package/dist/{evolution → src/evolution}/safety.d.ts +0 -0
- /package/dist/{evolution → src/evolution}/safety.js +0 -0
- /package/dist/{evolution → src/evolution}/tracker.d.ts +0 -0
- /package/dist/{evolution → src/evolution}/tracker.js +0 -0
- /package/dist/{index.d.ts → src/index.d.ts} +0 -0
- /package/dist/{index.js → src/index.js} +0 -0
- /package/dist/{memory → src/memory}/index.d.ts +0 -0
- /package/dist/{memory → src/memory}/index.js +0 -0
- /package/dist/{memory → src/memory}/postgres-memory.d.ts +0 -0
- /package/dist/{memory → src/memory}/postgres-memory.js +0 -0
- /package/dist/{memory → src/memory}/sqlite-memory.d.ts +0 -0
- /package/dist/{memory → src/memory}/sqlite-memory.js +0 -0
- /package/dist/{providers → src/providers}/anthropic.d.ts +0 -0
- /package/dist/{providers → src/providers}/anthropic.js +0 -0
- /package/dist/{providers → src/providers}/claude-cli.d.ts +0 -0
- /package/dist/{providers → src/providers}/claude-cli.js +0 -0
- /package/dist/{providers → src/providers}/index.d.ts +0 -0
- /package/dist/{providers → src/providers}/index.js +0 -0
- /package/dist/{providers → src/providers}/ollama.d.ts +0 -0
- /package/dist/{providers → src/providers}/ollama.js +0 -0
- /package/dist/{providers → src/providers}/openai.d.ts +0 -0
- /package/dist/{providers → src/providers}/openai.js +0 -0
- /package/dist/{providers → src/providers}/types.d.ts +0 -0
- /package/dist/{providers → src/providers}/types.js +0 -0
- /package/dist/{types.d.ts → src/types.d.ts} +0 -0
- /package/dist/{types.js → src/types.js} +0 -0
|
@@ -0,0 +1,542 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example: Generic MCP-Memory Bridge (Darwin → any MCP-compliant memory server)
|
|
3
|
+
*
|
|
4
|
+
* Wraps the `FeedbackStore` interface from closed-loop-feedback.ts with a
|
|
5
|
+
* thin JSON-RPC 2.0 client for MCP. Works with any MCP server that exposes
|
|
6
|
+
* a write tool (e.g. `memory_learn`) and a read tool (e.g. `memory_search`).
|
|
7
|
+
*
|
|
8
|
+
* Default wiring targets `@studiomeyer/local-memory-mcp` (zero-config, lives
|
|
9
|
+
* in a single SQLite file under the OS data dir, no cloud, no API keys).
|
|
10
|
+
* Drop in Mem0 / Zep / Letta / Cognee / your own self-hosted MCP server by
|
|
11
|
+
* overriding `writeTool` / `readTool` and providing schema mappers.
|
|
12
|
+
*
|
|
13
|
+
* Why raw JSON-RPC and not @modelcontextprotocol/sdk?
|
|
14
|
+
* - Darwin keeps a "zero hard deps" policy (peerDependencies only).
|
|
15
|
+
* - MCP wire protocol is three messages: initialize, tools/list, tools/call.
|
|
16
|
+
* - Keeps the bridge testable without a mock SDK.
|
|
17
|
+
*
|
|
18
|
+
* Why a single bridge instead of per-provider clients?
|
|
19
|
+
* - The wire is the same. Only tool names and arg/result shapes vary.
|
|
20
|
+
* - One reconnect / lifecycle path, one place to harden timeouts.
|
|
21
|
+
*
|
|
22
|
+
* Run demo (requires `@studiomeyer/local-memory-mcp` installed):
|
|
23
|
+
* npm install -g @studiomeyer/local-memory-mcp
|
|
24
|
+
* npx tsx examples/mcp-memory-bridge.ts
|
|
25
|
+
*/
|
|
26
|
+
import { spawn } from 'node:child_process';
|
|
27
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
28
|
+
// ─── Defaults ────────────────────────────────────────
|
|
29
|
+
const DEFAULT_WRITE_TOOL = 'memory_learn';
|
|
30
|
+
const DEFAULT_READ_TOOL = 'memory_search';
|
|
31
|
+
const DEFAULT_REQUEST_TIMEOUT_MS = 10_000;
|
|
32
|
+
const DEFAULT_MAX_RESPAWN = 1;
|
|
33
|
+
const DEFAULT_HTTP_MAX_RETRIES = 2;
|
|
34
|
+
const DEFAULT_PROTOCOL_VERSION = '2025-11-25';
|
|
35
|
+
/** Default mapping for `memory_learn` shape (local-memory-mcp, mcp-nex). */
|
|
36
|
+
export function defaultMapWriteArgs(rec) {
|
|
37
|
+
return {
|
|
38
|
+
category: rec.polarity === 'mistake' ? 'mistake' : 'pattern',
|
|
39
|
+
content: rec.content,
|
|
40
|
+
tags: rec.tags,
|
|
41
|
+
confidence: rec.confidence,
|
|
42
|
+
source: 'automated',
|
|
43
|
+
memoryType: 'semantic',
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Default mapping for `memory_search` shape.
|
|
48
|
+
* Accepts both the raw MCP CallToolResult shape `{content:[{text:JSON}]}`
|
|
49
|
+
* and a pre-parsed object — that keeps the parser tolerant when a custom
|
|
50
|
+
* server returns structured content directly.
|
|
51
|
+
*/
|
|
52
|
+
export function defaultMapReadResult(raw) {
|
|
53
|
+
const parsed = extractStructured(raw);
|
|
54
|
+
if (!parsed || typeof parsed !== 'object')
|
|
55
|
+
return [];
|
|
56
|
+
// Common envelope shapes observed in the wild across MCP-Memory servers:
|
|
57
|
+
// { success, data: { results: [{ body, ... }] } }
|
|
58
|
+
// { success, data: { results: [{ content, tags }] } }
|
|
59
|
+
// { results: [...] }
|
|
60
|
+
// [...] (plain array)
|
|
61
|
+
const envelope = parsed;
|
|
62
|
+
const data = (envelope.data ?? envelope);
|
|
63
|
+
const results = (data.results ?? envelope.results ?? envelope);
|
|
64
|
+
if (!Array.isArray(results))
|
|
65
|
+
return [];
|
|
66
|
+
const out = [];
|
|
67
|
+
for (const item of results) {
|
|
68
|
+
if (!item || typeof item !== 'object')
|
|
69
|
+
continue;
|
|
70
|
+
const row = item;
|
|
71
|
+
const content = typeof row.content === 'string'
|
|
72
|
+
? row.content
|
|
73
|
+
: typeof row.body === 'string'
|
|
74
|
+
? row.body
|
|
75
|
+
: typeof row.text === 'string'
|
|
76
|
+
? row.text
|
|
77
|
+
: null;
|
|
78
|
+
if (!content)
|
|
79
|
+
continue;
|
|
80
|
+
const tags = Array.isArray(row.tags)
|
|
81
|
+
? row.tags.filter((t) => typeof t === 'string')
|
|
82
|
+
: [];
|
|
83
|
+
const scoreRaw = row.score ?? row.rank ?? row.rrfScore;
|
|
84
|
+
const score = typeof scoreRaw === 'number' && Number.isFinite(scoreRaw) ? scoreRaw : undefined;
|
|
85
|
+
out.push({ content, tags, score });
|
|
86
|
+
}
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
/** Pull structured payload out of an MCP CallToolResult envelope. */
|
|
90
|
+
function extractStructured(raw) {
|
|
91
|
+
if (raw == null || typeof raw !== 'object')
|
|
92
|
+
return raw;
|
|
93
|
+
const r = raw;
|
|
94
|
+
if (Array.isArray(r.content)) {
|
|
95
|
+
for (const block of r.content) {
|
|
96
|
+
if (block?.type === 'text' && typeof block.text === 'string') {
|
|
97
|
+
try {
|
|
98
|
+
return JSON.parse(block.text);
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return block.text;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if ('structuredContent' in r && r.structuredContent != null)
|
|
107
|
+
return r.structuredContent;
|
|
108
|
+
return raw;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Generic MCP-Memory bridge.
|
|
112
|
+
*
|
|
113
|
+
* Lifecycle:
|
|
114
|
+
* const m = openLocalMemory(); // or openRemoteMemory(...)
|
|
115
|
+
* await m.save({...}); // FeedbackStore.save
|
|
116
|
+
* const lessons = await m.fetchRelevant({ query: 'topic', limit: 5 });
|
|
117
|
+
* await m.close();
|
|
118
|
+
*
|
|
119
|
+
* Errors in save/fetch are swallowed at the FeedbackStore level (the
|
|
120
|
+
* closed-loop callsite logs them) — the bridge surfaces them as thrown
|
|
121
|
+
* Errors so the caller decides whether to fail-loud or fail-quiet.
|
|
122
|
+
*/
|
|
123
|
+
export class McpMemoryBridge {
|
|
124
|
+
config;
|
|
125
|
+
child = null;
|
|
126
|
+
nextId = 1;
|
|
127
|
+
pending = new Map();
|
|
128
|
+
stdoutBuffer = '';
|
|
129
|
+
respawnCount = 0;
|
|
130
|
+
initialized = false;
|
|
131
|
+
initInFlight = null;
|
|
132
|
+
closed = false;
|
|
133
|
+
constructor(config) {
|
|
134
|
+
this.config = {
|
|
135
|
+
transport: config.transport,
|
|
136
|
+
endpoint: config.endpoint,
|
|
137
|
+
authHeader: config.authHeader,
|
|
138
|
+
writeTool: config.writeTool ?? DEFAULT_WRITE_TOOL,
|
|
139
|
+
readTool: config.readTool ?? DEFAULT_READ_TOOL,
|
|
140
|
+
mapWriteArgs: config.mapWriteArgs ?? defaultMapWriteArgs,
|
|
141
|
+
mapReadResult: config.mapReadResult ?? defaultMapReadResult,
|
|
142
|
+
requestTimeoutMs: config.requestTimeoutMs ?? DEFAULT_REQUEST_TIMEOUT_MS,
|
|
143
|
+
maxRespawn: config.maxRespawn ?? DEFAULT_MAX_RESPAWN,
|
|
144
|
+
httpMaxRetries: config.httpMaxRetries ?? DEFAULT_HTTP_MAX_RETRIES,
|
|
145
|
+
protocolVersion: config.protocolVersion ?? DEFAULT_PROTOCOL_VERSION,
|
|
146
|
+
logger: config.logger ?? {
|
|
147
|
+
warn: (msg) => console.warn(`[mcp-memory-bridge] ${msg}`),
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
// ── FeedbackStore.save ─────────────────────────────
|
|
152
|
+
async save(record) {
|
|
153
|
+
await this.callTool(this.config.writeTool, this.config.mapWriteArgs(record));
|
|
154
|
+
}
|
|
155
|
+
// ── RetrievableFeedbackStore.fetchRelevant ─────────
|
|
156
|
+
async fetchRelevant(queryOrOpts, legacyLimit) {
|
|
157
|
+
const opts = typeof queryOrOpts === 'string'
|
|
158
|
+
? { query: queryOrOpts, limit: legacyLimit }
|
|
159
|
+
: queryOrOpts;
|
|
160
|
+
const args = { query: opts.query, limit: opts.limit ?? 5 };
|
|
161
|
+
if (opts.tags && opts.tags.length > 0)
|
|
162
|
+
args.tags = opts.tags;
|
|
163
|
+
const result = await this.callTool(this.config.readTool, args);
|
|
164
|
+
return this.config.mapReadResult(result);
|
|
165
|
+
}
|
|
166
|
+
// ── Lifecycle ──────────────────────────────────────
|
|
167
|
+
async close() {
|
|
168
|
+
if (this.closed)
|
|
169
|
+
return;
|
|
170
|
+
this.closed = true;
|
|
171
|
+
this.initInFlight = null; // F5: cleared so callers awaiting a stale promise hit closed-bridge guard
|
|
172
|
+
if (this.child) {
|
|
173
|
+
const c = this.child;
|
|
174
|
+
this.child = null;
|
|
175
|
+
try {
|
|
176
|
+
c.kill('SIGTERM');
|
|
177
|
+
}
|
|
178
|
+
catch {
|
|
179
|
+
// ignore — already dead
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
for (const [, p] of this.pending) {
|
|
183
|
+
clearTimeout(p.timer);
|
|
184
|
+
p.reject(new Error('bridge closed'));
|
|
185
|
+
}
|
|
186
|
+
this.pending.clear();
|
|
187
|
+
}
|
|
188
|
+
// ── Internals ──────────────────────────────────────
|
|
189
|
+
async ensureReady() {
|
|
190
|
+
if (this.closed)
|
|
191
|
+
throw new Error('bridge is closed');
|
|
192
|
+
if (this.initialized)
|
|
193
|
+
return;
|
|
194
|
+
if (!this.initInFlight) {
|
|
195
|
+
// F4: do NOT auto-clear initInFlight in .catch. Leave the rejected
|
|
196
|
+
// promise in place so concurrent awaiters all see the same failure;
|
|
197
|
+
// the next caller after the failure makes a fresh attempt.
|
|
198
|
+
this.initInFlight = this.initialize();
|
|
199
|
+
}
|
|
200
|
+
// Snapshot the promise reference so concurrent failed-init callers
|
|
201
|
+
// don't wipe a newer in-flight promise on cleanup (R2 narrow race).
|
|
202
|
+
const myPromise = this.initInFlight;
|
|
203
|
+
try {
|
|
204
|
+
await myPromise;
|
|
205
|
+
}
|
|
206
|
+
catch (err) {
|
|
207
|
+
// Reset only if no fresher attempt has replaced ours.
|
|
208
|
+
if (!this.initialized && this.initInFlight === myPromise) {
|
|
209
|
+
this.initInFlight = null;
|
|
210
|
+
}
|
|
211
|
+
throw err;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
async initialize() {
|
|
215
|
+
if (this.config.transport === 'stdio') {
|
|
216
|
+
this.spawnStdio();
|
|
217
|
+
}
|
|
218
|
+
// initialize handshake (per MCP spec 2025-11-25)
|
|
219
|
+
await this.rpc('initialize', {
|
|
220
|
+
protocolVersion: this.config.protocolVersion,
|
|
221
|
+
capabilities: {},
|
|
222
|
+
clientInfo: { name: 'darwin-mcp-memory-bridge', version: '0.4.7' },
|
|
223
|
+
});
|
|
224
|
+
// best-effort notify; the server doesn't reply to notifications
|
|
225
|
+
if (this.config.transport === 'stdio' && this.child) {
|
|
226
|
+
this.sendStdio({
|
|
227
|
+
jsonrpc: '2.0',
|
|
228
|
+
method: 'notifications/initialized',
|
|
229
|
+
params: {},
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
this.initialized = true;
|
|
233
|
+
// F4: keep initInFlight set to the resolved promise; ensureReady's
|
|
234
|
+
// early-return path on `initialized=true` covers all subsequent calls.
|
|
235
|
+
// onChildExit nulls it on crash, close() nulls it on shutdown.
|
|
236
|
+
}
|
|
237
|
+
spawnStdio() {
|
|
238
|
+
const cmd = Array.isArray(this.config.endpoint)
|
|
239
|
+
? this.config.endpoint
|
|
240
|
+
: [this.config.endpoint];
|
|
241
|
+
if (cmd.length === 0)
|
|
242
|
+
throw new Error('stdio endpoint must have at least one entry');
|
|
243
|
+
const [bin, ...args] = cmd;
|
|
244
|
+
const child = spawn(bin, args, {
|
|
245
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
246
|
+
// local-memory-mcp logs to stderr; we forward it through the logger
|
|
247
|
+
});
|
|
248
|
+
child.stdout.setEncoding('utf8');
|
|
249
|
+
child.stdout.on('data', (chunk) => this.onStdoutData(chunk));
|
|
250
|
+
child.stderr.setEncoding('utf8');
|
|
251
|
+
child.stderr.on('data', (chunk) => {
|
|
252
|
+
this.config.logger.debug?.(`stderr: ${chunk.trimEnd()}`);
|
|
253
|
+
});
|
|
254
|
+
child.on('exit', (code, signal) => this.onChildExit(code, signal));
|
|
255
|
+
child.on('error', (err) => {
|
|
256
|
+
this.config.logger.warn(`spawn error: ${err.message}`);
|
|
257
|
+
});
|
|
258
|
+
// F3: stdin EPIPE on a dying child emits as a stream error event. Without
|
|
259
|
+
// a handler, Node treats it as an uncaught exception and crashes the
|
|
260
|
+
// host process. Catch and log; the next RPC will see child=null and
|
|
261
|
+
// ensureReady triggers a clean respawn.
|
|
262
|
+
child.stdin.on('error', (err) => {
|
|
263
|
+
this.config.logger.warn(`stdin error (likely race with child exit): ${err.message}`);
|
|
264
|
+
});
|
|
265
|
+
this.child = child;
|
|
266
|
+
}
|
|
267
|
+
onChildExit(code, signal) {
|
|
268
|
+
const wasInitialized = this.initialized;
|
|
269
|
+
this.initialized = false;
|
|
270
|
+
this.initInFlight = null;
|
|
271
|
+
this.child = null;
|
|
272
|
+
const reason = `child exited (code=${code}, signal=${signal})`;
|
|
273
|
+
for (const [, p] of this.pending) {
|
|
274
|
+
clearTimeout(p.timer);
|
|
275
|
+
p.reject(new Error(reason));
|
|
276
|
+
}
|
|
277
|
+
this.pending.clear();
|
|
278
|
+
if (this.closed)
|
|
279
|
+
return;
|
|
280
|
+
if (wasInitialized && this.respawnCount < this.config.maxRespawn) {
|
|
281
|
+
this.respawnCount += 1;
|
|
282
|
+
this.config.logger.warn(`${reason}; will respawn on next call`);
|
|
283
|
+
}
|
|
284
|
+
else if (wasInitialized) {
|
|
285
|
+
this.config.logger.warn(`${reason}; respawn budget exhausted`);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
onStdoutData(chunk) {
|
|
289
|
+
this.stdoutBuffer += chunk;
|
|
290
|
+
// MCP stdio framing: one JSON-RPC message per newline-delimited line.
|
|
291
|
+
let idx;
|
|
292
|
+
while ((idx = this.stdoutBuffer.indexOf('\n')) >= 0) {
|
|
293
|
+
const line = this.stdoutBuffer.slice(0, idx).trim();
|
|
294
|
+
this.stdoutBuffer = this.stdoutBuffer.slice(idx + 1);
|
|
295
|
+
if (!line)
|
|
296
|
+
continue;
|
|
297
|
+
this.handleIncomingLine(line);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
handleIncomingLine(line) {
|
|
301
|
+
let msg;
|
|
302
|
+
try {
|
|
303
|
+
msg = JSON.parse(line);
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
this.config.logger.warn(`unparseable line: ${line.slice(0, 200)}`);
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
if (typeof msg.id !== 'number') {
|
|
310
|
+
// notification or progress — ignore for now
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const pending = this.pending.get(msg.id);
|
|
314
|
+
if (!pending)
|
|
315
|
+
return;
|
|
316
|
+
this.pending.delete(msg.id);
|
|
317
|
+
clearTimeout(pending.timer);
|
|
318
|
+
if (msg.error) {
|
|
319
|
+
const err = msg.error;
|
|
320
|
+
pending.reject(new Error(`mcp error ${err.code ?? '?'}: ${err.message ?? 'unknown'}`));
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
pending.resolve(msg.result);
|
|
324
|
+
}
|
|
325
|
+
sendStdio(payload) {
|
|
326
|
+
if (!this.child)
|
|
327
|
+
throw new Error('stdio child not running');
|
|
328
|
+
try {
|
|
329
|
+
this.child.stdin.write(`${JSON.stringify(payload)}\n`);
|
|
330
|
+
}
|
|
331
|
+
catch (err) {
|
|
332
|
+
// ERR_STREAM_DESTROYED / EPIPE on a child that's exiting. Caller's
|
|
333
|
+
// pending entry will be rejected via onChildExit; just surface here.
|
|
334
|
+
throw err;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Send one JSON-RPC request and await the matching response.
|
|
339
|
+
* Used for both initialize and tools/call.
|
|
340
|
+
* F1: no inline respawn — ensureReady/initialize is the only path that
|
|
341
|
+
* spawns. This avoids the double-spawn race where rpc() and ensureReady()
|
|
342
|
+
* both spawn children competing for `this.child`.
|
|
343
|
+
*/
|
|
344
|
+
async rpc(method, params) {
|
|
345
|
+
if (this.config.transport === 'http') {
|
|
346
|
+
return this.rpcHttp(method, params);
|
|
347
|
+
}
|
|
348
|
+
return this.rpcStdio(method, params);
|
|
349
|
+
}
|
|
350
|
+
rpcStdio(method, params) {
|
|
351
|
+
return new Promise((resolve, reject) => {
|
|
352
|
+
const id = this.nextId++;
|
|
353
|
+
const timer = setTimeout(() => {
|
|
354
|
+
this.pending.delete(id);
|
|
355
|
+
reject(new Error(`rpc ${method} timed out after ${this.config.requestTimeoutMs}ms`));
|
|
356
|
+
}, this.config.requestTimeoutMs);
|
|
357
|
+
this.pending.set(id, { resolve, reject, timer });
|
|
358
|
+
try {
|
|
359
|
+
this.sendStdio({ jsonrpc: '2.0', id, method, params });
|
|
360
|
+
}
|
|
361
|
+
catch (err) {
|
|
362
|
+
this.pending.delete(id);
|
|
363
|
+
clearTimeout(timer);
|
|
364
|
+
reject(err);
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* HTTP transport with bounded retries on 5xx + transient network errors.
|
|
370
|
+
* Per-attempt timeout via AbortController. Honors `httpMaxRetries`.
|
|
371
|
+
*/
|
|
372
|
+
async rpcHttp(method, params) {
|
|
373
|
+
const url = typeof this.config.endpoint === 'string'
|
|
374
|
+
? this.config.endpoint
|
|
375
|
+
: this.config.endpoint.join('');
|
|
376
|
+
const headers = {
|
|
377
|
+
'content-type': 'application/json',
|
|
378
|
+
accept: 'application/json, text/event-stream',
|
|
379
|
+
};
|
|
380
|
+
if (this.config.authHeader)
|
|
381
|
+
headers.authorization = this.config.authHeader;
|
|
382
|
+
const id = this.nextId++;
|
|
383
|
+
const payload = JSON.stringify({ jsonrpc: '2.0', id, method, params });
|
|
384
|
+
const maxAttempts = this.config.httpMaxRetries + 1;
|
|
385
|
+
let lastErr = null;
|
|
386
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
387
|
+
if (attempt > 0) {
|
|
388
|
+
const backoffMs = 250 * Math.pow(2, attempt - 1);
|
|
389
|
+
await delay(backoffMs);
|
|
390
|
+
}
|
|
391
|
+
const controller = new AbortController();
|
|
392
|
+
const timer = setTimeout(() => controller.abort(), this.config.requestTimeoutMs);
|
|
393
|
+
try {
|
|
394
|
+
const res = await fetch(url, {
|
|
395
|
+
method: 'POST',
|
|
396
|
+
headers,
|
|
397
|
+
body: payload,
|
|
398
|
+
signal: controller.signal,
|
|
399
|
+
});
|
|
400
|
+
// 4xx is the server saying "your request is bad" — do not retry.
|
|
401
|
+
if (res.status >= 400 && res.status < 500) {
|
|
402
|
+
const text = await res.text().catch(() => '');
|
|
403
|
+
throw new Error(`http ${res.status} ${res.statusText}: ${text.slice(0, 200)}`);
|
|
404
|
+
}
|
|
405
|
+
// 5xx → retry
|
|
406
|
+
if (res.status >= 500) {
|
|
407
|
+
lastErr = new Error(`http ${res.status} ${res.statusText}`);
|
|
408
|
+
this.config.logger.warn(`${lastErr.message} (attempt ${attempt + 1}/${maxAttempts})`);
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
const text = await res.text();
|
|
412
|
+
const body = parseHttpRpcBody(text);
|
|
413
|
+
// F7: validate JSON-RPC envelope; warn-not-throw for unexpected shapes
|
|
414
|
+
if (!isJsonRpcEnvelope(body)) {
|
|
415
|
+
this.config.logger.warn(`non-JSON-RPC response from ${url}: ${text.slice(0, 160)}`);
|
|
416
|
+
return undefined;
|
|
417
|
+
}
|
|
418
|
+
if (body.id !== undefined && body.id !== id) {
|
|
419
|
+
this.config.logger.warn(`id mismatch — sent ${id}, got ${body.id}; accepting anyway`);
|
|
420
|
+
}
|
|
421
|
+
if (body.error) {
|
|
422
|
+
const err = body.error;
|
|
423
|
+
throw new Error(`mcp error ${err.code ?? '?'}: ${err.message ?? 'unknown'}`);
|
|
424
|
+
}
|
|
425
|
+
return body.result;
|
|
426
|
+
}
|
|
427
|
+
catch (err) {
|
|
428
|
+
const e = err;
|
|
429
|
+
// Abort due to timeout, network reset, or DNS failure → retry
|
|
430
|
+
const isTransient = e.name === 'AbortError' ||
|
|
431
|
+
/ECONNRESET|ETIMEDOUT|ENOTFOUND|EAI_AGAIN|fetch failed/i.test(e.message ?? '');
|
|
432
|
+
if (isTransient && attempt < maxAttempts - 1) {
|
|
433
|
+
lastErr = e;
|
|
434
|
+
this.config.logger.warn(`transient http error: ${e.message} (attempt ${attempt + 1}/${maxAttempts})`);
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
throw err;
|
|
438
|
+
}
|
|
439
|
+
finally {
|
|
440
|
+
clearTimeout(timer);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
throw lastErr ?? new Error('rpcHttp exhausted retries');
|
|
444
|
+
}
|
|
445
|
+
async callTool(name, args) {
|
|
446
|
+
await this.ensureReady();
|
|
447
|
+
return this.rpc('tools/call', { name, arguments: args });
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* Parse an HTTP body that may be either plain JSON or Server-Sent Events.
|
|
452
|
+
* F2: SSE framing is `data: …\n\n` per event with possible multi-line data
|
|
453
|
+
* payloads. We take the last well-formed event so a streamed in-progress
|
|
454
|
+
* frame doesn't override the final result.
|
|
455
|
+
*/
|
|
456
|
+
export function parseHttpRpcBody(text) {
|
|
457
|
+
const trimmed = text.trim();
|
|
458
|
+
if (!trimmed)
|
|
459
|
+
return {};
|
|
460
|
+
if (trimmed.startsWith('data:') || trimmed.includes('\n\ndata:')) {
|
|
461
|
+
const events = trimmed.split(/\n\n+/).filter(Boolean);
|
|
462
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
463
|
+
const lines = events[i].split('\n').filter((l) => l.startsWith('data:'));
|
|
464
|
+
if (lines.length === 0)
|
|
465
|
+
continue;
|
|
466
|
+
// SSE multi-line data MUST be joined with \n per spec (not concat).
|
|
467
|
+
const payload = lines.map((l) => l.slice(5).trim()).join('\n');
|
|
468
|
+
try {
|
|
469
|
+
return JSON.parse(payload);
|
|
470
|
+
}
|
|
471
|
+
catch {
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
return {};
|
|
476
|
+
}
|
|
477
|
+
return JSON.parse(trimmed);
|
|
478
|
+
}
|
|
479
|
+
function isJsonRpcEnvelope(body) {
|
|
480
|
+
return (body != null &&
|
|
481
|
+
typeof body === 'object' &&
|
|
482
|
+
body.jsonrpc === '2.0' &&
|
|
483
|
+
('result' in body || 'error' in body));
|
|
484
|
+
}
|
|
485
|
+
// ─── Convenience factories ───────────────────────────
|
|
486
|
+
/**
|
|
487
|
+
* Default zero-config wiring: spawn `@studiomeyer/local-memory-mcp` via npx.
|
|
488
|
+
*
|
|
489
|
+
* Requires the package to be reachable (either installed globally or via
|
|
490
|
+
* `npx -y @studiomeyer/local-memory-mcp` which fetches on first run).
|
|
491
|
+
*/
|
|
492
|
+
export function localMemory(overrides = {}) {
|
|
493
|
+
return new McpMemoryBridge({
|
|
494
|
+
transport: 'stdio',
|
|
495
|
+
endpoint: ['npx', '-y', '@studiomeyer/local-memory-mcp'],
|
|
496
|
+
...overrides,
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
/**
|
|
500
|
+
* Connect to a remote MCP server over HTTP.
|
|
501
|
+
*
|
|
502
|
+
* Example: connect to your hosted memory.studiomeyer.io endpoint
|
|
503
|
+
* remoteMemory('https://memory.studiomeyer.io/mcp', { authHeader: `Bearer ${KEY}` })
|
|
504
|
+
*
|
|
505
|
+
* Example: Mem0 with tool-name + arg aliasing
|
|
506
|
+
* remoteMemory('https://api.mem0.ai/mcp', {
|
|
507
|
+
* authHeader: `Bearer ${process.env.MEM0_KEY}`,
|
|
508
|
+
* writeTool: 'mem0_add',
|
|
509
|
+
* readTool: 'mem0_search',
|
|
510
|
+
* mapWriteArgs: (rec) => ({ content: rec.content, metadata: { tags: rec.tags } }),
|
|
511
|
+
* })
|
|
512
|
+
*/
|
|
513
|
+
export function remoteMemory(url, overrides = {}) {
|
|
514
|
+
return new McpMemoryBridge({
|
|
515
|
+
transport: 'http',
|
|
516
|
+
endpoint: url,
|
|
517
|
+
...overrides,
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
// ─── Demo ────────────────────────────────────────────
|
|
521
|
+
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
522
|
+
const memory = localMemory();
|
|
523
|
+
try {
|
|
524
|
+
await memory.save({
|
|
525
|
+
polarity: 'pattern',
|
|
526
|
+
content: 'Darwin demo: bridge save round-trip works.',
|
|
527
|
+
tags: ['darwin-demo', 'bridge'],
|
|
528
|
+
confidence: 0.7,
|
|
529
|
+
});
|
|
530
|
+
// give SQLite a tick to settle
|
|
531
|
+
await delay(50);
|
|
532
|
+
const lessons = await memory.fetchRelevant({ query: 'Darwin demo bridge', limit: 3 });
|
|
533
|
+
console.log('[demo] fetched lessons:', lessons.length);
|
|
534
|
+
for (const l of lessons) {
|
|
535
|
+
console.log(` - score=${l.score ?? '-'} tags=[${l.tags.join(',')}] :: ${l.content.slice(0, 80)}`);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
finally {
|
|
539
|
+
await memory.close();
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
//# sourceMappingURL=mcp-memory-bridge.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-memory-bridge.js","sourceRoot":"","sources":["../../examples/mcp-memory-bridge.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AAEH,OAAO,EAAE,KAAK,EAAuC,MAAM,oBAAoB,CAAC;AAChF,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAqE3D,wDAAwD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,CAAC,EAAE,EAAE,CAAC;IACpD,MAAM,MAAM,GAAG,WAAW,EAAE,CAAC;IAC7B,IAAI,CAAC;QACH,MAAM,MAAM,CAAC,IAAI,CAAC;YAChB,QAAQ,EAAE,SAAS;YACnB,OAAO,EAAE,4CAA4C;YACrD,IAAI,EAAE,CAAC,aAAa,EAAE,QAAQ,CAAC;YAC/B,UAAU,EAAE,GAAG;SAChB,CAAC,CAAC;QACH,+BAA+B;QAC/B,MAAM,KAAK,CAAC,EAAE,CAAC,CAAC;QAChB,MAAM,OAAO,GAAG,MAAM,MAAM,CAAC,aAAa,CAAC,EAAE,KAAK,EAAE,oBAAoB,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC;QACtF,OAAO,CAAC,GAAG,CAAC,yBAAyB,EAAE,OAAO,CAAC,MAAM,CAAC,CAAC;QACvD,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;YACxB,OAAO,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC,KAAK,IAAI,GAAG,UAAU,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,CAAC;QACrG,CAAC;IACH,CAAC;YAAS,CAAC;QACT,MAAM,MAAM,CAAC,KAAK,EAAE,CAAC;IACvB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example: End-to-End Memory + Darwin Integration
|
|
3
|
+
*
|
|
4
|
+
* Closed-loop in three lines:
|
|
5
|
+
* 1. Run an agent.
|
|
6
|
+
* 2. After multi-critic, persist findings via McpMemoryBridge.
|
|
7
|
+
* 3. Before the next run, fetch relevant lessons and inject them.
|
|
8
|
+
*
|
|
9
|
+
* Backend-agnostic: defaults to `@studiomeyer/local-memory-mcp` so it works
|
|
10
|
+
* out of the box with no API keys. Override `localMemory()` with
|
|
11
|
+
* `remoteMemory(url, …)` to point at any MCP-compliant memory server.
|
|
12
|
+
*
|
|
13
|
+
* Run demo (requires `@studiomeyer/local-memory-mcp` reachable via npx):
|
|
14
|
+
* npx tsx examples/memory-darwin-integration.ts
|
|
15
|
+
*
|
|
16
|
+
* The demo deliberately uses a stub agent runner so it doesn't depend on
|
|
17
|
+
* a live LLM connection. Replace `simulateAgentRun()` with your real
|
|
18
|
+
* `runAgent()` call once you wire this into your codebase.
|
|
19
|
+
*/
|
|
20
|
+
import { type Lesson, type RetrievableFeedbackStore } from './mcp-memory-bridge.js';
|
|
21
|
+
import type { MultiCriticResult } from '../src/evolution/multi-critic.js';
|
|
22
|
+
/**
|
|
23
|
+
* Render a list of past lessons as a context block that you can prepend to
|
|
24
|
+
* the next agent's system prompt. Empty lessons → empty string (cheap to
|
|
25
|
+
* call unconditionally).
|
|
26
|
+
*/
|
|
27
|
+
export declare function renderLessonContext(lessons: Lesson[], maxChars?: number): string;
|
|
28
|
+
export interface AgentRunInput {
|
|
29
|
+
agentName: string;
|
|
30
|
+
topic: string;
|
|
31
|
+
/** Optional context block (e.g. rendered past lessons). */
|
|
32
|
+
context?: string;
|
|
33
|
+
}
|
|
34
|
+
export interface AgentRunOutput {
|
|
35
|
+
text: string;
|
|
36
|
+
critic: MultiCriticResult;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The orchestration shape. Bring your own implementations for the two
|
|
40
|
+
* function fields when wiring this into a real codebase:
|
|
41
|
+
* - runner: anything that takes (topic, context) → {text, critic}
|
|
42
|
+
* - store: the McpMemoryBridge (or any RetrievableFeedbackStore)
|
|
43
|
+
*/
|
|
44
|
+
export interface ClosedLoopOptions {
|
|
45
|
+
runner: (input: AgentRunInput) => Promise<AgentRunOutput>;
|
|
46
|
+
store: RetrievableFeedbackStore;
|
|
47
|
+
/** Cap how many lessons to inject. Default: 5. */
|
|
48
|
+
fetchLimit?: number;
|
|
49
|
+
/** Override persist thresholds (same shape as persistFeedback options). */
|
|
50
|
+
persistThresholds?: {
|
|
51
|
+
lowThreshold?: number;
|
|
52
|
+
highThreshold?: number;
|
|
53
|
+
minOutputChars?: number;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** Run one closed-loop turn: fetch lessons → run → persist findings. */
|
|
57
|
+
export declare function runClosedLoopTurn(input: AgentRunInput, options: ClosedLoopOptions): Promise<{
|
|
58
|
+
run: AgentRunOutput;
|
|
59
|
+
lessonsUsed: number;
|
|
60
|
+
persisted: boolean;
|
|
61
|
+
reason: string;
|
|
62
|
+
}>;
|
|
63
|
+
//# sourceMappingURL=memory-darwin-integration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory-darwin-integration.d.ts","sourceRoot":"","sources":["../../examples/memory-darwin-integration.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAAe,KAAK,MAAM,EAAE,KAAK,wBAAwB,EAAE,MAAM,wBAAwB,CAAC;AAEjG,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAC;AAI1E;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE,QAAQ,SAAO,GAAG,MAAM,CAoB9E;AAID,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,2DAA2D;IAC3D,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,iBAAiB,CAAC;CAC3B;AAED;;;;;GAKG;AACH,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,CAAC,KAAK,EAAE,aAAa,KAAK,OAAO,CAAC,cAAc,CAAC,CAAC;IAC1D,KAAK,EAAE,wBAAwB,CAAC;IAChC,kDAAkD;IAClD,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,2EAA2E;IAC3E,iBAAiB,CAAC,EAAE;QAClB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,cAAc,CAAC,EAAE,MAAM,CAAC;KACzB,CAAC;CACH;AAED,wEAAwE;AACxE,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,aAAa,EACpB,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC;IAAE,GAAG,EAAE,cAAc,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,CAiC3F"}
|