Class: LLM::Tool::SwapText

Inherits:
LLM::Tool show all
Defined in:
lib/llm/tools/swap_text.rb

Overview

The LLM::Tool::SwapText class implements a tool that can substitute one piece of text for another piece of text in a given file.

Instance Method Summary collapse

Methods inherited from LLM::Tool

a2a, a2a?, #a2a?, clear_registry!, description, find_by_name!, function, #function, inherited, mcp, mcp?, #mcp?, name, #on_cancel, #on_interrupt, params, register, registry, skill?, #skill?, unregister

Methods included from Param

#param, #required

Methods included from Function::Registry

#clear_registry!, extended, #find_by_name, #lock, #register, #registry, #registry_key, #tool_name, #unregister

Instance Method Details

#call(path:, before:, after:, expected_count: 1) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/llm/tools/swap_text.rb', line 17

def call(path:, before:, after:, expected_count: 1)
  content = File.read(path)
  count = content.scan(before).length
  raise "expected #{expected_count} match(es), found #{count}" unless count == expected_count.to_i
  File.write(path, content.sub(before, after))
  {ok: true, replaced: count}
end