feat(local): 文件树提升为桌面端通用入口,接管旧文件面板
桌面端(含服务器形态)用左栏懒加载文件树取代输入行右侧的旧「任务文件」浮层: 点文件进右侧预览,行内可下载 / 删除;local 形态额外提供「在 Finder 中显示」。 - routes/files.js:新增 GET /:id/tree(单层懒加载、忽略大目录、上限 500、 字面 + realpath 双重越界校验),替代会同步递归整棵目录的 GET /:id/files - routes/local.js:移除 /tree(上收为通用接口),只留 pick-dir / reveal / terminal - local-files.js → files-tree.js:两种形态共用;local 能力靠 /api/local/info 探测, 服务器形态自动隐藏 Finder/终端入口 - 树头部复用预览栏的 .pv-head / .pv-addr / .pv-actions / .icon-btn: 左右并排时高度、图标尺寸与描边完全一致 - 树头不再显示会话标题(原样搬过来容易和会话列表重复),改为显示工作目录路径 - 桌面端隐藏 #files-btn;手机端保留旧面板 - 上传完成后同步刷新文件树
This commit is contained in:
@@ -872,30 +872,23 @@ body.voice-active .scroll-bottom-btn { display: none; }
|
||||
width: 20px; height: 20px; fill: none; stroke: currentColor;
|
||||
stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
|
||||
}
|
||||
/* 手机/窄屏没有右侧预览栏(预览走全屏浮层),这个按钮无意义 */
|
||||
@media (max-width: 959px) { #da-preview { display: none !important; } }
|
||||
/* 手机/窄屏没有右侧预览栏(预览走全屏浮层),也没有左栏文件树(窄屏不适合) */
|
||||
@media (max-width: 959px) { #da-preview, #da-files { display: none !important; } }
|
||||
|
||||
.files-tree { display: none; flex: 1; min-height: 0; flex-direction: column; }
|
||||
#drawer.drawer-mode-files .drawer-search,
|
||||
#drawer.drawer-mode-files .drawer-list { display: none; }
|
||||
#drawer.drawer-mode-files .files-tree { display: flex; }
|
||||
|
||||
.tree-head {
|
||||
flex: none; display: flex; align-items: center; gap: 4px;
|
||||
padding: 8px 12px 6px; border-bottom: 1px solid var(--border);
|
||||
/* 左栏树头部复用预览栏的 .pv-head/.pv-addr/.pv-actions/.icon-btn:
|
||||
它俩在桌面上左右并排,高一点点或图标小一号都会明显错位。 */
|
||||
.tree-head .pv-actions { gap: 4px; margin-left: 6px; } /* 侧栏窄,4 个按钮也塞得下 */
|
||||
.tree-path {
|
||||
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||
cursor: default;
|
||||
}
|
||||
.tree-title {
|
||||
flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
||||
font-size: 13px; color: var(--ink);
|
||||
}
|
||||
.tree-actions { display: flex; gap: 2px; }
|
||||
.tree-hbtn {
|
||||
width: 26px; height: 26px; display: inline-flex; align-items: center; justify-content: center;
|
||||
border: 0; background: none; border-radius: 8px; color: var(--gray); cursor: pointer;
|
||||
}
|
||||
.tree-hbtn:hover { background: var(--surface); color: var(--active); }
|
||||
.tree-hbtn.active { color: var(--active); background: var(--active-soft); }
|
||||
.tree-hbtn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
|
||||
/* 头部图标的「开启中」态(如显示隐藏文件) */
|
||||
.pv-actions .icon-btn.active { color: var(--active); background: var(--active-soft); }
|
||||
|
||||
.tree-body { flex: 1; min-height: 0; overflow: auto; padding: 4px 6px 12px; }
|
||||
.tree-row {
|
||||
@@ -924,11 +917,13 @@ body.voice-active .scroll-bottom-btn { display: none; }
|
||||
}
|
||||
.tree-row:hover .tree-act { display: inline-flex; }
|
||||
.tree-act:hover { background: var(--border); color: var(--active); }
|
||||
.tree-act.danger:hover { background: #fdf3f2; color: #c0392b; }
|
||||
.tree-act svg { width: 13px; height: 13px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
|
||||
.tree-empty, .tree-loading, .tree-more { padding: 10px 12px; font-size: 12px; color: var(--gray); }
|
||||
|
||||
/* 本地模式:隐藏旧的底部「任务文件」浮层入口(它走整棵递归接口,大项目会卡死。改用左栏树) */
|
||||
body.local-mode #files-btn { display: none !important; }
|
||||
/* 桌面端:文件浏览交给左栏文件树,隐藏输入行右侧的旧「任务文件」浮层入口。
|
||||
手机端仍用它(窄屏放不下树)。 */
|
||||
@media (min-width: 960px) { #files-btn { display: none !important; } }
|
||||
/* 会话分组表头(按今天/昨天分组):滚动时吸顶,下一个 group-label 滚过来时把它顶走
|
||||
背景色与抽屉一致,避免 sticky 时下面会话透过看到上一组的尾巴 */
|
||||
.group-label {
|
||||
|
||||
@@ -234,13 +234,13 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="drawer-list" id="drawer-list"></div>
|
||||
<div class="files-tree hidden" id="files-tree"></div>
|
||||
<!-- 底部图标操作栏:会话 / 文件树 / 预览 / 设置(文件树仅本地模式出现)。选中项高亮。 -->
|
||||
<div class="files-tree" id="files-tree"></div>
|
||||
<!-- 底部图标操作栏:会话 / 文件树 / 预览 / 设置(文件树仅桌面端出现)。选中项高亮。 -->
|
||||
<div class="drawer-actions" id="drawer-actions">
|
||||
<button class="drawer-act active" id="da-sessions" type="button" title="会话" aria-label="会话">
|
||||
<svg viewBox="0 0 24 24"><path d="M21 11.5a8.5 8.5 0 0 1-11.8 7.8L4 21l1.7-5.2A8.5 8.5 0 1 1 21 11.5z"></path></svg>
|
||||
</button>
|
||||
<button class="drawer-act hidden" id="da-files" type="button" title="文件树" aria-label="文件树">
|
||||
<button class="drawer-act" id="da-files" type="button" title="文件树" aria-label="文件树">
|
||||
<svg viewBox="0 0 24 24"><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"></path></svg>
|
||||
</button>
|
||||
<button class="drawer-act" id="da-preview" type="button" title="预览" aria-label="预览">
|
||||
@@ -557,7 +557,7 @@
|
||||
<script src="/js/files.js"></script>
|
||||
<script src="/js/voice.js"></script>
|
||||
<script src="/js/preview.js"></script>
|
||||
<script src="/js/local-files.js"></script>
|
||||
<script src="/js/files-tree.js"></script>
|
||||
<script src="/js/settings.js"></script>
|
||||
<script>
|
||||
// 启动自愈:外部工具改前端文件是非原子写入,极端时机下浏览器可能拿到半截或重复拼接的 HTML
|
||||
|
||||
@@ -0,0 +1,287 @@
|
||||
// 桌面端左栏文件树:会话文件的唯一浏览入口(服务器形态与 local 形态共用)。
|
||||
// · 懒加载:一次只请求一层目录,展开才拉下一层;node_modules 这类大目录后端只回标记、不展开,
|
||||
// 避免旧的整棵递归接口在单线程 core 上卡死;
|
||||
// · 点文件 → 右侧预览;行内 hover 出「下载 / 删除 / 在 Finder 中显示」;
|
||||
// · 「在 Finder / 终端中打开」只在 local 启动器下可用(服务器上这些动作没有意义),
|
||||
// 靠 /api/local/info 探测,拿不到就隐藏这两个按钮。
|
||||
// 手机端不启用(收纳在抽屉里体验太差),仍走输入行右侧的旧文件面板。
|
||||
// 依赖(全局):state $ req toast downloadFile confirmModal openFilePreview
|
||||
'use strict';
|
||||
(function () {
|
||||
const SVG_FOLDER = '<svg class="ic sm" viewBox="0 0 24 24"><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></svg>';
|
||||
const SVG_FILE = '<svg class="ic sm" viewBox="0 0 24 24"><path d="M6 3h8l4 4v14H6z"/><path d="M14 3v4h4"/></svg>';
|
||||
const SVG_LINK = '<svg class="ic sm" viewBox="0 0 24 24"><path d="M10 13a5 5 0 0 0 7 0l2-2a5 5 0 0 0-7-7l-1 1"/><path d="M14 11a5 5 0 0 0-7 0l-2 2a5 5 0 0 0 7 7l1-1"/></svg>';
|
||||
const SVG_REVEAL = '<svg class="ic sm" viewBox="0 0 24 24"><path d="M14 4h6v6"/><path d="M20 4l-9 9"/><path d="M19 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1h5"/></svg>';
|
||||
const SVG_REFRESH = '<svg class="ic sm" viewBox="0 0 24 24"><path d="M20 12a8 8 0 1 1-2.3-5.7"/><path d="M20 4v5h-5"/></svg>';
|
||||
const SVG_TERM = '<svg class="ic sm" viewBox="0 0 24 24"><rect x="3" y="4" width="18" height="16" rx="2"/><path d="M7 9l3 3-3 3"/><path d="M13 15h4"/></svg>';
|
||||
const SVG_EYE = '<svg class="ic sm" viewBox="0 0 24 24"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z"/><circle cx="12" cy="12" r="2.6"/></svg>';
|
||||
const SVG_DL = '<svg viewBox="0 0 24 24"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>';
|
||||
const SVG_DEL = '<svg viewBox="0 0 24 24"><polyline points="3 6 5 6 21 6"/><path d="M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"/><line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/></svg>';
|
||||
const SVG_REVEAL_SM = '<svg viewBox="0 0 24 24"><path d="M14 4h6v6"/><path d="M20 4l-9 9"/><path d="M19 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1h5"/></svg>';
|
||||
|
||||
let localCapable = false; // local 启动器:才显示「在 Finder/终端中打开」
|
||||
let showHidden = false;
|
||||
let expanded = new Set(['']); // 已展开的相对目录('' = 根)
|
||||
let cache = new Map(); // rel -> /tree 响应
|
||||
let loading = new Map(); // rel -> Promise(并发去重)
|
||||
let gen = 0; // 会话切换/刷新时自增,防止旧请求回填到新树的缓存
|
||||
let rootPath = ''; // 会话根绝对路径(后端回的),显示在头部
|
||||
|
||||
// 失效缓存。gen 自增让在飞的旧请求回来时不再写缓存(否则切会话会闪旧目录)。
|
||||
function invalidate(keepExpanded) {
|
||||
gen++;
|
||||
cache = new Map();
|
||||
loading = new Map();
|
||||
if (!keepExpanded) expanded = new Set(['']);
|
||||
}
|
||||
|
||||
function sid() { return (state && state.sessionId) || ''; }
|
||||
|
||||
function tip(parent, text, cls) {
|
||||
const el = document.createElement('div');
|
||||
el.className = cls || 'tree-empty';
|
||||
el.textContent = text;
|
||||
parent.appendChild(el);
|
||||
}
|
||||
|
||||
function openDir(rel) {
|
||||
if (cache.has(rel)) return Promise.resolve(cache.get(rel));
|
||||
if (loading.has(rel)) return loading.get(rel);
|
||||
if (!sid()) return Promise.resolve(null);
|
||||
const url = '/api/sessions/' + encodeURIComponent(sid()) + '/tree?dir=' +
|
||||
encodeURIComponent(rel) + (showHidden ? '&hidden=1' : '');
|
||||
const myGen = gen;
|
||||
const p = req('GET', url).then(function (data) {
|
||||
if (myGen === gen) { cache.set(rel, data); loading.delete(rel); }
|
||||
return data;
|
||||
}, function (e) {
|
||||
if (myGen === gen) loading.delete(rel);
|
||||
throw e;
|
||||
});
|
||||
loading.set(rel, p);
|
||||
return p;
|
||||
}
|
||||
|
||||
function actBtn(act, title, svg, danger) {
|
||||
const b = document.createElement('button');
|
||||
b.type = 'button';
|
||||
b.className = 'tree-act' + (danger ? ' danger' : '');
|
||||
b.dataset.act = act;
|
||||
b.title = title;
|
||||
b.innerHTML = svg;
|
||||
return b;
|
||||
}
|
||||
|
||||
function rowFor(ent, depth) {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'tree-row';
|
||||
row.dataset.rel = ent.rel;
|
||||
row.dataset.type = ent.type;
|
||||
row.style.paddingLeft = (6 + depth * 14) + 'px';
|
||||
row.title = ent.rel;
|
||||
if (ent.type === 'dir') {
|
||||
row.classList.add('is-dir');
|
||||
if (expanded.has(ent.rel)) row.classList.add('open');
|
||||
}
|
||||
if (ent.type === 'link') row.classList.add('is-link');
|
||||
if (ent.ignored) row.classList.add('is-ignored');
|
||||
|
||||
const caret = document.createElement('span');
|
||||
caret.className = 'tree-caret';
|
||||
row.appendChild(caret);
|
||||
|
||||
const ico = document.createElement('span');
|
||||
ico.className = 'tree-ico';
|
||||
ico.innerHTML = ent.type === 'dir' ? SVG_FOLDER : (ent.type === 'link' ? SVG_LINK : SVG_FILE);
|
||||
row.appendChild(ico);
|
||||
|
||||
const name = document.createElement('span');
|
||||
name.className = 'tree-name';
|
||||
name.textContent = ent.name;
|
||||
row.appendChild(name);
|
||||
|
||||
if (ent.ignored) {
|
||||
row.title = ent.rel + '(已忽略的大目录/构建产物)';
|
||||
return row;
|
||||
}
|
||||
// 行内动作:服务器形态给「下载 / 删除」;local 本就跑在同一台机器上,
|
||||
// 「下载」到本地是多余的(用 Finder 直接看原文件更顺),所以换成「在 Finder 中显示」。
|
||||
if (ent.type === 'file' && !localCapable) row.appendChild(actBtn('download', '下载', SVG_DL, false));
|
||||
if (localCapable) row.appendChild(actBtn('reveal', '在 Finder 中显示', SVG_REVEAL_SM, false));
|
||||
row.appendChild(actBtn('delete', '删除', SVG_DEL, true));
|
||||
return row;
|
||||
}
|
||||
|
||||
async function renderDirInto(container, rel, depth) {
|
||||
container.innerHTML = '';
|
||||
const data = await openDir(rel);
|
||||
if (!data) return;
|
||||
if (!rel && data.root) rootPath = data.root;
|
||||
const frag = document.createDocumentFragment();
|
||||
const list = data.entries || [];
|
||||
if (!list.length) tip(frag, '(空目录)');
|
||||
for (const ent of list) {
|
||||
frag.appendChild(rowFor(ent, depth));
|
||||
if (ent.type === 'dir' && !ent.ignored && expanded.has(ent.rel)) {
|
||||
const kids = document.createElement('div');
|
||||
kids.className = 'tree-children';
|
||||
frag.appendChild(kids);
|
||||
renderDirInto(kids, ent.rel, depth + 1).catch(function () {}); // 异步填充子层
|
||||
}
|
||||
}
|
||||
if (data.capped) tip(frag, '仅显示前 ' + list.length + ' 项(共 ' + data.total + ' 项)', 'tree-more');
|
||||
container.appendChild(frag);
|
||||
}
|
||||
|
||||
function renderPathLabel() {
|
||||
const el = $('tree-path');
|
||||
if (!el) return;
|
||||
// 根路径太长时只留末两段,完整路径放 title(地址条的直觉:能看个大概,悬停能看全)
|
||||
const parts = String(rootPath || '').split('/').filter(Boolean);
|
||||
const short = parts.length > 2 ? '.../' + parts.slice(-2).join('/') : (rootPath || '会话文件');
|
||||
el.textContent = short;
|
||||
el.title = rootPath || '';
|
||||
}
|
||||
|
||||
async function renderBody() {
|
||||
const body = $('tree-body');
|
||||
if (!body) return;
|
||||
if (!sid()) { body.innerHTML = ''; rootPath = ''; renderPathLabel(); tip(body, '尚未开始会话'); return; }
|
||||
const sc = body.scrollTop;
|
||||
body.innerHTML = '';
|
||||
const root = document.createElement('div');
|
||||
body.appendChild(root);
|
||||
try {
|
||||
await renderDirInto(root, '', 0);
|
||||
} catch (e) {
|
||||
root.innerHTML = '';
|
||||
tip(root, '加载失败:' + e.message);
|
||||
}
|
||||
renderPathLabel();
|
||||
body.scrollTop = sc;
|
||||
}
|
||||
|
||||
async function localAction(kind, rel) {
|
||||
if (!localCapable) return;
|
||||
if (!sid()) { toast('尚未开始会话'); return; }
|
||||
try {
|
||||
await req('POST', '/api/local/' + kind, { session: sid(), path: rel });
|
||||
} catch (e) {
|
||||
toast((kind === 'reveal' ? '打开目录失败:' : '打开终端失败:') + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteNode(rel, isDir) {
|
||||
if (!sid()) return;
|
||||
const name = rel.split('/').pop();
|
||||
const ok = typeof confirmModal === 'function'
|
||||
? await confirmModal('删除' + (isDir ? '文件夹' : '文件'),
|
||||
'删除「' + name + '」' + (isDir ? '(含其中全部内容)' : '') + '?', '删除', true)
|
||||
: true;
|
||||
if (!ok) return;
|
||||
try {
|
||||
await req('DELETE', '/api/sessions/' + sid() + '/files?path=' + encodeURIComponent(rel));
|
||||
// 预览里正开着被删的文件 → 一起关掉,别留一份已不存在的内容
|
||||
if (typeof pvState === 'object' && pvState && pvState.name === rel && typeof pvCloseDoc === 'function') pvCloseDoc();
|
||||
invalidate(true);
|
||||
renderBody();
|
||||
toast('已删除');
|
||||
} catch (e) { toast('删除失败:' + e.message); }
|
||||
}
|
||||
|
||||
function onTreeClick(e) {
|
||||
const row = e.target.closest('.tree-row');
|
||||
if (!row) return;
|
||||
const rel = row.dataset.rel;
|
||||
const isDir = row.dataset.type === 'dir';
|
||||
const act = e.target.closest('.tree-act');
|
||||
if (act) {
|
||||
e.stopPropagation();
|
||||
const a = act.dataset.act;
|
||||
if (a === 'download') { if (typeof downloadFile === 'function') downloadFile(rel); }
|
||||
else if (a === 'delete') deleteNode(rel, isDir);
|
||||
else if (a === 'reveal') localAction('reveal', rel);
|
||||
return;
|
||||
}
|
||||
if (row.classList.contains('is-ignored') || row.dataset.type === 'link') return;
|
||||
if (isDir) {
|
||||
if (expanded.has(rel)) expanded.delete(rel); else expanded.add(rel);
|
||||
renderBody();
|
||||
} else if (typeof openFilePreview === 'function') {
|
||||
openFilePreview(rel, '');
|
||||
}
|
||||
}
|
||||
|
||||
// local 能力探测结果回来后才把「在 Finder/终端中打开」露出来(服务器形态永远隐藏)
|
||||
function syncLocalButtons() {
|
||||
const rv = $('tree-reveal'), tm = $('tree-terminal');
|
||||
if (rv) rv.classList.toggle('hidden', !localCapable);
|
||||
if (tm) tm.classList.toggle('hidden', !localCapable);
|
||||
if (localCapable && $('tree-body')) renderBody(); // 行内 reveal 按钮也要重新生成
|
||||
}
|
||||
|
||||
function ensureShell() {
|
||||
const box = $('files-tree');
|
||||
if (!box || box.dataset.ready) return;
|
||||
box.dataset.ready = '1';
|
||||
box.innerHTML = '';
|
||||
|
||||
// 头部直接复用预览栏的 .pv-head / .pv-addr / .pv-actions / .icon-btn:
|
||||
// 两者在桌面上左右并排,尺寸/描边/内边距必须一模一样,否则一眼就看出错位。
|
||||
const head = document.createElement('header');
|
||||
head.className = 'pv-head tree-head';
|
||||
head.innerHTML =
|
||||
'<span class="pv-addr"><span class="pv-title tree-path" id="tree-path"></span></span>' +
|
||||
'<span class="pv-actions">' +
|
||||
'<button class="icon-btn" type="button" id="tree-hidden" title="显示隐藏文件" aria-label="显示隐藏文件">' + SVG_EYE + '</button>' +
|
||||
'<button class="icon-btn" type="button" id="tree-refresh" title="刷新" aria-label="刷新">' + SVG_REFRESH + '</button>' +
|
||||
'<button class="icon-btn hidden" type="button" id="tree-reveal" title="在 Finder 中打开" aria-label="在 Finder 中打开">' + SVG_REVEAL + '</button>' +
|
||||
'<button class="icon-btn hidden" type="button" id="tree-terminal" title="在终端中打开" aria-label="在终端中打开">' + SVG_TERM + '</button>' +
|
||||
'</span>';
|
||||
|
||||
const body = document.createElement('div');
|
||||
body.className = 'tree-body';
|
||||
body.id = 'tree-body';
|
||||
|
||||
box.appendChild(head);
|
||||
box.appendChild(body);
|
||||
|
||||
head.querySelector('#tree-hidden').addEventListener('click', function () {
|
||||
showHidden = !showHidden;
|
||||
invalidate(false);
|
||||
this.classList.toggle('active', showHidden);
|
||||
renderBody();
|
||||
});
|
||||
head.querySelector('#tree-refresh').addEventListener('click', function () {
|
||||
invalidate(true);
|
||||
renderBody();
|
||||
});
|
||||
head.querySelector('#tree-reveal').addEventListener('click', function () { localAction('reveal', ''); });
|
||||
head.querySelector('#tree-terminal').addEventListener('click', function () { localAction('terminal', ''); });
|
||||
body.addEventListener('click', onTreeClick);
|
||||
|
||||
syncLocalButtons();
|
||||
}
|
||||
|
||||
// 侧栏底部「文件树」图标 → 切到这个面板(由 files.js 的 setDrawerPane 调用)。
|
||||
window.showFileTree = function () {
|
||||
ensureShell();
|
||||
renderBody();
|
||||
};
|
||||
|
||||
// 会话切换后由 sessions.js 调用,避免树停在旧会话。
|
||||
window.refreshFileTree = function () {
|
||||
invalidate(false);
|
||||
const drawer = $('drawer');
|
||||
if (drawer && drawer.classList.contains('drawer-mode-files')) renderBody();
|
||||
};
|
||||
|
||||
(function init() {
|
||||
if (!$('drawer-actions') || !$('files-tree')) return;
|
||||
req('GET', '/api/local/info').then(function (info) {
|
||||
if (!info || !info.enabled) return;
|
||||
localCapable = true;
|
||||
syncLocalButtons();
|
||||
}).catch(function () { /* 服务器形态:接口 404,保持纯服务器行为 */ });
|
||||
})();
|
||||
})();
|
||||
@@ -910,6 +910,8 @@
|
||||
// 全部成功不再弹提示(卡片已完成态即反馈);有失败才提醒
|
||||
if (errCount) toast('已上传 ' + done + ' 个,失败 ' + errCount + ' 个');
|
||||
loadFiles();
|
||||
// 桌面端左栏文件树是新的浏览入口,上传后要同步(旧面板只在手机端用)
|
||||
if (typeof window.refreshFileTree === 'function') window.refreshFileTree();
|
||||
// 1.2s 后收起进度条(还有别的批次在传就不收)
|
||||
setTimeout(() => {
|
||||
if (uploadBatches.size) return;
|
||||
|
||||
@@ -1,238 +0,0 @@
|
||||
// 本地端左栏文件树(只读)。仅在 local 启动器下启用:
|
||||
// 后端 /api/local/info 拿得到才显示侧栏底部的「文件树」图标与树,服务器/手机形态完全不受影响。
|
||||
// 设计要点:
|
||||
// - 目录是用户的真实项目,可能含 node_modules 等大目录,所以「展开才请求、一次一层」;
|
||||
// - 树只负责看:点文件进右侧既有预览(#pv-pane),不提供删除/新建/重命名;
|
||||
// - 「在 Finder/终端中打开」走 /api/local/reveal|terminal(后端已做会话目录内校验)。
|
||||
'use strict';
|
||||
(function () {
|
||||
const SVG_FOLDER = '<svg viewBox="0 0 24 24"><path d="M3 7a2 2 0 0 1 2-2h4l2 2h8a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></svg>';
|
||||
const SVG_FILE = '<svg viewBox="0 0 24 24"><path d="M6 3h8l4 4v14H6z"/><path d="M14 3v4h4"/></svg>';
|
||||
const SVG_LINK = '<svg viewBox="0 0 24 24"><path d="M10 13a5 5 0 0 0 7 0l2-2a5 5 0 0 0-7-7l-1 1"/><path d="M14 11a5 5 0 0 0-7 0l-2 2a5 5 0 0 0 7 7l1-1"/></svg>';
|
||||
const SVG_REVEAL = '<svg viewBox="0 0 24 24"><path d="M14 4h6v6"/><path d="M20 4l-9 9"/><path d="M19 14v5a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1h5"/></svg>';
|
||||
const SVG_REFRESH = '<svg viewBox="0 0 24 24"><path d="M20 12a8 8 0 1 1-2.3-5.7"/><path d="M20 4v5h-5"/></svg>';
|
||||
const SVG_TERM = '<svg viewBox="0 0 24 24"><rect x="3" y="4" width="18" height="16" rx="2"/><path d="M7 9l3 3-3 3"/><path d="M13 15h4"/></svg>';
|
||||
const SVG_EYE = '<svg viewBox="0 0 24 24"><path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z"/><circle cx="12" cy="12" r="2.6"/></svg>';
|
||||
|
||||
let enabled = false;
|
||||
let showHidden = false;
|
||||
let expanded = new Set(['']); // 已展开的相对目录('' = 根)
|
||||
let cache = new Map(); // rel -> /tree 响应
|
||||
let loading = new Map(); // rel -> Promise(并发去重)
|
||||
let gen = 0; // 会话切换/刷新时自增,防止旧请求回填到新树的缓存
|
||||
|
||||
// 失效缓存。gen 自增让在飞的旧请求回来时不再写缓存(否则切会话会闪旧目录)。
|
||||
function invalidate(keepExpanded) {
|
||||
gen++;
|
||||
cache = new Map();
|
||||
loading = new Map();
|
||||
if (!keepExpanded) expanded = new Set(['']);
|
||||
}
|
||||
|
||||
function sid() { return (state && state.sessionId) || ''; }
|
||||
|
||||
function tip(parent, text, cls) {
|
||||
const el = document.createElement('div');
|
||||
el.className = cls || 'tree-empty';
|
||||
el.textContent = text;
|
||||
parent.appendChild(el);
|
||||
}
|
||||
|
||||
function openDir(rel) {
|
||||
if (cache.has(rel)) return Promise.resolve(cache.get(rel));
|
||||
if (loading.has(rel)) return loading.get(rel);
|
||||
if (!sid()) return Promise.resolve(null);
|
||||
const url = '/api/local/tree?session=' + encodeURIComponent(sid()) +
|
||||
'&dir=' + encodeURIComponent(rel) + (showHidden ? '&hidden=1' : '');
|
||||
const myGen = gen;
|
||||
const p = req('GET', url).then(function (data) {
|
||||
if (myGen === gen) { cache.set(rel, data); loading.delete(rel); }
|
||||
return data;
|
||||
}, function (e) {
|
||||
if (myGen === gen) loading.delete(rel);
|
||||
throw e;
|
||||
});
|
||||
loading.set(rel, p);
|
||||
return p;
|
||||
}
|
||||
|
||||
function rowFor(ent, depth) {
|
||||
const row = document.createElement('div');
|
||||
row.className = 'tree-row';
|
||||
row.dataset.rel = ent.rel;
|
||||
row.dataset.type = ent.type;
|
||||
row.style.paddingLeft = (6 + depth * 14) + 'px';
|
||||
row.title = ent.rel;
|
||||
if (ent.type === 'dir') {
|
||||
row.classList.add('is-dir');
|
||||
if (expanded.has(ent.rel)) row.classList.add('open');
|
||||
}
|
||||
if (ent.type === 'link') row.classList.add('is-link');
|
||||
if (ent.ignored) row.classList.add('is-ignored');
|
||||
|
||||
const caret = document.createElement('span');
|
||||
caret.className = 'tree-caret';
|
||||
row.appendChild(caret);
|
||||
|
||||
const ico = document.createElement('span');
|
||||
ico.className = 'tree-ico';
|
||||
ico.innerHTML = ent.type === 'dir' ? SVG_FOLDER : (ent.type === 'link' ? SVG_LINK : SVG_FILE);
|
||||
row.appendChild(ico);
|
||||
|
||||
const name = document.createElement('span');
|
||||
name.className = 'tree-name';
|
||||
name.textContent = ent.name;
|
||||
row.appendChild(name);
|
||||
|
||||
if (ent.ignored) {
|
||||
row.title = ent.rel + '(已忽略的大目录/构建产物)';
|
||||
} else {
|
||||
const act = document.createElement('button');
|
||||
act.type = 'button';
|
||||
act.className = 'tree-act';
|
||||
act.dataset.act = 'reveal';
|
||||
act.title = '在 Finder 中显示';
|
||||
act.innerHTML = SVG_REVEAL;
|
||||
row.appendChild(act);
|
||||
}
|
||||
return row;
|
||||
}
|
||||
|
||||
async function renderDirInto(container, rel, depth) {
|
||||
container.innerHTML = '';
|
||||
const data = await openDir(rel);
|
||||
if (!data) return;
|
||||
const frag = document.createDocumentFragment();
|
||||
const list = data.entries || [];
|
||||
if (!list.length) tip(frag, '(空目录)');
|
||||
for (const ent of list) {
|
||||
frag.appendChild(rowFor(ent, depth));
|
||||
if (ent.type === 'dir' && !ent.ignored && expanded.has(ent.rel)) {
|
||||
const kids = document.createElement('div');
|
||||
kids.className = 'tree-children';
|
||||
frag.appendChild(kids);
|
||||
renderDirInto(kids, ent.rel, depth + 1).catch(function () {}); // 异步填充子层
|
||||
}
|
||||
}
|
||||
if (data.capped) tip(frag, '仅显示前 ' + list.length + ' 项(共 ' + data.total + ' 项)', 'tree-more');
|
||||
container.appendChild(frag);
|
||||
}
|
||||
|
||||
async function renderBody() {
|
||||
const body = $('tree-body');
|
||||
if (!body) return;
|
||||
const titleEl = $('tree-title');
|
||||
if (titleEl) titleEl.textContent = (state && (state.sessionTitle || state.sessionId)) || '文件';
|
||||
if (!sid()) { body.innerHTML = ''; tip(body, '尚未开始会话'); return; }
|
||||
const sc = body.scrollTop;
|
||||
body.innerHTML = '';
|
||||
const root = document.createElement('div');
|
||||
body.appendChild(root);
|
||||
try {
|
||||
await renderDirInto(root, '', 0);
|
||||
} catch (e) {
|
||||
root.innerHTML = '';
|
||||
tip(root, '加载失败:' + e.message);
|
||||
}
|
||||
body.scrollTop = sc;
|
||||
}
|
||||
|
||||
async function localAction(kind, rel) {
|
||||
if (!sid()) { toast('尚未开始会话'); return; }
|
||||
try {
|
||||
await req('POST', '/api/local/' + kind, { session: sid(), path: rel });
|
||||
} catch (e) {
|
||||
toast((kind === 'reveal' ? '打开目录失败:' : '打开终端失败:') + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function onTreeClick(e) {
|
||||
const row = e.target.closest('.tree-row');
|
||||
if (!row) return;
|
||||
const act = e.target.closest('[data-act="reveal"]');
|
||||
if (act) {
|
||||
e.stopPropagation();
|
||||
localAction('reveal', row.dataset.rel);
|
||||
return;
|
||||
}
|
||||
const type = row.dataset.type;
|
||||
const rel = row.dataset.rel;
|
||||
if (row.classList.contains('is-ignored') || type === 'link') return;
|
||||
if (type === 'dir') {
|
||||
if (expanded.has(rel)) expanded.delete(rel); else expanded.add(rel);
|
||||
await renderBody();
|
||||
} else if (type === 'file') {
|
||||
if (typeof openFilePreview === 'function') openFilePreview(rel, '');
|
||||
}
|
||||
}
|
||||
|
||||
function ensureShell() {
|
||||
const box = $('files-tree');
|
||||
if (!box || box.dataset.ready) return;
|
||||
box.dataset.ready = '1';
|
||||
box.innerHTML = '';
|
||||
|
||||
const head = document.createElement('div');
|
||||
head.className = 'tree-head';
|
||||
head.innerHTML =
|
||||
'<span class="tree-title" id="tree-title">文件</span>' +
|
||||
'<span class="tree-actions">' +
|
||||
'<button class="tree-hbtn" type="button" id="tree-hidden" title="显示隐藏文件">' + SVG_EYE + '</button>' +
|
||||
'<button class="tree-hbtn" type="button" id="tree-refresh" title="刷新">' + SVG_REFRESH + '</button>' +
|
||||
'<button class="tree-hbtn" type="button" id="tree-reveal" title="在 Finder 中打开">' + SVG_REVEAL + '</button>' +
|
||||
'<button class="tree-hbtn" type="button" id="tree-terminal" title="在终端中打开">' + SVG_TERM + '</button>' +
|
||||
'</span>';
|
||||
|
||||
const body = document.createElement('div');
|
||||
body.className = 'tree-body';
|
||||
body.id = 'tree-body';
|
||||
|
||||
box.appendChild(head);
|
||||
box.appendChild(body);
|
||||
|
||||
head.querySelector('#tree-hidden').addEventListener('click', function () {
|
||||
showHidden = !showHidden;
|
||||
invalidate(false);
|
||||
this.classList.toggle('active', showHidden);
|
||||
renderBody();
|
||||
});
|
||||
head.querySelector('#tree-refresh').addEventListener('click', function () {
|
||||
invalidate(true);
|
||||
renderBody();
|
||||
});
|
||||
head.querySelector('#tree-reveal').addEventListener('click', function () { localAction('reveal', ''); });
|
||||
head.querySelector('#tree-terminal').addEventListener('click', function () { localAction('terminal', ''); });
|
||||
body.addEventListener('click', onTreeClick);
|
||||
}
|
||||
|
||||
// 侧栏底部「文件树」图标 → 切到这个面板(由 files.js 的 setDrawerPane 调用)。
|
||||
// 面板切换本身由 CSS 的 drawer-mode-files 控制,这里只负责首次建壳 + 拉一层目录。
|
||||
// 只在确认是本地模式后挂到 window 上:files.js 用它是否存在来判断「文件树」按钮可不可用。
|
||||
function exposeShowFileTree() {
|
||||
window.showFileTree = function () {
|
||||
ensureShell();
|
||||
renderBody();
|
||||
};
|
||||
}
|
||||
|
||||
// 会话切换后由 sessions.js 调用,避免树停在旧会话。
|
||||
window.refreshFileTree = function () {
|
||||
if (!enabled) return;
|
||||
invalidate(false);
|
||||
const drawer = $('drawer');
|
||||
// 只在实际显示「文件」页时才去拉取,避免后台白跑一层目录请求。
|
||||
if (drawer && drawer.classList.contains('drawer-mode-files')) renderBody();
|
||||
};
|
||||
|
||||
(function init() {
|
||||
if (!$('drawer-actions')) return;
|
||||
req('GET', '/api/local/info').then(function (info) {
|
||||
if (!info || !info.enabled) return;
|
||||
enabled = true;
|
||||
document.body.classList.add('local-mode');
|
||||
$('files-tree').classList.remove('hidden');
|
||||
$('da-files').classList.remove('hidden'); // 本地才有文件树
|
||||
exposeShowFileTree();
|
||||
}).catch(function () { /* 服务器形态:接口 404,保持原样 */ });
|
||||
})();
|
||||
})();
|
||||
@@ -76,6 +76,72 @@ router.post('/:id/files', upload.array('files', 10), function (req, res) {
|
||||
}
|
||||
});
|
||||
|
||||
// ---------- 会话文件树(桌面端左栏唯一浏览入口) ----------
|
||||
// 为什么不复用 GET /:id/files:那个接口同步递归整棵目录,会话里一旦有大项目 /
|
||||
// node_modules,单线程 core 会被卡死(连 SSE 一起挂)。这里一律「一次一层」、
|
||||
// 忽略大目录、限量返回,客户端展开才请求下一层。
|
||||
const TREE_IGNORE_DIRS = new Set([
|
||||
'node_modules', '.git', 'dist', 'build', 'out', 'target', '.venv', 'venv',
|
||||
'__pycache__', '.next', '.cache', 'coverage', '.idea', '.vscode',
|
||||
]);
|
||||
const TREE_MAX = 500;
|
||||
|
||||
// 相对路径 → 会话根内的绝对路径。字面 + realpath 双重校验,防符号链接跳出会话目录。
|
||||
function resolveTreeAbs(rootAbs, rel) {
|
||||
const p = String(rel || '').replace(/^[\\/]+/, '');
|
||||
const abs = p ? path.resolve(rootAbs, p) : rootAbs;
|
||||
const inside = (root, x) => x === root || x.startsWith(root + path.sep);
|
||||
if (!inside(rootAbs, abs)) throw new Error('路径越界');
|
||||
let realAbs = abs, realRoot = rootAbs;
|
||||
try { realAbs = fs.realpathSync(abs); } catch (_) {}
|
||||
try { realRoot = fs.realpathSync(rootAbs); } catch (_) {}
|
||||
if (!inside(realRoot, realAbs)) throw new Error('路径越界');
|
||||
return abs;
|
||||
}
|
||||
|
||||
router.get('/:id/tree', function (req, res) {
|
||||
const session = engine.getSession(req.params.id);
|
||||
if (!session) return res.status(404).json({ error: '会话不存在' });
|
||||
const rootAbs = path.resolve(engine.taskDirOf(req.params.id));
|
||||
const rel = String(req.query.dir || '').replace(/^[\\/]+/, '').replace(/\/+$/, '');
|
||||
let abs;
|
||||
try { abs = resolveTreeAbs(rootAbs, rel); }
|
||||
catch (err) { return res.status(400).json({ error: err.message }); }
|
||||
let st;
|
||||
try { st = fs.statSync(abs); } catch (_) { return res.status(404).json({ error: '目录不存在' }); }
|
||||
if (!st.isDirectory()) return res.status(400).json({ error: '不是目录' });
|
||||
const showHidden = req.query.hidden === '1';
|
||||
const entries = [];
|
||||
try {
|
||||
for (const e of fs.readdirSync(abs, { withFileTypes: true })) {
|
||||
if (!showHidden && e.name.startsWith('.')) continue;
|
||||
const childRel = rel ? rel + '/' + e.name : e.name;
|
||||
if (e.isSymbolicLink()) { entries.push({ name: e.name, rel: childRel, type: 'link' }); continue; }
|
||||
if (e.isDirectory()) {
|
||||
entries.push({ name: e.name, rel: childRel, type: 'dir', ignored: TREE_IGNORE_DIRS.has(e.name) });
|
||||
} else if (e.isFile()) {
|
||||
let size = null;
|
||||
try { size = fs.statSync(path.join(abs, e.name)).size; } catch (_) {}
|
||||
entries.push({ name: e.name, rel: childRel, type: 'file', size: size });
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
return res.status(500).json({ error: String((err && err.message) || err) });
|
||||
}
|
||||
// 排序:正常目录 → 正常文件 → 被忽略的大目录(灰显沉底);同类按名称
|
||||
entries.sort((a, b) =>
|
||||
(a.ignored ? 1 : 0) - (b.ignored ? 1 : 0) ||
|
||||
(a.type === 'dir' ? 0 : 1) - (b.type === 'dir' ? 0 : 1) ||
|
||||
(a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
||||
res.json({
|
||||
root: rootAbs,
|
||||
dir: rel,
|
||||
total: entries.length,
|
||||
capped: entries.length > TREE_MAX,
|
||||
entries: entries.slice(0, TREE_MAX),
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/:id/files', function (req, res) {
|
||||
try {
|
||||
const session = engine.getSession(req.params.id);
|
||||
|
||||
@@ -66,15 +66,9 @@ async function pickDir() {
|
||||
throw Object.assign(new Error('没有可用的目录选择器(Linux 请安装 zenity 或 kdialog)'), { status_code: 501 });
|
||||
}
|
||||
|
||||
// ---------- 只读文件树 / 原生文件管理器(仅本机会话目录内) ----------
|
||||
// 目录可能是真实项目,含 node_modules 等大目录:一律「一次只列一层」,客户端展开才请求。
|
||||
// 绝不能复用 listTaskFiles 的整棵递归——同步递归会卡死单线程 core,连 SSE 一起挂。
|
||||
const IGNORE_DIRS = new Set([
|
||||
'node_modules', '.git', 'dist', 'build', 'out', 'target', '.venv', 'venv',
|
||||
'__pycache__', '.next', '.cache', 'coverage', '.idea', '.vscode',
|
||||
]);
|
||||
const TREE_MAX = 500;
|
||||
|
||||
// ---------- 原生文件管理器(仅本机会话目录内) ----------
|
||||
// 文件树的列表接口已上收为通用的 GET /api/sessions/:id/tree(桌面端服务器形态也用),
|
||||
// 这里只剩「在 Finder / 终端中打开」这两个只能开在本机的动作。
|
||||
function isInside(rootAbs, abs) {
|
||||
return abs === rootAbs || abs.startsWith(rootAbs + path.sep);
|
||||
}
|
||||
@@ -96,44 +90,6 @@ function resolveInside(sessionId, rel) {
|
||||
return { rootAbs: rootAbs, abs: abs };
|
||||
}
|
||||
|
||||
// 懒加载单层目录。ignored 的目录只回标记、前端灰显且不展开(避免 node_modules 那种灾难)。
|
||||
router.get('/tree', (req, res) => {
|
||||
let abs;
|
||||
try { abs = resolveInside(req.query.session, req.query.dir || '').abs; }
|
||||
catch (err) { return sendErr(res, err); }
|
||||
let st;
|
||||
try { st = fs.statSync(abs); } catch (_) { return res.status(404).json({ error: '目录不存在' }); }
|
||||
if (!st.isDirectory()) return res.status(400).json({ error: '不是目录' });
|
||||
const showHidden = req.query.hidden === '1';
|
||||
const rel = String(req.query.dir || '').replace(/^[\\/]+/, '').replace(/\/+$/, '');
|
||||
const entries = [];
|
||||
try {
|
||||
for (const e of fs.readdirSync(abs, { withFileTypes: true })) {
|
||||
if (!showHidden && e.name.startsWith('.')) continue;
|
||||
const childRel = rel ? rel + '/' + e.name : e.name;
|
||||
if (e.isSymbolicLink()) { entries.push({ name: e.name, rel: childRel, type: 'link' }); continue; }
|
||||
if (e.isDirectory()) {
|
||||
entries.push({ name: e.name, rel: childRel, type: 'dir', ignored: IGNORE_DIRS.has(e.name) });
|
||||
} else if (e.isFile()) {
|
||||
let size = null;
|
||||
try { size = fs.statSync(path.join(abs, e.name)).size; } catch (_) {}
|
||||
entries.push({ name: e.name, rel: childRel, type: 'file', size: size });
|
||||
}
|
||||
}
|
||||
} catch (err) { return sendErr(res, err); }
|
||||
entries.sort((a, b) =>
|
||||
(a.ignored ? 1 : 0) - (b.ignored ? 1 : 0) ||
|
||||
(a.type === 'dir' ? 0 : 1) - (b.type === 'dir' ? 0 : 1) ||
|
||||
(a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
||||
res.json({
|
||||
root: path.resolve(engine.taskDirOf(String(req.query.session || ''))),
|
||||
dir: rel,
|
||||
total: entries.length,
|
||||
capped: entries.length > TREE_MAX,
|
||||
entries: entries.slice(0, TREE_MAX),
|
||||
});
|
||||
});
|
||||
|
||||
// execFile 异步(同步会阻塞事件循环);open/xdg-open 一般秒回,10s 兜底。
|
||||
function runOpen(cmd, args) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
+8
-4
@@ -32,12 +32,13 @@
|
||||
|
||||
## 3. 本次改动清单
|
||||
|
||||
### 后端(`app/`,只 2 个文件)
|
||||
### 后端(`app/`)
|
||||
|
||||
| 文件 | 改动 |
|
||||
|---|---|
|
||||
| `app/server/server.js` | 读 `HOST` 环境变量;有值则 `server.listen(PORT, HOST, cb)`,无值维持 `listen(PORT, cb)`(= `0.0.0.0`)。启动日志带上 host |
|
||||
| `app/server/tools/executor.js` | 新增 `WIDE_SCOPE`(由 `AGENT_FILE_SCOPE=workspace` 打开)与 `fsRoot(ctx)`;`resolveInTask()` 增加可选第三参 `rootDir`;6 个业务工具的调用点改为传 `fsRoot(ctx)`;`recordFile()` 跳过会话目录之外的文件 |
|
||||
| `app/server/routes/files.js` | 新增 `GET /:id/tree`(桌面端左栏文件树用:单层懒加载、忽略大目录、上限 500、越界双重校验) |
|
||||
|
||||
`resolveInTask` 的两个分叉点:
|
||||
|
||||
@@ -48,10 +49,13 @@
|
||||
### 刻意没有动的(重要)
|
||||
|
||||
- `resolvePastPath()`(`@<会话id>/...` 跨会话只读)—— 仍锁在对方会话目录
|
||||
- `routes/files.js`、`routes/public-files.js`、`routes/data-page.js` —— HTTP 文件 API 与公网短链
|
||||
仍按会话目录校验,前台不能借本地模式越权
|
||||
- `routes/public-files.js`、`routes/data-page.js` —— 公网短链与 data-page 仍按会话目录校验
|
||||
- `mint_public_link` —— 仍会话目录语义(本地本来也没配 `public_link.base_url`)
|
||||
- 前端 —— 一行没改;首次配置靠终端提示「用户区 → 设置 → 模型池」
|
||||
- 前端 —— 本地模式没有专属前端代码;左栏文件树是桌面端共用的(见下)
|
||||
|
||||
> 追加:文件树最初只给 local 用,后来上收为桌面端通用能力 —— `/tree` 移到 `routes/files.js`,
|
||||
> 服务器形态的桌面端也用它取代「输入行右侧的旧文件面板」(含下载/删除);`reveal`、`terminal`、
|
||||
> `pick-dir` 仍只在 `AGENT_LOCAL_PICK=1` 下挂载(它们开的是跑 core 那台机器的窗口)。
|
||||
|
||||
### 新增 `local/`
|
||||
|
||||
|
||||
Reference in New Issue
Block a user