Class: AgentsSkillVault::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/agents_skill_vault/manifest.rb

Overview

Manages the JSON manifest file that tracks all resources in a vault.

The manifest stores metadata about each resource including labels, URLs, and sync timestamps. It persists to disk as a JSON file.

Examples:

manifest = Manifest.new(path: "/vault/manifest.json")
manifest.add_resource(resource)
manifest.find_resource("user/repo")

Constant Summary collapse

VERSION =

Current manifest file format version

"1.0"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:) ⇒ Manifest

Creates a new Manifest instance.

Parameters:

  • path (String)

    Path to the manifest JSON file



27
28
29
# File 'lib/agents_skill_vault/manifest.rb', line 27

def initialize(path:)
  @path = path
end

Instance Attribute Details

#pathString (readonly)

Returns Absolute path to the manifest JSON file.

Returns:

  • (String)

    Absolute path to the manifest JSON file



21
22
23
# File 'lib/agents_skill_vault/manifest.rb', line 21

def path
  @path
end

Instance Method Details

#add_resource(resource) ⇒ void

This method returns an undefined value.

Adds a new resource to the manifest.

Parameters:

  • resource (Resource)

    The resource to add

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/agents_skill_vault/manifest.rb', line 64

def add_resource(resource)
  data = load
  data[:resources] ||= []

  if data[:resources].any? { |r| r[:label] == resource.label }
    raise Errors::DuplicateLabel, "Resource with label '#{resource.label}' already exists. " \
                                  "Use a custom label with: add(url, label: 'custom-label')"
  end

  data[:resources] << resource.to_h
  save(data)
end

#clearvoid

This method returns an undefined value.

Clears all resources from the manifest.



124
125
126
# File 'lib/agents_skill_vault/manifest.rb', line 124

def clear
  save(default_manifest)
end

#find_resource(label, storage_path: nil) ⇒ Resource?

Finds a resource by its label.

Parameters:

  • label (String)

    The label to search for

  • storage_path (String, nil) (defaults to: nil)

    Storage path to set on the returned Resource

Returns:

  • (Resource, nil)

    The resource if found, nil otherwise



113
114
115
116
117
118
# File 'lib/agents_skill_vault/manifest.rb', line 113

def find_resource(label, storage_path: nil)
  hash = resources.find { |r| r[:label] == label }
  return nil unless hash

  Resource.from_h(hash, storage_path: storage_path || storage_path_from_manifest)
end

#loadHash

Loads the manifest data from disk.

Returns:

  • (Hash)

    The manifest data with :version and :resources keys



35
36
37
38
39
# File 'lib/agents_skill_vault/manifest.rb', line 35

def load
  return default_manifest unless File.exist?(path)

  JSON.parse(File.read(path), symbolize_names: true)
end

#remove_resource(label) ⇒ void

This method returns an undefined value.

Removes a resource from the manifest by label.

Does nothing if the label doesn't exist (no error raised).

Parameters:

  • label (String)

    The label of the resource to remove



84
85
86
87
88
# File 'lib/agents_skill_vault/manifest.rb', line 84

def remove_resource(label)
  data = load
  data[:resources]&.reject! { |r| r[:label] == label }
  save(data)
end

#resourcesArray<Hash>

Returns all resource hashes from the manifest.

Returns:

  • (Array<Hash>)

    Array of resource attribute hashes



54
55
56
# File 'lib/agents_skill_vault/manifest.rb', line 54

def resources
  load[:resources] || []
end

#save(data) ⇒ void

This method returns an undefined value.

Saves manifest data to disk.

Parameters:

  • data (Hash)

    The manifest data to save



46
47
48
# File 'lib/agents_skill_vault/manifest.rb', line 46

def save(data)
  File.write(path, JSON.pretty_generate(data))
end

#update_resource(resource) ⇒ Boolean

Updates an existing resource in the manifest.

Parameters:

  • resource (Resource)

    The resource with updated attributes

Returns:

  • (Boolean)

    true if the resource was found and updated, false otherwise



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/agents_skill_vault/manifest.rb', line 95

def update_resource(resource) # rubocop:disable Naming/PredicateMethod
  data = load
  data[:resources] ||= []
  index = data[:resources].find_index { |r| r[:label] == resource.label }

  return false unless index

  data[:resources][index] = resource.to_h
  save(data)
  true
end