Class: Rubino::Tools::WriteTool

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

Overview

Writes content to a file, creating parent directories if needed. Overwrites existing files (the LLM is expected to Read first when in doubt). Kept intentionally narrow — no append mode, no partial writes; those belong in ‘edit` / `multi_edit`.

Instance Attribute Summary

Attributes inherited from Base

#cancel_token, #read_tracker, #stream_chunk

Instance Method Summary collapse

Methods inherited from Base

#cancellation_requested?, #config_key, #emit_chunk, #risky?, #to_tool_definition, workspace_root, workspace_roots

Instance Method Details

#call(arguments) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubino/tools/write_tool.rb', line 37

def call(arguments)
  file_path = arguments["file_path"] || arguments[:file_path]
  content   = arguments["content"]   || arguments[:content] || ""

  return "Error: file_path is required" if file_path.nil? || file_path.to_s.empty?

  expanded = File.expand_path(file_path)
  return workspace_violation_message(file_path) unless within_workspace?(expanded)

  FileUtils.mkdir_p(File.dirname(expanded))

  existed = File.exist?(expanded)
  File.write(expanded, content)

  verb  = existed ? "overwrote" : "created"
  bytes = content.to_s.bytesize
  lines = content.to_s.lines.size
  { output: "#{verb} #{file_path} (#{bytes} bytes)",
    metrics: "#{lines} line#{"s" if lines != 1} · #{bytes}B" }
rescue StandardError => e
  "Error writing #{file_path}: #{e.message}"
end

#descriptionObject



16
17
18
19
20
# File 'lib/rubino/tools/write_tool.rb', line 16

def description
  "Write content to a file, overwriting any existing content. " \
    "Creates parent directories if they do not exist. " \
    "Use `edit` or `multi_edit` to modify an existing file in place."
end

#input_schemaObject



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

def input_schema
  {
    type: "object",
    properties: {
      file_path: { type: "string", description: "Absolute or relative file path" },
      content: { type: "string", description: "Full file content to write" }
    },
    required: %w[file_path content]
  }
end

#nameObject



12
13
14
# File 'lib/rubino/tools/write_tool.rb', line 12

def name
  "write"
end

#risk_levelObject



33
34
35
# File 'lib/rubino/tools/write_tool.rb', line 33

def risk_level
  :medium
end