From cba599337d1fd6f37ff50248185899da0f566ee8 Mon Sep 17 00:00:00 2001 From: henrycunh Date: Mon, 22 May 2023 01:30:41 -0300 Subject: [PATCH] refactor(config.ts): handle undefined values and improve validation for OCO_OPENAI_MAX_TOKENS and OCO_MODEL (#176) fix(config.ts): set default values for OCO_MODEL and OCO_LANGUAGE when reading from environment variables refactor(generateCommitMessageFromGitDiff.ts): remove unused GenerateCommitMessageError interface --- src/commands/commit.ts | 1 - src/commands/config.ts | 16 ++++++++++------ src/generateCommitMessageFromGitDiff.ts | 3 --- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/commands/commit.ts b/src/commands/commit.ts index 6bb1a86..b6c337b 100644 --- a/src/commands/commit.ts +++ b/src/commands/commit.ts @@ -1,6 +1,5 @@ import { execa } from 'execa'; import { - GenerateCommitMessageErrorEnum, generateCommitMessageByDiff } from '../generateCommitMessageFromGitDiff'; import { diff --git a/src/commands/config.ts b/src/commands/config.ts index c879a49..868c977 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -80,7 +80,7 @@ export const configValidators = { } validateConfig( CONFIG_KEYS.OCO_OPENAI_MAX_TOKENS, - typeof value === 'number', + value ? typeof value === 'number' : undefined, 'Must be a number' ); @@ -117,8 +117,8 @@ export const configValidators = { [CONFIG_KEYS.OCO_MODEL](value: any) { validateConfig( - CONFIG_KEYS.OCO_OPENAI_BASE_PATH, - value === 'gpt-3.5-turbo' || value === 'gpt-4', + CONFIG_KEYS.OCO_MODEL, + ['gpt-3.5-turbo', 'gpt-4'].includes(value), `${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)` ); return value; @@ -134,12 +134,12 @@ const configPath = pathJoin(homedir(), '.opencommit'); export const getConfig = (): ConfigType | null => { const configFromEnv = { OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY, - OCO_OPENAI_MAX_TOKENS: Number(process.env.OCO_OPENAI_MAX_TOKENS), + OCO_OPENAI_MAX_TOKENS: process.env.OCO_OPENAI_MAX_TOKENS ? Number(process.env.OCO_OPENAI_MAX_TOKENS) : undefined, OCO_OPENAI_BASE_PATH: process.env.OCO_OPENAI_BASE_PATH, OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === 'true' ? true : false, OCO_EMOJI: process.env.OCO_EMOJI === 'true' ? true : false, - OCO_MODEL: process.env.OCO_MODEL, - OCO_LANGUAGE: process.env.OCO_LANGUAGE + OCO_MODEL: process.env.OCO_MODEL || 'gpt-3.5-turbo', + OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en' }; const configExists = existsSync(configPath); @@ -149,6 +149,10 @@ export const getConfig = (): ConfigType | null => { const config = iniParse(configFile); for (const configKey of Object.keys(config)) { + if (!config[configKey] || ['null', 'undefined'].includes(config[configKey])) { + config[configKey] = undefined; + continue; + } try { const validator = configValidators[configKey as CONFIG_KEYS]; const validValue = validator( diff --git a/src/generateCommitMessageFromGitDiff.ts b/src/generateCommitMessageFromGitDiff.ts index 65f973d..57a79ab 100644 --- a/src/generateCommitMessageFromGitDiff.ts +++ b/src/generateCommitMessageFromGitDiff.ts @@ -74,9 +74,6 @@ export enum GenerateCommitMessageErrorEnum { emptyMessage = 'EMPTY_MESSAGE' } -interface GenerateCommitMessageError { - error: GenerateCommitMessageErrorEnum; -} const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map( (msg) => tokenCount(msg.content) + 4