Class: Chocomint::Tools::WriteFile

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

Overview

許可ディレクトリ内にファイルを書き込む (DESIGN §8)。

Instance Method Summary collapse

Methods inherited from Base

#to_tool_definition

Constructor Details

#initialize(path_guard:, max_file_bytes:) ⇒ WriteFile

Returns a new instance of WriteFile.



9
10
11
12
# File 'lib/chocomint/tools/write_file.rb', line 9

def initialize(path_guard:, max_file_bytes:)
  @path_guard = path_guard
  @max_file_bytes = max_file_bytes
end

Instance Method Details

#call(args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chocomint/tools/write_file.rb', line 32

def call(args)
  measure do
    content = args["content"].to_s
    if content.bytesize > @max_file_bytes
      next fail(stderr: "content exceeds max_file_bytes (#{@max_file_bytes})")
    end

    abs = @path_guard.resolve(args["path"])
    FileUtils_mkdir(File.dirname(abs))
    bytes = File.write(abs, content)
    ok(stdout: "wrote #{bytes} bytes to #{args['path']}")
  end
end

#descriptionObject



16
17
18
# File 'lib/chocomint/tools/write_file.rb', line 16

def description
  "指定パスにテキストを書き込む。path は許可ディレクトリ内であること。"
end

#nameObject



14
# File 'lib/chocomint/tools/write_file.rb', line 14

def name = "write_file"

#schemaObject



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/chocomint/tools/write_file.rb', line 20

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