Module: CompletionKit::ApplicationHelper

Defined in:
app/helpers/completion_kit/application_helper.rb

Constant Summary collapse

OPENAI_MODEL_FAMILY_ORDER =
["GPT-5", "GPT-4", "o-series", "GPT-3.5", "GPT-OSS", "Other"].freeze
CHECK_KIND_LABELS =
{
  "contains" => "Contains a phrase",
  "not_contains" => "Does not contain a phrase",
  "equals" => "Equals exactly",
  "regex" => "Matches a pattern",
  "valid_json" => "Is valid JSON",
  "json_path_equals" => "A JSON field equals a value",
  "length_bounds" => "Length is within a range"
}.freeze
CHECK_COMPARE_TO_LABELS =
{
  "constant" => "A value you type",
  "expected" => "Each row's expected value"
}.freeze
CHECK_TARGET_LABELS =
{
  "response_text" => "The response text",
  "input_data" => "The input row",
  "json_path" => "A value from the response JSON"
}.freeze
CHECK_FIELD_LABELS =
{
  "value" => "Text to look for",
  "pattern" => "Pattern",
  "json_path" => "JSON path",
  "expected" => "Expected value",
  "expected_path" => "Field in the answer key",
  "compare_to" => "Compare against",
  "target_path" => "Path into the JSON",
  "min" => "Shortest allowed",
  "max" => "Longest allowed",
  "case_sensitive" => "Case sensitive",
  "multiline" => "Multiline",
  "trim" => "Trim whitespace"
}.freeze

Instance Method Summary collapse

Instance Method Details

#ck_badge_classes(kind) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/completion_kit/application_helper.rb', line 73

def ck_badge_classes(kind)
  case kind.to_s
  when "high"
    "ck-badge ck-badge--high"
  when "medium"
    "ck-badge ck-badge--medium"
  when "low"
    "ck-badge ck-badge--low"
  when "pending"
    "ck-badge ck-badge--pending"
  when "running"
    "ck-badge ck-badge--running"
  when "completed"
    "ck-badge ck-badge--high"
  when "failed"
    "ck-badge ck-badge--low"
  else
    "ck-badge ck-badge--pending"
  end
end

#ck_button_classes(tone = :dark, variant: :solid) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/completion_kit/application_helper.rb', line 36

def ck_button_classes(tone = :dark, variant: :solid)
  base = "ck-button"

  styles = case [tone, variant]
           when [:dark, :solid]
             "ck-button--primary"
           when [:light, :outline]
             "ck-button--secondary"
           when [:green, :solid]
             "ck-button--success"
           when [:red, :outline]
             "ck-button--danger"
           when [:amber, :outline]
             "ck-button--warning"
           when [:blue, :outline]
             "ck-button--info"
           else
             "ck-button--primary"
           end

  "#{base} #{styles}"
end

#ck_check_badge(passed) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'app/helpers/completion_kit/application_helper.rb', line 203

def ck_check_badge(passed)
  if passed == true
    (:span, "Pass", class: ck_badge_classes(:high))
  elsif passed == false
    (:span, "Fail", class: ck_badge_classes(:low))
  else
    (:span, "Pending", class: ck_badge_classes(:pending))
  end
end

#ck_check_field_label(key) ⇒ Object



275
276
277
# File 'app/helpers/completion_kit/application_helper.rb', line 275

def ck_check_field_label(key)
  CHECK_FIELD_LABELS.fetch(key.to_s) { key.to_s.humanize }
end

#ck_check_field_value(key, value) ⇒ Object



279
280
281
282
283
# File 'app/helpers/completion_kit/application_helper.rb', line 279

def ck_check_field_value(key, value)
  return CHECK_COMPARE_TO_LABELS.fetch(value.to_s, value) if key.to_s == "compare_to"

  value
end

#ck_check_kind_label(kind) ⇒ Object



267
268
269
# File 'app/helpers/completion_kit/application_helper.rb', line 267

def ck_check_kind_label(kind)
  CHECK_KIND_LABELS.fetch(kind.to_s) { kind.to_s.humanize }
end

#ck_check_target_label(target) ⇒ Object



271
272
273
# File 'app/helpers/completion_kit/application_helper.rb', line 271

def ck_check_target_label(target)
  CHECK_TARGET_LABELS.fetch(target.to_s) { target.to_s.humanize }
end

#ck_dataset_path(dataset) ⇒ Object



323
324
325
# File 'app/helpers/completion_kit/application_helper.rb', line 323

def ck_dataset_path(dataset)
  CompletionKit::Engine.routes.url_helpers.dataset_path(dataset, **ck_engine_path_options)
end

#ck_delete_trigger(form_id:, label:, confirm: nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/completion_kit/application_helper.rb', line 59

def ck_delete_trigger(form_id:, label:, confirm: nil)
  (
    :button,
    type: "submit",
    form: form_id,
    class: "ck-icon-btn",
    title: label,
    "aria-label": label,
    data: {turbo_confirm: confirm, ck_confirm_label: "Delete", ck_confirm_tone: "danger"}
  ) do
    heroicon_tag "trash", variant: :outline, size: 16, "aria-hidden": "true"
  end
end

#ck_engine_path_optionsObject

Dynamic route segments owned by the host's mount scope (e.g. an org_slug from scope "/orgs/:org_slug") live in url_options[:_recall]. The engine's url_helpers won't read them out of the nested recall hash to satisfy a required segment — they have to arrive as explicit kwargs.



331
332
333
# File 'app/helpers/completion_kit/application_helper.rb', line 331

def ck_engine_path_options
  (url_options[:_recall] || {}).except(:controller, :action, :id, :format)
end

#ck_field_aria(form, field) ⇒ Object



425
426
427
428
# File 'app/helpers/completion_kit/application_helper.rb', line 425

def ck_field_aria(form, field)
  return {} unless form.object.errors[field].any?
  { "aria-invalid" => "true", "aria-describedby" => ck_field_error_id(form, field) }
end

#ck_field_error(form, field) ⇒ Object



430
431
432
433
# File 'app/helpers/completion_kit/application_helper.rb', line 430

def ck_field_error(form, field)
  return nil unless form.object.errors[field].any?
  (:p, form.object.errors[field].first, class: "ck-field-error", id: ck_field_error_id(form, field))
end

#ck_field_error_id(form, field) ⇒ Object



435
436
437
# File 'app/helpers/completion_kit/application_helper.rb', line 435

def ck_field_error_id(form, field)
  "#{form.object.model_name.param_key}_#{field}_error"
end

#ck_format_maybe_json(text) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
# File 'app/helpers/completion_kit/application_helper.rb', line 335

def ck_format_maybe_json(text)
  s = text.to_s
  return s if s.strip.empty?
  payload = ck_unwrap_json_fence(s.strip)
  first = payload[0]
  return s unless first == "{" || first == "["
  begin
    ck_highlight_json(JSON.pretty_generate(JSON.parse(payload)))
  rescue JSON::ParserError
    s
  end
end

#ck_grouped_models(models, selected = nil) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/helpers/completion_kit/application_helper.rb', line 154

def ck_grouped_models(models, selected = nil)
  if selected.present? && models.none? { |m| m[:id] == selected }
    retired = CompletionKit::Model.find_by(model_id: selected)
    if retired
      models = models + [{ id: retired.model_id, name: "#{retired.display_name || retired.model_id} (retired)", provider: retired.provider }]
    end
  end

  groups = models.group_by { |m| ck_model_optgroup_label(m) }
  ordered_keys = groups.keys.sort_by { |label| ck_model_optgroup_sort_key(label) }
  grouped = ordered_keys.map { |label| [label, groups[label].map { |m| [ck_model_option_label(m), m[:id]] }] }
  grouped_options_for_select(grouped, selected)
end

#ck_highlight_json(text) ⇒ Object



353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'app/helpers/completion_kit/application_helper.rb', line 353

def ck_highlight_json(text)
  tokens = ck_tokenize_json(text)
  is_key = ck_mark_json_keys(tokens)
  parts = tokens.each_with_index.map do |(type, value), idx|
    escaped = ERB::Util.html_escape(value)
    case type
    when :punct then %(<span class="ck-json-punct">#{escaped}</span>)
    when :string
      %(<span class="#{is_key[idx] ? "ck-json-key" : "ck-json-string"}">#{escaped}</span>)
    when :number then %(<span class="ck-json-number">#{escaped}</span>)
    when :keyword then %(<span class="ck-json-keyword">#{escaped}</span>)
    else escaped
    end
  end
  parts.join.html_safe
end

#ck_mark_json_keys(tokens) ⇒ Object



409
410
411
412
413
414
415
416
# File 'app/helpers/completion_kit/application_helper.rb', line 409

def ck_mark_json_keys(tokens)
  tokens.each_with_index.map do |(type, _), idx|
    next false unless type == :string
    j = idx + 1
    j += 1 while j < tokens.length && tokens[j][0] == :ws
    j < tokens.length && tokens[j] == [:punct, ":"]
  end
end

#ck_markdown(text) ⇒ Object



3
4
5
6
7
8
9
10
# File 'app/helpers/completion_kit/application_helper.rb', line 3

def ck_markdown(text)
  return "".html_safe if text.blank?

  inline = text.to_s
    .gsub(/\*\*(.+?)\*\*/, '<strong>\1</strong>')
    .gsub(/`([^`]+)`/, '<code>\1</code>')
  simple_format(inline)
end

#ck_masked_token(token) ⇒ Object



108
109
110
111
112
# File 'app/helpers/completion_kit/application_helper.rb', line 108

def ck_masked_token(token)
  return "YOUR_TOKEN" if token.blank?
  return "••••••••" if token.length < 12
  "#{token[0..3]}#{'' * [token.length - 8, 4].max}#{token[-4..]}"
end

#ck_model_optgroup_label(model) ⇒ Object

Optgroup label for the model select — mirrors the provider models table: OpenRouter splits by upstream vendor, OpenAI splits by family, everyone else is a single group.



171
172
173
174
175
176
177
# File 'app/helpers/completion_kit/application_helper.rb', line 171

def ck_model_optgroup_label(model)
  case model[:provider]
  when "openrouter" then "OpenRouter — #{model[:id].to_s.split("/", 2).first.delete_prefix("~")}"
  when "openai"     then "OpenAI — #{ck_openai_model_family(model[:id])}"
  else ck_provider_label(model[:provider])
  end
end

#ck_model_optgroup_sort_key(label) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'app/helpers/completion_kit/application_helper.rb', line 179

def ck_model_optgroup_sort_key(label)
  if label.start_with?("OpenAI — ")
    [0, OPENAI_MODEL_FAMILY_ORDER.index(label.delete_prefix("OpenAI — ")), label]
  elsif label.start_with?("OpenRouter")
    [2, 0, label]
  else
    [1, 0, label]
  end
end

#ck_model_option_label(model) ⇒ Object



149
150
151
152
# File 'app/helpers/completion_kit/application_helper.rb', line 149

def ck_model_option_label(model)
  return "#{model[:name]} (?)" if model.key?(:judging_confirmed) && !model[:judging_confirmed]
  model[:name]
end

#ck_model_options_html(scope) ⇒ Object



189
190
191
192
193
# File 'app/helpers/completion_kit/application_helper.rb', line 189

def ck_model_options_html(scope)
  models = CompletionKit::ApiConfig.available_models(scope: scope)
  return "" if models.empty?
  ck_grouped_models(models)
end

#ck_model_table_sections(models) ⇒ Object

Groups a provider's models for the models-card table, mirroring how the dropdown sub-groups: OpenRouter clusters by upstream vendor (the part before "/"); OpenAI clusters by family; everyone else stays flat. Returns [[section_label_or_nil, [models]], ...]. A single section collapses to a nil label so we don't render a redundant header.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/helpers/completion_kit/application_helper.rb', line 131

def ck_model_table_sections(models)
  models = models.to_a
  sections =
    case models.first&.provider
    when "openrouter"
      models.group_by { |m| m.model_id.to_s.split("/", 2).first.delete_prefix("~") }
            .sort_by { |label, _| label }
    when "openai"
      grouped = models.group_by { |m| ck_openai_model_family(m.model_id) }
      ordered = OPENAI_MODEL_FAMILY_ORDER.filter_map { |label| [label, grouped[label]] if grouped[label] }
      extras = (grouped.keys - OPENAI_MODEL_FAMILY_ORDER).sort.map { |label| [label, grouped[label]] }
      ordered + extras
    else
      [[nil, models]]
    end
  sections.size <= 1 ? [[nil, models]] : sections
end

#ck_openai_model_family(model_id) ⇒ Object



116
117
118
119
120
121
122
123
124
# File 'app/helpers/completion_kit/application_helper.rb', line 116

def ck_openai_model_family(model_id)
  id = model_id.to_s
  return "GPT-5" if id.match?(/\Agpt-5/i)
  return "GPT-4" if id.match?(/\Agpt-4/i)
  return "GPT-3.5" if id.match?(/\Agpt-3/i)
  return "GPT-OSS" if id.match?(/\Agpt-oss/i)
  return "o-series" if id.match?(/\Ao\d/i)
  "Other"
end

#ck_pass_rate_kind(rate) ⇒ Object



296
297
298
299
300
301
# File 'app/helpers/completion_kit/application_helper.rb', line 296

def ck_pass_rate_kind(rate)
  return :high if rate >= 0.9
  return :medium if rate >= 0.7

  :low
end

#ck_prompt_path(prompt) ⇒ Object



319
320
321
# File 'app/helpers/completion_kit/application_helper.rb', line 319

def ck_prompt_path(prompt)
  CompletionKit::Engine.routes.url_helpers.prompt_path(prompt, **ck_engine_path_options)
end

#ck_provider_label(provider) ⇒ Object



104
105
106
# File 'app/helpers/completion_kit/application_helper.rb', line 104

def ck_provider_label(provider)
  CompletionKit::ProviderCredential::PROVIDER_LABELS[provider.to_s] || provider.to_s.titleize
end

#ck_result_change_badge(change) ⇒ Object



285
286
287
288
289
290
291
292
293
294
# File 'app/helpers/completion_kit/application_helper.rb', line 285

def ck_result_change_badge(change)
  case change
  when "broke"
    (:span, "Broke", class: "ck-delta ck-delta--negative")
  when "fixed"
    (:span, "Fixed", class: "ck-delta ck-delta--positive")
  when "same"
    (:span, "Same", class: "ck-delta ck-delta--zero")
  end
end

#ck_retry_failures_label(generated_failed, judged_failed) ⇒ Object



28
29
30
31
32
33
34
# File 'app/helpers/completion_kit/application_helper.rb', line 28

def ck_retry_failures_label(generated_failed, judged_failed)
  if generated_failed.zero?
    "Retry scoring on #{pluralize(judged_failed, "response")}"
  else
    "Retry #{pluralize(generated_failed + judged_failed, "failed response")}"
  end
end

#ck_review_section_header(reviews, judge_model) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'app/helpers/completion_kit/application_helper.rb', line 213

def ck_review_section_header(reviews, judge_model)
  kinds = reviews.filter_map(&:effective_metric_type)
  judge = kinds.include?("llm_judge")
  check = kinds.include?("check")

  heading = if judge && check
              "Judge's review and checks"
            elsif judge
              "Judge's review"
            elsif check
              "Checks"
            else
              "Reviews"
            end

  {heading: heading, judge_model: judge ? judge_model.presence : nil}
end

#ck_run_dot(run) ⇒ Object



94
95
96
97
98
99
100
101
# File 'app/helpers/completion_kit/application_helper.rb', line 94

def ck_run_dot(run)
  case run.status
  when "running" then "ck-dot ck-dot--running"
  when "failed" then "ck-dot ck-dot--failed"
  when "completed" then "ck-dot ck-dot--completed"
  else "ck-dot ck-dot--pending"
  end
end

#ck_run_path(run) ⇒ Object



315
316
317
# File 'app/helpers/completion_kit/application_helper.rb', line 315

def ck_run_path(run)
  CompletionKit::Engine.routes.url_helpers.run_path(run, **ck_engine_path_options)
end


22
23
24
25
26
# File 'app/helpers/completion_kit/application_helper.rb', line 22

def ck_runs_display_footer(runs)
  partial = CompletionKit.config.runs_display_footer_partial
  return unless partial
  render partial, runs: runs
end

#ck_score_kind(score) ⇒ Object



195
196
197
198
199
200
201
# File 'app/helpers/completion_kit/application_helper.rb', line 195

def ck_score_kind(score)
  return :pending if score.nil?
  return :high if score >= CompletionKit.config.high_quality_threshold
  return :medium if score >= CompletionKit.config.medium_quality_threshold

  :low
end

#ck_strip_markdown(text) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'app/helpers/completion_kit/application_helper.rb', line 12

def ck_strip_markdown(text)
  text.to_s
    .gsub(/\*\*(.+?)\*\*/, '\1')
    .gsub(/`([^`]+)`/, '\1')
    .gsub(/[#>]+/, " ")
    .gsub(/(?:^|\s)[-*•]\s+/, " ")
    .gsub(/\s+/, " ")
    .strip
end

#ck_tokenize_json(text) ⇒ Object



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# File 'app/helpers/completion_kit/application_helper.rb', line 370

def ck_tokenize_json(text)
  tokens = []
  i = 0
  len = text.length
  while i < len
    ch = text[i]
    if ch == " " || ch == "\n" || ch == "\t"
      tokens << [:ws, ch]
      i += 1
    elsif "{}[]:,".include?(ch)
      tokens << [:punct, ch]
      i += 1
    elsif ch == '"'
      j = i + 1
      while j < len && text[j] != '"'
        j += text[j] == "\\" ? 2 : 1
      end
      j = len - 1 if j >= len
      tokens << [:string, text[i..j]]
      i = j + 1
    elsif ch == "-" || (ch >= "0" && ch <= "9")
      j = i + 1
      j += 1 while j < len && "0123456789.eE+-".include?(text[j])
      tokens << [:number, text[i...j]]
      i = j
    elsif text[i, 4] == "true" || text[i, 4] == "null"
      tokens << [:keyword, text[i, 4]]
      i += 4
    elsif text[i, 5] == "false"
      tokens << [:keyword, "false"]
      i += 5
    else
      tokens << [:other, ch]
      i += 1
    end
  end
  tokens
end

#ck_unwrap_json_fence(text) ⇒ Object



348
349
350
351
# File 'app/helpers/completion_kit/application_helper.rb', line 348

def ck_unwrap_json_fence(text)
  m = text.match(/\A```(?:json|JSON)?\s*\n(.*?)\n?```\s*\z/m)
  m ? m[1].strip : text
end

#ck_word_diff_new(old_text, new_text) ⇒ Object



307
308
309
# File 'app/helpers/completion_kit/application_helper.rb', line 307

def ck_word_diff_new(old_text, new_text)
  diff_tokens(old_text, new_text, :new)
end

#ck_word_diff_old(old_text, new_text) ⇒ Object



303
304
305
# File 'app/helpers/completion_kit/application_helper.rb', line 303

def ck_word_diff_old(old_text, new_text)
  diff_tokens(old_text, new_text, :old)
end

#tag_filter_url(base_path, selected, toggling) ⇒ Object



418
419
420
421
422
423
# File 'app/helpers/completion_kit/application_helper.rb', line 418

def tag_filter_url(base_path, selected, toggling)
  remaining = selected.reject { |t| t.id == toggling.id }
  next_set = selected.include?(toggling) ? remaining : remaining + [toggling]
  return base_path if next_set.empty?
  "#{base_path}?#{{ tag: next_set.map(&:name) }.to_query}"
end

#tag_pill_class(tag, outline: false) ⇒ Object



311
312
313
# File 'app/helpers/completion_kit/application_helper.rb', line 311

def tag_pill_class(tag, outline: false)
  ["tag", "tag-#{tag.color}", ("tag-outline" if outline)].compact.join(" ")
end