示例工程 58-agent-app-architecture

58-agent-app-architecture/app/.tsc/shared/arch.js

javascript · 4.5 KB

export const LAYERS = [
    {
        id: "ui",
        title: "渲染进程 / UI",
        items: ["对话流", "工具卡片", "审批弹层", "设置"],
        note: "只渲染状态与发送意图,不直接访问文件、shell 或 provider。",
    },
    {
        id: "preload",
        title: "preload 桥",
        items: ["白名单方法", "参数校验", "事件订阅"],
        note: "只暴露具体方法,不暴露 ipcRenderer。",
    },
    {
        id: "main",
        title: "主进程 / Agent 运行时",
        items: ["agent loop", "provider", "tool registry", "审批", "取消"],
        note: "唯一能调用系统与网络的地方。",
    },
    {
        id: "data",
        title: "本地数据",
        items: ["sessions(SQLite)", "secrets(safeStorage)", "审计日志"],
        note: "会话与密钥只在主进程读写。",
    },
    {
        id: "system",
        title: "系统能力",
        items: ["工作区文件", "shell", "剪贴板", "通知"],
        note: "必须经过路径校验与审批。",
    },
    {
        id: "provider",
        title: "模型 Provider",
        items: ["OpenAI 兼容 HTTP", "流式 SSE", "超时/重试"],
        note: "只由主进程访问,密钥不进渲染进程。",
    },
];
export const EDGES = [
    { from: "ui", to: "preload", kind: "allowed", label: "window.api" },
    { from: "preload", to: "main", kind: "allowed", label: "ipcRenderer.invoke" },
    { from: "main", to: "system", kind: "allowed", label: "fs / shell" },
    { from: "main", to: "provider", kind: "allowed", label: "fetch" },
    { from: "main", to: "data", kind: "allowed", label: "SQLite" },
    { from: "ui", to: "system", kind: "forbidden", label: "直接读文件" },
    { from: "ui", to: "provider", kind: "forbidden", label: "直连模型" },
    { from: "preload", to: "provider", kind: "forbidden", label: "密钥外泄" },
];
export const TOOLS = [
    { id: "read", name: "read_file", risk: "read" },
    { id: "write", name: "write_file", risk: "write" },
    { id: "exec", name: "run_shell", risk: "exec" },
    { id: "fetch", name: "fetch_url", risk: "network" },
];
export const FAILURES = [
    {
        id: "timeout",
        symptom: "provider 超过 30s 无响应",
        detect: "AbortController 超时",
        retry: "最多 2 次,指数退避",
        fallback: "提示检查网络/模型配置",
    },
    {
        id: "rate-limit",
        symptom: "429 限流",
        detect: "HTTP 429 / retry-after",
        retry: "按响应头等待",
        fallback: "排队并显示预计时间",
    },
    {
        id: "tool-error",
        symptom: "工具返回非零退出码",
        detect: "result.exitCode",
        retry: "不自动重试",
        fallback: "把错误反馈给模型",
    },
    {
        id: "invalid-args",
        symptom: "工具参数不合法",
        detect: "schema 校验",
        retry: "不执行",
        fallback: "要求模型修正参数",
    },
    {
        id: "cancel",
        symptom: "用户点击停止",
        detect: "AbortSignal",
        retry: "不重试",
        fallback: "保留已生成内容",
    },
    {
        id: "crash",
        symptom: "渲染进程崩溃",
        detect: "render-process-gone",
        retry: "重建窗口",
        fallback: "从会话记录恢复",
    },
];
export function decide(tool, mode, granted = false, trusted = true) {
    if (tool.risk === "read") {
        return trusted
            ? { allowed: true, needApproval: false, reason: "READ_TRUSTED" }
            : { allowed: false, needApproval: true, reason: "READ_OUTSIDE" };
    }
    if (mode === "never") {
        return { allowed: false, needApproval: false, reason: "POLICY_DENIED" };
    }
    if (tool.risk === "exec") {
        return {
            allowed: false,
            needApproval: true,
            reason: "EXEC_ALWAYS_APPROVE",
        };
    }
    if (mode === "always" && trusted) {
        return { allowed: true, needApproval: false, reason: "ALWAYS_TRUSTED" };
    }
    if ((mode === "once" || mode === "session") && granted) {
        return { allowed: true, needApproval: false, reason: "APPROVED" };
    }
    return { allowed: false, needApproval: true, reason: "NEEDS_APPROVAL" };
}
export function validateArchitecture(edges) {
    const violations = [];
    for (const edge of edges) {
        if (edge.kind !== "forbidden")
            continue;
        violations.push(`${edge.from}→${edge.to}`);
    }
    return violations;
}
//# sourceMappingURL=arch.js.map