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

Feature/add env openai base url and fix pr#279 #280

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: Test

on:
push:
branches: [main, develop]
branches: [ main, develop ]
pull_request:

jobs:
test:
name: Test
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
os: [ ubuntu-latest, windows-latest ]

runs-on: ${{ matrix.os }}
timeout-minutes: 10
Expand Down Expand Up @@ -44,6 +44,7 @@ jobs:
- name: Test
env:
OPENAI_KEY: ${{ secrets.OPENAI_KEY }}
OPENAI_BASE_URL: ${{ secrets.OPENAI_BASE_URL }}
run: |
pnpm test
pnpm --use-node-version=14.21.3 test
27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
3. Set the key so aicommits can use it:

```sh
aicommits config set OPENAI_KEY=<your token>
aicommits config set OPENAI_KEY=<your token> OPENAI_BASE_URL=<your-custom-openai-server-address>
```

This will create a `.aicommits` file in your home directory.
Expand Down Expand Up @@ -137,10 +137,16 @@ For example, to retrieve the API key, you can use:
aicommits config get OPENAI_KEY
```

For example, to retrieve the API URL, you can use:

```sh
aicommits config get OPENAI_BASE_URL
```

You can also retrieve multiple configuration options at once by separating them with spaces:

```sh
aicommits config get OPENAI_KEY generate
aicommits config get OPENAI_KEY OPENAI_BASE_URL generate
```

### Setting a configuration value
Expand All @@ -157,10 +163,16 @@ For example, to set the API key, you can use:
aicommits config set OPENAI_KEY=<your-api-key>
```

For example, to set the API URL, you can use:

```sh
aicommits config set OPENAI_BASE_URL=<your-custom-openai-server-address>
```

You can also set multiple configuration options at once by separating them with spaces, like

```sh
aicommits config set OPENAI_KEY=<your-api-key> generate=3 locale=en
aicommits config set OPENAI_KEY=<your-api-key> OPENAI_BASE_URL=<your-custom-openai-server-address> generate=3 locale=en
```

### Options
Expand All @@ -171,6 +183,15 @@ Required

The OpenAI API key. You can retrieve it from [OpenAI API Keys page](https://platform.openai.com/account/api-keys).

#### OPENAI_BASE_URL

Default: `https://api.openai.com/v1`
Optional

The base URL for the OpenAI API. You can use this to point to a different API endpoint, such as a local development server.



#### locale

Default: `en`
Expand Down
2 changes: 2 additions & 0 deletions src/commands/aicommits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default async (
const { env } = process;
const config = await getConfig({
OPENAI_KEY: env.OPENAI_KEY || env.OPENAI_API_KEY,
OPENAI_BASE_URL: env.OPENAI_BASE_URL || env.OPENAI_API_BASE_URL,
proxy:
env.https_proxy || env.HTTPS_PROXY || env.http_proxy || env.HTTP_PROXY,
generate: generate?.toString(),
Expand All @@ -66,6 +67,7 @@ export default async (
try {
messages = await generateCommitMessage(
config.OPENAI_KEY,
config.OPENAI_BASE_URL,
config.model,
config.locale,
staged.diff,
Expand Down
26 changes: 21 additions & 5 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import fs from 'fs/promises';
import path from 'path';
import os from 'os';
import ini from 'ini';
import type { TiktokenModel } from '@dqbd/tiktoken';
import { fileExists } from './fs.js';
import { KnownError } from './error.js';
import type {TiktokenModel} from '@dqbd/tiktoken';
import {fileExists} from './fs.js';
import {KnownError} from './error.js';

const commitTypes = ['', 'conventional'] as const;

export type CommitType = (typeof commitTypes)[number];

const { hasOwnProperty } = Object.prototype;
const {hasOwnProperty} = Object.prototype;
export const hasOwn = (object: unknown, key: PropertyKey) =>
hasOwnProperty.call(object, key);

Expand All @@ -21,6 +21,21 @@ const parseAssert = (name: string, condition: any, message: string) => {
};

const configParsers = {
// Add the custom OPENAI_BASE_URL parameter to accommodate the custom proxy address
OPENAI_BASE_URL(url?: string) {
if (!url) {
// https://github.com/openai/openai-node/blob/ed4219a565976750637d8c68b2b35409aca447af/src/index.ts#L103
return 'https://api.openai.com/v1';
}
// Remove the protocol from the URL
const domain = url.replace(/^https?:\/\//, '');

// Validate the domain
parseAssert('OPENAI_BASE_URL', /^[a-zA-Z0-9.-]+$/.test(domain), 'Must be a valid domain');

return domain;
},

OPENAI_KEY(key?: string) {
if (!key) {
throw new KnownError(
Expand Down Expand Up @@ -153,7 +168,8 @@ export const getConfig = async (
if (suppressErrors) {
try {
parsedConfig[key] = parser(value);
} catch {}
} catch {
}
} else {
parsedConfig[key] = parser(value);
}
Expand Down
5 changes: 4 additions & 1 deletion src/utils/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,13 @@ const httpsPost = async (

const createChatCompletion = async (
apiKey: string,
baseUrl:string,
json: CreateChatCompletionRequest,
timeout: number,
proxy?: string
) => {
const { response, data } = await httpsPost(
'api.openai.com',
baseUrl,
'/v1/chat/completions',
{
Authorization: `Bearer ${apiKey}`,
Expand Down Expand Up @@ -132,6 +133,7 @@ const deduplicateMessages = (array: string[]) => Array.from(new Set(array));

export const generateCommitMessage = async (
apiKey: string,
baseUrl: string,
model: TiktokenModel,
locale: string,
diff: string,
Expand All @@ -144,6 +146,7 @@ export const generateCommitMessage = async (
try {
const completion = await createChatCompletion(
apiKey,
baseUrl,
{
model,
messages: [
Expand Down
2 changes: 2 additions & 0 deletions tests/specs/cli/commits.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { testSuite, expect } from 'manten';
import {
assertOpenAiToken,
assertOpenAIBaseUrl,
createFixture,
createGit,
files,
Expand All @@ -16,6 +17,7 @@ export default testSuite(({ describe }) => {
}

assertOpenAiToken();
assertOpenAIBaseUrl();

describe('Commits', async ({ test, describe }) => {
test('Excludes files', async () => {
Expand Down
47 changes: 29 additions & 18 deletions tests/specs/config.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
import fs from 'fs/promises';
import path from 'path';
import { testSuite, expect } from 'manten';
import { createFixture } from '../utils.js';
import {testSuite, expect} from 'manten';
import {createFixture} from '../utils.js';

export default testSuite(({ describe }) => {
describe('config', async ({ test, describe }) => {
const { fixture, aicommits } = await createFixture();
export default testSuite(({describe}) => {
describe('config', async ({test, describe}) => {
const {fixture, aicommits} = await createFixture();
const configPath = path.join(fixture.path, '.aicommits');
const openAiToken = 'OPENAI_KEY=sk-abc';
const openAiBaseUrl = 'OPENAI_BASE_URL=https://api.openai.com/v1';

test('set unknown config file', async () => {
const { stderr } = await aicommits(['config', 'set', 'UNKNOWN=1'], {
const {stderr} = await aicommits(['config', 'set', 'UNKNOWN=1'], {
reject: false,
});

expect(stderr).toMatch('Invalid config property: UNKNOWN');
});

test('set invalid OPENAI_KEY', async () => {
const { stderr } = await aicommits(['config', 'set', 'OPENAI_KEY=abc'], {
const {stderr} = await aicommits(['config', 'set', 'OPENAI_KEY=abc'], {
reject: false,
});

Expand All @@ -29,30 +30,35 @@ export default testSuite(({ describe }) => {

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

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

await test('get config file', async () => {
const { stdout } = await aicommits(['config', 'get', 'OPENAI_KEY']);
const {stdout} = await aicommits(['config', 'get', 'OPENAI_KEY']);
expect(stdout).toBe(openAiToken);
const {stdout: urlStdout} = await aicommits(['config', 'get', 'OPENAI_BASE_URL']);
expect(urlStdout).toBe(openAiBaseUrl);

});

await test('reading unknown config', async () => {
await fs.appendFile(configPath, 'UNKNOWN=1');

const { stdout, stderr } = await aicommits(['config', 'get', 'UNKNOWN'], {
const {stdout, stderr} = await aicommits(['config', 'get', 'UNKNOWN'], {
reject: false,
});

expect(stdout).toBe('');
expect(stderr).toBe('');
});

await describe('timeout', ({ test }) => {
await describe('timeout', ({test}) => {
test('setting invalid timeout config', async () => {
const { stderr } = await aicommits(['config', 'set', 'timeout=abc'], {
const {stderr} = await aicommits(['config', 'set', 'timeout=abc'], {
reject: false,
});

Expand All @@ -71,9 +77,9 @@ export default testSuite(({ describe }) => {
});
});

await describe('max-length', ({ test }) => {
await describe('max-length', ({test}) => {
test('must be an integer', async () => {
const { stderr } = await aicommits(
const {stderr} = await aicommits(
['config', 'set', 'max-length=abc'],
{
reject: false,
Expand All @@ -84,7 +90,7 @@ export default testSuite(({ describe }) => {
});

test('must be at least 20 characters', async () => {
const { stderr } = await aicommits(['config', 'set', 'max-length=10'], {
const {stderr} = await aicommits(['config', 'set', 'max-length=10'], {
reject: false,
});

Expand All @@ -108,16 +114,21 @@ export default testSuite(({ describe }) => {

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

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

await test('get config file', async () => {
const { stdout } = await aicommits(['config', 'get', 'OPENAI_KEY']);
const {stdout} = await aicommits(['config', 'get', 'OPENAI_KEY']);
expect(stdout).toBe(openAiToken);
});

await fixture.rm();
const {stdout: urlStdout} = await aicommits(['config', 'get', 'OPENAI_BASE_URL']);
expect(urlStdout).toBe(openAiBaseUrl);

await fixture.rm();
});
});
});
})
13 changes: 10 additions & 3 deletions tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'path';
import fs from 'fs/promises';
import { execa, execaNode, type Options } from 'execa';
import {execa, execaNode, type Options} from 'execa';
import {
createFixture as createFixtureBase,
type FileTree,
Expand Down Expand Up @@ -59,9 +59,9 @@ export const createFixture = async (source?: string | FileTree) => {
};

export const files = Object.freeze({
'.aicommits': `OPENAI_KEY=${process.env.OPENAI_KEY}`,
'.aicommits': `OPENAI_KEY=${process.env.OPENAI_KEY}\nOPENAI_BASE_URL=${process.env.OPENAI_BASE_URL}`,
'data.json': Array.from(
{ length: 10 },
{length: 10},
(_, i) => `${i}. Lorem ipsum dolor sit amet`
).join('\n'),
});
Expand All @@ -74,6 +74,13 @@ export const assertOpenAiToken = () => {
}
};

export const assertOpenAIBaseUrl = () => {
if (!process.env.OPENAI_KEY) {
// eslint-disable-next-line no-console
process.env.OPENAI_KEY = "https://api.openai.com/v1"
}
}

// See ./diffs/README.md in order to generate diff files
export const getDiff = async (diffName: string): Promise<string> =>
fs.readFile(new URL(`fixtures/${diffName}`, import.meta.url), 'utf8');