myagent-ai 1.8.7 → 1.8.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/web/ui/chat/chat_main.js +48 -6
- package/web/ui/chat/groupchat.js +12 -0
package/package.json
CHANGED
package/web/ui/chat/chat_main.js
CHANGED
|
@@ -1643,19 +1643,30 @@ async function selectSession(id) {
|
|
|
1643
1643
|
const mySeq = ++state._sessionLoadSeq;
|
|
1644
1644
|
const session = state.sessions.find(s => s.id === id);
|
|
1645
1645
|
document.getElementById('headerTitle').textContent = session ? session.name : formatSessionName(id);
|
|
1646
|
-
document.getElementById('welcomeCard').style.display = 'none';
|
|
1647
1646
|
renderSessions();
|
|
1648
1647
|
|
|
1648
|
+
// 清空聊天区域,显示加载状态
|
|
1649
|
+
var _mi = document.getElementById('messagesInner');
|
|
1650
|
+
if (_mi) _mi.innerHTML = '<div style="padding:30px;text-align:center;color:var(--text3)">加载消息中...</div>';
|
|
1651
|
+
// 隐藏 welcome card
|
|
1652
|
+
var wc = document.getElementById('welcomeCard');
|
|
1653
|
+
if (wc) wc.style.display = 'none';
|
|
1654
|
+
|
|
1649
1655
|
// Load messages
|
|
1650
1656
|
try {
|
|
1651
1657
|
const data = await api(`/api/session/messages?sid=${encodeURIComponent(id)}`);
|
|
1652
1658
|
// 竞态保护:如果在此请求期间又切换了其他会话,丢弃过期结果
|
|
1653
1659
|
if (mySeq !== state._sessionLoadSeq) return;
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1660
|
+
// 验证并过滤数据
|
|
1661
|
+
state.messages = (Array.isArray(data) ? data : []).filter(function(m) {
|
|
1662
|
+
return m && (m.role === 'user' || m.role === 'assistant');
|
|
1663
|
+
}).map(function(m) {
|
|
1664
|
+
return {
|
|
1665
|
+
role: m.role || 'assistant',
|
|
1666
|
+
content: (m.content != null) ? String(m.content) : '',
|
|
1667
|
+
time: m.time || m.created_at || '',
|
|
1668
|
+
};
|
|
1669
|
+
});
|
|
1659
1670
|
} catch (e) {
|
|
1660
1671
|
if (mySeq !== state._sessionLoadSeq) return;
|
|
1661
1672
|
state.messages = [];
|
|
@@ -1786,6 +1797,19 @@ async function clearCurrentChat() {
|
|
|
1786
1797
|
|
|
1787
1798
|
// ── Messages ──
|
|
1788
1799
|
function renderMessages() {
|
|
1800
|
+
try {
|
|
1801
|
+
_renderMessagesInner();
|
|
1802
|
+
} catch (e) {
|
|
1803
|
+
console.error('renderMessages crashed:', e);
|
|
1804
|
+
// 兜底:至少显示消息数量,不要白屏
|
|
1805
|
+
const container = document.getElementById('messagesInner');
|
|
1806
|
+
if (container) {
|
|
1807
|
+
container.innerHTML = '<div style="padding:20px;text-align:center;color:var(--danger)">消息渲染出错,请刷新页面重试</div>';
|
|
1808
|
+
}
|
|
1809
|
+
}
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
function _renderMessagesInner() {
|
|
1789
1813
|
if (currentView === 'group') {
|
|
1790
1814
|
renderGroupMessages();
|
|
1791
1815
|
return;
|
|
@@ -1940,7 +1964,14 @@ function renderMessages() {
|
|
|
1940
1964
|
|
|
1941
1965
|
function renderMarkdown(text) {
|
|
1942
1966
|
if (!text) return '';
|
|
1967
|
+
if (typeof text !== 'string') text = String(text);
|
|
1943
1968
|
|
|
1969
|
+
// ── 安全限制:超长文本截断,防止正则卡死 ──
|
|
1970
|
+
if (text.length > 80000) {
|
|
1971
|
+
text = text.substring(0, 80000) + '\n\n... [内容过长,已截断]';
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
try {
|
|
1944
1975
|
// ── Step 1: 提取代码块,用占位符替换(避免后续 HTML 转义影响代码块) ──
|
|
1945
1976
|
const codeBlocks = [];
|
|
1946
1977
|
text = text.replace(/```(\w*)\n([\s\S]*?)```/g, (_, lang, code) => {
|
|
@@ -1952,6 +1983,12 @@ function renderMarkdown(text) {
|
|
|
1952
1983
|
return placeholder;
|
|
1953
1984
|
});
|
|
1954
1985
|
|
|
1986
|
+
// ── Step 1.5: 处理未闭合的代码块(```开头但没有结尾) ──
|
|
1987
|
+
// 如果还有残留的 ```,将其转义为普通文本
|
|
1988
|
+
text = text.replace(/```(\w*)/g, function(match, lang) {
|
|
1989
|
+
return escapeHtml(match);
|
|
1990
|
+
});
|
|
1991
|
+
|
|
1955
1992
|
// ── Step 2: 转义 HTML 特殊字符,防止 XSS 和排版破坏 ──
|
|
1956
1993
|
text = escapeHtml(text);
|
|
1957
1994
|
|
|
@@ -1985,6 +2022,11 @@ function renderMarkdown(text) {
|
|
|
1985
2022
|
}
|
|
1986
2023
|
|
|
1987
2024
|
return text;
|
|
2025
|
+
} catch (e) {
|
|
2026
|
+
console.error('renderMarkdown error:', e);
|
|
2027
|
+
// 兜底:返回纯转义文本
|
|
2028
|
+
return '<p>' + escapeHtml(typeof text === 'string' ? text : String(text)) + '</p>';
|
|
2029
|
+
}
|
|
1988
2030
|
}
|
|
1989
2031
|
|
|
1990
2032
|
// 高效的 HTML 转义(不创建 DOM 元素,避免大文本时性能问题)
|
package/web/ui/chat/groupchat.js
CHANGED
|
@@ -212,6 +212,18 @@ function exitGroupChat() {
|
|
|
212
212
|
// ══════════════════════════════════════════════════════
|
|
213
213
|
|
|
214
214
|
function renderGroupMessages() {
|
|
215
|
+
try {
|
|
216
|
+
_renderGroupMessagesInner();
|
|
217
|
+
} catch (e) {
|
|
218
|
+
console.error('renderGroupMessages crashed:', e);
|
|
219
|
+
var container = document.getElementById('messagesInner');
|
|
220
|
+
if (container) {
|
|
221
|
+
container.innerHTML = '<div style="padding:20px;text-align:center;color:var(--danger)">群聊消息渲染出错,请刷新页面重试</div>';
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function _renderGroupMessagesInner() {
|
|
215
227
|
var container = document.getElementById('messagesInner');
|
|
216
228
|
var html = '';
|
|
217
229
|
|