Skip to content

Commit

Permalink
feat(config): export OCO_AI_PROVIDER_ENUM to allow external access to…
Browse files Browse the repository at this point in the history
… AI provider constants

refactor(config): simplify mergeObjects function to improve readability and maintainability
refactor(setConfig): remove unnecessary keysToSet variable to streamline logging
refactor(engine): update switch cases to use OCO_AI_PROVIDER_ENUM for better consistency and clarity
  • Loading branch information
di-sukharev committed Aug 20, 2024
1 parent 42a3649 commit 5fa12e2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
28 changes: 11 additions & 17 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ export const configValidators = {
}
};

enum OCO_AI_PROVIDER_ENUM {
export enum OCO_AI_PROVIDER_ENUM {
OLLAMA = 'ollama',
OPENAI = 'openai',
ANTHROPIC = 'anthropic',
GEMINI = 'gemini',
Expand Down Expand Up @@ -458,7 +459,7 @@ export const getConfig = ({
}: {
configPath?: string;
envPath?: string;
} = {}) => {
} = {}): ConfigType => {
dotenv.config({ path: envPath });

const configFromEnv = {
Expand Down Expand Up @@ -501,15 +502,12 @@ export const getConfig = ({
globalConfig = iniParse(configFile) as ConfigType;
}

function mergeObjects(main: Partial<ConfigType>, fallback: ConfigType) {
return Object.keys(fallback).reduce(
(acc, key) => {
acc[key] = main[key] !== undefined ? main[key] : fallback[key];
return acc;
},
{ ...main }
);
}
const mergeObjects = (main: Partial<ConfigType>, fallback: ConfigType) =>
Object.keys(fallback).reduce((acc, key) => {
acc[key] = parseEnvVarValue(main[key] || fallback[key]);

return acc;
}, {} as ConfigType);

// env config takes precedence over global ~/.opencommit config file
const config = mergeObjects(configFromEnv, globalConfig);
Expand All @@ -521,10 +519,6 @@ export const setConfig = (
keyValues: [key: string, value: string][],
configPath: string = defaultConfigPath
) => {
const keysToSet = keyValues
.map(([key, value]) => `${key} to ${value}`)
.join(', ');

const config = getConfig();

for (let [key, value] of keyValues) {
Expand All @@ -548,14 +542,14 @@ export const setConfig = (
config
);

config[key as CONFIG_KEYS] = validValue;
config[key] = validValue;
}

writeFileSync(configPath, iniStringify(config), 'utf8');

assertConfigsAreValid(config);

outro(`${chalk.green('✔')} config successfully set: ${keysToSet}`);
outro(`${chalk.green('✔')} config successfully set`);
};

export const configCommand = command(
Expand Down
16 changes: 7 additions & 9 deletions src/utils/engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getConfig } from '../commands/config';
import { getConfig, OCO_AI_PROVIDER_ENUM } from '../commands/config';
import { AnthropicEngine } from '../engine/anthropic';
import { AzureEngine } from '../engine/azure';
import { AiEngine } from '../engine/Engine';
Expand All @@ -20,38 +20,36 @@ export function getEngine(): AiEngine {
};

switch (provider) {
case provider?.startsWith('ollama') && provider:
const model = provider.substring('ollama/'.length);
case OCO_AI_PROVIDER_ENUM.OLLAMA:
return new OllamaAi({
...DEFAULT_CONFIG,
apiKey: '',
model,
baseURL: config.OCO_OLLAMA_API_URL!
});

case 'anthropic':
case OCO_AI_PROVIDER_ENUM.ANTHROPIC:
return new AnthropicEngine({
...DEFAULT_CONFIG,
apiKey: config.OCO_ANTHROPIC_API_KEY!
});

case 'test':
case OCO_AI_PROVIDER_ENUM.TEST:
return new TestAi(config.OCO_TEST_MOCK_TYPE as TestMockType);

case 'gemini':
case OCO_AI_PROVIDER_ENUM.GEMINI:
return new Gemini({
...DEFAULT_CONFIG,
apiKey: config.OCO_GEMINI_API_KEY!,
baseURL: config.OCO_GEMINI_BASE_PATH!
});

case 'azure':
case OCO_AI_PROVIDER_ENUM.AZURE:
return new AzureEngine({
...DEFAULT_CONFIG,
apiKey: config.OCO_AZURE_API_KEY!
});

case 'flowise':
case OCO_AI_PROVIDER_ENUM.FLOWISE:
return new FlowiseAi({
...DEFAULT_CONFIG,
baseURL: config.OCO_FLOWISE_ENDPOINT || DEFAULT_CONFIG.baseURL,
Expand Down

0 comments on commit 5fa12e2

Please sign in to comment.