mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-05 16:02:12 +00:00
1837 lines
59 KiB
JavaScript
1837 lines
59 KiB
JavaScript
(() => {
|
||
const modelChip = document.getElementById('modelChip');
|
||
const modelLabel = document.getElementById('modelLabel');
|
||
const modelDropdown = document.getElementById('modelDropdown');
|
||
let modelValue = 'grok-4.20-beta';
|
||
let modelList = [];
|
||
const tempRange = document.getElementById('tempRange');
|
||
const tempValue = document.getElementById('tempValue');
|
||
const topPRange = document.getElementById('topPRange');
|
||
const topPValue = document.getElementById('topPValue');
|
||
const systemInput = document.getElementById('systemInput');
|
||
const promptInput = document.getElementById('promptInput');
|
||
const sendBtn = document.getElementById('sendBtn');
|
||
const settingsToggle = document.getElementById('settingsToggle');
|
||
const settingsPanel = document.getElementById('settingsPanel');
|
||
const chatLog = document.getElementById('chatLog');
|
||
const emptyState = document.getElementById('emptyState');
|
||
const statusText = document.getElementById('statusText');
|
||
const attachBtn = document.getElementById('attachBtn');
|
||
const fileInput = document.getElementById('fileInput');
|
||
const fileBadge = document.getElementById('fileBadge');
|
||
const fileName = document.getElementById('fileName');
|
||
const fileRemoveBtn = document.getElementById('fileRemoveBtn');
|
||
const chatSidebar = document.getElementById('chatSidebar');
|
||
const sidebarOverlay = document.getElementById('sidebarOverlay');
|
||
const sidebarToggle = document.getElementById('sidebarToggle');
|
||
const newChatBtn = document.getElementById('newChatBtn');
|
||
const collapseSidebarBtn = document.getElementById('collapseSidebarBtn');
|
||
const sidebarExpandBtn = document.getElementById('sidebarExpandBtn');
|
||
const sessionListEl = document.getElementById('sessionList');
|
||
|
||
const STORAGE_KEY = 'grok2api_chat_sessions';
|
||
const SIDEBAR_STATE_KEY = 'grok2api_chat_sidebar_collapsed';
|
||
const MAX_CONTEXT_MESSAGES = 5;
|
||
|
||
let messageHistory = [];
|
||
let isSending = false;
|
||
let abortController = null;
|
||
let attachment = null;
|
||
let activeStreamInfo = null;
|
||
const feedbackUrl = 'https://github.com/chenyme/grok2api/issues/new';
|
||
const CHAT_COMPLETIONS_ENDPOINT = '/v1/function/chat/completions';
|
||
const DEFAULT_SESSION_TITLES = ['新会话', 'New Session'];
|
||
|
||
let sessionsData = null;
|
||
|
||
function generateId() {
|
||
return crypto.randomUUID ? crypto.randomUUID() : Date.now().toString(36) + Math.random().toString(36).slice(2);
|
||
}
|
||
|
||
function isDefaultTitleValue(title) {
|
||
return DEFAULT_SESSION_TITLES.includes(title);
|
||
}
|
||
|
||
function loadSessions() {
|
||
try {
|
||
const raw = localStorage.getItem(STORAGE_KEY);
|
||
if (raw) {
|
||
sessionsData = JSON.parse(raw);
|
||
if (!sessionsData || !Array.isArray(sessionsData.sessions)) {
|
||
sessionsData = null;
|
||
}
|
||
}
|
||
} catch (e) {
|
||
sessionsData = null;
|
||
}
|
||
if (!sessionsData || !sessionsData.sessions.length) {
|
||
const id = generateId();
|
||
sessionsData = {
|
||
activeId: id,
|
||
sessions: [{
|
||
id,
|
||
title: t('chat.newSession'),
|
||
isDefaultTitle: true,
|
||
createdAt: Date.now(),
|
||
updatedAt: Date.now(),
|
||
messages: []
|
||
}]
|
||
};
|
||
saveSessions();
|
||
}
|
||
sessionsData.sessions.forEach((session) => {
|
||
if (session && typeof session.isDefaultTitle === 'undefined') {
|
||
session.isDefaultTitle = isDefaultTitleValue(session.title);
|
||
}
|
||
});
|
||
if (!sessionsData.activeId || !sessionsData.sessions.find(s => s.id === sessionsData.activeId)) {
|
||
sessionsData.activeId = sessionsData.sessions[0].id;
|
||
}
|
||
restoreActiveSession();
|
||
renderSessionList();
|
||
}
|
||
|
||
function getMessageDisplay(msg) {
|
||
if (!msg) return '';
|
||
if (typeof msg.content === 'string') return msg.content;
|
||
if (typeof msg.display === 'string' && msg.display.trim()) return msg.display;
|
||
if (Array.isArray(msg.content)) {
|
||
const textParts = [];
|
||
let hasFile = false;
|
||
for (const block of msg.content) {
|
||
if (!block) continue;
|
||
if (block.type === 'text' && block.text) {
|
||
textParts.push(block.text);
|
||
}
|
||
if (block.type === 'file') {
|
||
hasFile = true;
|
||
}
|
||
}
|
||
const name = msg.attachmentName || '';
|
||
const fileLabel = hasFile ? (name ? t('chat.fileLabel') + ' ' + name : t('chat.fileLabel')) : '';
|
||
if (textParts.length && fileLabel) return `${textParts.join('\n')}\n${fileLabel}`;
|
||
if (textParts.length) return textParts.join('\n');
|
||
return fileLabel || t('chat.compositeContent');
|
||
}
|
||
return t('chat.compositeContent');
|
||
}
|
||
|
||
function serializeMessage(msg) {
|
||
if (!msg || typeof msg !== 'object') return msg;
|
||
if (Array.isArray(msg.content)) {
|
||
return {
|
||
...msg,
|
||
content: getMessageDisplay(msg)
|
||
};
|
||
}
|
||
return msg;
|
||
}
|
||
|
||
function serializeSessions() {
|
||
if (!sessionsData) return null;
|
||
return {
|
||
activeId: sessionsData.activeId,
|
||
sessions: sessionsData.sessions.map((session) => ({
|
||
...session,
|
||
messages: Array.isArray(session.messages)
|
||
? session.messages.map(serializeMessage)
|
||
: []
|
||
}))
|
||
};
|
||
}
|
||
|
||
function saveSessions() {
|
||
if (!sessionsData) return;
|
||
const snapshot = serializeSessions();
|
||
if (!snapshot) return;
|
||
try {
|
||
localStorage.setItem(STORAGE_KEY, JSON.stringify(snapshot));
|
||
} catch (e) {
|
||
toast(t('chat.storageFull'), 'error');
|
||
}
|
||
}
|
||
|
||
function trimMessageHistory(maxCount = MAX_CONTEXT_MESSAGES) {
|
||
if (!maxCount || maxCount <= 0) return;
|
||
if (messageHistory.length <= maxCount) return;
|
||
messageHistory = messageHistory.slice(-maxCount);
|
||
const session = getActiveSession();
|
||
if (session) {
|
||
session.messages = messageHistory.slice();
|
||
session.updatedAt = Date.now();
|
||
saveSessions();
|
||
renderSessionList();
|
||
}
|
||
if (chatLog) {
|
||
const rows = Array.from(chatLog.querySelectorAll('.message-row'));
|
||
const removeCount = rows.length - messageHistory.length;
|
||
if (removeCount > 0) {
|
||
rows.slice(0, removeCount).forEach((row) => row.remove());
|
||
}
|
||
}
|
||
if (!messageHistory.length) {
|
||
showEmptyState();
|
||
}
|
||
}
|
||
|
||
function getActiveSession() {
|
||
if (!sessionsData) return null;
|
||
return sessionsData.sessions.find(s => s.id === sessionsData.activeId) || null;
|
||
}
|
||
|
||
function restoreActiveSession() {
|
||
const session = getActiveSession();
|
||
if (!session) return;
|
||
messageHistory = session.messages.slice();
|
||
trimMessageHistory();
|
||
if (chatLog) chatLog.innerHTML = '';
|
||
if (!messageHistory.length) {
|
||
showEmptyState();
|
||
return;
|
||
}
|
||
hideEmptyState();
|
||
for (const msg of messageHistory) {
|
||
const displayContent = getMessageDisplay(msg);
|
||
const editable = !msg.hasAttachment && typeof msg.content === 'string';
|
||
const entry = createMessage(msg.role, displayContent, true, { editable });
|
||
if (entry && msg.role === 'assistant') {
|
||
updateMessage(entry, displayContent, true);
|
||
}
|
||
}
|
||
if (activeStreamInfo && activeStreamInfo.sessionId === session.id && activeStreamInfo.entry.row) {
|
||
chatLog.appendChild(activeStreamInfo.entry.row);
|
||
}
|
||
scrollToBottom();
|
||
}
|
||
|
||
function createSession() {
|
||
const id = generateId();
|
||
const session = {
|
||
id,
|
||
title: t('chat.newSession'),
|
||
isDefaultTitle: true,
|
||
createdAt: Date.now(),
|
||
updatedAt: Date.now(),
|
||
messages: []
|
||
};
|
||
sessionsData.sessions.unshift(session);
|
||
sessionsData.activeId = id;
|
||
messageHistory = [];
|
||
if (chatLog) chatLog.innerHTML = '';
|
||
showEmptyState();
|
||
saveSessions();
|
||
renderSessionList();
|
||
if (isMobileSidebar()) closeSidebar();
|
||
}
|
||
|
||
function deleteSession(id) {
|
||
const idx = sessionsData.sessions.findIndex(s => s.id === id);
|
||
if (idx === -1) return;
|
||
sessionsData.sessions.splice(idx, 1);
|
||
if (!sessionsData.sessions.length) {
|
||
createSession();
|
||
return;
|
||
}
|
||
if (sessionsData.activeId === id) {
|
||
const newIdx = Math.min(idx, sessionsData.sessions.length - 1);
|
||
sessionsData.activeId = sessionsData.sessions[newIdx].id;
|
||
restoreActiveSession();
|
||
}
|
||
saveSessions();
|
||
renderSessionList();
|
||
}
|
||
|
||
function switchSession(id) {
|
||
if (sessionsData.activeId === id) return;
|
||
syncCurrentSession();
|
||
syncSessionModel();
|
||
sessionsData.activeId = id;
|
||
const target = getActiveSession();
|
||
if (target) target.unread = false;
|
||
restoreActiveSession();
|
||
restoreSessionModel();
|
||
saveSessions();
|
||
renderSessionList();
|
||
if (isMobileSidebar()) closeSidebar();
|
||
}
|
||
|
||
function syncCurrentSession() {
|
||
const session = getActiveSession();
|
||
if (!session) return;
|
||
session.messages = messageHistory.slice();
|
||
session.updatedAt = Date.now();
|
||
}
|
||
|
||
function updateSessionTitle(session) {
|
||
if (!session) return;
|
||
if (session.isDefaultTitle === false) return;
|
||
const firstUser = session.messages.find(m => m.role === 'user');
|
||
if (!firstUser) return;
|
||
const text = getMessageDisplay(firstUser);
|
||
if (!text) return;
|
||
const title = text.replace(/\n/g, ' ').trim().slice(0, 20);
|
||
if (title) {
|
||
session.title = title;
|
||
session.isDefaultTitle = false;
|
||
}
|
||
}
|
||
|
||
function renameSession(id, newTitle) {
|
||
const session = sessionsData.sessions.find(s => s.id === id);
|
||
if (!session) return;
|
||
const trimmed = (newTitle || '').trim();
|
||
session.title = trimmed || t('chat.newSession');
|
||
session.isDefaultTitle = !trimmed && isDefaultTitleValue(session.title);
|
||
session.updatedAt = Date.now();
|
||
saveSessions();
|
||
renderSessionList();
|
||
}
|
||
|
||
function startRenameSession(sessionId, titleSpan) {
|
||
const session = sessionsData.sessions.find(s => s.id === sessionId);
|
||
if (!session) return;
|
||
const input = document.createElement('input');
|
||
input.type = 'text';
|
||
input.className = 'session-rename-input';
|
||
input.value = session.title || '';
|
||
input.maxLength = 40;
|
||
titleSpan.replaceWith(input);
|
||
input.focus();
|
||
input.select();
|
||
const commit = () => {
|
||
renameSession(sessionId, input.value);
|
||
};
|
||
input.addEventListener('blur', commit);
|
||
input.addEventListener('keydown', (e) => {
|
||
if (e.key === 'Enter') { e.preventDefault(); input.blur(); }
|
||
if (e.key === 'Escape') { input.value = session.title || t('chat.newSession'); input.blur(); }
|
||
});
|
||
}
|
||
|
||
function syncSessionModel() {
|
||
const session = getActiveSession();
|
||
if (!session) return;
|
||
session.model = modelValue || '';
|
||
}
|
||
|
||
function restoreSessionModel() {
|
||
const session = getActiveSession();
|
||
if (!session || !session.model) return;
|
||
if (modelList.includes(session.model)) {
|
||
selectModel(session.model);
|
||
}
|
||
}
|
||
|
||
function renderSessionList() {
|
||
if (!sessionListEl || !sessionsData) return;
|
||
sessionListEl.innerHTML = '';
|
||
for (const session of sessionsData.sessions) {
|
||
const item = document.createElement('div');
|
||
item.className = 'session-item' + (session.id === sessionsData.activeId ? ' active' : '');
|
||
item.dataset.id = session.id;
|
||
|
||
const titleSpan = document.createElement('span');
|
||
titleSpan.className = 'session-title';
|
||
titleSpan.textContent = session.title || t('chat.newSession');
|
||
titleSpan.addEventListener('dblclick', (e) => {
|
||
e.stopPropagation();
|
||
startRenameSession(session.id, titleSpan);
|
||
});
|
||
item.appendChild(titleSpan);
|
||
|
||
if (session.unread && session.id !== sessionsData.activeId) {
|
||
const dot = document.createElement('span');
|
||
dot.className = 'session-unread';
|
||
item.appendChild(dot);
|
||
}
|
||
|
||
const delBtn = document.createElement('button');
|
||
delBtn.className = 'session-delete';
|
||
delBtn.type = 'button';
|
||
delBtn.title = t('common.delete');
|
||
delBtn.textContent = '×';
|
||
delBtn.addEventListener('click', (e) => {
|
||
e.stopPropagation();
|
||
deleteSession(session.id);
|
||
});
|
||
item.appendChild(delBtn);
|
||
|
||
item.addEventListener('click', () => switchSession(session.id));
|
||
sessionListEl.appendChild(item);
|
||
}
|
||
}
|
||
|
||
function isMobileSidebar() {
|
||
return window.matchMedia('(max-width: 1024px)').matches;
|
||
}
|
||
|
||
function setSidebarCollapsed(collapsed) {
|
||
const layout = chatSidebar ? chatSidebar.closest('.chat-layout') : null;
|
||
if (!layout) return;
|
||
layout.classList.toggle('collapsed', collapsed);
|
||
try {
|
||
localStorage.setItem(SIDEBAR_STATE_KEY, collapsed ? '1' : '0');
|
||
} catch (e) {
|
||
// ignore storage failures
|
||
}
|
||
}
|
||
|
||
function openSidebar() {
|
||
if (isMobileSidebar()) {
|
||
if (chatSidebar) chatSidebar.classList.add('open');
|
||
if (sidebarOverlay) sidebarOverlay.classList.add('open');
|
||
return;
|
||
}
|
||
setSidebarCollapsed(false);
|
||
}
|
||
|
||
function closeSidebar() {
|
||
if (isMobileSidebar()) {
|
||
if (chatSidebar) chatSidebar.classList.remove('open');
|
||
if (sidebarOverlay) sidebarOverlay.classList.remove('open');
|
||
return;
|
||
}
|
||
setSidebarCollapsed(true);
|
||
}
|
||
|
||
function toggleSidebar() {
|
||
if (isMobileSidebar()) {
|
||
if (chatSidebar && chatSidebar.classList.contains('open')) {
|
||
closeSidebar();
|
||
} else {
|
||
openSidebar();
|
||
}
|
||
return;
|
||
}
|
||
const layout = chatSidebar ? chatSidebar.closest('.chat-layout') : null;
|
||
if (!layout) return;
|
||
setSidebarCollapsed(!layout.classList.contains('collapsed'));
|
||
}
|
||
|
||
function toast(message, type) {
|
||
if (typeof showToast === 'function') {
|
||
showToast(message, type);
|
||
}
|
||
}
|
||
|
||
function setStatus(state, text) {
|
||
if (!statusText) return;
|
||
statusText.textContent = text || t('chat.ready');
|
||
statusText.classList.remove('connected', 'connecting', 'error');
|
||
if (state) statusText.classList.add(state);
|
||
}
|
||
|
||
function setSendingState(sending) {
|
||
isSending = sending;
|
||
if (sendBtn) sendBtn.disabled = sending;
|
||
}
|
||
|
||
function updateRangeValues() {
|
||
if (tempValue && tempRange) {
|
||
tempValue.textContent = Number(tempRange.value).toFixed(2);
|
||
}
|
||
if (topPValue && topPRange) {
|
||
topPValue.textContent = Number(topPRange.value).toFixed(2);
|
||
}
|
||
}
|
||
|
||
function scrollToBottom() {
|
||
const body = document.scrollingElement || document.documentElement;
|
||
if (!body) return;
|
||
const hasOwnScroll = chatLog && chatLog.scrollHeight > chatLog.clientHeight + 1;
|
||
if (hasOwnScroll) {
|
||
chatLog.scrollTop = chatLog.scrollHeight;
|
||
return;
|
||
}
|
||
body.scrollTop = body.scrollHeight;
|
||
}
|
||
|
||
function hideEmptyState() {
|
||
if (emptyState) emptyState.classList.add('hidden');
|
||
}
|
||
|
||
function showEmptyState() {
|
||
if (emptyState) emptyState.classList.remove('hidden');
|
||
}
|
||
|
||
function setRenderedHTML(el, html) {
|
||
// html is pre-sanitized through renderMarkdown → escapeHtml pipeline;
|
||
// all user text is entity-escaped before any HTML construction.
|
||
el.innerHTML = html;
|
||
}
|
||
|
||
function escapeHtml(value) {
|
||
return value
|
||
.replace(/&/g, '&')
|
||
.replace(/</g, '<')
|
||
.replace(/>/g, '>')
|
||
.replace(/"/g, '"')
|
||
.replace(/'/g, ''');
|
||
}
|
||
|
||
function isSafeLinkUrl(url) {
|
||
const val = String(url || '').trim().toLowerCase();
|
||
if (!val) return false;
|
||
return /^(https?:|mailto:|tel:|\/(?!\/)|\.\.?\/|#)/.test(val);
|
||
}
|
||
|
||
function isSafeImageUrl(url) {
|
||
const val = String(url || '').trim().toLowerCase();
|
||
if (!val) return false;
|
||
return /^(https?:|data:image\/(?:png|jpe?g|gif|webp|bmp|ico);base64,|\/(?!\/)|\.\.?\/)/.test(val);
|
||
}
|
||
|
||
function renderBasicMarkdown(rawText) {
|
||
const text = (rawText || '').replace(/\\n/g, '\n');
|
||
const escaped = escapeHtml(text);
|
||
const codeBlocks = [];
|
||
const fenced = escaped.replace(/```([a-zA-Z0-9_-]+)?\n([\s\S]*?)```/g, (match, lang, code) => {
|
||
const safeLang = lang ? escapeHtml(lang) : '';
|
||
const html = `<pre class="code-block"><code${safeLang ? ` class="language-${safeLang}"` : ''}>${code}</code></pre>`;
|
||
const token = `@@CODEBLOCK_${codeBlocks.length}@@`;
|
||
codeBlocks.push(html);
|
||
return token;
|
||
});
|
||
|
||
const renderInline = (value) => {
|
||
const inlineCodes = [];
|
||
let output = value.replace(/`([^`]+)`/g, (match, code) => {
|
||
const token = `@@INLINE_${inlineCodes.length}@@`;
|
||
inlineCodes.push(`<code class="inline-code">${code}</code>`);
|
||
return token;
|
||
});
|
||
|
||
output = output
|
||
.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
|
||
.replace(/\*([^*]+)\*/g, '<em>$1</em>')
|
||
.replace(/~~([^~]+)~~/g, '<del>$1</del>');
|
||
|
||
output = output.replace(/!\[([^\]]*)\]\(([^)]+)\)/g, (match, alt, url) => {
|
||
const safeAlt = escapeHtml(alt || 'image');
|
||
if (!isSafeImageUrl(url)) return safeAlt;
|
||
const safeUrl = escapeHtml(url || '');
|
||
return `<img src="${safeUrl}" alt="${safeAlt}" loading="lazy">`;
|
||
});
|
||
|
||
output = output.replace(/\[([^\]]+)\]\(([^)]+)\)/g, (match, label, url) => {
|
||
const safeLabel = escapeHtml(label || '');
|
||
if (!isSafeLinkUrl(url)) return safeLabel;
|
||
const safeUrl = escapeHtml(url || '');
|
||
return `<a href="${safeUrl}" target="_blank" rel="noopener">${safeLabel}</a>`;
|
||
});
|
||
|
||
output = output.replace(/(data:image\/[a-zA-Z0-9.+-]+;base64,[A-Za-z0-9+/=]+)/g, (match, uri) => {
|
||
if (!isSafeImageUrl(uri)) return '';
|
||
const safeUrl = escapeHtml(uri || '');
|
||
return `<img src="${safeUrl}" alt="image" loading="lazy">`;
|
||
});
|
||
|
||
inlineCodes.forEach((html, i) => {
|
||
output = output.replace(new RegExp(`@@INLINE_${i}@@`, 'g'), html);
|
||
});
|
||
|
||
return output;
|
||
};
|
||
|
||
const lines = fenced.split(/\r?\n/);
|
||
const htmlParts = [];
|
||
let inUl = false;
|
||
let inUlTask = false;
|
||
let inOl = false;
|
||
let inTable = false;
|
||
let paragraphLines = [];
|
||
|
||
const closeLists = () => {
|
||
if (inUl) {
|
||
htmlParts.push('</ul>');
|
||
inUl = false;
|
||
inUlTask = false;
|
||
}
|
||
if (inOl) {
|
||
htmlParts.push('</ol>');
|
||
inOl = false;
|
||
}
|
||
};
|
||
|
||
const closeTable = () => {
|
||
if (inTable) {
|
||
htmlParts.push('</tbody></table>');
|
||
inTable = false;
|
||
}
|
||
};
|
||
|
||
const flushParagraph = () => {
|
||
if (!paragraphLines.length) return;
|
||
const joined = paragraphLines.join('<br>');
|
||
htmlParts.push(`<p>${renderInline(joined)}</p>`);
|
||
paragraphLines = [];
|
||
};
|
||
|
||
const isTableSeparator = (line) => /^\s*\|?(?:\s*:?-+:?\s*\|)+\s*$/.test(line);
|
||
const splitTableRow = (line) => {
|
||
const trimmed = line.trim();
|
||
const row = trimmed.replace(/^\|/, '').replace(/\|$/, '');
|
||
return row.split('|').map(cell => cell.trim());
|
||
};
|
||
|
||
for (let i = 0; i < lines.length; i += 1) {
|
||
const line = lines[i];
|
||
const trimmed = line.trim();
|
||
if (!trimmed) {
|
||
flushParagraph();
|
||
closeLists();
|
||
closeTable();
|
||
continue;
|
||
}
|
||
|
||
const codeTokenMatch = trimmed.match(/^@@CODEBLOCK_(\d+)@@$/);
|
||
if (codeTokenMatch) {
|
||
flushParagraph();
|
||
closeLists();
|
||
closeTable();
|
||
htmlParts.push(trimmed);
|
||
continue;
|
||
}
|
||
|
||
const headingMatch = trimmed.match(/^(#{1,6})\s+(.*)$/);
|
||
if (headingMatch) {
|
||
flushParagraph();
|
||
closeLists();
|
||
closeTable();
|
||
const level = headingMatch[1].length;
|
||
htmlParts.push(`<h${level}>${renderInline(headingMatch[2])}</h${level}>`);
|
||
continue;
|
||
}
|
||
|
||
if (/^(-{3,}|\*{3,}|_{3,})$/.test(trimmed)) {
|
||
flushParagraph();
|
||
closeLists();
|
||
closeTable();
|
||
htmlParts.push('<hr>');
|
||
continue;
|
||
}
|
||
|
||
if (/^\s*>/.test(line)) {
|
||
flushParagraph();
|
||
closeLists();
|
||
closeTable();
|
||
const quoteLines = [];
|
||
let j = i;
|
||
for (; j < lines.length; j += 1) {
|
||
const currentLine = lines[j];
|
||
if (!/^\s*>/.test(currentLine)) break;
|
||
quoteLines.push(currentLine.replace(/^\s*>\s?/, ''));
|
||
}
|
||
i = j - 1;
|
||
const quoteText = quoteLines.join('\n');
|
||
htmlParts.push(`<blockquote>${renderBasicMarkdown(quoteText)}</blockquote>`);
|
||
continue;
|
||
}
|
||
|
||
if (trimmed.includes('|')) {
|
||
const nextLine = lines[i + 1] || '';
|
||
if (!inTable && isTableSeparator(nextLine.trim())) {
|
||
flushParagraph();
|
||
closeLists();
|
||
const headers = splitTableRow(trimmed);
|
||
htmlParts.push('<div class="table-wrap"><table><thead><tr>');
|
||
headers.forEach(cell => htmlParts.push(`<th>${renderInline(cell)}</th>`));
|
||
htmlParts.push('</tr></thead><tbody>');
|
||
inTable = true;
|
||
i += 1;
|
||
continue;
|
||
}
|
||
if (inTable && !isTableSeparator(trimmed)) {
|
||
const cells = splitTableRow(trimmed);
|
||
htmlParts.push('<tr>');
|
||
cells.forEach(cell => htmlParts.push(`<td>${renderInline(cell)}</td>`));
|
||
htmlParts.push('</tr>');
|
||
continue;
|
||
}
|
||
}
|
||
|
||
const taskMatch = trimmed.match(/^[-*+•·]\s+\[([ xX])\]\s+(.*)$/);
|
||
if (taskMatch) {
|
||
flushParagraph();
|
||
if (inUl && !inUlTask) {
|
||
closeLists();
|
||
}
|
||
if (!inUl) {
|
||
closeLists();
|
||
closeTable();
|
||
htmlParts.push('<ul class="task-list">');
|
||
inUl = true;
|
||
inUlTask = true;
|
||
}
|
||
const checked = taskMatch[1].toLowerCase() === 'x';
|
||
htmlParts.push(`<li class="task-item"><input type="checkbox" disabled${checked ? ' checked' : ''}>${renderInline(taskMatch[2])}</li>`);
|
||
continue;
|
||
}
|
||
|
||
const ulMatch = trimmed.match(/^[-*+•·]\s+(.*)$/);
|
||
if (ulMatch) {
|
||
flushParagraph();
|
||
if (!inUl) {
|
||
closeLists();
|
||
closeTable();
|
||
htmlParts.push('<ul>');
|
||
inUl = true;
|
||
inUlTask = false;
|
||
}
|
||
htmlParts.push(`<li>${renderInline(ulMatch[1])}</li>`);
|
||
continue;
|
||
}
|
||
|
||
const olMatch = trimmed.match(/^\d+[.)、]\s+(.*)$/);
|
||
if (olMatch) {
|
||
flushParagraph();
|
||
if (!inOl) {
|
||
closeLists();
|
||
closeTable();
|
||
htmlParts.push('<ol>');
|
||
inOl = true;
|
||
}
|
||
htmlParts.push(`<li>${renderInline(olMatch[1])}</li>`);
|
||
continue;
|
||
}
|
||
|
||
paragraphLines.push(trimmed);
|
||
}
|
||
|
||
flushParagraph();
|
||
closeLists();
|
||
closeTable();
|
||
|
||
let output = htmlParts.join('');
|
||
codeBlocks.forEach((html, index) => {
|
||
output = output.replace(`@@CODEBLOCK_${index}@@`, html);
|
||
});
|
||
return output;
|
||
}
|
||
|
||
function parseThinkSections(raw) {
|
||
const parts = [];
|
||
let cursor = 0;
|
||
while (cursor < raw.length) {
|
||
const start = raw.indexOf('<think>', cursor);
|
||
if (start === -1) {
|
||
parts.push({ type: 'text', value: raw.slice(cursor) });
|
||
break;
|
||
}
|
||
if (start > cursor) {
|
||
parts.push({ type: 'text', value: raw.slice(cursor, start) });
|
||
}
|
||
const thinkStart = start + 7;
|
||
const end = raw.indexOf('</think>', thinkStart);
|
||
if (end === -1) {
|
||
parts.push({ type: 'think', value: raw.slice(thinkStart), open: true });
|
||
cursor = raw.length;
|
||
} else {
|
||
parts.push({ type: 'think', value: raw.slice(thinkStart, end), open: false });
|
||
cursor = end + 8;
|
||
}
|
||
}
|
||
return parts;
|
||
}
|
||
|
||
function parseRolloutBlocks(text) {
|
||
const lines = (text || '').split(/\r?\n/);
|
||
const blocks = [];
|
||
let current = null;
|
||
for (const line of lines) {
|
||
const match = line.match(/^\s*\[([^\]]+)\]\[([^\]]+)\]\s*(.*)$/);
|
||
if (match) {
|
||
if (current) blocks.push(current);
|
||
current = { id: match[1], type: match[2], lines: [] };
|
||
if (match[3]) current.lines.push(match[3]);
|
||
continue;
|
||
}
|
||
if (current) {
|
||
current.lines.push(line);
|
||
}
|
||
}
|
||
if (current) blocks.push(current);
|
||
return blocks;
|
||
}
|
||
|
||
function parseAgentSections(text) {
|
||
const lines = (text || '').split(/\r?\n/);
|
||
const sections = [];
|
||
let current = { title: null, lines: [] };
|
||
let hasAgentHeading = false;
|
||
for (const line of lines) {
|
||
const trimmed = line.trim();
|
||
if (!trimmed) {
|
||
current.lines.push(line);
|
||
continue;
|
||
}
|
||
const agentMatch = trimmed.match(/^(Grok\s+Leader|(?:Grok\s+)?Agent\s*\d+)$/i);
|
||
if (agentMatch) {
|
||
hasAgentHeading = true;
|
||
if (current.lines.length) {
|
||
sections.push(current);
|
||
}
|
||
current = { title: agentMatch[1], lines: [] };
|
||
continue;
|
||
}
|
||
current.lines.push(line);
|
||
}
|
||
if (current.lines.length) {
|
||
sections.push(current);
|
||
}
|
||
if (!hasAgentHeading) {
|
||
return [{ title: null, lines }];
|
||
}
|
||
return sections;
|
||
}
|
||
|
||
const toolTypeMap = {
|
||
websearch: { icon: '', label: t('chat.toolWebSearch') },
|
||
searchimage: { icon: '', label: t('chat.toolImageSearch') },
|
||
agentthink: { icon: '', label: t('chat.toolAgentThink') }
|
||
};
|
||
const defaultToolType = { icon: '', label: t('chat.toolDefault') };
|
||
|
||
function getToolMeta(typeStr) {
|
||
const key = String(typeStr || '').trim().toLowerCase().replace(/\s+/g, '');
|
||
return toolTypeMap[key] || defaultToolType;
|
||
}
|
||
|
||
function renderThinkContent(text, openAll) {
|
||
const sections = parseAgentSections(text);
|
||
if (!sections.length) {
|
||
return renderBasicMarkdown(text);
|
||
}
|
||
const renderGroups = (blocks, openAllGroups) => {
|
||
const groups = [];
|
||
const map = new Map();
|
||
for (const block of blocks) {
|
||
const key = block.id;
|
||
let group = map.get(key);
|
||
if (!group) {
|
||
group = { id: key, items: [] };
|
||
map.set(key, group);
|
||
groups.push(group);
|
||
}
|
||
group.items.push(block);
|
||
}
|
||
return groups.map((group) => {
|
||
const items = group.items.map((item) => {
|
||
const body = renderBasicMarkdown(item.lines.join('\n').trim());
|
||
const typeKey = String(item.type || '').trim().toLowerCase().replace(/\s+/g, '');
|
||
const typeAttr = escapeHtml(typeKey);
|
||
const meta = getToolMeta(item.type);
|
||
const iconHtml = meta.icon ? `<span class="think-tool-icon">${meta.icon}</span>` : '';
|
||
const typeLabel = `${iconHtml}${escapeHtml(meta.label)}`;
|
||
return `<div class="think-item-row think-tool-card" data-tool-type="${typeAttr}"><div class="think-item-type" data-type="${typeAttr}">${typeLabel}</div><div class="think-item-body">${body || '<em>' + t('chat.empty') + '</em>'}</div></div>`;
|
||
}).join('');
|
||
const title = escapeHtml(group.id);
|
||
const openAttr = openAllGroups ? ' open' : '';
|
||
return `<details class="think-rollout-group"${openAttr}><summary><span class="think-rollout-title">${title}</span></summary><div class="think-rollout-body">${items}</div></details>`;
|
||
}).join('');
|
||
};
|
||
|
||
const agentBlocks = sections.map((section, idx) => {
|
||
const blocks = parseRolloutBlocks(section.lines.join('\n'));
|
||
const inner = blocks.length
|
||
? renderGroups(blocks, openAll)
|
||
: `<div class="think-rollout-body">${renderBasicMarkdown(section.lines.join('\n').trim())}</div>`;
|
||
if (!section.title) {
|
||
return `<div class="think-agent-items">${inner}</div>`;
|
||
}
|
||
const title = escapeHtml(section.title);
|
||
const openAttr = openAll ? ' open' : (idx === 0 ? ' open' : '');
|
||
return `<details class="think-agent"${openAttr}><summary>${title}</summary><div class="think-agent-items">${inner}</div></details>`;
|
||
});
|
||
return `<div class="think-agents">${agentBlocks.join('')}</div>`;
|
||
}
|
||
|
||
function renderMarkdown(text) {
|
||
const raw = text || '';
|
||
const parts = parseThinkSections(raw);
|
||
return parts.map((part) => {
|
||
if (part.type === 'think') {
|
||
const body = renderThinkContent(part.value.trim(), part.open);
|
||
const openAttr = part.open ? ' open' : '';
|
||
return `<details class="think-block" data-think="true"${openAttr}><summary class="think-summary">${t('chat.thinkLabel')}</summary><div class="think-content">${body || '<em>' + t('chat.empty') + '</em>'}</div></details>`;
|
||
}
|
||
return renderBasicMarkdown(part.value);
|
||
}).join('');
|
||
}
|
||
|
||
function deleteMessageByRow(row) {
|
||
if (!row || !chatLog) return;
|
||
if (activeStreamInfo && activeStreamInfo.entry.row === row) return;
|
||
const rows = chatLog.querySelectorAll('.message-row');
|
||
const idx = Array.from(rows).indexOf(row);
|
||
if (idx === -1 || idx >= messageHistory.length) return;
|
||
messageHistory.splice(idx, 1);
|
||
row.remove();
|
||
const session = getActiveSession();
|
||
if (session) {
|
||
session.messages = messageHistory.slice();
|
||
session.updatedAt = Date.now();
|
||
saveSessions();
|
||
}
|
||
if (!messageHistory.length) {
|
||
showEmptyState();
|
||
}
|
||
}
|
||
|
||
function editMessageByRow(row) {
|
||
if (isSending || !row || !chatLog) return;
|
||
const rows = chatLog.querySelectorAll('.message-row');
|
||
const idx = Array.from(rows).indexOf(row);
|
||
if (idx === -1 || idx >= messageHistory.length) return;
|
||
const msg = messageHistory[idx];
|
||
if (msg && (msg.hasAttachment || typeof msg.content !== 'string')) {
|
||
toast(t('chat.attachmentNoEdit'), 'error');
|
||
return;
|
||
}
|
||
const currentText = typeof msg.content === 'string' ? msg.content : '';
|
||
|
||
const contentNode = row.querySelector('.message-content');
|
||
if (!contentNode) return;
|
||
|
||
const textarea = document.createElement('textarea');
|
||
textarea.className = 'edit-msg-input';
|
||
textarea.value = currentText;
|
||
textarea.rows = Math.max(3, currentText.split('\n').length);
|
||
|
||
const btnWrap = document.createElement('div');
|
||
btnWrap.className = 'edit-msg-actions';
|
||
const saveBtn = createActionButton(t('chat.saveEdit'), t('chat.saveEditTitle'), () => commit());
|
||
const cancelBtn = createActionButton(t('chat.cancelEdit'), t('chat.cancelEditTitle'), () => cancel());
|
||
btnWrap.appendChild(saveBtn);
|
||
btnWrap.appendChild(cancelBtn);
|
||
|
||
const savedChildren = Array.from(contentNode.childNodes).map(n => n.cloneNode(true));
|
||
const originalClass = contentNode.className;
|
||
contentNode.className = 'message-content';
|
||
contentNode.innerHTML = '';
|
||
contentNode.appendChild(textarea);
|
||
contentNode.appendChild(btnWrap);
|
||
row.classList.add('editing');
|
||
textarea.focus();
|
||
|
||
const actionsEl = row.querySelector('.message-actions');
|
||
if (actionsEl) actionsEl.classList.add('hidden');
|
||
|
||
function finish() {
|
||
row.classList.remove('editing');
|
||
if (actionsEl) actionsEl.classList.remove('hidden');
|
||
}
|
||
|
||
function commit() {
|
||
const newText = textarea.value.trim();
|
||
if (!newText) {
|
||
toast(t('chat.contentEmpty'), 'error');
|
||
return;
|
||
}
|
||
msg.content = newText;
|
||
contentNode.className = originalClass;
|
||
contentNode.textContent = '';
|
||
if (msg.role === 'assistant') {
|
||
contentNode.classList.add('rendered');
|
||
setRenderedHTML(contentNode, renderMarkdown(newText));
|
||
} else {
|
||
contentNode.textContent = newText;
|
||
}
|
||
finish();
|
||
const session = getActiveSession();
|
||
if (session) {
|
||
session.messages = messageHistory.slice();
|
||
session.updatedAt = Date.now();
|
||
saveSessions();
|
||
}
|
||
}
|
||
|
||
function cancel() {
|
||
contentNode.className = originalClass;
|
||
contentNode.textContent = '';
|
||
savedChildren.forEach(n => contentNode.appendChild(n));
|
||
finish();
|
||
}
|
||
}
|
||
|
||
function regenerateFromRow(row) {
|
||
if (isSending || !row || !chatLog) return;
|
||
const rows = chatLog.querySelectorAll('.message-row');
|
||
const idx = Array.from(rows).indexOf(row);
|
||
if (idx === -1 || idx >= messageHistory.length) return;
|
||
if (messageHistory[idx].role !== 'user') return;
|
||
|
||
// 丢弃该用户消息之后的所有消息和 DOM
|
||
const allRows = Array.from(rows);
|
||
for (let i = allRows.length - 1; i > idx; i--) {
|
||
allRows[i].remove();
|
||
}
|
||
messageHistory.splice(idx + 1);
|
||
|
||
const session = getActiveSession();
|
||
if (session) {
|
||
session.messages = messageHistory.slice();
|
||
session.updatedAt = Date.now();
|
||
saveSessions();
|
||
}
|
||
|
||
// 从该位置重新发送
|
||
const sendSessionId = sessionsData.activeId;
|
||
const assistantEntry = createMessage('assistant', '');
|
||
setSendingState(true);
|
||
setStatus('connecting', t('common.sending'));
|
||
|
||
abortController = new AbortController();
|
||
const payload = buildPayload();
|
||
|
||
(async () => {
|
||
let headers = { 'Content-Type': 'application/json' };
|
||
try {
|
||
const authHeader = await ensureFunctionKey();
|
||
headers = { ...headers, ...buildAuthHeaders(authHeader) };
|
||
} catch (e) {}
|
||
|
||
try {
|
||
const res = await fetch(CHAT_COMPLETIONS_ENDPOINT, {
|
||
method: 'POST',
|
||
headers,
|
||
body: JSON.stringify(payload),
|
||
signal: abortController.signal
|
||
});
|
||
if (!res.ok) throw new Error(t('chat.requestFailedStatus', { status: res.status }));
|
||
await handleStream(res, assistantEntry, sendSessionId);
|
||
setStatus('connected', t('common.done'));
|
||
} catch (e) {
|
||
if (e && e.name === 'AbortError') {
|
||
updateMessage(assistantEntry, assistantEntry.raw || t('common.stopped'), true);
|
||
setStatus('error', t('common.stopped'));
|
||
if (!assistantEntry.committed) {
|
||
assistantEntry.committed = true;
|
||
commitToSession(sendSessionId, assistantEntry.raw || '');
|
||
}
|
||
} else {
|
||
updateMessage(assistantEntry, t('chat.requestFailedStatus', { status: e.message || e }), true);
|
||
setStatus('error', t('common.failed'));
|
||
toast(t('chat.requestFailedCheck'), 'error');
|
||
}
|
||
} finally {
|
||
setSendingState(false);
|
||
abortController = null;
|
||
scrollToBottom();
|
||
}
|
||
})();
|
||
}
|
||
|
||
function createMessage(role, content, skipScroll, options) {
|
||
if (!chatLog) return null;
|
||
hideEmptyState();
|
||
const row = document.createElement('div');
|
||
row.className = `message-row ${role === 'user' ? 'user' : 'assistant'}`;
|
||
|
||
const bubble = document.createElement('div');
|
||
bubble.className = 'message-bubble';
|
||
const contentNode = document.createElement('div');
|
||
contentNode.className = 'message-content';
|
||
contentNode.textContent = content || '';
|
||
bubble.appendChild(contentNode);
|
||
row.appendChild(bubble);
|
||
|
||
chatLog.appendChild(row);
|
||
if (!skipScroll) scrollToBottom();
|
||
const entry = {
|
||
row,
|
||
contentNode,
|
||
role,
|
||
raw: content || '',
|
||
committed: false,
|
||
startedAt: Date.now(),
|
||
firstTokenAt: null,
|
||
hasThink: false,
|
||
thinkElapsed: null,
|
||
thinkAutoCollapsed: false
|
||
};
|
||
if (role === 'user') {
|
||
const editable = options && options.editable === false ? false : true;
|
||
const actions = document.createElement('div');
|
||
actions.className = 'message-actions';
|
||
if (editable) {
|
||
actions.appendChild(createActionButton(t('chat.editMessage'), t('chat.editMessageTitle'), () => editMessageByRow(row)));
|
||
}
|
||
actions.appendChild(createActionButton(t('chat.regenerate'), t('chat.regenerateTitle'), () => regenerateFromRow(row)));
|
||
row.appendChild(actions);
|
||
}
|
||
return entry;
|
||
}
|
||
|
||
function applyImageGrid(root) {
|
||
if (!root) return;
|
||
const isIgnorable = (node) => {
|
||
if (node.nodeType === Node.TEXT_NODE) {
|
||
return !node.textContent.trim();
|
||
}
|
||
return node.nodeType === Node.ELEMENT_NODE && node.tagName === 'BR';
|
||
};
|
||
|
||
const isImageLink = (node) => {
|
||
if (!node || node.nodeType !== Node.ELEMENT_NODE || node.tagName !== 'A') return false;
|
||
const children = Array.from(node.childNodes);
|
||
if (!children.length) return false;
|
||
return children.every((child) => {
|
||
if (child.nodeType === Node.TEXT_NODE) {
|
||
return !child.textContent.trim();
|
||
}
|
||
return child.nodeType === Node.ELEMENT_NODE && child.tagName === 'IMG';
|
||
});
|
||
};
|
||
|
||
const extractImageItems = (node) => {
|
||
if (!node || node.nodeType !== Node.ELEMENT_NODE) return null;
|
||
if (node.classList && node.classList.contains('img-grid')) return null;
|
||
if (node.tagName === 'IMG') {
|
||
return { items: [node], removeNode: null };
|
||
}
|
||
if (isImageLink(node)) {
|
||
return { items: [node], removeNode: null };
|
||
}
|
||
if (node.tagName === 'P') {
|
||
const items = [];
|
||
const children = Array.from(node.childNodes);
|
||
if (!children.length) return null;
|
||
for (const child of children) {
|
||
if (child.nodeType === Node.TEXT_NODE) {
|
||
if (!child.textContent.trim()) continue;
|
||
return null;
|
||
}
|
||
if (child.nodeType === Node.ELEMENT_NODE) {
|
||
if (child.tagName === 'IMG' || isImageLink(child)) {
|
||
items.push(child);
|
||
continue;
|
||
}
|
||
if (child.tagName === 'BR') continue;
|
||
return null;
|
||
}
|
||
return null;
|
||
}
|
||
if (!items.length) return null;
|
||
return { items, removeNode: node };
|
||
}
|
||
return null;
|
||
};
|
||
|
||
const wrapImagesInContainer = (container) => {
|
||
const children = Array.from(container.childNodes);
|
||
let group = [];
|
||
let groupStart = null;
|
||
let removeNodes = [];
|
||
|
||
const flush = () => {
|
||
if (group.length < 2) {
|
||
group = [];
|
||
groupStart = null;
|
||
removeNodes = [];
|
||
return;
|
||
}
|
||
const wrapper = document.createElement('div');
|
||
wrapper.className = 'img-grid';
|
||
const cols = Math.min(4, group.length);
|
||
wrapper.style.setProperty('--cols', String(cols));
|
||
if (groupStart) {
|
||
container.insertBefore(wrapper, groupStart);
|
||
} else {
|
||
container.appendChild(wrapper);
|
||
}
|
||
group.forEach((img) => wrapper.appendChild(img));
|
||
removeNodes.forEach((n) => n.parentNode && n.parentNode.removeChild(n));
|
||
group = [];
|
||
groupStart = null;
|
||
removeNodes = [];
|
||
};
|
||
|
||
children.forEach((node) => {
|
||
if (group.length && isIgnorable(node)) {
|
||
removeNodes.push(node);
|
||
return;
|
||
}
|
||
const extracted = extractImageItems(node);
|
||
if (extracted && extracted.items.length) {
|
||
if (!groupStart) groupStart = node;
|
||
group.push(...extracted.items);
|
||
if (extracted.removeNode) {
|
||
removeNodes.push(extracted.removeNode);
|
||
}
|
||
return;
|
||
}
|
||
flush();
|
||
});
|
||
flush();
|
||
};
|
||
|
||
const containers = [root, ...root.querySelectorAll('.think-content, .think-item-body, .think-rollout-body, .think-agent-items')];
|
||
containers.forEach((container) => {
|
||
if (!container || container.closest('.img-grid')) return;
|
||
if (!container.querySelector || !container.querySelector('img')) return;
|
||
wrapImagesInContainer(container);
|
||
});
|
||
}
|
||
|
||
function updateMessage(entry, content, finalize = false) {
|
||
if (!entry) return;
|
||
entry.raw = content || '';
|
||
if (!entry.contentNode) return;
|
||
if (!entry.hasThink && entry.raw.includes('<think>')) {
|
||
entry.hasThink = true;
|
||
}
|
||
let savedThinkStates = null;
|
||
if (entry.hasThink && entry.thinkAutoCollapsed) {
|
||
const blocks = entry.contentNode.querySelectorAll('.think-block[data-think="true"]');
|
||
if (blocks.length) {
|
||
savedThinkStates = Array.from(blocks).map(b => b.hasAttribute('open'));
|
||
}
|
||
}
|
||
if (finalize) {
|
||
entry.contentNode.classList.add('rendered');
|
||
setRenderedHTML(entry.contentNode, renderMarkdown(entry.raw));
|
||
} else {
|
||
if (entry.role === 'assistant') {
|
||
setRenderedHTML(entry.contentNode, renderMarkdown(entry.raw));
|
||
} else {
|
||
entry.contentNode.textContent = entry.raw;
|
||
}
|
||
}
|
||
if (entry.hasThink) {
|
||
updateThinkSummary(entry, finalize ? (entry.thinkElapsed ?? 0) : entry.thinkElapsed);
|
||
const thinkBlocks = entry.contentNode.querySelectorAll('.think-block[data-think="true"]');
|
||
thinkBlocks.forEach((block, i) => {
|
||
if (savedThinkStates && i < savedThinkStates.length) {
|
||
if (savedThinkStates[i]) {
|
||
block.setAttribute('open', '');
|
||
} else {
|
||
block.removeAttribute('open');
|
||
}
|
||
} else if (entry.thinkElapsed === null || entry.thinkElapsed === undefined) {
|
||
block.setAttribute('open', '');
|
||
} else if (!entry.thinkAutoCollapsed) {
|
||
block.removeAttribute('open');
|
||
entry.thinkAutoCollapsed = true;
|
||
}
|
||
});
|
||
}
|
||
if (entry.role === 'assistant') {
|
||
applyImageGrid(entry.contentNode);
|
||
const thinkNodes = entry.contentNode.querySelectorAll('.think-content');
|
||
thinkNodes.forEach((node) => {
|
||
node.scrollTop = node.scrollHeight;
|
||
});
|
||
enhanceBrokenImages(entry.contentNode);
|
||
if (finalize && entry.row && !entry.row.querySelector('.message-actions')) {
|
||
attachAssistantActions(entry);
|
||
}
|
||
}
|
||
scrollToBottom();
|
||
}
|
||
|
||
function enhanceBrokenImages(root) {
|
||
if (!root) return;
|
||
const images = root.querySelectorAll('img');
|
||
images.forEach((img) => {
|
||
if (img.dataset.retryBound) return;
|
||
img.dataset.retryBound = '1';
|
||
img.addEventListener('error', () => {
|
||
if (img.dataset.failed) return;
|
||
img.dataset.failed = '1';
|
||
const wrapper = document.createElement('button');
|
||
wrapper.type = 'button';
|
||
wrapper.className = 'img-retry';
|
||
wrapper.textContent = t('chat.clickRetry');
|
||
wrapper.addEventListener('click', () => {
|
||
wrapper.classList.add('loading');
|
||
const original = img.getAttribute('src') || '';
|
||
const cacheBust = original.includes('?') ? '&' : '?';
|
||
img.dataset.failed = '';
|
||
img.src = `${original}${cacheBust}t=${Date.now()}`;
|
||
});
|
||
img.replaceWith(wrapper);
|
||
});
|
||
img.addEventListener('load', () => {
|
||
if (img.dataset.failed) {
|
||
img.dataset.failed = '';
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
function updateThinkSummary(entry, elapsedSec) {
|
||
if (!entry || !entry.contentNode) return;
|
||
const summaries = entry.contentNode.querySelectorAll('.think-summary');
|
||
if (!summaries.length) return;
|
||
const text = typeof elapsedSec === 'number' ? (elapsedSec > 0 ? t('chat.thinkingSec', { sec: elapsedSec }) : t('chat.thought')) : t('chat.thinking');
|
||
summaries.forEach((node) => {
|
||
node.textContent = text;
|
||
const block = node.closest('.think-block');
|
||
if (!block) return;
|
||
if (typeof elapsedSec === 'number') {
|
||
block.removeAttribute('data-thinking');
|
||
} else {
|
||
block.setAttribute('data-thinking', 'true');
|
||
}
|
||
});
|
||
}
|
||
|
||
function buildMessages() {
|
||
return buildMessagesFrom(messageHistory);
|
||
}
|
||
|
||
function buildMessagesFrom(history) {
|
||
const payload = [];
|
||
const systemPrompt = systemInput ? systemInput.value.trim() : '';
|
||
if (systemPrompt) {
|
||
payload.push({ role: 'system', content: systemPrompt });
|
||
}
|
||
for (const msg of history) {
|
||
payload.push({ role: msg.role, content: msg.content });
|
||
}
|
||
return payload;
|
||
}
|
||
|
||
function buildPayload() {
|
||
const payload = {
|
||
model: modelValue || 'grok-3',
|
||
messages: buildMessages(),
|
||
stream: true,
|
||
temperature: Number(tempRange ? tempRange.value : 0.8),
|
||
top_p: Number(topPRange ? topPRange.value : 0.95)
|
||
};
|
||
return payload;
|
||
}
|
||
|
||
function buildPayloadFrom(history) {
|
||
const payload = {
|
||
model: modelValue || 'grok-3',
|
||
messages: buildMessagesFrom(history),
|
||
stream: true,
|
||
temperature: Number(tempRange ? tempRange.value : 0.8),
|
||
top_p: Number(topPRange ? topPRange.value : 0.95)
|
||
};
|
||
return payload;
|
||
}
|
||
|
||
function selectModel(value) {
|
||
modelValue = value;
|
||
if (modelLabel) modelLabel.textContent = value;
|
||
renderModelDropdown();
|
||
}
|
||
|
||
function renderModelDropdown() {
|
||
if (!modelDropdown) return;
|
||
modelDropdown.innerHTML = '';
|
||
for (const id of modelList) {
|
||
const opt = document.createElement('div');
|
||
opt.className = 'model-option' + (id === modelValue ? ' selected' : '');
|
||
opt.dataset.value = id;
|
||
opt.textContent = id;
|
||
modelDropdown.appendChild(opt);
|
||
}
|
||
}
|
||
|
||
function toggleModelDropdown(show) {
|
||
if (!modelDropdown || !modelChip) return;
|
||
if (typeof show === 'boolean') {
|
||
modelDropdown.classList.toggle('hidden', !show);
|
||
modelChip.classList.toggle('open', show);
|
||
return;
|
||
}
|
||
const visible = !modelDropdown.classList.contains('hidden');
|
||
modelDropdown.classList.toggle('hidden', visible);
|
||
modelChip.classList.toggle('open', !visible);
|
||
}
|
||
|
||
async function loadModels() {
|
||
if (!modelDropdown) return;
|
||
const fallback = ['grok-4.1-fast', 'grok-4', 'grok-3', 'grok-3-mini', 'grok-3-thinking', 'grok-4.20-beta', 'grok-imagine-1.0-fast'];
|
||
const preferred = 'grok-4.20-beta';
|
||
try {
|
||
const res = await fetch('/v1/models', { cache: 'no-store' });
|
||
if (!res.ok) throw new Error('models fetch failed');
|
||
const data = await res.json();
|
||
const items = Array.isArray(data && data.data) ? data.data : [];
|
||
const ids = items
|
||
.map(item => item && item.id)
|
||
.filter(Boolean)
|
||
.filter((id) => {
|
||
const name = String(id);
|
||
if (name.startsWith('grok-imagine')) {
|
||
return name === 'grok-imagine-1.0-fast';
|
||
}
|
||
return !name.includes('video');
|
||
});
|
||
modelList = ids.length ? ids : fallback;
|
||
} catch (e) {
|
||
modelList = fallback;
|
||
}
|
||
if (modelList.includes(preferred)) {
|
||
modelValue = preferred;
|
||
} else {
|
||
modelValue = modelList[modelList.length - 1] || preferred;
|
||
}
|
||
if (modelLabel) modelLabel.textContent = modelValue;
|
||
renderModelDropdown();
|
||
restoreSessionModel();
|
||
}
|
||
|
||
function showAttachmentBadge() {
|
||
if (!fileBadge || !fileName) return;
|
||
if (attachment) {
|
||
fileName.textContent = attachment.name;
|
||
fileBadge.classList.remove('hidden');
|
||
} else {
|
||
fileBadge.classList.add('hidden');
|
||
fileName.textContent = '';
|
||
}
|
||
}
|
||
|
||
function clearAttachment() {
|
||
attachment = null;
|
||
if (fileInput) fileInput.value = '';
|
||
showAttachmentBadge();
|
||
}
|
||
|
||
function readFileAsDataUrl(file) {
|
||
return new Promise((resolve, reject) => {
|
||
const reader = new FileReader();
|
||
reader.onload = () => resolve(reader.result);
|
||
reader.onerror = () => reject(new Error(t('common.fileReadFailed')));
|
||
reader.readAsDataURL(file);
|
||
});
|
||
}
|
||
|
||
async function handleFileSelect(file) {
|
||
if (!file) return;
|
||
try {
|
||
const dataUrl = await readFileAsDataUrl(file);
|
||
attachment = {
|
||
name: file.name || 'file',
|
||
data: dataUrl
|
||
};
|
||
showAttachmentBadge();
|
||
} catch (e) {
|
||
toast(t('common.fileReadFailed'), 'error');
|
||
}
|
||
}
|
||
|
||
function createActionButton(label, title, onClick) {
|
||
const btn = document.createElement('button');
|
||
btn.className = 'action-btn';
|
||
btn.type = 'button';
|
||
btn.textContent = label;
|
||
if (title) btn.title = title;
|
||
if (onClick) btn.addEventListener('click', onClick);
|
||
return btn;
|
||
}
|
||
|
||
function attachAssistantActions(entry) {
|
||
if (!entry || !entry.row) return;
|
||
const actions = document.createElement('div');
|
||
actions.className = 'message-actions';
|
||
|
||
const retryBtn = createActionButton(t('common.retry'), t('chat.retryTitle'), () => retryLast());
|
||
const editBtn = createActionButton(t('chat.editAnswer'), t('chat.editAnswerTitle'), () => editMessageByRow(entry.row));
|
||
const copyBtn = createActionButton(t('chat.copyAnswer'), t('chat.copyAnswerTitle'), () => copyToClipboard(entry.raw || ''));
|
||
const feedbackBtn = createActionButton(t('chat.feedback'), t('chat.feedbackTitle'), () => {
|
||
window.open(feedbackUrl, '_blank', 'noopener');
|
||
});
|
||
|
||
actions.appendChild(retryBtn);
|
||
actions.appendChild(editBtn);
|
||
actions.appendChild(copyBtn);
|
||
actions.appendChild(feedbackBtn);
|
||
entry.row.appendChild(actions);
|
||
}
|
||
|
||
async function copyToClipboard(text) {
|
||
if (!text) {
|
||
toast(t('chat.noContentToCopy'), 'error');
|
||
return;
|
||
}
|
||
try {
|
||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||
await navigator.clipboard.writeText(text);
|
||
} else {
|
||
const temp = document.createElement('textarea');
|
||
temp.value = text;
|
||
temp.style.position = 'fixed';
|
||
temp.style.opacity = '0';
|
||
document.body.appendChild(temp);
|
||
temp.select();
|
||
document.execCommand('copy');
|
||
document.body.removeChild(temp);
|
||
}
|
||
toast(t('common.copied'), 'success');
|
||
} catch (e) {
|
||
toast(t('common.copyFailed'), 'error');
|
||
}
|
||
}
|
||
|
||
async function retryLast() {
|
||
if (isSending) return;
|
||
if (!messageHistory.length) return;
|
||
let lastUserIndex = -1;
|
||
for (let i = messageHistory.length - 1; i >= 0; i -= 1) {
|
||
if (messageHistory[i].role === 'user') {
|
||
lastUserIndex = i;
|
||
break;
|
||
}
|
||
}
|
||
if (lastUserIndex === -1) {
|
||
toast(t('chat.noChatToRetry'), 'error');
|
||
return;
|
||
}
|
||
const historySlice = messageHistory.slice(0, lastUserIndex + 1);
|
||
const retrySessionId = sessionsData.activeId;
|
||
const assistantEntry = createMessage('assistant', '');
|
||
setSendingState(true);
|
||
setStatus('connecting', t('common.sending'));
|
||
|
||
abortController = new AbortController();
|
||
const payload = buildPayloadFrom(historySlice);
|
||
|
||
let headers = { 'Content-Type': 'application/json' };
|
||
try {
|
||
const authHeader = await ensureFunctionKey();
|
||
headers = { ...headers, ...buildAuthHeaders(authHeader) };
|
||
} catch (e) {
|
||
// ignore auth helper failures
|
||
}
|
||
|
||
try {
|
||
const res = await fetch(CHAT_COMPLETIONS_ENDPOINT, {
|
||
method: 'POST',
|
||
headers,
|
||
body: JSON.stringify(payload),
|
||
signal: abortController.signal
|
||
});
|
||
|
||
if (!res.ok) {
|
||
throw new Error(t('chat.requestFailedStatus', { status: res.status }));
|
||
}
|
||
|
||
await handleStream(res, assistantEntry, retrySessionId);
|
||
setStatus('connected', t('common.done'));
|
||
} catch (e) {
|
||
updateMessage(assistantEntry, t('chat.requestFailedStatus', { status: e.message || e }), true);
|
||
setStatus('error', t('common.failed'));
|
||
toast(t('chat.requestFailedCheck'), 'error');
|
||
} finally {
|
||
setSendingState(false);
|
||
abortController = null;
|
||
scrollToBottom();
|
||
}
|
||
}
|
||
|
||
async function sendMessage() {
|
||
if (isSending) return;
|
||
const prompt = promptInput ? promptInput.value.trim() : '';
|
||
if (!prompt && !attachment) {
|
||
toast(t('common.enterContent'), 'error');
|
||
return;
|
||
}
|
||
|
||
let displayText = prompt || '';
|
||
if (attachment) {
|
||
const label = t('chat.fileLabel') + ' ' + attachment.name;
|
||
displayText = displayText ? `${displayText}\n${label}` : label;
|
||
}
|
||
|
||
createMessage('user', displayText, false, { editable: !attachment });
|
||
|
||
let content = prompt;
|
||
if (attachment) {
|
||
const blocks = [];
|
||
if (prompt) {
|
||
blocks.push({ type: 'text', text: prompt });
|
||
}
|
||
blocks.push({ type: 'file', file: { file_data: attachment.data } });
|
||
content = blocks;
|
||
}
|
||
|
||
messageHistory.push({
|
||
role: 'user',
|
||
content,
|
||
display: displayText,
|
||
hasAttachment: !!attachment,
|
||
attachmentName: attachment ? attachment.name : ''
|
||
});
|
||
trimMessageHistory();
|
||
if (promptInput) promptInput.value = '';
|
||
clearAttachment();
|
||
syncCurrentSession();
|
||
syncSessionModel();
|
||
updateSessionTitle(getActiveSession());
|
||
saveSessions();
|
||
renderSessionList();
|
||
|
||
const sendSessionId = sessionsData.activeId;
|
||
const assistantEntry = createMessage('assistant', '');
|
||
setSendingState(true);
|
||
setStatus('connecting', t('common.sending'));
|
||
|
||
abortController = new AbortController();
|
||
const payload = buildPayload();
|
||
|
||
let headers = { 'Content-Type': 'application/json' };
|
||
try {
|
||
const authHeader = await ensureFunctionKey();
|
||
headers = { ...headers, ...buildAuthHeaders(authHeader) };
|
||
} catch (e) {
|
||
// ignore auth helper failures
|
||
}
|
||
|
||
try {
|
||
const res = await fetch(CHAT_COMPLETIONS_ENDPOINT, {
|
||
method: 'POST',
|
||
headers,
|
||
body: JSON.stringify(payload),
|
||
signal: abortController.signal
|
||
});
|
||
|
||
if (!res.ok) {
|
||
throw new Error(t('chat.requestFailedStatus', { status: res.status }));
|
||
}
|
||
|
||
await handleStream(res, assistantEntry, sendSessionId);
|
||
setStatus('connected', t('common.done'));
|
||
} catch (e) {
|
||
if (e && e.name === 'AbortError') {
|
||
updateMessage(assistantEntry, assistantEntry.raw || t('common.stopped'), true);
|
||
if (assistantEntry.hasThink) {
|
||
const elapsed = assistantEntry.thinkElapsed || Math.max(1, Math.round((Date.now() - assistantEntry.startedAt) / 1000));
|
||
updateThinkSummary(assistantEntry, elapsed);
|
||
}
|
||
setStatus('error', t('common.stopped'));
|
||
if (!assistantEntry.committed) {
|
||
assistantEntry.committed = true;
|
||
commitToSession(sendSessionId, assistantEntry.raw || '');
|
||
}
|
||
} else {
|
||
updateMessage(assistantEntry, t('chat.requestFailedStatus', { status: e.message || e }), true);
|
||
setStatus('error', t('common.failed'));
|
||
toast(t('chat.requestFailedCheck'), 'error');
|
||
}
|
||
} finally {
|
||
setSendingState(false);
|
||
abortController = null;
|
||
scrollToBottom();
|
||
}
|
||
}
|
||
|
||
function commitToSession(sessionId, assistantText) {
|
||
const session = sessionsData.sessions.find(s => s.id === sessionId);
|
||
if (!session) return;
|
||
session.messages.push({ role: 'assistant', content: assistantText });
|
||
if (session.messages.length > MAX_CONTEXT_MESSAGES) {
|
||
session.messages = session.messages.slice(-MAX_CONTEXT_MESSAGES);
|
||
}
|
||
session.updatedAt = Date.now();
|
||
updateSessionTitle(session);
|
||
if (sessionsData.activeId === sessionId) {
|
||
messageHistory = session.messages.slice();
|
||
trimMessageHistory();
|
||
} else {
|
||
session.unread = true;
|
||
}
|
||
saveSessions();
|
||
renderSessionList();
|
||
}
|
||
|
||
async function handleStream(res, assistantEntry, targetSessionId) {
|
||
activeStreamInfo = { sessionId: targetSessionId, entry: assistantEntry };
|
||
const reader = res.body.getReader();
|
||
const decoder = new TextDecoder('utf-8');
|
||
let buffer = '';
|
||
let assistantText = '';
|
||
|
||
try {
|
||
while (true) {
|
||
const { value, done } = await reader.read();
|
||
if (done) break;
|
||
buffer += decoder.decode(value, { stream: true });
|
||
const parts = buffer.split('\n\n');
|
||
buffer = parts.pop() || '';
|
||
for (const part of parts) {
|
||
const lines = part.split('\n');
|
||
for (const line of lines) {
|
||
const trimmed = line.trim();
|
||
if (!trimmed.startsWith('data:')) continue;
|
||
const payload = trimmed.slice(5).trim();
|
||
if (!payload) continue;
|
||
if (payload === '[DONE]') {
|
||
updateMessage(assistantEntry, assistantText, true);
|
||
if (assistantEntry.hasThink) {
|
||
const elapsed = assistantEntry.thinkElapsed || Math.max(1, Math.round((Date.now() - assistantEntry.startedAt) / 1000));
|
||
updateThinkSummary(assistantEntry, elapsed);
|
||
}
|
||
assistantEntry.committed = true;
|
||
commitToSession(targetSessionId, assistantText);
|
||
return;
|
||
}
|
||
try {
|
||
const json = JSON.parse(payload);
|
||
const delta = json && json.choices && json.choices[0] && json.choices[0].delta
|
||
? json.choices[0].delta.content
|
||
: '';
|
||
if (delta) {
|
||
assistantText += delta;
|
||
if (!assistantEntry.firstTokenAt) {
|
||
assistantEntry.firstTokenAt = Date.now();
|
||
}
|
||
if (!assistantEntry.hasThink && assistantText.includes('<think>')) {
|
||
assistantEntry.hasThink = true;
|
||
assistantEntry.thinkStartAt = Date.now();
|
||
assistantEntry.thinkElapsed = null;
|
||
updateThinkSummary(assistantEntry, null);
|
||
}
|
||
if (assistantEntry.hasThink && assistantEntry.thinkStartAt && assistantEntry.thinkElapsed === null) {
|
||
if (assistantText.includes('</think>')) {
|
||
assistantEntry.thinkElapsed = Math.max(1, Math.round((Date.now() - assistantEntry.thinkStartAt) / 1000));
|
||
updateThinkSummary(assistantEntry, assistantEntry.thinkElapsed);
|
||
}
|
||
}
|
||
if (sessionsData.activeId === targetSessionId) {
|
||
updateMessage(assistantEntry, assistantText, false);
|
||
}
|
||
}
|
||
} catch (e) {
|
||
// ignore parse errors
|
||
}
|
||
}
|
||
}
|
||
}
|
||
updateMessage(assistantEntry, assistantText, true);
|
||
if (assistantEntry.hasThink) {
|
||
const elapsed = assistantEntry.thinkElapsed || Math.max(1, Math.round((Date.now() - assistantEntry.startedAt) / 1000));
|
||
updateThinkSummary(assistantEntry, elapsed);
|
||
}
|
||
assistantEntry.committed = true;
|
||
commitToSession(targetSessionId, assistantText);
|
||
} finally {
|
||
activeStreamInfo = null;
|
||
}
|
||
}
|
||
|
||
function toggleSettings(show) {
|
||
if (!settingsPanel) return;
|
||
if (typeof show === 'boolean') {
|
||
settingsPanel.classList.toggle('hidden', !show);
|
||
return;
|
||
}
|
||
settingsPanel.classList.toggle('hidden');
|
||
}
|
||
|
||
function restoreSidebarState() {
|
||
try {
|
||
const raw = localStorage.getItem(SIDEBAR_STATE_KEY);
|
||
setSidebarCollapsed(raw === '1');
|
||
} catch (e) {}
|
||
}
|
||
|
||
function bindEvents() {
|
||
if (tempRange) tempRange.addEventListener('input', updateRangeValues);
|
||
if (topPRange) topPRange.addEventListener('input', updateRangeValues);
|
||
if (modelChip) {
|
||
modelChip.addEventListener('click', (event) => {
|
||
if (event.target.closest('.model-dropdown')) return;
|
||
event.stopPropagation();
|
||
toggleModelDropdown();
|
||
});
|
||
}
|
||
if (modelDropdown) {
|
||
modelDropdown.addEventListener('click', (event) => {
|
||
const opt = event.target.closest('.model-option');
|
||
if (!opt) return;
|
||
event.stopPropagation();
|
||
selectModel(opt.dataset.value);
|
||
toggleModelDropdown(false);
|
||
});
|
||
}
|
||
if (sendBtn) sendBtn.addEventListener('click', sendMessage);
|
||
if (settingsToggle) {
|
||
settingsToggle.addEventListener('click', (event) => {
|
||
event.stopPropagation();
|
||
toggleSettings();
|
||
});
|
||
}
|
||
document.addEventListener('click', (event) => {
|
||
if (settingsPanel && !settingsPanel.classList.contains('hidden')) {
|
||
if (!settingsPanel.contains(event.target) && !(settingsToggle && settingsToggle.contains(event.target))) {
|
||
toggleSettings(false);
|
||
}
|
||
}
|
||
if (modelDropdown && !modelDropdown.classList.contains('hidden')) {
|
||
if (!(modelChip && modelChip.contains(event.target))) {
|
||
toggleModelDropdown(false);
|
||
}
|
||
}
|
||
});
|
||
if (promptInput) {
|
||
let composing = false;
|
||
promptInput.addEventListener('compositionstart', () => {
|
||
composing = true;
|
||
});
|
||
promptInput.addEventListener('compositionend', () => {
|
||
composing = false;
|
||
});
|
||
promptInput.addEventListener('keydown', (event) => {
|
||
if (event.key === 'Enter' && !event.shiftKey) {
|
||
if (composing || event.isComposing) return;
|
||
event.preventDefault();
|
||
sendMessage();
|
||
}
|
||
});
|
||
}
|
||
if (attachBtn && fileInput) {
|
||
attachBtn.addEventListener('click', () => fileInput.click());
|
||
fileInput.addEventListener('change', () => {
|
||
if (fileInput.files && fileInput.files[0]) {
|
||
handleFileSelect(fileInput.files[0]);
|
||
}
|
||
});
|
||
}
|
||
if (fileRemoveBtn) {
|
||
fileRemoveBtn.addEventListener('click', clearAttachment);
|
||
}
|
||
if (newChatBtn) {
|
||
newChatBtn.addEventListener('click', createSession);
|
||
}
|
||
if (collapseSidebarBtn) {
|
||
collapseSidebarBtn.addEventListener('click', toggleSidebar);
|
||
}
|
||
if (sidebarExpandBtn) {
|
||
sidebarExpandBtn.addEventListener('click', openSidebar);
|
||
}
|
||
if (sidebarToggle) {
|
||
sidebarToggle.addEventListener('click', toggleSidebar);
|
||
}
|
||
if (sidebarOverlay) {
|
||
sidebarOverlay.addEventListener('click', closeSidebar);
|
||
}
|
||
}
|
||
|
||
updateRangeValues();
|
||
loadModels();
|
||
bindEvents();
|
||
restoreSidebarState();
|
||
|
||
(async () => {
|
||
try {
|
||
const authResult = await ensureFunctionKey();
|
||
if (authResult === null) {
|
||
window.location.href = '/login';
|
||
return;
|
||
}
|
||
} catch (e) {
|
||
window.location.href = '/login';
|
||
return;
|
||
}
|
||
loadSessions();
|
||
})();
|
||
})();
|