Module: AIA::SkillUtils

Extended by:
SkillUtils
Included in:
ChatLoop, PromptHandler, PromptPipeline, SkillUtils, WebAndFileDirectives
Defined in:
lib/aia/skill_utils.rb

Instance Method Summary collapse

Instance Method Details

#find_skill_dir(skill_name, base_dir) ⇒ Object



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

def find_skill_dir(skill_name, base_dir)
  if path_based_id?(skill_name)
    expanded = File.expand_path(skill_name)
    return expanded if Dir.exist?(expanded)
    return expanded if File.file?(expanded)
    return nil
  end

  exact = File.join(base_dir, skill_name)
  return safe_skill_path(exact, base_dir) if Dir.exist?(exact)

  Dir.children(base_dir).sort.each do |entry|
    next unless entry.start_with?(skill_name)
    candidate = File.join(base_dir, entry)
    return safe_skill_path(candidate, base_dir) if Dir.exist?(candidate)
  end

  nil
rescue Errno::ENOENT
  nil
end

#parse_front_matter(path) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/aia/skill_utils.rb', line 13

def parse_front_matter(path)
  return {} unless File.exist?(path)
  content = File.read(path)
  return {} unless content.start_with?('---')
  end_marker = content.index("\n---", 3)
  return {} unless end_marker
  yaml_text = content[3...end_marker]
  YAML.safe_load(yaml_text) || {}
rescue StandardError
  {}
end

#path_based_id?(id) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/aia/skill_utils.rb', line 9

def path_based_id?(id)
  id.to_s.start_with?('/', './', '../', '~/')
end

#safe_skill_path(path, dir) ⇒ Object



54
55
56
57
58
59
# File 'lib/aia/skill_utils.rb', line 54

def safe_skill_path(path, dir)
  resolved = File.realpath(path)
  resolved.start_with?(File.realpath(dir) + File::SEPARATOR) ? resolved : nil
rescue Errno::ENOENT
  nil
end

#skill_body(content) ⇒ Object



47
48
49
50
51
52
# File 'lib/aia/skill_utils.rb', line 47

def skill_body(content)
  return content unless content.start_with?('---')
  end_marker = content.index("\n---", 3)
  return content unless end_marker
  content[(end_marker + 4)..].lstrip
end