Module: Relay::Concerns::View

Included in:
Pages::Base, Router, Routes::Base
Defined in:
app/concerns/view.rb

Overview

Shared view-layer functionality for page and route renderers.

This concern exists to hold presentation-focused helpers that shape data for templates and fragments, such as status-bar labels and formatted cost/context-window values. Keeping these helpers here separates view concerns from session/context resolution.

Instance Method Summary collapse

Instance Method Details

#cancellable?(status) ⇒ Boolean

Returns true when the status represents an interruptible request.

Parameters:

  • status (String)

Returns:

  • (Boolean)

    Returns true when the status represents an interruptible request.



59
60
61
62
# File 'app/concerns/view.rb', line 59

def cancellable?(status)
  text = status.to_s
  text.start_with?("Thinking", "Running", "Compacting")
end

#context_window(ctx) ⇒ Hash

Returns the current context-window display payload.

Parameters:

Returns:

  • (Hash)

    Returns the current context-window display payload.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/concerns/view.rb', line 68

def context_window(ctx)
  if ctx.compacted?
    max = ctx.context_window || 0
    {used: 0, max:, label: "Context compacted"}
  else
    used = ctx.usage.total_tokens || 0
    max = ctx.context_window || 0
    {used:, max:, label: "#{used} / #{max} tokens"}
  end
rescue LLM::NoSuchModelError, LLM::NoSuchRegistryError
  {used: 0, max: 0, label: "0 / 0 tokens"}
end

#format_cost(cost) ⇒ String

Returns the formatted cost string.

Parameters:

  • cost (String)

Returns:

  • (String)

    Returns the formatted cost string.



85
86
87
88
89
90
# File 'app/concerns/view.rb', line 85

def format_cost(cost)
  return "unknown" if cost == "unknown"
  "$#{cost}"
rescue LLM::NoSuchModelError, LLM::NoSuchRegistryError
  "unknown"
end

#format_name(name) ⇒ String

Parameters:

  • (LLM::Provider)

Returns:

  • (String)


95
96
97
98
99
100
101
102
# File 'app/concerns/view.rb', line 95

def format_name(name)
  case name
  when :openai then "OpenAI"
  when :xai then "xAI"
  when :deepseek then "DeepSeek"
  else name.to_s.capitalize
  end
end

#initials(text) ⇒ String

Returns up to two initials for compact UI badges.

Parameters:

  • text (#to_s)

Returns:

  • (String)

    Returns up to two initials for compact UI badges.



30
31
32
33
34
# File 'app/concerns/view.rb', line 30

def initials(text)
  words = text.to_s.strip.split(/\s+/).reject(&:empty?)
  return "?" if words.empty?
  words.first(2).map { _1[0] }.join.upcase
end

#markdown(text) ⇒ String

Returns Renders markdown to HTML for templates and fragments.

Parameters:

  • text (String)

Returns:

  • (String)

    Renders markdown to HTML for templates and fragments.



40
41
42
# File 'app/concerns/view.rb', line 40

def markdown(text)
  Relay.markdown(text)
end

#status_bar(status: "Ready", ctx: self.ctx, context_window: nil, cost: nil) ⇒ Hash

Returns the status-bar payload for the current context.

Returns:

  • (Hash)

    Returns the status-bar payload for the current context.



47
48
49
50
51
52
53
# File 'app/concerns/view.rb', line 47

def status_bar(status: "Ready", ctx: self.ctx, context_window: nil, cost: nil)
  {
    status:,
    context_window: context_window || context_window(ctx),
    cost: cost || format_cost(ctx.cost)
  }
end

#themeString

Returns the active theme identifier.

Returns:

  • (String)

    Returns the active theme identifier.



15
16
17
# File 'app/concerns/view.rb', line 15

def theme
  Relay::THEME
end

#theme_hrefString

Returns the current theme stylesheet path.

Returns:

  • (String)

    Returns the current theme stylesheet path.



22
23
24
# File 'app/concerns/view.rb', line 22

def theme_href
  "/themes/#{theme}.css"
end