Class: SwarmMemory::DSL::MemoryConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm_memory/dsl/memory_config.rb

Overview

Memory configuration for agents

This class is injected into SwarmSDK when swarm_memory is required, allowing agents to configure memory via the DSL.

Supports custom adapters through options hash that gets passed through to the adapter constructor.

Examples:

Filesystem adapter

memory do
  adapter :filesystem
  directory ".swarm/memory"
end

Custom adapter

memory do
  adapter :activerecord
  option :namespace, "my_agent"
  option :table_name, "memory_entries"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemoryConfig

Returns a new instance of MemoryConfig.



28
29
30
31
32
# File 'lib/swarm_memory/dsl/memory_config.rb', line 28

def initialize
  @adapter_type = :filesystem # Default adapter
  @adapter_options = {} # Options passed to adapter constructor
  @mode = :read_write # Default mode
end

Instance Attribute Details

#adapter_optionsObject (readonly)

Returns the value of attribute adapter_options.



26
27
28
# File 'lib/swarm_memory/dsl/memory_config.rb', line 26

def adapter_options
  @adapter_options
end

#adapter_typeObject (readonly)

Returns the value of attribute adapter_type.



26
27
28
# File 'lib/swarm_memory/dsl/memory_config.rb', line 26

def adapter_type
  @adapter_type
end

Instance Method Details

#adapter(value = nil) ⇒ Symbol

DSL method to set/get adapter type

Parameters:

  • value (Symbol, nil) (defaults to: nil)

    Adapter type

Returns:

  • (Symbol)

    Current adapter



38
39
40
41
42
# File 'lib/swarm_memory/dsl/memory_config.rb', line 38

def adapter(value = nil)
  return @adapter_type if value.nil?

  @adapter_type = value.to_sym
end

#directory(value = nil) ⇒ String

DSL method to set/get directory (convenience for filesystem adapter)

Equivalent to: option :directory, value

Parameters:

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

    Memory directory path

Returns:

  • (String)

    Current directory



64
65
66
67
68
69
70
# File 'lib/swarm_memory/dsl/memory_config.rb', line 64

def directory(value = nil)
  if value.nil?
    @adapter_options[:directory]
  else
    @adapter_options[:directory] = value
  end
end

#enabled?Boolean

Check if memory is enabled

Returns:

  • (Boolean)

    True if adapter is configured with required options



127
128
129
130
131
132
133
134
135
136
# File 'lib/swarm_memory/dsl/memory_config.rb', line 127

def enabled?
  case @adapter_type
  when :filesystem
    !@adapter_options[:directory].nil?
  else
    # For custom adapters, assume enabled if adapter is set
    # Custom adapter will validate its own requirements
    true
  end
end

#keyword_weight(value = nil) ⇒ Float?

DSL method to set/get keyword weight for hybrid search

Controls how much keyword (tag) matching affects search results. Default is 0.5 (50%). Set to 0.0 to disable keyword matching.

Examples:

Disable keyword matching

keyword_weight 0.0

Parameters:

  • value (Float, nil) (defaults to: nil)

    Weight between 0.0 and 1.0

Returns:

  • (Float, nil)

    Current keyword weight



116
117
118
119
120
121
122
# File 'lib/swarm_memory/dsl/memory_config.rb', line 116

def keyword_weight(value = nil)
  if value.nil?
    @adapter_options[:keyword_weight]
  else
    @adapter_options[:keyword_weight] = value.to_f
  end
end

#mode(value = nil) ⇒ Symbol

DSL method to set/get mode

Modes:

  • :read_write (default) - Read + Write + Edit, balanced for learning and retrieval
  • :read_only - Read-only, optimized for Q&A
  • :full_access - All tools including Delete and Defrag, optimized for knowledge management

Parameters:

  • value (Symbol, nil) (defaults to: nil)

    Memory mode

Returns:

  • (Symbol)

    Current mode



81
82
83
84
85
# File 'lib/swarm_memory/dsl/memory_config.rb', line 81

def mode(value = nil)
  return @mode if value.nil?

  @mode = value.to_sym
end

#option(key, value) ⇒ Object

DSL method to set adapter option (generic)

This allows passing any option to the adapter constructor.

Examples:

option :namespace, "my_agent"
option :connection_pool_size, 5

Parameters:

  • key (Symbol)

    Option key

  • value (Object)

    Option value



54
55
56
# File 'lib/swarm_memory/dsl/memory_config.rb', line 54

def option(key, value)
  @adapter_options[key.to_sym] = value
end

#semantic_weight(value = nil) ⇒ Float?

DSL method to set/get semantic weight for hybrid search

Controls how much semantic (embedding) similarity affects search results. Default is 0.5 (50%). Set to 1.0 for pure semantic search.

Examples:

Pure semantic search (no keyword penalty)

semantic_weight 1.0
keyword_weight 0.0

Parameters:

  • value (Float, nil) (defaults to: nil)

    Weight between 0.0 and 1.0

Returns:

  • (Float, nil)

    Current semantic weight



98
99
100
101
102
103
104
# File 'lib/swarm_memory/dsl/memory_config.rb', line 98

def semantic_weight(value = nil)
  if value.nil?
    @adapter_options[:semantic_weight]
  else
    @adapter_options[:semantic_weight] = value.to_f
  end
end

#to_hHash

Convert config to hash (for SDK plugin)

Returns:

  • (Hash)

    Configuration as hash



141
142
143
144
145
146
147
148
# File 'lib/swarm_memory/dsl/memory_config.rb', line 141

def to_h
  {
    adapter: @adapter_type,
    mode: @mode,
    loadskill_preserve_delegation: @loadskill_preserve_delegation,
    **@adapter_options,
  }
end