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/shared (preferred) or legacy 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
FROM_PREFIXES =

Logical import prefixes → physical shared root (app/agents/shared). "library/" kept for backwards-compatible imports.yml files.

%w[shared/ library/].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent_directory, library_root: nil) ⇒ LibraryImports

Returns a new instance of LibraryImports.



31
32
33
34
# File 'lib/rails_agents/library_imports.rb', line 31

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.



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

def agent_directory
  @agent_directory
end

#library_rootObject (readonly)

Returns the value of attribute library_root.



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

def library_root
  @library_root
end

Class Method Details

.resolve_library_root(agents_root) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/rails_agents/library_imports.rb', line 70

def self.resolve_library_root(agents_root)
  agents_root = Pathname.new(agents_root)
  shared = agents_root.join("shared")
  return shared if shared.exist?

  legacy = agents_root.parent.join("agents_library")
  return legacy if legacy.exist?

  shared
end

Instance Method Details

#entriesObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rails_agents/library_imports.rb', line 36

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



66
67
68
# File 'lib/rails_agents/library_imports.rb', line 66

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

#virtual_filesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails_agents/library_imports.rb', line 48

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

    if entry.kind == "package"
      package_virtual_files(entry)
    else
      raise Error, "Missing shared 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