Skip to content

Commit

Permalink
Merge pull request #64 from tak-bro/feature/update-config2
Browse files Browse the repository at this point in the history
Feature/update config2
  • Loading branch information
tak-bro committed Aug 13, 2024
2 parents bff0d73 + db1c82d commit 9139752
Show file tree
Hide file tree
Showing 27 changed files with 1,303 additions and 1,301 deletions.
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"globals": { "BigInt": true, "console": true, "WebAssembly": true },
"rules": {
"@typescript-eslint/ban-ts-comment": [
"error",
{
"ts-ignore": "allow-with-description"
}
],
"import/order": [
"error",
{
Expand Down
758 changes: 379 additions & 379 deletions README.md

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@
"dependencies": {
"@anthropic-ai/sdk": "^0.20.8",
"@dqbd/tiktoken": "^1.0.2",
"@google/generative-ai": "^0.11.4",
"@google/generative-ai": "^0.15.0",
"@inquirer/prompts": "^3.0.0",
"@pacote/xxhash": "^0.3.2",
"axios": "^1.6.8",
"chalk": "^5.3.0",
"cohere-ai": "^7.10.2",
"cohere-ai": "^7.11.0",
"copy-paste": "^1.5.3",
"figlet": "^1.7.0",
"formdata-node": "^6.0.3",
"groq-sdk": "^0.4.0",
"inquirer": "9.2.8",
"inquirer-reactive-list-prompt": "^1.0.8",
"inquirer-reactive-list-prompt": "^1.0.9",
"ollama": "^0.5.6",
"ora": "^8.0.1",
"readline": "^1.3.0",
Expand Down
24 changes: 12 additions & 12 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ cli(
},
prompt: {
type: String,
description: 'Additional prompt to let users fine-tune provided prompt',
description: 'Custom prompt to let users fine-tune provided prompt',
alias: 'p',
},
},
Expand Down
66 changes: 29 additions & 37 deletions src/commands/aicommit2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ import ora from 'ora';
import { AIRequestManager } from '../managers/ai-request.manager.js';
import { ConsoleManager } from '../managers/console.manager.js';
import { ReactivePromptManager } from '../managers/reactive-prompt.manager.js';
import { AIType, ApiKeyName, ApiKeyNames } from '../services/ai/ai.service.js';
import { getConfig } from '../utils/config.js';
import { ModelName, RawConfig, getConfig, modelNames } from '../utils/config.js';
import { KnownError, handleCliError } from '../utils/error.js';
import { assertGitRepo, getStagedDiff } from '../utils/git.js';


const consoleManager = new ConsoleManager();

export default async (
Expand All @@ -25,7 +23,7 @@ export default async (
commitType: string | undefined,
confirm: boolean,
useClipboard: boolean,
promptPath: string | undefined,
prompt: string | undefined,
rawArgv: string[]
) =>
(async () => {
Expand All @@ -46,56 +44,50 @@ export default async (
}
consoleManager.printStagedFiles(staged);

const { env } = process;
const config = await getConfig({
OPENAI_KEY: env.OPENAI_KEY || env.OPENAI_API_KEY,
OPENAI_MODEL: env.OPENAI_MODEL || env['openai-model'] || env['openai_model'],
OPENAI_URL: env.OPENAI_URL || env['openai-url'] || env['OPENAI_URL'],
GEMINI_KEY: env.GEMINI_KEY || env.GEMINI_API_KEY,
GEMINI_MODEL: env.GEMINI_MODEL || env['gemini-model'] || env['gemini_model'],
ANTHROPIC_KEY: env.ANTHROPIC_KEY || env.ANTHROPIC_API_KEY,
ANTHROPIC_MODEL: env.ANTHROPIC_MODEL || env['anthropic-model'] || env['anthropic_model'],
HUGGINGFACE_MODEL: env.HUGGINGFACE_MODEL,
MISTRAL_KEY: env.MISTRAL_KEY || env.MISTRAL_API_KEY,
CODESTRAL_KEY: env.CODESTRAL_KEY || env.CODESTRAL_API_KEY,
MISTRAL_MODEL: env.MISTRAL_MODEL || env['mistral-model'] || env['mistral_model'],
proxy: env.https_proxy || env.HTTPS_PROXY || env.http_proxy || env.HTTP_PROXY,
temperature: env.temperature,
generate: generate?.toString() || env.generate,
type: commitType?.toString() || env.type,
locale: locale?.toString() || env.locale,
promptPath: promptPath?.toString() || env.promptPath,
});

if (config.promptPath) {
const config = await getConfig(
{
locale: locale?.toString() as string,
generate: generate?.toString() as string,
commitType: commitType?.toString() as string,
systemPrompt: prompt?.toString() as string,
},
rawArgv
);

if (config.systemPromptPath) {
try {
fs.readFileSync(path.resolve(config.promptPath), 'utf-8');
fs.readFileSync(path.resolve(config.systemPromptPath), 'utf-8');
} catch (error) {
throw new KnownError(`Error reading user prompt file: ${config.promptPath}`);
throw new KnownError(`Error reading system prompt file: ${config.systemPromptPath}`);
}
}

const availableAPIKeyNames: ApiKeyName[] = Object.entries(config)
.filter(([key]) => ApiKeyNames.includes(key as ApiKeyName))
const availableAIs: ModelName[] = Object.entries(config)
.filter(([key]) => modelNames.includes(key as ModelName))
.map(([key, value]) => [key, value] as [ModelName, RawConfig])
.filter(([key, value]) => {
if (key === AIType.OLLAMA) {
return !!value && (value as string[]).length > 0;
if (key === 'OLLAMA') {
return !!value && !!value.model && (value.model as string[]).length > 0;
}
if (key === 'HUGGINGFACE') {
return !!value && !!value.cookie;
}
return !!value;
// @ts-ignore ignore
return !!value.key && value.key.length > 0;
})
.map(([key]) => key as ApiKeyName);
.map(([key]) => key);

const hasNoAvailableAIs = availableAPIKeyNames.length === 0;
const hasNoAvailableAIs = availableAIs.length === 0;
if (hasNoAvailableAIs) {
throw new KnownError('Please set at least one API key via the `aicommit2 config set` command');
}

const aiRequestManager = new AIRequestManager(config, staged);
const reactivePromptManager = new ReactivePromptManager();
const selectPrompt = reactivePromptManager.initPrompt(!config.ignoreBody);
const selectPrompt = reactivePromptManager.initPrompt();

reactivePromptManager.startLoader();
const subscription = aiRequestManager.createAIRequests$(availableAPIKeyNames).subscribe(
const subscription = aiRequestManager.createAIRequests$(availableAIs).subscribe(
(choice: ReactiveListChoice) => reactivePromptManager.refreshChoices(choice),
() => {
/* empty */
Expand Down
15 changes: 13 additions & 2 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { command } from 'cleye';

import { ConsoleManager } from '../managers/console.manager.js';
import { getConfig, hasOwn, setConfigs } from '../utils/config.js';
import { ModelName, addConfigs, getConfig, hasOwn, modelNames, setConfigs } from '../utils/config.js';
import { KnownError, handleCliError } from '../utils/error.js';

export default command(
Expand All @@ -14,9 +14,15 @@ export default command(
const { mode, keyValue: keyValues } = argv._;

if (mode === 'get') {
const config = await getConfig({}, true);
const config = await getConfig({}, []);
for (const key of keyValues) {
if (hasOwn(config, key)) {
const isModel = modelNames.includes(key as ModelName);
if (isModel) {
// @ts-ignore ignore
console.log(key, config[key]);
return;
}
console.log(`${key}=${config[key as keyof typeof config]}`);
}
}
Expand All @@ -28,6 +34,11 @@ export default command(
return;
}

if (mode === 'add') {
await addConfigs(keyValues.map(keyValue => keyValue.split('=') as [string, string]));
return;
}

throw new KnownError(`Invalid mode: ${mode}`);
})().catch(error => {
const commandLineManager = new ConsoleManager();
Expand Down
39 changes: 24 additions & 15 deletions src/commands/prepare-commit-msg-hook.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fs from 'fs/promises';
import path from 'path';

import { filter, lastValueFrom, map, toArray } from 'rxjs';

import { AIRequestManager } from '../managers/ai-request.manager.js';
import { ConsoleManager } from '../managers/console.manager.js';
import { AIType, ApiKeyName, ApiKeyNames } from '../services/ai/ai.service.js';
import { getConfig } from '../utils/config.js';
import { ModelName, RawConfig, getConfig, modelNames } from '../utils/config.js';
import { KnownError, handleCliError } from '../utils/error.js';
import { getStagedDiff } from '../utils/git.js';

Expand All @@ -31,32 +31,41 @@ export default () =>
const consoleManager = new ConsoleManager();
consoleManager.printTitle();

const { env } = process;
const config = await getConfig({
proxy: env.https_proxy || env.HTTPS_PROXY || env.http_proxy || env.HTTP_PROXY,
});
const config = await getConfig({});
if (config.systemPromptPath) {
try {
await fs.readFile(path.resolve(config.systemPromptPath), 'utf-8');
} catch (error) {
throw new KnownError(`Error reading system prompt file: ${config.systemPromptPath}`);
}
}

const availableAPIKeyNames: ApiKeyName[] = Object.entries(config)
.filter(([key]) => ApiKeyNames.includes(key as ApiKeyName))
const availableAIs: ModelName[] = Object.entries(config)
.filter(([key]) => modelNames.includes(key as ModelName))
.map(([key, value]) => [key, value] as [ModelName, RawConfig])
.filter(([key, value]) => {
if (key === AIType.OLLAMA) {
return !!value && (value as string[]).length > 0;
if (key === 'OLLAMA') {
return !!value && !!value.model && (value.model as string[]).length > 0;
}
if (key === 'HUGGINGFACE') {
return !!value && !!value.cookie;
}
return !!value;
// @ts-ignore ignore
return !!value.key && value.key.length > 0;
})
.map(([key]) => key as ApiKeyName);
.map(([key]) => key);

const hasNoAvailableAIs = availableAPIKeyNames.length === 0;
const hasNoAvailableAIs = availableAIs.length === 0;
if (hasNoAvailableAIs) {
throw new KnownError('Please set at least one API key via `aicommit2 config set OPENAI_KEY=<your token>`');
throw new KnownError('Please set at least one API key via the `aicommit2 config set` command');
}

const aiRequestManager = new AIRequestManager(config, staged);
const spinner = consoleManager.displaySpinner('The AI is analyzing your changes');
let messages: string[];
try {
messages = await lastValueFrom(
aiRequestManager.createAIRequests$(availableAPIKeyNames).pipe(
aiRequestManager.createAIRequests$(availableAIs).pipe(
filter(data => !data.isError),
map(data => data.value),
toArray()
Expand Down
Loading

0 comments on commit 9139752

Please sign in to comment.