Class: PWN::AI::Agent::RequestRuntime

Inherits:
Object
  • Object
show all
Defined in:
lib/pwn/ai/agent/request_runtime.rb

Overview

Connect request-scoped routing, evidence context and replay records.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ RequestRuntime

Returns a new instance of RequestRuntime.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pwn/ai/agent/request_runtime.rb', line 13

def initialize(opts = {})
  @session_id = opts[:session_id].to_s
  @request = opts[:request].to_s
  @profile = opts[:profile]
  @router = Profiles.new(profiles: opts[:profiles] || {})
  @memory = EngagementMemory.new(original_goal: @request, session_id: @session_id,
                                 root: opts[:artifact_root] || File.expand_path('~/.pwn/artifacts'),
                                 window: opts[:window] || 128_000, tool_cap: opts[:tool_cap] || 8192)
  @route = {}
  @retrieval = nil
  record(event: 'request', data: { content: @request })
end

Class Method Details

.authorsObject



79
80
81
# File 'lib/pwn/ai/agent/request_runtime.rb', line 79

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <[email protected]>\n"
end

.helpObject



83
84
85
86
87
88
89
90
# File 'lib/pwn/ai/agent/request_runtime.rb', line 83

public_class_method def self.help
  puts "USAGE:
    # Display author information.
    #{self}.authors
    # Print this usage.
    #{self}.help
  "
end

Instance Method Details

#call(messages:, tools:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pwn/ai/agent/request_runtime.rb', line 26

def call(messages:, tools:)
  prepared = @memory.compact(messages: messages)
  prepared = retrieval_context(prepared) unless tools.nil?
  routes = @router.routes(name: @profile, engine: Thread.current[:pwn_swarm_engine], model: Thread.current[:pwn_swarm_model])
  if routes.empty?
    @route = {}
    response = yield(prepared, @route)
  else
    response = @router.call(name: @profile, engine: Thread.current[:pwn_swarm_engine], model: Thread.current[:pwn_swarm_model]) do |route|
      @route = route
      input = prepared
      input = [{ role: 'system', content: route[:system_prompt] }] + prepared if route[:system_prompt] && !route[:system_prompt].empty?
      yield(input, route)
    end
  end
  record(event: 'response', data: response)
  response
end

#tool_call(call) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/pwn/ai/agent/request_runtime.rb', line 45

def tool_call(call)
  arguments = call.dig(:function, :arguments)
  arguments = JSON.parse(arguments) if arguments.is_a?(String)
  record(event: 'tool_call', data: { id: call[:id], name: call.dig(:function, :name), arguments: arguments })
rescue JSON::ParserError
  record(event: 'tool_call', data: { id: call[:id], name: call.dig(:function, :name), invalid_arguments: true })
end

#tool_result(call, result) ⇒ Object



53
54
55
56
# File 'lib/pwn/ai/agent/request_runtime.rb', line 53

def tool_result(call, result)
  record(event: 'tool_result', data: { id: call[:id], name: call.dig(:function, :name), content: result })
  @memory.spill(content: result)
end