Documentation
¶
Overview ¶
Package permission provides enhanced permission system aligned with Claude Agent SDK.
Package permission provides a comprehensive permission system for tool execution. It supports multiple approval modes (auto, smart, always_ask) and integrates with the Control Channel for human-in-the-loop interactions.
This package is inspired by Goose's permission system with smart approval mode.
Index ¶
- Variables
- func AddRule(rule Rule)
- func Check(ctx context.Context, call *types.ToolCallSnapshot) (*types.ControlPermissionRequiredEvent, error)
- func SetMode(mode Mode)
- type CheckResult
- type Condition
- type Decision
- type EnhancedInspector
- func (i *EnhancedInspector) AddRule(rule Rule)
- func (i *EnhancedInspector) Check(ctx context.Context, call *types.ToolCallSnapshot) (*CheckResult, error)
- func (i *EnhancedInspector) ClearSessionRules()
- func (i *EnhancedInspector) GetMode() Mode
- func (i *EnhancedInspector) GetRules() []Rule
- func (i *EnhancedInspector) GetToolRisk(toolName string) RiskLevel
- func (i *EnhancedInspector) GetViolations() []types.SandboxViolation
- func (i *EnhancedInspector) RecordDecision(req *Request, decision Decision, note string) *Response
- func (i *EnhancedInspector) RecordViolation(violation types.SandboxViolation)
- func (i *EnhancedInspector) RemoveRule(pattern string) bool
- func (i *EnhancedInspector) SetCanUseTool(fn types.CanUseToolFunc)
- func (i *EnhancedInspector) SetMode(mode Mode)
- func (i *EnhancedInspector) SetSandboxConfig(cfg *types.SandboxConfig)
- func (i *EnhancedInspector) SetToolRisk(toolName string, level RiskLevel)
- type EnhancedInspectorConfig
- type Inspector
- func (i *Inspector) AddRule(rule Rule)
- func (i *Inspector) Check(ctx context.Context, call *types.ToolCallSnapshot) (*types.ControlPermissionRequiredEvent, error)
- func (i *Inspector) GetMode() Mode
- func (i *Inspector) GetRules() []Rule
- func (i *Inspector) GetToolRisk(toolName string) RiskLevel
- func (i *Inspector) RecordDecision(req *Request, decision Decision, note string) *Response
- func (i *Inspector) RemoveRule(pattern string) bool
- func (i *Inspector) SetMode(mode Mode)
- func (i *Inspector) SetToolRisk(toolName string, level RiskLevel)
- type InspectorOption
- type Mode
- type Request
- type Response
- type RiskLevel
- type Rule
Constants ¶
This section is empty.
Variables ¶
var DefaultInspector = NewInspector(ModeSmartApprove)
DefaultInspector is a global default inspector
Functions ¶
func Check ¶
func Check(ctx context.Context, call *types.ToolCallSnapshot) (*types.ControlPermissionRequiredEvent, error)
Check is a convenience function using the default inspector
Types ¶
type CheckResult ¶
type CheckResult struct {
// Allowed 是否允许
Allowed bool
// NeedsApproval 是否需要用户审批
NeedsApproval bool
// DecidedBy 决策来源
DecidedBy string
// Message 消息
Message string
// Interrupt 是否中断执行
Interrupt bool
// UpdatedInput 修改后的输入
UpdatedInput map[string]any
// ApprovalRequest 审批请求事件
ApprovalRequest *types.ControlPermissionRequiredEvent
}
CheckResult 权限检查结果
type Condition ¶
type Condition struct {
// Field is the parameter field to check
Field string `json:"field"`
// Operator is the comparison operator (eq, ne, contains, prefix, suffix, regex)
Operator string `json:"operator"`
// Value is the value to compare against
Value string `json:"value"`
}
Condition defines an additional condition for a rule
type EnhancedInspector ¶
type EnhancedInspector struct {
// contains filtered or unexported fields
}
EnhancedInspector 增强版权限检查器 (Claude Agent SDK 风格) 支持 CanUseTool 回调、沙箱集成、动态权限更新
func NewEnhancedInspector ¶
func NewEnhancedInspector(cfg *EnhancedInspectorConfig) *EnhancedInspector
NewEnhancedInspector 创建增强版权限检查器
func (*EnhancedInspector) Check ¶
func (i *EnhancedInspector) Check(ctx context.Context, call *types.ToolCallSnapshot) (*CheckResult, error)
Check 执行权限检查 (Claude Agent SDK 风格)
func (*EnhancedInspector) ClearSessionRules ¶
func (i *EnhancedInspector) ClearSessionRules()
ClearSessionRules 清除会话级规则
func (*EnhancedInspector) GetToolRisk ¶
func (i *EnhancedInspector) GetToolRisk(toolName string) RiskLevel
GetToolRisk 获取工具风险级别
func (*EnhancedInspector) GetViolations ¶
func (i *EnhancedInspector) GetViolations() []types.SandboxViolation
GetViolations 获取违规记录
func (*EnhancedInspector) RecordDecision ¶
func (i *EnhancedInspector) RecordDecision(req *Request, decision Decision, note string) *Response
RecordDecision 记录决策
func (*EnhancedInspector) RecordViolation ¶
func (i *EnhancedInspector) RecordViolation(violation types.SandboxViolation)
RecordViolation 记录沙箱违规
func (*EnhancedInspector) RemoveRule ¶
func (i *EnhancedInspector) RemoveRule(pattern string) bool
RemoveRule 移除规则
func (*EnhancedInspector) SetCanUseTool ¶
func (i *EnhancedInspector) SetCanUseTool(fn types.CanUseToolFunc)
SetCanUseTool 设置自定义权限回调
func (*EnhancedInspector) SetSandboxConfig ¶
func (i *EnhancedInspector) SetSandboxConfig(cfg *types.SandboxConfig)
SetSandboxConfig 设置沙箱配置
func (*EnhancedInspector) SetToolRisk ¶
func (i *EnhancedInspector) SetToolRisk(toolName string, level RiskLevel)
SetToolRisk 设置工具风险级别
type EnhancedInspectorConfig ¶
type EnhancedInspectorConfig struct {
Mode Mode
SandboxConfig *types.SandboxConfig
CanUseTool types.CanUseToolFunc
PersistPath string
AutoLoad bool
}
EnhancedInspectorConfig 增强检查器配置
type Inspector ¶
type Inspector struct {
// contains filtered or unexported fields
}
Inspector provides permission inspection and approval
func NewInspector ¶
func NewInspector(mode Mode, opts ...InspectorOption) *Inspector
NewInspector creates a new permission inspector
func (*Inspector) Check ¶
func (i *Inspector) Check(ctx context.Context, call *types.ToolCallSnapshot) (*types.ControlPermissionRequiredEvent, error)
Check evaluates whether a tool call should be allowed Returns nil if auto-approved, or a ControlPermissionRequiredEvent if approval needed
func (*Inspector) GetToolRisk ¶
GetToolRisk returns the risk level for a tool
func (*Inspector) RecordDecision ¶
RecordDecision records a user's decision for future reference
func (*Inspector) RemoveRule ¶
RemoveRule removes a rule by pattern
func (*Inspector) SetToolRisk ¶
SetToolRisk sets the risk level for a specific tool
type InspectorOption ¶
type InspectorOption func(*Inspector)
InspectorOption configures an Inspector
func WithAutoLoad ¶
func WithAutoLoad(autoLoad bool) InspectorOption
WithAutoLoad enables/disables auto-loading rules from disk
func WithPersistPath ¶
func WithPersistPath(path string) InspectorOption
WithPersistPath sets the path for rule persistence
type Mode ¶
type Mode string
Mode defines the approval mode for tool execution
const ( // ModeAutoApprove automatically approves all tool executions ModeAutoApprove Mode = "auto_approve" // ModeSmartApprove uses intelligent rules to determine approval // - Low-risk tools (read operations) are auto-approved // - Medium-risk tools (write operations) require approval // - High-risk tools (system commands) always require approval ModeSmartApprove Mode = "smart_approve" // ModeAlwaysAsk always prompts for user approval ModeAlwaysAsk Mode = "always_ask" )
type Request ¶
type Request struct {
// ToolName is the name of the tool
ToolName string `json:"tool_name"`
// Arguments are the tool arguments
Arguments map[string]any `json:"arguments"`
// RiskLevel is the assessed risk level
RiskLevel RiskLevel `json:"risk_level"`
// Context provides additional context
Context map[string]any `json:"context,omitempty"`
// CallID is the unique identifier for this tool call
CallID string `json:"call_id"`
}
Request represents a permission request
type Response ¶
type Response struct {
// Request is the original request
Request *Request `json:"request"`
// Decision is the approval decision
Decision Decision `json:"decision"`
// DecidedBy indicates who made the decision (system, user, rule)
DecidedBy string `json:"decided_by"`
// Note is an optional explanation
Note string `json:"note,omitempty"`
// DecidedAt is when the decision was made
DecidedAt time.Time `json:"decided_at"`
}
Response represents a permission response
type Rule ¶
type Rule struct {
// Pattern is the tool name or glob pattern to match
Pattern string `json:"pattern"`
// Decision is the default decision for matching tools
Decision Decision `json:"decision"`
// RiskLevel is the assigned risk level
RiskLevel RiskLevel `json:"risk_level,omitempty"`
// Conditions are additional conditions for the rule
Conditions []Condition `json:"conditions,omitempty"`
// ExpiresAt is when this rule expires (for temporary rules)
ExpiresAt *time.Time `json:"expires_at,omitempty"`
// CreatedAt is when this rule was created
CreatedAt time.Time `json:"created_at"`
// Note is an optional explanation for this rule
Note string `json:"note,omitempty"`
}
Rule defines a permission rule for a tool or pattern