index.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. type FolderForm = {
  3. name: string;
  4. data?: Record<string, any>;
  5. };
  6. export const createNewFolder = async (token: string, folderForm: FolderForm) => {
  7. let error = null;
  8. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/`, {
  9. method: 'POST',
  10. headers: {
  11. Accept: 'application/json',
  12. 'Content-Type': 'application/json',
  13. authorization: `Bearer ${token}`
  14. },
  15. body: JSON.stringify(folderForm)
  16. })
  17. .then(async (res) => {
  18. if (!res.ok) throw await res.json();
  19. return res.json();
  20. })
  21. .catch((err) => {
  22. error = err.detail;
  23. return null;
  24. });
  25. if (error) {
  26. throw error;
  27. }
  28. return res;
  29. };
  30. export const getFolders = async (token: string = '') => {
  31. let error = null;
  32. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/`, {
  33. method: 'GET',
  34. headers: {
  35. Accept: 'application/json',
  36. 'Content-Type': 'application/json',
  37. authorization: `Bearer ${token}`
  38. }
  39. })
  40. .then(async (res) => {
  41. if (!res.ok) throw await res.json();
  42. return res.json();
  43. })
  44. .then((json) => {
  45. return json;
  46. })
  47. .catch((err) => {
  48. error = err.detail;
  49. console.error(err);
  50. return null;
  51. });
  52. if (error) {
  53. throw error;
  54. }
  55. return res;
  56. };
  57. export const getFolderById = async (token: string, id: string) => {
  58. let error = null;
  59. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/${id}`, {
  60. method: 'GET',
  61. headers: {
  62. Accept: 'application/json',
  63. 'Content-Type': 'application/json',
  64. authorization: `Bearer ${token}`
  65. }
  66. })
  67. .then(async (res) => {
  68. if (!res.ok) throw await res.json();
  69. return res.json();
  70. })
  71. .then((json) => {
  72. return json;
  73. })
  74. .catch((err) => {
  75. error = err.detail;
  76. console.error(err);
  77. return null;
  78. });
  79. if (error) {
  80. throw error;
  81. }
  82. return res;
  83. };
  84. export const updateFolderById = async (token: string, id: string, folderForm: FolderForm) => {
  85. let error = null;
  86. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/${id}/update`, {
  87. method: 'POST',
  88. headers: {
  89. Accept: 'application/json',
  90. 'Content-Type': 'application/json',
  91. authorization: `Bearer ${token}`
  92. },
  93. body: JSON.stringify(folderForm)
  94. })
  95. .then(async (res) => {
  96. if (!res.ok) throw await res.json();
  97. return res.json();
  98. })
  99. .then((json) => {
  100. return json;
  101. })
  102. .catch((err) => {
  103. error = err.detail;
  104. console.error(err);
  105. return null;
  106. });
  107. if (error) {
  108. throw error;
  109. }
  110. return res;
  111. };
  112. export const updateFolderIsExpandedById = async (
  113. token: string,
  114. id: string,
  115. isExpanded: boolean
  116. ) => {
  117. let error = null;
  118. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/${id}/update/expanded`, {
  119. method: 'POST',
  120. headers: {
  121. Accept: 'application/json',
  122. 'Content-Type': 'application/json',
  123. authorization: `Bearer ${token}`
  124. },
  125. body: JSON.stringify({
  126. is_expanded: isExpanded
  127. })
  128. })
  129. .then(async (res) => {
  130. if (!res.ok) throw await res.json();
  131. return res.json();
  132. })
  133. .then((json) => {
  134. return json;
  135. })
  136. .catch((err) => {
  137. error = err.detail;
  138. console.error(err);
  139. return null;
  140. });
  141. if (error) {
  142. throw error;
  143. }
  144. return res;
  145. };
  146. export const updateFolderParentIdById = async (token: string, id: string, parentId?: string) => {
  147. let error = null;
  148. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/${id}/update/parent`, {
  149. method: 'POST',
  150. headers: {
  151. Accept: 'application/json',
  152. 'Content-Type': 'application/json',
  153. authorization: `Bearer ${token}`
  154. },
  155. body: JSON.stringify({
  156. parent_id: parentId
  157. })
  158. })
  159. .then(async (res) => {
  160. if (!res.ok) throw await res.json();
  161. return res.json();
  162. })
  163. .then((json) => {
  164. return json;
  165. })
  166. .catch((err) => {
  167. error = err.detail;
  168. console.error(err);
  169. return null;
  170. });
  171. if (error) {
  172. throw error;
  173. }
  174. return res;
  175. };
  176. type FolderItems = {
  177. chat_ids: string[];
  178. file_ids: string[];
  179. };
  180. export const updateFolderItemsById = async (token: string, id: string, items: FolderItems) => {
  181. let error = null;
  182. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/${id}/update/items`, {
  183. method: 'POST',
  184. headers: {
  185. Accept: 'application/json',
  186. 'Content-Type': 'application/json',
  187. authorization: `Bearer ${token}`
  188. },
  189. body: JSON.stringify({
  190. items: items
  191. })
  192. })
  193. .then(async (res) => {
  194. if (!res.ok) throw await res.json();
  195. return res.json();
  196. })
  197. .then((json) => {
  198. return json;
  199. })
  200. .catch((err) => {
  201. error = err.detail;
  202. console.error(err);
  203. return null;
  204. });
  205. if (error) {
  206. throw error;
  207. }
  208. return res;
  209. };
  210. export const deleteFolderById = async (token: string, id: string) => {
  211. let error = null;
  212. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/${id}`, {
  213. method: 'DELETE',
  214. headers: {
  215. Accept: 'application/json',
  216. 'Content-Type': 'application/json',
  217. authorization: `Bearer ${token}`
  218. }
  219. })
  220. .then(async (res) => {
  221. if (!res.ok) throw await res.json();
  222. return res.json();
  223. })
  224. .then((json) => {
  225. return json;
  226. })
  227. .catch((err) => {
  228. error = err.detail;
  229. console.error(err);
  230. return null;
  231. });
  232. if (error) {
  233. throw error;
  234. }
  235. return res;
  236. };