import { query } from "@anthropic-ai/claude-agent-sdk";
async function promptForToolApproval(toolName: string, input: any) {
console.log("\n🔧 工具请求:");
console.log(` 工具: ${toolName}`);
// 显示工具参数
if (input && Object.keys(input).length > 0) {
console.log(" 参数:");
for (const [key, value] of Object.entries(input)) {
let displayValue = value;
if (typeof value === 'string' && value.length > 100) {
displayValue = value.substring(0, 100) + "...";
} else if (typeof value === 'object') {
displayValue = JSON.stringify(value, null, 2);
}
console.log(` ${key}: ${displayValue}`);
}
}
// 获取用户批准(用您的 UI 逻辑替换)
const approved = await getUserApproval();
if (approved) {
console.log(" ✅ 已批准\n");
return {
behavior: "allow",
updatedInput: input
};
} else {
console.log(" ❌ 已拒绝\n");
return {
behavior: "deny",
message: "用户拒绝了此工具的权限"
};
}
}
// 使用权限回调
const result = await query({
prompt: "帮我分析这个代码库",
options: {
canUseTool: async (toolName, input) => {
return promptForToolApproval(toolName, input);
}
}
});