Feat/add user input code context (#420)

* feat(cli): add context flag for providing additional commit message input
This commit is contained in:
Welington Sampaio
2024-11-20 07:47:58 -03:00
committed by GitHub
parent 0f315ae793
commit eca4083a04
6 changed files with 111 additions and 43 deletions
+36 -14
View File
@@ -44931,7 +44931,14 @@ var CONVENTIONAL_COMMIT_KEYWORDS = "Do not preface the commit with anything, exc
var getCommitConvention = (fullGitMojiSpec) => config4.OCO_EMOJI ? fullGitMojiSpec ? FULL_GITMOJI_SPEC : GITMOJI_HELP : CONVENTIONAL_COMMIT_KEYWORDS; var getCommitConvention = (fullGitMojiSpec) => config4.OCO_EMOJI ? fullGitMojiSpec ? FULL_GITMOJI_SPEC : GITMOJI_HELP : CONVENTIONAL_COMMIT_KEYWORDS;
var getDescriptionInstruction = () => config4.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."; var getDescriptionInstruction = () => config4.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.";
var getOneLineCommitInstruction = () => config4.OCO_ONE_LINE_COMMIT ? "Craft a concise commit message that encapsulates all changes made, with an emphasis on the primary updates. If the modifications share a common theme or scope, mention it succinctly; otherwise, leave the scope out to maintain focus. The goal is to provide a clear and unified overview of the changes in a one single message, without diverging into a list of commit per file change." : ""; var getOneLineCommitInstruction = () => config4.OCO_ONE_LINE_COMMIT ? "Craft a concise commit message that encapsulates all changes made, with an emphasis on the primary updates. If the modifications share a common theme or scope, mention it succinctly; otherwise, leave the scope out to maintain focus. The goal is to provide a clear and unified overview of the changes in a one single message, without diverging into a list of commit per file change." : "";
var INIT_MAIN_PROMPT2 = (language, fullGitMojiSpec) => ({ var userInputCodeContext = (context) => {
if (context !== "" && context !== " ") {
return `Additional context provided by the user: <context>${context}</context>
Consider this context when generating the commit message, incorporating relevant information when appropriate.`;
}
return "";
};
var INIT_MAIN_PROMPT2 = (language, fullGitMojiSpec, context) => ({
role: "system", role: "system",
content: (() => { content: (() => {
const commitConvention = fullGitMojiSpec ? "GitMoji specification" : "Conventional Commit Convention"; const commitConvention = fullGitMojiSpec ? "GitMoji specification" : "Conventional Commit Convention";
@@ -44941,12 +44948,14 @@ var INIT_MAIN_PROMPT2 = (language, fullGitMojiSpec) => ({
const descriptionGuideline = getDescriptionInstruction(); const descriptionGuideline = getDescriptionInstruction();
const oneLineCommitGuideline = getOneLineCommitInstruction(); const oneLineCommitGuideline = getOneLineCommitInstruction();
const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`; const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`;
const userInputContext = userInputCodeContext(context);
return `${missionStatement} return `${missionStatement}
${diffInstruction} ${diffInstruction}
${conventionGuidelines} ${conventionGuidelines}
${descriptionGuideline} ${descriptionGuideline}
${oneLineCommitGuideline} ${oneLineCommitGuideline}
${generalGuidelines}`; ${generalGuidelines}
${userInputContext}`;
})() })()
}); });
var INIT_DIFF_PROMPT = { var INIT_DIFF_PROMPT = {
@@ -44988,7 +44997,7 @@ var INIT_CONSISTENCY_PROMPT = (translation4) => ({
role: "assistant", role: "assistant",
content: getContent(translation4) content: getContent(translation4)
}); });
var getMainCommitPrompt = async (fullGitMojiSpec) => { var getMainCommitPrompt = async (fullGitMojiSpec, context) => {
switch (config4.OCO_PROMPT_MODULE) { switch (config4.OCO_PROMPT_MODULE) {
case "@commitlint": case "@commitlint":
if (!await commitlintLLMConfigExists()) { if (!await commitlintLLMConfigExists()) {
@@ -45010,7 +45019,7 @@ var getMainCommitPrompt = async (fullGitMojiSpec) => {
]; ];
default: default:
return [ return [
INIT_MAIN_PROMPT2(translation3.localLanguage, fullGitMojiSpec), INIT_MAIN_PROMPT2(translation3.localLanguage, fullGitMojiSpec, context),
INIT_DIFF_PROMPT, INIT_DIFF_PROMPT,
INIT_CONSISTENCY_PROMPT(translation3) INIT_CONSISTENCY_PROMPT(translation3)
]; ];
@@ -45037,8 +45046,8 @@ function mergeDiffs(arr, maxStringLength) {
var config5 = getConfig(); var config5 = getConfig();
var MAX_TOKENS_INPUT = config5.OCO_TOKENS_MAX_INPUT; var MAX_TOKENS_INPUT = config5.OCO_TOKENS_MAX_INPUT;
var MAX_TOKENS_OUTPUT = config5.OCO_TOKENS_MAX_OUTPUT; var MAX_TOKENS_OUTPUT = config5.OCO_TOKENS_MAX_OUTPUT;
var generateCommitMessageChatCompletionPrompt = async (diff, fullGitMojiSpec) => { var generateCommitMessageChatCompletionPrompt = async (diff, fullGitMojiSpec, context) => {
const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(fullGitMojiSpec); const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(fullGitMojiSpec, context);
const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT]; const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT];
chatContextAsCompletionRequest.push({ chatContextAsCompletionRequest.push({
role: "user", role: "user",
@@ -45054,9 +45063,12 @@ var GenerateCommitMessageErrorEnum = ((GenerateCommitMessageErrorEnum2) => {
return GenerateCommitMessageErrorEnum2; return GenerateCommitMessageErrorEnum2;
})(GenerateCommitMessageErrorEnum || {}); })(GenerateCommitMessageErrorEnum || {});
var ADJUSTMENT_FACTOR = 20; var ADJUSTMENT_FACTOR = 20;
var generateCommitMessageByDiff = async (diff, fullGitMojiSpec = false) => { var generateCommitMessageByDiff = async (diff, fullGitMojiSpec = false, context = "") => {
try { try {
const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(fullGitMojiSpec); const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(
fullGitMojiSpec,
context
);
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((a4, b7) => a4 + b7, 0); ).reduce((a4, b7) => a4 + b7, 0);
@@ -45076,7 +45088,8 @@ var generateCommitMessageByDiff = async (diff, fullGitMojiSpec = false) => {
} }
const messages = await generateCommitMessageChatCompletionPrompt( const messages = await generateCommitMessageChatCompletionPrompt(
diff, diff,
fullGitMojiSpec fullGitMojiSpec,
context
); );
const engine = getEngine(); const engine = getEngine();
const commitMessage = await engine.generateCommitMessage(messages); const commitMessage = await engine.generateCommitMessage(messages);
@@ -45283,6 +45296,7 @@ var checkMessageTemplate = (extraArgs2) => {
var generateCommitMessageFromGitDiff = async ({ var generateCommitMessageFromGitDiff = async ({
diff, diff,
extraArgs: extraArgs2, extraArgs: extraArgs2,
context = "",
fullGitMojiSpec = false, fullGitMojiSpec = false,
skipCommitConfirmation = false skipCommitConfirmation = false
}) => { }) => {
@@ -45292,7 +45306,8 @@ var generateCommitMessageFromGitDiff = async ({
try { try {
let commitMessage = await generateCommitMessageByDiff( let commitMessage = await generateCommitMessageByDiff(
diff, diff,
fullGitMojiSpec fullGitMojiSpec,
context
); );
const messageTemplate = checkMessageTemplate(extraArgs2); const messageTemplate = checkMessageTemplate(extraArgs2);
if (config6.OCO_MESSAGE_TEMPLATE_PLACEHOLDER && typeof messageTemplate === "string") { if (config6.OCO_MESSAGE_TEMPLATE_PLACEHOLDER && typeof messageTemplate === "string") {
@@ -45402,7 +45417,7 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
process.exit(1); process.exit(1);
} }
}; };
async function commit(extraArgs2 = [], isStageAllFlag = false, fullGitMojiSpec = false, skipCommitConfirmation = false) { async function commit(extraArgs2 = [], context = "", isStageAllFlag = false, fullGitMojiSpec = false, skipCommitConfirmation = false) {
if (isStageAllFlag) { if (isStageAllFlag) {
const changedFiles2 = await getChangedFiles(); const changedFiles2 = await getChangedFiles();
if (changedFiles2) if (changedFiles2)
@@ -45433,7 +45448,7 @@ async function commit(extraArgs2 = [], isStageAllFlag = false, fullGitMojiSpec =
if (hD2(isStageAllAndCommitConfirmedByUser)) if (hD2(isStageAllAndCommitConfirmedByUser))
process.exit(1); process.exit(1);
if (isStageAllAndCommitConfirmedByUser) { if (isStageAllAndCommitConfirmedByUser) {
await commit(extraArgs2, true, fullGitMojiSpec); await commit(extraArgs2, context, true, fullGitMojiSpec);
process.exit(1); process.exit(1);
} }
if (stagedFiles.length === 0 && changedFiles.length > 0) { if (stagedFiles.length === 0 && changedFiles.length > 0) {
@@ -45448,7 +45463,7 @@ async function commit(extraArgs2 = [], isStageAllFlag = false, fullGitMojiSpec =
process.exit(1); process.exit(1);
await gitAdd({ files }); await gitAdd({ files });
} }
await commit(extraArgs2, false, fullGitMojiSpec); await commit(extraArgs2, context, false, fullGitMojiSpec);
process.exit(1); process.exit(1);
} }
stagedFilesSpinner.stop( stagedFilesSpinner.stop(
@@ -45459,6 +45474,7 @@ ${stagedFiles.map((file) => ` ${file}`).join("\n")}`
generateCommitMessageFromGitDiff({ generateCommitMessageFromGitDiff({
diff: await getDiff({ files: stagedFiles }), diff: await getDiff({ files: stagedFiles }),
extraArgs: extraArgs2, extraArgs: extraArgs2,
context,
fullGitMojiSpec, fullGitMojiSpec,
skipCommitConfirmation skipCommitConfirmation
}) })
@@ -45817,6 +45833,12 @@ Z2(
commands: [configCommand, hookCommand, commitlintConfigCommand], commands: [configCommand, hookCommand, commitlintConfigCommand],
flags: { flags: {
fgm: Boolean, fgm: Boolean,
context: {
type: String,
alias: "c",
description: "Additional user input context for the commit message",
default: ""
},
yes: { yes: {
type: Boolean, type: Boolean,
alias: "y", alias: "y",
@@ -45833,7 +45855,7 @@ Z2(
if (await isHookCalled()) { if (await isHookCalled()) {
prepareCommitMessageHook(); prepareCommitMessageHook();
} else { } else {
commit(extraArgs, false, flags.fgm, flags.yes); commit(extraArgs, flags.context, false, flags.fgm, flags.yes);
} }
}, },
extraArgs extraArgs
+22 -9
View File
@@ -63732,7 +63732,14 @@ var CONVENTIONAL_COMMIT_KEYWORDS = "Do not preface the commit with anything, exc
var getCommitConvention = (fullGitMojiSpec) => config4.OCO_EMOJI ? fullGitMojiSpec ? FULL_GITMOJI_SPEC : GITMOJI_HELP : CONVENTIONAL_COMMIT_KEYWORDS; var getCommitConvention = (fullGitMojiSpec) => config4.OCO_EMOJI ? fullGitMojiSpec ? FULL_GITMOJI_SPEC : GITMOJI_HELP : CONVENTIONAL_COMMIT_KEYWORDS;
var getDescriptionInstruction = () => config4.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."; var getDescriptionInstruction = () => config4.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.";
var getOneLineCommitInstruction = () => config4.OCO_ONE_LINE_COMMIT ? "Craft a concise commit message that encapsulates all changes made, with an emphasis on the primary updates. If the modifications share a common theme or scope, mention it succinctly; otherwise, leave the scope out to maintain focus. The goal is to provide a clear and unified overview of the changes in a one single message, without diverging into a list of commit per file change." : ""; var getOneLineCommitInstruction = () => config4.OCO_ONE_LINE_COMMIT ? "Craft a concise commit message that encapsulates all changes made, with an emphasis on the primary updates. If the modifications share a common theme or scope, mention it succinctly; otherwise, leave the scope out to maintain focus. The goal is to provide a clear and unified overview of the changes in a one single message, without diverging into a list of commit per file change." : "";
var INIT_MAIN_PROMPT2 = (language, fullGitMojiSpec) => ({ var userInputCodeContext = (context2) => {
if (context2 !== "" && context2 !== " ") {
return `Additional context provided by the user: <context>${context2}</context>
Consider this context when generating the commit message, incorporating relevant information when appropriate.`;
}
return "";
};
var INIT_MAIN_PROMPT2 = (language, fullGitMojiSpec, context2) => ({
role: "system", role: "system",
content: (() => { content: (() => {
const commitConvention = fullGitMojiSpec ? "GitMoji specification" : "Conventional Commit Convention"; const commitConvention = fullGitMojiSpec ? "GitMoji specification" : "Conventional Commit Convention";
@@ -63742,12 +63749,14 @@ var INIT_MAIN_PROMPT2 = (language, fullGitMojiSpec) => ({
const descriptionGuideline = getDescriptionInstruction(); const descriptionGuideline = getDescriptionInstruction();
const oneLineCommitGuideline = getOneLineCommitInstruction(); const oneLineCommitGuideline = getOneLineCommitInstruction();
const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`; const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`;
const userInputContext = userInputCodeContext(context2);
return `${missionStatement} return `${missionStatement}
${diffInstruction} ${diffInstruction}
${conventionGuidelines} ${conventionGuidelines}
${descriptionGuideline} ${descriptionGuideline}
${oneLineCommitGuideline} ${oneLineCommitGuideline}
${generalGuidelines}`; ${generalGuidelines}
${userInputContext}`;
})() })()
}); });
var INIT_DIFF_PROMPT = { var INIT_DIFF_PROMPT = {
@@ -63789,7 +63798,7 @@ var INIT_CONSISTENCY_PROMPT = (translation4) => ({
role: "assistant", role: "assistant",
content: getContent(translation4) content: getContent(translation4)
}); });
var getMainCommitPrompt = async (fullGitMojiSpec) => { var getMainCommitPrompt = async (fullGitMojiSpec, context2) => {
switch (config4.OCO_PROMPT_MODULE) { switch (config4.OCO_PROMPT_MODULE) {
case "@commitlint": case "@commitlint":
if (!await commitlintLLMConfigExists()) { if (!await commitlintLLMConfigExists()) {
@@ -63811,7 +63820,7 @@ var getMainCommitPrompt = async (fullGitMojiSpec) => {
]; ];
default: default:
return [ return [
INIT_MAIN_PROMPT2(translation3.localLanguage, fullGitMojiSpec), INIT_MAIN_PROMPT2(translation3.localLanguage, fullGitMojiSpec, context2),
INIT_DIFF_PROMPT, INIT_DIFF_PROMPT,
INIT_CONSISTENCY_PROMPT(translation3) INIT_CONSISTENCY_PROMPT(translation3)
]; ];
@@ -63838,8 +63847,8 @@ function mergeDiffs(arr, maxStringLength) {
var config5 = getConfig(); var config5 = getConfig();
var MAX_TOKENS_INPUT = config5.OCO_TOKENS_MAX_INPUT; var MAX_TOKENS_INPUT = config5.OCO_TOKENS_MAX_INPUT;
var MAX_TOKENS_OUTPUT = config5.OCO_TOKENS_MAX_OUTPUT; var MAX_TOKENS_OUTPUT = config5.OCO_TOKENS_MAX_OUTPUT;
var generateCommitMessageChatCompletionPrompt = async (diff, fullGitMojiSpec) => { var generateCommitMessageChatCompletionPrompt = async (diff, fullGitMojiSpec, context2) => {
const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(fullGitMojiSpec); const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(fullGitMojiSpec, context2);
const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT]; const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT];
chatContextAsCompletionRequest.push({ chatContextAsCompletionRequest.push({
role: "user", role: "user",
@@ -63855,9 +63864,12 @@ var GenerateCommitMessageErrorEnum = ((GenerateCommitMessageErrorEnum2) => {
return GenerateCommitMessageErrorEnum2; return GenerateCommitMessageErrorEnum2;
})(GenerateCommitMessageErrorEnum || {}); })(GenerateCommitMessageErrorEnum || {});
var ADJUSTMENT_FACTOR = 20; var ADJUSTMENT_FACTOR = 20;
var generateCommitMessageByDiff = async (diff, fullGitMojiSpec = false) => { var generateCommitMessageByDiff = async (diff, fullGitMojiSpec = false, context2 = "") => {
try { try {
const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(fullGitMojiSpec); const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(
fullGitMojiSpec,
context2
);
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((a3, b3) => a3 + b3, 0); ).reduce((a3, b3) => a3 + b3, 0);
@@ -63877,7 +63889,8 @@ var generateCommitMessageByDiff = async (diff, fullGitMojiSpec = false) => {
} }
const messages = await generateCommitMessageChatCompletionPrompt( const messages = await generateCommitMessageChatCompletionPrompt(
diff, diff,
fullGitMojiSpec fullGitMojiSpec,
context2
); );
const engine = getEngine(); const engine = getEngine();
const commitMessage = await engine.generateCommitMessage(messages); const commitMessage = await engine.generateCommitMessage(messages);
+7 -1
View File
@@ -20,6 +20,12 @@ cli(
commands: [configCommand, hookCommand, commitlintConfigCommand], commands: [configCommand, hookCommand, commitlintConfigCommand],
flags: { flags: {
fgm: Boolean, fgm: Boolean,
context: {
type: String,
alias: 'c',
description: 'Additional user input context for the commit message',
default: ''
},
yes: { yes: {
type: Boolean, type: Boolean,
alias: 'y', alias: 'y',
@@ -37,7 +43,7 @@ cli(
if (await isHookCalled()) { if (await isHookCalled()) {
prepareCommitMessageHook(); prepareCommitMessageHook();
} else { } else {
commit(extraArgs, false, flags.fgm, flags.yes); commit(extraArgs, flags.context, false, flags.fgm, flags.yes);
} }
}, },
extraArgs extraArgs
+9 -5
View File
@@ -39,6 +39,7 @@ const checkMessageTemplate = (extraArgs: string[]): string | false => {
interface GenerateCommitMessageFromGitDiffParams { interface GenerateCommitMessageFromGitDiffParams {
diff: string; diff: string;
extraArgs: string[]; extraArgs: string[];
context?: string;
fullGitMojiSpec?: boolean; fullGitMojiSpec?: boolean;
skipCommitConfirmation?: boolean; skipCommitConfirmation?: boolean;
} }
@@ -46,6 +47,7 @@ interface GenerateCommitMessageFromGitDiffParams {
const generateCommitMessageFromGitDiff = async ({ const generateCommitMessageFromGitDiff = async ({
diff, diff,
extraArgs, extraArgs,
context = '',
fullGitMojiSpec = false, fullGitMojiSpec = false,
skipCommitConfirmation = false skipCommitConfirmation = false
}: GenerateCommitMessageFromGitDiffParams): Promise<void> => { }: GenerateCommitMessageFromGitDiffParams): Promise<void> => {
@@ -56,7 +58,8 @@ const generateCommitMessageFromGitDiff = async ({
try { try {
let commitMessage = await generateCommitMessageByDiff( let commitMessage = await generateCommitMessageByDiff(
diff, diff,
fullGitMojiSpec fullGitMojiSpec,
context
); );
const messageTemplate = checkMessageTemplate(extraArgs); const messageTemplate = checkMessageTemplate(extraArgs);
@@ -135,8 +138,7 @@ ${chalk.grey('——————————————————')}`
]); ]);
pushSpinner.stop( pushSpinner.stop(
`${chalk.green('✔')} Successfully pushed all commits to ${ `${chalk.green('✔')} Successfully pushed all commits to ${remotes[0]
remotes[0]
}` }`
); );
@@ -197,6 +199,7 @@ ${chalk.grey('——————————————————')}`
export async function commit( export async function commit(
extraArgs: string[] = [], extraArgs: string[] = [],
context: string = '',
isStageAllFlag: Boolean = false, isStageAllFlag: Boolean = false,
fullGitMojiSpec: boolean = false, fullGitMojiSpec: boolean = false,
skipCommitConfirmation: boolean = false skipCommitConfirmation: boolean = false
@@ -238,7 +241,7 @@ export async function commit(
if (isCancel(isStageAllAndCommitConfirmedByUser)) process.exit(1); if (isCancel(isStageAllAndCommitConfirmedByUser)) process.exit(1);
if (isStageAllAndCommitConfirmedByUser) { if (isStageAllAndCommitConfirmedByUser) {
await commit(extraArgs, true, fullGitMojiSpec); await commit(extraArgs, context, true, fullGitMojiSpec);
process.exit(1); process.exit(1);
} }
@@ -256,7 +259,7 @@ export async function commit(
await gitAdd({ files }); await gitAdd({ files });
} }
await commit(extraArgs, false, fullGitMojiSpec); await commit(extraArgs, context, false, fullGitMojiSpec);
process.exit(1); process.exit(1);
} }
@@ -270,6 +273,7 @@ export async function commit(
generateCommitMessageFromGitDiff({ generateCommitMessageFromGitDiff({
diff: await getDiff({ files: stagedFiles }), diff: await getDiff({ files: stagedFiles }),
extraArgs, extraArgs,
context,
fullGitMojiSpec, fullGitMojiSpec,
skipCommitConfirmation skipCommitConfirmation
}) })
+11 -5
View File
@@ -11,9 +11,10 @@ const MAX_TOKENS_OUTPUT = config.OCO_TOKENS_MAX_OUTPUT;
const generateCommitMessageChatCompletionPrompt = async ( const generateCommitMessageChatCompletionPrompt = async (
diff: string, diff: string,
fullGitMojiSpec: boolean fullGitMojiSpec: boolean,
context: string
): Promise<Array<OpenAI.Chat.Completions.ChatCompletionMessageParam>> => { ): Promise<Array<OpenAI.Chat.Completions.ChatCompletionMessageParam>> => {
const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(fullGitMojiSpec); const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(fullGitMojiSpec, context);
const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT]; const chatContextAsCompletionRequest = [...INIT_MESSAGES_PROMPT];
@@ -36,10 +37,14 @@ const ADJUSTMENT_FACTOR = 20;
export const generateCommitMessageByDiff = async ( export const generateCommitMessageByDiff = async (
diff: string, diff: string,
fullGitMojiSpec: boolean = false fullGitMojiSpec: boolean = false,
context: string = ""
): Promise<string> => { ): Promise<string> => {
try { try {
const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(fullGitMojiSpec); const INIT_MESSAGES_PROMPT = await getMainCommitPrompt(
fullGitMojiSpec,
context
);
const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map( const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map(
(msg) => tokenCount(msg.content as string) + 4 (msg) => tokenCount(msg.content as string) + 4
@@ -69,7 +74,8 @@ export const generateCommitMessageByDiff = async (
const messages = await generateCommitMessageChatCompletionPrompt( const messages = await generateCommitMessageChatCompletionPrompt(
diff, diff,
fullGitMojiSpec fullGitMojiSpec,
context,
); );
const engine = getEngine(); const engine = getEngine();
+26 -9
View File
@@ -111,9 +111,24 @@ const getOneLineCommitInstruction = () =>
? 'Craft a concise commit message that encapsulates all changes made, with an emphasis on the primary updates. If the modifications share a common theme or scope, mention it succinctly; otherwise, leave the scope out to maintain focus. The goal is to provide a clear and unified overview of the changes in a one single message, without diverging into a list of commit per file change.' ? 'Craft a concise commit message that encapsulates all changes made, with an emphasis on the primary updates. If the modifications share a common theme or scope, mention it succinctly; otherwise, leave the scope out to maintain focus. The goal is to provide a clear and unified overview of the changes in a one single message, without diverging into a list of commit per file change.'
: ''; : '';
/**
* Get the context of the user input
* @param extraArgs - The arguments passed to the command line
* @example
* $ oco -- This is a context used to generate the commit message
* @returns - The context of the user input
*/
const userInputCodeContext = (context: string) => {
if (context !== '' && context !== ' ') {
return `Additional context provided by the user: <context>${context}</context>\nConsider this context when generating the commit message, incorporating relevant information when appropriate.`;
}
return '';
};
const INIT_MAIN_PROMPT = ( const INIT_MAIN_PROMPT = (
language: string, language: string,
fullGitMojiSpec: boolean fullGitMojiSpec: boolean,
context: string
): OpenAI.Chat.Completions.ChatCompletionMessageParam => ({ ): OpenAI.Chat.Completions.ChatCompletionMessageParam => ({
role: 'system', role: 'system',
content: (() => { content: (() => {
@@ -127,15 +142,16 @@ const INIT_MAIN_PROMPT = (
const descriptionGuideline = getDescriptionInstruction(); const descriptionGuideline = getDescriptionInstruction();
const oneLineCommitGuideline = getOneLineCommitInstruction(); const oneLineCommitGuideline = getOneLineCommitInstruction();
const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`; const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`;
const userInputContext = userInputCodeContext(context);
return `${missionStatement}\n${diffInstruction}\n${conventionGuidelines}\n${descriptionGuideline}\n${oneLineCommitGuideline}\n${generalGuidelines}`; return `${missionStatement}\n${diffInstruction}\n${conventionGuidelines}\n${descriptionGuideline}\n${oneLineCommitGuideline}\n${generalGuidelines}\n${userInputContext}`;
})() })()
}); });
export const INIT_DIFF_PROMPT: OpenAI.Chat.Completions.ChatCompletionMessageParam = export const INIT_DIFF_PROMPT: OpenAI.Chat.Completions.ChatCompletionMessageParam =
{ {
role: 'user', role: '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
@@ -159,7 +175,7 @@ export const INIT_DIFF_PROMPT: OpenAI.Chat.Completions.ChatCompletionMessagePara
+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}\`);
});` });`
}; };
const getContent = (translation: ConsistencyPrompt) => { const getContent = (translation: ConsistencyPrompt) => {
const fix = config.OCO_EMOJI const fix = config.OCO_EMOJI
@@ -185,7 +201,8 @@ const INIT_CONSISTENCY_PROMPT = (
}); });
export const getMainCommitPrompt = async ( export const getMainCommitPrompt = async (
fullGitMojiSpec: boolean fullGitMojiSpec: boolean,
context: string
): Promise<Array<OpenAI.Chat.Completions.ChatCompletionMessageParam>> => { ): Promise<Array<OpenAI.Chat.Completions.ChatCompletionMessageParam>> => {
switch (config.OCO_PROMPT_MODULE) { switch (config.OCO_PROMPT_MODULE) {
case '@commitlint': case '@commitlint':
@@ -207,14 +224,14 @@ export const getMainCommitPrompt = async (
INIT_DIFF_PROMPT, INIT_DIFF_PROMPT,
INIT_CONSISTENCY_PROMPT( INIT_CONSISTENCY_PROMPT(
commitLintConfig.consistency[ commitLintConfig.consistency[
translation.localLanguage translation.localLanguage
] as ConsistencyPrompt ] as ConsistencyPrompt
) )
]; ];
default: default:
return [ return [
INIT_MAIN_PROMPT(translation.localLanguage, fullGitMojiSpec), INIT_MAIN_PROMPT(translation.localLanguage, fullGitMojiSpec, context),
INIT_DIFF_PROMPT, INIT_DIFF_PROMPT,
INIT_CONSISTENCY_PROMPT(translation) INIT_CONSISTENCY_PROMPT(translation)
]; ];