Class: Kreator::Resources

Inherits:
Object
  • Object
show all
Defined in:
lib/kreator/resources.rb

Defined Under Namespace

Classes: ResourceFile

Constant Summary collapse

DEFAULT_HOME =
File.expand_path("~/.kreator")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cwd: Dir.pwd, home_dir: DEFAULT_HOME, plugin_manager: nil, plugins_enabled: true, plugin_names: nil) ⇒ Resources

Returns a new instance of Resources.



11
12
13
14
15
16
17
18
19
20
# File 'lib/kreator/resources.rb', line 11

def initialize(cwd: Dir.pwd, home_dir: DEFAULT_HOME, plugin_manager: nil, plugins_enabled: true, plugin_names: nil)
  @cwd = File.expand_path(cwd)
  @home_dir = File.expand_path(home_dir)
  @plugin_manager = plugin_manager || PluginManager.new(
    cwd: @cwd,
    home_dir: @home_dir,
    enabled_names: plugin_names,
    plugins_enabled: plugins_enabled
  )
end

Instance Attribute Details

#cwdObject (readonly)

Returns the value of attribute cwd.



9
10
11
# File 'lib/kreator/resources.rb', line 9

def cwd
  @cwd
end

#home_dirObject (readonly)

Returns the value of attribute home_dir.



9
10
11
# File 'lib/kreator/resources.rb', line 9

def home_dir
  @home_dir
end

#plugin_managerObject (readonly)

Returns the value of attribute plugin_manager.



9
10
11
# File 'lib/kreator/resources.rb', line 9

def plugin_manager
  @plugin_manager
end

Instance Method Details

#apply_prompt_template(name, prompt) ⇒ Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
# File 'lib/kreator/resources.rb', line 52

def apply_prompt_template(name, prompt)
  template = prompt_template(name)
  raise ArgumentError, "prompt template not found: #{name}" unless template

  content = template.content
  return content.gsub("{{prompt}}", prompt.to_s) if content.include?("{{prompt}}")

  [content.rstrip, prompt.to_s].reject(&:empty?).join("\n\n")
end

#invoked_skills(prompt) ⇒ Object



70
71
72
# File 'lib/kreator/resources.rb', line 70

def invoked_skills(prompt)
  skills.select { |candidate| skill_invoked?(candidate.name, prompt) }
end

#plugin_toolsObject



78
79
80
# File 'lib/kreator/resources.rb', line 78

def plugin_tools
  PluginToolLoader.new.load_tools(plugins)
end

#pluginsObject



74
75
76
# File 'lib/kreator/resources.rb', line 74

def plugins
  plugin_manager.plugins
end

#project_instructionsObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/kreator/resources.rb', line 33

def project_instructions
  paths = ancestor_dirs.flat_map do |directory|
    [
      File.join(directory, "AGENTS.md"),
      File.join(directory, ".kreator", "instructions.md")
    ]
  end
  paths.select { |path| File.file?(path) }
       .map { |path| ResourceFile.new(name: File.basename(path), path: path, content: File.read(path)) }
end

#prompt_template(name) ⇒ Object



48
49
50
# File 'lib/kreator/resources.rb', line 48

def prompt_template(name)
  prompt_templates.find { |template| template.name == name.to_s }
end

#prompt_templatesObject



44
45
46
# File 'lib/kreator/resources.rb', line 44

def prompt_templates
  merge_named_resources(global_prompt_templates, plugin_prompt_templates, project_prompt_templates)
end

#skill(name) ⇒ Object



66
67
68
# File 'lib/kreator/resources.rb', line 66

def skill(name)
  skills.find { |candidate| candidate.name == name.to_s }
end

#skillsObject



62
63
64
# File 'lib/kreator/resources.rb', line 62

def skills
  merge_named_resources(global_skills, plugin_skills, project_skills)
end

#system_prompt(base_prompt:, prompt: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/kreator/resources.rb', line 22

def system_prompt(base_prompt:, prompt: nil)
  sections = [base_prompt.to_s]
  sections << project_instruction_section
  sections << plugin_instruction_section
  sections << prompt_template_section
  sections << skill_index_section
  loaded = merge_named_resources(autoloaded_skills, invoked_skills(prompt.to_s))
  sections << loaded_skill_section(loaded) unless loaded.empty?
  sections.compact.reject(&:empty?).join("\n\n")
end