Class: Kward::Tools::EditFile

Inherits:
Base
  • Object
show all
Defined in:
lib/kward/tools/edit_file.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#schema

Constructor Details

#initialize(workspace:) ⇒ EditFile

Returns a new instance of EditFile.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kward/tools/edit_file.rb', line 6

def initialize(workspace:)
  @workspace = workspace
  super(
    "edit_file",
    "Edit a read workspace file by exact replacements. Each old_text must match once; edits must not overlap.",
    properties: {
      path: { type: "string", description: "Workspace-relative path." },
      edits: {
        type: "array",
        description: "Non-overlapping replacements against original content.",
        items: {
          type: "object",
          properties: {
            old_text: { type: "string", description: "Unique exact text to replace." },
            new_text: { type: "string", description: "Replacement text." }
          },
          required: ["old_text", "new_text"],
          additionalProperties: false
        }
      }
    },
    required: ["path", "edits"]
  )
end

Instance Method Details

#call(args, conversation, cancellation: nil) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/kward/tools/edit_file.rb', line 31

def call(args, conversation, cancellation: nil)
  path = argument(args, :path, "")
  edits = argument(args, :edits, [])

  result = @workspace.edit_file(path, edits, read_paths: conversation.read_paths)
  conversation.refresh_system_message! if agents_file_changed?(@workspace, path, result)
  result
end