Files
open-webui/src/lib/components/admin/Settings/Tasks.svelte
T
2026-12-02 23:39:17 +04:00

477 lines
17 KiB
Svelte

<script lang="ts">
import { getBackendConfig, getModels, getTaskConfig, updateTaskConfig } from '$lib/apis';
import { getBaseModels } from '$lib/apis/models';
import { onMount, getContext, tick } from 'svelte';
import { toast } from 'svelte-sonner';
import Switch from '$lib/components/common/Switch.svelte';
import Textarea from '$lib/components/common/Textarea.svelte';
import Spinner from '$lib/components/common/Spinner.svelte';
import SettingsSelect from '$lib/components/common/SettingsSelect.svelte';
import AdvancedParams from '$lib/components/chat/Settings/Advanced/AdvancedParams.svelte';
import AdminSettingField from './AdminSettingField.svelte';
import AdminSettingRow from './AdminSettingRow.svelte';
import AdminSettingSection from './AdminSettingSection.svelte';
import AdminSettingSaveButton from './AdminSettingSaveButton.svelte';
import { getChangedKeys } from '$lib/utils';
import { config as appConfig } from '$lib/stores';
import { isMachine } from '$lib/utils/machines';
export let saveHandler: () => void;
const i18n: any = getContext('i18n');
let taskConfig: any = null;
let showTaskParameters = false;
let saving = false;
let savedConfig: Record<string, any> | null = null;
export let dirty = false;
$: dirty = getChangedKeys(savedConfig, settings).length > 0;
$: settings = {
...taskConfig,
TASK_MODEL_PARAMS: configuredParams(taskConfig?.TASK_MODEL_PARAMS)
};
$: settingLabels = {
TASK_MODEL: $i18n.t('settings.admin.interface.localTaskModel.label'),
TASK_MODEL_EXTERNAL: $i18n.t('settings.admin.interface.externalTaskModel.label'),
TASK_MODEL_PARAMS: $i18n.t('settings.admin.interface.taskModelParameters.label'),
ENABLE_TITLE_GENERATION: $i18n.t('settings.admin.interface.titleGeneration.label'),
TITLE_GENERATION_PROMPT_TEMPLATE: $i18n.t(
'settings.admin.interface.titleGenerationPrompt.label'
),
ENABLE_VOICE_MODE_PROMPT: $i18n.t('settings.admin.interface.voiceModePrompt.label'),
VOICE_MODE_PROMPT_TEMPLATE: $i18n.t('settings.admin.interface.promptTemplate.label'),
ENABLE_FOLLOW_UP_GENERATION: $i18n.t('settings.admin.interface.followUpGeneration.label'),
FOLLOW_UP_GENERATION_PROMPT_TEMPLATE: $i18n.t(
'settings.admin.interface.followUpGenerationPrompt.label'
),
ENABLE_TAGS_GENERATION: $i18n.t('settings.admin.interface.tagsGeneration.label'),
TAGS_GENERATION_PROMPT_TEMPLATE: $i18n.t('settings.admin.interface.tagsGenerationPrompt.label'),
ENABLE_RETRIEVAL_QUERY_GENERATION: $i18n.t(
'settings.admin.interface.retrievalQueryGeneration.label'
),
ENABLE_SEARCH_QUERY_GENERATION: $i18n.t(
'settings.admin.interface.webSearchQueryGeneration.label'
),
QUERY_GENERATION_PROMPT_TEMPLATE: $i18n.t(
'settings.admin.interface.queryGenerationPrompt.label'
),
ENABLE_AUTOCOMPLETE_GENERATION: $i18n.t(
'settings.admin.interface.autocompleteGeneration.label'
),
AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH: $i18n.t(
'settings.admin.interface.autocompleteGenerationInputMaxLength.label'
),
AUTOCOMPLETE_GENERATION_PROMPT_TEMPLATE: $i18n.t(
'settings.admin.interface.autocompleteGenerationPrompt.label'
),
IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE: $i18n.t(
'settings.admin.interface.imagePromptGenerationPrompt.label'
),
TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE: $i18n.t(
'settings.admin.interface.toolsFunctionCallingPrompt.label'
)
};
const configuredParams = (params: Record<string, any> = {}) =>
Object.fromEntries(
Object.entries(params).filter(
([_, value]) => value !== null && value !== '' && value !== undefined
)
);
let workspaceModels: any[] = [];
let baseModels: any[] = [];
let models: any[] | null = null;
$: modelOptions = models ?? [];
const normalizeModelSelection = (modelId: string | null | undefined) => {
if (!modelId) {
return '';
}
const model = modelOptions.find((m: any) => m.id === modelId);
if (!model) {
return '';
}
if (
model?.access_grants &&
!model.access_grants.some(
(g: any) => g.principal_type === 'user' && g.principal_id === '*' && g.permission === 'read'
)
) {
toast.error($i18n.t('This model is not publicly available. Please select another model.'));
}
return model.id;
};
const inputClass =
'w-full h-7 rounded-lg border border-base-100/50 bg-base-50/40 px-2 text-xs text-base-700 outline-hidden transition-colors placeholder:text-base-300 focus:border-blue-400 dark:border-base-0/[0.04] dark:bg-base-0/[0.03] dark:text-base-300 dark:placeholder:text-base-700 dark:focus:border-blue-500';
const textareaClass =
'w-full rounded-lg border border-base-100/50 bg-base-50/40 px-2 py-1.5 text-xs text-base-700 outline-hidden transition-colors placeholder:text-base-300 focus:border-blue-400 dark:border-base-0/[0.04] dark:bg-base-0/[0.03] dark:text-base-300 dark:placeholder:text-base-700 dark:focus:border-blue-500';
const updateHandler = async () => {
if (saving) return;
saving = true;
try {
taskConfig = await updateTaskConfig(localStorage.token, {
...taskConfig,
TASK_MODEL_PARAMS: configuredParams(taskConfig.TASK_MODEL_PARAMS)
});
await appConfig.set(await getBackendConfig());
await tick();
savedConfig = structuredClone(settings);
saveHandler();
} catch (error) {
toast.error(
error instanceof Error
? error.message
: Array.isArray(error)
? error.map((entry) => entry.msg).join('\n')
: String(error)
);
} finally {
saving = false;
}
};
onMount(async () => {
try {
[taskConfig, workspaceModels, baseModels] = await Promise.all([
getTaskConfig(localStorage.token),
getBaseModels(localStorage.token),
getModels(localStorage.token)
]);
taskConfig.TASK_MODEL_PARAMS = taskConfig.TASK_MODEL_PARAMS ?? {};
baseModels = baseModels.filter((model: any) => !isMachine(model.owned_by));
models = baseModels.map((m: any) => {
const workspaceModel = workspaceModels.find((wm: any) => wm.id === m.id);
return workspaceModel ? { ...m, ...workspaceModel } : { ...m, is_active: true };
});
} catch (error) {
toast.error(
error instanceof Error
? error.message
: Array.isArray(error)
? error.map((entry) => entry.msg).join('\n')
: String(error)
);
models = [];
}
await tick();
savedConfig = structuredClone(settings);
});
</script>
{#if models !== null && taskConfig}
<form
class="flex h-full flex-col justify-between text-sm"
on:submit|preventDefault={() => {
updateHandler();
}}
>
<div class="flex-1 min-h-0 overflow-y-auto scrollbar-hover pr-1.5">
<AdminSettingSection title={$i18n.t('Model')} first>
<div>
<div class="mb-2">
<div class="text-xs text-base-800 dark:text-base-200">
{$i18n.t('settings.admin.interface.taskModel.label')}
</div>
<div class="mt-1.5 text-[0.6875rem] text-base-500 dark:text-base-400">
{$i18n.t('settings.admin.interface.taskModel.description')}
</div>
</div>
<div class="grid w-full grid-cols-1 gap-2.5 sm:grid-cols-2">
<AdminSettingField
label={$i18n.t('settings.admin.interface.localTaskModel.label')}
forId="task-model"
>
<SettingsSelect
id="task-model"
bind:value={taskConfig.TASK_MODEL}
className="w-full"
placeholder={$i18n.t('Select a model')}
on:change={() => {
taskConfig.TASK_MODEL = normalizeModelSelection(taskConfig.TASK_MODEL);
}}
>
<option value="" selected>{$i18n.t('Current Model')}</option>
{#each modelOptions as model}
<option value={model.id} class="bg-base-100 dark:bg-base-700">
{model.name}
{model?.connection_type === 'local' ? `(${$i18n.t('Local')})` : ''}
</option>
{/each}
</SettingsSelect>
</AdminSettingField>
<AdminSettingField
label={$i18n.t('settings.admin.interface.externalTaskModel.label')}
forId="task-model-external"
>
<SettingsSelect
id="task-model-external"
bind:value={taskConfig.TASK_MODEL_EXTERNAL}
className="w-full"
placeholder={$i18n.t('Select a model')}
on:change={() => {
taskConfig.TASK_MODEL_EXTERNAL = normalizeModelSelection(
taskConfig.TASK_MODEL_EXTERNAL
);
}}
>
<option value="" selected>{$i18n.t('Current Model')}</option>
{#each modelOptions as model}
<option value={model.id} class="bg-base-100 dark:bg-base-700">
{model.name}
{model?.connection_type === 'local' ? `(${$i18n.t('Local')})` : ''}
</option>
{/each}
</SettingsSelect>
</AdminSettingField>
</div>
<div class="mt-2.5">
<button
class="flex w-full items-center justify-between gap-4 py-0.5 text-left"
type="button"
on:click={() => {
showTaskParameters = !showTaskParameters;
}}
>
<span class="text-xs text-base-800 dark:text-base-200">
{$i18n.t('settings.admin.interface.taskModelParameters.label')}
</span>
<span class="text-[0.6875rem] text-base-500 dark:text-base-400">
{showTaskParameters ? $i18n.t('Close') : $i18n.t('Configure')}
</span>
</button>
{#if showTaskParameters}
<div class="max-h-[24rem] overflow-y-auto pb-2 pr-1 scrollbar-hover">
<AdvancedParams
admin={true}
custom={true}
bind:params={taskConfig.TASK_MODEL_PARAMS}
/>
</div>
{/if}
</div>
</div>
</AdminSettingSection>
<AdminSettingSection title={$i18n.t('Chats')}>
<AdminSettingRow
label={$i18n.t('settings.admin.interface.titleGeneration.label')}
description={$i18n.t('settings.admin.interface.titleGeneration.description')}
let:labelId
>
<Switch bind:state={taskConfig.ENABLE_TITLE_GENERATION} ariaLabelledbyId={labelId} />
</AdminSettingRow>
{#if taskConfig.ENABLE_TITLE_GENERATION}
<AdminSettingField
label={$i18n.t('settings.admin.interface.titleGenerationPrompt.label')}
description={$i18n.t('settings.admin.interface.titleGenerationPrompt.description')}
>
<Textarea
className={textareaClass}
bind:value={taskConfig.TITLE_GENERATION_PROMPT_TEMPLATE}
maxSize={300}
placeholder={$i18n.t(
'Leave empty to use the default prompt, or enter a custom prompt'
)}
/>
</AdminSettingField>
{/if}
<AdminSettingRow
label={$i18n.t('settings.admin.interface.followUpGeneration.label')}
description={$i18n.t('settings.admin.interface.followUpGeneration.description')}
let:labelId
>
<Switch bind:state={taskConfig.ENABLE_FOLLOW_UP_GENERATION} ariaLabelledbyId={labelId} />
</AdminSettingRow>
{#if taskConfig.ENABLE_FOLLOW_UP_GENERATION}
<AdminSettingField
label={$i18n.t('settings.admin.interface.followUpGenerationPrompt.label')}
description={$i18n.t('settings.admin.interface.followUpGenerationPrompt.description')}
>
<Textarea
className={textareaClass}
bind:value={taskConfig.FOLLOW_UP_GENERATION_PROMPT_TEMPLATE}
maxSize={300}
placeholder={$i18n.t(
'Leave empty to use the default prompt, or enter a custom prompt'
)}
/>
</AdminSettingField>
{/if}
<AdminSettingRow
label={$i18n.t('settings.admin.interface.tagsGeneration.label')}
description={$i18n.t('settings.admin.interface.tagsGeneration.description')}
let:labelId
>
<Switch bind:state={taskConfig.ENABLE_TAGS_GENERATION} ariaLabelledbyId={labelId} />
</AdminSettingRow>
{#if taskConfig.ENABLE_TAGS_GENERATION}
<AdminSettingField
label={$i18n.t('settings.admin.interface.tagsGenerationPrompt.label')}
description={$i18n.t('settings.admin.interface.tagsGenerationPrompt.description')}
>
<Textarea
className={textareaClass}
bind:value={taskConfig.TAGS_GENERATION_PROMPT_TEMPLATE}
maxSize={300}
placeholder={$i18n.t(
'Leave empty to use the default prompt, or enter a custom prompt'
)}
/>
</AdminSettingField>
{/if}
</AdminSettingSection>
<AdminSettingSection title={$i18n.t('Search')}>
<AdminSettingRow
label={$i18n.t('settings.admin.interface.retrievalQueryGeneration.label')}
description={$i18n.t('settings.admin.interface.retrievalQueryGeneration.description')}
let:labelId
>
<Switch
bind:state={taskConfig.ENABLE_RETRIEVAL_QUERY_GENERATION}
ariaLabelledbyId={labelId}
/>
</AdminSettingRow>
{#if $appConfig?.features?.enable_web_search}
<AdminSettingRow
label={$i18n.t('settings.admin.interface.webSearchQueryGeneration.label')}
description={$i18n.t('settings.admin.interface.webSearchQueryGeneration.description')}
let:labelId
>
<Switch
bind:state={taskConfig.ENABLE_SEARCH_QUERY_GENERATION}
ariaLabelledbyId={labelId}
/>
</AdminSettingRow>
{/if}
<AdminSettingField
label={$i18n.t('settings.admin.interface.queryGenerationPrompt.label')}
description={$i18n.t('settings.admin.interface.queryGenerationPrompt.description')}
>
<Textarea
className={textareaClass}
bind:value={taskConfig.QUERY_GENERATION_PROMPT_TEMPLATE}
maxSize={300}
placeholder={$i18n.t('Leave empty to use the default prompt, or enter a custom prompt')}
/>
</AdminSettingField>
</AdminSettingSection>
<AdminSettingSection title={$i18n.t('Input')}>
<AdminSettingRow
label={$i18n.t('settings.admin.interface.autocompleteGeneration.label')}
description={$i18n.t('settings.admin.interface.autocompleteGeneration.description')}
let:labelId
>
<Switch
bind:state={taskConfig.ENABLE_AUTOCOMPLETE_GENERATION}
ariaLabelledbyId={labelId}
/>
</AdminSettingRow>
{#if taskConfig.ENABLE_AUTOCOMPLETE_GENERATION}
<AdminSettingRow
label={$i18n.t('settings.admin.interface.autocompleteGenerationInputMaxLength.label')}
description={$i18n.t(
'settings.admin.interface.autocompleteGenerationInputMaxLength.description'
)}
let:labelId
>
<input
id={`${labelId}-control`}
type="number"
min="-1"
step="1"
class="{inputClass} w-28!"
bind:value={taskConfig.AUTOCOMPLETE_GENERATION_INPUT_MAX_LENGTH}
placeholder={$i18n.t('-1 for no limit, or a positive integer for a specific limit')}
/>
</AdminSettingRow>
<AdminSettingField
label={$i18n.t('settings.admin.interface.autocompleteGenerationPrompt.label')}
description={$i18n.t(
'settings.admin.interface.autocompleteGenerationPrompt.description'
)}
>
<Textarea
className={textareaClass}
bind:value={taskConfig.AUTOCOMPLETE_GENERATION_PROMPT_TEMPLATE}
maxSize={300}
placeholder={$i18n.t(
'Leave empty to use the default prompt, or enter a custom prompt'
)}
/>
</AdminSettingField>
{/if}
</AdminSettingSection>
<AdminSettingSection title={$i18n.t('Prompts')}>
<AdminSettingField
label={$i18n.t('settings.admin.interface.imagePromptGenerationPrompt.label')}
description={$i18n.t('settings.admin.interface.imagePromptGenerationPrompt.description')}
>
<Textarea
className={textareaClass}
bind:value={taskConfig.IMAGE_PROMPT_GENERATION_PROMPT_TEMPLATE}
maxSize={300}
placeholder={$i18n.t('Leave empty to use the default prompt, or enter a custom prompt')}
/>
</AdminSettingField>
<AdminSettingRow
label={$i18n.t('settings.admin.interface.voiceModePrompt.label')}
description={$i18n.t('settings.admin.interface.voiceModePrompt.description')}
let:labelId
>
<Switch bind:state={taskConfig.ENABLE_VOICE_MODE_PROMPT} ariaLabelledbyId={labelId} />
</AdminSettingRow>
{#if taskConfig.ENABLE_VOICE_MODE_PROMPT}
<AdminSettingField
label={$i18n.t('settings.admin.interface.promptTemplate.label')}
description={$i18n.t('settings.admin.interface.promptTemplate.description')}
>
<Textarea
className={textareaClass}
bind:value={taskConfig.VOICE_MODE_PROMPT_TEMPLATE}
maxSize={300}
placeholder={$i18n.t(
'Leave empty to use the default prompt, or enter a custom prompt'
)}
/>
</AdminSettingField>
{/if}
<AdminSettingField
label={$i18n.t('settings.admin.interface.toolsFunctionCallingPrompt.label')}
description={$i18n.t('settings.admin.interface.toolsFunctionCallingPrompt.description')}
>
<Textarea
className={textareaClass}
bind:value={taskConfig.TOOLS_FUNCTION_CALLING_PROMPT_TEMPLATE}
maxSize={300}
placeholder={$i18n.t('Leave empty to use the default prompt, or enter a custom prompt')}
/>
</AdminSettingField>
</AdminSettingSection>
</div>
<div class="flex justify-end pt-3 text-sm font-normal">
<AdminSettingSaveButton {settings} {savedConfig} labels={settingLabels} disabled={saving} />
</div>
</form>
{:else}
<div class="flex h-full w-full items-center justify-center">
<Spinner className="size-5" />
</div>
{/if}