Class: RubyRich::Transcript
- Inherits:
-
Object
- Object
- RubyRich::Transcript
- Defined in:
- lib/ruby_rich/transcript.rb
Defined Under Namespace
Constant Summary collapse
- ENTRY_TYPES =
[ :user, :assistant, :thinking, :tool, :tool_result, :system, :error, :markdown, :diff, :separator, :progress ].freeze
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#store ⇒ Object
readonly
Returns the value of attribute store.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #add_assistant(text, **options) ⇒ Object
- #add_block(type, text = "", **options) ⇒ Object
- #add_markdown(text, **options) ⇒ Object
- #add_separator(label = nil, **options) ⇒ Object
- #add_thinking(text, status: "idle", collapsed: true, **options) ⇒ Object
- #add_tool(name, status: :running, result: nil, collapsed: false, **options) ⇒ Object
- #add_user(text, **options) ⇒ Object
- #append_block(id, delta) ⇒ Object
- #attach(layout, priority: 150) ⇒ Object
- #blocks ⇒ Object
- #blur ⇒ Object
- #collapse_entry(id) ⇒ Object
- #expand_entry(id) ⇒ Object
- #find_block(id) ⇒ Object
- #focus ⇒ Object
- #handle_event(event_data) ⇒ Object
-
#initialize(store: Store.new) ⇒ Transcript
constructor
A new instance of Transcript.
- #remove_block(id) ⇒ Object
- #render ⇒ Object
- #replace_block(id, text, **options) ⇒ Object
- #toggle_entry(id) ⇒ Object
- #version ⇒ Object
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
#height ⇒ Object
Returns the value of attribute height.
274 275 276 |
# File 'lib/ruby_rich/transcript.rb', line 274 def height @height end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
275 276 277 |
# File 'lib/ruby_rich/transcript.rb', line 275 def store @store end |
#width ⇒ Object
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, **) add_block(:assistant, text, **) 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 = "", **) id = .delete(:id) status = .delete(:status) collapsed = .delete(:collapsed) name = .delete(:name) = .delete(:metadata) || @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, **) add_block(:markdown, text, **) end |
#add_separator(label = nil, **options) ⇒ Object
342 343 344 |
# File 'lib/ruby_rich/transcript.rb', line 342 def add_separator(label = nil, **) add_block(:separator, label.to_s, **) 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, **) add_block(:thinking, text, status: status, collapsed: collapsed, **) 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, **) = (.delete(:metadata) || {}).merge(name: name) add_block(:tool, result.to_s, name: name, status: status, collapsed: collapsed, metadata: , **) end |
#add_user(text, **options) ⇒ Object
325 326 327 |
# File 'lib/ruby_rich/transcript.rb', line 325 def add_user(text, **) add_block(:user, text, **) 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 |
#blocks ⇒ Object
285 286 287 |
# File 'lib/ruby_rich/transcript.rb', line 285 def blocks @store.entries end |
#blur ⇒ Object
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 (id) @store.(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 |
#focus ⇒ Object
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 |
#render ⇒ Object
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, **) return false unless @store.replace(id, text) return true if .empty? @store.update(id) do |entry| .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 |
#version ⇒ Object
289 290 291 |
# File 'lib/ruby_rich/transcript.rb', line 289 def version @store.version end |