feat: 添加对自定义 baseURL 的支持

- 在 config.ts 中添加 baseURL 解析器
- 在 openai.ts 中更新 createChatCompletion 和 generateCommitMessage 函数
- 在 aicommits.ts 中传递 baseURL 参数
- 支持使用自定义 API 端点替代默认的 OpenAI API
This commit is contained in:
史悦
2025-08-27 15:20:19 +08:00
parent 604def8284
commit e7f26efc71
4 changed files with 2309 additions and 5 deletions

2284
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -73,7 +73,8 @@ export default async (
config['max-length'], config['max-length'],
config.type, config.type,
config.timeout, config.timeout,
config.proxy config.proxy,
config.baseURL
); );
} finally { } finally {
s.stop('Changes analyzed'); s.stop('Changes analyzed');

View File

@@ -115,6 +115,21 @@ const configParsers = {
return parsed; return parsed;
}, },
baseURL(url?: string) {
if (!url || url.length === 0) {
return 'api.openai.com';
}
parseAssert('baseURL', /^https?:\/\/.+/.test(url), 'Must be a valid URL');
// 如果用户输入完整URL提取域名部分
try {
const urlObj = new URL(url);
return urlObj.hostname;
} catch {
return url;
}
},
} as const; } as const;
type ConfigKeys = keyof typeof configParsers; type ConfigKeys = keyof typeof configParsers;

View File

@@ -71,10 +71,12 @@ const createChatCompletion = async (
apiKey: string, apiKey: string,
json: CreateChatCompletionRequest, json: CreateChatCompletionRequest,
timeout: number, timeout: number,
proxy?: string proxy?: string,
baseURL?: string
) => { ) => {
const hostname = baseURL || 'api.openai.com';
const { response, data } = await httpsPost( const { response, data } = await httpsPost(
'api.openai.com', hostname,
'/v1/chat/completions', '/v1/chat/completions',
{ {
Authorization: `Bearer ${apiKey}`, Authorization: `Bearer ${apiKey}`,
@@ -139,7 +141,8 @@ export const generateCommitMessage = async (
maxLength: number, maxLength: number,
type: CommitType, type: CommitType,
timeout: number, timeout: number,
proxy?: string proxy?: string,
baseURL?: string
) => { ) => {
try { try {
const completion = await createChatCompletion( const completion = await createChatCompletion(
@@ -165,7 +168,8 @@ export const generateCommitMessage = async (
n: completions, n: completions,
}, },
timeout, timeout,
proxy proxy,
baseURL
); );
return deduplicateMessages( return deduplicateMessages(