新建任务可选空闲模型;任务文件面板点外部自动关闭

This commit is contained in:
2026-09-01 12:49:14 +08:00
parent 9ebf4ada91
commit 24b09281f5
5 changed files with 51 additions and 10 deletions
+11 -2
View File
@@ -433,16 +433,25 @@ function sessionContextContent(session) {
].join('\n'); ].join('\n');
} }
function createSession(title) { function createSession(title, preferModelId) {
const id = genTaskId(); const id = genTaskId();
const dir = taskDirOf(id); const dir = taskDirOf(id);
fs.mkdirSync(dir, { recursive: true }); fs.mkdirSync(dir, { recursive: true });
db.prepare( db.prepare(
"INSERT INTO sessions (id, title, status, phase, model_id, created_at, updated_at) VALUES (?,?,'queued','planning',NULL,?,?)" "INSERT INTO sessions (id, title, status, phase, model_id, created_at, updated_at) VALUES (?,?,'queued','planning',NULL,?,?)"
).run(id, title || '', now(), now()); ).run(id, title || '', now(), now());
// 指定了模型且它恰好空闲 → 直接绑定,跳过排队;否则回退到 FIFO 队列
const prefer = preferModelId
? db.prepare('SELECT * FROM model_pool WHERE id = ? AND is_busy = 0').get(preferModelId)
: null;
if (prefer) {
bindModel(getSession(id), prefer);
emitStatus(id);
} else {
enqueue(id); enqueue(id);
dispatchQueued(); dispatchQueued();
// 放在 dispatchQueued 之后:若能立即绑到空闲模型,上下文里就能带上模型显示名。 }
// 放在绑定/派发之后:若能立即绑到空闲模型,上下文里就能带上模型显示名。
const session = getSession(id); const session = getSession(id);
saveMessage(id, { role: 'user', name: SESSION_CONTEXT_NAME, content: sessionContextContent(session) }); saveMessage(id, { role: 'user', name: SESSION_CONTEXT_NAME, content: sessionContextContent(session) });
return session; return session;
+6
View File
@@ -258,6 +258,12 @@ body {
padding: 10px 12px; font-size: 15px; margin-bottom: 10px; outline: none; padding: 10px 12px; font-size: 15px; margin-bottom: 10px; outline: none;
} }
.modal input:focus { border-color: #2a5299; } .modal input:focus { border-color: #2a5299; }
.modal select {
width: 100%; border: 1px solid #dde; border-radius: 8px;
padding: 10px 12px; font-size: 15px; margin-bottom: 10px; outline: none;
background: #fff; color: #334; appearance: auto;
}
.modal select:focus { border-color: #2a5299; }
.modal-close-btn { .modal-close-btn {
position: absolute; right: 10px; top: 10px; position: absolute; right: 10px; top: 10px;
background: none; border: none; cursor: pointer; color: #99a; background: none; border: none; cursor: pointer; color: #99a;
+3 -2
View File
@@ -39,7 +39,7 @@
</button> </button>
<span class="chat-title" id="chat-title"></span> <span class="chat-title" id="chat-title"></span>
<div class="header-right"> <div class="header-right">
<button class="icon-btn" onclick="ui.toggleFiles()" title="文件"> <button class="icon-btn" onclick="ui.toggleFiles(event)" title="文件">
<svg viewBox="0 0 24 24"><path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"></path></svg> <svg viewBox="0 0 24 24"><path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"></path></svg>
</button> </button>
</div> </div>
@@ -90,6 +90,7 @@
</button> </button>
<h3>新建任务会话</h3> <h3>新建任务会话</h3>
<input type="text" id="new-title" placeholder="任务标题(留空自动取首条消息)" style="width:100%"> <input type="text" id="new-title" placeholder="任务标题(留空自动取首条消息)" style="width:100%">
<select id="new-model" style="width:100%"><option value="">自动分配(使用任一空闲模型)</option></select>
<button class="primary-btn" onclick="ui.createSession()" style="width:100%">创建</button> <button class="primary-btn" onclick="ui.createSession()" style="width:100%">创建</button>
</div> </div>
</div> </div>
@@ -113,6 +114,6 @@
<div class="toast hidden" id="toast"></div> <div class="toast hidden" id="toast"></div>
<!-- 发版时更新 v 参数,避免移动端浏览器缓存旧 JS --> <!-- 发版时更新 v 参数,避免移动端浏览器缓存旧 JS -->
<script src="/js/app.js?v=20260901a"></script> <script src="/js/app.js?v=20260901b"></script>
</body> </body>
</html> </html>
+27 -3
View File
@@ -401,12 +401,26 @@ async function loadFiles() {
// ---------- 主逻辑 ---------- // ---------- 主逻辑 ----------
const ui = { const ui = {
downloadFile(name) { return downloadFile(name); }, downloadFile(name) { return downloadFile(name); },
openCreate() { $('create-modal').classList.remove('hidden'); $('new-title').focus(); }, async openCreate() {
// 每次打开时刷新一次空闲模型列表
const sel = $('new-model');
try {
const rows = await req('GET', '/api/models');
const free = rows.filter(m => !m.is_busy);
sel.innerHTML = free.length
? '<option value="">自动分配(使用任一空闲模型)</option>' + free.map(m =>
'<option value="' + esc(m.id) + '">' + esc(m.display_name || m.id) + '</option>').join('')
: '<option value="">暂无空闲模型(将排队等待)</option>';
} catch (_) { /* 保留默认选项 */ }
$('create-modal').classList.remove('hidden');
$('new-title').focus();
},
closeCreate() { $('create-modal').classList.add('hidden'); }, closeCreate() { $('create-modal').classList.add('hidden'); },
async createSession() { async createSession() {
try { try {
const title = $('new-title').value.trim(); const title = $('new-title').value.trim();
const s = await req('POST', '/api/sessions', { title }); const model_id = $('new-model') ? $('new-model').value : '';
const s = await req('POST', '/api/sessions', { title, model_id });
this.closeCreate(); this.closeCreate();
$('new-title').value = ''; $('new-title').value = '';
await loadSessions(); await loadSessions();
@@ -472,7 +486,9 @@ const ui = {
scrollBottom(); scrollBottom();
} catch (e) { toast('发送失败:' + e.message); } } catch (e) { toast('发送失败:' + e.message); }
}, },
toggleFiles() { toggleFiles(ev) {
// 阻断冒泡,避免开启面板的同一次点击被「点外部自动关闭」监听器立刻关掉
if (ev) ev.stopPropagation();
state.filePanelOpen = !state.filePanelOpen; state.filePanelOpen = !state.filePanelOpen;
$('files-panel').classList.toggle('hidden', !state.filePanelOpen); $('files-panel').classList.toggle('hidden', !state.filePanelOpen);
if (state.filePanelOpen) loadFiles(); if (state.filePanelOpen) loadFiles();
@@ -513,6 +529,14 @@ const ui = {
logout() { location.href = '/logout'; }, logout() { location.href = '/logout'; },
}; };
// 文件面板展开时,点击面板以外的区域自动关闭
document.addEventListener('click', e => {
if (!state.filePanelOpen) return;
if ($('files-panel').contains(e.target)) return;
state.filePanelOpen = false;
$('files-panel').classList.add('hidden');
});
// ---------- 登录态:显示当前用户;未启用认证(/api/me 404)则隐藏退出按钮 ---------- // ---------- 登录态:显示当前用户;未启用认证(/api/me 404)则隐藏退出按钮 ----------
async function initAuth() { async function initAuth() {
try { try {
+2 -1
View File
@@ -47,7 +47,8 @@ function listTaskFiles(dir) {
router.post('/', wrap((req, res) => { router.post('/', wrap((req, res) => {
const title = (req.body && req.body.title || '').trim(); const title = (req.body && req.body.title || '').trim();
const session = engine.createSession(title); const modelId = (req.body && req.body.model_id || '').trim();
const session = engine.createSession(title, modelId);
res.json(session); res.json(session);
})); }));