function escapeHtml(text) {
return text.replace(/[&<>"]/g, (char) => {
if (char === "&")
return "&";
if (char === "<")
return "<";
if (char === ">")
return ">";
return """;
});
}
const RISK_CLASS = {
read: "read",
write: "write",
exec: "exec",
network: "network",
};
const STATUS_CLASS = {
ok: "done",
denied: "error",
"approval-required": "tool",
timeout: "cancel",
invalid: "error",
};
function toolRows(snapshot) {
return snapshot.tools
.map((tool) => `
${escapeHtml(tool.name)} |
${tool.risk} |
${tool.needsApproval ? "审批" : "自动"} |
${tool.timeoutMs}ms |
${tool.fields.length} |
${escapeHtml(tool.description)} |
`)
.join("");
}
function scenarioButtons(scenarios) {
return scenarios
.map((scenario) => ``)
.join("");
}
function decisionButton(decision, label, kind = "") {
return ``;
}
function pendingCard(scenario, grants) {
if (!scenario) {
return `从左侧选择一个场景,审批卡会显示参数与可选决定。
`;
}
const grant = grants.includes(scenario.toolId);
return `
${grant ? "已有会话授权" : "待处理"}
${escapeHtml(scenario.label)}
${escapeHtml(scenario.toolId)}
${escapeHtml(JSON.stringify(scenario.args, null, 2))}
${escapeHtml(scenario.note)}
${decisionButton("allow-once", "允许一次", "primary")}
${decisionButton("allow-session", "本会话允许")}
${decisionButton("use-grant", "使用已有授权")}
${decisionButton("deny", "拒绝")}
`;
}
function auditRows(audit) {
if (audit.length === 0) {
return `| 暂无审计记录 |
`;
}
return [...audit]
.reverse()
.map((entry) => `
| ${entry.seq} |
${escapeHtml(entry.scenarioId)} |
${entry.status} |
${escapeHtml(entry.decision)} |
${escapeHtml(entry.approvedBy)} |
${entry.durationMs}ms |
${escapeHtml(entry.summary)} |
`)
.join("");
}
function checkRows(snapshot) {
return snapshot.checks
.map((check) => `
${check.pass ? "PASS" : "FAIL"}
${escapeHtml(check.label)}
${escapeHtml(check.detail)}
`)
.join("");
}
export function mountApp(root) {
root.innerHTML = `
工具在主进程执行;审批是用户决定,超时与参数错误在执行前就被拦截。
审批请求
allow / deny / grant
`;
const score = root.querySelector('[data-testid="score"]');
const grantsPill = root.querySelector('[data-testid="grants"]');
const toolCount = root.querySelector('[data-testid="tool-count"]');
const toolsEl = root.querySelector('[data-testid="tools"]');
const scenariosEl = root.querySelector('[data-testid="scenarios"]');
const pendingEl = root.querySelector('[data-testid="pending"]');
const auditEl = root.querySelector('[data-testid="audit"]');
const auditCount = root.querySelector('[data-testid="audit-count"]');
const checksEl = root.querySelector('[data-testid="checks"]');
const checkCount = root.querySelector('[data-testid="check-count"]');
let state = null;
let pending = null;
let busy = false;
function render() {
if (!state)
return;
if (toolsEl)
toolsEl.innerHTML = toolRows(state);
if (toolCount)
toolCount.textContent = `${state.tools.length} 个`;
if (scenariosEl)
scenariosEl.innerHTML = scenarioButtons(state.scenarios);
if (pendingEl)
pendingEl.innerHTML = pendingCard(pending, state.grants);
if (auditEl)
auditEl.innerHTML = auditRows(state.audit);
if (auditCount)
auditCount.textContent = `${state.audit.length} 条`;
if (checksEl)
checksEl.innerHTML = checkRows(state);
if (checkCount) {
const passed = state.checks.filter((check) => check.pass).length;
checkCount.textContent = `${passed}/${state.checks.length} 通过`;
}
if (grantsPill)
grantsPill.textContent = `${state.grants.length} grants`;
if (score) {
score.textContent = `${state.score} / 100`;
score.className = `pill ${state.score === 100 ? "ok-pill" : "bad-pill"}`;
}
root
.querySelectorAll("[data-scenario]")
.forEach((button) => {
button.disabled = busy;
button.classList.toggle("active", button.dataset.scenario === pending?.id);
});
root
.querySelectorAll("[data-decision]")
.forEach((button) => {
button.disabled = busy;
});
}
async function refresh() {
state = await window.api.tools.snapshot();
render();
}
async function runOne(scenarioId, decision) {
if (busy)
return null;
busy = true;
render();
try {
const response = await window.api.tools.run({ scenarioId, decision });
state = response.snapshot;
return response.entry;
}
finally {
busy = false;
pending = null;
render();
}
}
async function runSequence() {
await runOne("read-config", "deny");
await runOne("write-notes", "deny");
await runOne("write-notes", "allow-once");
await runOne("write-notes", "allow-session");
await runOne("write-notes", "use-grant");
await runOne("run-tests", "use-grant");
await runOne("bad-args", "allow-once");
await runOne("outside", "allow-once");
await runOne("slow-tool", "allow-once");
}
void refresh();
window.api.tools.onChanged(() => {
void refresh();
});
root.addEventListener("click", (event) => {
const target = event.target;
const scenarioButton = target.closest("[data-scenario]");
if (scenarioButton?.dataset.scenario) {
const id = scenarioButton.dataset.scenario;
pending = state?.scenarios.find((scenario) => scenario.id === id) ?? null;
render();
return;
}
const decisionButton = target.closest("[data-decision]");
if (decisionButton?.dataset.decision && pending) {
void runOne(pending.id, decisionButton.dataset.decision);
}
});
root
.querySelector('[data-testid="reset"]')
?.addEventListener("click", async () => {
state = await window.api.tools.reset();
pending = null;
render();
});
window.__prepareShots = async (stage) => {
document.body.dataset.shot = stage;
const reset = await window.api.tools.reset();
state = reset;
pending = null;
await runSequence();
if (stage === "approval") {
pending =
state?.scenarios.find((scenario) => scenario.id === "run-tests") ??
null;
}
else if (stage === "audit") {
pending = null;
}
window.scrollTo(0, 0);
render();
};
window.__demo = async () => {
state = await window.api.tools.reset();
pending = null;
await runSequence();
const find = (scenarioId, decision) => state?.audit.find((entry) => entry.scenarioId === scenarioId &&
(decision ? entry.decision === decision : true));
return {
score: state?.score ?? 0,
checks: state?.checks.length ?? 0,
passed: state?.checks.filter((check) => check.pass).length ?? 0,
tools: state?.tools.length ?? 0,
scenarios: state?.scenarios.length ?? 0,
audit: state?.audit.length ?? 0,
grants: state?.grants.length ?? 0,
readAuto: find("read-config")?.status === "ok",
writeDenied: find("write-notes", "deny")?.status === "denied",
grantReuse: find("write-notes", "grant")?.status === "ok",
execApproval: find("run-tests", "use-grant")?.status === "approval-required",
invalid: find("bad-args")?.status === "invalid",
timeout: find("slow-tool")?.status === "timeout",
outside: find("outside")?.status === "denied",
};
};
}
//# sourceMappingURL=App.js.map