Class: Kward::SessionDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/session_diff.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(additions: 0, deletions: 0) ⇒ SessionDiff

Returns a new instance of SessionDiff.



7
8
9
10
# File 'lib/kward/session_diff.rb', line 7

def initialize(additions: 0, deletions: 0)
  @additions = additions.to_i
  @deletions = deletions.to_i
end

Instance Attribute Details

#additionsObject (readonly)

Returns the value of attribute additions.



5
6
7
# File 'lib/kward/session_diff.rb', line 5

def additions
  @additions
end

#deletionsObject (readonly)

Returns the value of attribute deletions.



5
6
7
# File 'lib/kward/session_diff.rb', line 5

def deletions
  @deletions
end

Class Method Details

.common_line_count(left, right) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/kward/session_diff.rb', line 98

def self.common_line_count(left, right)
  previous = Array.new(right.length + 1, 0)
  left.each do |left_line|
    current = Array.new(right.length + 1, 0)
    right.each_with_index do |right_line, index|
      current[index + 1] = if left_line == right_line
                             previous[index] + 1
                           else
                             [current[index], previous[index + 1]].max
                           end
    end
    previous = current
  end
  previous.last
end

.count(diff) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/kward/session_diff.rb', line 33

def self.count(diff)
  if (stats = truncated_diff_stats(diff))
    return stats
  elsif truncated_diff?(diff)
    return { additions: 0, deletions: 0 }
  end

  additions = 0
  deletions = 0
  removed = []
  added = []
  flush = lambda do
    common = common_line_count(removed, added)
    additions += added.length - common
    deletions += removed.length - common
    removed.clear
    added.clear
  end

  diff.to_s.each_line do |line|
    if line.start_with?("+") && !line.start_with?("+++")
      added << line[1..]
    elsif line.start_with?("-") && !line.start_with?("---")
      removed << line[1..]
    else
      flush.call
    end
  end
  flush.call
  { additions: additions, deletions: deletions }
end

.from_records(records) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kward/session_diff.rb', line 19

def self.from_records(records)
  execution_records = records.select { |record| record["type"] == "tool_execution_end" }
  source_records = execution_records.empty? ? records : execution_records
  source_records.each_with_object(new) do |record, diff|
    if record["type"] == "tool_execution_end"
      next if record["isError"] || record.dig("result", "isError")

      diff.add_diff(record.dig("result", "diff"))
    elsif record["type"] == "message" && (record.dig("message", "role") == "tool" || record.dig("message", :role) == "tool")
      diff.add_tool_result(record.dig("message", "content") || record.dig("message", :content))
    end
  end
end

.from_session_file(path) ⇒ Object



12
13
14
15
16
17
# File 'lib/kward/session_diff.rb', line 12

def self.from_session_file(path)
  records = File.readlines(path, chomp: true).filter_map { |line| parse_record(line) }
  from_records(records)
rescue Errno::ENOENT, Errno::EACCES
  new
end

.parse_record(line) ⇒ Object



114
115
116
117
118
# File 'lib/kward/session_diff.rb', line 114

def self.parse_record(line)
  JSON.parse(line)
rescue JSON::ParserError
  nil
end

.truncated_diff?(diff) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/kward/session_diff.rb', line 94

def self.truncated_diff?(diff)
  diff.to_s.match?(/^\.\.\. diff truncated to \d+ bytes;/)
end

.truncated_diff_stats(diff) ⇒ Object



87
88
89
90
91
92
# File 'lib/kward/session_diff.rb', line 87

def self.truncated_diff_stats(diff)
  match = diff.to_s.match(/^\.\.\. diff truncated to \d+ bytes; full diff stats: \+(\d+)\|-(\d+)\./)
  return nil unless match

  { additions: match[1].to_i, deletions: match[2].to_i }
end

Instance Method Details

#add_diff(diff) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/kward/session_diff.rb', line 76

def add_diff(diff)
  counts = self.class.count(diff)
  return false if counts[:additions].zero? && counts[:deletions].zero?

  @additions += counts[:additions]
  @deletions += counts[:deletions]
  true
end

#add_tool_result(content) ⇒ Object



69
70
71
72
73
74
# File 'lib/kward/session_diff.rb', line 69

def add_tool_result(content)
  text = content.to_s
  return false if text.start_with?("Error:", "Declined:")

  add_diff(extract_unified_diff(text))
end

#empty?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/kward/session_diff.rb', line 65

def empty?
  @additions.zero? && @deletions.zero?
end