Skip to content

Commit 6f24afc

Browse files
committed
feature(commands/configs.ts, engine/flowise.ts, utils/engine.ts): Incorporated flowise in configuration variables, added it as an AiEngine, and added it as a return value for getEngine()
1 parent a80dcb0 commit 6f24afc

File tree

7 files changed

+921
-749
lines changed

7 files changed

+921
-749
lines changed

out/cli.cjs

Lines changed: 420 additions & 372 deletions
Large diffs are not rendered by default.

out/github-action.cjs

Lines changed: 412 additions & 364 deletions
Large diffs are not rendered by default.

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/config.ts

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ export enum CONFIG_KEYS {
3333
OCO_AZURE_ENDPOINT = 'OCO_AZURE_ENDPOINT',
3434
OCO_TEST_MOCK_TYPE = 'OCO_TEST_MOCK_TYPE',
3535
OCO_API_URL = 'OCO_API_URL',
36-
OCO_OLLAMA_API_URL = 'OCO_OLLAMA_API_URL'
36+
OCO_OLLAMA_API_URL = 'OCO_OLLAMA_API_URL',
37+
OCO_FLOWISE_ENDPOINT = 'OCO_FLOWISE_ENDPOINT',
38+
OCO_FLOWISE_API_KEY = 'OCO_FLOWISE_API_KEY'
3739
}
3840

3941
export enum CONFIG_MODES {
@@ -119,7 +121,7 @@ const validateConfig = (
119121
}
120122
};
121123

122-
export const configValidators = {
124+
export const configValidators = {
123125
[CONFIG_KEYS.OCO_OPENAI_API_KEY](value: any, config: any = {}) {
124126
if (config.OCO_AI_PROVIDER == 'gemini') return value;
125127

@@ -130,7 +132,8 @@ export const configValidators = {
130132
config.OCO_ANTHROPIC_API_KEY ||
131133
config.OCO_AI_PROVIDER.startsWith('ollama') ||
132134
config.OCO_AZURE_API_KEY ||
133-
config.OCO_AI_PROVIDER == 'test',
135+
config.OCO_AI_PROVIDER == 'test' ||
136+
config.OCO_AI_PROVIDER == 'flowise',
134137
'You need to provide an OpenAI/Anthropic/Azure API key'
135138
);
136139
validateConfig(
@@ -149,7 +152,8 @@ export const configValidators = {
149152
config.OCO_OPENAI_API_KEY ||
150153
config.OCO_AZURE_API_KEY ||
151154
config.OCO_AI_PROVIDER == 'ollama' ||
152-
config.OCO_AI_PROVIDER == 'test',
155+
config.OCO_AI_PROVIDER == 'test' ||
156+
config.OCO_AI_PROVIDER == 'flowise',
153157
'You need to provide an OpenAI/Anthropic/Azure API key'
154158
);
155159

@@ -175,13 +179,25 @@ export const configValidators = {
175179
value ||
176180
config.OCO_OPENAI_API_KEY ||
177181
config.OCO_AI_PROVIDER == 'ollama' ||
178-
config.OCO_AI_PROVIDER == 'test',
182+
config.OCO_AI_PROVIDER == 'test' ||
183+
config.OCO_AI_PROVIDER == 'flowise',
179184
'You need to provide an OpenAI/Anthropic API key'
180185
);
181186

182187
return value;
183188
},
184189

190+
[CONFIG_KEYS.OCO_FLOWISE_API_KEY](value: any, config: any = {}) {
191+
validateConfig(
192+
CONFIG_KEYS.OCO_FLOWISE_API_KEY,
193+
value || config.OCO_AI_PROVIDER != 'flowise',
194+
'You need to provide a flowise API key'
195+
);
196+
197+
return value;
198+
},
199+
200+
185201
[CONFIG_KEYS.OCO_DESCRIPTION](value: any) {
186202
validateConfig(
187203
CONFIG_KEYS.OCO_DESCRIPTION,
@@ -268,7 +284,8 @@ export const configValidators = {
268284
].includes(value) ||
269285
config.OCO_AI_PROVIDER == 'ollama' ||
270286
config.OCO_AI_PROVIDER == 'azure' ||
271-
config.OCO_AI_PROVIDER == 'test',
287+
config.OCO_AI_PROVIDER == 'test'||
288+
config.OCO_AI_PROVIDER == 'flowise',
272289
`${value} is not supported yet, use:\n\n ${[
273290
...MODEL_LIST.openai,
274291
...MODEL_LIST.anthropic,
@@ -308,9 +325,9 @@ export const configValidators = {
308325
[CONFIG_KEYS.OCO_AI_PROVIDER](value: any) {
309326
validateConfig(
310327
CONFIG_KEYS.OCO_AI_PROVIDER,
311-
['', 'openai', 'anthropic', 'gemini', 'azure', 'test'].includes(value) ||
328+
['', 'openai', 'anthropic', 'gemini', 'azure', 'test', 'flowise'].includes(value) ||
312329
value.startsWith('ollama'),
313-
`${value} is not supported yet, use 'ollama', 'anthropic', 'azure', 'gemini' or 'openai' (default)`
330+
`${value} is not supported yet, use 'ollama', 'anthropic', 'azure', 'gemini', 'flowise' or 'openai' (default)`
314331
);
315332
return value;
316333
},
@@ -324,6 +341,7 @@ export const configValidators = {
324341

325342
return value;
326343
},
344+
327345
[CONFIG_KEYS.OCO_AZURE_ENDPOINT](value: any) {
328346
validateConfig(
329347
CONFIG_KEYS.OCO_AZURE_ENDPOINT,
@@ -333,6 +351,17 @@ export const configValidators = {
333351

334352
return value;
335353
},
354+
355+
[CONFIG_KEYS.OCO_FLOWISE_ENDPOINT](value: any) {
356+
validateConfig(
357+
CONFIG_KEYS.OCO_FLOWISE_ENDPOINT,
358+
typeof value === 'string' && value.includes(':'),
359+
'Value must be string and should include both I.P. and port number' // Considering the possibility of DNS lookup or feeding the I.P. explicitely, there is no pattern to verify, except a column for the port number
360+
);
361+
362+
return value;
363+
},
364+
336365
[CONFIG_KEYS.OCO_TEST_MOCK_TYPE](value: any) {
337366
validateConfig(
338367
CONFIG_KEYS.OCO_TEST_MOCK_TYPE,
@@ -361,7 +390,6 @@ export type ConfigType = {
361390

362391
const defaultConfigPath = pathJoin(homedir(), '.opencommit');
363392
const defaultEnvPath = pathResolve(process.cwd(), '.env');
364-
365393
export const getConfig = ({
366394
configPath = defaultConfigPath,
367395
envPath = defaultEnvPath
@@ -396,9 +424,10 @@ export const getConfig = ({
396424
OCO_ONE_LINE_COMMIT:
397425
process.env.OCO_ONE_LINE_COMMIT === 'true' ? true : false,
398426
OCO_AZURE_ENDPOINT: process.env.OCO_AZURE_ENDPOINT || '',
399-
OCO_TEST_MOCK_TYPE: process.env.OCO_TEST_MOCK_TYPE || 'commit-message'
427+
OCO_TEST_MOCK_TYPE: process.env.OCO_TEST_MOCK_TYPE || 'commit-message',
428+
OCO_FLOWISE_ENDPOINT: process.env.OCO_FLOWISE_ENDPOINT || ':',
429+
OCO_FLOWISE_API_KEY: process.env.OCO_FLOWISE_API_KEY || 'undefined'
400430
};
401-
402431
const configExists = existsSync(configPath);
403432
if (!configExists) return configFromEnv;
404433

@@ -437,7 +466,7 @@ export const setConfig = (
437466
const config = getConfig() || {};
438467

439468
for (const [configKey, configValue] of keyValues) {
440-
if (!configValidators.hasOwnProperty(configKey)) {
469+
if (!configValidators.hasOwnProperty(configKey)) {
441470
throw new Error(`Unsupported config key: ${configKey}`);
442471
}
443472

src/engine/flowise.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import axios, { AxiosError } from 'axios';
2+
import { ChatCompletionRequestMessage } from 'openai';
3+
import { AiEngine } from './Engine';
4+
5+
import {
6+
getConfig
7+
} from '../commands/config';
8+
9+
const config = getConfig();
10+
11+
export class FlowiseAi implements AiEngine {
12+
13+
async generateCommitMessage(
14+
messages: Array<ChatCompletionRequestMessage>
15+
): Promise<string | undefined> {
16+
17+
const gitDiff = messages[ messages.length - 1 ]?.content?.replace(/\\/g, '\\\\')
18+
.replace(/"/g, '\\"')
19+
.replace(/\n/g, '\\n')
20+
.replace(/\r/g, '\\r')
21+
.replace(/\t/g, '\\t');
22+
const url = `http://${config?.OCO_FLOWISE_ENDPOINT}/api/v1/prediction/${config?.OCO_FLOWISE_API_KEY}`;
23+
const payload = {
24+
question : gitDiff,
25+
overrideConfig : {
26+
systemMessagePrompt: messages[0]?.content,
27+
},
28+
history : messages.slice( 1, -1 )
29+
}
30+
try {
31+
const response = await axios.post(url, payload, {
32+
headers: {
33+
'Content-Type': 'application/json'
34+
}
35+
});
36+
const message = response.data;
37+
return message?.text;
38+
} catch (err: any) {
39+
const message = err.response?.data?.error ?? err.message;
40+
throw new Error('local model issues. details: ' + message);
41+
}
42+
}
43+
}

src/utils/engine.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { OllamaAi } from '../engine/ollama';
66
import { AnthropicAi } from '../engine/anthropic'
77
import { TestAi } from '../engine/testAi';
88
import { Azure } from '../engine/azure';
9+
import { FlowiseAi } from '../engine/flowise'
910

1011
export function getEngine(): AiEngine {
1112
const config = getConfig();
@@ -25,6 +26,8 @@ export function getEngine(): AiEngine {
2526
return new Gemini();
2627
} else if (provider == 'azure') {
2728
return new Azure();
29+
} else if( provider == 'flowise'){
30+
return new FlowiseAi();
2831
}
2932

3033
//open ai gpt by default

test/unit/config.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,4 @@ OCO_ONE_LINE_COMMIT="true"
102102

103103
await envFile.cleanup();
104104
});
105-
});
105+
});

0 commit comments

Comments
 (0)