From e231333bcd16497575f4adaa044b040eb3742c98 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" <timothyjrbeck@gmail.com> Date: Tue, 28 May 2024 09:50:17 -0700 Subject: [PATCH 1/6] refac --- backend/main.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/backend/main.py b/backend/main.py index 3bb4f0f6b..b5a1569b8 100644 --- a/backend/main.py +++ b/backend/main.py @@ -12,6 +12,7 @@ import mimetypes from fastapi import FastAPI, Request, Depends, status from fastapi.staticfiles import StaticFiles +from fastapi.responses import JSONResponse from fastapi import HTTPException from fastapi.middleware.wsgi import WSGIMiddleware from fastapi.middleware.cors import CORSMiddleware @@ -123,15 +124,6 @@ app.state.MODELS = {} origins = ["*"] -app.add_middleware( - CORSMiddleware, - allow_origins=origins, - allow_credentials=True, - allow_methods=["*"], - allow_headers=["*"], -) - - # Custom middleware to add security headers # class SecurityHeadersMiddleware(BaseHTTPMiddleware): # async def dispatch(self, request: Request, call_next): @@ -276,10 +268,8 @@ class PipelineMiddleware(BaseHTTPMiddleware): except: pass - print(sorted_filters) - for filter in sorted_filters: - + r = None try: urlIdx = filter["urlIdx"] @@ -303,7 +293,20 @@ class PipelineMiddleware(BaseHTTPMiddleware): except Exception as e: # Handle connection error here print(f"Connection error: {e}") - pass + + if r is not None: + try: + res = r.json() + if "detail" in res: + return JSONResponse( + status_code=r.status_code, + content=res, + ) + except: + pass + + else: + pass modified_body_bytes = json.dumps(data).encode("utf-8") # Replace the request body with the modified one @@ -328,6 +331,15 @@ class PipelineMiddleware(BaseHTTPMiddleware): app.add_middleware(PipelineMiddleware) +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + + @app.middleware("http") async def check_url(request: Request, call_next): if len(app.state.MODELS) == 0: From 0383efa20750eb80785e299f84eeedc5e81d3f8a Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" <timothyjrbeck@gmail.com> Date: Tue, 28 May 2024 11:43:48 -0700 Subject: [PATCH 2/6] refac: pipelines --- backend/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backend/main.py b/backend/main.py index b5a1569b8..0e5924574 100644 --- a/backend/main.py +++ b/backend/main.py @@ -245,6 +245,7 @@ class PipelineMiddleware(BaseHTTPMiddleware): model for model in app.state.MODELS.values() if "pipeline" in model + and "type" in model["pipeline"] and model["pipeline"]["type"] == "filter" and ( model["pipeline"]["pipelines"] == ["*"] @@ -279,11 +280,10 @@ class PipelineMiddleware(BaseHTTPMiddleware): if key != "": headers = {"Authorization": f"Bearer {key}"} r = requests.post( - f"{url}/filter", + f"{url}/{filter['id']}/filter", headers=headers, json={ "user": user, - "model": filter["id"], "body": data, }, ) @@ -448,7 +448,7 @@ async def get_models(user=Depends(get_verified_user)): models = [ model for model in models - if "pipeline" not in model or model["pipeline"]["type"] != "filter" + if "pipeline" not in model or model["pipeline"].get("type", None) != "filter" ] if app.state.config.ENABLE_MODEL_FILTER: From 0bef1b44c03f44c6540feb9203e81e076bb578e2 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" <timothyjrbeck@gmail.com> Date: Tue, 28 May 2024 12:04:19 -0700 Subject: [PATCH 3/6] feat: pipeline valves --- backend/main.py | 7 +++ src/lib/apis/index.ts | 29 ++++++++++ .../admin/Settings/Pipelines.svelte | 55 +++++++++++++++++++ src/lib/components/admin/SettingsModal.svelte | 38 +++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 src/lib/components/admin/Settings/Pipelines.svelte diff --git a/backend/main.py b/backend/main.py index 0e5924574..648bedea0 100644 --- a/backend/main.py +++ b/backend/main.py @@ -464,6 +464,13 @@ async def get_models(user=Depends(get_verified_user)): return {"data": models} +@app.get("/api/pipelines") +async def get_pipelines(user=Depends(get_admin_user)): + models = await get_all_models() + pipelines = [model for model in models if "pipeline" in model] + return {"data": pipelines} + + @app.get("/api/config") async def get_app_config(): # Checking and Handling the Absence of 'ui' in CONFIG_DATA diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index dc51abd52..3d6e51684 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -49,6 +49,35 @@ export const getModels = async (token: string = '') => { return models; }; +export const getPipelines = async (token: string = '') => { + let error = null; + + const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err; + return null; + }); + + if (error) { + throw error; + } + + let pipelines = res?.data ?? []; + return pipelines; +}; + export const getBackendConfig = async () => { let error = null; diff --git a/src/lib/components/admin/Settings/Pipelines.svelte b/src/lib/components/admin/Settings/Pipelines.svelte new file mode 100644 index 000000000..b1b34ecca --- /dev/null +++ b/src/lib/components/admin/Settings/Pipelines.svelte @@ -0,0 +1,55 @@ +<script lang="ts"> + import { v4 as uuidv4 } from 'uuid'; + + import { getContext, onMount } from 'svelte'; + import { models } from '$lib/stores'; + + import type { Writable } from 'svelte/store'; + import type { i18n as i18nType } from 'i18next'; + import Tooltip from '$lib/components/common/Tooltip.svelte'; + import Switch from '$lib/components/common/Switch.svelte'; + import { stringify } from 'postcss'; + import { getPipelines } from '$lib/apis'; + const i18n: Writable<i18nType> = getContext('i18n'); + + export let saveHandler: Function; + + let pipelines = []; + let selectedPipelineIdx = 0; + + onMount(async () => { + pipelines = await getPipelines(localStorage.token); + }); +</script> + +<form + class="flex flex-col h-full justify-between space-y-3 text-sm" + on:submit|preventDefault={async () => { + saveHandler(); + }} +> + <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80 h-full"> + <div class=" space-y-3 pr-1.5"> + <div class="flex w-full justify-between mb-2"> + <div class=" self-center text-sm font-semibold"> + {$i18n.t('Pipeline Valves')} + </div> + </div> + <div class="flex flex-col space-y-1"> + {#each pipelines as pipeline} + <div class=" flex justify-between"> + {JSON.stringify(pipeline)} + </div> + {/each} + </div> + </div> + </div> + <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> diff --git a/src/lib/components/admin/SettingsModal.svelte b/src/lib/components/admin/SettingsModal.svelte index 38a2602b6..6da1bda6f 100644 --- a/src/lib/components/admin/SettingsModal.svelte +++ b/src/lib/components/admin/SettingsModal.svelte @@ -8,6 +8,7 @@ import Banners from '$lib/components/admin/Settings/Banners.svelte'; import { toast } from 'svelte-sonner'; + import Pipelines from './Settings/Pipelines.svelte'; const i18n = getContext('i18n'); @@ -149,6 +150,36 @@ </div> <div class=" self-center">{$i18n.t('Banners')}</div> </button> + + <button + class="px-2.5 py-2.5 min-w-fit rounded-lg flex-1 md:flex-none flex text-right transition {selectedTab === + 'pipelines' + ? 'bg-gray-200 dark:bg-gray-700' + : ' hover:bg-gray-300 dark:hover:bg-gray-800'}" + on:click={() => { + selectedTab = 'pipelines'; + }} + > + <div class=" self-center mr-2"> + <svg + xmlns="http://www.w3.org/2000/svg" + viewBox="0 0 24 24" + fill="currentColor" + class="size-4" + > + <path + d="M11.644 1.59a.75.75 0 0 1 .712 0l9.75 5.25a.75.75 0 0 1 0 1.32l-9.75 5.25a.75.75 0 0 1-.712 0l-9.75-5.25a.75.75 0 0 1 0-1.32l9.75-5.25Z" + /> + <path + d="m3.265 10.602 7.668 4.129a2.25 2.25 0 0 0 2.134 0l7.668-4.13 1.37.739a.75.75 0 0 1 0 1.32l-9.75 5.25a.75.75 0 0 1-.71 0l-9.75-5.25a.75.75 0 0 1 0-1.32l1.37-.738Z" + /> + <path + d="m10.933 19.231-7.668-4.13-1.37.739a.75.75 0 0 0 0 1.32l9.75 5.25c.221.12.489.12.71 0l9.75-5.25a.75.75 0 0 0 0-1.32l-1.37-.738-7.668 4.13a2.25 2.25 0 0 1-2.134-.001Z" + /> + </svg> + </div> + <div class=" self-center">{$i18n.t('Pipelines')}</div> + </button> </div> <div class="flex-1 md:min-h-[380px]"> {#if selectedTab === 'general'} @@ -179,6 +210,13 @@ toast.success($i18n.t('Settings saved successfully!')); }} /> + {:else if selectedTab === 'pipelines'} + <Pipelines + saveHandler={() => { + show = false; + toast.success($i18n.t('Settings saved successfully!')); + }} + /> {/if} </div> </div> From 130d15a2fb4b1fdf5a98ab8e9475ab7f5bdcaec8 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" <timothyjrbeck@gmail.com> Date: Tue, 28 May 2024 12:32:49 -0700 Subject: [PATCH 4/6] feat: pipeline valves --- backend/main.py | 116 ++++++++++++++++++ src/lib/apis/index.ts | 89 ++++++++++++++ .../admin/Settings/Pipelines.svelte | 74 ++++++++--- 3 files changed, 265 insertions(+), 14 deletions(-) diff --git a/backend/main.py b/backend/main.py index 648bedea0..2de27111d 100644 --- a/backend/main.py +++ b/backend/main.py @@ -471,6 +471,122 @@ async def get_pipelines(user=Depends(get_admin_user)): return {"data": pipelines} +@app.get("/api/pipelines/{pipeline_id}/valves") +async def get_pipeline_valves(pipeline_id: str, user=Depends(get_admin_user)): + models = await get_all_models() + if pipeline_id in app.state.MODELS and "pipeline" in app.state.MODELS[pipeline_id]: + pipeline = app.state.MODELS[pipeline_id] + + try: + urlIdx = pipeline["urlIdx"] + + url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx] + key = openai_app.state.config.OPENAI_API_KEYS[urlIdx] + + if key != "": + headers = {"Authorization": f"Bearer {key}"} + r = requests.get(f"{url}/{pipeline['id']}/valves", headers=headers) + + r.raise_for_status() + data = r.json() + + return {**data} + except Exception as e: + # Handle connection error here + print(f"Connection error: {e}") + + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + + return {"data": pipeline["pipeline"]["valves"]} + else: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + + +@app.get("/api/pipelines/{pipeline_id}/valves/spec") +async def get_pipeline_valves_spec(pipeline_id: str, user=Depends(get_admin_user)): + models = await get_all_models() + if pipeline_id in app.state.MODELS and "pipeline" in app.state.MODELS[pipeline_id]: + pipeline = app.state.MODELS[pipeline_id] + + try: + urlIdx = pipeline["urlIdx"] + + url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx] + key = openai_app.state.config.OPENAI_API_KEYS[urlIdx] + + if key != "": + headers = {"Authorization": f"Bearer {key}"} + r = requests.get(f"{url}/{pipeline['id']}/valves/spec", headers=headers) + + r.raise_for_status() + data = r.json() + + return {**data} + except Exception as e: + # Handle connection error here + print(f"Connection error: {e}") + + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + + return {"data": pipeline["pipeline"]["valves"]} + else: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + + +@app.post("/api/pipelines/{pipeline_id}/valves/update") +async def update_pipeline_valves( + pipeline_id: str, form_data: dict, user=Depends(get_admin_user) +): + models = await get_all_models() + + if pipeline_id in app.state.MODELS and "pipeline" in app.state.MODELS[pipeline_id]: + pipeline = app.state.MODELS[pipeline_id] + + try: + urlIdx = pipeline["urlIdx"] + + url = openai_app.state.config.OPENAI_API_BASE_URLS[urlIdx] + key = openai_app.state.config.OPENAI_API_KEYS[urlIdx] + + if key != "": + headers = {"Authorization": f"Bearer {key}"} + r = requests.post( + f"{url}/{pipeline['id']}/valves/update", + headers=headers, + json={**form_data}, + ) + + r.raise_for_status() + data = r.json() + + return {**data} + except Exception as e: + # Handle connection error here + print(f"Connection error: {e}") + + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + else: + raise HTTPException( + status_code=status.HTTP_404_NOT_FOUND, + detail="Pipeline not found", + ) + + @app.get("/api/config") async def get_app_config(): # Checking and Handling the Absence of 'ui' in CONFIG_DATA diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index 3d6e51684..9850581f9 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -78,6 +78,95 @@ export const getPipelines = async (token: string = '') => { return pipelines; }; +export const getPipelineValves = async (token: string = '', pipeline_id: string) => { + let error = null; + + const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err; + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + +export const getPipelineValvesSpec = async (token: string = '', pipeline_id: string) => { + let error = null; + + const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/spec`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err; + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + +export const updatePipelineValves = async ( + token: string = '', + pipeline_id: string, + valves: object +) => { + let error = null; + + const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/update`, { + method: 'POST', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + }, + body: JSON.stringify(valves) + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err; + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + export const getBackendConfig = async () => { let error = null; diff --git a/src/lib/components/admin/Settings/Pipelines.svelte b/src/lib/components/admin/Settings/Pipelines.svelte index b1b34ecca..62007bdc0 100644 --- a/src/lib/components/admin/Settings/Pipelines.svelte +++ b/src/lib/components/admin/Settings/Pipelines.svelte @@ -2,21 +2,33 @@ import { v4 as uuidv4 } from 'uuid'; import { getContext, onMount } from 'svelte'; - import { models } from '$lib/stores'; import type { Writable } from 'svelte/store'; import type { i18n as i18nType } from 'i18next'; - import Tooltip from '$lib/components/common/Tooltip.svelte'; - import Switch from '$lib/components/common/Switch.svelte'; import { stringify } from 'postcss'; - import { getPipelines } from '$lib/apis'; + import { getPipelineValves, getPipelines } from '$lib/apis'; + import Spinner from '$lib/components/common/Spinner.svelte'; + const i18n: Writable<i18nType> = getContext('i18n'); export let saveHandler: Function; - let pipelines = []; + let pipelines = null; + let valves = null; + let selectedPipelineIdx = 0; + $: if ( + pipelines !== null && + pipelines.length > 0 && + pipelines[selectedPipelineIdx] !== undefined && + pipelines[selectedPipelineIdx].pipeline.valves + ) { + (async () => { + valves = await getPipelineValves(localStorage.token, pipelines[selectedPipelineIdx].id); + })(); + } + onMount(async () => { pipelines = await getPipelines(localStorage.token); }); @@ -28,21 +40,55 @@ saveHandler(); }} > - <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80 h-full"> - <div class=" space-y-3 pr-1.5"> + <div class=" space-y-2 pr-1.5 overflow-y-scroll max-h-80 h-full"> + {#if pipelines !== null} <div class="flex w-full justify-between mb-2"> <div class=" self-center text-sm font-semibold"> - {$i18n.t('Pipeline Valves')} + {$i18n.t('Pipelines')} </div> </div> - <div class="flex flex-col space-y-1"> - {#each pipelines as pipeline} - <div class=" flex justify-between"> - {JSON.stringify(pipeline)} + <div class="space-y-2"> + {#if pipelines.length > 0} + <div class="flex gap-2"> + <div class="flex-1 pb-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 an Ollama instance')} + > + {#each pipelines as pipeline, idx} + <option value={idx} class="bg-gray-100 dark:bg-gray-700">{pipeline.name}</option> + {/each} + </select> + </div> </div> - {/each} + {/if} + + <div class="text-sm font-medium">{$i18n.t('Valves')}</div> + + <div class="space-y-2"> + {#if pipelines[selectedPipelineIdx].pipeline.valves} + {#if valves} + {#each Object.keys(valves) as valve, idx} + <div>{valve}</div> + {/each} + {:else} + <Spinner className="size-5" /> + {/if} + {:else} + <div>No valves</div> + {/if} + </div> </div> - </div> + {:else if pipelines !== null && pipelines.length === 0} + <div>Pipelines Not Detected</div> + {:else} + <div class="flex h-full justify-center"> + <div class="my-auto"> + <Spinner className="size-6" /> + </div> + </div> + {/if} </div> <div class="flex justify-end pt-3 text-sm font-medium"> <button From 7c271734a1bb56f4b690797af4bb4e629477f498 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" <timothyjrbeck@gmail.com> Date: Tue, 28 May 2024 12:36:22 -0700 Subject: [PATCH 5/6] fix --- src/lib/components/admin/Settings/Pipelines.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/components/admin/Settings/Pipelines.svelte b/src/lib/components/admin/Settings/Pipelines.svelte index 62007bdc0..3680e3621 100644 --- a/src/lib/components/admin/Settings/Pipelines.svelte +++ b/src/lib/components/admin/Settings/Pipelines.svelte @@ -41,7 +41,7 @@ }} > <div class=" space-y-2 pr-1.5 overflow-y-scroll max-h-80 h-full"> - {#if pipelines !== null} + {#if pipelines !== null && pipelines.length > 0} <div class="flex w-full justify-between mb-2"> <div class=" self-center text-sm font-semibold"> {$i18n.t('Pipelines')} From 2d596d7307b157ba8af762bf04154d505f5bfe44 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" <timothyjrbeck@gmail.com> Date: Tue, 28 May 2024 13:05:31 -0700 Subject: [PATCH 6/6] feat: valves full integration --- backend/main.py | 55 +++++++-- src/lib/apis/index.ts | 7 +- .../admin/Settings/Pipelines.svelte | 107 ++++++++++++++---- src/lib/components/admin/SettingsModal.svelte | 5 - 4 files changed, 138 insertions(+), 36 deletions(-) diff --git a/backend/main.py b/backend/main.py index 2de27111d..f3d8371b0 100644 --- a/backend/main.py +++ b/backend/main.py @@ -477,6 +477,7 @@ async def get_pipeline_valves(pipeline_id: str, user=Depends(get_admin_user)): if pipeline_id in app.state.MODELS and "pipeline" in app.state.MODELS[pipeline_id]: pipeline = app.state.MODELS[pipeline_id] + r = None try: urlIdx = pipeline["urlIdx"] @@ -495,12 +496,23 @@ async def get_pipeline_valves(pipeline_id: str, user=Depends(get_admin_user)): # Handle connection error here print(f"Connection error: {e}") + detail = "Pipeline not found" + + if r is not None: + try: + res = r.json() + if "detail" in res: + detail = res["detail"] + except: + pass + raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="Pipeline not found", + status_code=( + r.status_code if r is not None else status.HTTP_404_NOT_FOUND + ), + detail=detail, ) - return {"data": pipeline["pipeline"]["valves"]} else: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, @@ -514,6 +526,7 @@ async def get_pipeline_valves_spec(pipeline_id: str, user=Depends(get_admin_user if pipeline_id in app.state.MODELS and "pipeline" in app.state.MODELS[pipeline_id]: pipeline = app.state.MODELS[pipeline_id] + r = None try: urlIdx = pipeline["urlIdx"] @@ -532,12 +545,21 @@ async def get_pipeline_valves_spec(pipeline_id: str, user=Depends(get_admin_user # Handle connection error here print(f"Connection error: {e}") - raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="Pipeline not found", - ) + detail = "Pipeline not found" + if r is not None: + try: + res = r.json() + if "detail" in res: + detail = res["detail"] + except: + pass - return {"data": pipeline["pipeline"]["valves"]} + raise HTTPException( + status_code=( + r.status_code if r is not None else status.HTTP_404_NOT_FOUND + ), + detail=detail, + ) else: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, @@ -554,6 +576,7 @@ async def update_pipeline_valves( if pipeline_id in app.state.MODELS and "pipeline" in app.state.MODELS[pipeline_id]: pipeline = app.state.MODELS[pipeline_id] + r = None try: urlIdx = pipeline["urlIdx"] @@ -576,9 +599,21 @@ async def update_pipeline_valves( # Handle connection error here print(f"Connection error: {e}") + detail = "Pipeline not found" + + if r is not None: + try: + res = r.json() + if "detail" in res: + detail = res["detail"] + except: + pass + raise HTTPException( - status_code=status.HTTP_404_NOT_FOUND, - detail="Pipeline not found", + status_code=( + r.status_code if r is not None else status.HTTP_404_NOT_FOUND + ), + detail=detail, ) else: raise HTTPException( diff --git a/src/lib/apis/index.ts b/src/lib/apis/index.ts index 9850581f9..ffa5c6bbb 100644 --- a/src/lib/apis/index.ts +++ b/src/lib/apis/index.ts @@ -156,7 +156,12 @@ export const updatePipelineValves = async ( }) .catch((err) => { console.log(err); - error = err; + + if ('detail' in err) { + error = err.detail; + } else { + error = err; + } return null; }); diff --git a/src/lib/components/admin/Settings/Pipelines.svelte b/src/lib/components/admin/Settings/Pipelines.svelte index 3680e3621..dda33d055 100644 --- a/src/lib/components/admin/Settings/Pipelines.svelte +++ b/src/lib/components/admin/Settings/Pipelines.svelte @@ -1,43 +1,63 @@ <script lang="ts"> import { v4 as uuidv4 } from 'uuid'; - import { getContext, onMount } from 'svelte'; + import { getContext, onMount, tick } from 'svelte'; import type { Writable } from 'svelte/store'; import type { i18n as i18nType } from 'i18next'; import { stringify } from 'postcss'; - import { getPipelineValves, getPipelines } from '$lib/apis'; + import { + getPipelineValves, + getPipelineValvesSpec, + updatePipelineValves, + getPipelines + } from '$lib/apis'; import Spinner from '$lib/components/common/Spinner.svelte'; + import { toast } from 'svelte-sonner'; const i18n: Writable<i18nType> = getContext('i18n'); export let saveHandler: Function; let pipelines = null; + let valves = null; + let valves_spec = null; - let selectedPipelineIdx = 0; + let selectedPipelineIdx = null; - $: if ( - pipelines !== null && - pipelines.length > 0 && - pipelines[selectedPipelineIdx] !== undefined && - pipelines[selectedPipelineIdx].pipeline.valves - ) { - (async () => { - valves = await getPipelineValves(localStorage.token, pipelines[selectedPipelineIdx].id); - })(); - } + const updateHandler = async () => { + const pipeline = pipelines[selectedPipelineIdx]; + + if (pipeline && (pipeline?.pipeline?.valves ?? false)) { + const res = await updatePipelineValves(localStorage.token, pipeline.id, valves).catch( + (error) => { + toast.error(error); + } + ); + + if (res) { + toast.success('Valves updated successfully'); + saveHandler(); + } + } else { + toast.error('No valves to update'); + } + }; onMount(async () => { pipelines = await getPipelines(localStorage.token); + + if (pipelines.length > 0) { + selectedPipelineIdx = 0; + } }); </script> <form class="flex flex-col h-full justify-between space-y-3 text-sm" on:submit|preventDefault={async () => { - saveHandler(); + updateHandler(); }} > <div class=" space-y-2 pr-1.5 overflow-y-scroll max-h-80 h-full"> @@ -47,17 +67,30 @@ {$i18n.t('Pipelines')} </div> </div> - <div class="space-y-2"> + <div class="space-y-1"> {#if pipelines.length > 0} <div class="flex gap-2"> <div class="flex-1 pb-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 an Ollama instance')} + placeholder={$i18n.t('Select a pipeline')} + on:change={async () => { + await tick(); + valves_spec = await getPipelineValvesSpec( + localStorage.token, + pipelines[selectedPipelineIdx].id + ); + valves = await getPipelineValves( + localStorage.token, + pipelines[selectedPipelineIdx].id + ); + }} > {#each pipelines as pipeline, idx} - <option value={idx} class="bg-gray-100 dark:bg-gray-700">{pipeline.name}</option> + <option value={idx} class="bg-gray-100 dark:bg-gray-700" + >{pipeline.name} ({pipeline.pipeline.type ?? 'pipe'})</option + > {/each} </select> </div> @@ -66,11 +99,45 @@ <div class="text-sm font-medium">{$i18n.t('Valves')}</div> - <div class="space-y-2"> + <div class="space-y-1"> {#if pipelines[selectedPipelineIdx].pipeline.valves} {#if valves} - {#each Object.keys(valves) as valve, idx} - <div>{valve}</div> + {#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> + + <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> + + {#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> + {/if} + </div> {/each} {:else} <Spinner className="size-5" /> diff --git a/src/lib/components/admin/SettingsModal.svelte b/src/lib/components/admin/SettingsModal.svelte index 6da1bda6f..78f48cdfc 100644 --- a/src/lib/components/admin/SettingsModal.svelte +++ b/src/lib/components/admin/SettingsModal.svelte @@ -185,35 +185,30 @@ {#if selectedTab === 'general'} <General saveHandler={() => { - show = false; toast.success($i18n.t('Settings saved successfully!')); }} /> {:else if selectedTab === 'users'} <Users saveHandler={() => { - show = false; toast.success($i18n.t('Settings saved successfully!')); }} /> {:else if selectedTab === 'db'} <Database saveHandler={() => { - show = false; toast.success($i18n.t('Settings saved successfully!')); }} /> {:else if selectedTab === 'banners'} <Banners saveHandler={() => { - show = false; toast.success($i18n.t('Settings saved successfully!')); }} /> {:else if selectedTab === 'pipelines'} <Pipelines saveHandler={() => { - show = false; toast.success($i18n.t('Settings saved successfully!')); }} />