format files
This commit is contained in:
+11
-11
@@ -1,7 +1,5 @@
|
|||||||
import { execa } from 'execa';
|
import { execa } from 'execa';
|
||||||
import {
|
import { generateCommitMessageByDiff } from '../generateCommitMessageFromGitDiff';
|
||||||
generateCommitMessageByDiff
|
|
||||||
} from '../generateCommitMessageFromGitDiff';
|
|
||||||
import {
|
import {
|
||||||
assertGitRepo,
|
assertGitRepo,
|
||||||
getChangedFiles,
|
getChangedFiles,
|
||||||
@@ -18,9 +16,7 @@ import {
|
|||||||
multiselect,
|
multiselect,
|
||||||
select
|
select
|
||||||
} from '@clack/prompts';
|
} from '@clack/prompts';
|
||||||
import {
|
import { getConfig } from '../commands/config';
|
||||||
getConfig
|
|
||||||
} from '../commands/config';
|
|
||||||
import chalk from 'chalk';
|
import chalk from 'chalk';
|
||||||
import { trytm } from '../utils/trytm';
|
import { trytm } from '../utils/trytm';
|
||||||
|
|
||||||
@@ -32,9 +28,10 @@ const getGitRemotes = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Check for the presence of message templates
|
// Check for the presence of message templates
|
||||||
const checkMessageTemplate = (extraArgs: string[]): string | false => {
|
const checkMessageTemplate = (extraArgs: string[]): string | false => {
|
||||||
for(const key in extraArgs){
|
for (const key in extraArgs) {
|
||||||
if(extraArgs[key].includes(config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER)) return extraArgs[key];
|
if (extraArgs[key].includes(config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER))
|
||||||
|
return extraArgs[key];
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
@@ -51,8 +48,11 @@ const generateCommitMessageFromGitDiff = async (
|
|||||||
try {
|
try {
|
||||||
let commitMessage = await generateCommitMessageByDiff(diff);
|
let commitMessage = await generateCommitMessageByDiff(diff);
|
||||||
|
|
||||||
if(typeof messageTemplate === 'string'){
|
if (typeof messageTemplate === 'string') {
|
||||||
commitMessage = messageTemplate.replace(config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER, commitMessage);
|
commitMessage = messageTemplate.replace(
|
||||||
|
config?.OCO_MESSAGE_TEMPLATE_PLACEHOLDER,
|
||||||
|
commitMessage
|
||||||
|
);
|
||||||
}
|
}
|
||||||
commitSpinner.stop('📝 Commit message generated');
|
commitSpinner.stop('📝 Commit message generated');
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +1,28 @@
|
|||||||
import {
|
import {
|
||||||
ChatCompletionRequestMessage,
|
ChatCompletionRequestMessage,
|
||||||
ChatCompletionRequestMessageRoleEnum
|
ChatCompletionRequestMessageRoleEnum
|
||||||
} from 'openai';
|
} from 'openai';
|
||||||
import {api} from './api';
|
import { api } from './api';
|
||||||
import {DEFAULT_MODEL_TOKEN_LIMIT, getConfig} from './commands/config';
|
import { DEFAULT_MODEL_TOKEN_LIMIT, getConfig } from './commands/config';
|
||||||
import {mergeDiffs} from './utils/mergeDiffs';
|
import { mergeDiffs } from './utils/mergeDiffs';
|
||||||
import {i18n, I18nLocals} from './i18n';
|
import { i18n, I18nLocals } from './i18n';
|
||||||
import {tokenCount} from './utils/tokenCount';
|
import { tokenCount } from './utils/tokenCount';
|
||||||
|
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
const translation = i18n[(config?.OCO_LANGUAGE as I18nLocals) || 'en'];
|
const translation = i18n[(config?.OCO_LANGUAGE as I18nLocals) || 'en'];
|
||||||
|
|
||||||
const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
|
const INIT_MESSAGES_PROMPT: Array<ChatCompletionRequestMessage> = [
|
||||||
{
|
{
|
||||||
role: ChatCompletionRequestMessageRoleEnum.System,
|
role: ChatCompletionRequestMessageRoleEnum.System,
|
||||||
// prettier-ignore
|
// 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 WHAT were the changes and WHY the changes were done. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message.
|
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 WHAT were the changes and WHY the changes were done. I'll send you an output of 'git diff --staged' command, and you convert it into a commit message.
|
||||||
${config?.OCO_EMOJI ? 'Use GitMoji convention to preface the commit.' : 'Do not preface the commit with anything.'}
|
${config?.OCO_EMOJI ? 'Use GitMoji convention to preface the commit.' : 'Do not preface the commit with anything.'}
|
||||||
${config?.OCO_DESCRIPTION ? 'Add a short description of WHY the changes are 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."}
|
${config?.OCO_DESCRIPTION ? 'Add a short description of WHY the changes are 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.`
|
Use the present tense. Lines must not be longer than 74 characters. Use ${translation.localLanguage} to answer.`
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: ChatCompletionRequestMessageRoleEnum.User,
|
role: ChatCompletionRequestMessageRoleEnum.User,
|
||||||
content: `diff --git a/src/server.ts b/src/server.ts
|
content: `diff --git a/src/server.ts b/src/server.ts
|
||||||
index ad4db42..f3b18a9 100644
|
index ad4db42..f3b18a9 100644
|
||||||
--- a/src/server.ts
|
--- a/src/server.ts
|
||||||
+++ b/src/server.ts
|
+++ b/src/server.ts
|
||||||
@@ -46,183 +46,182 @@ app.use((_, res, next) => {
|
|||||||
+app.listen(process.env.PORT || PORT, () => {
|
+app.listen(process.env.PORT || PORT, () => {
|
||||||
+ console.log(\`Server listening on port \${PORT}\`);
|
+ console.log(\`Server listening on port \${PORT}\`);
|
||||||
});`
|
});`
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
role: ChatCompletionRequestMessageRoleEnum.Assistant,
|
role: ChatCompletionRequestMessageRoleEnum.Assistant,
|
||||||
content: `${config?.OCO_EMOJI ? '🐛 ' : ''}${translation.commitFix}
|
content: `${config?.OCO_EMOJI ? '🐛 ' : ''}${translation.commitFix}
|
||||||
${config?.OCO_EMOJI ? '✨ ' : ''}${translation.commitFeat}
|
${config?.OCO_EMOJI ? '✨ ' : ''}${translation.commitFeat}
|
||||||
${config?.OCO_DESCRIPTION ? translation.commitDescription : ''}`
|
${config?.OCO_DESCRIPTION ? translation.commitDescription : ''}`
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const generateCommitMessageChatCompletionPrompt = (
|
const generateCommitMessageChatCompletionPrompt = (
|
||||||
diff: string
|
diff: string
|
||||||
): Array<ChatCompletionRequestMessage> => {
|
): Array<ChatCompletionRequestMessage> => {
|
||||||
const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT];
|
const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT];
|
||||||
|
|
||||||
chatContextAsCompletionRequest.push({
|
chatContextAsCompletionRequest.push({
|
||||||
role: ChatCompletionRequestMessageRoleEnum.User,
|
role: ChatCompletionRequestMessageRoleEnum.User,
|
||||||
content: diff
|
content: diff
|
||||||
});
|
});
|
||||||
|
|
||||||
return chatContextAsCompletionRequest;
|
return chatContextAsCompletionRequest;
|
||||||
};
|
};
|
||||||
|
|
||||||
export enum GenerateCommitMessageErrorEnum {
|
export enum GenerateCommitMessageErrorEnum {
|
||||||
tooMuchTokens = 'TOO_MUCH_TOKENS',
|
tooMuchTokens = 'TOO_MUCH_TOKENS',
|
||||||
internalError = 'INTERNAL_ERROR',
|
internalError = 'INTERNAL_ERROR',
|
||||||
emptyMessage = 'EMPTY_MESSAGE'
|
emptyMessage = 'EMPTY_MESSAGE'
|
||||||
}
|
}
|
||||||
|
|
||||||
const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map(
|
const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map(
|
||||||
(msg) => tokenCount(msg.content) + 4
|
(msg) => tokenCount(msg.content) + 4
|
||||||
).reduce((a, b) => a + b, 0);
|
).reduce((a, b) => a + b, 0);
|
||||||
|
|
||||||
const ADJUSTMENT_FACTOR = 20;
|
const ADJUSTMENT_FACTOR = 20;
|
||||||
|
|
||||||
export const generateCommitMessageByDiff = async (
|
export const generateCommitMessageByDiff = async (
|
||||||
diff: string
|
diff: string
|
||||||
): Promise<string> => {
|
): Promise<string> => {
|
||||||
try {
|
try {
|
||||||
const MAX_REQUEST_TOKENS = DEFAULT_MODEL_TOKEN_LIMIT
|
const MAX_REQUEST_TOKENS =
|
||||||
- ADJUSTMENT_FACTOR
|
DEFAULT_MODEL_TOKEN_LIMIT -
|
||||||
- INIT_MESSAGES_PROMPT_LENGTH
|
ADJUSTMENT_FACTOR -
|
||||||
- config?.OCO_OPENAI_MAX_TOKENS;
|
INIT_MESSAGES_PROMPT_LENGTH -
|
||||||
|
config?.OCO_OPENAI_MAX_TOKENS;
|
||||||
|
|
||||||
if (tokenCount(diff) >= MAX_REQUEST_TOKENS) {
|
if (tokenCount(diff) >= MAX_REQUEST_TOKENS) {
|
||||||
const commitMessagePromises = getCommitMsgsPromisesFromFileDiffs(
|
const commitMessagePromises = getCommitMsgsPromisesFromFileDiffs(
|
||||||
diff,
|
diff,
|
||||||
MAX_REQUEST_TOKENS
|
MAX_REQUEST_TOKENS
|
||||||
);
|
);
|
||||||
|
|
||||||
const commitMessages = [];
|
const commitMessages = [];
|
||||||
for (const promise of commitMessagePromises) {
|
for (const promise of commitMessagePromises) {
|
||||||
commitMessages.push(await promise);
|
commitMessages.push(await promise);
|
||||||
await delay(2000);
|
await delay(2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
return commitMessages.join('\n\n');
|
return commitMessages.join('\n\n');
|
||||||
} else {
|
} else {
|
||||||
const messages = generateCommitMessageChatCompletionPrompt(diff);
|
const messages = generateCommitMessageChatCompletionPrompt(diff);
|
||||||
|
|
||||||
const commitMessage = await api.generateCommitMessage(messages);
|
const commitMessage = await api.generateCommitMessage(messages);
|
||||||
|
|
||||||
if (!commitMessage)
|
if (!commitMessage)
|
||||||
throw new Error(GenerateCommitMessageErrorEnum.emptyMessage);
|
throw new Error(GenerateCommitMessageErrorEnum.emptyMessage);
|
||||||
|
|
||||||
return commitMessage;
|
return commitMessage;
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function getMessagesPromisesByChangesInFile(
|
function getMessagesPromisesByChangesInFile(
|
||||||
fileDiff: string,
|
fileDiff: string,
|
||||||
separator: string,
|
separator: string,
|
||||||
maxChangeLength: number
|
maxChangeLength: number
|
||||||
) {
|
) {
|
||||||
const hunkHeaderSeparator = '@@ ';
|
const hunkHeaderSeparator = '@@ ';
|
||||||
const [fileHeader, ...fileDiffByLines] = fileDiff.split(hunkHeaderSeparator);
|
const [fileHeader, ...fileDiffByLines] = fileDiff.split(hunkHeaderSeparator);
|
||||||
|
|
||||||
// merge multiple line-diffs into 1 to save tokens
|
// merge multiple line-diffs into 1 to save tokens
|
||||||
const mergedChanges = mergeDiffs(
|
const mergedChanges = mergeDiffs(
|
||||||
fileDiffByLines.map((line) => hunkHeaderSeparator + line),
|
fileDiffByLines.map((line) => hunkHeaderSeparator + line),
|
||||||
maxChangeLength
|
maxChangeLength
|
||||||
|
);
|
||||||
|
|
||||||
|
const lineDiffsWithHeader = [];
|
||||||
|
for (const change of mergedChanges) {
|
||||||
|
const totalChange = fileHeader + change;
|
||||||
|
if (tokenCount(totalChange) > maxChangeLength) {
|
||||||
|
// If the totalChange is too large, split it into smaller pieces
|
||||||
|
const splitChanges = splitDiff(totalChange, maxChangeLength);
|
||||||
|
lineDiffsWithHeader.push(...splitChanges);
|
||||||
|
} else {
|
||||||
|
lineDiffsWithHeader.push(totalChange);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const commitMsgsFromFileLineDiffs = lineDiffsWithHeader.map((lineDiff) => {
|
||||||
|
const messages = generateCommitMessageChatCompletionPrompt(
|
||||||
|
separator + lineDiff
|
||||||
);
|
);
|
||||||
|
|
||||||
const lineDiffsWithHeader = [];
|
return api.generateCommitMessage(messages);
|
||||||
for (const change of mergedChanges) {
|
});
|
||||||
const totalChange = fileHeader + change;
|
|
||||||
if (tokenCount(totalChange) > maxChangeLength) {
|
|
||||||
// If the totalChange is too large, split it into smaller pieces
|
|
||||||
const splitChanges = splitDiff(totalChange, maxChangeLength);
|
|
||||||
lineDiffsWithHeader.push(...splitChanges);
|
|
||||||
} else {
|
|
||||||
lineDiffsWithHeader.push(totalChange);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const commitMsgsFromFileLineDiffs = lineDiffsWithHeader.map((lineDiff) => {
|
return commitMsgsFromFileLineDiffs;
|
||||||
const messages = generateCommitMessageChatCompletionPrompt(
|
|
||||||
separator + lineDiff
|
|
||||||
);
|
|
||||||
|
|
||||||
return api.generateCommitMessage(messages);
|
|
||||||
});
|
|
||||||
|
|
||||||
return commitMsgsFromFileLineDiffs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function splitDiff(diff: string, maxChangeLength: number) {
|
function splitDiff(diff: string, maxChangeLength: number) {
|
||||||
const lines = diff.split('\n');
|
const lines = diff.split('\n');
|
||||||
const splitDiffs = [];
|
const splitDiffs = [];
|
||||||
let currentDiff = '';
|
let currentDiff = '';
|
||||||
|
|
||||||
for (let line of lines) {
|
for (let line of lines) {
|
||||||
// If a single line exceeds maxChangeLength, split it into multiple lines
|
// If a single line exceeds maxChangeLength, split it into multiple lines
|
||||||
while (tokenCount(line) > maxChangeLength) {
|
while (tokenCount(line) > maxChangeLength) {
|
||||||
const subLine = line.substring(0, maxChangeLength);
|
const subLine = line.substring(0, maxChangeLength);
|
||||||
line = line.substring(maxChangeLength);
|
line = line.substring(maxChangeLength);
|
||||||
splitDiffs.push(subLine);
|
splitDiffs.push(subLine);
|
||||||
}
|
|
||||||
|
|
||||||
// Check the tokenCount of the currentDiff and the line separately
|
|
||||||
if (tokenCount(currentDiff) + tokenCount('\n' + line) > maxChangeLength) {
|
|
||||||
// If adding the next line would exceed the maxChangeLength, start a new diff
|
|
||||||
splitDiffs.push(currentDiff);
|
|
||||||
currentDiff = line;
|
|
||||||
} else {
|
|
||||||
// Otherwise, add the line to the current diff
|
|
||||||
currentDiff += '\n' + line;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the last diff
|
// Check the tokenCount of the currentDiff and the line separately
|
||||||
if (currentDiff) {
|
if (tokenCount(currentDiff) + tokenCount('\n' + line) > maxChangeLength) {
|
||||||
splitDiffs.push(currentDiff);
|
// If adding the next line would exceed the maxChangeLength, start a new diff
|
||||||
|
splitDiffs.push(currentDiff);
|
||||||
|
currentDiff = line;
|
||||||
|
} else {
|
||||||
|
// Otherwise, add the line to the current diff
|
||||||
|
currentDiff += '\n' + line;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return splitDiffs;
|
// Add the last diff
|
||||||
|
if (currentDiff) {
|
||||||
|
splitDiffs.push(currentDiff);
|
||||||
|
}
|
||||||
|
|
||||||
|
return splitDiffs;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCommitMsgsPromisesFromFileDiffs(
|
export function getCommitMsgsPromisesFromFileDiffs(
|
||||||
diff: string,
|
diff: string,
|
||||||
maxDiffLength: number
|
maxDiffLength: number
|
||||||
) {
|
) {
|
||||||
const separator = 'diff --git ';
|
const separator = 'diff --git ';
|
||||||
|
|
||||||
const diffByFiles = diff.split(separator).slice(1);
|
const diffByFiles = diff.split(separator).slice(1);
|
||||||
|
|
||||||
// merge multiple files-diffs into 1 prompt to save tokens
|
// merge multiple files-diffs into 1 prompt to save tokens
|
||||||
const mergedFilesDiffs = mergeDiffs(diffByFiles, maxDiffLength);
|
const mergedFilesDiffs = mergeDiffs(diffByFiles, maxDiffLength);
|
||||||
|
|
||||||
const commitMessagePromises = [];
|
const commitMessagePromises = [];
|
||||||
|
|
||||||
for (const fileDiff of mergedFilesDiffs) {
|
for (const fileDiff of mergedFilesDiffs) {
|
||||||
if (tokenCount(fileDiff) >= maxDiffLength) {
|
if (tokenCount(fileDiff) >= maxDiffLength) {
|
||||||
// if file-diff is bigger than gpt context — split fileDiff into lineDiff
|
// if file-diff is bigger than gpt context — split fileDiff into lineDiff
|
||||||
const messagesPromises = getMessagesPromisesByChangesInFile(
|
const messagesPromises = getMessagesPromisesByChangesInFile(
|
||||||
fileDiff,
|
fileDiff,
|
||||||
separator,
|
separator,
|
||||||
maxDiffLength
|
maxDiffLength
|
||||||
);
|
);
|
||||||
|
|
||||||
commitMessagePromises.push(...messagesPromises);
|
commitMessagePromises.push(...messagesPromises);
|
||||||
} else {
|
} else {
|
||||||
const messages = generateCommitMessageChatCompletionPrompt(
|
const messages = generateCommitMessageChatCompletionPrompt(
|
||||||
separator + fileDiff
|
separator + fileDiff
|
||||||
);
|
);
|
||||||
|
|
||||||
commitMessagePromises.push(api.generateCommitMessage(messages));
|
commitMessagePromises.push(api.generateCommitMessage(messages));
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return commitMessagePromises;
|
||||||
return commitMessagePromises;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function delay(ms: number) {
|
function delay(ms: number) {
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -36,7 +36,7 @@ export enum I18nLocals {
|
|||||||
'id_ID' = 'id_ID',
|
'id_ID' = 'id_ID',
|
||||||
'pl' = 'pl',
|
'pl' = 'pl',
|
||||||
'tr' = 'tr',
|
'tr' = 'tr',
|
||||||
'th' = 'th',
|
'th' = 'th'
|
||||||
}
|
}
|
||||||
|
|
||||||
export const i18n = {
|
export const i18n = {
|
||||||
|
|||||||
Reference in New Issue
Block a user