miniai/test_approve.ts

53 lines
2.3 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 验è¯<C3A8>å½ä»¤æŽˆæ<CB86>ƒçº§åˆ«ï¼šlow=全部ä¸<C3A4>审æ ? high=æ¯<C3A6>个都审æ ? medium=ä»…å<E280A6>±é™©å®¡æ ?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,åºä¸<EFBFBD>å®¡æ ¸ç´æŽ¥æ§è¡?===");
await setLevel("low");
const low = await run("请æ§è¡Œå½ä»? echo 你好å°<EFBFBD>é«");
console.log(" éœå®¡æ¹?", low.approval, "| åŒå<EFBFBD>«è¾åº?", low.text.includes("你好å°<EFBFBD>é«"));
console.log(" 判定:", !low.approval && low.text.includes("你好å°<EFBFBD>é«") ? "PASS âœ? : "FAIL â<>?);
console.log("\n=== high:æ§è¡Œå½ä»?echoï¼Œåºæ¯<EFBFBD>次都审æ ?===");
await setLevel("high");
const high = await run("请æ§è¡Œå½ä»? echo 你好å°<EFBFBD>é«");
console.log(" éœå®¡æ¹?", high.approval);
console.log(" 判定:", high.approval ? "PASS âœ? : "FAIL â<>?);
console.log("\n=== medium:å<EFBFBD>±é©å½ä»?删é¤)åºå®¡æ ¸ï¼Œå®å¨å½ä»¤ä¸<EFBFBD>审æ ?===");
await setLevel("medium");
const med = await run("请æ§è¡Œå½ä»? echo å®å¨å½ä»¤æµè¯");
console.log(" å®å¨å½ä»¤éœå®¡æ¹?", med.approval, "(åºä¸?falseï¼?);
console.log(" 判定:", !med.approval ? "PASS âœ? : "FAIL â<EFBFBD>?);
// æ<>¢å¤<C3A5>默认
await setLevel("medium");
console.log("\·²æ<EFBFBD>¢å¤?mediumã?);