mirror of
https://github.com/hansjone/oclaw.git
synced 2026-10-09 07:20:44 +08:00
本次迁移将网关/通道/工具/技能/脚本与协议资源集中到新结构,统一路径常量与脚本转发机制,减少顶层噪音并保证运行与测试行为一致。 Made-with: Cursor
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from oclaw.runtime.orchestration.protocol import AgentTask
|
|
|
|
_HIGH_RISK_ACTIONS = ("删除", "drop", "重启", "批量", "扫描", "写入", "变更")
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class GuardrailResult:
|
|
allowed: bool
|
|
needs_confirmation: bool
|
|
reason: str
|
|
|
|
|
|
def evaluate_risk(task: AgentTask) -> GuardrailResult:
|
|
text = (task.user_text or "").lower()
|
|
high_risk = task.risk_level == "high" or any(k in text for k in _HIGH_RISK_ACTIONS)
|
|
if high_risk:
|
|
return GuardrailResult(
|
|
allowed=True,
|
|
needs_confirmation=True,
|
|
reason="High-risk action detected, require explicit confirmation token.",
|
|
)
|
|
return GuardrailResult(allowed=True, needs_confirmation=False, reason="Low-risk request")
|
|
|
|
|
|
def has_explicit_confirmation(user_text: str) -> bool:
|
|
return has_explicit_confirmation_token(user_text, token=None)
|
|
|
|
|
|
def has_explicit_confirmation_token(user_text: str, token: str | None) -> bool:
|
|
text = (user_text or "").strip()
|
|
if not text:
|
|
return False
|
|
low = text.lower()
|
|
if low.startswith("confirm "):
|
|
if token:
|
|
parts = low.split()
|
|
return len(parts) >= 2 and parts[1].strip() == token.lower()
|
|
return True
|
|
if "[confirm]" in low or "确认执行" in low:
|
|
return True
|
|
if token:
|
|
t = token.strip()
|
|
if not t:
|
|
return False
|
|
if f"[confirm:{t}]".lower() in low or f"confirm:{t}".lower() in low:
|
|
return True
|
|
return False
|
|
|
|
|
|
__all__ = ["GuardrailResult", "evaluate_risk", "has_explicit_confirmation", "has_explicit_confirmation_token"]
|