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
-
#cancellable?(status) ⇒ Boolean
Returns true when the status represents an interruptible request.
-
#context_window(ctx) ⇒ Hash
Returns the current context-window display payload.
-
#format_cost(cost) ⇒ String
Returns the formatted cost string.
- #format_name(name) ⇒ String
-
#initials(text) ⇒ String
Returns up to two initials for compact UI badges.
-
#markdown(text) ⇒ String
Renders markdown to HTML for templates and fragments.
-
#status_bar(status: "Ready", ctx: self.ctx, context_window: nil, cost: nil) ⇒ Hash
Returns the status-bar payload for the current context.
Instance Method Details
#cancellable?(status) ⇒ Boolean
Returns true when the status represents an interruptible request.
45 46 47 48 |
# File 'app/concerns/view.rb', line 45 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.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/concerns/view.rb', line 54 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.
71 72 73 74 75 76 |
# File 'app/concerns/view.rb', line 71 def format_cost(cost) return "unknown" if cost == "unknown" "$#{cost}" rescue LLM::NoSuchModelError, LLM::NoSuchRegistryError "unknown" end |
#format_name(name) ⇒ String
81 82 83 84 85 86 87 88 |
# File 'app/concerns/view.rb', line 81 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.
16 17 18 19 20 |
# File 'app/concerns/view.rb', line 16 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.
26 27 28 |
# File 'app/concerns/view.rb', line 26 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.
33 34 35 36 37 38 39 |
# File 'app/concerns/view.rb', line 33 def (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 |