加入预览卡片支持

This commit is contained in:
2026-10-19 09:10:51 +08:00
parent 1c74dbd3d5
commit 6c1aebd01f
6 changed files with 138 additions and 3 deletions
+1 -1
View File
@@ -755,7 +755,7 @@ async function runLoopInner(sessionId, run) {
} else {
try {
output = await executeTool({ sessionId, taskDir: taskDirOf(sessionId), run, cfg, db, vision,
onTaskCreated: () => { run.taskWrap = true; } }, // 定时即下班:工具只打标记,收口在 finalize(回合结束后才能解绑归档)
onTaskCreated: () => { run.taskWrap = true; }, onCard: (card) => bus.emit(sessionId, 'ui_card', card) }, // 通用 UI 卡片 → SSE(ctx 由 executeTool 透传给 MCP 与内置工具) // 定时即下班:工具只打标记,收口在 finalize(回合结束后才能解绑归档)
tc.function.name, chk.args);
} catch (err) {
+6
View File
@@ -203,6 +203,12 @@ async function callTool(name, args, ctx) {
}
const content = result && result.content;
if (Array.isArray(content)) {
// 通用 UI 卡片块(type==="ui_card"):剥离后不进模型面(下一行只拼 text),整块交 ctx.onCard(任意服务/工具可产出,engine 注入 → bus SSE 给前端)
if (ctx && typeof ctx.onCard === "function") {
for (const c of content) if (c && c.type === "ui_card") {
try { ctx.onCard(Object.assign({}, c, { from: entry.server.name + "/" + entry.tool })); } catch (_) { /* 卡片是增强,不影响工具结果 */ }
}
}
const text = content.filter(c => c && c.type === 'text').map(c => c.text || '').join('\n');
if (text) return text;
}
+16 -1
View File
@@ -1273,7 +1273,7 @@ label.icon-btn { cursor: pointer; }
body.pv-open.drawer-open #app { width: calc(100% - var(--sidebar-w) - var(--pv-w, min(50vw, 900px))); margin-left: var(--sidebar-w); }
body.pv-open.pv-max #app { width: 100%; margin-left: 0; }
}
.pv-head { flex: none; display: flex; align-items: center; gap: 8px; padding: 8px 10px 8px 14px; border-bottom: 1px solid var(--border); background: var(--drawer-bg); }
.pv-head { flex: none; position: relative; display: flex; align-items: center; gap: 8px; padding: 8px 10px 8px 14px; border-bottom: 1px solid var(--border); background: var(--drawer-bg); }
/* 地址条胶囊:路径 input + 内嵌动作钮,模拟浏览器地址栏 */
.pv-addr { flex: 1; min-width: 0; display: flex; align-items: center; gap: 2px; background: var(--surface); border: 1px solid transparent; border-radius: 10px; padding: 2px 2px 2px 4px; }
.pv-addr:focus-within { border-color: var(--border); background: var(--page); }
@@ -1281,6 +1281,10 @@ label.icon-btn { cursor: pointer; }
.pv-addr .icon-btn { flex: none; width: 26px; height: 26px; border-radius: 7px; display: flex; align-items: center; justify-content: center; color: var(--gray); }
.pv-addr .icon-btn:hover { color: var(--ink); }
.pv-addr .icon-btn:active { background: rgba(0, 0, 0, .06); }
/* 地址条补全下拉:挂在头部下方,候选只显示末段,目录候选带尾斜杠 */
.pv-suggest { position: absolute; top: calc(100% + 2px); left: 12px; z-index: 8; min-width: 200px; max-width: 420px; max-height: 240px; overflow: auto; background: var(--page); border: 1px solid var(--border); border-radius: 10px; padding: 4px; box-shadow: 0 10px 28px rgba(0, 0, 0, .14); }
.pv-suggest .it { padding: 6px 9px; font-size: 12.5px; border-radius: 7px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--ink); }
.pv-suggest .it:hover, .pv-suggest .it.sel { background: var(--surface); }
.pv-actions { flex: none; display: flex; align-items: center; gap: 10px; margin-left: 12px; padding-right: 2px; }
.pv-actions .icon-btn { width: 32px; height: 32px; border-radius: 8px; display: flex; align-items: center; justify-content: center; color: var(--ink); }
.pv-actions .icon-btn:active { background: var(--surface); }
@@ -1311,3 +1315,14 @@ body.pv-resizing #pv-pane iframe { pointer-events: none; }
.pv-none .hint { font-size: 13px; color: var(--gray-light); line-height: 1.7; max-width: 420px; }
.pv-none button { margin-top: 8px; border: 0; background: var(--active); color: #fff; border-radius: 8px; padding: 9px 22px; font-size: 13px; cursor: pointer; }
.pv-none button:hover { opacity: .88; }
/* 通用 UI 卡片:SSE ui_card → 聊天流可点卡片(动作注册表驱动,见 preview.js) */
.ui-card { display: flex; align-items: center; gap: 10px; max-width: 420px; margin: 8px 0; padding: 10px 14px;
border: 1px solid var(--border); border-radius: 12px; background: var(--page); cursor: pointer; }
.ui-card:hover { border-color: var(--active); background: var(--active-light); }
.ui-card .ico { color: var(--gray-light); flex-shrink: 0; }
.ui-card .ico svg { width: 22px; height: 22px; display: block; }
.ui-card .main { flex: 1; min-width: 0; }
.ui-card .name { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ui-card .sub { display: block; font-size: 12px; color: var(--gray); }
.ui-card .go { font-size: 13px; color: var(--active); white-space: nowrap; }
+2 -1
View File
@@ -406,7 +406,7 @@
<header class="pv-head">
<!-- 地址条胶囊:路径 input(点击全选可复制) + 内嵌 刷新/新窗口,模拟浏览器地址栏 -->
<span class="pv-addr">
<input class="pv-title" id="pv-title" readonly aria-label="文件路径">
<input class="pv-title" id="pv-title" aria-label="文件路径或网址">
<button class="icon-btn hidden" id="pv-refresh" title="重新载入" aria-label="重新载入">
<svg class="ic sm" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"></polyline><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"></path></svg>
</button>
@@ -424,6 +424,7 @@
</button>
<button class="icon-btn" id="pv-close" title="关闭预览" aria-label="关闭预览">✕</button>
</span>
<div class="pv-suggest hidden" id="pv-suggest"></div>
</header>
<div class="pv-body" id="pv-body"></div>
</aside>
+109
View File
@@ -327,6 +327,90 @@ document.getElementById('pv-popup').addEventListener('click', () => {
});
// 地址条:点击/聚焦全选——长路径被截断时可选中看全文、直接 Ctrl+C 复制
document.getElementById('pv-title').addEventListener('focus', (e) => e.target.select());
// 地址条回车跳转:http(s) 网址新标签打开,其余按任务目录相对路径进预览
function pvClean(s) { return String(s || '').trim().replace(/^["']+|["']+$/g, ''); }
function pvAddrGo(v) {
if (/^https?:\/\//i.test(v)) { window.open(v, '_blank', 'noopener'); return; }
document.getElementById('pv-title').blur();
openFilePreview(v);
}
// ---------- 地址条文件名补全:候选取自 files.js 缓存的目录树(filesNodeAt),零请求 ----------
const pvSugBox = document.getElementById('pv-suggest');
const pvSug = { items: [], idx: -1 }; // items: [{ name, full, dir }]
function pvSugHide() { pvSugBox.classList.add('hidden'); pvSugBox.innerHTML = ''; pvSug.items = []; pvSug.idx = -1; }
function pvSugMark() {
const kids = pvSugBox.children;
for (let i = 0; i < kids.length; i++) kids[i].classList.toggle('sel', i === pvSug.idx);
}
function pvSugFill(i) {
const it = pvSug.items[i];
const t = document.getElementById('pv-title');
if (!it) return;
t.value = it.full;
t.focus();
if (it.dir) { pvSugUpdate(t); return; } // 目录:回填后继续列它的内容
const cut = it.full.lastIndexOf('/') + 1;
t.setSelectionRange(cut, it.full.length); // 选中末段,接着打字继续筛
pvSugHide();
}
function pvSugUpdate(t) {
const v = t.value;
pvSug.items = [];
pvSug.idx = -1;
if (!/^https?:\/\//i.test(v.trim())) {
const slash = v.lastIndexOf('/');
const dir = v.slice(0, slash + 1); // 末斜杠前当目录;无斜杠则 dir='' 列根目录
const frag = v.slice(slash + 1).toLowerCase();
const node = typeof filesNodeAt === 'function' ? filesNodeAt(dir.replace(/\/$/, '')) : null;
if (node) {
const out = [];
for (const d of node.dirs.values()) out.push({ name: d.name + '/', full: dir + d.name + '/', dir: true });
for (const f of node.files) {
const nm = String(f.filename).split('/').pop();
if (nm) out.push({ name: nm, full: dir + nm, dir: false });
}
// 前缀匹配;已输入与候选全等时不再提示该条
pvSug.items = out.filter((it) => it.name.toLowerCase().startsWith(frag) && it.name.toLowerCase() !== frag).slice(0, 8);
}
}
pvSugBox.innerHTML = '';
if (!pvSug.items.length) { pvSugBox.classList.add('hidden'); return; }
pvSug.items.forEach((it, i) => {
const d = document.createElement('div');
d.className = 'it';
d.textContent = it.name;
d.addEventListener('mousedown', (e) => e.preventDefault()); // 保持输入框焦点
d.addEventListener('click', () => pvSugFill(i));
pvSugBox.appendChild(d);
});
pvSugBox.classList.remove('hidden');
}
// 地址条按键:↑↓选、Tab 补全、Enter 打开(高亮文件直接开,目录仅补全)、Esc 先收下拉;组字回车不触发
document.getElementById('pv-title').addEventListener('input', (e) => pvSugUpdate(e.target));
document.getElementById('pv-title').addEventListener('blur', pvSugHide);
document.getElementById('pv-title').addEventListener('keydown', (e) => {
if (e.isComposing) return;
if (e.key === 'Escape' && !pvSugBox.classList.contains('hidden')) { pvSugHide(); e.stopPropagation(); return; }
if ((e.key === 'ArrowDown' || e.key === 'ArrowUp') && pvSug.items.length) {
e.preventDefault();
const n = pvSug.items.length;
if (e.key === 'ArrowDown') pvSug.idx = (pvSug.idx + 1) % n;
else pvSug.idx = pvSug.idx <= 0 ? n - 1 : pvSug.idx - 1;
pvSugMark();
return;
}
if (e.key === 'Tab' && pvSug.items.length) { pvSugFill(pvSug.idx >= 0 ? pvSug.idx : 0); e.preventDefault(); return; }
if (e.key !== 'Enter') return;
if (pvSug.idx >= 0) {
const it = pvSug.items[pvSug.idx];
if (it.dir) { pvSugFill(pvSug.idx); return; }
pvSugHide();
pvAddrGo(it.full);
return;
}
const v = pvClean(e.target.value);
if (v) pvAddrGo(v);
});
// md 代码块复制钮与 fs-viewer 同一委托逻辑
document.getElementById('pv-pane').addEventListener('click', (e) => {
const b = e.target.closest('.act-copy');
@@ -381,3 +465,28 @@ window.matchMedia('(min-width: 960px)').addEventListener('change', () => { if (!
localStorage.removeItem('pv-w');
});
})();
// —— 通用 UI 卡片(stream.js 转发 SSE ui_card;任何 MCP 服务/工具回包附 type:"ui_card" 块即可产出)——
// 产生方契约 {action, arg, title?, note?};新交互在动作注册表加一条即可,未知 action 忽略(向前兼容)。
// 图标由前端统一持有(与 pv-none 同一枚文档 SVG),产生方不提供图形,只给数据。
const UI_CARD_ICON_DOC = '<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round" stroke-linejoin="round"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline></svg>';
const UI_CARD_ACTIONS = {
// arg = 相对会话目录的文件路径,点击 = openFilePreview(office 走预览网关,其余类型走文本/图片渲染)
open_preview: { go: '预览 ›', hint: '点击打开预览', icon: UI_CARD_ICON_DOC, run: (d) => openFilePreview(d.arg) },
};
function appendUiCard(d) {
const act = d && UI_CARD_ACTIONS[d.action];
if (!act || !d.arg) { if (d && d.action) console.warn('[ui_card] unknown action:', d.action); return; }
const cv = document.getElementById('chat-view');
if (!cv) return;
const el = document.createElement('div');
el.className = 'ui-card';
el.innerHTML =
'<span class="ico">' + act.icon + '</span>' +
'<span class="main"><span class="name">' + esc(d.title || d.arg) + '</span>' +
'<span class="sub">' + (d.note ? esc(d.note) + ' · ' : '') + act.hint + '</span></span>' +
'<span class="go">' + act.go + '</span>';
el.addEventListener('click', () => { act.run(d); });
cv.appendChild(el);
maybeAutoScroll();
}
+4
View File
@@ -298,6 +298,10 @@
fillToolResult(d.tool_call_id, d.content, d.is_error, d.image_url, d.msg_id);
maybeAutoScroll();
});
// 通用 UI 卡片:工具回包附 ui_card 块(MCP 层剥离转发),渲染器与动作注册表见 preview.js appendUiCard
es.addEventListener('ui_card', ev => {
appendUiCard(JSON.parse(ev.data));
});
// 后端回合结束+宽限期后的自动压缩提议:不再自动执行,弹 modal 请用户手动确认(见 proposeAutoCompress)
es.addEventListener('auto_compress', ev => {
proposeAutoCompress(JSON.parse(ev.data));