feat: groups integration

This commit is contained in:
Timothy Jaeryang Baek 2024-11-14 18:37:42 -08:00
parent 2f893fd373
commit dae764fa5a
3 changed files with 217 additions and 52 deletions

View File

@ -0,0 +1,163 @@
import { WEBUI_API_BASE_URL } from '$lib/constants';
export const createNewGroup = async (token: string, group: object) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/groups/create`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({
...group
})
})
.then(async (res) => {
if (!res.ok) throw await res.json();
return res.json();
})
.catch((err) => {
error = err.detail;
console.log(err);
return null;
});
if (error) {
throw error;
}
return res;
};
export const getGroups = async (token: string = '') => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/groups/`, {
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 getGroupById = async (token: string, id: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/groups/id/${id}`, {
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 updateGroupById = async (token: string, id: string, group: object) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/groups/id/${id}/update`, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({
...group
})
})
.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 deleteGroupById = async (token: string, id: string) => {
let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/groups/id/${id}/delete`, {
method: 'DELETE',
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;
};

View File

@ -22,6 +22,7 @@
import Pencil from '$lib/components/icons/Pencil.svelte'; import Pencil from '$lib/components/icons/Pencil.svelte';
import GroupItem from './Groups/GroupItem.svelte'; import GroupItem from './Groups/GroupItem.svelte';
import AddGroupModal from './Groups/AddGroupModal.svelte'; import AddGroupModal from './Groups/AddGroupModal.svelte';
import { createNewGroup, getGroups } from '$lib/apis/groups';
const i18n = getContext('i18n'); const i18n = getContext('i18n');
@ -51,56 +52,58 @@
if ($user?.role !== 'admin') { if ($user?.role !== 'admin') {
await goto('/'); await goto('/');
} else { } else {
groups = [ groups = await getGroups(localStorage.token);
{
id: '1', // [
name: 'Group A', // {
description: 'Group A description', // id: '1',
permissions: { // name: 'Group A',
model: { // description: 'Group A description',
enable_filter: false, // boolean // permissions: {
ids: [], // array of strings // model: {
default_id: null // null or string // enable_filter: false, // boolean
}, // ids: [], // array of strings
workspace: { // default_id: null // null or string
models: false, // boolean // },
knowledge: false, // boolean // workspace: {
prompts: false // boolean // models: false, // boolean
}, // knowledge: false, // boolean
chat: { // prompts: false // boolean
delete: true, // boolean // },
edit: true, // boolean // chat: {
temporary: true // boolean // delete: true, // boolean
} // edit: true, // boolean
}, // temporary: true // boolean
user_ids: ['1', '2', '3'], // array of strings // }
admin_ids: ['1'] // array of strings // },
}, // user_ids: ['1', '2', '3'], // array of strings
{ // admin_ids: ['1'] // array of strings
id: '2', // },
name: 'Moderators', // {
description: 'Moderators description', // id: '2',
permissions: { // name: 'Moderators',
model: { // description: 'Moderators description',
enable_filter: false, // boolean // permissions: {
ids: [], // array of strings // model: {
default_id: null // null or string // enable_filter: false, // boolean
}, // ids: [], // array of strings
workspace: { // default_id: null // null or string
models: false, // boolean // },
knowledge: false, // boolean // workspace: {
prompts: false // boolean // models: false, // boolean
}, // knowledge: false, // boolean
chat: { // prompts: false // boolean
delete: true, // boolean // },
edit: true, // boolean // chat: {
temporary: true // boolean // delete: true, // boolean
} // edit: true, // boolean
}, // temporary: true // boolean
user_ids: ['1', '5', '6'], // array of strings // }
admin_ids: ['1'] // array of strings // },
} // user_ids: ['1', '5', '6'], // array of strings
]; // admin_ids: ['1'] // array of strings
// }
// ];
} }
loaded = true; loaded = true;
}); });

View File

@ -19,8 +19,7 @@
const group = { const group = {
name, name,
description, description
user_ids: userIds
}; };
await onSubmit(group); await onSubmit(group);