Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hostname config to support other api services #232

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/commands/aicommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default async (
config['max-length'],
config.type,
config.timeout,
config.hostname,
config.proxy,
);
} finally {
Expand Down
1 change: 1 addition & 0 deletions src/commands/prepare-commit-msg-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export default () => (async () => {
config['max-length'],
config.type,
config.timeout,
config.hostname,
config.proxy,
);
} finally {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ const configParsers = {

return parsed;
},
hostname(hostname?: string) {
if (!hostname) {
return 'api.openai.com';
}

// eslint-disable-next-line regexp/no-unused-capturing-group
parseAssert('hostname', /^([a-zA-Z\d-]+\.)*[a-zA-Z\d-]+\.[a-zA-Z]+$/.test(hostname), 'Must be an hostname');
savoygu marked this conversation as resolved.
Show resolved Hide resolved

return hostname;
},
} as const;

type ConfigKeys = keyof typeof configParsers;
Expand Down
5 changes: 4 additions & 1 deletion src/utils/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ const createChatCompletion = async (
apiKey: string,
json: CreateChatCompletionRequest,
timeout: number,
hostname: string,
proxy?: string,
) => {
const { response, data } = await httpsPost(
'api.openai.com',
hostname,
'/v1/chat/completions',
{
Authorization: `Bearer ${apiKey}`,
Expand Down Expand Up @@ -131,6 +132,7 @@ export const generateCommitMessage = async (
maxLength: number,
type: CommitType,
timeout: number,
hostname: string,
proxy?: string,
) => {
try {
Expand All @@ -157,6 +159,7 @@ export const generateCommitMessage = async (
n: completions,
},
timeout,
hostname,
proxy,
);

Expand Down
24 changes: 24 additions & 0 deletions tests/specs/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,30 @@ export default testSuite(({ describe }) => {
});
});

await describe('hostname', ({ test }) => {
test('must be an hostname', async () => {
const { stderr } = await aicommits(['config', 'set', 'hostname=https://api.openai.com'], {
reject: false,
});

expect(stderr).toMatch('Must be an hostname');
});

test('updates config', async () => {
const defaultConfig = await aicommits(['config', 'get', 'hostname']);
expect(defaultConfig.stdout).toBe('hostname=api.openai.com');

const hostname = 'hostname=api.chatanywhere.com.cn';
await aicommits(['config', 'set', hostname]);

const configFile = await fs.readFile(configPath, 'utf8');
expect(configFile).toMatch(hostname);

const get = await aicommits(['config', 'get', 'hostname']);
expect(get.stdout).toBe(hostname);
});
});

await test('set config file', async () => {
await aicommits(['config', 'set', openAiToken]);

Expand Down
2 changes: 1 addition & 1 deletion tests/specs/openai/conventional-commits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export default testSuite(({ describe }) => {
'max-length': 50,
...configOverrides,
} as ValidConfig;
const commitMessages = await generateCommitMessage(OPENAI_KEY!, 'gpt-3.5-turbo', config.locale, gitDiff, config.generate, config['max-length'], config.type, 7000);
const commitMessages = await generateCommitMessage(OPENAI_KEY!, 'gpt-3.5-turbo', config.locale, gitDiff, config.generate, config['max-length'], config.type, 7000, config.hostname);

return commitMessages[0];
}
Expand Down