Skip to content

Commit 1f2443b

Browse files
feat: add provider headers support for AI commit generation
1 parent aa830d7 commit 1f2443b

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

src/commands/aicommits.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export default async (
126126
try {
127127
const baseUrl = providerInstance.getBaseUrl();
128128
const apiKey = providerInstance.getApiKey() || '';
129+
const providerHeaders = providerInstance.getHeaders();
129130

130131
if (isChunking) {
131132
// Split files into chunks
@@ -162,7 +163,8 @@ export default async (
162163
config['max-length'],
163164
config.type,
164165
timeout,
165-
customPrompt
166+
customPrompt,
167+
providerHeaders
166168
);
167169
chunkMessages.push(...result.messages);
168170
if (result.usage) {
@@ -192,7 +194,8 @@ export default async (
192194
config['max-length'],
193195
config.type,
194196
timeout,
195-
customPrompt
197+
customPrompt,
198+
providerHeaders
196199
);
197200
messages = combineResult.messages;
198201
if (combineResult.usage) {
@@ -229,7 +232,8 @@ export default async (
229232
config['max-length'],
230233
config.type,
231234
timeout,
232-
customPrompt
235+
customPrompt,
236+
providerHeaders
233237
);
234238
messages = result.messages;
235239
usage = result.usage;

src/commands/prepare-commit-msg-hook.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export default () =>
5151

5252
const baseUrl = providerInstance.getBaseUrl();
5353
const apiKey = providerInstance.getApiKey() || '';
54+
const providerHeaders = providerInstance.getHeaders();
5455

5556
// Use config timeout, or default per provider
5657
const timeout =
@@ -72,7 +73,9 @@ export default () =>
7273
config.generate,
7374
config['max-length'],
7475
config.type,
75-
timeout
76+
timeout,
77+
undefined,
78+
providerHeaders
7679
);
7780
messages = result.messages;
7881
} finally {

src/feature/providers/base.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export type ProviderDef = {
99
modelsFilter?: (models: any[]) => string[];
1010
defaultModels: string[];
1111
requiresApiKey: boolean;
12+
headers?: Record<string, string>;
1213
};
1314

1415
export class Provider {
@@ -111,6 +112,10 @@ export class Provider {
111112
return this.def.defaultModels;
112113
}
113114

115+
getHeaders(): Record<string, string> | undefined {
116+
return this.def.headers;
117+
}
118+
114119
validateConfig(): { valid: boolean; errors: string[] } {
115120
const errors: string[] = [];
116121
if (this.def.requiresApiKey && !this.getApiKey()) {

src/feature/providers/openrouter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ export const OpenRouterProvider: ProviderDef = {
1111
.map((m: any) => m.id),
1212
defaultModels: ['openai/gpt-oss-20b:free', 'z-ai/glm-4.5-air:free'],
1313
requiresApiKey: true,
14+
headers: {
15+
'HTTP-Referer': 'https://github.com/nutlope/aicommits',
16+
'X-Title': 'aicommits',
17+
},
1418
};

src/utils/openai.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export const generateCommitMessage = async (
7979
maxLength: number,
8080
type: CommitType,
8181
timeout: number,
82-
customPrompt?: string
82+
customPrompt?: string,
83+
headers?: Record<string, string>
8384
) => {
8485
if (process.env.DEBUG) {
8586
console.log('Diff being sent to AI:');
@@ -94,6 +95,7 @@ export const generateCommitMessage = async (
9495
name: 'custom',
9596
apiKey,
9697
baseURL: baseUrl,
98+
headers,
9799
});
98100

99101
const abortController = new AbortController();
@@ -208,7 +210,8 @@ export const combineCommitMessages = async (
208210
maxLength: number,
209211
type: CommitType,
210212
timeout: number,
211-
customPrompt?: string
213+
customPrompt?: string,
214+
headers?: Record<string, string>
212215
) => {
213216
try {
214217
const provider =
@@ -218,6 +221,7 @@ export const combineCommitMessages = async (
218221
name: 'custom',
219222
apiKey,
220223
baseURL: baseUrl,
224+
headers,
221225
});
222226

223227
const abortController = new AbortController();

0 commit comments

Comments
 (0)