Class: Chocomint::Tools::AppendFile

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

Overview

許可ディレクトリ内のファイルに追記する。ファイルが無ければ新規作成。

Instance Method Summary collapse

Methods inherited from Base

#to_tool_definition

Constructor Details

#initialize(path_guard:, max_file_bytes:) ⇒ AppendFile

Returns a new instance of AppendFile.



9
10
11
12
# File 'lib/chocomint/tools/append_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
45
46
47
# File 'lib/chocomint/tools/append_file.rb', line 32

def call(args)
  measure do
    content = args["content"].to_s
    abs = @path_guard.resolve(args["path"])

    current = File.exist?(abs) ? File.size(abs) : 0
    if current + content.bytesize > @max_file_bytes
      next fail(stderr: "resulting file exceeds max_file_bytes (#{@max_file_bytes})")
    end

    require "fileutils"
    FileUtils.mkdir_p(File.dirname(abs))
    File.open(abs, "a") { |f| f.write(content) }
    ok(stdout: "appended #{content.bytesize} bytes to #{args['path']}")
  end
end

#descriptionObject



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

def description
  "指定パスの末尾にテキストを追記する。無ければ新規作成。path は許可ディレクトリ内であること。"
end

#nameObject



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

def name = "append_file"

#schemaObject



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

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