diff --git a/.gitignore b/.gitignore index 89dcc5c..e95a4db 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ logfile.log uncaughtExceptions.log .vscode src/*.json -.idea \ No newline at end of file +.idea +test.ts \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 0cc093e..d799cb4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "MIT", "dependencies": { "@clack/prompts": "^0.6.1", + "@dqbd/tiktoken": "^1.0.2", "axios": "^1.3.4", "chalk": "^5.2.0", "cleye": "^1.3.2", @@ -83,6 +84,11 @@ "node": ">=12" } }, + "node_modules/@dqbd/tiktoken": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@dqbd/tiktoken/-/tiktoken-1.0.2.tgz", + "integrity": "sha512-AjGTBRWsMoVmVeN55NLyupyM8TNamOUBl6tj5t/leLDVup3CFGO9tVagNL1jf3GyZLkWZSTmYVbPQ/M2LEcNzw==" + }, "node_modules/@esbuild/android-arm": { "version": "0.15.18", "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.15.18.tgz", diff --git a/package.json b/package.json index 760b0c1..0ce4593 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ }, "dependencies": { "@clack/prompts": "^0.6.1", + "@dqbd/tiktoken": "^1.0.2", "axios": "^1.3.4", "chalk": "^5.2.0", "cleye": "^1.3.2", diff --git a/src/commands/prepare-commit-msg-hook.ts b/src/commands/prepare-commit-msg-hook.ts index 68547dc..afde485 100644 --- a/src/commands/prepare-commit-msg-hook.ts +++ b/src/commands/prepare-commit-msg-hook.ts @@ -1,6 +1,6 @@ import fs from 'fs/promises'; import chalk from 'chalk'; -import { intro, outro } from '@clack/prompts'; +import { intro, outro, spinner } from '@clack/prompts'; import { getChangedFiles, getDiff, getStagedFiles, gitAdd } from '../utils/git'; import { getConfig } from './config'; import { generateCommitMessageWithChatCompletion } from '../generateCommitMessageFromGitDiff'; @@ -17,11 +17,13 @@ export const prepareCommitMessageHook = async () => { if (commitSource) return; + const stagedFiles = await getStagedFiles(); const changedFiles = await getChangedFiles(); - if (changedFiles) await gitAdd({ files: changedFiles }); + + if (!stagedFiles && changedFiles) await gitAdd({ files: changedFiles }); else { - outro("No changes detected, write some code and run `oc` again"); - process.exit(1); + outro('No changes detected, write some code and run `oc` again'); + process.exit(1); } const staged = await getStagedFiles(); @@ -38,11 +40,15 @@ export const prepareCommitMessageHook = async () => { ); } + const spin = spinner(); + spin.start('Generating commit message'); const commitMessage = await generateCommitMessageWithChatCompletion( await getDiff({ files: staged }) ); - - if (typeof commitMessage !== 'string') throw new Error(commitMessage.error); + if (typeof commitMessage !== 'string') { + spin.stop('Error'); + throw new Error(commitMessage.error); + } else spin.stop('Done'); const fileContent = await fs.readFile(messageFilePath); diff --git a/src/generateCommitMessageFromGitDiff.ts b/src/generateCommitMessageFromGitDiff.ts index 69100b0..7386f25 100644 --- a/src/generateCommitMessageFromGitDiff.ts +++ b/src/generateCommitMessageFromGitDiff.ts @@ -4,8 +4,9 @@ import { } from 'openai'; import { api } from './api'; import { getConfig } from './commands/config'; -import { mergeStrings } from './utils/mergeStrings'; +import { mergeDiffs } from './utils/mergeDiffs'; import { i18n, I18nLocals } from './i18n'; +import { tokenCount } from './utils/tokenCount'; const config = getConfig(); const translation = i18n[(config?.language as I18nLocals) || 'en']; @@ -13,40 +14,38 @@ const translation = i18n[(config?.language as I18nLocals) || 'en']; const INIT_MESSAGES_PROMPT: Array = [ { role: ChatCompletionRequestMessageRoleEnum.System, - content: `You are to act as the author of a commit message in git. Your mission is to create clean and comprehensive commit messages in the conventional commit convention. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message. ${ - config?.emoji - ? 'Use Gitmoji convention to preface the commit' - : 'Do not preface the commit with anything' - }, use the present tense. ${ - config?.description - ? 'Add a short description of what commit is about after the commit message. Don\'t start it with "This commit", just describe the changes.' - : "Don't add any descriptions to the commit, only commit message." - } Use ${translation.localLanguage} to answer.` + // prettier-ignore + content: `You are to act as the author of a commit message in git. Your mission is to create clean and comprehensive commit messages in the conventional commit convention and explain why a change was done. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message. +${config?.emoji? 'Use Gitmoji convention to preface the commit.': 'Do not preface the commit with anything.'} +${config?.description ? 'Add a short description of why the commit is done after the commit message. Don\'t start it with "This commit", just describe the changes.': "Don't add any descriptions to the commit, only commit message."} +Use the present tense. Lines must not be longer than 74 characters. Use ${translation.localLanguage} to answer.` }, { role: ChatCompletionRequestMessageRoleEnum.User, content: `diff --git a/src/server.ts b/src/server.ts - index ad4db42..f3b18a9 100644 - --- a/src/server.ts - +++ b/src/server.ts - @@ -10,7 +10,7 @@ import { - initWinstonLogger(); - - const app = express(); - -const port = 7799; - +const PORT = 7799; - - app.use(express.json()); - - @@ -34,6 +34,6 @@ app.use((_, res, next) => { - // ROUTES - app.use(PROTECTED_ROUTER_URL, protectedRouter); - - -app.listen(port, () => { - - console.log(\`Server listening on port \${port}\`); - +app.listen(process.env.PORT || PORT, () => { - + console.log(\`Server listening on port \${PORT}\`); - });` +index ad4db42..f3b18a9 100644 +--- a/src/server.ts ++++ b/src/server.ts +@@ -10,7 +10,7 @@ +import { + initWinstonLogger(); + + const app = express(); + -const port = 7799; + +const PORT = 7799; + + app.use(express.json()); + +@@ -34,6 +34,6 @@ +app.use((_, res, next) => { + // ROUTES + app.use(PROTECTED_ROUTER_URL, protectedRouter); + + -app.listen(port, () => { + - console.log(\`Server listening on port \${port}\`); + +app.listen(process.env.PORT || PORT, () => { + + console.log(\`Server listening on port \${PORT}\`); + });` }, { role: ChatCompletionRequestMessageRoleEnum.Assistant, @@ -80,8 +79,8 @@ interface GenerateCommitMessageError { } const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map( - (msg) => msg.content -).join('').length; + (msg) => tokenCount(msg.content) + 4 +).reduce((a, b) => a + b, 0); const MAX_REQ_TOKENS = 3900 - INIT_MESSAGES_PROMPT_LENGTH; @@ -89,8 +88,11 @@ export const generateCommitMessageWithChatCompletion = async ( diff: string ): Promise => { try { - if (diff.length >= MAX_REQ_TOKENS) { - const commitMessagePromises = getCommitMsgsPromisesFromFileDiffs(diff); + if (tokenCount(diff) >= MAX_REQ_TOKENS) { + const commitMessagePromises = getCommitMsgsPromisesFromFileDiffs( + diff, + MAX_REQ_TOKENS + ); const commitMessages = await Promise.all(commitMessagePromises); @@ -110,22 +112,28 @@ export const generateCommitMessageWithChatCompletion = async ( } }; -function getMessagesPromisesByLines(fileDiff: string, separator: string) { - const lineSeparator = '\n@@'; - const [fileHeader, ...fileDiffByLines] = fileDiff.split(lineSeparator); +function getMessagesPromisesByChangesInFile( + fileDiff: string, + separator: string, + maxChangeLength: number +) { + const hunkHeaderSeparator = '@@ '; + const [fileHeader, ...fileDiffByLines] = fileDiff.split(hunkHeaderSeparator); // merge multiple line-diffs into 1 to save tokens - const mergedLines = mergeStrings( - fileDiffByLines.map((line) => lineSeparator + line), - MAX_REQ_TOKENS + const mergedChanges = mergeDiffs( + fileDiffByLines.map((line) => hunkHeaderSeparator + line), + maxChangeLength ); - const lineDiffsWithHeader = mergedLines.map( - (d) => fileHeader + lineSeparator + d + const lineDiffsWithHeader = mergedChanges.map( + (change) => fileHeader + change ); - const commitMsgsFromFileLineDiffs = lineDiffsWithHeader.map((d) => { - const messages = generateCommitMessageChatCompletionPrompt(separator + d); + const commitMsgsFromFileLineDiffs = lineDiffsWithHeader.map((lineDiff) => { + const messages = generateCommitMessageChatCompletionPrompt( + separator + lineDiff + ); return api.generateCommitMessage(messages); }); @@ -133,20 +141,27 @@ function getMessagesPromisesByLines(fileDiff: string, separator: string) { return commitMsgsFromFileLineDiffs; } -function getCommitMsgsPromisesFromFileDiffs(diff: string) { +export function getCommitMsgsPromisesFromFileDiffs( + diff: string, + maxDiffLength: number +) { const separator = 'diff --git '; const diffByFiles = diff.split(separator).slice(1); // merge multiple files-diffs into 1 prompt to save tokens - const mergedFilesDiffs = mergeStrings(diffByFiles, MAX_REQ_TOKENS); + const mergedFilesDiffs = mergeDiffs(diffByFiles, maxDiffLength); const commitMessagePromises = []; for (const fileDiff of mergedFilesDiffs) { - if (fileDiff.length >= MAX_REQ_TOKENS) { + if (tokenCount(fileDiff) >= maxDiffLength) { // if file-diff is bigger than gpt context — split fileDiff into lineDiff - const messagesPromises = getMessagesPromisesByLines(fileDiff, separator); + const messagesPromises = getMessagesPromisesByChangesInFile( + fileDiff, + separator, + maxDiffLength + ); commitMessagePromises.push(...messagesPromises); } else { diff --git a/src/i18n/index.ts b/src/i18n/index.ts index 53594c4..079e05e 100644 --- a/src/i18n/index.ts +++ b/src/i18n/index.ts @@ -9,6 +9,7 @@ import ja from '../i18n/ja.json' assert { type: 'json' }; import pt_br from '../i18n/pt_br.json' assert { type: 'json' }; import vi_VN from '../i18n/vi_VN.json' assert { type: 'json' }; import es_ES from '../i18n/es_ES.json' assert { type: 'json' }; +import sv from '../i18n/sv.json' assert { type: 'json' }; export enum I18nLocals { 'en' = 'en', @@ -20,7 +21,8 @@ export enum I18nLocals { 'it' = 'it', 'ko' = 'ko', 'pt_br' = 'pt_br', - 'es_ES' = 'es_ES' + 'es_ES' = 'es_ES', + 'sv' = 'sv', }; export const i18n = { @@ -34,7 +36,8 @@ export const i18n = { ko, pt_br, vi_VN, - es_ES + es_ES, + sv }; export const I18N_CONFIG_ALIAS: { [key: string]: string[] } = { @@ -49,6 +52,7 @@ export const I18N_CONFIG_ALIAS: { [key: string]: string[] } = { vi_VN: ['vi_VN', 'Vietnamese', 'tiếng Việt'], en: ['en', 'English', 'english'], es_ES: ['es_ES', 'Spanish', 'español'], + sv: ['sv', 'Swedish', 'Svenska'], }; export function getI18nLocal(value: string): string | boolean { diff --git a/src/i18n/sv.json b/src/i18n/sv.json new file mode 100644 index 0000000..eb86aee --- /dev/null +++ b/src/i18n/sv.json @@ -0,0 +1,6 @@ +{ +"localLanguage": "svenska", +"commitFix": "fixa(server.ts): ändra variabelnamnet för port från små bokstäver till stora bokstäver PORT", +"commitFeat": "nyhet(server.ts): lägg till stöd för process.env.PORT miljövariabel", +"commitDescription": "Variabeln som innehåller portnumret heter nu PORT vilket förbättrar konsekvensen med namngivningskonventionerna eftersom PORT är en konstant. Stöd för en miljövariabel gör att applikationen kan vara mer flexibel då den nu kan köras på vilken port som helst som specificeras via miljövariabeln process.env.PORT." +} \ No newline at end of file diff --git a/src/utils/mergeStrings.ts b/src/utils/mergeDiffs.ts similarity index 59% rename from src/utils/mergeStrings.ts rename to src/utils/mergeDiffs.ts index 7b55a99..a7b6aa0 100644 --- a/src/utils/mergeStrings.ts +++ b/src/utils/mergeDiffs.ts @@ -1,8 +1,9 @@ -export function mergeStrings(arr: string[], maxStringLength: number): string[] { +import { tokenCount } from './tokenCount'; +export function mergeDiffs(arr: string[], maxStringLength: number): string[] { const mergedArr: string[] = []; let currentItem: string = arr[0]; for (const item of arr.slice(1)) { - if (currentItem.length + item.length <= maxStringLength) { + if (tokenCount(currentItem + item) <= maxStringLength) { currentItem += item; } else { mergedArr.push(currentItem); diff --git a/src/utils/tokenCount.ts b/src/utils/tokenCount.ts new file mode 100644 index 0000000..84e4f23 --- /dev/null +++ b/src/utils/tokenCount.ts @@ -0,0 +1,14 @@ +import { Tiktoken } from "@dqbd/tiktoken/lite" +import cl100k_base from "@dqbd/tiktoken/encoders/cl100k_base.json" assert{type: "json"} + +export function tokenCount(content: string): number { + const encoding = new Tiktoken( + cl100k_base.bpe_ranks, + cl100k_base.special_tokens, + cl100k_base.pat_str + ); + const tokens = encoding.encode(content); + encoding.free(); + + return tokens.length; +}