AI Commit Message Generator — JavaScript 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 JavaScript implementation — the same logic the interactive tool runs, in a shareable, citable form.
/**
* commit-message-generator — diff stats, prompt building, completion parsing.
*
* Language: JavaScript (ES module; runs in browsers, Node >= 16, Bun, Deno).
* CosmoDev polyglot showcase port of the `commit-message-generator` tool,
* ported from src/lib/commit-message.ts (the canonical TypeScript implementation).
*
* Why this file exists: everything the AI Studio island does besides the LLM
* call itself — counting a diff, shaping the chat prompt, parsing the model's
* reply — is pure string work. Keeping those pieces byte-identical across the
* polyglot set makes each page a runnable spec of the tool, not just art.
*
* display source — part of CosmoDev's polyglot tool pages.
*/
/** The commit types the island's picker offers (TS `CommitType`). */
export const COMMIT_TYPES = ['feat', 'fix', 'docs', 'refactor', 'chore'];
/** Subject-line contract the model must satisfy: `type(scope)!: subject`. */
const SUBJECT_RE = /^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|security|audit)(\([^)]+\))?!?: .+$/;
/** One match per `diff --git a/… b/…` file header. */
const DIFF_FILE_RE = /^diff --git /gm;
/** An added line: a `+` not followed by another `+` (skips the `+++` header). */
const ADD_RE = /^\+[^+]/gm;
/** A deleted line: a `-` not followed by another `-` (skips the `---` header). */
const DEL_RE = /^-[^-]/gm;
/** Git stamps brand-new files with a `new file mode` extended header. */
const NEW_FILE_RE = /^new file mode /gm;
/** Diffs larger than this are cut before they reach the model. */
const MAX_DIFF_CHARS = 8000;
/**
* Count files / ±lines / new files from a unified diff; tolerant of non-diff
* input. `String.prototype.match` with the `g` flag ignores `lastIndex`, so
* the shared module-level regexes stay stateless across calls.
*/
export function diffStats(diff) {
if (!diff.trim()) return { files: 0, additions: 0, deletions: 0, newFiles: 0 };
return {
files: (diff.match(DIFF_FILE_RE) ?? []).length,
additions: (diff.match(ADD_RE) ?? []).length,
deletions: (diff.match(DEL_RE) ?? []).length,
newFiles: (diff.match(NEW_FILE_RE) ?? []).length,
};
}
/**
* Build the [system, user] chat messages for the provider layer. A diff over
* MAX_DIFF_CHARS is cut with an explicit marker so a runaway `git diff` can
* never blow the context window silently.
*/
export function buildPromptMessages(diff, opts) {
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}`,
},
];
}
/**
* Parse the model's reply into {subject, body}; null when unusable.
*
* Strips ``` fences, then scans line by line for the first line that is a
* valid conventional-commit subject; everything after it is the body, minus
* a bare "here…:" / "sure…:" aside the model may have led with.
*/
export function parseCompletion(text) {
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 →