Module: PWN::AI::Agent::Dispatch

Defined in:
lib/pwn/ai/agent/dispatch.rb

Overview

Tool-call dispatch: takes a single tool_call object (OpenAI shape), looks up the registered handler, parses args, runs it, and returns a JSON string suitable for a role:‘tool’ message.

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. <support@0dayinc.com>



58
59
60
# File 'lib/pwn/ai/agent/dispatch.rb', line 58

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <support@0dayinc.com>\n"
end

.call(opts = {}) ⇒ Object

Supported Method Parameters

json_str = PWN::AI::Agent::Dispatch.call(

tool_call: 'required - Hash { id:, type:, function: { name:, arguments: } }'

)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/pwn/ai/agent/dispatch.rb', line 17

public_class_method def self.call(opts = {})
  tool_call = opts[:tool_call]
  raise 'ERROR: tool_call is required' if tool_call.nil?

  fn   = tool_call[:function] || tool_call['function'] || {}
  name = (fn[:name] || fn['name']).to_s
  raw  = fn[:arguments] || fn['arguments'] || '{}'

  entry = Registry.lookup(name: name)
  return JSON.generate(error: "unknown tool: #{name}") unless entry

  args = parse_args(raw: raw)
  result = entry.handler.call(args)
  JSON.generate(success: true, result: result)
rescue StandardError => e
  JSON.generate(
    success: false,
    error: "#{e.class}: #{e.message}",
    backtrace: Array(e.backtrace).first(3)
  )
end

.helpObject

Display Usage for this Module



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/pwn/ai/agent/dispatch.rb', line 64

public_class_method def self.help
  puts <<~USAGE
    USAGE:
      json_str = PWN::AI::Agent::Dispatch.call(
        tool_call: {
          id: 'call_1',
          type: 'function',
          function: { name: 'shell', arguments: '{"command":"id"}' }
        }
      )

      #{self}.authors
  USAGE
end