|
1 | 1 | import https from 'https'; |
2 | 2 | import type { ClientRequest, IncomingMessage } from 'http'; |
3 | | -import type { ChatCompletionRequestMessage, CreateChatCompletionRequest, CreateChatCompletionResponse } from 'openai'; |
| 3 | +import type { CreateChatCompletionRequest, CreateChatCompletionResponse } from 'openai'; |
4 | 4 | import { |
5 | 5 | type TiktokenModel, |
6 | 6 | // encoding_for_model, |
7 | 7 | } from '@dqbd/tiktoken'; |
8 | 8 | import createHttpsProxyAgent from 'https-proxy-agent'; |
9 | 9 | import { KnownError } from './error.js'; |
10 | 10 | import type { CommitType } from './config.js'; |
| 11 | +import { generatePrompt } from './prompt.js'; |
11 | 12 |
|
12 | 13 | const httpsPost = async ( |
13 | 14 | hostname: string, |
@@ -104,52 +105,6 @@ const sanitizeMessage = (message: string) => message.trim().replace(/[\n\r]/g, ' |
104 | 105 |
|
105 | 106 | const deduplicateMessages = (array: string[]) => Array.from(new Set(array)); |
106 | 107 |
|
107 | | -const getBasePrompt = ( |
108 | | - locale: string, |
109 | | - maxLength: number, |
110 | | -) => `${[ |
111 | | - 'Generate a concise git commit message written in present tense for the following code diff with the given specifications below:', |
112 | | - `Message language: ${locale}`, |
113 | | - `Commit message must be a maximum of ${maxLength} characters.`, |
114 | | - 'Exclude anything unnecessary such as translation. Your entire response will be passed directly into git commit.', |
115 | | -].join('\n')}`; |
116 | | - |
117 | | -const getCommitMessageFormatOutputExample = (type: CommitType) => `The output response must be in format:\n${getCommitMessageFormat(type)}`; |
118 | | - |
119 | | -const getCommitMessageFormat = (type: CommitType) => { |
120 | | - if (type === 'conventional') { |
121 | | - return '<type>(<optional scope>): <commit message>'; |
122 | | - } |
123 | | - |
124 | | - return '<commit message>'; |
125 | | -}; |
126 | | - |
127 | | -/** |
128 | | - * References: |
129 | | - * Commitlint: |
130 | | - * https://github.com/conventional-changelog/commitlint/blob/18fbed7ea86ac0ec9d5449b4979b762ec4305a92/%40commitlint/config-conventional/index.js#L40-L100 |
131 | | - * |
132 | | - * Conventional Changelog: |
133 | | - * https://github.com/conventional-changelog/conventional-changelog/blob/d0e5d5926c8addba74bc962553dd8bcfba90e228/packages/conventional-changelog-conventionalcommits/writer-opts.js#L182-L193 |
134 | | - */ |
135 | | -const getExtraContextForConventionalCommits = () => ( |
136 | | - `Choose a type from the type-to-description JSON below that best describes the git diff:\n${ |
137 | | - JSON.stringify({ |
138 | | - docs: 'Documentation only changes', |
139 | | - style: 'Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)', |
140 | | - refactor: 'A code change that neither fixes a bug nor adds a feature', |
141 | | - perf: 'A code change that improves performance', |
142 | | - test: 'Adding missing tests or correcting existing tests', |
143 | | - build: 'Changes that affect the build system or external dependencies', |
144 | | - ci: 'Changes to our CI configuration files and scripts', |
145 | | - chore: "Other changes that don't modify src or test files", |
146 | | - revert: 'Reverts a previous commit', |
147 | | - feat: 'A new feature', |
148 | | - fix: 'A bug fix', |
149 | | - }, null, 2) |
150 | | - }` |
151 | | -); |
152 | | - |
153 | 108 | // const generateStringFromLength = (length: number) => { |
154 | 109 | // let result = ''; |
155 | 110 | // const highestTokenChar = 'z'; |
@@ -178,31 +133,21 @@ export const generateCommitMessage = async ( |
178 | 133 | timeout: number, |
179 | 134 | proxy?: string, |
180 | 135 | ) => { |
181 | | - const prompt = getBasePrompt(locale, maxLength); |
182 | | - |
183 | | - const conventionalCommitsExtraContext = type === 'conventional' |
184 | | - ? getExtraContextForConventionalCommits() |
185 | | - : ''; |
186 | | - |
187 | | - const commitMessageFormatOutputExample = getCommitMessageFormatOutputExample(type); |
188 | | - |
189 | | - const messages: ChatCompletionRequestMessage[] = [ |
190 | | - { |
191 | | - role: 'system', |
192 | | - content: `${prompt}\n${conventionalCommitsExtraContext}\n${commitMessageFormatOutputExample}`, |
193 | | - }, |
194 | | - { |
195 | | - role: 'user', |
196 | | - content: diff, |
197 | | - }, |
198 | | - ]; |
199 | | - |
200 | 136 | try { |
201 | 137 | const completion = await createChatCompletion( |
202 | 138 | apiKey, |
203 | 139 | { |
204 | 140 | model, |
205 | | - messages, |
| 141 | + messages: [ |
| 142 | + { |
| 143 | + role: 'system', |
| 144 | + content: generatePrompt(locale, maxLength, type), |
| 145 | + }, |
| 146 | + { |
| 147 | + role: 'user', |
| 148 | + content: diff, |
| 149 | + }, |
| 150 | + ], |
206 | 151 | temperature: 0.7, |
207 | 152 | top_p: 1, |
208 | 153 | frequency_penalty: 0, |
|
0 commit comments