Remove conversation context/recall system from ClaudeTools
Completely removed the database context recall system while preserving database tables for safety. This major cleanup removes 80+ files and 16,831 lines of code. What was removed: - API layer: 4 routers (conversation-contexts, context-snippets, project-states, decision-logs) with 35+ endpoints - Database models: 5 models (ConversationContext, ContextSnippet, DecisionLog, ProjectState, ContextTag) - Services: 4 service layers with business logic - Schemas: 4 Pydantic schema files - Claude Code hooks: 13 hook files (user-prompt-submit, task-complete, sync-contexts, periodic saves) - Scripts: 15+ scripts (import, migration, testing, tombstone checking) - Tests: 5 test files (context recall, compression, diagnostics) - Documentation: 30+ markdown files (guides, architecture, quick starts) - Utilities: context compression, conversation parsing Files modified: - api/main.py: Removed router registrations - api/models/__init__.py: Removed model imports - api/schemas/__init__.py: Removed schema imports - api/services/__init__.py: Removed service imports - .claude/claude.md: Completely rewritten without context references Database tables preserved: - conversation_contexts, context_snippets, context_tags, project_states, decision_logs (5 orphaned tables remain for safety) - Migration created but NOT applied: 20260118_172743_remove_context_system.py - Tables can be dropped later when confirmed not needed New files added: - CONTEXT_SYSTEM_REMOVAL_SUMMARY.md: Detailed removal report - CONTEXT_SYSTEM_REMOVAL_COMPLETE.md: Final status - CONTEXT_EXPORT_RESULTS.md: Export attempt results - scripts/export-tombstoned-contexts.py: Export tool for future use - migrations/versions/20260118_172743_remove_context_system.py Impact: - Reduced from 130 to 95 API endpoints - Reduced from 43 to 38 active database tables - Removed 16,831 lines of code - System fully operational without context recall Reason for removal: - System was not actively used (no tombstoned contexts found) - Reduces codebase complexity - Focuses on core MSP work tracking functionality - Database preserved for safety (can rollback if needed) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,226 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Periodic Context Save Hook
|
||||
# Runs as a background daemon to save context every 5 minutes of active time
|
||||
#
|
||||
# Usage: bash .claude/hooks/periodic-context-save start
|
||||
# bash .claude/hooks/periodic-context-save stop
|
||||
# bash .claude/hooks/periodic-context-save status
|
||||
#
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
CLAUDE_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||
PID_FILE="$CLAUDE_DIR/.periodic-save.pid"
|
||||
STATE_FILE="$CLAUDE_DIR/.periodic-save-state"
|
||||
CONFIG_FILE="$CLAUDE_DIR/context-recall-config.env"
|
||||
|
||||
# Load configuration
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Configuration
|
||||
SAVE_INTERVAL_SECONDS=300 # 5 minutes
|
||||
CHECK_INTERVAL_SECONDS=60 # Check every minute
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
|
||||
# Detect project ID
|
||||
detect_project_id() {
|
||||
# Try git config first
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Try to derive from git remote URL
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
|
||||
echo "$PROJECT_ID"
|
||||
}
|
||||
|
||||
# Check if Claude Code is active (not idle)
|
||||
is_claude_active() {
|
||||
# Check if there are recent Claude Code processes or activity
|
||||
# This is a simple heuristic - can be improved
|
||||
|
||||
# On Windows with Git Bash, check for claude process
|
||||
if command -v tasklist.exe >/dev/null 2>&1; then
|
||||
tasklist.exe 2>/dev/null | grep -i claude >/dev/null 2>&1
|
||||
return $?
|
||||
fi
|
||||
|
||||
# Assume active if we can't detect
|
||||
return 0
|
||||
}
|
||||
|
||||
# Get active time from state file
|
||||
get_active_time() {
|
||||
if [ -f "$STATE_FILE" ]; then
|
||||
cat "$STATE_FILE" | grep "^active_seconds=" | cut -d'=' -f2
|
||||
else
|
||||
echo "0"
|
||||
fi
|
||||
}
|
||||
|
||||
# Update active time in state file
|
||||
update_active_time() {
|
||||
local active_seconds=$1
|
||||
echo "active_seconds=$active_seconds" > "$STATE_FILE"
|
||||
echo "last_update=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> "$STATE_FILE"
|
||||
}
|
||||
|
||||
# Save context to database
|
||||
save_periodic_context() {
|
||||
local project_id=$(detect_project_id)
|
||||
|
||||
# Generate context summary
|
||||
local title="Periodic Save - $(date +"%Y-%m-%d %H:%M")"
|
||||
local summary="Auto-saved context after 5 minutes of active work. Session in progress on project: ${project_id:-unknown}"
|
||||
|
||||
# Create JSON payload
|
||||
local payload=$(cat <<EOF
|
||||
{
|
||||
"context_type": "session_summary",
|
||||
"title": "$title",
|
||||
"dense_summary": "$summary",
|
||||
"relevance_score": 5.0,
|
||||
"tags": "[\"auto-save\", \"periodic\", \"active-session\"]"
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# POST to API
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
curl -s -X POST "${API_URL}/api/conversation-contexts" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$payload" >/dev/null 2>&1
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "[$(date)] Context saved successfully" >&2
|
||||
else
|
||||
echo "[$(date)] Failed to save context" >&2
|
||||
fi
|
||||
else
|
||||
echo "[$(date)] No JWT token - cannot save context" >&2
|
||||
fi
|
||||
}
|
||||
|
||||
# Main monitoring loop
|
||||
monitor_loop() {
|
||||
local active_seconds=0
|
||||
|
||||
echo "[$(date)] Periodic context save daemon started (PID: $$)" >&2
|
||||
echo "[$(date)] Will save context every ${SAVE_INTERVAL_SECONDS}s of active time" >&2
|
||||
|
||||
while true; do
|
||||
# Check if Claude is active
|
||||
if is_claude_active; then
|
||||
# Increment active time
|
||||
active_seconds=$((active_seconds + CHECK_INTERVAL_SECONDS))
|
||||
update_active_time $active_seconds
|
||||
|
||||
# Check if we've reached the save interval
|
||||
if [ $active_seconds -ge $SAVE_INTERVAL_SECONDS ]; then
|
||||
echo "[$(date)] ${SAVE_INTERVAL_SECONDS}s of active time reached - saving context" >&2
|
||||
save_periodic_context
|
||||
|
||||
# Reset timer
|
||||
active_seconds=0
|
||||
update_active_time 0
|
||||
fi
|
||||
else
|
||||
echo "[$(date)] Claude Code inactive - not counting time" >&2
|
||||
fi
|
||||
|
||||
# Wait before next check
|
||||
sleep $CHECK_INTERVAL_SECONDS
|
||||
done
|
||||
}
|
||||
|
||||
# Start daemon
|
||||
start_daemon() {
|
||||
if [ -f "$PID_FILE" ]; then
|
||||
local pid=$(cat "$PID_FILE")
|
||||
if kill -0 $pid 2>/dev/null; then
|
||||
echo "Periodic context save daemon already running (PID: $pid)"
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
|
||||
# Start in background
|
||||
nohup bash "$0" _monitor >> "$CLAUDE_DIR/periodic-save.log" 2>&1 &
|
||||
local pid=$!
|
||||
echo $pid > "$PID_FILE"
|
||||
|
||||
echo "Started periodic context save daemon (PID: $pid)"
|
||||
echo "Logs: $CLAUDE_DIR/periodic-save.log"
|
||||
}
|
||||
|
||||
# Stop daemon
|
||||
stop_daemon() {
|
||||
if [ ! -f "$PID_FILE" ]; then
|
||||
echo "Periodic context save daemon not running"
|
||||
return 1
|
||||
fi
|
||||
|
||||
local pid=$(cat "$PID_FILE")
|
||||
if kill $pid 2>/dev/null; then
|
||||
echo "Stopped periodic context save daemon (PID: $pid)"
|
||||
rm -f "$PID_FILE"
|
||||
rm -f "$STATE_FILE"
|
||||
else
|
||||
echo "Failed to stop daemon (PID: $pid) - may not be running"
|
||||
rm -f "$PID_FILE"
|
||||
fi
|
||||
}
|
||||
|
||||
# Check status
|
||||
check_status() {
|
||||
if [ -f "$PID_FILE" ]; then
|
||||
local pid=$(cat "$PID_FILE")
|
||||
if kill -0 $pid 2>/dev/null; then
|
||||
local active_seconds=$(get_active_time)
|
||||
echo "Periodic context save daemon is running (PID: $pid)"
|
||||
echo "Active time: ${active_seconds}s / ${SAVE_INTERVAL_SECONDS}s"
|
||||
return 0
|
||||
else
|
||||
echo "Daemon PID file exists but process not running"
|
||||
rm -f "$PID_FILE"
|
||||
return 1
|
||||
fi
|
||||
else
|
||||
echo "Periodic context save daemon not running"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Command dispatcher
|
||||
case "$1" in
|
||||
start)
|
||||
start_daemon
|
||||
;;
|
||||
stop)
|
||||
stop_daemon
|
||||
;;
|
||||
status)
|
||||
check_status
|
||||
;;
|
||||
_monitor)
|
||||
# Internal command - run monitor loop
|
||||
monitor_loop
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {start|stop|status}"
|
||||
echo ""
|
||||
echo "Periodic context save daemon - saves context every 5 minutes of active time"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " start - Start the background daemon"
|
||||
echo " stop - Stop the daemon"
|
||||
echo " status - Check daemon status"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
@@ -1,429 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Periodic Context Save Daemon
|
||||
|
||||
Monitors Claude Code activity and saves context every 5 minutes of active time.
|
||||
Runs as a background process that tracks when Claude is actively working.
|
||||
|
||||
Usage:
|
||||
python .claude/hooks/periodic_context_save.py start
|
||||
python .claude/hooks/periodic_context_save.py stop
|
||||
python .claude/hooks/periodic_context_save.py status
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import json
|
||||
import signal
|
||||
import subprocess
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
# FIX BUG #1: Set UTF-8 encoding for stdout/stderr on Windows
|
||||
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
||||
|
||||
import requests
|
||||
|
||||
# Configuration
|
||||
SCRIPT_DIR = Path(__file__).parent
|
||||
CLAUDE_DIR = SCRIPT_DIR.parent
|
||||
PID_FILE = CLAUDE_DIR / ".periodic-save.pid"
|
||||
STATE_FILE = CLAUDE_DIR / ".periodic-save-state.json"
|
||||
LOG_FILE = CLAUDE_DIR / "periodic-save.log"
|
||||
CONFIG_FILE = CLAUDE_DIR / "context-recall-config.env"
|
||||
|
||||
SAVE_INTERVAL_SECONDS = 300 # 5 minutes
|
||||
CHECK_INTERVAL_SECONDS = 60 # Check every minute
|
||||
|
||||
|
||||
def log(message):
|
||||
"""Write log message to file and stderr (encoding-safe)"""
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
log_message = f"[{timestamp}] {message}\n"
|
||||
|
||||
# Write to log file with UTF-8 encoding to handle Unicode characters
|
||||
try:
|
||||
with open(LOG_FILE, "a", encoding="utf-8") as f:
|
||||
f.write(log_message)
|
||||
except Exception:
|
||||
pass # Silent fail on log file write errors
|
||||
|
||||
# FIX BUG #5: Safe stderr printing (handles encoding errors)
|
||||
try:
|
||||
print(log_message.strip(), file=sys.stderr)
|
||||
except UnicodeEncodeError:
|
||||
# Fallback: encode with error handling
|
||||
safe_message = log_message.encode('ascii', errors='replace').decode('ascii')
|
||||
print(safe_message.strip(), file=sys.stderr)
|
||||
|
||||
|
||||
def load_config():
|
||||
"""Load configuration from context-recall-config.env"""
|
||||
config = {
|
||||
"api_url": "http://172.16.3.30:8001",
|
||||
"jwt_token": None,
|
||||
"project_id": None, # FIX BUG #2: Add project_id to config
|
||||
}
|
||||
|
||||
if CONFIG_FILE.exists():
|
||||
with open(CONFIG_FILE) as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line.startswith("CLAUDE_API_URL=") or line.startswith("API_BASE_URL="):
|
||||
config["api_url"] = line.split("=", 1)[1]
|
||||
elif line.startswith("JWT_TOKEN="):
|
||||
config["jwt_token"] = line.split("=", 1)[1]
|
||||
elif line.startswith("CLAUDE_PROJECT_ID="):
|
||||
config["project_id"] = line.split("=", 1)[1]
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def detect_project_id():
|
||||
"""Detect project ID from git config"""
|
||||
try:
|
||||
# Try git config first
|
||||
result = subprocess.run(
|
||||
["git", "config", "--local", "claude.projectid"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
return result.stdout.strip()
|
||||
|
||||
# Try to derive from git remote URL
|
||||
result = subprocess.run(
|
||||
["git", "config", "--get", "remote.origin.url"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
import hashlib
|
||||
return hashlib.md5(result.stdout.strip().encode()).hexdigest()
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def is_claude_active():
|
||||
"""
|
||||
Check if Claude Code is actively running.
|
||||
|
||||
Returns True if:
|
||||
- Claude Code process is running
|
||||
- Recent file modifications in project directory
|
||||
- Not waiting for user input (heuristic)
|
||||
"""
|
||||
try:
|
||||
# Check for Claude process on Windows
|
||||
if sys.platform == "win32":
|
||||
result = subprocess.run(
|
||||
["tasklist.exe"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
if "claude" in result.stdout.lower() or "node" in result.stdout.lower():
|
||||
return True
|
||||
|
||||
# Check for recent file modifications (within last 2 minutes)
|
||||
cwd = Path.cwd()
|
||||
two_minutes_ago = time.time() - 120
|
||||
|
||||
for file in cwd.rglob("*"):
|
||||
if file.is_file() and file.stat().st_mtime > two_minutes_ago:
|
||||
# Recent activity detected
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
log(f"Error checking activity: {e}")
|
||||
|
||||
# Default to inactive if we can't detect
|
||||
return False
|
||||
|
||||
|
||||
def load_state():
|
||||
"""Load state from state file"""
|
||||
if STATE_FILE.exists():
|
||||
try:
|
||||
with open(STATE_FILE) as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {
|
||||
"active_seconds": 0,
|
||||
"last_update": None,
|
||||
"last_save": None,
|
||||
}
|
||||
|
||||
|
||||
def save_state(state):
|
||||
"""Save state to state file"""
|
||||
state["last_update"] = datetime.now(timezone.utc).isoformat()
|
||||
with open(STATE_FILE, "w") as f:
|
||||
json.dump(state, f, indent=2)
|
||||
|
||||
|
||||
def save_periodic_context(config, project_id):
|
||||
"""Save context to database via API"""
|
||||
# FIX BUG #7: Validate before attempting save
|
||||
if not config["jwt_token"]:
|
||||
log("[ERROR] No JWT token - cannot save context")
|
||||
return False
|
||||
|
||||
if not project_id:
|
||||
log("[ERROR] No project_id - cannot save context")
|
||||
return False
|
||||
|
||||
title = f"Periodic Save - {datetime.now().strftime('%Y-%m-%d %H:%M')}"
|
||||
summary = f"Auto-saved context after 5 minutes of active work. Session in progress on project: {project_id}"
|
||||
|
||||
# FIX BUG #2: Include project_id in payload
|
||||
payload = {
|
||||
"project_id": project_id,
|
||||
"context_type": "session_summary",
|
||||
"title": title,
|
||||
"dense_summary": summary,
|
||||
"relevance_score": 5.0,
|
||||
"tags": json.dumps(["auto-save", "periodic", "active-session"]),
|
||||
}
|
||||
|
||||
try:
|
||||
url = f"{config['api_url']}/api/conversation-contexts"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {config['jwt_token']}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
response = requests.post(url, json=payload, headers=headers, timeout=10)
|
||||
|
||||
if response.status_code in [200, 201]:
|
||||
context_id = response.json().get('id', 'unknown')
|
||||
log(f"[SUCCESS] Context saved (ID: {context_id}, Project: {project_id})")
|
||||
return True
|
||||
else:
|
||||
# FIX BUG #4: Improved error logging with full details
|
||||
error_detail = response.text[:200] if response.text else "No error detail"
|
||||
log(f"[ERROR] Failed to save context: HTTP {response.status_code}")
|
||||
log(f"[ERROR] Response: {error_detail}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
# FIX BUG #4: More detailed error logging
|
||||
log(f"[ERROR] Exception saving context: {type(e).__name__}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def monitor_loop():
|
||||
"""Main monitoring loop"""
|
||||
log("Periodic context save daemon started")
|
||||
log(f"Will save context every {SAVE_INTERVAL_SECONDS}s of active time")
|
||||
|
||||
config = load_config()
|
||||
state = load_state()
|
||||
|
||||
# FIX BUG #7: Validate configuration on startup
|
||||
if not config["jwt_token"]:
|
||||
log("[WARNING] No JWT token found in config - saves will fail")
|
||||
|
||||
# Determine project_id (config takes precedence over git detection)
|
||||
project_id = config["project_id"]
|
||||
if not project_id:
|
||||
project_id = detect_project_id()
|
||||
if project_id:
|
||||
log(f"[INFO] Detected project_id from git: {project_id}")
|
||||
else:
|
||||
log("[WARNING] No project_id found - saves will fail")
|
||||
|
||||
# Reset state on startup
|
||||
state["active_seconds"] = 0
|
||||
save_state(state)
|
||||
|
||||
while True:
|
||||
try:
|
||||
# Check if Claude is active
|
||||
if is_claude_active():
|
||||
# Increment active time
|
||||
state["active_seconds"] += CHECK_INTERVAL_SECONDS
|
||||
save_state(state)
|
||||
|
||||
log(f"Active: {state['active_seconds']}s / {SAVE_INTERVAL_SECONDS}s")
|
||||
|
||||
# Check if we've reached the save interval
|
||||
if state["active_seconds"] >= SAVE_INTERVAL_SECONDS:
|
||||
log(f"{SAVE_INTERVAL_SECONDS}s of active time reached - saving context")
|
||||
|
||||
# Try to save context
|
||||
save_success = save_periodic_context(config, project_id)
|
||||
|
||||
if save_success:
|
||||
state["last_save"] = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
# FIX BUG #3: Always reset timer in finally block (see below)
|
||||
|
||||
else:
|
||||
log("Claude Code inactive - not counting time")
|
||||
|
||||
# Wait before next check
|
||||
time.sleep(CHECK_INTERVAL_SECONDS)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
log("Daemon stopped by user")
|
||||
break
|
||||
except Exception as e:
|
||||
# FIX BUG #4: Better exception logging
|
||||
log(f"[ERROR] Exception in monitor loop: {type(e).__name__}: {e}")
|
||||
time.sleep(CHECK_INTERVAL_SECONDS)
|
||||
finally:
|
||||
# FIX BUG #3: Reset counter in finally block to prevent infinite save attempts
|
||||
if state["active_seconds"] >= SAVE_INTERVAL_SECONDS:
|
||||
state["active_seconds"] = 0
|
||||
save_state(state)
|
||||
|
||||
|
||||
def start_daemon():
|
||||
"""Start the daemon as a background process"""
|
||||
if PID_FILE.exists():
|
||||
with open(PID_FILE) as f:
|
||||
pid = int(f.read().strip())
|
||||
|
||||
# Check if process is running
|
||||
try:
|
||||
os.kill(pid, 0) # Signal 0 checks if process exists
|
||||
print(f"Periodic context save daemon already running (PID: {pid})")
|
||||
return 1
|
||||
except OSError:
|
||||
# Process not running, remove stale PID file
|
||||
PID_FILE.unlink()
|
||||
|
||||
# Start daemon process
|
||||
if sys.platform == "win32":
|
||||
# On Windows, use subprocess.Popen with DETACHED_PROCESS
|
||||
import subprocess
|
||||
CREATE_NO_WINDOW = 0x08000000
|
||||
|
||||
process = subprocess.Popen(
|
||||
[sys.executable, __file__, "_monitor"],
|
||||
creationflags=subprocess.DETACHED_PROCESS | CREATE_NO_WINDOW,
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
else:
|
||||
# On Unix, fork
|
||||
import subprocess
|
||||
process = subprocess.Popen(
|
||||
[sys.executable, __file__, "_monitor"],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL,
|
||||
)
|
||||
|
||||
# Save PID
|
||||
with open(PID_FILE, "w") as f:
|
||||
f.write(str(process.pid))
|
||||
|
||||
print(f"Started periodic context save daemon (PID: {process.pid})")
|
||||
print(f"Logs: {LOG_FILE}")
|
||||
return 0
|
||||
|
||||
|
||||
def stop_daemon():
|
||||
"""Stop the daemon"""
|
||||
if not PID_FILE.exists():
|
||||
print("Periodic context save daemon not running")
|
||||
return 1
|
||||
|
||||
with open(PID_FILE) as f:
|
||||
pid = int(f.read().strip())
|
||||
|
||||
try:
|
||||
if sys.platform == "win32":
|
||||
# On Windows, use taskkill
|
||||
subprocess.run(["taskkill", "/F", "/PID", str(pid)], check=True, timeout=10) # Prevent hung processes
|
||||
else:
|
||||
# On Unix, use kill
|
||||
os.kill(pid, signal.SIGTERM)
|
||||
|
||||
print(f"Stopped periodic context save daemon (PID: {pid})")
|
||||
PID_FILE.unlink()
|
||||
|
||||
if STATE_FILE.exists():
|
||||
STATE_FILE.unlink()
|
||||
|
||||
return 0
|
||||
|
||||
except Exception as e:
|
||||
print(f"Failed to stop daemon (PID: {pid}): {e}")
|
||||
PID_FILE.unlink()
|
||||
return 1
|
||||
|
||||
|
||||
def check_status():
|
||||
"""Check daemon status"""
|
||||
if not PID_FILE.exists():
|
||||
print("Periodic context save daemon not running")
|
||||
return 1
|
||||
|
||||
with open(PID_FILE) as f:
|
||||
pid = int(f.read().strip())
|
||||
|
||||
# Check if process is running
|
||||
try:
|
||||
os.kill(pid, 0)
|
||||
except OSError:
|
||||
print("Daemon PID file exists but process not running")
|
||||
PID_FILE.unlink()
|
||||
return 1
|
||||
|
||||
state = load_state()
|
||||
active_seconds = state.get("active_seconds", 0)
|
||||
|
||||
print(f"Periodic context save daemon is running (PID: {pid})")
|
||||
print(f"Active time: {active_seconds}s / {SAVE_INTERVAL_SECONDS}s")
|
||||
|
||||
if state.get("last_save"):
|
||||
print(f"Last save: {state['last_save']}")
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
def main():
|
||||
"""Main entry point"""
|
||||
if len(sys.argv) < 2:
|
||||
print("Usage: python periodic_context_save.py {start|stop|status}")
|
||||
print()
|
||||
print("Periodic context save daemon - saves context every 5 minutes of active time")
|
||||
print()
|
||||
print("Commands:")
|
||||
print(" start - Start the background daemon")
|
||||
print(" stop - Stop the daemon")
|
||||
print(" status - Check daemon status")
|
||||
return 1
|
||||
|
||||
command = sys.argv[1]
|
||||
|
||||
if command == "start":
|
||||
return start_daemon()
|
||||
elif command == "stop":
|
||||
return stop_daemon()
|
||||
elif command == "status":
|
||||
return check_status()
|
||||
elif command == "_monitor":
|
||||
# Internal command - run monitor loop
|
||||
monitor_loop()
|
||||
return 0
|
||||
else:
|
||||
print(f"Unknown command: {command}")
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
@@ -1,315 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Periodic Context Save - Windows Task Scheduler Version
|
||||
|
||||
This script is designed to be called every minute by Windows Task Scheduler.
|
||||
It tracks active time and saves context every 5 minutes of activity.
|
||||
|
||||
Usage:
|
||||
Schedule this to run every minute via Task Scheduler:
|
||||
python .claude/hooks/periodic_save_check.py
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import subprocess
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
# FIX BUG #1: Set UTF-8 encoding for stdout/stderr on Windows
|
||||
os.environ['PYTHONIOENCODING'] = 'utf-8'
|
||||
|
||||
import requests
|
||||
|
||||
# Configuration
|
||||
SCRIPT_DIR = Path(__file__).parent
|
||||
CLAUDE_DIR = SCRIPT_DIR.parent
|
||||
PROJECT_ROOT = CLAUDE_DIR.parent
|
||||
STATE_FILE = CLAUDE_DIR / ".periodic-save-state.json"
|
||||
LOG_FILE = CLAUDE_DIR / "periodic-save.log"
|
||||
CONFIG_FILE = CLAUDE_DIR / "context-recall-config.env"
|
||||
LOCK_FILE = CLAUDE_DIR / ".periodic-save.lock" # Mutex lock to prevent overlaps
|
||||
|
||||
SAVE_INTERVAL_SECONDS = 300 # 5 minutes
|
||||
|
||||
|
||||
def log(message):
|
||||
"""Write log message (encoding-safe)"""
|
||||
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
log_message = f"[{timestamp}] {message}\n"
|
||||
|
||||
try:
|
||||
with open(LOG_FILE, "a", encoding="utf-8") as f:
|
||||
f.write(log_message)
|
||||
except Exception:
|
||||
pass # Silent fail if can't write log
|
||||
|
||||
# FIX BUG #5: Safe stderr printing (handles encoding errors)
|
||||
try:
|
||||
print(log_message.strip(), file=sys.stderr)
|
||||
except UnicodeEncodeError:
|
||||
# Fallback: encode with error handling
|
||||
safe_message = log_message.encode('ascii', errors='replace').decode('ascii')
|
||||
print(safe_message.strip(), file=sys.stderr)
|
||||
|
||||
|
||||
def load_config():
|
||||
"""Load configuration from context-recall-config.env"""
|
||||
config = {
|
||||
"api_url": "http://172.16.3.30:8001",
|
||||
"jwt_token": None,
|
||||
"project_id": None, # FIX BUG #2: Add project_id to config
|
||||
}
|
||||
|
||||
if CONFIG_FILE.exists():
|
||||
with open(CONFIG_FILE) as f:
|
||||
for line in f:
|
||||
line = line.strip()
|
||||
if line.startswith("CLAUDE_API_URL=") or line.startswith("API_BASE_URL="):
|
||||
config["api_url"] = line.split("=", 1)[1]
|
||||
elif line.startswith("JWT_TOKEN="):
|
||||
config["jwt_token"] = line.split("=", 1)[1]
|
||||
elif line.startswith("CLAUDE_PROJECT_ID="):
|
||||
config["project_id"] = line.split("=", 1)[1]
|
||||
|
||||
return config
|
||||
|
||||
|
||||
def detect_project_id():
|
||||
"""Detect project ID from git config"""
|
||||
try:
|
||||
os.chdir(PROJECT_ROOT)
|
||||
|
||||
# Try git config first
|
||||
result = subprocess.run(
|
||||
["git", "config", "--local", "claude.projectid"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
cwd=PROJECT_ROOT,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
return result.stdout.strip()
|
||||
|
||||
# Try to derive from git remote URL
|
||||
result = subprocess.run(
|
||||
["git", "config", "--get", "remote.origin.url"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
cwd=PROJECT_ROOT,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
if result.returncode == 0 and result.stdout.strip():
|
||||
import hashlib
|
||||
return hashlib.md5(result.stdout.strip().encode()).hexdigest()
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def is_claude_active():
|
||||
"""Check if Claude Code is actively running"""
|
||||
try:
|
||||
# Check for Claude Code process
|
||||
result = subprocess.run(
|
||||
["tasklist.exe"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=False,
|
||||
timeout=5, # Prevent hung processes
|
||||
)
|
||||
|
||||
# Look for claude, node, or other indicators
|
||||
output_lower = result.stdout.lower()
|
||||
if any(proc in output_lower for proc in ["claude", "node.exe", "code.exe"]):
|
||||
# Also check for recent file modifications
|
||||
import time
|
||||
two_minutes_ago = time.time() - 120
|
||||
|
||||
# Check a few common directories for recent activity
|
||||
for check_dir in [PROJECT_ROOT, PROJECT_ROOT / "api", PROJECT_ROOT / ".claude"]:
|
||||
if check_dir.exists():
|
||||
for file in check_dir.rglob("*"):
|
||||
if file.is_file():
|
||||
try:
|
||||
if file.stat().st_mtime > two_minutes_ago:
|
||||
return True
|
||||
except:
|
||||
continue
|
||||
|
||||
except Exception as e:
|
||||
log(f"Error checking activity: {e}")
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def acquire_lock():
|
||||
"""Acquire execution lock to prevent overlapping runs"""
|
||||
try:
|
||||
# Check if lock file exists and is recent (< 60 seconds old)
|
||||
if LOCK_FILE.exists():
|
||||
lock_age = datetime.now().timestamp() - LOCK_FILE.stat().st_mtime
|
||||
if lock_age < 60: # Lock is fresh, another instance is running
|
||||
log("[INFO] Another instance is running, skipping")
|
||||
return False
|
||||
|
||||
# Create/update lock file
|
||||
LOCK_FILE.touch()
|
||||
return True
|
||||
except Exception as e:
|
||||
log(f"[WARNING] Lock acquisition failed: {e}")
|
||||
return True # Proceed anyway if lock fails
|
||||
|
||||
|
||||
def release_lock():
|
||||
"""Release execution lock"""
|
||||
try:
|
||||
if LOCK_FILE.exists():
|
||||
LOCK_FILE.unlink()
|
||||
except Exception:
|
||||
pass # Ignore errors on cleanup
|
||||
|
||||
|
||||
def load_state():
|
||||
"""Load state from state file"""
|
||||
if STATE_FILE.exists():
|
||||
try:
|
||||
with open(STATE_FILE) as f:
|
||||
return json.load(f)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {
|
||||
"active_seconds": 0,
|
||||
"last_check": None,
|
||||
"last_save": None,
|
||||
}
|
||||
|
||||
|
||||
def save_state(state):
|
||||
"""Save state to state file"""
|
||||
state["last_check"] = datetime.now(timezone.utc).isoformat()
|
||||
try:
|
||||
with open(STATE_FILE, "w") as f:
|
||||
json.dump(state, f, indent=2)
|
||||
except:
|
||||
pass # Silent fail
|
||||
|
||||
|
||||
def save_periodic_context(config, project_id):
|
||||
"""Save context to database via API"""
|
||||
# FIX BUG #7: Validate before attempting save
|
||||
if not config["jwt_token"]:
|
||||
log("[ERROR] No JWT token - cannot save context")
|
||||
return False
|
||||
|
||||
if not project_id:
|
||||
log("[ERROR] No project_id - cannot save context")
|
||||
return False
|
||||
|
||||
title = f"Periodic Save - {datetime.now().strftime('%Y-%m-%d %H:%M')}"
|
||||
summary = f"Auto-saved context after {SAVE_INTERVAL_SECONDS // 60} minutes of active work. Session in progress on project: {project_id}"
|
||||
|
||||
# FIX BUG #2: Include project_id in payload
|
||||
payload = {
|
||||
"project_id": project_id,
|
||||
"context_type": "session_summary",
|
||||
"title": title,
|
||||
"dense_summary": summary,
|
||||
"relevance_score": 5.0,
|
||||
"tags": json.dumps(["auto-save", "periodic", "active-session", project_id]),
|
||||
}
|
||||
|
||||
try:
|
||||
url = f"{config['api_url']}/api/conversation-contexts"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {config['jwt_token']}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
|
||||
response = requests.post(url, json=payload, headers=headers, timeout=10)
|
||||
|
||||
if response.status_code in [200, 201]:
|
||||
context_id = response.json().get('id', 'unknown')
|
||||
log(f"[SUCCESS] Context saved (ID: {context_id}, Active time: {SAVE_INTERVAL_SECONDS}s)")
|
||||
return True
|
||||
else:
|
||||
# FIX BUG #4: Improved error logging with full details
|
||||
error_detail = response.text[:200] if response.text else "No error detail"
|
||||
log(f"[ERROR] Failed to save: HTTP {response.status_code}")
|
||||
log(f"[ERROR] Response: {error_detail}")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
# FIX BUG #4: More detailed error logging
|
||||
log(f"[ERROR] Exception saving context: {type(e).__name__}: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
"""Main entry point - called every minute by Task Scheduler"""
|
||||
# Acquire lock to prevent overlapping executions
|
||||
if not acquire_lock():
|
||||
return 0 # Another instance is running, exit gracefully
|
||||
|
||||
try:
|
||||
config = load_config()
|
||||
state = load_state()
|
||||
|
||||
# FIX BUG #7: Validate configuration
|
||||
if not config["jwt_token"]:
|
||||
log("[WARNING] No JWT token found in config")
|
||||
|
||||
# Determine project_id (config takes precedence over git detection)
|
||||
project_id = config["project_id"]
|
||||
if not project_id:
|
||||
project_id = detect_project_id()
|
||||
if not project_id:
|
||||
log("[WARNING] No project_id found")
|
||||
|
||||
# Check if Claude is active
|
||||
if is_claude_active():
|
||||
# Increment active time (60 seconds per check)
|
||||
state["active_seconds"] += 60
|
||||
|
||||
# Check if we've reached the save interval
|
||||
if state["active_seconds"] >= SAVE_INTERVAL_SECONDS:
|
||||
log(f"{SAVE_INTERVAL_SECONDS}s active time reached - saving context")
|
||||
|
||||
save_success = save_periodic_context(config, project_id)
|
||||
|
||||
if save_success:
|
||||
state["last_save"] = datetime.now(timezone.utc).isoformat()
|
||||
|
||||
# FIX BUG #3: Always reset counter in finally block (see below)
|
||||
|
||||
save_state(state)
|
||||
else:
|
||||
# Not active - don't increment timer but save state
|
||||
save_state(state)
|
||||
|
||||
return 0
|
||||
except Exception as e:
|
||||
# FIX BUG #4: Better exception logging
|
||||
log(f"[ERROR] Fatal error: {type(e).__name__}: {e}")
|
||||
return 1
|
||||
finally:
|
||||
# FIX BUG #3: Reset counter in finally block to prevent infinite save attempts
|
||||
if state["active_seconds"] >= SAVE_INTERVAL_SECONDS:
|
||||
state["active_seconds"] = 0
|
||||
save_state(state)
|
||||
# Always release lock, even if error occurs
|
||||
release_lock()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
sys.exit(main())
|
||||
except Exception as e:
|
||||
log(f"Fatal error: {e}")
|
||||
sys.exit(1)
|
||||
@@ -1,11 +0,0 @@
|
||||
@echo off
|
||||
REM Windows wrapper for periodic context save
|
||||
REM Can be run from Task Scheduler every minute
|
||||
|
||||
cd /d D:\ClaudeTools
|
||||
|
||||
REM Run the check-and-save script
|
||||
python .claude\hooks\periodic_save_check.py
|
||||
|
||||
REM Exit silently
|
||||
exit /b 0
|
||||
@@ -1,69 +0,0 @@
|
||||
# Setup Periodic Context Save - Windows Task Scheduler
|
||||
# This script creates a scheduled task to run periodic_save_check.py every minute
|
||||
# Uses pythonw.exe to run without console window
|
||||
|
||||
$TaskName = "ClaudeTools - Periodic Context Save"
|
||||
$ScriptPath = "D:\ClaudeTools\.claude\hooks\periodic_save_check.py"
|
||||
$WorkingDir = "D:\ClaudeTools"
|
||||
|
||||
# Use pythonw.exe instead of python.exe to run without console window
|
||||
$PythonExe = (Get-Command python).Source
|
||||
$PythonDir = Split-Path $PythonExe -Parent
|
||||
$PythonwPath = Join-Path $PythonDir "pythonw.exe"
|
||||
|
||||
# Fallback to python.exe if pythonw.exe doesn't exist (shouldn't happen)
|
||||
if (-not (Test-Path $PythonwPath)) {
|
||||
Write-Warning "pythonw.exe not found at $PythonwPath, falling back to python.exe"
|
||||
$PythonwPath = $PythonExe
|
||||
}
|
||||
|
||||
# Check if task already exists
|
||||
$ExistingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
|
||||
|
||||
if ($ExistingTask) {
|
||||
Write-Host "Task '$TaskName' already exists. Removing old task..."
|
||||
Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false
|
||||
}
|
||||
|
||||
# Create action to run Python script with pythonw.exe (no console window)
|
||||
$Action = New-ScheduledTaskAction -Execute $PythonwPath `
|
||||
-Argument $ScriptPath `
|
||||
-WorkingDirectory $WorkingDir
|
||||
|
||||
# Create trigger to run every 5 minutes (indefinitely) - Reduced from 1min to prevent zombie accumulation
|
||||
$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 5)
|
||||
|
||||
# Create settings - Hidden and DisallowStartIfOnBatteries set to false
|
||||
$Settings = New-ScheduledTaskSettingsSet `
|
||||
-AllowStartIfOnBatteries `
|
||||
-DontStopIfGoingOnBatteries `
|
||||
-StartWhenAvailable `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Minutes 5) `
|
||||
-Hidden
|
||||
|
||||
# Create principal (run as current user, no window)
|
||||
$Principal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType S4U
|
||||
|
||||
# Register the task
|
||||
Register-ScheduledTask -TaskName $TaskName `
|
||||
-Action $Action `
|
||||
-Trigger $Trigger `
|
||||
-Settings $Settings `
|
||||
-Principal $Principal `
|
||||
-Description "Automatically saves Claude Code context every 5 minutes of active work"
|
||||
|
||||
Write-Host "[SUCCESS] Scheduled task created successfully!"
|
||||
Write-Host ""
|
||||
Write-Host "Task Name: $TaskName"
|
||||
Write-Host "Runs: Every 5 minutes (HIDDEN - no console window)"
|
||||
Write-Host "Action: Checks activity and saves context every 5 minutes"
|
||||
Write-Host "Executable: $PythonwPath (pythonw.exe = no window)"
|
||||
Write-Host ""
|
||||
Write-Host "To verify task is hidden:"
|
||||
Write-Host " Get-ScheduledTask -TaskName '$TaskName' | Select-Object -ExpandProperty Settings"
|
||||
Write-Host ""
|
||||
Write-Host "To remove:"
|
||||
Write-Host " Unregister-ScheduledTask -TaskName '$TaskName' -Confirm:`$false"
|
||||
Write-Host ""
|
||||
Write-Host "View logs:"
|
||||
Write-Host ' Get-Content D:\ClaudeTools\.claude\periodic-save.log -Tail 20'
|
||||
@@ -1,110 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Sync Queued Contexts to Database
|
||||
# Uploads any locally queued contexts to the central API
|
||||
# Can be run manually or called automatically by hooks
|
||||
#
|
||||
# Usage: bash .claude/hooks/sync-contexts
|
||||
#
|
||||
|
||||
# Load configuration
|
||||
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
CONFIG_FILE="$CLAUDE_DIR/context-recall-config.env"
|
||||
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
QUEUE_DIR="$CLAUDE_DIR/context-queue"
|
||||
PENDING_DIR="$QUEUE_DIR/pending"
|
||||
UPLOADED_DIR="$QUEUE_DIR/uploaded"
|
||||
FAILED_DIR="$QUEUE_DIR/failed"
|
||||
|
||||
# Exit if no JWT token
|
||||
if [ -z "$JWT_TOKEN" ]; then
|
||||
echo "ERROR: No JWT token available" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Create directories if they don't exist
|
||||
mkdir -p "$PENDING_DIR" "$UPLOADED_DIR" "$FAILED_DIR" 2>/dev/null
|
||||
|
||||
# Check if there are any pending files
|
||||
PENDING_COUNT=$(find "$PENDING_DIR" -type f -name "*.json" 2>/dev/null | wc -l)
|
||||
|
||||
if [ "$PENDING_COUNT" -eq 0 ]; then
|
||||
# No pending contexts to sync
|
||||
exit 0
|
||||
fi
|
||||
|
||||
echo "==================================="
|
||||
echo "Syncing Queued Contexts"
|
||||
echo "==================================="
|
||||
echo "Found $PENDING_COUNT pending context(s)"
|
||||
echo ""
|
||||
|
||||
# Process each pending file
|
||||
SUCCESS_COUNT=0
|
||||
FAIL_COUNT=0
|
||||
|
||||
for QUEUE_FILE in "$PENDING_DIR"/*.json; do
|
||||
# Skip if no files match
|
||||
[ -e "$QUEUE_FILE" ] || continue
|
||||
|
||||
FILENAME=$(basename "$QUEUE_FILE")
|
||||
echo "Processing: $FILENAME"
|
||||
|
||||
# Read the payload
|
||||
PAYLOAD=$(cat "$QUEUE_FILE")
|
||||
|
||||
# Determine endpoint based on filename
|
||||
if [[ "$FILENAME" == *"_state.json" ]]; then
|
||||
ENDPOINT="${API_URL}/api/project-states"
|
||||
else
|
||||
ENDPOINT="${API_URL}/api/conversation-contexts"
|
||||
fi
|
||||
|
||||
# Try to POST to API
|
||||
RESPONSE=$(curl -s --max-time 10 -w "\n%{http_code}" \
|
||||
-X POST "$ENDPOINT" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$PAYLOAD" 2>/dev/null)
|
||||
|
||||
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ]; then
|
||||
# Success - move to uploaded directory
|
||||
mv "$QUEUE_FILE" "$UPLOADED_DIR/"
|
||||
echo " [OK] Uploaded successfully"
|
||||
((SUCCESS_COUNT++))
|
||||
else
|
||||
# Failed - move to failed directory for manual review
|
||||
mv "$QUEUE_FILE" "$FAILED_DIR/"
|
||||
echo " [ERROR] Upload failed (HTTP $HTTP_CODE) - moved to failed/"
|
||||
((FAIL_COUNT++))
|
||||
fi
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "==================================="
|
||||
echo "Sync Complete"
|
||||
echo "==================================="
|
||||
echo "Successful: $SUCCESS_COUNT"
|
||||
echo "Failed: $FAIL_COUNT"
|
||||
echo ""
|
||||
|
||||
# Clean up old uploaded files (keep last 100)
|
||||
UPLOADED_COUNT=$(find "$UPLOADED_DIR" -type f -name "*.json" 2>/dev/null | wc -l)
|
||||
if [ "$UPLOADED_COUNT" -gt 100 ]; then
|
||||
echo "Cleaning up old uploaded contexts (keeping last 100)..."
|
||||
find "$UPLOADED_DIR" -type f -name "*.json" -printf '%T@ %p\n' | \
|
||||
sort -n | \
|
||||
head -n -100 | \
|
||||
cut -d' ' -f2- | \
|
||||
xargs rm -f
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -1,182 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: task-complete (v2 - with offline support)
|
||||
# Runs AFTER a task is completed
|
||||
# Saves conversation context to the database for future recall
|
||||
# FALLBACK: Queues locally when API is unavailable, syncs later
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://172.16.3.30:8001)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# TASK_SUMMARY - Summary of completed task (auto-generated by Claude)
|
||||
# TASK_FILES - Files modified during task (comma-separated)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
|
||||
# Local storage paths
|
||||
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
QUEUE_DIR="$CLAUDE_DIR/context-queue"
|
||||
PENDING_DIR="$QUEUE_DIR/pending"
|
||||
UPLOADED_DIR="$QUEUE_DIR/uploaded"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID (same logic as user-prompt-submit)
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Create queue directories if they don't exist
|
||||
mkdir -p "$PENDING_DIR" "$UPLOADED_DIR" 2>/dev/null
|
||||
|
||||
# Gather task information
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
TIMESTAMP_FILENAME=$(date -u +"%Y%m%d_%H%M%S")
|
||||
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
|
||||
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "none")
|
||||
|
||||
# Get recent git changes
|
||||
CHANGED_FILES=$(git diff --name-only HEAD~1 2>/dev/null | head -10 | tr '\n' ',' | sed 's/,$//')
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
CHANGED_FILES="${TASK_FILES:-}"
|
||||
fi
|
||||
|
||||
# Create task summary
|
||||
if [ -z "$TASK_SUMMARY" ]; then
|
||||
# Generate basic summary from git log if no summary provided
|
||||
TASK_SUMMARY=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "Task completed")
|
||||
fi
|
||||
|
||||
# Build context payload
|
||||
CONTEXT_TITLE="Session: ${TIMESTAMP}"
|
||||
CONTEXT_TYPE="session_summary"
|
||||
RELEVANCE_SCORE=7.0
|
||||
|
||||
# Create dense summary
|
||||
DENSE_SUMMARY="Task completed on branch '${GIT_BRANCH}' (commit: ${GIT_COMMIT}).
|
||||
|
||||
Summary: ${TASK_SUMMARY}
|
||||
|
||||
Modified files: ${CHANGED_FILES:-none}
|
||||
|
||||
Timestamp: ${TIMESTAMP}"
|
||||
|
||||
# Escape JSON strings
|
||||
escape_json() {
|
||||
echo "$1" | python3 -c "import sys, json; print(json.dumps(sys.stdin.read())[1:-1])"
|
||||
}
|
||||
|
||||
ESCAPED_TITLE=$(escape_json "$CONTEXT_TITLE")
|
||||
ESCAPED_SUMMARY=$(escape_json "$DENSE_SUMMARY")
|
||||
|
||||
# Save context to database
|
||||
CONTEXT_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"context_type": "${CONTEXT_TYPE}",
|
||||
"title": ${ESCAPED_TITLE},
|
||||
"dense_summary": ${ESCAPED_SUMMARY},
|
||||
"relevance_score": ${RELEVANCE_SCORE},
|
||||
"metadata": {
|
||||
"git_branch": "${GIT_BRANCH}",
|
||||
"git_commit": "${GIT_COMMIT}",
|
||||
"files_modified": "${CHANGED_FILES}",
|
||||
"timestamp": "${TIMESTAMP}"
|
||||
}
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# Update project state
|
||||
PROJECT_STATE_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"state_data": {
|
||||
"last_task_completion": "${TIMESTAMP}",
|
||||
"last_git_commit": "${GIT_COMMIT}",
|
||||
"last_git_branch": "${GIT_BRANCH}",
|
||||
"recent_files": "${CHANGED_FILES}"
|
||||
},
|
||||
"state_type": "task_completion"
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# Try to POST to API if we have a JWT token
|
||||
API_SUCCESS=false
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
RESPONSE=$(curl -s --max-time 5 -w "\n%{http_code}" \
|
||||
-X POST "${API_URL}/api/conversation-contexts" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$CONTEXT_PAYLOAD" 2>/dev/null)
|
||||
|
||||
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
|
||||
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ]; then
|
||||
API_SUCCESS=true
|
||||
|
||||
# Also update project state
|
||||
curl -s --max-time 5 \
|
||||
-X POST "${API_URL}/api/project-states" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$PROJECT_STATE_PAYLOAD" 2>/dev/null >/dev/null
|
||||
fi
|
||||
fi
|
||||
|
||||
# If API call failed, queue locally
|
||||
if [ "$API_SUCCESS" = "false" ]; then
|
||||
# Save context to pending queue
|
||||
QUEUE_FILE="$PENDING_DIR/${PROJECT_ID}_${TIMESTAMP_FILENAME}_context.json"
|
||||
echo "$CONTEXT_PAYLOAD" > "$QUEUE_FILE"
|
||||
|
||||
# Save project state to pending queue
|
||||
STATE_QUEUE_FILE="$PENDING_DIR/${PROJECT_ID}_${TIMESTAMP_FILENAME}_state.json"
|
||||
echo "$PROJECT_STATE_PAYLOAD" > "$STATE_QUEUE_FILE"
|
||||
|
||||
echo "[WARNING] Context queued locally (API unavailable) - will sync when online" >&2
|
||||
|
||||
# Try to sync (opportunistic) - Changed from background (&) to synchronous to prevent zombie processes
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1
|
||||
fi
|
||||
else
|
||||
echo "[OK] Context saved to database" >&2
|
||||
|
||||
# Trigger sync of any queued items - Changed from background (&) to synchronous to prevent zombie processes
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1
|
||||
fi
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -1,182 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: task-complete (v2 - with offline support)
|
||||
# Runs AFTER a task is completed
|
||||
# Saves conversation context to the database for future recall
|
||||
# FALLBACK: Queues locally when API is unavailable, syncs later
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://172.16.3.30:8001)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# TASK_SUMMARY - Summary of completed task (auto-generated by Claude)
|
||||
# TASK_FILES - Files modified during task (comma-separated)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
|
||||
# Local storage paths
|
||||
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
QUEUE_DIR="$CLAUDE_DIR/context-queue"
|
||||
PENDING_DIR="$QUEUE_DIR/pending"
|
||||
UPLOADED_DIR="$QUEUE_DIR/uploaded"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID (same logic as user-prompt-submit)
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Create queue directories if they don't exist
|
||||
mkdir -p "$PENDING_DIR" "$UPLOADED_DIR" 2>/dev/null
|
||||
|
||||
# Gather task information
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
TIMESTAMP_FILENAME=$(date -u +"%Y%m%d_%H%M%S")
|
||||
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
|
||||
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "none")
|
||||
|
||||
# Get recent git changes
|
||||
CHANGED_FILES=$(git diff --name-only HEAD~1 2>/dev/null | head -10 | tr '\n' ',' | sed 's/,$//')
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
CHANGED_FILES="${TASK_FILES:-}"
|
||||
fi
|
||||
|
||||
# Create task summary
|
||||
if [ -z "$TASK_SUMMARY" ]; then
|
||||
# Generate basic summary from git log if no summary provided
|
||||
TASK_SUMMARY=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "Task completed")
|
||||
fi
|
||||
|
||||
# Build context payload
|
||||
CONTEXT_TITLE="Session: ${TIMESTAMP}"
|
||||
CONTEXT_TYPE="session_summary"
|
||||
RELEVANCE_SCORE=7.0
|
||||
|
||||
# Create dense summary
|
||||
DENSE_SUMMARY="Task completed on branch '${GIT_BRANCH}' (commit: ${GIT_COMMIT}).
|
||||
|
||||
Summary: ${TASK_SUMMARY}
|
||||
|
||||
Modified files: ${CHANGED_FILES:-none}
|
||||
|
||||
Timestamp: ${TIMESTAMP}"
|
||||
|
||||
# Escape JSON strings
|
||||
escape_json() {
|
||||
echo "$1" | python3 -c "import sys, json; print(json.dumps(sys.stdin.read())[1:-1])"
|
||||
}
|
||||
|
||||
ESCAPED_TITLE=$(escape_json "$CONTEXT_TITLE")
|
||||
ESCAPED_SUMMARY=$(escape_json "$DENSE_SUMMARY")
|
||||
|
||||
# Save context to database
|
||||
CONTEXT_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"context_type": "${CONTEXT_TYPE}",
|
||||
"title": ${ESCAPED_TITLE},
|
||||
"dense_summary": ${ESCAPED_SUMMARY},
|
||||
"relevance_score": ${RELEVANCE_SCORE},
|
||||
"metadata": {
|
||||
"git_branch": "${GIT_BRANCH}",
|
||||
"git_commit": "${GIT_COMMIT}",
|
||||
"files_modified": "${CHANGED_FILES}",
|
||||
"timestamp": "${TIMESTAMP}"
|
||||
}
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# Update project state
|
||||
PROJECT_STATE_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"state_data": {
|
||||
"last_task_completion": "${TIMESTAMP}",
|
||||
"last_git_commit": "${GIT_COMMIT}",
|
||||
"last_git_branch": "${GIT_BRANCH}",
|
||||
"recent_files": "${CHANGED_FILES}"
|
||||
},
|
||||
"state_type": "task_completion"
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# Try to POST to API if we have a JWT token
|
||||
API_SUCCESS=false
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
RESPONSE=$(curl -s --max-time 5 -w "\n%{http_code}" \
|
||||
-X POST "${API_URL}/api/conversation-contexts" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$CONTEXT_PAYLOAD" 2>/dev/null)
|
||||
|
||||
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
|
||||
RESPONSE_BODY=$(echo "$RESPONSE" | sed '$d')
|
||||
|
||||
if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ]; then
|
||||
API_SUCCESS=true
|
||||
|
||||
# Also update project state
|
||||
curl -s --max-time 5 \
|
||||
-X POST "${API_URL}/api/project-states" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$PROJECT_STATE_PAYLOAD" 2>/dev/null >/dev/null
|
||||
fi
|
||||
fi
|
||||
|
||||
# If API call failed, queue locally
|
||||
if [ "$API_SUCCESS" = "false" ]; then
|
||||
# Save context to pending queue
|
||||
QUEUE_FILE="$PENDING_DIR/${PROJECT_ID}_${TIMESTAMP_FILENAME}_context.json"
|
||||
echo "$CONTEXT_PAYLOAD" > "$QUEUE_FILE"
|
||||
|
||||
# Save project state to pending queue
|
||||
STATE_QUEUE_FILE="$PENDING_DIR/${PROJECT_ID}_${TIMESTAMP_FILENAME}_state.json"
|
||||
echo "$PROJECT_STATE_PAYLOAD" > "$STATE_QUEUE_FILE"
|
||||
|
||||
echo "[WARNING] Context queued locally (API unavailable) - will sync when online" >&2
|
||||
|
||||
# Try to sync in background (opportunistic)
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1 &
|
||||
fi
|
||||
else
|
||||
echo "[OK] Context saved to database" >&2
|
||||
|
||||
# Trigger background sync of any queued items
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1 &
|
||||
fi
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -1,140 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: task-complete
|
||||
# Runs AFTER a task is completed
|
||||
# Saves conversation context to the database for future recall
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://localhost:8000)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# TASK_SUMMARY - Summary of completed task (auto-generated by Claude)
|
||||
# TASK_FILES - Files modified during task (comma-separated)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://localhost:8000}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID (same logic as user-prompt-submit)
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID or JWT token
|
||||
if [ -z "$PROJECT_ID" ] || [ -z "$JWT_TOKEN" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Gather task information
|
||||
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||
GIT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
|
||||
GIT_COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "none")
|
||||
|
||||
# Get recent git changes
|
||||
CHANGED_FILES=$(git diff --name-only HEAD~1 2>/dev/null | head -10 | tr '\n' ',' | sed 's/,$//')
|
||||
if [ -z "$CHANGED_FILES" ]; then
|
||||
CHANGED_FILES="${TASK_FILES:-}"
|
||||
fi
|
||||
|
||||
# Create task summary
|
||||
if [ -z "$TASK_SUMMARY" ]; then
|
||||
# Generate basic summary from git log if no summary provided
|
||||
TASK_SUMMARY=$(git log -1 --pretty=format:"%s" 2>/dev/null || echo "Task completed")
|
||||
fi
|
||||
|
||||
# Build context payload
|
||||
CONTEXT_TITLE="Session: ${TIMESTAMP}"
|
||||
CONTEXT_TYPE="session_summary"
|
||||
RELEVANCE_SCORE=7.0
|
||||
|
||||
# Create dense summary
|
||||
DENSE_SUMMARY="Task completed on branch '${GIT_BRANCH}' (commit: ${GIT_COMMIT}).
|
||||
|
||||
Summary: ${TASK_SUMMARY}
|
||||
|
||||
Modified files: ${CHANGED_FILES:-none}
|
||||
|
||||
Timestamp: ${TIMESTAMP}"
|
||||
|
||||
# Escape JSON strings
|
||||
escape_json() {
|
||||
echo "$1" | python3 -c "import sys, json; print(json.dumps(sys.stdin.read())[1:-1])"
|
||||
}
|
||||
|
||||
ESCAPED_TITLE=$(escape_json "$CONTEXT_TITLE")
|
||||
ESCAPED_SUMMARY=$(escape_json "$DENSE_SUMMARY")
|
||||
|
||||
# Save context to database
|
||||
CONTEXT_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"context_type": "${CONTEXT_TYPE}",
|
||||
"title": ${ESCAPED_TITLE},
|
||||
"dense_summary": ${ESCAPED_SUMMARY},
|
||||
"relevance_score": ${RELEVANCE_SCORE},
|
||||
"metadata": {
|
||||
"git_branch": "${GIT_BRANCH}",
|
||||
"git_commit": "${GIT_COMMIT}",
|
||||
"files_modified": "${CHANGED_FILES}",
|
||||
"timestamp": "${TIMESTAMP}"
|
||||
}
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
# POST to conversation-contexts endpoint
|
||||
RESPONSE=$(curl -s --max-time 5 \
|
||||
-X POST "${API_URL}/api/conversation-contexts" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$CONTEXT_PAYLOAD" 2>/dev/null)
|
||||
|
||||
# Update project state
|
||||
PROJECT_STATE_PAYLOAD=$(cat <<EOF
|
||||
{
|
||||
"project_id": "${PROJECT_ID}",
|
||||
"state_data": {
|
||||
"last_task_completion": "${TIMESTAMP}",
|
||||
"last_git_commit": "${GIT_COMMIT}",
|
||||
"last_git_branch": "${GIT_BRANCH}",
|
||||
"recent_files": "${CHANGED_FILES}"
|
||||
},
|
||||
"state_type": "task_completion"
|
||||
}
|
||||
EOF
|
||||
)
|
||||
|
||||
curl -s --max-time 5 \
|
||||
-X POST "${API_URL}/api/project-states" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$PROJECT_STATE_PAYLOAD" 2>/dev/null >/dev/null
|
||||
|
||||
# Log success (optional - comment out for silent operation)
|
||||
if [ -n "$RESPONSE" ]; then
|
||||
echo "✓ Context saved to database" >&2
|
||||
fi
|
||||
|
||||
exit 0
|
||||
@@ -1,85 +0,0 @@
|
||||
# Quick Update - Make Existing Periodic Save Task Invisible
|
||||
# This script updates the existing task to run without showing a window
|
||||
|
||||
$TaskName = "ClaudeTools - Periodic Context Save"
|
||||
|
||||
Write-Host "Updating task '$TaskName' to run invisibly..."
|
||||
Write-Host ""
|
||||
|
||||
# Check if task exists
|
||||
$Task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
|
||||
if (-not $Task) {
|
||||
Write-Host "ERROR: Task '$TaskName' not found."
|
||||
Write-Host "Run setup_periodic_save.ps1 to create it first."
|
||||
exit 1
|
||||
}
|
||||
|
||||
# Find pythonw.exe path
|
||||
$PythonExe = (Get-Command python).Source
|
||||
$PythonDir = Split-Path $PythonExe -Parent
|
||||
$PythonwPath = Join-Path $PythonDir "pythonw.exe"
|
||||
|
||||
if (-not (Test-Path $PythonwPath)) {
|
||||
Write-Host "ERROR: pythonw.exe not found at $PythonwPath"
|
||||
Write-Host "Please reinstall Python to get pythonw.exe"
|
||||
exit 1
|
||||
}
|
||||
|
||||
Write-Host "Found pythonw.exe at: $PythonwPath"
|
||||
|
||||
# Update the action to use pythonw.exe
|
||||
$NewAction = New-ScheduledTaskAction -Execute $PythonwPath `
|
||||
-Argument "D:\ClaudeTools\.claude\hooks\periodic_save_check.py" `
|
||||
-WorkingDirectory "D:\ClaudeTools"
|
||||
|
||||
# Update settings to be hidden
|
||||
$NewSettings = New-ScheduledTaskSettingsSet `
|
||||
-AllowStartIfOnBatteries `
|
||||
-DontStopIfGoingOnBatteries `
|
||||
-StartWhenAvailable `
|
||||
-ExecutionTimeLimit (New-TimeSpan -Minutes 5) `
|
||||
-Hidden
|
||||
|
||||
# Update principal to run in background (S4U = Service-For-User)
|
||||
$NewPrincipal = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType S4U
|
||||
|
||||
# Get existing trigger (preserve it)
|
||||
$ExistingTrigger = $Task.Triggers
|
||||
|
||||
# Update the task
|
||||
Set-ScheduledTask -TaskName $TaskName `
|
||||
-Action $NewAction `
|
||||
-Settings $NewSettings `
|
||||
-Principal $NewPrincipal `
|
||||
-Trigger $ExistingTrigger | Out-Null
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "[SUCCESS] Task updated successfully!"
|
||||
Write-Host ""
|
||||
Write-Host "Changes made:"
|
||||
Write-Host " 1. Changed executable: python.exe -> pythonw.exe"
|
||||
Write-Host " 2. Set task to Hidden"
|
||||
Write-Host " 3. Changed LogonType: Interactive -> S4U (background)"
|
||||
Write-Host ""
|
||||
Write-Host "Verification:"
|
||||
|
||||
# Show current settings
|
||||
$UpdatedTask = Get-ScheduledTask -TaskName $TaskName
|
||||
$Settings = $UpdatedTask.Settings
|
||||
$Action = $UpdatedTask.Actions[0]
|
||||
$Principal = $UpdatedTask.Principal
|
||||
|
||||
Write-Host " Executable: $($Action.Execute)"
|
||||
Write-Host " Hidden: $($Settings.Hidden)"
|
||||
Write-Host " LogonType: $($Principal.LogonType)"
|
||||
Write-Host ""
|
||||
|
||||
if ($Settings.Hidden -and $Action.Execute -like "*pythonw.exe" -and $Principal.LogonType -eq "S4U") {
|
||||
Write-Host "[OK] All settings correct - task will run invisibly!"
|
||||
} else {
|
||||
Write-Host "[WARNING] Some settings may not be correct - please verify manually"
|
||||
}
|
||||
|
||||
Write-Host ""
|
||||
Write-Host "The task will now run invisibly without showing any console window."
|
||||
Write-Host ""
|
||||
@@ -1,163 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: user-prompt-submit (v2 - with offline support)
|
||||
# Runs BEFORE each user message is processed
|
||||
# Injects relevant context from the database into the conversation
|
||||
# FALLBACK: Uses local cache when API is unavailable
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://172.16.3.30:8001)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# MIN_RELEVANCE_SCORE - Minimum score for context (default: 5.0)
|
||||
# MAX_CONTEXTS - Maximum number of contexts to retrieve (default: 10)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
MIN_SCORE="${MIN_RELEVANCE_SCORE:-5.0}"
|
||||
MAX_ITEMS="${MAX_CONTEXTS:-10}"
|
||||
|
||||
# Local storage paths
|
||||
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
CACHE_DIR="$CLAUDE_DIR/context-cache"
|
||||
QUEUE_DIR="$CLAUDE_DIR/context-queue"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID from git repo if not set
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
# Try to get from git config
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Try to derive from git remote URL
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
# Hash the remote URL to create a consistent ID
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID available
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Silent exit - no context available
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Create cache directory if it doesn't exist
|
||||
PROJECT_CACHE_DIR="$CACHE_DIR/$PROJECT_ID"
|
||||
mkdir -p "$PROJECT_CACHE_DIR" 2>/dev/null
|
||||
|
||||
# Try to sync any queued contexts first (opportunistic)
|
||||
# NOTE: Changed from background (&) to synchronous to prevent zombie processes
|
||||
if [ -d "$QUEUE_DIR/pending" ] && [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1
|
||||
fi
|
||||
|
||||
# Build API request URL
|
||||
RECALL_URL="${API_URL}/api/conversation-contexts/recall"
|
||||
QUERY_PARAMS="project_id=${PROJECT_ID}&limit=${MAX_ITEMS}&min_relevance_score=${MIN_SCORE}"
|
||||
|
||||
# Try to fetch context from API (with timeout and error handling)
|
||||
API_AVAILABLE=false
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
CONTEXT_RESPONSE=$(curl -s --max-time 3 \
|
||||
"${RECALL_URL}?${QUERY_PARAMS}" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Accept: application/json" 2>/dev/null)
|
||||
|
||||
if [ $? -eq 0 ] && [ -n "$CONTEXT_RESPONSE" ]; then
|
||||
# Check if response is valid JSON (not an error)
|
||||
echo "$CONTEXT_RESPONSE" | python3 -c "import sys, json; json.load(sys.stdin)" 2>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
API_AVAILABLE=true
|
||||
# Save to cache for offline use
|
||||
echo "$CONTEXT_RESPONSE" > "$PROJECT_CACHE_DIR/latest.json"
|
||||
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" > "$PROJECT_CACHE_DIR/last_updated"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Fallback to local cache if API unavailable
|
||||
if [ "$API_AVAILABLE" = "false" ]; then
|
||||
if [ -f "$PROJECT_CACHE_DIR/latest.json" ]; then
|
||||
CONTEXT_RESPONSE=$(cat "$PROJECT_CACHE_DIR/latest.json")
|
||||
CACHE_AGE="unknown"
|
||||
if [ -f "$PROJECT_CACHE_DIR/last_updated" ]; then
|
||||
CACHE_AGE=$(cat "$PROJECT_CACHE_DIR/last_updated")
|
||||
fi
|
||||
echo "<!-- Using cached context (API unavailable) - Last updated: $CACHE_AGE -->" >&2
|
||||
else
|
||||
# No cache available, exit silently
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Parse and format context
|
||||
CONTEXT_COUNT=$(echo "$CONTEXT_RESPONSE" | grep -o '"id"' | wc -l)
|
||||
|
||||
if [ "$CONTEXT_COUNT" -gt 0 ]; then
|
||||
if [ "$API_AVAILABLE" = "true" ]; then
|
||||
echo "<!-- Context Recall: Retrieved $CONTEXT_COUNT relevant context(s) from API -->"
|
||||
else
|
||||
echo "<!-- Context Recall: Retrieved $CONTEXT_COUNT relevant context(s) from LOCAL CACHE (offline mode) -->"
|
||||
fi
|
||||
echo ""
|
||||
echo "## Previous Context"
|
||||
echo ""
|
||||
if [ "$API_AVAILABLE" = "false" ]; then
|
||||
echo "[WARNING] **Offline Mode** - Using cached context (API unavailable)"
|
||||
echo ""
|
||||
fi
|
||||
echo "The following context has been automatically recalled:"
|
||||
echo ""
|
||||
|
||||
# Extract and format each context entry
|
||||
echo "$CONTEXT_RESPONSE" | python3 -c "
|
||||
import sys, json
|
||||
try:
|
||||
contexts = json.load(sys.stdin)
|
||||
if isinstance(contexts, list):
|
||||
for i, ctx in enumerate(contexts, 1):
|
||||
title = ctx.get('title', 'Untitled')
|
||||
summary = ctx.get('dense_summary', '')
|
||||
score = ctx.get('relevance_score', 0)
|
||||
ctx_type = ctx.get('context_type', 'unknown')
|
||||
|
||||
print(f'### {i}. {title} (Score: {score}/10)')
|
||||
print(f'*Type: {ctx_type}*')
|
||||
print()
|
||||
print(summary)
|
||||
print()
|
||||
print('---')
|
||||
print()
|
||||
except:
|
||||
pass
|
||||
" 2>/dev/null
|
||||
|
||||
echo ""
|
||||
if [ "$API_AVAILABLE" = "true" ]; then
|
||||
echo "*Context automatically injected to maintain continuity across sessions.*"
|
||||
else
|
||||
echo "*Context from local cache - new context will sync when API is available.*"
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# Exit successfully
|
||||
exit 0
|
||||
@@ -1,162 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: user-prompt-submit (v2 - with offline support)
|
||||
# Runs BEFORE each user message is processed
|
||||
# Injects relevant context from the database into the conversation
|
||||
# FALLBACK: Uses local cache when API is unavailable
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://172.16.3.30:8001)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# MIN_RELEVANCE_SCORE - Minimum score for context (default: 5.0)
|
||||
# MAX_CONTEXTS - Maximum number of contexts to retrieve (default: 10)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://172.16.3.30:8001}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
MIN_SCORE="${MIN_RELEVANCE_SCORE:-5.0}"
|
||||
MAX_ITEMS="${MAX_CONTEXTS:-10}"
|
||||
|
||||
# Local storage paths
|
||||
CLAUDE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
CACHE_DIR="$CLAUDE_DIR/context-cache"
|
||||
QUEUE_DIR="$CLAUDE_DIR/context-queue"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID from git repo if not set
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
# Try to get from git config
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Try to derive from git remote URL
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
# Hash the remote URL to create a consistent ID
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID available
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Silent exit - no context available
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Create cache directory if it doesn't exist
|
||||
PROJECT_CACHE_DIR="$CACHE_DIR/$PROJECT_ID"
|
||||
mkdir -p "$PROJECT_CACHE_DIR" 2>/dev/null
|
||||
|
||||
# Try to sync any queued contexts first (opportunistic)
|
||||
if [ -d "$QUEUE_DIR/pending" ] && [ -n "$JWT_TOKEN" ]; then
|
||||
bash "$(dirname "${BASH_SOURCE[0]}")/sync-contexts" >/dev/null 2>&1 &
|
||||
fi
|
||||
|
||||
# Build API request URL
|
||||
RECALL_URL="${API_URL}/api/conversation-contexts/recall"
|
||||
QUERY_PARAMS="project_id=${PROJECT_ID}&limit=${MAX_ITEMS}&min_relevance_score=${MIN_SCORE}"
|
||||
|
||||
# Try to fetch context from API (with timeout and error handling)
|
||||
API_AVAILABLE=false
|
||||
if [ -n "$JWT_TOKEN" ]; then
|
||||
CONTEXT_RESPONSE=$(curl -s --max-time 3 \
|
||||
"${RECALL_URL}?${QUERY_PARAMS}" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Accept: application/json" 2>/dev/null)
|
||||
|
||||
if [ $? -eq 0 ] && [ -n "$CONTEXT_RESPONSE" ]; then
|
||||
# Check if response is valid JSON (not an error)
|
||||
echo "$CONTEXT_RESPONSE" | python3 -c "import sys, json; json.load(sys.stdin)" 2>/dev/null
|
||||
if [ $? -eq 0 ]; then
|
||||
API_AVAILABLE=true
|
||||
# Save to cache for offline use
|
||||
echo "$CONTEXT_RESPONSE" > "$PROJECT_CACHE_DIR/latest.json"
|
||||
echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ")" > "$PROJECT_CACHE_DIR/last_updated"
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Fallback to local cache if API unavailable
|
||||
if [ "$API_AVAILABLE" = "false" ]; then
|
||||
if [ -f "$PROJECT_CACHE_DIR/latest.json" ]; then
|
||||
CONTEXT_RESPONSE=$(cat "$PROJECT_CACHE_DIR/latest.json")
|
||||
CACHE_AGE="unknown"
|
||||
if [ -f "$PROJECT_CACHE_DIR/last_updated" ]; then
|
||||
CACHE_AGE=$(cat "$PROJECT_CACHE_DIR/last_updated")
|
||||
fi
|
||||
echo "<!-- Using cached context (API unavailable) - Last updated: $CACHE_AGE -->" >&2
|
||||
else
|
||||
# No cache available, exit silently
|
||||
exit 0
|
||||
fi
|
||||
fi
|
||||
|
||||
# Parse and format context
|
||||
CONTEXT_COUNT=$(echo "$CONTEXT_RESPONSE" | grep -o '"id"' | wc -l)
|
||||
|
||||
if [ "$CONTEXT_COUNT" -gt 0 ]; then
|
||||
if [ "$API_AVAILABLE" = "true" ]; then
|
||||
echo "<!-- Context Recall: Retrieved $CONTEXT_COUNT relevant context(s) from API -->"
|
||||
else
|
||||
echo "<!-- Context Recall: Retrieved $CONTEXT_COUNT relevant context(s) from LOCAL CACHE (offline mode) -->"
|
||||
fi
|
||||
echo ""
|
||||
echo "## Previous Context"
|
||||
echo ""
|
||||
if [ "$API_AVAILABLE" = "false" ]; then
|
||||
echo "[WARNING] **Offline Mode** - Using cached context (API unavailable)"
|
||||
echo ""
|
||||
fi
|
||||
echo "The following context has been automatically recalled:"
|
||||
echo ""
|
||||
|
||||
# Extract and format each context entry
|
||||
echo "$CONTEXT_RESPONSE" | python3 -c "
|
||||
import sys, json
|
||||
try:
|
||||
contexts = json.load(sys.stdin)
|
||||
if isinstance(contexts, list):
|
||||
for i, ctx in enumerate(contexts, 1):
|
||||
title = ctx.get('title', 'Untitled')
|
||||
summary = ctx.get('dense_summary', '')
|
||||
score = ctx.get('relevance_score', 0)
|
||||
ctx_type = ctx.get('context_type', 'unknown')
|
||||
|
||||
print(f'### {i}. {title} (Score: {score}/10)')
|
||||
print(f'*Type: {ctx_type}*')
|
||||
print()
|
||||
print(summary)
|
||||
print()
|
||||
print('---')
|
||||
print()
|
||||
except:
|
||||
pass
|
||||
" 2>/dev/null
|
||||
|
||||
echo ""
|
||||
if [ "$API_AVAILABLE" = "true" ]; then
|
||||
echo "*Context automatically injected to maintain continuity across sessions.*"
|
||||
else
|
||||
echo "*Context from local cache - new context will sync when API is available.*"
|
||||
fi
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# Exit successfully
|
||||
exit 0
|
||||
@@ -1,119 +0,0 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Claude Code Hook: user-prompt-submit
|
||||
# Runs BEFORE each user message is processed
|
||||
# Injects relevant context from the database into the conversation
|
||||
#
|
||||
# Expected environment variables:
|
||||
# CLAUDE_PROJECT_ID - UUID of the current project
|
||||
# JWT_TOKEN - Authentication token for API
|
||||
# CLAUDE_API_URL - API base URL (default: http://localhost:8000)
|
||||
# CONTEXT_RECALL_ENABLED - Set to "false" to disable (default: true)
|
||||
# MIN_RELEVANCE_SCORE - Minimum score for context (default: 5.0)
|
||||
# MAX_CONTEXTS - Maximum number of contexts to retrieve (default: 10)
|
||||
#
|
||||
|
||||
# Load configuration if exists
|
||||
CONFIG_FILE="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/context-recall-config.env"
|
||||
if [ -f "$CONFIG_FILE" ]; then
|
||||
source "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Default values
|
||||
API_URL="${CLAUDE_API_URL:-http://localhost:8000}"
|
||||
ENABLED="${CONTEXT_RECALL_ENABLED:-true}"
|
||||
MIN_SCORE="${MIN_RELEVANCE_SCORE:-5.0}"
|
||||
MAX_ITEMS="${MAX_CONTEXTS:-10}"
|
||||
|
||||
# Exit early if disabled
|
||||
if [ "$ENABLED" != "true" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Detect project ID from git repo if not set
|
||||
if [ -z "$CLAUDE_PROJECT_ID" ]; then
|
||||
# Try to get from git config
|
||||
PROJECT_ID=$(git config --local claude.projectid 2>/dev/null)
|
||||
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Try to derive from git remote URL
|
||||
GIT_REMOTE=$(git config --get remote.origin.url 2>/dev/null)
|
||||
if [ -n "$GIT_REMOTE" ]; then
|
||||
# Hash the remote URL to create a consistent ID
|
||||
PROJECT_ID=$(echo -n "$GIT_REMOTE" | md5sum | cut -d' ' -f1)
|
||||
fi
|
||||
fi
|
||||
else
|
||||
PROJECT_ID="$CLAUDE_PROJECT_ID"
|
||||
fi
|
||||
|
||||
# Exit if no project ID available
|
||||
if [ -z "$PROJECT_ID" ]; then
|
||||
# Silent exit - no context available
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Exit if no JWT token
|
||||
if [ -z "$JWT_TOKEN" ]; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Build API request URL
|
||||
RECALL_URL="${API_URL}/api/conversation-contexts/recall"
|
||||
QUERY_PARAMS="project_id=${PROJECT_ID}&limit=${MAX_ITEMS}&min_relevance_score=${MIN_SCORE}"
|
||||
|
||||
# Fetch context from API (with timeout and error handling)
|
||||
CONTEXT_RESPONSE=$(curl -s --max-time 3 \
|
||||
"${RECALL_URL}?${QUERY_PARAMS}" \
|
||||
-H "Authorization: Bearer ${JWT_TOKEN}" \
|
||||
-H "Accept: application/json" 2>/dev/null)
|
||||
|
||||
# Check if request was successful
|
||||
if [ $? -ne 0 ] || [ -z "$CONTEXT_RESPONSE" ]; then
|
||||
# Silent failure - API unavailable
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Parse and format context (expects JSON array of context objects)
|
||||
# Example response: [{"title": "...", "dense_summary": "...", "relevance_score": 8.5}, ...]
|
||||
CONTEXT_COUNT=$(echo "$CONTEXT_RESPONSE" | grep -o '"id"' | wc -l)
|
||||
|
||||
if [ "$CONTEXT_COUNT" -gt 0 ]; then
|
||||
echo "<!-- Context Recall: Retrieved $CONTEXT_COUNT relevant context(s) -->"
|
||||
echo ""
|
||||
echo "## 📚 Previous Context"
|
||||
echo ""
|
||||
echo "The following context has been automatically recalled from previous sessions:"
|
||||
echo ""
|
||||
|
||||
# Extract and format each context entry
|
||||
# Note: This uses simple text parsing. For production, consider using jq if available.
|
||||
echo "$CONTEXT_RESPONSE" | python3 -c "
|
||||
import sys, json
|
||||
try:
|
||||
contexts = json.load(sys.stdin)
|
||||
if isinstance(contexts, list):
|
||||
for i, ctx in enumerate(contexts, 1):
|
||||
title = ctx.get('title', 'Untitled')
|
||||
summary = ctx.get('dense_summary', '')
|
||||
score = ctx.get('relevance_score', 0)
|
||||
ctx_type = ctx.get('context_type', 'unknown')
|
||||
|
||||
print(f'### {i}. {title} (Score: {score}/10)')
|
||||
print(f'*Type: {ctx_type}*')
|
||||
print()
|
||||
print(summary)
|
||||
print()
|
||||
print('---')
|
||||
print()
|
||||
except:
|
||||
pass
|
||||
" 2>/dev/null
|
||||
|
||||
echo ""
|
||||
echo "*This context was automatically injected to help maintain continuity across sessions.*"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# Exit successfully
|
||||
exit 0
|
||||
Reference in New Issue
Block a user