Skip to content

Commit 42029ff

Browse files
committed
refactor(prompts): null-safe, trim-aware user context handling
The previous userInputCodeContext only skipped the context block when context was exactly '' or ' '. Anything else (e.g. a string of whitespace, null, undefined) would inject an empty or whitespace-only <context>…</context> tag into the system prompt. Trim the input and guard against null/undefined: - accept string | undefined | null - normalize via `(context ?? '').trim()` - skip the injection whenever the trimmed value is empty Also inline the INIT_MAIN_PROMPT IIFE into a normal function body and introduce a `content` local, removing a layer of nesting that obscured the prompt assembly. Behavior is unchanged.
1 parent 4d767da commit 42029ff

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/prompts.ts

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -123,36 +123,36 @@ const getScopeInstruction = () =>
123123
* $ oco -- This is a context used to generate the commit message
124124
* @returns - The context of the user input
125125
*/
126-
const userInputCodeContext = (context: string) => {
127-
if (context !== '' && context !== ' ') {
128-
return `Additional context provided by the user: <context>${context}</context>\nConsider this context when generating the commit message, incorporating relevant information when appropriate.`;
126+
const userInputCodeContext = (context: string | undefined | null) => {
127+
const trimmed = (context ?? '').trim();
128+
if (trimmed === '') {
129+
return '';
129130
}
130-
return '';
131+
return `Additional context provided by the user: <context>${trimmed}</context>\nConsider this context when generating the commit message, incorporating relevant information when appropriate.`;
131132
};
132133

133134
const INIT_MAIN_PROMPT = (
134135
language: string,
135136
fullGitMojiSpec: boolean,
136137
context: string
137-
): OpenAI.Chat.Completions.ChatCompletionMessageParam => ({
138-
role: 'system',
139-
content: (() => {
140-
const commitConvention = fullGitMojiSpec
141-
? 'GitMoji specification'
142-
: 'Conventional Commit Convention';
143-
const missionStatement = `${IDENTITY} Your mission is to create clean and comprehensive commit messages as per the ${commitConvention} and explain WHAT were the changes and mainly WHY the changes were done.`;
144-
const diffInstruction =
145-
"I'll send you an output of 'git diff --staged' command, and you are to convert it into a commit message.";
146-
const conventionGuidelines = getCommitConvention(fullGitMojiSpec);
147-
const descriptionGuideline = getDescriptionInstruction();
148-
const oneLineCommitGuideline = getOneLineCommitInstruction();
149-
const scopeInstruction = getScopeInstruction();
150-
const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`;
151-
const userInputContext = userInputCodeContext(context);
138+
): OpenAI.Chat.Completions.ChatCompletionMessageParam => {
139+
const commitConvention = fullGitMojiSpec
140+
? 'GitMoji specification'
141+
: 'Conventional Commit Convention';
142+
const missionStatement = `${IDENTITY} Your mission is to create clean and comprehensive commit messages as per the ${commitConvention} and explain WHAT were the changes and mainly WHY the changes were done.`;
143+
const diffInstruction =
144+
"I'll send you an output of 'git diff --staged' command, and you are to convert it into a commit message.";
145+
const conventionGuidelines = getCommitConvention(fullGitMojiSpec);
146+
const descriptionGuideline = getDescriptionInstruction();
147+
const oneLineCommitGuideline = getOneLineCommitInstruction();
148+
const scopeInstruction = getScopeInstruction();
149+
const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`;
150+
const userInputContext = userInputCodeContext(context);
152151

153-
return `${missionStatement}\n${diffInstruction}\n${conventionGuidelines}\n${descriptionGuideline}\n${oneLineCommitGuideline}\n${scopeInstruction}\n${generalGuidelines}\n${userInputContext}`;
154-
})()
155-
});
152+
const content = `${missionStatement}\n${diffInstruction}\n${conventionGuidelines}\n${descriptionGuideline}\n${oneLineCommitGuideline}\n${scopeInstruction}\n${generalGuidelines}\n${userInputContext}`;
153+
154+
return { role: 'system', content };
155+
};
156156

157157
export const INIT_DIFF_PROMPT: OpenAI.Chat.Completions.ChatCompletionMessageParam =
158158
{

0 commit comments

Comments
 (0)