Class: RailsAgents::LibraryImports

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_agents/library_imports.rb

Overview

Resolves workspace-shared capabilities declared by an agent's imports.yml. Sources live under app/agents_library; sync uploads virtual agent-local copies so the cloud runtime keeps its existing app/agents// contract.

Defined Under Namespace

Classes: Entry, Error

Constant Summary collapse

KINDS =
%w[tool skill connector plugin knowledge package].freeze
EXTENSIONS =
{
  "tool" => ".rb",
  "skill" => ".rb",
  "connector" => ".yml",
  "plugin" => ".yml",
  "knowledge" => ".md",
  "package" => ".yml"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_directory, library_root: nil) ⇒ LibraryImports

Returns a new instance of LibraryImports.



27
28
29
30
# File 'lib/rails_agents/library_imports.rb', line 27

def initialize(agent_directory, library_root: nil)
  @agent_directory = Pathname.new(agent_directory).expand_path
  @library_root = library_root ? Pathname.new(library_root).expand_path : default_library_root
end

Instance Attribute Details

#agent_directoryObject (readonly)

Returns the value of attribute agent_directory.



25
26
27
# File 'lib/rails_agents/library_imports.rb', line 25

def agent_directory
  @agent_directory
end

#library_rootObject (readonly)

Returns the value of attribute library_root.



25
26
27
# File 'lib/rails_agents/library_imports.rb', line 25

def library_root
  @library_root
end

Instance Method Details

#entriesObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rails_agents/library_imports.rb', line 32

def entries
  return [] unless manifest_path.file?

  document = YAML.safe_load(manifest_path.read, permitted_classes: [], aliases: false) || {}
  imports = document.fetch("imports", [])
  raise Error, "#{manifest_path} must contain an imports array" unless imports.is_a?(Array)

  imports.map { |raw| build_entry(raw) }
rescue Psych::Exception => e
  raise Error, "Invalid #{manifest_path}: #{e.message}"
end

#knowledge_pathsObject



62
63
64
# File 'lib/rails_agents/library_imports.rb', line 62

def knowledge_paths
  entries.select { |entry| entry.kind == "knowledge" }.map { |entry| entry.target.to_s }
end

#virtual_filesObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rails_agents/library_imports.rb', line 44

def virtual_files
  entries.flat_map do |entry|
    raise Error, "Missing Library source: #{entry.source}" unless entry.source.exist?

    if entry.kind == "package"
      package_virtual_files(entry)
    else
      raise Error, "Missing Library source: #{entry.source}" unless entry.source.file?

      [{
        "path" => cloud_path(entry.target),
        "content" => entry.source.read,
        "library_source" => entry.source.relative_path_from(library_root).to_s
      }]
    end
  end
end