mirror of
https://github.com/catlog22/Claude-Code-Workflow.git
synced 2026-02-05 01:50:27 +08:00
feat: add issue discovery by prompt command with Gemini planning
- Introduced `/issue:discover-by-prompt` command for user-driven issue discovery. - Implemented multi-agent exploration with iterative feedback loops. - Added ACE semantic search for context gathering and cross-module comparison capabilities. - Enhanced user experience with natural language input and adaptive exploration strategies. feat: implement memory update queue tool for batching updates - Created `memory-update-queue.js` for managing CLAUDE.md updates. - Added functionality for queuing paths, deduplication, and auto-flushing based on thresholds and timeouts. - Implemented methods for queue status retrieval, flushing, and timeout checks. - Configured to store queue data persistently in `~/.claude/.memory-queue.json`.
This commit is contained in:
@@ -1256,5 +1256,89 @@ RULES: Be concise. Focus on practical understanding. Include function signatures
|
||||
return true;
|
||||
}
|
||||
|
||||
// API: Memory Queue - Add path to queue
|
||||
if (pathname === '/api/memory/queue/add' && req.method === 'POST') {
|
||||
handlePostRequest(req, res, async (body) => {
|
||||
const { path: modulePath, tool = 'gemini', strategy = 'single-layer' } = body;
|
||||
|
||||
if (!modulePath) {
|
||||
return { error: 'path is required', status: 400 };
|
||||
}
|
||||
|
||||
try {
|
||||
const { memoryQueueTool } = await import('../../tools/memory-update-queue.js');
|
||||
const result = await memoryQueueTool.execute({
|
||||
action: 'add',
|
||||
path: modulePath,
|
||||
tool,
|
||||
strategy
|
||||
}) as { queueSize?: number; willFlush?: boolean; flushed?: boolean };
|
||||
|
||||
// Broadcast queue update event
|
||||
broadcastToClients({
|
||||
type: 'MEMORY_QUEUE_UPDATED',
|
||||
payload: {
|
||||
action: 'add',
|
||||
path: modulePath,
|
||||
queueSize: result.queueSize || 0,
|
||||
willFlush: result.willFlush || false,
|
||||
flushed: result.flushed || false,
|
||||
timestamp: new Date().toISOString()
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true, ...result };
|
||||
} catch (error: unknown) {
|
||||
return { error: (error as Error).message, status: 500 };
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
// API: Memory Queue - Get queue status
|
||||
if (pathname === '/api/memory/queue/status' && req.method === 'GET') {
|
||||
try {
|
||||
const { memoryQueueTool } = await import('../../tools/memory-update-queue.js');
|
||||
const result = await memoryQueueTool.execute({ action: 'status' }) as Record<string, unknown>;
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: true, ...result }));
|
||||
} catch (error: unknown) {
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: (error as Error).message }));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// API: Memory Queue - Flush queue immediately
|
||||
if (pathname === '/api/memory/queue/flush' && req.method === 'POST') {
|
||||
handlePostRequest(req, res, async () => {
|
||||
try {
|
||||
const { memoryQueueTool } = await import('../../tools/memory-update-queue.js');
|
||||
const result = await memoryQueueTool.execute({ action: 'flush' }) as {
|
||||
processed?: number;
|
||||
success?: boolean;
|
||||
errors?: unknown[];
|
||||
};
|
||||
|
||||
// Broadcast queue flushed event
|
||||
broadcastToClients({
|
||||
type: 'MEMORY_QUEUE_FLUSHED',
|
||||
payload: {
|
||||
processed: result.processed || 0,
|
||||
success: result.success || false,
|
||||
errors: result.errors?.length || 0,
|
||||
timestamp: new Date().toISOString()
|
||||
}
|
||||
});
|
||||
|
||||
return { success: true, ...result };
|
||||
} catch (error: unknown) {
|
||||
return { error: (error as Error).message, status: 500 };
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -9,6 +9,15 @@ import { getCliToolsStatus } from '../../tools/cli-executor.js';
|
||||
import { checkVenvStatus, checkSemanticStatus } from '../../tools/codex-lens.js';
|
||||
import type { RouteContext } from './types.js';
|
||||
|
||||
// Performance logging helper
|
||||
const PERF_LOG_ENABLED = process.env.CCW_PERF_LOG === '1' || true; // Enable by default for debugging
|
||||
function perfLog(label: string, startTime: number, extra?: Record<string, unknown>): void {
|
||||
if (!PERF_LOG_ENABLED) return;
|
||||
const duration = Date.now() - startTime;
|
||||
const extraStr = extra ? ` | ${JSON.stringify(extra)}` : '';
|
||||
console.log(`[PERF][Status] ${label}: ${duration}ms${extraStr}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check CCW installation status
|
||||
* Verifies that required workflow files are installed in user's home directory
|
||||
@@ -62,16 +71,39 @@ export async function handleStatusRoutes(ctx: RouteContext): Promise<boolean> {
|
||||
|
||||
// API: Aggregated Status (all statuses in one call)
|
||||
if (pathname === '/api/status/all') {
|
||||
const totalStart = Date.now();
|
||||
console.log('[PERF][Status] === /api/status/all START ===');
|
||||
|
||||
try {
|
||||
// Check CCW installation status (sync, fast)
|
||||
const ccwStart = Date.now();
|
||||
const ccwInstallStatus = checkCcwInstallStatus();
|
||||
perfLog('checkCcwInstallStatus', ccwStart);
|
||||
|
||||
// Execute all status checks in parallel with individual timing
|
||||
const cliStart = Date.now();
|
||||
const codexStart = Date.now();
|
||||
const semanticStart = Date.now();
|
||||
|
||||
// Execute all status checks in parallel
|
||||
const [cliStatus, codexLensStatus, semanticStatus] = await Promise.all([
|
||||
getCliToolsStatus(),
|
||||
checkVenvStatus(),
|
||||
getCliToolsStatus().then(result => {
|
||||
perfLog('getCliToolsStatus', cliStart, { toolCount: Object.keys(result).length });
|
||||
return result;
|
||||
}),
|
||||
checkVenvStatus().then(result => {
|
||||
perfLog('checkVenvStatus', codexStart, { ready: result.ready });
|
||||
return result;
|
||||
}),
|
||||
// Always check semantic status (will return available: false if CodexLens not ready)
|
||||
checkSemanticStatus().catch(() => ({ available: false, backend: null }))
|
||||
checkSemanticStatus()
|
||||
.then(result => {
|
||||
perfLog('checkSemanticStatus', semanticStart, { available: result.available });
|
||||
return result;
|
||||
})
|
||||
.catch(() => {
|
||||
perfLog('checkSemanticStatus (error)', semanticStart);
|
||||
return { available: false, backend: null };
|
||||
})
|
||||
]);
|
||||
|
||||
const response = {
|
||||
@@ -82,10 +114,13 @@ export async function handleStatusRoutes(ctx: RouteContext): Promise<boolean> {
|
||||
timestamp: new Date().toISOString()
|
||||
};
|
||||
|
||||
perfLog('=== /api/status/all TOTAL ===', totalStart);
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify(response));
|
||||
return true;
|
||||
} catch (error) {
|
||||
perfLog('=== /api/status/all ERROR ===', totalStart);
|
||||
console.error('[Status Routes] Error fetching aggregated status:', error);
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: (error as Error).message }));
|
||||
|
||||
@@ -42,6 +42,10 @@ import { randomBytes } from 'crypto';
|
||||
// Import health check service
|
||||
import { getHealthCheckService } from './services/health-check-service.js';
|
||||
|
||||
// Import status check functions for warmup
|
||||
import { checkSemanticStatus, checkVenvStatus } from '../tools/codex-lens.js';
|
||||
import { getCliToolsStatus } from '../tools/cli-executor.js';
|
||||
|
||||
import type { ServerConfig } from '../types/config.js';
|
||||
import type { PostRequestHandler } from './routes/types.js';
|
||||
|
||||
@@ -290,6 +294,56 @@ function setCsrfCookie(res: http.ServerResponse, token: string, maxAgeSeconds: n
|
||||
appendSetCookie(res, attributes.join('; '));
|
||||
}
|
||||
|
||||
/**
|
||||
* Warmup function to pre-populate caches on server startup
|
||||
* This runs asynchronously and non-blocking after the server starts
|
||||
*/
|
||||
async function warmupCaches(initialPath: string): Promise<void> {
|
||||
console.log('[WARMUP] Starting cache warmup...');
|
||||
const startTime = Date.now();
|
||||
|
||||
// Run all warmup tasks in parallel for faster startup
|
||||
const warmupTasks = [
|
||||
// Warmup semantic status cache (Python process startup - can be slow first time)
|
||||
(async () => {
|
||||
const taskStart = Date.now();
|
||||
try {
|
||||
const semanticStatus = await checkSemanticStatus();
|
||||
console.log(`[WARMUP] Semantic status: ${semanticStatus.available ? 'available' : 'not available'} (${Date.now() - taskStart}ms)`);
|
||||
} catch (err) {
|
||||
console.warn(`[WARMUP] Semantic status check failed: ${(err as Error).message}`);
|
||||
}
|
||||
})(),
|
||||
|
||||
// Warmup venv status cache
|
||||
(async () => {
|
||||
const taskStart = Date.now();
|
||||
try {
|
||||
const venvStatus = await checkVenvStatus();
|
||||
console.log(`[WARMUP] Venv status: ${venvStatus.ready ? 'ready' : 'not ready'} (${Date.now() - taskStart}ms)`);
|
||||
} catch (err) {
|
||||
console.warn(`[WARMUP] Venv status check failed: ${(err as Error).message}`);
|
||||
}
|
||||
})(),
|
||||
|
||||
// Warmup CLI tools status cache
|
||||
(async () => {
|
||||
const taskStart = Date.now();
|
||||
try {
|
||||
const cliStatus = await getCliToolsStatus();
|
||||
const availableCount = Object.values(cliStatus).filter(s => s.available).length;
|
||||
const totalCount = Object.keys(cliStatus).length;
|
||||
console.log(`[WARMUP] CLI tools status: ${availableCount}/${totalCount} available (${Date.now() - taskStart}ms)`);
|
||||
} catch (err) {
|
||||
console.warn(`[WARMUP] CLI tools status check failed: ${(err as Error).message}`);
|
||||
}
|
||||
})()
|
||||
];
|
||||
|
||||
await Promise.allSettled(warmupTasks);
|
||||
console.log(`[WARMUP] Cache warmup complete (${Date.now() - startTime}ms total)`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate dashboard HTML with embedded CSS and JS
|
||||
*/
|
||||
@@ -650,6 +704,14 @@ export async function startServer(options: ServerOptions = {}): Promise<http.Ser
|
||||
console.warn('[Server] Failed to start health check service:', err);
|
||||
}
|
||||
|
||||
// Start cache warmup asynchronously (non-blocking)
|
||||
// Uses setImmediate to not delay server startup response
|
||||
setImmediate(() => {
|
||||
warmupCaches(initialPath).catch((err) => {
|
||||
console.warn('[WARMUP] Cache warmup failed:', err);
|
||||
});
|
||||
});
|
||||
|
||||
resolve(server);
|
||||
});
|
||||
server.on('error', reject);
|
||||
|
||||
@@ -33,11 +33,14 @@ function initCliStatus() {
|
||||
* Load all statuses using aggregated endpoint (single API call)
|
||||
*/
|
||||
async function loadAllStatuses() {
|
||||
const totalStart = performance.now();
|
||||
console.log('[PERF][Frontend] loadAllStatuses START');
|
||||
|
||||
// 1. 尝试从缓存获取(预加载的数据)
|
||||
if (window.cacheManager) {
|
||||
const cached = window.cacheManager.get('all-status');
|
||||
if (cached) {
|
||||
console.log('[CLI Status] Loaded all statuses from cache');
|
||||
console.log(`[PERF][Frontend] Cache hit: ${(performance.now() - totalStart).toFixed(1)}ms`);
|
||||
// 应用缓存数据
|
||||
cliToolStatus = cached.cli || {};
|
||||
codexLensStatus = cached.codexLens || { ready: false };
|
||||
@@ -45,25 +48,32 @@ async function loadAllStatuses() {
|
||||
ccwInstallStatus = cached.ccwInstall || { installed: true, workflowsInstalled: true, missingFiles: [], installPath: '' };
|
||||
|
||||
// Load CLI tools config, API endpoints, and CLI Settings(这些有自己的缓存)
|
||||
const configStart = performance.now();
|
||||
await Promise.all([
|
||||
loadCliToolsConfig(),
|
||||
loadApiEndpoints(),
|
||||
loadCliSettingsEndpoints()
|
||||
]);
|
||||
console.log(`[PERF][Frontend] Config/Endpoints load: ${(performance.now() - configStart).toFixed(1)}ms`);
|
||||
|
||||
// Update badges
|
||||
updateCliBadge();
|
||||
updateCodexLensBadge();
|
||||
updateCcwInstallBadge();
|
||||
|
||||
console.log(`[PERF][Frontend] loadAllStatuses TOTAL (cached): ${(performance.now() - totalStart).toFixed(1)}ms`);
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 缓存未命中,从服务器获取
|
||||
try {
|
||||
const fetchStart = performance.now();
|
||||
console.log('[PERF][Frontend] Fetching /api/status/all...');
|
||||
const response = await fetch('/api/status/all');
|
||||
if (!response.ok) throw new Error('Failed to load status');
|
||||
const data = await response.json();
|
||||
console.log(`[PERF][Frontend] /api/status/all fetch: ${(performance.now() - fetchStart).toFixed(1)}ms`);
|
||||
|
||||
// 存入缓存
|
||||
if (window.cacheManager) {
|
||||
@@ -77,10 +87,11 @@ async function loadAllStatuses() {
|
||||
ccwInstallStatus = data.ccwInstall || { installed: true, workflowsInstalled: true, missingFiles: [], installPath: '' };
|
||||
|
||||
// Load CLI tools config, API endpoints, and CLI Settings
|
||||
await Promise.all([
|
||||
loadCliToolsConfig(),
|
||||
loadApiEndpoints(),
|
||||
loadCliSettingsEndpoints()
|
||||
const configStart = performance.now();
|
||||
const [configResult, endpointsResult, settingsResult] = await Promise.all([
|
||||
loadCliToolsConfig().then(r => { console.log(`[PERF][Frontend] loadCliToolsConfig: ${(performance.now() - configStart).toFixed(1)}ms`); return r; }),
|
||||
loadApiEndpoints().then(r => { console.log(`[PERF][Frontend] loadApiEndpoints: ${(performance.now() - configStart).toFixed(1)}ms`); return r; }),
|
||||
loadCliSettingsEndpoints().then(r => { console.log(`[PERF][Frontend] loadCliSettingsEndpoints: ${(performance.now() - configStart).toFixed(1)}ms`); return r; })
|
||||
]);
|
||||
|
||||
// Update badges
|
||||
@@ -88,9 +99,11 @@ async function loadAllStatuses() {
|
||||
updateCodexLensBadge();
|
||||
updateCcwInstallBadge();
|
||||
|
||||
console.log(`[PERF][Frontend] loadAllStatuses TOTAL: ${(performance.now() - totalStart).toFixed(1)}ms`);
|
||||
return data;
|
||||
} catch (err) {
|
||||
console.error('Failed to load aggregated status:', err);
|
||||
console.log(`[PERF][Frontend] loadAllStatuses ERROR after: ${(performance.now() - totalStart).toFixed(1)}ms`);
|
||||
// Fallback to individual calls if aggregated endpoint fails
|
||||
return await loadAllStatusesFallback();
|
||||
}
|
||||
|
||||
@@ -53,39 +53,39 @@ const HOOK_TEMPLATES = {
|
||||
event: 'Stop',
|
||||
matcher: '',
|
||||
command: 'bash',
|
||||
args: ['-c', 'ccw tool exec update_module_claude \'{"strategy":"related","tool":"gemini"}\''],
|
||||
description: 'Update CLAUDE.md for changed modules when session ends',
|
||||
args: ['-c', 'ccw tool exec memory_queue "{\\"action\\":\\"add\\",\\"path\\":\\"$CLAUDE_PROJECT_DIR\\",\\"tool\\":\\"gemini\\",\\"strategy\\":\\"single-layer\\"}"'],
|
||||
description: 'Queue CLAUDE.md update for changed modules when session ends',
|
||||
category: 'memory',
|
||||
configurable: true,
|
||||
config: {
|
||||
tool: { type: 'select', options: ['gemini', 'qwen', 'codex'], default: 'gemini', label: 'CLI Tool' },
|
||||
strategy: { type: 'select', options: ['related', 'single-layer'], default: 'related', label: 'Strategy' }
|
||||
strategy: { type: 'select', options: ['single-layer', 'multi-layer'], default: 'single-layer', label: 'Strategy' }
|
||||
}
|
||||
},
|
||||
'memory-update-periodic': {
|
||||
event: 'PostToolUse',
|
||||
matcher: 'Write|Edit',
|
||||
command: 'bash',
|
||||
args: ['-c', 'INTERVAL=300; LAST_FILE="$HOME/.claude/.last_memory_update"; mkdir -p "$HOME/.claude"; NOW=$(date +%s); LAST=0; [ -f "$LAST_FILE" ] && LAST=$(cat "$LAST_FILE" 2>/dev/null || echo 0); if [ $((NOW - LAST)) -ge $INTERVAL ]; then echo $NOW > "$LAST_FILE"; ccw tool exec update_module_claude \'{"strategy":"related","tool":"gemini"}\' & fi'],
|
||||
description: 'Periodically update CLAUDE.md (default: 5 min interval)',
|
||||
args: ['-c', 'ccw tool exec memory_queue "{\\"action\\":\\"add\\",\\"path\\":\\"$CLAUDE_PROJECT_DIR\\",\\"tool\\":\\"gemini\\",\\"strategy\\":\\"single-layer\\"}"'],
|
||||
description: 'Queue CLAUDE.md update on file changes (batched with threshold/timeout)',
|
||||
category: 'memory',
|
||||
configurable: true,
|
||||
config: {
|
||||
tool: { type: 'select', options: ['gemini', 'qwen', 'codex'], default: 'gemini', label: 'CLI Tool' },
|
||||
interval: { type: 'number', default: 300, min: 60, max: 3600, label: 'Interval (seconds)', step: 60 }
|
||||
strategy: { type: 'select', options: ['single-layer', 'multi-layer'], default: 'single-layer', label: 'Strategy' }
|
||||
}
|
||||
},
|
||||
'memory-update-count-based': {
|
||||
event: 'PostToolUse',
|
||||
matcher: 'Write|Edit',
|
||||
command: 'bash',
|
||||
args: ['-c', 'THRESHOLD=10; COUNT_FILE="$HOME/.claude/.memory_update_count"; mkdir -p "$HOME/.claude"; INPUT=$(cat); FILE_PATH=$(echo "$INPUT" | jq -r ".tool_input.file_path // .tool_input.path // empty" 2>/dev/null); [ -z "$FILE_PATH" ] && exit 0; COUNT=0; [ -f "$COUNT_FILE" ] && COUNT=$(cat "$COUNT_FILE" 2>/dev/null || echo 0); COUNT=$((COUNT + 1)); echo $COUNT > "$COUNT_FILE"; if [ $COUNT -ge $THRESHOLD ]; then echo 0 > "$COUNT_FILE"; ccw tool exec update_module_claude \'{"strategy":"related","tool":"gemini"}\' & fi'],
|
||||
description: 'Update CLAUDE.md when file changes reach threshold (default: 10 files)',
|
||||
args: ['-c', 'ccw tool exec memory_queue "{\\"action\\":\\"add\\",\\"path\\":\\"$CLAUDE_PROJECT_DIR\\",\\"tool\\":\\"gemini\\",\\"strategy\\":\\"single-layer\\"}"'],
|
||||
description: 'Queue CLAUDE.md update on file changes (auto-flush at 5 paths or 5min timeout)',
|
||||
category: 'memory',
|
||||
configurable: true,
|
||||
config: {
|
||||
tool: { type: 'select', options: ['gemini', 'qwen', 'codex'], default: 'gemini', label: 'CLI Tool' },
|
||||
threshold: { type: 'number', default: 10, min: 3, max: 50, label: 'File count threshold', step: 1 }
|
||||
strategy: { type: 'select', options: ['single-layer', 'multi-layer'], default: 'single-layer', label: 'Strategy' }
|
||||
}
|
||||
},
|
||||
// SKILL Context Loader templates
|
||||
@@ -1154,21 +1154,17 @@ function generateWizardCommand() {
|
||||
}
|
||||
|
||||
// Handle memory-update wizard (default)
|
||||
// Now uses memory_queue for batched updates
|
||||
const tool = wizardConfig.tool || 'gemini';
|
||||
const strategy = wizardConfig.strategy || 'related';
|
||||
const interval = wizardConfig.interval || 300;
|
||||
const threshold = wizardConfig.threshold || 10;
|
||||
const strategy = wizardConfig.strategy || 'single-layer';
|
||||
|
||||
// Build the ccw tool command based on configuration
|
||||
const params = JSON.stringify({ strategy, tool });
|
||||
// Build the ccw tool command using memory_queue
|
||||
// Use double quotes to allow bash $CLAUDE_PROJECT_DIR expansion
|
||||
const params = `"{\\"action\\":\\"add\\",\\"path\\":\\"$CLAUDE_PROJECT_DIR\\",\\"tool\\":\\"${tool}\\",\\"strategy\\":\\"${strategy}\\"}"`;
|
||||
|
||||
if (triggerType === 'periodic') {
|
||||
return `INTERVAL=${interval}; LAST_FILE="$HOME/.claude/.last_memory_update"; mkdir -p "$HOME/.claude"; NOW=$(date +%s); LAST=0; [ -f "$LAST_FILE" ] && LAST=$(cat "$LAST_FILE" 2>/dev/null || echo 0); if [ $((NOW - LAST)) -ge $INTERVAL ]; then echo $NOW > "$LAST_FILE"; ccw tool exec update_module_claude '${params}' & fi`;
|
||||
} else if (triggerType === 'count-based') {
|
||||
return `THRESHOLD=${threshold}; COUNT_FILE="$HOME/.claude/.memory_update_count"; mkdir -p "$HOME/.claude"; INPUT=$(cat); FILE_PATH=$(echo "$INPUT" | jq -r ".tool_input.file_path // .tool_input.path // empty" 2>/dev/null); [ -z "$FILE_PATH" ] && exit 0; COUNT=0; [ -f "$COUNT_FILE" ] && COUNT=$(cat "$COUNT_FILE" 2>/dev/null || echo 0); COUNT=$((COUNT + 1)); echo $COUNT > "$COUNT_FILE"; if [ $COUNT -ge $THRESHOLD ]; then echo 0 > "$COUNT_FILE"; ccw tool exec update_module_claude '${params}' & fi`;
|
||||
} else {
|
||||
return `ccw tool exec update_module_claude '${params}'`;
|
||||
}
|
||||
// All trigger types now use the same queue-based command
|
||||
// The queue handles batching (threshold: 5 paths, timeout: 5 min)
|
||||
return `ccw tool exec memory_queue ${params}`;
|
||||
}
|
||||
|
||||
async function submitHookWizard() {
|
||||
|
||||
@@ -1190,6 +1190,9 @@ export {
|
||||
* - api-endpoint: Check LiteLLM endpoint configuration exists
|
||||
*/
|
||||
export async function getCliToolsStatus(): Promise<Record<string, ToolAvailability>> {
|
||||
const funcStart = Date.now();
|
||||
debugLog('PERF', 'getCliToolsStatus START');
|
||||
|
||||
// Default built-in tools
|
||||
const builtInTools = ['gemini', 'qwen', 'codex', 'claude', 'opencode'];
|
||||
|
||||
@@ -1202,6 +1205,7 @@ export async function getCliToolsStatus(): Promise<Record<string, ToolAvailabili
|
||||
}
|
||||
let toolsInfo: ToolInfo[] = builtInTools.map(name => ({ name, type: 'builtin' }));
|
||||
|
||||
const configLoadStart = Date.now();
|
||||
try {
|
||||
// Dynamic import to avoid circular dependencies
|
||||
const { loadClaudeCliTools } = await import('./claude-cli-tools.js');
|
||||
@@ -1225,11 +1229,15 @@ export async function getCliToolsStatus(): Promise<Record<string, ToolAvailabili
|
||||
// Fallback to built-in tools if config load fails
|
||||
debugLog('cli-executor', `Using built-in tools (config load failed: ${(e as Error).message})`);
|
||||
}
|
||||
debugLog('PERF', `Config load: ${Date.now() - configLoadStart}ms, tools: ${toolsInfo.length}`);
|
||||
|
||||
const results: Record<string, ToolAvailability> = {};
|
||||
const toolTimings: Record<string, number> = {};
|
||||
|
||||
const checksStart = Date.now();
|
||||
await Promise.all(toolsInfo.map(async (toolInfo) => {
|
||||
const { name, type, enabled, id } = toolInfo;
|
||||
const toolStart = Date.now();
|
||||
|
||||
// Check availability based on tool type
|
||||
if (type === 'cli-wrapper') {
|
||||
@@ -1271,8 +1279,13 @@ export async function getCliToolsStatus(): Promise<Record<string, ToolAvailabili
|
||||
// For builtin: check system PATH availability
|
||||
results[name] = await checkToolAvailability(name);
|
||||
}
|
||||
|
||||
toolTimings[name] = Date.now() - toolStart;
|
||||
}));
|
||||
|
||||
debugLog('PERF', `Tool checks: ${Date.now() - checksStart}ms | Individual: ${JSON.stringify(toolTimings)}`);
|
||||
debugLog('PERF', `getCliToolsStatus TOTAL: ${Date.now() - funcStart}ms`);
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
|
||||
@@ -49,6 +49,14 @@ interface VenvStatusCache {
|
||||
let venvStatusCache: VenvStatusCache | null = null;
|
||||
const VENV_STATUS_TTL = 5 * 60 * 1000; // 5 minutes TTL
|
||||
|
||||
// Semantic status cache with TTL (same as venv cache)
|
||||
interface SemanticStatusCache {
|
||||
status: SemanticStatus;
|
||||
timestamp: number;
|
||||
}
|
||||
let semanticStatusCache: SemanticStatusCache | null = null;
|
||||
const SEMANTIC_STATUS_TTL = 5 * 60 * 1000; // 5 minutes TTL
|
||||
|
||||
// Track running indexing process for cancellation
|
||||
let currentIndexingProcess: ReturnType<typeof spawn> | null = null;
|
||||
let currentIndexingAborted = false;
|
||||
@@ -147,8 +155,12 @@ function clearVenvStatusCache(): void {
|
||||
* @returns Ready status
|
||||
*/
|
||||
async function checkVenvStatus(force = false): Promise<ReadyStatus> {
|
||||
const funcStart = Date.now();
|
||||
console.log('[PERF][CodexLens] checkVenvStatus START');
|
||||
|
||||
// Use cached result if available and not expired
|
||||
if (!force && venvStatusCache && (Date.now() - venvStatusCache.timestamp < VENV_STATUS_TTL)) {
|
||||
console.log(`[PERF][CodexLens] checkVenvStatus CACHE HIT: ${Date.now() - funcStart}ms`);
|
||||
return venvStatusCache.status;
|
||||
}
|
||||
|
||||
@@ -156,6 +168,7 @@ async function checkVenvStatus(force = false): Promise<ReadyStatus> {
|
||||
if (!existsSync(CODEXLENS_VENV)) {
|
||||
const result = { ready: false, error: 'Venv not found' };
|
||||
venvStatusCache = { status: result, timestamp: Date.now() };
|
||||
console.log(`[PERF][CodexLens] checkVenvStatus (no venv): ${Date.now() - funcStart}ms`);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -163,12 +176,16 @@ async function checkVenvStatus(force = false): Promise<ReadyStatus> {
|
||||
if (!existsSync(VENV_PYTHON)) {
|
||||
const result = { ready: false, error: 'Python executable not found in venv' };
|
||||
venvStatusCache = { status: result, timestamp: Date.now() };
|
||||
console.log(`[PERF][CodexLens] checkVenvStatus (no python): ${Date.now() - funcStart}ms`);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Check codexlens is importable
|
||||
// Check codexlens and core dependencies are importable
|
||||
const spawnStart = Date.now();
|
||||
console.log('[PERF][CodexLens] checkVenvStatus spawning Python...');
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const child = spawn(VENV_PYTHON, ['-c', 'import codexlens; print(codexlens.__version__)'], {
|
||||
const child = spawn(VENV_PYTHON, ['-c', 'import codexlens; import watchdog; print(codexlens.__version__)'], {
|
||||
stdio: ['ignore', 'pipe', 'pipe'],
|
||||
timeout: 10000,
|
||||
});
|
||||
@@ -192,29 +209,54 @@ async function checkVenvStatus(force = false): Promise<ReadyStatus> {
|
||||
}
|
||||
// Cache the result
|
||||
venvStatusCache = { status: result, timestamp: Date.now() };
|
||||
console.log(`[PERF][CodexLens] checkVenvStatus Python spawn: ${Date.now() - spawnStart}ms | TOTAL: ${Date.now() - funcStart}ms | ready: ${result.ready}`);
|
||||
resolve(result);
|
||||
});
|
||||
|
||||
child.on('error', (err) => {
|
||||
const result = { ready: false, error: `Failed to check venv: ${err.message}` };
|
||||
venvStatusCache = { status: result, timestamp: Date.now() };
|
||||
console.log(`[PERF][CodexLens] checkVenvStatus ERROR: ${Date.now() - funcStart}ms`);
|
||||
resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear semantic status cache (call after install/uninstall operations)
|
||||
*/
|
||||
function clearSemanticStatusCache(): void {
|
||||
semanticStatusCache = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if semantic search dependencies are installed
|
||||
* @param force - Force refresh cache (default: false)
|
||||
* @returns Semantic status
|
||||
*/
|
||||
async function checkSemanticStatus(): Promise<SemanticStatus> {
|
||||
async function checkSemanticStatus(force = false): Promise<SemanticStatus> {
|
||||
const funcStart = Date.now();
|
||||
console.log('[PERF][CodexLens] checkSemanticStatus START');
|
||||
|
||||
// Use cached result if available and not expired
|
||||
if (!force && semanticStatusCache && (Date.now() - semanticStatusCache.timestamp < SEMANTIC_STATUS_TTL)) {
|
||||
console.log(`[PERF][CodexLens] checkSemanticStatus CACHE HIT: ${Date.now() - funcStart}ms`);
|
||||
return semanticStatusCache.status;
|
||||
}
|
||||
|
||||
// First check if CodexLens is installed
|
||||
const venvStatus = await checkVenvStatus();
|
||||
if (!venvStatus.ready) {
|
||||
return { available: false, error: 'CodexLens not installed' };
|
||||
const result: SemanticStatus = { available: false, error: 'CodexLens not installed' };
|
||||
semanticStatusCache = { status: result, timestamp: Date.now() };
|
||||
console.log(`[PERF][CodexLens] checkSemanticStatus (no venv): ${Date.now() - funcStart}ms`);
|
||||
return result;
|
||||
}
|
||||
|
||||
// Check semantic module availability and accelerator info
|
||||
const spawnStart = Date.now();
|
||||
console.log('[PERF][CodexLens] checkSemanticStatus spawning Python...');
|
||||
|
||||
return new Promise((resolve) => {
|
||||
const checkCode = `
|
||||
import sys
|
||||
@@ -274,21 +316,31 @@ except Exception as e:
|
||||
const output = stdout.trim();
|
||||
try {
|
||||
const result = JSON.parse(output);
|
||||
resolve({
|
||||
console.log(`[PERF][CodexLens] checkSemanticStatus Python spawn: ${Date.now() - spawnStart}ms | TOTAL: ${Date.now() - funcStart}ms | available: ${result.available}`);
|
||||
const status: SemanticStatus = {
|
||||
available: result.available || false,
|
||||
backend: result.backend,
|
||||
accelerator: result.accelerator || 'CPU',
|
||||
providers: result.providers || [],
|
||||
litellmAvailable: result.litellm_available || false,
|
||||
error: result.error
|
||||
});
|
||||
};
|
||||
// Cache the result
|
||||
semanticStatusCache = { status, timestamp: Date.now() };
|
||||
resolve(status);
|
||||
} catch {
|
||||
resolve({ available: false, error: output || stderr || 'Unknown error' });
|
||||
console.log(`[PERF][CodexLens] checkSemanticStatus PARSE ERROR: ${Date.now() - funcStart}ms`);
|
||||
const errorStatus: SemanticStatus = { available: false, error: output || stderr || 'Unknown error' };
|
||||
semanticStatusCache = { status: errorStatus, timestamp: Date.now() };
|
||||
resolve(errorStatus);
|
||||
}
|
||||
});
|
||||
|
||||
child.on('error', (err) => {
|
||||
resolve({ available: false, error: `Check failed: ${err.message}` });
|
||||
console.log(`[PERF][CodexLens] checkSemanticStatus ERROR: ${Date.now() - funcStart}ms`);
|
||||
const errorStatus: SemanticStatus = { available: false, error: `Check failed: ${err.message}` };
|
||||
semanticStatusCache = { status: errorStatus, timestamp: Date.now() };
|
||||
resolve(errorStatus);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -583,6 +635,7 @@ async function bootstrapWithUv(gpuMode: GpuMode = 'cpu'): Promise<BootstrapResul
|
||||
|
||||
// Clear cache after successful installation
|
||||
clearVenvStatusCache();
|
||||
clearSemanticStatusCache();
|
||||
console.log(`[CodexLens] Bootstrap with UV complete (${gpuMode} mode)`);
|
||||
return { success: true, message: `Installed with UV (${gpuMode} mode)` };
|
||||
}
|
||||
@@ -878,6 +931,7 @@ async function bootstrapVenv(): Promise<BootstrapResult> {
|
||||
|
||||
// Clear cache after successful installation
|
||||
clearVenvStatusCache();
|
||||
clearSemanticStatusCache();
|
||||
return { success: true };
|
||||
} catch (err) {
|
||||
return { success: false, error: `Failed to install codexlens: ${(err as Error).message}` };
|
||||
@@ -1631,6 +1685,7 @@ async function uninstallCodexLens(): Promise<BootstrapResult> {
|
||||
bootstrapChecked = false;
|
||||
bootstrapReady = false;
|
||||
clearVenvStatusCache();
|
||||
clearSemanticStatusCache();
|
||||
|
||||
console.log('[CodexLens] CodexLens uninstalled successfully');
|
||||
return { success: true, message: 'CodexLens uninstalled successfully' };
|
||||
|
||||
@@ -30,6 +30,7 @@ import type { ProgressInfo } from './codex-lens.js';
|
||||
import { uiGeneratePreviewTool } from './ui-generate-preview.js';
|
||||
import { uiInstantiatePrototypesTool } from './ui-instantiate-prototypes.js';
|
||||
import { updateModuleClaudeTool } from './update-module-claude.js';
|
||||
import { memoryQueueTool } from './memory-update-queue.js';
|
||||
|
||||
interface LegacyTool {
|
||||
name: string;
|
||||
@@ -366,6 +367,7 @@ registerTool(toLegacyTool(skillContextLoaderMod));
|
||||
registerTool(uiGeneratePreviewTool);
|
||||
registerTool(uiInstantiatePrototypesTool);
|
||||
registerTool(updateModuleClaudeTool);
|
||||
registerTool(memoryQueueTool);
|
||||
|
||||
// Export for external tool registration
|
||||
export { registerTool };
|
||||
|
||||
421
ccw/src/tools/memory-update-queue.js
Normal file
421
ccw/src/tools/memory-update-queue.js
Normal file
@@ -0,0 +1,421 @@
|
||||
/**
|
||||
* Memory Update Queue Tool
|
||||
* Queue mechanism for batching CLAUDE.md updates
|
||||
*
|
||||
* Configuration:
|
||||
* - Threshold: 5 paths trigger update
|
||||
* - Timeout: 5 minutes auto-trigger
|
||||
* - Storage: ~/.claude/.memory-queue.json
|
||||
* - Deduplication: Same path only kept once
|
||||
*/
|
||||
|
||||
import { existsSync, readFileSync, writeFileSync, mkdirSync } from 'fs';
|
||||
import { join, dirname, resolve } from 'path';
|
||||
import { homedir } from 'os';
|
||||
|
||||
// Configuration constants
|
||||
const QUEUE_THRESHOLD = 5;
|
||||
const QUEUE_TIMEOUT_MS = 5 * 60 * 1000; // 5 minutes
|
||||
const QUEUE_FILE_PATH = join(homedir(), '.claude', '.memory-queue.json');
|
||||
|
||||
// In-memory timeout reference (for cross-call persistence, we track via file timestamp)
|
||||
let scheduledTimeoutId = null;
|
||||
|
||||
/**
|
||||
* Ensure parent directory exists
|
||||
*/
|
||||
function ensureDir(filePath) {
|
||||
const dir = dirname(filePath);
|
||||
if (!existsSync(dir)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load queue from file
|
||||
* @returns {{ items: Array<{path: string, tool: string, strategy: string, addedAt: string}>, createdAt: string | null }}
|
||||
*/
|
||||
function loadQueue() {
|
||||
try {
|
||||
if (existsSync(QUEUE_FILE_PATH)) {
|
||||
const content = readFileSync(QUEUE_FILE_PATH, 'utf8');
|
||||
const data = JSON.parse(content);
|
||||
return {
|
||||
items: Array.isArray(data.items) ? data.items : [],
|
||||
createdAt: data.createdAt || null
|
||||
};
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[MemoryQueue] Failed to load queue:', e.message);
|
||||
}
|
||||
return { items: [], createdAt: null };
|
||||
}
|
||||
|
||||
/**
|
||||
* Save queue to file
|
||||
* @param {{ items: Array<{path: string, tool: string, strategy: string, addedAt: string}>, createdAt: string | null }} data
|
||||
*/
|
||||
function saveQueue(data) {
|
||||
try {
|
||||
ensureDir(QUEUE_FILE_PATH);
|
||||
writeFileSync(QUEUE_FILE_PATH, JSON.stringify(data, null, 2), 'utf8');
|
||||
} catch (e) {
|
||||
console.error('[MemoryQueue] Failed to save queue:', e.message);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize path for comparison (handle Windows/Unix differences)
|
||||
* @param {string} p
|
||||
* @returns {string}
|
||||
*/
|
||||
function normalizePath(p) {
|
||||
return resolve(p).replace(/\\/g, '/').toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add path to queue with deduplication
|
||||
* @param {string} path - Module path to update
|
||||
* @param {{ tool?: string, strategy?: string }} options
|
||||
* @returns {{ queued: boolean, queueSize: number, willFlush: boolean, message: string }}
|
||||
*/
|
||||
function addToQueue(path, options = {}) {
|
||||
const { tool = 'gemini', strategy = 'single-layer' } = options;
|
||||
const queue = loadQueue();
|
||||
const normalizedPath = normalizePath(path);
|
||||
const now = new Date().toISOString();
|
||||
|
||||
// Check for duplicates
|
||||
const existingIndex = queue.items.findIndex(
|
||||
item => normalizePath(item.path) === normalizedPath
|
||||
);
|
||||
|
||||
if (existingIndex !== -1) {
|
||||
// Update existing entry timestamp but keep it deduplicated
|
||||
queue.items[existingIndex].addedAt = now;
|
||||
queue.items[existingIndex].tool = tool;
|
||||
queue.items[existingIndex].strategy = strategy;
|
||||
saveQueue(queue);
|
||||
|
||||
return {
|
||||
queued: false,
|
||||
queueSize: queue.items.length,
|
||||
willFlush: queue.items.length >= QUEUE_THRESHOLD,
|
||||
message: `Path already in queue (updated): ${path}`
|
||||
};
|
||||
}
|
||||
|
||||
// Add new item
|
||||
queue.items.push({
|
||||
path,
|
||||
tool,
|
||||
strategy,
|
||||
addedAt: now
|
||||
});
|
||||
|
||||
// Set createdAt if this is the first item
|
||||
if (!queue.createdAt) {
|
||||
queue.createdAt = now;
|
||||
}
|
||||
|
||||
saveQueue(queue);
|
||||
|
||||
const willFlush = queue.items.length >= QUEUE_THRESHOLD;
|
||||
|
||||
// Schedule timeout if not already scheduled
|
||||
scheduleTimeout();
|
||||
|
||||
return {
|
||||
queued: true,
|
||||
queueSize: queue.items.length,
|
||||
willFlush,
|
||||
message: willFlush
|
||||
? `Queue threshold reached (${queue.items.length}/${QUEUE_THRESHOLD}), will flush`
|
||||
: `Added to queue (${queue.items.length}/${QUEUE_THRESHOLD})`
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current queue status
|
||||
* @returns {{ queueSize: number, threshold: number, items: Array, timeoutMs: number | null, createdAt: string | null }}
|
||||
*/
|
||||
function getQueueStatus() {
|
||||
const queue = loadQueue();
|
||||
let timeUntilTimeout = null;
|
||||
|
||||
if (queue.createdAt && queue.items.length > 0) {
|
||||
const createdTime = new Date(queue.createdAt).getTime();
|
||||
const elapsed = Date.now() - createdTime;
|
||||
timeUntilTimeout = Math.max(0, QUEUE_TIMEOUT_MS - elapsed);
|
||||
}
|
||||
|
||||
return {
|
||||
queueSize: queue.items.length,
|
||||
threshold: QUEUE_THRESHOLD,
|
||||
items: queue.items,
|
||||
timeoutMs: QUEUE_TIMEOUT_MS,
|
||||
timeUntilTimeout,
|
||||
createdAt: queue.createdAt
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush queue - execute batch update
|
||||
* @returns {Promise<{ success: boolean, processed: number, results: Array, errors: Array }>}
|
||||
*/
|
||||
async function flushQueue() {
|
||||
const queue = loadQueue();
|
||||
|
||||
if (queue.items.length === 0) {
|
||||
return {
|
||||
success: true,
|
||||
processed: 0,
|
||||
results: [],
|
||||
errors: [],
|
||||
message: 'Queue is empty'
|
||||
};
|
||||
}
|
||||
|
||||
// Clear timeout
|
||||
clearScheduledTimeout();
|
||||
|
||||
// Import update_module_claude dynamically to avoid circular deps
|
||||
const { updateModuleClaudeTool } = await import('./update-module-claude.js');
|
||||
|
||||
const results = [];
|
||||
const errors = [];
|
||||
|
||||
// Group by tool and strategy for efficiency
|
||||
const groups = new Map();
|
||||
for (const item of queue.items) {
|
||||
const key = `${item.tool}:${item.strategy}`;
|
||||
if (!groups.has(key)) {
|
||||
groups.set(key, []);
|
||||
}
|
||||
groups.get(key).push(item);
|
||||
}
|
||||
|
||||
// Process each group
|
||||
for (const [key, items] of groups) {
|
||||
const [tool, strategy] = key.split(':');
|
||||
console.log(`[MemoryQueue] Processing ${items.length} items with ${tool}/${strategy}`);
|
||||
|
||||
for (const item of items) {
|
||||
try {
|
||||
const result = await updateModuleClaudeTool.execute({
|
||||
path: item.path,
|
||||
tool: item.tool,
|
||||
strategy: item.strategy
|
||||
});
|
||||
|
||||
results.push({
|
||||
path: item.path,
|
||||
success: result.success !== false,
|
||||
result
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(`[MemoryQueue] Failed to update ${item.path}:`, e.message);
|
||||
errors.push({
|
||||
path: item.path,
|
||||
error: e.message
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clear queue after processing
|
||||
saveQueue({ items: [], createdAt: null });
|
||||
|
||||
return {
|
||||
success: errors.length === 0,
|
||||
processed: queue.items.length,
|
||||
results,
|
||||
errors,
|
||||
message: `Processed ${results.length} items, ${errors.length} errors`
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule timeout for auto-flush
|
||||
*/
|
||||
function scheduleTimeout() {
|
||||
// We use file-based timeout tracking for persistence across process restarts
|
||||
// The actual timeout check happens on next add/status call
|
||||
const queue = loadQueue();
|
||||
|
||||
if (!queue.createdAt || queue.items.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const createdTime = new Date(queue.createdAt).getTime();
|
||||
const elapsed = Date.now() - createdTime;
|
||||
|
||||
if (elapsed >= QUEUE_TIMEOUT_MS) {
|
||||
// Timeout already exceeded, should flush
|
||||
console.log('[MemoryQueue] Timeout exceeded, auto-flushing');
|
||||
// Don't await here to avoid blocking
|
||||
flushQueue().catch(e => {
|
||||
console.error('[MemoryQueue] Auto-flush failed:', e.message);
|
||||
});
|
||||
} else if (!scheduledTimeoutId) {
|
||||
// Schedule in-memory timeout for current process
|
||||
const remaining = QUEUE_TIMEOUT_MS - elapsed;
|
||||
scheduledTimeoutId = setTimeout(() => {
|
||||
scheduledTimeoutId = null;
|
||||
const currentQueue = loadQueue();
|
||||
if (currentQueue.items.length > 0) {
|
||||
console.log('[MemoryQueue] Timeout reached, auto-flushing');
|
||||
flushQueue().catch(e => {
|
||||
console.error('[MemoryQueue] Auto-flush failed:', e.message);
|
||||
});
|
||||
}
|
||||
}, remaining);
|
||||
|
||||
// Prevent timeout from keeping process alive
|
||||
if (scheduledTimeoutId.unref) {
|
||||
scheduledTimeoutId.unref();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear scheduled timeout
|
||||
*/
|
||||
function clearScheduledTimeout() {
|
||||
if (scheduledTimeoutId) {
|
||||
clearTimeout(scheduledTimeoutId);
|
||||
scheduledTimeoutId = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if timeout has expired and auto-flush if needed
|
||||
* @returns {Promise<{ expired: boolean, flushed: boolean, result?: object }>}
|
||||
*/
|
||||
async function checkTimeout() {
|
||||
const queue = loadQueue();
|
||||
|
||||
if (!queue.createdAt || queue.items.length === 0) {
|
||||
return { expired: false, flushed: false };
|
||||
}
|
||||
|
||||
const createdTime = new Date(queue.createdAt).getTime();
|
||||
const elapsed = Date.now() - createdTime;
|
||||
|
||||
if (elapsed >= QUEUE_TIMEOUT_MS) {
|
||||
console.log('[MemoryQueue] Timeout expired, triggering flush');
|
||||
const result = await flushQueue();
|
||||
return { expired: true, flushed: true, result };
|
||||
}
|
||||
|
||||
return { expired: false, flushed: false };
|
||||
}
|
||||
|
||||
/**
|
||||
* Main execute function for tool interface
|
||||
* @param {Record<string, unknown>} params
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
async function execute(params) {
|
||||
const { action, path, tool = 'gemini', strategy = 'single-layer' } = params;
|
||||
|
||||
switch (action) {
|
||||
case 'add':
|
||||
if (!path) {
|
||||
throw new Error('Parameter "path" is required for add action');
|
||||
}
|
||||
// Check timeout first
|
||||
const timeoutCheck = await checkTimeout();
|
||||
if (timeoutCheck.flushed) {
|
||||
// Queue was flushed due to timeout, add to fresh queue
|
||||
const result = addToQueue(path, { tool, strategy });
|
||||
return {
|
||||
...result,
|
||||
timeoutFlushed: true,
|
||||
flushResult: timeoutCheck.result
|
||||
};
|
||||
}
|
||||
|
||||
const addResult = addToQueue(path, { tool, strategy });
|
||||
|
||||
// Auto-flush if threshold reached
|
||||
if (addResult.willFlush) {
|
||||
const flushResult = await flushQueue();
|
||||
return {
|
||||
...addResult,
|
||||
flushed: true,
|
||||
flushResult
|
||||
};
|
||||
}
|
||||
|
||||
return addResult;
|
||||
|
||||
case 'status':
|
||||
// Check timeout first
|
||||
await checkTimeout();
|
||||
return getQueueStatus();
|
||||
|
||||
case 'flush':
|
||||
return await flushQueue();
|
||||
|
||||
default:
|
||||
throw new Error(`Unknown action: ${action}. Valid actions: add, status, flush`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tool Definition
|
||||
*/
|
||||
export const memoryQueueTool = {
|
||||
name: 'memory_queue',
|
||||
description: `Memory update queue management. Batches CLAUDE.md updates for efficiency.
|
||||
|
||||
Actions:
|
||||
- add: Add path to queue (auto-flushes at threshold ${QUEUE_THRESHOLD} or timeout ${QUEUE_TIMEOUT_MS / 1000}s)
|
||||
- status: Get queue status
|
||||
- flush: Immediately execute all queued updates`,
|
||||
parameters: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
action: {
|
||||
type: 'string',
|
||||
enum: ['add', 'status', 'flush'],
|
||||
description: 'Queue action to perform'
|
||||
},
|
||||
path: {
|
||||
type: 'string',
|
||||
description: 'Module directory path (required for add action)'
|
||||
},
|
||||
tool: {
|
||||
type: 'string',
|
||||
enum: ['gemini', 'qwen', 'codex'],
|
||||
description: 'CLI tool to use (default: gemini)',
|
||||
default: 'gemini'
|
||||
},
|
||||
strategy: {
|
||||
type: 'string',
|
||||
enum: ['single-layer', 'multi-layer'],
|
||||
description: 'Update strategy (default: single-layer)',
|
||||
default: 'single-layer'
|
||||
}
|
||||
},
|
||||
required: ['action']
|
||||
},
|
||||
execute
|
||||
};
|
||||
|
||||
// Export individual functions for direct use
|
||||
export {
|
||||
loadQueue,
|
||||
saveQueue,
|
||||
addToQueue,
|
||||
getQueueStatus,
|
||||
flushQueue,
|
||||
scheduleTimeout,
|
||||
clearScheduledTimeout,
|
||||
checkTimeout,
|
||||
QUEUE_THRESHOLD,
|
||||
QUEUE_TIMEOUT_MS,
|
||||
QUEUE_FILE_PATH
|
||||
};
|
||||
Reference in New Issue
Block a user