Class: Tools::Write

Inherits:
Base
  • Object
show all
Defined in:
lib/tools/write.rb

Overview

Creates or overwrites files with automatic intermediate directory creation. Writes content exactly as given — no line ending normalization, no BOM handling. Full replacement only; no append or merge.

Examples:

Creating a new file

tool.execute("path" => "config/new.yml", "content" => "key: value\n")
# => "Wrote 11 bytes to /home/user/project/config/new.yml"

Overwriting an existing file

tool.execute("path" => "README.md", "content" => "# Title\n")
# => "Wrote 9 bytes to /home/user/project/README.md"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

schema, truncation_threshold

Constructor Details

#initialize(shell_session: nil) ⇒ Write

Returns a new instance of Write.

Parameters:

  • shell_session (ShellSession, nil) (defaults to: nil)

    provides working directory for resolving relative paths



34
35
36
# File 'lib/tools/write.rb', line 34

def initialize(shell_session: nil, **)
  @working_directory = shell_session&.pwd
end

Class Method Details

.descriptionObject



20
# File 'lib/tools/write.rb', line 20

def self.description = "Write file."

.input_schemaObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/tools/write.rb', line 22

def self.input_schema
  {
    type: "object",
    properties: {
      path: {type: "string", description: "Relative paths resolve against working directory. Creates intermediate directories."},
      content: {type: "string"}
    },
    required: %w[path content]
  }
end

.tool_nameObject



18
# File 'lib/tools/write.rb', line 18

def self.tool_name = "write_file"

Instance Method Details

#execute(input) ⇒ String, Hash

Parameters:

  • input (Hash<String, Object>)

    string-keyed hash from the Anthropic API

Returns:

  • (String)

    confirmation with bytes written and resolved path

  • (Hash)

    with :error key on failure



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/tools/write.rb', line 41

def execute(input)
  path, content = extract_params(input)
  return {error: "Path cannot be blank"} if path.empty?

  path = resolve_path(path)

  error = validate_target(path)
  return error if error

  write_file(path, content)
end