fix: файлы агента — кнопки скачивания прямо в чате (event: files) + бамп версии

Агент создавал файлы в work_dir, но UI о них не узнавал — скачать было
нечем (только скрытая кнопка «Файлы»). Теперь _stream_and_save диффит
work_dir до/после ответа, шлёт event: files и пишет role='files' в
историю — кнопки переживают перезагрузку. Служебные файлы (.py/.log/
скрытые) отфильтрованы, .md разрешён.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
anor
2026-07-03 10:37:03 +10:00
co-authored by Claude Fable 5
parent f084f71c72
commit 025c5dbfb2
3 changed files with 199 additions and 19 deletions
+41 -18
View File
@@ -76,6 +76,8 @@ button { font-family: inherit; }
.msg-file { align-self: flex-start; background: var(--file-bg); border: 1px solid var(--file-border); font-size: .85rem; }
.msg-file a { color: #16a34a; text-decoration: none; font-weight: 500; }
.msg-file a:hover { text-decoration: underline; }
.dl-btn { display: block; width: fit-content; margin: .35rem 0 0; padding: .45rem .8rem; background: #16a34a; color: #fff !important; border-radius: var(--radius); font-weight: 600; }
.dl-btn:hover { background: #15803d; text-decoration: none !important; }
.msg-error { align-self: center; background: var(--error-bg); border: 1px solid var(--error-border); color: #e03131; font-size: .85rem; box-shadow: none; }
.msg-system { align-self: center; background: var(--system-bg); border: none; font-size: .8rem; color: var(--system-text); box-shadow: none; padding: .4rem .8rem; }
.typing { align-self: flex-start; color: var(--text-light); font-style: italic; font-size: .85rem; padding: .4rem 0; }
@@ -978,6 +980,9 @@ async function sendMessage() {
if (!aiText && data.result) {
aiDiv = addMsg('ai', data.result);
}
} else if (eventType === 'files') {
// Агент подготовил файлы — кнопки скачивания прямо в чате
if (data.files && data.files.length) addFilesMsg(data.files);
}
eventType = '';
}
@@ -1019,29 +1024,44 @@ function closeSidebar() {
document.getElementById('sidebar-backdrop').classList.remove('show');
}
function downloadWorkFile(name) {
fetch(API + '/api/files/' + encodeURIComponent(name), {headers: authHeaders()})
.then(r => { if (!r.ok) throw new Error(r.status); return r.blob(); })
.then(b => {
const u = URL.createObjectURL(b);
const l = document.createElement('a');
l.href = u; l.download = name; l.click();
// Немедленный revoke в Firefox может оборвать ещё не начавшееся скачивание
setTimeout(() => URL.revokeObjectURL(u), 30000);
})
.catch(() => addMsg('error', 'Не удалось скачать «' + name + '» — файл больше не существует.'));
}
function fmtSize(size) {
return size >= 1048576 ? (size / 1048576).toFixed(1) + ' МБ' : (size / 1024).toFixed(1) + ' КБ';
}
// Message with download buttons for files the agent prepared. files: [{name, size}]
function addFilesMsg(files, ts, title) {
const div = addMsg('file', title || 'Готовые файлы:', ts);
const copyBtn = div.querySelector('.copy-btn'); // кнопки файлов — над «Копировать»
files.forEach(f => {
const a = document.createElement('a');
a.className = 'dl-btn';
a.textContent = '⬇ ' + f.name + ' (' + fmtSize(f.size) + ')';
a.href = API + '/api/files/' + encodeURIComponent(f.name);
a.onclick = e => { e.preventDefault(); downloadWorkFile(f.name); };
div.insertBefore(a, copyBtn);
});
return div;
}
async function listFiles() {
const r = await fetch(API + '/api/files', {headers: authHeaders()});
if (!r.ok) return;
const files = await r.json();
if (files.length === 0) { addMsg('system', 'Файлов нет.'); return; }
const list = files.map(f => {
const kb = (f.size / 1024).toFixed(1);
return f.name + ' (' + kb + ' KB)';
}).join('\n');
const div = addMsg('file', 'Файлы в рабочей директории:\n' + list);
// Add download links
files.forEach(f => {
const a = document.createElement('a');
a.textContent = ' [скачать ' + f.name + ']';
a.href = API + '/api/files/' + encodeURIComponent(f.name);
a.onclick = e => {
e.preventDefault();
fetch(a.href, {headers: authHeaders()})
.then(r => r.blob())
.then(b => { const u = URL.createObjectURL(b); const l = document.createElement('a'); l.href = u; l.download = f.name; l.click(); });
};
div.appendChild(a);
});
addFilesMsg(files, null, 'Файлы в рабочей директории:');
}
// --- History ---
@@ -1056,6 +1076,9 @@ async function loadHistory() {
if (m.role === 'user') addMsg('user', m.content, m.timestamp);
else if (m.role === 'assistant') addMsg('ai', m.content, m.timestamp);
else if (m.role === 'system') addMsg('system', m.content, m.timestamp);
else if (m.role === 'files') {
try { addFilesMsg(JSON.parse(m.content), m.timestamp); } catch {}
}
}
} catch {}
}