Class: SwarmMemory::Tools::MemorySearch

Inherits:
SwarmSDK::Tools::Base
  • Object
show all
Defined in:
lib/swarm_memory/tools/memory_search.rb

Overview

Tool for semantic search across memory entries

Searches content stored in memory using AI embeddings to find conceptually related entries even when exact keywords don't match. Each agent has its own isolated memory storage.

Constant Summary collapse

MAX_RESULTS =

Maximum results to prevent context overflow

50

Instance Method Summary collapse

Constructor Details

#initialize(storage:, agent_name:) ⇒ MemorySearch

Initialize with storage instance

Parameters:

  • Storage instance

  • Agent identifier



71
72
73
74
75
# File 'lib/swarm_memory/tools/memory_search.rb', line 71

def initialize(storage:, agent_name:)
  super()
  @storage = storage
  @agent_name = agent_name.to_sym
end

Instance Method Details

#execute(query:, top_k: 10, threshold: 0.0, filter_type: nil, filter_domain: nil) ⇒ String

Execute the tool

Parameters:

  • Natural language search query

  • (defaults to: 10)

    Maximum number of results

  • (defaults to: 0.0)

    Minimum similarity score

  • (defaults to: nil)

    Type filter

  • (defaults to: nil)

    Domain filter

Returns:

  • Formatted search results



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/swarm_memory/tools/memory_search.rb', line 90

def execute(query:, top_k: 10, threshold: 0.0, filter_type: nil, filter_domain: nil)
  # Reset state for multiple types post-filtering
  @requested_types = nil

  # 1. Check semantic index availability
  unless @storage.semantic_index
    return validation_error("Semantic search not available (no embedder configured)")
  end

  # 2. Validate parameters
  top_k = validate_top_k(top_k)
  return top_k if top_k.is_a?(String) # Error message

  threshold = validate_threshold(threshold)
  return threshold if threshold.is_a?(String) # Error message

  # 3. Build filter hash (may set @requested_types for multi-type filtering)
  filter = build_filter(filter_type, filter_domain)

  # 4. Perform semantic search
  # For multiple types, we get extra results to compensate for post-filtering
  search_top_k = @requested_types && @requested_types.size > 1 ? top_k * 3 : top_k
  results = @storage.semantic_index.search(
    query: query,
    top_k: search_top_k,
    threshold: threshold,
    filter: filter,
  )

  # 5. Format and return results (handles multi-type post-filtering)
  format_results(results, query, threshold, top_k)
rescue StandardError => e
  validation_error("Search failed: #{e.message}")
end

#nameObject

Override name to return simple "MemorySearch"



78
79
80
# File 'lib/swarm_memory/tools/memory_search.rb', line 78

def name
  "MemorySearch"
end