Class: AdminSuite::Renderers::LegacyGleania::PromptTemplateRenderer

Inherits:
AdminSuite::Renderer show all
Defined in:
lib/admin_suite/renderers/legacy_gleania.rb

Overview

Verbatim from BaseHelper#render_prompt_template.

Instance Attribute Summary

Attributes inherited from AdminSuite::Renderer

#options, #record, #view

Instance Method Summary collapse

Methods inherited from AdminSuite::Renderer

#initialize

Constructor Details

This class inherits a constructor from AdminSuite::Renderer

Instance Method Details

#renderObject



43
44
45
46
47
48
49
50
51
52
53
54
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
# File 'lib/admin_suite/renderers/legacy_gleania.rb', line 43

def render
  LegacyGleania.warn_once(:prompt_template_preview)

  template = record.respond_to?(:prompt_template) ? record.prompt_template : nil
  return (:p, "No template defined", class: "text-slate-500 italic") if template.blank?

  highlighted_template = h(template).gsub(/\{\{(\w+)\}\}/) do
    "<span class=\"text-amber-400 bg-amber-900/30 px-1 rounded\">{{#{$1}}}</span>"
  end

  (:div, class: "relative group") do
    view.concat((:div, class: "absolute top-2 right-2 flex items-center gap-2") do
      view.concat((:span, "TEMPLATE", class: "text-xs font-medium text-slate-400 uppercase tracking-wider"))
      view.concat((:button,
        '<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z"/></svg>'.html_safe,
        type: "button",
        class: "p-1 text-slate-400 hover:text-slate-600 opacity-0 group-hover:opacity-100 transition-opacity",
        data: { controller: "admin-suite--clipboard", action: "click->admin-suite--clipboard#copy", "admin-suite--clipboard-text-value": template },
        title: "Copy to clipboard"))
    end)

    view.concat((:pre, class: "bg-slate-900 text-slate-100 p-4 rounded-lg overflow-x-auto text-sm font-mono max-h-[600px] overflow-y-auto whitespace-pre-wrap leading-relaxed") do
      highlighted_template.html_safe
    end)

    variables = template.scan(/\{\{(\w+)\}\}/).flatten.uniq
    if variables.any?
      view.concat((:div, class: "mt-3 pt-3 border-t border-slate-700") do
        view.concat((:span, "Variables: ", class: "text-sm text-slate-400"))
        view.concat((:div, class: "inline-flex flex-wrap gap-1 mt-1") do
          variables.each do |var|
            view.concat((:code, "{{#{var}}}", class: "text-xs px-2 py-0.5 bg-amber-900/30 text-amber-400 rounded"))
          end
        end)
      end)
    end
  end
end