Skip to content
Kward Search API index

Class: Kward::Tools::EditEditorBuffer

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

Overview

Proposes edits against the cursor or selected spans of one editor snapshot.

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#schema

Constructor Details

#initialize(editor_prompt_session:) ⇒ EditEditorBuffer

Returns a new instance of EditEditorBuffer.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kward/tools/edit_editor_buffer.rb', line 7

def initialize(editor_prompt_session:)
  @session = editor_prompt_session
  super(
    "edit_editor_buffer",
    "Propose targeted in-memory edits in one call. With a selection, target selection:1, selection:2, etc. Otherwise target cursor to insert or current_line to replace its text without consuming the line ending.",
    properties: {
      edits: {
        type: "array",
        items: {
          type: "object", additionalProperties: false, required: %w[target content],
          properties: { target: { type: "string" }, content: { type: "string", description: "Replacement text, or an empty string to delete the target." } }
        }
      }
    },
    required: ["edits"]
  )
end

Instance Method Details

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



25
26
27
28
29
30
31
# File 'lib/kward/tools/edit_editor_buffer.rb', line 25

def call(args, _conversation, cancellation: nil)
  cancellation&.raise_if_cancelled!
  @session.edit(argument(args, :edits))
  "Editor draft updated. The file has not been saved."
rescue EditorPromptSession::InvalidEdit => e
  "Editor edit rejected: #{e.message}"
end