Class: AIA::ContextDirectives

Inherits:
Directive
  • Object
show all
Defined in:
lib/aia/directives/context_directives.rb

Constant Summary

Constants inherited from Directive

Directive::DIRECTIVE_PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Directive

build_dispatch_block, help, state_setting!, state_setting_methods

Constructor Details

#initializeContextDirectives

Returns a new instance of ContextDirectives.



12
13
14
15
# File 'lib/aia/directives/context_directives.rb', line 12

def initialize
  super
  reset!
end

Instance Attribute Details

#checkpoint_counterObject

Returns the value of attribute checkpoint_counter.



10
11
12
# File 'lib/aia/directives/context_directives.rb', line 10

def checkpoint_counter
  @checkpoint_counter
end

#checkpoint_storeObject

Returns the value of attribute checkpoint_store.



10
11
12
# File 'lib/aia/directives/context_directives.rb', line 10

def checkpoint_store
  @checkpoint_store
end

#last_checkpoint_nameObject

Returns the value of attribute last_checkpoint_name.



10
11
12
# File 'lib/aia/directives/context_directives.rb', line 10

def last_checkpoint_name
  @last_checkpoint_name
end

Instance Method Details

#checkpoint(args, _unused = nil) ⇒ Object Also known as: ckp, cp



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/aia/directives/context_directives.rb', line 24

def checkpoint(args, _unused = nil)
  name = args.empty? ? nil : args.join(' ').strip

  if name.nil? || name.empty?
    @checkpoint_counter += 1
    name = @checkpoint_counter.to_s
  end

  chats = get_chats
  return "Error: No active chat sessions found." if chats.nil? || chats.empty?

  first_chat_messages = chats.values.first&.messages || []
  @checkpoint_store[name] = {
    messages: chats.transform_values do |chat|
      chat.messages.map { |msg| deep_copy_message(msg) }
    end,
    position: first_chat_messages.size,
    created_at: Time.now,
    topic_preview: extract_last_user_message(first_chat_messages)
  }
  @last_checkpoint_name = name

  puts "Checkpoint '#{name}' created at position #{@checkpoint_store[name][:position]}."
  ""
end

#checkpoint_namesObject

--- helpers (no desc → not registered as directives) ---



172
173
174
# File 'lib/aia/directives/context_directives.rb', line 172

def checkpoint_names
  @checkpoint_store.keys
end

#checkpoint_positionsObject



176
177
178
179
180
181
182
183
184
# File 'lib/aia/directives/context_directives.rb', line 176

def checkpoint_positions
  positions = {}
  @checkpoint_store.each do |name, data|
    pos = data[:position]
    positions[pos] ||= []
    positions[pos] << name
  end
  positions
end

#checkpoints_list(args, _unused = nil) ⇒ Object Also known as: checkpoints



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/aia/directives/context_directives.rb', line 151

def checkpoints_list(args, _unused = nil)
  if @checkpoint_store.empty?
    puts "No checkpoints available."
    return ""
  end

  puts "\n=== Available Checkpoints ==="
  @checkpoint_store.each do |name, data|
    created = data[:created_at]&.strftime('%H:%M:%S') || 'unknown'
    puts "  #{name}: position #{data[:position]}, created #{created}"
    if data[:topic_preview] && !data[:topic_preview].empty?
      puts "    → \"#{data[:topic_preview]}\""
    end
  end
  puts "=== End of Checkpoints ==="
  ""
end

#clear(args, _unused = nil) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/aia/directives/context_directives.rb', line 92

def clear(args, _unused = nil)
  keep_system = !args.include?('--all')

  robots = get_robots
  return "Error: No active chat sessions found." if robots.nil? || robots.empty?

  robots.each_value do |robot|
    robot.clear_messages(keep_system: keep_system)
  end

  @checkpoint_store.clear
  @checkpoint_counter = 0
  @last_checkpoint_name = nil

  "Chat context cleared."
end

#find_previous_checkpointObject



192
193
194
195
196
197
# File 'lib/aia/directives/context_directives.rb', line 192

def find_previous_checkpoint
  return nil if @checkpoint_store.size < 2

  sorted = @checkpoint_store.sort_by { |_name, data| -data[:position] }
  sorted[1]&.first
end

#remove_invalid_checkpoints(max_position) ⇒ Object



186
187
188
189
190
# File 'lib/aia/directives/context_directives.rb', line 186

def remove_invalid_checkpoints(max_position)
  invalid_names = @checkpoint_store.select { |_name, data| data[:position] > max_position }.keys
  invalid_names.each { |name| @checkpoint_store.delete(name) }
  invalid_names.size
end

#reset!Object



17
18
19
20
21
# File 'lib/aia/directives/context_directives.rb', line 17

def reset!
  @checkpoint_store     = {}
  @checkpoint_counter   = 0
  @last_checkpoint_name = nil
end

#restore(args, _unused = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aia/directives/context_directives.rb', line 53

def restore(args, _unused = nil)
  name = args.empty? ? nil : args.join(' ').strip

  if name.nil? || name.empty?
    name = find_previous_checkpoint
    if name.nil?
      return "Error: No previous checkpoint to restore to."
    end
  end

  unless @checkpoint_store.key?(name)
    available = checkpoint_names.empty? ? "none" : checkpoint_names.join(', ')
    return "Error: Checkpoint '#{name}' not found. Available: #{available}"
  end

  checkpoint_data = @checkpoint_store[name]
  robots = get_robots

  return "Error: No active chat sessions found." if robots.nil? || robots.empty?

  checkpoint_data[:messages].each do |model_id, saved_messages|
    robot = robots[model_id]
    next unless robot

    restored_messages = saved_messages.map { |msg| deep_copy_message(msg) }
    robot.replace_messages(restored_messages)
  end

  restored_position = checkpoint_data[:position]
  removed_count = remove_invalid_checkpoints(restored_position)

  @last_checkpoint_name = name

  msg = "Context restored to checkpoint '#{name}' (position #{restored_position})."
  msg += " Removed #{removed_count} checkpoint(s) that were beyond this position." if removed_count.positive?
  msg
end

#review(args, _unused = nil) ⇒ Object Also known as: context

rubocop:disable Metrics/AbcSize



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/aia/directives/context_directives.rb', line 111

def review(args, _unused = nil)
  chats = get_chats
  return "Error: No active chat sessions found." if chats.nil? || chats.empty?

  first_chat = chats.values.first
  messages = first_chat&.messages || []

  puts "\n=== Chat Context (RubyLLM) ==="
  puts "Total messages: #{messages.size}"
  puts "Models: #{chats.keys.join(', ')}"
  puts "Checkpoints: #{checkpoint_names.join(', ')}" if checkpoint_names.any?
  puts

  positions = checkpoint_positions

  messages.each_with_index do |msg, index|
    if positions[index]
      puts "📍 [Checkpoint: #{positions[index].join(', ')}]"
      puts "-" * 40
    end

    role = msg.role.to_s.capitalize
    content = format_message_content(msg)

    puts "#{index + 1}. [#{role}]: #{content}"
    puts
  end

  if positions[messages.size]
    puts "📍 [Checkpoint: #{positions[messages.size].join(', ')}]"
    puts "-" * 40
  end

  puts "=== End of Context ==="
  ""
end