Compare commits

...

4 Commits

Author SHA1 Message Date
di-sukharev
d990bf0bf5 build 2024-09-07 18:07:02 +03:00
di-sukharev
119fedad53 fix(commit.ts): update error handling to provide clearer feedback when commit message generation fails
feat(config.ts): add cleanUndefinedValues function to sanitize config values by converting 'undefined' and 'null' strings to actual values
refactor(config.ts): return cleaned config from getConfig function to ensure consistent data types
chore(migrations): log entriesToSet in migration to assist with debugging and tracking changes
2024-09-07 18:06:53 +03:00
di-sukharev
3493cbb42c build 2024-09-07 18:06:39 +03:00
di-sukharev
2c07d5be44 refactor(config.ts): improve code readability by formatting array elements and conditions
fix(migrations): handle undefined values correctly when setting default config values
fix(migrations): ensure process exits with error code on migration failure
2024-09-07 18:06:20 +03:00
6 changed files with 173 additions and 34 deletions

View File

@@ -27331,7 +27331,7 @@ function G3(t2, e3) {
// package.json // package.json
var package_default = { var package_default = {
name: "opencommit", name: "opencommit",
version: "3.2.1", version: "3.2.2",
description: "Auto-generate impressive commits in 1 second. Killing lame commits with AI \u{1F92F}\u{1F52B}", description: "Auto-generate impressive commits in 1 second. Killing lame commits with AI \u{1F92F}\u{1F52B}",
keywords: [ keywords: [
"git", "git",
@@ -29918,6 +29918,15 @@ var MODEL_LIST = {
"gemini-1.0-pro", "gemini-1.0-pro",
"gemini-pro-vision", "gemini-pro-vision",
"text-embedding-004" "text-embedding-004"
],
groq: [
"llama3-70b-8192",
"llama3-8b-8192",
"llama-guard-3-8b",
"llama-3.1-8b-instant",
"llama-3.1-70b-versatile",
"gemma-7b-it",
"gemma2-9b-it"
] ]
}; };
var getDefaultModel = (provider) => { var getDefaultModel = (provider) => {
@@ -29928,6 +29937,8 @@ var getDefaultModel = (provider) => {
return MODEL_LIST.anthropic[0]; return MODEL_LIST.anthropic[0];
case "gemini": case "gemini":
return MODEL_LIST.gemini[0]; return MODEL_LIST.gemini[0];
case "groq":
return MODEL_LIST.groq[0];
default: default:
return MODEL_LIST.openai[0]; return MODEL_LIST.openai[0];
} }
@@ -30051,9 +30062,15 @@ var configValidators = {
value = "openai"; value = "openai";
validateConfig( validateConfig(
"OCO_AI_PROVIDER" /* OCO_AI_PROVIDER */, "OCO_AI_PROVIDER" /* OCO_AI_PROVIDER */,
["openai", "anthropic", "gemini", "azure", "test", "flowise"].includes( [
value "openai",
) || value.startsWith("ollama"), "anthropic",
"gemini",
"azure",
"test",
"flowise",
"groq"
].includes(value) || value.startsWith("ollama"),
`${value} is not supported yet, use 'ollama', 'anthropic', 'azure', 'gemini', 'flowise' or 'openai' (default)` `${value} is not supported yet, use 'ollama', 'anthropic', 'azure', 'gemini', 'flowise' or 'openai' (default)`
); );
return value; return value;
@@ -30093,6 +30110,7 @@ var OCO_AI_PROVIDER_ENUM = /* @__PURE__ */ ((OCO_AI_PROVIDER_ENUM2) => {
OCO_AI_PROVIDER_ENUM2["AZURE"] = "azure"; OCO_AI_PROVIDER_ENUM2["AZURE"] = "azure";
OCO_AI_PROVIDER_ENUM2["TEST"] = "test"; OCO_AI_PROVIDER_ENUM2["TEST"] = "test";
OCO_AI_PROVIDER_ENUM2["FLOWISE"] = "flowise"; OCO_AI_PROVIDER_ENUM2["FLOWISE"] = "flowise";
OCO_AI_PROVIDER_ENUM2["GROQ"] = "groq";
return OCO_AI_PROVIDER_ENUM2; return OCO_AI_PROVIDER_ENUM2;
})(OCO_AI_PROVIDER_ENUM || {}); })(OCO_AI_PROVIDER_ENUM || {});
var defaultConfigPath = (0, import_path.join)((0, import_os.homedir)(), ".opencommit"); var defaultConfigPath = (0, import_path.join)((0, import_os.homedir)(), ".opencommit");
@@ -30109,7 +30127,6 @@ var DEFAULT_CONFIG = {
OCO_AI_PROVIDER: "openai" /* OPENAI */, OCO_AI_PROVIDER: "openai" /* OPENAI */,
OCO_ONE_LINE_COMMIT: false, OCO_ONE_LINE_COMMIT: false,
OCO_TEST_MOCK_TYPE: "commit-message", OCO_TEST_MOCK_TYPE: "commit-message",
OCO_FLOWISE_ENDPOINT: ":",
OCO_WHY: false, OCO_WHY: false,
OCO_GITPUSH: true OCO_GITPUSH: true
}; };
@@ -30169,6 +30186,25 @@ var mergeConfigs = (main, fallback) => {
return acc; return acc;
}, {}); }, {});
}; };
var cleanUndefinedValues = (config7) => {
return Object.fromEntries(
Object.entries(config7).map(([_7, v5]) => {
try {
if (typeof v5 === "string") {
if (v5 === "undefined")
return [_7, void 0];
if (v5 === "null")
return [_7, null];
const parsedValue = JSON.parse(v5);
return [_7, parsedValue];
}
return [_7, v5];
} catch (error) {
return [_7, v5];
}
})
);
};
var getConfig = ({ var getConfig = ({
envPath = defaultEnvPath, envPath = defaultEnvPath,
globalPath = defaultConfigPath globalPath = defaultConfigPath
@@ -30176,7 +30212,8 @@ var getConfig = ({
const envConfig = getEnvConfig(envPath); const envConfig = getEnvConfig(envPath);
const globalConfig = getGlobalConfig(globalPath); const globalConfig = getGlobalConfig(globalPath);
const config7 = mergeConfigs(envConfig, globalConfig); const config7 = mergeConfigs(envConfig, globalConfig);
return config7; const cleanConfig = cleanUndefinedValues(config7);
return cleanConfig;
}; };
var setConfig = (keyValues, globalConfigPath = defaultConfigPath) => { var setConfig = (keyValues, globalConfigPath = defaultConfigPath) => {
const config7 = getConfig({ const config7 = getConfig({
@@ -44471,7 +44508,19 @@ var OpenAiEngine = class {
} }
}; };
this.config = config7; this.config = config7;
if (!config7.baseURL) {
this.client = new OpenAI({ apiKey: config7.apiKey }); this.client = new OpenAI({ apiKey: config7.apiKey });
} else {
this.client = new OpenAI({ apiKey: config7.apiKey, baseURL: config7.baseURL });
}
}
};
// src/engine/groq.ts
var GroqEngine = class extends OpenAiEngine {
constructor(config7) {
config7.baseURL = "https://api.groq.com/openai/v1";
super(config7);
} }
}; };
@@ -44499,6 +44548,8 @@ function getEngine() {
return new AzureEngine(DEFAULT_CONFIG2); return new AzureEngine(DEFAULT_CONFIG2);
case "flowise" /* FLOWISE */: case "flowise" /* FLOWISE */:
return new FlowiseEngine(DEFAULT_CONFIG2); return new FlowiseEngine(DEFAULT_CONFIG2);
case "groq" /* GROQ */:
return new GroqEngine(DEFAULT_CONFIG2);
default: default:
return new OpenAiEngine(DEFAULT_CONFIG2); return new OpenAiEngine(DEFAULT_CONFIG2);
} }
@@ -45342,7 +45393,10 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2
} }
} }
} catch (error) { } catch (error) {
commitGenerationSpinner.stop("\u{1F4DD} Commit message generated"); commitGenerationSpinner.stop(
`${source_default.red("\u2716")} Failed to generate the commit message`
);
console.log(error);
const err = error; const err = error;
ce(`${source_default.red("\u2716")} ${err?.message || err}`); ce(`${source_default.red("\u2716")} ${err?.message || err}`);
process.exit(1); process.exit(1);
@@ -45677,11 +45731,12 @@ function set_missing_default_values_default() {
const entriesToSet = []; const entriesToSet = [];
for (const entry of Object.entries(DEFAULT_CONFIG)) { for (const entry of Object.entries(DEFAULT_CONFIG)) {
const [key, _value] = entry; const [key, _value] = entry;
if (config7[key] === "undefined") if (config7[key] === "undefined" || config7[key] === void 0)
entriesToSet.push(entry); entriesToSet.push(entry);
} }
if (entriesToSet.length > 0) if (entriesToSet.length > 0)
setConfig(entriesToSet); setConfig(entriesToSet);
console.log(entriesToSet);
}; };
setDefaultConfigValues(getGlobalConfig()); setDefaultConfigValues(getGlobalConfig());
} }
@@ -45738,6 +45793,7 @@ var runMigrations = async () => {
ce( ce(
`${source_default.red("Failed to apply migration")} ${migration.name}: ${error}` `${source_default.red("Failed to apply migration")} ${migration.name}: ${error}`
); );
process.exit(1);
} }
isMigrated = true; isMigrated = true;
} }

View File

@@ -48730,6 +48730,15 @@ var MODEL_LIST = {
"gemini-1.0-pro", "gemini-1.0-pro",
"gemini-pro-vision", "gemini-pro-vision",
"text-embedding-004" "text-embedding-004"
],
groq: [
"llama3-70b-8192",
"llama3-8b-8192",
"llama-guard-3-8b",
"llama-3.1-8b-instant",
"llama-3.1-70b-versatile",
"gemma-7b-it",
"gemma2-9b-it"
] ]
}; };
var getDefaultModel = (provider) => { var getDefaultModel = (provider) => {
@@ -48740,6 +48749,8 @@ var getDefaultModel = (provider) => {
return MODEL_LIST.anthropic[0]; return MODEL_LIST.anthropic[0];
case "gemini": case "gemini":
return MODEL_LIST.gemini[0]; return MODEL_LIST.gemini[0];
case "groq":
return MODEL_LIST.groq[0];
default: default:
return MODEL_LIST.openai[0]; return MODEL_LIST.openai[0];
} }
@@ -48863,9 +48874,15 @@ var configValidators = {
value = "openai"; value = "openai";
validateConfig( validateConfig(
"OCO_AI_PROVIDER" /* OCO_AI_PROVIDER */, "OCO_AI_PROVIDER" /* OCO_AI_PROVIDER */,
["openai", "anthropic", "gemini", "azure", "test", "flowise"].includes( [
value "openai",
) || value.startsWith("ollama"), "anthropic",
"gemini",
"azure",
"test",
"flowise",
"groq"
].includes(value) || value.startsWith("ollama"),
`${value} is not supported yet, use 'ollama', 'anthropic', 'azure', 'gemini', 'flowise' or 'openai' (default)` `${value} is not supported yet, use 'ollama', 'anthropic', 'azure', 'gemini', 'flowise' or 'openai' (default)`
); );
return value; return value;
@@ -48911,7 +48928,6 @@ var DEFAULT_CONFIG = {
OCO_AI_PROVIDER: "openai" /* OPENAI */, OCO_AI_PROVIDER: "openai" /* OPENAI */,
OCO_ONE_LINE_COMMIT: false, OCO_ONE_LINE_COMMIT: false,
OCO_TEST_MOCK_TYPE: "commit-message", OCO_TEST_MOCK_TYPE: "commit-message",
OCO_FLOWISE_ENDPOINT: ":",
OCO_WHY: false, OCO_WHY: false,
OCO_GITPUSH: true OCO_GITPUSH: true
}; };
@@ -48971,6 +48987,25 @@ var mergeConfigs = (main, fallback) => {
return acc; return acc;
}, {}); }, {});
}; };
var cleanUndefinedValues = (config6) => {
return Object.fromEntries(
Object.entries(config6).map(([_3, v2]) => {
try {
if (typeof v2 === "string") {
if (v2 === "undefined")
return [_3, void 0];
if (v2 === "null")
return [_3, null];
const parsedValue = JSON.parse(v2);
return [_3, parsedValue];
}
return [_3, v2];
} catch (error) {
return [_3, v2];
}
})
);
};
var getConfig = ({ var getConfig = ({
envPath = defaultEnvPath, envPath = defaultEnvPath,
globalPath = defaultConfigPath globalPath = defaultConfigPath
@@ -48978,7 +49013,8 @@ var getConfig = ({
const envConfig = getEnvConfig(envPath); const envConfig = getEnvConfig(envPath);
const globalConfig = getGlobalConfig(globalPath); const globalConfig = getGlobalConfig(globalPath);
const config6 = mergeConfigs(envConfig, globalConfig); const config6 = mergeConfigs(envConfig, globalConfig);
return config6; const cleanConfig = cleanUndefinedValues(config6);
return cleanConfig;
}; };
var setConfig = (keyValues, globalConfigPath = defaultConfigPath) => { var setConfig = (keyValues, globalConfigPath = defaultConfigPath) => {
const config6 = getConfig({ const config6 = getConfig({
@@ -63273,7 +63309,19 @@ var OpenAiEngine = class {
} }
}; };
this.config = config6; this.config = config6;
if (!config6.baseURL) {
this.client = new OpenAI({ apiKey: config6.apiKey }); this.client = new OpenAI({ apiKey: config6.apiKey });
} else {
this.client = new OpenAI({ apiKey: config6.apiKey, baseURL: config6.baseURL });
}
}
};
// src/engine/groq.ts
var GroqEngine = class extends OpenAiEngine {
constructor(config6) {
config6.baseURL = "https://api.groq.com/openai/v1";
super(config6);
} }
}; };
@@ -63301,6 +63349,8 @@ function getEngine() {
return new AzureEngine(DEFAULT_CONFIG2); return new AzureEngine(DEFAULT_CONFIG2);
case "flowise" /* FLOWISE */: case "flowise" /* FLOWISE */:
return new FlowiseEngine(DEFAULT_CONFIG2); return new FlowiseEngine(DEFAULT_CONFIG2);
case "groq" /* GROQ */:
return new GroqEngine(DEFAULT_CONFIG2);
default: default:
return new OpenAiEngine(DEFAULT_CONFIG2); return new OpenAiEngine(DEFAULT_CONFIG2);
} }

View File

@@ -183,7 +183,11 @@ ${chalk.grey('——————————————————')}`
} }
} }
} catch (error) { } catch (error) {
commitGenerationSpinner.stop('📝 Commit message generated'); commitGenerationSpinner.stop(
`${chalk.red('✖')} Failed to generate the commit message`
);
console.log(error);
const err = error as Error; const err = error as Error;
outro(`${chalk.red('✖')} ${err?.message || err}`); outro(`${chalk.red('✖')} ${err?.message || err}`);

View File

@@ -253,9 +253,15 @@ export const configValidators = {
validateConfig( validateConfig(
CONFIG_KEYS.OCO_AI_PROVIDER, CONFIG_KEYS.OCO_AI_PROVIDER,
['openai', 'anthropic', 'gemini', 'azure', 'test', 'flowise', 'groq'].includes( [
value 'openai',
) || value.startsWith('ollama'), 'anthropic',
'gemini',
'azure',
'test',
'flowise',
'groq'
].includes(value) || value.startsWith('ollama'),
`${value} is not supported yet, use 'ollama', 'anthropic', 'azure', 'gemini', 'flowise' or 'openai' (default)` `${value} is not supported yet, use 'ollama', 'anthropic', 'azure', 'gemini', 'flowise' or 'openai' (default)`
); );
@@ -301,7 +307,7 @@ export enum OCO_AI_PROVIDER_ENUM {
AZURE = 'azure', AZURE = 'azure',
TEST = 'test', TEST = 'test',
FLOWISE = 'flowise', FLOWISE = 'flowise',
GROQ = 'groq', GROQ = 'groq'
} }
export type ConfigType = { export type ConfigType = {
@@ -365,7 +371,6 @@ export const DEFAULT_CONFIG = {
OCO_AI_PROVIDER: OCO_AI_PROVIDER_ENUM.OPENAI, OCO_AI_PROVIDER: OCO_AI_PROVIDER_ENUM.OPENAI,
OCO_ONE_LINE_COMMIT: false, OCO_ONE_LINE_COMMIT: false,
OCO_TEST_MOCK_TYPE: 'commit-message', OCO_TEST_MOCK_TYPE: 'commit-message',
OCO_FLOWISE_ENDPOINT: ':',
OCO_WHY: false, OCO_WHY: false,
OCO_GITPUSH: true // todo: deprecate OCO_GITPUSH: true // todo: deprecate
}; };
@@ -457,16 +462,37 @@ interface GetConfigOptions {
setDefaultValues?: boolean; setDefaultValues?: boolean;
} }
const cleanUndefinedValues = (config: ConfigType) => {
return Object.fromEntries(
Object.entries(config).map(([_, v]) => {
try {
if (typeof v === 'string') {
if (v === 'undefined') return [_, undefined];
if (v === 'null') return [_, null];
const parsedValue = JSON.parse(v);
return [_, parsedValue];
}
return [_, v];
} catch (error) {
return [_, v];
}
})
);
};
export const getConfig = ({ export const getConfig = ({
envPath = defaultEnvPath, envPath = defaultEnvPath,
globalPath = defaultConfigPath globalPath = defaultConfigPath
}: GetConfigOptions = {}): ConfigType => { }: GetConfigOptions = {}): ConfigType => {
const envConfig = getEnvConfig(envPath); const envConfig = getEnvConfig(envPath);
const globalConfig = getGlobalConfig(globalPath); const globalConfig = getGlobalConfig(globalPath);
const config = mergeConfigs(envConfig, globalConfig); const config = mergeConfigs(envConfig, globalConfig);
return config; const cleanConfig = cleanUndefinedValues(config);
return cleanConfig as ConfigType;
}; };
export const setConfig = ( export const setConfig = (

View File

@@ -10,10 +10,12 @@ export default function () {
const entriesToSet: [key: string, value: string | boolean | number][] = []; const entriesToSet: [key: string, value: string | boolean | number][] = [];
for (const entry of Object.entries(DEFAULT_CONFIG)) { for (const entry of Object.entries(DEFAULT_CONFIG)) {
const [key, _value] = entry; const [key, _value] = entry;
if (config[key] === 'undefined') entriesToSet.push(entry); if (config[key] === 'undefined' || config[key] === undefined)
entriesToSet.push(entry);
} }
if (entriesToSet.length > 0) setConfig(entriesToSet); if (entriesToSet.length > 0) setConfig(entriesToSet);
console.log(entriesToSet);
}; };
setDefaultConfigValues(getGlobalConfig()); setDefaultConfigValues(getGlobalConfig());

View File

@@ -53,6 +53,7 @@ export const runMigrations = async () => {
migration.name migration.name
}: ${error}` }: ${error}`
); );
process.exit(1);
} }
isMigrated = true; isMigrated = true;