// static/js/chat.js
/**
* Main chat functionality - message handling and streaming
*/
// ES6 module — IIFE removed
import Storage from './storage.js';
import uiModule from './ui.js';
import sessionModule from './sessions.js';
import chatRenderer from './chatRenderer.js';
import chatStream from './chatStream.js';
import { addAITTSButton } from './tts-ai.js';
import markdownModule from './markdown.js';
import spinnerModule from './spinner.js';
import presetsModule from './presets.js';
import fileHandlerModule from './fileHandler.js';
import searchModule from './search.js';
import documentModule from './document.js';
import * as emailInbox from './emailInbox.js';
import codeRunnerModule from './codeRunner.js';
import slashCommands, { initSlashCommands, isCommand, handleSlashCommand, handleSetupInput, handleSetupWizard, typewriterInto } from './slashCommands.js';
import createResearchSynapse from './researchSynapse.js';
const RESEARCH_TIMEOUT_MS = 360000;
const DEFAULT_TIMEOUT_MS = 120000;
const RESEARCH_SVG = '';
let API_BASE = '';
let currentAbort = null;
let isStreaming = false;
// Continuous stall watchdog: while streaming, if the SSE stream produces
// NOTHING for STALL_THRESHOLD_MS (no deltas, no tool heartbeat — tools beat
// every 2s, so a full minute of silence means it's genuinely stuck or the
// model quietly stopped), surface a non-destructive "still working?" prompt
// instead of silently hanging. Replaces relying only on the tab-refocus
// recovery (which fired only on visibilitychange and silently reloaded).
let _stallWatchdog = null;
let _stallBannerShown = false;
const STALL_THRESHOLD_MS = 60000;
let _sendInFlight = false; // covers the window from click → streaming start
let _displayOverride = null; // Override visible user bubble text (hides injected prompts)
let _hideUserBubble = false; // Skip user bubble entirely (e.g. continue after stop)
let _pendingContinue = null; // Stores the stopped AI element to merge with new response
// ── Auto-recovery: when a turn's stream silently dies (connection drop) or
// goes quiet while the connection is alive, re-engage the model with a
// completion handshake instead of leaving it hung. Capped so it can't loop.
let _autoNudges = 0; // handshakes fired for the CURRENT user turn
let _autoContinuePending = false; // marks the next submit as an auto-continue (don't reset the counter)
const _AUTO_NUDGE_CAP = 3;
// shortModel and modelColor are now in chatRenderer.js
var _shortModel = chatRenderer.shortModel;
var _applyModelColor = chatRenderer.applyModelColor;
// Per-session research tracking (supports concurrent research across sessions)
const _researchingStreamIds = new Set();
let _researchTimerEl = null, _researchTimerInterval = null;
let _researchStartTime = 0, _researchAvgDuration = null;
let _researchSynapse = null;
function _clearResearchTimer() {
if (_researchTimerInterval) { clearInterval(_researchTimerInterval); _researchTimerInterval = null; }
if (_researchTimerEl) { _researchTimerEl.remove(); _researchTimerEl = null; }
if (_researchSynapse) {
// Mark complete first so the user briefly sees the "done" state,
// then tear it down on next tick.
try { _researchSynapse.complete(); } catch {}
const s = _researchSynapse;
_researchSynapse = null;
setTimeout(() => { try { s.destroy(); } catch {} }, 800);
}
_researchStartTime = 0;
_researchAvgDuration = null;
}
/** Append a "Generate Visual Report" button — delegates to chatRenderer. */
function _appendViewReportLink(msgEl, sessionId) {
const body = msgEl.querySelector('.body');
if (body) chatRenderer.appendReportButton(body, sessionId);
}
let currentAccumulated = ''; // Track accumulated text across function scope
let currentHolder = null; // Track current message holder
let currentSpinner = null; // Track current spinner for stop cleanup
// Background streaming support
const _backgroundStreams = new Map(); // sessionId -> { status, accumulated, sourcesHtml, abortCtrl, query, metrics }
let _streamSessionId = null; // Session ID for the currently active reader loop
let _lastReaderActivity = 0; // Timestamp of last reader.read() success — used to detect frozen streams
let _webLockRelease = null; // Function to release the Web Lock held during streaming
/** Check if an SSE reader is still actively connected for a session. */
function hasActiveStream(sessionId) {
return _streamSessionId === sessionId || _backgroundStreams.has(sessionId);
}
// Sources box builder and toggleSources are now in chatRenderer.js
var _buildSourcesBox = chatRenderer.buildSourcesBox;
// Browser notifications now in chatStream.js
var _notifyResearchComplete = chatStream.notifyResearchComplete;
// Model/image pricing, _buildImageBubble now in chatRenderer.js
var _buildImageBubble = chatRenderer.buildImageBubble;
var getModelCost = chatRenderer.getModelCost;
var getImageCost = chatRenderer.getImageCost;
// stripToolBlocks and roleTimestamp now in chatRenderer.js
var stripToolBlocks = chatRenderer.stripToolBlocks;
function _normalizeEndpointForCompare(url) {
if (!url) return '';
try {
const u = new URL(String(url), window.location.origin);
let path = u.pathname.replace(/\/+$/, '');
const suffixes = [
'/v1/chat/completions', '/chat/completions',
'/v1/completions', '/completions',
'/v1/messages', '/messages',
'/v1/models', '/models',
];
for (const suffix of suffixes) {
if (path.toLowerCase().endsWith(suffix)) {
path = path.slice(0, -suffix.length).replace(/\/+$/, '');
break;
}
}
return (u.origin + path).toLowerCase();
} catch (_) {
return String(url).trim().replace(/\/+$/, '').toLowerCase();
}
}
async function _probeCurrentEndpointStatus(endpointUrl, signal) {
const target = _normalizeEndpointForCompare(endpointUrl);
if (!target) return null;
const modelsRes = await fetch(`${API_BASE}/api/models`, { credentials: 'same-origin', signal });
if (!modelsRes.ok) return null;
const modelsData = await modelsRes.json().catch(() => ({}));
const item = (modelsData.items || []).find(ep =>
_normalizeEndpointForCompare(ep.url || ep.endpoint_url || ep.base_url) === target
);
if (!item || !item.endpoint_id) return null;
const probesRes = await fetch(`${API_BASE}/api/model-endpoints/probe-local`, {
credentials: 'same-origin',
signal,
});
if (!probesRes.ok) return null;
const probes = await probesRes.json().catch(() => ({}));
return probes[item.endpoint_id] || null;
}
/**
* Initialize with dependencies
*/
export function init(apiBase) {
API_BASE = apiBase;
initSlashCommands({ apiBase, isStreaming: () => isStreaming });
// Initialize email inbox
emailInbox.init(documentModule);
}
// addMessage, createMsgFooter, displayMetrics, hideWelcomeScreen, showWelcomeScreen
// are now in chatRenderer.js — referenced via the public API delegation above.
var addMessage = chatRenderer.addMessage;
var createMsgFooter = chatRenderer.createMsgFooter;
var displayMetrics = chatRenderer.displayMetrics;
var hideWelcomeScreen = chatRenderer.hideWelcomeScreen;
var showWelcomeScreen = chatRenderer.showWelcomeScreen;
/**
* Update submit button state
*/
function updateSubmitButton(state, submitBtn) {
if (!submitBtn) return;
if (state === 'streaming') {
// Clear any pending transitions from + → arrow swap
submitBtn.classList.remove('anim-spin', 'anim-spin-swap', 'anim-land', 'mic-mode', 'newchat-mode', 'newchat-expanded', 'recording');
// Ensure arrow icon is showing before launch
var icons = window._odysseusBtnIcons;
if (icons) submitBtn.innerHTML = icons.send;
void submitBtn.offsetWidth;
// Arrow launches up, then stop icon lands in
submitBtn.classList.add('anim-launch');
const _stopSvg = '';
// Wait for the launch keyframe to finish (0.3s) before swapping the
// arrow out for the stop icon — otherwise the swap happens mid-flight
// and the user sees nothing fly out.
setTimeout(() => {
submitBtn.innerHTML = _stopSvg;
submitBtn.classList.remove('anim-launch');
void submitBtn.offsetWidth;
submitBtn.classList.add('anim-land');
submitBtn.addEventListener('animationend', () => submitBtn.classList.remove('anim-land'), { once: true });
}, 300);
submitBtn.title = 'Stop generation';
submitBtn.dataset.mode = 'streaming';
submitBtn.dataset.phase = 'processing';
isStreaming = true;
_startStallWatchdog();
} else if (state === 'idle') {
submitBtn.dataset.mode = '';
delete submitBtn.dataset.phase;
submitBtn.classList.remove('recording');
isStreaming = false;
_stopStallWatchdog();
// Defer to global updater which handles mic/newchat/send modes
if (window._updateSendBtnIcon) {
setTimeout(window._updateSendBtnIcon, 50);
} else {
var icons = window._odysseusBtnIcons;
submitBtn.innerHTML = icons ? icons.send : '';
submitBtn.title = 'Send message';
submitBtn.classList.remove('mic-mode', 'newchat-mode');
}
}
}
// -----------------------------------------------------------------------
// Slash commands — now in slashCommands.js
// -----------------------------------------------------------------------
// API key pattern for the guard in handleChatSubmit
const API_KEY_RE = /^(sk-[a-zA-Z0-9_\-]{20,}|gsk_[a-zA-Z0-9]{20,}|AIza[a-zA-Z0-9_\-]{30,}|xai-[a-zA-Z0-9]{20,})$/;
/**
* Handle chat form submission
*/
export async function handleChatSubmit(e) {
e.preventDefault();
// Cancel research clarification timeout if active
if (window._researchTimeoutTimer) {
clearTimeout(window._researchTimeoutTimer);
window._researchTimeoutTimer = null;
}
// Get current session
const sessionId = sessionModule.getCurrentSessionId();
const session = sessionModule.getSessions().find(s => s.id === sessionId);
const submitBtn = document.querySelector('.send-btn');
// If compare is active, stop all compare streams
if (window.compareModule && window.compareModule.isActive()) {
window.compareModule.handleCompareSubmit();
return;
}
// If currently streaming, stop it
if (isStreaming) {
// Cancel server-side research if in progress
const _cancelSid = sessionModule.getCurrentSessionId();
if (_cancelSid && _researchingStreamIds.has(_cancelSid)) {
fetch(`${API_BASE}/api/research/cancel/${_cancelSid}`, { method: 'POST' }).catch(e => console.warn('Research cancel failed:', e));
_researchingStreamIds.delete(_cancelSid);
_clearResearchTimer();
}
abortCurrentRequest(true); // explicit user Stop → also cancel the detached server run
// Clean up any running agent thread nodes (stop wave animation, remove "running" state)
document.querySelectorAll('.agent-thread-node.running').forEach(node => {
if (node._waveInterval) { clearInterval(node._waveInterval); node._waveInterval = null; }
if (node._elapsedTicker) { clearInterval(node._elapsedTicker); node._elapsedTicker = null; }
node.classList.remove('running');
const wave = node.querySelector('.agent-thread-wave');
if (wave) wave.textContent = '';
const icon = node.querySelector('.agent-thread-icon');
if (icon) icon.textContent = '\u25A0'; // stop square
const statusEl = node.querySelector('.agent-thread-status');
if (!statusEl) {
const header = node.querySelector('.agent-thread-header');
if (header) {
const s = document.createElement('span');
s.className = 'agent-thread-status';
s.textContent = 'stopped';
header.appendChild(s);
}
}
});
document.querySelectorAll('.agent-thread.streaming').forEach(t => t.classList.remove('streaming'));
// Clean up any thinking spinners
document.querySelectorAll('.agent-thinking-dots').forEach(el => {
if (el._spinner) el._spinner.destroy();
el.remove();
});
// No text accumulated — remove the empty holder with spinner
if (currentHolder && !currentAccumulated) {
if (currentSpinner) { currentSpinner.destroy(); currentSpinner = null; }
// Empty cancel — keep the assistant bubble around with a "Cancelled
// by user" indicator and persist a placeholder server-side so the
// turn survives a refresh instead of vanishing without a trace.
_renderCancelledBubble(currentHolder);
currentHolder = null;
updateSubmitButton('idle', submitBtn);
const messageInput = uiModule.el('message');
if (messageInput) messageInput.disabled = false;
currentAccumulated = '';
return;
}
// Render whatever was accumulated so far
if (currentHolder && currentAccumulated) {
// Store accumulated in a closure variable before it gets cleared
const stoppedContent = currentAccumulated;
// Store raw content in dataset for consistency with other messages
currentHolder.dataset.raw = stoppedContent;
currentHolder.querySelector('.body').innerHTML = markdownModule.processWithThinking(
markdownModule.squashOutsideCode(stoppedContent)
);
// Highlight code blocks
if (window.hljs) {
currentHolder.querySelectorAll('pre code').forEach((block) => {
window.hljs.highlightElement(block);
});
}
// Add the stopped indicator with continue button
const stoppedIndicator = document.createElement('div');
stoppedIndicator.className = 'stopped-indicator';
const stoppedLabel = document.createElement('span');
stoppedLabel.textContent = '[Message interrupted]';
stoppedIndicator.appendChild(stoppedLabel);
const continueBtn = document.createElement('button');
continueBtn.className = 'continue-btn';
continueBtn.title = 'Continue';
continueBtn.textContent = '\u25B8';
const _stoppedHolder = currentHolder; // capture before it gets cleared
continueBtn.addEventListener('click', () => {
stoppedIndicator.remove();
_hideUserBubble = true;
_pendingContinue = _stoppedHolder;
const cutoff = stoppedContent;
const msgInput = uiModule.el('message');
if (msgInput) {
msgInput.value = 'Your previous response was interrupted. It ended with:\n\n' + cutoff.slice(-500) + '\n\nDo NOT repeat what you already said. Continue exactly from where you were cut off.';
const sb = document.querySelector('.send-btn');
if (sb) sb.click();
}
});
stoppedIndicator.appendChild(continueBtn);
currentHolder.querySelector('.body').appendChild(stoppedIndicator);
// Tell server to mark this message as stopped
const _sid = sessionModule.getCurrentSessionId();
if (_sid) fetch(`${API_BASE}/api/session/${_sid}/mark-stopped`, { method: 'POST' }).catch(e => console.warn('mark-stopped failed:', e));
// Add footer with copy/regen if not already present
if (!currentHolder.querySelector('.msg-footer')) {
currentHolder.dataset.raw = stoppedContent;
currentHolder.appendChild(createMsgFooter(currentHolder));
}
uiModule.scrollHistory();
}
// Reset button state
updateSubmitButton('idle', submitBtn);
// Re-enable message input
const messageInput = uiModule.el('message');
if (messageInput) messageInput.disabled = false;
// Clear tracking variables
currentAccumulated = '';
currentHolder = null;
return;
}
// --- Send-path entry: block re-clicks between submit and stream start ---
if (_sendInFlight) return;
_sendInFlight = true;
// Instant visual feedback so the user sees their click was accepted
// even before the streaming button state kicks in below.
const _earlyMessageInput = uiModule.el('message');
if (_earlyMessageInput) _earlyMessageInput.disabled = true;
if (submitBtn) submitBtn.classList.add('send-pending');
const _releaseSendFlag = () => {
_sendInFlight = false;
if (_earlyMessageInput) _earlyMessageInput.disabled = false;
if (submitBtn) submitBtn.classList.remove('send-pending');
};
// --- Setup mode: intercept next message (but let slash commands through) ---
{
const el = uiModule.el;
const rawMsg = (el('message').value || '').trim();
const currentSetupMode = slashCommands.getSetupMode();
if (currentSetupMode && rawMsg && !isCommand(rawMsg)) {
const mode = currentSetupMode;
slashCommands.clearSetupMode(mode === 'endpoint-provider' || mode === 'endpoint-key-for-provider');
el('message').value = '';
if (window._syncModelPickerAutohide) window._syncModelPickerAutohide();
if (uiModule.autoResize) uiModule.autoResize(el('message'));
if (mode === true || mode === 'endpoint') {
handleSetupInput(rawMsg);
} else {
handleSetupWizard(mode, rawMsg);
}
_releaseSendFlag();
return;
}
if (currentSetupMode && rawMsg && isCommand(rawMsg)) {
slashCommands.clearSetupMode(); // Clear setup mode, fall through to slash handler
}
}
const el = uiModule.el;
const msg = el('message').value;
// Allow empty text when a regen carries over the original message's
// attachment ids — a photo-only message still has something to send.
if (!msg.trim() && !fileHandlerModule.getPendingCount() && !(_pendingRegenAttachments && _pendingRegenAttachments.length)) { _releaseSendFlag(); return; }
// --- Slash commands: execute directly without AI (no session needed) ---
if (isCommand(msg.trim())) {
const handled = await handleSlashCommand(msg.trim());
if (handled) {
el('message').value = '';
if (window._syncModelPickerAutohide) window._syncModelPickerAutohide();
if (uiModule.autoResize) uiModule.autoResize(el('message'));
_releaseSendFlag();
return;
}
}
// Materialize pending session (deferred from model click) on first message
if (sessionModule.hasPendingChat && sessionModule.hasPendingChat()) {
const ok = await sessionModule.materializePendingSession();
if (!ok || !sessionModule.getCurrentSessionId()) { _releaseSendFlag(); return; }
}
if (!sessionModule.getCurrentSessionId()) {
// Auto-create a session using default chat config. Always fetch fresh
// so that a recent Settings change takes effect without a page reload.
try {
let dc = null;
try {
const dcRes = await fetch('/api/default-chat');
dc = await dcRes.json();
if (dc && dc.endpoint_url && dc.model) {
try { window.__odysseusDefaultChat = dc; } catch (_) {}
}
} catch (_) {
dc = (typeof window !== 'undefined' && window.__odysseusDefaultChat) || null;
}
if (dc.endpoint_url && dc.model) {
await sessionModule.createDirectChat(dc.endpoint_url, dc.model, dc.endpoint_id);
const ok = await sessionModule.materializePendingSession();
if (!ok || !sessionModule.getCurrentSessionId()) { _releaseSendFlag(); return; }
} else {
addMessage('assistant',
'No chat session active. You can:\n\n' +
'- Open the model picker in the chat box and pick a model\n' +
'- Use the `+` button in the model picker to add a model endpoint\n' +
'- Use `/help` to see all available commands');
_releaseSendFlag();
return;
}
} catch (e) {
addMessage('assistant',
'No chat session active. You can:\n\n' +
'- Open the model picker in the chat box and pick a model\n' +
'- Use the `+` button in the model picker to add a model endpoint\n' +
'- Use `/help` to see all available commands');
_releaseSendFlag();
return;
}
}
// --- API key guard: warn if message looks like an API key ---
if (API_KEY_RE.test(msg.trim())) {
if (!await window.styledConfirm('This looks like an API key. Sending it to the AI could expose it.\n\nDid you mean to use /setup instead?', { confirmText: 'Send anyway', danger: true })) {
_releaseSendFlag();
return;
}
}
const messageInput = el('message');
const originalBtnText = submitBtn ? submitBtn.innerHTML : '';
// Re-enable the textarea now that we've handed off to the stream: the
// user wants to compose the next message while the AI is still talking.
// The `isStreaming` flag is the re-click guard for the send button.
if (messageInput) messageInput.disabled = false;
updateSubmitButton('streaming', submitBtn);
if (submitBtn) submitBtn.classList.remove('send-pending');
_sendInFlight = false;
// Capture session ID for background stream detection
const streamSessionId = sessionModule.getCurrentSessionId();
_streamSessionId = streamSessionId;
const streamQuery = msg;
_lastReaderActivity = Date.now();
// Acquire Web Lock to hint browser not to discard this tab while streaming
if (navigator.locks) {
navigator.locks.request('odysseus-stream-' + streamSessionId, { mode: 'exclusive', ifAvailable: true }, lock => {
if (!lock) return; // Another stream already holds a lock — fine
return new Promise(resolve => { _webLockRelease = resolve; });
}).catch(e => console.warn('web lock acquire failed:', e)); // Ignore lock errors — best-effort
}
// Declare accumulated outside try block so it's accessible in catch
let accumulated = '';
let holder = null;
let finalMeta = null;
let finalModelName = null;
let spinner = null;
let timedOut = false;
let processingProbeTimer = null;
let processingProbeAbort = null;
let _renderStream = () => {};
let _cancelThinkingTimer = () => {};
let _removeThinkingSpinner = () => {};
const clearProcessingProbe = () => {
if (processingProbeTimer) {
clearTimeout(processingProbeTimer);
processingProbeTimer = null;
}
if (processingProbeAbort) {
try { processingProbeAbort.abort(); } catch (_) {}
processingProbeAbort = null;
}
};
// Reset tracking variables at start
currentAccumulated = '';
currentHolder = null;
try {
// Re-enable auto-scroll when user sends a message
uiModule.setAutoScroll(true);
uiModule.scrollHistoryInstant();
// Clear completed dot now that user is interacting
if (sessionModule.clearStreamComplete) sessionModule.clearStreamComplete(sessionModule.getCurrentSessionId());
// Check for document selection context before consuming display override
const docSel = documentModule && documentModule.getSelectionContext();
if (docSel) {
const sels = Array.isArray(docSel) ? docSel : [docSel];
const lineRefs = sels.map(s =>
s.startLine === s.endLine ? `L${s.startLine}` : `L${s.startLine}-${s.endLine}`
);
_displayOverride = `[Doc edit: ${lineRefs.join(', ')}] ${msg}`;
}
const userDisplay = _displayOverride || msg;
_displayOverride = null;
const skipBubble = _hideUserBubble;
_hideUserBubble = false;
// Auto-recovery counter: carries across a turn's auto-continues, but resets
// when the user genuinely sends a new message (so each task gets a fresh cap).
// A real user turn (visible bubble) ALWAYS resets the budget — even if a
// prior auto-continue's deferred click never cleared the pending flag — so a
// stuck flag can't silently eat the next turn's recovery budget.
if (!skipBubble) { _autoNudges = 0; _autoContinuePending = false; }
else if (_autoContinuePending) { _autoContinuePending = false; }
const _pendingAttachInfo = fileHandlerModule.getPendingCount() ? fileHandlerModule.getPendingInfo() : null;
// Pre-read importable file contents before upload clears pending files
const IMPORTABLE_EXT = /\.(txt|py|js|ts|html|htm|css|md|json|csv|yml|yaml|sh|sql|rs|go|java|c|cpp|h|rb|php|xml|jsx|tsx|log|toml|ini|conf|env|vue|svelte|scss|sass|less)$/i;
const _importableFiles = [];
if (_pendingAttachInfo && documentModule) {
const rawFiles = fileHandlerModule.getPendingRaw ? fileHandlerModule.getPendingRaw() : [];
for (let i = 0; i < _pendingAttachInfo.length; i++) {
const att = _pendingAttachInfo[i];
if (IMPORTABLE_EXT.test(att.name) && rawFiles[i]) {
_importableFiles.push({ info: att, file: rawFiles[i] });
}
}
}
let _userMsgEl = null;
if (!skipBubble) {
_userMsgEl = addMessage('user', userDisplay, null, _pendingAttachInfo ? { attachments: _pendingAttachInfo } : null);
}
messageInput.value = '';
messageInput.style.height = '';
messageInput.dispatchEvent(new Event('input'));
// Mobile: dismiss the on-screen keyboard after sending. iOS in
// particular ignores a bare blur() in some cases (or some other
// listener refocuses straight after), so we temporarily mark the
// input readonly which forces the keyboard to retract, then blur,
// then drop the readonly attribute after the keyboard is gone so
// typing still works for the next message.
if (window.innerWidth <= 768) {
try {
messageInput.setAttribute('readonly', 'readonly');
messageInput.blur();
const _dropReadonly = () => { try { messageInput.removeAttribute('readonly'); } catch {} };
setTimeout(() => {
// If the blur stuck, the input is no longer the active element —
// safe to drop readonly now so the next message can be typed.
// If it did NOT stick (some mobile browsers keep the textarea
// focused after a programmatic blur), removing readonly here would
// re-summon the keyboard mid-stream — the "bounce up" that then
// lingers until the end-of-stream blur. In that case keep readonly
// on (keyboard stays down) and drop it the moment the user taps to
// type again, so typing still works without the bounce.
if (document.activeElement === messageInput) {
messageInput.addEventListener('pointerdown', _dropReadonly, { once: true });
messageInput.addEventListener('focus', _dropReadonly, { once: true });
} else {
_dropReadonly();
}
}, 120);
} catch {}
}
let ids = [];
try {
ids = await fileHandlerModule.uploadPending();
} catch(e) {
console.error('upload failed', e);
}
// Carry over the original message's file-ids on a regenerate so the new
// send still references the same photos / docs (and picks up the user's
// edited OCR text via the server-side .vision cache). Always CONSUME the
// slot — even when empty / errored — so the regen ids can't bleed into
// an unrelated next message if uploadPending() above had thrown.
if (_pendingRegenAttachments && _pendingRegenAttachments.length) {
ids = ids.concat(_pendingRegenAttachments);
}
_pendingRegenAttachments = null;
// The optimistic user bubble was rendered before the upload assigned ids,
// so image previews couldn't show (the renderer needs att.id). Now that
// the upload resolved, stamp the ids — plus width/height for images so
// the skeleton can size itself to the photo's aspect ratio — and
// re-render so the thumbnail appears live, no refresh needed.
if (_userMsgEl && _pendingAttachInfo && ids.length) {
const _meta = fileHandlerModule.getLastUploadedMeta?.() || [];
for (let i = 0; i < _pendingAttachInfo.length && i < ids.length; i++) {
_pendingAttachInfo[i].id = ids[i];
const _m = _meta[i];
if (_m) {
if (_m.width) _pendingAttachInfo[i].width = _m.width;
if (_m.height) _pendingAttachInfo[i].height = _m.height;
}
}
chatRenderer.updateMessageAttachments(_userMsgEl, _pendingAttachInfo);
}
// Offer to import text files to document library
if (_importableFiles.length > 0) {
const existing = document.getElementById('import-prompt-banner');
if (existing) existing.remove();
const banner = document.createElement('div');
banner.id = 'import-prompt-banner';
banner.className = 'import-prompt-banner';
const label = _importableFiles.length === 1
? `Import "${_importableFiles[0].info.name}" to document library?`
: `Import ${_importableFiles.length} files to document library?`;
const textEl = document.createElement('span');
textEl.textContent = label;
banner.appendChild(textEl);
const importBtn = document.createElement('button');
importBtn.textContent = 'Import';
importBtn.addEventListener('click', async () => {
importBtn.disabled = true;
importBtn.textContent = 'Importing…';
const EXT_LANG = {'.py':'python','.js':'javascript','.ts':'typescript','.html':'html','.css':'css','.md':'markdown','.json':'json','.yml':'yaml','.yaml':'yaml','.sh':'bash','.sql':'sql','.rs':'rust','.go':'go','.java':'java','.c':'c','.cpp':'cpp','.rb':'ruby','.php':'php','.xml':'xml','.jsx':'javascript','.tsx':'typescript'};
let imported = 0;
for (const { info, file } of _importableFiles) {
try {
const content = await file.text();
const dotIdx = info.name.lastIndexOf('.');
const title = dotIdx > 0 ? info.name.slice(0, dotIdx) : info.name;
const ext = dotIdx >= 0 ? info.name.slice(dotIdx).toLowerCase() : '';
await fetch(`${API_BASE}/api/document`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ title, language: EXT_LANG[ext] || '', content }),
});
imported++;
} catch (e) { console.error('Import failed:', info.name, e); }
}
banner.textContent = `Imported ${imported} file${imported !== 1 ? 's' : ''}`;
setTimeout(() => banner.remove(), 2000);
});
banner.appendChild(importBtn);
const dismissBtn = document.createElement('button');
dismissBtn.textContent = '\u00d7';
dismissBtn.className = 'import-prompt-dismiss';
dismissBtn.addEventListener('click', () => banner.remove());
banner.appendChild(dismissBtn);
const chatBar = document.getElementById('chat-bar');
if (chatBar) chatBar.parentNode.insertBefore(banner, chatBar);
// Auto-dismiss after 15 seconds
setTimeout(() => { if (banner.parentNode) banner.remove(); }, 15000);
}
// Auto-save document editor content before sending so the AI sees latest text
if (documentModule && documentModule.isPanelOpen() && documentModule.getCurrentDocId()) {
try { await documentModule.saveDocument(); } catch(e) { console.warn('doc auto-save failed', e); }
}
// Inject document selection context if present
let finalMsg = msg;
if (docSel) {
const sels = Array.isArray(docSel) ? docSel : [docSel];
if (sels.length === 1) {
const s = sels[0];
const lineRef = s.startLine === s.endLine ? `line ${s.startLine}` : `lines ${s.startLine}-${s.endLine}`;
finalMsg = `In the document, edit this specific text (${lineRef}):\n\`\`\`\n${s.text}\n\`\`\`\n\nInstruction: ${msg}`;
} else {
const parts = sels.map((s, i) => {
const lineRef = s.startLine === s.endLine ? `line ${s.startLine}` : `lines ${s.startLine}-${s.endLine}`;
return `Selection ${i + 1} (${lineRef}):\n\`\`\`\n${s.text}\n\`\`\``;
});
finalMsg = `In the document, edit these specific sections:\n\n${parts.join('\n\n')}\n\nInstruction: ${msg}`;
}
}
// Apply inject prefix/suffix
const _inject = presetsModule.getInject ? presetsModule.getInject() : { prefix: '', suffix: '' };
let _finalMsgWithInject = finalMsg;
if (_inject.prefix) _finalMsgWithInject = _inject.prefix + ' ' + _finalMsgWithInject;
if (_inject.suffix) _finalMsgWithInject = _finalMsgWithInject + ' ' + _inject.suffix;
const fd = new FormData();
fd.append('message', _finalMsgWithInject);
fd.append('session', streamSessionId);
if (ids.length) fd.append('attachments', JSON.stringify(ids));
// Auto-save & send active doc ID so the backend sees latest content
if (documentModule && documentModule.isPanelOpen() && documentModule.getCurrentDocId()) {
try { await documentModule.saveDocument({ silent: true }); } catch (_e) { /* best-effort */ }
fd.append('active_doc_id', documentModule.getCurrentDocId());
}
// Web toggle: pre-search in Chat mode, tool permission in Agent mode
const toggleState = Storage.loadToggleState();
let isAgentMode = (toggleState.mode || 'chat') === 'agent';
// Auto-escalate to agent mode when a document is open — the user expects
// the AI to see the document and have tools to edit it
if (!isAgentMode && documentModule && documentModule.isPanelOpen() && documentModule.getCurrentDocId()) {
isAgentMode = true;
}
fd.append('mode', isAgentMode ? 'agent' : 'chat');
if (el('web-toggle').checked) {
if (isAgentMode) {
fd.append('allow_web_search', 'true');
} else {
fd.append('use_web', 'true');
}
}
if (el('research-toggle').checked) {
fd.append('use_research', 'true');
// Research always runs in chat mode — override agent if set
fd.set('mode', 'chat');
}
if (el('bash-toggle').checked) {
fd.append('allow_bash', 'true');
}
const ragChk = el('rag-toggle');
if (ragChk && !ragChk.checked) {
fd.append('use_rag', 'false');
}
const incognitoChk = el('incognito-toggle');
if (incognitoChk && incognitoChk.checked) {
fd.append('incognito', 'true');
}
if (presetsModule.getSelectedPreset()) {
fd.append('preset_id', presetsModule.getSelectedPreset());
}
const abortCtrl = new AbortController();
abortCtrl._reason = '';
currentAbort = abortCtrl;
const _tState = Storage.loadToggleState();
const _isAgent = (_tState.mode || 'chat') === 'agent';
// Timeout: 6 min for research and agent mode, 3 min otherwise
const timeoutMs = el('research-toggle').checked || _isAgent ? RESEARCH_TIMEOUT_MS : DEFAULT_TIMEOUT_MS;
const timeoutId = setTimeout(() => {
if (!abortCtrl.signal.aborted) {
timedOut = true;
abortCtrl._reason = 'timeout';
abortCtrl.abort();
}
}, timeoutMs);
const box = el('chat-history');
holder = document.createElement('div');
holder.className = 'msg msg-ai streaming';
// Track holder globally so stop button can access it
currentHolder = holder;
holder._researchQuery = msg; // Store query for notification text
const modelName = sessionModule.getCurrentModel() || null;
let loadingText = 'Initializing...';
if (el('web-toggle').checked && !_isAgent) {
const _searchLabel = searchModule ? searchModule.getProviderLabel() : 'web';
loadingText = `Searching via ${_searchLabel}...
Query: "${msg.substring(0, 50)}${msg.length > 50 ? '...' : ''}"
Fetching top results...`;
} else if (el('research-toggle').checked) {
loadingText = 'Deep research mode active...';
} else {
loadingText = 'Processing request...';
}
var roleLabel = _shortModel(modelName);
var _charNameInit = presetsModule.getCharacterName ? presetsModule.getCharacterName() : '';
if (_charNameInit) roleLabel = _charNameInit;
const roleTs = new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
holder.innerHTML = `
${esc(cmd)}` : '';
node.innerHTML = `${esc(json.output)}${esc(cmd)}` : '';
// Preserve the user's .open choice across the innerHTML
// rewrite \u2014 otherwise expanding a running tool collapses
// it as soon as the result lands, forcing the user to
// click again. Click handling is delegated (see init at
// bottom of file) so no per-node listener needed.
const _wasOpen = currentToolBubble.classList.contains('open');
currentToolBubble.className = 'agent-thread-node' + (ok ? '' : ' error') + (_wasOpen ? ' open' : '');
currentToolBubble.innerHTML = `${esc(teacherName)}${why}`;
chatBox.appendChild(banner);
// Reset round bubble state so the teacher's first text starts a new bubble
roundHolder = null;
roundText = '';
roundFinalized = false;
currentToolBubble = null;
uiModule.scrollHistory();
} else if (json.type === 'skill_saved') {
if (_isBg) continue;
const chatBox = document.getElementById('chat-history');
const note = document.createElement('div');
note.className = 'skill-saved-note';
note.style.cssText = 'margin:6px 0;padding:6px 10px;border-left:3px solid #4a8a4a;background:rgba(74,138,74,0.07);font-size:12px;color:var(--fg);border-radius:4px;';
note.innerHTML = `Skill learned: ${esc(json.name || '')}${json.category ? ` [${esc(json.category)}]` : ''}`;
chatBox.appendChild(note);
uiModule.scrollHistory();
} else if (json.type === 'escalation_failed' || json.type === 'skill_save_failed') {
if (_isBg) continue;
const chatBox = document.getElementById('chat-history');
const note = document.createElement('div');
note.className = 'escalation-failed-note';
note.style.cssText = 'margin:6px 0;padding:6px 10px;border-left:3px solid #8a4a4a;background:rgba(138,74,74,0.07);font-size:12px;color:var(--fg);border-radius:4px;';
const label = json.type === 'escalation_failed' ? 'Teacher could not solve it' : 'Skill not saved';
note.innerHTML = `${label}: ${esc(json.reason || '')}`;
chatBox.appendChild(note);
uiModule.scrollHistory();
} else if (json.error) {
// --- Backend error (timeout, connection issue, etc.) ---
console.error('Stream error from backend:', json.error);
if (_isBg) continue;
if (spinner && spinner.element) spinner.destroy();
const errDiv = document.createElement('div');
errDiv.style.cssText = 'color: var(--color-error); font-style: italic; padding: 4px 0;';
errDiv.textContent = `[Error: ${json.error}]`;
roundHolder.querySelector('.body').appendChild(errDiv);
uiModule.scrollHistory();
}
} catch (e) {
console.error('Error parsing SSE data:', e);
}
}
}
}
if (!_streamSawDone) {
throw new Error('Stream closed before completion');
}
_renderStream();
_cancelThinkingTimer();
_removeThinkingSpinner();
// Stop any thread pulse animations
document.querySelectorAll('.agent-thread.streaming').forEach(t => t.classList.remove('streaming'));
// --- Final render (skip if stream was ever backgrounded or currently in background) ---
// Remove streaming class from all round bubbles
holder.classList.remove('streaming');
if (roundHolder && roundHolder !== holder) roundHolder.classList.remove('streaming');
const _isBgFinal = (sessionModule.getCurrentSessionId() !== streamSessionId) || _backgroundStreams.has(streamSessionId);
if (!_isBgFinal) {
finalMeta = sessionModule.getSessions().find(s => s.id === sessionModule.getCurrentSessionId());
finalModelName = _shortModel(metrics?.model || finalMeta?.model);
// Preserve suffix (e.g. "Research") if set by model_info event
if (holder._roleSuffix) finalModelName += ' (' + holder._roleSuffix + ')';
// Prepend character name if set
var _charNameFinal = presetsModule.getCharacterName ? presetsModule.getCharacterName() : '';
if (_charNameFinal) finalModelName = _charNameFinal;
const roleEl = holder.querySelector('.role');
if (roleEl) {
const tsSpan = roleEl.querySelector('.role-timestamp');
roleEl.textContent = finalModelName + ' ';
_applyModelColor(roleEl, metrics?.model || finalMeta?.model);
if (tsSpan) roleEl.appendChild(tsSpan);
}
holder.dataset.raw = accumulated;
// Anti-stall: a turn that ran tools but ended with essentially no
// final prose usually means the model stopped mid-task (the case
// where you had to type "did you finish?"). Offer a one-click
// Continue that resumes exactly where it left off — reuses the same
// resume mechanism as the user-stop "[Message interrupted]" button.
try {
const _usedTools = holder.querySelector('.agent-thread-node');
const _proseLen = (accumulated || '').replace(/<[^>]*>/g, '').trim().length;
if (_usedTools && _proseLen < 24 && !holder.querySelector('.agent-continue-btn')) {
const _stall = document.createElement('div');
_stall.className = 'stopped-indicator';
const _lbl = document.createElement('span');
_lbl.style.cssText = 'font-style:italic;opacity:0.7;';
_lbl.textContent = 'Paused mid-task';
_stall.appendChild(_lbl);
const _cont = document.createElement('button');
_cont.className = 'continue-btn agent-continue-btn';
_cont.title = 'Continue — pick up where it left off';
_cont.textContent = '▸';
_cont.addEventListener('click', () => {
_stall.remove();
const mi = uiModule.el('message');
if (mi) {
mi.value = 'Continue — you stopped before finishing. Pick up exactly where you left off and complete the task.';
const sb = document.querySelector('.send-btn');
if (sb) sb.click();
}
});
_stall.appendChild(_cont);
(holder.querySelector('.body') || holder).appendChild(_stall);
}
} catch (_) {}
// Clear streaming minHeight lock
const _streamContent = roundHolder.querySelector('.stream-content');
if (_streamContent) _streamContent.style.minHeight = '';
// Finalize the last round's bubble — flatten stream-content wrapper for clean DOM
const finalDisplay = stripToolBlocks(roundText);
if (finalDisplay.trim()) {
var _body4 = roundHolder.querySelector('.body');
// Preserve sources expanded state before final render
var _wasExpanded = _sourcesExpanded || !!(_body4 && _body4.querySelector('.sources-content.expanded'));
// If thinking was collapsed in-place during streaming, preserve it
var _liveReplyEl = _body4 && _body4.querySelector('.live-reply-content');
var _extracted = _liveReplyEl ? markdownModule.extractThinkingBlocks(finalDisplay) : null;
var _finalReply = '';
if (_liveReplyEl) {
// Try standard extraction first (for native added anywhere in the app (chat stream,
// chat re-renders, document library chat previews, slash commands,
// research previews, etc.) gets tagged without each call site needing
// to remember.
(function _initCompactPreObserver() {
if (window._cmpPreObserverWired) return;
window._cmpPreObserverWired = true;
_scanCompactPres(document.body);
const obs = new MutationObserver((muts) => {
for (const m of muts) {
for (const n of m.addedNodes) {
if (n.nodeType !== 1) continue;
if (n.tagName === 'PRE') _markCompactPre(n);
if (n.querySelectorAll) _scanCompactPres(n);
}
}
});
obs.observe(document.body, { childList: true, subtree: true });
})();
/**
* Initialize event listeners
*/
export function initListeners() {
// Global event delegation for copy-code buttons
document.addEventListener('click', (e) => {
const btn = e.target.closest('.copy-code');
if (!btn) return;
e.stopPropagation();
const code = btn.getAttribute('data-code');
if (code && uiModule) {
uiModule.copyToClipboard(code);
// Visual feedback: swap the icon to a checkmark (regular size)
// and add .copied which the CSS uses to flash green + pulse.
// For slim/.pre-compact buttons the label text comes from a
// CSS ::before — swap it via data-state so we don't break the
// text-button layout.
const origHTML = btn.innerHTML;
const isCompact = !!btn.closest('pre.pre-compact');
if (!isCompact) {
btn.innerHTML = '';
}
btn.classList.add('copied');
btn.dataset.state = 'copied';
setTimeout(() => {
if (!isCompact) btn.innerHTML = origHTML;
btn.classList.remove('copied');
delete btn.dataset.state;
}, 1500);
}
});
// Run code button delegation
document.addEventListener('click', (e) => {
const btn = e.target.closest('.run-code');
if (!btn) return;
e.stopPropagation();
if (codeRunnerModule) codeRunnerModule.run(btn);
});
// Edit code button delegation — toggle contentEditable on the code element
document.addEventListener('click', (e) => {
const btn = e.target.closest('.edit-code');
if (!btn) return;
e.stopPropagation();
const pre = btn.closest('pre');
if (!pre) return;
const codeEl = pre.querySelector('code');
if (!codeEl) return;
const isEditing = codeEl.contentEditable !== 'false' && codeEl.contentEditable !== 'inherit';
if (isEditing) {
// Save: exit edit mode, update data-code on copy/run buttons
codeEl.contentEditable = 'false';
codeEl.classList.remove('editing');
pre.classList.remove('editing');
const newCode = codeEl.textContent;
const copyBtn = pre.querySelector('.copy-code');
if (copyBtn) copyBtn.setAttribute('data-code', newCode);
const runBtn = pre.querySelector('.run-code');
if (runBtn) runBtn.setAttribute('data-code', newCode);
// Swap icon back to pencil
btn.innerHTML = '';
btn.title = 'Edit';
btn.classList.remove('active');
} else {
// Enter edit mode. Firefox (especially on mobile) historically lacks
// contentEditable="plaintext-only" — setting it there leaves the block
// non-editable, so the tap "just gets a checkmark" with no way to type.
// Fall back to "true" when plaintext-only didn't take.
try { codeEl.contentEditable = 'plaintext-only'; } catch (_) { /* unsupported value */ }
if (codeEl.contentEditable !== 'plaintext-only') codeEl.contentEditable = 'true';
codeEl.classList.add('editing');
pre.classList.add('editing');
// preventScroll keeps the page from jumping to the codeblock when
// focusing the editable on mobile — the browser would otherwise
// scroll it into view above the keyboard, which reads as "auto-
// scroll triggered by clicking Edit".
try { codeEl.focus({ preventScroll: true }); } catch (_) { codeEl.focus(); }
// Swap icon to checkmark
btn.innerHTML = '';
btn.title = 'Done editing';
btn.classList.add('active');
}
});
// Tapping a code block body (not its buttons) toggles the overlay
// copy/edit/run buttons, which otherwise cover the text on mobile.
document.addEventListener('click', (e) => {
if (e.target.closest('.copy-code, .edit-code, .run-code')) return;
const pre = e.target.closest('pre');
if (!pre || !pre.querySelector('.copy-code')) return;
// Don't hide while editing — the buttons (incl. the Done checkmark) matter.
if (pre.classList.contains('editing')) return;
pre.classList.toggle('buttons-hidden');
});
// Position copy/run buttons top or bottom based on viewport position
// — DESKTOP ONLY. On mobile this was constantly retriggering on tap
// (synthetic mouseenter) and made the buttons jump, so the user's
// finger landed on the moved target. Keep them pinned at the top on
// touch — no auto-repositioning.
document.addEventListener('mouseenter', (e) => {
if (window.matchMedia('(max-width: 768px)').matches) return;
const pre = e.target.closest ? e.target.closest('pre') : null;
if (!pre || pre.dataset.btnPosComputed) return;
const rect = pre.getBoundingClientRect();
const threshold = window.innerHeight * 0.35;
const isBottom = rect.top < threshold;
const copyBtn = pre.querySelector('.copy-code');
if (copyBtn) copyBtn.classList.toggle('bottom', isBottom);
const editBtn = pre.querySelector('.edit-code');
if (editBtn) editBtn.classList.toggle('bottom', isBottom);
const runBtn = pre.querySelector('.run-code');
if (runBtn) runBtn.classList.toggle('bottom', isBottom);
pre.dataset.btnPosComputed = '1';
}, true);
// Tab suspension recovery: when user tabs back in, check if stream froze
document.addEventListener('visibilitychange', () => {
if (document.visibilityState !== 'visible') return;
if (!isStreaming) return;
// Stream claims to be running — check if reader is actually alive
const staleSince = Date.now() - _lastReaderActivity;
if (staleSince < 20000) return; // Active recently, probably fine
// Reader hasn't produced data in 5+ seconds after tab resume.
// Give it a short grace period then recover.
console.warn('[tab-recovery] Stream appears frozen (no activity for ' + Math.round(staleSince/1000) + 's). Recovering...');
setTimeout(() => {
// Re-check — maybe the reader woke up during the grace period
if (!isStreaming) return;
const stillStale = Date.now() - _lastReaderActivity;
if (stillStale < 5000) return; // Came back to life
console.warn('[tab-recovery] Stream confirmed dead. Aborting and reloading session.');
// Abort the frozen stream, but preserve the visible bubble.
if (currentAbort) {
currentAbort._reason = 'recovery';
currentAbort.abort();
}
isStreaming = false;
// Release Web Lock
if (_webLockRelease) {
_webLockRelease();
_webLockRelease = null;
}
// Reset UI state
var _submitBtn = document.getElementById('submit');
updateSubmitButton('idle', _submitBtn);
var _msgInput = document.getElementById('message');
if (_msgInput) _msgInput.disabled = false;
}, 2000); // 2 second grace period
});
// On mobile, fade out welcome text when keyboard opens to prevent overlap
if (window.innerWidth <= 768) {
const msgInput = document.getElementById('message');
if (msgInput) {
msgInput.addEventListener('focus', () => {
const ws = document.getElementById('welcome-screen');
if (ws && !ws.classList.contains('hidden')) {
ws.classList.add('kb-hidden');
}
});
msgInput.addEventListener('blur', () => {
const ws = document.getElementById('welcome-screen');
if (ws && !ws.classList.contains('hidden')) {
// Delay re-show so tapping within chatbox doesn't flash
setTimeout(() => {
if (document.activeElement !== msgInput) {
ws.classList.remove('kb-hidden');
}
}, 200);
}
});
}
// Smooth viewport resize when keyboard opens/closes
if (window.visualViewport) {
window.visualViewport.addEventListener('resize', () => {
document.documentElement.style.setProperty('--vh', window.visualViewport.height + 'px');
});
document.documentElement.style.setProperty('--vh', window.visualViewport.height + 'px');
}
}
// If the browser discarded and restored this tab, reload the current session
// so the user sees the server-saved partial response instead of a blank page
if (document.wasDiscarded) {
console.warn('[tab-recovery] Tab was discarded by browser — reloading session');
setTimeout(() => {
var _sid = sessionModule && sessionModule.getCurrentSessionId();
if (_sid) sessionModule.selectSession(_sid);
}, 500);
}
}
/**
* Regenerate response: truncate history to the user message before this AI message,
* then re-submit that user message.
*/
/**
* Edit a user message: show an input, truncate to before it, resubmit the edited text.
*/
export async function editUserMessage(userMsgElement) {
const box = document.getElementById('chat-history');
const allMsgs = Array.from(box.querySelectorAll('.msg'));
const msgIndex = allMsgs.indexOf(userMsgElement);
if (msgIndex < 0) return;
const bodyEl = userMsgElement.querySelector('.body');
const currentText = bodyEl ? bodyEl.textContent.trim().replace(/\s*\[\d+ attachment\(s\)\]$/, '') : '';
// Replace body with an editable textarea
const editor = document.createElement('textarea');
editor.className = 'edit-textarea';
editor.value = currentText;
editor.rows = Math.max(2, currentText.split('\n').length);
const btnRow = document.createElement('div');
btnRow.style.cssText = 'display:flex; gap:6px; margin-top:4px;';
const saveBtn = document.createElement('button');
saveBtn.className = 'edit-save-btn';
saveBtn.textContent = 'Send';
const cancelBtn = document.createElement('button');
cancelBtn.className = 'edit-cancel-btn';
cancelBtn.textContent = 'Cancel';
btnRow.appendChild(saveBtn);
btnRow.appendChild(cancelBtn);
const originalHTML = bodyEl.innerHTML;
bodyEl.innerHTML = '';
bodyEl.appendChild(editor);
bodyEl.appendChild(btnRow);
editor.focus();
cancelBtn.addEventListener('click', (e) => {
e.stopPropagation();
bodyEl.innerHTML = originalHTML;
});
saveBtn.addEventListener('click', async (e) => {
e.stopPropagation();
const newText = editor.value.trim();
if (!newText) return;
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) return;
const keepCount = msgIndex;
try {
await fetch(`${API_BASE}/api/session/${sessionId}/truncate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ keep_count: keepCount })
});
// Remove DOM elements from msgIndex onward
for (let i = allMsgs.length - 1; i >= msgIndex; i--) {
allMsgs[i].remove();
}
// Submit the edited text
const messageInput = uiModule.el('message');
messageInput.value = newText;
const submitBtn = document.querySelector('.send-btn');
if (submitBtn) submitBtn.click();
} catch (err) {
console.error('Edit failed:', err);
if (uiModule) uiModule.showError('Edit failed: ' + err.message);
bodyEl.innerHTML = originalHTML;
}
});
// Also submit on Enter (without shift)
editor.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
saveBtn.click();
}
});
}
/**
* Resend a user message — truncates history to that point and resubmits.
*/
export async function resendUserMessage(userMsgElement) {
const box = document.getElementById('chat-history');
const allMsgs = Array.from(box.querySelectorAll('.msg'));
const msgIndex = allMsgs.indexOf(userMsgElement);
if (msgIndex < 0) return;
// Prefer dataset.raw (stripped original user text) over .body.textContent
// — the latter slurps the rendered "View image description" collapsible
// content too, which would then be sent back as the user's question and
// the AI would reply to that gibberish instead of the actual prompt.
const bodyEl = userMsgElement.querySelector('.body');
let text = (userMsgElement.dataset.raw || (bodyEl ? bodyEl.textContent : '') || '').trim();
text = text.replace(/\s*\[\d+ attachment\(s\)\]$/, '');
// Collect file_ids attached to this user message so the resend re-carries
// the photos / docs (and the chat handler picks up the user-edited OCR
// text cached server-side under those file ids).
const _attachEls = userMsgElement.querySelectorAll('[data-file-id]');
let _ids = Array.from(_attachEls).map(el => el.dataset.fileId).filter(Boolean);
if (!_ids.length) {
const _imgs = userMsgElement.querySelectorAll('.attach-image-preview img, .attach-card img');
for (const _im of _imgs) {
const _m = (_im.getAttribute('src') || '').match(/\/api\/upload\/([A-Za-z0-9_\-]+)/);
if (_m && _m[1] && !_ids.includes(_m[1])) _ids.push(_m[1]);
}
}
// Rescue: legacy bubbles may have stored the filename as the message
// content (artifact of earlier broken resends). Don't re-send that as
// the user prompt if we still have the file attached. Loosen the regex
// to cover real-world camera/screenshot names with spaces, parens,
// multi-dots: "Screen Shot 2026-05-28 at 4.05.32 PM.png", "IMG (1).JPG".
if (text && _ids.length && /^[^\n\r]{1,200}\.(png|jpe?g|gif|webp|svg|bmp|heic|heif)$/i.test(text)) {
text = '';
}
// Empty text + no attachments → tell the user instead of silently bailing.
// The common case is a regen during a pre-upload race where the bubble
// never had an `[data-file-id]` to scrape.
if (!text && !_ids.length) {
if (uiModule?.showError) uiModule.showError('Nothing to resend — message has no text and no attachments yet (try again after the upload finishes).');
return;
}
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) return;
// Truncate backend to keep everything before this user message
const keepCount = msgIndex;
try {
await fetch(`${API_BASE}/api/session/${sessionId}/truncate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ keep_count: keepCount })
});
// Drop the AI replies after the user message but KEEP the user bubble
// itself (so its photo stays visible). Then suppress the new user
// bubble that send would otherwise add — same pattern as regenerate.
let sibling = userMsgElement.nextSibling;
while (sibling) {
const next = sibling.nextSibling;
sibling.remove();
sibling = next;
}
_hideUserBubble = true;
_pendingRegenAttachments = _ids;
// Resubmit
const messageInput = uiModule.el('message');
messageInput.value = text;
const submitBtn = document.querySelector('.send-btn');
if (submitBtn) submitBtn.click();
} catch (err) {
console.error('Resend failed:', err);
if (uiModule) uiModule.showError('Resend failed: ' + err.message);
}
}
export async function regenerateFrom(aiMsgElement) {
const box = document.getElementById('chat-history');
const allMsgs = Array.from(box.querySelectorAll('.msg'));
const aiIndex = allMsgs.indexOf(aiMsgElement);
if (aiIndex < 0) return;
// Find the preceding user message
let userIndex = -1;
let userText = '';
let userMsgEl = null;
for (let i = aiIndex - 1; i >= 0; i--) {
if (allMsgs[i].classList.contains('msg-user')) {
userIndex = i;
userMsgEl = allMsgs[i];
// Prefer dataset.raw (set by addMessage with the stripped, original
// user text) over the rendered body's textContent — the latter
// pulls in the "View image description" collapsible content too,
// duplicating the OCR text on regen.
const bodyEl = userMsgEl.querySelector('.body');
userText = (userMsgEl.dataset.raw || (bodyEl ? bodyEl.textContent : '') || '').trim();
userText = userText.replace(/\s*\[\d+ attachment\(s\)\]$/, '');
break;
}
}
if (userIndex < 0) {
if (uiModule) uiModule.showError('Could not find the user message to regenerate');
return;
}
// Collect any file_ids attached to the original user message so the
// regenerated send re-uses them. Without this the AI is regenerated on
// text alone — photos (and the user-edited OCR text cached server-side
// under that file_id) would be silently dropped.
const _attachEls = userMsgEl ? userMsgEl.querySelectorAll('[data-file-id]') : [];
let _regenIds = Array.from(_attachEls).map(el => el.dataset.fileId).filter(Boolean);
// Fallback for bubbles rendered before the data-file-id stamp landed:
// sniff the file id straight out of any `.attach-image-preview img`
// src URLs (matches /api/upload/). Otherwise an older bubble would
// regen with zero attachments and the photo would be lost from the
// resulting message even though the file still exists on disk.
if (!_regenIds.length && userMsgEl) {
const _imgs = userMsgEl.querySelectorAll('.attach-image-preview img, .attach-card img');
for (const _im of _imgs) {
const _m = (_im.getAttribute('src') || '').match(/\/api\/upload\/([A-Za-z0-9_\-]+)/);
if (_m && _m[1] && !_regenIds.includes(_m[1])) _regenIds.push(_m[1]);
}
}
_pendingRegenAttachments = _regenIds;
// Rescue: earlier-version regens (before the dataset.raw fix) stored the
// photo's filename as the user-message content. On a follow-up regen,
// that filename would be sent back as the literal user prompt, so the
// AI thinks the question is "blue_night_preview.jpg" and replies "that's
// an image file". If userText is just a bare image filename and we have
// attachments, drop it so the OCR text (or the image bytes for vision
// models) is what the model actually sees.
if (userText && _pendingRegenAttachments.length &&
/^[^\n\r]{1,200}\.(png|jpe?g|gif|webp|svg|bmp|heic|heif)$/i.test(userText.trim())) {
userText = '';
}
// A photo-only message has empty user text — regen must still proceed,
// because the attachments themselves are the message. Bail only if there
// is no text AND no attachments to send.
if (!userText && !_pendingRegenAttachments.length) {
if (uiModule) uiModule.showError('Nothing to regenerate — the user message has no text and no attachments');
return;
}
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) return;
// Save current response as a variant
const oldRaw = aiMsgElement.dataset.raw || aiMsgElement.querySelector('.body')?.textContent || '';
const oldHtml = aiMsgElement.querySelector('.body')?.innerHTML || '';
let variants = [];
try { variants = JSON.parse(aiMsgElement.dataset.variants || '[]'); } catch(_) {}
if (variants.length === 0) {
// First regen — save the original as variant 0
variants.push({ raw: oldRaw, html: oldHtml, label: 'original' });
}
const keepCount = userIndex;
try {
await fetch(`${API_BASE}/api/session/${sessionId}/truncate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ keep_count: keepCount })
});
for (let i = allMsgs.length - 1; i > aiIndex; i--) {
allMsgs[i].remove();
}
// Remove the AI message from DOM — it will be replaced by the new streaming response
// But first, stash the variants data so we can transfer it to the new element
_pendingVariants = variants;
_pendingVariantLabel = 'regen';
aiMsgElement.remove();
_hideUserBubble = true;
const messageInput = uiModule.el('message');
messageInput.value = userText;
const submitBtn = document.querySelector('.send-btn');
if (submitBtn) submitBtn.click();
} catch (err) {
console.error('Regenerate failed:', err);
if (uiModule) uiModule.showError('Regenerate failed: ' + err.message);
}
}
// Pending variants from a regeneration — transferred to new streaming element
let _pendingVariants = null;
let _pendingVariantLabel = null;
// File-ids carried over from the original user message during a regen, so
// photos / OCR overrides survive into the new send. Consumed once.
let _pendingRegenAttachments = null;
/**
* Called after streaming completes to attach variant navigation if this was a regen.
*/
function _attachVariantNav(msgElement) {
if (!_pendingVariants) return;
const variants = _pendingVariants;
_pendingVariants = null;
// Add the new response as the latest variant
const newRaw = msgElement.dataset.raw || msgElement.querySelector('.body')?.textContent || '';
const newHtml = msgElement.querySelector('.body')?.innerHTML || '';
const varLabel = _pendingVariantLabel || 'regen';
_pendingVariantLabel = null;
variants.push({ raw: newRaw, html: newHtml, label: varLabel });
msgElement.dataset.variants = JSON.stringify(variants);
msgElement.dataset.variantIndex = String(variants.length - 1);
_renderVariantNav(msgElement, variants, variants.length - 1);
// Persist variants to server
const sid = sessionModule.getCurrentSessionId();
if (sid) {
fetch(`${API_BASE}/api/session/${sid}/update-last-meta`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ metadata: { variants: variants, variantIndex: variants.length - 1 } })
}).catch(e => console.warn('update-last-meta (variants) failed:', e));
}
}
const _VARIANT_ICONS = { regen: '\u21BB', shorter: '\u2702', simpler: '?', original: '\u25CB' };
function _variantTagText(label) {
return _VARIANT_ICONS[label] || _VARIANT_ICONS['original'];
}
function _renderVariantNav(msgElement, variants, currentIdx) {
// Remove existing nav if any
const old = msgElement.querySelector('.variant-nav');
if (old) old.remove();
if (variants.length < 2) return;
const nav = document.createElement('span');
nav.className = 'variant-nav';
nav.addEventListener('click', (e) => e.stopPropagation());
// Label showing what this variant is
// Divider
const divider = document.createElement('span');
divider.className = 'variant-divider';
divider.textContent = '|';
nav.appendChild(divider);
// Label
const curVariant = variants[currentIdx];
const tagLabel = document.createElement('span');
tagLabel.className = 'variant-tag' + (curVariant?.label === 'shorter' ? ' variant-tag-scissors' : '');
tagLabel.textContent = _variantTagText(curVariant?.label);
nav.appendChild(tagLabel);
// < button
const prevBtn = document.createElement('button');
prevBtn.className = 'variant-btn';
prevBtn.textContent = '<';
prevBtn.disabled = currentIdx === 0;
prevBtn.addEventListener('click', (e) => { e.stopPropagation(); _switchVariant(msgElement, variants, currentIdx - 1); });
nav.appendChild(prevBtn);
// Clickable number for current index (click left number = go left, right = go right)
const numLeft = document.createElement('button');
numLeft.className = 'variant-num';
numLeft.textContent = String(currentIdx + 1);
numLeft.disabled = currentIdx === 0;
numLeft.addEventListener('click', (e) => { e.stopPropagation(); _switchVariant(msgElement, variants, currentIdx - 1); });
nav.appendChild(numLeft);
const slash = document.createElement('span');
slash.className = 'variant-slash';
slash.textContent = '/';
nav.appendChild(slash);
const numRight = document.createElement('button');
numRight.className = 'variant-num';
numRight.textContent = String(variants.length);
numRight.disabled = currentIdx === variants.length - 1;
numRight.addEventListener('click', (e) => { e.stopPropagation(); _switchVariant(msgElement, variants, currentIdx + 1); });
nav.appendChild(numRight);
// > button
const nextBtn = document.createElement('button');
nextBtn.className = 'variant-btn';
nextBtn.textContent = '>';
nextBtn.disabled = currentIdx === variants.length - 1;
nextBtn.addEventListener('click', (e) => { e.stopPropagation(); _switchVariant(msgElement, variants, currentIdx + 1); });
nav.appendChild(nextBtn);
// Insert into the .role header
const roleEl = msgElement.querySelector('.role');
if (roleEl) {
roleEl.appendChild(nav);
} else {
msgElement.appendChild(nav);
}
}
function _switchVariant(msgElement, variants, newIdx) {
if (newIdx < 0 || newIdx >= variants.length) return;
const v = variants[newIdx];
const body = msgElement.querySelector('.body');
if (body) body.innerHTML = v.html;
msgElement.dataset.raw = v.raw;
msgElement.dataset.variantIndex = String(newIdx);
if (window.hljs) {
msgElement.querySelectorAll('pre code').forEach(block => window.hljs.highlightElement(block));
}
_renderVariantNav(msgElement, variants, newIdx);
// Persist selected variant to server
const sid = sessionModule.getCurrentSessionId();
if (sid) {
fetch(`${API_BASE}/api/session/${sid}/update-last-meta`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ metadata: { variantIndex: newIdx } })
}).catch(e => console.warn('update-last-meta (variantIndex) failed:', e));
}
}
export async function forkFrom(aiMsgElement) {
const box = document.getElementById('chat-history');
const allMsgs = Array.from(box.querySelectorAll('.msg'));
const aiIndex = allMsgs.indexOf(aiMsgElement);
if (aiIndex < 0) return;
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) return;
const keepCount = aiIndex + 1;
try {
const res = await fetch(`${API_BASE}/api/session/${sessionId}/fork`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ keep_count: keepCount }),
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
await sessionModule.loadSessions();
await sessionModule.selectSession(data.id);
if (uiModule) uiModule.showToast(`Forked → ${data.name}`);
} catch (err) {
console.error('Fork failed:', err);
if (uiModule) uiModule.showError('Fork failed: ' + err.message);
}
}
/**
* Check for pending/completed research after page refresh or session switch.
* If research is still running, show a spinner and poll until done.
* If research is done, fetch result and render it.
*/
export async function checkPendingResearch(sessionId) {
if (!sessionId) return;
try {
const res = await fetch(`${API_BASE}/api/research/status/${sessionId}`);
if (!res.ok) return; // 404 = no research for this session
const data = await res.json();
if (data.status === 'done') {
// Fetch and render the completed result
_notifyResearchComplete(sessionId, data.query || '');
if (sessionModule && sessionModule.clearResearching) sessionModule.clearResearching(sessionId);
const resultRes = await fetch(`${API_BASE}/api/research/result/${sessionId}`, { method: 'POST' });
if (resultRes.ok) {
const resultData = await resultRes.json();
if (resultData.result) {
// Skip if history already has a research message for this session
if (document.querySelector(`#chat-history .msg-ai[data-research-session="${sessionId}"]`)) return;
var srcBox = '';
if (resultData.sources && resultData.sources.length > 0) {
srcBox = _buildSourcesBox(resultData.sources, 'research');
}
var findingsBox = chatRenderer.buildFindingsBox(resultData.raw_findings);
var cleanResult = resultData.result;
// Build DOM directly to avoid double-processing through addMessage
chatRenderer.hideWelcomeScreen();
var _box = document.getElementById('chat-history');
if (_box) {
var _wrap = document.createElement('div');
_wrap.className = 'msg msg-ai';
_wrap.dataset.researchSession = sessionId;
var _role = document.createElement('div');
_role.className = 'role';
var _meta = sessionModule.getSessions().find(function(s) { return s.id === sessionId; });
_role.textContent = _shortModel(_meta?.model);
_applyModelColor(_role, _meta?.model);
_role.appendChild(chatRenderer.roleTimestamp());
var _body = document.createElement('div');
_body.className = 'body';
_body.innerHTML = srcBox + markdownModule.processWithThinking(
markdownModule.squashOutsideCode(cleanResult)
) + findingsBox;
_wrap.dataset.raw = cleanResult;
_wrap.appendChild(_role);
_wrap.appendChild(_body);
_wrap.appendChild(chatRenderer.createMsgFooter(_wrap));
_appendViewReportLink(_wrap, sessionId);
_box.appendChild(_wrap);
if (window.hljs) _wrap.querySelectorAll('pre code').forEach(function(b) { window.hljs.highlightElement(b); });
uiModule.scrollHistory();
}
}
}
return;
}
if (data.status !== 'running') return;
// Don't show reconnect UI if we've already switched away
if (sessionModule.getCurrentSessionId() !== sessionId) return;
// Research is still running — show reconnect UI with spinner
const box = document.getElementById('chat-history');
if (!box) return;
const holder = document.createElement('div');
holder.className = 'msg msg-ai research-reconnect';
holder.dataset.researchSession = sessionId;
const roleTs = new Date().toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
const agentMeta = sessionModule.getSessions().find(s => s.id === sessionModule.getCurrentSessionId());
const agentModelLabel = _shortModel(agentMeta?.model);
holder.innerHTML = `${agentModelLabel} `;
_applyModelColor(holder.querySelector('.role'), agentMeta?.model);
box.appendChild(holder);
const bodyDiv = holder.querySelector('.body');
const spinner = spinnerModule.create('Reconnecting to research...', 'right');
bodyDiv.appendChild(spinner.createElement());
spinner.start();
// Update spinner with current progress if available
function updateSpinnerFromProgress(progress) {
if (!progress || !progress.phase) return;
const rp = progress;
if (rp.phase === 'probing') {
spinner.updateMessage(`Verifying model: ${rp.model || '?'}`);
} else if (rp.phase === 'planning') {
spinner.updateMessage('Analyzing question & planning research strategy');
} else if (rp.phase === 'searching') {
const q = rp.queries ? `${rp.queries} queries` : '';
const s = rp.total_sources ? ` · ${rp.total_sources} sources` : '';
spinner.updateMessage(`Round ${rp.round || '?'}: Searching${q ? ' (' + q + ')' : ''}${s}`);
} else if (rp.phase === 'reading') {
spinner.updateMessage(rp.title ? `Reading: ${rp.title}` : `Round ${rp.round || '?'}: Reading ${rp.new_sources || ''} pages · ${rp.total_sources || 0} sources total`);
} else if (rp.phase === 'analyzing') {
spinner.updateMessage(`Round ${rp.round || '?'}: Analyzing ${rp.total_findings || 0} findings`);
} else if (rp.phase === 'writing') {
spinner.updateMessage(`Writing report · ${rp.total_sources || 0} sources`);
}
}
updateSpinnerFromProgress(data.progress);
_researchingStreamIds.add(sessionId);
if (sessionModule && sessionModule.markResearching) sessionModule.markResearching(sessionId);
// Restore research timer from started_at
if (data.started_at && spinner && spinner.element) {
_researchStartTime = data.started_at * 1000;
_researchAvgDuration = data.avg_duration || null;
_researchTimerEl = document.createElement('div');
_researchTimerEl.className = 'research-timer';
_researchTimerEl.style.cssText = 'font-size:0.8em; opacity:0.6; margin-top:4px; font-family:monospace;';
spinner.element.parentNode.insertBefore(_researchTimerEl, spinner.element.nextSibling);
_researchTimerInterval = setInterval(() => {
if (!_researchTimerEl) return;
var elapsed = Math.floor((Date.now() - _researchStartTime) / 1000);
var mm = String(Math.floor(elapsed / 60)).padStart(2, '0');
var ss = String(elapsed % 60).padStart(2, '0');
var txt = mm + ':' + ss;
if (_researchAvgDuration) {
var avgM = String(Math.floor(_researchAvgDuration / 60)).padStart(2, '0');
var avgS = String(Math.round(_researchAvgDuration % 60)).padStart(2, '0');
txt += ' / avg ' + avgM + ':' + avgS;
}
_researchTimerEl.textContent = txt;
}, 1000);
// Reconnect synapse — seed it with whatever progress is already known
try {
_researchSynapse = createResearchSynapse(spinner.element.parentNode, {
query: data.query || '',
startedAt: _researchStartTime,
});
if (_researchSynapse.element && _researchTimerEl) {
spinner.element.parentNode.insertBefore(_researchSynapse.element, _researchTimerEl);
}
if (data.progress) {
_researchSynapse.setPhase(data.progress.phase, data.progress);
if (typeof data.progress.round === 'number') _researchSynapse.setRound(data.progress.round);
if (typeof data.progress.total_sources === 'number') _researchSynapse.setSourceCount(data.progress.total_sources);
}
} catch (e) { console.warn('synapse reconnect failed', e); }
}
// Poll for completion
const pollInterval = setInterval(async () => {
// Stop polling if user switched to a different session
if (sessionModule.getCurrentSessionId() !== sessionId) {
clearInterval(pollInterval);
spinner.destroy();
_clearResearchTimer();
if (holder.parentNode) holder.remove();
_researchingStreamIds.delete(sessionId);
if (_researchingStreamIds.size === 0) {
var _rToggleP = document.getElementById('research-toggle-btn');
if (_rToggleP) _rToggleP.classList.remove('research-running');
}
return;
}
try {
const pollRes = await fetch(`${API_BASE}/api/research/status/${sessionId}`);
if (!pollRes.ok) {
clearInterval(pollInterval);
spinner.destroy();
_clearResearchTimer();
_researchingStreamIds.delete(sessionId);
if (sessionModule && sessionModule.clearResearching) sessionModule.clearResearching(sessionId);
return;
}
const pollData = await pollRes.json();
updateSpinnerFromProgress(pollData.progress);
if (_researchSynapse && pollData.progress) {
_researchSynapse.setPhase(pollData.progress.phase, pollData.progress);
if (typeof pollData.progress.round === 'number') _researchSynapse.setRound(pollData.progress.round);
if (typeof pollData.progress.total_sources === 'number') _researchSynapse.setSourceCount(pollData.progress.total_sources);
}
if (pollData.status !== 'running') {
clearInterval(pollInterval);
spinner.destroy();
_clearResearchTimer();
_researchingStreamIds.delete(sessionId);
if (sessionModule && sessionModule.clearResearching) sessionModule.clearResearching(sessionId);
if (pollData.status === 'done') {
_notifyResearchComplete(sessionId, data.query || '');
const rRes = await fetch(`${API_BASE}/api/research/result/${sessionId}`, { method: 'POST' });
if (rRes.ok) {
const rData = await rRes.json();
if (rData.result) {
var srcHtml = '';
if (rData.sources && rData.sources.length > 0) {
srcHtml = _buildSourcesBox(rData.sources, 'research');
}
var findingsHtml = chatRenderer.buildFindingsBox(rData.raw_findings);
bodyDiv.innerHTML = srcHtml + markdownModule.processWithThinking(
markdownModule.squashOutsideCode(rData.result)
) + findingsHtml;
holder.dataset.raw = rData.result;
_appendViewReportLink(holder, sessionId);
if (window.hljs) {
holder.querySelectorAll('pre code').forEach(b => window.hljs.highlightElement(b));
}
}
}
} else {
bodyDiv.innerHTML = '[Research ' + pollData.status + ']';
}
}
} catch (e) {
console.error('Research poll error:', e);
}
}, 2000);
} catch (e) {
// No research pending, that's fine
}
}
/** Set a display override for the next user message bubble */
export function setDisplayOverride(text) {
_displayOverride = text;
}
/** Hide the user bubble for the next submit (e.g. continue after stop) */
export function setHideUserBubble() {
_hideUserBubble = true;
}
/** Set the AI element to merge with the next streamed response (continue after stop) */
export function setPendingContinue(el) {
_pendingContinue = el;
}
/**
* Delete an AI message and its preceding user message from the conversation.
*/
export async function deleteMessage(msgElement) {
const box = document.getElementById('chat-history');
const allMsgs = Array.from(box.querySelectorAll('.msg'));
const clickedIndex = allMsgs.indexOf(msgElement);
if (clickedIndex < 0) return;
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) return;
const clickedIsUser = msgElement.classList.contains('msg-user');
// Find the user+AI pair
let userIndex = -1;
let aiIndex = -1;
if (clickedIsUser) {
userIndex = clickedIndex;
// Find the following AI message
for (let i = clickedIndex + 1; i < allMsgs.length; i++) {
if (allMsgs[i].classList.contains('msg-ai') && !allMsgs[i].classList.contains('msg-continuation')) {
aiIndex = i;
break;
}
if (allMsgs[i].classList.contains('msg-user')) break; // next user msg, no AI response
}
} else {
// If clicked on a continuation, walk back to the main AI message
let mainAiIndex = clickedIndex;
if (allMsgs[mainAiIndex].classList.contains('msg-continuation')) {
for (let i = mainAiIndex - 1; i >= 0; i--) {
if (allMsgs[i].classList.contains('msg-ai') && !allMsgs[i].classList.contains('msg-continuation')) {
mainAiIndex = i;
break;
}
}
}
aiIndex = mainAiIndex;
// Find the preceding user message
for (let i = aiIndex - 1; i >= 0; i--) {
if (allMsgs[i].classList.contains('msg-user')) {
userIndex = i;
break;
}
}
}
// Collect DB message IDs and DOM elements to remove
const msgIds = [];
const domToRemove = [];
// Add the user message if found
if (userIndex >= 0) {
domToRemove.push(allMsgs[userIndex]);
const uid = allMsgs[userIndex].dataset.dbId;
if (uid) msgIds.push(uid);
}
// Add the AI message if found
if (aiIndex >= 0) {
domToRemove.push(allMsgs[aiIndex]);
const aid = allMsgs[aiIndex].dataset.dbId;
if (aid) msgIds.push(aid);
const aiEl = allMsgs[aiIndex];
// Also remove agent-thread elements BETWEEN user and AI
if (userIndex >= 0) {
let between = allMsgs[userIndex].nextElementSibling;
while (between && between !== aiEl) {
domToRemove.push(between);
between = between.nextElementSibling;
}
}
// Walk forward from the AI element to remove continuations and tool bubbles
let sibling = aiEl.nextElementSibling;
while (sibling) {
if (sibling.classList.contains('msg-user') ||
(sibling.classList.contains('msg-ai') && !sibling.classList.contains('msg-continuation'))) {
break;
}
domToRemove.push(sibling);
sibling = sibling.nextElementSibling;
}
}
if (!msgIds.length) {
// Fallback: just remove DOM elements if no DB IDs available
domToRemove.forEach(el => el.remove());
if (uiModule) uiModule.showToast('Message deleted');
return;
}
try {
const res = await fetch(`${API_BASE}/api/session/${sessionId}/delete-messages`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ msg_ids: msgIds })
});
if (!res.ok) throw new Error('Server error ' + res.status);
domToRemove.forEach(el => el.remove());
if (uiModule) uiModule.showToast('Message deleted');
} catch (err) {
console.error('Delete failed:', err);
if (uiModule) uiModule.showError('Delete failed: ' + err.message);
}
}
/**
* Edit an AI message inline. Makes the body contentEditable, saves to DB on confirm.
*/
export async function editAIMessage(msgElement) {
const body = msgElement.querySelector('.body');
if (!body) return;
const isEditing = body.contentEditable === 'true' || body.contentEditable === 'plaintext-only';
if (isEditing) return; // already editing
const originalRaw = msgElement.dataset.raw || body.textContent || '';
// Create editable textarea overlay
const textarea = document.createElement('textarea');
textarea.className = 'msg-edit-textarea';
textarea.value = originalRaw;
textarea.style.width = '100%';
textarea.style.minHeight = Math.max(100, body.offsetHeight) + 'px';
body.style.display = 'none';
body.parentNode.insertBefore(textarea, body.nextSibling);
textarea.focus();
// Add save/cancel bar
const bar = document.createElement('div');
bar.className = 'msg-edit-bar';
const saveBtn = document.createElement('button');
saveBtn.className = 'msg-edit-save';
saveBtn.textContent = 'Save';
const cancelBtn = document.createElement('button');
cancelBtn.className = 'msg-edit-cancel';
cancelBtn.textContent = 'Cancel';
bar.appendChild(saveBtn);
bar.appendChild(cancelBtn);
textarea.parentNode.insertBefore(bar, textarea.nextSibling);
function cleanup() {
textarea.remove();
bar.remove();
body.style.display = '';
}
cancelBtn.addEventListener('click', (e) => {
e.stopPropagation();
cleanup();
});
saveBtn.addEventListener('click', async (e) => {
e.stopPropagation();
const newContent = textarea.value;
if (newContent === originalRaw) { cleanup(); return; }
const msgId = msgElement.dataset.dbId;
if (!msgId) { if (uiModule) uiModule.showError('Cannot edit: message ID not found'); cleanup(); return; }
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) { cleanup(); return; }
try {
const res = await fetch(`${API_BASE}/api/session/${sessionId}/edit-message`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ msg_id: msgId, content: newContent }),
});
if (!res.ok) throw new Error('Server error ' + res.status);
// Re-render body with markdown
body.innerHTML = markdownModule.processWithThinking(markdownModule.squashOutsideCode(newContent));
msgElement.dataset.raw = newContent;
// Add edited indicator if not already present
if (!msgElement.querySelector('.edited-indicator')) {
const indicator = document.createElement('div');
indicator.className = 'edited-indicator';
indicator.textContent = '[Message edited]';
body.parentNode.insertBefore(indicator, body.nextSibling);
}
cleanup();
if (uiModule) uiModule.showToast('Message edited');
} catch (err) {
console.error('Edit failed:', err);
if (uiModule) uiModule.showError('Edit failed: ' + err.message);
}
});
}
/**
* Rewrite the AI's last response with a specific instruction.
* Uses the lightweight /api/rewrite endpoint — no tools, no agent loop.
* Just rewrites the text of the last AI bubble.
*/
export async function rewriteWith(aiMsgElement, instruction) {
const sessionId = sessionModule.getCurrentSessionId();
if (!sessionId) return;
// Get the original text from the AI bubble
const oldRaw = aiMsgElement.dataset.raw || aiMsgElement.querySelector('.body')?.textContent || '';
const oldHtml = aiMsgElement.querySelector('.body')?.innerHTML || '';
if (!oldRaw.trim()) {
if (uiModule) uiModule.showError('No text to rewrite');
return;
}
// Save current response as a variant
let variants = [];
try { variants = JSON.parse(aiMsgElement.dataset.variants || '[]'); } catch(_) {}
if (variants.length === 0) {
variants.push({ raw: oldRaw, html: oldHtml, label: 'original' });
}
// Determine label from instruction
let varLabel = 'rewrite';
if (instruction.includes('shorter')) varLabel = 'shorter';
else if (instruction.includes('simpler')) varLabel = 'simpler';
// Clear the bubble and show a whirlpool spinner while we wait for the
// rewrite (replaces the old "Rewriting..." text).
const bodyEl = aiMsgElement.querySelector('.body');
let _rwSpin = null;
if (bodyEl) {
bodyEl.innerHTML = '';
_rwSpin = spinnerModule.createWhirlpool(18);
_rwSpin.element.style.margin = '4px 0';
bodyEl.appendChild(_rwSpin.element);
}
// Stop + detach the spinner (called once real content starts rendering, and
// on the failure path so it never spins forever).
const _killRwSpin = () => { if (_rwSpin) { try { _rwSpin.destroy(); } catch (_) {} _rwSpin = null; } };
try {
const res = await fetch(`${API_BASE}/api/rewrite`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
session_id: sessionId,
original_text: oldRaw,
instruction: instruction,
}),
});
if (!res.ok) {
throw new Error(`HTTP ${res.status}`);
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = '';
let newText = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split('\n');
buffer = lines.pop() || '';
for (const line of lines) {
if (!line.startsWith('data: ')) continue;
const payload = line.slice(6).trim();
if (payload === '[DONE]') continue;
try {
const data = JSON.parse(payload);
// The endpoint streams `event: error\ndata: {error,status}` on
// failure — surface it instead of silently hanging on "Rewriting…".
if (data.error) {
throw new Error(data.error || ('HTTP ' + (data.status || 500)));
}
// Reasoning tokens (vLLM --reasoning-parser: Qwen3 / DeepSeek-R1)
// arrive as separate {delta, thinking:true} chunks. They are NOT
// the rewrite — fold them away so they don't pollute the result.
if (data.thinking) continue;
if (data.delta) {
newText += data.delta;
_killRwSpin();
if (bodyEl) {
bodyEl.innerHTML = markdownModule.processWithThinking(
markdownModule.squashOutsideCode(newText)
);
}
}
} catch (e) {
if (e instanceof Error && e.message) throw e; // re-throw real errors
/* ignore JSON parse noise */
}
}
}
// Strip any thinking markup from the answer. A reasoning model may emit
// an inline … block, a bare