refac: openai additional headers support

This commit is contained in:
Timothy Jaeryang Baek 2025-10-03 00:23:26 -05:00
parent 7e70f8d2c1
commit f25b7b73b4
2 changed files with 67 additions and 1 deletions

View file

@ -190,6 +190,9 @@ async def get_headers_and_cookies(
if token: if token:
headers["Authorization"] = f"Bearer {token}" headers["Authorization"] = f"Bearer {token}"
if config.get("headers") and isinstance(config.get("headers"), dict):
headers = {**headers, **config.get("headers")}
return headers, cookies return headers, cookies

View file

@ -17,6 +17,7 @@
import Tags from './common/Tags.svelte'; import Tags from './common/Tags.svelte';
import Spinner from '$lib/components/common/Spinner.svelte'; import Spinner from '$lib/components/common/Spinner.svelte';
import XMark from '$lib/components/icons/XMark.svelte'; import XMark from '$lib/components/icons/XMark.svelte';
import Textarea from './common/Textarea.svelte';
export let onSubmit: Function = () => {}; export let onSubmit: Function = () => {};
export let onDelete: Function = () => {}; export let onDelete: Function = () => {};
@ -42,6 +43,8 @@
let enable = true; let enable = true;
let apiVersion = ''; let apiVersion = '';
let headers = '';
let tags = []; let tags = [];
let modelId = ''; let modelId = '';
@ -69,6 +72,19 @@
// remove trailing slash from url // remove trailing slash from url
url = url.replace(/\/$/, ''); url = url.replace(/\/$/, '');
if (headers) {
try {
const _headers = JSON.parse(headers);
if (typeof _headers !== 'object' || Array.isArray(_headers)) {
throw new Error('Headers must be a valid JSON object');
}
headers = JSON.stringify(_headers, null, 2);
} catch (error) {
toast.error($i18n.t('Headers must be a valid JSON object'));
return;
}
}
const res = await verifyOpenAIConnection( const res = await verifyOpenAIConnection(
localStorage.token, localStorage.token,
{ {
@ -77,7 +93,8 @@
config: { config: {
auth_type, auth_type,
azure: azure, azure: azure,
api_version: apiVersion api_version: apiVersion,
headers: JSON.parse(headers)
} }
}, },
direct direct
@ -136,6 +153,19 @@
} }
} }
if (headers) {
try {
const _headers = JSON.parse(headers);
if (typeof _headers !== 'object' || Array.isArray(_headers)) {
throw new Error('Headers must be a valid JSON object');
}
headers = JSON.stringify(_headers, null, 2);
} catch (error) {
toast.error($i18n.t('Headers must be a valid JSON object'));
return;
}
}
// remove trailing slash from url // remove trailing slash from url
url = url.replace(/\/$/, ''); url = url.replace(/\/$/, '');
@ -149,6 +179,7 @@
model_ids: modelIds, model_ids: modelIds,
connection_type: connectionType, connection_type: connectionType,
auth_type, auth_type,
headers: headers ? JSON.parse(headers) : undefined,
...(!ollama && azure ? { azure: true, api_version: apiVersion } : {}) ...(!ollama && azure ? { azure: true, api_version: apiVersion } : {})
} }
}; };
@ -172,6 +203,9 @@
key = connection.key; key = connection.key;
auth_type = connection.config.auth_type ?? 'bearer'; auth_type = connection.config.auth_type ?? 'bearer';
headers = connection.config?.headers
? JSON.stringify(connection.config.headers, null, 2)
: '';
enable = connection.config?.enable ?? true; enable = connection.config?.enable ?? true;
tags = connection.config?.tags ?? []; tags = connection.config?.tags ?? [];
@ -376,6 +410,35 @@
</div> </div>
</div> </div>
{#if !ollama && !direct}
<div class="flex gap-2 mt-2">
<div class="flex flex-col w-full">
<label
for="headers-input"
class={`mb-0.5 text-xs text-gray-500
${($settings?.highContrastMode ?? false) ? 'text-gray-800 dark:text-gray-100' : ''}`}
>{$i18n.t('Headers')}</label
>
<div class="flex-1">
<Tooltip
content={$i18n.t(
'Enter additional headers in JSON format (e.g. {{\'{{"X-Custom-Header": "value"}}\'}})'
)}
>
<Textarea
className="w-full text-sm outline-hidden"
bind:value={headers}
placeholder={$i18n.t('Enter additional headers in JSON format')}
required={false}
minSize={30}
/>
</Tooltip>
</div>
</div>
</div>
{/if}
<div class="flex gap-2 mt-2"> <div class="flex gap-2 mt-2">
<div class="flex flex-col w-full"> <div class="flex flex-col w-full">
<label <label