Files

182 lines
5.7 KiB
JavaScript

'use strict';
window.AssistantCommands = {
mount(root, { getContext, onStatus, onSelect, onError, onModel }) {
const input = root.querySelector('#assistant-input'),
menu = root.querySelector('#assistant-command-menu');
let rows = [],
index = 0,
mode = 'command',
generation = 0;
const catalog = [
{ name: 'help', label: '帮助', detail: '/help · 固定诊断命令' },
{ name: 'status', label: '运行概况', detail: '/status · 只读刷新' },
{ name: 'model', label: '选择模型', detail: '/model · 按供应商选择模型' },
{ name: 'bpm', label: 'BPM 连接', detail: '/bpm · 选择连接并诊断' },
{ name: 'storage', label: '存储连接', detail: '/storage · 选择存储并诊断' },
{ name: 'archive', label: '归档失败', detail: '/archive · 选择归档记录' },
];
function close() {
generation++;
menu.hidden = true;
menu.replaceChildren();
input.removeAttribute('aria-activedescendant');
input.setAttribute('aria-expanded', 'false');
}
function draw() {
menu.replaceChildren();
menu.hidden = false;
input.setAttribute('aria-expanded', 'true');
for (const [i, row] of rows.entries()) {
const b = document.createElement('button');
b.type = 'button';
b.role = 'option';
b.id = 'aw-command-' + i;
b.setAttribute('aria-selected', String(i === index));
const label = document.createElement('span');
label.textContent = row.label;
const detail = document.createElement('small');
detail.textContent = row.detail || '';
b.append(label, detail);
b.onclick = () => choose(row);
menu.append(b);
}
if (!rows.length) {
const note = document.createElement('div');
note.className = 'p-3 text-secondary small';
note.textContent = '没有可见的匹配目标';
menu.append(note);
}
input.setAttribute('aria-activedescendant', 'aw-command-' + index);
}
function commands(query = '') {
mode = 'command';
rows = window.AssistantNative.filterOptions(
catalog.map((c) => ({ ...c, detail: c.detail + ' ' + c.name })),
query,
);
index = 0;
draw();
}
async function choose(row) {
if (mode !== 'command') {
const command = mode;
close();
onSelect(command, row);
input.focus();
return;
}
const command = row.name;
if (command === 'model') {
close();
input.dataset.diagnostic = '';
input.value = '';
await onModel?.();
return;
}
if (command === 'help') {
close();
input.dataset.diagnostic = '';
onStatus('help', catalog);
input.value = '';
return;
}
if (command === 'status') {
close();
input.dataset.diagnostic = '';
input.value = '';
await onStatus('status');
return;
}
const at = ++generation;
try {
const context = await getContext();
if (at !== generation) return;
mode = command;
rows = (
{ bpm: context.connections, storage: context.servers, archive: context.archives }[
command
] || []
).map((item) => ({
id: item.id,
label: item.name || item.orderNumber || item.id,
detail: item.status || '',
...item,
}));
index = 0;
draw();
input.focus();
} catch (error) {
if (at === generation) onError(error);
}
}
input.setAttribute('aria-controls', 'assistant-command-menu');
input.setAttribute('aria-expanded', 'false');
menu.role = 'listbox';
const onInput = () => {
if (/^\/[^\s]*$/.test(input.value)) commands(input.value.slice(1));
else if (mode === 'command') close();
};
input.addEventListener('input', onInput);
const button = root.querySelector('#assistant-commands');
button.onclick = () => {
if (menu.hidden) {
commands();
input.focus();
} else close();
};
const onKey = (event) => {
if (event.isComposing) return;
if (event.key === 'Escape' && !menu.hidden) {
event.preventDefault();
event.stopPropagation();
close();
input.focus();
return;
}
if (!menu.hidden && ['ArrowDown', 'ArrowUp'].includes(event.key)) {
event.preventDefault();
index =
(index + (event.key === 'ArrowDown' ? 1 : -1) + Math.max(rows.length, 1)) %
Math.max(rows.length, 1);
draw();
return;
}
if (!menu.hidden && ['Tab', 'Enter'].includes(event.key) && !event.shiftKey && rows[index]) {
event.preventDefault();
event.stopImmediatePropagation();
void choose(rows[index]);
}
};
input.addEventListener('keydown', onKey);
return {
close,
async consume() {
const value = input.value.trim();
if (!value.startsWith('/')) return false;
const found = window.AssistantNative.resolveCommand(value.slice(1), catalog);
if (!found) {
close();
onError(new Error('此命令未开放,请使用固定诊断命令。'));
return true;
}
mode = 'command';
await choose(found);
return true;
},
open(name) {
const found = window.AssistantNative.resolveCommand(name, catalog);
if (found) {
mode = 'command';
void choose(found);
}
},
dispose() {
close();
input.removeEventListener('input', onInput);
input.removeEventListener('keydown', onKey);
button.onclick = null;
},
};
},
};