mirror of
https://github.com/di-sukharev/opencommit.git
synced 2026-04-20 03:02:51 -04:00
* 🐛 fix(generateCommitMessageFromGitDiff.ts): add length of initial messages prompt to check for maximum request tokens
* ✨ feat(generateCommitMessageFromGitDiff.ts): add support for generating commit messages with chat completion
The length of the initial messages prompt is now added to the check for maximum request tokens to ensure that the request does not exceed the maximum allowed tokens. Support for generating commit messages with chat completion has been added, which allows for more efficient and streamlined commit message generation.
This commit is contained in:
@@ -77,13 +77,17 @@ interface GenerateCommitMessageError {
|
||||
error: GenerateCommitMessageErrorEnum;
|
||||
}
|
||||
|
||||
const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map(
|
||||
(msg) => msg.content
|
||||
).join('').length;
|
||||
|
||||
export const generateCommitMessageWithChatCompletion = async (
|
||||
diff: string
|
||||
): Promise<string | GenerateCommitMessageError> => {
|
||||
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 diffByFiles = diff.split(separator).slice(1);
|
||||
@@ -91,11 +95,13 @@ export const generateCommitMessageWithChatCompletion = async (
|
||||
const commitMessages = [];
|
||||
|
||||
for (const diffFile of diffByFiles) {
|
||||
if (diffFile.length >= MAX_REQ_TOKENS) continue;
|
||||
if (INIT_MESSAGES_PROMPT_LENGTH + diffFile.length >= MAX_REQ_TOKENS)
|
||||
continue;
|
||||
|
||||
const messages = generateCommitMessageChatCompletionPrompt(
|
||||
separator + diffFile
|
||||
);
|
||||
|
||||
const commitMessage = await api.generateCommitMessage(messages);
|
||||
|
||||
// TODO: handle this edge case
|
||||
|
||||
Reference in New Issue
Block a user