summaryrefslogtreecommitdiffhomepage
path: root/lib/dispatch/tool/files/create_file.rb
blob: 5cc13e4d0a6658fb0bd7e1014d3be3a22a58743b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

module Dispatch
  module Tool
    module Files
      CREATE_FILE = Dispatch::Tools::Definition.new(
        name: "create_file",
        description: "Create a new file with the given content. Fails if the file already exists.",
        parameters: {
          type: "object",
          properties: {
            path: {
              type: "string",
              description: "Path to the file to create, relative to the worktree root."
            },
            content: {
              type: "string",
              description: "The content to write to the new file."
            }
          },
          required: %w[path content],
          additionalProperties: false
        }
      ) do |params, context|
        worktree_path = context[:worktree_path]
        path = params[:path]
        content = params[:content]

        resolved = begin
          Sandbox.resolve_path(path, worktree_path:)
        rescue SandboxError => e
          next Dispatch::Tools::Result.failure(error: e.message)
        end

        if File.exist?(resolved)
          next Dispatch::Tools::Result.failure(error: "File already exists: #{path}. Use write_file to overwrite.")
        end

        FileUtils.mkdir_p(File.dirname(resolved))
        File.write(resolved, content)

        Dispatch::Tools::Result.success(output: "Created #{path}")
      end
    end
  end
end