From 2c07d5be4477f21d719c9e5b0fe3900e72817743 Mon Sep 17 00:00:00 2001 From: di-sukharev Date: Sat, 7 Sep 2024 18:06:20 +0300 Subject: [PATCH 1/4] 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 --- out/cli.cjs | 46 ++++++++++++++++--- out/github-action.cjs | 40 ++++++++++++++-- src/commands/config.ts | 37 ++++++++------- .../02_set_missing_default_values.ts | 3 +- src/migrations/_run.ts | 1 + 5 files changed, 98 insertions(+), 29 deletions(-) diff --git a/out/cli.cjs b/out/cli.cjs index 50fd4e2e..719a2947 100755 --- a/out/cli.cjs +++ b/out/cli.cjs @@ -27331,7 +27331,7 @@ function G3(t2, e3) { // package.json var package_default = { 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}", keywords: [ "git", @@ -29918,6 +29918,15 @@ var MODEL_LIST = { "gemini-1.0-pro", "gemini-pro-vision", "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) => { @@ -29928,6 +29937,8 @@ var getDefaultModel = (provider) => { return MODEL_LIST.anthropic[0]; case "gemini": return MODEL_LIST.gemini[0]; + case "groq": + return MODEL_LIST.groq[0]; default: return MODEL_LIST.openai[0]; } @@ -30051,9 +30062,15 @@ var configValidators = { value = "openai"; validateConfig( "OCO_AI_PROVIDER" /* OCO_AI_PROVIDER */, - ["openai", "anthropic", "gemini", "azure", "test", "flowise"].includes( - value - ) || value.startsWith("ollama"), + [ + "openai", + "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)` ); 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["TEST"] = "test"; OCO_AI_PROVIDER_ENUM2["FLOWISE"] = "flowise"; + OCO_AI_PROVIDER_ENUM2["GROQ"] = "groq"; return OCO_AI_PROVIDER_ENUM2; })(OCO_AI_PROVIDER_ENUM || {}); var defaultConfigPath = (0, import_path.join)((0, import_os.homedir)(), ".opencommit"); @@ -30109,7 +30127,6 @@ var DEFAULT_CONFIG = { OCO_AI_PROVIDER: "openai" /* OPENAI */, OCO_ONE_LINE_COMMIT: false, OCO_TEST_MOCK_TYPE: "commit-message", - OCO_FLOWISE_ENDPOINT: ":", OCO_WHY: false, OCO_GITPUSH: true }; @@ -44471,7 +44488,19 @@ var OpenAiEngine = class { } }; this.config = config7; - this.client = new OpenAI({ apiKey: config7.apiKey }); + if (!config7.baseURL) { + 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 +44528,8 @@ function getEngine() { return new AzureEngine(DEFAULT_CONFIG2); case "flowise" /* FLOWISE */: return new FlowiseEngine(DEFAULT_CONFIG2); + case "groq" /* GROQ */: + return new GroqEngine(DEFAULT_CONFIG2); default: return new OpenAiEngine(DEFAULT_CONFIG2); } @@ -45677,7 +45708,7 @@ function set_missing_default_values_default() { const entriesToSet = []; for (const entry of Object.entries(DEFAULT_CONFIG)) { const [key, _value] = entry; - if (config7[key] === "undefined") + if (config7[key] === "undefined" || config7[key] === void 0) entriesToSet.push(entry); } if (entriesToSet.length > 0) @@ -45738,6 +45769,7 @@ var runMigrations = async () => { ce( `${source_default.red("Failed to apply migration")} ${migration.name}: ${error}` ); + process.exit(1); } isMigrated = true; } diff --git a/out/github-action.cjs b/out/github-action.cjs index 68b14210..e9c33e26 100644 --- a/out/github-action.cjs +++ b/out/github-action.cjs @@ -48730,6 +48730,15 @@ var MODEL_LIST = { "gemini-1.0-pro", "gemini-pro-vision", "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) => { @@ -48740,6 +48749,8 @@ var getDefaultModel = (provider) => { return MODEL_LIST.anthropic[0]; case "gemini": return MODEL_LIST.gemini[0]; + case "groq": + return MODEL_LIST.groq[0]; default: return MODEL_LIST.openai[0]; } @@ -48863,9 +48874,15 @@ var configValidators = { value = "openai"; validateConfig( "OCO_AI_PROVIDER" /* OCO_AI_PROVIDER */, - ["openai", "anthropic", "gemini", "azure", "test", "flowise"].includes( - value - ) || value.startsWith("ollama"), + [ + "openai", + "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)` ); return value; @@ -48911,7 +48928,6 @@ var DEFAULT_CONFIG = { OCO_AI_PROVIDER: "openai" /* OPENAI */, OCO_ONE_LINE_COMMIT: false, OCO_TEST_MOCK_TYPE: "commit-message", - OCO_FLOWISE_ENDPOINT: ":", OCO_WHY: false, OCO_GITPUSH: true }; @@ -63273,7 +63289,19 @@ var OpenAiEngine = class { } }; this.config = config6; - this.client = new OpenAI({ apiKey: config6.apiKey }); + if (!config6.baseURL) { + 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 +63329,8 @@ function getEngine() { return new AzureEngine(DEFAULT_CONFIG2); case "flowise" /* FLOWISE */: return new FlowiseEngine(DEFAULT_CONFIG2); + case "groq" /* GROQ */: + return new GroqEngine(DEFAULT_CONFIG2); default: return new OpenAiEngine(DEFAULT_CONFIG2); } diff --git a/src/commands/config.ts b/src/commands/config.ts index 0b508a44..161e6001 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -79,13 +79,13 @@ export const MODEL_LIST = { ], groq: [ - 'llama3-70b-8192', // Meta Llama 3 70B (default one, no daily token limit and 14 400 reqs/day) - 'llama3-8b-8192', // Meta Llama 3 8B - 'llama-guard-3-8b', // Llama Guard 3 8B - 'llama-3.1-8b-instant', // Llama 3.1 8B (Preview) - 'llama-3.1-70b-versatile', // Llama 3.1 70B (Preview) - 'gemma-7b-it', // Gemma 7B - 'gemma2-9b-it' // Gemma 2 9B + 'llama3-70b-8192', // Meta Llama 3 70B (default one, no daily token limit and 14 400 reqs/day) + 'llama3-8b-8192', // Meta Llama 3 8B + 'llama-guard-3-8b', // Llama Guard 3 8B + 'llama-3.1-8b-instant', // Llama 3.1 8B (Preview) + 'llama-3.1-70b-versatile', // Llama 3.1 70B (Preview) + 'gemma-7b-it', // Gemma 7B + 'gemma2-9b-it' // Gemma 2 9B ] }; @@ -253,9 +253,15 @@ export const configValidators = { validateConfig( CONFIG_KEYS.OCO_AI_PROVIDER, - ['openai', 'anthropic', 'gemini', 'azure', 'test', 'flowise', 'groq'].includes( - value - ) || value.startsWith('ollama'), + [ + 'openai', + '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)` ); @@ -301,7 +307,7 @@ export enum OCO_AI_PROVIDER_ENUM { AZURE = 'azure', TEST = 'test', FLOWISE = 'flowise', - GROQ = 'groq', + GROQ = 'groq' } export type ConfigType = { @@ -365,7 +371,6 @@ export const DEFAULT_CONFIG = { OCO_AI_PROVIDER: OCO_AI_PROVIDER_ENUM.OPENAI, OCO_ONE_LINE_COMMIT: false, OCO_TEST_MOCK_TYPE: 'commit-message', - OCO_FLOWISE_ENDPOINT: ':', OCO_WHY: false, OCO_GITPUSH: true // todo: deprecate }; @@ -401,7 +406,7 @@ const getEnvConfig = (envPath: string) => { OCO_EMOJI: parseConfigVarValue(process.env.OCO_EMOJI), OCO_LANGUAGE: process.env.OCO_LANGUAGE, OCO_MESSAGE_TEMPLATE_PLACEHOLDER: - process.env.OCO_MESSAGE_TEMPLATE_PLACEHOLDER, + process.env.OCO_MESSAGE_TEMPLATE_PLACEHOLDER, OCO_PROMPT_MODULE: process.env.OCO_PROMPT_MODULE as OCO_PROMPT_MODULE_ENUM, OCO_ONE_LINE_COMMIT: parseConfigVarValue(process.env.OCO_ONE_LINE_COMMIT), OCO_TEST_MOCK_TYPE: process.env.OCO_TEST_MOCK_TYPE, @@ -458,9 +463,9 @@ interface GetConfigOptions { } export const getConfig = ({ - envPath = defaultEnvPath, - globalPath = defaultConfigPath - }: GetConfigOptions = {}): ConfigType => { + envPath = defaultEnvPath, + globalPath = defaultConfigPath +}: GetConfigOptions = {}): ConfigType => { const envConfig = getEnvConfig(envPath); const globalConfig = getGlobalConfig(globalPath); diff --git a/src/migrations/02_set_missing_default_values.ts b/src/migrations/02_set_missing_default_values.ts index 107fc06b..2e2764c4 100644 --- a/src/migrations/02_set_missing_default_values.ts +++ b/src/migrations/02_set_missing_default_values.ts @@ -10,7 +10,8 @@ export default function () { const entriesToSet: [key: string, value: string | boolean | number][] = []; for (const entry of Object.entries(DEFAULT_CONFIG)) { 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); diff --git a/src/migrations/_run.ts b/src/migrations/_run.ts index 03a6b1bc..bbf3bb82 100644 --- a/src/migrations/_run.ts +++ b/src/migrations/_run.ts @@ -53,6 +53,7 @@ export const runMigrations = async () => { migration.name }: ${error}` ); + process.exit(1); } isMigrated = true; From 3493cbb42ca8a613fbdd8ce16e1abf654380b549 Mon Sep 17 00:00:00 2001 From: di-sukharev Date: Sat, 7 Sep 2024 18:06:39 +0300 Subject: [PATCH 2/4] build --- out/cli.cjs | 29 +++++++++++++++++++++++++++-- out/github-action.cjs | 23 ++++++++++++++++++++++- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/out/cli.cjs b/out/cli.cjs index 719a2947..d511ce99 100755 --- a/out/cli.cjs +++ b/out/cli.cjs @@ -30186,6 +30186,25 @@ var mergeConfigs = (main, fallback) => { 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 = ({ envPath = defaultEnvPath, globalPath = defaultConfigPath @@ -30193,7 +30212,8 @@ var getConfig = ({ const envConfig = getEnvConfig(envPath); const globalConfig = getGlobalConfig(globalPath); const config7 = mergeConfigs(envConfig, globalConfig); - return config7; + const cleanConfig = cleanUndefinedValues(config7); + return cleanConfig; }; var setConfig = (keyValues, globalConfigPath = defaultConfigPath) => { const config7 = getConfig({ @@ -44508,6 +44528,7 @@ var GroqEngine = class extends OpenAiEngine { function getEngine() { const config7 = getConfig(); const provider = config7.OCO_AI_PROVIDER; + console.log(123123, { config: config7 }); const DEFAULT_CONFIG2 = { model: config7.OCO_MODEL, maxTokensOutput: config7.OCO_TOKENS_MAX_OUTPUT, @@ -45373,7 +45394,10 @@ ${source_default.grey("\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2 } } } 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; ce(`${source_default.red("\u2716")} ${err?.message || err}`); process.exit(1); @@ -45713,6 +45737,7 @@ function set_missing_default_values_default() { } if (entriesToSet.length > 0) setConfig(entriesToSet); + console.log(entriesToSet); }; setDefaultConfigValues(getGlobalConfig()); } diff --git a/out/github-action.cjs b/out/github-action.cjs index e9c33e26..9760d39a 100644 --- a/out/github-action.cjs +++ b/out/github-action.cjs @@ -48987,6 +48987,25 @@ var mergeConfigs = (main, fallback) => { 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 = ({ envPath = defaultEnvPath, globalPath = defaultConfigPath @@ -48994,7 +49013,8 @@ var getConfig = ({ const envConfig = getEnvConfig(envPath); const globalConfig = getGlobalConfig(globalPath); const config6 = mergeConfigs(envConfig, globalConfig); - return config6; + const cleanConfig = cleanUndefinedValues(config6); + return cleanConfig; }; var setConfig = (keyValues, globalConfigPath = defaultConfigPath) => { const config6 = getConfig({ @@ -63309,6 +63329,7 @@ var GroqEngine = class extends OpenAiEngine { function getEngine() { const config6 = getConfig(); const provider = config6.OCO_AI_PROVIDER; + console.log(123123, { config: config6 }); const DEFAULT_CONFIG2 = { model: config6.OCO_MODEL, maxTokensOutput: config6.OCO_TOKENS_MAX_OUTPUT, From 119fedad53ec21a50c3b198da82e3a4c8d903271 Mon Sep 17 00:00:00 2001 From: di-sukharev Date: Sat, 7 Sep 2024 18:06:53 +0300 Subject: [PATCH 3/4] 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 --- src/commands/commit.ts | 6 ++++- src/commands/config.ts | 23 ++++++++++++++++++- .../02_set_missing_default_values.ts | 1 + 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/commands/commit.ts b/src/commands/commit.ts index 8d61df0d..a26df269 100644 --- a/src/commands/commit.ts +++ b/src/commands/commit.ts @@ -183,7 +183,11 @@ ${chalk.grey('——————————————————')}` } } } 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; outro(`${chalk.red('✖')} ${err?.message || err}`); diff --git a/src/commands/config.ts b/src/commands/config.ts index 161e6001..e6736334 100644 --- a/src/commands/config.ts +++ b/src/commands/config.ts @@ -462,6 +462,25 @@ interface GetConfigOptions { 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 = ({ envPath = defaultEnvPath, globalPath = defaultConfigPath @@ -471,7 +490,9 @@ export const getConfig = ({ const config = mergeConfigs(envConfig, globalConfig); - return config; + const cleanConfig = cleanUndefinedValues(config); + + return cleanConfig as ConfigType; }; export const setConfig = ( diff --git a/src/migrations/02_set_missing_default_values.ts b/src/migrations/02_set_missing_default_values.ts index 2e2764c4..c93779a5 100644 --- a/src/migrations/02_set_missing_default_values.ts +++ b/src/migrations/02_set_missing_default_values.ts @@ -15,6 +15,7 @@ export default function () { } if (entriesToSet.length > 0) setConfig(entriesToSet); + console.log(entriesToSet); }; setDefaultConfigValues(getGlobalConfig()); From d990bf0bf50b58ed4edb9692304bec66ce60f163 Mon Sep 17 00:00:00 2001 From: di-sukharev Date: Sat, 7 Sep 2024 18:07:02 +0300 Subject: [PATCH 4/4] build --- out/cli.cjs | 1 - out/github-action.cjs | 1 - 2 files changed, 2 deletions(-) diff --git a/out/cli.cjs b/out/cli.cjs index d511ce99..a4fe603b 100755 --- a/out/cli.cjs +++ b/out/cli.cjs @@ -44528,7 +44528,6 @@ var GroqEngine = class extends OpenAiEngine { function getEngine() { const config7 = getConfig(); const provider = config7.OCO_AI_PROVIDER; - console.log(123123, { config: config7 }); const DEFAULT_CONFIG2 = { model: config7.OCO_MODEL, maxTokensOutput: config7.OCO_TOKENS_MAX_OUTPUT, diff --git a/out/github-action.cjs b/out/github-action.cjs index 9760d39a..5acd5e08 100644 --- a/out/github-action.cjs +++ b/out/github-action.cjs @@ -63329,7 +63329,6 @@ var GroqEngine = class extends OpenAiEngine { function getEngine() { const config6 = getConfig(); const provider = config6.OCO_AI_PROVIDER; - console.log(123123, { config: config6 }); const DEFAULT_CONFIG2 = { model: config6.OCO_MODEL, maxTokensOutput: config6.OCO_TOKENS_MAX_OUTPUT,