index.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const createNewFolder = async (token: string, name: string) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/`, {
  5. method: 'POST',
  6. headers: {
  7. Accept: 'application/json',
  8. 'Content-Type': 'application/json',
  9. authorization: `Bearer ${token}`
  10. },
  11. body: JSON.stringify({
  12. name: name
  13. })
  14. })
  15. .then(async (res) => {
  16. if (!res.ok) throw await res.json();
  17. return res.json();
  18. })
  19. .catch((err) => {
  20. error = err.detail;
  21. return null;
  22. });
  23. if (error) {
  24. throw error;
  25. }
  26. return res;
  27. };
  28. export const getFolders = async (token: string = '') => {
  29. let error = null;
  30. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/`, {
  31. method: 'GET',
  32. headers: {
  33. Accept: 'application/json',
  34. 'Content-Type': 'application/json',
  35. authorization: `Bearer ${token}`
  36. }
  37. })
  38. .then(async (res) => {
  39. if (!res.ok) throw await res.json();
  40. return res.json();
  41. })
  42. .then((json) => {
  43. return json;
  44. })
  45. .catch((err) => {
  46. error = err.detail;
  47. console.log(err);
  48. return null;
  49. });
  50. if (error) {
  51. throw error;
  52. }
  53. return res;
  54. };
  55. export const getFolderById = async (token: string, id: string) => {
  56. let error = null;
  57. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/${id}`, {
  58. method: 'GET',
  59. headers: {
  60. Accept: 'application/json',
  61. 'Content-Type': 'application/json',
  62. authorization: `Bearer ${token}`
  63. }
  64. })
  65. .then(async (res) => {
  66. if (!res.ok) throw await res.json();
  67. return res.json();
  68. })
  69. .then((json) => {
  70. return json;
  71. })
  72. .catch((err) => {
  73. error = err.detail;
  74. console.log(err);
  75. return null;
  76. });
  77. if (error) {
  78. throw error;
  79. }
  80. return res;
  81. };
  82. export const updateFolderNameById = async (token: string, id: string, name: string) => {
  83. let error = null;
  84. const res = await fetch(`${WEBUI_API_BASE_URL}/folders/${id}/update`, {
  85. method: 'POST',
  86. headers: {
  87. Accept: 'application/json',
  88. 'Content-Type': 'application/json',
  89. authorization: `Bearer ${token}`
  90. },
  91. body: JSON.stringify({
  92. name: name
  93. })
  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.log(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.log(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.log(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.log(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.log(err);
  230. return null;
  231. });
  232. if (error) {
  233. throw error;
  234. }
  235. return res;
  236. };