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.



269
270
271
272
273
274
275
# File 'lib/ruby_rich/transcript.rb', line 269

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.



266
267
268
# File 'lib/ruby_rich/transcript.rb', line 266

def height
  @height
end

#storeObject (readonly)

Returns the value of attribute store.



267
268
269
# File 'lib/ruby_rich/transcript.rb', line 267

def store
  @store
end

#widthObject

Returns the value of attribute width.



266
267
268
# File 'lib/ruby_rich/transcript.rb', line 266

def width
  @width
end

Instance Method Details

#add_assistant(text, **options) ⇒ Object



317
318
319
# File 'lib/ruby_rich/transcript.rb', line 317

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

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



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/ruby_rich/transcript.rb', line 338

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



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

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

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



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

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

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



321
322
323
# File 'lib/ruby_rich/transcript.rb', line 321

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



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

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



313
314
315
# File 'lib/ruby_rich/transcript.rb', line 313

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

#append_block(id, delta) ⇒ Object



355
356
357
# File 'lib/ruby_rich/transcript.rb', line 355

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

#attach(layout, priority: 150) ⇒ Object



291
292
293
294
295
296
297
298
299
300
# File 'lib/ruby_rich/transcript.rb', line 291

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



277
278
279
# File 'lib/ruby_rich/transcript.rb', line 277

def blocks
  @store.entries
end

#blurObject



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

def blur
  @focused = false
  self
end

#collapse_entry(id) ⇒ Object



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

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

#expand_entry(id) ⇒ Object



380
381
382
# File 'lib/ruby_rich/transcript.rb', line 380

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

#find_block(id) ⇒ Object



376
377
378
# File 'lib/ruby_rich/transcript.rb', line 376

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

#focusObject



281
282
283
284
# File 'lib/ruby_rich/transcript.rb', line 281

def focus
  @focused = true
  self
end

#handle_event(event_data) ⇒ Object



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

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



370
371
372
373
374
# File 'lib/ruby_rich/transcript.rb', line 370

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

#renderObject



392
393
394
395
396
397
398
# File 'lib/ruby_rich/transcript.rb', line 392

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



359
360
361
362
363
364
365
366
367
368
# File 'lib/ruby_rich/transcript.rb', line 359

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



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

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