Class: Ask::Agent::Extensions::PermissionGate

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/agent/extensions/permission_gate.rb

Constant Summary collapse

DEFAULT_TOOLS =
%i[write edit bash destroy].freeze

Instance Method Summary collapse

Constructor Details

#initialize(blocked_tools: DEFAULT_TOOLS, timeout: nil) ⇒ PermissionGate

Returns a new instance of PermissionGate.



9
10
11
12
13
14
# File 'lib/ask/agent/extensions/permission_gate.rb', line 9

def initialize(blocked_tools: DEFAULT_TOOLS, timeout: nil)
  @blocked_tools = Array(blocked_tools).map(&:to_sym)
  @timeout = timeout
  @pending = {}
  @mutex = Mutex.new
end

Instance Method Details

#approve(tool_call_id) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/ask/agent/extensions/permission_gate.rb', line 26

def approve(tool_call_id)
  @mutex.synchronize do
    entry = @pending[tool_call_id]
    return false unless entry
    entry[:approved] = true
  end
end

#before_tool_call(tool_call, _context) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/ask/agent/extensions/permission_gate.rb', line 16

def before_tool_call(tool_call, _context)
  return { action: :proceed } unless @blocked_tools.include?(tool_call.name.to_sym)

  if approved?(tool_call)
    { action: :proceed }
  else
    request_approval(tool_call)
  end
end

#pending_approvalsObject



34
35
36
# File 'lib/ask/agent/extensions/permission_gate.rb', line 34

def pending_approvals
  @mutex.synchronize { @pending.values.reject { |e| e[:approved] } }
end