Skip to content

#1 Persistent memory for AI coding agents based on real-world benchmarks

fix: address all code review findings

Rohit Ghumarec85ee6d1e5d3
diff --git a/src/functions/context.ts b/src/functions/context.ts
index 33f4330c..ace5ffae 100644
--- a/src/functions/context.ts
+++ b/src/functions/context.ts
@@ -1,81 +1,111 @@
-import type { ISdk } from 'iii-sdk'
-import { getContext } from 'iii-sdk'
-import type { Session, CompressedObservation, SessionSummary, ContextBlock } from '../types.js'
-import { KV } from '../state/schema.js'
-import { StateKV } from '../state/kv.js'
+import type { ISdk } from "iii-sdk";
+import { getContext } from "iii-sdk";
+import type {
+ Session,
+ CompressedObservation,
+ SessionSummary,
+ ContextBlock,
+} from "../types.js";
+import { KV } from "../state/schema.js";
+import { StateKV } from "../state/kv.js";
function estimateTokens(text: string): number {
- return Math.ceil(text.length / 4)
+ return Math.ceil(text.length / 3);
}
-export function registerContextFunction(sdk: ISdk, kv: StateKV, tokenBudget: number): void {
+function escapeXmlAttr(s: string): string {
+ return s
+ .replace(/&/g, "&")
+ .replace(/"/g, """)
+ .replace(/</g, "&lt;")
+ .replace(/>/g, "&gt;");
+}
+
+export function registerContextFunction(
+ sdk: ISdk,
+ kv: StateKV,
+ tokenBudget: number,
+): void {
sdk.registerFunction(
- { id: 'mem::context', description: 'Generate context for session injection' },
+ {
+ id: "mem::context",
+ description: "Generate context for session injection",
+ },
async (data: { sessionId: string; project: string; budget?: number }) => {
- const ctx = getContext()
- const budget = data.budget || tokenBudget
- const blocks: ContextBlock[] = []
+ const ctx = getContext();
+ const budget = data.budget || tokenBudget;
+ const blocks: ContextBlock[] = [];
- const allSessions = await kv.list<Session>(KV.sessions)
+ const allSessions = await kv.list<Session>(KV.sessions);
const sessions = allSessions
.filter((s) => s.project === data.project && s.id !== data.sessionId)
- .sort((a, b) => new Date(b.startedAt).getTime() - new Date(a.startedAt).getTime())
- .slice(0, 10)
+ .sort(
+ (a, b) =>
+ new Date(b.startedAt).getTime() - new Date(a.startedAt).getTime(),
+ )
+ .slice(0, 10);
for (const session of sessions) {
- const summary = await kv.get<SessionSummary>(KV.summaries, session.id)
+ const summary = await kv.get<SessionSummary>(KV.summaries, session.id);
if (summary) {
- const content = `## ${summary.title}\n${summary.narrative}\nDecisions: ${summary.keyDecisions.join('; ')}\nFiles: ${summary.filesModified.join(', ')}`
+ const content = `## ${summary.title}\n${summary.narrative}\nDecisions: ${summary.keyDecisions.join("; ")}\nFiles: ${summary.filesModified.join(", ")}`;
blocks.push({
- type: 'summary',
+ type: "summary",
content,
tokens: estimateTokens(content),
recency: new Date(summary.createdAt).getTime(),
- })
- continue
+ });
+ continue;
}
- const observations = await kv.list<CompressedObservation>(KV.observations(session.id))
- const important = observations.filter((o) => o.title && o.importance >= 5)
+ const observations = await kv.list<CompressedObservation>(
+ KV.observations(session.id),
+ );
+ const important = observations.filter(
+ (o) => o.title && o.importance >= 5,
+ );
if (important.length > 0) {
const content = important
.sort((a, b) => b.importance - a.importance)
.slice(0, 5)
.map((o) => `- [${o.type}] ${o.title}: ${o.narrative}`)
- .join('\n')
+ .join("\n");
blocks.push({
- type: 'observation',
+ type: "observation",
content: `## Session ${session.id.slice(0, 8)} (${session.startedAt})\n${content}`,
tokens: estimateTokens(content),
recency: new Date(session.startedAt).getTime(),
- })
+ });
}
}
- blocks.sort((a, b) => b.recency - a.recency)
+ blocks.sort((a, b) => b.recency - a.recency);
- let usedTokens = 0
- const selected: string[] = []
- const header = `<agentmemory-context project="${data.project}">`
- const footer = `</agentmemory-context>`
- usedTokens += estimateTokens(header) + estimateTokens(footer)
+ let usedTokens = 0;
+ const selected: string[] = [];
+ const header = `<agentmemory-context project="${escapeXmlAttr(data.project)}">`;
+ const footer = `</agentmemory-context>`;
+ usedTokens += estimateTokens(header) + estimateTokens(footer);
for (const block of blocks) {
- if (usedTokens + block.tokens > budget) break
- selected.push(block.content)
- usedTokens += block.tokens
+ if (usedTokens + block.tokens > budget) break;
+ selected.push(block.content);
+ usedTokens += block.tokens;
}
if (selected.length === 0) {
- ctx.logger.info('No context available', { project: data.project })
- return { context: '', blocks: 0, tokens: 0 }
+ ctx.logger.info("No context available", { project: data.project });
+ return { context: "", blocks: 0, tokens: 0 };
}
- const result = `${header}\n${selected.join('\n\n')}\n${footer}`
- ctx.logger.info('Context generated', { blocks: selected.length, tokens: usedTokens })
- return { context: result, blocks: selected.length, tokens: usedTokens }
- }
- )
+ const result = `${header}\n${selected.join("\n\n")}\n${footer}`;
+ ctx.logger.info("Context generated", {
+ blocks: selected.length,
+ tokens: usedTokens,
+ });
+ return { context: result, blocks: selected.length, tokens: usedTokens };
+ },
+ );
}
diff --git a/src/index.ts b/src/index.ts
index a8aa45a7..af645fa0 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,74 +1,83 @@
-import { init } from 'iii-sdk'
-import { loadConfig } from './config.js'
-import { createProvider } from './providers/index.js'
-import { StateKV } from './state/kv.js'
-import { registerPrivacyFunction } from './functions/privacy.js'
-import { registerObserveFunction } from './functions/observe.js'
-import { registerCompressFunction } from './functions/compress.js'
-import { registerSearchFunction, rebuildIndex } from './functions/search.js'
-import { registerContextFunction } from './functions/context.js'
-import { registerSummarizeFunction } from './functions/summarize.js'
-import { registerMigrateFunction } from './functions/migrate.js'
-import { registerApiTriggers } from './triggers/api.js'
-import { registerEventTriggers } from './triggers/events.js'
+import { init } from "iii-sdk";
+import { loadConfig, getEnvVar } from "./config.js";
+import { createProvider } from "./providers/index.js";
+import { StateKV } from "./state/kv.js";
+import { registerPrivacyFunction } from "./functions/privacy.js";
+import { registerObserveFunction } from "./functions/observe.js";
+import { registerCompressFunction } from "./functions/compress.js";
+import { registerSearchFunction, rebuildIndex } from "./functions/search.js";
+import { registerContextFunction } from "./functions/context.js";
+import { registerSummarizeFunction } from "./functions/summarize.js";
+import { registerMigrateFunction } from "./functions/migrate.js";
+import { registerApiTriggers } from "./triggers/api.js";
+import { registerEventTriggers } from "./triggers/events.js";
async function main() {
- const config = loadConfig()
- const provider = createProvider(config.provider)
+ const config = loadConfig();
+ const provider = createProvider(config.provider);
- console.log(`[agentmemory] Starting worker...`)
- console.log(`[agentmemory] Engine: ${config.engineUrl}`)
- console.log(`[agentmemory] Provider: ${config.provider.provider} (${config.provider.model})`)
- console.log(`[agentmemory] REST API: http://localhost:${config.restPort}/agentmemory/*`)
- console.log(`[agentmemory] Streams: ws://localhost:${config.streamsPort}`)
+ console.log(`[agentmemory] Starting worker...`);
+ console.log(`[agentmemory] Engine: ${config.engineUrl}`);
+ console.log(
+ `[agentmemory] Provider: ${config.provider.provider} (${config.provider.model})`,
+ );
+ console.log(
+ `[agentmemory] REST API: http://localhost:${config.restPort}/agentmemory/*`,
+ );
+ console.log(`[agentmemory] Streams: ws://localhost:${config.streamsPort}`);
const sdk = init(config.engineUrl, {
- workerName: 'agentmemory',
- })
+ workerName: "agentmemory",
+ });
- const kv = new StateKV(sdk)
+ const kv = new StateKV(sdk);
- registerPrivacyFunction(sdk)
- registerObserveFunction(sdk, kv)
- registerCompressFunction(sdk, kv, provider)
- registerSearchFunction(sdk, kv)
- registerContextFunction(sdk, kv, config.tokenBudget)
- registerSummarizeFunction(sdk, kv, provider)
- registerMigrateFunction(sdk, kv)
+ registerPrivacyFunction(sdk);
+ registerObserveFunction(sdk, kv);
+ registerCompressFunction(sdk, kv, provider);
+ registerSearchFunction(sdk, kv);
+ registerContextFunction(sdk, kv, config.tokenBudget);
+ registerSummarizeFunction(sdk, kv, provider);
+ registerMigrateFunction(sdk, kv);
- registerApiTriggers(sdk, kv)
- registerEventTriggers(sdk)
+ const secret = getEnvVar("AGENTMEMORY_SECRET");
+ registerApiTriggers(sdk, kv, secret);
+ registerEventTriggers(sdk);
- const indexCount = await rebuildIndex(kv).catch(() => 0)
+ const indexCount = await rebuildIndex(kv).catch(() => 0);
if (indexCount > 0) {
- console.log(`[agentmemory] Search index: ${indexCount} observations`)
+ console.log(`[agentmemory] Search index: ${indexCount} observations`);
}
- console.log(`[agentmemory] Ready. Endpoints:`)
- console.log(` POST /agentmemory/session/start - Start session + get context`)
- console.log(` POST /agentmemory/observe - Capture observation`)
- console.log(` POST /agentmemory/context - Generate context`)
- console.log(` POST /agentmemory/search - Search observations`)
- console.log(` POST /agentmemory/summarize - Summarize session`)
- console.log(` GET /agentmemory/sessions - List sessions`)
- console.log(` GET /agentmemory/observations - Get session observations`)
- console.log(` GET /agentmemory/health - Health check`)
- console.log(` GET /agentmemory/viewer - Web viewer`)
- console.log(` POST /agentmemory/migrate - Import from SQLite`)
+ console.log(`[agentmemory] Ready. Endpoints:`);
+ console.log(
+ ` POST /agentmemory/session/start - Start session + get context`,
+ );
+ console.log(` POST /agentmemory/observe - Capture observation`);
+ console.log(` POST /agentmemory/context - Generate context`);
+ console.log(` POST /agentmemory/search - Search observations`);
+ console.log(` POST /agentmemory/summarize - Summarize session`);
+ console.log(` GET /agentmemory/sessions - List sessions`);
+ console.log(
+ ` GET /agentmemory/observations - Get session observations`,
+ );
+ console.log(` GET /agentmemory/health - Health check`);
+ console.log(` GET /agentmemory/viewer - Web viewer`);
+ console.log(` POST /agentmemory/migrate - Import from SQLite`);
- process.on('SIGINT', async () => {
- console.log(`\n[agentmemory] Shutting down...`)
- await sdk.shutdown()
- process.exit(0)
- })
+ process.on("SIGINT", async () => {
+ console.log(`\n[agentmemory] Shutting down...`);
+ await sdk.shutdown();
+ process.exit(0);
+ });
- process.on('SIGTERM', async () => {
- await sdk.shutdown()
- process.exit(0)
- })
+ process.on("SIGTERM", async () => {
+ await sdk.shutdown();
+ process.exit(0);
+ });
}
main().catch((err) => {
- console.error(`[agentmemory] Fatal:`, err)
- process.exit(1)
-})
+ console.error(`[agentmemory] Fatal:`, err);
+ process.exit(1);
+});
diff --git a/src/prompts/xml.ts b/src/prompts/xml.ts
index ea00ff75..ed8f572e 100644
--- a/src/prompts/xml.ts
+++ b/src/prompts/xml.ts
@@ -1,16 +1,26 @@
+const VALID_TAG = /^[a-zA-Z_][a-zA-Z0-9_-]*$/;
+
export function getXmlTag(xml: string, tag: string): string {
- const match = xml.match(new RegExp(`<${tag}>([\\s\\S]*?)</${tag}>`))
- return match ? match[1].trim() : ''
+ if (!VALID_TAG.test(tag)) return "";
+ const match = xml.match(new RegExp(`<${tag}>([\\s\\S]*?)</${tag}>`));
+ return match ? match[1].trim() : "";
}
-export function getXmlChildren(xml: string, parentTag: string, childTag: string): string[] {
- const parentMatch = xml.match(new RegExp(`<${parentTag}>([\\s\\S]*?)</${parentTag}>`))
- if (!parentMatch) return []
- const items: string[] = []
- const re = new RegExp(`<${childTag}>([\\s\\S]*?)</${childTag}>`, 'g')
- let m
+export function getXmlChildren(
+ xml: string,
+ parentTag: string,
+ childTag: string,
+): string[] {
+ if (!VALID_TAG.test(parentTag) || !VALID_TAG.test(childTag)) return [];
+ const parentMatch = xml.match(
+ new RegExp(`<${parentTag}>([\\s\\S]*?)</${parentTag}>`),
+ );
+ if (!parentMatch) return [];
+ const items: string[] = [];
+ const re = new RegExp(`<${childTag}>([\\s\\S]*?)</${childTag}>`, "g");
+ let m;
while ((m = re.exec(parentMatch[1])) !== null) {
- items.push(m[1].trim())
+ items.push(m[1].trim());
}
- return items
+ return items;
}
diff --git a/src/state/search-index.ts b/src/state/search-index.ts
index 54163a82..bda1e810 100644
--- a/src/state/search-index.ts
+++ b/src/state/search-index.ts
@@ -44,7 +44,7 @@ export class SearchIndex {
}
for (const [indexTerm, obsIds] of this.invertedIndex) {
- if (indexTerm !== term && indexTerm.includes(term)) {
+ if (indexTerm !== term && indexTerm.startsWith(term)) {
for (const obsId of obsIds) {
scores.set(obsId, (scores.get(obsId) || 0) + 0.5);
}
diff --git a/src/triggers/api.ts b/src/triggers/api.ts
index 763556ce..db45a152 100644
--- a/src/triggers/api.ts
+++ b/src/triggers/api.ts
@@ -1,131 +1,242 @@
-import type { ISdk, ApiRequest } from 'iii-sdk'
-import type { Session, CompressedObservation, HookPayload } from '../types.js'
-import { KV } from '../state/schema.js'
-import { StateKV } from '../state/kv.js'
-import { readFileSync } from 'node:fs'
-import { join, dirname } from 'node:path'
-import { fileURLToPath } from 'node:url'
+import type { ISdk, ApiRequest } from "iii-sdk";
+import type { Session, CompressedObservation, HookPayload } from "../types.js";
+import { KV } from "../state/schema.js";
+import { StateKV } from "../state/kv.js";
+import { readFileSync } from "node:fs";
+import { join, dirname } from "node:path";
+import { fileURLToPath } from "node:url";
-type Response = { status_code: number; headers?: Record<string, string>; body: unknown }
+type Response = {
+ status_code: number;
+ headers?: Record<string, string>;
+ body: unknown;
+};
-export function registerApiTriggers(sdk: ISdk, kv: StateKV): void {
+function checkAuth(
+ req: ApiRequest,
+ secret: string | undefined,
+): Response | null {
+ if (!secret) return null;
+ const auth = req.headers?.["authorization"] || req.headers?.["Authorization"];
+ if (auth !== `Bearer ${secret}`) {
+ return { status_code: 401, body: { error: "unauthorized" } };
+ }
+ return null;
+}
+export function registerApiTriggers(
+ sdk: ISdk,
+ kv: StateKV,
+ secret?: string,
+): void {
sdk.registerFunction(
- { id: 'api::health' },
+ { id: "api::health" },
async (): Promise<Response> => ({
status_code: 200,
- body: { status: 'ok', service: 'agentmemory', version: '0.1.0' },
- })
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::health', config: { api_path: '/agentmemory/health', http_method: 'GET' } })
+ body: { status: "ok", service: "agentmemory", version: "0.1.0" },
+ }),
+ );
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::health",
+ config: { api_path: "/agentmemory/health", http_method: "GET" },
+ });
sdk.registerFunction(
- { id: 'api::observe' },
+ { id: "api::observe" },
async (req: ApiRequest<HookPayload>): Promise<Response> => {
- const result = await sdk.trigger('mem::observe', req.body)
- return { status_code: 201, body: result }
- }
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::observe', config: { api_path: '/agentmemory/observe', http_method: 'POST' } })
+ const authErr = checkAuth(req, secret);
+ if (authErr) return authErr;
+ const result = await sdk.trigger("mem::observe", req.body);
+ return { status_code: 201, body: result };
+ },
+ );
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::observe",
+ config: { api_path: "/agentmemory/observe", http_method: "POST" },
+ });
sdk.registerFunction(
- { id: 'api::context' },
- async (req: ApiRequest<{ sessionId: string; project: string; budget?: number }>): Promise<Response> => {
- const result = await sdk.trigger('mem::context', req.body)
- return { status_code: 200, body: result }
- }
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::context', config: { api_path: '/agentmemory/context', http_method: 'POST' } })
+ { id: "api::context" },
+ async (
+ req: ApiRequest<{ sessionId: string; project: string; budget?: number }>,
+ ): Promise<Response> => {
+ const authErr = checkAuth(req, secret);
+ if (authErr) return authErr;
+ const result = await sdk.trigger("mem::context", req.body);
+ return { status_code: 200, body: result };
+ },
+ );
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::context",
+ config: { api_path: "/agentmemory/context", http_method: "POST" },
+ });
sdk.registerFunction(
- { id: 'api::search' },
- async (req: ApiRequest<{ query: string; limit?: number }>): Promise<Response> => {
- const result = await sdk.trigger('mem::search', req.body)
- return { status_code: 200, body: result }
- }
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::search', config: { api_path: '/agentmemory/search', http_method: 'POST' } })
+ { id: "api::search" },
+ async (
+ req: ApiRequest<{ query: string; limit?: number }>,
+ ): Promise<Response> => {
+ const authErr = checkAuth(req, secret);
+ if (authErr) return authErr;
+ const result = await sdk.trigger("mem::search", req.body);
+ return { status_code: 200, body: result };
+ },
+ );
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::search",
+ config: { api_path: "/agentmemory/search", http_method: "POST" },
+ });
sdk.registerFunction(
- { id: 'api::session::start' },
- async (req: ApiRequest<{ sessionId: string; project: string; cwd: string }>): Promise<Response> => {
- const { sessionId, project, cwd } = req.body
+ { id: "api::session::start" },
+ async (
+ req: ApiRequest<{ sessionId: string; project: string; cwd: string }>,
+ ): Promise<Response> => {
+ const authErr = checkAuth(req, secret);
+ if (authErr) return authErr;
+ const { sessionId, project, cwd } = req.body;
const session: Session = {
- id: sessionId, project, cwd,
+ id: sessionId,
+ project,
+ cwd,
startedAt: new Date().toISOString(),
- status: 'active',
+ status: "active",
observationCount: 0,
- }
- await kv.set(KV.sessions, sessionId, session)
- const contextResult = await sdk.trigger<{ sessionId: string; project: string }, { context: string }>('mem::context', { sessionId, project })
- return { status_code: 200, body: { session, context: contextResult.context } }
- }
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::session::start', config: { api_path: '/agentmemory/session/start', http_method: 'POST' } })
+ };
+ await kv.set(KV.sessions, sessionId, session);
+ const contextResult = await sdk.trigger<
+ { sessionId: string; project: string },
+ { context: string }
+ >("mem::context", { sessionId, project });
+ return {
+ status_code: 200,
+ body: { session, context: contextResult.context },
+ };
+ },
+ );
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::session::start",
+ config: { api_path: "/agentmemory/session/start", http_method: "POST" },
+ });
sdk.registerFunction(
- { id: 'api::session::end' },
+ { id: "api::session::end" },
async (req: ApiRequest<{ sessionId: string }>): Promise<Response> => {
- const session = await kv.get<Session>(KV.sessions, req.body.sessionId)
+ const authErr = checkAuth(req, secret);
+ if (authErr) return authErr;
+ const session = await kv.get<Session>(KV.sessions, req.body.sessionId);
if (session) {
await kv.set(KV.sessions, req.body.sessionId, {
- ...session, endedAt: new Date().toISOString(), status: 'completed' as const,
- })
+ ...session,
+ endedAt: new Date().toISOString(),
+ status: "completed" as const,
+ });
}
- return { status_code: 200, body: { success: true } }
- }
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::session::end', config: { api_path: '/agentmemory/session/end', http_method: 'POST' } })
+ return { status_code: 200, body: { success: true } };
+ },
+ );
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::session::end",
+ config: { api_path: "/agentmemory/session/end", http_method: "POST" },
+ });
sdk.registerFunction(
- { id: 'api::summarize' },
+ { id: "api::summarize" },
async (req: ApiRequest<{ sessionId: string }>): Promise<Response> => {
- const result = await sdk.trigger('mem::summarize', req.body)
- return { status_code: 200, body: result }
- }
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::summarize', config: { api_path: '/agentmemory/summarize', http_method: 'POST' } })
+ const authErr = checkAuth(req, secret);
+ if (authErr) return authErr;
+ const result = await sdk.trigger("mem::summarize", req.body);
+ return { status_code: 200, body: result };
+ },
+ );
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::summarize",
+ config: { api_path: "/agentmemory/summarize", http_method: "POST" },
+ });
- sdk.registerFunction(
- { id: 'api::sessions' },
- async (): Promise<Response> => {
- const sessions = await kv.list<Session>(KV.sessions)
- return { status_code: 200, body: { sessions } }
- }
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::sessions', config: { api_path: '/agentmemory/sessions', http_method: 'GET' } })
+ sdk.registerFunction({ id: "api::sessions" }, async (): Promise<Response> => {
+ const sessions = await kv.list<Session>(KV.sessions);
+ return { status_code: 200, body: { sessions } };
+ });
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::sessions",
+ config: { api_path: "/agentmemory/sessions", http_method: "GET" },
+ });
sdk.registerFunction(
- { id: 'api::observations' },
+ { id: "api::observations" },
async (req: ApiRequest): Promise<Response> => {
- const sessionId = req.query_params['sessionId'] as string
- if (!sessionId) return { status_code: 400, body: { error: 'sessionId required' } }
- const observations = await kv.list<CompressedObservation>(KV.observations(sessionId))
- return { status_code: 200, body: { observations } }
- }
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::observations', config: { api_path: '/agentmemory/observations', http_method: 'GET' } })
+ const sessionId = req.query_params["sessionId"] as string;
+ if (!sessionId)
+ return { status_code: 400, body: { error: "sessionId required" } };
+ const observations = await kv.list<CompressedObservation>(
+ KV.observations(sessionId),
+ );
+ return { status_code: 200, body: { observations } };
+ },
+ );
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::observations",
+ config: { api_path: "/agentmemory/observations", http_method: "GET" },
+ });
sdk.registerFunction(
- { id: 'api::migrate' },
+ { id: "api::migrate" },
async (req: ApiRequest<{ dbPath: string }>): Promise<Response> => {
- const result = await sdk.trigger('mem::migrate', req.body)
- return { status_code: 200, body: result }
- }
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::migrate', config: { api_path: '/agentmemory/migrate', http_method: 'POST' } })
+ const authErr = checkAuth(req, secret);
+ if (authErr) return authErr;
+ const result = await sdk.trigger("mem::migrate", req.body);
+ return { status_code: 200, body: result };
+ },
+ );
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::migrate",
+ config: { api_path: "/agentmemory/migrate", http_method: "POST" },
+ });
- sdk.registerFunction(
- { id: 'api::viewer' },
- async (): Promise<Response> => {
- try {
- const viewerPath = join(dirname(fileURLToPath(import.meta.url)), '..', 'viewer', 'index.html')
- const html = readFileSync(viewerPath, 'utf-8')
- return { status_code: 200, headers: { 'Content-Type': 'text/html' }, body: html }
- } catch {
- return { status_code: 200, headers: { 'Content-Type': 'text/html' }, body: '<!DOCTYPE html><html><body><h1>agentmemory</h1></body></html>' }
- }
+ sdk.registerFunction({ id: "api::viewer" }, async (): Promise<Response> => {
+ try {
+ const viewerPath = join(
+ dirname(fileURLToPath(import.meta.url)),
+ "..",
+ "viewer",
+ "index.html",
+ );
+ const html = readFileSync(viewerPath, "utf-8");
+ return {
+ status_code: 200,
+ headers: {
+ "Content-Type": "text/html",
+ "Content-Security-Policy":
+ "default-src 'self'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self' ws://localhost:* wss://localhost:*",
+ },
+ body: html,
+ };
+ } catch {
+ return {
+ status_code: 200,
+ headers: {
+ "Content-Type": "text/html",
+ "Content-Security-Policy":
+ "default-src 'self'; script-src 'unsafe-inline'; style-src 'unsafe-inline'; connect-src 'self' ws://localhost:* wss://localhost:*",
+ },
+ body: "<!DOCTYPE html><html><body><h1>agentmemory</h1></body></html>",
+ };
}
- )
- sdk.registerTrigger({ type: 'http', function_id: 'api::viewer', config: { api_path: '/agentmemory/viewer', http_method: 'GET' } })
+ });
+ sdk.registerTrigger({
+ type: "http",
+ function_id: "api::viewer",
+ config: { api_path: "/agentmemory/viewer", http_method: "GET" },
+ });
}
diff --git a/src/viewer/index.html b/src/viewer/index.html
index 65edf553..64b34d48 100644
--- a/src/viewer/index.html
+++ b/src/viewer/index.html
@@ -102,8 +102,10 @@
</div>
<script>
- const REST = 'http://localhost:3111';
- const WS_URL = 'ws://localhost:3112';
+ const REST = window.location.origin;
+ const wsProto = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
+ const wsPort = parseInt(window.location.port || '3111') + 1;
+ const WS_URL = `${wsProto}//${window.location.hostname}:${wsPort}`;
const timeline = document.getElementById('timeline');
const statusEl = document.getElementById('status');
const searchEl = document.getElementById('search');
@@ -174,6 +176,7 @@
timeline.innerHTML = filtered.map(o => {
const imp = (o.importance || 5) >= 7 ? 'high' : (o.importance || 5) >= 4 ? 'med' : 'low';
+ const impClass = ['high', 'med', 'low'].includes(imp) ? imp : 'low';
const isRaw = !o.title;
const title = o.title || o.toolName || o.hookType || 'Raw observation';
const type = o.type || o.hookType || 'raw';
@@ -183,7 +186,7 @@
const concepts = (o.concepts || []).map(c => `<span class="tag">${esc(c)}</span>`).join('');
const files = (o.files || []).map(f => `<span class="tag file-tag">${esc(f)}</span>`).join('');
- return `<div class="obs importance-${imp}">
+ return `<div class="obs importance-${impClass}">
<div class="obs-header">
<span class="obs-type ${isRaw ? 'raw-badge' : ''}">${esc(type)}</span>
<span class="obs-time">${time}</span>
diff --git a/test/privacy.test.ts b/test/privacy.test.ts
new file mode 100644
index 00000000..c3d6e42d
--- /dev/null
+++ b/test/privacy.test.ts
@@ -0,0 +1,87 @@
+import { describe, it, expect } from "vitest";
+import { stripPrivateData } from "../src/functions/privacy.js";
+
+describe("stripPrivateData", () => {
+ it("strips private tags", () => {
+ expect(stripPrivateData("hello <private>secret</private> world")).toBe(
+ "hello [REDACTED] world",
+ );
+ });
+
+ it("strips private tags case-insensitive", () => {
+ expect(stripPrivateData("<Private>data</Private>")).toBe("[REDACTED]");
+ });
+
+ it("strips API keys", () => {
+ expect(stripPrivateData("api_key=sk-ant-1234567890abcdefghij")).toBe(
+ "[REDACTED_SECRET]",
+ );
+ });
+
+ it("strips GitHub PATs", () => {
+ expect(
+ stripPrivateData("token: ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefgh"),
+ ).toBe("[REDACTED_SECRET]");
+ });
+
+ it("strips standalone GitHub PATs", () => {
+ expect(
+ stripPrivateData("found ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij here"),
+ ).toBe("found [REDACTED_SECRET] here");
+ });
+
+ it("strips Slack tokens", () => {
+ expect(stripPrivateData("xoxb-123456-789012-abcdef")).toBe(
+ "[REDACTED_SECRET]",
+ );
+ });
+
+ it("strips AWS access keys", () => {
+ expect(stripPrivateData("key=AKIAIOSFODNN7EXAMPLE")).toBe(
+ "key=[REDACTED_SECRET]",
+ );
+ });
+
+ it("strips JWT tokens", () => {
+ expect(
+ stripPrivateData(
+ "eyJhbGciOiJIUzI1.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIn0.SflKxwRJSMeKKF2QT4fwpM",
+ ),
+ ).toBe("[REDACTED_SECRET]");
+ });
+
+ it("strips sk- prefixed keys", () => {
+ expect(stripPrivateData("sk-1234567890abcdefghijklmnopqr")).toBe(
+ "[REDACTED_SECRET]",
+ );
+ });
+
+ it("handles multiple secrets in one string", () => {
+ const input =
+ "sk-abcdefghijklmnopqrstuv and ghp_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghij";
+ const result = stripPrivateData(input);
+ expect(result).not.toContain("sk-");
+ expect(result).not.toContain("ghp_");
+ });
+
+ it("does not strip short strings", () => {
+ expect(stripPrivateData("api_key=short")).toBe("api_key=short");
+ });
+
+ it("returns empty string unchanged", () => {
+ expect(stripPrivateData("")).toBe("");
+ });
+
+ it("handles no secrets gracefully", () => {
+ expect(stripPrivateData("normal text without secrets")).toBe(
+ "normal text without secrets",
+ );
+ });
+
+ it("works correctly on consecutive calls (no regex statefulness)", () => {
+ const input = "sk-ABCDEFGHIJKLMNOPQRSTUVWXYZabc";
+ expect(stripPrivateData(input)).toBe("[REDACTED_SECRET]");
+ expect(stripPrivateData(input)).toBe("[REDACTED_SECRET]");
+ expect(stripPrivateData(input)).toBe("[REDACTED_SECRET]");
+ });
+});
diff --git a/test/schema.test.ts b/test/schema.test.ts
new file mode 100644
index 00000000..2d3f65d2
--- /dev/null
+++ b/test/schema.test.ts
@@ -0,0 +1,42 @@
+import { describe, it, expect } from 'vitest'
+import { KV, STREAM, generateId } from '../src/state/schema.js'
+
+describe('KV', () => {
+ it('has correct session scope', () => {
+ expect(KV.sessions).toBe('mem:sessions')
+ })
+
+ it('generates observation scope with session ID', () => {
+ expect(KV.observations('ses_123')).toBe('mem:obs:ses_123')
+ })
+
+ it('has correct summaries scope', () => {
+ expect(KV.summaries).toBe('mem:summaries')
+ })
+})
+
+describe('STREAM', () => {
+ it('has correct name', () => {
+ expect(STREAM.name).toBe('mem-live')
+ })
+
+ it('group returns session ID', () => {
+ expect(STREAM.group('ses_123')).toBe('ses_123')
+ })
+})
+
+describe('generateId', () => {
+ it('includes prefix', () => {
+ expect(generateId('obs')).toMatch(/^obs_/)
+ })
+
+ it('generates unique IDs', () => {
+ const ids = new Set(Array.from({ length: 100 }, () => generateId('test')))
+ expect(ids.size).toBe(100)
+ })
+
+ it('has sufficient length', () => {
+ const id = generateId('obs')
+ expect(id.length).toBeGreaterThan(15)
+ })
+})
diff --git a/test/search-index.test.ts b/test/search-index.test.ts
new file mode 100644
index 00000000..0a99681b
--- /dev/null
+++ b/test/search-index.test.ts
@@ -0,0 +1,128 @@
+import { describe, it, expect, beforeEach } from "vitest";
+import { SearchIndex } from "../src/state/search-index.js";
+import type { CompressedObservation } from "../src/types.js";
+
+function makeObs(
+ overrides: Partial<CompressedObservation> = {},
+): CompressedObservation {
+ return {
+ id: "obs_1",
+ sessionId: "ses_1",
+ timestamp: new Date().toISOString(),
+ type: "file_edit",
+ title: "Edit auth middleware",
+ subtitle: "JWT validation",
+ facts: ["Added token check"],
+ narrative: "Modified the auth middleware to validate JWT tokens",
+ concepts: ["authentication", "jwt"],
+ files: ["src/middleware/auth.ts"],
+ importance: 7,
+ ...overrides,
+ };
+}
+
+describe("SearchIndex", () => {
+ let index: SearchIndex;
+
+ beforeEach(() => {
+ index = new SearchIndex();
+ });
+
+ it("starts empty", () => {
+ expect(index.size).toBe(0);
+ });
+
+ it("adds and finds observations", () => {
+ index.add(makeObs());
+ expect(index.size).toBe(1);
+ const results = index.search("auth");
+ expect(results.length).toBe(1);
+ expect(results[0].obsId).toBe("obs_1");
+ });
+
+ it("returns empty for no matches", () => {
+ index.add(makeObs());
+ expect(index.search("database")).toEqual([]);
+ });
+
+ it("scores exact matches higher than prefix matches", () => {
+ index.add(
+ makeObs({
+ id: "obs_exact",
+ title: "redis cache",
+ narrative: "Set up redis caching layer",
+ concepts: ["redis"],
+ facts: ["Added redis"],
+ files: ["src/redis.ts"],
+ }),
+ );
+ index.add(
+ makeObs({
+ id: "obs_prefix",
+ title: "redistool handler",
+ narrative: "Set up redistool for ops",
+ concepts: ["redistool"],
+ facts: ["Added redistool"],
+ files: ["src/redistool.ts"],
+ }),
+ );
+ const results = index.search("redis");
+ const exact = results.find((r) => r.obsId === "obs_exact");
+ const prefix = results.find((r) => r.obsId === "obs_prefix");
+ expect(exact).toBeDefined();
+ expect(prefix).toBeDefined();
+ expect(exact!.score).toBeGreaterThanOrEqual(prefix!.score);
+ });
+
+ it("respects limit", () => {
+ for (let i = 0; i < 30; i++) {
+ index.add(makeObs({ id: `obs_${i}`, title: `auth feature ${i}` }));
+ }
+ expect(index.search("auth", 5).length).toBe(5);
+ });
+
+ it("clears the index", () => {
+ index.add(makeObs());
+ index.clear();
+ expect(index.size).toBe(0);
+ expect(index.search("auth")).toEqual([]);
+ });
+
+ it("returns empty for empty query", () => {
+ index.add(makeObs());
+ expect(index.search("")).toEqual([]);
+ });
+
+ it("searches across multiple fields", () => {
+ index.add(
+ makeObs({ id: "obs_file", title: "something", files: ["auth.ts"] }),
+ );
+ expect(index.search("auth").length).toBe(1);
+ });
+
+ it("handles multiple query terms", () => {
+ index.add(
+ makeObs({
+ id: "obs_both",
+ title: "redis cache",
+ narrative: "Set up redis and cache layer",
+ concepts: ["redis", "cache"],
+ facts: ["Added caching"],
+ files: ["src/cache.ts"],
+ }),
+ );
+ index.add(
+ makeObs({
+ id: "obs_one",
+ title: "redis only",
+ narrative: "Set up redis connection",
+ concepts: ["redis"],
+ facts: ["Added redis"],
+ files: ["src/redis.ts"],
+ }),
+ );
+ const results = index.search("redis cache");
+ expect(results[0].obsId).toBe("obs_both");
+ expect(results[0].score).toBeGreaterThan(results[1].score);
+ });
+});
diff --git a/test/xml.test.ts b/test/xml.test.ts
new file mode 100644
index 00000000..a080c9eb
--- /dev/null
+++ b/test/xml.test.ts
@@ -0,0 +1,65 @@
+import { describe, it, expect } from 'vitest'
+import { getXmlTag, getXmlChildren } from '../src/prompts/xml.js'
+
+describe('getXmlTag', () => {
+ it('extracts simple tag content', () => {
+ expect(getXmlTag('<title>Hello World</title>', 'title')).toBe('Hello World')
+ })
+
+ it('extracts multiline content', () => {
+ expect(getXmlTag('<narrative>\nLine 1\nLine 2\n</narrative>', 'narrative')).toBe('Line 1\nLine 2')
+ })
+
+ it('returns empty string for missing tag', () => {
+ expect(getXmlTag('<title>Hello</title>', 'missing')).toBe('')
+ })
+
+ it('returns first match for duplicate tags', () => {
+ expect(getXmlTag('<title>First</title><title>Second</title>', 'title')).toBe('First')
+ })
+
+ it('returns empty string for empty tag', () => {
+ expect(getXmlTag('<title></title>', 'title')).toBe('')
+ })
+
+ it('trims whitespace', () => {
+ expect(getXmlTag('<title> trimmed </title>', 'title')).toBe('trimmed')
+ })
+
+ it('returns empty for invalid tag names', () => {
+ expect(getXmlTag('<foo>bar</foo>', '.*')).toBe('')
+ })
+
+ it('returns empty for tag with special regex chars', () => {
+ expect(getXmlTag('<foo>bar</foo>', 'a(b')).toBe('')
+ })
+})
+
+describe('getXmlChildren', () => {
+ it('extracts child elements', () => {
+ const xml = '<facts><fact>One</fact><fact>Two</fact></facts>'
+ expect(getXmlChildren(xml, 'facts', 'fact')).toEqual(['One', 'Two'])
+ })
+
+ it('returns empty array for missing parent', () => {
+ expect(getXmlChildren('<foo>bar</foo>', 'facts', 'fact')).toEqual([])
+ })
+
+ it('returns empty array for missing children', () => {
+ expect(getXmlChildren('<facts></facts>', 'facts', 'fact')).toEqual([])
+ })
+
+ it('trims child content', () => {
+ const xml = '<facts><fact> trimmed </fact></facts>'
+ expect(getXmlChildren(xml, 'facts', 'fact')).toEqual(['trimmed'])
+ })
+
+ it('handles multiline children', () => {
+ const xml = '<decisions><decision>Use JWT\nfor auth</decision></decisions>'
+ expect(getXmlChildren(xml, 'decisions', 'decision')).toEqual(['Use JWT\nfor auth'])
+ })
+
+ it('returns empty for invalid parent tag name', () => {
+ expect(getXmlChildren('<facts><fact>A</fact></facts>', '.*', 'fact')).toEqual([])
+ })
+})