// ============================================ // Chat Interface Functions // ============================================ let chatSessionId = null; let chatMessages = []; let attachedSessionId = null; let isGenerating = false; // Track if Claude is currently generating let modeSuggestionTimeout = null; // Track mode suggestion auto-hide timer // Reset all chat state function resetChatState() { console.log('Resetting chat state...'); chatSessionId = null; chatMessages = []; attachedSessionId = null; console.log('Chat state reset complete'); } // Load Chat View async function loadChatView() { console.log('[loadChatView] Loading chat view...'); // Preserve attached session ID if it exists (for auto-session workflow) const preservedSessionId = attachedSessionId; // Reset state on view load to prevent stale session references resetChatState(); // Restore attached session if it was set (e.g., from auto-session initialization) if (preservedSessionId) { console.log('[loadChatView] Restoring attached session:', preservedSessionId); attachedSessionId = preservedSessionId; chatSessionId = preservedSessionId; } // Load chat sessions try { console.log('[loadChatView] Fetching sessions...'); const res = await fetch('/claude/api/claude/sessions'); if (!res.ok) { throw new Error(`HTTP ${res.status}: ${await res.text()}`); } const data = await res.json(); console.log('[loadChatView] Sessions data received:', data); const sessionsListEl = document.getElementById('chat-history-list'); if (!sessionsListEl) { console.error('[loadChatView] chat-history-list element not found!'); return; } // ONLY show active sessions - no historical sessions in chat view // Historical sessions are read-only and can't receive new messages let activeSessions = (data.active || []).filter(s => s.status === 'running'); // Filter by current project if in project context const currentProjectDir = window.currentProjectDir; if (currentProjectDir) { console.log('[loadChatView] Filtering sessions for project path:', currentProjectDir); // Filter sessions that belong to this project activeSessions = activeSessions.filter(session => { // Check if session's working directory is within current project directory const sessionWorkingDir = session.workingDir || ''; // Direct match: session working dir starts with project dir const directMatch = sessionWorkingDir.startsWith(currentProjectDir); // Metadata match: session metadata project matches const metadataMatch = session.metadata?.project === currentProjectDir; // For project sessions, also check if project path is in working dir const pathMatch = sessionWorkingDir.includes(currentProjectDir) || currentProjectDir.includes(sessionWorkingDir); const isMatch = directMatch || metadataMatch || pathMatch; console.log(`[loadChatView] Session ${session.id}:`, { workingDir: sessionWorkingDir, projectDir: currentProjectDir, directMatch, metadataMatch, pathMatch, isMatch }); return isMatch; }); console.log('[loadChatView] Project sessions found:', activeSessions.length, 'out of', (data.active || []).length); } console.log('Active sessions (can receive messages):', activeSessions.length); if (activeSessions.length > 0) { sessionsListEl.innerHTML = activeSessions.map(session => { const projectName = session.metadata && session.metadata.project ? session.metadata.project : session.id.substring(0, 20); return `
Creating session for project: ${currentProjectName}
No active sessions
`; sessionsListEl.innerHTML = `Error: ${error.message}
npm install
β’ ls -la
β’ git status
β’ python script.py
Would you like to:
1. Switch to Chat mode for conversational AI
2. Rephrase as a shell command`);
// Auto-switch to Chat mode after brief delay
setTimeout(() => {
if (currentChatMode === 'webcontainer') {
setChatMode('auto');
appendSystemMessage('β
Switched to Chat mode. You can continue your conversation.');
}
}, 3000);
return;
}
try {
appendSystemMessage(`π» Executing in session: ${message}`);
// Send shell command to the active Claude CLI session
const result = await sendShellCommand(sessionId, message);
hideStreamingIndicator();
setGeneratingState(false);
// Show command output
if (result.stdout) {
appendMessage('assistant', result.stdout);
}
if (result.stderr) {
appendMessage('assistant', `' + code.trim() + '';
});
// Handle inline code
formatted = formatted.replace(/`([^`]+)`/g, '$1');
// Handle line breaks
formatted = formatted.replace(/\n/g, 'Start a new conversation with Claude Code.