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

Constructor Details

#initializeContextDirectives

Returns a new instance of ContextDirectives.



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

def initialize
  super
  reset!
end

Instance Attribute Details

#checkpoint_counterObject

Returns the value of attribute checkpoint_counter.



8
9
10
# File 'lib/aia/directives/context_directives.rb', line 8

def checkpoint_counter
  @checkpoint_counter
end

#checkpoint_storeObject

Returns the value of attribute checkpoint_store.



8
9
10
# File 'lib/aia/directives/context_directives.rb', line 8

def checkpoint_store
  @checkpoint_store
end

#last_checkpoint_nameObject

Returns the value of attribute last_checkpoint_name.



8
9
10
# File 'lib/aia/directives/context_directives.rb', line 8

def last_checkpoint_name
  @last_checkpoint_name
end

Instance Method Details

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



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

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 { |chat|
      chat.messages.map { |msg| deep_copy_message(msg) }
    },
    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) —



174
175
176
# File 'lib/aia/directives/context_directives.rb', line 174

def checkpoint_names
  @checkpoint_store.keys
end

#checkpoint_positionsObject



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

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



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

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



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

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

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

  chats.each do |_model_id, chat|
    if keep_system
      system_msg = chat.messages.find { |m| m.role == :system }
      chat.instance_variable_set(:@messages, [])
      chat.add_message(system_msg) if system_msg
    else
      chat.instance_variable_set(:@messages, [])
    end
  end

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

  "Chat context cleared."
end

#find_previous_checkpointObject



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

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



188
189
190
191
192
# File 'lib/aia/directives/context_directives.rb', line 188

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



15
16
17
18
19
# File 'lib/aia/directives/context_directives.rb', line 15

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

#restore(args, _unused = nil) ⇒ Object



51
52
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
# File 'lib/aia/directives/context_directives.rb', line 51

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]
  chats = get_chats

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

  checkpoint_data[:messages].each do |model_id, saved_messages|
    chat = chats[model_id]
    next unless chat

    restored_messages = saved_messages.map { |msg| deep_copy_message(msg) }
    chat.instance_variable_set(:@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 > 0
  msg
end

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



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
147
148
149
# File 'lib/aia/directives/context_directives.rb', line 114

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