index.ts 19 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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, title?: 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. body: JSON.stringify({
  516. ...(title && { title: title })
  517. })
  518. })
  519. .then(async (res) => {
  520. if (!res.ok) throw await res.json();
  521. return res.json();
  522. })
  523. .then((json) => {
  524. return json;
  525. })
  526. .catch((err) => {
  527. error = err;
  528. if ('detail' in err) {
  529. error = err.detail;
  530. } else {
  531. error = err;
  532. }
  533. console.log(err);
  534. return null;
  535. });
  536. if (error) {
  537. throw error;
  538. }
  539. return res;
  540. };
  541. export const cloneSharedChatById = async (token: string, id: string) => {
  542. let error = null;
  543. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/clone/shared`, {
  544. method: 'POST',
  545. headers: {
  546. Accept: 'application/json',
  547. 'Content-Type': 'application/json',
  548. ...(token && { authorization: `Bearer ${token}` })
  549. }
  550. })
  551. .then(async (res) => {
  552. if (!res.ok) throw await res.json();
  553. return res.json();
  554. })
  555. .then((json) => {
  556. return json;
  557. })
  558. .catch((err) => {
  559. error = err;
  560. if ('detail' in err) {
  561. error = err.detail;
  562. } else {
  563. error = err;
  564. }
  565. console.log(err);
  566. return null;
  567. });
  568. if (error) {
  569. throw error;
  570. }
  571. return res;
  572. };
  573. export const shareChatById = async (token: string, id: string) => {
  574. let error = null;
  575. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/share`, {
  576. method: 'POST',
  577. headers: {
  578. Accept: 'application/json',
  579. 'Content-Type': 'application/json',
  580. ...(token && { authorization: `Bearer ${token}` })
  581. }
  582. })
  583. .then(async (res) => {
  584. if (!res.ok) throw await res.json();
  585. return res.json();
  586. })
  587. .then((json) => {
  588. return json;
  589. })
  590. .catch((err) => {
  591. error = err;
  592. console.log(err);
  593. return null;
  594. });
  595. if (error) {
  596. throw error;
  597. }
  598. return res;
  599. };
  600. export const updateChatFolderIdById = async (token: string, id: string, folderId?: string) => {
  601. let error = null;
  602. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/folder`, {
  603. method: 'POST',
  604. headers: {
  605. Accept: 'application/json',
  606. 'Content-Type': 'application/json',
  607. ...(token && { authorization: `Bearer ${token}` })
  608. },
  609. body: JSON.stringify({
  610. folder_id: folderId
  611. })
  612. })
  613. .then(async (res) => {
  614. if (!res.ok) throw await res.json();
  615. return res.json();
  616. })
  617. .then((json) => {
  618. return json;
  619. })
  620. .catch((err) => {
  621. error = err;
  622. console.log(err);
  623. return null;
  624. });
  625. if (error) {
  626. throw error;
  627. }
  628. return res;
  629. };
  630. export const archiveChatById = async (token: string, id: string) => {
  631. let error = null;
  632. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/archive`, {
  633. method: 'POST',
  634. headers: {
  635. Accept: 'application/json',
  636. 'Content-Type': 'application/json',
  637. ...(token && { authorization: `Bearer ${token}` })
  638. }
  639. })
  640. .then(async (res) => {
  641. if (!res.ok) throw await res.json();
  642. return res.json();
  643. })
  644. .then((json) => {
  645. return json;
  646. })
  647. .catch((err) => {
  648. error = err;
  649. console.log(err);
  650. return null;
  651. });
  652. if (error) {
  653. throw error;
  654. }
  655. return res;
  656. };
  657. export const deleteSharedChatById = async (token: string, id: string) => {
  658. let error = null;
  659. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/share`, {
  660. method: 'DELETE',
  661. headers: {
  662. Accept: 'application/json',
  663. 'Content-Type': 'application/json',
  664. ...(token && { authorization: `Bearer ${token}` })
  665. }
  666. })
  667. .then(async (res) => {
  668. if (!res.ok) throw await res.json();
  669. return res.json();
  670. })
  671. .then((json) => {
  672. return json;
  673. })
  674. .catch((err) => {
  675. error = err;
  676. console.log(err);
  677. return null;
  678. });
  679. if (error) {
  680. throw error;
  681. }
  682. return res;
  683. };
  684. export const updateChatById = async (token: string, id: string, chat: object) => {
  685. let error = null;
  686. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  687. method: 'POST',
  688. headers: {
  689. Accept: 'application/json',
  690. 'Content-Type': 'application/json',
  691. ...(token && { authorization: `Bearer ${token}` })
  692. },
  693. body: JSON.stringify({
  694. chat: chat
  695. })
  696. })
  697. .then(async (res) => {
  698. if (!res.ok) throw await res.json();
  699. return res.json();
  700. })
  701. .then((json) => {
  702. return json;
  703. })
  704. .catch((err) => {
  705. error = err;
  706. console.log(err);
  707. return null;
  708. });
  709. if (error) {
  710. throw error;
  711. }
  712. return res;
  713. };
  714. export const deleteChatById = async (token: string, id: string) => {
  715. let error = null;
  716. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  717. method: 'DELETE',
  718. headers: {
  719. Accept: 'application/json',
  720. 'Content-Type': 'application/json',
  721. ...(token && { authorization: `Bearer ${token}` })
  722. }
  723. })
  724. .then(async (res) => {
  725. if (!res.ok) throw await res.json();
  726. return res.json();
  727. })
  728. .then((json) => {
  729. return json;
  730. })
  731. .catch((err) => {
  732. error = err.detail;
  733. console.log(err);
  734. return null;
  735. });
  736. if (error) {
  737. throw error;
  738. }
  739. return res;
  740. };
  741. export const getTagsById = async (token: string, id: string) => {
  742. let error = null;
  743. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  744. method: 'GET',
  745. headers: {
  746. Accept: 'application/json',
  747. 'Content-Type': 'application/json',
  748. ...(token && { authorization: `Bearer ${token}` })
  749. }
  750. })
  751. .then(async (res) => {
  752. if (!res.ok) throw await res.json();
  753. return res.json();
  754. })
  755. .then((json) => {
  756. return json;
  757. })
  758. .catch((err) => {
  759. error = err;
  760. console.log(err);
  761. return null;
  762. });
  763. if (error) {
  764. throw error;
  765. }
  766. return res;
  767. };
  768. export const addTagById = async (token: string, id: string, tagName: string) => {
  769. let error = null;
  770. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  771. method: 'POST',
  772. headers: {
  773. Accept: 'application/json',
  774. 'Content-Type': 'application/json',
  775. ...(token && { authorization: `Bearer ${token}` })
  776. },
  777. body: JSON.stringify({
  778. name: tagName
  779. })
  780. })
  781. .then(async (res) => {
  782. if (!res.ok) throw await res.json();
  783. return res.json();
  784. })
  785. .then((json) => {
  786. return json;
  787. })
  788. .catch((err) => {
  789. error = err.detail;
  790. console.log(err);
  791. return null;
  792. });
  793. if (error) {
  794. throw error;
  795. }
  796. return res;
  797. };
  798. export const deleteTagById = async (token: string, id: string, tagName: string) => {
  799. let error = null;
  800. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  801. method: 'DELETE',
  802. headers: {
  803. Accept: 'application/json',
  804. 'Content-Type': 'application/json',
  805. ...(token && { authorization: `Bearer ${token}` })
  806. },
  807. body: JSON.stringify({
  808. name: tagName
  809. })
  810. })
  811. .then(async (res) => {
  812. if (!res.ok) throw await res.json();
  813. return res.json();
  814. })
  815. .then((json) => {
  816. return json;
  817. })
  818. .catch((err) => {
  819. error = err;
  820. console.log(err);
  821. return null;
  822. });
  823. if (error) {
  824. throw error;
  825. }
  826. return res;
  827. };
  828. export const deleteTagsById = async (token: string, id: string) => {
  829. let error = null;
  830. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags/all`, {
  831. method: 'DELETE',
  832. headers: {
  833. Accept: 'application/json',
  834. 'Content-Type': 'application/json',
  835. ...(token && { authorization: `Bearer ${token}` })
  836. }
  837. })
  838. .then(async (res) => {
  839. if (!res.ok) throw await res.json();
  840. return res.json();
  841. })
  842. .then((json) => {
  843. return json;
  844. })
  845. .catch((err) => {
  846. error = err;
  847. console.log(err);
  848. return null;
  849. });
  850. if (error) {
  851. throw error;
  852. }
  853. return res;
  854. };
  855. export const deleteAllChats = async (token: string) => {
  856. let error = null;
  857. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/`, {
  858. method: 'DELETE',
  859. headers: {
  860. Accept: 'application/json',
  861. 'Content-Type': 'application/json',
  862. ...(token && { authorization: `Bearer ${token}` })
  863. }
  864. })
  865. .then(async (res) => {
  866. if (!res.ok) throw await res.json();
  867. return res.json();
  868. })
  869. .then((json) => {
  870. return json;
  871. })
  872. .catch((err) => {
  873. error = err.detail;
  874. console.log(err);
  875. return null;
  876. });
  877. if (error) {
  878. throw error;
  879. }
  880. return res;
  881. };
  882. export const archiveAllChats = async (token: string) => {
  883. let error = null;
  884. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/archive/all`, {
  885. method: 'POST',
  886. headers: {
  887. Accept: 'application/json',
  888. 'Content-Type': 'application/json',
  889. ...(token && { authorization: `Bearer ${token}` })
  890. }
  891. })
  892. .then(async (res) => {
  893. if (!res.ok) throw await res.json();
  894. return res.json();
  895. })
  896. .then((json) => {
  897. return json;
  898. })
  899. .catch((err) => {
  900. error = err.detail;
  901. console.log(err);
  902. return null;
  903. });
  904. if (error) {
  905. throw error;
  906. }
  907. return res;
  908. };