Skip to content
Kward Search API index

Class: Kward::EditorPromptSession

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/cli/editor_prompt_session.rb

Overview

Transactional draft used while an agent works on an active editor buffer.

The live terminal editor remains owned by PromptInterface. Agent tool calls update this draft and the caller commits it only after a successful turn.

Defined Under Namespace

Classes: InvalidEdit

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ EditorPromptSession

Returns a new instance of EditorPromptSession.



12
13
14
15
16
17
18
# File 'lib/kward/cli/editor_prompt_session.rb', line 12

def initialize(context)
  @context = context.dup
  @initial_content = @context.fetch(:content).to_s.dup.freeze
  @content = @initial_content
  @targets = edit_targets
  @draft_proposed = false
end

Instance Attribute Details

#content ⇒ Object (readonly)

Returns the value of attribute content.



10
11
12
# File 'lib/kward/cli/editor_prompt_session.rb', line 10

def content
  @content
end

#context ⇒ Object (readonly)

Returns the value of attribute context.



10
11
12
# File 'lib/kward/cli/editor_prompt_session.rb', line 10

def context
  @context
end

Instance Method Details

#changed? ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/kward/cli/editor_prompt_session.rb', line 61

def changed?
  @content != @initial_content
end

#edit(edits) ⇒ Object

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kward/cli/editor_prompt_session.rb', line 29

def edit(edits)
  ensure_new_draft!
  raise InvalidEdit, "provide at least one edit" unless edits.is_a?(Array) && !edits.empty?

  seen = {}
  replacements = edits.map do |entry|
    raise InvalidEdit, "each edit needs a target and replacement text" unless entry.is_a?(Hash)

    target = entry[:target] || entry["target"]
    text = entry[:content] || entry["content"]
    range = @targets[target]
    raise InvalidEdit, "target is unavailable in this snapshot" unless range
    raise InvalidEdit, "duplicate target" if seen[target]
    raise InvalidEdit, "content must be a string" unless text.is_a?(String)

    seen[target] = true
    [*range, text]
  end

  cursor = 0
  length = @initial_content.length
  draft = String.new(encoding: @initial_content.encoding)
  replacements.sort_by { |first, last, _| [first, last] }.each do |first, last, text|
    raise InvalidEdit, "targets overlap or are out of bounds" unless cursor <= first && first <= last && last <= length

    draft << @initial_content[cursor...first] << text
    cursor = last
  end
  @content = (draft << @initial_content[cursor..]).freeze
  @draft_proposed = true
end

#initial_content ⇒ Object



65
66
67
# File 'lib/kward/cli/editor_prompt_session.rb', line 65

def initial_content
  @initial_content
end

#replace(content) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
# File 'lib/kward/cli/editor_prompt_session.rb', line 20

def replace(content)
  raise InvalidEdit, "clear the selection before replacing the whole buffer" if Array(@context[:selections]).any?
  ensure_new_draft!
  raise InvalidEdit, "content must be a string" unless content.is_a?(String)

  @content = content.dup.freeze
  @draft_proposed = true
end