Skip to content

Commit

Permalink
refactor(config.ts): improve code readability by formatting array ele…
Browse files Browse the repository at this point in the history
…ments and conditions

fix(migrations): handle undefined values correctly when setting default config values
fix(migrations): ensure process exits with error code on migration failure
  • Loading branch information
di-sukharev committed Sep 7, 2024
1 parent 03b570c commit 2c07d5b
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 29 deletions.
46 changes: 39 additions & 7 deletions out/cli.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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) => {
Expand All @@ -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];
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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
};
Expand Down Expand Up @@ -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);
}
};

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -45738,6 +45769,7 @@ var runMigrations = async () => {
ce(
`${source_default.red("Failed to apply migration")} ${migration.name}: ${error}`
);
process.exit(1);
}
isMigrated = true;
}
Expand Down
40 changes: 35 additions & 5 deletions out/github-action.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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];
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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);
}
};

Expand Down Expand Up @@ -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);
}
Expand Down
37 changes: 21 additions & 16 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
};

Expand Down Expand Up @@ -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)`
);

Expand Down Expand Up @@ -301,7 +307,7 @@ export enum OCO_AI_PROVIDER_ENUM {
AZURE = 'azure',
TEST = 'test',
FLOWISE = 'flowise',
GROQ = 'groq',
GROQ = 'groq'
}

export type ConfigType = {
Expand Down Expand Up @@ -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
};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/migrations/02_set_missing_default_values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/migrations/_run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const runMigrations = async () => {
migration.name
}: ${error}`
);
process.exit(1);
}

isMigrated = true;
Expand Down

0 comments on commit 2c07d5b

Please sign in to comment.