Skip to content

AI Commit Message Generator — TypeScript source

Paste a git diff, get a conventional commit message from an LLM — bring 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/commit-message.ts — the pure core of the commit-message generator
// (AI Studio tool #1, FEAT-049 BR-04). Prompt building + completion parsing +
// diff heuristics are pure so the island is a thin shell (and this file is a
// future Go-twin candidate once the class proves out).

export type CommitType = 'feat' | 'fix' | 'docs' | 'refactor' | 'chore';

export interface DiffStats {
  files: number;
  additions: number;
  deletions: number;
  newFiles: number;
}

const DIFF_FILE_RE = /^diff --git /gm;
const ADD_RE = /^\+[^+]/gm;
const DEL_RE = /^-[^-]/gm;
const NEW_FILE_RE = /^new file mode /gm;

/** Count files/±lines from a unified diff; tolerant of non-diff input. */
export function diffStats(diff: string): DiffStats {
  if (!diff.trim()) return { files: 0, additions: 0, deletions: 0, newFiles: 0 };
  return {
    files: countMatches(diff, DIFF_FILE_RE),
    additions: countMatches(diff, ADD_RE),
    deletions: countMatches(diff, DEL_RE),
    newFiles: countMatches(diff, NEW_FILE_RE),
  };
}

function countMatches(s: string, re: RegExp): number {
  return (s.match(re) ?? []).length;
}

const MAX_DIFF_CHARS = 8000;

/** Build the [system, user] chat messages for the provider layer. */
export function buildPromptMessages(
  diff: string,
  opts: { type: CommitType },
): Array<{ role: 'system' | 'user'; content: string }> {
  const stats = diffStats(diff);
  const truncated = diff.length > MAX_DIFF_CHARS;
  const body = truncated ? `${diff.slice(0, MAX_DIFF_CHARS)}\n… (diff truncated)` : diff;
  return [
    {
      role: 'system',
      content:
        'You write one conventional commit message for the given diff. Reply with ONLY the message: a subject line "type: imperative summary" (max 72 chars, lowercase after the type), then a blank line, then a short body explaining what and why. No fences, no preamble.',
    },
    {
      role: 'user',
      content: `Commit type: ${opts.type}\nDiff summary: ${stats.files} files changed, ${stats.additions} additions, ${stats.deletions} deletions, ${stats.newFiles} new files.\n\n${body}`,
    },
  ];
}

const SUBJECT_RE = /^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|security|audit)(\([^)]+\))?!?: .+$/;

/** Parse the model's reply into {subject, body}; null when unusable. */
export function parseCompletion(
  text: string,
): { subject: string; body: string } | null {
  const cleaned = text.replace(/```[a-z]*\n?/g, '').trim();
  for (const line of cleaned.split('\n')) {
    const t = line.trim();
    if (SUBJECT_RE.test(t)) {
      const body = cleaned
        .slice(cleaned.indexOf(t) + t.length)
        .replace(/^\s*(here.*?:|sure.*?:)\s*$/im, '')
        .trim();
      return { subject: t, body: body || '' };
    }
  }
  return 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 →