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
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
#height ⇒ Object
Returns the value of attribute height.
266 267 268 |
# File 'lib/ruby_rich/transcript.rb', line 266 def height @height end |
#store ⇒ Object (readonly)
Returns the value of attribute store.
267 268 269 |
# File 'lib/ruby_rich/transcript.rb', line 267 def store @store end |
#width ⇒ Object
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, **) add_block(:assistant, text, **) 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 = "", **) 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
334 335 336 |
# File 'lib/ruby_rich/transcript.rb', line 334 def add_markdown(text, **) add_block(:markdown, text, **) end |
#add_separator(label = nil, **options) ⇒ Object
330 331 332 |
# File 'lib/ruby_rich/transcript.rb', line 330 def add_separator(label = nil, **) add_block(:separator, label.to_s, **) 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, **) add_block(:thinking, text, status: status, collapsed: collapsed, **) 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, **) = (.delete(:metadata) || {}).merge(name: name) add_block(:tool, result.to_s, name: name, status: status, collapsed: collapsed, metadata: , **) end |
#add_user(text, **options) ⇒ Object
313 314 315 |
# File 'lib/ruby_rich/transcript.rb', line 313 def add_user(text, **) add_block(:user, text, **) 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 |
#blocks ⇒ Object
277 278 279 |
# File 'lib/ruby_rich/transcript.rb', line 277 def blocks @store.entries end |
#blur ⇒ Object
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 (id) @store.(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 |
#focus ⇒ Object
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 |
#render ⇒ Object
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, **) 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
388 389 390 |
# File 'lib/ruby_rich/transcript.rb', line 388 def toggle_entry(id) @store.toggle(id) end |