Class: AIA::WebAndFileDirectives

Inherits:
Directive
  • Object
show all
Includes:
SkillUtils
Defined in:
lib/aia/directives/web_and_file_directives.rb

Constant Summary collapse

PUREMD_API_KEY =
ENV.fetch('PUREMD_API_KEY', nil)

Constants inherited from Directive

Directive::DIRECTIVE_PREFIX

Instance Method Summary collapse

Methods included from SkillUtils

#find_skill_dir, #parse_front_matter, #path_based_id?, #safe_skill_path, #skill_body

Methods inherited from Directive

build_dispatch_block, help

Instance Method Details

#paste(_args = [], _context_manager = nil) ⇒ Object Also known as: clipboard



131
132
133
134
135
136
# File 'lib/aia/directives/web_and_file_directives.rb', line 131

def paste(_args = [], _context_manager = nil)
  content = Clipboard.paste
  content.to_s
rescue StandardError => e
  "Error: Unable to paste from clipboard - #{e.message}"
end

#skill(args = [], _context_manager = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/aia/directives/web_and_file_directives.rb', line 88

def skill(args = [], _context_manager = nil)
  args = Array(args)
  skill_name = args.first&.strip

  if skill_name.nil? || skill_name.empty?
    warn "Error: /skill requires a skill name. Use /skills to list available skills."
    AIA::LoggerManager.aia_logger.error("/skill requires a skill name")
    return nil
  end

  dir = aia_skills_dir

  unless Dir.exist?(dir)
    warn "Error: Skills directory not found at #{dir}"
    AIA::LoggerManager.aia_logger.error("Skills directory not found at #{dir}")
    return nil
  end

  skill_dir = find_skill_dir(skill_name, dir)
  unless skill_dir
    if path_based_id?(skill_name)
      warn "Error: No skill directory found at '#{File.expand_path(skill_name)}'. Use /skills to list available skills."
      AIA::LoggerManager.aia_logger.error("No skill directory found at '#{File.expand_path(skill_name)}'")
    else
      warn "Error: No skill matching '#{skill_name}' found in #{dir}. Use /skills to list available skills."
      AIA::LoggerManager.aia_logger.error("No skill matching '#{skill_name}' found in #{dir}")
    end
    return nil
  end

  return File.read(skill_dir) if File.file?(skill_dir)

  skill_path = File.join(skill_dir, 'SKILL.md')
  unless File.exist?(skill_path)
    warn "Error: Skill directory '#{File.basename(skill_dir)}' has no SKILL.md. Use /skills to list available skills."
    AIA::LoggerManager.aia_logger.error("Skill directory '#{File.basename(skill_dir)}' has no SKILL.md")
    return nil
  end

  File.read(skill_path)
end

#skills(args = [], _context_manager = nil) ⇒ Object



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aia/directives/web_and_file_directives.rb', line 37

def skills(args = [], _context_manager = nil)
  dir = aia_skills_dir

  unless Dir.exist?(dir)
    puts "No skills directory found at #{dir}"
    return nil
  end

  positive_terms, negative_terms = parse_search_terms(Array(args))

  skill_dirs = Dir.children(dir)
                  .select { |e| Dir.exist?(File.join(dir, e)) }
                  .sort

  if skill_dirs.empty?
    puts "No skills found in #{dir}"
    return nil
  end

  skill_data = skill_dirs.filter_map do |name|
    fm = parse_front_matter(File.join(dir, name, 'SKILL.md'))
    text = "#{name} #{fm.values.join(' ').downcase}"
    pos_ok = positive_terms.empty? || positive_terms.all? { |t| text.include?(t) }
    neg_ok = negative_terms.none? { |t| text.include?(t) }
    [name, fm] if pos_ok && neg_ok
  end

  if skill_data.empty?
    puts "No skills matching your query"
    return nil
  end

  width = terminal_width
  puts "\nAvailable Skills (#{dir}):"
  puts "=" * [width, 60].min

  skill_data.each do |name, fm|
    display_name = fm['name'] || name
    desc_text = fm['description'].to_s.strip
    puts "\n  #{display_name}"
    unless desc_text.empty?
      puts WordWrapper::MinimumRaggedness.new(width - 4, desc_text).wrap
        .split("\n").map { |l| "    #{l}" }.join("\n")
    end
  end

  puts "\nTotal: #{skill_data.size} skill#{'s' if skill_data.size != 1}"
  nil
end

#webpage(args, _context_manager = nil) ⇒ Object Also known as: website, web



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aia/directives/web_and_file_directives.rb', line 15

def webpage(args, _context_manager = nil)
  if PUREMD_API_KEY.nil?
    'ERROR: PUREMD_API_KEY is required in order to include a webpage'
  else
    url = args.shift.to_s.strip
    puremd_url = "https://pure.md/#{url}"

    response = Faraday.get(puremd_url) do |req|
      req.headers['x-puremd-api-token'] = PUREMD_API_KEY
    end

    if 200 == response.status
      response.body
    else
      "Error: Status was #{response.status}\n#{ap response}"
    end
  end
end