Class: Agentic::PersistentAgentStore

Inherits:
Object
  • Object
show all
Defined in:
lib/agentic/persistent_agent_store.rb

Overview

Responsible for storing and retrieving agent configurations persistently

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage_path = nil, registry = AgentCapabilityRegistry.instance, options = {}) ⇒ PersistentAgentStore

Initialize a new persistent agent store

Parameters:

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

    The path to the storage directory

  • registry (AgentCapabilityRegistry) (defaults to: AgentCapabilityRegistry.instance)

    The capability registry for instantiation

  • options (Hash) (defaults to: {})

    Additional options

Options Hash (options):

  • :logger (Logger)

    Custom logger (defaults to Agentic.logger)



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/agentic/persistent_agent_store.rb', line 20

def initialize(storage_path = nil, registry = AgentCapabilityRegistry.instance, options = {})
  @registry = registry
  @storage_path = storage_path || default_storage_path
  @logger = options[:logger] || Agentic.logger
  @index = {}

  # Create the storage directory if it doesn't exist
  FileUtils.mkdir_p(@storage_path) unless File.directory?(@storage_path)

  # Initialize the index
  initialize_index
end

Instance Attribute Details

#registryAgentCapabilityRegistry (readonly)

The capability registry used for instantiation

Returns:



12
13
14
# File 'lib/agentic/persistent_agent_store.rb', line 12

def registry
  @registry
end

#storage_pathString (readonly)

Directory to store agent configurations

Returns:

  • (String)

    the current value of storage_path



12
13
14
# File 'lib/agentic/persistent_agent_store.rb', line 12

def storage_path
  @storage_path
end

Instance Method Details

#build_agent(id_or_name, version: nil) ⇒ Agent?

Build an agent from a stored configuration

Parameters:

  • id_or_name (String)

    The ID or name of the agent

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

    The version to load (latest if nil)

Returns:

  • (Agent, nil)

    The built agent or nil if not found



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/agentic/persistent_agent_store.rb', line 74

def build_agent(id_or_name, version: nil)
  # First try to find by ID
  agent_data = find_agent_data(id_or_name, version)

  # If not found by ID, try to find by name
  unless agent_data
    id = find_id_by_name(id_or_name)
    agent_data = id ? find_agent_data(id, version) : nil
  end

  return nil unless agent_data

  # Create a new agent
  agent = Agent.build do |a|
    a.role = agent_data[:agent][:role]
    a.purpose = agent_data[:agent][:purpose]
    a.backstory = agent_data[:agent][:backstory]
  end

  # Add capabilities
  agent_data[:capabilities].each do |capability|
    agent.add_capability(capability[:name], capability[:version])
  rescue => e
    @logger.warn("Failed to add capability: #{capability[:name]} v#{capability[:version]} - #{e.message}")
  end

  agent
end

#delete(id_or_name, version: nil) ⇒ Boolean

Delete an agent from the store

Parameters:

  • id_or_name (String)

    The ID or name of the agent to delete

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

    The version to delete (all versions if nil)

Returns:

  • (Boolean)

    True if successfully deleted



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/agentic/persistent_agent_store.rb', line 175

def delete(id_or_name, version: nil)
  # First try to find by ID
  id = id_or_name

  # If not found in index, try to find by name
  unless @index[id]
    id = find_id_by_name(id_or_name)
    return false unless id
  end

  # Check if the agent exists
  return false unless @index[id]

  if version
    # Delete specific version
    return false unless @index[id][version]

    # Delete from storage
    delete_from_storage(id, version)

    # Update index
    @index[id].delete(version)
    @index.delete(id) if @index[id].empty?
  else
    # Delete all versions
    @index[id].each_key do |ver|
      delete_from_storage(id, ver)
    end

    # Update index
    @index.delete(id)
  end

  # Save the index
  save_index

  true
end

#list_all(filter = {}) ⇒ Array<Hash> Also known as: all

List all stored agent configurations

Parameters:

  • filter (Hash) (defaults to: {})

    Filter criteria

Options Hash (filter):

  • :capability (String)

    Filter by capability name

  • :capability_version (String)

    Filter by capability name and version

  • :after (Time, String)

    Filter agents stored after this time

  • :before (Time, String)

    Filter agents stored before this time

  • :metadata (Hash)

    Filter by metadata values

Returns:

  • (Array<Hash>)

    Array of agent configurations



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/agentic/persistent_agent_store.rb', line 111

def list_all(filter = {})
  results = []

  @index.each do |id, versions|
    # Get the latest version for each agent by default
    version = get_latest_version(id)

    # Skip if no version found
    next unless version

    # Get the agent data
    agent_data = versions[version]

    # Skip if no data found
    next unless agent_data

    # Add ID and version to the data
    full_data = agent_data.merge(
      id: id,
      version: version
    )

    # Apply filters
    if matches_filter?(full_data, filter)
      results << full_data
    end
  end

  # Sort by timestamp (newest first)
  results.sort_by { |data| data[:timestamp] }.reverse
end

#store(agent, name: nil, metadata: {}) ⇒ String

Store an agent configuration

Parameters:

  • agent (Agent)

    The agent to store

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

    The name to use for the agent (generated if nil)

  • metadata (Hash) (defaults to: {})

    Additional metadata to store with the agent

Returns:

  • (String)

    The ID of the stored agent



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/agentic/persistent_agent_store.rb', line 38

def store(agent, name: nil, metadata: {})
  # Generate ID if agent doesn't have one
  id = agent&.id || SecureRandom.uuid

  # Generate version
  version = generate_version(id)

  # Create agent data structure
  agent_data = {
    id: id,
    name: name || generate_name(agent),
    version: version,
    timestamp: Time.now.iso8601,
    agent: agent.to_h,
    capabilities: agent.capabilities.keys.map do |capability_name|
      {
        name: capability_name,
        version: agent.capability_specification(capability_name)&.version
      }
    end,
    metadata: 
  }

  # Save to storage
  save_to_storage(id, version, agent_data)

  # Update index
  update_index(id, version, agent_data)

  id
end

#version_history(id) ⇒ Array<Hash>

Get the version history for an agent

Parameters:

  • id (String)

    The ID of the agent

Returns:

  • (Array<Hash>)

    The version history or empty array if not found



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/agentic/persistent_agent_store.rb', line 151

def version_history(id)
  # Check if the agent exists
  return [] unless @index[id]

  # Get all versions and sort by timestamp
  versions = @index[id].map do |version, data|
    {
      id: id,
      name: data[:name],
      version: version,
      timestamp: data[:timestamp],
      capabilities: data[:capabilities],
      metadata: data[:metadata]
    }
  end

  # Sort by timestamp (newest first)
  versions.sort_by { |v| v[:timestamp] }.reverse
end