Skip to content
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
30 changes: 16 additions & 14 deletions src/providers/openai-compatible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ function buildOpenAiRequestBody(params: ChatParams, options: { stream?: boolean
return body;
}

function buildOpenAiHeaders(apiKey: string): Record<string, string> {
const headers: Record<string, string> = {
'Content-Type': 'application/json',
};

const trimmedApiKey = apiKey.trim();
if (trimmedApiKey) {
headers['Authorization'] = `Bearer ${trimmedApiKey}`;
}

return headers;
}

export class OpenAICompatibleProvider implements Provider {
async complete(params: ChatParams): Promise<ChatResult> {
const { model, apiKey, baseUrl } = params;
Expand All @@ -29,10 +42,7 @@ export class OpenAICompatibleProvider implements Provider {
url,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
headers: buildOpenAiHeaders(apiKey),
body: JSON.stringify(buildOpenAiRequestBody(params)),
},
'OpenAI-compatible API request',
Expand Down Expand Up @@ -68,10 +78,7 @@ export class OpenAICompatibleProvider implements Provider {
url,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
},
headers: buildOpenAiHeaders(apiKey),
body: JSON.stringify(buildOpenAiRequestBody(params, { stream: true })),
},
'OpenAI-compatible streaming request',
Expand Down Expand Up @@ -103,12 +110,7 @@ export class OpenAICompatibleProvider implements Provider {
async fetchModels(baseUrl: string, apiKey: string): Promise<string[]> {
const url = `${baseUrl.replace(/\/+$/, '')}/models`;

const headers: Record<string, string> = {
'Content-Type': 'application/json',
};
if (apiKey) {
headers['Authorization'] = `Bearer ${apiKey}`;
}
const headers = buildOpenAiHeaders(apiKey);

const response = await fetchWithTimeout(url, { headers }, 'OpenAI-compatible model request');

Expand Down
52 changes: 52 additions & 0 deletions tests/providers/provider-adapters.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,32 @@ test('OpenAICompatibleProvider complete forwards messages and trims the first ch
});
});

test('OpenAICompatibleProvider complete omits authorization for keyless providers', async (t) => {
const provider = new OpenAICompatibleProvider();
const requestLog = [];

mockFetch(t, async (url, init) => {
requestLog.push({ url, init });
return new Response(
JSON.stringify({
model: 'llama3',
choices: [{ message: { content: 'local answer' } }],
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } },
);
});

await provider.complete({
model: 'llama3',
baseUrl: 'http://localhost:11434/v1',
apiKey: '',
messages: [{ role: 'user', content: 'hello' }],
});

assert.equal(requestLog.length, 1);
assert.equal(requestLog[0].init?.headers.Authorization, undefined);
});

test('OpenAICompatibleProvider complete surfaces API errors with the response body', async (t) => {
const provider = new OpenAICompatibleProvider();

Expand Down Expand Up @@ -432,3 +458,29 @@ test('OpenAICompatibleProvider completeStream parses streaming chunks and stops
{ kind: 'text', text: ' world' },
]);
});

test('OpenAICompatibleProvider completeStream omits authorization for keyless providers', async (t) => {
const provider = new OpenAICompatibleProvider();
const requestLog = [];

mockFetch(t, async (url, init) => {
requestLog.push({ url, init });
return new Response('data: [DONE]', {
status: 200,
headers: { 'Content-Type': 'text/event-stream' },
});
});

const chunks = await collectChunks(
provider.completeStream({
model: 'llama3',
baseUrl: 'http://localhost:11434/v1',
apiKey: '',
messages: [{ role: 'user', content: 'hello' }],
}),
);

assert.deepEqual(chunks, []);
assert.equal(requestLog.length, 1);
assert.equal(requestLog[0].init?.headers.Authorization, undefined);
});