Skip to content

AI Tone Rewriter — Ruby source

Rewrite any message in a new tone — professional, friendly, concise, formal, or playful — with your own free Groq key, called straight from your browser.

This is the Ruby implementation — the same logic the interactive tool runs, in a shareable, citable form.

# tone-rewriter — tone registry, prompt building, and reply cleanup.
#
# Language: Ruby (3.x, standard library only — gsub carries the fence
#           cleanup, and String#length already counts characters)
# CosmoDev polyglot showcase port of the `tone-rewriter` tool, ported from
# src/lib/tone-rewriter.ts (the canonical TypeScript implementation).
#
# Why this file exists: the tone rewriter is an AI tool, but its contract is
# pure — which registers exist, how the system prompt is assembled, and how
# a raw model reply is cleaned for display. One regex is the only machinery
# the cleanup needs, so the whole port is stdlib with no provider dependency.

# frozen_string_literal: true

module ToneRewriter
  # Payload cap for the user text (the TS MAX_INPUT_CHARS).
  MAX_INPUT_CHARS = 12_000

  # The five supported registers — the SSOT for the UI's segmented control.
  TONES = [
    { id: "professional", label: "Professional" },
    { id: "friendly", label: "Friendly" },
    { id: "concise", label: "Concise" },
    { id: "formal", label: "Formal" },
    { id: "playful", label: "Playful" },
  ].freeze

  # Per-tone register sentence embedded in the system prompt.
  TONE_CONTRACT = {
    "professional" =>
      "polished and businesslike, no slang, clear and direct — the register " \
      "of a status update to stakeholders",
    "friendly" =>
      "warm and approachable, contractions welcome, like a teammate " \
      "chatting — never stiff",
    "concise" =>
      "as short as possible while keeping every fact; prefer one sentence; " \
      "cut filler words without exception",
    "formal" =>
      "ceremonial register, no contractions, complete sentences — the " \
      "register of official correspondence",
    "playful" =>
      "light and witty, a touch of humor, emojis allowed (at most two) — " \
      "but the facts stay exact",
  }.freeze

  module_function

  # Build the [system, user] chat messages for the provider layer.
  #
  # Input longer than MAX_INPUT_CHARS is sliced and tagged with a visible
  # "… (text truncated)" marker so the model knows the payload was capped.
  # Ruby String#length counts characters (code points) where the TS lib
  # counts UTF-16 units — they agree outside the astral planes.
  def build_prompt_messages(text, tone)
    body = if text.length > MAX_INPUT_CHARS
             "#{text[0, MAX_INPUT_CHARS]}\n… (text truncated)"
           else
             text
           end
    system =
      "You rewrite text in a target tone. Rewrite the user's text in a #{tone} " \
      "tone: #{TONE_CONTRACT.fetch(tone)}. Keep the same meaning and the same " \
      "facts — only the register changes. Output ONLY the rewritten text, no " \
      "preamble, no notes."
    [
      { role: "system", content: system },
      { role: "user", content: body },
    ]
  end

  # Clean the model's reply into displayable rewritten text.
  #
  # Strips every ```lang fence marker (the TS /```[a-z]*\n?/g regex) plus
  # surrounding whitespace; returns nil only when nothing survives.
  def parse_rewrite(text)
    cleaned = text.gsub(/```[a-z]*\n?/, "").strip
    cleaned.empty? ? nil : cleaned
  end
end

if $PROGRAM_NAME == __FILE__
  msgs = ToneRewriter.build_prompt_messages("The Q3 numbers are in.", "professional")
  msgs.each { |m| puts "#{m[:role]}: #{m[:content]}" }

  puts ToneRewriter.parse_rewrite("```text\nHere you go!\n```\n") || "(nil)"
  puts ToneRewriter.parse_rewrite("``` \n") || "(nil)" # a fence and nothing else
end

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →