Skip to content
Kward Search API index

Class: Kward::PromptInterface::StreamState

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/prompt_interface/stream_state.rb

Overview

State object for streamed assistant output blocks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStreamState

Returns a new instance of StreamState.



9
10
11
# File 'lib/kward/prompt_interface/stream_state.rb', line 9

def initialize
  reset
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/kward/prompt_interface/stream_state.rb', line 7

def block
  @block
end

#colObject (readonly)

Returns the value of attribute col.



7
8
9
# File 'lib/kward/prompt_interface/stream_state.rb', line 7

def col
  @col
end

Instance Method Details

#clear_pending_wrapObject



49
50
51
52
# File 'lib/kward/prompt_interface/stream_state.rb', line 49

def clear_pending_wrap
  @col = 0
  @pending_wrap = false
end

#finish_blockObject



30
31
32
# File 'lib/kward/prompt_interface/stream_state.rb', line 30

def finish_block
  @block = nil
end

#initialize_copy(source) ⇒ Object



13
14
15
16
17
18
# File 'lib/kward/prompt_interface/stream_state.rb', line 13

def initialize_copy(source)
  super
  @block = source.block&.dup
  @col = source.col
  @pending_wrap = source.pending_wrap?
end

#pending_wrap?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/kward/prompt_interface/stream_state.rb', line 34

def pending_wrap?
  @pending_wrap
end

#resetObject



20
21
22
23
24
# File 'lib/kward/prompt_interface/stream_state.rb', line 20

def reset
  @block = nil
  @col = 0
  @pending_wrap = false
end

#reset_position_from_rows(rows, width) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/kward/prompt_interface/stream_state.rb', line 38

def reset_position_from_rows(rows, width)
  last_length = rows.empty? ? 0 : ANSI.strip(rows.last).length
  if last_length >= width
    @col = 0
    @pending_wrap = true
  else
    @col = last_length
    @pending_wrap = false
  end
end

#start_block(label) ⇒ Object



26
27
28
# File 'lib/kward/prompt_interface/stream_state.rb', line 26

def start_block(label)
  @block = label
end

#update_position(text, width:) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kward/prompt_interface/stream_state.rb', line 54

def update_position(text, width:)
  ANSI.strip(text).each_char do |char|
    case char
    when "\n", "\r"
      @col = 0
      @pending_wrap = false
    else
      @pending_wrap = false
      @col += 1
      if @col >= width
        @col = 0
        @pending_wrap = true
      end
    end
  end
end