设置页改密码(prompt弹窗,免原密码,立即生效);上传附件改为随下一条消息统一告知AI

This commit is contained in:
2026-09-02 07:10:11 +08:00
parent c21281a135
commit 192db98c37
10 changed files with 235 additions and 34 deletions
+3 -2
View File
@@ -92,14 +92,15 @@ function confirmModal(title, text, okText, danger) {
});
}
function confirmClose(v) { _dlgSettle(v); }
// 输入框:resolve(字符串) 或取消 resolve(null)
function promptModal(title, placeholder, initial) {
// 输入框:resolve(字符串) 或取消 resolve(null);opts.type 可指定 password 等输入类型
function promptModal(title, placeholder, initial, opts) {
return new Promise(resolve => {
_dlgResolve = resolve;
$('prompt-title').textContent = title || '请输入';
const inp = $('prompt-input');
inp.placeholder = placeholder || '';
inp.value = initial || '';
inp.type = opts && opts.type ? opts.type : 'text';
$('prompt-modal').classList.remove('hidden');
setTimeout(() => inp.focus(), 60);
});
+28 -2
View File
@@ -57,6 +57,30 @@ function kvPlain(label, v) {
return '<div class="cfg-kv"><label>' + esc(label) + '</label><b>' + esc(dispVal(v)) + '</b></div>';
}
// 账号卡(仅启用认证时显示):当前登录 + 修改密码(prompt 弹窗,免原密码,立即生效)
function accountCard(me) {
return '<div class="settings-card"><h4>账号</h4>' +
kvPlain('当前登录', me) +
'<button class="primary-btn" id="pwd-btn" style="width:100%;margin-top:8px" onclick="cfgUI.changePassword()">修改密码</button>' +
'<div class="cfg-note">修改立即生效并写回 config.yml;其他已登录设备需重新登录时才用新密码。</div></div>';
}
async function changePassword() {
const btn = $('pwd-btn');
if (btn) btn.disabled = true;
try {
const newP = await promptModal('修改密码', '新密码,至少 6 位', '', { type: 'password' });
if (newP == null) return;
if (newP.length < 6) { toast('新密码至少 6 位'); return; }
await req('POST', '/api/password', { new_password: newP });
toast('密码已修改,立即生效');
} catch (e) {
toast('修改失败:' + e.message);
} finally {
if (btn) btn.disabled = false;
}
}
function svcCard(st) {
let html = '<div class="settings-card"><h4>服务状态' +
(pendingKeys.size ? ' <span class="badge held">待重启生效</span>' : '') + '</h4>';
@@ -103,12 +127,13 @@ async function loadSettings() {
if (!el) return;
el.innerHTML = '<div class="empty-tip">加载中…</div>';
try {
const [st, cg] = await Promise.all([
const [st, cg, me] = await Promise.all([
req('GET', '/api/system/status'),
req('GET', '/api/config'),
req('GET', '/api/me').catch(function () { return { user: null }; }), // 未启用认证时无此路由
]);
effCfg = cg.effective || {};
el.innerHTML = svcCard(st) + formCard();
el.innerHTML = (st.auth_enabled && me && me.user ? accountCard(me.user) : '') + svcCard(st) + formCard();
} catch (e) {
el.innerHTML = '<div class="empty-tip">加载失败:' + esc(e.message) + '</div>';
}
@@ -238,6 +263,7 @@ async function closeCfgEditor() {
window.cfgUI = {
saveForm: saveForm,
changePassword: changePassword,
restart: restart,
open: openCfgEditor,
save: saveCfgEditor,
+105 -12
View File
@@ -152,6 +152,91 @@ async function doSearch() {
} catch (e) { toast('搜索失败:' + e.message); }
}
// ---------- 待发送附件 ----------
// 选定文件后立即传到任务目录,但**不通知 AI**;下一条用户消息发送时,
// 由服务端在正文前拼【系统通知】一并送达(见 routes/messages.js)。
let pendingFiles = []; // { name, size, pct, status: 'up'|'ok'|'err', xhr? }
function renderAttach() {
const row = $('attach-row');
if (!row) return;
row.classList.toggle('hidden', !pendingFiles.length);
row.innerHTML = pendingFiles.map(function (f, i) {
const st = f.status === 'up' ? (f.pct + '%') : f.status === 'ok' ? '✓' : '失败';
return '<span class="attach-chip' + (f.status === 'err' ? ' err' : '') + '">' +
'<span class="nm">' + esc(f.name) + '</span><span class="st">' + st + '</span>' +
'<button class="x" onclick="ui.removeAttach(' + i + ')">×</button></span>';
}).join('');
// 输入框里的回形针图标在上传中换成总进度百分比
const lb = document.getElementById('attach-btn');
const pctEl = $('up-pct');
if (lb && pctEl) {
const ups = pendingFiles.filter(function (f) { return f.status === 'up'; });
lb.classList.toggle('uploading', ups.length > 0);
if (ups.length) {
let done = 0, tot = 0;
for (const f of pendingFiles) { if (f.status === 'err') continue; done += f.size * f.pct / 100; tot += f.size; }
pctEl.textContent = (tot ? Math.round(done / tot * 100) : 0) + '%';
}
}
}
function startUpload(file) {
if (!state.sessionId) return;
const item = { name: file.name, size: file.size, pct: 0, status: 'up' };
pendingFiles.push(item);
renderAttach();
const fd = new FormData();
fd.append('files', file);
const xhr = new XMLHttpRequest();
item.xhr = xhr;
xhr.open('POST', '/api/sessions/' + state.sessionId + '/files');
xhr.upload.onprogress = function (e) {
if (e.lengthComputable) { item.pct = Math.min(99, Math.round(e.loaded / e.total * 100)); renderAttach(); }
};
xhr.onload = function () {
let r = {};
try { r = JSON.parse(xhr.responseText); } catch (_) {}
if (xhr.status === 200 && r.uploaded && r.uploaded[0]) {
item.name = r.uploaded[0].filename; // 以服务端清洗/重命名后的文件名为准
item.size = r.uploaded[0].size;
item.pct = 100;
item.status = 'ok';
} else {
item.status = 'err';
toast('上传失败:' + (r.error || xhr.status));
}
item.xhr = null;
renderAttach();
};
xhr.onerror = function () { item.status = 'err'; item.xhr = null; renderAttach(); toast('上传失败:网络错误'); };
xhr.send(fd);
}
function removeAttach(i) {
const f = pendingFiles[i];
if (!f) return;
if (f.xhr) { try { f.xhr.abort(); } catch (_) {} }
else if (f.status === 'ok' && state.sessionId) {
// 已传完成的附件被移除:连带删掉已落盘的文件,避免任务目录里残留孤儿文件
req('DELETE', '/api/sessions/' + state.sessionId + '/files?path=' + encodeURIComponent(f.name)).catch(function () {});
}
pendingFiles.splice(i, 1);
renderAttach();
}
// 历史消息里的【系统通知】行:展示层拆成附件胶囊行 + 用户原文
function parseUploadNotice(content) {
const m = /^【系统通知】用户上传了文件:([^\n]*)(?:\n([\s\S]*))?$/.exec(content || '');
if (!m) return { text: content || '', files: null };
const files = m[1].split('、').map(function (s) { return s.replace(/(大小[^)]*)$/, '').trim(); }).filter(Boolean);
return { text: m[2] || '', files: files };
}
function attachBubbleLine(names) {
return names && names.length ? '<div class="msg-files">📎 ' + names.map(esc).join(' ') + '</div>' : '';
}
// ---------- 消息渲染 ----------
function md(text) {
if (typeof marked === 'undefined') return esc(text);
@@ -189,9 +274,11 @@ function msgNode(m, cardMap) {
}
const wrap = document.createElement('div');
if (m.role === 'user') {
const p = parseUploadNotice(m.content);
wrap.className = 'msg me';
wrap.innerHTML = '<div class="msg-user">我</div>' +
'<div class="msg-bubble">' + esc(m.content) + '</div>' +
'<div class="msg-bubble">' + attachBubbleLine(p.files) + esc(p.text) + '</div>';
} else if (m.role === 'assistant' && (m.content || !m.tool_calls)) {
'<div class="msg-time">' + fmtTime(m.created_at) + '</div>';
return wrap;
}
@@ -457,6 +544,9 @@ const ui = {
},
async openSession(id) {
state.sessionId = id;
// 待发送附件属于上一个会话的语境,切会话时清空展示(文件已落盘,可在文件面板查看)
pendingFiles = [];
renderAttach();
showChat();
try {
const detail = await req('GET', '/api/sessions/' + id);
@@ -474,19 +564,27 @@ const ui = {
async send() {
const ta = $('msg-input');
const content = ta.value.trim();
if (!content || !state.sessionId) return;
if (!state.sessionId) return;
if (pendingFiles.some(f => f.status === 'up')) { toast('附件上传中,请稍候'); return; }
const atts = pendingFiles.filter(f => f.status === 'ok').map(f => ({ name: f.name, size: f.size }));
if (!content && !atts.length) return;
ta.value = '';
ta.style.height = 'auto';
const box = $('messages');
const tip = box.querySelector('.empty-tip');
if (tip) tip.remove();
try {
const r = await req('POST', '/api/sessions/' + state.sessionId + '/messages', { content });
const body = { content };
if (atts.length) body.attachments = atts;
const r = await req('POST', '/api/sessions/' + state.sessionId + '/messages', body);
const wrap = document.createElement('div');
wrap.className = 'msg me';
wrap.innerHTML = '<div class="msg-user">我' + (r.delivered === 'queued' ? '(排队中,暂存)' : '') + '</div>' +
'<div class="msg-bubble">' + esc(content) + '</div>';
'<div class="msg-bubble">' + attachBubbleLine(atts.map(a => a.name)) + esc(content) + '</div>';
box.appendChild(wrap);
// 发送成功后附件已随消息告知 AI;失败的保留,由用户决定重试或移除
pendingFiles = pendingFiles.filter(f => f.status === 'err');
renderAttach();
// 立即给出回馈:占位气泡 + 等待计时,首个增量/回复到达时移除并填充内容
if (r.delivered !== 'queued') addPendingBubble();
scrollBottom();
@@ -515,16 +613,11 @@ const ui = {
async uploadFiles(ev) {
const input = ev.target;
const files = input.files;
if (!files || !files.length || !state.sessionId) return;
const fd = new FormData();
for (const f of files) fd.append('files', f);
input.value = '';
try {
const r = await req('POST', '/api/sessions/' + state.sessionId + '/files', fd, true);
toast('已上传 ' + r.uploaded.length + ' 个文件');
loadFiles();
} catch (e) { toast('上传失败:' + e.message); }
if (!files || !files.length || !state.sessionId) return;
for (const f of files) startUpload(f); // 多个文件并行上传,进度在回形针图标上汇总
},
removeAttach(i) { removeAttach(i); },
async deleteFile(name) {
if (!await confirmModal('删除文件', '删除文件 ' + name + ' ?', '删除', true)) return;
try {