|
|
|
function saveAuthToken(token) { |
|
const expiryTime = new Date().getTime() + (24 * 60 * 60 * 1000); |
|
localStorage.setItem('authToken', token); |
|
localStorage.setItem('authTokenExpiry', expiryTime); |
|
} |
|
|
|
function getAuthToken() { |
|
const token = localStorage.getItem('authToken'); |
|
const expiry = localStorage.getItem('authTokenExpiry'); |
|
|
|
if (!token || !expiry) { |
|
return null; |
|
} |
|
|
|
if (new Date().getTime() > parseInt(expiry)) { |
|
localStorage.removeItem('authToken'); |
|
localStorage.removeItem('authTokenExpiry'); |
|
return null; |
|
} |
|
|
|
return token; |
|
} |
|
|
|
|
|
function showMessage(elementId, text, isError = false) { |
|
const msg = document.getElementById(elementId); |
|
msg.className = `message ${isError ? 'error' : 'success'}`; |
|
msg.textContent = text; |
|
} |
|
|
|
function showGlobalMessage(text, isError = false) { |
|
showMessage('message', text, isError); |
|
|
|
setTimeout(() => { |
|
const msg = document.getElementById('message'); |
|
msg.textContent = ''; |
|
msg.className = 'message'; |
|
}, 3000); |
|
} |
|
|
|
|
|
function initializeTokenHandling(inputId) { |
|
document.addEventListener('DOMContentLoaded', () => { |
|
const authToken = getAuthToken(); |
|
if (authToken) { |
|
document.getElementById(inputId).value = authToken; |
|
} |
|
}); |
|
|
|
document.getElementById(inputId).addEventListener('change', (e) => { |
|
if (e.target.value) { |
|
saveAuthToken(e.target.value); |
|
} else { |
|
localStorage.removeItem('authToken'); |
|
localStorage.removeItem('authTokenExpiry'); |
|
} |
|
}); |
|
} |
|
|
|
|
|
async function makeAuthenticatedRequest(url, options = {}) { |
|
const tokenId = options.tokenId || 'authToken'; |
|
const token = document.getElementById(tokenId).value; |
|
|
|
if (!token) { |
|
showGlobalMessage('请输入 AUTH_TOKEN', true); |
|
return null; |
|
} |
|
|
|
const defaultOptions = { |
|
method: 'POST', |
|
headers: { |
|
'Authorization': `Bearer ${token}`, |
|
'Content-Type': 'application/json' |
|
} |
|
}; |
|
|
|
try { |
|
const response = await fetch(url, { ...defaultOptions, ...options }); |
|
|
|
if (!response.ok) { |
|
throw new Error(`HTTP error! status: ${response.status}`); |
|
} |
|
|
|
return await response.json(); |
|
} catch (error) { |
|
showGlobalMessage(`请求失败: ${error.message}`, true); |
|
return null; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function parseBooleanFromString(str, defaultValue = null) { |
|
if (typeof str !== 'string') { |
|
return defaultValue; |
|
} |
|
|
|
const lowercaseStr = str.toLowerCase().trim(); |
|
|
|
if (lowercaseStr === 'true' || lowercaseStr === '1') { |
|
return true; |
|
} else if (lowercaseStr === 'false' || lowercaseStr === '0') { |
|
return false; |
|
} else { |
|
return defaultValue; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function parseStringFromBoolean(value, defaultValue = null) { |
|
if (typeof value !== 'boolean') { |
|
return defaultValue; |
|
} |
|
|
|
return value ? 'true' : 'false'; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function parsePrompt(promptStr) { |
|
if (!promptStr) return []; |
|
|
|
const messages = []; |
|
const lines = promptStr.split('\n'); |
|
let currentRole = ''; |
|
let currentContent = ''; |
|
|
|
const roleMap = { |
|
'BEGIN_SYSTEM': 'system', |
|
'BEGIN_USER': 'user', |
|
'BEGIN_ASSISTANT': 'assistant' |
|
}; |
|
|
|
for (let i = 0; i < lines.length; i++) { |
|
const line = lines[i]; |
|
|
|
|
|
let foundRole = false; |
|
for (const [marker, role] of Object.entries(roleMap)) { |
|
if (line.includes(marker)) { |
|
|
|
if (currentRole && currentContent.trim()) { |
|
messages.push({ |
|
role: currentRole, |
|
content: currentContent.trim() |
|
}); |
|
} |
|
|
|
currentRole = role; |
|
currentContent = ''; |
|
foundRole = true; |
|
break; |
|
} |
|
} |
|
|
|
|
|
if (!foundRole && !line.includes('END_')) { |
|
currentContent += line + '\n'; |
|
} |
|
} |
|
|
|
|
|
if (currentRole && currentContent.trim()) { |
|
messages.push({ |
|
role: currentRole, |
|
content: currentContent.trim() |
|
}); |
|
} |
|
|
|
return messages; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
function formatPromptToTable(messages) { |
|
if (!messages || messages.length === 0) { |
|
return '<p>无对话内容</p>'; |
|
} |
|
|
|
const roleLabels = { |
|
'system': '系统', |
|
'user': '用户', |
|
'assistant': '助手' |
|
}; |
|
|
|
function escapeHtml(content) { |
|
|
|
const escaped = content |
|
.replace(/&/g, '&') |
|
.replace(/</g, '<') |
|
.replace(/>/g, '>') |
|
.replace(/"/g, '"') |
|
.replace(/'/g, '''); |
|
|
|
|
|
|
|
return escaped; |
|
} |
|
|
|
return `<table class="message-table"><thead><tr><th>角色</th><th>内容</th></tr></thead><tbody>${messages.map(msg => `<tr><td>${roleLabels[msg.role] || msg.role}</td><td>${escapeHtml(msg.content).replace(/\n/g, '<br>')}</td></tr>`).join('')}</tbody></table>`; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
function showPromptModal(promptStr) { |
|
try { |
|
const modal = document.getElementById('promptModal'); |
|
const content = document.getElementById('promptContent'); |
|
|
|
if (!modal || !content) { |
|
console.error('Modal elements not found'); |
|
return; |
|
} |
|
|
|
const messages = parsePrompt(promptStr); |
|
content.innerHTML = formatPromptToTable(messages); |
|
modal.style.display = 'block'; |
|
} catch (e) { |
|
console.error('显示prompt对话框失败:', e); |
|
console.error('原始prompt:', promptStr); |
|
} |
|
} |