Module: Legion::Extensions::Agentic::Integration::Context::Runners::Context

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/agentic/integration/context/runners/context.rb

Instance Method Summary collapse

Instance Method Details

#activate_context(frame_id:) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/legion/extensions/agentic/integration/context/runners/context.rb', line 19

def activate_context(frame_id:, **)
  result = context_manager.activate(frame_id)
  return { success: false, reason: :not_found } unless result

  frame = result[:frame]
  log.debug("[context] activated frame=#{frame.name} cost=#{result[:switch_cost].round(3)}")
  { success: true, frame: frame.to_h, switch_cost: result[:switch_cost] }
end

#add_cue(frame_id:, cue:) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/legion/extensions/agentic/integration/context/runners/context.rb', line 61

def add_cue(frame_id:, cue:, **)
  frame = context_manager.find(frame_id)
  return { success: false, reason: :not_found } unless frame

  frame.add_cue(cue)
  { success: true, frame: frame.to_h }
end

#auto_switch(input_cues:) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/legion/extensions/agentic/integration/context/runners/context.rb', line 39

def auto_switch(input_cues:, **)
  result = context_manager.auto_switch(input_cues)
  return { success: true, switched: false, reason: :no_better_match } unless result

  frame = result[:frame]
  log.debug("[context] auto-switched to frame=#{frame.name} cost=#{result[:switch_cost].round(3)}")
  { success: true, switched: true, frame: frame.to_h, switch_cost: result[:switch_cost] }
end

#context_statsObject



79
80
81
# File 'lib/legion/extensions/agentic/integration/context/runners/context.rb', line 79

def context_stats(**)
  { success: true, stats: context_manager.to_h }
end

#create_context(name:, domain: :general, cues: []) ⇒ Object



13
14
15
16
17
# File 'lib/legion/extensions/agentic/integration/context/runners/context.rb', line 13

def create_context(name:, domain: :general, cues: [], **)
  frame = context_manager.create_frame(name: name, domain: domain, cues: cues)
  log.debug("[context] created frame=#{name} domain=#{domain} cues=#{cues.size}")
  { success: true, frame: frame.to_h }
end

#current_contextObject



48
49
50
51
52
53
# File 'lib/legion/extensions/agentic/integration/context/runners/context.rb', line 48

def current_context(**)
  frame = context_manager.current_frame
  return { success: true, frame: nil } unless frame

  { success: true, frame: frame.to_h }
end

#detect_context(input_cues:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/legion/extensions/agentic/integration/context/runners/context.rb', line 28

def detect_context(input_cues:, **)
  matches = context_manager.detect_context(input_cues)
  log.debug("[context] detect: #{matches.size} candidates for #{input_cues.size} cues")
  {
    success:    true,
    candidates: matches.map { |m| { frame: m[:frame].to_h, relevance: m[:relevance] } },
    count:      matches.size,
    best:       matches.first && { name: matches.first[:frame].name, relevance: matches.first[:relevance] }
  }
end

#frames_in_domain(domain:) ⇒ Object



69
70
71
72
# File 'lib/legion/extensions/agentic/integration/context/runners/context.rb', line 69

def frames_in_domain(domain:, **)
  frames = context_manager.in_domain(domain)
  { success: true, frames: frames.map(&:to_h), count: frames.size }
end

#remove_context(frame_id:) ⇒ Object



74
75
76
77
# File 'lib/legion/extensions/agentic/integration/context/runners/context.rb', line 74

def remove_context(frame_id:, **)
  context_manager.remove(frame_id)
  { success: true }
end

#update_contextObject



55
56
57
58
59
# File 'lib/legion/extensions/agentic/integration/context/runners/context.rb', line 55

def update_context(**)
  context_manager.decay_all
  log.debug("[context] tick: frames=#{context_manager.frames.size} active=#{context_manager.current_frame&.name}")
  { success: true, frame_count: context_manager.frames.size, active: context_manager.current_frame&.name }
end