open-webui/src/lib/components/workspace/common/ValvesModal.svelte

173 lines
4.2 KiB
Svelte
Raw Normal View History

2024-06-24 00:18:36 +00:00
<script lang="ts">
import { toast } from 'svelte-sonner';
import { createEventDispatcher } from 'svelte';
import { onMount, getContext } from 'svelte';
import { addUser } from '$lib/apis/auths';
2024-06-24 04:00:34 +00:00
import Modal from '../../common/Modal.svelte';
2024-06-24 02:02:27 +00:00
import {
getFunctionValvesById,
getFunctionValvesSpecById,
updateFunctionValvesById
} from '$lib/apis/functions';
import { getToolValvesById, getToolValvesSpecById, updateToolValvesById } from '$lib/apis/tools';
2024-06-24 04:00:34 +00:00
import Spinner from '../../common/Spinner.svelte';
2024-07-02 05:14:09 +00:00
import Switch from '$lib/components/common/Switch.svelte';
2024-07-12 00:18:18 +00:00
import Valves from '$lib/components/common/Valves.svelte';
2025-06-25 22:44:45 +00:00
import XMark from '$lib/components/icons/XMark.svelte';
2024-06-24 00:18:36 +00:00
const i18n = getContext('i18n');
const dispatch = createEventDispatcher();
export let show = false;
2024-06-24 01:34:42 +00:00
export let type = 'tool';
export let id = null;
2024-06-24 02:02:27 +00:00
let saving = false;
2024-06-24 00:18:36 +00:00
let loading = false;
2024-06-24 02:02:27 +00:00
let valvesSpec = null;
let valves = {};
2024-06-24 00:18:36 +00:00
const submitHandler = async () => {
2024-06-24 02:02:27 +00:00
saving = true;
2024-06-24 02:49:48 +00:00
if (valvesSpec) {
// Convert string to array
for (const property in valvesSpec.properties) {
if (valvesSpec.properties[property]?.type === 'array') {
valves[property] = (valves[property] ?? '').split(',').map((v) => v.trim());
}
}
let res = null;
if (type === 'tool') {
res = await updateToolValvesById(localStorage.token, id, valves).catch((error) => {
2025-01-21 06:41:32 +00:00
toast.error(`${error}`);
2024-06-24 02:49:48 +00:00
});
} else if (type === 'function') {
res = await updateFunctionValvesById(localStorage.token, id, valves).catch((error) => {
2025-01-21 06:41:32 +00:00
toast.error(`${error}`);
2024-06-24 02:49:48 +00:00
});
}
if (res) {
toast.success('Valves updated successfully');
2024-06-24 17:47:40 +00:00
dispatch('save');
2024-06-24 02:49:48 +00:00
}
2024-06-24 02:02:27 +00:00
}
saving = false;
2024-06-24 00:18:36 +00:00
};
2024-06-24 02:02:27 +00:00
const initHandler = async () => {
loading = true;
valves = {};
valvesSpec = null;
if (type === 'tool') {
valves = await getToolValvesById(localStorage.token, id);
valvesSpec = await getToolValvesSpecById(localStorage.token, id);
} else if (type === 'function') {
valves = await getFunctionValvesById(localStorage.token, id);
valvesSpec = await getFunctionValvesSpecById(localStorage.token, id);
}
if (!valves) {
valves = {};
}
2024-06-24 02:49:48 +00:00
if (valvesSpec) {
// Convert array to string
for (const property in valvesSpec.properties) {
if (valvesSpec.properties[property]?.type === 'array') {
valves[property] = (valves[property] ?? []).join(',');
}
}
}
2024-06-24 02:02:27 +00:00
loading = false;
};
$: if (show) {
initHandler();
}
2024-06-24 00:18:36 +00:00
</script>
<Modal size="sm" bind:show>
<div>
<div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-2">
2024-06-24 00:41:59 +00:00
<div class=" text-lg font-medium self-center">{$i18n.t('Valves')}</div>
2024-06-24 00:18:36 +00:00
<button
class="self-center"
on:click={() => {
show = false;
}}
>
2025-06-25 22:44:45 +00:00
<XMark />
2024-06-24 00:18:36 +00:00
</button>
</div>
<div class="flex flex-col md:flex-row w-full px-5 pb-4 md:space-x-4 dark:text-gray-200">
<div class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6">
<form
class="flex flex-col w-full"
on:submit|preventDefault={() => {
submitHandler();
}}
>
<div class="px-1">
2024-06-24 02:02:27 +00:00
{#if !loading}
2024-07-12 00:18:18 +00:00
<Valves {valvesSpec} bind:valves />
2024-06-24 02:02:27 +00:00
{:else}
<Spinner className="size-5" />
{/if}
2024-06-24 00:18:36 +00:00
</div>
<div class="flex justify-end pt-3 text-sm font-medium">
<button
2025-01-30 05:06:01 +00:00
class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full {saving
2024-06-24 00:18:36 +00:00
? ' cursor-not-allowed'
: ''}"
type="submit"
2024-06-24 02:02:27 +00:00
disabled={saving}
2024-06-24 00:18:36 +00:00
>
2024-06-24 02:02:27 +00:00
{$i18n.t('Save')}
2024-06-24 00:18:36 +00:00
2024-06-24 02:02:27 +00:00
{#if saving}
2024-06-24 00:18:36 +00:00
<div class="ml-2 self-center">
2025-06-25 22:44:45 +00:00
<Spinner />
2024-06-24 00:18:36 +00:00
</div>
{/if}
</button>
</div>
</form>
</div>
</div>
</div>
</Modal>
<style>
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
/* display: none; <- Crashes Chrome on hover */
-webkit-appearance: none;
margin: 0; /* <-- Apparently some margin are still there even though it's hidden */
}
.tabs::-webkit-scrollbar {
display: none; /* for Chrome, Safari and Opera */
}
.tabs {
-ms-overflow-style: none; /* IE and Edge */
scrollbar-width: none; /* Firefox */
}
input[type='number'] {
-moz-appearance: textfield; /* Firefox */
}
</style>