Class: Agent::SessionContext::EvidenceCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/agent/session_context/evidence_collector.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

DOCUMENT_BASENAMES =
%w[agents.md claude.md readme readme.md].freeze
DOCUMENT_EXTENSIONS =
%w[.md .markdown .txt .pdf .doc .docx .odt .rtf].freeze
PATH_KEYS =
%w[path file_path filename source destination target].freeze
SCHEMELESS_URL_PATTERN =

rubocop:disable-next Layout/LineLength -- splitting this regexp would obscure its URL grammar

%r{(?<![A-Za-z0-9_./-])(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,}(?::\d+)?(?:/[^\s"'<>?#]+)+(?:\?[^\s"'<>#]*)?(?:#[^\s"'<>]*)?}i
SCHEME_URL_PATTERN =
%r{[A-Za-z][A-Za-z0-9+\-.]*://[^\s"'<>]+}
TOOL_ACTIONS =
{
  "Read" => :read,
  "read_file" => :read,
  "view_image" => :read,
  "Write" => :modified,
  "Edit" => :modified,
  "MultiEdit" => :modified,
  "apply_patch" => :modified,
  "move_file" => :modified
}.freeze
PATH_SCAN_PATTERN =
%r{(?:\.\.?/|/)?[A-Za-z0-9._-]+(?:/[A-Za-z0-9._-]+)+}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.document_item?(item) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/agent/session_context/evidence_collector.rb', line 31

def document_item?(item)
  item.kind == :file && document_path?(item.label)
end

.document_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
# File 'lib/agent/session_context/evidence_collector.rb', line 35

def document_path?(path)
  basename = File.basename(path).downcase
  extension = File.extname(path).downcase

  DOCUMENT_BASENAMES.include?(basename) || DOCUMENT_EXTENSIONS.include?(extension)
end

Instance Method Details

#call(transcript) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/agent/session_context/evidence_collector.rb', line 43

def call(transcript)
  tool_activity = []
  files = {}

  transcript.entries.each do |entry|
    entry.parts.each do |part|
      next unless part.type == :tool_use

      parsed_input = parse_input(part.text)
      tool_activity << build_tool_item(part, parsed_input)

      extract_paths(part.text, parsed_input).each do |path|
        add_item(files, kind: :file, path: path, action: action_for(part.name), source_ref: part.source_ref)
      end
    end
  end

  file_items = files.values.freeze

  Result.new(files: file_items, tool_activity: tool_activity.freeze)
end