Skip to content

Commit 27d6130

Browse files
committed
refac: tools valves
1 parent 685cca5 commit 27d6130

File tree

4 files changed

+136
-27
lines changed

4 files changed

+136
-27
lines changed

backend/open_webui/models/tools.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ class ToolResponse(BaseModel):
9595
class ToolUserResponse(ToolResponse):
9696
user: Optional[UserResponse] = None
9797

98+
model_config = ConfigDict(extra="allow")
99+
98100

99101
class ToolForm(BaseModel):
100102
id: str

backend/open_webui/routers/tools.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@
4141

4242
@router.get("/", response_model=list[ToolUserResponse])
4343
async def get_tools(request: Request, user=Depends(get_verified_user)):
44-
tools = Tools.get_tools()
44+
tools = [
45+
ToolUserResponse(
46+
**{
47+
**tool.model_dump(),
48+
"has_user_valves": "class UserValves(BaseModel):" in tool.content,
49+
}
50+
)
51+
for tool in Tools.get_tools()
52+
]
4553

4654
# OpenAPI Tool Servers
4755
for server in await get_tool_servers(request):

src/lib/components/chat/MessageInput/IntegrationsMenu.svelte

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import Terminal from '$lib/components/icons/Terminal.svelte';
2020
import ChevronRight from '$lib/components/icons/ChevronRight.svelte';
2121
import ChevronLeft from '$lib/components/icons/ChevronLeft.svelte';
22+
import ValvesModal from '$lib/components/workspace/common/ValvesModal.svelte';
2223
2324
const i18n = getContext('i18n');
2425
@@ -43,6 +44,11 @@
4344
let show = false;
4445
let tab = '';
4546
47+
let showValvesModal = false;
48+
49+
let selectedValvesType = 'tool';
50+
let selectedValvesItemId = null;
51+
4652
let tools = null;
4753
4854
$: if (show) {
@@ -64,7 +70,8 @@
6470
a[tool.id] = {
6571
name: tool.name,
6672
description: tool.meta.description,
67-
enabled: selectedToolIds.includes(tool.id)
73+
enabled: selectedToolIds.includes(tool.id),
74+
...tool
6875
};
6976
return a;
7077
}, {});
@@ -87,6 +94,16 @@
8794
};
8895
</script>
8996

97+
<ValvesModal
98+
bind:show={showValvesModal}
99+
userValves={true}
100+
type={selectedValvesType}
101+
id={selectedValvesItemId ?? null}
102+
on:save={async () => {
103+
await tick();
104+
}}
105+
/>
106+
90107
<Dropdown
91108
bind:show
92109
on:change={(e) => {
@@ -322,6 +339,44 @@
322339
</div>
323340
</div>
324341

342+
{#if tools[toolId]?.has_user_valves}
343+
<div class=" shrink-0">
344+
<Tooltip content={$i18n.t('Valves')}>
345+
<button
346+
class="self-center w-fit text-sm text-gray-600 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300 transition rounded-full"
347+
type="button"
348+
on:click={(e) => {
349+
e.stopPropagation();
350+
e.preventDefault();
351+
selectedValvesType = 'tool';
352+
selectedValvesItemId = toolId;
353+
showValvesModal = true;
354+
}}
355+
>
356+
<svg
357+
xmlns="http://www.w3.org/2000/svg"
358+
fill="none"
359+
viewBox="0 0 24 24"
360+
stroke-width="1.5"
361+
stroke="currentColor"
362+
class="size-4"
363+
>
364+
<path
365+
stroke-linecap="round"
366+
stroke-linejoin="round"
367+
d="M9.594 3.94c.09-.542.56-.94 1.11-.94h2.593c.55 0 1.02.398 1.11.94l.213 1.281c.063.374.313.686.645.87.074.04.147.083.22.127.325.196.72.257 1.075.124l1.217-.456a1.125 1.125 0 0 1 1.37.49l1.296 2.247a1.125 1.125 0 0 1-.26 1.431l-1.003.827c-.293.241-.438.613-.43.992a7.723 7.723 0 0 1 0 .255c-.008.378.137.75.43.991l1.004.827c.424.35.534.955.26 1.43l-1.298 2.247a1.125 1.125 0 0 1-1.369.491l-1.217-.456c-.355-.133-.75-.072-1.076.124a6.47 6.47 0 0 1-.22.128c-.331.183-.581.495-.644.869l-.213 1.281c-.09.543-.56.94-1.11.94h-2.594c-.55 0-1.019-.398-1.11-.94l-.213-1.281c-.062-.374-.312-.686-.644-.87a6.52 6.52 0 0 1-.22-.127c-.325-.196-.72-.257-1.076-.124l-1.217.456a1.125 1.125 0 0 1-1.369-.49l-1.297-2.247a1.125 1.125 0 0 1 .26-1.431l1.004-.827c.292-.24.437-.613.43-.991a6.932 6.932 0 0 1 0-.255c.007-.38-.138-.751-.43-.992l-1.004-.827a1.125 1.125 0 0 1-.26-1.43l1.297-2.247a1.125 1.125 0 0 1 1.37-.491l1.216.456c.356.133.751.072 1.076-.124.072-.044.146-.086.22-.128.332-.183.582-.495.644-.869l.214-1.28Z"
368+
/>
369+
<path
370+
stroke-linecap="round"
371+
stroke-linejoin="round"
372+
d="M15 12a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
373+
/>
374+
</svg>
375+
</button>
376+
</Tooltip>
377+
</div>
378+
{/if}
379+
325380
<div class=" shrink-0">
326381
<Switch
327382
state={tools[toolId].enabled}

src/lib/components/workspace/common/ValvesModal.svelte

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@
1111
updateFunctionValvesById
1212
} from '$lib/apis/functions';
1313
import { getToolValvesById, getToolValvesSpecById, updateToolValvesById } from '$lib/apis/tools';
14+
15+
import {
16+
getUserValvesSpecById as getToolUserValvesSpecById,
17+
getUserValvesById as getToolUserValvesById,
18+
updateUserValvesById as updateToolUserValvesById,
19+
getTools
20+
} from '$lib/apis/tools';
21+
import {
22+
getUserValvesSpecById as getFunctionUserValvesSpecById,
23+
getUserValvesById as getFunctionUserValvesById,
24+
updateUserValvesById as updateFunctionUserValvesById,
25+
getFunctions
26+
} from '$lib/apis/functions';
27+
1428
import Spinner from '../../common/Spinner.svelte';
1529
import Switch from '$lib/components/common/Switch.svelte';
1630
import Valves from '$lib/components/common/Valves.svelte';
@@ -23,6 +37,7 @@
2337
2438
export let type = 'tool';
2539
export let id = null;
40+
export let userValves = false;
2641
2742
let saving = false;
2843
let loading = false;
@@ -43,14 +58,28 @@
4358
4459
let res = null;
4560
46-
if (type === 'tool') {
47-
res = await updateToolValvesById(localStorage.token, id, valves).catch((error) => {
48-
toast.error(`${error}`);
49-
});
50-
} else if (type === 'function') {
51-
res = await updateFunctionValvesById(localStorage.token, id, valves).catch((error) => {
52-
toast.error(`${error}`);
53-
});
61+
if (userValves) {
62+
if (type === 'tool') {
63+
res = await updateToolUserValvesById(localStorage.token, id, valves).catch((error) => {
64+
toast.error(`${error}`);
65+
});
66+
} else if (type === 'function') {
67+
res = await updateFunctionUserValvesById(localStorage.token, id, valves).catch(
68+
(error) => {
69+
toast.error(`${error}`);
70+
}
71+
);
72+
}
73+
} else {
74+
if (type === 'tool') {
75+
res = await updateToolValvesById(localStorage.token, id, valves).catch((error) => {
76+
toast.error(`${error}`);
77+
});
78+
} else if (type === 'function') {
79+
res = await updateFunctionValvesById(localStorage.token, id, valves).catch((error) => {
80+
toast.error(`${error}`);
81+
});
82+
}
5483
}
5584
5685
if (res) {
@@ -67,28 +96,43 @@
6796
valves = {};
6897
valvesSpec = null;
6998
70-
if (type === 'tool') {
71-
valves = await getToolValvesById(localStorage.token, id);
72-
valvesSpec = await getToolValvesSpecById(localStorage.token, id);
73-
} else if (type === 'function') {
74-
valves = await getFunctionValvesById(localStorage.token, id);
75-
valvesSpec = await getFunctionValvesSpecById(localStorage.token, id);
76-
}
99+
try {
100+
if (userValves) {
101+
if (type === 'tool') {
102+
valves = await getToolUserValvesById(localStorage.token, id);
103+
valvesSpec = await getToolUserValvesSpecById(localStorage.token, id);
104+
} else if (type === 'function') {
105+
valves = await getFunctionUserValvesById(localStorage.token, id);
106+
valvesSpec = await getFunctionUserValvesSpecById(localStorage.token, id);
107+
}
108+
} else {
109+
if (type === 'tool') {
110+
valves = await getToolValvesById(localStorage.token, id);
111+
valvesSpec = await getToolValvesSpecById(localStorage.token, id);
112+
} else if (type === 'function') {
113+
valves = await getFunctionValvesById(localStorage.token, id);
114+
valvesSpec = await getFunctionValvesSpecById(localStorage.token, id);
115+
}
116+
}
77117
78-
if (!valves) {
79-
valves = {};
80-
}
118+
if (!valves) {
119+
valves = {};
120+
}
81121
82-
if (valvesSpec) {
83-
// Convert array to string
84-
for (const property in valvesSpec.properties) {
85-
if (valvesSpec.properties[property]?.type === 'array') {
86-
valves[property] = (valves[property] ?? []).join(',');
122+
if (valvesSpec) {
123+
// Convert array to string
124+
for (const property in valvesSpec.properties) {
125+
if (valvesSpec.properties[property]?.type === 'array') {
126+
valves[property] = (valves[property] ?? []).join(',');
127+
}
87128
}
88129
}
89-
}
90130
91-
loading = false;
131+
loading = false;
132+
} catch (e) {
133+
toast.error(`Error fetching valves`);
134+
show = false;
135+
}
92136
};
93137
94138
$: if (show) {

0 commit comments

Comments
 (0)