Class: Kreator::Tools::Write

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

Instance Method Summary collapse

Instance Method Details

#call(args:, context:, signal:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kreator/tools/write.rb', line 28

def call(args:, context:, signal:)
  path = context.ensure_path_allowed!(context.resolve_path(args.fetch("path")), action: :write)
  content = args.fetch("content")
  context.approve!(
    action: :write,
    target: path,
    details: { "bytes" => content.bytesize }
  )
  context.ensure_not_cancelled!(signal)

  FileMutationLocks.with(path) do
    existed = File.exist?(path)
    FileUtils.mkdir_p(File.dirname(path))
    context.ensure_not_cancelled!(signal)
    File.write(path, content)
    ToolResult.new(
      tool_call_id: "",
      name: name,
      content: "wrote #{content.bytesize} bytes to #{path}",
      metadata: { "path" => path, "created" => !existed, "bytes_written" => content.bytesize }
    )
  end
end

#descriptionObject



12
13
14
# File 'lib/kreator/tools/write.rb', line 12

def description
  "Write complete UTF-8 text contents to a file, creating parent directories."
end

#nameObject



8
9
10
# File 'lib/kreator/tools/write.rb', line 8

def name
  "write"
end

#schemaObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kreator/tools/write.rb', line 16

def schema
  {
    "type" => "object",
    "additionalProperties" => false,
    "required" => %w[path content],
    "properties" => {
      "path" => { "type" => "string", "minLength" => 1 },
      "content" => { "type" => "string" }
    }
  }
end