Module: Crimson::Tools::WriteFile
- Defined in:
- lib/crimson/tools/write_file.rb
Overview
Write content to a file, creating parent directories and showing a diff.
Constant Summary collapse
- TOOL_NAME =
"write_file"- PARAMS =
Tool parameter definitions.
{ path: { type: "string", description: "The path to the file to write" }, content: { type: "string", description: "The content to write" } }.freeze
- MUTATION_QUEUE =
FileMutationQueue.new
Class Method Summary collapse
-
.anthropic_definition ⇒ Hash
Anthropic-compatible tool definition.
-
.call(path:, content:) ⇒ String
Execute the tool.
-
.definition ⇒ Hash
OpenAI-compatible tool definition.
Class Method Details
.anthropic_definition ⇒ Hash
Returns Anthropic-compatible tool definition.
25 26 27 |
# File 'lib/crimson/tools/write_file.rb', line 25 def self.anthropic_definition Schema.build_anthropic(name: TOOL_NAME, description: "Write content to a file. Creates the file and parent directories if needed.", parameters: PARAMS, required: %w[path content]) end |
.call(path:, content:) ⇒ String
Execute the tool.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/crimson/tools/write_file.rb', line 33 def self.call(path:, content:) return "Error: No path provided" if path.nil? || path.strip.empty? = File.(path) MUTATION_QUEUE.with_file() do dir = File.dirname() old_content = File.exist?() ? File.read() : nil FileUtils.mkdir_p(dir) unless Dir.exist?(dir) File.write(, content) diff = DiffUtil.format_diff(old_content || "", content, path) "Successfully wrote to #{path}\n#{diff}" end rescue => e "Error writing file: #{e.}" end |
.definition ⇒ Hash
Returns OpenAI-compatible tool definition.
20 21 22 |
# File 'lib/crimson/tools/write_file.rb', line 20 def self.definition Schema.build(name: TOOL_NAME, description: "Write content to a file. Creates the file and parent directories if needed.", parameters: PARAMS, required: %w[path content]) end |