From b0a2c1a85802cd45dd685e1bee760006a41ad90b Mon Sep 17 00:00:00 2001 From: awwaawwa <8493196+awwaawwa@users.noreply.github.com> Date: Thu, 25 May 2023 20:09:31 +0800 Subject: [PATCH 1/2] * chore(package.json): update version from 2.1.1 to 2.1.2 * chore(package.json): add configuration autocommit.openAI.apiUrl * feat(autocommit): add support for openAI.apiUrl configuration --- package.json | 9 +++++++-- src/autocommit/index.ts | 4 +++- src/autocommit/utils/openai.ts | 2 ++ src/extension.ts | 33 ++++++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 564f41b..ac6af34 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "auto-commit-gpt", "displayName": "Auto Commit with GPT", "description": "Generate commit messages using ChatGPT", - "version": "2.1.1", + "version": "2.1.2", "homepage": "https://github.com/sohanemon/AutoCommit#readme", "bugs": "https://github.com/sohanemon/AutoCommit/issues", "repository": { @@ -57,6 +57,11 @@ "default": "", "description": "Needed for generating AI commit messages" }, + "autocommit.openAI.apiUrl": { + "type": "string", + "default": "https://api.openai.com", + "description": "Required for generating AI commit messages. Should start with \"https://\"" + }, "autocommit.appearance.delimeter": { "type": "string", "default": "*", @@ -90,4 +95,4 @@ "execa": "^7.1.1", "openai": "^3.2.1" } -} +} \ No newline at end of file diff --git a/src/autocommit/index.ts b/src/autocommit/index.ts index ac28e8d..7a029f2 100644 --- a/src/autocommit/index.ts +++ b/src/autocommit/index.ts @@ -10,7 +10,7 @@ import { assertGitRepo, getStagedDiff, stageAllChanges } from './utils/git'; import { generateCommitMessage } from './utils/openai'; import { runTaskWithTimeout } from './utils/timer'; -async function generateAICommitMessage(apiKey: string, delimeter?: string) { +async function generateAICommitMessage(apiKey: string,apiUrl: string, delimeter?: string) { try { const assertResult = await assertGitRepo(); @@ -56,6 +56,7 @@ async function generateAICommitMessage(apiKey: string, delimeter?: string) { const commitMessage = await generateCommitMessage( apiKey, + apiUrl, staged.diff, delimeter ); @@ -73,6 +74,7 @@ async function generateAICommitMessage(apiKey: string, delimeter?: string) { return commitMessage; } catch (error: any) { + vscode.window.showErrorMessage(error); const errorMessage = error?.response?.data?.error?.message; if (errorMessage) { diff --git a/src/autocommit/utils/openai.ts b/src/autocommit/utils/openai.ts index 8d48fac..b6de347 100644 --- a/src/autocommit/utils/openai.ts +++ b/src/autocommit/utils/openai.ts @@ -11,6 +11,7 @@ import { trimNewLines } from './text'; export const generateCommitMessage = async ( apiKey: string, + apiUrl: string, diff: string, delimeter?: string ) => { @@ -19,6 +20,7 @@ export const generateCommitMessage = async ( const openAI = new OpenAIApi( new Configuration({ apiKey: apiKey, + basePath: `${apiUrl}/v1`.replace(/\/+$/, ""), }) ); diff --git a/src/extension.ts b/src/extension.ts index 29f3816..1e7ad9d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -33,6 +33,21 @@ async function setOpenAiApiKey(apiKey: string) { ); } +function getOpenAiApiUrl() { + const configuration = vscode.workspace.getConfiguration('autocommit'); + const apiUrl = configuration.get('openAI.apiUrl'); + return apiUrl; +} + +async function setOpenAiApiUrl(apiUrl: string) { + const configuration = vscode.workspace.getConfiguration('autocommit'); + await configuration.update( + 'openAI.apiUrl', + apiUrl, + vscode.ConfigurationTarget.Global + ); +} + function getDelimeter() { const configuration = vscode.workspace.getConfiguration('autocommit'); const delimeter = configuration.get('appearance.delimeter'); @@ -70,9 +85,25 @@ async function generateAICommitCommand() { await setOpenAiApiKey(apiKey); } + let apiUrl = getOpenAiApiUrl(); + + if (!apiUrl || !apiUrl.startsWith('https://')) { + apiUrl = await vscode.window.showInputBox({ + title: 'Please enter your OpenAi Url starting with https://', + }); + + if (!apiUrl || apiUrl.trim() === '') { + vscode.window.showErrorMessage( + 'You should set OpenAi API Url before extension using!' + ); + return; + } + + await setOpenAiApiUrl(apiUrl); + } const delimeter = getDelimeter(); - const commitMessage = await generateAICommitMessage(apiKey, delimeter); + const commitMessage = await generateAICommitMessage(apiKey, apiUrl, delimeter); if (!commitMessage) { return; From bba3f36b1da9ebf82789df5fa7f7338e7774660d Mon Sep 17 00:00:00 2001 From: awwaawwa <8493196+awwaawwa@users.noreply.github.com> Date: Thu, 25 May 2023 20:12:19 +0800 Subject: [PATCH 2/2] * fix(autocommit/index.ts): Remove redundant code --- src/autocommit/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/autocommit/index.ts b/src/autocommit/index.ts index 7a029f2..5627910 100644 --- a/src/autocommit/index.ts +++ b/src/autocommit/index.ts @@ -74,7 +74,6 @@ async function generateAICommitMessage(apiKey: string,apiUrl: string, delimeter? return commitMessage; } catch (error: any) { - vscode.window.showErrorMessage(error); const errorMessage = error?.response?.data?.error?.message; if (errorMessage) {