Skip to content

AI Tone Rewriter — Kotlin 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 Kotlin implementation — the same logic the interactive tool runs, in a shareable, citable form.

// tone-rewriter — tone registry, prompt building, and reply cleanup.
//
// Language: Kotlin (1.9, JVM, standard library only — the enum carries the
//           registry, Regex carries the fence cleanup, and String.length
//           already counts UTF-16 units exactly like the TS lib)
// 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. No provider, no keys, no network.
//
// display source — part of CosmoDev's polyglot tool pages.
//
// Run: kotlinc kotlin.kt -include-runtime -d tone-rewriter.jar && java -jar tone-rewriter.jar

/** The five supported registers — the SSOT for the picker, in declaration
 *  order; each constant carries its wire id, label, and prompt sentence. */
enum class Tone(
    val id: String,
    val label: String,
    val contract: String,
) {
    PROFESSIONAL(
        "professional", "Professional",
        "polished and businesslike, no slang, clear and direct — the register " +
            "of a status update to stakeholders",
    ),
    FRIENDLY(
        "friendly", "Friendly",
        "warm and approachable, contractions welcome, like a teammate " +
            "chatting — never stiff",
    ),
    CONCISE(
        "concise", "Concise",
        "as short as possible while keeping every fact; prefer one sentence; " +
            "cut filler words without exception",
    ),
    FORMAL(
        "formal", "Formal",
        "ceremonial register, no contractions, complete sentences — the " +
            "register of official correspondence",
    ),
    PLAYFUL(
        "playful", "Playful",
        "light and witty, a touch of humor, emojis allowed (at most two) — " +
            "but the facts stay exact",
    ),
}

/** One chat message (`role` is "system" or "user"). */
data class Message(val role: String, val content: String)

/** Payload cap for the user text (the TS MAX_INPUT_CHARS). */
const val MAX_INPUT_CHARS = 12000

/**
 * 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.
 */
fun buildPromptMessages(text: String, tone: Tone): List<Message> {
    val body = if (text.length > MAX_INPUT_CHARS) {
        text.substring(0, MAX_INPUT_CHARS) + "\n… (text truncated)"
    } else {
        text
    }
    val system =
        "You rewrite text in a target tone. Rewrite the user's text in a ${tone.id} " +
            "tone: ${tone.contract}. Keep the same meaning and the same facts — only " +
            "the register changes. Output ONLY the rewritten text, no preamble, no notes."
    return listOf(Message("system", system), Message("user", body))
}

/**
 * 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 null only when nothing survives.
 */
fun parseRewrite(text: String): String? =
    text.replace(Regex("```[a-z]*\n?"), "").trim().ifEmpty { null }

fun main() {
    val msgs = buildPromptMessages("The Q3 numbers are in.", Tone.PROFESSIONAL)
    msgs.forEach { println("${it.role}: ${it.content}") }

    println(parseRewrite("```text\nHere you go!\n```\n"))
    println(parseRewrite("``` \n")) // a fence and nothing else
}

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 →