@agentmemory/agentmemory 0.9.29 → 0.9.31
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/.env.example +102 -35
- package/AGENTS.md +19 -26
- package/README.md +417 -525
- package/dist/.env.example +102 -35
- package/dist/cli.d.mts.map +1 -1
- package/dist/cli.mjs +2316 -655
- package/dist/cli.mjs.map +1 -1
- package/dist/compression-recovery-CnhLQC6N.mjs +5912 -0
- package/dist/compression-recovery-CnhLQC6N.mjs.map +1 -0
- package/dist/{connect-Qaeq15TQ.mjs → connect-BSLIAMpw.mjs} +900 -302
- package/dist/connect-BSLIAMpw.mjs.map +1 -0
- package/dist/hooks/_capture.d.mts +25 -0
- package/dist/hooks/_capture.d.mts.map +1 -0
- package/dist/hooks/_capture.mjs +944 -0
- package/dist/hooks/_capture.mjs.map +1 -0
- package/dist/hooks/antigravity-bridge.d.mts +1 -1
- package/dist/hooks/antigravity-bridge.d.mts.map +1 -1
- package/dist/hooks/antigravity-bridge.mjs +24 -15
- package/dist/hooks/antigravity-bridge.mjs.map +1 -1
- package/dist/hooks/notification.mjs +35 -29
- package/dist/hooks/notification.mjs.map +1 -1
- package/dist/hooks/post-commit.mjs +91 -1
- package/dist/hooks/post-commit.mjs.map +1 -1
- package/dist/hooks/post-tool-failure.mjs +71 -29
- package/dist/hooks/post-tool-failure.mjs.map +1 -1
- package/dist/hooks/post-tool-use.mjs +99 -39
- package/dist/hooks/post-tool-use.mjs.map +1 -1
- package/dist/hooks/pre-compact.mjs +113 -6
- package/dist/hooks/pre-compact.mjs.map +1 -1
- package/dist/hooks/pre-tool-use.mjs +91 -1
- package/dist/hooks/pre-tool-use.mjs.map +1 -1
- package/dist/hooks/prompt-submit.mjs +25 -25
- package/dist/hooks/prompt-submit.mjs.map +1 -1
- package/dist/hooks/session-end.mjs +67 -31
- package/dist/hooks/session-end.mjs.map +1 -1
- package/dist/hooks/session-start.mjs +110 -6
- package/dist/hooks/session-start.mjs.map +1 -1
- package/dist/hooks/stop.mjs +80 -8
- package/dist/hooks/stop.mjs.map +1 -1
- package/dist/hooks/subagent-start.mjs +28 -29
- package/dist/hooks/subagent-start.mjs.map +1 -1
- package/dist/hooks/subagent-stop.mjs +33 -29
- package/dist/hooks/subagent-stop.mjs.map +1 -1
- package/dist/hooks/task-completed.mjs +31 -31
- package/dist/hooks/task-completed.mjs.map +1 -1
- package/dist/{image-refs-BKLmp_sD.mjs → image-refs-BaT-9oMU.mjs} +2 -2
- package/dist/image-refs-BaT-9oMU.mjs.map +1 -0
- package/dist/{image-refs-CJgZmb9X.mjs → image-refs-Cyta4Lf9.mjs} +3 -3
- package/dist/image-refs-Cyta4Lf9.mjs.map +1 -0
- package/dist/{keyed-mutex-DTHtcZrt.mjs → image-store-BsN4vyjr.mjs} +2 -14
- package/dist/image-store-BsN4vyjr.mjs.map +1 -0
- package/dist/index.mjs +15180 -4119
- package/dist/index.mjs.map +1 -1
- package/dist/integrations/dsh.d.mts +55 -0
- package/dist/integrations/dsh.d.mts.map +1 -0
- package/dist/integrations/dsh.mjs +1145 -0
- package/dist/integrations/dsh.mjs.map +1 -0
- package/dist/{schema-BFZhNTF7.mjs → keyed-mutex-DCUh7PG-.mjs} +81 -8
- package/dist/keyed-mutex-DCUh7PG-.mjs.map +1 -0
- package/dist/secret-store-D9GwO0xp.mjs +2449 -0
- package/dist/secret-store-D9GwO0xp.mjs.map +1 -0
- package/dist/session-arguments-zxsbbd8y.mjs +113 -0
- package/dist/session-arguments-zxsbbd8y.mjs.map +1 -0
- package/dist/{src-G7yt8gGm.mjs → src-DaMtDzt_.mjs} +8320 -4260
- package/dist/src-DaMtDzt_.mjs.map +1 -0
- package/dist/{standalone-BqNJDJ04.mjs → standalone-BlqshyZ5.mjs} +246 -62
- package/dist/standalone-BlqshyZ5.mjs.map +1 -0
- package/dist/standalone.d.mts +1 -1
- package/dist/standalone.d.mts.map +1 -1
- package/dist/standalone.mjs +866 -78
- package/dist/standalone.mjs.map +1 -1
- package/dist/viewer/index.html +7394 -2816
- package/integrations/hermes/README.md +84 -0
- package/integrations/hermes/__init__.py +669 -0
- package/integrations/hermes/plugin.yaml +27 -0
- package/integrations/omp/LICENSE +190 -0
- package/integrations/omp/README.md +76 -0
- package/integrations/omp/capture.ts +1 -0
- package/integrations/omp/dist/index.mjs +1450 -0
- package/integrations/omp/index.ts +488 -0
- package/integrations/omp/package.json +13 -0
- package/integrations/omp/security.ts +67 -0
- package/integrations/omp/skills/agentmemory-agents/REFERENCE.md +32 -0
- package/integrations/omp/skills/agentmemory-agents/SKILL.md +48 -0
- package/integrations/omp/skills/agentmemory-agents/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/agentmemory-architecture/SKILL.md +33 -0
- package/integrations/omp/skills/agentmemory-architecture/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/agentmemory-config/REFERENCE.md +90 -0
- package/integrations/omp/skills/agentmemory-config/SKILL.md +78 -0
- package/integrations/omp/skills/agentmemory-config/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/agentmemory-hooks/REFERENCE.md +20 -0
- package/integrations/omp/skills/agentmemory-hooks/SKILL.md +49 -0
- package/integrations/omp/skills/agentmemory-hooks/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/agentmemory-mcp-tools/REFERENCE.md +66 -0
- package/integrations/omp/skills/agentmemory-mcp-tools/SKILL.md +42 -0
- package/integrations/omp/skills/agentmemory-mcp-tools/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/agentmemory-rest-api/REFERENCE.md +158 -0
- package/integrations/omp/skills/agentmemory-rest-api/SKILL.md +49 -0
- package/integrations/omp/skills/agentmemory-rest-api/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/commit-context/EXAMPLES.md +71 -0
- package/integrations/omp/skills/commit-context/SKILL.md +64 -0
- package/integrations/omp/skills/commit-context/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/commit-history/EXAMPLES.md +71 -0
- package/integrations/omp/skills/commit-history/SKILL.md +62 -0
- package/integrations/omp/skills/commit-history/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/forget/EXAMPLES.md +40 -0
- package/integrations/omp/skills/forget/SKILL.md +73 -0
- package/integrations/omp/skills/forget/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/handoff/EXAMPLES.md +57 -0
- package/integrations/omp/skills/handoff/SKILL.md +75 -0
- package/integrations/omp/skills/handoff/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/lesson/SKILL.md +64 -0
- package/integrations/omp/skills/lesson/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/memory-discipline/SKILL.md +65 -0
- package/integrations/omp/skills/memory-discipline/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/recall/EXAMPLES.md +74 -0
- package/integrations/omp/skills/recall/SKILL.md +74 -0
- package/integrations/omp/skills/recall/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/recap/EXAMPLES.md +62 -0
- package/integrations/omp/skills/recap/SKILL.md +68 -0
- package/integrations/omp/skills/recap/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/remember/EXAMPLES.md +67 -0
- package/integrations/omp/skills/remember/SKILL.md +66 -0
- package/integrations/omp/skills/remember/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/session-history/EXAMPLES.md +59 -0
- package/integrations/omp/skills/session-history/SKILL.md +67 -0
- package/integrations/omp/skills/session-history/TROUBLESHOOTING.md +70 -0
- package/integrations/omp/skills/write-agentmemory-skill/SKILL.md +41 -0
- package/integrations/omp/skills/write-agentmemory-skill/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/LICENSE +190 -0
- package/integrations/openclaw/README.md +88 -0
- package/integrations/openclaw/openclaw.plugin.json +71 -0
- package/integrations/openclaw/package.json +38 -0
- package/integrations/openclaw/plugin.mjs +425 -0
- package/integrations/openclaw/plugin.yaml +26 -0
- package/integrations/openclaw/skills/agentmemory-agents/REFERENCE.md +32 -0
- package/integrations/openclaw/skills/agentmemory-agents/SKILL.md +48 -0
- package/integrations/openclaw/skills/agentmemory-agents/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/agentmemory-architecture/SKILL.md +33 -0
- package/integrations/openclaw/skills/agentmemory-architecture/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/agentmemory-config/REFERENCE.md +90 -0
- package/integrations/openclaw/skills/agentmemory-config/SKILL.md +78 -0
- package/integrations/openclaw/skills/agentmemory-config/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/agentmemory-hooks/REFERENCE.md +20 -0
- package/integrations/openclaw/skills/agentmemory-hooks/SKILL.md +49 -0
- package/integrations/openclaw/skills/agentmemory-hooks/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/agentmemory-mcp-tools/REFERENCE.md +66 -0
- package/integrations/openclaw/skills/agentmemory-mcp-tools/SKILL.md +42 -0
- package/integrations/openclaw/skills/agentmemory-mcp-tools/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/agentmemory-rest-api/REFERENCE.md +158 -0
- package/integrations/openclaw/skills/agentmemory-rest-api/SKILL.md +49 -0
- package/integrations/openclaw/skills/agentmemory-rest-api/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/commit-context/EXAMPLES.md +71 -0
- package/integrations/openclaw/skills/commit-context/SKILL.md +64 -0
- package/integrations/openclaw/skills/commit-context/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/commit-history/EXAMPLES.md +71 -0
- package/integrations/openclaw/skills/commit-history/SKILL.md +62 -0
- package/integrations/openclaw/skills/commit-history/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/forget/EXAMPLES.md +40 -0
- package/integrations/openclaw/skills/forget/SKILL.md +73 -0
- package/integrations/openclaw/skills/forget/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/handoff/EXAMPLES.md +57 -0
- package/integrations/openclaw/skills/handoff/SKILL.md +75 -0
- package/integrations/openclaw/skills/handoff/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/lesson/SKILL.md +64 -0
- package/integrations/openclaw/skills/lesson/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/memory-discipline/SKILL.md +65 -0
- package/integrations/openclaw/skills/memory-discipline/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/recall/EXAMPLES.md +74 -0
- package/integrations/openclaw/skills/recall/SKILL.md +74 -0
- package/integrations/openclaw/skills/recall/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/recap/EXAMPLES.md +62 -0
- package/integrations/openclaw/skills/recap/SKILL.md +68 -0
- package/integrations/openclaw/skills/recap/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/remember/EXAMPLES.md +67 -0
- package/integrations/openclaw/skills/remember/SKILL.md +66 -0
- package/integrations/openclaw/skills/remember/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/session-history/EXAMPLES.md +59 -0
- package/integrations/openclaw/skills/session-history/SKILL.md +67 -0
- package/integrations/openclaw/skills/session-history/TROUBLESHOOTING.md +70 -0
- package/integrations/openclaw/skills/write-agentmemory-skill/SKILL.md +41 -0
- package/integrations/openclaw/skills/write-agentmemory-skill/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/LICENSE +190 -0
- package/integrations/pi/README.md +45 -51
- package/integrations/pi/dist/index.mjs +1379 -0
- package/integrations/pi/index.ts +139 -173
- package/integrations/pi/package.json +16 -6
- package/integrations/pi/security.ts +54 -22
- package/integrations/pi/skills/agentmemory-agents/REFERENCE.md +32 -0
- package/integrations/pi/skills/agentmemory-agents/SKILL.md +48 -0
- package/integrations/pi/skills/agentmemory-agents/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/agentmemory-architecture/SKILL.md +33 -0
- package/integrations/pi/skills/agentmemory-architecture/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/agentmemory-config/REFERENCE.md +90 -0
- package/integrations/pi/skills/agentmemory-config/SKILL.md +78 -0
- package/integrations/pi/skills/agentmemory-config/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/agentmemory-hooks/REFERENCE.md +20 -0
- package/integrations/pi/skills/agentmemory-hooks/SKILL.md +49 -0
- package/integrations/pi/skills/agentmemory-hooks/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/agentmemory-mcp-tools/REFERENCE.md +66 -0
- package/integrations/pi/skills/agentmemory-mcp-tools/SKILL.md +42 -0
- package/integrations/pi/skills/agentmemory-mcp-tools/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/agentmemory-rest-api/REFERENCE.md +158 -0
- package/integrations/pi/skills/agentmemory-rest-api/SKILL.md +49 -0
- package/integrations/pi/skills/agentmemory-rest-api/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/commit-context/EXAMPLES.md +71 -0
- package/integrations/pi/skills/commit-context/SKILL.md +64 -0
- package/integrations/pi/skills/commit-context/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/commit-history/EXAMPLES.md +71 -0
- package/integrations/pi/skills/commit-history/SKILL.md +62 -0
- package/integrations/pi/skills/commit-history/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/forget/EXAMPLES.md +40 -0
- package/integrations/pi/skills/forget/SKILL.md +73 -0
- package/integrations/pi/skills/forget/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/handoff/EXAMPLES.md +57 -0
- package/integrations/pi/skills/handoff/SKILL.md +75 -0
- package/integrations/pi/skills/handoff/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/lesson/SKILL.md +64 -0
- package/integrations/pi/skills/lesson/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/memory-discipline/SKILL.md +65 -0
- package/integrations/pi/skills/memory-discipline/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/recall/EXAMPLES.md +74 -0
- package/integrations/pi/skills/recall/SKILL.md +74 -0
- package/integrations/pi/skills/recall/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/recap/EXAMPLES.md +62 -0
- package/integrations/pi/skills/recap/SKILL.md +68 -0
- package/integrations/pi/skills/recap/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/remember/EXAMPLES.md +67 -0
- package/integrations/pi/skills/remember/SKILL.md +66 -0
- package/integrations/pi/skills/remember/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/session-history/EXAMPLES.md +59 -0
- package/integrations/pi/skills/session-history/SKILL.md +67 -0
- package/integrations/pi/skills/session-history/TROUBLESHOOTING.md +70 -0
- package/integrations/pi/skills/write-agentmemory-skill/SKILL.md +41 -0
- package/integrations/pi/skills/write-agentmemory-skill/TROUBLESHOOTING.md +70 -0
- package/package.json +42 -12
- package/plugin/.claude-plugin/plugin.json +2 -2
- package/plugin/.codex-plugin/plugin.json +27 -4
- package/plugin/.devin-plugin/plugin.json +5 -3
- package/plugin/.mcp.codex.json +12 -0
- package/plugin/.mcp.copilot.json +2 -7
- package/plugin/.mcp.json +2 -7
- package/plugin/assets/icon.svg +35 -0
- package/plugin/cursor/hooks.json +23 -8
- package/plugin/hooks/hooks.copilot.json +11 -11
- package/plugin/hooks.json +67 -0
- package/plugin/opencode/README.md +267 -31
- package/plugin/opencode/agentmemory-capture.ts +594 -11
- package/plugin/opencode/plugin.json +1 -1
- package/plugin/plugin.json +1 -1
- package/plugin/scripts/_capture.d.mts +25 -0
- package/plugin/scripts/_capture.d.mts.map +1 -0
- package/plugin/scripts/_capture.mjs +944 -0
- package/plugin/scripts/_capture.mjs.map +1 -0
- package/plugin/scripts/antigravity-bridge.d.mts +1 -1
- package/plugin/scripts/antigravity-bridge.d.mts.map +1 -1
- package/plugin/scripts/antigravity-bridge.mjs +24 -15
- package/plugin/scripts/antigravity-bridge.mjs.map +1 -1
- package/plugin/scripts/cursor/run-detached.d.mts +1 -0
- package/plugin/scripts/cursor/run-detached.mjs +786 -0
- package/plugin/scripts/cursor/run-detached.mjs.map +1 -0
- package/plugin/scripts/cursor/run-hook.d.mts +1 -0
- package/plugin/scripts/cursor/run-hook.mjs +704 -0
- package/plugin/scripts/cursor/run-hook.mjs.map +1 -0
- package/plugin/scripts/notification.mjs +35 -29
- package/plugin/scripts/notification.mjs.map +1 -1
- package/plugin/scripts/plugin-bridge.mjs +409 -0
- package/plugin/scripts/post-commit.mjs +91 -1
- package/plugin/scripts/post-commit.mjs.map +1 -1
- package/plugin/scripts/post-tool-failure.mjs +71 -29
- package/plugin/scripts/post-tool-failure.mjs.map +1 -1
- package/plugin/scripts/post-tool-use.mjs +99 -39
- package/plugin/scripts/post-tool-use.mjs.map +1 -1
- package/plugin/scripts/pre-compact.mjs +113 -6
- package/plugin/scripts/pre-compact.mjs.map +1 -1
- package/plugin/scripts/pre-tool-use.mjs +91 -1
- package/plugin/scripts/pre-tool-use.mjs.map +1 -1
- package/plugin/scripts/prompt-submit.mjs +25 -25
- package/plugin/scripts/prompt-submit.mjs.map +1 -1
- package/plugin/scripts/session-end.mjs +67 -31
- package/plugin/scripts/session-end.mjs.map +1 -1
- package/plugin/scripts/session-start.mjs +110 -6
- package/plugin/scripts/session-start.mjs.map +1 -1
- package/plugin/scripts/stop.mjs +80 -8
- package/plugin/scripts/stop.mjs.map +1 -1
- package/plugin/scripts/subagent-start.mjs +28 -29
- package/plugin/scripts/subagent-start.mjs.map +1 -1
- package/plugin/scripts/subagent-stop.mjs +33 -29
- package/plugin/scripts/subagent-stop.mjs.map +1 -1
- package/plugin/scripts/task-completed.mjs +31 -31
- package/plugin/scripts/task-completed.mjs.map +1 -1
- package/plugin/skills/_shared/TROUBLESHOOTING.md +42 -12
- package/plugin/skills/agentmemory-agents/REFERENCE.md +10 -9
- package/plugin/skills/agentmemory-agents/SKILL.md +19 -5
- package/plugin/skills/agentmemory-agents/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/agentmemory-architecture/SKILL.md +4 -4
- package/plugin/skills/agentmemory-architecture/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/agentmemory-config/REFERENCE.md +48 -3
- package/plugin/skills/agentmemory-config/SKILL.md +51 -10
- package/plugin/skills/agentmemory-config/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/agentmemory-hooks/SKILL.md +15 -5
- package/plugin/skills/agentmemory-hooks/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/agentmemory-mcp-tools/REFERENCE.md +5 -5
- package/plugin/skills/agentmemory-mcp-tools/SKILL.md +6 -3
- package/plugin/skills/agentmemory-mcp-tools/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/agentmemory-rest-api/REFERENCE.md +18 -2
- package/plugin/skills/agentmemory-rest-api/SKILL.md +12 -6
- package/plugin/skills/agentmemory-rest-api/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/commit-context/SKILL.md +1 -1
- package/plugin/skills/commit-context/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/commit-history/SKILL.md +1 -1
- package/plugin/skills/commit-history/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/forget/EXAMPLES.md +19 -54
- package/plugin/skills/forget/SKILL.md +16 -12
- package/plugin/skills/forget/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/handoff/EXAMPLES.md +5 -4
- package/plugin/skills/handoff/SKILL.md +13 -6
- package/plugin/skills/handoff/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/lesson/SKILL.md +1 -1
- package/plugin/skills/lesson/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/memory-discipline/SKILL.md +14 -6
- package/plugin/skills/memory-discipline/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/recall/SKILL.md +18 -7
- package/plugin/skills/recall/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/recap/EXAMPLES.md +5 -3
- package/plugin/skills/recap/SKILL.md +11 -6
- package/plugin/skills/recap/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/remember/EXAMPLES.md +3 -3
- package/plugin/skills/remember/SKILL.md +1 -1
- package/plugin/skills/remember/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/session-history/EXAMPLES.md +2 -2
- package/plugin/skills/session-history/SKILL.md +10 -4
- package/plugin/skills/session-history/TROUBLESHOOTING.md +70 -0
- package/plugin/skills/write-agentmemory-skill/SKILL.md +2 -1
- package/plugin/skills/write-agentmemory-skill/TROUBLESHOOTING.md +70 -0
- package/dist/connect-Qaeq15TQ.mjs.map +0 -1
- package/dist/docker-compose.yml +0 -47
- package/dist/iii-config.docker.yaml +0 -53
- package/dist/iii-config.yaml +0 -61
- package/dist/image-refs-BKLmp_sD.mjs.map +0 -1
- package/dist/image-refs-CJgZmb9X.mjs.map +0 -1
- package/dist/keyed-mutex-DTHtcZrt.mjs.map +0 -1
- package/dist/logger-BEB7pCI9.mjs +0 -43
- package/dist/logger-BEB7pCI9.mjs.map +0 -1
- package/dist/schema-BFZhNTF7.mjs.map +0 -1
- package/dist/src-G7yt8gGm.mjs.map +0 -1
- package/dist/standalone-BqNJDJ04.mjs.map +0 -1
- package/dist/tools-registry-BV_2bu7z.mjs +0 -1405
- package/dist/tools-registry-BV_2bu7z.mjs.map +0 -1
- package/docker-compose.yml +0 -47
- package/iii-config.docker.yaml +0 -53
- package/iii-config.yaml +0 -61
|
@@ -0,0 +1,944 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { chmodSync, closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, readdirSync, renameSync, statSync, unlinkSync, utimesSync, writeFileSync, writeSync } from "node:fs";
|
|
3
|
+
import { createHash } from "node:crypto";
|
|
4
|
+
import { homedir, platform } from "node:os";
|
|
5
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
6
|
+
//#region src/secret-store.ts
|
|
7
|
+
const SECRET_KEY = "AGENTMEMORY_SECRET";
|
|
8
|
+
function agentmemoryHomeDir() {
|
|
9
|
+
return process.env["AGENTMEMORY_DATA_DIR"] || join(homedir(), ".agentmemory");
|
|
10
|
+
}
|
|
11
|
+
function secretFilePath() {
|
|
12
|
+
return join(agentmemoryHomeDir(), "secret");
|
|
13
|
+
}
|
|
14
|
+
function usable(value) {
|
|
15
|
+
if (typeof value !== "string") return "";
|
|
16
|
+
const trimmed = value.trim();
|
|
17
|
+
if (!trimmed) return "";
|
|
18
|
+
if (trimmed.startsWith("${") && trimmed.endsWith("}")) return "";
|
|
19
|
+
return trimmed;
|
|
20
|
+
}
|
|
21
|
+
function unquote(value) {
|
|
22
|
+
const quote = value[0];
|
|
23
|
+
if ((quote === "\"" || quote === "'") && value.length > 1) {
|
|
24
|
+
const close = value.indexOf(quote, 1);
|
|
25
|
+
if (close !== -1) return value.slice(1, close);
|
|
26
|
+
}
|
|
27
|
+
const hash = value.indexOf(" #");
|
|
28
|
+
return hash === -1 ? value : value.slice(0, hash).trim();
|
|
29
|
+
}
|
|
30
|
+
function readEnvFileSecret() {
|
|
31
|
+
let content;
|
|
32
|
+
try {
|
|
33
|
+
content = readFileSync(process.env["AGENTMEMORY_ENV_FILE"] || join(homedir(), ".agentmemory", ".env"), "utf-8");
|
|
34
|
+
} catch {
|
|
35
|
+
return "";
|
|
36
|
+
}
|
|
37
|
+
if (typeof content !== "string") return "";
|
|
38
|
+
let found = "";
|
|
39
|
+
for (const line of content.split("\n")) {
|
|
40
|
+
const trimmed = line.trim();
|
|
41
|
+
if (trimmed.startsWith("#")) continue;
|
|
42
|
+
const eq = trimmed.indexOf("=");
|
|
43
|
+
if (eq === -1) continue;
|
|
44
|
+
if (trimmed.slice(0, eq).replace(/^export\s+/, "").trim() !== SECRET_KEY) continue;
|
|
45
|
+
found = usable(unquote(trimmed.slice(eq + 1).trim()));
|
|
46
|
+
}
|
|
47
|
+
return found;
|
|
48
|
+
}
|
|
49
|
+
function readSecretFile(path) {
|
|
50
|
+
try {
|
|
51
|
+
return usable(readFileSync(path, "utf-8"));
|
|
52
|
+
} catch {
|
|
53
|
+
return "";
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
function legacyDataDirSecretPaths(env = process.env) {
|
|
57
|
+
const dataDir = env["AGENTMEMORY_DATA_DIR"];
|
|
58
|
+
if (!dataDir) return [];
|
|
59
|
+
const paths = [
|
|
60
|
+
join(dataDir, "secret"),
|
|
61
|
+
join(dataDir, ".hmac"),
|
|
62
|
+
join(dataDir, "..", ".hmac")
|
|
63
|
+
];
|
|
64
|
+
return [...new Set(paths.filter((path) => path !== secretFilePath()))];
|
|
65
|
+
}
|
|
66
|
+
function readStoredSecret(env = process.env) {
|
|
67
|
+
const canonicalPath = canonicalSecretPath();
|
|
68
|
+
const localPath = secretFilePath();
|
|
69
|
+
const legacyPaths = legacyDataDirSecretPaths(env);
|
|
70
|
+
return readSecretFile(canonicalPath) || (localPath === canonicalPath ? "" : readSecretFile(localPath)) || legacyPaths.map(readSecretFile).find(Boolean) || "";
|
|
71
|
+
}
|
|
72
|
+
function isLoopbackUrl(url) {
|
|
73
|
+
let hostname;
|
|
74
|
+
try {
|
|
75
|
+
hostname = new URL(url).hostname.toLowerCase();
|
|
76
|
+
} catch {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const bare = hostname.replace(/^\[|\]$/g, "");
|
|
80
|
+
return bare === "localhost" || bare === "::1" || /^127(?:\.\d{1,3}){3}$/.test(bare);
|
|
81
|
+
}
|
|
82
|
+
function resolveClientSecret(baseUrl, env = process.env) {
|
|
83
|
+
const fromEnv = usable(env[SECRET_KEY]);
|
|
84
|
+
if (fromEnv) return fromEnv;
|
|
85
|
+
if (env.AGENTMEMORY_CREDENTIAL_MODE === "explicit") return "";
|
|
86
|
+
if (!isLoopbackUrl(baseUrl)) return "";
|
|
87
|
+
return readEnvFileSecret() || readStoredSecret(env);
|
|
88
|
+
}
|
|
89
|
+
function canonicalSecretPath() {
|
|
90
|
+
return join(homedir(), ".agentmemory", "secret");
|
|
91
|
+
}
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/capture/event-id.ts
|
|
94
|
+
const HOST_ID_FIELDS = [
|
|
95
|
+
"event_id",
|
|
96
|
+
"eventId",
|
|
97
|
+
"tool_use_id",
|
|
98
|
+
"toolUseId",
|
|
99
|
+
"tool_call_id",
|
|
100
|
+
"toolCallId",
|
|
101
|
+
"call_id",
|
|
102
|
+
"callId",
|
|
103
|
+
"prompt_id",
|
|
104
|
+
"promptId",
|
|
105
|
+
"message_id",
|
|
106
|
+
"messageId"
|
|
107
|
+
];
|
|
108
|
+
const HOST_TIME_FIELDS = [
|
|
109
|
+
"timestamp",
|
|
110
|
+
"ts",
|
|
111
|
+
"created_at",
|
|
112
|
+
"createdAt",
|
|
113
|
+
"event_time"
|
|
114
|
+
];
|
|
115
|
+
function stableStringify(value) {
|
|
116
|
+
if (value === void 0) return "null";
|
|
117
|
+
if (value === null || typeof value !== "object") {
|
|
118
|
+
const encoded = JSON.stringify(value);
|
|
119
|
+
return encoded === void 0 ? "null" : encoded;
|
|
120
|
+
}
|
|
121
|
+
if (Array.isArray(value)) return `[${value.map(stableStringify).join(",")}]`;
|
|
122
|
+
const obj = value;
|
|
123
|
+
return `{${Object.keys(obj).filter((k) => obj[k] !== void 0).sort().map((k) => `${JSON.stringify(k)}:${stableStringify(obj[k])}`).join(",")}}`;
|
|
124
|
+
}
|
|
125
|
+
function hostScalar(host, fields) {
|
|
126
|
+
for (const field of fields) {
|
|
127
|
+
const v = host[field];
|
|
128
|
+
if (typeof v === "string" && v.trim()) return v.trim();
|
|
129
|
+
if (typeof v === "number" && Number.isFinite(v)) return String(v);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
function digest(parts) {
|
|
133
|
+
return createHash("sha256").update(parts.join("\0")).digest("hex").slice(0, 32);
|
|
134
|
+
}
|
|
135
|
+
function hasHostIdentity(host) {
|
|
136
|
+
const source = host && typeof host === "object" ? host : {};
|
|
137
|
+
return hostScalar(source, HOST_ID_FIELDS) !== void 0 || hostScalar(source, HOST_TIME_FIELDS) !== void 0;
|
|
138
|
+
}
|
|
139
|
+
function deriveEventId(hookType, sessionId, host, content) {
|
|
140
|
+
const source = host && typeof host === "object" ? host : {};
|
|
141
|
+
const hostId = hostScalar(source, HOST_ID_FIELDS);
|
|
142
|
+
if (hostId) return `evh_${digest([
|
|
143
|
+
"host",
|
|
144
|
+
sessionId,
|
|
145
|
+
hookType,
|
|
146
|
+
hostId
|
|
147
|
+
])}`;
|
|
148
|
+
return `evc_${digest([
|
|
149
|
+
"content",
|
|
150
|
+
sessionId,
|
|
151
|
+
hookType,
|
|
152
|
+
hostScalar(source, HOST_TIME_FIELDS) ?? "",
|
|
153
|
+
stableStringify(content)
|
|
154
|
+
])}`;
|
|
155
|
+
}
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/cli-data-dir.ts
|
|
158
|
+
function argValue(args, name) {
|
|
159
|
+
const equals = `${name}=`;
|
|
160
|
+
const inline = args.find((arg) => arg.startsWith(equals));
|
|
161
|
+
if (inline) return inline.slice(equals.length);
|
|
162
|
+
const idx = args.indexOf(name);
|
|
163
|
+
if (idx !== -1) return args[idx + 1];
|
|
164
|
+
}
|
|
165
|
+
function expandHome(pathValue, home) {
|
|
166
|
+
if (pathValue === "~") return home;
|
|
167
|
+
if (pathValue.startsWith("~/") || pathValue.startsWith("~\\")) return join(home, pathValue.slice(2));
|
|
168
|
+
return pathValue;
|
|
169
|
+
}
|
|
170
|
+
function absoluteDataDir(pathValue, cwd, home) {
|
|
171
|
+
const expanded = expandHome(pathValue, home);
|
|
172
|
+
return isAbsolute(expanded) ? expanded : resolve(cwd, expanded);
|
|
173
|
+
}
|
|
174
|
+
function platformDefaultDataDir(env, home, nodePlatform, useXdg = true) {
|
|
175
|
+
if (nodePlatform === "darwin") return join(home, "Library", "Application Support", "agentmemory");
|
|
176
|
+
if (nodePlatform === "win32") {
|
|
177
|
+
const appData = env["APPDATA"];
|
|
178
|
+
return appData ? join(appData, "agentmemory") : join(home, ".agentmemory");
|
|
179
|
+
}
|
|
180
|
+
const xdgDataHome = env["XDG_DATA_HOME"];
|
|
181
|
+
if (useXdg && xdgDataHome && isAbsolute(xdgDataHome)) return join(xdgDataHome, "agentmemory");
|
|
182
|
+
return join(home, ".local", "share", "agentmemory");
|
|
183
|
+
}
|
|
184
|
+
function nearestGitParent(pathValue) {
|
|
185
|
+
let current = pathValue;
|
|
186
|
+
while (true) {
|
|
187
|
+
if (existsSync(join(current, ".git"))) return current;
|
|
188
|
+
const parent = dirname(current);
|
|
189
|
+
if (parent === current) return void 0;
|
|
190
|
+
current = parent;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function isWithin(parent, child) {
|
|
194
|
+
const pathFromParent = relative(parent, child);
|
|
195
|
+
return pathFromParent === "" || pathFromParent !== ".." && !pathFromParent.startsWith(`..${sep}`) && !isAbsolute(pathFromParent);
|
|
196
|
+
}
|
|
197
|
+
function resolveDataDir(options = {}) {
|
|
198
|
+
const args = options.args ?? process.argv.slice(2);
|
|
199
|
+
const env = options.env ?? process.env;
|
|
200
|
+
const cwd = options.cwd ?? process.cwd();
|
|
201
|
+
const home = options.home ?? homedir();
|
|
202
|
+
const nodePlatform = options.platform ?? platform();
|
|
203
|
+
const parsedInstance = parseInt(argValue(args, "--instance") ?? "0", 10);
|
|
204
|
+
const instance = parsedInstance > 0 && parsedInstance <= 50 ? parsedInstance : 0;
|
|
205
|
+
const forInstance = (dataDir) => instance ? join(dataDir, `instance-${instance}`) : dataDir;
|
|
206
|
+
const flagValue = argValue(args, "--data-dir");
|
|
207
|
+
if (flagValue) return {
|
|
208
|
+
dataDir: forInstance(absoluteDataDir(flagValue, cwd, home)),
|
|
209
|
+
source: "flag"
|
|
210
|
+
};
|
|
211
|
+
const envValue = env["AGENTMEMORY_DATA_DIR"];
|
|
212
|
+
if (envValue) return {
|
|
213
|
+
dataDir: forInstance(absoluteDataDir(envValue, cwd, home)),
|
|
214
|
+
source: "env"
|
|
215
|
+
};
|
|
216
|
+
const legacyDir = resolve(cwd, "data");
|
|
217
|
+
const hasLegacyStore = existsSync(join(legacyDir, "state_store.db")) || existsSync(join(legacyDir, "iii-config.yaml"));
|
|
218
|
+
if (instance === 0 && hasLegacyStore) return {
|
|
219
|
+
dataDir: legacyDir,
|
|
220
|
+
source: "default"
|
|
221
|
+
};
|
|
222
|
+
const defaultDir = platformDefaultDataDir(env, home, nodePlatform);
|
|
223
|
+
const gitParent = nearestGitParent(cwd);
|
|
224
|
+
if (gitParent && isWithin(gitParent, defaultDir)) {
|
|
225
|
+
const relocated = platformDefaultDataDir(env, home, nodePlatform, false);
|
|
226
|
+
if (relocated !== defaultDir) return {
|
|
227
|
+
dataDir: forInstance(relocated),
|
|
228
|
+
source: "default",
|
|
229
|
+
relocatedFrom: defaultDir
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
dataDir: forInstance(defaultDir),
|
|
234
|
+
source: "default"
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/functions/privacy.ts
|
|
239
|
+
const PRIVATE_TAG_RE = /<private>[\s\S]*?<\/private>/gi;
|
|
240
|
+
const SECRET_PATTERN_SOURCES = [
|
|
241
|
+
/(?:api[_-]?key|secret|token|password|credential|auth)[\s]*[=:]\s*["']?[A-Za-z0-9_\-/.+]{20,}["']?/gi,
|
|
242
|
+
/Bearer\s+[A-Za-z0-9._\-+/=]{20,}/gi,
|
|
243
|
+
/(?<![\p{L}\p{N}\p{M}_])sk-proj-[A-Za-z0-9\-_]{20,}/gu,
|
|
244
|
+
/(?<![\p{L}\p{N}\p{M}_])(?:sk|pk|rk|ak)-[A-Za-z0-9][A-Za-z0-9\-_]{19,}/gu,
|
|
245
|
+
/(?<![\p{L}\p{N}\p{M}_])sk-ant-[A-Za-z0-9\-_]{20,}/gu,
|
|
246
|
+
/gh[pus]_[A-Za-z0-9]{36,}/g,
|
|
247
|
+
/github_pat_[A-Za-z0-9_]{22,}/g,
|
|
248
|
+
/xoxb-[A-Za-z0-9\-]+/g,
|
|
249
|
+
/AKIA[0-9A-Z]{16}/g,
|
|
250
|
+
/AIza[A-Za-z0-9\-_]{35}/g,
|
|
251
|
+
/eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}/g,
|
|
252
|
+
/npm_[A-Za-z0-9]{36}/g,
|
|
253
|
+
/glpat-[A-Za-z0-9\-_]{20,}/g,
|
|
254
|
+
/dop_v1_[A-Za-z0-9]{64}/g
|
|
255
|
+
];
|
|
256
|
+
const PRIVATE_KEY_BLOCK_RE = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----[\s\S]*?(?:-----END [A-Z0-9 ]*PRIVATE KEY(?: BLOCK)?-----|$)/g;
|
|
257
|
+
const URL_CREDENTIALS_RE = /\b([a-z][a-z0-9+.-]*:\/\/)[^\s/?#@:"'<>]*:[^\s/?#@"'<>]+@/gi;
|
|
258
|
+
function stripPrivateData(input) {
|
|
259
|
+
let result = input.replace(PRIVATE_TAG_RE, "[REDACTED]");
|
|
260
|
+
result = result.replace(new RegExp(PRIVATE_KEY_BLOCK_RE.source, PRIVATE_KEY_BLOCK_RE.flags), "[REDACTED_SECRET]");
|
|
261
|
+
result = result.replace(new RegExp(URL_CREDENTIALS_RE.source, URL_CREDENTIALS_RE.flags), "$1[REDACTED_SECRET]@");
|
|
262
|
+
for (const source of SECRET_PATTERN_SOURCES) {
|
|
263
|
+
const pattern = new RegExp(source.source, source.flags);
|
|
264
|
+
result = result.replace(pattern, "[REDACTED_SECRET]");
|
|
265
|
+
}
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
268
|
+
function scrubRecord(value, options = {}) {
|
|
269
|
+
if (typeof value === "string") return stripPrivateData(value);
|
|
270
|
+
if (Array.isArray(value)) return value.map((item) => scrubRecord(item, options));
|
|
271
|
+
if (value !== null && typeof value === "object") {
|
|
272
|
+
const proto = Object.getPrototypeOf(value);
|
|
273
|
+
if (proto !== Object.prototype && proto !== null) return value;
|
|
274
|
+
const entries = Object.entries(value).map(([key, item]) => [options.scrubKeys ? stripPrivateData(key) : key, item]);
|
|
275
|
+
const reserved = new Set(entries.map(([key]) => key));
|
|
276
|
+
const used = /* @__PURE__ */ new Set();
|
|
277
|
+
return Object.fromEntries(entries.map(([key, item]) => {
|
|
278
|
+
let unique = key;
|
|
279
|
+
if (used.has(unique)) {
|
|
280
|
+
let suffix = 2;
|
|
281
|
+
do
|
|
282
|
+
unique = `${key}_${suffix++}`;
|
|
283
|
+
while (reserved.has(unique) || used.has(unique));
|
|
284
|
+
}
|
|
285
|
+
used.add(unique);
|
|
286
|
+
return [unique, scrubRecord(item, options)];
|
|
287
|
+
}));
|
|
288
|
+
}
|
|
289
|
+
return value;
|
|
290
|
+
}
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region src/capture/spool.ts
|
|
293
|
+
const DEFAULT_MAX_BYTES = 5 * 1024 * 1024;
|
|
294
|
+
const MIN_MAX_BYTES = 64 * 1024;
|
|
295
|
+
const DEFAULT_MAX_AGE_HOURS = 168;
|
|
296
|
+
const LOCK_STALE_MS = 1e4;
|
|
297
|
+
const DRAIN_LOCK_STALE_MS = 12e4;
|
|
298
|
+
const ORPHAN_CLAIM_MS = 12e4;
|
|
299
|
+
const LOCK_WAIT_MS = 500;
|
|
300
|
+
const IMAGE_PLACEHOLDER = "[image dropped from capture spool]";
|
|
301
|
+
const BOOT_ID_RE = /^[A-Za-z0-9]{8,64}$/;
|
|
302
|
+
const SENT_FILE_RE = /^([A-Za-z0-9]{8,64})-(\d+)\.jsonl$/;
|
|
303
|
+
const SENT_BUCKET_MS = 1e3;
|
|
304
|
+
const MAX_DURABLE_AFTER_MS = 61 * 6e4;
|
|
305
|
+
function emptyStats() {
|
|
306
|
+
return {
|
|
307
|
+
spooled: 0,
|
|
308
|
+
dropped: 0,
|
|
309
|
+
droppedBytes: 0,
|
|
310
|
+
expired: 0,
|
|
311
|
+
delivered: 0,
|
|
312
|
+
duplicates: 0,
|
|
313
|
+
rejected: 0
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
function positiveInt(raw, fallback) {
|
|
317
|
+
if (!raw || !/^\d+$/.test(raw.trim())) return fallback;
|
|
318
|
+
const n = parseInt(raw.trim(), 10);
|
|
319
|
+
return n > 0 ? n : fallback;
|
|
320
|
+
}
|
|
321
|
+
function spoolPolicy(env = process.env) {
|
|
322
|
+
const maxBytes = Math.max(MIN_MAX_BYTES, positiveInt(env["AGENTMEMORY_CAPTURE_SPOOL_MAX_BYTES"], DEFAULT_MAX_BYTES));
|
|
323
|
+
const maxAgeHours = positiveInt(env["AGENTMEMORY_CAPTURE_SPOOL_MAX_AGE_HOURS"], DEFAULT_MAX_AGE_HOURS);
|
|
324
|
+
return {
|
|
325
|
+
enabled: env["AGENTMEMORY_CAPTURE_SPOOL"] !== "false",
|
|
326
|
+
maxBytes,
|
|
327
|
+
maxAgeMs: maxAgeHours * 36e5,
|
|
328
|
+
maxRecordBytes: Math.min(256 * 1024, Math.floor(maxBytes / 4))
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
function spoolDir(env = process.env) {
|
|
332
|
+
const explicit = env["AGENTMEMORY_CAPTURE_SPOOL_DIR"];
|
|
333
|
+
if (explicit && explicit.trim()) return resolve(explicit.trim());
|
|
334
|
+
return join(resolveDataDir({
|
|
335
|
+
args: [],
|
|
336
|
+
env
|
|
337
|
+
}).dataDir, "capture-spool");
|
|
338
|
+
}
|
|
339
|
+
const LOOPBACK = new Set([
|
|
340
|
+
"localhost",
|
|
341
|
+
"127.0.0.1",
|
|
342
|
+
"::1",
|
|
343
|
+
"[::1]",
|
|
344
|
+
"0.0.0.0"
|
|
345
|
+
]);
|
|
346
|
+
function spoolTargetName(url) {
|
|
347
|
+
try {
|
|
348
|
+
const u = new URL(url);
|
|
349
|
+
return `${LOOPBACK.has(u.hostname) ? "local" : u.hostname.toLowerCase().replace(/[^a-z0-9.-]/g, "_")}-${u.port || (u.protocol === "https:" ? "443" : "80")}`;
|
|
350
|
+
} catch {
|
|
351
|
+
return "local-3111";
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
function spoolPaths(url, dir = spoolDir()) {
|
|
355
|
+
const name = spoolTargetName(url);
|
|
356
|
+
return {
|
|
357
|
+
dir,
|
|
358
|
+
name,
|
|
359
|
+
file: join(dir, `${name}.jsonl`),
|
|
360
|
+
lock: join(dir, `${name}.lock`),
|
|
361
|
+
drainLock: join(dir, `${name}.drain.lock`),
|
|
362
|
+
stats: join(dir, `${name}.stats.json`)
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
function ensureDir(dir) {
|
|
366
|
+
mkdirSync(dir, {
|
|
367
|
+
recursive: true,
|
|
368
|
+
mode: 448
|
|
369
|
+
});
|
|
370
|
+
try {
|
|
371
|
+
chmodSync(dir, 448);
|
|
372
|
+
} catch {}
|
|
373
|
+
}
|
|
374
|
+
function sleepSync(ms) {
|
|
375
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
|
|
376
|
+
}
|
|
377
|
+
function tryLock(path, staleMs) {
|
|
378
|
+
try {
|
|
379
|
+
const fd = openSync(path, "wx", 384);
|
|
380
|
+
writeSync(fd, `${process.pid}\n`);
|
|
381
|
+
closeSync(fd);
|
|
382
|
+
return true;
|
|
383
|
+
} catch (err) {
|
|
384
|
+
if (err.code !== "EEXIST") return false;
|
|
385
|
+
try {
|
|
386
|
+
if (Date.now() - statSync(path).mtimeMs > staleMs) unlinkSync(path);
|
|
387
|
+
} catch {}
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
function releaseLock(path) {
|
|
392
|
+
try {
|
|
393
|
+
unlinkSync(path);
|
|
394
|
+
} catch {}
|
|
395
|
+
}
|
|
396
|
+
function withLock(path, fn, waitMs = LOCK_WAIT_MS) {
|
|
397
|
+
const deadline = Date.now() + waitMs;
|
|
398
|
+
let locked = tryLock(path, LOCK_STALE_MS);
|
|
399
|
+
while (!locked && Date.now() < deadline) {
|
|
400
|
+
sleepSync(5);
|
|
401
|
+
locked = tryLock(path, LOCK_STALE_MS);
|
|
402
|
+
}
|
|
403
|
+
try {
|
|
404
|
+
return fn(locked);
|
|
405
|
+
} finally {
|
|
406
|
+
if (locked) releaseLock(path);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
function readSpoolStats(paths) {
|
|
410
|
+
try {
|
|
411
|
+
const parsed = JSON.parse(readFileSync(paths.stats, "utf-8"));
|
|
412
|
+
return {
|
|
413
|
+
...emptyStats(),
|
|
414
|
+
...parsed
|
|
415
|
+
};
|
|
416
|
+
} catch {
|
|
417
|
+
return emptyStats();
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
function writeStats(paths, patch) {
|
|
421
|
+
try {
|
|
422
|
+
const stats = readSpoolStats(paths);
|
|
423
|
+
patch(stats);
|
|
424
|
+
const tmp = `${paths.stats}.${process.pid}.tmp`;
|
|
425
|
+
writeFileSync(tmp, JSON.stringify(stats), { mode: 384 });
|
|
426
|
+
renameSync(tmp, paths.stats);
|
|
427
|
+
} catch {}
|
|
428
|
+
}
|
|
429
|
+
function fileSize(path) {
|
|
430
|
+
try {
|
|
431
|
+
return statSync(path).size;
|
|
432
|
+
} catch {
|
|
433
|
+
return 0;
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
function withoutImage(body) {
|
|
437
|
+
const data = body["data"];
|
|
438
|
+
if (!data || typeof data !== "object" || Array.isArray(data)) return body;
|
|
439
|
+
const d = data;
|
|
440
|
+
if (d["image_data"] === void 0) return body;
|
|
441
|
+
return {
|
|
442
|
+
...body,
|
|
443
|
+
data: {
|
|
444
|
+
...d,
|
|
445
|
+
image_data: IMAGE_PLACEHOLDER
|
|
446
|
+
}
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
function parseLines(text) {
|
|
450
|
+
const records = [];
|
|
451
|
+
let corrupt = 0;
|
|
452
|
+
for (const line of text.split("\n")) {
|
|
453
|
+
if (!line.trim()) continue;
|
|
454
|
+
try {
|
|
455
|
+
const rec = JSON.parse(line);
|
|
456
|
+
if (rec && typeof rec.eventId === "string" && rec.body && typeof rec.body === "object") records.push(rec);
|
|
457
|
+
else corrupt++;
|
|
458
|
+
} catch {
|
|
459
|
+
corrupt++;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
records,
|
|
464
|
+
corrupt
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
function isExpired(rec, policy, now) {
|
|
468
|
+
const at = Date.parse(rec.spooledAt);
|
|
469
|
+
return Number.isFinite(at) && now - at > policy.maxAgeMs;
|
|
470
|
+
}
|
|
471
|
+
function serialize(records) {
|
|
472
|
+
return records.map((r) => JSON.stringify(r)).join("\n") + (records.length ? "\n" : "");
|
|
473
|
+
}
|
|
474
|
+
function rewriteLocked(paths, records) {
|
|
475
|
+
const tmp = `${paths.file}.${process.pid}.tmp`;
|
|
476
|
+
const fd = openSync(tmp, "w", 384);
|
|
477
|
+
try {
|
|
478
|
+
writeSync(fd, serialize(records));
|
|
479
|
+
fsyncSync(fd);
|
|
480
|
+
} finally {
|
|
481
|
+
closeSync(fd);
|
|
482
|
+
}
|
|
483
|
+
renameSync(tmp, paths.file);
|
|
484
|
+
}
|
|
485
|
+
function pruneExpiredLocked(paths, policy) {
|
|
486
|
+
if (!existsSync(paths.file)) return 0;
|
|
487
|
+
const { records } = parseLines(readFileSync(paths.file, "utf-8"));
|
|
488
|
+
const now = Date.now();
|
|
489
|
+
const kept = records.filter((r) => !isExpired(r, policy, now));
|
|
490
|
+
const expired = records.length - kept.length;
|
|
491
|
+
if (expired > 0) rewriteLocked(paths, kept);
|
|
492
|
+
return expired;
|
|
493
|
+
}
|
|
494
|
+
function appendSpool(url, eventId, body, reason, options = {}) {
|
|
495
|
+
const policy = options.policy ?? spoolPolicy();
|
|
496
|
+
if (!policy.enabled) return {
|
|
497
|
+
spooled: false,
|
|
498
|
+
dropped: "disabled"
|
|
499
|
+
};
|
|
500
|
+
const paths = spoolPaths(url, options.dir);
|
|
501
|
+
const note = (dropReason, bytes) => {
|
|
502
|
+
writeStats(paths, (s) => {
|
|
503
|
+
s.dropped++;
|
|
504
|
+
s.droppedBytes += bytes;
|
|
505
|
+
s.lastDropAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
506
|
+
s.lastDropReason = dropReason;
|
|
507
|
+
});
|
|
508
|
+
return {
|
|
509
|
+
spooled: false,
|
|
510
|
+
dropped: dropReason
|
|
511
|
+
};
|
|
512
|
+
};
|
|
513
|
+
try {
|
|
514
|
+
ensureDir(paths.dir);
|
|
515
|
+
const record = {
|
|
516
|
+
v: 1,
|
|
517
|
+
eventId,
|
|
518
|
+
spooledAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
519
|
+
reason,
|
|
520
|
+
attempts: options.attempts ?? 0,
|
|
521
|
+
body: scrubRecord(JSON.parse(JSON.stringify(body)), { scrubKeys: true })
|
|
522
|
+
};
|
|
523
|
+
let line = JSON.stringify(record) + "\n";
|
|
524
|
+
if (Buffer.byteLength(line) > policy.maxRecordBytes) {
|
|
525
|
+
record.body = withoutImage(record.body);
|
|
526
|
+
line = JSON.stringify(record) + "\n";
|
|
527
|
+
}
|
|
528
|
+
const bytes = Buffer.byteLength(line);
|
|
529
|
+
if (bytes > policy.maxRecordBytes) return note("too-large", bytes);
|
|
530
|
+
return withLock(paths.lock, (locked) => {
|
|
531
|
+
let expired = 0;
|
|
532
|
+
if (locked && fileSize(paths.file) + bytes > policy.maxBytes) expired = pruneExpiredLocked(paths, policy);
|
|
533
|
+
const over = fileSize(paths.file) + sentBytes(paths) + bytes - policy.maxBytes;
|
|
534
|
+
if (locked && over > 0) evictSent(paths, over);
|
|
535
|
+
if (fileSize(paths.file) + sentBytes(paths) + bytes > policy.maxBytes) {
|
|
536
|
+
if (expired) writeStats(paths, (s) => void (s.expired += expired));
|
|
537
|
+
return note("full", bytes);
|
|
538
|
+
}
|
|
539
|
+
const fd = openSync(paths.file, "a", 384);
|
|
540
|
+
try {
|
|
541
|
+
writeSync(fd, line);
|
|
542
|
+
fsyncSync(fd);
|
|
543
|
+
} finally {
|
|
544
|
+
closeSync(fd);
|
|
545
|
+
}
|
|
546
|
+
writeStats(paths, (s) => {
|
|
547
|
+
s.spooled++;
|
|
548
|
+
s.expired += expired;
|
|
549
|
+
s.lastSpooledAt = record.spooledAt;
|
|
550
|
+
s.lastSpoolReason = reason;
|
|
551
|
+
});
|
|
552
|
+
return { spooled: true };
|
|
553
|
+
});
|
|
554
|
+
} catch {
|
|
555
|
+
return {
|
|
556
|
+
spooled: false,
|
|
557
|
+
dropped: "error"
|
|
558
|
+
};
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
function listSent(paths) {
|
|
562
|
+
const segments = [];
|
|
563
|
+
let entries;
|
|
564
|
+
try {
|
|
565
|
+
entries = readdirSync(paths.dir);
|
|
566
|
+
} catch {
|
|
567
|
+
return segments;
|
|
568
|
+
}
|
|
569
|
+
const prefix = `${paths.name}.sent-`;
|
|
570
|
+
for (const entry of entries) {
|
|
571
|
+
if (!entry.startsWith(prefix)) continue;
|
|
572
|
+
const match = entry.slice(prefix.length).match(SENT_FILE_RE);
|
|
573
|
+
if (!match) continue;
|
|
574
|
+
const file = join(paths.dir, entry);
|
|
575
|
+
segments.push({
|
|
576
|
+
file,
|
|
577
|
+
bootId: match[1],
|
|
578
|
+
dueAt: Number(match[2]),
|
|
579
|
+
bytes: fileSize(file)
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
return segments;
|
|
583
|
+
}
|
|
584
|
+
function sentBytes(paths) {
|
|
585
|
+
return listSent(paths).reduce((n, s) => n + s.bytes, 0);
|
|
586
|
+
}
|
|
587
|
+
function evictSent(paths, need) {
|
|
588
|
+
let freed = 0;
|
|
589
|
+
for (const seg of listSent(paths).sort((a, b) => a.dueAt - b.dueAt)) {
|
|
590
|
+
if (freed >= need) return;
|
|
591
|
+
try {
|
|
592
|
+
unlinkSync(seg.file);
|
|
593
|
+
freed += seg.bytes;
|
|
594
|
+
} catch {}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
function parseSentMark(body) {
|
|
598
|
+
if (!body || typeof body !== "object" || Array.isArray(body)) return null;
|
|
599
|
+
const bootId = body["bootId"];
|
|
600
|
+
const after = body["durableAfterMs"];
|
|
601
|
+
if (typeof bootId !== "string" || !BOOT_ID_RE.test(bootId)) return null;
|
|
602
|
+
if (typeof after !== "number" || !Number.isFinite(after) || after < 0) return null;
|
|
603
|
+
return {
|
|
604
|
+
bootId,
|
|
605
|
+
durableAfterMs: Math.min(Math.ceil(after), MAX_DURABLE_AFTER_MS)
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
function retainSent(url, eventId, body, mark, options = {}) {
|
|
609
|
+
const policy = options.policy ?? spoolPolicy();
|
|
610
|
+
if (!policy.enabled || !BOOT_ID_RE.test(mark.bootId)) return false;
|
|
611
|
+
const paths = spoolPaths(url, options.dir);
|
|
612
|
+
const now = options.now ?? Date.now();
|
|
613
|
+
try {
|
|
614
|
+
ensureDir(paths.dir);
|
|
615
|
+
const record = {
|
|
616
|
+
v: 1,
|
|
617
|
+
eventId,
|
|
618
|
+
spooledAt: new Date(now).toISOString(),
|
|
619
|
+
reason: "sent",
|
|
620
|
+
attempts: 0,
|
|
621
|
+
body: scrubRecord(JSON.parse(JSON.stringify(body)), { scrubKeys: true }),
|
|
622
|
+
bootId: mark.bootId
|
|
623
|
+
};
|
|
624
|
+
let line = JSON.stringify(record) + "\n";
|
|
625
|
+
if (Buffer.byteLength(line) > policy.maxRecordBytes) {
|
|
626
|
+
record.body = withoutImage(record.body);
|
|
627
|
+
line = JSON.stringify(record) + "\n";
|
|
628
|
+
}
|
|
629
|
+
const bytes = Buffer.byteLength(line);
|
|
630
|
+
if (bytes > policy.maxRecordBytes) return false;
|
|
631
|
+
const dueAt = Math.ceil((now + mark.durableAfterMs) / SENT_BUCKET_MS) * SENT_BUCKET_MS;
|
|
632
|
+
const file = join(paths.dir, `${paths.name}.sent-${mark.bootId}-${dueAt}.jsonl`);
|
|
633
|
+
return withLock(paths.lock, () => {
|
|
634
|
+
if (fileSize(paths.file) + sentBytes(paths) + bytes > policy.maxBytes) return false;
|
|
635
|
+
const fd = openSync(file, "a", 384);
|
|
636
|
+
try {
|
|
637
|
+
writeSync(fd, line);
|
|
638
|
+
} finally {
|
|
639
|
+
closeSync(fd);
|
|
640
|
+
}
|
|
641
|
+
return true;
|
|
642
|
+
});
|
|
643
|
+
} catch {
|
|
644
|
+
return false;
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
function reconcileSent(url, bootId, options = {}) {
|
|
648
|
+
const result = {
|
|
649
|
+
released: 0,
|
|
650
|
+
requeued: 0
|
|
651
|
+
};
|
|
652
|
+
if (!(options.policy ?? spoolPolicy()).enabled) return result;
|
|
653
|
+
const paths = spoolPaths(url, options.dir);
|
|
654
|
+
const now = options.now ?? Date.now();
|
|
655
|
+
for (const seg of listSent(paths)) {
|
|
656
|
+
if (seg.bootId === bootId) {
|
|
657
|
+
if (seg.dueAt > now) continue;
|
|
658
|
+
try {
|
|
659
|
+
unlinkSync(seg.file);
|
|
660
|
+
result.released++;
|
|
661
|
+
} catch {}
|
|
662
|
+
continue;
|
|
663
|
+
}
|
|
664
|
+
const target = join(paths.dir, `${paths.name}.draining-${process.pid}-${now}-${result.requeued}.jsonl`);
|
|
665
|
+
try {
|
|
666
|
+
renameSync(seg.file, target);
|
|
667
|
+
} catch {
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
try {
|
|
671
|
+
utimesSync(target, 0, 0);
|
|
672
|
+
} catch {}
|
|
673
|
+
result.requeued++;
|
|
674
|
+
}
|
|
675
|
+
return result;
|
|
676
|
+
}
|
|
677
|
+
function claimFiles(paths) {
|
|
678
|
+
const claimed = [];
|
|
679
|
+
try {
|
|
680
|
+
const prefix = `${paths.name}.draining-`;
|
|
681
|
+
for (const entry of readdirSync(paths.dir)) {
|
|
682
|
+
if (!entry.startsWith(prefix) || !entry.endsWith(".jsonl")) continue;
|
|
683
|
+
const full = join(paths.dir, entry);
|
|
684
|
+
try {
|
|
685
|
+
if (Date.now() - statSync(full).mtimeMs > ORPHAN_CLAIM_MS) claimed.push(full);
|
|
686
|
+
} catch {}
|
|
687
|
+
}
|
|
688
|
+
} catch {}
|
|
689
|
+
if (fileSize(paths.file) > 0) {
|
|
690
|
+
const target = join(paths.dir, `${paths.name}.draining-${process.pid}-${Date.now()}.jsonl`);
|
|
691
|
+
withLock(paths.lock, (locked) => {
|
|
692
|
+
if (!locked) return;
|
|
693
|
+
try {
|
|
694
|
+
renameSync(paths.file, target);
|
|
695
|
+
claimed.push(target);
|
|
696
|
+
} catch {}
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
return claimed;
|
|
700
|
+
}
|
|
701
|
+
function requeueLocked(paths, leftover) {
|
|
702
|
+
if (leftover.length === 0) return;
|
|
703
|
+
const existing = existsSync(paths.file) ? parseLines(readFileSync(paths.file, "utf-8")).records : [];
|
|
704
|
+
rewriteLocked(paths, [...leftover, ...existing]);
|
|
705
|
+
}
|
|
706
|
+
function requeueUnlocked(paths, leftover) {
|
|
707
|
+
if (leftover.length === 0) return;
|
|
708
|
+
const target = join(paths.dir, `${paths.name}.draining-${process.pid}-${Date.now()}-requeue.jsonl`);
|
|
709
|
+
const fd = openSync(target, "wx", 384);
|
|
710
|
+
try {
|
|
711
|
+
writeSync(fd, serialize(leftover));
|
|
712
|
+
fsyncSync(fd);
|
|
713
|
+
} finally {
|
|
714
|
+
closeSync(fd);
|
|
715
|
+
}
|
|
716
|
+
try {
|
|
717
|
+
utimesSync(target, 0, 0);
|
|
718
|
+
} catch {}
|
|
719
|
+
}
|
|
720
|
+
function spoolHasRecords(url, dir) {
|
|
721
|
+
return fileSize(spoolPaths(url, dir).file) > 0;
|
|
722
|
+
}
|
|
723
|
+
function drainInProgress(url, dir) {
|
|
724
|
+
const paths = spoolPaths(url, dir);
|
|
725
|
+
try {
|
|
726
|
+
return Date.now() - statSync(paths.drainLock).mtimeMs < DRAIN_LOCK_STALE_MS;
|
|
727
|
+
} catch {
|
|
728
|
+
return false;
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
async function drainSpool(url, send, options = {}) {
|
|
732
|
+
const policy = options.policy ?? spoolPolicy();
|
|
733
|
+
const result = {
|
|
734
|
+
claimed: 0,
|
|
735
|
+
delivered: 0,
|
|
736
|
+
duplicates: 0,
|
|
737
|
+
rejected: 0,
|
|
738
|
+
expired: 0,
|
|
739
|
+
corrupt: 0,
|
|
740
|
+
remaining: 0
|
|
741
|
+
};
|
|
742
|
+
const paths = spoolPaths(url, options.dir);
|
|
743
|
+
if (!existsSync(paths.dir)) return {
|
|
744
|
+
...result,
|
|
745
|
+
skipped: "empty"
|
|
746
|
+
};
|
|
747
|
+
try {
|
|
748
|
+
ensureDir(paths.dir);
|
|
749
|
+
} catch {}
|
|
750
|
+
if (!tryLock(paths.drainLock, DRAIN_LOCK_STALE_MS)) return {
|
|
751
|
+
...result,
|
|
752
|
+
skipped: "locked"
|
|
753
|
+
};
|
|
754
|
+
try {
|
|
755
|
+
const files = claimFiles(paths);
|
|
756
|
+
if (files.length === 0) return {
|
|
757
|
+
...result,
|
|
758
|
+
skipped: "empty"
|
|
759
|
+
};
|
|
760
|
+
const records = [];
|
|
761
|
+
for (const file of files) try {
|
|
762
|
+
const parsed = parseLines(readFileSync(file, "utf-8"));
|
|
763
|
+
records.push(...parsed.records);
|
|
764
|
+
result.corrupt += parsed.corrupt;
|
|
765
|
+
} catch {}
|
|
766
|
+
result.claimed = records.length;
|
|
767
|
+
const seen = /* @__PURE__ */ new Set();
|
|
768
|
+
const now = Date.now();
|
|
769
|
+
const deadline = options.deadlineMs ? now + options.deadlineMs : Number.POSITIVE_INFINITY;
|
|
770
|
+
const maxRecords = options.maxRecords ?? Number.POSITIVE_INFINITY;
|
|
771
|
+
const leftover = [];
|
|
772
|
+
let stopped = false;
|
|
773
|
+
let processed = 0;
|
|
774
|
+
for (const rec of records) {
|
|
775
|
+
if (seen.has(rec.eventId)) {
|
|
776
|
+
result.duplicates++;
|
|
777
|
+
continue;
|
|
778
|
+
}
|
|
779
|
+
seen.add(rec.eventId);
|
|
780
|
+
if (isExpired(rec, policy, now)) {
|
|
781
|
+
result.expired++;
|
|
782
|
+
continue;
|
|
783
|
+
}
|
|
784
|
+
if (stopped || processed >= maxRecords || Date.now() > deadline) {
|
|
785
|
+
leftover.push(rec);
|
|
786
|
+
continue;
|
|
787
|
+
}
|
|
788
|
+
processed++;
|
|
789
|
+
let outcome;
|
|
790
|
+
try {
|
|
791
|
+
outcome = await send(rec);
|
|
792
|
+
} catch (err) {
|
|
793
|
+
outcome = "retry";
|
|
794
|
+
result.error = err instanceof Error ? err.message : String(err);
|
|
795
|
+
}
|
|
796
|
+
if (outcome === "delivered") result.delivered++;
|
|
797
|
+
else if (outcome === "duplicate") result.duplicates++;
|
|
798
|
+
else if (outcome === "rejected") result.rejected++;
|
|
799
|
+
else {
|
|
800
|
+
stopped = true;
|
|
801
|
+
leftover.push({
|
|
802
|
+
...rec,
|
|
803
|
+
attempts: (rec.attempts ?? 0) + 1
|
|
804
|
+
});
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
result.remaining = leftover.length;
|
|
808
|
+
withLock(paths.lock, (locked) => locked ? requeueLocked(paths, leftover) : requeueUnlocked(paths, leftover), 2e3);
|
|
809
|
+
for (const file of files) try {
|
|
810
|
+
unlinkSync(file);
|
|
811
|
+
} catch {}
|
|
812
|
+
writeStats(paths, (s) => {
|
|
813
|
+
s.delivered += result.delivered;
|
|
814
|
+
s.duplicates += result.duplicates;
|
|
815
|
+
s.rejected += result.rejected;
|
|
816
|
+
s.expired += result.expired;
|
|
817
|
+
s.lastDrainAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
818
|
+
s.lastDrainDelivered = result.delivered;
|
|
819
|
+
s.lastDrainDuplicates = result.duplicates;
|
|
820
|
+
s.lastDrainRejected = result.rejected;
|
|
821
|
+
s.lastDrainRemaining = result.remaining;
|
|
822
|
+
if (result.error) s.lastDrainError = result.error;
|
|
823
|
+
else delete s.lastDrainError;
|
|
824
|
+
});
|
|
825
|
+
return result;
|
|
826
|
+
} finally {
|
|
827
|
+
releaseLock(paths.drainLock);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
//#endregion
|
|
831
|
+
//#region src/hooks/_capture.ts
|
|
832
|
+
const REST_URL = process.env["AGENTMEMORY_URL"] || "http://localhost:3111";
|
|
833
|
+
const SECRET = resolveClientSecret(REST_URL);
|
|
834
|
+
const DRAIN_CHILD_ENV = "AGENTMEMORY_CAPTURE_DRAIN_CHILD";
|
|
835
|
+
const DRAIN_MAX_RECORDS = 500;
|
|
836
|
+
const DRAIN_DEADLINE_MS = 2e4;
|
|
837
|
+
const DRAIN_REQUEST_TIMEOUT_MS = 5e3;
|
|
838
|
+
function authHeaders() {
|
|
839
|
+
const h = { "Content-Type": "application/json" };
|
|
840
|
+
if (SECRET) h["Authorization"] = `Bearer ${SECRET}`;
|
|
841
|
+
return h;
|
|
842
|
+
}
|
|
843
|
+
function withEventId(body, host, content, options = {}) {
|
|
844
|
+
const source = options.stable || hasHostIdentity(host) ? host : {
|
|
845
|
+
...host,
|
|
846
|
+
timestamp: body.timestamp
|
|
847
|
+
};
|
|
848
|
+
return {
|
|
849
|
+
...body,
|
|
850
|
+
eventId: deriveEventId(body.hookType, body.sessionId, source, content ?? body.data)
|
|
851
|
+
};
|
|
852
|
+
}
|
|
853
|
+
function classify(status) {
|
|
854
|
+
if (status >= 200 && status < 300) return status === 200 ? "duplicate" : "delivered";
|
|
855
|
+
if (status === 408 || status === 429 || status >= 500) return "retry";
|
|
856
|
+
return "rejected";
|
|
857
|
+
}
|
|
858
|
+
async function post(body, timeoutMs) {
|
|
859
|
+
const res = await fetch(`${REST_URL}/agentmemory/observe`, {
|
|
860
|
+
method: "POST",
|
|
861
|
+
headers: authHeaders(),
|
|
862
|
+
body: JSON.stringify(body),
|
|
863
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
864
|
+
});
|
|
865
|
+
const text = await res.text().catch(() => "");
|
|
866
|
+
const outcome = classify(res.status);
|
|
867
|
+
if (outcome !== "delivered" && outcome !== "duplicate") return {
|
|
868
|
+
outcome,
|
|
869
|
+
mark: null
|
|
870
|
+
};
|
|
871
|
+
try {
|
|
872
|
+
return {
|
|
873
|
+
outcome,
|
|
874
|
+
mark: parseSentMark(JSON.parse(text))
|
|
875
|
+
};
|
|
876
|
+
} catch {
|
|
877
|
+
return {
|
|
878
|
+
outcome,
|
|
879
|
+
mark: null
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
}
|
|
883
|
+
function keepUntilDurable(eventId, body, mark) {
|
|
884
|
+
const { requeued } = reconcileSent(REST_URL, mark.bootId);
|
|
885
|
+
retainSent(REST_URL, eventId, body, mark);
|
|
886
|
+
return requeued;
|
|
887
|
+
}
|
|
888
|
+
function startDrainChild() {
|
|
889
|
+
if (process.env[DRAIN_CHILD_ENV] === "1") return;
|
|
890
|
+
if (!process.argv[1] || drainInProgress(REST_URL)) return;
|
|
891
|
+
try {
|
|
892
|
+
const child = spawn(process.execPath, [process.argv[1]], {
|
|
893
|
+
detached: true,
|
|
894
|
+
stdio: "ignore",
|
|
895
|
+
windowsHide: true,
|
|
896
|
+
env: {
|
|
897
|
+
...process.env,
|
|
898
|
+
[DRAIN_CHILD_ENV]: "1"
|
|
899
|
+
}
|
|
900
|
+
});
|
|
901
|
+
child.on("error", () => {});
|
|
902
|
+
child.unref();
|
|
903
|
+
} catch {}
|
|
904
|
+
}
|
|
905
|
+
async function captureObservation(body, timeoutMs) {
|
|
906
|
+
const eventId = body.eventId ?? deriveEventId(body.hookType, body.sessionId, null, body.data);
|
|
907
|
+
const payload = {
|
|
908
|
+
...body,
|
|
909
|
+
eventId
|
|
910
|
+
};
|
|
911
|
+
let reason;
|
|
912
|
+
try {
|
|
913
|
+
const { outcome, mark } = await post(payload, timeoutMs);
|
|
914
|
+
if (outcome !== "retry") {
|
|
915
|
+
if ((mark ? keepUntilDurable(eventId, payload, mark) : 0) > 0 || spoolHasRecords(REST_URL)) startDrainChild();
|
|
916
|
+
return outcome;
|
|
917
|
+
}
|
|
918
|
+
reason = "server-error";
|
|
919
|
+
} catch (err) {
|
|
920
|
+
const name = err instanceof Error ? err.name : "";
|
|
921
|
+
reason = name === "TimeoutError" || name === "AbortError" ? "timeout" : "unreachable";
|
|
922
|
+
}
|
|
923
|
+
return appendSpool(REST_URL, eventId, payload, reason).spooled ? "spooled" : "dropped";
|
|
924
|
+
}
|
|
925
|
+
function isDrainChild() {
|
|
926
|
+
return process.env[DRAIN_CHILD_ENV] === "1";
|
|
927
|
+
}
|
|
928
|
+
async function runDrainChild() {
|
|
929
|
+
await drainSpool(REST_URL, async (record) => {
|
|
930
|
+
const { outcome, mark } = await post({
|
|
931
|
+
...record.body,
|
|
932
|
+
eventId: record.eventId
|
|
933
|
+
}, DRAIN_REQUEST_TIMEOUT_MS);
|
|
934
|
+
if (mark) retainSent(REST_URL, record.eventId, record.body, mark);
|
|
935
|
+
return outcome;
|
|
936
|
+
}, {
|
|
937
|
+
maxRecords: DRAIN_MAX_RECORDS,
|
|
938
|
+
deadlineMs: DRAIN_DEADLINE_MS
|
|
939
|
+
}).catch(() => void 0);
|
|
940
|
+
}
|
|
941
|
+
//#endregion
|
|
942
|
+
export { REST_URL, authHeaders, captureObservation, isDrainChild, runDrainChild, stripPrivateData, withEventId };
|
|
943
|
+
|
|
944
|
+
//# sourceMappingURL=_capture.mjs.map
|