From 55e9adf73d045b5b6e8abfd66eaa512a4b4da1b3 Mon Sep 17 00:00:00 2001 From: di-sukharev Date: Tue, 7 Mar 2023 15:21:16 +0800 Subject: [PATCH] =?UTF-8?q?*=20=F0=9F=90=9B=20fix(cli.ts):=20add=20assert?= =?UTF-8?q?=20statement=20to=20packageJSON=20import=20The=20assert=20state?= =?UTF-8?q?ment=20was=20added=20to=20ensure=20that=20the=20imported=20pack?= =?UTF-8?q?ageJSON=20is=20of=20type=20JSON.=20This=20prevents=20any=20pote?= =?UTF-8?q?ntial=20runtime=20errors=20that=20may=20occur=20if=20the=20impo?= =?UTF-8?q?rted=20file=20is=20not=20of=20the=20expected=20type.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 🐛 fix(generateCommitMessageFromGitDiff.ts): adjust MAX_REQ_TOKENS to account for INIT_MESSAGES_PROMPT_LENGTH * 💄 style(generateCommitMessageFromGitDiff.ts): remove unnecessary separator variable The MAX_REQ_TOKENS constant was not accounting for the length of the INIT_MESSAGES_PROMPT, which caused the function to fail when the diff length was close to the limit. This has been fixed by adjusting the constant to include the prompt length. The separator variable was not being used and has been removed for clarity. * 🚀 chore(tsconfig.json): update compiler options The target compiler option has been updated to ESNext to allow for the use of the latest ECMAScript features. The lib compiler option has been updated to include ES6 in addition to ES5. The module compiler option has been updated to ESNext to allow for the use of the latest module syntax. --- src/cli.ts | 2 +- src/generateCommitMessageFromGitDiff.ts | 26 +++++++++++-------------- tsconfig.json | 6 +++--- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/cli.ts b/src/cli.ts index 5c8aa2e..5bc56e5 100755 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,7 +1,7 @@ #!/usr/bin/env node import { cli } from 'cleye'; -import packageJSON from '../package.json'; +import packageJSON from '../package.json' assert { type: 'json' }; import { configCommand } from './commands/config'; import { hookCommand, isHookCalled } from './commands/githook.js'; diff --git a/src/generateCommitMessageFromGitDiff.ts b/src/generateCommitMessageFromGitDiff.ts index bd94625..6c09479 100644 --- a/src/generateCommitMessageFromGitDiff.ts +++ b/src/generateCommitMessageFromGitDiff.ts @@ -81,34 +81,30 @@ const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map( (msg) => msg.content ).join('').length; +const MAX_REQ_TOKENS = 3900 - INIT_MESSAGES_PROMPT_LENGTH; + export const generateCommitMessageWithChatCompletion = async ( diff: string ): Promise => { try { - const MAX_REQ_TOKENS = 3900; - - if (INIT_MESSAGES_PROMPT_LENGTH + diff.length >= MAX_REQ_TOKENS) { + if (diff.length >= MAX_REQ_TOKENS) { const separator = 'diff --git '; const diffByFiles = diff.split(separator).slice(1); - const commitMessages = []; - - for (const diffFile of diffByFiles) { - if (INIT_MESSAGES_PROMPT_LENGTH + diffFile.length >= MAX_REQ_TOKENS) - continue; + const commitMessagePromises = diffByFiles.map((fileDiff) => { + // TODO: split by files + if (INIT_MESSAGES_PROMPT_LENGTH + fileDiff.length >= MAX_REQ_TOKENS) + return null; const messages = generateCommitMessageChatCompletionPrompt( - separator + diffFile + separator + fileDiff ); - const commitMessage = await api.generateCommitMessage(messages); + return api.generateCommitMessage(messages); + }); - // TODO: handle this edge case - if (!commitMessage?.content) continue; - - commitMessages.push(commitMessage?.content); - } + const commitMessages = await Promise.all(commitMessagePromises); return commitMessages.join('\n\n'); } diff --git a/tsconfig.json b/tsconfig.json index 487dd70..6dedf1a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,9 +1,9 @@ { "compilerOptions": { - "target": "ES2020", - "lib": ["ES5"], + "target": "ESNext", + "lib": ["ES5", "ES6"], - "module": "CommonJS", + "module": "ESNext", // "rootDir": "./src", "moduleResolution": "node", "resolveJsonModule": true,