* 🐛 fix(cli.ts): add assert statement to packageJSON import

The assert statement was added to ensure that the imported packageJSON is of type JSON. This prevents any potential runtime errors that may occur if the imported file is not of the expected type.

* 🐛 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.
This commit is contained in:
di-sukharev
2023-03-07 15:21:16 +08:00
parent 60325f53b9
commit 55e9adf73d
3 changed files with 15 additions and 19 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env node #!/usr/bin/env node
import { cli } from 'cleye'; import { cli } from 'cleye';
import packageJSON from '../package.json'; import packageJSON from '../package.json' assert { type: 'json' };
import { configCommand } from './commands/config'; import { configCommand } from './commands/config';
import { hookCommand, isHookCalled } from './commands/githook.js'; import { hookCommand, isHookCalled } from './commands/githook.js';
+11 -15
View File
@@ -81,34 +81,30 @@ const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map(
(msg) => msg.content (msg) => msg.content
).join('').length; ).join('').length;
const MAX_REQ_TOKENS = 3900 - INIT_MESSAGES_PROMPT_LENGTH;
export const generateCommitMessageWithChatCompletion = async ( export const generateCommitMessageWithChatCompletion = async (
diff: string diff: string
): Promise<string | GenerateCommitMessageError> => { ): Promise<string | GenerateCommitMessageError> => {
try { try {
const MAX_REQ_TOKENS = 3900; if (diff.length >= MAX_REQ_TOKENS) {
if (INIT_MESSAGES_PROMPT_LENGTH + diff.length >= MAX_REQ_TOKENS) {
const separator = 'diff --git '; const separator = 'diff --git ';
const diffByFiles = diff.split(separator).slice(1); const diffByFiles = diff.split(separator).slice(1);
const commitMessages = []; const commitMessagePromises = diffByFiles.map((fileDiff) => {
// TODO: split by files
for (const diffFile of diffByFiles) { if (INIT_MESSAGES_PROMPT_LENGTH + fileDiff.length >= MAX_REQ_TOKENS)
if (INIT_MESSAGES_PROMPT_LENGTH + diffFile.length >= MAX_REQ_TOKENS) return null;
continue;
const messages = generateCommitMessageChatCompletionPrompt( const messages = generateCommitMessageChatCompletionPrompt(
separator + diffFile separator + fileDiff
); );
const commitMessage = await api.generateCommitMessage(messages); return api.generateCommitMessage(messages);
});
// TODO: handle this edge case const commitMessages = await Promise.all(commitMessagePromises);
if (!commitMessage?.content) continue;
commitMessages.push(commitMessage?.content);
}
return commitMessages.join('\n\n'); return commitMessages.join('\n\n');
} }
+3 -3
View File
@@ -1,9 +1,9 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "ES2020", "target": "ESNext",
"lib": ["ES5"], "lib": ["ES5", "ES6"],
"module": "CommonJS", "module": "ESNext",
// "rootDir": "./src", // "rootDir": "./src",
"moduleResolution": "node", "moduleResolution": "node",
"resolveJsonModule": true, "resolveJsonModule": true,