#!/usr/bin/env node import { resolveProject } from "./_project.js"; function isSdkChildContext(payload: unknown): boolean { if (process.env["AGENTMEMORY_SDK_CHILD"] === "1") return true; if (!payload || typeof payload !== "object") return false; return (payload as { entrypoint?: unknown }).entrypoint === "sdk-ts"; } const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111"; const SECRET = process.env["AGENTMEMORY_SECRET"] || ""; function authHeaders(): Record { const h: Record = { "Content-Type": "application/json" }; if (SECRET) h["Authorization"] = `Bearer ${SECRET}`; return h; } async function main() { let input = ""; for await (const chunk of process.stdin) { input += chunk; } let data: Record; try { data = JSON.parse(input); } catch { return; } if (isSdkChildContext(data)) return; const notificationType = data.notification_type ?? data.notificationType; if (notificationType !== "permission_prompt") return; const rawSessionId = data.session_id ?? data.sessionId; const sessionId = typeof rawSessionId === "string" && rawSessionId.length > 0 ? rawSessionId : "unknown"; fetch(`${REST_URL}/agentmemory/observe`, { method: "POST", headers: authHeaders(), body: JSON.stringify({ hookType: "notification", sessionId, project: resolveProject(data.cwd as string | undefined), cwd: (data.cwd as string | undefined) || process.cwd(), timestamp: new Date().toISOString(), data: { notification_type: notificationType, title: data.title, message: data.message, }, }), signal: AbortSignal.timeout(2000), }).catch(() => {}); setTimeout(() => process.exit(0), 500).unref(); } main();