diff --git a/src/lib/components/chat/Chat.svelte b/src/lib/components/chat/Chat.svelte index 38048e4cf5..7a22133795 100644 --- a/src/lib/components/chat/Chat.svelte +++ b/src/lib/components/chat/Chat.svelte @@ -374,14 +374,14 @@ let loadedChatIdProp = ''; let currentDraftKey = ''; - const mergeChatVariableSchemas = (modelIds = []) => { + const mergeChatVariableSchemas = (modelIds = [], availableModels = []) => { const byKey: Record = {}; const conflicts: any[] = []; for (const modelId of modelIds.filter(Boolean)) { const fields = - $models.find((model) => model.id === modelId)?.info?.meta?.chat_variables_schema?.fields ?? - []; + availableModels.find((model) => model.id === modelId)?.info?.meta?.chat_variables_schema + ?.fields ?? []; for (const rawField of fields) { const field = { ...rawField, @@ -425,20 +425,20 @@ const hasValue = (value) => value !== undefined && value !== null && value !== ''; - const getChatVariablesForm = () => { - const { fields, conflicts } = mergeChatVariableSchemas(selectedModelIds); + const getChatVariablesForm = (modelIds = [], values = {}, availableModels = []) => { + const { fields, conflicts } = mergeChatVariableSchemas(modelIds, availableModels); const empty = fields.length > 0 && - fields.every((field) => !hasValue(chatVariables?.[field.key]) && !hasValue(field.default)); + fields.every((field) => !hasValue(values?.[field.key]) && !hasValue(field.default)); const missing = fields.some( - (field) => field.required && !hasValue(chatVariables?.[field.key]) && !hasValue(field.default) + (field) => field.required && !hasValue(values?.[field.key]) && !hasValue(field.default) ); const variables = fields.reduce( (acc, field) => { const { key, ...inputField } = field; acc[key] = { ...inputField, - default: hasValue(chatVariables?.[key]) ? chatVariables[key] : inputField.default + default: hasValue(values?.[key]) ? values[key] : inputField.default }; return acc; }, @@ -463,8 +463,6 @@ } }; - $: chatVariablesForm = getChatVariablesForm(); - let oldSelectedModelIds = ['']; $: if (!equal(selectedModelIds, oldSelectedModelIds)) { onSelectedModelIdsChange(); @@ -2650,12 +2648,13 @@ toast.error($i18n.t('Model not selected')); return; } - if (chatVariablesForm.conflicts.length > 0) { + const form = getChatVariablesForm(selectedModelIds, chatVariables, $models); + if (form.conflicts.length > 0) { showChatVariablesModal = true; toast.error($i18n.t('Chat Variables have conflicting model definitions')); return; } - if (chatVariablesForm.missing || chatVariablesForm.empty) { + if (form.missing || form.empty) { showChatVariablesModal = true; return; } @@ -3626,7 +3625,7 @@ -{#if chatVariablesForm.conflicts.length > 0} +{#if getChatVariablesForm(selectedModelIds, chatVariables, $models).conflicts.length > 0}
@@ -3646,7 +3645,7 @@ {$i18n.t('Selected models define incompatible Chat Variables.')}
- {#each chatVariablesForm.conflicts as conflict} + {#each getChatVariablesForm(selectedModelIds, chatVariables, $models).conflicts as conflict}
{conflict.key}
@@ -3662,7 +3661,7 @@ {/if}