From 2d1db36b9ddf2108207888602e9fafb3fa1fb300 Mon Sep 17 00:00:00 2001 From: hiroki osame Date: Wed, 15 Feb 2023 02:31:16 -0500 Subject: [PATCH] refactor: consolidated src to one file (#39) --- src/{aicommits.ts => cli.ts} | 70 ++++++++++++++++++------------------ src/index.ts | 6 ---- 2 files changed, 35 insertions(+), 41 deletions(-) rename src/{aicommits.ts => cli.ts} (98%) delete mode 100644 src/index.ts diff --git a/src/aicommits.ts b/src/cli.ts similarity index 98% rename from src/aicommits.ts rename to src/cli.ts index ae30934..fe19784 100644 --- a/src/aicommits.ts +++ b/src/cli.ts @@ -7,7 +7,40 @@ import fetch from 'node-fetch'; const OPENAI_KEY = process.env.OPENAI_KEY ?? process.env.OPENAI_API_KEY; -export async function main() { +async function generateCommitMessage(prompt: string) { + const payload = { + model: 'text-davinci-003', + prompt, + temperature: 0.7, + top_p: 1, + frequency_penalty: 0, + presence_penalty: 0, + max_tokens: 200, + stream: false, + n: 1, + }; + const response = await fetch('https://api.openai.com/v1/completions', { + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${OPENAI_KEY ?? ''}`, + }, + method: 'POST', + body: JSON.stringify(payload), + }); + if (response.status !== 200) { + const errorJson: any = await response.json(); + throw new Error( + `OpenAI API failed while processing the request '${errorJson?.error?.message}'`, + ); + } + + const json: any = await response.json(); + const aiCommit = json.choices[0].text; + + return aiCommit.replace(/(\r\n|\n|\r)/g, ''); +} + +(async () => { console.log(chalk.white('▲ ') + chalk.green('Welcome to AICommits!')); if (!OPENAI_KEY) { @@ -85,37 +118,4 @@ export async function main() { console.error(chalk.white('▲ ') + chalk.red(error.message)); process.exit(1); } -} - -async function generateCommitMessage(prompt: string) { - const payload = { - model: 'text-davinci-003', - prompt, - temperature: 0.7, - top_p: 1, - frequency_penalty: 0, - presence_penalty: 0, - max_tokens: 200, - stream: false, - n: 1, - }; - const response = await fetch('https://api.openai.com/v1/completions', { - headers: { - 'Content-Type': 'application/json', - Authorization: `Bearer ${OPENAI_KEY ?? ''}`, - }, - method: 'POST', - body: JSON.stringify(payload), - }); - if (response.status !== 200) { - const errorJson: any = await response.json(); - throw new Error( - `OpenAI API failed while processing the request '${errorJson?.error?.message}'`, - ); - } - - const json: any = await response.json(); - const aiCommit = json.choices[0].text; - - return aiCommit.replace(/(\r\n|\n|\r)/g, ''); -} +})(); diff --git a/src/index.ts b/src/index.ts deleted file mode 100644 index d793607..0000000 --- a/src/index.ts +++ /dev/null @@ -1,6 +0,0 @@ -#! /usr/bin/env node -import { main } from './aicommits'; - -(async () => { - await main(); -})();