优化:不传 tool 消息,tool card 展开时按需加载内容
彻底不传 role=tool 消息,API 体积大幅缩小。 sessions.js: - GET /:id:过滤掉 tool 消息;但为 assistant 消息附加 tool_call_msg_ids 映射 (tool_call.id → message.id),供前端懒加载用 - GET /:id/messages/:mid:返回单条消息完整内容(供懒加载调用) tasks.js: - renderDetail:构建并存储 toolCallMsgIds 映射 - 新增 loadToolCardContent():按 tool_call_id 懒加载对应 tool 消息 content - tool card 的 <details> 展开时自动触发懒加载 - SSE tool_result 路径完全不受影响(仍直接内联 liveImageUrl) 效果:初始加载仅传文本,tool 内容(及图片)仅在展开 tool 卡时才按需获取。
This commit is contained in:
@@ -458,6 +458,8 @@ async function renderDetail(detail) {
|
||||
state.streamText = '';
|
||||
state.turnEl = null;
|
||||
state.toolCards = {};
|
||||
// tool_call_id → message id 映射(从 assistant 消息的 tool_call_msg_ids 字段构建)
|
||||
state.toolCallMsgIds = {};
|
||||
// 切换会话后重置滚动跟随状态
|
||||
userPaused = false;
|
||||
state.streaming = false;
|
||||
@@ -469,6 +471,10 @@ async function renderDetail(detail) {
|
||||
updateScrollBottomBtn(); // 打开仍在运行中的会话时,立即恢复右下角转圈
|
||||
$('chat-title').textContent = s.title || s.id;
|
||||
for (const m of detail.messages) {
|
||||
// 收集 tool_call_msg_ids(assistant 消息携带的 tool_call_id → message_id 映射)
|
||||
if (m.role === 'assistant' && m.tool_call_msg_ids) {
|
||||
Object.assign(state.toolCallMsgIds, m.tool_call_msg_ids);
|
||||
}
|
||||
const node = msgNode(m, state.toolCards);
|
||||
if (node) box.appendChild(node);
|
||||
if (m.role === 'assistant') appendToolCardsFromAssistant(m, box);
|
||||
@@ -476,10 +482,48 @@ async function renderDetail(detail) {
|
||||
if (!detail.messages.length) {
|
||||
box.innerHTML = '<div class="empty-tip">描述你的需求,AI 会和你讨论并直接干活。</div>';
|
||||
}
|
||||
// 事件委托:tool card 的 <details> 展开时懒加载内容
|
||||
box.addEventListener('toggle', e => {
|
||||
const details = e.target.closest('details');
|
||||
if (!details) return;
|
||||
const card = details.closest('.tool-card');
|
||||
if (!card) return;
|
||||
const toolCallId = card.id.replace(/^tool-/, '');
|
||||
const pre = card.querySelector('.tool-result');
|
||||
// 内容为空或"执行中…"才需要加载(避免重复加载)
|
||||
if (!pre || pre.textContent === '执行中…') {
|
||||
loadToolCardContent(card, toolCallId);
|
||||
}
|
||||
}, true);
|
||||
scrollBottom();
|
||||
loadFiles();
|
||||
}
|
||||
|
||||
// 懒加载 tool card 内容:按需从服务端获取 tool 消息完整 content(含 base64 图片)
|
||||
async function loadToolCardContent(card, toolCallId) {
|
||||
const msgId = state.toolCallMsgIds && state.toolCallMsgIds[toolCallId];
|
||||
if (!msgId) {
|
||||
// 映射不存在(不应该),什么也不做
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const resp = await fetch('/api/sessions/' + state.sessionId + '/messages/' + msgId);
|
||||
if (!resp.ok) return;
|
||||
const msg = await resp.json();
|
||||
if (msg.role !== 'tool') return;
|
||||
// 解析 image_url(parseToolContent 依赖 content 格式)
|
||||
let imageUrl = null;
|
||||
if (typeof msg.content === 'string' && msg.content.startsWith('{"__blocks__":')) {
|
||||
try {
|
||||
const blocks = JSON.parse(msg.content).__blocks__ || [];
|
||||
const img = blocks.find(b => b && b.type === 'image_url');
|
||||
imageUrl = img && img.image_url && img.image_url.url || null;
|
||||
} catch (_) {}
|
||||
}
|
||||
fillToolResult(toolCallId, msg.content, false, imageUrl, false, msg.id);
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
// ---------- 滚动跟随 ----------
|
||||
// 判断是否「贴近底部」:用户在底部附近时,新内容到来自动滚动跟随;
|
||||
// 一旦手动上翻阅读历史,暂停自动滚动以免打斷阅读。
|
||||
|
||||
Reference in New Issue
Block a user