mirror of
https://github.com/wisdgod/cursor-api.git
synced 2025-10-07 23:50:06 +08:00
v0.1.3-rc.5.2-pre
This commit is contained in:
114
static/shared.js
114
static/shared.js
@@ -184,120 +184,6 @@ function parseStringFromBoolean(value, defaultValue = null) {
|
||||
return value ? 'true' : 'false';
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析对话内容
|
||||
* @param {string} promptStr - 原始prompt字符串
|
||||
* @returns {Array<{role: string, content: string}>} 解析后的对话数组
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果不是角色标记行且不是END标记行,则添加到当前内容
|
||||
if (!foundRole && !line.includes('END_')) {
|
||||
currentContent += line + '\n';
|
||||
}
|
||||
}
|
||||
|
||||
// 添加最后一条消息
|
||||
if (currentRole && currentContent.trim()) {
|
||||
messages.push({
|
||||
role: currentRole,
|
||||
content: currentContent.trim()
|
||||
});
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化对话内容为HTML表格
|
||||
* @param {Array<{role: string, content: string}>} messages - 对话消息数组
|
||||
* @returns {string} HTML表格字符串
|
||||
*/
|
||||
function formatPromptToTable(messages) {
|
||||
if (!messages || messages.length === 0) {
|
||||
return '<p>无对话内容</p>';
|
||||
}
|
||||
|
||||
const roleLabels = {
|
||||
'system': '系统',
|
||||
'user': '用户',
|
||||
'assistant': '助手'
|
||||
};
|
||||
|
||||
function escapeHtml(content) {
|
||||
// 先转义HTML特殊字符
|
||||
const escaped = content
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
|
||||
// 将HTML标签文本用引号包裹,使其更易读
|
||||
// return escaped.replace(/<(\/?[^>]+)>/g, '"<$1>"');
|
||||
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>`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全地显示prompt对话框
|
||||
* @param {string} promptStr - 原始prompt字符串
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将会员类型代码转换为显示名称
|
||||
* @param {string|null} type - 会员类型代码,如 'free_trial', 'pro', 'free', 'enterprise' 等
|
||||
|
Reference in New Issue
Block a user