# semantic_chunks

file_path: d:/Claude_dms3/ccw/src/tools/codex-lens.js
created_at: 2025-12-12 11:33:08
content:
  : join(CODEXLENS_VENV, 'bin', 'python');

// Bootstrap status cache
let bootstrapChecked = false;
let bootstrapReady = false;

/**
 * Detect available Python 3 executable
 * @returns {string} - Python executable command
 */
function getSystemPython() {
  const commands = process.platform === 'win32'
    ? ['python', 'py', 'python3']
    : ['python3', 'python'];

  for (const cmd of commands) {
    try {
      const version = execSync(`${cmd} --version 2>&1`, { encoding: 'utf8' });
      if (version.includes('Python 3')) {
        return cmd;
      }
    } catch {
      // Try next command
    }
  }
  throw new Error('Python 3 not found. Please install Python 3 and ensure it is in PATH.');
}

/**
 * Check if CodexLens venv exists and has required packages
 * @returns {Promise<{ready: boolean, error?: string}>}
 */
async function checkVenvStatus() {
  // Check venv exists
  if (!existsSync(CODEXLENS_VENV)) {
    return { ready: false, error: 'Venv not found' };
  }

  // Check python executable exists

metadata:
{"file": "d:/Claude_dms3/ccw/src/tools/codex-lens.js", "language": "javascript", "symbol_name": "checkVenvStatus", "symbol_kind": "function", "start_line": 27, "end_line": 65, "strategy": "symbol"}


file_path: d:/Claude_dms3/ccw/src/tools/codex-lens.js
created_at: 2025-12-12 11:33:08
content:
    return { ready: false, error: 'Python executable not found in venv' };
  }

  // Check codexlens is importable
  return new Promise((resolve) => {
    const child = spawn(VENV_PYTHON, ['-c', 'import codexlens; print(codexlens.__version__)'], {
      stdio: ['ignore', 'pipe', 'pipe'],
      timeout: 10000
    });

    let stdout = '';
    let stderr = '';

    child.stdout.on('data', (data) => { stdout += data.toString(); });
    child.stderr.on('data', (data) => { stderr += data.toString(); });

    child.on('close', (code) => {
      if (code === 0) {
        resolve({ ready: true, version: stdout.trim() });
      } else {
        resolve({ ready: false, error: `CodexLens not installed: ${stderr}` });
      }
    });

    child.on('error', (err) => {
      resolve({ ready: false, error: `Failed to check venv: ${err.message}` });
    });
  });
}

/**
 * Check if semantic search dependencies are installed
 * @returns {Promise<{available: boolean, backend?: string, error?: string}>}
 */
async function checkSemanticStatus() {
  // First check if CodexLens is installed
  const venvStatus = await checkVenvStatus();

metadata:
{"file": "d:/Claude_dms3/ccw/src/tools/codex-lens.js", "language": "javascript", "symbol_name": "checkSemanticStatus", "symbol_kind": "function", "start_line": 67, "end_line": 103, "strategy": "symbol"}


file_path: d:/Claude_dms3/ccw/src/tools/codex-lens.js
created_at: 2025-12-12 11:33:08
content:
    return { available: false, error: 'CodexLens not installed' };
  }

  // Check semantic module availability
  return new Promise((resolve) => {
    const checkCode = `
import sys
try:
    from codexlens.semantic import SEMANTIC_AVAILABLE, SEMANTIC_BACKEND
    if SEMANTIC_AVAILABLE:
        print(f"available:{SEMANTIC_BACKEND}")
    else:
        print("unavailable")
except Exception as e:
    print(f"error:{e}")
`;
    const child = spawn(VENV_PYTHON, ['-c', checkCode], {
      stdio: ['ignore', 'pipe', 'pipe'],
      timeout: 15000
    });

    let stdout = '';
    let stderr = '';

    child.stdout.on('data', (data) => { stdout += data.toString(); });
    child.stderr.on('data', (data) => { stderr += data.toString(); });

    child.on('close', (code) => {
      const output = stdout.trim();
      if (output.startsWith('available:')) {
        const backend = output.split(':')[1];
        resolve({ available: true, backend });
      } else if (output === 'unavailable') {
        resolve({ available: false, error: 'Semantic dependencies not installed' });
      } else {
        resolve({ available: false, error: output || stderr || 'Unknown error' });
      }
    });

    child.on('error', (err) => {
      resolve({ available: false, error: `Check failed: ${err.message}` });

metadata:
{"file": "d:/Claude_dms3/ccw/src/tools/codex-lens.js", "language": "javascript", "symbol_name": "installSemantic", "symbol_kind": "function", "start_line": 105, "end_line": 145, "strategy": "symbol"}


file_path: d:/Claude_dms3/ccw/src/tools/codex-lens.js
created_at: 2025-12-12 11:33:09
content:
  });
}

/**
 * Install semantic search dependencies (fastembed, ONNX-based, ~200MB)
 * @returns {Promise<{success: boolean, error?: string}>}
 */
async function installSemantic() {
  // First ensure CodexLens is installed
  const venvStatus = await checkVenvStatus();
  if (!venvStatus.ready) {
    return { success: false, error: 'CodexLens not installed. Install CodexLens first.' };
  }

  const pipPath = process.platform === 'win32'
    ? join(CODEXLENS_VENV, 'Scripts', 'pip.exe')
    : join(CODEXLENS_VENV, 'bin', 'pip');

  return new Promise((resolve) => {
    console.log('[CodexLens] Installing semantic search dependencies (fastembed)...');
    console.log('[CodexLens] Using ONNX-based fastembed backend (~200MB)');

    const child = spawn(pipPath, ['install', 'numpy>=1.24', 'fastembed>=0.2'], {
      stdio: ['ignore', 'pipe', 'pipe'],
      timeout: 600000 // 10 minutes for potential model download
    });

    let stdout = '';
    let stderr = '';

    child.stdout.on('data', (data) => {
      stdout += data.toString();
      // Log progress
      const line = data.toString().trim();
      if (line.includes('Downloading') || line.includes('Installing') || line.includes('Collecting')) {
        console.log(`[CodexLens] ${line}`);
      }
    });

    child.stderr.on('data', (data) => { stderr += data.toString(); });

    child.on('close', (code) => {
      if (code === 0) {
        console.log('[CodexLens] Semantic dependencies installed successfully');
        resolve({ success: true });
      } else {
        resolve({ success: false, error: `Installation failed: ${stderr || stdout}` });
      }
    });

    child.on('error', (err) => {
      resolve({ success: false, error: `Failed to run pip: ${err.message}` });
    });
  });
}

/**
 * Bootstrap CodexLens venv with required packages
 * @returns {Promise<{success: boolean, error?: string}>}

metadata:
{"file": "d:/Claude_dms3/ccw/src/tools/codex-lens.js", "language": "javascript", "symbol_name": "executeCodexLens", "symbol_kind": "function", "start_line": 147, "end_line": 205, "strategy": "symbol"}


