Skip to content

AI Text Summarizer — TypeScript source

Paste any text and get an LLM summary — TL;DR, bullets, or detailed — using your own free Groq key, called straight from your browser.

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

// src/lib/summarize.ts — the pure core of the AI text summarizer
// (AI Studio tool #2, FEAT-074; class rollout per the FEAT-049 verdict).
// Prompt building + reply cleanup + text stats are pure so the island is a
// thin shell over the shared provider layer (src/lib/ai-studio), and this
// file is a future Go-twin candidate once the class proves out.

export type SummaryStyle = 'tldr' | 'bullets' | 'detailed';

export interface TextStats {
  words: number;
  sentences: number;
  chars: number;
}

const MAX_INPUT_CHARS = 12000;

/** Count words / sentences / characters; tolerant of empty input. */
export function textStats(text: string): TextStats {
  const words = text.trim() ? text.trim().split(/\s+/).length : 0;
  // Count sentence TERMINATORS, not sentence bodies: the body regex
  // (/[^.!?]+[.!?]+/) is quadratic on terminator-free input — 1.7s on a
  // 30k-char paste (the recurring under-load cap-test timeout).
  const sentences = (text.match(/[.!?]+/g) ?? []).length;
  return { words, sentences, chars: text.length };
}

const STYLE_CONTRACT: Record<SummaryStyle, string> = {
  tldr: 'Summarize the text as ONE sentence of at most 25 words. Plain text, no preamble, no bullets.',
  bullets:
    'Summarize the text as 3 to 6 markdown bullets, each starting with "- " and covering one distinct point. No preamble, no closing remark.',
  detailed:
    'Summarize the text as 2 to 4 short paragraphs: what it is about, the key facts, and the outcome or next step. No preamble, no headings.',
};

/** Build the [system, user] chat messages for the provider layer. */
export function buildPromptMessages(
  text: string,
  opts: { style: SummaryStyle },
): Array<{ role: 'system' | 'user'; content: string }> {
  const stats = textStats(text);
  const truncated = text.length > MAX_INPUT_CHARS;
  const body = truncated ? `${text.slice(0, MAX_INPUT_CHARS)}\n… (text truncated)` : text;
  return [
    { role: 'system', content: `You summarize text faithfully. ${STYLE_CONTRACT[opts.style]}` },
    {
      role: 'user',
      content: `Summarize the following text (${opts.style} style; ${stats.words} words, ${stats.sentences} sentences).\n\n${body}`,
    },
  ];
}

/**
 * Clean the model's reply into displayable summary text. Unlike the
 * commit-message parser there is no strict format contract — any non-empty
 * cleaned text is the summary; null only when nothing survives.
 */
export function parseSummary(text: string): string | null {
  const cleaned = text.replace(/```[a-z]*\n?/g, '').trim();
  return cleaned.length > 0 ? cleaned : null;
}

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 →