137 lines
6.3 KiB
JavaScript
137 lines
6.3 KiB
JavaScript
'use strict';
|
|
window.AssistantOperations = {
|
|
mount(container, { api, esc, formatDate, user, settings, isActive, onRepair }) {
|
|
const stepNames = {
|
|
management_query: '查询受控管理资源',
|
|
schedule_create: '创建原生定时任务',
|
|
schedule_update: '修改原生定时任务',
|
|
schedule_delete: '删除原生定时任务',
|
|
configuration_review: '审查设置差异与风险',
|
|
configuration_authorized: '用户授权设置修改',
|
|
configuration_rejected: '用户拒绝修改',
|
|
configuration_saved: '保存设置',
|
|
configuration_result: '核实设置结果',
|
|
sample_progress: '逐项采样',
|
|
context: '读取上下文',
|
|
model: '模型分析',
|
|
prepare: '准备工作区',
|
|
files_written: '写入建议文件',
|
|
commit: 'Git提交',
|
|
validate: '运行测试',
|
|
rollback: '恢复基线',
|
|
review: '提交审查',
|
|
restart_recovery: '重启核验',
|
|
execute: '执行受控操作',
|
|
reject: '请求被拒绝',
|
|
error: '异常',
|
|
},
|
|
statusNames = {
|
|
reviewed: '已审查,待授权',
|
|
running: '执行中',
|
|
success: '已完成',
|
|
ready_for_review: '测试通过,待审查',
|
|
rolled_back: '已回退',
|
|
needs_review: '待人工核验',
|
|
cancelled: '已取消',
|
|
error: '失败',
|
|
rejected: '已拒绝',
|
|
};
|
|
const host = document.createElement('section');
|
|
host.className = 'card mb-3';
|
|
const layout = container.querySelector('.assistant-layout');
|
|
if (layout) layout.before(host);
|
|
else container.append(host);
|
|
host.innerHTML =
|
|
'<div class="card-header"><h3 class="card-title">AI 操作记录</h3><button type="button" class="btn btn-sm ms-auto" data-operation-refresh>刷新操作记录</button></div><div class="card-body"><div data-operation-list class="small text-secondary">正在读取操作记录…</div>' +
|
|
(user.role === 'admin'
|
|
? '<details class="mt-3"><summary>提出代码修复</summary><form data-repair-form class="mt-3"><label class="form-label" for="repair-paths">已跟踪源码文件(相对路径,每行一个)</label><textarea id="repair-paths" class="form-control font-monospace" rows="2" required placeholder="src/modules/example.cjs"></textarea><label class="form-label mt-3" for="repair-goal">修复目标</label><textarea id="repair-goal" class="form-control" rows="2" maxlength="4000" required></textarea><p class="small text-secondary mt-2">仅将选定源码发送给已配置的模型。先生成可审查建议,不直接修改代码;执行时在独立 Git 工作区运行本机测试。工作区不是操作系统沙箱,通过后仍需人工审查合并,不自动部署。</p><button class="btn btn-primary" type="submit" ' +
|
|
(!settings.allowActions || !settings.allowCodeRepair ? 'disabled' : '') +
|
|
'>生成修复建议</button>' +
|
|
(!settings.allowCodeRepair
|
|
? '<span class="small text-secondary ms-2">请先在助手设置开启代码修复并指定源码仓库。</span>'
|
|
: '') +
|
|
'</form></details>'
|
|
: '') +
|
|
'<div class="text-danger mt-2" data-operation-error role="alert"></div></div>';
|
|
const error = host.querySelector('[data-operation-error]');
|
|
let rows = [],
|
|
busy = false;
|
|
const more = document.createElement('button');
|
|
more.type = 'button';
|
|
more.className = 'btn btn-sm mt-2';
|
|
more.textContent = '加载更早记录';
|
|
more.hidden = true;
|
|
host.querySelector('[data-operation-list]').after(more);
|
|
async function refresh(append = false) {
|
|
if (busy) return;
|
|
busy = true;
|
|
more.disabled = true;
|
|
try {
|
|
const { events } = await api(
|
|
'/assistant/operations' + (append && rows.length ? '?beforeId=' + rows.at(-1).id : ''),
|
|
);
|
|
if (!isActive() || !host.isConnected) return;
|
|
rows = append ? [...rows, ...events] : events;
|
|
more.hidden = events.length < 100;
|
|
host.querySelector('[data-operation-list]').innerHTML = rows.length
|
|
? '<div class="table-responsive"><table class="table table-sm"><thead><tr><th>时间 / 操作编号</th><th>步骤 / 状态</th><th>结果与依据</th></tr></thead><tbody>' +
|
|
rows
|
|
.map(
|
|
(e) =>
|
|
'<tr><td>' +
|
|
esc(formatDate(e.createdAt)) +
|
|
' · ' +
|
|
esc(e.actorLabel || e.actor) +
|
|
'<div class="font-monospace small">' +
|
|
esc(e.operationId) +
|
|
'</div></td><td>' +
|
|
esc(stepNames[e.step] || e.step) +
|
|
' / ' +
|
|
esc(statusNames[e.status] || e.status) +
|
|
'</td><td>' +
|
|
esc(e.detail || '') +
|
|
(e.resultCommit
|
|
? '<div>提交:<code>' + esc(e.resultCommit) + '</code></div>'
|
|
: '') +
|
|
(e.recovery ? '<div>恢复:' + esc(e.recovery) + '</div>' : '') +
|
|
'</td></tr>',
|
|
)
|
|
.join('') +
|
|
'</tbody></table></div>'
|
|
: '暂无操作记录。诊断和恢复操作均会记录,代码修改另有 Git 提交。';
|
|
} catch (e) {
|
|
if (isActive()) error.textContent = e.message;
|
|
} finally {
|
|
busy = false;
|
|
more.disabled = false;
|
|
}
|
|
}
|
|
host.querySelector('[data-operation-refresh]').onclick = () => refresh();
|
|
more.onclick = () => refresh(true);
|
|
void refresh();
|
|
const form = host.querySelector('[data-repair-form]');
|
|
if (form)
|
|
form.onsubmit = async (e) => {
|
|
e.preventDefault();
|
|
const button = e.submitter;
|
|
button.disabled = true;
|
|
error.textContent = '';
|
|
try {
|
|
await onRepair(
|
|
form.querySelector('#repair-goal').value,
|
|
form
|
|
.querySelector('#repair-paths')
|
|
.value.split(/\r?\n/)
|
|
.map((s) => s.trim())
|
|
.filter(Boolean),
|
|
);
|
|
if (isActive()) await refresh();
|
|
} catch (cause) {
|
|
if (isActive()) error.textContent = cause.message;
|
|
} finally {
|
|
if (button.isConnected) button.disabled = false;
|
|
}
|
|
};
|
|
},
|
|
};
|