Class: SkillBench::Tools::WriteFile

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

Overview

Handles writing content to a file within the working directory.

Class Method Summary collapse

Class Method Details

.call(path, content, working_dir_path) ⇒ String

Writes content to a file. Creates missing parent directories.

Parameters:

  • path (String)

    The relative path to the file.

  • content (String)

    The content to write.

  • working_dir_path (Pathname)

    The working directory to resolve the path against.

Returns:

  • (String)

    A success message.



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/skill_bench/tools/write_file.rb', line 37

def self.call(path, content, working_dir_path)
  validate_write_path!(path)

  target = secure_path(path, working_dir_path)
  target.dirname.mkpath
  # Re-verify path after mkpath to mitigate TOCTOU vulnerabilities
  target = secure_path(path, working_dir_path)

  File.open(target, File::WRONLY | File::CREAT | File::TRUNC, 0o644) do |f|
    f.write(content)
  end
  "Successfully wrote to #{path}"
end

.definitionHash

Returns The tool definition for the LLM API.

Returns:

  • (Hash)

    The tool definition for the LLM API.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/skill_bench/tools/write_file.rb', line 12

def self.definition
  {
    type: 'function',
    function: {
      name: 'write_file',
      description: 'Write content to a file. Overwrites the file if it exists.',
      parameters: {
        type: 'object',
        properties: {
          path: { type: 'string', description: 'Relative path to the file to write.' },
          content: { type: 'string', description: 'The content to write into the file.' }
        },
        required: %w[path content],
        additionalProperties: false
      }
    }
  }
end