Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config.ts): handle undefined values and improve validation for OCO_OPENAI_MAX_TOKENS and OCO_MODEL #176

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/commands/commit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { execa } from 'execa';
import {
GenerateCommitMessageErrorEnum,
generateCommitMessageByDiff
} from '../generateCommitMessageFromGitDiff';
import {
Expand Down
16 changes: 10 additions & 6 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);

Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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(
Expand Down
3 changes: 0 additions & 3 deletions src/generateCommitMessageFromGitDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down