Module: SwarmMemory::Tools::TitleLookup

Included in:
MemoryGrep, MemoryRead
Defined in:
lib/swarm_memory/tools/title_lookup.rb

Overview

Shared module for looking up memory entry titles

Provides a consistent way to look up titles for memory entries across different tools (MemoryRead, MemoryGrep, etc.)

Examples:

Including in a tool

class MemoryGrep < RubyLLM::Tool
  include TitleLookup

  def some_method
    title = lookup_title("concept/ruby/classes.md")
  end
end

Instance Method Summary collapse

Instance Method Details

#format_memory_path_with_title(path) ⇒ String

Format a memory path with its title

Normalizes the path (removes memory:// prefix if present) and formats it with the title in quotes if available.

Examples:

With title found

format_memory_path_with_title("concept/ruby/classes.md")
# => 'memory://concept/ruby/classes.md "Ruby Classes"'

With memory:// prefix

format_memory_path_with_title("memory://concept/ruby/classes.md")
# => 'memory://concept/ruby/classes.md "Ruby Classes"'

When title not found

format_memory_path_with_title("nonexistent.md")
# => 'memory://nonexistent.md'

Parameters:

  • Path to the memory entry (with or without memory:// prefix)

Returns:

  • Formatted string like 'memory://path "Title"' or 'memory://path'



53
54
55
56
57
58
59
60
61
62
# File 'lib/swarm_memory/tools/title_lookup.rb', line 53

def format_memory_path_with_title(path)
  normalized_path = path.sub(%r{^memory://}, "")
  title = lookup_title(normalized_path)

  if title
    "memory://#{normalized_path} \"#{title}\""
  else
    "memory://#{normalized_path}"
  end
end

#lookup_title(path) ⇒ String?

Look up the title of a memory entry

Examples:

title = lookup_title("concept/ruby/classes.md")
# => "Ruby Classes"

Parameters:

  • Path to the memory entry

Returns:

  • Title if found, nil otherwise



27
28
29
30
31
32
# File 'lib/swarm_memory/tools/title_lookup.rb', line 27

def lookup_title(path)
  entry = @storage.read_entry(file_path: path)
  entry.title
rescue StandardError
  nil
end