index.ts 21 KB

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