// CodexLens Manager - Configuration, Model Management, and Semantic Dependencies
// Extracted from cli-manager.js for better maintainability
// ============================================================
// CODEXLENS CONFIGURATION MODAL
// ============================================================
/**
* Show CodexLens configuration modal
*/
async function showCodexLensConfigModal() {
try {
showRefreshToast(t('codexlens.loadingConfig'), 'info');
// Fetch current config
const response = await fetch('/api/codexlens/config');
const config = await response.json();
const modalHtml = buildCodexLensConfigContent(config);
// Create and show modal
const tempContainer = document.createElement('div');
tempContainer.innerHTML = modalHtml;
const modal = tempContainer.firstElementChild;
document.body.appendChild(modal);
// Initialize icons
if (window.lucide) lucide.createIcons();
// Initialize event handlers
initCodexLensConfigEvents(config);
} catch (err) {
showRefreshToast(t('common.error') + ': ' + err.message, 'error');
}
}
/**
* Build CodexLens configuration modal content
*/
function buildCodexLensConfigContent(config) {
const indexDir = config.index_dir || '~/.codexlens/indexes';
const indexCount = config.index_count || 0;
const isInstalled = window.cliToolsStatus?.codexlens?.installed || false;
return '
' +
'
' +
'' +
'
' +
// Status Section
'
' +
// Index Storage Path Section
'
' +
// Actions Section
'
' +
// Semantic Dependencies Section
(isInstalled
? '
'
: '') +
// Model Management Section
(isInstalled
? '
'
: '') +
// Test Search Section
(isInstalled
? '
'
: '') +
'
' +
// Footer
'' +
'
';
}
/**
* Initialize CodexLens config modal event handlers
*/
function initCodexLensConfigEvents(currentConfig) {
// Save button
var saveBtn = document.getElementById('saveCodexLensConfigBtn');
if (saveBtn) {
saveBtn.onclick = async function() {
var indexDirInput = document.getElementById('indexDirInput');
var newIndexDir = indexDirInput ? indexDirInput.value.trim() : '';
if (!newIndexDir) {
showRefreshToast(t('codexlens.pathEmpty'), 'error');
return;
}
if (newIndexDir === currentConfig.index_dir) {
closeModal();
return;
}
saveBtn.disabled = true;
saveBtn.innerHTML = '
' + t('common.saving') + '';
try {
var response = await fetch('/api/codexlens/config', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ index_dir: newIndexDir })
});
var result = await response.json();
if (result.success) {
showRefreshToast(t('codexlens.configSaved'), 'success');
closeModal();
// Refresh CodexLens status
if (typeof loadCodexLensStatus === 'function') {
await loadCodexLensStatus();
renderToolsSection();
if (window.lucide) lucide.createIcons();
}
} else {
showRefreshToast(t('common.saveFailed') + ': ' + result.error, 'error');
saveBtn.disabled = false;
saveBtn.innerHTML = '
' + t('codexlens.saveConfig');
if (window.lucide) lucide.createIcons();
}
} catch (err) {
showRefreshToast(t('common.error') + ': ' + err.message, 'error');
saveBtn.disabled = false;
saveBtn.innerHTML = '
' + t('codexlens.saveConfig');
if (window.lucide) lucide.createIcons();
}
};
}
// Test Search Button
var runSearchBtn = document.getElementById('runSearchBtn');
if (runSearchBtn) {
runSearchBtn.onclick = async function() {
var searchType = document.getElementById('searchTypeSelect').value;
var searchMode = document.getElementById('searchModeSelect').value;
var query = document.getElementById('searchQueryInput').value.trim();
var resultsDiv = document.getElementById('searchResults');
var resultCount = document.getElementById('searchResultCount');
var resultContent = document.getElementById('searchResultContent');
if (!query) {
showRefreshToast(t('codexlens.enterQuery'), 'warning');
return;
}
runSearchBtn.disabled = true;
runSearchBtn.innerHTML = '
' + t('codexlens.searching') + '';
resultsDiv.classList.add('hidden');
try {
var endpoint = '/api/codexlens/' + searchType;
var params = new URLSearchParams({ query: query, limit: '20' });
// Add mode parameter for search and search_files (not for symbol search)
if (searchType === 'search' || searchType === 'search_files') {
params.append('mode', searchMode);
}
var response = await fetch(endpoint + '?' + params.toString());
var result = await response.json();
console.log('[CodexLens Test] Search result:', result);
if (result.success) {
var results = result.results || result.files || [];
resultCount.textContent = results.length + ' ' + t('codexlens.resultsCount');
resultContent.textContent = JSON.stringify(results, null, 2);
resultsDiv.classList.remove('hidden');
showRefreshToast(t('codexlens.searchCompleted') + ': ' + results.length + ' ' + t('codexlens.resultsCount'), 'success');
} else {
resultContent.textContent = t('common.error') + ': ' + (result.error || t('common.unknownError'));
resultsDiv.classList.remove('hidden');
showRefreshToast(t('codexlens.searchFailed') + ': ' + result.error, 'error');
}
runSearchBtn.disabled = false;
runSearchBtn.innerHTML = '
' + t('codexlens.runSearch');
if (window.lucide) lucide.createIcons();
} catch (err) {
console.error('[CodexLens Test] Error:', err);
resultContent.textContent = t('common.exception') + ': ' + err.message;
resultsDiv.classList.remove('hidden');
showRefreshToast(t('common.error') + ': ' + err.message, 'error');
runSearchBtn.disabled = false;
runSearchBtn.innerHTML = '
' + t('codexlens.runSearch');
if (window.lucide) lucide.createIcons();
}
};
}
// Load semantic dependencies status
loadSemanticDepsStatus();
// Load model list
loadModelList();
}
// ============================================================
// SEMANTIC DEPENDENCIES MANAGEMENT
// ============================================================
/**
* Load semantic dependencies status
*/
async function loadSemanticDepsStatus() {
var container = document.getElementById('semanticDepsStatus');
if (!container) return;
try {
var response = await fetch('/api/codexlens/semantic/status');
var result = await response.json();
if (result.available) {
container.innerHTML =
'
' +
'' +
'' + t('codexlens.semanticInstalled') + '' +
'(' + (result.backend || 'fastembed') + ')' +
'
';
} else {
container.innerHTML =
'
' +
'
' +
'' +
'' + t('codexlens.semanticNotInstalled') + '' +
'
' +
'
' +
'
';
}
if (window.lucide) lucide.createIcons();
} catch (err) {
container.innerHTML =
'
' + t('common.error') + ': ' + err.message + '
';
}
}
/**
* Install semantic dependencies
*/
async function installSemanticDeps() {
var container = document.getElementById('semanticDepsStatus');
if (!container) return;
container.innerHTML =
'
' + t('codexlens.installingDeps') + '
';
try {
var response = await fetch('/api/codexlens/semantic/install', { method: 'POST' });
var result = await response.json();
if (result.success) {
showRefreshToast(t('codexlens.depsInstalled'), 'success');
await loadSemanticDepsStatus();
await loadModelList();
} else {
showRefreshToast(t('codexlens.depsInstallFailed') + ': ' + result.error, 'error');
await loadSemanticDepsStatus();
}
} catch (err) {
showRefreshToast(t('common.error') + ': ' + err.message, 'error');
await loadSemanticDepsStatus();
}
}
// ============================================================
// MODEL MANAGEMENT
// ============================================================
/**
* Load model list
*/
async function loadModelList() {
var container = document.getElementById('modelListContainer');
if (!container) return;
try {
var response = await fetch('/api/codexlens/models');
var result = await response.json();
if (!result.success || !result.result || !result.result.models) {
container.innerHTML =
'
' + t('codexlens.semanticNotInstalled') + '
';
return;
}
var models = result.result.models;
var html = '
';
models.forEach(function(model) {
var statusIcon = model.installed
? '
'
: '
';
var sizeText = model.installed
? model.actual_size_mb.toFixed(1) + ' MB'
: '~' + model.estimated_size_mb + ' MB';
var actionBtn = model.installed
? '
'
: '
';
html +=
'
' +
'
' +
'
' +
'
' +
statusIcon +
'' + model.profile + '' +
'(' + model.dimensions + ' dims)' +
'
' +
'
' + model.model_name + '
' +
'
' + model.use_case + '
' +
'
' +
'
' +
'
' + sizeText + '
' +
actionBtn +
'
' +
'
' +
'
';
});
html += '
';
container.innerHTML = html;
if (window.lucide) lucide.createIcons();
} catch (err) {
container.innerHTML =
'
' + t('common.error') + ': ' + err.message + '
';
}
}
/**
* Download model
*/
async function downloadModel(profile) {
var modelCard = document.getElementById('model-' + profile);
if (!modelCard) return;
var originalHTML = modelCard.innerHTML;
modelCard.innerHTML =
'
' +
'' + t('codexlens.downloading') + '' +
'
';
try {
var response = await fetch('/api/codexlens/models/download', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ profile: profile })
});
var result = await response.json();
if (result.success) {
showRefreshToast(t('codexlens.modelDownloaded') + ': ' + profile, 'success');
await loadModelList();
} else {
showRefreshToast(t('codexlens.modelDownloadFailed') + ': ' + result.error, 'error');
modelCard.innerHTML = originalHTML;
if (window.lucide) lucide.createIcons();
}
} catch (err) {
showRefreshToast(t('common.error') + ': ' + err.message, 'error');
modelCard.innerHTML = originalHTML;
if (window.lucide) lucide.createIcons();
}
}
/**
* Delete model
*/
async function deleteModel(profile) {
if (!confirm(t('codexlens.deleteModelConfirm') + ' ' + profile + '?')) {
return;
}
var modelCard = document.getElementById('model-' + profile);
if (!modelCard) return;
var originalHTML = modelCard.innerHTML;
modelCard.innerHTML =
'
' +
'' + t('codexlens.deleting') + '' +
'
';
try {
var response = await fetch('/api/codexlens/models/delete', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ profile: profile })
});
var result = await response.json();
if (result.success) {
showRefreshToast(t('codexlens.modelDeleted') + ': ' + profile, 'success');
await loadModelList();
} else {
showRefreshToast(t('codexlens.modelDeleteFailed') + ': ' + result.error, 'error');
modelCard.innerHTML = originalHTML;
if (window.lucide) lucide.createIcons();
}
} catch (err) {
showRefreshToast(t('common.error') + ': ' + err.message, 'error');
modelCard.innerHTML = originalHTML;
if (window.lucide) lucide.createIcons();
}
}
// ============================================================
// CODEXLENS ACTIONS
// ============================================================
/**
* Initialize CodexLens index with bottom floating progress bar
*/
function initCodexLensIndex() {
// Remove existing progress bar if any
closeCodexLensIndexModal();
// Create bottom floating progress bar
var progressBar = document.createElement('div');
progressBar.id = 'codexlensIndexFloating';
progressBar.className = 'fixed bottom-0 left-0 right-0 z-50 bg-card border-t border-border shadow-lg transform transition-transform duration-300';
progressBar.innerHTML =
'
' +
'
' +
'
' +
'
' +
'
' +
'
' +
'' + t('codexlens.indexing') + '' +
'0%' +
'
' +
'
' + t('codexlens.preparingIndex') + '
' +
'
' +
'
' +
'
' +
'
' +
'
' +
'
';
document.body.appendChild(progressBar);
if (window.lucide) lucide.createIcons();
// Start indexing
startCodexLensIndexing();
}
/**
* Start the indexing process
*/
async function startCodexLensIndexing() {
var statusText = document.getElementById('codexlensIndexStatus');
var progressBar = document.getElementById('codexlensIndexProgressBar');
var percentText = document.getElementById('codexlensIndexPercent');
var spinner = document.getElementById('codexlensIndexSpinner');
// Setup WebSocket listener for progress events
window.codexlensIndexProgressHandler = function(data) {
var payload = data.payload || data;
console.log('[CodexLens] Progress event received:', payload);
if (statusText) statusText.textContent = payload.message || t('codexlens.indexing');
if (progressBar) progressBar.style.width = (payload.percent || 0) + '%';
if (percentText) percentText.textContent = (payload.percent || 0) + '%';
// Handle completion
if (payload.stage === 'complete') {
handleIndexComplete(true, payload.message);
} else if (payload.stage === 'error') {
handleIndexComplete(false, payload.message);
}
};
// Register with notification system
if (typeof registerWsEventHandler === 'function') {
registerWsEventHandler('CODEXLENS_INDEX_PROGRESS', window.codexlensIndexProgressHandler);
console.log('[CodexLens] Registered WebSocket progress handler');
} else {
console.warn('[CodexLens] registerWsEventHandler not available');
}
try {
console.log('[CodexLens] Starting index for:', projectPath);
var response = await fetch('/api/codexlens/init', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ path: projectPath })
});
var result = await response.json();
console.log('[CodexLens] Init result:', result);
// Check if completed successfully (WebSocket might have already reported)
if (result.success) {
handleIndexComplete(true, t('codexlens.indexComplete'));
} else if (!result.success) {
handleIndexComplete(false, result.error || t('common.unknownError'));
}
} catch (err) {
console.error('[CodexLens] Init error:', err);
handleIndexComplete(false, err.message);
}
}
/**
* Handle index completion
*/
function handleIndexComplete(success, message) {
var statusText = document.getElementById('codexlensIndexStatus');
var progressBar = document.getElementById('codexlensIndexProgressBar');
var percentText = document.getElementById('codexlensIndexPercent');
var spinner = document.getElementById('codexlensIndexSpinner');
var floatingBar = document.getElementById('codexlensIndexFloating');
// Unregister WebSocket handler
if (typeof unregisterWsEventHandler === 'function' && window.codexlensIndexProgressHandler) {
unregisterWsEventHandler('CODEXLENS_INDEX_PROGRESS', window.codexlensIndexProgressHandler);
}
if (success) {
if (progressBar) progressBar.style.width = '100%';
if (percentText) percentText.textContent = '100%';
if (statusText) statusText.textContent = t('codexlens.indexComplete');
if (spinner) {
spinner.classList.remove('animate-spin', 'border-primary');
spinner.classList.add('border-green-500');
spinner.innerHTML = '
';
if (window.lucide) lucide.createIcons();
}
if (floatingBar) {
floatingBar.classList.add('bg-green-500/10');
}
showRefreshToast(t('codexlens.indexSuccess'), 'success');
// Auto-close after 3 seconds
setTimeout(function() {
closeCodexLensIndexModal();
// Refresh status
if (typeof loadCodexLensStatus === 'function') {
loadCodexLensStatus().then(function() {
renderToolsSection();
if (window.lucide) lucide.createIcons();
});
}
}, 3000);
} else {
if (progressBar) {
progressBar.classList.remove('bg-primary');
progressBar.classList.add('bg-destructive');
}
if (statusText) statusText.textContent = message || t('codexlens.indexFailed');
if (spinner) {
spinner.classList.remove('animate-spin', 'border-primary');
spinner.innerHTML = '
';
if (window.lucide) lucide.createIcons();
}
if (floatingBar) {
floatingBar.classList.add('bg-destructive/10');
}
showRefreshToast(t('codexlens.indexFailed') + ': ' + message, 'error');
}
}
/**
* Close floating progress bar
*/
function closeCodexLensIndexModal() {
var floatingBar = document.getElementById('codexlensIndexFloating');
if (floatingBar) {
floatingBar.classList.add('translate-y-full');
setTimeout(function() {
floatingBar.remove();
}, 300);
}
// Unregister WebSocket handler
if (typeof unregisterWsEventHandler === 'function' && window.codexlensIndexProgressHandler) {
unregisterWsEventHandler('CODEXLENS_INDEX_PROGRESS', window.codexlensIndexProgressHandler);
}
}
/**
* Install CodexLens
*/
function installCodexLens() {
openCliInstallWizard('codexlens');
}
/**
* Uninstall CodexLens
*/
function uninstallCodexLens() {
openCliUninstallWizard('codexlens');
}
/**
* Clean current workspace index
*/
async function cleanCurrentWorkspaceIndex() {
if (!confirm(t('codexlens.cleanCurrentWorkspaceConfirm'))) {
return;
}
try {
showRefreshToast(t('codexlens.cleaning'), 'info');
// Get current workspace path (projectPath is a global variable from state.js)
var workspacePath = projectPath;
var response = await fetch('/api/codexlens/clean', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ path: workspacePath })
});
var result = await response.json();
if (result.success) {
showRefreshToast(t('codexlens.cleanCurrentWorkspaceSuccess'), 'success');
// Refresh status
if (typeof loadCodexLensStatus === 'function') {
await loadCodexLensStatus();
renderToolsSection();
if (window.lucide) lucide.createIcons();
}
} else {
showRefreshToast(t('codexlens.cleanFailed') + ': ' + result.error, 'error');
}
} catch (err) {
showRefreshToast(t('common.error') + ': ' + err.message, 'error');
}
}
/**
* Clean all CodexLens indexes
*/
async function cleanCodexLensIndexes() {
if (!confirm(t('codexlens.cleanConfirm'))) {
return;
}
try {
showRefreshToast(t('codexlens.cleaning'), 'info');
var response = await fetch('/api/codexlens/clean', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ all: true })
});
var result = await response.json();
if (result.success) {
showRefreshToast(t('codexlens.cleanSuccess'), 'success');
// Refresh status
if (typeof loadCodexLensStatus === 'function') {
await loadCodexLensStatus();
renderToolsSection();
if (window.lucide) lucide.createIcons();
}
} else {
showRefreshToast(t('codexlens.cleanFailed') + ': ' + result.error, 'error');
}
} catch (err) {
showRefreshToast(t('common.error') + ': ' + err.message, 'error');
}
}