Class: RubynCode::Tools::WriteFile

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

Constant Summary collapse

TOOL_NAME =
'write_file'
DESCRIPTION =
'Writes content to a file. Creates parent directories if needed.'
PARAMETERS =
{
  path: { type: :string, required: true,
          description: 'Path to the file to write (relative to project root or absolute)' },
  content: { type: :string, required: true, description: 'Content to write to the file' }
}.freeze
RISK_LEVEL =
:write
REQUIRES_CONFIRMATION =
false
PREVIEW_LINES =
15

Instance Attribute Summary

Attributes inherited from Base

#project_root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

description, #initialize, parameters, requires_confirmation?, risk_level, #safe_path, to_schema, tool_name, #truncate

Constructor Details

This class inherits a constructor from RubynCode::Tools::Base

Class Method Details

.summarize(output, _args) ⇒ Object

Take the first line of the tool’s output, which is already formatted as “Updated /path.rb (N bytes)” or “Created /path.rb (N bytes)”.



23
24
25
# File 'lib/rubyn_code/tools/write_file.rb', line 23

def self.summarize(output, _args)
  output.to_s.lines.first.to_s.chomp[0, 200]
end

Instance Method Details

#execute(path:, content:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/rubyn_code/tools/write_file.rb', line 27

def execute(path:, content:)
  resolved = safe_path(path)
  existed = File.exist?(resolved)
  old_content = existed ? File.read(resolved) : nil

  FileUtils.mkdir_p(File.dirname(resolved))
  bytes = File.write(resolved, content)

  format_result(path, bytes, existed, old_content, content)
end

#preview_content(path:, content:) ⇒ Hash

Compute the proposed file content without writing to disk. Used by IDE mode to preview the write in a diff view (modify) or preview tab (create) before the user accepts.

Returns:

  • (Hash)

    { content: String, type: ‘modify’ | ‘create’ }



43
44
45
46
47
# File 'lib/rubyn_code/tools/write_file.rb', line 43

def preview_content(path:, content:)
  resolved = safe_path(path)
  type = File.exist?(resolved) ? 'modify' : 'create'
  { content: content, type: type }
end