feat: export tools

This commit is contained in:
Timothy J. Baek 2024-06-10 21:36:13 -07:00
parent b434ebf3ad
commit 1611a3aa70
3 changed files with 43 additions and 5 deletions

View File

@ -62,7 +62,7 @@ async def get_toolkits(user=Depends(get_current_user)):
@router.get("/export", response_model=List[ToolModel])
async def get_toolkits(user=Depends(get_current_user)):
async def get_toolkits(user=Depends(get_admin_user)):
toolkits = [toolkit for toolkit in Tools.get_tools()]
return toolkits

View File

@ -62,6 +62,37 @@ export const getTools = async (token: string = '') => {
return res;
};
export const exportTools = async (token: string = '') => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/tools/export`, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
}
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.then((json) => {
return json;
})
.catch((err) => {
error = err.detail;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};
export const getToolById = async (token: string, id: string) => {
let error = null;

View File

@ -8,7 +8,7 @@
import { createNewPrompt, deletePromptByCommand, getPrompts } from '$lib/apis/prompts';
import { goto } from '$app/navigation';
import { deleteToolById, getTools } from '$lib/apis/tools';
import { deleteToolById, exportTools, getTools } from '$lib/apis/tools';
const i18n = getContext('i18n');
@ -221,10 +221,17 @@
<button
class="flex text-xs items-center space-x-1 px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 dark:text-gray-200 transition"
on:click={async () => {
let blob = new Blob([JSON.stringify($tools)], {
type: 'application/json'
const _tools = await exportTools(localStorage.token).catch((error) => {
toast.error(error);
return null;
});
saveAs(blob, `tools-export-${Date.now()}.json`);
if (_tools) {
let blob = new Blob([JSON.stringify(_tools)], {
type: 'application/json'
});
saveAs(blob, `tools-export-${Date.now()}.json`);
}
}}
>
<div class=" self-center mr-2 font-medium">{$i18n.t('Export Tools')}</div>