示例工程 60-tool-registry-approval
60-tool-registry-approval/demo-02-approval-grant/approval.ts
type Risk = "read" | "write" | "exec" | "network";
type Decision = "auto" | "deny" | "allow-once" | "allow-session" | "use-grant";
type Outcome = {
allowed: boolean;
needApproval: boolean;
grant: boolean;
reason: string;
};
const grants = new Set<string>();
function decide(tool: string, risk: Risk, decision: Decision): Outcome {
if (risk === "read") {
return { allowed: true, needApproval: false, grant: false, reason: "READ" };
}
if (decision === "deny") {
return {
allowed: false,
needApproval: false,
grant: false,
reason: "USER_DENIED",
};
}
if (risk === "exec") {
if (decision === "allow-once" || decision === "allow-session") {
return {
allowed: true,
needApproval: false,
grant: false,
reason: "EXEC_APPROVED",
};
}
return {
allowed: false,
needApproval: true,
grant: false,
reason: "EXEC_ALWAYS_APPROVE",
};
}
if (decision === "allow-once") {
return {
allowed: true,
needApproval: false,
grant: false,
reason: "APPROVED_ONCE",
};
}
if (decision === "allow-session") {
grants.add(tool);
return {
allowed: true,
needApproval: false,
grant: true,
reason: "SESSION_GRANTED",
};
}
if (decision === "use-grant" && grants.has(tool)) {
return {
allowed: true,
needApproval: false,
grant: false,
reason: "GRANT_REUSED",
};
}
return {
allowed: false,
needApproval: true,
grant: false,
reason: "NEEDS_APPROVAL",
};
}
const exec = decide("run_shell", "exec", "use-grant");
const writeOnce = decide("write_file", "write", "allow-once");
const session = decide("write_file", "write", "allow-session");
const reuse = decide("write_file", "write", "use-grant");
const denied = decide("write_file", "write", "deny");
const ok =
!exec.allowed &&
exec.needApproval &&
writeOnce.allowed &&
!writeOnce.grant &&
session.allowed &&
session.grant &&
reuse.allowed &&
reuse.reason === "GRANT_REUSED" &&
!denied.allowed &&
!denied.needApproval;
console.log(
JSON.stringify({
evt: "smoke",
ok,
exec,
writeOnce,
session,
reuse,
denied,
grants: [...grants],
}),
);
export {};