Skip to content
Kward Search API index

Class: Kward::RPC::SessionTreeRows

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/rpc/session_tree_rows.rb

Overview

Builds frontend-neutral RPC row payloads from a persisted session tree.

SessionManager owns session lifecycle and decides when a tree is needed; this class owns only the mechanics of flattening tree nodes into the row fields sent over JSON-RPC. Keeping row presentation here prevents the RPC session coordinator from accumulating rendering details while preserving the exact RPC payload shape.

Instance Method Summary collapse

Constructor Details

#initialize(roots:, current_leaf:, selectable:) ⇒ SessionTreeRows

Returns a new instance of SessionTreeRows.

Parameters:

  • roots (Array<Hash>)

    tree roots returned by SessionStore#session_tree

  • current_leaf (String, nil)

    active persisted tree leaf id

  • selectable (#call)

    predicate receiving an entry hash



21
22
23
24
25
# File 'lib/kward/rpc/session_tree_rows.rb', line 21

def initialize(roots:, current_leaf:, selectable:)
  @roots = roots
  @current_leaf = current_leaf
  @selectable = selectable
end

Instance Method Details

#rowsArray<Hash>

Returns flattened RPC row hashes in active-path-first display order.

Returns:

  • (Array<Hash>)

    rows for the session/tree RPC method



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/kward/rpc/session_tree_rows.rb', line 30

def rows
  tree_nodes = SessionTreeNodes.new(roots: @roots, current_leaf: @current_leaf)
  tool_calls_by_id = tree_nodes.tool_calls

  tree_nodes.layout_rows.map do |row|
    entry = row[:entry]
    entry_id = entry["id"].to_s
    formatted = tree_entry_display(entry, tool_calls_by_id)
    {
      entryId: entry_id,
      parentId: entry["parentId"],
      role: formatted[:role],
      text: formatted[:text],
      current: !@current_leaf.to_s.empty? && entry_id == @current_leaf.to_s,
      depth: row[:depth],
      isLast: row[:is_last],
      ancestorContinues: row[:gutters].map { |gutter| gutter[:show] },
      activePath: row[:active_path],
      selectable: @selectable.call(entry),
      label: row[:source]["label"] || entry["resolvedLabel"],
      labelTimestamp: row[:source]["labelTimestamp"],
      prefix: row[:prefix]
    }.compact
  end
end