Compare commits

...

41 Commits

Author SHA1 Message Date
Alex Newman f7f11b2a4b chore(release): v8.5.0 - Cursor Support Now Available
🎉 Major release introducing full Cursor IDE support

New Features:
- Cursor IDE integration with native hook system
- Interactive setup wizard (bun run cursor:setup)
- Works without Claude Code using Gemini (free) or OpenRouter
- Cross-platform: macOS, Linux, Windows (PowerShell)
- Context injection via .cursor/rules directory
- Project registry for multi-workspace support
- MCP search tools for Cursor

Documentation:
- Full docs at docs.claude-mem.ai/cursor
- Gemini and OpenRouter setup guides

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 23:12:15 -05:00
Alex Newman 69b2355b22 Merge pull request #493 from thedotmack/cursor-hooks-integration
feat(cursor): Complete Cursor IDE integration with cross-platform hooks
2025-12-29 23:07:07 -05:00
Alex Newman 9f1498ed24 refactor(docs): Update links in setup guides for consistency and clarity 2025-12-29 23:06:50 -05:00
Alex Newman e012c3e885 build worker service 2025-12-29 22:59:08 -05:00
Alex Newman 129c22c48d Add comprehensive tests for Cursor functionality
- Implement tests for cursor context updates in `cursor-context-update.test.ts`, validating context file creation, content structure, and edge cases.
- Create tests for cursor hook outputs in `cursor-hook-outputs.test.ts`, ensuring correct JSON output from hook scripts and handling of various input scenarios.
- Add tests for JSON utility functions in `cursor-hooks-json-utils.test.ts`, covering parsing, project name extraction, and URL encoding.
- Introduce tests for MCP configuration in `cursor-mcp-config.test.ts`, verifying configuration creation, updates, and format validation.
- Develop tests for the cursor project registry in `cursor-registry.test.ts`, ensuring correct registration, unregistration, and JSON format compliance.
2025-12-29 22:58:42 -05:00
Alex Newman 110d055b8b refactor(cursor): Update installation instructions for clarity and consistency 2025-12-29 22:40:03 -05:00
Alex Newman 101d2dd514 Implement feature X to enhance user experience and optimize performance 2025-12-29 22:32:28 -05:00
Alex Newman 646db6c490 refactor(cursor): Improve portability of sed commands in install script and update error handling in common.ps1 2025-12-29 22:32:07 -05:00
Alex Newman d160df4b86 refactor(cursor): Migrate setup commands from npm to Bun for improved performance
- Updated installation and setup commands in documentation to use Bun instead of npm.
- Adjusted commands across various setup guides including QUICKSTART.md, STANDALONE-SETUP.md, and others.
- Ensured consistency in command usage for all platforms (macOS, Linux, Windows).

This change enhances the installation process and aligns with the recent performance improvements associated with Bun.
2025-12-29 22:15:47 -05:00
Alex Newman b5e45377b0 feat: Enhance interactive setup for Claude memory integration
- Updated environment check to verify Claude Code presence.
- Improved provider selection process with clearer options and descriptions.
- Added functionality to keep current settings during provider configuration.
- Introduced installation scope selection for cursor hooks (project/user/skip).
- Implemented MCP server configuration in Cursor's mcp.json with error handling.
- Added utility functions to find MCP server script path and manage configurations.
2025-12-29 22:02:52 -05:00
Alex Newman 89a0a1baa3 feat(cursor): Update cursor commands to use Bun for improved performance 2025-12-29 21:33:48 -05:00
Alex Newman a82d1a24b9 feat(cursor): Add Windows PowerShell support for Cursor hooks
Complete Windows parity with bash scripts:
- Create 7 PowerShell scripts mirroring bash functionality
- Update installer to detect platform and install appropriate scripts
- Generate platform-specific hooks.json with PowerShell invocation
- Add enterprise support for Windows (ProgramData/Cursor)
- Update findCursorHooksDir to check for both .sh and .ps1
- Add comprehensive Windows documentation to STANDALONE-SETUP.md

Scripts added: common.ps1, session-init.ps1, context-inject.ps1,
save-observation.ps1, save-file-edit.ps1, session-summary.ps1, user-message.ps1

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 21:16:41 -05:00
Alex Newman ed8269250d docs(cursor): Add marketing copy and value proposition messaging
Add compelling marketing copy to Cursor documentation with consistent
messaging across all files:

- README.md: Added headline, "Why Claude-Mem?" section, quick install
- STANDALONE-SETUP.md: Added tagline and enhanced value props
- QUICKSTART.md: Added tagline and benefit statement
- docs/public/cursor/index.mdx: Added card grid highlighting benefits

Key messaging themes:
- "Persistent AI Memory for Cursor"
- "Your AI stops forgetting"
- "Free tier options available"
- "Works with or without Claude Code"

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 21:09:00 -05:00
Alex Newman 5325db1978 docs(cursor): Add Mintlify public documentation for Cursor integration
Create searchable public documentation for Cursor users:
- cursor/index.mdx: Landing page with installation paths and quick reference
- cursor/gemini-setup.mdx: Step-by-step Gemini free tier setup guide
- cursor/openrouter-setup.mdx: OpenRouter setup with model recommendations

Add Cursor Integration navigation group to docs.json.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 21:06:12 -05:00
Alex Newman 01ac957a23 feat(cursor): Add interactive setup wizard for standalone Cursor users
Phase 2 implementation: Enhanced CLI UX with guided first-run experience.

- Add `npm run cursor:setup` command for interactive wizard
- Auto-detect Claude Code installation
- Guide provider selection (Gemini recommended for free tier)
- Configure API keys interactively with settings persistence
- Auto-start worker and install hooks
- Clear instructions for next steps

This enables Cursor users without Claude Code to easily configure
claude-mem with free-tier providers like Gemini.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 21:02:15 -05:00
Alex Newman 8397f63af1 docs(cursor): Add standalone setup guide for Cursor users without Claude Code
- Create STANDALONE-SETUP.md with complete setup instructions for Gemini/OpenRouter
- Add "Quick Start for Cursor Users" section to README.md with standalone link
- Add provider configuration section to QUICKSTART.md with API key setup
- Fix worker restart command in README.md troubleshooting section

This enables Cursor users without Claude Code subscription to use claude-mem
with free-tier AI providers (Gemini 1500 req/day, OpenRouter free models).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 20:47:22 -05:00
Alex Newman bca6b06919 feat(cursor): Enhance context injection and project registry management
- Updated `context-inject.sh` to refresh context before prompt submission and ensure worker is running.
- Added functionality to register and unregister projects for automatic context updates in `worker-service.ts`.
- Implemented methods to read and write the cursor project registry, allowing for better management of installed hooks.
- Integrated context updates into the `GeminiAgent`, `OpenRouterAgent`, and `SDKAgent` to ensure the latest context is available during sessions.

This update improves the integration of Claude-Mem with Cursor, ensuring that context is consistently updated and accessible across sessions.
2025-12-29 20:25:16 -05:00
Alex Newman 8d485890b9 feat(cursor): Add Claude-Mem Cursor hooks installation and management
- Introduced functionality for installing, uninstalling, and checking the status of Cursor hooks.
- Added a new command structure for managing hooks with detailed usage instructions.
- Implemented a method to locate the cursor-hooks directory across different environments.
- Updated build-hooks script to inform users about the location of Cursor hooks.

This enhancement streamlines the integration of Claude-Mem with Cursor, improving user experience and accessibility of hooks.
2025-12-29 20:14:23 -05:00
Alex Newman 6c25bbcbf4 docs: update CHANGELOG.md for v8.2.10 release 2025-12-29 19:11:41 -05:00
Alex Newman 22760f0b7a chore(release): v8.2.10
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 19:10:32 -05:00
Alex Newman b30c3d193f fix(worker): Auto-restart worker on version mismatch (#484)
When the plugin updates but the worker was already running on the old version,
hooks would fail with 400 errors because the new hook scripts tried to call
APIs that don't exist in the old worker.

Changes:
- /api/version now returns BUILT_IN_VERSION (compiled at build time) instead
  of reading from disk at runtime
- worker-service start command now checks for version mismatch and
  auto-restarts if the running worker version differs from plugin version
- Downgraded hook version mismatch warning to debug logging (now handled
  by auto-restart)

Fixes #484

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 19:08:24 -05:00
Alex Newman 78f6008d63 docs: update CHANGELOG.md for v8.2.9 release
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 18:45:13 -05:00
Alex Newman 89919db7ce chore(release): v8.2.9
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-29 18:42:46 -05:00
Alex Newman 794a93489f Merge pull request #492 from thedotmack/windows-fix
refactor(worker): Remove file-based locking and improve Windows stability
2025-12-29 18:41:00 -05:00
Alex Newman fdb3eb11c3 fix(hooks): reduce timeout values for worker service commands to improve responsiveness 2025-12-29 18:22:33 -05:00
Alex Newman a85d523371 remove bun lock 2025-12-29 18:05:34 -05:00
Alex Newman 3b28b779c8 Increase polling duration for worker readiness check from 5 seconds to 15 seconds, updating related comments for clarity. 2025-12-29 17:55:11 -05:00
Erik M Jacobs 3ea180c1ef refactor(worker): Remove file-based locking and improve Windows stability
This commit simplifies worker startup coordination and addresses Windows-specific issues:

**Lock Removal**:
- Removed entire file-based locking system (~100 lines)
- Replaced with health-check-first approach
- Port binding provides natural mutual exclusion - multiple spawns fail cleanly

**Windows Stability**:
- Removed all AbortSignal.timeout() calls to reduce Bun libuv assertion errors
- Added 500ms shutdown delays on Windows to prevent zombie ports
- Worker service has its own timeouts, so client-side timeouts are redundant

**Package.json Updates**:
- Updated worker scripts to use worker-service.cjs directly
- Removed references to deleted worker-cli.js and worker-wrapper.cjs

**Key Changes**:
- src/services/worker-service.ts: Lock removal, shutdown delays, simplified start logic
- src/hooks/*.ts: Removed AbortSignal.timeout from all HTTP requests
- src/shared/worker-utils.ts: Removed AbortSignal.timeout from health checks
- package.json: Updated worker:* scripts

Resolves startup hangs, reduces assertion errors, and prevents zombie port issues.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-29 16:44:40 -05:00
Alex Newman 0330b4d37e docs: update CHANGELOG.md for v8.2.8
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-29 16:25:28 -05:00
Alex Newman 6b1e91188b chore: bump version to 8.2.8
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-29 16:24:06 -05:00
Alex Newman 87169a78a4 Merge pull request #489 from thedotmack/bugfix/chroma-mcp-orphan-cleanup
fix: prevent orphaned chroma-mcp processes via signal handlers
2025-12-29 16:21:45 -05:00
Alex Newman 2bb07dd41a feat: register signal handlers in constructor for earlier cleanup protection
Addresses review feedback to register signal handlers earlier in the
lifecycle. Previously handlers were registered in start() after HTTP
server initialization, leaving a vulnerability window.

Benefits:
- Signal handlers now active immediately after WorkerService construction
- Protects against signals received during initialization (DB setup,
  HTTP server binding, background initialization)
- Prevents orphaned chroma-mcp processes even if killed during startup
- shutdown() method is defensive and safe to call at any stage

This closes the gap where a SIGTERM/SIGINT during initializeBackground()
could leave chroma-mcp subprocess running without cleanup.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-29 16:17:58 -05:00
Alex Newman 9eedbd4fbd fix: address PR review feedback - remove duplicate signal handlers and ensure PID cleanup
Addresses code review feedback from PR #489:

1. Moved PID file cleanup into shutdown() method to ensure it's always
   cleaned up regardless of how shutdown is triggered
2. Removed duplicate signal handlers in main() function that were
   redundant with the handlers in start() method

This eliminates the race condition where both sets of handlers could
trigger, and ensures consistent PID file cleanup behavior.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-29 16:10:19 -05:00
Alex Newman 5bd8181db9 build: rebuild worker service with signal handler fix
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-29 15:55:35 -05:00
Alex Newman 571926ecfa feat: add graceful shutdown handling to WorkerService 2025-12-29 15:54:22 -05:00
Alex Newman f7561bd4f8 update worker service 2025-12-29 00:37:08 -05:00
Alex Newman 7194443e42 Merge branch 'updates/docs' 2025-12-29 00:35:21 -05:00
Alex Newman d874ce6eb3 fix: escape less-than character in search-architecture.mdx to resolve MDX parsing error
Changed '<10ms' to 'Sub-10ms' to avoid MDX interpreting the < character as an HTML tag opening, which was causing deployment failure.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-29 00:34:14 -05:00
Alex Newman dce4d7a3f9 Merge pull request #480 from thedotmack/updates/docs
Documentation: Update to MCP Architecture and 3-Layer Workflow
2025-12-29 00:31:01 -05:00
Alex Newman d28e71298b docs: update CHANGELOG.md from releases
🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2025-12-29 00:26:54 -05:00
Alex Newman 00d0bc51e0 Refactor search documentation to implement a 3-layer workflow for memory retrieval; update tool names and usage examples for clarity and efficiency. Enhance troubleshooting section with new error handling and token management strategies. 2025-12-29 00:26:06 -05:00
69 changed files with 8905 additions and 1599 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
"plugins": [
{
"name": "claude-mem",
"version": "8.2.7",
"version": "8.5.0",
"source": "./plugin",
"description": "Persistent memory system for Claude Code - context compression across sessions"
}
+2 -1
View File
@@ -19,4 +19,5 @@ datasets/
src/ui/viewer.html
# Local MCP server config (for development only)
.mcp.json
.mcp.json
.cursor/
+431 -379
View File
File diff suppressed because it is too large Load Diff
+27 -22
View File
@@ -172,35 +172,40 @@ See [Architecture Overview](https://docs.claude-mem.ai/architecture/overview) fo
---
## mem-search Skill
## MCP Search Tools
Claude-Mem provides intelligent search through the mem-search skill that auto-invokes when you ask about past work:
Claude-Mem provides intelligent memory search through **4 MCP tools** following a token-efficient **3-layer workflow pattern**:
**The 3-Layer Workflow:**
1. **`search`** - Get compact index with IDs (~50-100 tokens/result)
2. **`timeline`** - Get chronological context around interesting results
3. **`get_observations`** - Fetch full details ONLY for filtered IDs (~500-1,000 tokens/result)
**How It Works:**
- Just ask naturally: *"What did we do last session?"* or *"Did we fix this bug before?"*
- Claude automatically invokes the mem-search skill to find relevant context
- Claude uses MCP tools to search your memory
- Start with `search` to get an index of results
- Use `timeline` to see what was happening around specific observations
- Use `get_observations` to fetch full details for relevant IDs
- **~10x token savings** by filtering before fetching details
**Available Search Operations:**
**Available MCP Tools:**
1. **Search Observations** - Full-text search across observations
2. **Search Sessions** - Full-text search across session summaries
3. **Search Prompts** - Search raw user requests
4. **By Concept** - Find by concept tags (discovery, problem-solution, pattern, etc.)
5. **By File** - Find observations referencing specific files
6. **By Type** - Find by type (decision, bugfix, feature, refactor, discovery, change)
7. **Recent Context** - Get recent session context for a project
8. **Timeline** - Get unified timeline of context around a specific point in time
9. **Timeline by Query** - Search for observations and get timeline context around best match
10. **API Help** - Get search API documentation
1. **`search`** - Search memory index with full-text queries, filters by type/date/project
2. **`timeline`** - Get chronological context around a specific observation or query
3. **`get_observations`** - Fetch full observation details by IDs (always batch multiple IDs)
4. **`__IMPORTANT`** - Workflow documentation (always visible to Claude)
**Example Natural Language Queries:**
**Example Usage:**
```
"What bugs did we fix last session?"
"How did we implement authentication?"
"What changes were made to worker-service.ts?"
"Show me recent work on this project"
"What was happening when we added the viewer UI?"
```typescript
// Step 1: Search for index
search(query="authentication bug", type="bugfix", limit=10)
// Step 2: Review index, identify relevant IDs (e.g., #123, #456)
// Step 3: Fetch full details
get_observations(ids=[123, 456])
```
See [Search Tools Guide](https://docs.claude-mem.ai/usage/search-tools) for detailed examples.
+3
View File
@@ -0,0 +1,3 @@
# Ignore backup files created by sed
*.bak
+173
View File
@@ -0,0 +1,173 @@
# Context Injection in Cursor Hooks
## The Solution: Auto-Updated Rules File
Context is automatically injected via Cursor's **Rules** system:
1. **Install**: `claude-mem cursor install` creates initial context file
2. **Stop hook**: `session-summary.sh` updates context after each session ends
3. **Cursor**: Automatically includes `.cursor/rules/claude-mem-context.mdc` in all chats
**Result**: Context appears at the start of every conversation, just like Claude Code!
## How It Works
### Installation Creates Initial Context
```bash
claude-mem cursor install
```
This:
1. Copies hook scripts to `.cursor/hooks/`
2. Creates `hooks.json` configuration
3. Fetches existing context from claude-mem and writes to `.cursor/rules/claude-mem-context.mdc`
### Context Updates at Three Points
Context is refreshed **three times** per session for maximum freshness:
1. **Before prompt submission** (`context-inject.sh`): Ensures you start with the latest context from previous sessions
2. **After summary completes** (worker auto-update): Immediately after the summary is saved, worker updates the context file
3. **After session ends** (`session-summary.sh`): Fallback update in case worker update was missed
### Before Prompt Hook Updates Context
When you submit a prompt, `context-inject.sh`:
```bash
# 1. Ensure worker is running
ensure_worker_running "$worker_port"
# 2. Fetch fresh context
context=$(curl -s ".../api/context/inject?project=...")
# 3. Write to rules file (used immediately by Cursor)
cat > .cursor/rules/claude-mem-context.mdc << EOF
---
alwaysApply: true
---
# Memory Context
${context}
EOF
```
### Stop Hook Updates Context
After each session ends, `session-summary.sh`:
```bash
# 1. Generate session summary
curl -X POST .../api/sessions/summarize
# 2. Fetch fresh context (includes new observations)
context=$(curl -s ".../api/context/inject?project=...")
# 3. Write to rules file for next session
cat > .cursor/rules/claude-mem-context.mdc << EOF
---
alwaysApply: true
---
# Memory Context
${context}
EOF
```
### The Rules File
Located at: `.cursor/rules/claude-mem-context.mdc`
```markdown
---
alwaysApply: true
description: "Claude-mem context from past sessions (auto-updated)"
---
# Memory Context from Past Sessions
[Your context from claude-mem appears here]
---
*Updated after last session.*
```
### Update Flow
Context updates at **three points**:
**Before each prompt:**
1. User submits a prompt
2. `beforeSubmitPrompt` hook runs `context-inject.sh`
3. Context file refreshed with latest observations from previous sessions
4. Cursor reads the updated rules file
**After summary completes (worker auto-update):**
1. Summary is saved to database
2. Worker checks if project is registered for Cursor
3. If yes, immediately writes updated context file with new observations
4. No hook involved - happens in the worker process
**After session ends (fallback):**
1. Agent completes (loop ends)
2. `stop` hook runs `session-summary.sh`
3. Context file updated (ensures nothing was missed)
4. Ready for next session
## Project Registry
When you run `claude-mem cursor install`, the project is registered in `~/.claude-mem/cursor-projects.json`. This allows the worker to automatically update your context file whenever a new summary is generated - even if it happens from Claude Code or another IDE working on the same project.
To see registered projects:
```bash
cat ~/.claude-mem/cursor-projects.json
```
## Comparison with Claude Code
| Feature | Claude Code | Cursor |
|---------|-------------|--------|
| Context injection | ✅ `additionalContext` in hook output | ✅ Auto-updated rules file |
| Injection timing | Immediate (same prompt) | Before prompt + after summary + after session |
| Persistence | Session only | File-based (persists across restarts) |
| Initial setup | Automatic | `claude-mem cursor install` creates initial context |
| MCP tool access | ✅ Full support | ✅ Full support |
| Web viewer | ✅ Available | ✅ Available |
## First Session Behavior
When you run `claude-mem cursor install`:
- If worker is running with existing memory → initial context is generated
- If no existing memory → placeholder file created
Context is then automatically refreshed:
- Before each prompt (ensures latest observations are included)
- After each session ends (captures new observations from the session)
## Additional Access Methods
### 1. MCP Tools
Configure claude-mem's MCP server in Cursor for search tools:
- `search(query, project, limit)`
- `timeline(anchor, depth_before, depth_after)`
- `get_observations(ids)`
### 2. Web Viewer
Access context manually at `http://localhost:37777`
### 3. Manual Request
Ask the agent: "Check claude-mem for any previous work on authentication"
## File Location
The context file is created at:
```
<workspace>/.cursor/rules/claude-mem-context.mdc
```
This is version-controlled by default. Add to `.gitignore` if you don't want to commit it:
```
.cursor/rules/claude-mem-context.mdc
```
+251
View File
@@ -0,0 +1,251 @@
# Claude-Mem ↔ Cursor Integration Architecture
## Overview
This integration connects claude-mem's persistent memory system to Cursor's hook system, enabling:
- Automatic capture of agent actions (MCP tools, shell commands, file edits)
- Context retrieval from past sessions
- Session summarization for future reference
## Architecture
```
┌─────────────┐
│ Cursor │
│ Agent │
└──────┬──────┘
│ Events (MCP, Shell, File Edits, Prompts)
┌─────────────────────────────────────┐
│ Cursor Hooks System │
│ ┌────────────────────────────────┐ │
│ │ beforeSubmitPrompt │ │
│ │ afterMCPExecution │ │
│ │ afterShellExecution │ │
│ │ afterFileEdit │ │
│ │ stop │ │
│ └────────────────────────────────┘ │
└──────┬──────────────────────────────┘
│ HTTP Requests
┌─────────────────────────────────────┐
│ Hook Scripts (Bash) │
│ ┌────────────────────────────────┐ │
│ │ session-init.sh │ │
│ │ context-inject.sh │ │
│ │ save-observation.sh │ │
│ │ save-file-edit.sh │ │
│ │ session-summary.sh │ │
│ └────────────────────────────────┘ │
└──────┬──────────────────────────────┘
│ HTTP API Calls
┌─────────────────────────────────────┐
│ Claude-Mem Worker Service │
│ (Port 37777) │
│ ┌────────────────────────────────┐ │
│ │ /api/sessions/init │ │
│ │ /api/sessions/observations │ │
│ │ /api/sessions/summarize │ │
│ │ /api/context/inject │ │
│ └────────────────────────────────┘ │
└──────┬──────────────────────────────┘
│ Database Operations
┌─────────────────────────────────────┐
│ SQLite Database │
│ + Chroma Vector DB │
└─────────────────────────────────────┘
```
## Event Flow
### 1. Prompt Submission Flow
```
User submits prompt
beforeSubmitPrompt hook fires
session-init.sh
├─ Extract conversation_id, project name
├─ POST /api/sessions/init
└─ Initialize session in claude-mem
context-inject.sh
├─ GET /api/context/inject?project=...
└─ Fetch relevant context (for future use)
Prompt proceeds to agent
```
### 2. Tool Execution Flow
```
Agent executes MCP tool or shell command
afterMCPExecution / afterShellExecution hook fires
save-observation.sh
├─ Extract tool_name, tool_input, tool_response
├─ Map to claude-mem observation format
├─ POST /api/sessions/observations
└─ Store observation in database
```
### 3. File Edit Flow
```
Agent edits file
afterFileEdit hook fires
save-file-edit.sh
├─ Extract file_path, edits
├─ Create "write_file" observation
├─ POST /api/sessions/observations
└─ Store file edit observation
```
### 4. Session End Flow
```
Agent loop ends
stop hook fires
session-summary.sh
├─ POST /api/sessions/summarize
└─ Generate session summary for future retrieval
```
## Data Mapping
### Session ID Mapping
| Cursor Field | Claude-Mem Field | Notes |
|-------------|------------------|-------|
| `conversation_id` | `contentSessionId` | Stable across turns, used as primary session identifier |
| `generation_id` | (fallback) | Used if conversation_id unavailable |
### Tool Mapping
| Cursor Event | Claude-Mem Tool Name | Input Format |
|-------------|---------------------|--------------|
| `afterMCPExecution` | `tool_name` from event | `tool_input` as JSON |
| `afterShellExecution` | `"Bash"` | `{command: "..."}` |
| `afterFileEdit` | `"write_file"` | `{file_path: "...", edits: [...]}` |
### Project Mapping
| Source | Target | Notes |
|--------|--------|-------|
| `workspace_roots[0]` | Project name | Basename of workspace root directory |
## API Endpoints Used
### Session Management
- `POST /api/sessions/init` - Initialize new session
- `POST /api/sessions/summarize` - Generate session summary
### Observation Storage
- `POST /api/sessions/observations` - Store tool usage observation
### Context Retrieval
- `GET /api/context/inject?project=...` - Get relevant context for injection
### Health Checks
- `GET /api/readiness` - Check if worker is ready
## Configuration
### Worker Settings
Located in `~/.claude-mem/settings.json`:
- `CLAUDE_MEM_WORKER_PORT` (default: 37777)
- `CLAUDE_MEM_WORKER_HOST` (default: 127.0.0.1)
### Hook Settings
Located in `hooks.json`:
- Hook event names
- Script paths (relative or absolute)
## Error Handling
### Worker Unavailable
- Hooks poll `/api/readiness` with 30 retries (6 seconds)
- If worker unavailable, hooks fail gracefully (exit 0)
- Observations are fire-and-forget (curl errors ignored)
### Missing Data
- Empty `conversation_id` → use `generation_id`
- Empty `workspace_root` → use `pwd`
- Missing tool data → skip observation
### Network Errors
- All HTTP requests use `curl -s` (silent)
- Errors redirected to `/dev/null`
- Hooks always exit 0 to avoid blocking Cursor
## Limitations
1. **Context Injection**: Cursor's `beforeSubmitPrompt` doesn't support prompt modification. Context must be retrieved via:
- MCP tools (claude-mem provides search tools)
- Manual retrieval from web viewer
- Future: Agent SDK integration
2. **Transcript Access**: Cursor hooks don't provide transcript paths, limiting summary quality compared to Claude Code integration.
3. **Session Model**: Uses `conversation_id` which may not perfectly match Claude Code's session model.
4. **Tab Hooks**: Currently only supports Agent hooks. Tab (inline completion) hooks could be added separately.
## Future Enhancements
- [ ] Enhanced context injection via MCP tools
- [ ] Support for `beforeTabFileRead` and `afterTabFileEdit` hooks
- [ ] Better error reporting and logging
- [ ] Integration with Cursor's agent SDK
- [ ] Support for blocking/approval workflows
- [ ] Real-time context injection via agent messages
## Testing
### Manual Testing
1. **Test session initialization**:
```bash
echo '{"conversation_id":"test-123","workspace_roots":["/tmp/test"],"prompt":"test"}' | \
~/.cursor/hooks/session-init.sh
```
2. **Test observation capture**:
```bash
echo '{"conversation_id":"test-123","hook_event_name":"afterMCPExecution","tool_name":"test","tool_input":{},"result_json":{}}' | \
~/.cursor/hooks/save-observation.sh
```
3. **Test context retrieval**:
```bash
curl "http://127.0.0.1:37777/api/context/inject?project=test"
```
### Integration Testing
1. Enable hooks in Cursor
2. Submit a prompt
3. Execute some tools
4. Check web viewer: `http://localhost:37777`
5. Verify observations appear in database
## Troubleshooting
See [README.md](README.md#troubleshooting) for detailed troubleshooting steps.
+168
View File
@@ -0,0 +1,168 @@
# Feature Parity: Claude-Mem Hooks vs Cursor Hooks
This document compares claude-mem's Claude Code hooks with the Cursor hooks implementation to ensure feature parity.
## Hook Mapping
| Claude Code Hook | Cursor Hook | Status | Notes |
|-----------------|-------------|--------|-------|
| `SessionStart``context-hook.js` | `beforeSubmitPrompt``context-inject.sh` | ✅ Partial | Context fetched but not injectable in Cursor |
| `SessionStart``user-message-hook.js` | (Optional) `user-message.sh` | ⚠️ Optional | No SessionStart equivalent; can run on beforeSubmitPrompt |
| `UserPromptSubmit``new-hook.js` | `beforeSubmitPrompt``session-init.sh` | ✅ Complete | Session init, privacy checks, slash stripping |
| `PostToolUse``save-hook.js` | `afterMCPExecution` + `afterShellExecution``save-observation.sh` | ✅ Complete | Tool observation capture |
| `PostToolUse` → (file edits) | `afterFileEdit``save-file-edit.sh` | ✅ Complete | File edit observation capture |
| `Stop``summary-hook.js` | `stop``session-summary.sh` | ⚠️ Partial | Summary generation (no transcript access) |
## Feature Comparison
### 1. Session Initialization (`new-hook.js``session-init.sh`)
| Feature | Claude Code | Cursor | Status |
|---------|-------------|--------|--------|
| Worker health check | ✅ 75 retries (15s) | ✅ 75 retries (15s) | ✅ Match |
| Session init API call | ✅ `/api/sessions/init` | ✅ `/api/sessions/init` | ✅ Match |
| Privacy check handling | ✅ Checks `skipped` + `reason` | ✅ Checks `skipped` + `reason` | ✅ Match |
| Slash stripping | ✅ Strips leading `/` | ✅ Strips leading `/` | ✅ Match |
| SDK agent init | ✅ `/sessions/{id}/init` | ❌ Not needed | ✅ N/A (Cursor-specific) |
**Status**: ✅ Complete parity (SDK agent init not applicable to Cursor)
### 2. Context Injection (`context-hook.js``context-inject.sh`)
| Feature | Claude Code | Cursor | Status |
|---------|-------------|--------|--------|
| Worker health check | ✅ 75 retries | ✅ 75 retries | ✅ Match |
| Context fetch | ✅ `/api/context/inject` | ✅ `/api/context/inject` | ✅ Match |
| Output format | ✅ JSON with `hookSpecificOutput` | ✅ Write to `.cursor/rules/` file | ✅ Alternative |
| Project name extraction | ✅ `getProjectName(cwd)` | ✅ `basename(workspace_root)` | ✅ Match |
| Auto-refresh | ✅ Each session start | ✅ Each prompt submission | ✅ Enhanced |
**Status**: ✅ Complete parity via auto-updated rules file
**How it works**:
- Hook writes context to `.cursor/rules/claude-mem-context.mdc`
- File has `alwaysApply: true` frontmatter
- Cursor auto-includes this rule in all chat sessions
- Context refreshes on every prompt submission
### 3. User Message Display (`user-message-hook.js``user-message.sh`)
| Feature | Claude Code | Cursor | Status |
|---------|-------------|--------|--------|
| Context fetch with colors | ✅ `/api/context/inject?colors=true` | ✅ `/api/context/inject?colors=true` | ✅ Match |
| Output channel | ✅ stderr | ✅ stderr | ✅ Match |
| Display format | ✅ Formatted with emojis | ✅ Formatted with emojis | ✅ Match |
| Hook trigger | ✅ SessionStart | ⚠️ Optional (no SessionStart) | ⚠️ Cursor limitation |
**Status**: ⚠️ Optional (no SessionStart equivalent in Cursor)
**Note**: Can be added to `beforeSubmitPrompt` if desired, but may be verbose.
### 4. Observation Capture (`save-hook.js``save-observation.sh`)
| Feature | Claude Code | Cursor | Status |
|---------|-------------|--------|--------|
| Worker health check | ✅ 75 retries | ✅ 75 retries | ✅ Match |
| Tool name extraction | ✅ From `tool_name` | ✅ From `tool_name` or "Bash" | ✅ Match |
| Tool input capture | ✅ Full JSON | ✅ Full JSON | ✅ Match |
| Tool response capture | ✅ Full JSON | ✅ Full JSON or output | ✅ Match |
| Privacy tag stripping | ✅ Worker handles | ✅ Worker handles | ✅ Match |
| Error handling | ✅ Fire-and-forget | ✅ Fire-and-forget | ✅ Match |
| Shell command mapping | ✅ N/A (separate hook) | ✅ Maps to "Bash" tool | ✅ Enhanced |
**Status**: ✅ Complete parity (enhanced with shell command support)
### 5. File Edit Capture (N/A ↔ `save-file-edit.sh`)
| Feature | Claude Code | Cursor | Status |
|---------|-------------|--------|--------|
| File path extraction | N/A | ✅ From `file_path` | ✅ New |
| Edit details | N/A | ✅ From `edits` array | ✅ New |
| Tool name | N/A | ✅ "write_file" | ✅ New |
| Edit summary | N/A | ✅ Generated from edits | ✅ New |
**Status**: ✅ New feature (Cursor-specific, not in Claude Code)
### 6. Session Summary (`summary-hook.js``session-summary.sh`)
| Feature | Claude Code | Cursor | Status |
|---------|-------------|--------|--------|
| Worker health check | ✅ 75 retries | ✅ 75 retries | ✅ Match |
| Transcript parsing | ✅ Extracts last messages | ❌ No transcript access | ⚠️ Cursor limitation |
| Summary API call | ✅ `/api/sessions/summarize` | ✅ `/api/sessions/summarize` | ✅ Match |
| Last message extraction | ✅ From transcript | ❌ Empty strings | ⚠️ Cursor limitation |
| Error handling | ✅ Fire-and-forget | ✅ Fire-and-forget | ✅ Match |
**Status**: ⚠️ Partial parity (no transcript access in Cursor)
**Note**: Summary generation still works but may be less accurate without last messages. Worker generates summary from observations stored during session.
## Implementation Details
### Worker Health Checks
- **Claude Code**: 75 retries × 200ms = 15 seconds
- **Cursor**: 75 retries × 200ms = 15 seconds
- **Status**: ✅ Match
### Error Handling
- **Claude Code**: Fire-and-forget with logging
- **Cursor**: Fire-and-forget with graceful exit (exit 0)
- **Status**: ✅ Match (adapted for Cursor's hook system)
### Privacy Handling
- **Claude Code**: Worker performs privacy checks, hooks respect `skipped` flag
- **Cursor**: Worker performs privacy checks, hooks respect `skipped` flag
- **Status**: ✅ Match
### Tag Stripping
- **Claude Code**: Worker handles `<private>` and `<claude-mem-context>` tags
- **Cursor**: Worker handles tags (hooks don't need to strip)
- **Status**: ✅ Match
## Missing Features (Cursor Limitations)
1. ~~**Direct Context Injection**~~: **SOLVED** via auto-updated rules file
- Hook writes context to `.cursor/rules/claude-mem-context.mdc`
- Cursor auto-includes rules with `alwaysApply: true`
- Context refreshes on every prompt
2. **Transcript Access**: Cursor hooks don't provide transcript paths
- **Impact**: Summary generation less accurate
- **Workaround**: Worker generates from observations
3. **SessionStart Hook**: Cursor doesn't have session start event
- **Impact**: User message display must be optional
- **Workaround**: Can run on `beforeSubmitPrompt` if desired
4. **SDK Agent Session**: Cursor doesn't use SDK agent pattern
- **Impact**: No `/sessions/{id}/init` call needed
- **Status**: ✅ Not applicable (Cursor-specific)
## Enhancements (Cursor-Specific)
1. **Shell Command Capture**: Maps shell commands to "Bash" tool observations
- **Status**: ✅ Enhanced beyond Claude Code
2. **File Edit Capture**: Dedicated hook for file edits
- **Status**: ✅ New feature
3. **MCP Tool Capture**: Captures MCP tool usage separately
- **Status**: ✅ Enhanced beyond Claude Code
## Summary
| Category | Status |
|----------|--------|
| Core Functionality | ✅ Complete parity |
| Session Management | ✅ Complete parity |
| Observation Capture | ✅ Complete parity (enhanced) |
| Context Injection | ✅ Complete parity (via rules file) |
| Summary Generation | ⚠️ Partial (no transcript) |
| User Experience | ⚠️ Partial (no SessionStart) |
**Overall**: The Cursor hooks implementation achieves **full functional parity** with claude-mem's Claude Code hooks:
- ✅ Session initialization
- ✅ Context injection (via auto-updated `.cursor/rules/` file)
- ✅ Observation capture (MCP tools, shell commands, file edits)
- ⚠️ Summary generation (works, but no transcript access)
+112
View File
@@ -0,0 +1,112 @@
# Quick Start: Claude-Mem + Cursor Integration
> **Give your Cursor AI persistent memory in under 5 minutes**
## What This Does
Connects claude-mem to Cursor so that:
- **Agent actions** (MCP tools, shell commands, file edits) are automatically saved
- **Context from past sessions** is automatically injected via `.cursor/rules/`
- **Sessions are summarized** for future reference
Your AI stops forgetting. It remembers the patterns, decisions, and context from previous sessions.
## Don't Have Claude Code?
If you're using Cursor without Claude Code, see [STANDALONE-SETUP.md](STANDALONE-SETUP.md) for setup with free-tier providers like Gemini or OpenRouter.
---
## Installation (1 minute)
```bash
# Install globally for all projects (recommended)
claude-mem cursor install user
# Or install for current project only
claude-mem cursor install
# Check installation status
claude-mem cursor status
```
## Configure Provider (Required for Standalone)
If you don't have Claude Code, configure a provider for AI summarization:
```bash
# Option A: Gemini (free tier available - recommended)
claude-mem settings set CLAUDE_MEM_PROVIDER gemini
claude-mem settings set CLAUDE_MEM_GEMINI_API_KEY your-api-key
# Option B: OpenRouter (free models available)
claude-mem settings set CLAUDE_MEM_PROVIDER openrouter
claude-mem settings set CLAUDE_MEM_OPENROUTER_API_KEY your-api-key
```
**Get free API keys**:
- Gemini: https://aistudio.google.com/apikey
- OpenRouter: https://openrouter.ai/keys
## Start Worker
```bash
claude-mem start
# Verify it's running
claude-mem status
```
## Restart Cursor
Restart Cursor to load the hooks.
## Verify It's Working
1. Open Cursor Settings → Hooks tab
2. You should see the hooks listed
3. Submit a prompt in Cursor
4. Check the web viewer: http://localhost:37777
5. You should see observations appearing
## What Gets Captured
- **MCP Tool Usage**: All MCP tool executions
- **Shell Commands**: All terminal commands
- **File Edits**: All file modifications
- **Sessions**: Each conversation is tracked
## Accessing Memory
### Via Web Viewer
- Open http://localhost:37777
- Browse sessions, observations, and summaries
- Search your project history
### Via MCP Tools (if enabled)
- claude-mem provides search tools via MCP
- Use `search`, `timeline`, and `get_observations` tools
## Troubleshooting
**Hooks not running?**
- Check Cursor Settings → Hooks tab for errors
- Verify scripts are executable: `chmod +x ~/.cursor/hooks/*.sh`
- Check Hooks output channel in Cursor
**Worker not responding?**
- Check if worker is running: `curl http://127.0.0.1:37777/api/readiness`
- Check logs: `tail -f ~/.claude-mem/logs/worker-$(date +%Y-%m-%d).log`
- Restart worker: `bun run worker:restart`
**Observations not saving?**
- Check worker logs for errors
- Verify session was initialized in web viewer
- Test API directly: `curl -X POST http://127.0.0.1:37777/api/sessions/observations ...`
## Next Steps
- Read [README.md](README.md) for detailed documentation
- Read [INTEGRATION.md](INTEGRATION.md) for architecture details
- Visit [claude-mem docs](https://docs.claude-mem.ai) for full feature set
+246
View File
@@ -0,0 +1,246 @@
# Claude-Mem Cursor Hooks Integration
> **Persistent AI Memory for Cursor - Free Options Available**
Give your Cursor AI persistent memory across sessions. Your agent remembers what it worked on, the decisions it made, and the patterns in your codebase - automatically.
### Why Claude-Mem?
- **Remember context across sessions**: No more re-explaining your codebase every time
- **Automatic capture**: MCP tools, shell commands, and file edits are logged without effort
- **Free tier options**: Works with Gemini (1500 free req/day) or OpenRouter (free models available)
- **Works with or without Claude Code**: Full functionality either way
### Quick Install (5 minutes)
```bash
# Clone and build
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem && bun install && bun run build
# Interactive setup (configures provider + installs hooks)
bun run cursor:setup
```
---
## Quick Start for Cursor Users
**Using Claude Code?** Skip to [Installation](#installation) - everything works automatically.
**Cursor-only (no Claude Code)?** See [STANDALONE-SETUP.md](STANDALONE-SETUP.md) for free-tier options using Gemini or OpenRouter.
---
## Overview
The hooks bridge Cursor's hook system to claude-mem's worker API, allowing:
- **Session Management**: Initialize sessions and generate summaries
- **Observation Capture**: Record MCP tool usage, shell commands, and file edits
- **Worker Readiness**: Ensure the worker is running before prompt submission
## Context Injection
Context is automatically injected via Cursor's **Rules** system:
1. **Install**: `claude-mem cursor install` generates initial context
2. **Stop hook**: Updates context in `.cursor/rules/claude-mem-context.mdc` after each session
3. **Cursor**: Automatically includes this rule in ALL chat sessions
**The context updates after each session ends**, so the next session sees fresh context.
### Additional Access Methods
- **MCP Tools**: Configure claude-mem's MCP server for `search`, `timeline`, `get_observations` tools
- **Web Viewer**: Access context at `http://localhost:37777`
- **Manual Request**: Ask the agent to search memory
See [CONTEXT-INJECTION.md](CONTEXT-INJECTION.md) for details.
## Installation
### Quick Install (Recommended)
```bash
# Install globally for all projects (recommended)
claude-mem cursor install user
# Or install for current project only
claude-mem cursor install
```
### Manual Installation
<details>
<summary>Click to expand manual installation steps</summary>
**User-level** (recommended - applies to all projects):
```bash
# Copy hooks.json to your home directory
cp cursor-hooks/hooks.json ~/.cursor/hooks.json
# Copy hook scripts
mkdir -p ~/.cursor/hooks
cp cursor-hooks/*.sh ~/.cursor/hooks/
chmod +x ~/.cursor/hooks/*.sh
```
**Project-level** (for per-project hooks):
```bash
# Copy hooks.json to your project
mkdir -p .cursor
cp cursor-hooks/hooks.json .cursor/hooks.json
# Copy hook scripts to your project
mkdir -p .cursor/hooks
cp cursor-hooks/*.sh .cursor/hooks/
chmod +x .cursor/hooks/*.sh
```
</details>
### After Installation
1. **Start the worker**:
```bash
claude-mem start
```
2. **Restart Cursor** to load the hooks
3. **Verify installation**:
```bash
claude-mem cursor status
```
## Hook Mappings
| Cursor Hook | Script | Purpose |
|-------------|--------|---------|
| `beforeSubmitPrompt` | `session-init.sh` | Initialize claude-mem session |
| `beforeSubmitPrompt` | `context-inject.sh` | Ensure worker is running |
| `afterMCPExecution` | `save-observation.sh` | Capture MCP tool usage |
| `afterShellExecution` | `save-observation.sh` | Capture shell command execution |
| `afterFileEdit` | `save-file-edit.sh` | Capture file edits |
| `stop` | `session-summary.sh` | Generate summary + update context file |
## How It Works
### Session Initialization (`session-init.sh`)
- Called before each prompt submission
- Initializes a new session in claude-mem using `conversation_id` as the session ID
- Extracts project name from workspace root
- Outputs `{"continue": true}` to allow prompt submission
### Context Hook (`context-inject.sh`)
- Ensures claude-mem worker is running before session
- Outputs `{"continue": true}` to allow prompt submission
- Note: Context file is updated by `session-summary.sh` (stop hook), not here
### Observation Capture (`save-observation.sh`)
- Captures MCP tool executions and shell commands
- Maps them to claude-mem's observation format
- Sends to `/api/sessions/observations` endpoint (fire-and-forget)
### File Edit Capture (`save-file-edit.sh`)
- Captures file edits made by the agent
- Treats edits as "write_file" tool usage
- Includes edit summaries in observations
### Session Summary (`session-summary.sh`)
- Called when agent loop ends (stop hook)
- Requests summary generation from claude-mem
- **Updates context file** in `.cursor/rules/claude-mem-context.mdc` for next session
## Configuration
The hooks read configuration from `~/.claude-mem/settings.json`:
- `CLAUDE_MEM_WORKER_PORT`: Worker port (default: 37777)
- `CLAUDE_MEM_WORKER_HOST`: Worker host (default: 127.0.0.1)
## Dependencies
The hook scripts require:
- `jq` - JSON processing
- `curl` - HTTP requests
- `bash` - Shell interpreter
Install on macOS: `brew install jq curl`
Install on Ubuntu: `apt-get install jq curl`
## Troubleshooting
### Hooks not executing
1. Check hooks are in the correct location:
```bash
ls .cursor/hooks.json # Project-level
ls ~/.cursor/hooks.json # User-level
```
2. Verify scripts are executable:
```bash
chmod +x ~/.cursor/hooks/*.sh
```
3. Check Cursor Settings → Hooks tab for configuration status
4. Check Hooks output channel in Cursor for error messages
### Worker not responding
1. Verify worker is running:
```bash
curl http://127.0.0.1:37777/api/readiness
```
2. Check worker logs:
```bash
tail -f ~/.claude-mem/logs/worker-$(date +%Y-%m-%d).log
```
3. Restart worker:
```bash
claude-mem restart
```
### Observations not being saved
1. Monitor worker logs for incoming requests
2. Verify session was initialized via web viewer at `http://localhost:37777`
3. Test observation endpoint directly:
```bash
curl -X POST http://127.0.0.1:37777/api/sessions/observations \
-H "Content-Type: application/json" \
-d '{"contentSessionId":"test","tool_name":"test","tool_input":{},"tool_response":{},"cwd":"/tmp"}'
```
## Comparison with Claude Code Integration
| Feature | Claude Code | Cursor |
|---------|-------------|--------|
| Session Initialization | ✅ `SessionStart` hook | ✅ `beforeSubmitPrompt` hook |
| Context Injection | ✅ `additionalContext` field | ✅ Auto-updated `.cursor/rules/` file |
| Observation Capture | ✅ `PostToolUse` hook | ✅ `afterMCPExecution`, `afterShellExecution`, `afterFileEdit` |
| Session Summary | ✅ `Stop` hook with transcript | ⚠️ `stop` hook (no transcript) |
| MCP Search Tools | ✅ Full support | ✅ Full support (if MCP configured) |
## Files
- `hooks.json` - Hook configuration
- `common.sh` - Shared utility functions
- `session-init.sh` - Session initialization
- `context-inject.sh` - Context/worker readiness hook
- `save-observation.sh` - MCP and shell observation capture
- `save-file-edit.sh` - File edit observation capture
- `session-summary.sh` - Summary generation
- `cursorrules-template.md` - Template for `.cursorrules` file
## See Also
- [Claude-Mem Documentation](https://docs.claude-mem.ai)
- [Cursor Hooks Reference](../docs/context/cursor-hooks-reference.md)
- [Claude-Mem Architecture](https://docs.claude-mem.ai/architecture/overview)
+327
View File
@@ -0,0 +1,327 @@
# Comprehensive Review: Cursor Hooks Integration
## Overview
This document provides a thorough review of the Cursor hooks integration, covering all aspects from implementation details to edge cases and potential issues.
## Architecture Review
### ✅ Strengths
1. **Modular Design**: Common utilities extracted to `common.sh` for reusability
2. **Error Handling**: Graceful degradation - hooks never block Cursor even on failures
3. **Parity with Claude Code**: Matches claude-mem's hook behavior where possible
4. **Fire-and-Forget**: Observations sent asynchronously, don't block agent execution
### ⚠️ Limitations (Platform-Specific)
1. **No Windows Support**: Bash scripts require Unix-like environment
- **Mitigation**: Could add PowerShell equivalents or use Node.js/Python wrappers
2. **Dependency on jq/curl**: Requires external tools
- **Mitigation**: Dependency checks added, graceful fallback
## Script-by-Script Review
### 1. `common.sh` - Utility Functions
**Purpose**: Shared utilities for all hook scripts
**Functions**:
- ✅ `check_dependencies()` - Validates jq and curl exist
- ✅ `read_json_input()` - Safely reads and validates JSON from stdin
- ✅ `get_worker_port()` - Reads port from settings with validation
- ✅ `ensure_worker_running()` - Health checks with retries
- ✅ `url_encode()` - URL encoding for special characters
- ✅ `get_project_name()` - Extracts project name with edge case handling
- ✅ `json_get()` - Safe JSON field extraction with array support
- ✅ `is_empty()` - Null/empty string detection
**Edge Cases Handled**:
- ✅ Empty stdin
- ✅ Malformed JSON
- ✅ Missing settings file
- ✅ Invalid port numbers
- ✅ Windows drive roots (C:\, etc.)
- ✅ Empty workspace roots
- ✅ Array field access (`workspace_roots[0]`)
**Potential Issues**:
- ⚠️ `url_encode()` uses jq - if jq fails, encoding fails silently
- ✅ **Fixed**: Falls back to original string if encoding fails
### 2. `session-init.sh` - Session Initialization
**Purpose**: Initialize claude-mem session when prompt is submitted
**Flow**:
1. Read and validate JSON input
2. Extract session_id, project, prompt
3. Ensure worker is running
4. Strip leading slash from prompt (parity with new-hook.ts)
5. Call `/api/sessions/init`
6. Handle privacy checks
**Edge Cases Handled**:
- ✅ Empty conversation_id → fallback to generation_id
- ✅ Empty workspace_root → fallback to pwd
- ✅ Empty prompt → still initializes session
- ✅ Worker unavailable → graceful exit
- ✅ Privacy-skipped sessions → silent exit
- ✅ Invalid JSON → graceful exit
**Potential Issues**:
- ✅ **Fixed**: String slicing now checks for empty strings
- ✅ **Fixed**: All jq operations have error handling
- ✅ **Fixed**: Worker health check with proper retries
**Parity with Claude Code**:
- ✅ Session initialization
- ✅ Privacy check handling
- ✅ Slash stripping
- ❌ SDK agent init (not applicable to Cursor)
### 3. `save-observation.sh` - Observation Capture
**Purpose**: Capture MCP tool usage and shell commands
**Flow**:
1. Read and validate JSON input
2. Determine hook type (MCP vs Shell)
3. Extract tool data
4. Validate JSON structures
5. Ensure worker is running
6. Send observation (fire-and-forget)
**Edge Cases Handled**:
- ✅ Empty tool_name → exit gracefully
- ✅ Invalid tool_input/tool_response → default to {}
- ✅ Malformed JSON in tool data → validated and sanitized
- ✅ Empty session_id → exit gracefully
- ✅ Worker unavailable → exit gracefully
**Potential Issues**:
- ✅ **Fixed**: JSON validation for tool_input and tool_response
- ✅ **Fixed**: Proper handling of empty/null values
- ✅ **Fixed**: Error handling for all jq operations
**Parity with Claude Code**:
- ✅ Tool observation capture
- ✅ Privacy tag stripping (handled by worker)
- ✅ Fire-and-forget pattern
- ✅ Enhanced: Shell command capture (not in Claude Code)
### 4. `save-file-edit.sh` - File Edit Capture
**Purpose**: Capture file edits as observations
**Flow**:
1. Read and validate JSON input
2. Extract file_path and edits array
3. Validate edits array
4. Create edit summary
5. Ensure worker is running
6. Send observation (fire-and-forget)
**Edge Cases Handled**:
- ✅ Empty file_path → exit gracefully
- ✅ Empty edits array → exit gracefully
- ✅ Invalid edits JSON → default to []
- ✅ Malformed edit objects → summary generation handles gracefully
- ✅ Empty session_id → exit gracefully
**Potential Issues**:
- ✅ **Fixed**: Edit summary generation with error handling
- ✅ **Fixed**: Array validation before processing
- ✅ **Fixed**: Safe string slicing in summary generation
**Parity with Claude Code**:
- ✅ File edit capture (new feature for Cursor)
- ✅ Observation format matches claude-mem structure
### 5. `session-summary.sh` - Summary Generation
**Purpose**: Generate session summary when agent loop ends
**Flow**:
1. Read and validate JSON input
2. Extract session_id
3. Ensure worker is running
4. Send summarize request with empty messages (no transcript access)
5. Output empty JSON (required by Cursor)
**Edge Cases Handled**:
- ✅ Empty session_id → exit gracefully
- ✅ Worker unavailable → exit gracefully
- ✅ Missing transcript → empty messages (worker handles gracefully)
**Potential Issues**:
- ✅ **Fixed**: Proper JSON output for Cursor stop hook
- ✅ **Fixed**: Worker handles empty messages (verified in codebase)
**Parity with Claude Code**:
- ⚠️ Partial: No transcript access, so no last_user_message/last_assistant_message
- ✅ Summary generation still works (based on observations)
### 6. `context-inject.sh` - Context Injection via Rules File
**Purpose**: Fetch context and write to `.cursor/rules/` for auto-injection
**How It Works**:
1. Fetches context from claude-mem worker
2. Writes to `.cursor/rules/claude-mem-context.mdc` with `alwaysApply: true`
3. Cursor auto-includes this rule in all chat sessions
4. Context refreshes on every prompt submission
**Flow**:
1. Read and validate JSON input
2. Extract workspace root
3. Get project name
4. Ensure worker is running
5. Fetch context from `/api/context/inject`
6. Write context to `.cursor/rules/claude-mem-context.mdc`
7. Output `{"continue": true}`
**Edge Cases Handled**:
- ✅ Empty workspace_root → fallback to pwd
- ✅ Worker unavailable → allow prompt to continue
- ✅ Context fetch failure → allow prompt to continue (no file written)
- ✅ Special characters in project name → URL encoded
- ✅ Missing `.cursor/rules/` directory → created automatically
**Parity with Claude Code**:
- ✅ Context injection achieved via rules file workaround
- ✅ Worker readiness check matches Claude Code
- ✅ Context available immediately in next prompt
## Error Handling Review
### ✅ Comprehensive Error Handling
1. **Input Validation**:
- ✅ Empty stdin → default to `{}`
- ✅ Malformed JSON → validated and sanitized
- ✅ Missing fields → safe fallbacks
2. **Dependency Checks**:
- ✅ jq and curl existence checked
- ✅ Non-blocking (warns but continues)
3. **Network Errors**:
- ✅ Worker unavailable → graceful exit
- ✅ HTTP failures → fire-and-forget (don't block)
- ✅ Timeout handling → 15 second retries
4. **Data Validation**:
- ✅ Port number validation (1-65535)
- ✅ JSON structure validation
- ✅ Empty/null value handling
## Security Review
### ✅ Security Considerations
1. **Input Sanitization**:
- ✅ JSON validation prevents injection
- ✅ URL encoding for special characters
- ✅ Worker handles privacy tag stripping
2. **Error Information**:
- ✅ Errors don't expose sensitive data
- ✅ Fire-and-forget prevents information leakage
3. **Dependency Security**:
- ✅ Uses standard tools (jq, curl)
- ✅ No custom code execution
## Performance Review
### ✅ Performance Optimizations
1. **Non-Blocking**:
- ✅ All hooks exit quickly (don't block Cursor)
- ✅ Observations sent asynchronously
2. **Efficient Health Checks**:
- ✅ 200ms polling interval
- ✅ 15 second maximum wait
- ✅ Early exit on success
3. **Resource Usage**:
- ✅ Minimal memory footprint
- ✅ No long-running processes
- ✅ Fire-and-forget HTTP requests
## Testing Recommendations
### Unit Tests Needed
1. **common.sh functions**:
- [ ] Test `json_get()` with various field types
- [ ] Test `get_project_name()` with edge cases
- [ ] Test `url_encode()` with special characters
- [ ] Test `ensure_worker_running()` with various states
2. **Hook scripts**:
- [ ] Test with empty input
- [ ] Test with malformed JSON
- [ ] Test with missing fields
- [ ] Test with worker unavailable
- [ ] Test with invalid port numbers
### Integration Tests Needed
1. **End-to-end flow**:
- [ ] Session initialization → observation capture → summary
- [ ] Multiple concurrent hooks
- [ ] Worker restart scenarios
2. **Edge cases**:
- [ ] Very long prompts/commands
- [ ] Special characters in paths
- [ ] Unicode in tool inputs
- [ ] Large file edits
## Known Limitations
1. **Cursor Hook System**:
- ✅ Context injection solved via `.cursor/rules/` file
- ❌ No transcript access for summary generation
- ❌ No SessionStart equivalent
2. **Platform Support**:
- ⚠️ Bash scripts (Unix-like only)
- ⚠️ Requires jq and curl
3. **Context Injection**:
- ✅ Solved via auto-updated `.cursor/rules/claude-mem-context.mdc`
- ✅ Context also available via MCP tools
- ✅ Context also available via web viewer
## Recommendations
### Immediate Improvements
1. ✅ **DONE**: Comprehensive error handling
2. ✅ **DONE**: Input validation
3. ✅ **DONE**: Dependency checks
4. ✅ **DONE**: URL encoding
### Future Enhancements
1. **Logging**: Add optional debug logging to help troubleshoot
2. **Metrics**: Track hook execution times and success rates
3. **Windows Support**: PowerShell or Node.js equivalents
4. **Testing**: Automated test suite
5. **Documentation**: More examples and troubleshooting guides
## Conclusion
The Cursor hooks integration is **production-ready** with:
- ✅ Comprehensive error handling
- ✅ Input validation and sanitization
- ✅ Graceful degradation
- ✅ Feature parity with Claude Code hooks (where applicable)
- ✅ Enhanced features (shell/file edit capture)
The implementation handles edge cases well and follows best practices for reliability and maintainability.
+293
View File
@@ -0,0 +1,293 @@
# Claude-Mem for Cursor (No Claude Code Required)
> **Persistent AI Memory for Cursor - Zero Cost to Start**
## Overview
Use claude-mem's persistent memory in Cursor without a Claude Code subscription. Choose between free-tier providers (Gemini, OpenRouter) or paid options.
**What You Get**:
- **Persistent memory** that survives across sessions - your AI remembers what it worked on
- **Automatic capture** of MCP tools, shell commands, and file edits
- **Context injection** via `.cursor/rules/` - relevant history included in every chat
- **Web viewer** at http://localhost:37777 - browse and search your project history
**Why This Matters**: Every Cursor session starts fresh. Claude-mem bridges that gap - your AI agent builds cumulative knowledge about your codebase, decisions, and patterns over time.
## Prerequisites
### macOS / Linux
- Cursor IDE
- [Bun](https://bun.sh) (`curl -fsSL https://bun.sh/install | bash`)
- Git
- `jq` and `curl`:
- **macOS**: `brew install jq curl`
- **Linux**: `apt install jq curl`
### Windows
- Cursor IDE
- [Bun](https://bun.sh) (PowerShell: `powershell -c "irm bun.sh/install.ps1 | iex"`)
- Git
- PowerShell 5.1+ (included with Windows 10/11)
## Step 1: Clone Claude-Mem
```bash
# Clone the repository
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem
# Install dependencies
bun install
# Build the project
bun run build
```
## Step 2: Configure Provider (Choose One)
Since you don't have Claude Code, you need to configure an AI provider for claude-mem's summarization engine.
### Option A: Gemini (Recommended - Free Tier)
Gemini offers 1500 free requests per day, plenty for typical usage.
```bash
# Create settings directory
mkdir -p ~/.claude-mem
# Create settings file
cat > ~/.claude-mem/settings.json << 'EOF'
{
"CLAUDE_MEM_PROVIDER": "gemini",
"CLAUDE_MEM_GEMINI_API_KEY": "YOUR_GEMINI_API_KEY",
"CLAUDE_MEM_GEMINI_MODEL": "gemini-2.5-flash-lite",
"CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED": true
}
EOF
```
**Get your free API key**: https://aistudio.google.com/apikey
### Option B: OpenRouter (100+ Models)
OpenRouter provides access to many models, including free options.
```bash
mkdir -p ~/.claude-mem
cat > ~/.claude-mem/settings.json << 'EOF'
{
"CLAUDE_MEM_PROVIDER": "openrouter",
"CLAUDE_MEM_OPENROUTER_API_KEY": "YOUR_OPENROUTER_API_KEY"
}
EOF
```
**Get your API key**: https://openrouter.ai/keys
**Free models available**:
- `google/gemini-2.0-flash-exp:free`
- `xiaomi/mimo-v2-flash:free`
### Option C: Claude API (If You Have API Access)
If you have Anthropic API credits but not a Claude Code subscription:
```bash
mkdir -p ~/.claude-mem
cat > ~/.claude-mem/settings.json << 'EOF'
{
"CLAUDE_MEM_PROVIDER": "claude",
"ANTHROPIC_API_KEY": "YOUR_ANTHROPIC_API_KEY"
}
EOF
```
## Step 3: Install Cursor Hooks
```bash
# From the claude-mem repo directory (recommended - all projects)
bun run cursor:install -- user
# Or for project-level only:
bun run cursor:install
```
This installs:
- Hook scripts to `.cursor/hooks/`
- Hook configuration to `.cursor/hooks.json`
- Context template to `.cursor/rules/`
## Step 4: Start the Worker
```bash
bun run worker:start
```
The worker runs in the background and handles:
- Session management
- Observation processing
- AI-powered summarization
- Context file updates
## Step 5: Restart Cursor & Verify
1. **Restart Cursor IDE** to load the new hooks
2. **Check installation status**:
```bash
bun run cursor:status
```
3. **Verify the worker is running**:
```bash
curl http://127.0.0.1:37777/api/readiness
```
Should return: `{"status":"ready"}`
4. **Open the web viewer**: http://localhost:37777
## How It Works
1. **Before each prompt**: Hooks initialize a session and ensure the worker is running
2. **During agent work**: MCP tools, shell commands, and file edits are captured
3. **When agent stops**: Summary is generated and context file is updated
4. **Next session**: Fresh context is automatically injected via `.cursor/rules/`
## Troubleshooting
### "No provider configured" error
Verify your settings file exists and has valid credentials:
```bash
cat ~/.claude-mem/settings.json
```
### Worker not starting
Check logs:
```bash
tail -f ~/.claude-mem/logs/worker-$(date +%Y-%m-%d).log
```
### Hooks not executing
1. Check Cursor Settings → Hooks tab for errors
2. Verify scripts are executable:
```bash
chmod +x ~/.cursor/hooks/*.sh
```
3. Check the Hooks output channel in Cursor
### Rate limiting (Gemini free tier)
If you hit the 1500 requests/day limit:
- Wait until the next day
- Upgrade to a paid plan
- Switch to OpenRouter with a paid model
## Next Steps
- Read [README.md](README.md) for detailed hook documentation
- Check [CONTEXT-INJECTION.md](CONTEXT-INJECTION.md) for context behavior details
- Visit https://docs.claude-mem.ai for full documentation
## Quick Reference
| Command | Purpose |
|---------|---------|
| `bun run cursor:install -- user` | Install hooks for all projects (recommended) |
| `bun run cursor:install` | Install hooks for current project only |
| `bun run cursor:status` | Check installation status |
| `bun run worker:start` | Start the background worker |
| `bun run worker:stop` | Stop the background worker |
| `bun run worker:restart` | Restart the worker |
---
## Windows Installation
Windows users get full support via PowerShell scripts. The installer automatically detects Windows and installs the appropriate scripts.
### Enable Script Execution (if needed)
PowerShell may require you to enable script execution:
```powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
```
### Step-by-Step for Windows
```powershell
# Clone and build
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem
bun install
bun run build
# Configure provider (Gemini example)
$settingsDir = "$env:USERPROFILE\.claude-mem"
New-Item -ItemType Directory -Force -Path $settingsDir
@"
{
"CLAUDE_MEM_PROVIDER": "gemini",
"CLAUDE_MEM_GEMINI_API_KEY": "YOUR_GEMINI_API_KEY"
}
"@ | Out-File -FilePath "$settingsDir\settings.json" -Encoding UTF8
# Interactive setup (recommended - walks you through everything)
bun run cursor:setup
# Or manual installation
bun run cursor:install
bun run worker:start
```
### What Gets Installed on Windows
The installer copies these PowerShell scripts to `.cursor\hooks\`:
| Script | Purpose |
|--------|---------|
| `common.ps1` | Shared utilities |
| `session-init.ps1` | Initialize session on prompt |
| `context-inject.ps1` | Inject memory context |
| `save-observation.ps1` | Capture MCP/shell usage |
| `save-file-edit.ps1` | Capture file edits |
| `session-summary.ps1` | Generate summary on stop |
The `hooks.json` file is configured to invoke PowerShell with `-ExecutionPolicy Bypass` to ensure scripts run without additional configuration.
### Windows Troubleshooting
**"Execution of scripts is disabled on this system"**
Run as Administrator:
```powershell
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine
```
**PowerShell scripts not running**
Verify the hooks.json contains PowerShell invocations:
```powershell
Get-Content .cursor\hooks.json
```
Should show commands like:
```
powershell.exe -ExecutionPolicy Bypass -File "./.cursor/hooks/session-init.ps1"
```
**Worker not responding**
Check if port 37777 is in use:
```powershell
Get-NetTCPConnection -LocalPort 37777
```
**Antivirus blocking scripts**
Some antivirus software may block PowerShell scripts. Add an exception for the `.cursor\hooks\` directory if needed.
+192
View File
@@ -0,0 +1,192 @@
# Common utility functions for Cursor hooks (PowerShell)
# Dot-source this file in hook scripts: . "$PSScriptRoot\common.ps1"
# Note: ErrorActionPreference should be set in each script, not globally here
# Get worker port from settings with validation
function Get-WorkerPort {
$settingsPath = Join-Path $env:USERPROFILE ".claude-mem\settings.json"
$port = 37777
if (Test-Path $settingsPath) {
try {
$settings = Get-Content $settingsPath -Raw | ConvertFrom-Json
if ($settings.CLAUDE_MEM_WORKER_PORT) {
$parsedPort = [int]$settings.CLAUDE_MEM_WORKER_PORT
if ($parsedPort -ge 1 -and $parsedPort -le 65535) {
$port = $parsedPort
}
}
} catch {
# Ignore parse errors, use default
}
}
return $port
}
# Ensure worker is running with retries
function Test-WorkerReady {
param(
[int]$Port = 37777,
[int]$MaxRetries = 75
)
for ($i = 0; $i -lt $MaxRetries; $i++) {
try {
$response = Invoke-RestMethod -Uri "http://127.0.0.1:$Port/api/readiness" -Method Get -TimeoutSec 1 -ErrorAction Stop
return $true
} catch {
Start-Sleep -Milliseconds 200
}
}
return $false
}
# Get project name from workspace root
function Get-ProjectName {
param([string]$WorkspaceRoot)
if ([string]::IsNullOrEmpty($WorkspaceRoot)) {
return "unknown-project"
}
# Handle Windows drive root (e.g., "C:\")
if ($WorkspaceRoot -match '^([A-Za-z]):\\?$') {
return "drive-$($Matches[1].ToUpper())"
}
$projectName = Split-Path $WorkspaceRoot -Leaf
if ([string]::IsNullOrEmpty($projectName)) {
return "unknown-project"
}
return $projectName
}
# URL encode a string
function Get-UrlEncodedString {
param([string]$String)
if ([string]::IsNullOrEmpty($String)) {
return ""
}
return [System.Uri]::EscapeDataString($String)
}
# Check if string is empty or null
function Test-IsEmpty {
param([string]$String)
return [string]::IsNullOrEmpty($String) -or $String -eq "null" -or $String -eq "empty"
}
# Safely read JSON from stdin with error handling
function Read-JsonInput {
try {
$input = [Console]::In.ReadToEnd()
if ([string]::IsNullOrEmpty($input)) {
return @{}
}
return $input | ConvertFrom-Json -ErrorAction Stop
} catch {
return @{}
}
}
# Safely get JSON field with fallback
function Get-JsonField {
param(
[PSObject]$Json,
[string]$Field,
[string]$Fallback = ""
)
if ($null -eq $Json) {
return $Fallback
}
# Handle array access syntax (e.g., "workspace_roots[0]")
if ($Field -match '^(.+)\[(\d+)\]$') {
$arrayField = $Matches[1]
$index = [int]$Matches[2]
if ($Json.PSObject.Properties.Name -contains $arrayField) {
$array = $Json.$arrayField
if ($null -ne $array -and $array.Count -gt $index) {
$value = $array[$index]
if (-not (Test-IsEmpty $value)) {
return $value
}
}
}
return $Fallback
}
# Simple field access
if ($Json.PSObject.Properties.Name -contains $Field) {
$value = $Json.$Field
if (-not (Test-IsEmpty $value)) {
return $value
}
}
return $Fallback
}
# Convert object to JSON string (compact)
function ConvertTo-JsonCompact {
param([object]$Object)
return $Object | ConvertTo-Json -Compress -Depth 10
}
# Send HTTP POST request (fire-and-forget style)
function Send-HttpPostAsync {
param(
[string]$Uri,
[object]$Body
)
try {
$bodyJson = ConvertTo-JsonCompact $Body
Start-Job -ScriptBlock {
param($u, $b)
try {
Invoke-RestMethod -Uri $u -Method Post -Body $b -ContentType "application/json" -TimeoutSec 5 -ErrorAction SilentlyContinue | Out-Null
} catch {}
} -ArgumentList $Uri, $bodyJson | Out-Null
} catch {
# Ignore errors - fire and forget
}
}
# Send HTTP POST request (synchronous)
function Send-HttpPost {
param(
[string]$Uri,
[object]$Body
)
try {
$bodyJson = ConvertTo-JsonCompact $Body
Invoke-RestMethod -Uri $u -Method Post -Body $bodyJson -ContentType "application/json" -TimeoutSec 5 -ErrorAction SilentlyContinue | Out-Null
} catch {
# Ignore errors
}
}
# Get HTTP response
function Get-HttpResponse {
param(
[string]$Uri,
[int]$TimeoutSec = 5
)
try {
return Invoke-RestMethod -Uri $Uri -Method Get -TimeoutSec $TimeoutSec -ErrorAction Stop
} catch {
return $null
}
}
+140
View File
@@ -0,0 +1,140 @@
#!/bin/bash
# Common utility functions for Cursor hooks
# Source this file in hook scripts: source "$(dirname "$0")/common.sh"
# Check if required commands exist
check_dependencies() {
local missing=()
if ! command -v jq >/dev/null 2>&1; then
missing+=("jq")
fi
if ! command -v curl >/dev/null 2>&1; then
missing+=("curl")
fi
if [ ${#missing[@]} -gt 0 ]; then
echo "Error: Missing required dependencies: ${missing[*]}" >&2
echo "Please install: ${missing[*]}" >&2
return 1
fi
return 0
}
# Safely read JSON from stdin with error handling
read_json_input() {
local input
input=$(cat 2>/dev/null || echo "{}")
# Validate JSON
if ! echo "$input" | jq empty 2>/dev/null; then
# Invalid JSON - return empty object
echo "{}"
return 1
fi
echo "$input"
return 0
}
# Get worker port from settings with validation
get_worker_port() {
local data_dir="${HOME}/.claude-mem"
local settings_file="${data_dir}/settings.json"
local port="37777"
if [ -f "$settings_file" ]; then
local parsed_port
parsed_port=$(jq -r '.CLAUDE_MEM_WORKER_PORT // "37777"' "$settings_file" 2>/dev/null || echo "37777")
# Validate port is a number between 1-65535
if [[ "$parsed_port" =~ ^[0-9]+$ ]] && [ "$parsed_port" -ge 1 ] && [ "$parsed_port" -le 65535 ]; then
port="$parsed_port"
fi
fi
echo "$port"
}
# Ensure worker is running with retries
ensure_worker_running() {
local port="${1:-37777}"
local max_retries="${2:-75}" # 15 seconds total (75 * 0.2s)
local retry_count=0
while [ $retry_count -lt $max_retries ]; do
if curl -s -f "http://127.0.0.1:${port}/api/readiness" >/dev/null 2>&1; then
return 0
fi
sleep 0.2
retry_count=$((retry_count + 1))
done
return 1
}
# URL encode a string (basic implementation)
url_encode() {
local string="$1"
# Use printf to URL encode
printf '%s' "$string" | jq -sRr @uri
}
# Get project name from workspace root
get_project_name() {
local workspace_root="$1"
if [ -z "$workspace_root" ]; then
echo "unknown-project"
return
fi
# Use basename, fallback to unknown-project
local project_name
project_name=$(basename "$workspace_root" 2>/dev/null || echo "unknown-project")
# Handle edge case: empty basename (root directory)
if [ -z "$project_name" ]; then
# Check if it's a Windows drive root
if [[ "$workspace_root" =~ ^[A-Za-z]:\\?$ ]]; then
local drive_letter
drive_letter=$(echo "$workspace_root" | grep -oE '^[A-Za-z]' | tr '[:lower:]' '[:upper:]')
echo "drive-${drive_letter}"
else
echo "unknown-project"
fi
else
echo "$project_name"
fi
}
# Safely extract JSON field with fallback
# Supports both simple fields (e.g., "conversation_id") and array access (e.g., "workspace_roots[0]")
json_get() {
local json="$1"
local field="$2"
local fallback="${3:-}"
local value
# Handle array access syntax (e.g., "workspace_roots[0]")
if [[ "$field" =~ ^(.+)\[([0-9]+)\]$ ]]; then
local array_field="${BASH_REMATCH[1]}"
local index="${BASH_REMATCH[2]}"
value=$(echo "$json" | jq -r --arg f "$array_field" --arg i "$index" --arg fb "$fallback" '.[$f] // [] | .[$i | tonumber] // $fb' 2>/dev/null || echo "$fallback")
else
# Simple field access
value=$(echo "$json" | jq -r --arg f "$field" --arg fb "$fallback" '.[$f] // $fb' 2>/dev/null || echo "$fallback")
fi
echo "$value"
}
# Check if string is empty or null
is_empty() {
local str="$1"
[ -z "$str" ] || [ "$str" = "null" ] || [ "$str" = "empty" ]
}
+74
View File
@@ -0,0 +1,74 @@
# Context Hook for Cursor (beforeSubmitPrompt) - PowerShell
# Ensures worker is running and refreshes context before prompt submission
#
# Context is updated in BOTH places:
# - Here (beforeSubmitPrompt): Fresh context at session start
# - stop hook (session-summary.ps1): Updated context after observations are made
$ErrorActionPreference = "SilentlyContinue"
# Source common utilities
$commonPath = Join-Path $PSScriptRoot "common.ps1"
if (Test-Path $commonPath) {
. $commonPath
} else {
Write-Output '{"continue": true}'
exit 0
}
# Read JSON input from stdin
$input = Read-JsonInput
# Extract workspace root
$workspaceRoot = Get-JsonField $input "workspace_roots[0]" ""
if (Test-IsEmpty $workspaceRoot) {
$workspaceRoot = Get-Location
}
# Get project name
$projectName = Get-ProjectName $workspaceRoot
# Get worker port from settings
$workerPort = Get-WorkerPort
# Ensure worker is running (with retries)
# This primes the worker before the session starts
if (Test-WorkerReady -Port $workerPort) {
# Refresh context file with latest observations
$projectEncoded = Get-UrlEncodedString $projectName
$contextUri = "http://127.0.0.1:$workerPort/api/context/inject?project=$projectEncoded"
$context = Get-HttpResponse -Uri $contextUri
if (-not [string]::IsNullOrEmpty($context)) {
$rulesDir = Join-Path $workspaceRoot ".cursor\rules"
$rulesFile = Join-Path $rulesDir "claude-mem-context.mdc"
# Create rules directory if it doesn't exist
if (-not (Test-Path $rulesDir)) {
New-Item -ItemType Directory -Path $rulesDir -Force | Out-Null
}
# Write context as a Cursor rule with alwaysApply: true
$ruleContent = @"
---
alwaysApply: true
description: "Claude-mem context from past sessions (auto-updated)"
---
# Memory Context from Past Sessions
The following context is from claude-mem, a persistent memory system that tracks your coding sessions.
$context
---
*Updated after last session. Use claude-mem's MCP search tools for more detailed queries.*
"@
Set-Content -Path $rulesFile -Value $ruleContent -Encoding UTF8 -Force
}
}
# Allow prompt to continue
Write-Output '{"continue": true}'
exit 0
+70
View File
@@ -0,0 +1,70 @@
#!/bin/bash
# Context Hook for Cursor (beforeSubmitPrompt)
# Ensures worker is running and refreshes context before prompt submission
#
# Context is updated in BOTH places:
# - Here (beforeSubmitPrompt): Fresh context at session start
# - stop hook (session-summary.sh): Updated context after observations are made
# Source common utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/common.sh" 2>/dev/null || {
echo '{"continue": true}'
exit 0
}
# Check dependencies (non-blocking)
check_dependencies >/dev/null 2>&1 || true
# Read JSON input from stdin
input=$(read_json_input)
# Extract workspace root
workspace_root=$(json_get "$input" "workspace_roots[0]" "")
if is_empty "$workspace_root"; then
workspace_root=$(pwd)
fi
# Get project name
project_name=$(get_project_name "$workspace_root")
# Get worker port from settings
worker_port=$(get_worker_port)
# Ensure worker is running (with retries)
# This primes the worker before the session starts
if ensure_worker_running "$worker_port" >/dev/null 2>&1; then
# Refresh context file with latest observations
project_encoded=$(url_encode "$project_name")
context=$(curl -s -f "http://127.0.0.1:${worker_port}/api/context/inject?project=${project_encoded}" 2>/dev/null || echo "")
if [ -n "$context" ]; then
rules_dir="${workspace_root}/.cursor/rules"
rules_file="${rules_dir}/claude-mem-context.mdc"
# Create rules directory if it doesn't exist
mkdir -p "$rules_dir" 2>/dev/null || true
# Write context as a Cursor rule with alwaysApply: true
cat > "$rules_file" 2>/dev/null << EOF
---
alwaysApply: true
description: "Claude-mem context from past sessions (auto-updated)"
---
# Memory Context from Past Sessions
The following context is from claude-mem, a persistent memory system that tracks your coding sessions.
${context}
---
*Updated after last session. Use claude-mem's MCP search tools for more detailed queries.*
EOF
fi
fi
# Allow prompt to continue
echo '{"continue": true}'
exit 0
+84
View File
@@ -0,0 +1,84 @@
# Claude-Mem Rules for Cursor
## Automatic Context Injection
The `context-inject.sh` hook **automatically creates and updates** a rules file at:
```
.cursor/rules/claude-mem-context.mdc
```
This file:
- Has `alwaysApply: true` so it's included in every chat session
- Contains recent context from past sessions
- Auto-refreshes on every prompt submission
**You don't need to manually create any rules file!**
## Optional: Additional Instructions
If you want to add custom instructions about claude-mem (beyond the auto-injected context), create a separate rules file:
### `.cursor/rules/claude-mem-instructions.mdc`
```markdown
---
alwaysApply: true
description: "Instructions for using claude-mem memory system"
---
# Memory System Usage
You have access to claude-mem, a persistent memory system. In addition to the auto-injected context above, you can search for more detailed information using MCP tools:
## Available MCP Tools
1. **search** - Find relevant past observations
```
search(query="authentication bug", project="my-project", limit=10)
```
2. **timeline** - Get context around a specific observation
```
timeline(anchor=<observation_id>, depth_before=3, depth_after=3)
```
3. **get_observations** - Fetch full details for specific IDs
```
get_observations(ids=[123, 456])
```
## When to Search Memory
- When the user asks about previous work or decisions
- When encountering unfamiliar code patterns in this project
- When debugging issues that might have been addressed before
- When asked to continue or build upon previous work
## 3-Layer Workflow
Follow this pattern for token efficiency:
1. **Search first** - Get compact index (~50-100 tokens/result)
2. **Timeline** - Get chronological context around interesting results
3. **Fetch details** - Only for relevant observations (~500-1000 tokens/result)
Never fetch full details without filtering first.
```
## File Locations
| File | Purpose | Created By |
|------|---------|------------|
| `.cursor/rules/claude-mem-context.mdc` | Auto-injected context | Hook (automatic) |
| `.cursor/rules/claude-mem-instructions.mdc` | MCP tool instructions | You (optional) |
## Git Ignore
If you don't want to commit the auto-generated context file:
```gitignore
# .gitignore
.cursor/rules/claude-mem-context.mdc
```
The instructions file can be committed to share with your team.
+34
View File
@@ -0,0 +1,34 @@
{
"version": 1,
"hooks": {
"beforeSubmitPrompt": [
{
"command": "./cursor-hooks/session-init.sh"
},
{
"command": "./cursor-hooks/context-inject.sh"
}
],
"afterMCPExecution": [
{
"command": "./cursor-hooks/save-observation.sh"
}
],
"afterShellExecution": [
{
"command": "./cursor-hooks/save-observation.sh"
}
],
"afterFileEdit": [
{
"command": "./cursor-hooks/save-file-edit.sh"
}
],
"stop": [
{
"command": "./cursor-hooks/session-summary.sh"
}
]
}
}
+71
View File
@@ -0,0 +1,71 @@
#!/bin/bash
# Installation script for claude-mem Cursor hooks
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
INSTALL_TYPE="${1:-user}" # 'user' (recommended) or 'project'
echo "Installing claude-mem Cursor hooks (${INSTALL_TYPE} level)..."
case "$INSTALL_TYPE" in
"project")
if [ ! -d ".cursor" ]; then
mkdir -p .cursor
fi
TARGET_DIR=".cursor"
HOOKS_DIR=".cursor/hooks"
;;
"user")
TARGET_DIR="${HOME}/.cursor"
HOOKS_DIR="${HOME}/.cursor/hooks"
;;
*)
echo "Invalid install type: $INSTALL_TYPE"
echo "Usage: $0 [user (recommended)|project]"
exit 1
;;
esac
# Create hooks directory
mkdir -p "$HOOKS_DIR"
# Copy hook scripts
echo "Copying hook scripts..."
cp "$SCRIPT_DIR"/*.sh "$HOOKS_DIR/"
chmod +x "$HOOKS_DIR"/*.sh
# Copy hooks.json
echo "Copying hooks.json..."
cp "$SCRIPT_DIR/hooks.json" "$TARGET_DIR/hooks.json"
# Update paths in hooks.json if needed
# Use portable sed approach that works on both BSD (macOS) and GNU (Linux) sed
if [ "$INSTALL_TYPE" = "project" ]; then
# For project-level, paths should be relative
# Create temp file, modify, then move (portable across sed variants)
tmp_file=$(mktemp)
sed 's|\./cursor-hooks/|\./\.cursor/hooks/|g' "$TARGET_DIR/hooks.json" > "$tmp_file"
mv "$tmp_file" "$TARGET_DIR/hooks.json"
else
# For user-level, use absolute paths
tmp_file=$(mktemp)
sed "s|\./cursor-hooks/|${HOOKS_DIR}/|g" "$TARGET_DIR/hooks.json" > "$tmp_file"
mv "$tmp_file" "$TARGET_DIR/hooks.json"
fi
echo ""
echo "✓ Installation complete!"
echo ""
echo "Hooks installed to: $TARGET_DIR/hooks.json"
echo "Scripts installed to: $HOOKS_DIR"
echo ""
echo "Next steps:"
echo "1. Ensure claude-mem worker is running:"
echo " cd ~/.claude/plugins/marketplaces/thedotmack && npm run worker:start"
echo ""
echo "2. Restart Cursor to load the hooks"
echo ""
echo "3. Check Cursor Settings → Hooks tab to verify hooks are active"
echo ""
+126
View File
@@ -0,0 +1,126 @@
# Save File Edit Hook for Cursor (PowerShell)
# Captures file edits made by the agent
# Maps file edits to claude-mem observations
$ErrorActionPreference = "SilentlyContinue"
# Source common utilities
$commonPath = Join-Path $PSScriptRoot "common.ps1"
if (Test-Path $commonPath) {
. $commonPath
} else {
Write-Warning "common.ps1 not found, using fallback functions"
exit 0
}
# Read JSON input from stdin with error handling
$input = Read-JsonInput
# Extract common fields with safe fallbacks
$conversationId = Get-JsonField $input "conversation_id" ""
$generationId = Get-JsonField $input "generation_id" ""
$filePath = Get-JsonField $input "file_path" ""
$workspaceRoot = Get-JsonField $input "workspace_roots[0]" ""
# Fallback to current directory if no workspace root
if (Test-IsEmpty $workspaceRoot) {
$workspaceRoot = Get-Location
}
# Exit if no file_path
if (Test-IsEmpty $filePath) {
exit 0
}
# Use conversation_id as session_id, fallback to generation_id
$sessionId = $conversationId
if (Test-IsEmpty $sessionId) {
$sessionId = $generationId
}
# Exit if no session_id available
if (Test-IsEmpty $sessionId) {
exit 0
}
# Get worker port from settings with validation
$workerPort = Get-WorkerPort
# Extract edits array, defaulting to [] if invalid
$edits = @()
if ($input.PSObject.Properties.Name -contains "edits") {
$edits = $input.edits
if ($null -eq $edits -or -not ($edits -is [array])) {
$edits = @()
}
}
# Exit if no edits
if ($edits.Count -eq 0) {
exit 0
}
# Create a summary of the edits for the observation
$editSummaries = @()
foreach ($edit in $edits) {
$oldStr = ""
$newStr = ""
if ($edit.PSObject.Properties.Name -contains "old_string") {
$oldStr = $edit.old_string
if ($oldStr.Length -gt 50) {
$oldStr = $oldStr.Substring(0, 50) + "..."
}
}
if ($edit.PSObject.Properties.Name -contains "new_string") {
$newStr = $edit.new_string
if ($newStr.Length -gt 50) {
$newStr = $newStr.Substring(0, 50) + "..."
}
}
$editSummaries += "$oldStr$newStr"
}
$editSummary = $editSummaries -join "; "
if ([string]::IsNullOrEmpty($editSummary)) {
$editSummary = "File edited"
}
# Treat file edits as a "write_file" tool usage
$toolInput = @{
file_path = $filePath
edits = $edits
}
$toolResponse = @{
success = $true
summary = $editSummary
}
$payload = @{
contentSessionId = $sessionId
tool_name = "write_file"
tool_input = $toolInput
tool_response = $toolResponse
cwd = $workspaceRoot
}
# Ensure worker is running (with retries like claude-mem hooks)
if (-not (Test-WorkerReady -Port $workerPort)) {
# Worker not ready - exit gracefully (don't block Cursor)
exit 0
}
# Send observation to claude-mem worker (fire-and-forget)
$uri = "http://127.0.0.1:$workerPort/api/sessions/observations"
try {
$bodyJson = ConvertTo-JsonCompact $payload
Invoke-RestMethod -Uri $uri -Method Post -Body $bodyJson -ContentType "application/json" -TimeoutSec 5 -ErrorAction SilentlyContinue | Out-Null
} catch {
# Ignore errors - don't block Cursor
}
exit 0
+112
View File
@@ -0,0 +1,112 @@
#!/bin/bash
# Save File Edit Hook for Cursor
# Captures file edits made by the agent
# Maps file edits to claude-mem observations
# Source common utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/common.sh" 2>/dev/null || {
echo "Warning: common.sh not found, using fallback functions" >&2
}
# Check dependencies (non-blocking)
check_dependencies >/dev/null 2>&1 || true
# Read JSON input from stdin with error handling
input=$(read_json_input)
# Extract common fields with safe fallbacks
conversation_id=$(json_get "$input" "conversation_id" "")
generation_id=$(json_get "$input" "generation_id" "")
file_path=$(json_get "$input" "file_path" "")
workspace_root=$(json_get "$input" "workspace_roots[0]" "")
# Fallback to current directory if no workspace root
if is_empty "$workspace_root"; then
workspace_root=$(pwd)
fi
# Exit if no file_path
if is_empty "$file_path"; then
exit 0
fi
# Use conversation_id as session_id, fallback to generation_id
session_id="$conversation_id"
if is_empty "$session_id"; then
session_id="$generation_id"
fi
# Exit if no session_id available
if is_empty "$session_id"; then
exit 0
fi
# Get worker port from settings with validation
worker_port=$(get_worker_port)
# Extract edits array, defaulting to [] if invalid
edits=$(echo "$input" | jq -c '.edits // []' 2>/dev/null || echo "[]")
# Validate edits is a valid JSON array
if ! echo "$edits" | jq 'type == "array"' 2>/dev/null | grep -q true; then
edits="[]"
fi
# Exit if no edits
if [ "$edits" = "[]" ] || is_empty "$edits"; then
exit 0
fi
# Create a summary of the edits for the observation (with error handling)
edit_summary=$(echo "$edits" | jq -r '[.[] | "\(.old_string[0:50] // "")... → \(.new_string[0:50] // "")..."] | join("; ")' 2>/dev/null || echo "File edited")
# Treat file edits as a "write_file" tool usage
tool_input=$(jq -n \
--arg path "$file_path" \
--argjson edits "$edits" \
'{
file_path: $path,
edits: $edits
}' 2>/dev/null || echo '{}')
tool_response=$(jq -n \
--arg summary "$edit_summary" \
'{
success: true,
summary: $summary
}' 2>/dev/null || echo '{}')
payload=$(jq -n \
--arg sessionId "$session_id" \
--arg cwd "$workspace_root" \
--argjson toolInput "$tool_input" \
--argjson toolResponse "$tool_response" \
'{
contentSessionId: $sessionId,
tool_name: "write_file",
tool_input: $toolInput,
tool_response: $toolResponse,
cwd: $cwd
}' 2>/dev/null)
# Exit if payload creation failed
if [ -z "$payload" ]; then
exit 0
fi
# Ensure worker is running (with retries like claude-mem hooks)
if ! ensure_worker_running "$worker_port"; then
# Worker not ready - exit gracefully (don't block Cursor)
exit 0
fi
# Send observation to claude-mem worker (fire-and-forget)
curl -s -X POST \
"http://127.0.0.1:${worker_port}/api/sessions/observations" \
-H "Content-Type: application/json" \
-d "$payload" \
>/dev/null 2>&1 || true
exit 0
+126
View File
@@ -0,0 +1,126 @@
# Save Observation Hook for Cursor (PowerShell)
# Captures MCP tool usage and shell command execution
# Maps to claude-mem's save-hook functionality
$ErrorActionPreference = "SilentlyContinue"
# Source common utilities
$commonPath = Join-Path $PSScriptRoot "common.ps1"
if (Test-Path $commonPath) {
. $commonPath
} else {
Write-Warning "common.ps1 not found, using fallback functions"
exit 0
}
# Read JSON input from stdin with error handling
$input = Read-JsonInput
# Extract common fields with safe fallbacks
$conversationId = Get-JsonField $input "conversation_id" ""
$generationId = Get-JsonField $input "generation_id" ""
$workspaceRoot = Get-JsonField $input "workspace_roots[0]" ""
# Fallback to current directory if no workspace root
if (Test-IsEmpty $workspaceRoot) {
$workspaceRoot = Get-Location
}
# Use conversation_id as session_id (stable across turns), fallback to generation_id
$sessionId = $conversationId
if (Test-IsEmpty $sessionId) {
$sessionId = $generationId
}
# Exit if no session_id available
if (Test-IsEmpty $sessionId) {
exit 0
}
# Get worker port from settings with validation
$workerPort = Get-WorkerPort
# Determine hook type and extract relevant data
$hookEvent = Get-JsonField $input "hook_event_name" ""
$payload = $null
if ($hookEvent -eq "afterMCPExecution") {
# MCP tool execution
$toolName = Get-JsonField $input "tool_name" ""
if (Test-IsEmpty $toolName) {
exit 0
}
# Extract tool_input and tool_response, defaulting to {} if invalid
$toolInput = @{}
$toolResponse = @{}
if ($input.PSObject.Properties.Name -contains "tool_input") {
$toolInput = $input.tool_input
if ($null -eq $toolInput) { $toolInput = @{} }
}
if ($input.PSObject.Properties.Name -contains "result_json") {
$toolResponse = $input.result_json
if ($null -eq $toolResponse) { $toolResponse = @{} }
}
# Prepare observation payload
$payload = @{
contentSessionId = $sessionId
tool_name = $toolName
tool_input = $toolInput
tool_response = $toolResponse
cwd = $workspaceRoot
}
} elseif ($hookEvent -eq "afterShellExecution") {
# Shell command execution
$command = Get-JsonField $input "command" ""
if (Test-IsEmpty $command) {
exit 0
}
$output = Get-JsonField $input "output" ""
# Treat shell commands as "Bash" tool usage
$toolInput = @{ command = $command }
$toolResponse = @{ output = $output }
$payload = @{
contentSessionId = $sessionId
tool_name = "Bash"
tool_input = $toolInput
tool_response = $toolResponse
cwd = $workspaceRoot
}
} else {
exit 0
}
# Exit if payload creation failed
if ($null -eq $payload) {
exit 0
}
# Ensure worker is running (with retries like claude-mem hooks)
if (-not (Test-WorkerReady -Port $workerPort)) {
# Worker not ready - exit gracefully (don't block Cursor)
exit 0
}
# Send observation to claude-mem worker (fire-and-forget)
$uri = "http://127.0.0.1:$workerPort/api/sessions/observations"
try {
$bodyJson = ConvertTo-JsonCompact $payload
Invoke-RestMethod -Uri $uri -Method Post -Body $bodyJson -ContentType "application/json" -TimeoutSec 5 -ErrorAction SilentlyContinue | Out-Null
} catch {
# Ignore errors - don't block Cursor
}
exit 0
+129
View File
@@ -0,0 +1,129 @@
#!/bin/bash
# Save Observation Hook for Cursor
# Captures MCP tool usage and shell command execution
# Maps to claude-mem's save-hook functionality
# Source common utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/common.sh" 2>/dev/null || {
echo "Warning: common.sh not found, using fallback functions" >&2
}
# Check dependencies (non-blocking)
check_dependencies >/dev/null 2>&1 || true
# Read JSON input from stdin with error handling
input=$(read_json_input)
# Extract common fields with safe fallbacks
conversation_id=$(json_get "$input" "conversation_id" "")
generation_id=$(json_get "$input" "generation_id" "")
workspace_root=$(json_get "$input" "workspace_roots[0]" "")
# Fallback to current directory if no workspace root
if is_empty "$workspace_root"; then
workspace_root=$(pwd)
fi
# Use conversation_id as session_id (stable across turns), fallback to generation_id
session_id="$conversation_id"
if is_empty "$session_id"; then
session_id="$generation_id"
fi
# Exit if no session_id available
if is_empty "$session_id"; then
exit 0
fi
# Get worker port from settings with validation
worker_port=$(get_worker_port)
# Determine hook type and extract relevant data
hook_event=$(json_get "$input" "hook_event_name" "")
if [ "$hook_event" = "afterMCPExecution" ]; then
# MCP tool execution
tool_name=$(json_get "$input" "tool_name" "")
if is_empty "$tool_name"; then
exit 0
fi
# Extract tool_input and tool_response, defaulting to {} if invalid
tool_input=$(echo "$input" | jq -c '.tool_input // {}' 2>/dev/null || echo "{}")
tool_response=$(echo "$input" | jq -c '.result_json // {}' 2>/dev/null || echo "{}")
# Validate JSON
if ! echo "$tool_input" | jq empty 2>/dev/null; then
tool_input="{}"
fi
if ! echo "$tool_response" | jq empty 2>/dev/null; then
tool_response="{}"
fi
# Prepare observation payload
payload=$(jq -n \
--arg sessionId "$session_id" \
--arg toolName "$tool_name" \
--argjson toolInput "$tool_input" \
--argjson toolResponse "$tool_response" \
--arg cwd "$workspace_root" \
'{
contentSessionId: $sessionId,
tool_name: $toolName,
tool_input: $toolInput,
tool_response: $toolResponse,
cwd: $cwd
}' 2>/dev/null)
elif [ "$hook_event" = "afterShellExecution" ]; then
# Shell command execution
command=$(json_get "$input" "command" "")
if is_empty "$command"; then
exit 0
fi
output=$(json_get "$input" "output" "")
# Treat shell commands as "Bash" tool usage
tool_input=$(jq -n --arg cmd "$command" '{command: $cmd}' 2>/dev/null || echo '{}')
tool_response=$(jq -n --arg out "$output" '{output: $out}' 2>/dev/null || echo '{}')
payload=$(jq -n \
--arg sessionId "$session_id" \
--arg cwd "$workspace_root" \
--argjson toolInput "$tool_input" \
--argjson toolResponse "$tool_response" \
'{
contentSessionId: $sessionId,
tool_name: "Bash",
tool_input: $toolInput,
tool_response: $toolResponse,
cwd: $cwd
}' 2>/dev/null)
else
exit 0
fi
# Exit if payload creation failed
if [ -z "$payload" ]; then
exit 0
fi
# Ensure worker is running (with retries like claude-mem hooks)
if ! ensure_worker_running "$worker_port"; then
# Worker not ready - exit gracefully (don't block Cursor)
exit 0
fi
# Send observation to claude-mem worker (fire-and-forget)
curl -s -X POST \
"http://127.0.0.1:${worker_port}/api/sessions/observations" \
-H "Content-Type: application/json" \
-d "$payload" \
>/dev/null 2>&1 || true
exit 0
+79
View File
@@ -0,0 +1,79 @@
# Session Initialization Hook for Cursor (PowerShell)
# Maps to claude-mem's new-hook functionality
# Initializes a new session when a prompt is submitted
#
# NOTE: This hook runs as part of beforeSubmitPrompt and MUST output valid JSON
# with at least {"continue": true} to allow prompt submission.
$ErrorActionPreference = "SilentlyContinue"
# Source common utilities
$commonPath = Join-Path $PSScriptRoot "common.ps1"
if (Test-Path $commonPath) {
. $commonPath
} else {
# Fallback - output continue and exit
Write-Output '{"continue": true}'
exit 0
}
# Read JSON input from stdin with error handling
$input = Read-JsonInput
# Extract common fields with safe fallbacks
$conversationId = Get-JsonField $input "conversation_id" ""
$generationId = Get-JsonField $input "generation_id" ""
$prompt = Get-JsonField $input "prompt" ""
$workspaceRoot = Get-JsonField $input "workspace_roots[0]" ""
# Fallback to current directory if no workspace root
if (Test-IsEmpty $workspaceRoot) {
$workspaceRoot = Get-Location
}
# Get project name from workspace root
$projectName = Get-ProjectName $workspaceRoot
# Use conversation_id as session_id (stable across turns), fallback to generation_id
$sessionId = $conversationId
if (Test-IsEmpty $sessionId) {
$sessionId = $generationId
}
# Exit gracefully if no session_id available (still allow prompt)
if (Test-IsEmpty $sessionId) {
Write-Output '{"continue": true}'
exit 0
}
# Get worker port from settings with validation
$workerPort = Get-WorkerPort
# Ensure worker is running (with retries like claude-mem hooks)
if (-not (Test-WorkerReady -Port $workerPort)) {
# Worker not ready - still allow prompt to continue
Write-Output '{"continue": true}'
exit 0
}
# Strip leading slash from commands for memory agent (parity with new-hook.ts)
# /review 101 → review 101 (more semantic for observations)
$cleanedPrompt = $prompt
if (-not [string]::IsNullOrEmpty($prompt) -and $prompt.StartsWith("/")) {
$cleanedPrompt = $prompt.Substring(1)
}
# Initialize session via HTTP - handles DB operations and privacy checks
$payload = @{
contentSessionId = $sessionId
project = $projectName
prompt = $cleanedPrompt
}
# Send request to worker (fire-and-forget, don't wait for response)
$uri = "http://127.0.0.1:$workerPort/api/sessions/init"
Send-HttpPostAsync -Uri $uri -Body $payload
# Always allow prompt to continue
Write-Output '{"continue": true}'
exit 0
+93
View File
@@ -0,0 +1,93 @@
#!/bin/bash
# Session Initialization Hook for Cursor
# Maps to claude-mem's new-hook functionality
# Initializes a new session when a prompt is submitted
#
# NOTE: This hook runs as part of beforeSubmitPrompt and MUST output valid JSON
# with at least {"continue": true} to allow prompt submission.
# Source common utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/common.sh" 2>/dev/null || {
# Fallback - output continue and exit
echo '{"continue": true}'
exit 0
}
# Check dependencies (non-blocking - just warn)
check_dependencies >/dev/null 2>&1 || true
# Read JSON input from stdin with error handling
input=$(read_json_input)
# Extract common fields with safe fallbacks
conversation_id=$(json_get "$input" "conversation_id" "")
generation_id=$(json_get "$input" "generation_id" "")
prompt=$(json_get "$input" "prompt" "")
workspace_root=$(json_get "$input" "workspace_roots[0]" "")
# Fallback to current directory if no workspace root
if is_empty "$workspace_root"; then
workspace_root=$(pwd)
fi
# Get project name from workspace root
project_name=$(get_project_name "$workspace_root")
# Use conversation_id as session_id (stable across turns), fallback to generation_id
session_id="$conversation_id"
if is_empty "$session_id"; then
session_id="$generation_id"
fi
# Exit gracefully if no session_id available (still allow prompt)
if is_empty "$session_id"; then
echo '{"continue": true}'
exit 0
fi
# Get worker port from settings with validation
worker_port=$(get_worker_port)
# Ensure worker is running (with retries like claude-mem hooks)
if ! ensure_worker_running "$worker_port"; then
# Worker not ready - still allow prompt to continue
echo '{"continue": true}'
exit 0
fi
# Strip leading slash from commands for memory agent (parity with new-hook.ts)
# /review 101 → review 101 (more semantic for observations)
cleaned_prompt="$prompt"
if [ -n "$prompt" ] && [ "${prompt:0:1}" = "/" ]; then
cleaned_prompt="${prompt:1}"
fi
# Initialize session via HTTP - handles DB operations and privacy checks
payload=$(jq -n \
--arg sessionId "$session_id" \
--arg project "$project_name" \
--arg promptText "$cleaned_prompt" \
'{
contentSessionId: $sessionId,
project: $project,
prompt: $promptText
}' 2>/dev/null)
# Exit if payload creation failed (still allow prompt)
if [ -z "$payload" ]; then
echo '{"continue": true}'
exit 0
fi
# Send request to worker (fire-and-forget, don't wait for response)
curl -s -X POST \
"http://127.0.0.1:${worker_port}/api/sessions/init" \
-H "Content-Type: application/json" \
-d "$payload" \
>/dev/null 2>&1 &
# Always allow prompt to continue
echo '{"continue": true}'
exit 0
+108
View File
@@ -0,0 +1,108 @@
# Session Summary Hook for Cursor (stop) - PowerShell
# Called when agent loop ends
#
# This hook:
# 1. Generates session summary
# 2. Updates context file for next session
#
# Output: Empty JSON {} or {"followup_message": "..."} for auto-iteration
$ErrorActionPreference = "SilentlyContinue"
# Source common utilities
$commonPath = Join-Path $PSScriptRoot "common.ps1"
if (Test-Path $commonPath) {
. $commonPath
} else {
Write-Output '{}'
exit 0
}
# Read JSON input from stdin with error handling
$input = Read-JsonInput
# Extract common fields with safe fallbacks
$conversationId = Get-JsonField $input "conversation_id" ""
$generationId = Get-JsonField $input "generation_id" ""
$workspaceRoot = Get-JsonField $input "workspace_roots[0]" ""
$status = Get-JsonField $input "status" "completed"
# Fallback workspace to current directory
if (Test-IsEmpty $workspaceRoot) {
$workspaceRoot = Get-Location
}
# Get project name
$projectName = Get-ProjectName $workspaceRoot
# Use conversation_id as session_id, fallback to generation_id
$sessionId = $conversationId
if (Test-IsEmpty $sessionId) {
$sessionId = $generationId
}
# Exit if no session_id available
if (Test-IsEmpty $sessionId) {
Write-Output '{}'
exit 0
}
# Get worker port from settings with validation
$workerPort = Get-WorkerPort
# Ensure worker is running (with retries)
if (-not (Test-WorkerReady -Port $workerPort)) {
Write-Output '{}'
exit 0
}
# 1. Request summary generation (fire-and-forget)
# Note: Cursor doesn't provide transcript_path like Claude Code does,
# so we can't extract last_user_message and last_assistant_message.
$summaryPayload = @{
contentSessionId = $sessionId
last_user_message = ""
last_assistant_message = ""
}
$summaryUri = "http://127.0.0.1:$workerPort/api/sessions/summarize"
Send-HttpPostAsync -Uri $summaryUri -Body $summaryPayload
# 2. Update context file for next session
# Fetch fresh context (includes observations from this session)
$projectEncoded = Get-UrlEncodedString $projectName
$contextUri = "http://127.0.0.1:$workerPort/api/context/inject?project=$projectEncoded"
$context = Get-HttpResponse -Uri $contextUri
if (-not [string]::IsNullOrEmpty($context)) {
$rulesDir = Join-Path $workspaceRoot ".cursor\rules"
$rulesFile = Join-Path $rulesDir "claude-mem-context.mdc"
# Create rules directory if it doesn't exist
if (-not (Test-Path $rulesDir)) {
New-Item -ItemType Directory -Path $rulesDir -Force | Out-Null
}
# Write context as a Cursor rule with alwaysApply: true
$ruleContent = @"
---
alwaysApply: true
description: "Claude-mem context from past sessions (auto-updated)"
---
# Memory Context from Past Sessions
The following context is from claude-mem, a persistent memory system that tracks your coding sessions.
$context
---
*Updated after last session. Use claude-mem's MCP search tools for more detailed queries.*
"@
Set-Content -Path $rulesFile -Value $ruleContent -Encoding UTF8 -Force
}
# Output empty JSON - no followup message
Write-Output '{}'
exit 0
+111
View File
@@ -0,0 +1,111 @@
#!/bin/bash
# Session Summary Hook for Cursor (stop)
# Called when agent loop ends
#
# This hook:
# 1. Generates session summary
# 2. Updates context file for next session
#
# Output: Empty JSON {} or {"followup_message": "..."} for auto-iteration
# Source common utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/common.sh" 2>/dev/null || {
echo '{}'
exit 0
}
# Check dependencies (non-blocking)
check_dependencies >/dev/null 2>&1 || true
# Read JSON input from stdin with error handling
input=$(read_json_input)
# Extract common fields with safe fallbacks
conversation_id=$(json_get "$input" "conversation_id" "")
generation_id=$(json_get "$input" "generation_id" "")
workspace_root=$(json_get "$input" "workspace_roots[0]" "")
status=$(json_get "$input" "status" "completed")
# Fallback workspace to current directory
if is_empty "$workspace_root"; then
workspace_root=$(pwd)
fi
# Get project name
project_name=$(get_project_name "$workspace_root")
# Use conversation_id as session_id, fallback to generation_id
session_id="$conversation_id"
if is_empty "$session_id"; then
session_id="$generation_id"
fi
# Exit if no session_id available
if is_empty "$session_id"; then
echo '{}'
exit 0
fi
# Get worker port from settings with validation
worker_port=$(get_worker_port)
# Ensure worker is running (with retries)
if ! ensure_worker_running "$worker_port"; then
echo '{}'
exit 0
fi
# 1. Request summary generation (fire-and-forget)
# Note: Cursor doesn't provide transcript_path like Claude Code does,
# so we can't extract last_user_message and last_assistant_message.
payload=$(jq -n \
--arg sessionId "$session_id" \
'{
contentSessionId: $sessionId,
last_user_message: "",
last_assistant_message: ""
}' 2>/dev/null)
if [ -n "$payload" ]; then
curl -s -X POST \
"http://127.0.0.1:${worker_port}/api/sessions/summarize" \
-H "Content-Type: application/json" \
-d "$payload" \
>/dev/null 2>&1 &
fi
# 2. Update context file for next session
# Fetch fresh context (includes observations from this session)
project_encoded=$(url_encode "$project_name")
context=$(curl -s -f "http://127.0.0.1:${worker_port}/api/context/inject?project=${project_encoded}" 2>/dev/null || echo "")
if [ -n "$context" ]; then
rules_dir="${workspace_root}/.cursor/rules"
rules_file="${rules_dir}/claude-mem-context.mdc"
# Create rules directory if it doesn't exist
mkdir -p "$rules_dir" 2>/dev/null || true
# Write context as a Cursor rule with alwaysApply: true
cat > "$rules_file" 2>/dev/null << EOF
---
alwaysApply: true
description: "Claude-mem context from past sessions (auto-updated)"
---
# Memory Context from Past Sessions
The following context is from claude-mem, a persistent memory system that tracks your coding sessions.
${context}
---
*Updated after last session. Use claude-mem's MCP search tools for more detailed queries.*
EOF
fi
# Output empty JSON - no followup message
echo '{}'
exit 0
+103
View File
@@ -0,0 +1,103 @@
# User Message Hook for Cursor (PowerShell)
# Displays context information to the user
# Maps to claude-mem's user-message-hook functionality
# Note: Cursor doesn't have a direct equivalent, but we can output to stderr
# for visibility in Cursor's output channels
#
# This is an OPTIONAL hook. It can be added to beforeSubmitPrompt if desired,
# but may be verbose since it runs on every prompt submission.
$ErrorActionPreference = "SilentlyContinue"
# Read JSON input from stdin (if any)
$inputJson = $null
try {
$inputText = [Console]::In.ReadToEnd()
if (-not [string]::IsNullOrEmpty($inputText)) {
$inputJson = $inputText | ConvertFrom-Json -ErrorAction SilentlyContinue
}
} catch {
$inputJson = $null
}
# Extract workspace root
$workspaceRoot = ""
if ($null -ne $inputJson -and $inputJson.PSObject.Properties.Name -contains "workspace_roots") {
$wsRoots = $inputJson.workspace_roots
if ($null -ne $wsRoots -and $wsRoots.Count -gt 0) {
$workspaceRoot = $wsRoots[0]
}
}
if ([string]::IsNullOrEmpty($workspaceRoot)) {
$workspaceRoot = Get-Location
}
# Get project name
$projectName = Split-Path $workspaceRoot -Leaf
if ([string]::IsNullOrEmpty($projectName)) {
$projectName = "unknown-project"
}
# Get worker port from settings
$settingsPath = Join-Path $env:USERPROFILE ".claude-mem\settings.json"
$workerPort = 37777
if (Test-Path $settingsPath) {
try {
$settings = Get-Content $settingsPath -Raw | ConvertFrom-Json
if ($settings.CLAUDE_MEM_WORKER_PORT) {
$workerPort = [int]$settings.CLAUDE_MEM_WORKER_PORT
}
} catch {
# Use default
}
}
# Ensure worker is running
$maxRetries = 75
$workerReady = $false
for ($i = 0; $i -lt $maxRetries; $i++) {
try {
$response = Invoke-RestMethod -Uri "http://127.0.0.1:$workerPort/api/readiness" -Method Get -TimeoutSec 1 -ErrorAction Stop
$workerReady = $true
break
} catch {
Start-Sleep -Milliseconds 200
}
}
# If worker not ready, exit silently
if (-not $workerReady) {
exit 0
}
# Fetch formatted context from worker API (with colors)
$projectEncoded = [System.Uri]::EscapeDataString($projectName)
$contextUrl = "http://127.0.0.1:$workerPort/api/context/inject?project=$projectEncoded&colors=true"
$output = $null
try {
$output = Invoke-RestMethod -Uri $contextUrl -Method Get -TimeoutSec 5 -ErrorAction Stop
} catch {
$output = $null
}
# Output to stderr for visibility (parity with user-message-hook.ts)
# Note: Cursor may not display stderr the same way Claude Code does,
# but this is the best we can do without direct UI integration
if (-not [string]::IsNullOrEmpty($output)) {
[Console]::Error.WriteLine("")
[Console]::Error.WriteLine("📝 Claude-Mem Context Loaded")
[Console]::Error.WriteLine(" ️ Viewing context from past sessions")
[Console]::Error.WriteLine("")
[Console]::Error.WriteLine($output)
[Console]::Error.WriteLine("")
[Console]::Error.WriteLine("💡 Tip: Wrap content with <private> ... </private> to prevent storing sensitive information.")
[Console]::Error.WriteLine("💬 Community: https://discord.gg/J4wttp9vDu")
[Console]::Error.WriteLine("📺 Web Viewer: http://localhost:$workerPort/")
[Console]::Error.WriteLine("")
}
exit 0
+70
View File
@@ -0,0 +1,70 @@
#!/bin/bash
# User Message Hook for Cursor
# Displays context information to the user
# Maps to claude-mem's user-message-hook functionality
# Note: Cursor doesn't have a direct equivalent, but we can output to stderr
# for visibility in Cursor's output channels
#
# This is an OPTIONAL hook. It can be added to beforeSubmitPrompt if desired,
# but may be verbose since it runs on every prompt submission.
# Read JSON input from stdin (if any)
input=$(cat 2>/dev/null || echo "{}")
# Extract workspace root
workspace_root=$(echo "$input" | jq -r '.workspace_roots[0] // empty' 2>/dev/null || echo "")
if [ -z "$workspace_root" ]; then
workspace_root=$(pwd)
fi
# Get project name
project_name=$(basename "$workspace_root" 2>/dev/null || echo "unknown-project")
# Get worker port from settings
data_dir="${HOME}/.claude-mem"
settings_file="${data_dir}/settings.json"
worker_port="37777"
if [ -f "$settings_file" ]; then
worker_port=$(jq -r '.CLAUDE_MEM_WORKER_PORT // "37777"' "$settings_file" 2>/dev/null || echo "37777")
fi
# Ensure worker is running
max_retries=75
retry_count=0
while [ $retry_count -lt $max_retries ]; do
if curl -s -f "http://127.0.0.1:${worker_port}/api/readiness" > /dev/null 2>&1; then
break
fi
sleep 0.2
retry_count=$((retry_count + 1))
done
# If worker not ready, exit silently
if [ $retry_count -eq $max_retries ]; then
exit 0
fi
# Fetch formatted context from worker API (with colors)
context_url="http://127.0.0.1:${worker_port}/api/context/inject?project=${project_name}&colors=true"
output=$(curl -s -f "$context_url" 2>/dev/null || echo "")
# Output to stderr for visibility (parity with user-message-hook.ts)
# Note: Cursor may not display stderr the same way Claude Code does,
# but this is the best we can do without direct UI integration
if [ -n "$output" ]; then
echo "" >&2
echo "📝 Claude-Mem Context Loaded" >&2
echo " ️ Viewing context from past sessions" >&2
echo "" >&2
echo "$output" >&2
echo "" >&2
echo "💡 Tip: Wrap content with <private> ... </private> to prevent storing sensitive information." >&2
echo "💬 Community: https://discord.gg/J4wttp9vDu" >&2
echo "📺 Web Viewer: http://localhost:${worker_port}/" >&2
echo "" >&2
fi
exit 0
+586
View File
@@ -0,0 +1,586 @@
# Hooks
Hooks let you observe, control, and extend the agent loop using custom scripts. Hooks are spawned processes that communicate over stdio using JSON in both directions. They run before or after defined stages of the agent loop and can observe, block, or modify behavior.
With hooks, you can:
- Run formatters after edits
- Add analytics for events
- Scan for PII or secrets
- Gate risky operations (e.g., SQL writes)
<Tip>
Looking for ready-to-use integrations? See [Partner Integrations](#partner-integrations) for security, governance, and secrets management solutions from our ecosystem partners.
</Tip>
## Agent and Tab Support
Hooks work with both **Cursor Agent** (Cmd+K/Agent Chat) and **Cursor Tab** (inline completions), but they use different hook events:
**Agent (Cmd+K/Agent Chat)** uses the standard hooks:
- `beforeShellExecution` / `afterShellExecution` - Control shell commands
- `beforeMCPExecution` / `afterMCPExecution` - Control MCP tool usage
- `beforeReadFile` / `afterFileEdit` - Control file access and edits
- `beforeSubmitPrompt` - Validate prompts before submission
- `stop` - Handle agent completion
- `afterAgentResponse` / `afterAgentThought` - Track agent responses
**Tab (inline completions)** uses specialized hooks:
- `beforeTabFileRead` - Control file access for Tab completions
- `afterTabFileEdit` - Post-process Tab edits
These separate hooks allow different policies for autonomous Tab operations versus user-directed Agent operations.
## Quickstart
Create a `hooks.json` file. You can create it at the project level (`<project>/.cursor/hooks.json`) or in your home directory (`~/.cursor/hooks.json`). Project-level hooks apply only to that specific project, while home directory hooks apply globally.
```json
{
"version": 1,
"hooks": {
"afterFileEdit": [{ "command": "./hooks/format.sh" }]
}
}
```
Create your hook script at `~/.cursor/hooks/format.sh`:
```bash
#!/bin/bash
# Read input, do something, exit 0
cat > /dev/null
exit 0
```
Make it executable:
```bash
chmod +x ~/.cursor/hooks/format.sh
```
Restart Cursor. Your hook now runs after every file edit.
## Examples
<CodeGroup>
```json title="hooks.json"
{
"version": 1,
"hooks": {
"beforeShellExecution": [
{
"command": "./hooks/audit.sh"
},
{
"command": "./hooks/block-git.sh"
}
],
"beforeMCPExecution": [
{
"command": "./hooks/audit.sh"
}
],
"afterShellExecution": [
{
"command": "./hooks/audit.sh"
}
],
"afterMCPExecution": [
{
"command": "./hooks/audit.sh"
}
],
"afterFileEdit": [
{
"command": "./hooks/audit.sh"
}
],
"beforeSubmitPrompt": [
{
"command": "./hooks/audit.sh"
}
],
"stop": [
{
"command": "./hooks/audit.sh"
}
],
"beforeTabFileRead": [
{
"command": "./hooks/redact-secrets-tab.sh"
}
],
"afterTabFileEdit": [
{
"command": "./hooks/format-tab.sh"
}
]
}
}
```
```sh title="audit.sh"
#!/bin/bash
# audit.sh - Hook script that writes all JSON input to /tmp/agent-audit.log
# This script is designed to be called by Cursor's hooks system for auditing purposes
# Read JSON input from stdin
json_input=$(cat)
# Create timestamp for the log entry
timestamp=$(date '+%Y-%m-%d %H:%M:%S')
# Create the log directory if it doesn't exist
mkdir -p "$(dirname /tmp/agent-audit.log)"
# Write the timestamped JSON entry to the audit log
echo "[$timestamp] $json_input" >> /tmp/agent-audit.log
# Exit successfully
exit 0
```
```sh title="block-git.sh"
#!/bin/bash
# Hook to block git commands and redirect to gh tool usage
# This hook implements the beforeShellExecution hook from the Cursor Hooks Spec
# Initialize debug logging
echo "Hook execution started" >> /tmp/hooks.log
# Read JSON input from stdin
input=$(cat)
echo "Received input: $input" >> /tmp/hooks.log
# Parse the command from the JSON input
command=$(echo "$input" | jq -r '.command // empty')
echo "Parsed command: '$command'" >> /tmp/hooks.log
# Check if the command contains 'git' or 'gh'
if [[ "$command" =~ git[[:space:]] ]] || [[ "$command" == "git" ]]; then
echo "Git command detected - blocking: '$command'" >> /tmp/hooks.log
# Block the git command and provide guidance to use gh tool instead
cat << EOF
{
"continue": true,
"permission": "deny",
"user_message": "Git command blocked. Please use the GitHub CLI (gh) tool instead.",
"agent_message": "The git command '$command' has been blocked by a hook. Instead of using raw git commands, please use the 'gh' tool which provides better integration with GitHub and follows best practices. For example:\n- Instead of 'git clone', use 'gh repo clone'\n- Instead of 'git push', use 'gh repo sync' or the appropriate gh command\n- For other git operations, check if there's an equivalent gh command or use the GitHub web interface\n\nThis helps maintain consistency and leverages GitHub's enhanced tooling."
}
EOF
elif [[ "$command" =~ gh[[:space:]] ]] || [[ "$command" == "gh" ]]; then
echo "GitHub CLI command detected - asking for permission: '$command'" >> /tmp/hooks.log
# Ask for permission for gh commands
cat << EOF
{
"continue": true,
"permission": "ask",
"user_message": "GitHub CLI command requires permission: $command",
"agent_message": "The command '$command' uses the GitHub CLI (gh) which can interact with your GitHub repositories and account. Please review and approve this command if you want to proceed."
}
EOF
else
echo "Non-git/non-gh command detected - allowing: '$command'" >> /tmp/hooks.log
# Allow non-git/non-gh commands
cat << EOF
{
"continue": true,
"permission": "allow"
}
EOF
fi
```
</CodeGroup>
## Partner Integrations
We partner with ecosystem vendors who have built hooks support with Cursor. These integrations cover security scanning, governance, secrets management, and more.
### MCP governance and visibility
| Partner | Description |
|---------|-------------|
| [MintMCP](https://www.mintmcp.com/blog/mcp-governance-cursor-hooks) | Build a complete inventory of MCP servers, monitor tool usage patterns, and scan responses for sensitive data before it reaches the AI model. |
| [Oasis Security](https://www.oasis.security/blog/cursor-oasis-governing-agentic-access) | Enforce least-privilege policies on AI agent actions and maintain full audit trails across enterprise systems. |
| [Runlayer](https://www.runlayer.com/blog/cursor-hooks) | Wrap MCP tools and integrate with their MCP broker for centralized control and visibility over agent-to-tool interactions. |
### Code security and best practices
| Partner | Description |
|---------|-------------|
| [Corridor](https://corridor.dev/blog/corridor-cursor-hooks/) | Get real-time feedback on code implementation and security design decisions as code is being written. |
| [Semgrep](https://semgrep.dev/blog/2025/cursor-hooks-mcp-server) | Automatically scan AI-generated code for vulnerabilities with real-time feedback to regenerate code until security issues are resolved. |
### Dependency security
| Partner | Description |
|---------|-------------|
| [Endor Labs](https://www.endorlabs.com/learn/bringing-malware-detection-into-ai-coding-workflows-with-cursor-hooks) | Intercept package installations and scan for malicious dependencies, preventing supply chain attacks before they enter your codebase. |
### Agent security and safety
| Partner | Description |
|---------|-------------|
| [Snyk](https://snyk.io/blog/evo-agent-guard-cursor-integration/) | Review agent actions in real-time with Evo Agent Guard, detecting and preventing issues like prompt injection and dangerous tool calls. |
### Secrets management
| Partner | Description |
|---------|-------------|
| [1Password](https://marketplace.1password.com/integration/cursor-hooks) | Validate that environment files from 1Password Environments are properly mounted before shell commands execute, enabling just-in-time secrets access without writing credentials to disk. |
For more details about our hooks partners, see the [Hooks for security and platform teams](/blog/hooks-partners) blog post.
## Configuration
Define hooks in a `hooks.json` file. Configuration can exist at multiple levels; higher-priority sources override lower ones:
```sh
~/.cursor/
├── hooks.json
└── hooks/
├── audit.sh
└── block-git.sh
```
- **Global** (Enterprise-managed):
- macOS: `/Library/Application Support/Cursor/hooks.json`
- Linux/WSL: `/etc/cursor/hooks.json`
- Windows: `C:\\ProgramData\\Cursor\\hooks.json`
- **Project Directory** (Project-specific):
- `<project-root>/.cursor/hooks.json`
- Project hooks run in any trusted workspace and are checked into version control with your project
- **Home Directory** (User-specific):
- `~/.cursor/hooks.json`
Priority order (highest to lowest): Enterprise → Project → User
The `hooks` object maps hook names to arrays of hook definitions. Each definition currently supports a `command` property that can be a shell string, an absolute path, or a path relative to the `hooks.json` file.
### Configuration file
```json
{
"version": 1,
"hooks": {
"beforeShellExecution": [{ "command": "./script.sh" }],
"afterShellExecution": [{ "command": "./script.sh" }],
"afterMCPExecution": [{ "command": "./script.sh" }],
"afterFileEdit": [{ "command": "./format.sh" }],
"beforeTabFileRead": [{ "command": "./redact-secrets-tab.sh" }],
"afterTabFileEdit": [{ "command": "./format-tab.sh" }]
}
}
```
The Agent hooks (`beforeShellExecution`, `afterShellExecution`, `beforeMCPExecution`, `afterMCPExecution`, `beforeReadFile`, `afterFileEdit`, `beforeSubmitPrompt`, `stop`, `afterAgentResponse`, `afterAgentThought`) apply to Cmd+K and Agent Chat operations. The Tab hooks (`beforeTabFileRead`, `afterTabFileEdit`) apply specifically to inline Tab completions.
## Team Distribution
Hooks can be distributed to team members using project hooks (via version control), MDM tools, or Cursor's cloud distribution system.
### Project Hooks (Version Control)
Project hooks are the simplest way to share hooks with your team. Place a `hooks.json` file at `<project-root>/.cursor/hooks.json` and commit it to your repository. When team members open the project in a trusted workspace, Cursor automatically loads and runs the project hooks.
Project hooks:
- Are stored in version control alongside your code
- Automatically load for all team members in trusted workspaces
- Can be project-specific (e.g., enforce formatting standards for a particular codebase)
- Require the workspace to be trusted to run (for security)
### MDM Distribution
Distribute hooks across your organization using Mobile Device Management (MDM) tools. Place the `hooks.json` file and hook scripts in the target directories on each machine.
**User home directory** (per-user distribution):
- `~/.cursor/hooks.json`
- `~/.cursor/hooks/` (for hook scripts)
**Global directories** (system-wide distribution):
- macOS: `/Library/Application Support/Cursor/hooks.json`
- Linux/WSL: `/etc/cursor/hooks.json`
- Windows: `C:\\ProgramData\\Cursor\\hooks.json`
Note: MDM-based distribution is fully managed by your organization. Cursor does not deploy or manage files through your MDM solution. Ensure your internal IT or security team handles configuration, deployment, and updates in accordance with your organization's policies.
### Cloud Distribution (Enterprise Only)
Enterprise teams can use Cursor's native cloud distribution to automatically sync hooks to all team members. Configure hooks in the [web dashboard](https://cursor.com/dashboard?tab=team-content&section=hooks). Cursor automatically delivers configured hooks to all client machines when team members log in.
Cloud distribution provides:
- Automatic synchronization to all team members (every thirty minutes)
- Operating system targeting for platform-specific hooks
- Centralized management through the dashboard
Enterprise administrators can create, edit, and manage team hooks from the dashboard without requiring access to individual machines.
## Reference
### Common schema
#### Input (all hooks)
All hooks receive a base set of fields in addition to their hook-specific fields:
```json
{
"conversation_id": "string",
"generation_id": "string",
"model": "string",
"hook_event_name": "string",
"cursor_version": "string",
"workspace_roots": ["<path>"],
"user_email": "string | null"
}
```
| Field | Type | Description |
|-------|------|-------------|
| `conversation_id` | string | Stable ID of the conversation across many turns |
| `generation_id` | string | The current generation that changes with every user message |
| `model` | string | The model configured for the composer that triggered the hook |
| `hook_event_name` | string | Which hook is being run |
| `cursor_version` | string | Cursor application version (e.g. "1.7.2") |
| `workspace_roots` | string[] | The list of root folders in the workspace (normally just one, but multiroot workspaces can have multiple) |
| `user_email` | string \| null | Email address of the authenticated user, if available |
### Hook events
#### beforeShellExecution / beforeMCPExecution
Called before any shell command or MCP tool is executed. Return a permission decision.
```json
// beforeShellExecution input
{
"command": "<full terminal command>",
"cwd": "<current working directory>"
}
// beforeMCPExecution input
{
"tool_name": "<tool name>",
"tool_input": "<json params>"
}
// Plus either:
{ "url": "<server url>" }
// Or:
{ "command": "<command string>" }
// Output
{
"permission": "allow" | "deny" | "ask",
"user_message": "<message shown in client>",
"agent_message": "<message sent to agent>"
}
```
#### afterShellExecution
Fires after a shell command executes; useful for auditing or collecting metrics from command output.
```json
// Input
{
"command": "<full terminal command>",
"output": "<full terminal output>",
"duration": 1234
}
```
| Field | Type | Description |
|-------|------|-------------|
| `command` | string | The full terminal command that was executed |
| `output` | string | Full output captured from the terminal |
| `duration` | number | Duration in milliseconds spent executing the shell command (excludes approval wait time) |
#### afterMCPExecution
Fires after an MCP tool executes; includes the tool's input parameters and full JSON result.
```json
// Input
{
"tool_name": "<tool name>",
"tool_input": "<json params>",
"result_json": "<tool result json>",
"duration": 1234
}
```
| Field | Type | Description |
|-------|------|-------------|
| `tool_name` | string | Name of the MCP tool that was executed |
| `tool_input` | string | JSON params string passed to the tool |
| `result_json` | string | JSON string of the tool response |
| `duration` | number | Duration in milliseconds spent executing the MCP tool (excludes approval wait time) |
#### afterFileEdit
Fires after the Agent edits a file; useful for formatters or accounting of agent-written code.
```json
// Input
{
"file_path": "<absolute path>",
"edits": [{ "old_string": "<search>", "new_string": "<replace>" }]
}
```
#### beforeTabFileRead
Called before Tab (inline completions) reads a file. Enable redaction or access control before Tab accesses file contents.
**Key differences from `beforeReadFile`:**
- Only triggered by Tab, not Agent
- Does not include `attachments` field (Tab doesn't use prompt attachments)
- Useful for applying different policies to autonomous Tab operations
```json
// Input
{
"file_path": "<absolute path>",
"content": "<file contents>"
}
// Output
{
"permission": "allow" | "deny"
}
```
#### afterTabFileEdit
Called after Tab (inline completions) edits a file. Useful for formatters or auditing of Tab-written code.
**Key differences from `afterFileEdit`:**
- Only triggered by Tab, not Agent
- Includes detailed edit information: `range`, `old_line`, and `new_line` for precise edit tracking
- Useful for fine-grained formatting or analysis of Tab edits
```json
// Input
{
"file_path": "<absolute path>",
"edits": [
{
"old_string": "<search>",
"new_string": "<replace>",
"range": {
"start_line_number": 10,
"start_column": 5,
"end_line_number": 10,
"end_column": 20
},
"old_line": "<line before edit>",
"new_line": "<line after edit>"
}
]
}
// Output
{
// No output fields currently supported
}
```
#### beforeSubmitPrompt
Called right after user hits send but before backend request. Can prevent submission.
```json
// Input
{
"prompt": "<user prompt text>",
"attachments": [
{
"type": "file" | "rule",
"filePath": "<absolute path>"
}
]
}
// Output
{
"continue": true | false,
"user_message": "<message shown to user when blocked>"
}
```
| Output Field | Type | Description |
|--------------|------|-------------|
| `continue` | boolean | Whether to allow the prompt submission to proceed |
| `user_message` | string (optional) | Message shown to the user when the prompt is blocked |
#### afterAgentResponse
Called after the agent has completed an assistant message.
```json
// Input
{
"text": "<assistant final text>"
}
```
#### afterAgentThought
Called after the agent completes a thinking block. Useful for observing the agent's reasoning process.
```json
// Input
{
"text": "<fully aggregated thinking text>",
"duration_ms": 5000
}
// Output
{
// No output fields currently supported
}
```
| Field | Type | Description |
|-------|------|-------------|
| `text` | string | Fully aggregated thinking text for the completed block |
| `duration_ms` | number (optional) | Duration in milliseconds for the thinking block |
#### stop
Called when the agent loop ends. Can optionally auto-submit a follow-up user message to keep iterating.
```json
// Input
{
"status": "completed" | "aborted" | "error",
"loop_count": 0
}
```
```json
// Output
{
"followup_message": "<message text>"
}
```
- The optional `followup_message` is a string. When provided and non-empty, Cursor will automatically submit it as the next user message. This enables loop-style flows (e.g., iterate until a goal is met).
- The `loop_count` field indicates how many times the stop hook has already triggered an automatic follow-up for this conversation (starts at 0). To prevent infinite loops, a maximum of 5 auto follow-ups is enforced.
## Troubleshooting
**How to confirm hooks are active**
There is a Hooks tab in Cursor Settings to debug configured and executed hooks, as well as a Hooks output channel to see errors.
**If hooks are not working**
- Restart Cursor to ensure the hooks service is running.
- Ensure hook script paths are relative to `hooks.json` when using relative paths.
+158
View File
@@ -248,6 +248,164 @@ search_observations({
---
## MCP Architecture Simplification (December 2025)
### The Problem: Complex MCP Implementation
**Before:**
```
9+ MCP tools registered at session start:
- search_observations
- find_by_type
- find_by_file
- find_by_concept
- get_recent_context
- get_observation
- get_session
- get_prompt
- help
Problems:
- Overlapping operations (search_observations vs find_by_type)
- Complex parameter schemas (~2,500 tokens in tool definitions)
- No built-in workflow guidance
- High cognitive load for Claude (which tool to use?)
- Code size: ~2,718 lines in mcp-server.ts
```
**The Insight:** Progressive disclosure should be built into tool design itself, not something Claude has to remember.
### The Solution: 3-Layer Workflow
**After:**
```
4 MCP tools following 3-layer workflow:
1. __IMPORTANT - Workflow documentation (always visible)
"3-LAYER WORKFLOW (ALWAYS FOLLOW):
1. search(query) → Get index with IDs
2. timeline(anchor=ID) → Get context
3. get_observations([IDs]) → Fetch details
NEVER fetch full details without filtering first."
2. search - Layer 1: Get index with IDs (~50-100 tokens/result)
3. timeline - Layer 2: Get chronological context
4. get_observations - Layer 3: Fetch full details (~500-1,000 tokens/result)
Benefits:
- Progressive disclosure enforced by tool structure
- No overlapping operations
- Simple schemas (additionalProperties: true)
- Clear workflow pattern
- Code size: ~312 lines in mcp-server.ts (88% reduction)
- ~10x token savings
```
### Migration: Skill-Based Search Removed
**Previously:** Used skill-based search
- mem-search skill invoked via natural language
- HTTP API called directly via curl
- Progressive disclosure through skill loading
- 17 skill documentation files
**Now:** Removed skill-based approach
- MCP-only architecture
- Native MCP protocol (better Claude integration)
- Works with both Claude Desktop and Claude Code
- Simpler to maintain (no skill files)
- All 19 mem-search skill files removed (~2,744 lines)
### Key Architectural Changes
**MCP Server Refactor:**
Before:
```typescript
// Complex parameter schemas
{
name: "search_observations",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "..." },
type: { type: "array", items: { enum: [...] } },
format: { enum: ["index", "full"] },
limit: { type: "number", minimum: 1, maximum: 100 },
// ... many more parameters
}
}
}
```
After:
```typescript
// Simple schemas with workflow guidance
{
name: "search",
description: "Step 1: Search memory. Returns index with IDs.",
inputSchema: {
type: "object",
properties: {},
additionalProperties: true // Accept any parameters
}
}
```
**Workflow Enforcement:**
Before: Claude had to remember progressive disclosure pattern
After: Tool structure makes it impossible to skip steps
- Can't get details without IDs from search
- Can't search without seeing __IMPORTANT reminder
- Timeline provides middle ground (context without full details)
### Impact
**Token Efficiency:**
```
Traditional: Fetch 20 observations upfront
→ 10,000-20,000 tokens
→ Only 2 observations relevant (90% waste)
3-Layer Workflow:
→ search (20 results): ~1,000-2,000 tokens
→ Review index, identify 3 relevant IDs
→ get_observations (3 IDs): ~1,500-3,000 tokens
→ Total: 2,500-5,000 tokens (50-75% savings)
```
**Code Simplicity:**
- MCP server: 2,718 lines → 312 lines (88% reduction)
- Removed: 19 skill files (~2,744 lines)
- Net reduction: ~5,150 lines of code removed
**User Experience:**
- Same natural language interaction
- Better token efficiency
- Clearer architecture
- Works identically on Claude Desktop and Claude Code
### Design Philosophy
**Progressive Disclosure Through Structure:**
The 3-layer workflow embodies progressive disclosure at the architectural level:
1. **Layer 1 (Index)** - "What exists?" - Cheap survey of options
2. **Layer 2 (Timeline)** - "What was happening?" - Context around specific points
3. **Layer 3 (Details)** - "Tell me everything" - Full details only when justified
Each layer provides a decision point where Claude can:
- Stop if irrelevant
- Get more context if uncertain
- Dive deep if confident
This makes it structurally difficult to waste tokens.
---
## v1-v2: The Naive Approach
### The First Attempt: Dump Everything
+403 -354
View File
@@ -1,448 +1,497 @@
---
title: "Search Architecture"
description: "mem-search skill with HTTP API and progressive disclosure"
description: "MCP tools with 3-layer workflow for token-efficient memory retrieval"
---
# Search Architecture
Claude-Mem uses a skill-based search architecture that provides intelligent memory retrieval through natural language queries. This replaced the MCP-based approach in v5.4.0 with a more efficient implementation. The skill was enhanced and renamed to "mem-search" in v5.5.0 for better scope differentiation.
Claude-mem uses an **MCP-based search architecture** that provides intelligent memory retrieval through 4 streamlined tools following a 3-layer workflow pattern.
## Overview
**Architecture**: Skill-Based Search + HTTP API + Progressive Disclosure
**Architecture**: MCP Tools → MCP Protocol → HTTP API → Worker Service
**Key Components**:
1. **mem-search Skill** (`plugin/skills/mem-search/SKILL.md`) - Auto-invoked when users ask about past work
2. **HTTP API Endpoints** (10 routes) - Fast, efficient search operations on port 37777
3. **Worker Service** - Express.js server with FTS5 full-text search
4. **SQLite Database** - Persistent storage with FTS5 virtual tables
5. **Chroma Vector DB** - Semantic search with hybrid retrieval
1. **MCP Tools** (4 tools) - `search`, `timeline`, `get_observations`, `__IMPORTANT`
2. **MCP Server** (`plugin/scripts/mcp-server.cjs`) - Thin wrapper over HTTP API
3. **HTTP API Endpoints** - Fast search operations on Worker Service (port 37777)
4. **Worker Service** - Express.js server with FTS5 full-text search
5. **SQLite Database** - Persistent storage with FTS5 virtual tables
6. **Chroma Vector DB** - Semantic search with hybrid retrieval
**v5.5.0 Enhancement**: Renamed from "search" to "mem-search" with:
- Effectiveness increased from 67% to 100%
- Concrete triggers increased from 44% to 85%
- 5+ unique identifiers for better scope differentiation
- Comprehensive documentation (17 files, 12 operation guides)
**Token Efficiency**: ~10x savings through 3-layer workflow pattern
## How It Works
### 1. User Query (Natural Language)
### 1. User Query
Claude has access to 4 MCP tools. When searching memory, Claude follows the 3-layer workflow:
```
User: "What bugs did we fix last session?"
Step 1: search(query="authentication bug", type="bugfix", limit=10)
Step 2: timeline(anchor=<observation_id>, depth_before=3, depth_after=3)
Step 3: get_observations(ids=[123, 456, 789])
```
### 2. Skill Invocation
### 2. MCP Protocol
Claude recognizes the intent and invokes the mem-search skill:
- Skill frontmatter (~250 tokens) loaded at session start
- Full skill instructions loaded on-demand when skill is invoked
- Progressive disclosure pattern minimizes context overhead
- "mem-search" naming provides clear scope differentiation from native memory
MCP server receives tool call via JSON-RPC over stdio:
```json
{
"method": "tools/call",
"params": {
"name": "search",
"arguments": {
"query": "authentication bug",
"type": "bugfix",
"limit": 10
}
}
}
```
### 3. HTTP API Call
The skill uses `curl` to call the HTTP API:
MCP server translates to HTTP request:
```bash
curl "http://localhost:37777/api/search/observations?query=bugs&type=bugfix&limit=5"
```typescript
const url = `http://localhost:37777/api/search?query=authentication%20bug&type=bugfix&limit=10`;
const response = await fetch(url);
```
### 4. FTS5 Search
### 4. Worker Processing
Worker service queries SQLite FTS5 virtual tables:
Worker service executes FTS5 query:
```sql
SELECT * FROM observations_fts
WHERE observations_fts MATCH ?
AND type = 'bugfix'
ORDER BY rank
LIMIT 5
LIMIT 10
```
### 5. Results Formatted
### 5. Results Returned
Skill formats results and returns to Claude:
Worker returns structured data → MCP server → Claude:
```
## Recent Bugfixes
1. [bugfix] Fixed authentication token expiry
Date: 2025-11-08 14:23:45
Files: src/auth/jwt.ts
2. [bugfix] Resolved database connection leak
Date: 2025-11-08 13:15:22
Files: src/services/database.ts
```
### 6. User Sees Answer
Claude presents the formatted results naturally in conversation.
## Architecture Change (v5.4.0)
### Before: MCP-Based Search
**Approach**: 9 MCP tools registered at session start
**Token Cost**: ~2,500 tokens in tool definitions per session
- Each tool's schema, parameters, descriptions loaded
- All 9 tools available whether needed or not
- No progressive disclosure
**Example MCP Tool**:
```json
{
"name": "search_observations",
"description": "Full-text search across observations...",
"inputSchema": {
"type": "object",
"properties": {
"query": { "type": "string", "description": "..." },
"type": { "type": "array", "items": { "enum": [...] } },
"format": { "enum": ["index", "full"] },
// ... many more parameters
"content": [{
"type": "text",
"text": "| ID | Time | Title | Type |\n|---|---|---|---|\n| #123 | 2:15 PM | Fixed auth token expiry | bugfix |"
}]
}
```
### 6. Claude Processes Results
Claude reviews the index, decides which observations are relevant, and can:
- Use `timeline` to get context
- Use `get_observations` to fetch full details for selected IDs
## The 4 MCP Tools
### `__IMPORTANT` - Workflow Documentation
Always visible to Claude. Explains the 3-layer workflow pattern.
**Description:**
```
3-LAYER WORKFLOW (ALWAYS FOLLOW):
1. search(query) → Get index with IDs (~50-100 tokens/result)
2. timeline(anchor=ID) → Get context around interesting results
3. get_observations([IDs]) → Fetch full details ONLY for filtered IDs
NEVER fetch full details without filtering first. 10x token savings.
```
**Purpose:** Ensures Claude follows token-efficient pattern
### `search` - Search Memory Index
**Tool Definition:**
```typescript
{
name: 'search',
description: 'Step 1: Search memory. Returns index with IDs. Params: query, limit, project, type, obs_type, dateStart, dateEnd, offset, orderBy',
inputSchema: {
type: 'object',
properties: {},
additionalProperties: true // Accepts any parameters
}
}
```
**HTTP Endpoint:** `GET /api/search`
**Parameters:**
- `query` - Full-text search query
- `limit` - Maximum results (default: 20)
- `type` - Filter by observation type
- `project` - Filter by project name
- `dateStart`, `dateEnd` - Date range filters
- `offset` - Pagination offset
- `orderBy` - Sort order
**Returns:** Compact index with IDs, titles, dates, types (~50-100 tokens per result)
### `timeline` - Get Chronological Context
**Tool Definition:**
```typescript
{
name: 'timeline',
description: 'Step 2: Get context around results. Params: anchor (observation ID) OR query (finds anchor automatically), depth_before, depth_after, project',
inputSchema: {
type: 'object',
properties: {},
additionalProperties: true
}
}
```
**HTTP Endpoint:** `GET /api/timeline`
**Parameters:**
- `anchor` - Observation ID to center timeline around (optional if query provided)
- `query` - Search query to find anchor automatically (optional if anchor provided)
- `depth_before` - Number of observations before anchor (default: 3)
- `depth_after` - Number of observations after anchor (default: 3)
- `project` - Filter by project name
**Returns:** Chronological view showing what happened before/during/after
### `get_observations` - Fetch Full Details
**Tool Definition:**
```typescript
{
name: 'get_observations',
description: 'Step 3: Fetch full details for filtered IDs. Params: ids (array of observation IDs, required), orderBy, limit, project',
inputSchema: {
type: 'object',
properties: {
ids: {
type: 'array',
items: { type: 'number' },
description: 'Array of observation IDs to fetch (required)'
}
},
required: ['ids'],
additionalProperties: true
}
}
```
**HTTP Endpoint:** `POST /api/observations/batch`
**Body:**
```json
{
"ids": [123, 456, 789],
"orderBy": "date_desc",
"project": "my-app"
}
```
**Returns:** Complete observation details (~500-1,000 tokens per observation)
## MCP Server Implementation
**Location:** `/Users/YOUR_USERNAME/.claude/plugins/marketplaces/thedotmack/plugin/scripts/mcp-server.cjs`
**Role:** Thin wrapper that translates MCP protocol to HTTP API calls
**Key Characteristics:**
- ~312 lines of code (reduced from ~2,718 lines in old implementation)
- No business logic - just protocol translation
- Single source of truth: Worker HTTP API
- Simple schemas with `additionalProperties: true`
**Handler Example:**
```typescript
{
name: 'search',
handler: async (args: any) => {
const endpoint = '/api/search';
const searchParams = new URLSearchParams();
for (const [key, value] of Object.entries(args)) {
searchParams.append(key, String(value));
}
const url = `http://localhost:37777${endpoint}?${searchParams}`;
const response = await fetch(url);
return await response.json();
}
}
```
## Worker HTTP API
**Location:** `src/services/worker-service.ts`
**Port:** 37777
**Search Endpoints:**
```typescript
GET /api/search # Main search (used by MCP search tool)
GET /api/timeline # Timeline context (used by MCP timeline tool)
POST /api/observations/batch # Fetch by IDs (used by MCP get_observations tool)
GET /api/health # Health check
```
**Database Access:**
- Uses `SessionSearch` service for FTS5 queries
- Uses `SessionStore` for structured queries
- Hybrid search with ChromaDB for semantic similarity
**FTS5 Full-Text Search:**
```typescript
// search tool → HTTP GET → FTS5 query
SELECT * FROM observations_fts
WHERE observations_fts MATCH ?
AND type = ?
AND date >= ? AND date <= ?
ORDER BY rank
LIMIT ? OFFSET ?
```
## The 3-Layer Workflow Pattern
### Design Philosophy
The 3-layer workflow embodies **progressive disclosure** - a core principle of claude-mem's architecture.
**Layer 1: Index (Search)**
- **What:** Compact table with IDs, titles, dates, types
- **Cost:** ~50-100 tokens per result
- **Purpose:** Survey what exists before committing tokens
- **Decision Point:** "Which observations are relevant?"
**Layer 2: Context (Timeline)**
- **What:** Chronological view of observations around a point
- **Cost:** Variable based on depth
- **Purpose:** Understand narrative arc, see what led to/from a point
- **Decision Point:** "Do I need full details?"
**Layer 3: Details (Get Observations)**
- **What:** Complete observation data (narrative, facts, files, concepts)
- **Cost:** ~500-1,000 tokens per observation
- **Purpose:** Deep dive on validated, relevant observations
- **Decision Point:** "Apply knowledge to current task"
### Token Efficiency
**Traditional RAG Approach:**
```
Fetch 20 observations upfront: 10,000-20,000 tokens
Relevance: ~10% (only 2 observations actually useful)
Waste: 18,000 tokens on irrelevant context
```
**3-Layer Workflow:**
```
Step 1: search (20 results) ~1,000-2,000 tokens
Step 2: Review index, filter to 3 relevant IDs
Step 3: get_observations (3 IDs) ~1,500-3,000 tokens
Total: 2,500-5,000 tokens (50-75% savings)
```
**10x Savings:** By filtering at index level before fetching full details
## Architecture Evolution
### Before: Complex MCP Implementation
**Approach:** 9 MCP tools with detailed parameter schemas
**Token Cost:** ~2,500 tokens in tool definitions per session
- `search_observations` - Full-text search
- `find_by_type` - Filter by type
- `find_by_file` - Filter by file
- `find_by_concept` - Filter by concept
- `get_recent_context` - Recent sessions
- `get_observation` - Fetch single observation
- `get_session` - Fetch session
- `get_prompt` - Fetch prompt
- `help` - API documentation
**Problems:**
- Overlapping operations (search_observations vs find_by_type)
- Complex parameter schemas
- No built-in workflow guidance
- High token cost at session start
**Code Size:** ~2,718 lines in mcp-server.ts
### After: Streamlined MCP Implementation
**Approach:** 4 MCP tools following 3-layer workflow
**Token Cost:** ~312 lines of code, simplified tool definitions
**Tools:**
1. `__IMPORTANT` - Workflow guidance (always visible)
2. `search` - Step 1 (index)
3. `timeline` - Step 2 (context)
4. `get_observations` - Step 3 (details)
**Benefits:**
- Progressive disclosure built into tool design
- No overlapping operations
- Simple schemas (`additionalProperties: true`)
- Clear workflow pattern
- ~10x token savings
**Code Size:** ~312 lines in mcp-server.ts (88% reduction)
### Key Insight
**Before:** Progressive disclosure was something Claude had to remember
**After:** Progressive disclosure is enforced by tool design itself
The 3-layer workflow pattern makes it structurally difficult to waste tokens:
- Can't fetch details without first getting IDs from search
- Can't search without seeing workflow reminder (`__IMPORTANT`)
- Timeline provides middle ground between index and full details
## Configuration
### Claude Desktop
Add to `claude_desktop_config.json`:
```json
{
"mcpServers": {
"mcp-search": {
"command": "node",
"args": [
"/Users/YOUR_USERNAME/.claude/plugins/marketplaces/thedotmack/plugin/scripts/mcp-server.cjs"
]
}
}
}
```
### After: Skill-Based Search
### Claude Code
**Approach**: 1 mem-search skill with progressive disclosure
MCP server is automatically configured via plugin installation. No manual setup required.
**Token Cost**: ~250 tokens in skill frontmatter per session
- Only skill description loaded at session start
- Full instructions loaded on-demand when skill is invoked
- HTTP API endpoints instead of MCP protocol
**Both clients use the same MCP tools** - the architecture works identically for Claude Desktop and Claude Code.
**Example Skill Frontmatter**:
```markdown
# Claude-Mem mem-search Skill
## Security
Access claude-mem's persistent memory through a comprehensive HTTP API.
Search for past work, understand context, and learn from previous decisions.
### FTS5 Injection Prevention
## When to Use This Skill
All search queries are escaped before FTS5 processing:
Invoke this skill when users ask about:
- Past work: "What did we do last session?"
- Bug fixes: "Did we fix this before?"
- Features: "How did we implement authentication?"
...
```
**Token Efficiency**: Minimal frontmatter at session start with progressive disclosure
## HTTP API Endpoints
The worker service exposes 10 search endpoints:
### Full-Text Search
```
GET /api/search/observations
GET /api/search/sessions
GET /api/search/prompts
```
**Parameters**:
- `query` - FTS5 search query (required)
- `type` - Filter by type (bugfix, feature, refactor, etc.)
- `project` - Filter by project name
- `limit` - Maximum results (default: 20)
- `offset` - Pagination offset
- `format` - Response format (index or full)
**Example**:
```bash
curl "http://localhost:37777/api/search/observations?query=authentication&type=decision&limit=5"
```
### Filtered Search
```
GET /api/search/by-type
GET /api/search/by-concept
GET /api/search/by-file
```
**Parameters**:
- `type` / `concept` / `filePath` - Filter criteria (required)
- `project` - Filter by project
- `limit` - Maximum results
- `format` - Response format
**Example**:
```bash
curl "http://localhost:37777/api/search/by-file?filePath=worker-service.ts&limit=10"
```
### Context Retrieval
```
GET /api/context/recent
GET /api/context/timeline
GET /api/timeline/by-query
```
**Parameters**:
- `project` - Filter by project
- `limit` - Number of sessions/records
- `anchor` - Timeline anchor point (ID or timestamp)
- `depth_before` - Records before anchor
- `depth_after` - Records after anchor
**Example**:
```bash
curl "http://localhost:37777/api/context/recent?project=claude-mem&limit=5"
```
### Documentation
```
GET /api/search/help
```
Returns API documentation in JSON format.
## Progressive Disclosure Pattern
The mem-search skill uses progressive disclosure to minimize token usage:
### Layer 1: Skill Frontmatter (Session Start)
**What's Loaded**: Skill description and when to use it (~250 tokens)
**Purpose**: Claude can recognize when to invoke the skill
**Example**:
```markdown
# Claude-Mem mem-search Skill
Access claude-mem's persistent memory through a comprehensive HTTP API.
## When to Use This Skill
Invoke this skill when users ask about:
- Past work: "What did we do last session?"
- Bug fixes: "Did we fix this before?"
...
```
### Layer 2: Full Skill Instructions (On-Demand)
**What's Loaded**: Complete operation documentation (~2,500 tokens)
**Purpose**: Detailed instructions for each search operation
**When Loaded**: Only when Claude invokes the skill
**Example Structure**:
```
/skills/search/
├── SKILL.md (main frontmatter)
├── operations/
│ ├── observations.md (detailed instructions)
│ ├── sessions.md
│ ├── prompts.md
│ ├── by-type.md
│ ├── by-concept.md
│ ├── by-file.md
│ ├── recent-context.md
│ ├── timeline.md
│ ├── timeline-by-query.md
│ ├── help.md
│ ├── formatting.md
│ └── common-workflows.md
```
### Layer 3: API Response
**What's Returned**: Search results in requested format
**Format Options**:
- `index` - Titles, dates, IDs only (~50-100 tokens per result)
- `full` - Complete details (~500-1000 tokens per result)
**Progressive Usage**: Start with `index`, drill down with `full` as needed
## Implementation Details
### mem-search Skill Structure
```
plugin/skills/mem-search/
├── SKILL.md # Main frontmatter (~250 tokens)
├── operations/
│ ├── observations.md # Search observations
│ ├── sessions.md # Search sessions
│ ├── prompts.md # Search prompts
│ ├── by-type.md # Filter by type
│ ├── by-concept.md # Filter by concept
│ ├── by-file.md # Filter by file
│ ├── recent-context.md # Get recent context
│ ├── timeline.md # Timeline around point
│ ├── timeline-by-query.md # Search + timeline
│ ├── help.md # API documentation
│ ├── formatting.md # Result formatting guide
│ └── common-workflows.md # Usage patterns
```
### Worker Service Integration
**File**: `src/services/worker-service.ts`
**Search Routes**:
```typescript
// Full-text search
app.get('/api/search/observations', handleSearchObservations);
app.get('/api/search/sessions', handleSearchSessions);
app.get('/api/search/prompts', handleSearchPrompts);
// Filtered search
app.get('/api/search/by-type', handleSearchByType);
app.get('/api/search/by-concept', handleSearchByConcept);
app.get('/api/search/by-file', handleSearchByFile);
// Context retrieval
app.get('/api/context/recent', handleRecentContext);
app.get('/api/context/timeline', handleTimeline);
app.get('/api/timeline/by-query', handleTimelineByQuery);
// Documentation
app.get('/api/search/help', handleHelp);
```
**Database Access**:
- Uses `SessionSearch` service for FTS5 queries
- Uses `SessionStore` for structured queries
- Hybrid search with ChromaDB for semantic similarity
### Security
**FTS5 Injection Prevention** (v4.2.3):
```typescript
function escapeFTS5Query(query: string): string {
return query.replace(/"/g, '""');
}
```
All user-provided search queries are properly escaped to prevent SQL injection.
**Testing:** 332 injection attack tests covering special characters, SQL keywords, quote escaping, and boolean operators.
**Comprehensive Testing**: 332 injection attack tests covering:
- Special characters
- SQL keywords
- Quote escaping
- Boolean operators
### MCP Protocol Security
## Benefits
- Stdio transport (no network exposure)
- Local-only HTTP API (localhost:37777)
- No authentication needed (local development only)
### 1. Token Efficiency
## Performance
**Before (MCP)**:
- Session start: All tool definitions loaded upfront
- Every session pays this cost
- No progressive disclosure
**FTS5 Full-Text Search:** Sub-10ms for typical queries
**After (Skill)**:
- Session start: Minimal token cost for skill frontmatter
- Full instructions loaded only when invoked (progressive disclosure)
- More efficient than loading all tool definitions upfront
**MCP Overhead:** Minimal - simple protocol translation
### 2. Natural Language Interface
**Caching:** HTTP layer allows response caching (future enhancement)
**Before**: Users needed to learn MCP tool syntax
```
search_observations with query="authentication" and type="decision"
```
**Pagination:** Efficient with offset/limit
**After**: Users ask naturally
```
"What decisions did we make about authentication?"
```
**Batching:** `get_observations` accepts multiple IDs in single call
Claude translates to appropriate API call.
## Benefits Over Alternative Approaches
### 3. Flexibility
### vs. Traditional RAG
**HTTP API Benefits**:
- Can be called from skills, MCP tools, or other clients
- Easy to test with curl
- Standard REST conventions
- JSON responses
**Traditional RAG:**
- Fetches everything upfront
- High token cost
- Low relevance ratio
**Progressive Disclosure**:
- Loads only what's needed
- Can add more operations without increasing base cost
- Documentation co-located with operations
**3-Layer MCP:**
- Fetches only what's needed
- ~10x token savings
- 100% relevance (Claude chooses what to fetch)
### 4. Performance
### vs. Previous MCP Implementation (v5.x)
**Fast Queries**: FTS5 full-text search under 10ms for typical queries
**Previous (9 tools):**
- Complex schemas
- Overlapping operations
- No workflow guidance
- ~2,500 tokens in definitions
**Caching**: HTTP layer allows response caching
**Current (4 tools):**
- Simple schemas
- Clear workflow
- Built-in guidance
- ~312 lines of code
**Pagination**: Efficient result pagination with offset/limit
### vs. Skill-Based Approach (Previously)
## Migration Notes
**Skill approach:**
- Required separate skill files
- HTTP API called directly via curl
- Progressive disclosure through skill loading
### For Users
**MCP approach:**
- Native MCP protocol (better Claude integration)
- Cleaner architecture (protocol translation layer)
- Works with both Claude Desktop and Claude Code
- Simpler to maintain (no skill files)
**No Action Required**: The migration from MCP to skill-based search is transparent.
**Same Questions Work**: Natural language queries work exactly the same way.
**Invisible Change**: Users won't notice any difference except better performance.
### For Developers
**Renamed**: MCP server (formerly `search-server.ts`, now `src/servers/mcp-server.ts`)
- Source file kept for reference
- No longer built or registered
- MCP configuration removed from `plugin/.mcp.json`
**New Implementation**: Skill-based search
- Skill files: `plugin/skills/mem-search/`
- HTTP endpoints: `src/services/worker-service.ts` (lines 200-400)
- Build script: `npm run build` includes skill files
- Sync script: `npm run sync-marketplace` copies to plugin directory
**Migration:** Skill-based search was removed in favor of streamlined MCP architecture.
## Troubleshooting
### MCP Server Not Connected
**Symptoms:** Tools not appearing in Claude
**Solution:**
1. Check MCP server path in configuration
2. Verify worker service is running: `curl http://localhost:37777/api/health`
3. Restart Claude Desktop/Code
### Worker Service Not Running
If searches fail, check worker service:
**Symptoms:** MCP tools fail with connection errors
**Solution:**
```bash
npm run worker:status # Check status
npm run worker:restart # Restart worker
npm run worker:logs # View logs
```
### HTTP Endpoints Not Responding
### Empty Search Results
Test endpoints directly:
**Symptoms:** search() returns no results
```bash
# Health check
curl http://localhost:37777/health
# Search test
curl "http://localhost:37777/api/search/observations?query=test&limit=1"
```
### Skill Not Invoking
If Claude doesn't invoke the mem-search skill automatically:
1. Check skill files exist: `ls ~/.claude/plugins/marketplaces/thedotmack/plugin/skills/mem-search/`
2. Restart Claude Code session to reload skill definitions
3. Try more explicit phrasing: "Search past sessions for bug fixes" or "What did we do in yesterday's session?"
4. Ensure your question is about previous sessions (not current conversation context)
**Troubleshooting:**
1. Test API directly: `curl "http://localhost:37777/api/search?query=test"`
2. Check database: `ls ~/.claude-mem/claude-mem.db`
3. Verify observations exist: `curl "http://localhost:37777/api/health"`
## Next Steps
- [Search Tools Usage](/usage/search-tools) - User guide with examples
- [Memory Search Usage](/usage/search-tools) - User guide with examples
- [Progressive Disclosure](/progressive-disclosure) - Philosophy behind 3-layer workflow
- [Worker Service Architecture](/architecture/worker-service) - HTTP API details
- [Database Schema](/architecture/database) - FTS5 tables and indexes
+191
View File
@@ -0,0 +1,191 @@
---
title: "Cursor + Gemini Setup"
description: "Use Claude-Mem in Cursor with Google's free Gemini API"
---
# Cursor + Gemini Setup
This guide walks you through setting up Claude-Mem in Cursor using Google's Gemini API. Gemini offers a generous free tier that handles typical individual usage.
<Info>
**Free Tier:** 1,500 requests per day with `gemini-2.5-flash-lite`. No credit card required.
</Info>
## Step 1: Get a Gemini API Key
1. Go to [Google AI Studio](https://aistudio.google.com/apikey)
2. Sign in with your Google account
3. Accept the Terms of Service
4. Click **Create API key**
5. Choose or create a Google Cloud project
6. Copy your API key - you'll need it in Step 3
<Tip>
**Higher rate limits:** Enable billing on your Google Cloud project to unlock 4,000 RPM (vs 10 RPM without billing). You won't be charged unless you exceed the free quota.
</Tip>
## Step 2: Clone and Build Claude-Mem
```bash
# Clone the repository
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem
# Install dependencies
bun install
# Build the project
bun run build
```
## Step 3: Configure Gemini Provider
### Option A: Interactive Setup (Recommended)
Run the setup wizard which guides you through everything:
```bash
bun run cursor:setup
```
The wizard will:
1. Detect you don't have Claude Code
2. Ask you to choose Gemini as your provider
3. Prompt for your API key
4. Install hooks automatically
5. Start the worker
### Option B: Manual Configuration
Create the settings file manually:
```bash
# Create settings directory
mkdir -p ~/.claude-mem
# Create settings file with Gemini configuration
cat > ~/.claude-mem/settings.json << 'EOF'
{
"CLAUDE_MEM_PROVIDER": "gemini",
"CLAUDE_MEM_GEMINI_API_KEY": "YOUR_GEMINI_API_KEY"
}
EOF
```
Replace `YOUR_GEMINI_API_KEY` with your actual API key.
Then install hooks and start the worker:
```bash
bun run cursor:install
bun run worker:start
```
## Step 4: Restart Cursor
Close and reopen Cursor IDE for the hooks to take effect.
## Step 5: Verify Installation
```bash
# Check worker is running
bun run worker:status
# Check hooks are installed
bun run cursor:status
```
Open http://localhost:37777 to see the memory viewer.
## Available Gemini Models
| Model | Free Tier RPM | Notes |
|-------|---------------|-------|
| `gemini-2.5-flash-lite` | 10 (4,000 with billing) | **Default.** Fastest, highest free tier RPM |
| `gemini-2.5-flash` | 5 (1,000 with billing) | Higher capability |
| `gemini-3-flash` | 5 (1,000 with billing) | Latest model |
To change the model, update your settings:
```json
{
"CLAUDE_MEM_PROVIDER": "gemini",
"CLAUDE_MEM_GEMINI_API_KEY": "your-key",
"CLAUDE_MEM_GEMINI_MODEL": "gemini-2.5-flash"
}
```
## Rate Limiting
Claude-mem automatically handles rate limiting for free tier usage:
- Requests are spaced to stay within limits
- Processing may be slightly slower but stays within quota
- No errors or lost observations
**To remove rate limiting:** Enable billing on your Google Cloud project, then add to settings:
```json
{
"CLAUDE_MEM_GEMINI_BILLING_ENABLED": true
}
```
You'll still use the free quota but with much higher rate limits.
## Troubleshooting
### "Gemini API key not configured"
Ensure your settings file exists and has the correct format:
```bash
cat ~/.claude-mem/settings.json
```
Should output something like:
```json
{
"CLAUDE_MEM_PROVIDER": "gemini",
"CLAUDE_MEM_GEMINI_API_KEY": "AIza..."
}
```
### Rate limit errors (HTTP 429)
You're exceeding the free tier limits. Options:
1. Wait a few minutes for the rate limit to reset
2. Enable billing on Google Cloud to unlock higher limits
3. Switch to OpenRouter for higher volume needs
### API key invalid
1. Verify your key at [Google AI Studio](https://aistudio.google.com/apikey)
2. Ensure there are no extra spaces or newlines in your settings.json
3. Try generating a new API key
### Worker not processing observations
Check the worker logs:
```bash
bun run worker:logs
```
Look for error messages related to Gemini API calls.
## Switching Providers Later
You can switch between Gemini, OpenRouter, and Claude SDK at any time by updating your settings. No restart required - changes take effect on the next observation.
```json
{
"CLAUDE_MEM_PROVIDER": "openrouter"
}
```
## Next Steps
- [Cursor Integration Overview](/cursor/index) - All Cursor features
- [OpenRouter Setup](/cursor/openrouter-setup) - Alternative provider with 100+ models
- [Configuration Reference](../configuration) - All settings options
+180
View File
@@ -0,0 +1,180 @@
---
title: "Cursor Integration"
description: "Persistent AI memory for Cursor IDE - free tier options available"
---
# Cursor Integration
> **Your AI stops forgetting. Give Cursor persistent memory.**
Every Cursor session starts fresh - your AI doesn't remember what it worked on yesterday. Claude-mem changes that. Your agent builds cumulative knowledge about your codebase, decisions, and patterns over time.
<CardGroup cols={2}>
<Card title="Free to Start" icon="dollar-sign">
Works with Gemini's free tier (1500 req/day) - no subscription required
</Card>
<Card title="Automatic Capture" icon="bolt">
MCP tools, shell commands, and file edits logged without effort
</Card>
<Card title="Smart Context" icon="brain">
Relevant history injected into every chat session
</Card>
<Card title="Works Everywhere" icon="check">
With or without Claude Code subscription
</Card>
</CardGroup>
<Info>
**No Claude Code subscription required.** Use Gemini (free tier) or OpenRouter as your AI provider.
</Info>
## How It Works
Claude-mem integrates with Cursor through native hooks:
1. **Session hooks** capture tool usage, file edits, and shell commands
2. **AI extraction** compresses observations into semantic summaries
3. **Context injection** loads relevant history into each new session
4. **Memory viewer** at http://localhost:37777 shows your knowledge base
## Installation Paths
Choose the installation method that fits your setup:
### Path A: Cursor-Only Users (No Claude Code)
If you're using Cursor without a Claude Code subscription:
```bash
# Clone and build
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem && bun install && bun run build
# Run interactive setup wizard
bun run cursor:setup
```
The setup wizard will:
- Detect you don't have Claude Code
- Help you choose and configure a free AI provider (Gemini recommended)
- Install hooks automatically
- Start the worker service
**Detailed guides:**
- [Gemini Setup](/cursor/gemini-setup) - Recommended free option (1500 req/day)
- [OpenRouter Setup](/cursor/openrouter-setup) - 100+ models including free options
### Path B: Claude Code Users
If you have Claude Code installed:
```bash
# Install the plugin (if not already)
/plugin marketplace add thedotmack/claude-mem
/plugin install claude-mem
# Install Cursor hooks
claude-mem cursor install
```
The plugin uses Claude's SDK by default but you can switch to Gemini or OpenRouter anytime.
## Prerequisites
<AccordionGroup>
<Accordion title="macOS">
- [Bun](https://bun.sh): `curl -fsSL https://bun.sh/install | bash`
- Cursor IDE
- jq and curl: `brew install jq curl`
</Accordion>
<Accordion title="Linux">
- [Bun](https://bun.sh): `curl -fsSL https://bun.sh/install | bash`
- Cursor IDE
- jq and curl: `apt install jq curl` or `dnf install jq curl`
</Accordion>
<Accordion title="Windows">
- [Bun](https://bun.sh): `powershell -c "irm bun.sh/install.ps1 | iex"`
- Cursor IDE
- PowerShell 5.1+ (included in Windows 10/11)
- Git for Windows
</Accordion>
</AccordionGroup>
## Quick Commands Reference
After installation, these commands are available from the claude-mem directory:
| Command | Description |
|---------|-------------|
| `bun run cursor:setup` | Interactive setup wizard |
| `bun run cursor:install` | Install Cursor hooks |
| `bun run cursor:uninstall` | Remove Cursor hooks |
| `bun run cursor:status` | Check hook installation status |
| `bun run worker:start` | Start the worker service |
| `bun run worker:stop` | Stop the worker service |
| `bun run worker:status` | Check worker status |
## Verifying Installation
After setup, verify everything is working:
1. **Check worker status:**
```bash
bun run worker:status
```
2. **Check hook installation:**
```bash
bun run cursor:status
```
3. **Open the memory viewer:**
Open http://localhost:37777 in your browser
4. **Restart Cursor** and start a coding session - you should see context being captured
## Provider Comparison
| Provider | Cost | Rate Limit | Best For |
|----------|------|------------|----------|
| Gemini | Free tier | 1500 req/day | Individual use, getting started |
| OpenRouter | Pay-per-use + free models | Varies by model | Model variety, high volume |
| Claude SDK | Included with Claude Code | Unlimited | Claude Code subscribers |
<Tip>
**Recommendation:** Start with Gemini's free tier. It handles typical individual usage well. Switch to OpenRouter or Claude SDK if you need higher limits.
</Tip>
## Troubleshooting
### Worker not starting
```bash
# Check if port is in use
lsof -i :37777
# Force restart
bun run worker:stop && bun run worker:start
# Check logs
bun run worker:logs
```
### Hooks not firing
1. Restart Cursor IDE after installation
2. Check hooks are installed: `bun run cursor:status`
3. Verify hooks.json exists in `.cursor/` directory
### No context appearing
1. Ensure worker is running: `bun run worker:status`
2. Check that you have observations: visit http://localhost:37777
3. Verify your API key is configured correctly
## Next Steps
- [Gemini Setup Guide](/cursor/gemini-setup) - Detailed free tier setup
- [OpenRouter Setup Guide](/cursor/openrouter-setup) - Configure OpenRouter
- [Configuration Reference](../configuration) - All settings options
- [Troubleshooting](../troubleshooting) - Common issues and solutions
+191
View File
@@ -0,0 +1,191 @@
---
title: "Cursor + OpenRouter Setup"
description: "Use Claude-Mem in Cursor with OpenRouter's 100+ AI models"
---
# Cursor + OpenRouter Setup
This guide walks you through setting up Claude-Mem in Cursor using OpenRouter. OpenRouter provides access to 100+ AI models from various providers, including several free options.
<Info>
**Model variety:** Access Claude, GPT-4, Gemini, Llama, Mistral, and many more through a single API key.
</Info>
## Step 1: Get an OpenRouter API Key
1. Go to [OpenRouter](https://openrouter.ai)
2. Sign up or sign in
3. Navigate to [API Keys](https://openrouter.ai/keys)
4. Click **Create Key**
5. Copy your API key - you'll need it in Step 3
<Tip>
**Free models available:** OpenRouter offers free versions of several models including Gemini Flash and others. Check the [model list](https://openrouter.ai/models?show_free=true) for current free options.
</Tip>
## Step 2: Clone and Build Claude-Mem
```bash
# Clone the repository
git clone https://github.com/thedotmack/claude-mem.git
cd claude-mem
# Install dependencies
bun install
# Build the project
bun run build
```
## Step 3: Configure OpenRouter Provider
### Option A: Interactive Setup (Recommended)
Run the setup wizard which guides you through everything:
```bash
bun run cursor:setup
```
When prompted for provider, select **OpenRouter**.
### Option B: Manual Configuration
Create the settings file manually:
```bash
# Create settings directory
mkdir -p ~/.claude-mem
# Create settings file with OpenRouter configuration
cat > ~/.claude-mem/settings.json << 'EOF'
{
"CLAUDE_MEM_PROVIDER": "openrouter",
"CLAUDE_MEM_OPENROUTER_API_KEY": "YOUR_OPENROUTER_API_KEY"
}
EOF
```
Replace `YOUR_OPENROUTER_API_KEY` with your actual API key.
Then install hooks and start the worker:
```bash
bun run cursor:install
bun run worker:start
```
## Step 4: Restart Cursor
Close and reopen Cursor IDE for the hooks to take effect.
## Step 5: Verify Installation
```bash
# Check worker is running
bun run worker:status
# Check hooks are installed
bun run cursor:status
```
Open http://localhost:37777 to see the memory viewer.
## Recommended Models
### Free Models
| Model | Provider | Notes |
|-------|----------|-------|
| `google/gemini-2.0-flash-exp:free` | Google | Fast, capable |
| `xiaomi/mimo-v2-flash:free` | Xiaomi | Good general purpose |
### Paid Models (Low Cost)
| Model | Approx. Cost | Notes |
|-------|--------------|-------|
| `anthropic/claude-3-haiku` | ~$0.25/1M tokens | Fast, efficient |
| `google/gemini-flash-1.5` | ~$0.075/1M tokens | Great value |
| `mistralai/mistral-7b-instruct` | ~$0.07/1M tokens | Budget option |
To specify a model, add to your settings:
```json
{
"CLAUDE_MEM_PROVIDER": "openrouter",
"CLAUDE_MEM_OPENROUTER_API_KEY": "your-key",
"CLAUDE_MEM_OPENROUTER_MODEL": "google/gemini-2.0-flash-exp:free"
}
```
## Cost Management
OpenRouter charges per token. To manage costs:
1. **Use free models:** Several high-quality free models are available
2. **Monitor usage:** Check your [OpenRouter dashboard](https://openrouter.ai/activity)
3. **Set spending limits:** Configure limits in OpenRouter settings
<Warning>
**Cost awareness:** Unlike Gemini's free tier, OpenRouter paid models charge per request. Monitor your usage if using paid models.
</Warning>
## Troubleshooting
### "OpenRouter API key not configured"
Ensure your settings file exists with the correct format:
```bash
cat ~/.claude-mem/settings.json
```
Should output something like:
```json
{
"CLAUDE_MEM_PROVIDER": "openrouter",
"CLAUDE_MEM_OPENROUTER_API_KEY": "sk-or-..."
}
```
### Model not found
1. Check the model ID is correct at [OpenRouter Models](https://openrouter.ai/models)
2. Some models may require payment - check if you have credits
3. Free models have `:free` suffix in their ID
### Rate limits
OpenRouter rate limits vary by model and your account tier. If you hit limits:
1. Wait briefly and retry
2. Consider upgrading your OpenRouter account tier
3. Switch to a less popular model
### API errors
Check the worker logs for details:
```bash
bun run worker:logs
```
Common issues:
- Invalid API key (regenerate at OpenRouter)
- Insufficient credits for paid models
- Model temporarily unavailable
## Switching Providers Later
You can switch between OpenRouter, Gemini, and Claude SDK at any time by updating your settings. No restart required - changes take effect on the next observation.
```json
{
"CLAUDE_MEM_PROVIDER": "gemini"
}
```
## Next Steps
- [Cursor Integration Overview](/cursor/index) - All Cursor features
- [Gemini Setup](/cursor/gemini-setup) - Alternative free provider
- [Configuration Reference](../configuration) - All settings options
+9
View File
@@ -47,6 +47,15 @@
"endless-mode"
]
},
{
"group": "Cursor Integration",
"icon": "wand-magic-sparkles",
"pages": [
"cursor/index",
"cursor/gemini-setup",
"cursor/openrouter-setup"
]
},
{
"group": "Best Practices",
"icon": "lightbulb",
+55 -38
View File
@@ -260,14 +260,12 @@ The index is useless without retrieval mechanisms:
*Use claude-mem MCP search to access records with the given ID*
```
**Available tools:**
- `search_observations` - Full-text search
- `find_by_concept` - Concept-based retrieval
- `find_by_file` - File-based retrieval
- `find_by_type` - Type-based retrieval
- `get_recent_context` - Recent session summaries
**Available MCP tools:**
- `search` - Search memory index (Layer 1: Get IDs)
- `timeline` - Get chronological context (Layer 2: See narrative arc)
- `get_observations` - Fetch full details (Layer 3: Deep dive)
Each tool supports `format: "index"` (default) and `format: "full"`.
The 3-layer workflow ensures progressive disclosure: index → context → details.
---
@@ -318,16 +316,18 @@ Is my task related to npm? → YES
---
## The Two-Tier Search Strategy
## The Three-Layer Workflow
Claude-Mem implements progressive disclosure in search results too:
Claude-Mem implements progressive disclosure through a 3-layer workflow pattern:
### Tier 1: Index Format (Default)
### Layer 1: Search (Index)
Start by searching to get a compact index with IDs:
```typescript
search_observations({
search({
query: "hook timeout",
format: "index" // Default
limit: 10
})
```
@@ -335,23 +335,40 @@ search_observations({
```
Found 3 observations matching "hook timeout":
| ID | Date | Type | Title | Tokens |
|----|------|------|-------|--------|
| #2543 | Oct 26 | gotcha | Hook timeout: 60s too short | ~155 |
| #2891 | Oct 25 | how-it-works | Hook timeout configuration | ~203 |
| #2102 | Oct 20 | problem-solution | Fixed timeout in CI | ~89 |
| ID | Date | Type | Title |
|----|------|------|-------|
| #2543 | Oct 26 | gotcha | Hook timeout: 60s too short |
| #2891 | Oct 25 | how-it-works | Hook timeout configuration |
| #2102 | Oct 20 | problem-solution | Fixed timeout in CI |
```
**Cost:** ~100 tokens for 3 results
**Value:** Agent can scan and decide which to fetch
**Cost:** ~50-100 tokens per result
**Value:** Agent can scan and decide which observations are relevant
### Tier 2: Full Format (On-Demand)
### Layer 2: Timeline (Context)
Get chronological context around interesting observations:
```typescript
search_observations({
query: "hook timeout",
format: "full",
limit: 1 // Fetch just the most relevant
timeline({
anchor: 2543, // Observation ID from search
depth_before: 3,
depth_after: 3
})
```
**Returns:** Chronological view showing what happened before/during/after observation #2543
**Cost:** Variable based on depth
**Value:** Understand narrative arc and context
### Layer 3: Get Observations (Details)
Fetch full details only for relevant observations:
```typescript
get_observations({
ids: [2543, 2102] // Selected from search results
})
```
@@ -463,29 +480,30 @@ Here are 10 observations.
*Use MCP search tools to fetch full observation details on-demand*
```
### ❌ Defaulting to Full Format
### ❌ Skipping the Index Layer
**Bad:**
```typescript
search_observations({
query: "hooks",
format: "full" // Fetches everything
// Fetching full details immediately
get_observations({
ids: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] // Guessing which are relevant
})
```
**Good:**
```typescript
search_observations({
// Follow the 3-layer workflow
// Layer 1: Search for index
search({
query: "hooks",
format: "index", // Scan first
limit: 20
})
// Then, if needed:
search_observations({
query: "hooks",
format: "full",
limit: 1 // Just the most relevant
// Layer 2: Review index, identify 2-3 relevant IDs
// Layer 3: Fetch only relevant observations
get_observations({
ids: [2543, 2891] // Just the most relevant
})
```
@@ -595,10 +613,9 @@ SessionStart({ source: "compact" }):
```typescript
// Use embeddings to pre-sort index by relevance
search_observations({
search({
query: "authentication bug",
format: "index",
sort: "relevance" // Based on semantic similarity
orderBy: "relevance" // Based on semantic similarity (future enhancement)
})
```
+24 -12
View File
@@ -742,17 +742,17 @@ sqlite3 ~/.claude-mem/claude-mem.db "
3. Test simple query:
```bash
# In Claude Code
search_observations with query="test"
# Test MCP search tool
search(query="test", limit=5)
```
4. Check query syntax:
```bash
# Bad: Special characters
search_observations with query="[test]"
# Bad: Special characters may cause issues
search(query="[test]")
# Good: Simple words
search_observations with query="test"
search(query="test")
```
### Token Limit Errors
@@ -761,28 +761,40 @@ sqlite3 ~/.claude-mem/claude-mem.db "
**Solutions**:
1. Use index format:
1. Follow 3-layer workflow (don't skip to get_observations):
```bash
search_observations with query="..." and format="index"
# Start with search to get index
search(query="...", limit=10)
# Review IDs, then fetch only relevant ones
get_observations(ids=[<2-3 relevant IDs>])
```
2. Reduce limit:
2. Reduce limit in search:
```bash
search_observations with query="..." and limit=3
search(query="...", limit=3)
```
3. Use filters to narrow results:
```bash
search_observations with query="..." and type="decision" and limit=5
search(query="...", type="decision", limit=5)
```
4. Paginate results:
```bash
# First page
search_observations with query="..." and limit=5 and offset=0
search(query="...", limit=5, offset=0)
# Second page
search_observations with query="..." and limit=5 and offset=5
search(query="...", limit=5, offset=5)
```
5. Batch IDs in get_observations:
```bash
# Always batch multiple IDs in one call
get_observations(ids=[123, 456, 789])
# Don't make separate calls per ID
```
## Performance Issues
+3 -3
View File
@@ -167,6 +167,6 @@ If observations seem lower quality with Gemini:
## Next Steps
- [Configuration](../configuration) - Full settings reference
- [Getting Started](getting-started) - Basic usage guide
- [Troubleshooting](../troubleshooting) - Common issues
- [Configuration](/configuration) - Full settings reference
- [Getting Started](/usage/getting-started) - Basic usage guide
- [Troubleshooting](/troubleshooting) - Common issues
+4 -4
View File
@@ -314,7 +314,7 @@ Content-Type: application/json
## Next Steps
- [Configuration](../configuration) - Full settings reference
- [Gemini Provider](gemini-provider) - Alternative free provider
- [Getting Started](getting-started) - Basic usage guide
- [Troubleshooting](../troubleshooting) - Common issues
- [Configuration](/configuration) - Full settings reference
- [Gemini Provider](/usage/gemini-provider) - Alternative free provider
- [Getting Started](/usage/getting-started) - Basic usage guide
- [Troubleshooting](/troubleshooting) - Common issues
+357 -306
View File
@@ -1,403 +1,454 @@
---
title: "mem-search Skill"
description: "Query your project history with natural language"
title: "Memory Search"
description: "Search your project history with MCP tools"
---
# mem-search Skill Usage
# Memory Search with MCP Tools
Once claude-mem is installed as a plugin, you can search your project history using natural language. Claude automatically invokes the mem-search skill when you ask about past work.
Claude-mem provides persistent memory across sessions through **4 MCP tools** that follow a token-efficient **3-layer workflow pattern**.
## How It Works
## Overview
**v5.5.0 Enhancement**: The search skill was renamed to "mem-search" for better scope differentiation, with effectiveness increased from 67% to 100% and enhanced concrete triggers (85% vs 44%).
Instead of fetching all historical data upfront (expensive), claude-mem uses a progressive disclosure approach:
**v5.4.0 Architecture**: Claude-Mem uses a skill-based search architecture instead of MCP tools, saving ~2,250 tokens per session start through progressive disclosure.
1. **Search** → Get a compact index with IDs (~50-100 tokens/result)
2. **Timeline** → Get context around interesting results
3. **Get Observations** → Fetch full details ONLY for filtered IDs
**Simple Usage:**
- Just ask naturally: *"What did we do last session?"*
- Claude recognizes the intent and invokes the mem-search skill
- The skill uses HTTP API endpoints to query your memory
- Results are formatted and presented to you
This achieves **~10x token savings** compared to traditional RAG approaches.
**Benefits:**
- **Token Efficient**: ~250 tokens (skill frontmatter) vs ~2,500 tokens (MCP tool definitions)
- **Natural Language**: No need to learn specific tool syntax
- **Progressive Disclosure**: Only loads detailed instructions when needed
- **Auto-Invoked**: Claude knows when to search based on your questions
- **Scope Differentiation**: "mem-search" clearly distinguishes from native conversation memory
## The 3-Layer Workflow
## Quick Reference
### Layer 1: Search (Index)
| Operation | Purpose |
|-------------------------|----------------------------------------------|
| Search Observations | Full-text search across observations |
| Search Sessions | Full-text search across session summaries |
| Search Prompts | Full-text search across raw user prompts |
| By Concept | Find observations tagged with concepts |
| By File | Find observations referencing files |
| By Type | Find observations by type |
| Recent Context | Get recent session context |
| Timeline | Get unified timeline around a specific point |
| Timeline by Query | Search and get timeline context in one step |
| API Help | Get search API documentation |
## Example Queries
### Natural Language Queries
**Search Observations:**
```
"What bugs did we fix related to authentication?"
"Show me all decisions about the build system"
"Find refactoring work on the database"
```
**Search Sessions:**
```
"What did we learn about hooks?"
"What was accomplished in the API implementation?"
"Show me recent work on this project"
```
**Search Prompts:**
```
"When did I ask about authentication features?"
"Find all my requests about dark mode"
```
**Note**: Claude automatically translates your natural language queries into the appropriate search operations.
### Search by File
Start by searching to get a lightweight index of results:
```
"Show me everything related to worker-service.ts"
"What changes were made to migrations.ts?"
"Find all work on the database file"
search(query="authentication bug", type="bugfix", limit=10)
```
### Search by Concept
**Returns:** Compact table with IDs, titles, dates, types
**Cost:** ~50-100 tokens per result
**Purpose:** Survey what exists before fetching details
### Layer 2: Timeline (Context)
Get chronological context around specific observations:
```
"Show observations tagged with architecture"
"Find all security-related observations"
"What patterns have we used?"
timeline(anchor=<observation_id>, depth_before=3, depth_after=3)
```
### Search by Type
Or search and get timeline in one step:
```
"Find all feature implementations"
"Show me all decisions and discoveries"
"What bugs have we fixed?"
timeline(query="authentication", depth_before=2, depth_after=2)
```
### Recent Context
**Returns:** Chronological view showing what was happening before/after
**Cost:** Variable, depends on depth
**Purpose:** Understand narrative arc and context
### Layer 3: Get Observations (Details)
Fetch full details only for relevant observations:
```
"Show me what we've been working on"
"Get context from the last 5 sessions"
"What happened recently on this project?"
get_observations(ids=[123, 456, 789])
```
### Timeline Queries
**Returns:** Complete observation details (narrative, facts, files, concepts)
**Cost:** ~500-1000 tokens per observation
**Purpose:** Deep dive on specific, validated items
**Get timeline around a specific point:**
### Why This Works
**Traditional Approach:**
- Fetch everything upfront: 20,000 tokens
- Relevance: ~10% (2,000 tokens actually useful)
- Waste: 18,000 tokens on irrelevant context
**3-Layer Approach:**
- Search index: 1,000 tokens (10 results)
- Timeline context: 500 tokens (around 2 key results)
- Fetch details: 1,500 tokens (3 observations)
- **Total: 3,000 tokens, 100% relevant**
## Available Tools
### `__IMPORTANT` - Workflow Documentation
Always visible reminder of the 3-layer workflow pattern. Helps Claude understand how to use the search tools efficiently.
**Usage:** Automatically shown, no need to invoke
### `search` - Search Memory Index
Search your memory and get a compact index with IDs.
**Parameters:**
- `query` - Full-text search query (supports AND, OR, NOT, phrase searches)
- `limit` - Maximum results (default: 20)
- `offset` - Skip first N results for pagination
- `type` - Filter by observation type (bugfix, feature, decision, discovery, refactor, change)
- `obs_type` - Filter by record type (observation, session, prompt)
- `project` - Filter by project name
- `dateStart` - Filter by start date (YYYY-MM-DD)
- `dateEnd` - Filter by end date (YYYY-MM-DD)
- `orderBy` - Sort order (date_desc, date_asc, relevance)
**Returns:** Compact index table with IDs, titles, dates, types
**Example:**
```
"What was happening when we implemented authentication?"
"Show me the context around that bug fix"
"What led to the decision to refactor the database?"
search(query="database migration", type="bugfix", limit=5, orderBy="date_desc")
```
**Timeline by query:**
### `timeline` - Get Chronological Context
Get a chronological view of observations around a specific point or query.
**Parameters:**
- `anchor` - Observation ID to center timeline around (optional if query provided)
- `query` - Search query to find anchor automatically (optional if anchor provided)
- `depth_before` - Number of observations before anchor (default: 3)
- `depth_after` - Number of observations after anchor (default: 3)
- `project` - Filter by project name
**Returns:** Chronological list showing what happened before/during/after
**Example:**
```
"Find when we added the viewer UI and show what happened around that time"
"Search for authentication work and show the timeline"
timeline(anchor=12345, depth_before=5, depth_after=5)
```
**Benefits:**
- See the complete narrative arc around key events
- All record types (observations, sessions, prompts) in chronological view
- Understand what was happening before and after important changes
## Search Strategy
The mem-search skill uses a progressive disclosure pattern to efficiently retrieve information:
### 1. Ask Naturally
Start with a natural language question:
Or search-based:
```
"What bugs did we fix related to authentication?"
timeline(query="implemented JWT auth", depth_before=3, depth_after=3)
```
### 2. Claude Invokes mem-search Skill
### `get_observations` - Fetch Full Details
Claude recognizes your intent and loads the mem-search skill (~250 tokens for skill frontmatter).
Fetch complete observation details by IDs. **Always batch multiple IDs in a single call for efficiency.**
### 3. Skill Uses HTTP API
**Parameters:**
- `ids` - Array of observation IDs (required)
- `orderBy` - Sort order (date_desc, date_asc)
- `limit` - Maximum observations to return
- `project` - Filter by project name
The skill calls the appropriate HTTP endpoint (e.g., `/api/search/observations`) with the query.
**Returns:** Complete observation details including narrative, facts, files, concepts
### 4. Results Formatted
Results are formatted and presented to you, usually starting with an index/summary format.
### 5. Deep Dive if Needed
If you need more details, ask follow-up questions:
**Example:**
```
"Tell me more about observation #123"
"Show me the full details of that decision"
get_observations(ids=[123, 456, 789, 1011])
```
**Benefits of This Approach:**
- **Token Efficient**: Only loads what you need, when you need it
- **Natural**: No syntax to learn
- **Progressive**: Start with overview, drill down as needed
- **Automatic**: Claude handles the search invocation
**Important:** Always batch IDs instead of making separate calls per observation.
## Common Use Cases
### Debugging Issues
**Scenario:** Find what went wrong with database connections
```
Step 1: search(query="error database connection", type="bugfix", limit=10)
→ Review index, identify observations #245, #312, #489
Step 2: timeline(anchor=312, depth_before=3, depth_after=3)
→ See what was happening around the fix
Step 3: get_observations(ids=[312, 489])
→ Get full details on relevant fixes
```
### Understanding Decisions
**Scenario:** Review architectural choices about authentication
```
Step 1: search(query="authentication", type="decision", limit=5)
→ Find decision observations
Step 2: get_observations(ids=[<relevant_ids>])
→ Get full decision rationale, trade-offs, facts
```
### Code Archaeology
**Scenario:** Find when a specific file was modified
```
Step 1: search(query="worker-service.ts", limit=20)
→ Get all observations mentioning that file
Step 2: timeline(query="worker-service.ts refactor", depth_before=2, depth_after=2)
→ See what led to and followed from the refactor
Step 3: get_observations(ids=[<specific_observation_ids>])
→ Get implementation details
```
### Feature History
**Scenario:** Track how a feature evolved
```
Step 1: search(query="dark mode", type="feature", orderBy="date_asc")
→ Chronological view of feature work
Step 2: timeline(anchor=<first_observation_id>, depth_after=10)
→ See the full development timeline
Step 3: get_observations(ids=[<key_milestones>])
→ Deep dive on critical implementation points
```
### Learning from Past Work
**Scenario:** Review refactoring patterns
```
Step 1: search(type="refactor", limit=10, orderBy="date_desc")
→ Recent refactoring work
Step 2: get_observations(ids=[<interesting_ids>])
→ Study the patterns and approaches used
```
### Context Recovery
**Scenario:** Restore context after time away from project
```
Step 1: search(query="project-name", limit=10, orderBy="date_desc")
→ See recent work
Step 2: timeline(anchor=<most_recent_id>, depth_before=10)
→ Understand what led to current state
Step 3: get_observations(ids=[<critical_observations>])
→ Refresh memory on key decisions
```
## Search Query Syntax
The `query` parameter supports SQLite FTS5 full-text search syntax:
### Boolean Operators
```
query="authentication AND JWT" # Both terms must appear
query="OAuth OR JWT" # Either term can appear
query="security NOT deprecated" # Exclude deprecated items
```
### Phrase Searches
```
query='"database migration"' # Exact phrase match
```
### Column-Specific Searches
```
query="title:authentication" # Search in title only
query="content:database" # Search in content only
query="concepts:security" # Search in concepts only
```
### Combining Operators
```
query='"user auth" AND (JWT OR session) NOT deprecated'
```
## Token Management
### Token Efficiency Best Practices
1. **Always start with search** - Get index first (~50-100 tokens/result)
2. **Use small limits** - Start with 3-5 results, increase if needed
3. **Filter before fetching** - Use type, date, project filters
4. **Batch get_observations** - Always group multiple IDs in one call
5. **Use timeline strategically** - Get context only when narrative matters
### Token Cost Estimates
| Operation | Tokens per Result |
|-----------|-------------------|
| search (index) | 50-100 |
| timeline (per observation) | 100-200 |
| get_observations (full details) | 500-1,000 |
**Example Comparison:**
**Inefficient:**
```
# Fetching 20 full observations upfront: 10,000-20,000 tokens
get_observations(ids=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
```
**Efficient:**
```
# Search index: ~1,000 tokens
search(query="bug fix", limit=20)
# Review IDs, identify 3 relevant observations
# Fetch only relevant: ~1,500-3,000 tokens
get_observations(ids=[5, 12, 18])
# Total: 2,500-4,000 tokens (vs 10,000-20,000)
```
## Advanced Filtering
You can refine searches using natural language filters:
### Date Ranges
```
"What bugs did we fix in October?"
"Show me work from last week"
"Find decisions made between October 1-31"
search(
query="performance optimization",
dateStart="2025-10-01",
dateEnd="2025-10-31"
)
```
### Multiple Types
```
"Show me all decisions and features"
"Find bugfixes and refactorings"
```
### Concepts
For observations of multiple types, make multiple searches or use broader query:
```
"Find database work related to architecture and performance"
"Show security observations"
search(query="database", type="bugfix", limit=10)
search(query="database", type="feature", limit=10)
```
### File-Specific
### Project-Specific
```
"Show refactoring work that touched worker-service.ts"
"Find changes to auth files"
search(query="API", project="my-app", limit=15)
```
### Project Filtering
### Pagination
```
"Show authentication work on my-app project"
"What have we done on this codebase?"
# First page
search(query="refactor", limit=10, offset=0)
# Second page
search(query="refactor", limit=10, offset=10)
# Third page
search(query="refactor", limit=10, offset=20)
```
**Note**: Claude translates your natural language into the appropriate API filters automatically.
## Under the Hood: HTTP API
The mem-search skill uses HTTP endpoints on the worker service (port 37777):
- `GET /api/search/observations` - Full-text search observations
- `GET /api/search/sessions` - Full-text search session summaries
- `GET /api/search/prompts` - Full-text search user prompts
- `GET /api/search/by-concept` - Find observations by concept tag
- `GET /api/search/by-file` - Find work related to specific files
- `GET /api/search/by-type` - Find observations by type
- `GET /api/context/recent` - Get recent session context
- `GET /api/context/timeline` - Get timeline around specific point
- `GET /api/timeline/by-query` - Search + timeline in one call
- `GET /api/search/help` - API documentation
These endpoints use FTS5 full-text search with support for:
- Boolean operators (AND, OR, NOT)
- Phrase searches
- Column-specific searches
- Date range filtering
- Project filtering
## Result Metadata
All results include rich metadata:
All observations include rich metadata:
```
## JWT authentication decision
**Type**: decision
**Date**: 2025-10-21 14:23:45
**Concepts**: authentication, security, architecture
**Files Read**: src/auth/middleware.ts, src/utils/jwt.ts
**Files Modified**: src/auth/jwt-strategy.ts
**Narrative**:
Decided to implement JWT-based authentication instead of session-based
authentication for better scalability and stateless design...
**Facts**:
• JWT tokens expire after 1 hour
• Refresh tokens stored in httpOnly cookies
• Token signing uses RS256 algorithm
• Public keys rotated every 30 days
```
## Citations
All search results include observation IDs that can be accessed via the HTTP API:
- `http://localhost:37777/api/observation/{id}` - Get specific observation by ID
- View all observations in the web viewer at `http://localhost:37777`
These citations enable referencing specific historical context in your work.
## Token Management
### Token Efficiency Tips
1. **Start with index format**: ~50-100 tokens per result
2. **Use small limits**: Start with 3-5 results
3. **Apply filters**: Narrow results before searching
4. **Paginate**: Use offset to browse results in batches
### Token Estimates
| Format | Tokens per Result |
|--------|-------------------|
| Index | 50-100 |
| Full | 500-1000 |
**Example**:
- 20 results in index format: ~1,000-2,000 tokens
- 20 results in full format: ~10,000-20,000 tokens
## Common Use Cases
### 1. Debugging Issues
Find what went wrong:
```
search_observations with query="error database connection" and type="bugfix"
```
### 2. Understanding Decisions
Review architectural choices:
```
find_by_type with type="decision" and format="index"
```
Then deep dive on specific decisions:
```
search_observations with query="[DECISION TITLE]" and format="full"
```
### 3. Code Archaeology
Find when a file was modified:
```
find_by_file with filePath="worker-service.ts"
```
### 4. Feature History
Track feature development:
```
search_sessions with query="authentication feature"
search_user_prompts with query="add authentication"
```
### 5. Learning from Past Work
Review refactoring patterns:
```
find_by_type with type="refactor" and limit=10
```
### 6. Context Recovery
Restore context after time away:
```
get_recent_context with limit=5
search_sessions with query="[YOUR PROJECT NAME]" and orderBy="date_desc"
```
## Best Practices
1. **Index first, full later**: Always start with index format
2. **Small limits**: Start with 3-5 results to avoid token limits
3. **Use filters**: Narrow results before searching
4. **Specific queries**: More specific = better results
5. **Review citations**: Use citations to reference past decisions
6. **Date filtering**: Use date ranges for time-based searches
7. **Type filtering**: Use types to categorize searches
8. **Concept tags**: Use concepts for thematic searches
- **ID** - Unique observation identifier
- **Type** - bugfix, feature, decision, discovery, refactor, change
- **Date** - When the work occurred
- **Title** - Concise description
- **Concepts** - Tagged themes (e.g., security, performance, architecture)
- **Files Read** - Files examined during work
- **Files Modified** - Files changed during work
- **Narrative** - Story of what happened and why
- **Facts** - Key factual points (decisions made, patterns used, metrics)
## Troubleshooting
### No Results Found
1. Check database has data:
1. **Broaden your search:**
```
# Too specific
search(query="JWT authentication implementation with RS256")
# Better
search(query="authentication")
```
2. **Check database has data:**
```bash
sqlite3 ~/.claude-mem/claude-mem.db "SELECT COUNT(*) FROM observations;"
curl "http://localhost:37777/api/search?query=test"
```
2. Try broader natural language query:
3. **Try without filters:**
```
"Show me anything about authentication" # Broader
vs
"Find exact JWT authentication implementation" # Too specific
# Remove type/date filters to see if data exists
search(query="your-search-term")
```
3. Ask without filters first:
```
"What do we have about auth?"
# Then narrow down
"Show me auth-related decisions"
```
### IDs Not Found in get_observations
### Worker Service Not Running
**Error:** "Observation IDs not found: [123, 456]"
If search isn't working, check the worker service:
**Causes:**
- IDs from different project (use `project` parameter)
- IDs were deleted
- Typo in ID numbers
```bash
npm run worker:status # Check worker status
npm run worker:restart # Restart if needed
npm run worker:logs # View logs
**Solution:**
```
# Verify IDs exist
search(query="<related-search>")
# Use correct project filter
get_observations(ids=[123, 456], project="correct-project-name")
```
Or describe the issue to Claude and the troubleshoot skill will automatically activate to provide diagnosis.
### Token Limit Errors
### Performance Issues
**Error:** Response exceeds token limits
**Solution:** Use the 3-layer workflow to reduce upfront costs:
```
# Instead of fetching 50 full observations:
# get_observations(ids=[1,2,3,...,50]) # 25,000-50,000 tokens!
# Do this:
search(query="<your-query>", limit=50) # ~2,500-5,000 tokens
# Review index, identify 5 relevant observations
get_observations(ids=[<5-most-relevant>]) # ~2,500-5,000 tokens
# Total: 5,000-10,000 tokens (50-80% savings)
```
### Search Performance
If searches seem slow:
1. Be more specific in your queries
2. Ask for recent work (naturally filters by date)
3. Specify the project you're interested in
4. Ask for fewer results initially
1. Be more specific in queries (helps FTS5 index)
2. Use date range filters to narrow scope
3. Specify project filter when possible
4. Use smaller limit values
## Best Practices
1. **Index First, Details Later** - Always start with search to survey options
2. **Filter Before Fetching** - Use search parameters to narrow results
3. **Batch ID Fetches** - Group multiple IDs in one get_observations call
4. **Use Timeline for Context** - When narrative matters, timeline shows the story
5. **Specific Queries** - More specific = better relevance
6. **Small Limits Initially** - Start with 3-5 results, expand if needed
7. **Review Before Deep Dive** - Check index before fetching full details
## Technical Details
**Architecture Change (v5.4.0)**:
- **Before**: 9 MCP tools (~2,500 tokens in tool definitions per session start)
- **After**: 1 mem-search skill (~250 tokens in frontmatter, full instructions loaded on-demand)
- **Savings**: ~2,250 tokens per session start
- **Migration**: Transparent - users don't need to change how they ask questions
**Architecture:** MCP tools are a thin wrapper over the Worker HTTP API (localhost:37777). The MCP server translates tool calls into HTTP requests to the worker service, which handles all business logic, database queries, and Chroma vector search.
**v5.5.0 Enhancement**: Renamed from "search" to "mem-search" with improved effectiveness (67% → 100%) and enhanced triggers (44% → 85%).
**MCP Server:** Located at `~/.claude/plugins/marketplaces/thedotmack/plugin/scripts/mcp-server.cjs`
**How the Skill Works:**
1. User asks a question about past work
2. Claude recognizes the intent matches the mem-search skill description
3. Skill loads full instructions from `plugin/skills/mem-search/SKILL.md`
4. Skill uses `curl` to call HTTP API endpoints
5. Results formatted and returned to Claude
6. Claude presents results to user
**Worker Service:** Express API on port 37777, managed by Bun
**Database:** SQLite FTS5 full-text search on `~/.claude-mem/claude-mem.db`
**Vector Search:** Chroma embeddings for semantic search (underlying implementation)
## Next Steps
- [Progressive Disclosure](/progressive-disclosure) - Philosophy behind 3-layer workflow
- [Architecture Overview](/architecture/overview) - System components
- [Database Schema](/architecture/database) - Understanding the data
- [Getting Started](/usage/getting-started) - Automatic operation
- [Database Schema](/architecture/database) - Understanding the data structure
- [Claude Desktop Setup](/usage/claude-desktop) - Installation and configuration
+10 -6
View File
@@ -1,6 +1,6 @@
{
"name": "claude-mem",
"version": "8.2.7",
"version": "8.5.0",
"description": "Memory compression system for Claude Code - persist context across sessions",
"keywords": [
"claude",
@@ -41,10 +41,10 @@
"worker:tail": "tail -f 50 ~/.claude-mem/logs/worker-$(date +%Y-%m-%d).log",
"changelog:generate": "node scripts/generate-changelog.js",
"discord:notify": "node scripts/discord-release-notify.js",
"worker:start": "bun plugin/scripts/worker-cli.js start",
"worker:stop": "bun plugin/scripts/worker-cli.js stop",
"worker:restart": "bun plugin/scripts/worker-cli.js restart",
"worker:status": "bun plugin/scripts/worker-cli.js status",
"worker:start": "bun plugin/scripts/worker-service.cjs start",
"worker:stop": "bun plugin/scripts/worker-service.cjs stop",
"worker:restart": "bun plugin/scripts/worker-service.cjs restart",
"worker:status": "bun plugin/scripts/worker-service.cjs status",
"queue:check": "bun scripts/check-pending-queue.ts",
"queue:process": "bun scripts/check-pending-queue.ts --process",
"translate-readme": "bun scripts/translate-readme/cli.ts -v -o docs/i18n README.md",
@@ -53,7 +53,11 @@
"translate:tier3": "npm run translate-readme -- vi id th hi bn ro sv",
"translate:tier4": "npm run translate-readme -- it el hu fi da no",
"translate:all": "npm run translate:tier1 & npm run translate:tier2 & npm run translate:tier3 & npm run translate:tier4 & wait",
"bug-report": "npx tsx scripts/bug-report/cli.ts"
"bug-report": "npx tsx scripts/bug-report/cli.ts",
"cursor:install": "bun plugin/scripts/worker-service.cjs cursor install",
"cursor:uninstall": "bun plugin/scripts/worker-service.cjs cursor uninstall",
"cursor:status": "bun plugin/scripts/worker-service.cjs cursor status",
"cursor:setup": "bun plugin/scripts/worker-service.cjs cursor setup"
},
"dependencies": {
"@anthropic-ai/claude-agent-sdk": "^0.1.76",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "claude-mem",
"version": "8.2.7",
"version": "8.5.0",
"description": "Persistent memory system for Claude Code - seamlessly preserve context across sessions",
"author": {
"name": "Alex Newman"
+7 -7
View File
@@ -13,17 +13,17 @@
{
"type": "command",
"command": "bun \"${CLAUDE_PLUGIN_ROOT}/scripts/worker-service.cjs\" start",
"timeout": 180
"timeout": 15
},
{
"type": "command",
"command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/context-hook.js\"",
"timeout": 300
"timeout": 15
},
{
"type": "command",
"command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/user-message-hook.js\"",
"timeout": 10
"timeout": 15
}
]
}
@@ -34,12 +34,12 @@
{
"type": "command",
"command": "bun \"${CLAUDE_PLUGIN_ROOT}/scripts/worker-service.cjs\" start",
"timeout": 180
"timeout": 15
},
{
"type": "command",
"command": "node \"${CLAUDE_PLUGIN_ROOT}/scripts/new-hook.js\"",
"timeout": 300
"timeout": 15
}
]
}
@@ -51,7 +51,7 @@
{
"type": "command",
"command": "bun \"${CLAUDE_PLUGIN_ROOT}/scripts/worker-service.cjs\" start",
"timeout": 180
"timeout": 15
},
{
"type": "command",
@@ -67,7 +67,7 @@
{
"type": "command",
"command": "bun \"${CLAUDE_PLUGIN_ROOT}/scripts/worker-service.cjs\" start",
"timeout": 180
"timeout": 15
},
{
"type": "command",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "claude-mem-plugin",
"version": "8.2.7",
"version": "8.5.0",
"private": true,
"description": "Runtime dependencies for claude-mem bundled hooks",
"type": "module",
+7 -7
View File
@@ -1,13 +1,13 @@
#!/usr/bin/env bun
import{stdin as L}from"process";import A from"path";import{homedir as K}from"os";import{readFileSync as X}from"fs";import{readFileSync as v,writeFileSync as w,existsSync as F}from"fs";import{join as W}from"path";import{homedir as x}from"os";var U="bugfix,feature,refactor,discovery,decision,change",R="how-it-works,why-it-exists,what-changed,problem-solution,gotcha,pattern,trade-off";var a=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-5",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:"37777",CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:W(x(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_OBSERVATION_TYPES:U,CLAUDE_MEM_CONTEXT_OBSERVATION_CONCEPTS:R,CLAUDE_MEM_CONTEXT_FULL_COUNT:"5",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false"};static getAllDefaults(){return{...this.DEFAULTS}}static get(t){return this.DEFAULTS[t]}static getInt(t){let r=this.get(t);return parseInt(r,10)}static getBool(t){return this.get(t)==="true"}static loadFromFile(t){try{if(!F(t))return this.getAllDefaults();let r=v(t,"utf-8"),e=JSON.parse(r),n=e;if(e.env&&typeof e.env=="object"){n=e.env;try{w(t,JSON.stringify(n,null,2),"utf-8"),_.info("SETTINGS","Migrated settings file from nested to flat schema",{settingsPath:t})}catch(s){_.warn("SETTINGS","Failed to auto-migrate settings file",{settingsPath:t},s)}}let o={...this.DEFAULTS};for(let s of Object.keys(this.DEFAULTS))n[s]!==void 0&&(o[s]=n[s]);return o}catch(r){return _.warn("SETTINGS","Failed to load settings, using defaults",{settingsPath:t},r),this.getAllDefaults()}}};import{appendFileSync as b,existsSync as H,mkdirSync as G}from"fs";import{join as S}from"path";var f=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))(f||{}),p=class{level=null;useColor;logFilePath=null;constructor(){this.useColor=process.stdout.isTTY??!1,this.initializeLogFile()}initializeLogFile(){try{let t=a.get("CLAUDE_MEM_DATA_DIR"),r=S(t,"logs");H(r)||G(r,{recursive:!0});let e=new Date().toISOString().split("T")[0];this.logFilePath=S(r,`claude-mem-${e}.log`)}catch(t){console.error("[LOGGER] Failed to initialize log file:",t),this.logFilePath=null}}getLevel(){if(this.level===null)try{let t=a.get("CLAUDE_MEM_DATA_DIR"),r=S(t,"settings.json"),n=a.loadFromFile(r).CLAUDE_MEM_LOG_LEVEL.toUpperCase();this.level=f[n]??1}catch(t){console.error("[LOGGER] Failed to load settings, using INFO level:",t),this.level=1}return this.level}correlationId(t,r){return`obs-${t}-${r}`}sessionId(t){return`session-${t}`}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?JSON.stringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=typeof r=="string"?JSON.parse(r):r;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),o=String(t.getHours()).padStart(2,"0"),s=String(t.getMinutes()).padStart(2,"0"),E=String(t.getSeconds()).padStart(2,"0"),T=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${n} ${o}:${s}:${E}.${T}`}log(t,r,e,n,o){if(t<this.getLevel())return;let s=this.formatTimestamp(new Date),E=f[t].padEnd(5),T=r.padEnd(6),l="";n?.correlationId?l=`[${n.correlationId}] `:n?.sessionId&&(l=`[session-${n.sessionId}] `);let c="";o!=null&&(o instanceof Error?c=this.getLevel()===0?`
import{stdin as L}from"process";import A from"path";import{homedir as G}from"os";import{readFileSync as K}from"fs";import{readFileSync as k,writeFileSync as v,existsSync as w}from"fs";import{join as F}from"path";import{homedir as W}from"os";var U="bugfix,feature,refactor,discovery,decision,change",R="how-it-works,why-it-exists,what-changed,problem-solution,gotcha,pattern,trade-off";var a=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-5",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:"37777",CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:F(W(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_OBSERVATION_TYPES:U,CLAUDE_MEM_CONTEXT_OBSERVATION_CONCEPTS:R,CLAUDE_MEM_CONTEXT_FULL_COUNT:"5",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false"};static getAllDefaults(){return{...this.DEFAULTS}}static get(t){return this.DEFAULTS[t]}static getInt(t){let r=this.get(t);return parseInt(r,10)}static getBool(t){return this.get(t)==="true"}static loadFromFile(t){try{if(!w(t))return this.getAllDefaults();let r=k(t,"utf-8"),e=JSON.parse(r),n=e;if(e.env&&typeof e.env=="object"){n=e.env;try{v(t,JSON.stringify(n,null,2),"utf-8"),_.info("SETTINGS","Migrated settings file from nested to flat schema",{settingsPath:t})}catch(i){_.warn("SETTINGS","Failed to auto-migrate settings file",{settingsPath:t},i)}}let o={...this.DEFAULTS};for(let i of Object.keys(this.DEFAULTS))n[i]!==void 0&&(o[i]=n[i]);return o}catch(r){return _.warn("SETTINGS","Failed to load settings, using defaults",{settingsPath:t},r),this.getAllDefaults()}}};import{appendFileSync as x,existsSync as b,mkdirSync as H}from"fs";import{join as O}from"path";var S=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))(S||{}),f=class{level=null;useColor;logFilePath=null;constructor(){this.useColor=process.stdout.isTTY??!1,this.initializeLogFile()}initializeLogFile(){try{let t=a.get("CLAUDE_MEM_DATA_DIR"),r=O(t,"logs");b(r)||H(r,{recursive:!0});let e=new Date().toISOString().split("T")[0];this.logFilePath=O(r,`claude-mem-${e}.log`)}catch(t){console.error("[LOGGER] Failed to initialize log file:",t),this.logFilePath=null}}getLevel(){if(this.level===null)try{let t=a.get("CLAUDE_MEM_DATA_DIR"),r=O(t,"settings.json"),n=a.loadFromFile(r).CLAUDE_MEM_LOG_LEVEL.toUpperCase();this.level=S[n]??1}catch(t){console.error("[LOGGER] Failed to load settings, using INFO level:",t),this.level=1}return this.level}correlationId(t,r){return`obs-${t}-${r}`}sessionId(t){return`session-${t}`}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?JSON.stringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=typeof r=="string"?JSON.parse(r):r;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),o=String(t.getHours()).padStart(2,"0"),i=String(t.getMinutes()).padStart(2,"0"),E=String(t.getSeconds()).padStart(2,"0"),u=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${n} ${o}:${i}:${E}.${u}`}log(t,r,e,n,o){if(t<this.getLevel())return;let i=this.formatTimestamp(new Date),E=S[t].padEnd(5),u=r.padEnd(6),c="";n?.correlationId?c=`[${n.correlationId}] `:n?.sessionId&&(c=`[session-${n.sessionId}] `);let l="";o!=null&&(o instanceof Error?l=this.getLevel()===0?`
${o.message}
${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?c=`
`+JSON.stringify(o,null,2):c=" "+this.formatData(o));let O="";if(n){let{sessionId:m,memorySessionId:Q,correlationId:Z,...D}=n;Object.keys(D).length>0&&(O=` {${Object.entries(D).map(([k,$])=>`${k}=${$}`).join(", ")}}`)}let C=`[${s}] [${E}] [${T}] ${l}${e}${O}${c}`;if(this.logFilePath)try{b(this.logFilePath,C+`
${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?l=`
`+JSON.stringify(o,null,2):l=" "+this.formatData(o));let T="";if(n){let{sessionId:m,memorySessionId:q,correlationId:Q,...D}=n;Object.keys(D).length>0&&(T=` {${Object.entries(D).map(([P,$])=>`${P}=${$}`).join(", ")}}`)}let C=`[${i}] [${E}] [${u}] ${c}${e}${T}${l}`;if(this.logFilePath)try{x(this.logFilePath,C+`
`,"utf8")}catch(m){process.stderr.write(`[LOGGER] Failed to write to log file: ${m}
`)}else process.stderr.write(C+`
`)}debug(t,r,e,n){this.log(0,t,r,e,n)}info(t,r,e,n){this.log(1,t,r,e,n)}warn(t,r,e,n){this.log(2,t,r,e,n)}error(t,r,e,n){this.log(3,t,r,e,n)}dataIn(t,r,e,n){this.info(t,`\u2192 ${r}`,e,n)}dataOut(t,r,e,n){this.info(t,`\u2190 ${r}`,e,n)}success(t,r,e,n){this.info(t,`\u2713 ${r}`,e,n)}failure(t,r,e,n){this.error(t,`\u2717 ${r}`,e,n)}timing(t,r,e,n){this.info(t,`\u23F1 ${r}`,n,{duration:`${e}ms`})}happyPathError(t,r,e,n,o=""){let l=((new Error().stack||"").split(`
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),c=l?`${l[1].split("/").pop()}:${l[2]}`:"unknown",O={...e,location:c};return this.warn(t,`[HAPPY-PATH] ${r}`,O,n),o}},_=new p;var g={DEFAULT:3e5,HEALTH_CHECK:3e4,WORKER_STARTUP_WAIT:1e3,WORKER_STARTUP_RETRIES:300,PRE_RESTART_SETTLE_DELAY:2e3,WINDOWS_MULTIPLIER:1.5};function d(i){return process.platform==="win32"?Math.round(i*g.WINDOWS_MULTIPLIER):i}function h(i={}){let{port:t,includeSkillFallback:r=!1,customPrefix:e,actualError:n}=i,o=e||"Worker service connection failed.",s=t?` (port ${t})`:"",E=`${o}${s}
`)}debug(t,r,e,n){this.log(0,t,r,e,n)}info(t,r,e,n){this.log(1,t,r,e,n)}warn(t,r,e,n){this.log(2,t,r,e,n)}error(t,r,e,n){this.log(3,t,r,e,n)}dataIn(t,r,e,n){this.info(t,`\u2192 ${r}`,e,n)}dataOut(t,r,e,n){this.info(t,`\u2190 ${r}`,e,n)}success(t,r,e,n){this.info(t,`\u2713 ${r}`,e,n)}failure(t,r,e,n){this.error(t,`\u2717 ${r}`,e,n)}timing(t,r,e,n){this.info(t,`\u23F1 ${r}`,n,{duration:`${e}ms`})}happyPathError(t,r,e,n,o=""){let c=((new Error().stack||"").split(`
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),l=c?`${c[1].split("/").pop()}:${c[2]}`:"unknown",T={...e,location:l};return this.warn(t,`[HAPPY-PATH] ${r}`,T,n),o}},_=new f;var p={DEFAULT:3e5,HEALTH_CHECK:3e4,WORKER_STARTUP_WAIT:1e3,WORKER_STARTUP_RETRIES:300,PRE_RESTART_SETTLE_DELAY:2e3,WINDOWS_MULTIPLIER:1.5};function d(s){return process.platform==="win32"?Math.round(s*p.WINDOWS_MULTIPLIER):s}function h(s={}){let{port:t,includeSkillFallback:r=!1,customPrefix:e,actualError:n}=s,o=e||"Worker service connection failed.",i=t?` (port ${t})`:"",E=`${o}${i}
`;return E+=`To restart the worker:
`,E+=`1. Exit Claude Code completely
@@ -16,4 +16,4 @@ ${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?c=`
If that doesn't work, try: /troubleshoot`),n&&(E=`Worker Error: ${n}
${E}`),E}var j=A.join(K(),".claude","plugins","marketplaces","thedotmack"),I=d(g.HEALTH_CHECK),M=null;function u(){if(M!==null)return M;let i=A.join(a.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),t=a.loadFromFile(i);return M=parseInt(t.CLAUDE_MEM_WORKER_PORT,10),M}async function V(){let i=u();return(await fetch(`http://127.0.0.1:${i}/api/readiness`,{signal:AbortSignal.timeout(I)})).ok}function B(){let i=A.join(j,"package.json");return JSON.parse(X(i,"utf-8")).version}async function Y(){let i=u(),t=await fetch(`http://127.0.0.1:${i}/api/version`,{signal:AbortSignal.timeout(I)});if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function J(){let i=B(),t=await Y();i!==t&&_.warn("SYSTEM","Worker version mismatch",{pluginVersion:i,workerVersion:t,hint:"Restart worker with: claude-mem worker restart"})}async function N(){for(let r=0;r<25;r++){try{if(await V()){await J();return}}catch{}await new Promise(e=>setTimeout(e,200))}throw new Error(h({port:u(),customPrefix:"Worker did not become ready within 5 seconds."}))}import z from"path";function y(i){if(!i||i.trim()==="")return _.warn("PROJECT_NAME","Empty cwd provided, using fallback",{cwd:i}),"unknown-project";let t=z.basename(i);if(t===""){if(process.platform==="win32"){let e=i.match(/^([A-Z]):\\/i);if(e){let o=`drive-${e[1].toUpperCase()}`;return _.info("PROJECT_NAME","Drive root detected",{cwd:i,projectName:o}),o}}return _.warn("PROJECT_NAME","Root directory detected, using fallback",{cwd:i}),"unknown-project"}return t}async function P(i){await N();let t=i?.cwd??process.cwd(),r=y(t),n=`http://127.0.0.1:${u()}/api/context/inject?project=${encodeURIComponent(r)}`,o=await fetch(n,{signal:AbortSignal.timeout(g.DEFAULT)});if(!o.ok)throw new Error(`Context generation failed: ${o.status}`);return(await o.text()).trim()}var q=process.argv.includes("--colors");if(L.isTTY||q)P(void 0).then(i=>{console.log(i),process.exit(0)});else{let i="";L.on("data",t=>i+=t),L.on("end",async()=>{let t;try{t=i.trim()?JSON.parse(i):void 0}catch(e){throw new Error(`Failed to parse hook input: ${e instanceof Error?e.message:String(e)}`)}let r=await P(t);console.log(JSON.stringify({hookSpecificOutput:{hookEventName:"SessionStart",additionalContext:r}})),process.exit(0)})}
${E}`),E}var X=A.join(G(),".claude","plugins","marketplaces","thedotmack"),At=d(p.HEALTH_CHECK),M=null;function g(){if(M!==null)return M;let s=A.join(a.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),t=a.loadFromFile(s);return M=parseInt(t.CLAUDE_MEM_WORKER_PORT,10),M}async function j(){let s=g();return(await fetch(`http://127.0.0.1:${s}/api/readiness`)).ok}function V(){let s=A.join(X,"package.json");return JSON.parse(K(s,"utf-8")).version}async function B(){let s=g(),t=await fetch(`http://127.0.0.1:${s}/api/version`);if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function Y(){let s=V(),t=await B();s!==t&&_.debug("SYSTEM","Version check",{pluginVersion:s,workerVersion:t,note:"Mismatch will be auto-restarted by worker-service start command"})}async function I(){for(let r=0;r<75;r++){try{if(await j()){await Y();return}}catch{}await new Promise(e=>setTimeout(e,200))}throw new Error(h({port:g(),customPrefix:"Worker did not become ready within 15 seconds."}))}import J from"path";function N(s){if(!s||s.trim()==="")return _.warn("PROJECT_NAME","Empty cwd provided, using fallback",{cwd:s}),"unknown-project";let t=J.basename(s);if(t===""){if(process.platform==="win32"){let e=s.match(/^([A-Z]):\\/i);if(e){let o=`drive-${e[1].toUpperCase()}`;return _.info("PROJECT_NAME","Drive root detected",{cwd:s,projectName:o}),o}}return _.warn("PROJECT_NAME","Root directory detected, using fallback",{cwd:s}),"unknown-project"}return t}async function y(s){await I();let t=s?.cwd??process.cwd(),r=N(t),n=`http://127.0.0.1:${g()}/api/context/inject?project=${encodeURIComponent(r)}`,o=await fetch(n);if(!o.ok)throw new Error(`Context generation failed: ${o.status}`);return(await o.text()).trim()}var z=process.argv.includes("--colors");if(L.isTTY||z)y(void 0).then(s=>{console.log(s),process.exit(0)});else{let s="";L.on("data",t=>s+=t),L.on("end",async()=>{let t;try{t=s.trim()?JSON.parse(s):void 0}catch(e){throw new Error(`Failed to parse hook input: ${e instanceof Error?e.message:String(e)}`)}let r=await y(t);console.log(JSON.stringify({hookSpecificOutput:{hookEventName:"SessionStart",additionalContext:r}})),process.exit(0)})}
+7 -7
View File
@@ -1,13 +1,13 @@
#!/usr/bin/env bun
import{stdin as y}from"process";var S=JSON.stringify({continue:!0,suppressOutput:!0});import m from"path";import{homedir as X}from"os";import{readFileSync as j}from"fs";import{readFileSync as w,writeFileSync as b,existsSync as F}from"fs";import{join as H}from"path";import{homedir as W}from"os";var R="bugfix,feature,refactor,discovery,decision,change",U="how-it-works,why-it-exists,what-changed,problem-solution,gotcha,pattern,trade-off";var g=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-5",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:"37777",CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:H(W(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_OBSERVATION_TYPES:R,CLAUDE_MEM_CONTEXT_OBSERVATION_CONCEPTS:U,CLAUDE_MEM_CONTEXT_FULL_COUNT:"5",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false"};static getAllDefaults(){return{...this.DEFAULTS}}static get(t){return this.DEFAULTS[t]}static getInt(t){let r=this.get(t);return parseInt(r,10)}static getBool(t){return this.get(t)==="true"}static loadFromFile(t){try{if(!F(t))return this.getAllDefaults();let r=w(t,"utf-8"),e=JSON.parse(r),n=e;if(e.env&&typeof e.env=="object"){n=e.env;try{b(t,JSON.stringify(n,null,2),"utf-8"),E.info("SETTINGS","Migrated settings file from nested to flat schema",{settingsPath:t})}catch(a){E.warn("SETTINGS","Failed to auto-migrate settings file",{settingsPath:t},a)}}let o={...this.DEFAULTS};for(let a of Object.keys(this.DEFAULTS))n[a]!==void 0&&(o[a]=n[a]);return o}catch(r){return E.warn("SETTINGS","Failed to load settings, using defaults",{settingsPath:t},r),this.getAllDefaults()}}};import{appendFileSync as K,existsSync as x,mkdirSync as G}from"fs";import{join as T}from"path";var f=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))(f||{}),M=class{level=null;useColor;logFilePath=null;constructor(){this.useColor=process.stdout.isTTY??!1,this.initializeLogFile()}initializeLogFile(){try{let t=g.get("CLAUDE_MEM_DATA_DIR"),r=T(t,"logs");x(r)||G(r,{recursive:!0});let e=new Date().toISOString().split("T")[0];this.logFilePath=T(r,`claude-mem-${e}.log`)}catch(t){console.error("[LOGGER] Failed to initialize log file:",t),this.logFilePath=null}}getLevel(){if(this.level===null)try{let t=g.get("CLAUDE_MEM_DATA_DIR"),r=T(t,"settings.json"),n=g.loadFromFile(r).CLAUDE_MEM_LOG_LEVEL.toUpperCase();this.level=f[n]??1}catch(t){console.error("[LOGGER] Failed to load settings, using INFO level:",t),this.level=1}return this.level}correlationId(t,r){return`obs-${t}-${r}`}sessionId(t){return`session-${t}`}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?JSON.stringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=typeof r=="string"?JSON.parse(r):r;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),o=String(t.getHours()).padStart(2,"0"),a=String(t.getMinutes()).padStart(2,"0"),s=String(t.getSeconds()).padStart(2,"0"),l=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${n} ${o}:${a}:${s}.${l}`}log(t,r,e,n,o){if(t<this.getLevel())return;let a=this.formatTimestamp(new Date),s=f[t].padEnd(5),l=r.padEnd(6),_="";n?.correlationId?_=`[${n.correlationId}] `:n?.sessionId&&(_=`[session-${n.sessionId}] `);let c="";o!=null&&(o instanceof Error?c=this.getLevel()===0?`
import{stdin as P}from"process";var T=JSON.stringify({continue:!0,suppressOutput:!0});import L from"path";import{homedir as G}from"os";import{readFileSync as X}from"fs";import{readFileSync as v,writeFileSync as w,existsSync as b}from"fs";import{join as F}from"path";import{homedir as H}from"os";var R="bugfix,feature,refactor,discovery,decision,change",U="how-it-works,why-it-exists,what-changed,problem-solution,gotcha,pattern,trade-off";var g=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-5",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:"37777",CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:F(H(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_OBSERVATION_TYPES:R,CLAUDE_MEM_CONTEXT_OBSERVATION_CONCEPTS:U,CLAUDE_MEM_CONTEXT_FULL_COUNT:"5",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false"};static getAllDefaults(){return{...this.DEFAULTS}}static get(t){return this.DEFAULTS[t]}static getInt(t){let r=this.get(t);return parseInt(r,10)}static getBool(t){return this.get(t)==="true"}static loadFromFile(t){try{if(!b(t))return this.getAllDefaults();let r=v(t,"utf-8"),e=JSON.parse(r),n=e;if(e.env&&typeof e.env=="object"){n=e.env;try{w(t,JSON.stringify(n,null,2),"utf-8"),E.info("SETTINGS","Migrated settings file from nested to flat schema",{settingsPath:t})}catch(_){E.warn("SETTINGS","Failed to auto-migrate settings file",{settingsPath:t},_)}}let o={...this.DEFAULTS};for(let _ of Object.keys(this.DEFAULTS))n[_]!==void 0&&(o[_]=n[_]);return o}catch(r){return E.warn("SETTINGS","Failed to load settings, using defaults",{settingsPath:t},r),this.getAllDefaults()}}};import{appendFileSync as W,existsSync as K,mkdirSync as x}from"fs";import{join as S}from"path";var f=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))(f||{}),M=class{level=null;useColor;logFilePath=null;constructor(){this.useColor=process.stdout.isTTY??!1,this.initializeLogFile()}initializeLogFile(){try{let t=g.get("CLAUDE_MEM_DATA_DIR"),r=S(t,"logs");K(r)||x(r,{recursive:!0});let e=new Date().toISOString().split("T")[0];this.logFilePath=S(r,`claude-mem-${e}.log`)}catch(t){console.error("[LOGGER] Failed to initialize log file:",t),this.logFilePath=null}}getLevel(){if(this.level===null)try{let t=g.get("CLAUDE_MEM_DATA_DIR"),r=S(t,"settings.json"),n=g.loadFromFile(r).CLAUDE_MEM_LOG_LEVEL.toUpperCase();this.level=f[n]??1}catch(t){console.error("[LOGGER] Failed to load settings, using INFO level:",t),this.level=1}return this.level}correlationId(t,r){return`obs-${t}-${r}`}sessionId(t){return`session-${t}`}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?JSON.stringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=typeof r=="string"?JSON.parse(r):r;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),o=String(t.getHours()).padStart(2,"0"),_=String(t.getMinutes()).padStart(2,"0"),s=String(t.getSeconds()).padStart(2,"0"),l=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${n} ${o}:${_}:${s}.${l}`}log(t,r,e,n,o){if(t<this.getLevel())return;let _=this.formatTimestamp(new Date),s=f[t].padEnd(5),l=r.padEnd(6),a="";n?.correlationId?a=`[${n.correlationId}] `:n?.sessionId&&(a=`[session-${n.sessionId}] `);let c="";o!=null&&(o instanceof Error?c=this.getLevel()===0?`
${o.message}
${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?c=`
`+JSON.stringify(o,null,2):c=" "+this.formatData(o));let u="";if(n){let{sessionId:D,memorySessionId:Z,correlationId:tt,...d}=n;Object.keys(d).length>0&&(u=` {${Object.entries(d).map(([$,v])=>`${$}=${v}`).join(", ")}}`)}let C=`[${a}] [${s}] [${l}] ${_}${e}${u}${c}`;if(this.logFilePath)try{K(this.logFilePath,C+`
`+JSON.stringify(o,null,2):c=" "+this.formatData(o));let p="";if(n){let{sessionId:D,memorySessionId:Q,correlationId:Z,...d}=n;Object.keys(d).length>0&&(p=` {${Object.entries(d).map(([k,$])=>`${k}=${$}`).join(", ")}}`)}let m=`[${_}] [${s}] [${l}] ${a}${e}${p}${c}`;if(this.logFilePath)try{W(this.logFilePath,m+`
`,"utf8")}catch(D){process.stderr.write(`[LOGGER] Failed to write to log file: ${D}
`)}else process.stderr.write(C+`
`)}debug(t,r,e,n){this.log(0,t,r,e,n)}info(t,r,e,n){this.log(1,t,r,e,n)}warn(t,r,e,n){this.log(2,t,r,e,n)}error(t,r,e,n){this.log(3,t,r,e,n)}dataIn(t,r,e,n){this.info(t,`\u2192 ${r}`,e,n)}dataOut(t,r,e,n){this.info(t,`\u2190 ${r}`,e,n)}success(t,r,e,n){this.info(t,`\u2713 ${r}`,e,n)}failure(t,r,e,n){this.error(t,`\u2717 ${r}`,e,n)}timing(t,r,e,n){this.info(t,`\u23F1 ${r}`,n,{duration:`${e}ms`})}happyPathError(t,r,e,n,o=""){let _=((new Error().stack||"").split(`
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),c=_?`${_[1].split("/").pop()}:${_[2]}`:"unknown",u={...e,location:c};return this.warn(t,`[HAPPY-PATH] ${r}`,u,n),o}},E=new M;var A={DEFAULT:3e5,HEALTH_CHECK:3e4,WORKER_STARTUP_WAIT:1e3,WORKER_STARTUP_RETRIES:300,PRE_RESTART_SETTLE_DELAY:2e3,WINDOWS_MULTIPLIER:1.5};function h(i){return process.platform==="win32"?Math.round(i*A.WINDOWS_MULTIPLIER):i}function N(i={}){let{port:t,includeSkillFallback:r=!1,customPrefix:e,actualError:n}=i,o=e||"Worker service connection failed.",a=t?` (port ${t})`:"",s=`${o}${a}
`)}else process.stderr.write(m+`
`)}debug(t,r,e,n){this.log(0,t,r,e,n)}info(t,r,e,n){this.log(1,t,r,e,n)}warn(t,r,e,n){this.log(2,t,r,e,n)}error(t,r,e,n){this.log(3,t,r,e,n)}dataIn(t,r,e,n){this.info(t,`\u2192 ${r}`,e,n)}dataOut(t,r,e,n){this.info(t,`\u2190 ${r}`,e,n)}success(t,r,e,n){this.info(t,`\u2713 ${r}`,e,n)}failure(t,r,e,n){this.error(t,`\u2717 ${r}`,e,n)}timing(t,r,e,n){this.info(t,`\u23F1 ${r}`,n,{duration:`${e}ms`})}happyPathError(t,r,e,n,o=""){let a=((new Error().stack||"").split(`
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),c=a?`${a[1].split("/").pop()}:${a[2]}`:"unknown",p={...e,location:c};return this.warn(t,`[HAPPY-PATH] ${r}`,p,n),o}},E=new M;var A={DEFAULT:3e5,HEALTH_CHECK:3e4,WORKER_STARTUP_WAIT:1e3,WORKER_STARTUP_RETRIES:300,PRE_RESTART_SETTLE_DELAY:2e3,WINDOWS_MULTIPLIER:1.5};function h(i){return process.platform==="win32"?Math.round(i*A.WINDOWS_MULTIPLIER):i}function N(i={}){let{port:t,includeSkillFallback:r=!1,customPrefix:e,actualError:n}=i,o=e||"Worker service connection failed.",_=t?` (port ${t})`:"",s=`${o}${_}
`;return s+=`To restart the worker:
`,s+=`1. Exit Claude Code completely
@@ -16,4 +16,4 @@ ${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?c=`
If that doesn't work, try: /troubleshoot`),n&&(s=`Worker Error: ${n}
${s}`),s}var V=m.join(X(),".claude","plugins","marketplaces","thedotmack"),I=h(A.HEALTH_CHECK),O=null;function p(){if(O!==null)return O;let i=m.join(g.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),t=g.loadFromFile(i);return O=parseInt(t.CLAUDE_MEM_WORKER_PORT,10),O}async function B(){let i=p();return(await fetch(`http://127.0.0.1:${i}/api/readiness`,{signal:AbortSignal.timeout(I)})).ok}function Y(){let i=m.join(V,"package.json");return JSON.parse(j(i,"utf-8")).version}async function J(){let i=p(),t=await fetch(`http://127.0.0.1:${i}/api/version`,{signal:AbortSignal.timeout(I)});if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function z(){let i=Y(),t=await J();i!==t&&E.warn("SYSTEM","Worker version mismatch",{pluginVersion:i,workerVersion:t,hint:"Restart worker with: claude-mem worker restart"})}async function P(){for(let r=0;r<25;r++){try{if(await B()){await z();return}}catch{}await new Promise(e=>setTimeout(e,200))}throw new Error(N({port:p(),customPrefix:"Worker did not become ready within 5 seconds."}))}import q from"path";function k(i){if(!i||i.trim()==="")return E.warn("PROJECT_NAME","Empty cwd provided, using fallback",{cwd:i}),"unknown-project";let t=q.basename(i);if(t===""){if(process.platform==="win32"){let e=i.match(/^([A-Z]):\\/i);if(e){let o=`drive-${e[1].toUpperCase()}`;return E.info("PROJECT_NAME","Drive root detected",{cwd:i,projectName:o}),o}}return E.warn("PROJECT_NAME","Root directory detected, using fallback",{cwd:i}),"unknown-project"}return t}async function Q(i){if(await P(),!i)throw new Error("newHook requires input");let{session_id:t,cwd:r,prompt:e}=i,n=k(r);E.info("HOOK","new-hook: Received hook input",{session_id:t,has_prompt:!!e,cwd:r});let o=p();E.info("HOOK","new-hook: Calling /api/sessions/init",{contentSessionId:t,project:n,prompt_length:e?.length});let a=await fetch(`http://127.0.0.1:${o}/api/sessions/init`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({contentSessionId:t,project:n,prompt:e}),signal:AbortSignal.timeout(5e3)});if(!a.ok)throw new Error(`Session initialization failed: ${a.status}`);let s=await a.json(),l=s.sessionDbId,_=s.promptNumber;if(E.info("HOOK","new-hook: Received from /api/sessions/init",{sessionDbId:l,promptNumber:_,skipped:s.skipped}),s.skipped&&s.reason==="private"){E.info("HOOK",`new-hook: Session ${l}, prompt #${_} (fully private - skipped)`),console.log(S);return}E.info("HOOK",`new-hook: Session ${l}, prompt #${_}`);let c=e.startsWith("/")?e.substring(1):e;E.info("HOOK","new-hook: Calling /sessions/{sessionDbId}/init",{sessionDbId:l,promptNumber:_,userPrompt_length:c?.length});let u=await fetch(`http://127.0.0.1:${o}/sessions/${l}/init`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({userPrompt:c,promptNumber:_}),signal:AbortSignal.timeout(5e3)});if(!u.ok)throw new Error(`SDK agent start failed: ${u.status}`);console.log(S)}var L="";y.on("data",i=>L+=i);y.on("end",async()=>{let i;try{i=L?JSON.parse(L):void 0}catch(t){throw new Error(`Failed to parse hook input: ${t instanceof Error?t.message:String(t)}`)}await Q(i)});
${s}`),s}var j=L.join(G(),".claude","plugins","marketplaces","thedotmack"),Ct=h(A.HEALTH_CHECK),O=null;function u(){if(O!==null)return O;let i=L.join(g.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),t=g.loadFromFile(i);return O=parseInt(t.CLAUDE_MEM_WORKER_PORT,10),O}async function V(){let i=u();return(await fetch(`http://127.0.0.1:${i}/api/readiness`)).ok}function B(){let i=L.join(j,"package.json");return JSON.parse(X(i,"utf-8")).version}async function Y(){let i=u(),t=await fetch(`http://127.0.0.1:${i}/api/version`);if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function J(){let i=B(),t=await Y();i!==t&&E.debug("SYSTEM","Version check",{pluginVersion:i,workerVersion:t,note:"Mismatch will be auto-restarted by worker-service start command"})}async function I(){for(let r=0;r<75;r++){try{if(await V()){await J();return}}catch{}await new Promise(e=>setTimeout(e,200))}throw new Error(N({port:u(),customPrefix:"Worker did not become ready within 15 seconds."}))}import z from"path";function y(i){if(!i||i.trim()==="")return E.warn("PROJECT_NAME","Empty cwd provided, using fallback",{cwd:i}),"unknown-project";let t=z.basename(i);if(t===""){if(process.platform==="win32"){let e=i.match(/^([A-Z]):\\/i);if(e){let o=`drive-${e[1].toUpperCase()}`;return E.info("PROJECT_NAME","Drive root detected",{cwd:i,projectName:o}),o}}return E.warn("PROJECT_NAME","Root directory detected, using fallback",{cwd:i}),"unknown-project"}return t}async function q(i){if(await I(),!i)throw new Error("newHook requires input");let{session_id:t,cwd:r,prompt:e}=i,n=y(r);E.info("HOOK","new-hook: Received hook input",{session_id:t,has_prompt:!!e,cwd:r});let o=u();E.info("HOOK","new-hook: Calling /api/sessions/init",{contentSessionId:t,project:n,prompt_length:e?.length});let _=await fetch(`http://127.0.0.1:${o}/api/sessions/init`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({contentSessionId:t,project:n,prompt:e})});if(!_.ok)throw new Error(`Session initialization failed: ${_.status}`);let s=await _.json(),l=s.sessionDbId,a=s.promptNumber;if(E.info("HOOK","new-hook: Received from /api/sessions/init",{sessionDbId:l,promptNumber:a,skipped:s.skipped}),s.skipped&&s.reason==="private"){E.info("HOOK",`new-hook: Session ${l}, prompt #${a} (fully private - skipped)`),console.log(T);return}E.info("HOOK",`new-hook: Session ${l}, prompt #${a}`);let c=e.startsWith("/")?e.substring(1):e;E.info("HOOK","new-hook: Calling /sessions/{sessionDbId}/init",{sessionDbId:l,promptNumber:a,userPrompt_length:c?.length});let p=await fetch(`http://127.0.0.1:${o}/sessions/${l}/init`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({userPrompt:c,promptNumber:a})});if(!p.ok)throw new Error(`SDK agent start failed: ${p.status}`);console.log(T)}var C="";P.on("data",i=>C+=i);P.on("end",async()=>{let i;try{i=C?JSON.parse(C):void 0}catch(t){throw new Error(`Failed to parse hook input: ${t instanceof Error?t.message:String(t)}`)}await q(i)});
+7 -7
View File
@@ -1,13 +1,13 @@
#!/usr/bin/env bun
import{stdin as P}from"process";var U=JSON.stringify({continue:!0,suppressOutput:!0});import{readFileSync as v,writeFileSync as w,existsSync as F}from"fs";import{join as H}from"path";import{homedir as W}from"os";var R="bugfix,feature,refactor,discovery,decision,change",d="how-it-works,why-it-exists,what-changed,problem-solution,gotcha,pattern,trade-off";var a=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-5",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:"37777",CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:H(W(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_OBSERVATION_TYPES:R,CLAUDE_MEM_CONTEXT_OBSERVATION_CONCEPTS:d,CLAUDE_MEM_CONTEXT_FULL_COUNT:"5",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false"};static getAllDefaults(){return{...this.DEFAULTS}}static get(t){return this.DEFAULTS[t]}static getInt(t){let r=this.get(t);return parseInt(r,10)}static getBool(t){return this.get(t)==="true"}static loadFromFile(t){try{if(!F(t))return this.getAllDefaults();let r=v(t,"utf-8"),e=JSON.parse(r),n=e;if(e.env&&typeof e.env=="object"){n=e.env;try{w(t,JSON.stringify(n,null,2),"utf-8"),_.info("SETTINGS","Migrated settings file from nested to flat schema",{settingsPath:t})}catch(i){_.warn("SETTINGS","Failed to auto-migrate settings file",{settingsPath:t},i)}}let o={...this.DEFAULTS};for(let i of Object.keys(this.DEFAULTS))n[i]!==void 0&&(o[i]=n[i]);return o}catch(r){return _.warn("SETTINGS","Failed to load settings, using defaults",{settingsPath:t},r),this.getAllDefaults()}}};import{appendFileSync as b,existsSync as G,mkdirSync as x}from"fs";import{join as M}from"path";var f=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))(f||{}),p=class{level=null;useColor;logFilePath=null;constructor(){this.useColor=process.stdout.isTTY??!1,this.initializeLogFile()}initializeLogFile(){try{let t=a.get("CLAUDE_MEM_DATA_DIR"),r=M(t,"logs");G(r)||x(r,{recursive:!0});let e=new Date().toISOString().split("T")[0];this.logFilePath=M(r,`claude-mem-${e}.log`)}catch(t){console.error("[LOGGER] Failed to initialize log file:",t),this.logFilePath=null}}getLevel(){if(this.level===null)try{let t=a.get("CLAUDE_MEM_DATA_DIR"),r=M(t,"settings.json"),n=a.loadFromFile(r).CLAUDE_MEM_LOG_LEVEL.toUpperCase();this.level=f[n]??1}catch(t){console.error("[LOGGER] Failed to load settings, using INFO level:",t),this.level=1}return this.level}correlationId(t,r){return`obs-${t}-${r}`}sessionId(t){return`session-${t}`}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?JSON.stringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=typeof r=="string"?JSON.parse(r):r;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),o=String(t.getHours()).padStart(2,"0"),i=String(t.getMinutes()).padStart(2,"0"),E=String(t.getSeconds()).padStart(2,"0"),l=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${n} ${o}:${i}:${E}.${l}`}log(t,r,e,n,o){if(t<this.getLevel())return;let i=this.formatTimestamp(new Date),E=f[t].padEnd(5),l=r.padEnd(6),g="";n?.correlationId?g=`[${n.correlationId}] `:n?.sessionId&&(g=`[session-${n.sessionId}] `);let c="";o!=null&&(o instanceof Error?c=this.getLevel()===0?`
import{stdin as y}from"process";var U=JSON.stringify({continue:!0,suppressOutput:!0});import{readFileSync as k,writeFileSync as v,existsSync as w}from"fs";import{join as F}from"path";import{homedir as H}from"os";var R="bugfix,feature,refactor,discovery,decision,change",d="how-it-works,why-it-exists,what-changed,problem-solution,gotcha,pattern,trade-off";var a=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-5",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:"37777",CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:F(H(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_OBSERVATION_TYPES:R,CLAUDE_MEM_CONTEXT_OBSERVATION_CONCEPTS:d,CLAUDE_MEM_CONTEXT_FULL_COUNT:"5",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false"};static getAllDefaults(){return{...this.DEFAULTS}}static get(t){return this.DEFAULTS[t]}static getInt(t){let r=this.get(t);return parseInt(r,10)}static getBool(t){return this.get(t)==="true"}static loadFromFile(t){try{if(!w(t))return this.getAllDefaults();let r=k(t,"utf-8"),e=JSON.parse(r),n=e;if(e.env&&typeof e.env=="object"){n=e.env;try{v(t,JSON.stringify(n,null,2),"utf-8"),_.info("SETTINGS","Migrated settings file from nested to flat schema",{settingsPath:t})}catch(i){_.warn("SETTINGS","Failed to auto-migrate settings file",{settingsPath:t},i)}}let o={...this.DEFAULTS};for(let i of Object.keys(this.DEFAULTS))n[i]!==void 0&&(o[i]=n[i]);return o}catch(r){return _.warn("SETTINGS","Failed to load settings, using defaults",{settingsPath:t},r),this.getAllDefaults()}}};import{appendFileSync as W,existsSync as b,mkdirSync as G}from"fs";import{join as M}from"path";var S=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))(S||{}),f=class{level=null;useColor;logFilePath=null;constructor(){this.useColor=process.stdout.isTTY??!1,this.initializeLogFile()}initializeLogFile(){try{let t=a.get("CLAUDE_MEM_DATA_DIR"),r=M(t,"logs");b(r)||G(r,{recursive:!0});let e=new Date().toISOString().split("T")[0];this.logFilePath=M(r,`claude-mem-${e}.log`)}catch(t){console.error("[LOGGER] Failed to initialize log file:",t),this.logFilePath=null}}getLevel(){if(this.level===null)try{let t=a.get("CLAUDE_MEM_DATA_DIR"),r=M(t,"settings.json"),n=a.loadFromFile(r).CLAUDE_MEM_LOG_LEVEL.toUpperCase();this.level=S[n]??1}catch(t){console.error("[LOGGER] Failed to load settings, using INFO level:",t),this.level=1}return this.level}correlationId(t,r){return`obs-${t}-${r}`}sessionId(t){return`session-${t}`}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?JSON.stringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=typeof r=="string"?JSON.parse(r):r;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),o=String(t.getHours()).padStart(2,"0"),i=String(t.getMinutes()).padStart(2,"0"),E=String(t.getSeconds()).padStart(2,"0"),l=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${n} ${o}:${i}:${E}.${l}`}log(t,r,e,n,o){if(t<this.getLevel())return;let i=this.formatTimestamp(new Date),E=S[t].padEnd(5),l=r.padEnd(6),c="";n?.correlationId?c=`[${n.correlationId}] `:n?.sessionId&&(c=`[session-${n.sessionId}] `);let g="";o!=null&&(o instanceof Error?g=this.getLevel()===0?`
${o.message}
${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?c=`
`+JSON.stringify(o,null,2):c=" "+this.formatData(o));let T="";if(n){let{sessionId:D,memorySessionId:z,correlationId:Q,...m}=n;Object.keys(m).length>0&&(T=` {${Object.entries(m).map(([$,k])=>`${$}=${k}`).join(", ")}}`)}let C=`[${i}] [${E}] [${l}] ${g}${e}${T}${c}`;if(this.logFilePath)try{b(this.logFilePath,C+`
${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?g=`
`+JSON.stringify(o,null,2):g=" "+this.formatData(o));let u="";if(n){let{sessionId:D,memorySessionId:q,correlationId:z,...m}=n;Object.keys(m).length>0&&(u=` {${Object.entries(m).map(([P,$])=>`${P}=${$}`).join(", ")}}`)}let C=`[${i}] [${E}] [${l}] ${c}${e}${u}${g}`;if(this.logFilePath)try{W(this.logFilePath,C+`
`,"utf8")}catch(D){process.stderr.write(`[LOGGER] Failed to write to log file: ${D}
`)}else process.stderr.write(C+`
`)}debug(t,r,e,n){this.log(0,t,r,e,n)}info(t,r,e,n){this.log(1,t,r,e,n)}warn(t,r,e,n){this.log(2,t,r,e,n)}error(t,r,e,n){this.log(3,t,r,e,n)}dataIn(t,r,e,n){this.info(t,`\u2192 ${r}`,e,n)}dataOut(t,r,e,n){this.info(t,`\u2190 ${r}`,e,n)}success(t,r,e,n){this.info(t,`\u2713 ${r}`,e,n)}failure(t,r,e,n){this.error(t,`\u2717 ${r}`,e,n)}timing(t,r,e,n){this.info(t,`\u23F1 ${r}`,n,{duration:`${e}ms`})}happyPathError(t,r,e,n,o=""){let g=((new Error().stack||"").split(`
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),c=g?`${g[1].split("/").pop()}:${g[2]}`:"unknown",T={...e,location:c};return this.warn(t,`[HAPPY-PATH] ${r}`,T,n),o}},_=new p;import A from"path";import{homedir as K}from"os";import{readFileSync as X}from"fs";var u={DEFAULT:3e5,HEALTH_CHECK:3e4,WORKER_STARTUP_WAIT:1e3,WORKER_STARTUP_RETRIES:300,PRE_RESTART_SETTLE_DELAY:2e3,WINDOWS_MULTIPLIER:1.5};function I(s){return process.platform==="win32"?Math.round(s*u.WINDOWS_MULTIPLIER):s}function h(s={}){let{port:t,includeSkillFallback:r=!1,customPrefix:e,actualError:n}=s,o=e||"Worker service connection failed.",i=t?` (port ${t})`:"",E=`${o}${i}
`)}debug(t,r,e,n){this.log(0,t,r,e,n)}info(t,r,e,n){this.log(1,t,r,e,n)}warn(t,r,e,n){this.log(2,t,r,e,n)}error(t,r,e,n){this.log(3,t,r,e,n)}dataIn(t,r,e,n){this.info(t,`\u2192 ${r}`,e,n)}dataOut(t,r,e,n){this.info(t,`\u2190 ${r}`,e,n)}success(t,r,e,n){this.info(t,`\u2713 ${r}`,e,n)}failure(t,r,e,n){this.error(t,`\u2717 ${r}`,e,n)}timing(t,r,e,n){this.info(t,`\u23F1 ${r}`,n,{duration:`${e}ms`})}happyPathError(t,r,e,n,o=""){let c=((new Error().stack||"").split(`
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),g=c?`${c[1].split("/").pop()}:${c[2]}`:"unknown",u={...e,location:g};return this.warn(t,`[HAPPY-PATH] ${r}`,u,n),o}},_=new f;import A from"path";import{homedir as x}from"os";import{readFileSync as K}from"fs";var p={DEFAULT:3e5,HEALTH_CHECK:3e4,WORKER_STARTUP_WAIT:1e3,WORKER_STARTUP_RETRIES:300,PRE_RESTART_SETTLE_DELAY:2e3,WINDOWS_MULTIPLIER:1.5};function I(s){return process.platform==="win32"?Math.round(s*p.WINDOWS_MULTIPLIER):s}function h(s={}){let{port:t,includeSkillFallback:r=!1,customPrefix:e,actualError:n}=s,o=e||"Worker service connection failed.",i=t?` (port ${t})`:"",E=`${o}${i}
`;return E+=`To restart the worker:
`,E+=`1. Exit Claude Code completely
@@ -16,4 +16,4 @@ ${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?c=`
If that doesn't work, try: /troubleshoot`),n&&(E=`Worker Error: ${n}
${E}`),E}var V=A.join(K(),".claude","plugins","marketplaces","thedotmack"),N=I(u.HEALTH_CHECK),S=null;function O(){if(S!==null)return S;let s=A.join(a.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),t=a.loadFromFile(s);return S=parseInt(t.CLAUDE_MEM_WORKER_PORT,10),S}async function j(){let s=O();return(await fetch(`http://127.0.0.1:${s}/api/readiness`,{signal:AbortSignal.timeout(N)})).ok}function B(){let s=A.join(V,"package.json");return JSON.parse(X(s,"utf-8")).version}async function Y(){let s=O(),t=await fetch(`http://127.0.0.1:${s}/api/version`,{signal:AbortSignal.timeout(N)});if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function J(){let s=B(),t=await Y();s!==t&&_.warn("SYSTEM","Worker version mismatch",{pluginVersion:s,workerVersion:t,hint:"Restart worker with: claude-mem worker restart"})}async function y(){for(let r=0;r<25;r++){try{if(await j()){await J();return}}catch{}await new Promise(e=>setTimeout(e,200))}throw new Error(h({port:O(),customPrefix:"Worker did not become ready within 5 seconds."}))}async function q(s){if(await y(),!s)throw new Error("saveHook requires input");let{session_id:t,cwd:r,tool_name:e,tool_input:n,tool_response:o}=s,i=O(),E=_.formatTool(e,n);if(_.dataIn("HOOK",`PostToolUse: ${E}`,{workerPort:i}),!r)throw new Error(`Missing cwd in PostToolUse hook input for session ${t}, tool ${e}`);let l=await fetch(`http://127.0.0.1:${i}/api/sessions/observations`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({contentSessionId:t,tool_name:e,tool_input:n,tool_response:o,cwd:r}),signal:AbortSignal.timeout(u.DEFAULT)});if(!l.ok)throw new Error(`Observation storage failed: ${l.status}`);_.debug("HOOK","Observation sent successfully",{toolName:e}),console.log(U)}var L="";P.on("data",s=>L+=s);P.on("end",async()=>{let s;try{s=L?JSON.parse(L):void 0}catch(t){throw new Error(`Failed to parse hook input: ${t instanceof Error?t.message:String(t)}`)}await q(s)});
${E}`),E}var X=A.join(x(),".claude","plugins","marketplaces","thedotmack"),At=I(p.HEALTH_CHECK),T=null;function O(){if(T!==null)return T;let s=A.join(a.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),t=a.loadFromFile(s);return T=parseInt(t.CLAUDE_MEM_WORKER_PORT,10),T}async function V(){let s=O();return(await fetch(`http://127.0.0.1:${s}/api/readiness`)).ok}function j(){let s=A.join(X,"package.json");return JSON.parse(K(s,"utf-8")).version}async function B(){let s=O(),t=await fetch(`http://127.0.0.1:${s}/api/version`);if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function Y(){let s=j(),t=await B();s!==t&&_.debug("SYSTEM","Version check",{pluginVersion:s,workerVersion:t,note:"Mismatch will be auto-restarted by worker-service start command"})}async function N(){for(let r=0;r<75;r++){try{if(await V()){await Y();return}}catch{}await new Promise(e=>setTimeout(e,200))}throw new Error(h({port:O(),customPrefix:"Worker did not become ready within 15 seconds."}))}async function J(s){if(await N(),!s)throw new Error("saveHook requires input");let{session_id:t,cwd:r,tool_name:e,tool_input:n,tool_response:o}=s,i=O(),E=_.formatTool(e,n);if(_.dataIn("HOOK",`PostToolUse: ${E}`,{workerPort:i}),!r)throw new Error(`Missing cwd in PostToolUse hook input for session ${t}, tool ${e}`);let l=await fetch(`http://127.0.0.1:${i}/api/sessions/observations`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({contentSessionId:t,tool_name:e,tool_input:n,tool_response:o,cwd:r})});if(!l.ok)throw new Error(`Observation storage failed: ${l.status}`);_.debug("HOOK","Observation sent successfully",{toolName:e}),console.log(U)}var L="";y.on("data",s=>L+=s);y.on("end",async()=>{let s;try{s=L?JSON.parse(L):void 0}catch(t){throw new Error(`Failed to parse hook input: ${t instanceof Error?t.message:String(t)}`)}await J(s)});
+41 -77
View File
@@ -254,93 +254,57 @@ function installUv() {
}
/**
* Install the claude-mem CLI command to PATH
* Creates a wrapper script in ~/.local/bin (Unix) or %LOCALAPPDATA%\Programs\claude-mem (Windows)
* Add shell alias for claude-mem command
*/
function installCLI() {
const CLI_NAME = 'claude-mem';
const WORKER_CLI = join(ROOT, 'plugin', 'scripts', 'worker-cli.js');
const WORKER_CLI = join(ROOT, 'plugin', 'scripts', 'worker-service.cjs');
const bunPath = getBunPath() || 'bun';
const aliasLine = `alias claude-mem='${bunPath} "${WORKER_CLI}"'`;
const markerPath = join(ROOT, '.cli-installed');
if (IS_WINDOWS) {
// Windows: Create .cmd file in LocalAppData
const cliDir = join(process.env.LOCALAPPDATA || join(homedir(), 'AppData', 'Local'), 'Programs', 'claude-mem');
const cliPath = join(cliDir, `${CLI_NAME}.cmd`);
const markerPath = join(cliDir, '.cli-installed');
// Skip if already installed
if (existsSync(markerPath)) return;
// Skip if already installed
if (existsSync(markerPath)) return;
try {
if (IS_WINDOWS) {
// Windows: Add to PATH via PowerShell profile
const profilePath = join(process.env.USERPROFILE || homedir(), 'Documents', 'PowerShell', 'Microsoft.PowerShell_profile.ps1');
const profileDir = join(process.env.USERPROFILE || homedir(), 'Documents', 'PowerShell');
const functionDef = `function claude-mem { & "${bunPath}" "${WORKER_CLI}" $args }\n`;
try {
// Create directory if needed
if (!existsSync(cliDir)) {
execSync(`mkdir "${cliDir}"`, { stdio: 'ignore', shell: true });
if (!existsSync(profileDir)) {
execSync(`mkdir "${profileDir}"`, { stdio: 'ignore', shell: true });
}
// Get Bun path for the wrapper
const bunPath = getBunPath() || 'bun';
const existingContent = existsSync(profilePath) ? readFileSync(profilePath, 'utf-8') : '';
if (!existingContent.includes('function claude-mem')) {
writeFileSync(profilePath, existingContent + '\n' + functionDef);
console.error(`✅ PowerShell function added to profile`);
console.error(' Restart your terminal to use: claude-mem <command>');
}
} else {
// Unix: Add alias to shell configs
const shellConfigs = [
join(homedir(), '.bashrc'),
join(homedir(), '.zshrc')
];
// Create the wrapper script
const cmdContent = `@echo off
"${bunPath}" "${WORKER_CLI}" %*
`;
writeFileSync(cliPath, cmdContent);
writeFileSync(markerPath, new Date().toISOString());
console.error(`✅ CLI installed: ${cliPath}`);
console.error('');
console.error('📋 Add to PATH (run once in PowerShell as Admin):');
console.error(` [Environment]::SetEnvironmentVariable("Path", $env:Path + ";${cliDir}", "User")`);
console.error('');
console.error(' Then restart your terminal and use: npm run worker:start|stop|restart|status');
} catch (error) {
console.error(`⚠️ Could not install CLI: ${error.message}`);
console.error(` You can still use: bun "${WORKER_CLI}" <command>`);
for (const config of shellConfigs) {
if (existsSync(config)) {
const content = readFileSync(config, 'utf-8');
if (!content.includes('alias claude-mem=')) {
writeFileSync(config, content + '\n' + aliasLine + '\n');
console.error(`✅ Alias added to ${config}`);
}
}
}
console.error(' Restart your terminal to use: claude-mem <command>');
}
} else {
// Unix: Create shell script in ~/.local/bin
const cliDir = join(homedir(), '.local', 'bin');
const cliPath = join(cliDir, CLI_NAME);
const markerPath = join(ROOT, '.cli-installed');
// Skip if already installed
if (existsSync(markerPath) && existsSync(cliPath)) return;
try {
// Create directory if needed
if (!existsSync(cliDir)) {
execSync(`mkdir -p "${cliDir}"`, { stdio: 'ignore', shell: true });
}
// Get Bun path for the wrapper
const bunPath = getBunPath() || 'bun';
// Create the wrapper script
const shContent = `#!/usr/bin/env bash
# claude-mem CLI wrapper - manages the worker service
exec "${bunPath}" "${WORKER_CLI}" "$@"
`;
writeFileSync(cliPath, shContent, { mode: 0o755 });
writeFileSync(markerPath, new Date().toISOString());
console.error(`✅ CLI installed: ${cliPath}`);
// Check if ~/.local/bin is in PATH
const pathDirs = (process.env.PATH || '').split(':');
const localBinInPath = pathDirs.some(p => p === cliDir || p === '$HOME/.local/bin' || p.endsWith('/.local/bin'));
if (!localBinInPath) {
console.error('');
console.error('📋 Add to PATH (add to ~/.bashrc or ~/.zshrc):');
console.error(' export PATH="$HOME/.local/bin:$PATH"');
console.error('');
console.error(' Then restart your terminal and use: npm run worker:start|stop|restart|status');
} else {
console.error(' Usage: npm run worker:start|stop|restart|status');
}
} catch (error) {
console.error(`⚠️ Could not install CLI: ${error.message}`);
console.error(` You can still use: bun "${WORKER_CLI}" <command>`);
}
writeFileSync(markerPath, new Date().toISOString());
} catch (error) {
console.error(`⚠️ Could not add shell alias: ${error.message}`);
console.error(` Use directly: ${bunPath} "${WORKER_CLI}" <command>`);
}
}
+6 -6
View File
@@ -1,13 +1,13 @@
#!/usr/bin/env bun
import{stdin as k}from"process";var f=JSON.stringify({continue:!0,suppressOutput:!0});import{readFileSync as v,writeFileSync as F,existsSync as x}from"fs";import{join as H}from"path";import{homedir as W}from"os";var h="bugfix,feature,refactor,discovery,decision,change",d="how-it-works,why-it-exists,what-changed,problem-solution,gotcha,pattern,trade-off";var c=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-5",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:"37777",CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:H(W(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_OBSERVATION_TYPES:h,CLAUDE_MEM_CONTEXT_OBSERVATION_CONCEPTS:d,CLAUDE_MEM_CONTEXT_FULL_COUNT:"5",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false"};static getAllDefaults(){return{...this.DEFAULTS}}static get(t){return this.DEFAULTS[t]}static getInt(t){let r=this.get(t);return parseInt(r,10)}static getBool(t){return this.get(t)==="true"}static loadFromFile(t){try{if(!x(t))return this.getAllDefaults();let r=v(t,"utf-8"),e=JSON.parse(r),n=e;if(e.env&&typeof e.env=="object"){n=e.env;try{F(t,JSON.stringify(n,null,2),"utf-8"),g.info("SETTINGS","Migrated settings file from nested to flat schema",{settingsPath:t})}catch(E){g.warn("SETTINGS","Failed to auto-migrate settings file",{settingsPath:t},E)}}let o={...this.DEFAULTS};for(let E of Object.keys(this.DEFAULTS))n[E]!==void 0&&(o[E]=n[E]);return o}catch(r){return g.warn("SETTINGS","Failed to load settings, using defaults",{settingsPath:t},r),this.getAllDefaults()}}};import{appendFileSync as b,existsSync as G,mkdirSync as K}from"fs";import{join as M}from"path";var p=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))(p||{}),A=class{level=null;useColor;logFilePath=null;constructor(){this.useColor=process.stdout.isTTY??!1,this.initializeLogFile()}initializeLogFile(){try{let t=c.get("CLAUDE_MEM_DATA_DIR"),r=M(t,"logs");G(r)||K(r,{recursive:!0});let e=new Date().toISOString().split("T")[0];this.logFilePath=M(r,`claude-mem-${e}.log`)}catch(t){console.error("[LOGGER] Failed to initialize log file:",t),this.logFilePath=null}}getLevel(){if(this.level===null)try{let t=c.get("CLAUDE_MEM_DATA_DIR"),r=M(t,"settings.json"),n=c.loadFromFile(r).CLAUDE_MEM_LOG_LEVEL.toUpperCase();this.level=p[n]??1}catch(t){console.error("[LOGGER] Failed to load settings, using INFO level:",t),this.level=1}return this.level}correlationId(t,r){return`obs-${t}-${r}`}sessionId(t){return`session-${t}`}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?JSON.stringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=typeof r=="string"?JSON.parse(r):r;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),o=String(t.getHours()).padStart(2,"0"),E=String(t.getMinutes()).padStart(2,"0"),i=String(t.getSeconds()).padStart(2,"0"),_=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${n} ${o}:${E}:${i}.${_}`}log(t,r,e,n,o){if(t<this.getLevel())return;let E=this.formatTimestamp(new Date),i=p[t].padEnd(5),_=r.padEnd(6),a="";n?.correlationId?a=`[${n.correlationId}] `:n?.sessionId&&(a=`[session-${n.sessionId}] `);let l="";o!=null&&(o instanceof Error?l=this.getLevel()===0?`
import{stdin as $}from"process";var f=JSON.stringify({continue:!0,suppressOutput:!0});import{readFileSync as w,writeFileSync as v,existsSync as F}from"fs";import{join as x}from"path";import{homedir as H}from"os";var d="bugfix,feature,refactor,discovery,decision,change",h="how-it-works,why-it-exists,what-changed,problem-solution,gotcha,pattern,trade-off";var g=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-5",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:"37777",CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:x(H(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_OBSERVATION_TYPES:d,CLAUDE_MEM_CONTEXT_OBSERVATION_CONCEPTS:h,CLAUDE_MEM_CONTEXT_FULL_COUNT:"5",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false"};static getAllDefaults(){return{...this.DEFAULTS}}static get(t){return this.DEFAULTS[t]}static getInt(t){let r=this.get(t);return parseInt(r,10)}static getBool(t){return this.get(t)==="true"}static loadFromFile(t){try{if(!F(t))return this.getAllDefaults();let r=w(t,"utf-8"),e=JSON.parse(r),n=e;if(e.env&&typeof e.env=="object"){n=e.env;try{v(t,JSON.stringify(n,null,2),"utf-8"),c.info("SETTINGS","Migrated settings file from nested to flat schema",{settingsPath:t})}catch(E){c.warn("SETTINGS","Failed to auto-migrate settings file",{settingsPath:t},E)}}let o={...this.DEFAULTS};for(let E of Object.keys(this.DEFAULTS))n[E]!==void 0&&(o[E]=n[E]);return o}catch(r){return c.warn("SETTINGS","Failed to load settings, using defaults",{settingsPath:t},r),this.getAllDefaults()}}};import{appendFileSync as W,existsSync as b,mkdirSync as G}from"fs";import{join as T}from"path";var M=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))(M||{}),p=class{level=null;useColor;logFilePath=null;constructor(){this.useColor=process.stdout.isTTY??!1,this.initializeLogFile()}initializeLogFile(){try{let t=g.get("CLAUDE_MEM_DATA_DIR"),r=T(t,"logs");b(r)||G(r,{recursive:!0});let e=new Date().toISOString().split("T")[0];this.logFilePath=T(r,`claude-mem-${e}.log`)}catch(t){console.error("[LOGGER] Failed to initialize log file:",t),this.logFilePath=null}}getLevel(){if(this.level===null)try{let t=g.get("CLAUDE_MEM_DATA_DIR"),r=T(t,"settings.json"),n=g.loadFromFile(r).CLAUDE_MEM_LOG_LEVEL.toUpperCase();this.level=M[n]??1}catch(t){console.error("[LOGGER] Failed to load settings, using INFO level:",t),this.level=1}return this.level}correlationId(t,r){return`obs-${t}-${r}`}sessionId(t){return`session-${t}`}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?JSON.stringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=typeof r=="string"?JSON.parse(r):r;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),o=String(t.getHours()).padStart(2,"0"),E=String(t.getMinutes()).padStart(2,"0"),i=String(t.getSeconds()).padStart(2,"0"),_=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${n} ${o}:${E}:${i}.${_}`}log(t,r,e,n,o){if(t<this.getLevel())return;let E=this.formatTimestamp(new Date),i=M[t].padEnd(5),_=r.padEnd(6),a="";n?.correlationId?a=`[${n.correlationId}] `:n?.sessionId&&(a=`[session-${n.sessionId}] `);let l="";o!=null&&(o instanceof Error?l=this.getLevel()===0?`
${o.message}
${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?l=`
`+JSON.stringify(o,null,2):l=" "+this.formatData(o));let S="";if(n){let{sessionId:U,memorySessionId:tt,correlationId:et,...R}=n;Object.keys(R).length>0&&(S=` {${Object.entries(R).map(([P,w])=>`${P}=${w}`).join(", ")}}`)}let D=`[${E}] [${i}] [${_}] ${a}${e}${S}${l}`;if(this.logFilePath)try{b(this.logFilePath,D+`
`+JSON.stringify(o,null,2):l=" "+this.formatData(o));let O="";if(n){let{sessionId:U,memorySessionId:Z,correlationId:tt,...R}=n;Object.keys(R).length>0&&(O=` {${Object.entries(R).map(([k,P])=>`${k}=${P}`).join(", ")}}`)}let D=`[${E}] [${i}] [${_}] ${a}${e}${O}${l}`;if(this.logFilePath)try{W(this.logFilePath,D+`
`,"utf8")}catch(U){process.stderr.write(`[LOGGER] Failed to write to log file: ${U}
`)}else process.stderr.write(D+`
`)}debug(t,r,e,n){this.log(0,t,r,e,n)}info(t,r,e,n){this.log(1,t,r,e,n)}warn(t,r,e,n){this.log(2,t,r,e,n)}error(t,r,e,n){this.log(3,t,r,e,n)}dataIn(t,r,e,n){this.info(t,`\u2192 ${r}`,e,n)}dataOut(t,r,e,n){this.info(t,`\u2190 ${r}`,e,n)}success(t,r,e,n){this.info(t,`\u2713 ${r}`,e,n)}failure(t,r,e,n){this.error(t,`\u2717 ${r}`,e,n)}timing(t,r,e,n){this.info(t,`\u23F1 ${r}`,n,{duration:`${e}ms`})}happyPathError(t,r,e,n,o=""){let a=((new Error().stack||"").split(`
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),l=a?`${a[1].split("/").pop()}:${a[2]}`:"unknown",S={...e,location:l};return this.warn(t,`[HAPPY-PATH] ${r}`,S,n),o}},g=new A;import L from"path";import{homedir as X}from"os";import{readFileSync as V}from"fs";var u={DEFAULT:3e5,HEALTH_CHECK:3e4,WORKER_STARTUP_WAIT:1e3,WORKER_STARTUP_RETRIES:300,PRE_RESTART_SETTLE_DELAY:2e3,WINDOWS_MULTIPLIER:1.5};function I(s){return process.platform==="win32"?Math.round(s*u.WINDOWS_MULTIPLIER):s}function N(s={}){let{port:t,includeSkillFallback:r=!1,customPrefix:e,actualError:n}=s,o=e||"Worker service connection failed.",E=t?` (port ${t})`:"",i=`${o}${E}
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),l=a?`${a[1].split("/").pop()}:${a[2]}`:"unknown",O={...e,location:l};return this.warn(t,`[HAPPY-PATH] ${r}`,O,n),o}},c=new p;import L from"path";import{homedir as K}from"os";import{readFileSync as X}from"fs";var A={DEFAULT:3e5,HEALTH_CHECK:3e4,WORKER_STARTUP_WAIT:1e3,WORKER_STARTUP_RETRIES:300,PRE_RESTART_SETTLE_DELAY:2e3,WINDOWS_MULTIPLIER:1.5};function I(s){return process.platform==="win32"?Math.round(s*A.WINDOWS_MULTIPLIER):s}function N(s={}){let{port:t,includeSkillFallback:r=!1,customPrefix:e,actualError:n}=s,o=e||"Worker service connection failed.",E=t?` (port ${t})`:"",i=`${o}${E}
`;return i+=`To restart the worker:
`,i+=`1. Exit Claude Code completely
@@ -16,8 +16,8 @@ ${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?l=`
If that doesn't work, try: /troubleshoot`),n&&(i=`Worker Error: ${n}
${i}`),i}var j=L.join(X(),".claude","plugins","marketplaces","thedotmack"),y=I(u.HEALTH_CHECK),T=null;function O(){if(T!==null)return T;let s=L.join(c.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),t=c.loadFromFile(s);return T=parseInt(t.CLAUDE_MEM_WORKER_PORT,10),T}async function B(){let s=O();return(await fetch(`http://127.0.0.1:${s}/api/readiness`,{signal:AbortSignal.timeout(y)})).ok}function Y(){let s=L.join(j,"package.json");return JSON.parse(V(s,"utf-8")).version}async function J(){let s=O(),t=await fetch(`http://127.0.0.1:${s}/api/version`,{signal:AbortSignal.timeout(y)});if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function q(){let s=Y(),t=await J();s!==t&&g.warn("SYSTEM","Worker version mismatch",{pluginVersion:s,workerVersion:t,hint:"Restart worker with: claude-mem worker restart"})}async function $(){for(let r=0;r<25;r++){try{if(await B()){await q();return}}catch{}await new Promise(e=>setTimeout(e,200))}throw new Error(N({port:O(),customPrefix:"Worker did not become ready within 5 seconds."}))}import{readFileSync as z,existsSync as Q}from"fs";function m(s,t,r=!1){if(!s||!Q(s))throw new Error(`Transcript path missing or file does not exist: ${s}`);let e=z(s,"utf-8").trim();if(!e)throw new Error(`Transcript file exists but is empty: ${s}`);let n=e.split(`
${i}`),i}var V=L.join(K(),".claude","plugins","marketplaces","thedotmack"),Ct=I(A.HEALTH_CHECK),S=null;function u(){if(S!==null)return S;let s=L.join(g.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),t=g.loadFromFile(s);return S=parseInt(t.CLAUDE_MEM_WORKER_PORT,10),S}async function j(){let s=u();return(await fetch(`http://127.0.0.1:${s}/api/readiness`)).ok}function B(){let s=L.join(V,"package.json");return JSON.parse(X(s,"utf-8")).version}async function Y(){let s=u(),t=await fetch(`http://127.0.0.1:${s}/api/version`);if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function J(){let s=B(),t=await Y();s!==t&&c.debug("SYSTEM","Version check",{pluginVersion:s,workerVersion:t,note:"Mismatch will be auto-restarted by worker-service start command"})}async function y(){for(let r=0;r<75;r++){try{if(await j()){await J();return}}catch{}await new Promise(e=>setTimeout(e,200))}throw new Error(N({port:u(),customPrefix:"Worker did not become ready within 15 seconds."}))}import{readFileSync as q,existsSync as z}from"fs";function m(s,t,r=!1){if(!s||!z(s))throw new Error(`Transcript path missing or file does not exist: ${s}`);let e=q(s,"utf-8").trim();if(!e)throw new Error(`Transcript file exists but is empty: ${s}`);let n=e.split(`
`),o=!1;for(let E=n.length-1;E>=0;E--){let i=JSON.parse(n[E]);if(i.type===t&&(o=!0,i.message?.content)){let _="",a=i.message.content;if(typeof a=="string")_=a;else if(Array.isArray(a))_=a.filter(l=>l.type==="text").map(l=>l.text).join(`
`);else throw new Error(`Unknown message content format in transcript. Type: ${typeof a}`);return r&&(_=_.replace(/<system-reminder>[\s\S]*?<\/system-reminder>/g,""),_=_.replace(/\n{3,}/g,`
`).trim()),_}}if(!o)throw new Error(`No message found for role '${t}' in transcript: ${s}`);return""}async function Z(s){if(await $(),!s)throw new Error("summaryHook requires input");let{session_id:t}=s,r=O();if(!s.transcript_path)throw new Error(`Missing transcript_path in Stop hook input for session ${t}`);let e=m(s.transcript_path,"user"),n=m(s.transcript_path,"assistant",!0);g.dataIn("HOOK","Stop: Requesting summary",{workerPort:r,hasLastUserMessage:!!e,hasLastAssistantMessage:!!n});let o=await fetch(`http://127.0.0.1:${r}/api/sessions/summarize`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({contentSessionId:t,last_user_message:e,last_assistant_message:n}),signal:AbortSignal.timeout(u.DEFAULT)});if(!o.ok)throw console.log(f),new Error(`Summary generation failed: ${o.status}`);g.debug("HOOK","Summary request sent successfully"),console.log(f)}var C="";k.on("data",s=>C+=s);k.on("end",async()=>{let s;try{s=C?JSON.parse(C):void 0}catch(t){throw new Error(`Failed to parse hook input: ${t instanceof Error?t.message:String(t)}`)}await Z(s)});
`).trim()),_}}if(!o)throw new Error(`No message found for role '${t}' in transcript: ${s}`);return""}async function Q(s){if(await y(),!s)throw new Error("summaryHook requires input");let{session_id:t}=s,r=u();if(!s.transcript_path)throw new Error(`Missing transcript_path in Stop hook input for session ${t}`);let e=m(s.transcript_path,"user"),n=m(s.transcript_path,"assistant",!0);c.dataIn("HOOK","Stop: Requesting summary",{workerPort:r,hasLastUserMessage:!!e,hasLastAssistantMessage:!!n});let o=await fetch(`http://127.0.0.1:${r}/api/sessions/summarize`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({contentSessionId:t,last_user_message:e,last_assistant_message:n})});if(!o.ok)throw console.log(f),new Error(`Summary generation failed: ${o.status}`);c.debug("HOOK","Summary request sent successfully"),console.log(f)}var C="";$.on("data",s=>C+=s);$.on("end",async()=>{let s;try{s=C?JSON.parse(C):void 0}catch(t){throw new Error(`Failed to parse hook input: ${t instanceof Error?t.message:String(t)}`)}await Q(s)});
+12 -12
View File
@@ -1,30 +1,30 @@
#!/usr/bin/env bun
import{basename as z}from"path";import L from"path";import{homedir as K}from"os";import{readFileSync as X}from"fs";import{readFileSync as v,writeFileSync as F,existsSync as w}from"fs";import{join as W}from"path";import{homedir as b}from"os";var U="bugfix,feature,refactor,discovery,decision,change",R="how-it-works,why-it-exists,what-changed,problem-solution,gotcha,pattern,trade-off";var _=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-5",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:"37777",CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:W(b(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_OBSERVATION_TYPES:U,CLAUDE_MEM_CONTEXT_OBSERVATION_CONCEPTS:R,CLAUDE_MEM_CONTEXT_FULL_COUNT:"5",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false"};static getAllDefaults(){return{...this.DEFAULTS}}static get(t){return this.DEFAULTS[t]}static getInt(t){let r=this.get(t);return parseInt(r,10)}static getBool(t){return this.get(t)==="true"}static loadFromFile(t){try{if(!w(t))return this.getAllDefaults();let r=v(t,"utf-8"),e=JSON.parse(r),n=e;if(e.env&&typeof e.env=="object"){n=e.env;try{F(t,JSON.stringify(n,null,2),"utf-8"),c.info("SETTINGS","Migrated settings file from nested to flat schema",{settingsPath:t})}catch(E){c.warn("SETTINGS","Failed to auto-migrate settings file",{settingsPath:t},E)}}let o={...this.DEFAULTS};for(let E of Object.keys(this.DEFAULTS))n[E]!==void 0&&(o[E]=n[E]);return o}catch(r){return c.warn("SETTINGS","Failed to load settings, using defaults",{settingsPath:t},r),this.getAllDefaults()}}};import{appendFileSync as x,existsSync as G,mkdirSync as H}from"fs";import{join as O}from"path";var S=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))(S||{}),p=class{level=null;useColor;logFilePath=null;constructor(){this.useColor=process.stdout.isTTY??!1,this.initializeLogFile()}initializeLogFile(){try{let t=_.get("CLAUDE_MEM_DATA_DIR"),r=O(t,"logs");G(r)||H(r,{recursive:!0});let e=new Date().toISOString().split("T")[0];this.logFilePath=O(r,`claude-mem-${e}.log`)}catch(t){console.error("[LOGGER] Failed to initialize log file:",t),this.logFilePath=null}}getLevel(){if(this.level===null)try{let t=_.get("CLAUDE_MEM_DATA_DIR"),r=O(t,"settings.json"),n=_.loadFromFile(r).CLAUDE_MEM_LOG_LEVEL.toUpperCase();this.level=S[n]??1}catch(t){console.error("[LOGGER] Failed to load settings, using INFO level:",t),this.level=1}return this.level}correlationId(t,r){return`obs-${t}-${r}`}sessionId(t){return`session-${t}`}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?JSON.stringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=typeof r=="string"?JSON.parse(r):r;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),o=String(t.getHours()).padStart(2,"0"),E=String(t.getMinutes()).padStart(2,"0"),s=String(t.getSeconds()).padStart(2,"0"),T=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${n} ${o}:${E}:${s}.${T}`}log(t,r,e,n,o){if(t<this.getLevel())return;let E=this.formatTimestamp(new Date),s=S[t].padEnd(5),T=r.padEnd(6),a="";n?.correlationId?a=`[${n.correlationId}] `:n?.sessionId&&(a=`[session-${n.sessionId}] `);let l="";o!=null&&(o instanceof Error?l=this.getLevel()===0?`
import{basename as J}from"path";import f from"path";import{homedir as H}from"os";import{readFileSync as K}from"fs";import{readFileSync as k,writeFileSync as v,existsSync as F}from"fs";import{join as w}from"path";import{homedir as W}from"os";var U="bugfix,feature,refactor,discovery,decision,change",R="how-it-works,why-it-exists,what-changed,problem-solution,gotcha,pattern,trade-off";var _=class{static DEFAULTS={CLAUDE_MEM_MODEL:"claude-sonnet-4-5",CLAUDE_MEM_CONTEXT_OBSERVATIONS:"50",CLAUDE_MEM_WORKER_PORT:"37777",CLAUDE_MEM_WORKER_HOST:"127.0.0.1",CLAUDE_MEM_SKIP_TOOLS:"ListMcpResourcesTool,SlashCommand,Skill,TodoWrite,AskUserQuestion",CLAUDE_MEM_PROVIDER:"claude",CLAUDE_MEM_GEMINI_API_KEY:"",CLAUDE_MEM_GEMINI_MODEL:"gemini-2.5-flash-lite",CLAUDE_MEM_GEMINI_RATE_LIMITING_ENABLED:"true",CLAUDE_MEM_OPENROUTER_API_KEY:"",CLAUDE_MEM_OPENROUTER_MODEL:"xiaomi/mimo-v2-flash:free",CLAUDE_MEM_OPENROUTER_SITE_URL:"",CLAUDE_MEM_OPENROUTER_APP_NAME:"claude-mem",CLAUDE_MEM_OPENROUTER_MAX_CONTEXT_MESSAGES:"20",CLAUDE_MEM_OPENROUTER_MAX_TOKENS:"100000",CLAUDE_MEM_DATA_DIR:w(W(),".claude-mem"),CLAUDE_MEM_LOG_LEVEL:"INFO",CLAUDE_MEM_PYTHON_VERSION:"3.13",CLAUDE_CODE_PATH:"",CLAUDE_MEM_MODE:"code",CLAUDE_MEM_CONTEXT_SHOW_READ_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_WORK_TOKENS:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_AMOUNT:"true",CLAUDE_MEM_CONTEXT_SHOW_SAVINGS_PERCENT:"true",CLAUDE_MEM_CONTEXT_OBSERVATION_TYPES:U,CLAUDE_MEM_CONTEXT_OBSERVATION_CONCEPTS:R,CLAUDE_MEM_CONTEXT_FULL_COUNT:"5",CLAUDE_MEM_CONTEXT_FULL_FIELD:"narrative",CLAUDE_MEM_CONTEXT_SESSION_COUNT:"10",CLAUDE_MEM_CONTEXT_SHOW_LAST_SUMMARY:"true",CLAUDE_MEM_CONTEXT_SHOW_LAST_MESSAGE:"false"};static getAllDefaults(){return{...this.DEFAULTS}}static get(t){return this.DEFAULTS[t]}static getInt(t){let r=this.get(t);return parseInt(r,10)}static getBool(t){return this.get(t)==="true"}static loadFromFile(t){try{if(!F(t))return this.getAllDefaults();let r=k(t,"utf-8"),e=JSON.parse(r),n=e;if(e.env&&typeof e.env=="object"){n=e.env;try{v(t,JSON.stringify(n,null,2),"utf-8"),c.info("SETTINGS","Migrated settings file from nested to flat schema",{settingsPath:t})}catch(E){c.warn("SETTINGS","Failed to auto-migrate settings file",{settingsPath:t},E)}}let o={...this.DEFAULTS};for(let E of Object.keys(this.DEFAULTS))n[E]!==void 0&&(o[E]=n[E]);return o}catch(r){return c.warn("SETTINGS","Failed to load settings, using defaults",{settingsPath:t},r),this.getAllDefaults()}}};import{appendFileSync as b,existsSync as x,mkdirSync as G}from"fs";import{join as O}from"path";var S=(o=>(o[o.DEBUG=0]="DEBUG",o[o.INFO=1]="INFO",o[o.WARN=2]="WARN",o[o.ERROR=3]="ERROR",o[o.SILENT=4]="SILENT",o))(S||{}),p=class{level=null;useColor;logFilePath=null;constructor(){this.useColor=process.stdout.isTTY??!1,this.initializeLogFile()}initializeLogFile(){try{let t=_.get("CLAUDE_MEM_DATA_DIR"),r=O(t,"logs");x(r)||G(r,{recursive:!0});let e=new Date().toISOString().split("T")[0];this.logFilePath=O(r,`claude-mem-${e}.log`)}catch(t){console.error("[LOGGER] Failed to initialize log file:",t),this.logFilePath=null}}getLevel(){if(this.level===null)try{let t=_.get("CLAUDE_MEM_DATA_DIR"),r=O(t,"settings.json"),n=_.loadFromFile(r).CLAUDE_MEM_LOG_LEVEL.toUpperCase();this.level=S[n]??1}catch(t){console.error("[LOGGER] Failed to load settings, using INFO level:",t),this.level=1}return this.level}correlationId(t,r){return`obs-${t}-${r}`}sessionId(t){return`session-${t}`}formatData(t){if(t==null)return"";if(typeof t=="string")return t;if(typeof t=="number"||typeof t=="boolean")return t.toString();if(typeof t=="object"){if(t instanceof Error)return this.getLevel()===0?`${t.message}
${t.stack}`:t.message;if(Array.isArray(t))return`[${t.length} items]`;let r=Object.keys(t);return r.length===0?"{}":r.length<=3?JSON.stringify(t):`{${r.length} keys: ${r.slice(0,3).join(", ")}...}`}return String(t)}formatTool(t,r){if(!r)return t;let e=typeof r=="string"?JSON.parse(r):r;if(t==="Bash"&&e.command)return`${t}(${e.command})`;if(e.file_path)return`${t}(${e.file_path})`;if(e.notebook_path)return`${t}(${e.notebook_path})`;if(t==="Glob"&&e.pattern)return`${t}(${e.pattern})`;if(t==="Grep"&&e.pattern)return`${t}(${e.pattern})`;if(e.url)return`${t}(${e.url})`;if(e.query)return`${t}(${e.query})`;if(t==="Task"){if(e.subagent_type)return`${t}(${e.subagent_type})`;if(e.description)return`${t}(${e.description})`}return t==="Skill"&&e.skill?`${t}(${e.skill})`:t==="LSP"&&e.operation?`${t}(${e.operation})`:t}formatTimestamp(t){let r=t.getFullYear(),e=String(t.getMonth()+1).padStart(2,"0"),n=String(t.getDate()).padStart(2,"0"),o=String(t.getHours()).padStart(2,"0"),E=String(t.getMinutes()).padStart(2,"0"),i=String(t.getSeconds()).padStart(2,"0"),T=String(t.getMilliseconds()).padStart(3,"0");return`${r}-${e}-${n} ${o}:${E}:${i}.${T}`}log(t,r,e,n,o){if(t<this.getLevel())return;let E=this.formatTimestamp(new Date),i=S[t].padEnd(5),T=r.padEnd(6),a="";n?.correlationId?a=`[${n.correlationId}] `:n?.sessionId&&(a=`[session-${n.sessionId}] `);let l="";o!=null&&(o instanceof Error?l=this.getLevel()===0?`
${o.message}
${o.stack}`:` ${o.message}`:this.getLevel()===0&&typeof o=="object"?l=`
`+JSON.stringify(o,null,2):l=" "+this.formatData(o));let u="";if(n){let{sessionId:D,memorySessionId:Z,correlationId:tt,...m}=n;Object.keys(m).length>0&&(u=` {${Object.entries(m).map(([P,k])=>`${P}=${k}`).join(", ")}}`)}let C=`[${E}] [${s}] [${T}] ${a}${e}${u}${l}`;if(this.logFilePath)try{x(this.logFilePath,C+`
`+JSON.stringify(o,null,2):l=" "+this.formatData(o));let u="";if(n){let{sessionId:D,memorySessionId:Q,correlationId:Z,...m}=n;Object.keys(m).length>0&&(u=` {${Object.entries(m).map(([$,P])=>`${$}=${P}`).join(", ")}}`)}let C=`[${E}] [${i}] [${T}] ${a}${e}${u}${l}`;if(this.logFilePath)try{b(this.logFilePath,C+`
`,"utf8")}catch(D){process.stderr.write(`[LOGGER] Failed to write to log file: ${D}
`)}else process.stderr.write(C+`
`)}debug(t,r,e,n){this.log(0,t,r,e,n)}info(t,r,e,n){this.log(1,t,r,e,n)}warn(t,r,e,n){this.log(2,t,r,e,n)}error(t,r,e,n){this.log(3,t,r,e,n)}dataIn(t,r,e,n){this.info(t,`\u2192 ${r}`,e,n)}dataOut(t,r,e,n){this.info(t,`\u2190 ${r}`,e,n)}success(t,r,e,n){this.info(t,`\u2713 ${r}`,e,n)}failure(t,r,e,n){this.error(t,`\u2717 ${r}`,e,n)}timing(t,r,e,n){this.info(t,`\u23F1 ${r}`,n,{duration:`${e}ms`})}happyPathError(t,r,e,n,o=""){let a=((new Error().stack||"").split(`
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),l=a?`${a[1].split("/").pop()}:${a[2]}`:"unknown",u={...e,location:l};return this.warn(t,`[HAPPY-PATH] ${r}`,u,n),o}},c=new p;var A={DEFAULT:3e5,HEALTH_CHECK:3e4,WORKER_STARTUP_WAIT:1e3,WORKER_STARTUP_RETRIES:300,PRE_RESTART_SETTLE_DELAY:2e3,WINDOWS_MULTIPLIER:1.5},h={SUCCESS:0,FAILURE:1,USER_MESSAGE_ONLY:3};function I(i){return process.platform==="win32"?Math.round(i*A.WINDOWS_MULTIPLIER):i}function N(i={}){let{port:t,includeSkillFallback:r=!1,customPrefix:e,actualError:n}=i,o=e||"Worker service connection failed.",E=t?` (port ${t})`:"",s=`${o}${E}
`)[2]||"").match(/at\s+(?:.*\s+)?\(?([^:]+):(\d+):(\d+)\)?/),l=a?`${a[1].split("/").pop()}:${a[2]}`:"unknown",u={...e,location:l};return this.warn(t,`[HAPPY-PATH] ${r}`,u,n),o}},c=new p;var L={DEFAULT:3e5,HEALTH_CHECK:3e4,WORKER_STARTUP_WAIT:1e3,WORKER_STARTUP_RETRIES:300,PRE_RESTART_SETTLE_DELAY:2e3,WINDOWS_MULTIPLIER:1.5},h={SUCCESS:0,FAILURE:1,USER_MESSAGE_ONLY:3};function I(s){return process.platform==="win32"?Math.round(s*L.WINDOWS_MULTIPLIER):s}function d(s={}){let{port:t,includeSkillFallback:r=!1,customPrefix:e,actualError:n}=s,o=e||"Worker service connection failed.",E=t?` (port ${t})`:"",i=`${o}${E}
`;return s+=`To restart the worker:
`,s+=`1. Exit Claude Code completely
`,s+=`2. Run: npm run worker:restart
`,s+="3. Restart Claude Code",r&&(s+=`
`;return i+=`To restart the worker:
`,i+=`1. Exit Claude Code completely
`,i+=`2. Run: npm run worker:restart
`,i+="3. Restart Claude Code",r&&(i+=`
If that doesn't work, try: /troubleshoot`),n&&(s=`Worker Error: ${n}
If that doesn't work, try: /troubleshoot`),n&&(i=`Worker Error: ${n}
${s}`),s}var V=L.join(K(),".claude","plugins","marketplaces","thedotmack"),d=I(A.HEALTH_CHECK),M=null;function g(){if(M!==null)return M;let i=L.join(_.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),t=_.loadFromFile(i);return M=parseInt(t.CLAUDE_MEM_WORKER_PORT,10),M}async function j(){let i=g();return(await fetch(`http://127.0.0.1:${i}/api/readiness`,{signal:AbortSignal.timeout(d)})).ok}function B(){let i=L.join(V,"package.json");return JSON.parse(X(i,"utf-8")).version}async function Y(){let i=g(),t=await fetch(`http://127.0.0.1:${i}/api/version`,{signal:AbortSignal.timeout(d)});if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function J(){let i=B(),t=await Y();i!==t&&c.warn("SYSTEM","Worker version mismatch",{pluginVersion:i,workerVersion:t,hint:"Restart worker with: claude-mem worker restart"})}async function y(){for(let r=0;r<25;r++){try{if(await j()){await J();return}}catch{}await new Promise(e=>setTimeout(e,200))}throw new Error(N({port:g(),customPrefix:"Worker did not become ready within 5 seconds."}))}await y();var $=g(),q=z(process.cwd()),f=await fetch(`http://127.0.0.1:${$}/api/context/inject?project=${encodeURIComponent(q)}&colors=true`,{method:"GET",signal:AbortSignal.timeout(5e3)});if(!f.ok)throw new Error(`Failed to fetch context: ${f.status}`);var Q=await f.text();console.error(`
${i}`),i}var X=f.join(H(),".claude","plugins","marketplaces","thedotmack"),At=I(L.HEALTH_CHECK),M=null;function g(){if(M!==null)return M;let s=f.join(_.get("CLAUDE_MEM_DATA_DIR"),"settings.json"),t=_.loadFromFile(s);return M=parseInt(t.CLAUDE_MEM_WORKER_PORT,10),M}async function V(){let s=g();return(await fetch(`http://127.0.0.1:${s}/api/readiness`)).ok}function j(){let s=f.join(X,"package.json");return JSON.parse(K(s,"utf-8")).version}async function B(){let s=g(),t=await fetch(`http://127.0.0.1:${s}/api/version`);if(!t.ok)throw new Error(`Failed to get worker version: ${t.status}`);return(await t.json()).version}async function Y(){let s=j(),t=await B();s!==t&&c.debug("SYSTEM","Version check",{pluginVersion:s,workerVersion:t,note:"Mismatch will be auto-restarted by worker-service start command"})}async function N(){for(let r=0;r<75;r++){try{if(await V()){await Y();return}}catch{}await new Promise(e=>setTimeout(e,200))}throw new Error(d({port:g(),customPrefix:"Worker did not become ready within 15 seconds."}))}await N();var y=g(),z=J(process.cwd()),A=await fetch(`http://127.0.0.1:${y}/api/context/inject?project=${encodeURIComponent(z)}&colors=true`,{method:"GET"});if(!A.ok)throw new Error(`Failed to fetch context: ${A.status}`);var q=await A.text();console.error(`
\u{1F4DD} Claude-Mem Context Loaded
\u2139\uFE0F Note: This appears as stderr but is informational only
`+Q+`
`+q+`
\u{1F4A1} New! Wrap all or part of any message with <private> ... </private> to prevent storing sensitive information in your observation history.
\u{1F4AC} Community https://discord.gg/J4wttp9vDu
\u{1F4FA} Watch live in browser http://localhost:${$}/
\u{1F4FA} Watch live in browser http://localhost:${y}/
`);process.exit(h.USER_MESSAGE_ONLY);
File diff suppressed because one or more lines are too long
+1
View File
@@ -197,6 +197,7 @@ async function buildHooks() {
console.log(` - Worker: worker-service.cjs`);
console.log(` - MCP Server: mcp-server.cjs`);
console.log('\n💡 Note: Dependencies will be auto-installed on first hook execution');
console.log('📝 Cursor hooks are in cursor-hooks/ (no build needed - plain shell scripts)');
} catch (error) {
console.error('\n❌ Build failed:', error.message);
+3 -1
View File
@@ -29,7 +29,9 @@ async function contextHook(input?: SessionStartInput): Promise<string> {
const url = `http://127.0.0.1:${port}/api/context/inject?project=${encodeURIComponent(project)}`;
const response = await fetch(url, { signal: AbortSignal.timeout(HOOK_TIMEOUTS.DEFAULT) });
// Note: Removed AbortSignal.timeout due to Windows Bun cleanup issue (libuv assertion)
// Worker service has its own timeouts, so client-side timeout is redundant
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Context generation failed: ${response.status}`);
+4 -4
View File
@@ -39,8 +39,8 @@ async function newHook(input?: UserPromptSubmitInput): Promise<void> {
contentSessionId: session_id,
project,
prompt
}),
signal: AbortSignal.timeout(5000)
})
// Note: Removed signal to avoid Windows Bun cleanup issue (libuv assertion)
});
if (!initResponse.ok) {
@@ -72,8 +72,8 @@ async function newHook(input?: UserPromptSubmitInput): Promise<void> {
const response = await fetch(`http://127.0.0.1:${port}/sessions/${sessionDbId}/init`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ userPrompt: cleanedPrompt, promptNumber }),
signal: AbortSignal.timeout(5000)
body: JSON.stringify({ userPrompt: cleanedPrompt, promptNumber })
// Note: Removed signal to avoid Windows Bun cleanup issue (libuv assertion)
});
if (!response.ok) {
+2 -2
View File
@@ -56,8 +56,8 @@ async function saveHook(input?: PostToolUseInput): Promise<void> {
tool_input,
tool_response,
cwd
}),
signal: AbortSignal.timeout(HOOK_TIMEOUTS.DEFAULT)
})
// Note: Removed signal to avoid Windows Bun cleanup issue (libuv assertion)
});
if (!response.ok) {
+2 -2
View File
@@ -60,8 +60,8 @@ async function summaryHook(input?: StopInput): Promise<void> {
contentSessionId: session_id,
last_user_message: lastUserMessage,
last_assistant_message: lastAssistantMessage
}),
signal: AbortSignal.timeout(HOOK_TIMEOUTS.DEFAULT)
})
// Note: Removed signal to avoid Windows Bun cleanup issue (libuv assertion)
});
if (!response.ok) {
+2 -1
View File
@@ -18,9 +18,10 @@ const port = getWorkerPort();
const project = basename(process.cwd());
// Fetch formatted context directly from worker API
// Note: Removed AbortSignal.timeout to avoid Windows Bun cleanup issue (libuv assertion)
const response = await fetch(
`http://127.0.0.1:${port}/api/context/inject?project=${encodeURIComponent(project)}&colors=true`,
{ method: 'GET', signal: AbortSignal.timeout(5000) }
{ method: 'GET' }
);
if (!response.ok) {
File diff suppressed because it is too large Load Diff
+5
View File
@@ -20,6 +20,8 @@ import { buildInitPrompt, buildObservationPrompt, buildSummaryPrompt, buildConti
import { SettingsDefaultsManager } from '../../shared/SettingsDefaultsManager.js';
import type { ActiveSession, ConversationMessage } from '../worker-types.js';
import { ModeManager } from '../domain/ModeManager.js';
import { updateCursorContextForProject } from '../worker-service.js';
import { getWorkerPort } from '../../shared/worker-utils.js';
// Gemini API endpoint
const GEMINI_API_URL = 'https://generativelanguage.googleapis.com/v1beta/models';
@@ -493,6 +495,9 @@ export class GeminiAgent {
}
});
}
// Update Cursor context file for registered projects (fire-and-forget)
updateCursorContextForProject(session.project, getWorkerPort()).catch(() => {});
}
// Mark messages as processed
+5
View File
@@ -20,6 +20,8 @@ import { SettingsDefaultsManager } from '../../shared/SettingsDefaultsManager.js
import { USER_SETTINGS_PATH } from '../../shared/paths.js';
import type { ActiveSession, ConversationMessage } from '../worker-types.js';
import { ModeManager } from '../domain/ModeManager.js';
import { updateCursorContextForProject } from '../worker-service.js';
import { getWorkerPort } from '../../shared/worker-utils.js';
// OpenRouter API endpoint
const OPENROUTER_API_URL = 'https://openrouter.ai/api/v1/chat/completions';
@@ -536,6 +538,9 @@ export class OpenRouterAgent {
}
});
}
// Update Cursor context file for registered projects (fire-and-forget)
updateCursorContextForProject(session.project, getWorkerPort()).catch(() => {});
}
// Mark messages as processed
+5
View File
@@ -20,6 +20,8 @@ import { SettingsDefaultsManager } from '../../shared/SettingsDefaultsManager.js
import { USER_SETTINGS_PATH } from '../../shared/paths.js';
import type { ActiveSession, SDKUserMessage, PendingMessage } from '../worker-types.js';
import { ModeManager } from '../domain/ModeManager.js';
import { updateCursorContextForProject } from '../worker-service.js';
import { getWorkerPort } from '../../shared/worker-utils.js';
// Import Agent SDK (assumes it's installed)
// @ts-ignore - Agent SDK types may not be available
@@ -474,6 +476,9 @@ export class SDKAgent {
}
});
}
// Update Cursor context file for registered projects (fire-and-forget)
updateCursorContextForProject(session.project, getWorkerPort()).catch(() => {});
}
// Mark messages as processed after successful observation/summary storage
+12 -12
View File
@@ -62,9 +62,8 @@ export function clearPortCache(): void {
*/
async function isWorkerHealthy(): Promise<boolean> {
const port = getWorkerPort();
const response = await fetch(`http://127.0.0.1:${port}/api/readiness`, {
signal: AbortSignal.timeout(HEALTH_CHECK_TIMEOUT_MS)
});
// Note: Removed AbortSignal.timeout to avoid Windows Bun cleanup issue (libuv assertion)
const response = await fetch(`http://127.0.0.1:${port}/api/readiness`);
return response.ok;
}
@@ -82,9 +81,8 @@ function getPluginVersion(): string {
*/
async function getWorkerVersion(): Promise<string> {
const port = getWorkerPort();
const response = await fetch(`http://127.0.0.1:${port}/api/version`, {
signal: AbortSignal.timeout(HEALTH_CHECK_TIMEOUT_MS)
});
// Note: Removed AbortSignal.timeout to avoid Windows Bun cleanup issue (libuv assertion)
const response = await fetch(`http://127.0.0.1:${port}/api/version`);
if (!response.ok) {
throw new Error(`Failed to get worker version: ${response.status}`);
}
@@ -94,17 +92,19 @@ async function getWorkerVersion(): Promise<string> {
/**
* Check if worker version matches plugin version
* Logs a warning if mismatch is detected
* Note: Auto-restart on version mismatch is now handled in worker-service.ts start command (issue #484)
* This function logs for informational purposes only
*/
async function checkWorkerVersion(): Promise<void> {
const pluginVersion = getPluginVersion();
const workerVersion = await getWorkerVersion();
if (pluginVersion !== workerVersion) {
logger.warn('SYSTEM', 'Worker version mismatch', {
// Just log debug info - auto-restart handles the mismatch in worker-service.ts
logger.debug('SYSTEM', 'Version check', {
pluginVersion,
workerVersion,
hint: 'Restart worker with: claude-mem worker restart'
note: 'Mismatch will be auto-restarted by worker-service start command'
});
}
}
@@ -112,10 +112,10 @@ async function checkWorkerVersion(): Promise<void> {
/**
* Ensure worker service is running
* Polls until worker is ready (assumes worker-cli.js start was called by hooks.json)
* Polls until worker is ready (assumes worker-service.cjs start was called by hooks.json)
*/
export async function ensureWorkerRunning(): Promise<void> {
const maxRetries = 25; // 5 seconds total
const maxRetries = 75; // 15 seconds total
const pollInterval = 200;
for (let i = 0; i < maxRetries; i++) {
@@ -132,6 +132,6 @@ export async function ensureWorkerRunning(): Promise<void> {
throw new Error(getWorkerRestartInstructions({
port: getWorkerPort(),
customPrefix: 'Worker did not become ready within 5 seconds.'
customPrefix: 'Worker did not become ready within 15 seconds.'
}));
}
+258
View File
@@ -0,0 +1,258 @@
/**
* Cursor Integration Utilities
*
* Pure functions for Cursor project registry, context files, and MCP configuration.
* Designed for testability - all file paths are passed as parameters.
*/
import { existsSync, readFileSync, writeFileSync, mkdirSync, renameSync } from 'fs';
import { join, basename } from 'path';
// ============================================================================
// Types
// ============================================================================
export interface CursorProjectRegistry {
[projectName: string]: {
workspacePath: string;
installedAt: string;
};
}
export interface CursorMcpConfig {
mcpServers: {
[name: string]: {
command: string;
args?: string[];
env?: Record<string, string>;
};
};
}
// ============================================================================
// Project Registry Functions
// ============================================================================
/**
* Read the Cursor project registry from a file
*/
export function readCursorRegistry(registryFile: string): CursorProjectRegistry {
try {
if (!existsSync(registryFile)) return {};
return JSON.parse(readFileSync(registryFile, 'utf-8'));
} catch {
return {};
}
}
/**
* Write the Cursor project registry to a file
*/
export function writeCursorRegistry(registryFile: string, registry: CursorProjectRegistry): void {
const dir = join(registryFile, '..');
mkdirSync(dir, { recursive: true });
writeFileSync(registryFile, JSON.stringify(registry, null, 2));
}
/**
* Register a project in the Cursor registry
*/
export function registerCursorProject(
registryFile: string,
projectName: string,
workspacePath: string
): void {
const registry = readCursorRegistry(registryFile);
registry[projectName] = {
workspacePath,
installedAt: new Date().toISOString()
};
writeCursorRegistry(registryFile, registry);
}
/**
* Unregister a project from the Cursor registry
*/
export function unregisterCursorProject(registryFile: string, projectName: string): void {
const registry = readCursorRegistry(registryFile);
if (registry[projectName]) {
delete registry[projectName];
writeCursorRegistry(registryFile, registry);
}
}
// ============================================================================
// Context File Functions
// ============================================================================
/**
* Write context file to a Cursor project's .cursor/rules directory
* Uses atomic write (temp file + rename) to prevent corruption
*/
export function writeContextFile(workspacePath: string, context: string): void {
const rulesDir = join(workspacePath, '.cursor', 'rules');
const rulesFile = join(rulesDir, 'claude-mem-context.mdc');
const tempFile = `${rulesFile}.tmp`;
mkdirSync(rulesDir, { recursive: true });
const content = `---
alwaysApply: true
description: "Claude-mem context from past sessions (auto-updated)"
---
# Memory Context from Past Sessions
The following context is from claude-mem, a persistent memory system that tracks your coding sessions.
${context}
---
*Updated after last session. Use claude-mem's MCP search tools for more detailed queries.*
`;
// Atomic write: temp file + rename
writeFileSync(tempFile, content);
renameSync(tempFile, rulesFile);
}
/**
* Read context file from a Cursor project's .cursor/rules directory
*/
export function readContextFile(workspacePath: string): string | null {
const rulesFile = join(workspacePath, '.cursor', 'rules', 'claude-mem-context.mdc');
if (!existsSync(rulesFile)) return null;
return readFileSync(rulesFile, 'utf-8');
}
// ============================================================================
// MCP Configuration Functions
// ============================================================================
/**
* Configure claude-mem MCP server in Cursor's mcp.json
* Preserves existing MCP servers
*/
export function configureCursorMcp(mcpJsonPath: string, mcpServerScriptPath: string): void {
const dir = join(mcpJsonPath, '..');
mkdirSync(dir, { recursive: true });
// Load existing config or create new
let config: CursorMcpConfig = { mcpServers: {} };
if (existsSync(mcpJsonPath)) {
try {
config = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
if (!config.mcpServers) {
config.mcpServers = {};
}
} catch {
// Start fresh if corrupt
config = { mcpServers: {} };
}
}
// Add claude-mem MCP server
config.mcpServers['claude-mem'] = {
command: 'node',
args: [mcpServerScriptPath]
};
writeFileSync(mcpJsonPath, JSON.stringify(config, null, 2));
}
/**
* Remove claude-mem MCP server from Cursor's mcp.json
* Preserves other MCP servers
*/
export function removeMcpConfig(mcpJsonPath: string): void {
if (!existsSync(mcpJsonPath)) return;
try {
const config: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
if (config.mcpServers && config.mcpServers['claude-mem']) {
delete config.mcpServers['claude-mem'];
writeFileSync(mcpJsonPath, JSON.stringify(config, null, 2));
}
} catch {
// Ignore errors during cleanup
}
}
// ============================================================================
// JSON Utility Functions (mirrors common.sh logic)
// ============================================================================
/**
* Parse array field syntax like "workspace_roots[0]"
* Returns null for simple fields
*/
export function parseArrayField(field: string): { field: string; index: number } | null {
const match = field.match(/^(.+)\[(\d+)\]$/);
if (!match) return null;
return {
field: match[1],
index: parseInt(match[2], 10)
};
}
/**
* Extract JSON field with fallback (mirrors common.sh json_get)
* Supports array access like "field[0]"
*/
export function jsonGet(json: Record<string, unknown>, field: string, fallback: string = ''): string {
const arrayAccess = parseArrayField(field);
if (arrayAccess) {
const arr = json[arrayAccess.field];
if (!Array.isArray(arr)) return fallback;
const value = arr[arrayAccess.index];
if (value === undefined || value === null) return fallback;
return String(value);
}
const value = json[field];
if (value === undefined || value === null) return fallback;
return String(value);
}
/**
* Get project name from workspace path (mirrors common.sh get_project_name)
*/
export function getProjectName(workspacePath: string): string {
if (!workspacePath) return 'unknown-project';
// Handle Windows drive root (C:\ or C:)
const driveMatch = workspacePath.match(/^([A-Za-z]):[\\\/]?$/);
if (driveMatch) {
return `drive-${driveMatch[1].toUpperCase()}`;
}
// Normalize to forward slashes for cross-platform support
const normalized = workspacePath.replace(/\\/g, '/');
const name = basename(normalized);
if (!name) {
return 'unknown-project';
}
return name;
}
/**
* Check if string is empty/null (mirrors common.sh is_empty)
* Also treats jq's literal "null" string as empty
*/
export function isEmpty(str: string | null | undefined): boolean {
if (str === null || str === undefined) return true;
if (str === '') return true;
if (str === 'null') return true;
if (str === 'empty') return true;
return false;
}
/**
* URL encode a string (mirrors common.sh url_encode)
*/
export function urlEncode(str: string): string {
return encodeURIComponent(str);
}
+220
View File
@@ -0,0 +1,220 @@
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
import { mkdirSync, writeFileSync, existsSync, rmSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
import { writeContextFile, readContextFile } from '../src/utils/cursor-utils';
/**
* Tests for Cursor Context Update functionality
*
* These tests validate that context files are correctly written to
* .cursor/rules/claude-mem-context.mdc for registered projects.
*
* The context file uses Cursor's MDC format with frontmatter.
*/
describe('Cursor Context Update', () => {
let tempDir: string;
let workspacePath: string;
beforeEach(() => {
// Create unique temp directory for each test
tempDir = join(tmpdir(), `cursor-context-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
workspacePath = join(tempDir, 'my-project');
mkdirSync(workspacePath, { recursive: true });
});
afterEach(() => {
// Clean up temp directory
try {
rmSync(tempDir, { recursive: true, force: true });
} catch {
// Ignore cleanup errors
}
});
describe('writeContextFile', () => {
it('creates .cursor/rules directory structure', () => {
writeContextFile(workspacePath, 'test context');
const rulesDir = join(workspacePath, '.cursor', 'rules');
expect(existsSync(rulesDir)).toBe(true);
});
it('creates claude-mem-context.mdc file', () => {
writeContextFile(workspacePath, 'test context');
const rulesFile = join(workspacePath, '.cursor', 'rules', 'claude-mem-context.mdc');
expect(existsSync(rulesFile)).toBe(true);
});
it('includes alwaysApply: true in frontmatter', () => {
writeContextFile(workspacePath, 'test context');
const content = readContextFile(workspacePath);
expect(content).toContain('alwaysApply: true');
});
it('includes description in frontmatter', () => {
writeContextFile(workspacePath, 'test context');
const content = readContextFile(workspacePath);
expect(content).toContain('description: "Claude-mem context from past sessions (auto-updated)"');
});
it('includes the provided context in the file body', () => {
const testContext = `## Recent Session
- Fixed authentication bug
- Added new feature`;
writeContextFile(workspacePath, testContext);
const content = readContextFile(workspacePath);
expect(content).toContain('Fixed authentication bug');
expect(content).toContain('Added new feature');
});
it('includes Memory Context header', () => {
writeContextFile(workspacePath, 'test');
const content = readContextFile(workspacePath);
expect(content).toContain('# Memory Context from Past Sessions');
});
it('includes footer with MCP tools mention', () => {
writeContextFile(workspacePath, 'test');
const content = readContextFile(workspacePath);
expect(content).toContain("Use claude-mem's MCP search tools for more detailed queries");
});
it('uses atomic write (no temp file left behind)', () => {
writeContextFile(workspacePath, 'test context');
const tempFile = join(workspacePath, '.cursor', 'rules', 'claude-mem-context.mdc.tmp');
expect(existsSync(tempFile)).toBe(false);
});
it('overwrites existing context file', () => {
writeContextFile(workspacePath, 'first context');
writeContextFile(workspacePath, 'second context');
const content = readContextFile(workspacePath);
expect(content).not.toContain('first context');
expect(content).toContain('second context');
});
it('handles empty context gracefully', () => {
writeContextFile(workspacePath, '');
const content = readContextFile(workspacePath);
expect(content).toBeDefined();
expect(content).toContain('alwaysApply: true');
});
it('preserves multi-line context with proper formatting', () => {
const multilineContext = `Line 1
Line 2
Line 3
Paragraph 2`;
writeContextFile(workspacePath, multilineContext);
const content = readContextFile(workspacePath);
expect(content).toContain('Line 1\nLine 2\nLine 3');
expect(content).toContain('Paragraph 2');
});
});
describe('MDC format validation', () => {
it('has valid YAML frontmatter delimiters', () => {
writeContextFile(workspacePath, 'test');
const content = readContextFile(workspacePath)!;
const lines = content.split('\n');
// First line should be ---
expect(lines[0]).toBe('---');
// Should have closing --- for frontmatter
const secondDashIndex = lines.indexOf('---', 1);
expect(secondDashIndex).toBeGreaterThan(0);
});
it('frontmatter is parseable as YAML', () => {
writeContextFile(workspacePath, 'test');
const content = readContextFile(workspacePath)!;
const lines = content.split('\n');
const frontmatterEnd = lines.indexOf('---', 1);
const frontmatter = lines.slice(1, frontmatterEnd).join('\n');
// Should contain valid YAML key-value pairs
expect(frontmatter).toMatch(/alwaysApply:\s*true/);
expect(frontmatter).toMatch(/description:\s*"/);
});
it('content after frontmatter is proper markdown', () => {
writeContextFile(workspacePath, 'test');
const content = readContextFile(workspacePath)!;
// Should have markdown header
expect(content).toMatch(/^# Memory Context/m);
// Should have horizontal rule (---)
// Note: The footer uses --- which is also a horizontal rule in markdown
const bodyPart = content.split('---')[2]; // After frontmatter
expect(bodyPart).toBeDefined();
});
});
describe('edge cases', () => {
it('handles special characters in context', () => {
const specialContext = '`code` **bold** _italic_ <html> $variable @mention #tag';
writeContextFile(workspacePath, specialContext);
const content = readContextFile(workspacePath);
expect(content).toContain('`code`');
expect(content).toContain('**bold**');
expect(content).toContain('<html>');
});
it('handles unicode in context', () => {
const unicodeContext = 'Emoji: 🚀 Japanese: 日本語 Arabic: العربية';
writeContextFile(workspacePath, unicodeContext);
const content = readContextFile(workspacePath);
expect(content).toContain('🚀');
expect(content).toContain('日本語');
expect(content).toContain('العربية');
});
it('handles very long context', () => {
// 100KB of context
const longContext = 'x'.repeat(100 * 1024);
writeContextFile(workspacePath, longContext);
const content = readContextFile(workspacePath);
expect(content).toContain(longContext);
});
it('works when .cursor directory already exists', () => {
// Pre-create .cursor with other content
mkdirSync(join(workspacePath, '.cursor', 'other'), { recursive: true });
writeFileSync(join(workspacePath, '.cursor', 'other', 'file.txt'), 'existing');
writeContextFile(workspacePath, 'new context');
// Should not destroy existing content
expect(existsSync(join(workspacePath, '.cursor', 'other', 'file.txt'))).toBe(true);
expect(readContextFile(workspacePath)).toContain('new context');
});
});
});
+344
View File
@@ -0,0 +1,344 @@
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
import { execSync, spawn } from 'child_process';
import { mkdirSync, writeFileSync, existsSync, rmSync, readFileSync, chmodSync } from 'fs';
import { join } from 'path';
import { tmpdir, homedir } from 'os';
/**
* Tests for Cursor Hook Script Outputs
*
* These tests validate that hook scripts produce the correct JSON output
* required by Cursor's hook system.
*
* Critical requirements:
* - beforeSubmitPrompt hooks MUST output {"continue": true}
* - stop hooks MUST output valid JSON (usually {} or {"followup_message": "..."})
*
* If these outputs are wrong, Cursor will block prompts or fail silently.
*/
// Skip these tests if jq is not installed (required by the scripts)
function hasJq(): boolean {
try {
execSync('which jq', { stdio: 'pipe' });
return true;
} catch {
return false;
}
}
// Skip these tests on Windows (bash scripts)
function isUnix(): boolean {
return process.platform !== 'win32';
}
const describeOrSkip = (hasJq() && isUnix()) ? describe : describe.skip;
describeOrSkip('Cursor Hook Script Outputs', () => {
let tempDir: string;
let cursorHooksDir: string;
beforeEach(() => {
// Create unique temp directory for each test
tempDir = join(tmpdir(), `cursor-hook-output-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
mkdirSync(tempDir, { recursive: true });
// Find cursor-hooks directory
cursorHooksDir = join(process.cwd(), 'cursor-hooks');
if (!existsSync(cursorHooksDir)) {
throw new Error('cursor-hooks directory not found');
}
});
afterEach(() => {
// Clean up temp directory
try {
rmSync(tempDir, { recursive: true, force: true });
} catch {
// Ignore cleanup errors
}
});
/**
* Run a hook script with input and return the output
*/
function runHookScript(scriptName: string, input: object): string {
const scriptPath = join(cursorHooksDir, scriptName);
if (!existsSync(scriptPath)) {
throw new Error(`Script not found: ${scriptPath}`);
}
// Make sure script is executable
chmodSync(scriptPath, 0o755);
const result = execSync(`bash "${scriptPath}"`, {
input: JSON.stringify(input),
cwd: tempDir,
env: {
...process.env,
HOME: homedir(), // Ensure HOME is set for ~/.claude-mem access
},
encoding: 'utf-8',
timeout: 10000,
});
return result.trim();
}
describe('session-init.sh (beforeSubmitPrompt)', () => {
it('outputs {"continue": true} for valid input', () => {
const input = {
conversation_id: 'test-conv-123',
prompt: 'Hello world',
workspace_roots: [tempDir]
};
const output = runHookScript('session-init.sh', input);
const parsed = JSON.parse(output);
expect(parsed.continue).toBe(true);
});
it('outputs {"continue": true} even with empty input', () => {
const output = runHookScript('session-init.sh', {});
const parsed = JSON.parse(output);
expect(parsed.continue).toBe(true);
});
it('outputs {"continue": true} even with invalid JSON-like input', () => {
const input = {
conversation_id: null,
workspace_roots: null
};
const output = runHookScript('session-init.sh', input);
const parsed = JSON.parse(output);
expect(parsed.continue).toBe(true);
});
it('output is valid JSON', () => {
const input = {
conversation_id: 'test-123',
prompt: 'Test prompt'
};
const output = runHookScript('session-init.sh', input);
// Should not throw
expect(() => JSON.parse(output)).not.toThrow();
});
});
describe('context-inject.sh (beforeSubmitPrompt)', () => {
it('outputs {"continue": true} for valid input', () => {
const input = {
workspace_roots: [tempDir]
};
const output = runHookScript('context-inject.sh', input);
const parsed = JSON.parse(output);
expect(parsed.continue).toBe(true);
});
it('outputs {"continue": true} even with empty input', () => {
const output = runHookScript('context-inject.sh', {});
const parsed = JSON.parse(output);
expect(parsed.continue).toBe(true);
});
it('output is valid JSON', () => {
const output = runHookScript('context-inject.sh', {});
expect(() => JSON.parse(output)).not.toThrow();
});
});
describe('session-summary.sh (stop)', () => {
it('outputs valid JSON for typical input', () => {
const input = {
conversation_id: 'test-conv-456',
workspace_roots: [tempDir],
status: 'completed'
};
const output = runHookScript('session-summary.sh', input);
// Should be valid JSON
expect(() => JSON.parse(output)).not.toThrow();
});
it('outputs empty object {} when nothing to report', () => {
const input = {
// No conversation_id - should exit early with {}
};
const output = runHookScript('session-summary.sh', input);
const parsed = JSON.parse(output);
expect(parsed).toEqual({});
});
it('output is valid JSON even with minimal input', () => {
const output = runHookScript('session-summary.sh', {});
expect(() => JSON.parse(output)).not.toThrow();
});
});
describe('save-observation.sh (afterMCPExecution)', () => {
it('exits cleanly with no output for valid MCP input', () => {
const input = {
conversation_id: 'test-conv-789',
hook_event_name: 'afterMCPExecution',
tool_name: 'Bash',
tool_input: { command: 'ls' },
result_json: { output: 'file1.txt' },
workspace_roots: [tempDir]
};
// This script should exit with 0 and produce no output
const scriptPath = join(cursorHooksDir, 'save-observation.sh');
const result = execSync(`bash "${scriptPath}"`, {
input: JSON.stringify(input),
cwd: tempDir,
encoding: 'utf-8',
timeout: 10000,
});
// Should be empty or just whitespace
expect(result.trim()).toBe('');
});
it('exits cleanly for shell execution input', () => {
const input = {
conversation_id: 'test-conv-101',
hook_event_name: 'afterShellExecution',
command: 'ls -la',
output: 'file1.txt\nfile2.txt',
workspace_roots: [tempDir]
};
const scriptPath = join(cursorHooksDir, 'save-observation.sh');
const result = execSync(`bash "${scriptPath}"`, {
input: JSON.stringify(input),
cwd: tempDir,
encoding: 'utf-8',
timeout: 10000,
});
// Should be empty or just whitespace
expect(result.trim()).toBe('');
});
it('exits cleanly with no session_id', () => {
const input = {
hook_event_name: 'afterMCPExecution',
tool_name: 'Bash'
// No conversation_id or generation_id
};
const scriptPath = join(cursorHooksDir, 'save-observation.sh');
const result = execSync(`bash "${scriptPath}"`, {
input: JSON.stringify(input),
cwd: tempDir,
encoding: 'utf-8',
timeout: 10000,
});
// Should exit cleanly
expect(result.trim()).toBe('');
});
});
describe('save-file-edit.sh (afterFileEdit)', () => {
it('exits cleanly with valid file edit input', () => {
const input = {
conversation_id: 'test-conv-edit',
file_path: '/path/to/file.ts',
edits: [
{ old_string: 'old code', new_string: 'new code' }
],
workspace_roots: [tempDir]
};
const scriptPath = join(cursorHooksDir, 'save-file-edit.sh');
const result = execSync(`bash "${scriptPath}"`, {
input: JSON.stringify(input),
cwd: tempDir,
encoding: 'utf-8',
timeout: 10000,
});
// Should be empty or just whitespace
expect(result.trim()).toBe('');
});
it('exits cleanly with no file_path', () => {
const input = {
conversation_id: 'test-conv-edit',
edits: []
// No file_path - should exit early
};
const scriptPath = join(cursorHooksDir, 'save-file-edit.sh');
const result = execSync(`bash "${scriptPath}"`, {
input: JSON.stringify(input),
cwd: tempDir,
encoding: 'utf-8',
timeout: 10000,
});
// Should exit cleanly
expect(result.trim()).toBe('');
});
});
describe('script error handling', () => {
it('session-init.sh never outputs error to stdout', () => {
// Even with completely broken input, should still output valid JSON
const scriptPath = join(cursorHooksDir, 'session-init.sh');
// Pass invalid input that might cause jq errors
const result = execSync(`echo '{}' | bash "${scriptPath}"`, {
cwd: tempDir,
encoding: 'utf-8',
timeout: 10000,
});
// Output should still be valid JSON with continue: true
const parsed = JSON.parse(result.trim());
expect(parsed.continue).toBe(true);
});
it('context-inject.sh never outputs error to stdout', () => {
const scriptPath = join(cursorHooksDir, 'context-inject.sh');
const result = execSync(`echo '{}' | bash "${scriptPath}"`, {
cwd: tempDir,
encoding: 'utf-8',
timeout: 10000,
});
const parsed = JSON.parse(result.trim());
expect(parsed.continue).toBe(true);
});
it('session-summary.sh never outputs error to stdout', () => {
const scriptPath = join(cursorHooksDir, 'session-summary.sh');
const result = execSync(`echo '{}' | bash "${scriptPath}"`, {
cwd: tempDir,
encoding: 'utf-8',
timeout: 10000,
});
// Should be valid JSON
expect(() => JSON.parse(result.trim())).not.toThrow();
});
});
});
+265
View File
@@ -0,0 +1,265 @@
import { describe, it, expect } from 'bun:test';
import {
parseArrayField,
jsonGet,
getProjectName,
isEmpty,
urlEncode
} from '../src/utils/cursor-utils';
/**
* Tests for Cursor Hooks JSON/Utility Functions
*
* These tests validate the logic used in common.sh bash utilities.
* The TypeScript implementations in cursor-utils.ts mirror the bash logic,
* allowing us to verify correct behavior and catch edge cases.
*
* The bash scripts use these functions:
* - json_get: Extract fields from JSON, including array access
* - get_project_name: Extract project name from workspace path
* - is_empty: Check if a string is empty/null
* - url_encode: URL-encode a string
*/
describe('Cursor Hooks JSON Utilities', () => {
describe('parseArrayField', () => {
it('parses simple array access', () => {
const result = parseArrayField('workspace_roots[0]');
expect(result).toEqual({ field: 'workspace_roots', index: 0 });
});
it('parses array access with higher index', () => {
const result = parseArrayField('items[42]');
expect(result).toEqual({ field: 'items', index: 42 });
});
it('returns null for simple field', () => {
const result = parseArrayField('conversation_id');
expect(result).toBeNull();
});
it('returns null for empty string', () => {
const result = parseArrayField('');
expect(result).toBeNull();
});
it('returns null for malformed array syntax', () => {
expect(parseArrayField('field[]')).toBeNull();
expect(parseArrayField('field[-1]')).toBeNull();
expect(parseArrayField('[0]')).toBeNull();
});
it('handles underscores in field name', () => {
const result = parseArrayField('my_array_field[5]');
expect(result).toEqual({ field: 'my_array_field', index: 5 });
});
});
describe('jsonGet', () => {
const testJson = {
conversation_id: 'conv-123',
workspace_roots: ['/path/to/project', '/another/path'],
nested: { value: 'nested-value' },
empty_string: '',
null_value: null
};
it('gets simple field', () => {
expect(jsonGet(testJson, 'conversation_id')).toBe('conv-123');
});
it('gets array element with [0]', () => {
expect(jsonGet(testJson, 'workspace_roots[0]')).toBe('/path/to/project');
});
it('gets array element with higher index', () => {
expect(jsonGet(testJson, 'workspace_roots[1]')).toBe('/another/path');
});
it('returns fallback for missing field', () => {
expect(jsonGet(testJson, 'nonexistent', 'default')).toBe('default');
});
it('returns fallback for out-of-bounds array access', () => {
expect(jsonGet(testJson, 'workspace_roots[99]', 'default')).toBe('default');
});
it('returns fallback for array access on non-array', () => {
expect(jsonGet(testJson, 'conversation_id[0]', 'default')).toBe('default');
});
it('returns empty string fallback by default', () => {
expect(jsonGet(testJson, 'nonexistent')).toBe('');
});
it('returns fallback for null value', () => {
expect(jsonGet(testJson, 'null_value', 'fallback')).toBe('fallback');
});
it('returns empty string value (not fallback)', () => {
// Empty string is a valid value, should not trigger fallback
expect(jsonGet(testJson, 'empty_string', 'fallback')).toBe('');
});
});
describe('getProjectName', () => {
it('extracts basename from Unix path', () => {
expect(getProjectName('/Users/alex/projects/my-project')).toBe('my-project');
});
it('extracts basename from Windows path', () => {
expect(getProjectName('C:\\Users\\alex\\projects\\my-project')).toBe('my-project');
});
it('handles path with trailing slash', () => {
expect(getProjectName('/path/to/project/')).toBe('project');
});
it('returns unknown-project for empty string', () => {
expect(getProjectName('')).toBe('unknown-project');
});
it('handles Windows drive root C:\\', () => {
expect(getProjectName('C:\\')).toBe('drive-C');
});
it('handles Windows drive root C:', () => {
expect(getProjectName('C:')).toBe('drive-C');
});
it('handles lowercase drive letter', () => {
expect(getProjectName('d:\\')).toBe('drive-D');
});
it('handles project name with dots', () => {
expect(getProjectName('/path/to/my.project.v2')).toBe('my.project.v2');
});
it('handles project name with spaces', () => {
expect(getProjectName('/path/to/My Project')).toBe('My Project');
});
it('handles project name with special characters', () => {
expect(getProjectName('/path/to/project-name_v2.0')).toBe('project-name_v2.0');
});
});
describe('isEmpty', () => {
it('returns true for null', () => {
expect(isEmpty(null)).toBe(true);
});
it('returns true for undefined', () => {
expect(isEmpty(undefined)).toBe(true);
});
it('returns true for empty string', () => {
expect(isEmpty('')).toBe(true);
});
it('returns true for literal "null" string', () => {
// This is important - jq returns "null" as string when value is null
expect(isEmpty('null')).toBe(true);
});
it('returns true for literal "empty" string', () => {
expect(isEmpty('empty')).toBe(true);
});
it('returns false for non-empty string', () => {
expect(isEmpty('some-value')).toBe(false);
});
it('returns false for whitespace-only string', () => {
// Whitespace is not empty
expect(isEmpty(' ')).toBe(false);
});
it('returns false for "0" string', () => {
expect(isEmpty('0')).toBe(false);
});
it('returns false for "false" string', () => {
expect(isEmpty('false')).toBe(false);
});
});
describe('urlEncode', () => {
it('encodes spaces', () => {
expect(urlEncode('hello world')).toBe('hello%20world');
});
it('encodes special characters', () => {
expect(urlEncode('a&b=c')).toBe('a%26b%3Dc');
});
it('encodes unicode', () => {
const encoded = urlEncode('日本語');
expect(encoded).toContain('%');
expect(decodeURIComponent(encoded)).toBe('日本語');
});
it('preserves alphanumeric characters', () => {
expect(urlEncode('abc123')).toBe('abc123');
});
it('preserves dashes and underscores', () => {
expect(urlEncode('my-project_name')).toBe('my-project_name');
});
it('handles empty string', () => {
expect(urlEncode('')).toBe('');
});
it('encodes forward slash', () => {
expect(urlEncode('path/to/file')).toBe('path%2Fto%2Ffile');
});
});
describe('integration: hook payload parsing', () => {
// Simulates parsing a real Cursor hook payload
it('extracts all fields from typical beforeSubmitPrompt payload', () => {
const payload = {
conversation_id: 'abc-123',
generation_id: 'gen-456',
prompt: 'Fix the bug',
workspace_roots: ['/Users/alex/projects/my-project'],
hook_event_name: 'beforeSubmitPrompt'
};
const conversationId = jsonGet(payload, 'conversation_id');
const workspaceRoot = jsonGet(payload, 'workspace_roots[0]');
const projectName = getProjectName(workspaceRoot);
const hookEvent = jsonGet(payload, 'hook_event_name');
expect(conversationId).toBe('abc-123');
expect(workspaceRoot).toBe('/Users/alex/projects/my-project');
expect(projectName).toBe('my-project');
expect(hookEvent).toBe('beforeSubmitPrompt');
});
it('handles payload with missing optional fields', () => {
const payload = {
generation_id: 'gen-456',
// No conversation_id, no workspace_roots
};
const conversationId = jsonGet(payload, 'conversation_id', '');
const workspaceRoot = jsonGet(payload, 'workspace_roots[0]', '');
expect(isEmpty(conversationId)).toBe(true);
expect(isEmpty(workspaceRoot)).toBe(true);
});
it('constructs valid API URL with encoded project name', () => {
const projectName = 'my project (v2)';
const port = 37777;
const encoded = urlEncode(projectName);
const url = `http://127.0.0.1:${port}/api/context/inject?project=${encoded}`;
expect(url).toBe('http://127.0.0.1:37777/api/context/inject?project=my%20project%20(v2)');
});
});
});
+247
View File
@@ -0,0 +1,247 @@
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
import { mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
import {
configureCursorMcp,
removeMcpConfig,
type CursorMcpConfig
} from '../src/utils/cursor-utils';
/**
* Tests for Cursor MCP Configuration
*
* These tests validate the MCP server configuration that gets written
* to .cursor/mcp.json (project-level) or ~/.cursor/mcp.json (user-level).
*
* The config must match Cursor's expected format for MCP servers.
*/
describe('Cursor MCP Configuration', () => {
let tempDir: string;
let mcpJsonPath: string;
const mcpServerPath = '/path/to/mcp-server.cjs';
beforeEach(() => {
// Create unique temp directory for each test
tempDir = join(tmpdir(), `cursor-mcp-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
mkdirSync(tempDir, { recursive: true });
mcpJsonPath = join(tempDir, '.cursor', 'mcp.json');
});
afterEach(() => {
// Clean up temp directory
try {
rmSync(tempDir, { recursive: true, force: true });
} catch {
// Ignore cleanup errors
}
});
describe('configureCursorMcp', () => {
it('creates mcp.json if it does not exist', () => {
configureCursorMcp(mcpJsonPath, mcpServerPath);
expect(existsSync(mcpJsonPath)).toBe(true);
});
it('creates .cursor directory if it does not exist', () => {
configureCursorMcp(mcpJsonPath, mcpServerPath);
expect(existsSync(join(tempDir, '.cursor'))).toBe(true);
});
it('adds claude-mem server with correct structure', () => {
configureCursorMcp(mcpJsonPath, mcpServerPath);
const config: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(config.mcpServers).toBeDefined();
expect(config.mcpServers['claude-mem']).toBeDefined();
expect(config.mcpServers['claude-mem'].command).toBe('node');
expect(config.mcpServers['claude-mem'].args).toEqual([mcpServerPath]);
});
it('preserves existing MCP servers when adding claude-mem', () => {
// Pre-create config with another server
mkdirSync(join(tempDir, '.cursor'), { recursive: true });
const existingConfig = {
mcpServers: {
'other-server': {
command: 'python',
args: ['/path/to/other.py']
}
}
};
writeFileSync(mcpJsonPath, JSON.stringify(existingConfig));
configureCursorMcp(mcpJsonPath, mcpServerPath);
const config: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
// Both servers should exist
expect(config.mcpServers['other-server']).toBeDefined();
expect(config.mcpServers['other-server'].command).toBe('python');
expect(config.mcpServers['claude-mem']).toBeDefined();
});
it('updates existing claude-mem server path', () => {
// First config
configureCursorMcp(mcpJsonPath, '/old/path.cjs');
// Update with new path
const newPath = '/new/path.cjs';
configureCursorMcp(mcpJsonPath, newPath);
const config: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(config.mcpServers['claude-mem'].args).toEqual([newPath]);
});
it('recovers from corrupt mcp.json', () => {
// Create corrupt file
mkdirSync(join(tempDir, '.cursor'), { recursive: true });
writeFileSync(mcpJsonPath, 'not valid json {{{{');
// Should not throw, should overwrite
configureCursorMcp(mcpJsonPath, mcpServerPath);
const config: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(config.mcpServers['claude-mem']).toBeDefined();
});
it('handles mcp.json with missing mcpServers key', () => {
// Create file with empty object
mkdirSync(join(tempDir, '.cursor'), { recursive: true });
writeFileSync(mcpJsonPath, '{}');
configureCursorMcp(mcpJsonPath, mcpServerPath);
const config: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(config.mcpServers['claude-mem']).toBeDefined();
});
});
describe('MCP config format validation', () => {
it('produces valid JSON', () => {
configureCursorMcp(mcpJsonPath, mcpServerPath);
const content = readFileSync(mcpJsonPath, 'utf-8');
// Should not throw
expect(() => JSON.parse(content)).not.toThrow();
});
it('uses pretty-printed JSON (2-space indent)', () => {
configureCursorMcp(mcpJsonPath, mcpServerPath);
const content = readFileSync(mcpJsonPath, 'utf-8');
// Should contain newlines and indentation
expect(content).toContain('\n');
expect(content).toContain(' "mcpServers"');
});
it('matches Cursor MCP server schema', () => {
configureCursorMcp(mcpJsonPath, mcpServerPath);
const config = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
// Top-level must have mcpServers
expect(config).toHaveProperty('mcpServers');
expect(typeof config.mcpServers).toBe('object');
// Each server must have command (string) and optionally args (array)
for (const [name, server] of Object.entries(config.mcpServers)) {
expect(typeof name).toBe('string');
expect((server as { command: string }).command).toBeDefined();
expect(typeof (server as { command: string }).command).toBe('string');
const args = (server as { args?: string[] }).args;
if (args !== undefined) {
expect(Array.isArray(args)).toBe(true);
args.forEach((arg: string) => expect(typeof arg).toBe('string'));
}
}
});
});
describe('removeMcpConfig', () => {
it('removes claude-mem server from config', () => {
configureCursorMcp(mcpJsonPath, mcpServerPath);
removeMcpConfig(mcpJsonPath);
const config: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(config.mcpServers['claude-mem']).toBeUndefined();
});
it('preserves other servers when removing claude-mem', () => {
// Setup: both servers
mkdirSync(join(tempDir, '.cursor'), { recursive: true });
const config = {
mcpServers: {
'other-server': { command: 'python', args: ['/path.py'] },
'claude-mem': { command: 'node', args: ['/mcp.cjs'] }
}
};
writeFileSync(mcpJsonPath, JSON.stringify(config));
removeMcpConfig(mcpJsonPath);
const updated: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(updated.mcpServers['other-server']).toBeDefined();
expect(updated.mcpServers['claude-mem']).toBeUndefined();
});
it('does nothing if mcp.json does not exist', () => {
// Should not throw
expect(() => removeMcpConfig(mcpJsonPath)).not.toThrow();
expect(existsSync(mcpJsonPath)).toBe(false);
});
it('does nothing if claude-mem not in config', () => {
mkdirSync(join(tempDir, '.cursor'), { recursive: true });
const config = {
mcpServers: {
'other-server': { command: 'python', args: ['/path.py'] }
}
};
writeFileSync(mcpJsonPath, JSON.stringify(config));
removeMcpConfig(mcpJsonPath);
const updated: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(updated.mcpServers['other-server']).toBeDefined();
});
});
describe('path handling', () => {
it('handles absolute path with spaces', () => {
const pathWithSpaces = '/path/to/my project/mcp-server.cjs';
configureCursorMcp(mcpJsonPath, pathWithSpaces);
const config: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(config.mcpServers['claude-mem'].args).toEqual([pathWithSpaces]);
});
it('handles Windows-style path', () => {
const windowsPath = 'C:\\Users\\alex\\.claude\\plugins\\mcp-server.cjs';
configureCursorMcp(mcpJsonPath, windowsPath);
const config: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(config.mcpServers['claude-mem'].args).toEqual([windowsPath]);
});
it('handles path with special characters', () => {
const specialPath = "/path/to/project-name_v2.0 (beta)/mcp-server.cjs";
configureCursorMcp(mcpJsonPath, specialPath);
const config: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(config.mcpServers['claude-mem'].args).toEqual([specialPath]);
// Verify it survives JSON round-trip
const reread: CursorMcpConfig = JSON.parse(readFileSync(mcpJsonPath, 'utf-8'));
expect(reread.mcpServers['claude-mem'].args![0]).toBe(specialPath);
});
});
});
+171
View File
@@ -0,0 +1,171 @@
import { describe, it, expect, beforeEach, afterEach } from 'bun:test';
import { mkdirSync, writeFileSync, readFileSync, existsSync, rmSync } from 'fs';
import { join } from 'path';
import { tmpdir } from 'os';
import {
readCursorRegistry,
writeCursorRegistry,
registerCursorProject,
unregisterCursorProject
} from '../src/utils/cursor-utils';
/**
* Tests for Cursor Project Registry functionality
*
* These tests validate the file-based registry that tracks which projects
* have Cursor hooks installed for automatic context updates.
*
* The registry is stored at ~/.claude-mem/cursor-projects.json
*/
describe('Cursor Project Registry', () => {
let tempDir: string;
let registryFile: string;
beforeEach(() => {
// Create unique temp directory for each test
tempDir = join(tmpdir(), `cursor-registry-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
mkdirSync(tempDir, { recursive: true });
registryFile = join(tempDir, 'cursor-projects.json');
});
afterEach(() => {
// Clean up temp directory
try {
rmSync(tempDir, { recursive: true, force: true });
} catch {
// Ignore cleanup errors
}
});
describe('readCursorRegistry', () => {
it('returns empty object when registry file does not exist', () => {
const registry = readCursorRegistry(registryFile);
expect(registry).toEqual({});
});
it('returns empty object when registry file is corrupt JSON', () => {
writeFileSync(registryFile, 'not valid json {{{');
const registry = readCursorRegistry(registryFile);
expect(registry).toEqual({});
});
it('returns parsed registry when file exists', () => {
const expected = {
'my-project': {
workspacePath: '/home/user/projects/my-project',
installedAt: '2025-01-01T00:00:00.000Z'
}
};
writeFileSync(registryFile, JSON.stringify(expected));
const registry = readCursorRegistry(registryFile);
expect(registry).toEqual(expected);
});
});
describe('registerCursorProject', () => {
it('creates registry file if it does not exist', () => {
registerCursorProject(registryFile, 'new-project', '/path/to/project');
expect(existsSync(registryFile)).toBe(true);
});
it('stores project with workspacePath and installedAt', () => {
const before = Date.now();
registerCursorProject(registryFile, 'test-project', '/workspace/test');
const after = Date.now();
const registry = readCursorRegistry(registryFile);
expect(registry['test-project']).toBeDefined();
expect(registry['test-project'].workspacePath).toBe('/workspace/test');
// Verify installedAt is a valid ISO timestamp within the test window
const installedAt = new Date(registry['test-project'].installedAt).getTime();
expect(installedAt).toBeGreaterThanOrEqual(before);
expect(installedAt).toBeLessThanOrEqual(after);
});
it('preserves existing projects when registering new one', () => {
registerCursorProject(registryFile, 'project-a', '/path/a');
registerCursorProject(registryFile, 'project-b', '/path/b');
const registry = readCursorRegistry(registryFile);
expect(Object.keys(registry)).toHaveLength(2);
expect(registry['project-a'].workspacePath).toBe('/path/a');
expect(registry['project-b'].workspacePath).toBe('/path/b');
});
it('overwrites existing project with same name', () => {
registerCursorProject(registryFile, 'my-project', '/old/path');
registerCursorProject(registryFile, 'my-project', '/new/path');
const registry = readCursorRegistry(registryFile);
expect(Object.keys(registry)).toHaveLength(1);
expect(registry['my-project'].workspacePath).toBe('/new/path');
});
it('handles special characters in project name', () => {
const projectName = 'my-project_v2.0 (beta)';
registerCursorProject(registryFile, projectName, '/path/to/project');
const registry = readCursorRegistry(registryFile);
expect(registry[projectName]).toBeDefined();
expect(registry[projectName].workspacePath).toBe('/path/to/project');
});
});
describe('unregisterCursorProject', () => {
it('removes specified project from registry', () => {
registerCursorProject(registryFile, 'project-a', '/path/a');
registerCursorProject(registryFile, 'project-b', '/path/b');
unregisterCursorProject(registryFile, 'project-a');
const registry = readCursorRegistry(registryFile);
expect(registry['project-a']).toBeUndefined();
expect(registry['project-b']).toBeDefined();
});
it('does nothing when unregistering non-existent project', () => {
registerCursorProject(registryFile, 'existing', '/path');
// Should not throw
unregisterCursorProject(registryFile, 'non-existent');
const registry = readCursorRegistry(registryFile);
expect(registry['existing']).toBeDefined();
});
it('handles unregister when registry file does not exist', () => {
// Should not throw even when file doesn't exist
unregisterCursorProject(registryFile, 'any-project');
// File should not be created by unregister
expect(existsSync(registryFile)).toBe(false);
});
});
describe('registry format validation', () => {
it('stores registry as pretty-printed JSON', () => {
registerCursorProject(registryFile, 'test', '/path');
const content = readFileSync(registryFile, 'utf-8');
// Should be indented (pretty-printed)
expect(content).toContain('\n');
expect(content).toContain(' ');
});
it('registry file is valid JSON that can be read by other tools', () => {
registerCursorProject(registryFile, 'project-1', '/path/1');
registerCursorProject(registryFile, 'project-2', '/path/2');
// Read raw and parse with JSON.parse (not our helper)
const content = readFileSync(registryFile, 'utf-8');
const parsed = JSON.parse(content);
expect(parsed).toHaveProperty('project-1');
expect(parsed).toHaveProperty('project-2');
});
});
});