Class: Aias::PromptScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/aias/prompt_scanner.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

PROMPTS_DIR_ENVVAR_NEW =
"AIA_PROMPTS__DIR"
PROMPTS_DIR_ENVVAR_OLD =
"AIA_PROMPTS_DIR"

Instance Method Summary collapse

Constructor Details

#initialize(prompts_dir: nil) ⇒ PromptScanner

Returns a new instance of PromptScanner.



13
14
15
# File 'lib/aias/prompt_scanner.rb', line 13

def initialize(prompts_dir: nil)
  @prompts_dir = (prompts_dir || ENV[PROMPTS_DIR_ENVVAR_NEW] || ENV[PROMPTS_DIR_ENVVAR_OLD]).to_s
end

Instance Method Details

#scanObject

Returns Array of all prompts with a non-empty schedule: key. Raises Aias::Error if the prompts directory is missing or unreadable.



19
20
21
22
# File 'lib/aias/prompt_scanner.rb', line 19

def scan
  validate_prompts_dir!
  candidate_files.filter_map { |path| build_result(path) }
end

#scan_one(path) ⇒ Object

Parses a single prompt file by path (relative or absolute). Derives the prompt_id using the configured prompts_dir. Raises Aias::Error when the file is missing/unreadable, lies outside the prompts directory, or carries no schedule: in its frontmatter.

Raises:



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aias/prompt_scanner.rb', line 28

def scan_one(path)
  absolute = File.expand_path(path)

  raise Aias::Error, "Prompt file not found: #{absolute}"    unless File.exist?(absolute)
  raise Aias::Error, "Prompt file not readable: #{absolute}" unless File.readable?(absolute)

  validate_prompts_dir!

  base = @prompts_dir.chomp("/")
  unless absolute.start_with?("#{base}/")
    raise Aias::Error, "'#{absolute}' is not inside the prompts directory '#{@prompts_dir}'"
  end

  parsed, schedule = begin
    p = PM.parse(absolute)
    [p, p.&.schedule]
  rescue => e
    raise Aias::Error, "Failed to parse '#{prompt_id_for(absolute)}': #{e.message}"
  end

  if schedule.nil? || schedule.to_s.strip.empty?
    raise Aias::Error, "'#{prompt_id_for(absolute)}' has no schedule: in its frontmatter"
  end

  Result.new(
    prompt_id: prompt_id_for(absolute),
    schedule:  schedule.to_s.strip,
    metadata:  parsed.,
    file_path: absolute
  )
end