Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions src/commands/aicommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { KnownError, handleCliError } from '../utils/error.js';

export default async (
generate: number,
prompt: string,
rawArgv: string[],
) => (async () => {
intro(bgCyan(black(' aicommits ')));
Expand Down Expand Up @@ -46,9 +47,22 @@ export default async (
OPENAI_KEY,
staged.diff,
generate,
prompt,
);
s.stop('Changes analyzed');

if (!prompt) {
commitMessageProcess(messages, rawArgv);
} else {
customizedProcess(messages);
}
})().catch((error) => {
outro(`${red('✖')} ${error.message}`);
handleCliError(error);
process.exit(1);
});

const commitMessageProcess = async (messages: string[], rawArgv: string[]) => {
let message: string;
if (messages.length === 1) {
[message] = messages;
Expand Down Expand Up @@ -77,8 +91,9 @@ export default async (
await execa('git', ['commit', '-m', message, ...rawArgv]);

outro(`${green('✔')} Successfully committed!`);
})().catch((error) => {
outro(`${red('✖')} ${error.message}`);
handleCliError(error);
process.exit(1);
});
};

const customizedProcess = async (messages: string[]) => {
const message = messages.length === 1 ? messages[0] : messages.join('\n');
outro(`Response message:\n\n ${message}\n`);
};
3 changes: 2 additions & 1 deletion src/utils/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const sanitizeMessage = (message: string) => message.trim().replace(/[\n\r]/g, '

const deduplicateMessages = (array: string[]) => Array.from(new Set(array));

const promptTemplate = 'Write an insightful but concise Git commit message in a complete sentence in present tense for the following diff without prefacing it with anything:';
const promptDefaultTemplate = 'Write an insightful but concise Git commit message in a complete sentence in present tense for the following diff without prefacing it with anything:';

const model = 'text-davinci-003';
const encoder = encodingForModel(model);
Expand All @@ -67,6 +67,7 @@ export const generateCommitMessage = async (
apiKey: string,
diff: string,
completions: number,
promptTemplate: string = promptDefaultTemplate,
) => {
const prompt = `${promptTemplate}\n${diff}`;

Expand Down