AutoGLM 敏感操作确认回调——拦截危险操作前让人类过目
为什么需要
AutoGLM-Phone-9B 模型内置了一部分敏感操作检测逻辑(比如识别到支付弹窗时主动触发 Take_over),但很多场景下你希望更严格——比如在任何发送消息、任何下单、任何删除操作前都停下来让人类确认。
这就是 confirmation_callback 的作用:框架在执行一个动作前调用你注册的回调函数,回调返回 True 才继续执行,返回 False 则中止该动作。
基本用法
def my_confirmation(message: str) -> bool:
"""敏感操作确认回调"""
print(f"⚠️ 即将执行敏感操作:{message}")
answer = input("确认执行?(y/n): ").strip().lower()
return answer == "y"
agent = PhoneAgent(
model_config=model_config,
confirmation_callback=my_confirmation,
)
result = agent.run("打开美团下单购买奶茶")
运行时:
🎯 即将执行:do(action="Launch", app="美团")
⚠️ 即将执行敏感操作:打开美团
确认执行?(y/n): y
[任务继续...]
🎯 即将执行:do(action="Type", text="珍珠奶茶,去冰半糖")
⚠️ 即将执行敏感操作:输入文本到搜索框
确认执行?(y/n): y
[任务继续...]
🎯 即将执行:do(action="Tap", element=[540, 1200])
⚠️ 即将执行敏感操作:点击下单按钮
确认执行?(y/n): n
❌ 用户拒绝,任务中止
message 参数里有什么
message 参数是框架根据当前动作生成的可读描述,比如:
do(action="Tap", element=[540, 1200])→ "点击屏幕坐标 (540, 1200)"do(action="Launch", app="微信")→ "启动微信"do(action="Type", text="明天下午3点开会")→ "输入文本到当前聚焦框"
注意 message 里不包含模型的思考过程,只有执行动作本身。
异步确认(Web 场景)
如果你把 Agent 嵌入到了 Web 后台管理系统里,确认流程不能用 input() 同步阻塞。可以改成异步:
import threading
class ConfirmationQueue:
def __init__(self):
self._queue = []
self._event = threading.Event()
self._decision = None
def __call__(self, message: str) -> bool:
self._current_message = message
self._event.clear()
# 这里通知你的 Web 前端弹出确认对话框
notify_frontend(message)
# 等待前端回传决定
self._event.wait()
return self._decision
def approve(self):
self._decision = True
self._event.set()
def reject(self):
self._decision = False
self._event.set()
confirmation = ConfirmationQueue()
agent = PhoneAgent(
model_config=model_config,
confirmation_callback=confirmation,
)
Web 前端点"确认"或"拒绝"后,调用 confirmation.approve() / confirmation.reject(),Agent 就继续运行了。
和 Take_over 的区别
| 方面 | confirmation_callback | Take_over |
|---|---|---|
| 触发时机 | 每个动作执行前都可以触发 | 模型主动判断当前场景需要人工介入 |
| 控制粒度 | 细——可以拦截每一个动作 | 粗——模型觉得需要时才触发 |
| 典型用途 | 审批流、二次确认、安全审查 | 登录验证码、支付密码、二次验证 |
| 阻塞后行为 | 拒绝 = 中止当前动作,Agent 继续跑 | Agent 暂停,等人工完成后恢复循环 |
两者可以同时使用——confirmation_callback 负责"每步过审",Take_over 负责"模型自己搞不定的时候交给人"。