mirror of
https://github.com/open-webui/computer.git
synced 2026-11-22 21:50:43 -05:00
120 lines
5.5 KiB
Svelte
120 lines
5.5 KiB
Svelte
<script lang="ts">
|
|
import { toast } from 'svelte-sonner';
|
|
import Modal from '../Modal.svelte';
|
|
import { createConnection } from '$lib/apis/admin';
|
|
import { ApiError } from '$lib/apis';
|
|
import { refreshChatState } from '$lib/stores/chat';
|
|
import { t } from '$lib/i18n';
|
|
|
|
interface Props {
|
|
onclose: () => void;
|
|
oncreated: () => void;
|
|
}
|
|
|
|
let { onclose, oncreated }: Props = $props();
|
|
|
|
let formName = $state('');
|
|
let formProvider = $state<'anthropic' | 'openai'>('anthropic');
|
|
let formApiType = $state<'chat_completions' | 'responses'>('chat_completions');
|
|
let formBaseUrl = $state('');
|
|
let formApiKey = $state('');
|
|
let formPrefixId = $state('');
|
|
let formModels = $state('');
|
|
let creating = $state(false);
|
|
|
|
async function create() {
|
|
if (!formBaseUrl.trim() || !formApiKey.trim()) {
|
|
toast.error($t('connections.fieldsRequired'));
|
|
return;
|
|
}
|
|
creating = true;
|
|
try {
|
|
const models = formModels.trim()
|
|
? formModels.split(',').map(m => m.trim()).filter(Boolean)
|
|
: undefined;
|
|
const name = formName.trim() || (() => {
|
|
try { return new URL(formBaseUrl.trim()).hostname; } catch { return formProvider; }
|
|
})();
|
|
await createConnection({
|
|
name,
|
|
provider: formProvider,
|
|
api_type: formProvider === 'openai' ? formApiType : 'chat_completions',
|
|
prefix_id: formPrefixId.trim() || null,
|
|
base_url: formBaseUrl.trim(),
|
|
api_key: formApiKey.trim(),
|
|
models,
|
|
});
|
|
toast.success($t('connections.added'));
|
|
refreshChatState();
|
|
oncreated();
|
|
} catch (e) {
|
|
toast.error(e instanceof ApiError ? e.message : ($t('connections.addError') || 'Failed'));
|
|
} finally { creating = false; }
|
|
}
|
|
</script>
|
|
|
|
<Modal {onclose} class="w-full max-w-md mx-4">
|
|
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
<div class="p-4" onkeydown={(e) => { if (e.key === 'Enter' && formBaseUrl.trim() && formApiKey.trim()) create(); }}>
|
|
<h2 class="text-sm font-medium text-gray-900 dark:text-white mb-3">{$t('connections.add')}</h2>
|
|
|
|
<div class="flex gap-3">
|
|
<div class="flex-1">
|
|
<label class="text-[10px] text-gray-400 dark:text-gray-600">{$t('connections.name')}</label>
|
|
<input type="text" placeholder="Optional" bind:value={formName} autofocus autocomplete="off" spellcheck="false"
|
|
class="block w-full bg-transparent text-[13px] text-gray-700 dark:text-gray-300 placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none py-0.5" />
|
|
</div>
|
|
<div class="w-28 shrink-0">
|
|
<label class="text-[10px] text-gray-400 dark:text-gray-600">Provider</label>
|
|
<select bind:value={formProvider}
|
|
class="block w-full bg-transparent text-[13px] text-gray-700 dark:text-gray-300 outline-none py-0.5 cursor-pointer">
|
|
<option value="anthropic">Anthropic</option>
|
|
<option value="openai">OpenAI</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
|
|
{#if formProvider === 'openai'}
|
|
<label class="text-[10px] text-gray-400 dark:text-gray-600 mt-2">API Type</label>
|
|
<select bind:value={formApiType}
|
|
class="block w-full bg-transparent text-[13px] text-gray-700 dark:text-gray-300 outline-none py-0.5 cursor-pointer">
|
|
<option value="chat_completions">Chat Completions</option>
|
|
<option value="responses">Responses</option>
|
|
</select>
|
|
{/if}
|
|
|
|
<label class="text-[10px] text-gray-400 dark:text-gray-600 mt-2">{$t('connections.baseUrl')}</label>
|
|
<input type="text" placeholder="https://api.openai.com/v1" bind:value={formBaseUrl} autocomplete="off" spellcheck="false" list="base-url-suggestions"
|
|
class="block w-full bg-transparent text-[13px] text-gray-700 dark:text-gray-300 placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none py-0.5 font-mono" />
|
|
<datalist id="base-url-suggestions">
|
|
<option value="https://api.anthropic.com/v1" />
|
|
<option value="https://api.openai.com/v1" />
|
|
<option value="https://openrouter.ai/api/v1" />
|
|
<option value="http://localhost:11434/v1" />
|
|
</datalist>
|
|
|
|
<label class="text-[10px] text-gray-400 dark:text-gray-600 mt-2">{$t('connections.apiKey')}</label>
|
|
<input type="password" placeholder="sk-..." bind:value={formApiKey} autocomplete="off"
|
|
class="block w-full bg-transparent text-[13px] text-gray-700 dark:text-gray-300 placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none py-0.5 font-mono" />
|
|
|
|
<label class="text-[10px] text-gray-400 dark:text-gray-600 mt-2">{$t('connections.prefixId')}</label>
|
|
<input type="text" placeholder="e.g. openrouter" bind:value={formPrefixId} autocomplete="off" spellcheck="false"
|
|
class="block w-full bg-transparent text-[13px] text-gray-700 dark:text-gray-300 placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none py-0.5 font-mono" />
|
|
|
|
<label class="text-[10px] text-gray-400 dark:text-gray-600 mt-2">{$t('connections.models')}</label>
|
|
<input type="text" placeholder="claude-sonnet-4-20250514, claude-opus-4-20250514" bind:value={formModels} autocomplete="off" spellcheck="false"
|
|
class="block w-full bg-transparent text-[13px] text-gray-700 dark:text-gray-300 placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none py-0.5 font-mono" />
|
|
|
|
<div class="flex justify-end mt-3">
|
|
<button disabled={creating || !formBaseUrl.trim() || !formApiKey.trim()} onclick={create}
|
|
class="text-[13px] text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white transition-colors duration-100 disabled:opacity-30 disabled:pointer-events-none">
|
|
{#if creating}
|
|
<div class="w-3.5 h-3.5 border-2 border-gray-300 border-t-gray-600 dark:border-gray-700 dark:border-t-gray-400 rounded-full animate-spin"></div>
|
|
{:else}
|
|
{$t('connections.add')} →
|
|
{/if}
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</Modal>
|