281 lines
7.8 KiB
JavaScript
281 lines
7.8 KiB
JavaScript
#!/usr/bin/env node
|
|
import http from 'node:http';
|
|
import { URL } from 'node:url';
|
|
|
|
function readJson(req) {
|
|
return new Promise((resolve, reject) => {
|
|
let body = '';
|
|
req.setEncoding('utf8');
|
|
req.on('data', (chunk) => {
|
|
body += chunk;
|
|
});
|
|
req.on('end', () => {
|
|
if (!body.trim()) {
|
|
resolve({});
|
|
return;
|
|
}
|
|
try {
|
|
resolve(JSON.parse(body));
|
|
} catch (err) {
|
|
reject(err);
|
|
}
|
|
});
|
|
req.on('error', reject);
|
|
});
|
|
}
|
|
|
|
function sendJson(res, statusCode, payload) {
|
|
res.writeHead(statusCode, { 'content-type': 'application/json; charset=utf-8' });
|
|
res.end(JSON.stringify(payload));
|
|
}
|
|
|
|
export function createMockMemoryV2Service() {
|
|
const state = {
|
|
mem0: {
|
|
writes: [],
|
|
compacts: [],
|
|
},
|
|
letta: {
|
|
messages: [],
|
|
compacts: [],
|
|
},
|
|
langgraph: {
|
|
resolves: [],
|
|
},
|
|
qdrant: {
|
|
searches: [],
|
|
},
|
|
weaviate: {
|
|
searches: [],
|
|
},
|
|
neo4j: {
|
|
statements: [],
|
|
},
|
|
};
|
|
|
|
function reset() {
|
|
state.mem0.writes.length = 0;
|
|
state.mem0.compacts.length = 0;
|
|
state.letta.messages.length = 0;
|
|
state.letta.compacts.length = 0;
|
|
state.langgraph.resolves.length = 0;
|
|
state.qdrant.searches.length = 0;
|
|
state.weaviate.searches.length = 0;
|
|
state.neo4j.statements.length = 0;
|
|
}
|
|
|
|
async function handle(req, res) {
|
|
const url = new URL(req.url, 'http://127.0.0.1');
|
|
const pathname = url.pathname;
|
|
|
|
if (req.method === 'GET' && pathname === '/health') {
|
|
sendJson(res, 200, { ok: true, service: 'mock-memory-v2-services' });
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && pathname === '/__admin/reset') {
|
|
reset();
|
|
sendJson(res, 200, { ok: true });
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'GET' && pathname === '/__admin/state') {
|
|
sendJson(res, 200, {
|
|
ok: true,
|
|
state,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && pathname === '/mem0/v1/memories') {
|
|
const body = await readJson(req);
|
|
state.mem0.writes.push(body);
|
|
sendJson(res, 200, {
|
|
saved: 1,
|
|
analyzed: 1,
|
|
memory_count: Array.isArray(body.messages) ? body.messages.length : 0,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && pathname === '/mem0/v1/memories/compact') {
|
|
const body = await readJson(req);
|
|
state.mem0.compacts.push(body);
|
|
sendJson(res, 200, {
|
|
analyzed: 1,
|
|
memory_count: state.mem0.writes.filter((item) => item.user_id === body.user_id).length,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && /^\/letta\/v1\/agents\/[^/]+\/messages$/.test(pathname)) {
|
|
const body = await readJson(req);
|
|
const agentId = pathname.split('/')[4];
|
|
state.letta.messages.push({ agentId, ...body });
|
|
sendJson(res, 200, {
|
|
saved: 1,
|
|
analyzed: 0,
|
|
memory_count: Array.isArray(body.messages) ? body.messages.length : 0,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && /^\/letta\/v1\/agents\/[^/]+\/memory\/compact$/.test(pathname)) {
|
|
const body = await readJson(req);
|
|
const agentId = pathname.split('/')[4];
|
|
state.letta.compacts.push({ agentId, ...body });
|
|
sendJson(res, 200, {
|
|
analyzed: 1,
|
|
memory_count: state.letta.messages.filter((item) => item.user_id === body.user_id).length,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && /^\/letta\/v1\/agents\/[^/]+\/memory$/.test(pathname)) {
|
|
const body = await readJson(req);
|
|
const agentId = pathname.split('/')[4];
|
|
const messages = state.letta.messages
|
|
.filter((item) => item.agentId === agentId && item.user_id === body.user_id)
|
|
.flatMap((item) => Array.isArray(item.messages) ? item.messages : [])
|
|
.map((item, index) => ({
|
|
id: `letta-${index + 1}`,
|
|
label: 'lifecycle',
|
|
text: String(item.text ?? item.content ?? '').trim() || `letta-memory-${index + 1}`,
|
|
}))
|
|
.filter((item) => item.text);
|
|
sendJson(res, 200, {
|
|
memories: messages.length ? messages : ['letta lifecycle memory'],
|
|
activeGoals: ['memory-v2'],
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && pathname === '/langgraph/memory/resolve') {
|
|
const body = await readJson(req);
|
|
state.langgraph.resolves.push(body);
|
|
sendJson(res, 200, {
|
|
memories: [{
|
|
id: 'langgraph-1',
|
|
label: 'policy',
|
|
text: `langgraph route for ${body.user_id ?? 'unknown-user'}`,
|
|
}],
|
|
activeGoals: ['route-memory'],
|
|
behaviorSummary: null,
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'GET' && pathname === '/healthz') {
|
|
sendJson(res, 200, { ok: true, service: 'mock-qdrant' });
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'GET' && pathname === '/collections') {
|
|
sendJson(res, 200, { result: { collections: [{ name: 'memind_memory' }] } });
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && /^\/collections\/[^/]+\/points\/search$/.test(pathname)) {
|
|
const body = await readJson(req);
|
|
const collection = pathname.split('/')[2];
|
|
state.qdrant.searches.push({ collection, ...body });
|
|
sendJson(res, 200, {
|
|
result: [{
|
|
id: 'qdrant-1',
|
|
score: 0.91,
|
|
payload: {
|
|
id: 'qdrant-1',
|
|
label: 'semantic',
|
|
content: `qdrant memory for ${body?.filter?.must?.[0]?.match?.value ?? 'unknown-user'}`,
|
|
created_at: Date.now(),
|
|
},
|
|
}],
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'GET' && pathname === '/v1/.well-known/ready') {
|
|
sendJson(res, 200, { status: 'READY' });
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && pathname === '/v1/graphql') {
|
|
const body = await readJson(req);
|
|
state.weaviate.searches.push(body);
|
|
sendJson(res, 200, {
|
|
data: {
|
|
Get: {
|
|
MemoryItem: [{
|
|
id: 'weaviate-1',
|
|
label: 'semantic',
|
|
text: 'weaviate memory item',
|
|
_additional: { certainty: 0.9 },
|
|
}],
|
|
},
|
|
},
|
|
});
|
|
return;
|
|
}
|
|
|
|
if (req.method === 'POST' && /^\/db\/[^/]+\/tx\/commit$/.test(pathname)) {
|
|
const body = await readJson(req);
|
|
state.neo4j.statements.push(body);
|
|
const firstStatement = body?.statements?.[0]?.statement ?? '';
|
|
if (/RETURN e\.summary AS behaviorSummary/i.test(firstStatement)) {
|
|
sendJson(res, 200, {
|
|
results: [{
|
|
columns: ['behaviorSummary'],
|
|
data: [{ row: ['neo4j behavior summary'] }],
|
|
}],
|
|
errors: [],
|
|
});
|
|
return;
|
|
}
|
|
sendJson(res, 200, {
|
|
results: [{ columns: ['id(e)'], data: [{ row: [1] }] }],
|
|
errors: [],
|
|
});
|
|
return;
|
|
}
|
|
|
|
sendJson(res, 404, { ok: false, message: 'not found' });
|
|
}
|
|
|
|
return {
|
|
state,
|
|
reset,
|
|
createServer() {
|
|
return http.createServer((req, res) => {
|
|
Promise.resolve(handle(req, res)).catch((err) => {
|
|
sendJson(res, 500, {
|
|
ok: false,
|
|
message: err instanceof Error ? err.message : String(err),
|
|
});
|
|
});
|
|
});
|
|
},
|
|
};
|
|
}
|
|
|
|
export async function startMockMemoryV2Service({
|
|
port = Number(process.env.MEMORY_V2_MOCK_PORT ?? 19400) || 19400,
|
|
host = process.env.MEMORY_V2_MOCK_HOST ?? '127.0.0.1',
|
|
stdout = process.stdout,
|
|
} = {}) {
|
|
const service = createMockMemoryV2Service();
|
|
const server = service.createServer();
|
|
await new Promise((resolve, reject) => {
|
|
server.once('error', reject);
|
|
server.listen(port, host, resolve);
|
|
});
|
|
stdout.write(`${JSON.stringify({ ok: true, host, port }, null, 2)}\n`);
|
|
return { service, server, host, port };
|
|
}
|
|
|
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
|
startMockMemoryV2Service().catch((err) => {
|
|
console.error(err instanceof Error ? err.message : err);
|
|
process.exitCode = 1;
|
|
});
|
|
}
|