Class: Kward::SessionTreeNodes
- Inherits:
-
Object
- Object
- Kward::SessionTreeNodes
- Defined in:
- lib/kward/session_tree_nodes.rb
Overview
Shared traversal helpers for persisted session trees.
Frontends own their final labels or payloads. This class owns only the frontend-neutral mechanics needed by both terminal and RPC tree views: active-path lookup, hidden tool-call-only assistant nodes, visible-node flattening, and assistant tool-call lookup by id.
Class Method Summary collapse
- .tree_prefix(display_indent, gutters, show_connector, is_last, foldable) ⇒ Object
- .truncate_text(text) ⇒ Object
Instance Method Summary collapse
- #active_path ⇒ Object
-
#initialize(roots:, current_leaf: nil) ⇒ SessionTreeNodes
constructor
A new instance of SessionTreeNodes.
- #layout_rows ⇒ Object
- #tool_calls ⇒ Object
- #visible_roots ⇒ Object
Constructor Details
#initialize(roots:, current_leaf: nil) ⇒ SessionTreeNodes
Returns a new instance of SessionTreeNodes.
14 15 16 17 |
# File 'lib/kward/session_tree_nodes.rb', line 14 def initialize(roots:, current_leaf: nil) @roots = roots @current_leaf = current_leaf end |
Class Method Details
.tree_prefix(display_indent, gutters, show_connector, is_last, foldable) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/kward/session_tree_nodes.rb', line 103 def self.tree_prefix(display_indent, gutters, show_connector, is_last, foldable) return "" if display_indent.to_i <= 0 connector_position = show_connector ? display_indent - 1 : -1 (0...(display_indent * 3)).map do |index| level = index / 3 position = index % 3 gutter = gutters.find { |candidate| candidate[:position] == level } if gutter position.zero? && gutter[:show] ? "│" : " " elsif show_connector && level == connector_position if position.zero? is_last ? "└" : "├" elsif position == 1 foldable ? "⊟" : "─" else " " end else " " end end.join end |
.truncate_text(text) ⇒ Object
128 129 130 |
# File 'lib/kward/session_tree_nodes.rb', line 128 def self.truncate_text(text) MessageText.truncate_preview(text) end |
Instance Method Details
#active_path ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/kward/session_tree_nodes.rb', line 19 def active_path by_id = entries_by_id ids = [] entry = by_id[@current_leaf.to_s] seen = {} while entry && !seen[entry["id"].to_s] seen[entry["id"].to_s] = true ids << entry["id"].to_s entry = by_id[entry["parentId"].to_s] end ids end |
#layout_rows ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/kward/session_tree_nodes.rb', line 55 def layout_rows active = active_path roots = visible_roots active_nodes = active_node_lookup(roots, active) multiple_roots = roots.length > 1 result = [] stack = roots.sort_by { |root| active_nodes[root.object_id] ? 0 : 1 }.each_with_index.map do |root, index| [root, multiple_roots ? 1 : 0, multiple_roots, multiple_roots, index == roots.length - 1, [], multiple_roots] end.reverse until stack.empty? node, indent, just_branched, show_connector, is_last, gutters, virtual_root_child = stack.pop entry = node[:source]["entry"] || {} entry_id = entry["id"].to_s display_indent = multiple_roots ? [indent - 1, 0].max : indent show_node_connector = show_connector && !virtual_root_child result << { source: node[:source], entry: entry, depth: display_indent, is_last: is_last, gutters: gutters, active_path: active.include?(entry_id), prefix: self.class.tree_prefix(display_indent, gutters, show_node_connector, is_last, !node[:children].empty?) } children = node[:children].sort_by { |child| active_nodes[child.object_id] ? 0 : 1 } multiple_children = children.length > 1 child_indent = if multiple_children indent + 1 elsif just_branched && indent.positive? indent + 1 else indent end connector_position = [display_indent - 1, 0].max child_gutters = show_node_connector ? gutters + [{ position: connector_position, show: !is_last }] : gutters children.each_with_index.reverse_each do |child, index| stack << [child, child_indent, multiple_children, multiple_children, index == children.length - 1, child_gutters, false] end end result end |
#tool_calls ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/kward/session_tree_nodes.rb', line 36 def tool_calls @roots.each_with_object({}) do |root, calls| stack = [root] seen = {} until stack.empty? node = stack.pop next if seen[node.object_id] seen[node.object_id] = true entry = node["entry"] || {} = entry["message"] if entry["type"] == "message" && .is_a?(Hash) && MessageAccess.role() == "assistant" MessageAccess.tool_calls().each { |tool_call| calls[ToolCall.id(tool_call).to_s] = tool_call } end stack.concat(Array(node["children"])) end end end |