Merge branch 'master' into dev

This commit is contained in:
di-sukharev
2023-03-29 09:34:38 +08:00
7 changed files with 47 additions and 11 deletions
+9 -5
View File
@@ -6,6 +6,7 @@ import { api } from './api';
import { getConfig } from './commands/config';
import { mergeStrings } from './utils/mergeStrings';
import { i18n, I18nLocals } from './i18n';
import { tokenCount } from './utils/tokenCount';
const config = getConfig();
const translation = i18n[(config?.language as I18nLocals) || 'en'];
@@ -22,7 +23,10 @@ const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
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. Use ${translation.localLanguage} to answer.`
}.
Use the present tense.
Lines must not be longer 74 characters.
Use ${translation.localLanguage} to answer.`
},
{
role: ChatCompletionRequestMessageRoleEnum.User,
@@ -81,8 +85,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;
@@ -90,7 +94,7 @@ export const generateCommitMessageWithChatCompletion = async (
diff: string
): Promise<string | GenerateCommitMessageError> => {
try {
if (diff.length >= MAX_REQ_TOKENS) {
if (tokenCount(diff) >= MAX_REQ_TOKENS) {
const commitMessagePromises = getCommitMsgsPromisesFromFileDiffs(diff);
const commitMessages = await Promise.all(commitMessagePromises);
@@ -145,7 +149,7 @@ function getCommitMsgsPromisesFromFileDiffs(diff: string) {
const commitMessagePromises = [];
for (const fileDiff of mergedFilesDiffs) {
if (fileDiff.length >= MAX_REQ_TOKENS) {
if (tokenCount(fileDiff) >= MAX_REQ_TOKENS) {
// if file-diff is bigger than gpt context — split fileDiff into lineDiff
const messagesPromises = getMessagesPromisesByLines(fileDiff, separator);