Skip to content

Commit cba5993

Browse files
authored
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
1 parent 4e25f14 commit cba5993

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

src/commands/commit.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { execa } from 'execa';
22
import {
3-
GenerateCommitMessageErrorEnum,
43
generateCommitMessageByDiff
54
} from '../generateCommitMessageFromGitDiff';
65
import {

src/commands/config.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const configValidators = {
8080
}
8181
validateConfig(
8282
CONFIG_KEYS.OCO_OPENAI_MAX_TOKENS,
83-
typeof value === 'number',
83+
value ? typeof value === 'number' : undefined,
8484
'Must be a number'
8585
);
8686

@@ -117,8 +117,8 @@ export const configValidators = {
117117

118118
[CONFIG_KEYS.OCO_MODEL](value: any) {
119119
validateConfig(
120-
CONFIG_KEYS.OCO_OPENAI_BASE_PATH,
121-
value === 'gpt-3.5-turbo' || value === 'gpt-4',
120+
CONFIG_KEYS.OCO_MODEL,
121+
['gpt-3.5-turbo', 'gpt-4'].includes(value),
122122
`${value} is not supported yet, use 'gpt-4' or 'gpt-3.5-turbo' (default)`
123123
);
124124
return value;
@@ -134,12 +134,12 @@ const configPath = pathJoin(homedir(), '.opencommit');
134134
export const getConfig = (): ConfigType | null => {
135135
const configFromEnv = {
136136
OCO_OPENAI_API_KEY: process.env.OCO_OPENAI_API_KEY,
137-
OCO_OPENAI_MAX_TOKENS: Number(process.env.OCO_OPENAI_MAX_TOKENS),
137+
OCO_OPENAI_MAX_TOKENS: process.env.OCO_OPENAI_MAX_TOKENS ? Number(process.env.OCO_OPENAI_MAX_TOKENS) : undefined,
138138
OCO_OPENAI_BASE_PATH: process.env.OCO_OPENAI_BASE_PATH,
139139
OCO_DESCRIPTION: process.env.OCO_DESCRIPTION === 'true' ? true : false,
140140
OCO_EMOJI: process.env.OCO_EMOJI === 'true' ? true : false,
141-
OCO_MODEL: process.env.OCO_MODEL,
142-
OCO_LANGUAGE: process.env.OCO_LANGUAGE
141+
OCO_MODEL: process.env.OCO_MODEL || 'gpt-3.5-turbo',
142+
OCO_LANGUAGE: process.env.OCO_LANGUAGE || 'en'
143143
};
144144

145145
const configExists = existsSync(configPath);
@@ -149,6 +149,10 @@ export const getConfig = (): ConfigType | null => {
149149
const config = iniParse(configFile);
150150

151151
for (const configKey of Object.keys(config)) {
152+
if (!config[configKey] || ['null', 'undefined'].includes(config[configKey])) {
153+
config[configKey] = undefined;
154+
continue;
155+
}
152156
try {
153157
const validator = configValidators[configKey as CONFIG_KEYS];
154158
const validValue = validator(

src/generateCommitMessageFromGitDiff.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,6 @@ export enum GenerateCommitMessageErrorEnum {
7474
emptyMessage = 'EMPTY_MESSAGE'
7575
}
7676

77-
interface GenerateCommitMessageError {
78-
error: GenerateCommitMessageErrorEnum;
79-
}
8077

8178
const INIT_MESSAGES_PROMPT_LENGTH = INIT_MESSAGES_PROMPT.map(
8279
(msg) => tokenCount(msg.content) + 4

0 commit comments

Comments
 (0)