Class: Kward::Tools::EditFile

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

Overview

Tool wrapper for exact block replacement edits.

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#schema

Constructor Details

#initialize(workspace:) ⇒ EditFile

Builds the tool schema and stores the execution dependency.



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

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

Executes the tool and returns model-facing output text.



36
37
38
39
40
41
42
43
# File 'lib/kward/tools/edit_file.rb', line 36

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