AI Interaction Pattern
关键操作前确认,人在回路中,防误操作
解决的问题:当 AI 被赋予执行能力时,误操作的成本很高。如果 AI 理解错了用户意图就直接执行,后果可能不可逆。引用用户控制与自由(尼尔森第三原则)——用户需要明确的"出口"来避免不想要的状态。在 AI 交互中,"出口"就是"先确认再执行"。
设计决策:两阶段确认流——Stage 1 生成确认请求(含影响评估、是否可逆),Stage 2 用户批准后执行。10 分钟 TTL 防止过期确认。
生活类比:点外卖时下单前的那一步确认页——你一定会确认地址对不对、菜选没选错。即使再急的人也会看一眼再付款。因为你知道点错了就要重新下单、可能要等更久。AI 执行操作前让用户确认,就是那个"下单确认页"。
模拟一个关键操作的三步确认流程——人在回路,防误操作。
将首页主色调从蓝色改为橙色,所有按钮和链接颜色同步更新,保持现有布局和间距不变。
用户用自然语言描述想要执行的操作。无需精确指令——AI 会自行理解和结构化。
/api/confirm 接收操作描述,AI 解读并生成确认卡片:proposal(理解的操作)、impact(影响范围和风险评估)、reversible(是否可逆)。10 分钟 TTL。
/api/execute 接收用户决定。确认则执行操作并返回结果,取消则安全退出。
# Prompts
CONFIRM_PROMPT = """用户想让你执行一个操作。你的工作:
1. 用清晰的语言描述你理解的操作(proposal)
2. 评估这个操作的影响范围(impact)
3. 判断这个操作是否可逆(reversible)
4. 输出 JSON,让用户确认或取消"""
# patterns.py
pending_actions: Dict[str, dict] = {} # action_id → action with TTL
@router.post("/api/confirm")
async def confirm_action(req: ConfirmRequest):
response = await deepseek_chat(CONFIRM_PROMPT, req.context)
action = parse_json_response(response)
action["action_id"] = str(uuid4())
action["created_at"] = time.time()
action["status"] = "pending"
pending_actions[action["action_id"]] = action
return action # { proposal, impact, reversible, action_id }
@router.post("/api/execute")
async def execute_action(req: ExecuteRequest):
action = pending_actions.pop(req.action_id, None)
if not action or time.time() - action["created_at"] > 600:
raise HTTPException(410, "Action expired (>10min)")
if not req.confirmed:
return {"status": "cancelled", "result": "操作已取消"}
result = await perform_action(action)
return {"status": "executed", "result": result}