Skip to content

Commit 95a80fb

Browse files
authored
feat: enhance changelog generation by cleaning commit messages and estimating token usage (#342)
1 parent d478162 commit 95a80fb

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

scripts/changelog.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import https from 'node:https';
6262
import OpenAI from 'openai';
6363

6464
const ENDPOINT = 'https://models.github.ai/inference';
65-
const MODEL = 'openai/gpt-4.1'; // gpt-5 has max request size of 4000 tokens on free
65+
const MODEL = 'openai/gpt-4.1'; // 4000 token input limit (smaller than gpt-4o but sufficient after cleanup)
6666
const PROMPT = `
6767
You're the head of developer relations at a SaaS company. Write a concise, professional, and engaging changelog that prioritizes user-impacting changes and tells a story about the release.
6868
@@ -504,21 +504,51 @@ async function generateChangelog(version) {
504504
const commits = await getCommitsBetweenLatestReleaseAndMain(latestVersionTag);
505505

506506
console.log(`Found ${commits.length} commits between ${latestVersionTag} and main`);
507-
console.log('Commits:', JSON.stringify(commits, null, 2));
507+
508+
// Clean up commits to reduce token usage
509+
const cleanedCommits = commits.map((commit) => {
510+
// For dependabot commits, truncate message at first newline to remove verbose details
511+
// For other commits, keep full message
512+
const message = commit.author === 'dependabot[bot]' ? commit.message.split('\n')[0] : commit.message;
513+
514+
// Return simplified commit object without URL (can be inferred from PR number)
515+
return {
516+
author: commit.author,
517+
message: message,
518+
pullRequest: commit.pullRequest
519+
? {
520+
number: commit.pullRequest.number,
521+
title: commit.pullRequest.title,
522+
}
523+
: null,
524+
};
525+
});
526+
527+
console.log('Cleaned commits:', JSON.stringify(cleanedCommits, null, 2));
528+
529+
// Estimate token count (rough approximation: 1 token ≈ 4 characters)
530+
const commitDataString = JSON.stringify(cleanedCommits);
531+
const estimatedInputTokens = Math.ceil((PROMPT.length + commitDataString.length) / 4);
532+
console.log(`\n📏 Estimated input size: ~${estimatedInputTokens} tokens (limit: 4000 for gpt-4.1)`);
508533

509534
try {
510535
const client = new OpenAI({ baseURL: ENDPOINT, apiKey: process.env.GITHUB_TOKEN });
511536
const response = await client.chat.completions.create({
512537
messages: [
513538
{ role: 'system', content: PROMPT },
514-
{ role: 'user', content: JSON.stringify(commits) },
539+
{ role: 'user', content: JSON.stringify(cleanedCommits) },
515540
],
516541
temperature: 0.3, // Low temperature for consistent, focused output (0.0-2.0, lower = more deterministic)
517542
top_p: 0.9, // High nucleus sampling to maintain quality while reducing randomness (0.0-1.0)
518543
model: MODEL,
519544
});
520545

521-
console.log('Generated changelog content:');
546+
console.log('\n📊 Token Usage:');
547+
console.log(` Prompt tokens: ${response.usage.prompt_tokens}`);
548+
console.log(` Completion tokens: ${response.usage.completion_tokens}`);
549+
console.log(` Total tokens: ${response.usage.total_tokens}`);
550+
551+
console.log('\n✅ Generated changelog content:');
522552
console.log(response.choices[0].message.content);
523553

524554
return [

0 commit comments

Comments
 (0)