201 lines
13 KiB
JavaScript
201 lines
13 KiB
JavaScript
'use strict';
|
|
window.AssistantLayout = {
|
|
mount(host, { settings, usage, esc }) {
|
|
host.innerHTML = `<section class="assistant-workspace" aria-label="DSH 诊断工作区"><header class="aw-header"><button class="btn btn-sm" type="button" data-aw-left aria-label="会话列表" aria-expanded="true"><svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" aria-hidden="true"><path d="M4 6h16M4 12h16M4 18h16"/></svg></button><a href="#tools" class="btn btn-outline-primary aw-back" aria-label="返回工具"><svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M19 12H5m6-6-6 6 6 6"/></svg><span>返回工具</span></a><strong>DSH 助手</strong><span class="aw-model">${esc(settings.model || '模型未配置')}</span><span class="aw-connection" role="status">${settings.runtimePath ? '诊断已配置' : '待配置'}</span><span class="aw-usage">今日 ${usage.calls} / ${usage.limit}</span><button class="btn btn-sm" type="button" data-aw-right aria-label="右侧栏" aria-controls="assistant-context" aria-expanded="true" title="收起右侧栏"><svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3" y="3" width="18" height="18" rx="3"/><path d="M15 3v18"/></svg></button></header><div class="aw-body"><aside class="aw-sidebar" aria-label="个人会话"><div class="aw-sidebar-top"><button class="btn btn-primary w-100" id="assistant-new">新建对话</button><button class="btn w-100" type="button" id="assistant-schedules"><svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg> 定时任务</button><label class="visually-hidden" for="assistant-search">搜索会话</label><input class="form-control" id="assistant-search" maxlength="100" placeholder="搜索会话标题"><label class="visually-hidden" for="assistant-scope">会话范围</label><select class="form-select" id="assistant-scope"><option value="active">活跃会话</option><option value="archived">已归档</option><option value="deleted">回收站</option></select></div><div id="assistant-conversations" class="aw-conversations"></div><div class="aw-pager"><button class="btn btn-sm" id="assistant-prev" type="button">上一页</button><span id="assistant-page" class="small"></span><button class="btn btn-sm" id="assistant-next" type="button">下一页</button></div><div id="assistant-search-note" class="small text-secondary px-3 pb-2"></div></aside><div class="aw-drag aw-drag-left" role="separator" tabindex="0" aria-label="调整会话栏宽度" aria-orientation="vertical"></div><main class="assistant-chat aw-chat"><div class="aw-chat-header"><h2 id="assistant-title">新对话</h2><div id="assistant-lifecycle" class="btn-list"></div></div><div class="small text-secondary px-3" id="assistant-history-note"></div><div id="assistant-messages" class="assistant-messages" role="log" aria-live="polite"></div><button id="assistant-new-progress" class="aw-scroll-latest" type="button" aria-label="滚动到底部" title="滚动到底部" hidden><svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 5v14m-6-6 6 6 6-6"/></svg></button><footer class="aw-composer"><div id="assistant-suggestions" class="aw-suggestions">${[
|
|
['连接健康', 'status'],
|
|
['扫描异常', 'bpm'],
|
|
['归档失败', 'archive'],
|
|
['通知阻断', 'blockers'],
|
|
]
|
|
.map(
|
|
([label, command]) =>
|
|
`<button class="btn btn-sm" type="button" data-diagnostic-start="${command}">${label}</button>`,
|
|
)
|
|
.join(
|
|
'',
|
|
)}</div><form id="assistant-compose"><label class="visually-hidden" for="assistant-input">发送给 DSH</label><div class="aw-input-wrap"><textarea id="assistant-input" class="form-control" rows="2" maxlength="4000" placeholder="描述运行异常,或输入 / 选择诊断命令"></textarea><div id="assistant-command-menu" class="aw-command-menu" hidden></div></div><div id="assistant-attachment-list" class="aw-attachments" aria-live="polite"></div><p id="assistant-attachment-note" class="small text-secondary" hidden>附件在本机解析并暂存,点击发送才交给当前模型。最多 3 个、每个 2 MB、合计 40000 字;敏感凭据会脱敏。PDF 仅提取文字,扫描图片需先 OCR。</p><input id="assistant-files" type="file" accept=".txt,.log,.csv,.md,.json,.yaml,.yml,.xml,.ini,.conf,.js,.ts,.cjs,.mjs,.py,.css,.html,.pdf" multiple hidden><div class="aw-compose-tools"><button class="btn btn-sm aw-attach-button" type="button" id="assistant-attach" aria-label="添加附件" title="添加文件或图片"><svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48"/></svg></button><button class="btn btn-sm" type="button" id="assistant-commands" aria-label="诊断命令">/</button><button class="aw-submit-icon aw-stop-icon" type="button" id="assistant-stop" aria-label="停止回复" title="停止回复" hidden><svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true"><rect x="6" y="6" width="12" height="12" rx="2" fill="currentColor"/></svg></button><button class="aw-submit-icon" type="submit" id="assistant-send" aria-label="发送" title="发送" disabled><svg viewBox="0 0 24 24" width="22" height="22" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 19V5m-6 6 6-6 6 6"/></svg></button></div><div class="aw-shortcut-note" aria-hidden="true">Enter 发送 · Shift + Enter 换行</div><div id="assistant-send-hint" class="small text-warning mt-2" role="status"></div><div id="assistant-error" class="small text-danger" role="alert"></div></form></footer></main><div class="aw-drag aw-drag-right" role="separator" tabindex="0" aria-label="调整上下文栏宽度" aria-orientation="vertical"></div><aside class="aw-context" id="assistant-context" aria-label="诊断详情"><div class="aw-context-header"><div class="aw-context-tabs" role="tablist">${[
|
|
['overview', '概况'],
|
|
['evidence', '证据'],
|
|
['actions', '操作'],
|
|
['events', '记录'],
|
|
['schedules', '任务'],
|
|
]
|
|
.map(
|
|
([id, label]) =>
|
|
`<button type="button" role="tab" id="aw-tab-${id}" aria-controls="aw-panel-${id}" aria-selected="${id === 'overview'}" data-context-tab="${id}">${label}</button>`,
|
|
)
|
|
.join(
|
|
'',
|
|
)}</div><button type="button" class="aw-context-close" data-aw-close-right aria-label="收起右侧栏" title="收起右侧栏"><svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="m9 6 6 6-6 6M20 4v16"/></svg></button></div>${['overview', 'evidence', 'actions', 'events', 'schedules'].map((id) => `<section id="aw-panel-${id}" role="tabpanel" aria-labelledby="aw-tab-${id}" ${id === 'overview' ? '' : 'hidden'}></section>`).join('')}</aside></div></section>`;
|
|
const root = host.querySelector('.assistant-workspace'),
|
|
body = root.querySelector('.aw-body'),
|
|
leftButton = root.querySelector('[data-aw-left]'),
|
|
rightButton = root.querySelector('[data-aw-right]'),
|
|
closeButton = root.querySelector('[data-aw-close-right]');
|
|
let width = 0,
|
|
leftOpen = true,
|
|
rightOpen = true,
|
|
leftWidth = 264,
|
|
rightWidth = 320,
|
|
lastWidth = 0;
|
|
const shell = document.querySelector('#app>.page');
|
|
shell?.classList.add('dsh-focused');
|
|
function measure() {
|
|
root.style.height =
|
|
Math.max(
|
|
240,
|
|
(window.visualViewport?.height || innerHeight) - root.getBoundingClientRect().top,
|
|
) + 'px';
|
|
width = root.clientWidth;
|
|
if (width !== lastWidth) {
|
|
if (width < 880) {
|
|
leftOpen = false;
|
|
rightOpen = false;
|
|
} else if (width < 1180) {
|
|
leftOpen = false;
|
|
rightOpen = false;
|
|
} else if (!lastWidth) {
|
|
leftOpen = true;
|
|
rightOpen = true;
|
|
}
|
|
lastWidth = width;
|
|
}
|
|
apply();
|
|
}
|
|
function apply() {
|
|
const narrow = width < 880;
|
|
const columns = window.AssistantNative.computeColumns(
|
|
width,
|
|
leftOpen ? leftWidth : 0,
|
|
rightOpen ? rightWidth : 0,
|
|
narrow ? 0 : 56,
|
|
);
|
|
if (!narrow && rightOpen && columns.rightbar === 0) rightOpen = false;
|
|
root.classList.toggle('aw-narrow', narrow);
|
|
root.classList.toggle('aw-left-open', leftOpen);
|
|
root.classList.toggle('aw-right-open', rightOpen);
|
|
root.classList.toggle('aw-left-collapsed', !leftOpen);
|
|
leftButton.setAttribute('aria-expanded', String(leftOpen));
|
|
rightButton.setAttribute('aria-expanded', String(rightOpen));
|
|
rightButton.title = rightOpen ? '收起右侧栏' : '展开右侧栏';
|
|
body.style.gridTemplateColumns = narrow
|
|
? 'minmax(0,1fr)'
|
|
: `${columns.sidebar}px ${leftOpen ? '5px' : '0px'} minmax(0,1fr) ${rightOpen ? '5px ' + columns.rightbar + 'px' : '0px 0px'}`;
|
|
root.querySelector('.aw-sidebar').inert = !leftOpen;
|
|
root.querySelector('.aw-context').inert = !rightOpen;
|
|
}
|
|
function closeRight() {
|
|
rightOpen = false;
|
|
apply();
|
|
rightButton.focus();
|
|
}
|
|
closeButton.onclick = closeRight;
|
|
leftButton.onclick = () => {
|
|
leftOpen = !leftOpen;
|
|
if (width < 880 && leftOpen) rightOpen = false;
|
|
apply();
|
|
if (leftOpen) root.querySelector('#assistant-search').focus();
|
|
};
|
|
rightButton.onclick = () => {
|
|
rightOpen = !rightOpen;
|
|
if (width < 1180 && rightOpen) leftOpen = false;
|
|
apply();
|
|
if (rightOpen) root.querySelector('[data-context-tab][aria-selected=true]').focus();
|
|
};
|
|
const onKey = (event) => {
|
|
if (
|
|
event.key === 'Escape' &&
|
|
!root.querySelector('dialog[open]') &&
|
|
root.querySelector('#assistant-command-menu').hidden
|
|
) {
|
|
if (width < 880 && leftOpen) {
|
|
leftOpen = false;
|
|
apply();
|
|
leftButton.focus();
|
|
} else if (rightOpen) {
|
|
rightOpen = false;
|
|
apply();
|
|
rightButton.focus();
|
|
}
|
|
}
|
|
};
|
|
root.addEventListener('keydown', onKey);
|
|
const disposers = [];
|
|
for (const [side, selector] of [
|
|
['left', '.aw-drag-left'],
|
|
['right', '.aw-drag-right'],
|
|
]) {
|
|
const bar = root.querySelector(selector);
|
|
let drag = null;
|
|
const move = (event) => {
|
|
if (!drag) return;
|
|
if (side === 'left')
|
|
leftWidth = window.AssistantNative.clampWidth(
|
|
drag.value + event.clientX - drag.x,
|
|
264,
|
|
420,
|
|
);
|
|
else
|
|
rightWidth = window.AssistantNative.clampWidth(
|
|
drag.value - event.clientX + drag.x,
|
|
300,
|
|
width * 0.7,
|
|
);
|
|
apply();
|
|
};
|
|
bar.onpointerdown = (event) => {
|
|
drag = { x: event.clientX, value: side === 'left' ? leftWidth : rightWidth };
|
|
bar.setPointerCapture(event.pointerId);
|
|
};
|
|
bar.onpointermove = move;
|
|
bar.onpointerup = bar.onpointercancel = () => {
|
|
drag = null;
|
|
};
|
|
bar.onkeydown = (event) => {
|
|
if (!['ArrowLeft', 'ArrowRight'].includes(event.key)) return;
|
|
event.preventDefault();
|
|
const delta = event.key === 'ArrowRight' ? 16 : -16;
|
|
if (side === 'left')
|
|
leftWidth = window.AssistantNative.clampWidth(leftWidth + delta, 264, 420);
|
|
else rightWidth = window.AssistantNative.clampWidth(rightWidth - delta, 300, width * 0.7);
|
|
apply();
|
|
};
|
|
disposers.push(() => {
|
|
bar.onpointerdown =
|
|
bar.onpointermove =
|
|
bar.onpointerup =
|
|
bar.onpointercancel =
|
|
bar.onkeydown =
|
|
null;
|
|
});
|
|
}
|
|
const observer = new ResizeObserver(measure);
|
|
observer.observe(root);
|
|
window.visualViewport?.addEventListener('resize', measure);
|
|
measure();
|
|
return {
|
|
root,
|
|
showRight() {
|
|
rightOpen = true;
|
|
if (width < 1180) leftOpen = false;
|
|
apply();
|
|
},
|
|
closeLeft() {
|
|
if (width < 880) {
|
|
leftOpen = false;
|
|
apply();
|
|
}
|
|
},
|
|
closeRight,
|
|
dispose() {
|
|
closeButton.onclick = null;
|
|
observer.disconnect();
|
|
window.visualViewport?.removeEventListener('resize', measure);
|
|
for (const dispose of disposers) dispose();
|
|
root.removeEventListener('keydown', onKey);
|
|
shell?.classList.remove('dsh-focused');
|
|
},
|
|
};
|
|
},
|
|
};
|