Class: RubyRich::Transcript

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_rich/transcript.rb

Defined Under Namespace

Classes: Entry, Store

Constant Summary collapse

ENTRY_TYPES =
[
  :user,
  :assistant,
  :thinking,
  :tool,
  :tool_result,
  :system,
  :error,
  :markdown,
  :diff,
  :separator,
  :progress
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store: Store.new) ⇒ Transcript

Returns a new instance of Transcript.



277
278
279
280
281
282
283
# File 'lib/ruby_rich/transcript.rb', line 277

def initialize(store: Store.new)
  @store = store
  @width = 0
  @height = 0
  @selected_collapsible_id = nil
  @focused = true
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



274
275
276
# File 'lib/ruby_rich/transcript.rb', line 274

def height
  @height
end

#storeObject (readonly)

Returns the value of attribute store.



275
276
277
# File 'lib/ruby_rich/transcript.rb', line 275

def store
  @store
end

#widthObject

Returns the value of attribute width.



274
275
276
# File 'lib/ruby_rich/transcript.rb', line 274

def width
  @width
end

Instance Method Details

#add_assistant(text, **options) ⇒ Object



329
330
331
# File 'lib/ruby_rich/transcript.rb', line 329

def add_assistant(text, **options)
  add_block(:assistant, text, **options)
end

#add_block(type, text = "", **options) ⇒ Object



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/ruby_rich/transcript.rb', line 350

def add_block(type, text = "", **options)
  id = options.delete(:id)
  status = options.delete(:status)
  collapsed = options.delete(:collapsed)
  name = options.delete(:name)
   = options.delete(:metadata) || options
  @store.add(
    type: type,
    content: text,
    id: id,
    status: status,
    collapsed: collapsed,
    metadata: ,
    name: name
  ).id
end

#add_markdown(text, **options) ⇒ Object



346
347
348
# File 'lib/ruby_rich/transcript.rb', line 346

def add_markdown(text, **options)
  add_block(:markdown, text, **options)
end

#add_separator(label = nil, **options) ⇒ Object



342
343
344
# File 'lib/ruby_rich/transcript.rb', line 342

def add_separator(label = nil, **options)
  add_block(:separator, label.to_s, **options)
end

#add_thinking(text, status: "idle", collapsed: true, **options) ⇒ Object



333
334
335
# File 'lib/ruby_rich/transcript.rb', line 333

def add_thinking(text, status: "idle", collapsed: true, **options)
  add_block(:thinking, text, status: status, collapsed: collapsed, **options)
end

#add_tool(name, status: :running, result: nil, collapsed: false, **options) ⇒ Object



337
338
339
340
# File 'lib/ruby_rich/transcript.rb', line 337

def add_tool(name, status: :running, result: nil, collapsed: false, **options)
   = (options.delete(:metadata) || {}).merge(name: name)
  add_block(:tool, result.to_s, name: name, status: status, collapsed: collapsed, metadata: , **options)
end

#add_user(text, **options) ⇒ Object



325
326
327
# File 'lib/ruby_rich/transcript.rb', line 325

def add_user(text, **options)
  add_block(:user, text, **options)
end

#append_block(id, delta) ⇒ Object



367
368
369
# File 'lib/ruby_rich/transcript.rb', line 367

def append_block(id, delta)
  @store.append(id, delta)
end

#attach(layout, priority: 150) ⇒ Object



303
304
305
306
307
308
309
310
311
312
# File 'lib/ruby_rich/transcript.rb', line 303

def attach(layout, priority: 150)
  [:ctrl_o, :alt_v].each do |event_name|
    layout.key(event_name, priority) do |event_data, _live|
      handle_event(event_data)
      false
    end
  end

  self
end

#blocksObject



285
286
287
# File 'lib/ruby_rich/transcript.rb', line 285

def blocks
  @store.entries
end

#blurObject



298
299
300
301
# File 'lib/ruby_rich/transcript.rb', line 298

def blur
  @focused = false
  self
end

#collapse_entry(id) ⇒ Object



396
397
398
# File 'lib/ruby_rich/transcript.rb', line 396

def collapse_entry(id)
  @store.collapse(id)
end

#expand_entry(id) ⇒ Object



392
393
394
# File 'lib/ruby_rich/transcript.rb', line 392

def expand_entry(id)
  @store.expand(id)
end

#find_block(id) ⇒ Object



388
389
390
# File 'lib/ruby_rich/transcript.rb', line 388

def find_block(id)
  @store.find(id)
end

#focusObject



293
294
295
296
# File 'lib/ruby_rich/transcript.rb', line 293

def focus
  @focused = true
  self
end

#handle_event(event_data) ⇒ Object



314
315
316
317
318
319
320
321
322
323
# File 'lib/ruby_rich/transcript.rb', line 314

def handle_event(event_data)
  return false unless @focused

  case event_data[:name]
  when :ctrl_o
    toggle_next_collapsible
  when :alt_v
    toggle_next(:tool)
  end
end

#remove_block(id) ⇒ Object



382
383
384
385
386
# File 'lib/ruby_rich/transcript.rb', line 382

def remove_block(id)
  removed = @store.remove(id)
  @selected_collapsible_id = nil if removed && @selected_collapsible_id == id
  removed
end

#renderObject



404
405
406
407
408
409
410
# File 'lib/ruby_rich/transcript.rb', line 404

def render
  lines = []
  @store.entries.each_with_index do |entry, index|
    lines.concat(render_entry(entry, index))
  end
  lines
end

#replace_block(id, text, **options) ⇒ Object



371
372
373
374
375
376
377
378
379
380
# File 'lib/ruby_rich/transcript.rb', line 371

def replace_block(id, text, **options)
  return false unless @store.replace(id, text)
  return true if options.empty?

  @store.update(id) do |entry|
    options.each do |key, value|
      entry[key] = value
    end
  end
end

#toggle_entry(id) ⇒ Object



400
401
402
# File 'lib/ruby_rich/transcript.rb', line 400

def toggle_entry(id)
  @store.toggle(id)
end

#versionObject



289
290
291
# File 'lib/ruby_rich/transcript.rb', line 289

def version
  @store.version
end