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
This commit is contained in:
henrycunh
2023-05-22 01:30:41 -03:00
committed by GitHub
parent 4e25f1460a
commit cba599337d
3 changed files with 10 additions and 10 deletions
-1
View File
@@ -1,6 +1,5 @@
import { execa } from 'execa'; import { execa } from 'execa';
import { import {
GenerateCommitMessageErrorEnum,
generateCommitMessageByDiff generateCommitMessageByDiff
} from '../generateCommitMessageFromGitDiff'; } from '../generateCommitMessageFromGitDiff';
import { import {
+10 -6
View File
@@ -80,7 +80,7 @@ export const configValidators = {
} }
validateConfig( validateConfig(
CONFIG_KEYS.OCO_OPENAI_MAX_TOKENS, CONFIG_KEYS.OCO_OPENAI_MAX_TOKENS,
typeof value === 'number', value ? typeof value === 'number' : undefined,
'Must be a number' 'Must be a number'
); );
@@ -117,8 +117,8 @@ export const configValidators = {
[CONFIG_KEYS.OCO_MODEL](value: any) { [CONFIG_KEYS.OCO_MODEL](value: any) {
validateConfig( validateConfig(
CONFIG_KEYS.OCO_OPENAI_BASE_PATH, CONFIG_KEYS.OCO_MODEL,
value === 'gpt-3.5-turbo' || value === 'gpt-4', ['gpt-3.5-turbo', 'gpt-4'].includes(value),
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)` `${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
); );
return value; return value;
@@ -134,12 +134,12 @@ const configPath = pathJoin(homedir(), '.opencommit');
export const getConfig = (): ConfigType | null => { export const getConfig = (): ConfigType | null => {
const configFromEnv = { const configFromEnv = {
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY, 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_OPENAI_BASE_PATH: process.env.OCO_OPENAI_BASE_PATH,
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === 'true' ? true : false, OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === 'true' ? true : false,
OCO_EMOJI: process.env.OCO_EMOJI === 'true' ? true : false, OCO_EMOJI: process.env.OCO_EMOJI === 'true' ? true : false,
OCO_MODEL: process.env.OCO_MODEL, OCO_MODEL: process.env.OCO_MODEL || 'gpt-3.5-turbo',
OCO_LANGUAGE: process.env.OCO_LANGUAGE OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en'
}; };
const configExists = existsSync(configPath); const configExists = existsSync(configPath);
@@ -149,6 +149,10 @@ export const getConfig = (): ConfigType | null => {
const config = iniParse(configFile); const config = iniParse(configFile);
for (const configKey of Object.keys(config)) { for (const configKey of Object.keys(config)) {
if (!config[configKey] || ['null', 'undefined'].includes(config[configKey])) {
config[configKey] = undefined;
continue;
}
try { try {
const validator = configValidators[configKey as CONFIG_KEYS]; const validator = configValidators[configKey as CONFIG_KEYS];
const validValue = validator( const validValue = validator(
-3
View File
@@ -74,9 +74,6 @@ export enum GenerateCommitMessageErrorEnum {
emptyMessage = 'EMPTY_MESSAGE' emptyMessage = 'EMPTY_MESSAGE'
} }
interface GenerateCommitMessageError {
error: GenerateCommitMessageErrorEnum;
}
const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map( const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map(
(msg) => tokenCount(msg.content) + 4 (msg) => tokenCount(msg.content) + 4