Skip to content

Commit

Permalink
fix: fix ignoreBody config
Browse files Browse the repository at this point in the history
  • Loading branch information
tak-bro committed Aug 13, 2024
1 parent 3ff61b8 commit 1faf9bd
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 64 deletions.
3 changes: 3 additions & 0 deletions src/commands/aicommit2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export default async (
rawArgv
);

console.log(config);
// throw new KnownError('Please set at least one API key via the `aicommit2 config set` command');

if (config.systemPromptPath) {
try {
fs.readFileSync(path.resolve(config.systemPromptPath), 'utf-8');
Expand Down
2 changes: 1 addition & 1 deletion src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ 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)) {
console.log(`${key}=${config[key as keyof typeof config]}`);
Expand Down
30 changes: 1 addition & 29 deletions src/services/ai/ai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,6 @@ import { Observable, of } from 'rxjs';
import { CommitType, ModelConfig, ModelName } from '../../utils/config.js';
import { StagedDiff } from '../../utils/git.js';

// NOTE: get AI Type from key names
export const AIType = {
OPEN_AI: 'OPENAI_KEY',
GEMINI: 'GEMINI_KEY',
ANTHROPIC: 'ANTHROPIC_KEY',
HUGGINGFACE: 'HUGGINGFACE_COOKIE',
MISTRAL: 'MISTRAL_KEY',
CODESTRAL: 'CODESTRAL_KEY',
OLLAMA: 'OLLAMA_MODEL',
COHERE: 'COHERE_KEY',
GROQ: 'GROQ_KEY',
PERPLEXITY: 'PERPLEXITY_KEY',
} as const;
export type ApiKeyName = (typeof AIType)[keyof typeof AIType];
export const ApiKeyNames: ApiKeyName[] = Object.values(AIType).map(value => value);

export interface CommitMessage {
title: string;
value: string;
Expand Down Expand Up @@ -74,18 +58,12 @@ export abstract class AIService {
});
};

protected parseMessage(generatedText: string, type: CommitType, maxCount: number, ignoreBody: boolean): CommitMessage[] {
protected parseMessage(generatedText: string, type: CommitType, maxCount: number): CommitMessage[] {
try {
const commitMessages: RawCommitMessage[] = JSON.parse(generatedText);
const filteredMessages = commitMessages
.map(data => this.extractMessageAsType(data, type))
.map((data: RawCommitMessage) => {
if (ignoreBody) {
return {
title: `${data.subject}`,
value: `${data.subject}`,
};
}
return {
title: `${data.subject}`,
value: `${data.subject}${data.body ? `\n\n${data.body}` : ''}${data.footer ? `\n\n${data.footer}` : ''}`,
Expand All @@ -109,12 +87,6 @@ export abstract class AIService {
const filteredessages = commitMessages
.map(data => this.extractMessageAsType(data, type))
.map((data: RawCommitMessage) => {
if (ignoreBody) {
return {
title: `${data.subject}`,
value: `${data.subject}`,
};
}
return {
title: `${data.subject}`,
value: `${data.subject}${data.body ? `\n\n${data.body}` : ''}${data.footer ? `\n\n${data.footer}` : ''}`,
Expand Down
6 changes: 3 additions & 3 deletions src/services/ai/anthropic.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export class AnthropicService extends AIService {
map(data => ({
name: `${this.serviceName} ${data.title}`,
short: data.title,
value: data.value,
description: data.value,
value: this.params.config.ignoreBody ? data.title : data.value,
description: this.params.config.ignoreBody ? '' : data.value,
isError: false,
})),
catchError(this.handleError$)
Expand Down Expand Up @@ -76,7 +76,7 @@ export class AnthropicService extends AIService {
const result: Anthropic.Message = await this.anthropic.messages.create(params);
const completion = result.content.map(({ text }) => text).join('');
logging && createLogResponse('Anthropic', diff, generatedSystemPrompt, completion);
return this.parseMessage(completion, type, generate, this.params.config.ignoreBody);
return this.parseMessage(completion, type, generate);
} catch (error) {
const errorAsAny = error as any;
if (errorAsAny.code === 'ENOTFOUND') {
Expand Down
8 changes: 4 additions & 4 deletions src/services/ai/codestral.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ export class CodestralService extends AIService {
return fromPromise(this.generateMessage()).pipe(
concatMap(messages => from(messages)),
map(data => ({
name: `${this.serviceName} ${data.title}`,
name: `${this.serviceName} ${this.params.config.ignoreBody} ${data.title}`,
short: data.title,
value: data.value,
description: data.value,
value: this.params.config.ignoreBody ? data.title : data.value,
description: this.params.config.ignoreBody ? '' : data.value,
isError: false,
})),
catchError(this.handleError$)
Expand All @@ -58,7 +58,7 @@ export class CodestralService extends AIService {
this.checkAvailableModels();
const chatResponse = await this.createChatCompletions(generatedSystemPrompt);
logging && createLogResponse('Codestral', diff, generatedSystemPrompt, chatResponse);
return this.parseMessage(chatResponse, type, generate, this.params.config.ignoreBody);
return this.parseMessage(chatResponse, type, generate);
} catch (error) {
const errorAsAny = error as any;
if (errorAsAny.code === 'ENOTFOUND') {
Expand Down
6 changes: 3 additions & 3 deletions src/services/ai/cohere.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export class CohereService extends AIService {
map(data => ({
name: `${this.serviceName} ${data.title}`,
short: data.title,
value: data.value,
description: data.value,
value: this.params.config.ignoreBody ? data.title : data.value,
description: this.params.config.ignoreBody ? '' : data.value,
isError: false,
})),
catchError(this.handleError$)
Expand Down Expand Up @@ -67,7 +67,7 @@ export class CohereService extends AIService {
});

logging && createLogResponse('Cohere', diff, generatedSystemPrompt, prediction.text);
return this.parseMessage(prediction.text, type, generate, this.params.config.ignoreBody);
return this.parseMessage(prediction.text, type, generate);
} catch (error) {
const errorAsAny = error as any;
if (errorAsAny instanceof CohereTimeoutError) {
Expand Down
6 changes: 3 additions & 3 deletions src/services/ai/gemini.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export class GeminiService extends AIService {
map(data => ({
name: `${this.serviceName} ${data.title}`,
short: data.title,
value: data.value,
description: data.value,
value: this.params.config.ignoreBody ? data.title : data.value,
description: this.params.config.ignoreBody ? '' : data.value,
isError: false,
})),
catchError(this.handleError$)
Expand Down Expand Up @@ -66,7 +66,7 @@ export class GeminiService extends AIService {
const completion = response.text();

logging && createLogResponse('Gemini', diff, generatedSystemPrompt, completion);
return this.parseMessage(completion, type, generate, this.params.config.ignoreBody);
return this.parseMessage(completion, type, generate);
} catch (error) {
const errorAsAny = error as any;
if (errorAsAny.code === 'ENOTFOUND') {
Expand Down
6 changes: 3 additions & 3 deletions src/services/ai/groq.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export class GroqService extends AIService {
map(data => ({
name: `${this.serviceName} ${data.title}`,
short: data.title,
value: data.value,
description: data.value,
value: this.params.config.ignoreBody ? data.title : data.value,
description: this.params.config.ignoreBody ? '' : data.value,
isError: false,
})),
catchError(this.handleError$)
Expand Down Expand Up @@ -85,7 +85,7 @@ export class GroqService extends AIService {
const results = chatCompletion.choices
.filter(choice => choice.message?.content)
.map(choice => sanitizeMessage(choice.message!.content as string));
return flattenDeep(results.map(value => this.parseMessage(value, type, generate, this.params.config.ignoreBody)));
return flattenDeep(results.map(value => this.parseMessage(value, type, generate)));
} catch (error) {
throw error as any;
}
Expand Down
10 changes: 5 additions & 5 deletions src/services/ai/hugging-face.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ export class HuggingFaceService extends AIService {
return fromPromise(this.generateMessage()).pipe(
concatMap(messages => from(messages)),
map(data => ({
name: `${this.serviceName} ${data.title}`,
name: `${this.serviceName} ${this.params.config.ignoreBody} ${data.title}`,
short: data.title,
value: data.value,
description: data.value,
value: this.params.config.ignoreBody ? data.title : data.value,
description: this.params.config.ignoreBody ? '' : data.value,
isError: false,
})),
catchError(this.handleError$)
Expand Down Expand Up @@ -113,7 +113,7 @@ export class HuggingFaceService extends AIService {
// await this.deleteConversation(conversation.id);

logging && createLogResponse('HuggingFace', diff, generatedSystemPrompt, response);
return this.parseMessage(response, type, generate, this.params.config.ignoreBody);
return this.parseMessage(response, type, generate);
} catch (error) {
const errorAsAny = error as any;
if (errorAsAny.code === 'ENOTFOUND') {
Expand All @@ -130,7 +130,7 @@ export class HuggingFaceService extends AIService {
*/
private async intialize(): Promise<void> {
const models = await this.getRemoteLlms();
const model = models.find(model => model.name?.toLowerCase() === this.params.config.HUGGINGFACE_MODEL.toLowerCase())!;
const model = models.find(model => model.name?.toLowerCase() === this.params.config.model.toLowerCase())!;
if (model) {
this.currentModel = model;
this.currentModelId = model.id;
Expand Down
6 changes: 3 additions & 3 deletions src/services/ai/mistral.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export class MistralService extends AIService {
map(data => ({
name: `${this.serviceName} ${data.title}`,
short: data.title,
value: data.value,
description: data.value,
value: this.params.config.ignoreBody ? data.title : data.value,
description: this.params.config.ignoreBody ? '' : data.value,
isError: false,
})),
catchError(this.handleError$)
Expand All @@ -90,7 +90,7 @@ export class MistralService extends AIService {
await this.checkAvailableModels();
const chatResponse = await this.createChatCompletions(generatedSystemPrompt, `Here are diff: ${diff}`);
logging && createLogResponse('MistralAI', diff, generatedSystemPrompt, chatResponse);
return this.parseMessage(chatResponse, this.params.config.type, generate, this.params.config.ignoreBody);
return this.parseMessage(chatResponse, this.params.config.type, generate);
} catch (error) {
const errorAsAny = error as any;
if (errorAsAny.code === 'ENOTFOUND') {
Expand Down
8 changes: 4 additions & 4 deletions src/services/ai/ollama.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export class OllamaService extends AIService {
return fromPromise(this.generateMessage()).pipe(
concatMap(messages => from(messages)),
map(data => ({
name: `${this.serviceName} ${data.title}`,
name: `${this.serviceName} ${this.params.config.ignoreBody} ${data.title}`,
short: data.title,
value: data.value,
description: data.value,
value: this.params.config.ignoreBody ? data.title : data.value,
description: this.params.config.ignoreBody ? '' : data.value,
isError: false,
})),
catchError(this.handleError$)
Expand Down Expand Up @@ -85,7 +85,7 @@ export class OllamaService extends AIService {
await this.checkIsAvailableOllama();
const chatResponse = await this.createChatCompletions(generatedSystemPrompt, `Here are diff: ${diff}`);
logging && createLogResponse(`Ollama_${this.model}`, diff, generatedSystemPrompt, chatResponse);
return this.parseMessage(chatResponse, type, generate, this.params.config.ignoreBody);
return this.parseMessage(chatResponse, type, generate);
} catch (error) {
const errorAsAny = error as any;
if (errorAsAny.code === 'ENOTFOUND') {
Expand Down
6 changes: 3 additions & 3 deletions src/services/ai/openai.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class OpenAIService extends AIService {
map(data => ({
name: `${this.serviceName} ${data.title}`,
short: data.title,
value: data.value,
description: data.value,
value: this.params.config.ignoreBody ? data.title : data.value,
description: this.params.config.ignoreBody ? '' : data.value,
isError: false,
})),
catchError(this.handleError$)
Expand Down Expand Up @@ -93,6 +93,6 @@ export class OpenAIService extends AIService {
proxy
);

return flattenDeep(results.map(value => this.parseMessage(value, type, generate, this.params.config.ignoreBody)));
return flattenDeep(results.map(value => this.parseMessage(value, type, generate)));
}
}
7 changes: 4 additions & 3 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,14 @@ const generalConfigParsers = {
return parsed;
},
ignoreBody(ignore?: string | boolean) {
if (!ignore) {
return true;
}
if (typeof ignore === 'boolean') {
return ignore;
}

if (ignore === undefined || ignore === null) {
return true;
}

parseAssert('ignoreBody', /^(?:true|false)$/.test(ignore), 'Must be a boolean(true or false)');
return ignore === 'true';
},
Expand Down

0 comments on commit 1faf9bd

Please sign in to comment.