Module: Legion::CLI::Chat::OutputStyles

Defined in:
lib/legion/cli/chat/output_styles.rb

Constant Summary collapse

STYLE_DIRS =
['.legionio/output-styles', '~/.legionio/output-styles'].freeze

Class Method Summary collapse

Class Method Details

.activate(name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/legion/cli/chat/output_styles.rb', line 29

def activate(name)
  style = find(name)
  return nil unless style

  path = style[:path]
  content = File.read(path)
  content.sub!(/^---\s*$/, "---\nactive: true") unless content.match?(/^active:\s/)
  content.gsub!(/^active:\s+\w+/, 'active: true')
  File.write(path, content)
  style[:name]
end

.active_stylesObject



21
22
23
# File 'lib/legion/cli/chat/output_styles.rb', line 21

def active_styles
  discover.select { |s| s[:active] }
end

.discoverObject



12
13
14
15
16
17
18
19
# File 'lib/legion/cli/chat/output_styles.rb', line 12

def discover
  STYLE_DIRS.flat_map do |dir|
    expanded = File.expand_path(dir)
    next [] unless Dir.exist?(expanded)

    Dir.glob(File.join(expanded, '*.md')).filter_map { |f| parse(f) }
  end
end

.find(name) ⇒ Object



25
26
27
# File 'lib/legion/cli/chat/output_styles.rb', line 25

def find(name)
  discover.find { |s| s[:name] == name.to_s }
end

.parse(path) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/legion/cli/chat/output_styles.rb', line 48

def parse(path)
  raw = File.read(path)
  return nil unless raw.start_with?('---')

  parts = raw.split(/^---\s*$/, 3)
  return nil if parts.size < 3

  frontmatter = YAML.safe_load(parts[1], permitted_classes: [Symbol])
  body = parts[2]&.strip

  {
    name:        frontmatter['name'] || File.basename(path, '.md'),
    description: frontmatter['description'] || '',
    active:      frontmatter['active'] == true,
    content:     body,
    path:        path
  }
rescue StandardError => e
  Legion::Logging.warn "OutputStyles parse error #{path}: #{e.message}" if defined?(Legion::Logging)
  nil
end

.system_prompt_injectionObject



41
42
43
44
45
46
# File 'lib/legion/cli/chat/output_styles.rb', line 41

def system_prompt_injection
  active = active_styles
  return nil if active.empty?

  active.map { |s| s[:content] }.join("\n\n")
end