mirror of
https://github.com/open-webui/computer.git
synced 2026-11-16 19:01:55 +03:00
refac
This commit is contained in:
@@ -104,3 +104,27 @@ export const verifyConnection = (id: string) =>
|
||||
fetchJSON<{ ok: boolean; message: string }>(`/api/admin/connections/${id}/verify`, {
|
||||
method: 'POST'
|
||||
});
|
||||
|
||||
// ── Model Config ────────────────────────────────────────────
|
||||
|
||||
export interface ModelConfigEntry {
|
||||
is_active?: boolean;
|
||||
params?: { request_params?: Record<string, unknown> };
|
||||
}
|
||||
|
||||
export interface ModelConfigResponse {
|
||||
config: Record<string, ModelConfigEntry>;
|
||||
models: { id: string; name: string; provider: string; connection_id: string }[];
|
||||
}
|
||||
|
||||
export const getModelConfig = async (): Promise<ModelConfigResponse> =>
|
||||
fetchJSON<ModelConfigResponse>('/api/admin/models/config');
|
||||
|
||||
export const updateModelConfig = (
|
||||
modelId: string,
|
||||
update: { is_active?: boolean; params?: Record<string, unknown> }
|
||||
) =>
|
||||
fetchJSON(`/api/admin/models/${encodeURIComponent(modelId)}/config`, {
|
||||
...jsonBody(update),
|
||||
method: 'PUT'
|
||||
});
|
||||
|
||||
@@ -0,0 +1,227 @@
|
||||
<script lang="ts">
|
||||
import { onMount } from 'svelte';
|
||||
import { toast } from 'svelte-sonner';
|
||||
import {
|
||||
getModelConfig,
|
||||
updateModelConfig,
|
||||
type ModelConfigEntry
|
||||
} from '$lib/apis/admin';
|
||||
import { t } from '$lib/i18n';
|
||||
import Icon from '../Icon.svelte';
|
||||
import Spinner from '$lib/components/common/Spinner.svelte';
|
||||
|
||||
type ParamRow = { key: string; value: string };
|
||||
type ModelEntry = {
|
||||
id: string;
|
||||
name: string;
|
||||
provider: string;
|
||||
is_active: boolean;
|
||||
rows: ParamRow[];
|
||||
dirty: boolean;
|
||||
};
|
||||
|
||||
let loading = $state(true);
|
||||
let saving = $state(false);
|
||||
let models = $state<ModelEntry[]>([]);
|
||||
let selectedId = $state<string | null>(null);
|
||||
|
||||
let globalRows = $state<ParamRow[]>([]);
|
||||
let globalDirty = $state(false);
|
||||
let globalExpanded = $state(false);
|
||||
|
||||
let hasDirty = $derived(globalDirty || models.some((m) => m.dirty));
|
||||
|
||||
function parseRows(config: ModelConfigEntry | undefined): ParamRow[] {
|
||||
const rp = config?.params?.request_params;
|
||||
if (!rp || typeof rp !== 'object') return [];
|
||||
return Object.entries(rp).map(([key, value]) => ({
|
||||
key,
|
||||
value: typeof value === 'object' ? JSON.stringify(value) : String(value)
|
||||
}));
|
||||
}
|
||||
|
||||
function rowsToRequestParams(rows: ParamRow[]): Record<string, unknown> {
|
||||
const result: Record<string, unknown> = {};
|
||||
for (const { key, value } of rows) {
|
||||
if (!key.trim()) continue;
|
||||
try {
|
||||
result[key.trim()] = JSON.parse(value);
|
||||
} catch {
|
||||
result[key.trim()] = value;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
onMount(async () => {
|
||||
try {
|
||||
const data = await getModelConfig();
|
||||
const config = data.config || {};
|
||||
globalRows = parseRows(config['*']);
|
||||
globalExpanded = globalRows.length > 0;
|
||||
models = data.models.map((m) => {
|
||||
const mc = config[m.id];
|
||||
return {
|
||||
...m,
|
||||
is_active: mc?.is_active !== false,
|
||||
rows: parseRows(mc),
|
||||
dirty: false
|
||||
};
|
||||
});
|
||||
} catch {
|
||||
toast.error($t('models.failedToLoad'));
|
||||
} finally {
|
||||
loading = false;
|
||||
}
|
||||
});
|
||||
|
||||
async function toggleModel(e: Event, model: ModelEntry) {
|
||||
e.stopPropagation();
|
||||
const newVal = !model.is_active;
|
||||
model.is_active = newVal;
|
||||
models = [...models];
|
||||
try {
|
||||
await updateModelConfig(model.id, { is_active: newVal });
|
||||
} catch {
|
||||
model.is_active = !newVal;
|
||||
models = [...models];
|
||||
toast.error($t('models.failedToToggle'));
|
||||
}
|
||||
}
|
||||
|
||||
async function saveAll() {
|
||||
saving = true;
|
||||
try {
|
||||
const promises: Promise<unknown>[] = [];
|
||||
if (globalDirty) {
|
||||
const rp = rowsToRequestParams(globalRows);
|
||||
promises.push(
|
||||
updateModelConfig('*', {
|
||||
params: Object.keys(rp).length ? { request_params: rp } : {}
|
||||
})
|
||||
);
|
||||
}
|
||||
for (const model of models) {
|
||||
if (model.dirty) {
|
||||
const rp = rowsToRequestParams(model.rows);
|
||||
promises.push(
|
||||
updateModelConfig(model.id, {
|
||||
params: Object.keys(rp).length ? { request_params: rp } : {}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
await Promise.all(promises);
|
||||
globalDirty = false;
|
||||
models.forEach((m) => (m.dirty = false));
|
||||
toast.success($t('settings.saved'));
|
||||
} catch {
|
||||
toast.error($t('models.failedToSave'));
|
||||
} finally {
|
||||
saving = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
{#snippet paramRows(rows: ParamRow[], onInput: () => void, onRemove: (i: number) => void, onAdd: () => void)}
|
||||
<div class="mt-0.5 mb-1">
|
||||
<span class="text-[10px] text-gray-400 dark:text-gray-600 uppercase tracking-wide">request params</span>
|
||||
{#each rows as row, i}
|
||||
<div class="group/row flex items-center gap-1.5 h-6">
|
||||
<input type="text" placeholder="key" bind:value={row.key} oninput={onInput} autocomplete="off" spellcheck="false"
|
||||
class="w-24 shrink-0 bg-transparent text-[11px] font-mono text-gray-500 dark:text-gray-500 placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none" />
|
||||
<input type="text" placeholder="value" bind:value={row.value} oninput={onInput} autocomplete="off" spellcheck="false"
|
||||
class="flex-1 min-w-0 bg-transparent text-[11px] font-mono text-gray-500 dark:text-gray-500 placeholder:text-gray-300 dark:placeholder:text-gray-700 outline-none" />
|
||||
<button type="button" onclick={() => onRemove(i)}
|
||||
class="shrink-0 text-gray-300 dark:text-gray-700 opacity-0 group-hover/row:opacity-100 hover:text-gray-500 dark:hover:text-gray-400 transition-colors duration-75" aria-label="Remove">
|
||||
<Icon name="xmark" size={8} />
|
||||
</button>
|
||||
</div>
|
||||
{/each}
|
||||
<button
|
||||
class="flex items-center gap-1 h-6 text-[11px] text-gray-400 dark:text-gray-600 hover:text-gray-600 dark:hover:text-gray-400 transition-colors duration-75"
|
||||
onclick={onAdd}
|
||||
>
|
||||
<Icon name="plus" size={10} />
|
||||
<span>Add</span>
|
||||
</button>
|
||||
</div>
|
||||
{/snippet}
|
||||
|
||||
<div class="flex flex-col h-full">
|
||||
<h2 class="text-sm font-medium text-gray-900 dark:text-white mb-4 shrink-0">{$t('admin.models')}</h2>
|
||||
|
||||
{#if loading}
|
||||
<div class="flex justify-center py-8"><Spinner size={16} /></div>
|
||||
{:else}
|
||||
<div class="flex-1 min-h-0 overflow-y-auto">
|
||||
<!-- Global defaults -->
|
||||
<button
|
||||
class="group flex items-center gap-2 w-full h-7 text-left"
|
||||
onclick={() => (globalExpanded = !globalExpanded)}
|
||||
>
|
||||
<span class="flex-1 text-[13px] text-gray-500 dark:text-gray-400">{$t('models.defaults')}</span>
|
||||
{#if globalRows.filter((r) => r.key.trim()).length > 0}
|
||||
<span class="text-[10px] text-gray-400 dark:text-gray-600">{globalRows.filter((r) => r.key.trim()).length}</span>
|
||||
{/if}
|
||||
<Icon name={globalExpanded ? 'chevron-down' : 'chevron-right'} size={10} class="shrink-0 text-gray-300 dark:text-gray-700" />
|
||||
</button>
|
||||
|
||||
{#if globalExpanded}
|
||||
{@render paramRows(
|
||||
globalRows,
|
||||
() => (globalDirty = true),
|
||||
(i) => { globalRows = globalRows.filter((_, idx) => idx !== i); globalDirty = true; },
|
||||
() => { globalRows = [...globalRows, { key: '', value: '' }]; globalDirty = true; }
|
||||
)}
|
||||
{/if}
|
||||
|
||||
<!-- Per-model list -->
|
||||
{#each models as model}
|
||||
<button
|
||||
class="group flex items-center gap-2 w-full h-7 text-left"
|
||||
onclick={() => (selectedId = selectedId === model.id ? null : model.id)}
|
||||
>
|
||||
<span class="flex-1 text-[13px] truncate {model.is_active ? 'text-gray-700 dark:text-gray-300' : 'text-gray-400 dark:text-gray-600'}">
|
||||
{model.name}
|
||||
</span>
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
||||
<span
|
||||
class="relative w-6 h-3.5 rounded-full shrink-0 cursor-pointer transition-colors duration-150
|
||||
{model.is_active ? 'bg-gray-900 dark:bg-white' : 'bg-gray-200 dark:bg-gray-700'}"
|
||||
role="switch" tabindex="-1" aria-checked={model.is_active}
|
||||
onclick={(e) => toggleModel(e, model)}
|
||||
onkeydown={(e) => { if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); toggleModel(e, model); } }}
|
||||
>
|
||||
<span class="absolute top-0.5 w-2.5 h-2.5 rounded-full transition-all duration-150
|
||||
{model.is_active ? 'left-3 bg-white dark:bg-gray-900' : 'left-0.5 bg-white dark:bg-gray-500'}"></span>
|
||||
</span>
|
||||
</button>
|
||||
|
||||
{#if selectedId === model.id}
|
||||
{@render paramRows(
|
||||
model.rows,
|
||||
() => (model.dirty = true),
|
||||
(i) => { model.rows = model.rows.filter((_, idx) => idx !== i); model.dirty = true; },
|
||||
() => { model.rows = [...model.rows, { key: '', value: '' }]; model.dirty = true; }
|
||||
)}
|
||||
{/if}
|
||||
{/each}
|
||||
|
||||
{#if models.length === 0}
|
||||
<p class="text-[13px] text-gray-400 dark:text-gray-600 py-4">{$t('models.noModels')}</p>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<div class="shrink-0 pt-3 flex justify-end">
|
||||
<button
|
||||
class="text-[13px] text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white transition-colors duration-100"
|
||||
onclick={saveAll}
|
||||
>
|
||||
{#if saving}{$t('settings.saving')}{:else}{$t('settings.save')}{/if}
|
||||
</button>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
import Modal from './Modal.svelte';
|
||||
import Users from './Admin/Users.svelte';
|
||||
import Connections from './Admin/Connections.svelte';
|
||||
import Models from './Admin/Models.svelte';
|
||||
import AdminSettings from './Admin/Settings.svelte';
|
||||
import { t } from '$lib/i18n';
|
||||
|
||||
@@ -12,7 +13,7 @@
|
||||
|
||||
let { onclose }: Props = $props();
|
||||
|
||||
let activeTab = $state<'users' | 'connections' | 'settings'>('users');
|
||||
let activeTab = $state<'users' | 'connections' | 'models' | 'settings'>('users');
|
||||
</script>
|
||||
|
||||
<Modal
|
||||
@@ -30,7 +31,7 @@
|
||||
<Icon name="chevron-left" size={12} />
|
||||
<span>{$t('settings.back')}</span>
|
||||
</button>
|
||||
{#each [{ id: 'users', label: $t('admin.users'), icon: 'user' }, { id: 'connections', label: $t('admin.connections'), icon: 'plug' }, { id: 'settings', label: $t('admin.settings'), icon: 'settings' }] as tab}
|
||||
{#each [{ id: 'users', label: $t('admin.users'), icon: 'user' }, { id: 'connections', label: $t('admin.connections'), icon: 'plug' }, { id: 'models', label: $t('admin.models'), icon: 'cube' }, { id: 'settings', label: $t('admin.settings'), icon: 'settings' }] as tab}
|
||||
<button
|
||||
class="flex items-center gap-1.5 h-7 px-2 md:w-full shrink-0 rounded-lg text-xs text-left transition-colors duration-75
|
||||
{activeTab === tab.id
|
||||
@@ -50,6 +51,8 @@
|
||||
<Users />
|
||||
{:else if activeTab === 'connections'}
|
||||
<Connections />
|
||||
{:else if activeTab === 'models'}
|
||||
<Models />
|
||||
{:else if activeTab === 'settings'}
|
||||
<AdminSettings />
|
||||
{/if}
|
||||
|
||||
@@ -310,5 +310,9 @@
|
||||
{:else if name === 'clock'}
|
||||
<circle cx="12" cy="12" r="10" />
|
||||
<path d="M12 6V12H16.5" />
|
||||
{:else if name === 'cube'}
|
||||
<path d="M12 2L2 7L12 12L22 7L12 2Z" />
|
||||
<path d="M2 17L12 22L22 17" />
|
||||
<path d="M2 12L12 17L22 12" />
|
||||
{/if}
|
||||
</svg>
|
||||
|
||||
@@ -397,7 +397,7 @@
|
||||
{:else}
|
||||
<div class="px-0.5 py-0.5 flex flex-col max-h-48 overflow-y-auto">
|
||||
{#each paramRows as row, i}
|
||||
<div class="group flex items-center gap-1 px-1.5 h-7">
|
||||
<div class="group/row flex items-center gap-1 px-1.5 h-6">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="key"
|
||||
@@ -405,7 +405,7 @@
|
||||
onblur={syncParams}
|
||||
autocomplete="off"
|
||||
spellcheck="false"
|
||||
class="w-[72px] shrink-0 bg-transparent text-[11px] font-mono text-gray-700 dark:text-gray-300 placeholder:text-gray-300 dark:placeholder:text-gray-600 outline-none"
|
||||
class="w-[72px] shrink-0 bg-transparent text-[11px] font-mono text-gray-600 dark:text-gray-400 placeholder:text-gray-300 dark:placeholder:text-gray-600 outline-none"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
@@ -414,17 +414,15 @@
|
||||
onblur={syncParams}
|
||||
autocomplete="off"
|
||||
spellcheck="false"
|
||||
class="flex-1 min-w-0 bg-transparent text-[11px] font-mono text-gray-700 dark:text-gray-300 placeholder:text-gray-300 dark:placeholder:text-gray-600 outline-none"
|
||||
class="flex-1 min-w-0 bg-transparent text-[11px] font-mono text-gray-600 dark:text-gray-400 placeholder:text-gray-300 dark:placeholder:text-gray-600 outline-none"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
onclick={() => removeParamRow(i)}
|
||||
class="shrink-0 p-0.5 text-gray-300 dark:text-gray-700 opacity-0 group-hover:opacity-100 hover:text-gray-500 dark:hover:text-gray-400 transition-all"
|
||||
class="shrink-0 text-gray-300 dark:text-gray-700 opacity-0 group-hover/row:opacity-100 hover:text-gray-500 dark:hover:text-gray-400 transition-colors duration-75"
|
||||
aria-label="Remove parameter"
|
||||
>
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="size-3">
|
||||
<path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"/>
|
||||
</svg>
|
||||
<Icon name="xmark" size={8} />
|
||||
</button>
|
||||
</div>
|
||||
{/each}
|
||||
@@ -436,10 +434,7 @@
|
||||
onclick={addParamRow}
|
||||
disabled={!canAddParam}
|
||||
>
|
||||
<svg class="size-3 shrink-0" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<line x1="12" y1="5" x2="12" y2="19" />
|
||||
<line x1="5" y1="12" x2="19" y2="12" />
|
||||
</svg>
|
||||
<Icon name="plus" size={12} />
|
||||
<span class="flex-1 text-left">Add parameter</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -274,5 +274,19 @@
|
||||
"admin.webCcBaseUrl": "Base URL",
|
||||
"admin.webCcKey": "API Key",
|
||||
"admin.webCcModel": "Model",
|
||||
"admin.webCcHint": "Use any OpenAI-compatible endpoint (e.g. Perplexity Sonar, LiteLLM proxy)"
|
||||
"admin.webCcHint": "Use any OpenAI-compatible endpoint (e.g. Perplexity Sonar, LiteLLM proxy)",
|
||||
|
||||
"admin.models": "Models",
|
||||
"models.defaults": "Defaults",
|
||||
"models.noDefaults": "No global defaults",
|
||||
"models.noParams": "No parameters",
|
||||
"models.noModels": "No models available",
|
||||
"models.addConnection": "Add a connection first.",
|
||||
"models.failedToLoad": "Failed to load model config",
|
||||
"models.failedToToggle": "Failed to toggle model",
|
||||
"models.failedToSave": "Failed to save",
|
||||
"models.savedDefaults": "Saved global defaults",
|
||||
"models.savedParams": "Saved params for {{name}}",
|
||||
"models.add": "+ Add",
|
||||
"models.save": "Save"
|
||||
}
|
||||
|
||||
@@ -353,3 +353,72 @@ class UpdateConnectionRequest(BaseModel):
|
||||
api_key: Optional[str] = None
|
||||
enabled: Optional[bool] = None
|
||||
models: Optional[list[str]] = None
|
||||
|
||||
|
||||
# ── Model config ─────────────────────────────────────────────
|
||||
|
||||
CONFIG_KEY_CHAT_MODELS = "chat.models"
|
||||
|
||||
|
||||
@router.get("/models/config")
|
||||
async def get_model_config(request: Request):
|
||||
"""Get per-model config and full model list (including inactive) for the admin Models tab."""
|
||||
require_admin(request)
|
||||
config = await Config.get(CONFIG_KEY_CHAT_MODELS) or {}
|
||||
|
||||
# Build full model list from all enabled connections (same as chat.py
|
||||
# get_models but without filtering inactive models).
|
||||
from cptr.routers.chat import _get_connections, _get_connection_models
|
||||
|
||||
connections = [c for c in await _get_connections() if c.get("enabled", True)]
|
||||
models = []
|
||||
for conn in connections:
|
||||
model_ids = await _get_connection_models(conn, request.app.state)
|
||||
prefix = (conn.get("prefix_id") or "").strip()
|
||||
for model_id in model_ids or []:
|
||||
prefixed_id = f"{prefix}/{model_id}" if prefix else model_id
|
||||
models.append(
|
||||
{
|
||||
"id": prefixed_id,
|
||||
"name": model_id,
|
||||
"provider": conn.get("provider", ""),
|
||||
"connection_id": conn["id"],
|
||||
}
|
||||
)
|
||||
|
||||
return {"config": config, "models": models}
|
||||
|
||||
|
||||
class UpdateModelConfigRequest(BaseModel):
|
||||
is_active: Optional[bool] = None
|
||||
params: Optional[dict] = None
|
||||
|
||||
|
||||
@router.put("/models/{model_id:path}/config")
|
||||
async def update_model_config(
|
||||
model_id: str, body: UpdateModelConfigRequest, request: Request
|
||||
):
|
||||
"""Update config for a specific model (or '*' for global defaults)."""
|
||||
require_admin(request)
|
||||
all_config = await Config.get(CONFIG_KEY_CHAT_MODELS) or {}
|
||||
|
||||
entry = all_config.get(model_id, {})
|
||||
|
||||
if body.is_active is not None:
|
||||
entry["is_active"] = body.is_active
|
||||
|
||||
if body.params is not None:
|
||||
entry["params"] = body.params
|
||||
|
||||
# Clean up empty entries
|
||||
if not entry or (entry.keys() <= {"is_active"} and entry.get("is_active") is not False):
|
||||
if not entry.get("params"):
|
||||
all_config.pop(model_id, None)
|
||||
else:
|
||||
all_config[model_id] = entry
|
||||
else:
|
||||
all_config[model_id] = entry
|
||||
|
||||
await Config.upsert({CONFIG_KEY_CHAT_MODELS: all_config})
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@@ -196,6 +196,13 @@ async def get_models(request: Request):
|
||||
)
|
||||
|
||||
default_model = await Config.get("chat.default_model")
|
||||
|
||||
# Filter out inactive models
|
||||
chat_models_config = await Config.get("chat.models") or {}
|
||||
inactive = {k for k, v in chat_models_config.items() if v.get("is_active") is False}
|
||||
if inactive:
|
||||
models = [m for m in models if m["id"] not in inactive]
|
||||
|
||||
return {"models": models, "default": default_model}
|
||||
|
||||
|
||||
|
||||
+11
-1
@@ -730,7 +730,17 @@ async def run_chat_task(
|
||||
new_messages_since: int = 0 # messages appended since last API call
|
||||
|
||||
# Request params: arbitrary key-value pairs merged into the API request body
|
||||
request_params = chat_params.get("request_params") or None
|
||||
# Merge order: global ("*") → per-model → chat overrides (chat wins)
|
||||
chat_request_params = chat_params.get("request_params") or {}
|
||||
global_rp = {}
|
||||
model_rp = {}
|
||||
try:
|
||||
chat_models_config = await Config.get("chat.models") or {}
|
||||
global_rp = chat_models_config.get("*", {}).get("params", {}).get("request_params", {})
|
||||
model_rp = chat_models_config.get(model, {}).get("params", {}).get("request_params", {})
|
||||
except Exception:
|
||||
pass
|
||||
request_params = {**global_rp, **model_rp, **chat_request_params} or None
|
||||
|
||||
for _iteration in range(CHAT_MAX_ITERATIONS):
|
||||
# ── Context compaction: summarize older messages if too large ──
|
||||
|
||||
Reference in New Issue
Block a user