open-webui/src/lib/components/admin/Settings/Pipelines.svelte

530 lines
15 KiB
Svelte
Raw Normal View History

2024-05-28 19:04:19 +00:00
<script lang="ts">
import { v4 as uuidv4 } from 'uuid';
import { toast } from 'svelte-sonner';
import { models } from '$lib/stores';
2024-05-28 20:05:31 +00:00
import { getContext, onMount, tick } from 'svelte';
2024-05-28 19:04:19 +00:00
import type { Writable } from 'svelte/store';
import type { i18n as i18nType } from 'i18next';
2024-05-28 20:05:31 +00:00
import {
getPipelineValves,
getPipelineValvesSpec,
updatePipelineValves,
2024-05-28 23:12:54 +00:00
getPipelines,
getModels,
2024-05-30 05:03:22 +00:00
getPipelinesList,
downloadPipeline,
2024-06-05 20:57:48 +00:00
deletePipeline,
uploadPipeline
2024-05-28 20:05:31 +00:00
} from '$lib/apis';
2024-05-28 19:32:49 +00:00
import Spinner from '$lib/components/common/Spinner.svelte';
2024-05-28 19:04:19 +00:00
const i18n: Writable<i18nType> = getContext('i18n');
export let saveHandler: Function;
2024-05-30 05:03:22 +00:00
let downloading = false;
2024-06-05 20:57:48 +00:00
let uploading = false;
let pipelineFiles;
2024-05-30 05:03:22 +00:00
let PIPELINES_LIST = null;
let selectedPipelinesUrlIdx = '';
2024-05-28 19:32:49 +00:00
let pipelines = null;
2024-05-28 20:05:31 +00:00
2024-05-28 19:32:49 +00:00
let valves = null;
2024-05-28 20:05:31 +00:00
let valves_spec = null;
let selectedPipelineIdx = null;
2024-05-30 05:03:22 +00:00
let pipelineDownloadUrl = '';
2024-05-28 20:05:31 +00:00
const updateHandler = async () => {
const pipeline = pipelines[selectedPipelineIdx];
2024-05-28 19:32:49 +00:00
2024-05-30 05:18:27 +00:00
if (pipeline && (pipeline?.valves ?? false)) {
2024-06-01 02:40:58 +00:00
for (const property in valves_spec.properties) {
if (valves_spec.properties[property]?.type === 'array') {
valves[property] = valves[property].split(',').map((v) => v.trim());
}
2024-05-29 01:24:39 +00:00
}
2024-05-30 05:18:27 +00:00
const res = await updatePipelineValves(
localStorage.token,
pipeline.id,
valves,
selectedPipelinesUrlIdx
).catch((error) => {
toast.error(error);
});
2024-05-28 19:04:19 +00:00
2024-05-28 20:05:31 +00:00
if (res) {
toast.success('Valves updated successfully');
2024-05-28 22:47:14 +00:00
setPipelines();
2024-05-28 23:12:54 +00:00
models.set(await getModels(localStorage.token));
2024-05-28 20:05:31 +00:00
saveHandler();
}
} else {
toast.error('No valves to update');
}
};
2024-05-28 19:32:49 +00:00
2024-05-28 23:12:54 +00:00
const getValves = async (idx) => {
valves = null;
valves_spec = null;
2024-05-30 05:18:27 +00:00
valves_spec = await getPipelineValvesSpec(
localStorage.token,
pipelines[idx].id,
selectedPipelinesUrlIdx
);
valves = await getPipelineValves(
localStorage.token,
pipelines[idx].id,
selectedPipelinesUrlIdx
);
2024-05-29 01:24:39 +00:00
2024-06-01 02:40:58 +00:00
for (const property in valves_spec.properties) {
if (valves_spec.properties[property]?.type === 'array') {
valves[property] = valves[property].join(',');
}
2024-05-29 01:24:39 +00:00
}
2024-05-28 23:12:54 +00:00
};
2024-05-28 22:47:14 +00:00
const setPipelines = async () => {
pipelines = null;
valves = null;
valves_spec = null;
2024-05-30 05:27:10 +00:00
if (PIPELINES_LIST.length > 0) {
2024-05-30 05:41:51 +00:00
console.log(selectedPipelinesUrlIdx);
2024-05-30 05:27:10 +00:00
pipelines = await getPipelines(localStorage.token, selectedPipelinesUrlIdx);
2024-05-28 20:05:31 +00:00
2024-05-30 05:27:10 +00:00
if (pipelines.length > 0) {
selectedPipelineIdx = 0;
await getValves(selectedPipelineIdx);
}
} else {
pipelines = [];
2024-05-28 20:05:31 +00:00
}
2024-05-28 22:47:14 +00:00
};
2024-05-30 05:03:22 +00:00
const addPipelineHandler = async () => {
downloading = true;
const res = await downloadPipeline(
localStorage.token,
pipelineDownloadUrl,
selectedPipelinesUrlIdx
).catch((error) => {
toast.error(error);
return null;
});
if (res) {
toast.success('Pipeline downloaded successfully');
setPipelines();
models.set(await getModels(localStorage.token));
}
downloading = false;
};
2024-06-05 20:57:48 +00:00
const uploadPipelineHandler = async () => {
uploading = true;
if (pipelineFiles && pipelineFiles.length !== 0) {
const file = pipelineFiles[0];
console.log(file);
const res = await uploadPipeline(localStorage.token, file, selectedPipelinesUrlIdx).catch(
(error) => {
console.log(error);
toast.error('Something went wrong :/');
return null;
}
);
if (res) {
toast.success('Pipeline downloaded successfully');
setPipelines();
models.set(await getModels(localStorage.token));
}
} else {
toast.error('No file selected');
}
pipelineFiles = null;
const pipelineUploadInputElement = document.getElementById('pipeline-upload-input');
if (pipelineUploadInputElement) {
pipelineUploadInputElement.value = null;
}
uploading = false;
};
2024-05-30 05:03:22 +00:00
const deletePipelineHandler = async () => {
const res = await deletePipeline(
localStorage.token,
pipelines[selectedPipelineIdx].id,
selectedPipelinesUrlIdx
).catch((error) => {
toast.error(error);
return null;
});
if (res) {
toast.success('Pipeline deleted successfully');
setPipelines();
models.set(await getModels(localStorage.token));
}
};
2024-05-28 22:47:14 +00:00
onMount(async () => {
PIPELINES_LIST = await getPipelinesList(localStorage.token);
2024-05-30 05:27:10 +00:00
console.log(PIPELINES_LIST);
if (PIPELINES_LIST.length > 0) {
2024-05-30 05:41:51 +00:00
selectedPipelinesUrlIdx = PIPELINES_LIST[0]['idx'].toString();
}
2024-05-30 05:41:51 +00:00
await setPipelines();
2024-05-28 19:04:19 +00:00
});
</script>
<form
class="flex flex-col h-full justify-between space-y-3 text-sm"
on:submit|preventDefault={async () => {
2024-05-28 20:05:31 +00:00
updateHandler();
2024-05-28 19:04:19 +00:00
}}
>
2024-06-08 03:18:48 +00:00
<div class=" pr-1.5 overflow-y-scroll h-full">
{#if PIPELINES_LIST !== null}
2024-05-28 19:04:19 +00:00
<div class="flex w-full justify-between mb-2">
<div class=" self-center text-sm font-semibold">
{$i18n.t('Manage Pipelines')}
2024-05-28 19:04:19 +00:00
</div>
</div>
{#if PIPELINES_LIST.length > 0}
<div class="space-y-1">
2024-05-28 19:32:49 +00:00
<div class="flex gap-2">
<div class="flex-1">
2024-05-28 19:32:49 +00:00
<select
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
bind:value={selectedPipelinesUrlIdx}
placeholder={$i18n.t('Select a pipeline url')}
2024-05-28 20:05:31 +00:00
on:change={async () => {
await tick();
await setPipelines();
2024-05-28 20:05:31 +00:00
}}
2024-05-28 19:32:49 +00:00
>
<option value="" selected disabled class="bg-gray-100 dark:bg-gray-700"
>{$i18n.t('Select a pipeline url')}</option
>
{#each PIPELINES_LIST as pipelines, idx}
2024-05-30 05:41:51 +00:00
<option value={pipelines.idx.toString()} class="bg-gray-100 dark:bg-gray-700"
>{pipelines.url}</option
2024-05-28 20:05:31 +00:00
>
2024-05-28 19:32:49 +00:00
{/each}
</select>
</div>
2024-05-28 19:04:19 +00:00
</div>
</div>
2024-05-28 19:32:49 +00:00
2024-06-05 20:57:48 +00:00
<div class=" my-2">
<div class=" mb-2 text-sm font-medium">
{$i18n.t('Upload Pipeline')}
</div>
<div class="flex w-full">
<div class="flex-1 mr-2">
<input
id="pipelines-upload-input"
bind:files={pipelineFiles}
type="file"
accept=".py"
hidden
/>
<button
class="w-full text-sm font-medium py-2 bg-transparent hover:bg-gray-100 border border-dashed dark:border-gray-800 dark:hover:bg-gray-850 text-center rounded-xl"
type="button"
on:click={() => {
document.getElementById('pipelines-upload-input')?.click();
}}
>
{#if pipelineFiles}
{pipelineFiles.length > 0 ? `${pipelineFiles.length}` : ''} pipeline(s) selected.
{:else}
{$i18n.t('Click here to select a py file.')}
{/if}
</button>
</div>
<button
class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition"
on:click={() => {
uploadPipelineHandler();
}}
disabled={uploading}
type="button"
>
{#if uploading}
<div class="self-center">
<svg
class=" w-4 h-4"
viewBox="0 0 24 24"
fill="currentColor"
xmlns="http://www.w3.org/2000/svg"
>
<style>
.spinner_ajPY {
transform-origin: center;
animation: spinner_AtaB 0.75s infinite linear;
}
@keyframes spinner_AtaB {
100% {
transform: rotate(360deg);
}
}
</style>
<path
d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
opacity=".25"
/>
<path
d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
class="spinner_ajPY"
/>
</svg>
</div>
{:else}
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 16 16"
fill="currentColor"
class="size-4"
>
<path
d="M7.25 10.25a.75.75 0 0 0 1.5 0V4.56l2.22 2.22a.75.75 0 1 0 1.06-1.06l-3.5-3.5a.75.75 0 0 0-1.06 0l-3.5 3.5a.75.75 0 0 0 1.06 1.06l2.22-2.22v5.69Z"
/>
<path
d="M3.5 9.75a.75.75 0 0 0-1.5 0v1.5A2.75 2.75 0 0 0 4.75 14h6.5A2.75 2.75 0 0 0 14 11.25v-1.5a.75.75 0 0 0-1.5 0v1.5c0 .69-.56 1.25-1.25 1.25h-6.5c-.69 0-1.25-.56-1.25-1.25v-1.5Z"
/>
</svg>
{/if}
</button>
</div>
</div>
2024-05-30 05:41:51 +00:00
<div class=" my-2">
<div class=" mb-2 text-sm font-medium">
{$i18n.t('Install from Github URL')}
2024-05-30 05:03:22 +00:00
</div>
2024-05-30 05:41:51 +00:00
<div class="flex w-full">
<div class="flex-1 mr-2">
<input
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
placeholder={$i18n.t('Enter Github Raw URL')}
bind:value={pipelineDownloadUrl}
/>
</div>
<button
class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition"
on:click={() => {
addPipelineHandler();
}}
disabled={downloading}
type="button"
>
{#if downloading}
<div class="self-center">
<svg
class=" w-4 h-4"
viewBox="0 0 24 24"
fill="currentColor"
xmlns="http://www.w3.org/2000/svg"
>
<style>
.spinner_ajPY {
transform-origin: center;
animation: spinner_AtaB 0.75s infinite linear;
}
@keyframes spinner_AtaB {
100% {
transform: rotate(360deg);
}
}
</style>
<path
d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
opacity=".25"
/>
<path
d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
class="spinner_ajPY"
/>
</svg>
</div>
{:else}
2024-05-30 05:03:22 +00:00
<svg
xmlns="http://www.w3.org/2000/svg"
2024-05-30 05:41:51 +00:00
viewBox="0 0 16 16"
fill="currentColor"
class="w-4 h-4"
2024-05-30 05:03:22 +00:00
>
<path
2024-05-30 05:41:51 +00:00
d="M8.75 2.75a.75.75 0 0 0-1.5 0v5.69L5.03 6.22a.75.75 0 0 0-1.06 1.06l3.5 3.5a.75.75 0 0 0 1.06 0l3.5-3.5a.75.75 0 0 0-1.06-1.06L8.75 8.44V2.75Z"
2024-05-30 05:03:22 +00:00
/>
<path
2024-05-30 05:41:51 +00:00
d="M3.5 9.75a.75.75 0 0 0-1.5 0v1.5A2.75 2.75 0 0 0 4.75 14h6.5A2.75 2.75 0 0 0 14 11.25v-1.5a.75.75 0 0 0-1.5 0v1.5c0 .69-.56 1.25-1.25 1.25h-6.5c-.69 0-1.25-.56-1.25-1.25v-1.5Z"
2024-05-30 05:03:22 +00:00
/>
</svg>
2024-05-30 05:41:51 +00:00
{/if}
</button>
</div>
2024-06-01 19:17:18 +00:00
<div class="mt-2 text-xs text-gray-500">
<span class=" font-semibold dark:text-gray-200">Warning:</span> Pipelines are a plugin
system with arbitrary code execution —
<span class=" font-medium dark:text-gray-400"
>don't fetch random pipelines from sources you don't trust.</span
>
</div>
2024-05-30 05:03:22 +00:00
</div>
2024-05-30 05:41:51 +00:00
<hr class=" dark:border-gray-800 my-3 w-full" />
2024-05-28 19:32:49 +00:00
2024-05-30 05:41:51 +00:00
{#if pipelines !== null}
{#if pipelines.length > 0}
<div class="flex w-full justify-between mb-2">
<div class=" self-center text-sm font-semibold">
{$i18n.t('Pipelines Valves')}
</div>
</div>
2024-05-30 05:41:51 +00:00
<div class="space-y-1">
{#if pipelines.length > 0}
<div class="flex gap-2">
<div class="flex-1">
<select
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
bind:value={selectedPipelineIdx}
placeholder={$i18n.t('Select a pipeline')}
on:change={async () => {
await tick();
await getValves(selectedPipelineIdx);
}}
>
{#each pipelines as pipeline, idx}
<option value={idx} class="bg-gray-100 dark:bg-gray-700"
>{pipeline.name} ({pipeline.type ?? 'pipe'})</option
>
{/each}
</select>
</div>
<button
class="px-2.5 bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-100 rounded-lg transition"
on:click={() => {
deletePipelineHandler();
}}
2024-05-30 05:41:51 +00:00
type="button"
>
2024-05-30 05:41:51 +00:00
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 16 16"
fill="currentColor"
class="w-4 h-4"
>
<path
fill-rule="evenodd"
d="M5 3.25V4H2.75a.75.75 0 0 0 0 1.5h.3l.815 8.15A1.5 1.5 0 0 0 5.357 15h5.285a1.5 1.5 0 0 0 1.493-1.35l.815-8.15h.3a.75.75 0 0 0 0-1.5H11v-.75A2.25 2.25 0 0 0 8.75 1h-1.5A2.25 2.25 0 0 0 5 3.25Zm2.25-.75a.75.75 0 0 0-.75.75V4h3v-.75a.75.75 0 0 0-.75-.75h-1.5ZM6.05 6a.75.75 0 0 1 .787.713l.275 5.5a.75.75 0 0 1-1.498.075l-.275-5.5A.75.75 0 0 1 6.05 6Zm3.9 0a.75.75 0 0 1 .712.787l-.275 5.5a.75.75 0 0 1-1.498-.075l.275-5.5a.75.75 0 0 1 .786-.711Z"
clip-rule="evenodd"
/>
</svg>
</button>
</div>
2024-05-30 05:41:51 +00:00
{/if}
2024-05-30 05:03:22 +00:00
2024-05-30 05:41:51 +00:00
<div class="space-y-1">
{#if pipelines[selectedPipelineIdx].valves}
{#if valves}
{#each Object.keys(valves_spec.properties) as property, idx}
<div class=" py-0.5 w-full justify-between">
<div class="flex w-full justify-between">
<div class=" self-center text-xs font-medium">
{valves_spec.properties[property].title}
</div>
2024-05-28 20:05:31 +00:00
2024-05-30 05:41:51 +00:00
<button
class="p-1 px-3 text-xs flex rounded transition"
type="button"
on:click={() => {
valves[property] = (valves[property] ?? null) === null ? '' : null;
}}
>
{#if (valves[property] ?? null) === null}
<span class="ml-2 self-center"> {$i18n.t('None')} </span>
{:else}
<span class="ml-2 self-center"> {$i18n.t('Custom')} </span>
{/if}
</button>
</div>
2024-05-30 05:41:51 +00:00
{#if (valves[property] ?? null) !== null}
<div class="flex mt-0.5 space-x-2">
<div class=" flex-1">
<input
class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
type="text"
placeholder={valves_spec.properties[property].title}
bind:value={valves[property]}
autocomplete="off"
/>
</div>
</div>
2024-05-30 05:41:51 +00:00
{/if}
</div>
{/each}
{:else}
<Spinner className="size-5" />
{/if}
{:else}
2024-05-30 05:41:51 +00:00
<div>No valves</div>
{/if}
2024-05-30 05:41:51 +00:00
</div>
</div>
{:else if pipelines.length === 0}
<div>Pipelines Not Detected</div>
{/if}
{:else}
<div class="flex justify-center">
<div class="my-auto">
<Spinner className="size-4" />
</div>
</div>
{/if}
{/if}
2024-05-28 19:32:49 +00:00
{:else}
<div class="flex justify-center h-full">
2024-05-28 19:32:49 +00:00
<div class="my-auto">
<Spinner className="size-6" />
</div>
2024-05-28 19:04:19 +00:00
</div>
2024-05-28 19:32:49 +00:00
{/if}
2024-05-28 19:04:19 +00:00
</div>
2024-05-28 19:04:19 +00:00
<div class="flex justify-end pt-3 text-sm font-medium">
<button
class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
type="submit"
>
Save
</button>
</div>
</form>