mirror of
https://github.com/open-webui/open-webui.git
synced 2026-11-17 09:51:35 +00:00
refac
This commit is contained in:
parent
34d3601343
commit
1c572c49bc
1 changed files with 143 additions and 142 deletions
|
|
@ -257,6 +257,7 @@
|
|||
const fallbackModels = normalizeSelectedModels(selectedModels);
|
||||
if (!equal(fallbackModels, selectedModels)) {
|
||||
selectedModels = fallbackModels;
|
||||
if (!loading && !modelDefaults) setDefaults();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -353,7 +354,8 @@
|
|||
let selectedToolIds: string[] = [];
|
||||
let selectedSkillIds: string[] = [];
|
||||
let selectedFilterIds: string[] = [];
|
||||
let pendingOAuthTools = [];
|
||||
let pendingOAuthTools: { id: string; name: string; serverId: string; authType: string | null }[] =
|
||||
[];
|
||||
|
||||
let imageGenerationEnabled = false;
|
||||
let webSearchEnabled = false;
|
||||
|
|
@ -889,6 +891,7 @@
|
|||
}
|
||||
|
||||
const navigateHandler = async () => {
|
||||
modelDefaults = null;
|
||||
cancelBrowserInteractions();
|
||||
bridge?.end();
|
||||
noteChatDebug('navigateHandler start');
|
||||
|
|
@ -940,9 +943,7 @@
|
|||
await processNextInQueue(chatIdProp);
|
||||
}
|
||||
|
||||
if (!(await restoreChatInput(storageChatInput))) {
|
||||
await setDefaults();
|
||||
}
|
||||
await setDefaults(await restoreChatInput(storageChatInput));
|
||||
|
||||
messageInput?.focus({ preventScroll: true });
|
||||
} else if (!embedded) {
|
||||
|
|
@ -957,6 +958,7 @@
|
|||
};
|
||||
|
||||
const initEmbeddedDraft = async () => {
|
||||
modelDefaults = null;
|
||||
terminalContext = null;
|
||||
clearTimeout(saveControlsTimer);
|
||||
await saveControls();
|
||||
|
|
@ -1088,153 +1090,151 @@
|
|||
lastTerminalSkillSelector = $selectedTerminalId;
|
||||
}
|
||||
|
||||
let defaultsModelIds: string[] = [];
|
||||
let modelDefaults: Record<string, any> | null = null;
|
||||
let settingDefaults: Promise<void> | null = null;
|
||||
const setDefaults = async () => {
|
||||
// Concurrent callers must wait, or the earlier run overwrites what they write afterwards
|
||||
if (settingDefaults) return settingDefaults;
|
||||
|
||||
$: if (!loading && modelDefaults && !equal(selectedModelIds, defaultsModelIds)) {
|
||||
setDefaults(true);
|
||||
}
|
||||
|
||||
const setDefaults = async (preserveSelections = false) => {
|
||||
const modelIds = atSelectedModel ? [atSelectedModel.id] : [...selectedModels];
|
||||
const id = $chatId;
|
||||
while (settingDefaults) await settingDefaults;
|
||||
let resolveDefaults = () => {};
|
||||
settingDefaults = new Promise<void>((resolve) => (resolveDefaults = resolve));
|
||||
|
||||
try {
|
||||
if (!$tools) {
|
||||
tools.set(await getTools(localStorage.token));
|
||||
}
|
||||
if (!$functions) {
|
||||
functions.set(await getFunctions(localStorage.token));
|
||||
}
|
||||
if (!$skills) {
|
||||
skills.set(await getSkills(localStorage.token));
|
||||
}
|
||||
if (selectedModels.length !== 1 && !atSelectedModel) {
|
||||
const comparedModels = selectedModels
|
||||
.filter((id) => id)
|
||||
.map((id) => $models.find((m) => m.id === id));
|
||||
const isSharedDefaultFeature = (feature) =>
|
||||
comparedModels.length > 0 &&
|
||||
comparedModels.every(
|
||||
(model) =>
|
||||
model?.info?.meta?.capabilities?.[feature] &&
|
||||
model?.info?.meta?.defaultFeatureIds?.includes(feature)
|
||||
);
|
||||
|
||||
if (
|
||||
isSharedDefaultFeature('image_generation') &&
|
||||
$config?.features?.enable_image_generation &&
|
||||
($user?.role === 'admin' || $user?.permissions?.features?.image_generation)
|
||||
) {
|
||||
imageGenerationEnabled = true;
|
||||
}
|
||||
|
||||
if (
|
||||
isSharedDefaultFeature('web_search') &&
|
||||
$config?.features?.enable_web_search &&
|
||||
($user?.role === 'admin' || $user?.permissions?.features?.web_search)
|
||||
) {
|
||||
webSearchEnabled = true;
|
||||
}
|
||||
|
||||
if (
|
||||
isSharedDefaultFeature('code_interpreter') &&
|
||||
$config?.features?.enable_code_interpreter &&
|
||||
($user?.role === 'admin' || $user?.permissions?.features?.code_interpreter)
|
||||
) {
|
||||
codeInterpreterEnabled = true;
|
||||
}
|
||||
|
||||
if (!$tools) tools.set(await getTools(localStorage.token));
|
||||
if (!$functions) functions.set(await getFunctions(localStorage.token));
|
||||
if (!$skills) skills.set(await getSkills(localStorage.token));
|
||||
if ($tools === null || $functions === null || $skills === null) return;
|
||||
if (
|
||||
id !== $chatId ||
|
||||
!equal(modelIds, atSelectedModel ? [atSelectedModel.id] : selectedModels)
|
||||
)
|
||||
return;
|
||||
|
||||
const currentModels: any[] = modelIds.map((id) => $models.find((model) => model.id === id));
|
||||
if (!currentModels.length || currentModels.some((model) => !model)) return;
|
||||
const supports = (feature: string) =>
|
||||
currentModels.every((model) => model.info?.meta?.capabilities?.[feature] ?? true);
|
||||
const featureAvailable = (feature: 'web_search' | 'image_generation' | 'code_interpreter') =>
|
||||
supports(feature) &&
|
||||
$config?.features?.[`enable_${feature}`] &&
|
||||
($user?.role === 'admin' || $user?.permissions?.features?.[feature]);
|
||||
const defaultFeature = (feature: 'web_search' | 'image_generation' | 'code_interpreter') =>
|
||||
featureAvailable(feature) &&
|
||||
currentModels.every((model) => model.info?.meta?.defaultFeatureIds?.includes(feature));
|
||||
const terminalId = currentModels[0]?.info?.meta?.terminalId;
|
||||
const defaults: Record<string, any> = {
|
||||
selectedToolIds: [
|
||||
...new Set(
|
||||
currentModels.flatMap(
|
||||
(model) =>
|
||||
model.info?.meta?.toolIds?.filter(
|
||||
(id: string) =>
|
||||
selectedToolIds.includes(id) || ($tools as any[]).some((tool) => tool.id === id)
|
||||
) ??
|
||||
($settings as any)?.tools ??
|
||||
[]
|
||||
)
|
||||
)
|
||||
],
|
||||
selectedSkillIds: [
|
||||
...new Set(currentModels.flatMap((model) => model.info?.meta?.skillIds ?? []))
|
||||
].filter(
|
||||
(id) =>
|
||||
selectedSkillIds.includes(id) ||
|
||||
$skills.some((skill) => skill.id === id && skill.is_active)
|
||||
),
|
||||
selectedFilterIds: [
|
||||
...new Set(currentModels.flatMap((model) => model.info?.meta?.defaultFilterIds ?? []))
|
||||
],
|
||||
webSearchEnabled: !!defaultFeature('web_search'),
|
||||
imageGenerationEnabled: !!defaultFeature('image_generation'),
|
||||
codeInterpreterEnabled: !!defaultFeature('code_interpreter'),
|
||||
terminalId:
|
||||
terminalId &&
|
||||
isTerminalAvailable(terminalId) &&
|
||||
currentModels.every((model) => model.info?.meta?.terminalId === terminalId)
|
||||
? terminalId
|
||||
: preserveSelections
|
||||
? null
|
||||
: $selectedTerminalId
|
||||
};
|
||||
|
||||
if (preserveSelections) {
|
||||
const current: Record<string, any> = {
|
||||
selectedToolIds: [...selectedToolIds, ...pendingOAuthTools.map((tool) => tool.id)],
|
||||
selectedSkillIds,
|
||||
selectedFilterIds,
|
||||
webSearchEnabled,
|
||||
imageGenerationEnabled,
|
||||
codeInterpreterEnabled,
|
||||
terminalId: $selectedTerminalId
|
||||
};
|
||||
modelDefaults ??= structuredClone(defaults);
|
||||
for (const key of Object.keys(defaults)) {
|
||||
const previous = modelDefaults[key];
|
||||
if (Array.isArray(previous)) {
|
||||
const added = current[key].filter((id: string) => !previous.includes(id));
|
||||
const removed = previous.filter((id: string) => !current[key].includes(id));
|
||||
// Keep deselected defaults in the baseline so later presets cannot re-enable them.
|
||||
modelDefaults[key] = [
|
||||
...new Set([...removed, ...defaults[key].filter((id: string) => !added.includes(id))])
|
||||
];
|
||||
defaults[key] = [
|
||||
...new Set([...defaults[key].filter((id: string) => !removed.includes(id)), ...added])
|
||||
];
|
||||
} else if (current[key] !== previous) {
|
||||
defaults[key] = current[key];
|
||||
} else {
|
||||
modelDefaults[key] = defaults[key];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
modelDefaults = structuredClone(defaults);
|
||||
}
|
||||
|
||||
const model = atSelectedModel ?? $models.find((m) => m.id === selectedModels[0]);
|
||||
if (model) {
|
||||
// Set Default Tools
|
||||
if (model?.info?.meta?.toolIds) {
|
||||
const defaultIds = [
|
||||
...new Set(
|
||||
[...(model?.info?.meta?.toolIds ?? [])].filter((id) =>
|
||||
$tools.find((t) => t.id === id)
|
||||
)
|
||||
)
|
||||
];
|
||||
|
||||
// Separate unauthenticated OAuth tools
|
||||
const unauthed = [];
|
||||
const authed = [];
|
||||
for (const id of defaultIds) {
|
||||
const tool = $tools.find((t) => t.id === id);
|
||||
if (tool && tool.authenticated === false) {
|
||||
const parts = id.split(':');
|
||||
const serverId = parts.at(-1) ?? id;
|
||||
const authType =
|
||||
parts.length > 1 ? (parts[0] === 'server' ? parts[1] : parts[0]) : null;
|
||||
unauthed.push({ id, name: tool.name ?? id, serverId, authType });
|
||||
} else {
|
||||
authed.push(id);
|
||||
}
|
||||
}
|
||||
selectedToolIds = authed;
|
||||
pendingOAuthTools = unauthed;
|
||||
await continueOAuthRedirect();
|
||||
} else if ($settings?.tools) {
|
||||
selectedToolIds = $settings.tools;
|
||||
} else {
|
||||
selectedToolIds = selectedToolIds.filter((id) => !id.startsWith('direct_server:'));
|
||||
}
|
||||
|
||||
// Set Default Skills
|
||||
if (model?.info?.meta?.skillIds) {
|
||||
selectedSkillIds = [
|
||||
...new Set(
|
||||
[...(model?.info?.meta?.skillIds ?? [])].filter((id) =>
|
||||
($skills ?? []).find((s) => s.id === id && s.is_active)
|
||||
)
|
||||
)
|
||||
];
|
||||
} else {
|
||||
selectedSkillIds = [];
|
||||
}
|
||||
|
||||
// Set Default Filters (Toggleable only)
|
||||
if (model?.info?.meta?.defaultFilterIds) {
|
||||
selectedFilterIds = model.info.meta.defaultFilterIds.filter((id) =>
|
||||
model?.filters?.find((f) => f.id === id)
|
||||
);
|
||||
}
|
||||
|
||||
// Set Default Features
|
||||
if (model?.info?.meta?.defaultFeatureIds) {
|
||||
if (
|
||||
model.info?.meta?.capabilities?.['image_generation'] &&
|
||||
$config?.features?.enable_image_generation &&
|
||||
($user?.role === 'admin' || $user?.permissions?.features?.image_generation)
|
||||
) {
|
||||
imageGenerationEnabled = model.info.meta.defaultFeatureIds.includes('image_generation');
|
||||
}
|
||||
|
||||
if (
|
||||
model.info?.meta?.capabilities?.['web_search'] &&
|
||||
$config?.features?.enable_web_search &&
|
||||
($user?.role === 'admin' || $user?.permissions?.features?.web_search)
|
||||
) {
|
||||
webSearchEnabled = model.info.meta.defaultFeatureIds.includes('web_search');
|
||||
}
|
||||
|
||||
if (
|
||||
model.info?.meta?.capabilities?.['code_interpreter'] &&
|
||||
$config?.features?.enable_code_interpreter &&
|
||||
($user?.role === 'admin' || $user?.permissions?.features?.code_interpreter)
|
||||
) {
|
||||
codeInterpreterEnabled = model.info.meta.defaultFeatureIds.includes('code_interpreter');
|
||||
}
|
||||
}
|
||||
|
||||
// Set Default Terminal — only if the referenced terminal actually exists
|
||||
if (model?.info?.meta?.terminalId) {
|
||||
const tid = model.info.meta.terminalId;
|
||||
if (isTerminalAvailable(tid)) {
|
||||
selectedTerminalId.set(tid);
|
||||
}
|
||||
}
|
||||
defaults.selectedFilterIds = defaults.selectedFilterIds.filter((id: string) =>
|
||||
currentModels.every((model) =>
|
||||
model.filters?.some((filter: { id: string }) => filter.id === id)
|
||||
)
|
||||
);
|
||||
defaults.webSearchEnabled &&= !!featureAvailable('web_search');
|
||||
defaults.imageGenerationEnabled &&= !!featureAvailable('image_generation');
|
||||
defaults.codeInterpreterEnabled &&= !!featureAvailable('code_interpreter');
|
||||
if (!currentModels.some((model) => model.info?.meta?.capabilities?.terminal ?? true)) {
|
||||
defaults.terminalId = null;
|
||||
}
|
||||
if (defaults.terminalId) defaults.codeInterpreterEnabled = false;
|
||||
defaultsModelIds = modelIds;
|
||||
({
|
||||
selectedToolIds,
|
||||
selectedSkillIds,
|
||||
selectedFilterIds,
|
||||
webSearchEnabled,
|
||||
imageGenerationEnabled,
|
||||
codeInterpreterEnabled
|
||||
} = defaults);
|
||||
selectedTerminalId.set(defaults.terminalId);
|
||||
|
||||
pendingOAuthTools = [];
|
||||
selectedToolIds = selectedToolIds.filter((id) => {
|
||||
const tool = ($tools as any[]).find((tool) => tool.id === id);
|
||||
if (tool?.authenticated !== false) return true;
|
||||
const parts = id.split(':');
|
||||
pendingOAuthTools.push({
|
||||
id,
|
||||
name: tool.name ?? id,
|
||||
serverId: parts.at(-1) ?? id,
|
||||
authType: parts.length > 1 ? (parts[0] === 'server' ? parts[1] : parts[0]) : null
|
||||
});
|
||||
return false;
|
||||
});
|
||||
await continueOAuthRedirect();
|
||||
} finally {
|
||||
settingDefaults = null;
|
||||
resolveDefaults();
|
||||
|
|
@ -2619,6 +2619,7 @@
|
|||
};
|
||||
|
||||
const initNewChat = async () => {
|
||||
modelDefaults = null;
|
||||
clearTimeout(saveControlsTimer);
|
||||
await saveControls();
|
||||
if ($chatId) terminalContext = null;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue