From 42029fff4e48b9146087503a064b22e49d332ac5 Mon Sep 17 00:00:00 2001 From: SOV710 Date: Sun, 5 Apr 2026 03:39:40 +0000 Subject: [PATCH] refactor(prompts): null-safe, trim-aware user context handling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 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. --- src/prompts.ts | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/prompts.ts b/src/prompts.ts index a080804..8d831ad 100644 --- a/src/prompts.ts +++ b/src/prompts.ts @@ -123,36 +123,36 @@ const getScopeInstruction = () => * $ 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}\nConsider this context when generating the commit message, incorporating relevant information when appropriate.`; +const userInputCodeContext = (context: string | undefined | null) => { + const trimmed = (context ?? '').trim(); + if (trimmed === '') { + return ''; } - return ''; + return `Additional context provided by the user: ${trimmed}\nConsider this context when generating the commit message, incorporating relevant information when appropriate.`; }; const INIT_MAIN_PROMPT = ( language: string, fullGitMojiSpec: boolean, context: string -): OpenAI.Chat.Completions.ChatCompletionMessageParam => ({ - role: 'system', - content: (() => { - const commitConvention = fullGitMojiSpec - ? 'GitMoji specification' - : 'Conventional Commit Convention'; - 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.`; - const diffInstruction = - "I'll send you an output of 'git diff --staged' command, and you are to convert it into a commit message."; - const conventionGuidelines = getCommitConvention(fullGitMojiSpec); - const descriptionGuideline = getDescriptionInstruction(); - const oneLineCommitGuideline = getOneLineCommitInstruction(); - const scopeInstruction = getScopeInstruction(); - const generalGuidelines = `Use the present tense. Lines must not be longer than 74 characters. Use ${language} for the commit message.`; - const userInputContext = userInputCodeContext(context); +): OpenAI.Chat.Completions.ChatCompletionMessageParam => { + const commitConvention = fullGitMojiSpec + ? 'GitMoji specification' + : 'Conventional Commit Convention'; + 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.`; + const diffInstruction = + "I'll send you an output of 'git diff --staged' command, and you are to convert it into a commit message."; + const conventionGuidelines = getCommitConvention(fullGitMojiSpec); + const descriptionGuideline = getDescriptionInstruction(); + const oneLineCommitGuideline = getOneLineCommitInstruction(); + const scopeInstruction = getScopeInstruction(); + 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${scopeInstruction}\n${generalGuidelines}\n${userInputContext}`; - })() -}); + const content = `${missionStatement}\n${diffInstruction}\n${conventionGuidelines}\n${descriptionGuideline}\n${oneLineCommitGuideline}\n${scopeInstruction}\n${generalGuidelines}\n${userInputContext}`; + + return { role: 'system', content }; +}; export const INIT_DIFF_PROMPT: OpenAI.Chat.Completions.ChatCompletionMessageParam = {