import type { Check, Snapshot } from "../shared/ipc"; import type { ApprovalMode, Decision, Edge, Layer } from "../shared/arch"; function escapeHtml(text: string): string { return text.replace(/[&<>"]/g, (char) => { if (char === "&") return "&"; if (char === "<") return "<"; if (char === ">") return ">"; return """; }); } function time(at: number): string { return new Date(at).toLocaleTimeString("zh-CN", { hour12: false }); } function layerCard(layer: Layer): string { return `
${escapeHtml(layer.title)}
${layer.items.map((item) => `${escapeHtml(item)}`).join("")}

${escapeHtml(layer.note)}

`; } function edgeRow(edge: Edge): string { return `
  • ${edge.kind === "allowed" ? "允许" : "禁止"} ${escapeHtml(edge.from)} → ${escapeHtml(edge.to)} ${escapeHtml(edge.label)}
  • `; } const MODES: ApprovalMode[] = ["never", "once", "session", "always"]; function verdict(decision: Decision): string { if (decision.allowed) return '允许'; if (decision.needApproval) return '审批'; return '拒绝'; } function checkRow(check: Check): string { return `
  • ${check.pass ? "PASS" : "FAIL"} ${escapeHtml(check.label)} ${escapeHtml(check.detail)}
  • `; } export function mountApp(root: HTMLElement): void { root.innerHTML = `

    Agent 客户端架构规划

    分层 · 权限 · 审批 · 失败模式(离线)
    offline —

    架构分层

    6 层

    数据流

    —

      权限矩阵

      工具 × 审批模式
      工具风险neveroncesessionalways

      read 在受信工作区自动允许;write/network 按模式审批; exec 永不自动放行。

      失败模式

      —
      现象检测重试兜底

      架构自检

      —

        活动

        计算 / 刷新
          `; const score = root.querySelector('[data-testid="score"]'); const layersEl = root.querySelector('[data-testid="layers"]'); const edgesEl = root.querySelector('[data-testid="edges"]'); const violationsEl = root.querySelector( '[data-testid="violations"]', ); const matrixEl = root.querySelector('[data-testid="matrix"]'); const failureCount = root.querySelector( '[data-testid="failure-count"]', ); const failuresEl = root.querySelector( '[data-testid="failures"]', ); const checksEl = root.querySelector('[data-testid="checks"]'); const checkCount = root.querySelector( '[data-testid="check-count"]', ); const eventsEl = root.querySelector('[data-testid="events"]'); let state: Snapshot | null = null; function renderLayers(): void { if (!state) return; if (layersEl) layersEl.innerHTML = state.layers.map(layerCard).join(""); if (edgesEl) edgesEl.innerHTML = state.edges.map(edgeRow).join(""); if (violationsEl) { violationsEl.textContent = `${state.violations.length} 条禁止边`; } } function renderPermissions(): void { if (!state || !matrixEl) return; matrixEl.innerHTML = state.matrix .map( (row) => ` ${escapeHtml(row.tool.name)} ${escapeHtml(row.tool.risk)} ${MODES.map((mode) => `${verdict(row.decisions[mode])}`).join("")} `, ) .join(""); } function renderFailures(): void { if (!state) return; if (failureCount) { failureCount.textContent = `${state.failures.length} 类`; } if (failuresEl) { failuresEl.innerHTML = state.failures .map( (row) => ` ${escapeHtml(row.symptom)} ${escapeHtml(row.detect)} ${escapeHtml(row.retry)} ${escapeHtml(row.fallback)} `, ) .join(""); } } function renderChecks(): void { if (!state) return; if (checksEl) checksEl.innerHTML = state.checks.map(checkRow).join(""); if (checkCount) { const passed = state.checks.filter((check) => check.pass).length; checkCount.textContent = `${passed}/${state.checks.length} 通过`; } if (score) { score.textContent = `${state.score} / 100`; score.className = `pill ${state.score === 100 ? "ok-pill" : "bad-pill"}`; } } function renderEvents(): void { if (!eventsEl || !state) return; eventsEl.innerHTML = state.events.length === 0 ? `
        • 暂无活动
        • ` : state.events .slice(0, 8) .map( (row) => `
        • ${time(row.at)} ${escapeHtml(row.kind)} ${escapeHtml(row.detail)}
        • `, ) .join(""); } function render(next: Snapshot): void { state = next; renderLayers(); renderPermissions(); renderFailures(); renderChecks(); renderEvents(); } void window.api.arch.snapshot().then(render); window.api.arch.onEvent(() => { void window.api.arch.snapshot().then(render); }); root .querySelector('[data-testid="refresh"]') ?.addEventListener("click", async () => { render(await window.api.arch.refresh()); }); window.__prepareShots = async (stage) => { document.body.dataset.shot = stage; render(await window.api.arch.refresh()); window.scrollTo(0, 0); }; window.__demo = async () => { const next = await window.api.arch.snapshot(); state = next; const find = (id: string, mode: ApprovalMode) => next.matrix.find((row) => row.tool.id === id)?.decisions[mode]; return { score: next.score, checks: next.checks.length, passed: next.checks.filter((check) => check.pass).length, layers: next.layers.length, edges: next.edges.length, violations: next.violations.length, tools: next.tools.length, failures: next.failures.length, readAllowed: find("read", "never")?.allowed ?? false, writeNeverDenied: !(find("write", "never")?.allowed ?? true), execNeedsApproval: find("exec", "always")?.needApproval ?? false, matrix: next.matrix.length, }; }; }