// 验证命令授权级别:low=全部不审? high=每个都审? medium=仅危险审?const dec = new TextDecoder(); async function setLevel(level: string) { await fetch("http://127.0.0.1:3100/api/settings", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ CMD_APPROVE_LEVEL: level }), }); } async function run(prompt: string): Promise<{ text: string; approval: boolean }> { const resp = await fetch("http://127.0.0.1:3100/api/chat", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ messages: [{ role: "user", content: prompt }] }), }); const reader = resp.body!.getReader(); let buf = "", out = "", approval = false; while (true) { const { done, value } = await reader.read(); if (done) break; buf += dec.decode(value, { stream: true }); const lines = buf.split("\n"); buf = lines.pop() || ""; for (const line of lines) { const t = line.trim(); if (!t.startsWith("data:")) continue; let ev: any; try { ev = JSON.parse(t.slice(5).trim()); } catch { continue; } if (ev.type === "token") out += ev.content; if (ev.type === "approval_required") { approval = true; console.log(" [需审批]", ev.tool, "?, ev.command); } } } return { text: out, approval }; } console.log("=== low:执行命?echo,应不审核直接执?==="); await setLevel("low"); const low = await run("请执行命? echo 你好小鑫"); console.log(" 需审批?", low.approval, "| 包含输出?", low.text.includes("你好小鑫")); console.log(" 判定:", !low.approval && low.text.includes("你好小鑫") ? "PASS ? : "FAIL ?); console.log("\n=== high:执行命?echo,应每次都审?==="); await setLevel("high"); const high = await run("请执行命? echo 你好小鑫"); console.log(" 需审批?", high.approval); console.log(" 判定:", high.approval ? "PASS ? : "FAIL ?); console.log("\n=== medium:危险命?删除)应审核,安全命令不审?==="); await setLevel("medium"); const med = await run("请执行命? echo 安全命令测试"); console.log(" 安全命令需审批?", med.approval, "(应?false?); console.log(" 判定:", !med.approval ? "PASS ? : "FAIL ?); // 恢复默认 await setLevel("medium"); console.log("\n已恢?medium?);