index.ts 19 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. import { getTimeRange } from '$lib/utils';
  3. export const createNewChat = async (token: string, chat: object) => {
  4. let error = null;
  5. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/new`, {
  6. method: 'POST',
  7. headers: {
  8. Accept: 'application/json',
  9. 'Content-Type': 'application/json',
  10. authorization: `Bearer ${token}`
  11. },
  12. body: JSON.stringify({
  13. chat: chat
  14. })
  15. })
  16. .then(async (res) => {
  17. if (!res.ok) throw await res.json();
  18. return res.json();
  19. })
  20. .catch((err) => {
  21. error = err;
  22. console.log(err);
  23. return null;
  24. });
  25. if (error) {
  26. throw error;
  27. }
  28. return res;
  29. };
  30. export const importChat = async (
  31. token: string,
  32. chat: object,
  33. meta: object | null,
  34. pinned?: boolean,
  35. folderId?: string | null
  36. ) => {
  37. let error = null;
  38. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/import`, {
  39. method: 'POST',
  40. headers: {
  41. Accept: 'application/json',
  42. 'Content-Type': 'application/json',
  43. authorization: `Bearer ${token}`
  44. },
  45. body: JSON.stringify({
  46. chat: chat,
  47. meta: meta ?? {},
  48. pinned: pinned,
  49. folder_id: folderId
  50. })
  51. })
  52. .then(async (res) => {
  53. if (!res.ok) throw await res.json();
  54. return res.json();
  55. })
  56. .catch((err) => {
  57. error = err;
  58. console.log(err);
  59. return null;
  60. });
  61. if (error) {
  62. throw error;
  63. }
  64. return res;
  65. };
  66. export const getChatList = async (token: string = '', page: number | null = null) => {
  67. let error = null;
  68. const searchParams = new URLSearchParams();
  69. if (page !== null) {
  70. searchParams.append('page', `${page}`);
  71. }
  72. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/?${searchParams.toString()}`, {
  73. method: 'GET',
  74. headers: {
  75. Accept: 'application/json',
  76. 'Content-Type': 'application/json',
  77. ...(token && { authorization: `Bearer ${token}` })
  78. }
  79. })
  80. .then(async (res) => {
  81. if (!res.ok) throw await res.json();
  82. return res.json();
  83. })
  84. .then((json) => {
  85. return json;
  86. })
  87. .catch((err) => {
  88. error = err;
  89. console.log(err);
  90. return null;
  91. });
  92. if (error) {
  93. throw error;
  94. }
  95. return res.map((chat) => ({
  96. ...chat,
  97. time_range: getTimeRange(chat.updated_at)
  98. }));
  99. };
  100. export const getChatListByUserId = async (token: string = '', userId: string) => {
  101. let error = null;
  102. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/list/user/${userId}`, {
  103. method: 'GET',
  104. headers: {
  105. Accept: 'application/json',
  106. 'Content-Type': 'application/json',
  107. ...(token && { authorization: `Bearer ${token}` })
  108. }
  109. })
  110. .then(async (res) => {
  111. if (!res.ok) throw await res.json();
  112. return res.json();
  113. })
  114. .then((json) => {
  115. return json;
  116. })
  117. .catch((err) => {
  118. error = err;
  119. console.log(err);
  120. return null;
  121. });
  122. if (error) {
  123. throw error;
  124. }
  125. return res.map((chat) => ({
  126. ...chat,
  127. time_range: getTimeRange(chat.updated_at)
  128. }));
  129. };
  130. export const getArchivedChatList = async (token: string = '') => {
  131. let error = null;
  132. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/archived`, {
  133. method: 'GET',
  134. headers: {
  135. Accept: 'application/json',
  136. 'Content-Type': 'application/json',
  137. ...(token && { authorization: `Bearer ${token}` })
  138. }
  139. })
  140. .then(async (res) => {
  141. if (!res.ok) throw await res.json();
  142. return res.json();
  143. })
  144. .then((json) => {
  145. return json;
  146. })
  147. .catch((err) => {
  148. error = err;
  149. console.log(err);
  150. return null;
  151. });
  152. if (error) {
  153. throw error;
  154. }
  155. return res;
  156. };
  157. export const getAllChats = async (token: string) => {
  158. let error = null;
  159. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all`, {
  160. method: 'GET',
  161. headers: {
  162. Accept: 'application/json',
  163. 'Content-Type': 'application/json',
  164. ...(token && { authorization: `Bearer ${token}` })
  165. }
  166. })
  167. .then(async (res) => {
  168. if (!res.ok) throw await res.json();
  169. return res.json();
  170. })
  171. .then((json) => {
  172. return json;
  173. })
  174. .catch((err) => {
  175. error = err;
  176. console.log(err);
  177. return null;
  178. });
  179. if (error) {
  180. throw error;
  181. }
  182. return res;
  183. };
  184. export const getChatListBySearchText = async (token: string, text: string, page: number = 1) => {
  185. let error = null;
  186. const searchParams = new URLSearchParams();
  187. searchParams.append('text', text);
  188. searchParams.append('page', `${page}`);
  189. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/search?${searchParams.toString()}`, {
  190. method: 'GET',
  191. headers: {
  192. Accept: 'application/json',
  193. 'Content-Type': 'application/json',
  194. ...(token && { authorization: `Bearer ${token}` })
  195. }
  196. })
  197. .then(async (res) => {
  198. if (!res.ok) throw await res.json();
  199. return res.json();
  200. })
  201. .then((json) => {
  202. return json;
  203. })
  204. .catch((err) => {
  205. error = err;
  206. console.log(err);
  207. return null;
  208. });
  209. if (error) {
  210. throw error;
  211. }
  212. return res.map((chat) => ({
  213. ...chat,
  214. time_range: getTimeRange(chat.updated_at)
  215. }));
  216. };
  217. export const getChatsByFolderId = async (token: string, folderId: string) => {
  218. let error = null;
  219. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/folder/${folderId}`, {
  220. method: 'GET',
  221. headers: {
  222. Accept: 'application/json',
  223. 'Content-Type': 'application/json',
  224. ...(token && { authorization: `Bearer ${token}` })
  225. }
  226. })
  227. .then(async (res) => {
  228. if (!res.ok) throw await res.json();
  229. return res.json();
  230. })
  231. .then((json) => {
  232. return json;
  233. })
  234. .catch((err) => {
  235. error = err;
  236. console.log(err);
  237. return null;
  238. });
  239. if (error) {
  240. throw error;
  241. }
  242. return res;
  243. };
  244. export const getAllArchivedChats = async (token: string) => {
  245. let error = null;
  246. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all/archived`, {
  247. method: 'GET',
  248. headers: {
  249. Accept: 'application/json',
  250. 'Content-Type': 'application/json',
  251. ...(token && { authorization: `Bearer ${token}` })
  252. }
  253. })
  254. .then(async (res) => {
  255. if (!res.ok) throw await res.json();
  256. return res.json();
  257. })
  258. .then((json) => {
  259. return json;
  260. })
  261. .catch((err) => {
  262. error = err;
  263. console.log(err);
  264. return null;
  265. });
  266. if (error) {
  267. throw error;
  268. }
  269. return res;
  270. };
  271. export const getAllUserChats = async (token: string) => {
  272. let error = null;
  273. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all/db`, {
  274. method: 'GET',
  275. headers: {
  276. Accept: 'application/json',
  277. 'Content-Type': 'application/json',
  278. ...(token && { authorization: `Bearer ${token}` })
  279. }
  280. })
  281. .then(async (res) => {
  282. if (!res.ok) throw await res.json();
  283. return res.json();
  284. })
  285. .then((json) => {
  286. return json;
  287. })
  288. .catch((err) => {
  289. error = err;
  290. console.log(err);
  291. return null;
  292. });
  293. if (error) {
  294. throw error;
  295. }
  296. return res;
  297. };
  298. export const getAllTags = async (token: string) => {
  299. let error = null;
  300. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all/tags`, {
  301. method: 'GET',
  302. headers: {
  303. Accept: 'application/json',
  304. 'Content-Type': 'application/json',
  305. ...(token && { authorization: `Bearer ${token}` })
  306. }
  307. })
  308. .then(async (res) => {
  309. if (!res.ok) throw await res.json();
  310. return res.json();
  311. })
  312. .then((json) => {
  313. return json;
  314. })
  315. .catch((err) => {
  316. error = err;
  317. console.log(err);
  318. return null;
  319. });
  320. if (error) {
  321. throw error;
  322. }
  323. return res;
  324. };
  325. export const getPinnedChatList = async (token: string = '') => {
  326. let error = null;
  327. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/pinned`, {
  328. method: 'GET',
  329. headers: {
  330. Accept: 'application/json',
  331. 'Content-Type': 'application/json',
  332. ...(token && { authorization: `Bearer ${token}` })
  333. }
  334. })
  335. .then(async (res) => {
  336. if (!res.ok) throw await res.json();
  337. return res.json();
  338. })
  339. .then((json) => {
  340. return json;
  341. })
  342. .catch((err) => {
  343. error = err;
  344. console.log(err);
  345. return null;
  346. });
  347. if (error) {
  348. throw error;
  349. }
  350. return res.map((chat) => ({
  351. ...chat,
  352. time_range: getTimeRange(chat.updated_at)
  353. }));
  354. };
  355. export const getChatListByTagName = async (token: string = '', tagName: string) => {
  356. let error = null;
  357. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/tags`, {
  358. method: 'POST',
  359. headers: {
  360. Accept: 'application/json',
  361. 'Content-Type': 'application/json',
  362. ...(token && { authorization: `Bearer ${token}` })
  363. },
  364. body: JSON.stringify({
  365. name: tagName
  366. })
  367. })
  368. .then(async (res) => {
  369. if (!res.ok) throw await res.json();
  370. return res.json();
  371. })
  372. .then((json) => {
  373. return json;
  374. })
  375. .catch((err) => {
  376. error = err;
  377. console.log(err);
  378. return null;
  379. });
  380. if (error) {
  381. throw error;
  382. }
  383. return res.map((chat) => ({
  384. ...chat,
  385. time_range: getTimeRange(chat.updated_at)
  386. }));
  387. };
  388. export const getChatById = async (token: string, id: string) => {
  389. let error = null;
  390. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  391. method: 'GET',
  392. headers: {
  393. Accept: 'application/json',
  394. 'Content-Type': 'application/json',
  395. ...(token && { authorization: `Bearer ${token}` })
  396. }
  397. })
  398. .then(async (res) => {
  399. if (!res.ok) throw await res.json();
  400. return res.json();
  401. })
  402. .then((json) => {
  403. return json;
  404. })
  405. .catch((err) => {
  406. error = err;
  407. console.log(err);
  408. return null;
  409. });
  410. if (error) {
  411. throw error;
  412. }
  413. return res;
  414. };
  415. export const getChatByShareId = async (token: string, share_id: string) => {
  416. let error = null;
  417. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/share/${share_id}`, {
  418. method: 'GET',
  419. headers: {
  420. Accept: 'application/json',
  421. 'Content-Type': 'application/json',
  422. ...(token && { authorization: `Bearer ${token}` })
  423. }
  424. })
  425. .then(async (res) => {
  426. if (!res.ok) throw await res.json();
  427. return res.json();
  428. })
  429. .then((json) => {
  430. return json;
  431. })
  432. .catch((err) => {
  433. error = err;
  434. console.log(err);
  435. return null;
  436. });
  437. if (error) {
  438. throw error;
  439. }
  440. return res;
  441. };
  442. export const getChatPinnedStatusById = async (token: string, id: string) => {
  443. let error = null;
  444. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/pinned`, {
  445. method: 'GET',
  446. headers: {
  447. Accept: 'application/json',
  448. 'Content-Type': 'application/json',
  449. ...(token && { authorization: `Bearer ${token}` })
  450. }
  451. })
  452. .then(async (res) => {
  453. if (!res.ok) throw await res.json();
  454. return res.json();
  455. })
  456. .then((json) => {
  457. return json;
  458. })
  459. .catch((err) => {
  460. error = err;
  461. if ('detail' in err) {
  462. error = err.detail;
  463. } else {
  464. error = err;
  465. }
  466. console.log(err);
  467. return null;
  468. });
  469. if (error) {
  470. throw error;
  471. }
  472. return res;
  473. };
  474. export const toggleChatPinnedStatusById = async (token: string, id: string) => {
  475. let error = null;
  476. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/pin`, {
  477. method: 'POST',
  478. headers: {
  479. Accept: 'application/json',
  480. 'Content-Type': 'application/json',
  481. ...(token && { authorization: `Bearer ${token}` })
  482. }
  483. })
  484. .then(async (res) => {
  485. if (!res.ok) throw await res.json();
  486. return res.json();
  487. })
  488. .then((json) => {
  489. return json;
  490. })
  491. .catch((err) => {
  492. error = err;
  493. if ('detail' in err) {
  494. error = err.detail;
  495. } else {
  496. error = err;
  497. }
  498. console.log(err);
  499. return null;
  500. });
  501. if (error) {
  502. throw error;
  503. }
  504. return res;
  505. };
  506. export const cloneChatById = async (token: string, id: string) => {
  507. let error = null;
  508. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/clone`, {
  509. method: 'POST',
  510. headers: {
  511. Accept: 'application/json',
  512. 'Content-Type': 'application/json',
  513. ...(token && { authorization: `Bearer ${token}` })
  514. }
  515. })
  516. .then(async (res) => {
  517. if (!res.ok) throw await res.json();
  518. return res.json();
  519. })
  520. .then((json) => {
  521. return json;
  522. })
  523. .catch((err) => {
  524. error = err;
  525. if ('detail' in err) {
  526. error = err.detail;
  527. } else {
  528. error = err;
  529. }
  530. console.log(err);
  531. return null;
  532. });
  533. if (error) {
  534. throw error;
  535. }
  536. return res;
  537. };
  538. export const shareChatById = async (token: string, id: string) => {
  539. let error = null;
  540. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/share`, {
  541. method: 'POST',
  542. headers: {
  543. Accept: 'application/json',
  544. 'Content-Type': 'application/json',
  545. ...(token && { authorization: `Bearer ${token}` })
  546. }
  547. })
  548. .then(async (res) => {
  549. if (!res.ok) throw await res.json();
  550. return res.json();
  551. })
  552. .then((json) => {
  553. return json;
  554. })
  555. .catch((err) => {
  556. error = err;
  557. console.log(err);
  558. return null;
  559. });
  560. if (error) {
  561. throw error;
  562. }
  563. return res;
  564. };
  565. export const updateChatFolderIdById = async (token: string, id: string, folderId?: string) => {
  566. let error = null;
  567. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/folder`, {
  568. method: 'POST',
  569. headers: {
  570. Accept: 'application/json',
  571. 'Content-Type': 'application/json',
  572. ...(token && { authorization: `Bearer ${token}` })
  573. },
  574. body: JSON.stringify({
  575. folder_id: folderId
  576. })
  577. })
  578. .then(async (res) => {
  579. if (!res.ok) throw await res.json();
  580. return res.json();
  581. })
  582. .then((json) => {
  583. return json;
  584. })
  585. .catch((err) => {
  586. error = err;
  587. console.log(err);
  588. return null;
  589. });
  590. if (error) {
  591. throw error;
  592. }
  593. return res;
  594. };
  595. export const archiveChatById = async (token: string, id: string) => {
  596. let error = null;
  597. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/archive`, {
  598. method: 'POST',
  599. headers: {
  600. Accept: 'application/json',
  601. 'Content-Type': 'application/json',
  602. ...(token && { authorization: `Bearer ${token}` })
  603. }
  604. })
  605. .then(async (res) => {
  606. if (!res.ok) throw await res.json();
  607. return res.json();
  608. })
  609. .then((json) => {
  610. return json;
  611. })
  612. .catch((err) => {
  613. error = err;
  614. console.log(err);
  615. return null;
  616. });
  617. if (error) {
  618. throw error;
  619. }
  620. return res;
  621. };
  622. export const deleteSharedChatById = async (token: string, id: string) => {
  623. let error = null;
  624. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/share`, {
  625. method: 'DELETE',
  626. headers: {
  627. Accept: 'application/json',
  628. 'Content-Type': 'application/json',
  629. ...(token && { authorization: `Bearer ${token}` })
  630. }
  631. })
  632. .then(async (res) => {
  633. if (!res.ok) throw await res.json();
  634. return res.json();
  635. })
  636. .then((json) => {
  637. return json;
  638. })
  639. .catch((err) => {
  640. error = err;
  641. console.log(err);
  642. return null;
  643. });
  644. if (error) {
  645. throw error;
  646. }
  647. return res;
  648. };
  649. export const updateChatById = async (token: string, id: string, chat: object) => {
  650. let error = null;
  651. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  652. method: 'POST',
  653. headers: {
  654. Accept: 'application/json',
  655. 'Content-Type': 'application/json',
  656. ...(token && { authorization: `Bearer ${token}` })
  657. },
  658. body: JSON.stringify({
  659. chat: chat
  660. })
  661. })
  662. .then(async (res) => {
  663. if (!res.ok) throw await res.json();
  664. return res.json();
  665. })
  666. .then((json) => {
  667. return json;
  668. })
  669. .catch((err) => {
  670. error = err;
  671. console.log(err);
  672. return null;
  673. });
  674. if (error) {
  675. throw error;
  676. }
  677. return res;
  678. };
  679. export const deleteChatById = async (token: string, id: string) => {
  680. let error = null;
  681. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  682. method: 'DELETE',
  683. headers: {
  684. Accept: 'application/json',
  685. 'Content-Type': 'application/json',
  686. ...(token && { authorization: `Bearer ${token}` })
  687. }
  688. })
  689. .then(async (res) => {
  690. if (!res.ok) throw await res.json();
  691. return res.json();
  692. })
  693. .then((json) => {
  694. return json;
  695. })
  696. .catch((err) => {
  697. error = err.detail;
  698. console.log(err);
  699. return null;
  700. });
  701. if (error) {
  702. throw error;
  703. }
  704. return res;
  705. };
  706. export const getTagsById = async (token: string, id: string) => {
  707. let error = null;
  708. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  709. method: 'GET',
  710. headers: {
  711. Accept: 'application/json',
  712. 'Content-Type': 'application/json',
  713. ...(token && { authorization: `Bearer ${token}` })
  714. }
  715. })
  716. .then(async (res) => {
  717. if (!res.ok) throw await res.json();
  718. return res.json();
  719. })
  720. .then((json) => {
  721. return json;
  722. })
  723. .catch((err) => {
  724. error = err;
  725. console.log(err);
  726. return null;
  727. });
  728. if (error) {
  729. throw error;
  730. }
  731. return res;
  732. };
  733. export const addTagById = async (token: string, id: string, tagName: string) => {
  734. let error = null;
  735. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  736. method: 'POST',
  737. headers: {
  738. Accept: 'application/json',
  739. 'Content-Type': 'application/json',
  740. ...(token && { authorization: `Bearer ${token}` })
  741. },
  742. body: JSON.stringify({
  743. name: tagName
  744. })
  745. })
  746. .then(async (res) => {
  747. if (!res.ok) throw await res.json();
  748. return res.json();
  749. })
  750. .then((json) => {
  751. return json;
  752. })
  753. .catch((err) => {
  754. error = err.detail;
  755. console.log(err);
  756. return null;
  757. });
  758. if (error) {
  759. throw error;
  760. }
  761. return res;
  762. };
  763. export const deleteTagById = async (token: string, id: string, tagName: string) => {
  764. let error = null;
  765. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  766. method: 'DELETE',
  767. headers: {
  768. Accept: 'application/json',
  769. 'Content-Type': 'application/json',
  770. ...(token && { authorization: `Bearer ${token}` })
  771. },
  772. body: JSON.stringify({
  773. name: tagName
  774. })
  775. })
  776. .then(async (res) => {
  777. if (!res.ok) throw await res.json();
  778. return res.json();
  779. })
  780. .then((json) => {
  781. return json;
  782. })
  783. .catch((err) => {
  784. error = err;
  785. console.log(err);
  786. return null;
  787. });
  788. if (error) {
  789. throw error;
  790. }
  791. return res;
  792. };
  793. export const deleteTagsById = async (token: string, id: string) => {
  794. let error = null;
  795. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags/all`, {
  796. method: 'DELETE',
  797. headers: {
  798. Accept: 'application/json',
  799. 'Content-Type': 'application/json',
  800. ...(token && { authorization: `Bearer ${token}` })
  801. }
  802. })
  803. .then(async (res) => {
  804. if (!res.ok) throw await res.json();
  805. return res.json();
  806. })
  807. .then((json) => {
  808. return json;
  809. })
  810. .catch((err) => {
  811. error = err;
  812. console.log(err);
  813. return null;
  814. });
  815. if (error) {
  816. throw error;
  817. }
  818. return res;
  819. };
  820. export const deleteAllChats = async (token: string) => {
  821. let error = null;
  822. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/`, {
  823. method: 'DELETE',
  824. headers: {
  825. Accept: 'application/json',
  826. 'Content-Type': 'application/json',
  827. ...(token && { authorization: `Bearer ${token}` })
  828. }
  829. })
  830. .then(async (res) => {
  831. if (!res.ok) throw await res.json();
  832. return res.json();
  833. })
  834. .then((json) => {
  835. return json;
  836. })
  837. .catch((err) => {
  838. error = err.detail;
  839. console.log(err);
  840. return null;
  841. });
  842. if (error) {
  843. throw error;
  844. }
  845. return res;
  846. };
  847. export const archiveAllChats = async (token: string) => {
  848. let error = null;
  849. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/archive/all`, {
  850. method: 'POST',
  851. headers: {
  852. Accept: 'application/json',
  853. 'Content-Type': 'application/json',
  854. ...(token && { authorization: `Bearer ${token}` })
  855. }
  856. })
  857. .then(async (res) => {
  858. if (!res.ok) throw await res.json();
  859. return res.json();
  860. })
  861. .then((json) => {
  862. return json;
  863. })
  864. .catch((err) => {
  865. error = err.detail;
  866. console.log(err);
  867. return null;
  868. });
  869. if (error) {
  870. throw error;
  871. }
  872. return res;
  873. };