index.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  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 getChatList = async (token: string = '', skip: number = 0, limit: number = -1) => {
  31. let error = null;
  32. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/?skip=${skip}&limit=${limit}`, {
  33. method: 'GET',
  34. headers: {
  35. Accept: 'application/json',
  36. 'Content-Type': 'application/json',
  37. ...(token && { 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;
  49. console.log(err);
  50. return null;
  51. });
  52. if (error) {
  53. throw error;
  54. }
  55. return res.map((chat) => ({
  56. ...chat,
  57. time_range: getTimeRange(chat.updated_at)
  58. }));
  59. };
  60. export const getChatListByUserId = async (token: string = '', userId: string) => {
  61. let error = null;
  62. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/list/user/${userId}`, {
  63. method: 'GET',
  64. headers: {
  65. Accept: 'application/json',
  66. 'Content-Type': 'application/json',
  67. ...(token && { authorization: `Bearer ${token}` })
  68. }
  69. })
  70. .then(async (res) => {
  71. if (!res.ok) throw await res.json();
  72. return res.json();
  73. })
  74. .then((json) => {
  75. return json;
  76. })
  77. .catch((err) => {
  78. error = err;
  79. console.log(err);
  80. return null;
  81. });
  82. if (error) {
  83. throw error;
  84. }
  85. return res.map((chat) => ({
  86. ...chat,
  87. time_range: getTimeRange(chat.updated_at)
  88. }));
  89. };
  90. export const getArchivedChatList = async (token: string = '') => {
  91. let error = null;
  92. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/archived`, {
  93. method: 'GET',
  94. headers: {
  95. Accept: 'application/json',
  96. 'Content-Type': 'application/json',
  97. ...(token && { authorization: `Bearer ${token}` })
  98. }
  99. })
  100. .then(async (res) => {
  101. if (!res.ok) throw await res.json();
  102. return res.json();
  103. })
  104. .then((json) => {
  105. return json;
  106. })
  107. .catch((err) => {
  108. error = err;
  109. console.log(err);
  110. return null;
  111. });
  112. if (error) {
  113. throw error;
  114. }
  115. return res;
  116. };
  117. export const getAllChats = async (token: string) => {
  118. let error = null;
  119. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all`, {
  120. method: 'GET',
  121. headers: {
  122. Accept: 'application/json',
  123. 'Content-Type': 'application/json',
  124. ...(token && { authorization: `Bearer ${token}` })
  125. }
  126. })
  127. .then(async (res) => {
  128. if (!res.ok) throw await res.json();
  129. return res.json();
  130. })
  131. .then((json) => {
  132. return json;
  133. })
  134. .catch((err) => {
  135. error = err;
  136. console.log(err);
  137. return null;
  138. });
  139. if (error) {
  140. throw error;
  141. }
  142. return res;
  143. };
  144. export const getAllArchivedChats = async (token: string) => {
  145. let error = null;
  146. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all/archived`, {
  147. method: 'GET',
  148. headers: {
  149. Accept: 'application/json',
  150. 'Content-Type': 'application/json',
  151. ...(token && { authorization: `Bearer ${token}` })
  152. }
  153. })
  154. .then(async (res) => {
  155. if (!res.ok) throw await res.json();
  156. return res.json();
  157. })
  158. .then((json) => {
  159. return json;
  160. })
  161. .catch((err) => {
  162. error = err;
  163. console.log(err);
  164. return null;
  165. });
  166. if (error) {
  167. throw error;
  168. }
  169. return res;
  170. };
  171. export const getAllUserChats = async (token: string) => {
  172. let error = null;
  173. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all/db`, {
  174. method: 'GET',
  175. headers: {
  176. Accept: 'application/json',
  177. 'Content-Type': 'application/json',
  178. ...(token && { authorization: `Bearer ${token}` })
  179. }
  180. })
  181. .then(async (res) => {
  182. if (!res.ok) throw await res.json();
  183. return res.json();
  184. })
  185. .then((json) => {
  186. return json;
  187. })
  188. .catch((err) => {
  189. error = err;
  190. console.log(err);
  191. return null;
  192. });
  193. if (error) {
  194. throw error;
  195. }
  196. return res;
  197. };
  198. export const getAllChatTags = async (token: string) => {
  199. let error = null;
  200. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/tags/all`, {
  201. method: 'GET',
  202. headers: {
  203. Accept: 'application/json',
  204. 'Content-Type': 'application/json',
  205. ...(token && { authorization: `Bearer ${token}` })
  206. }
  207. })
  208. .then(async (res) => {
  209. if (!res.ok) throw await res.json();
  210. return res.json();
  211. })
  212. .then((json) => {
  213. return json;
  214. })
  215. .catch((err) => {
  216. error = err;
  217. console.log(err);
  218. return null;
  219. });
  220. if (error) {
  221. throw error;
  222. }
  223. return res;
  224. };
  225. export const getChatListByTagName = async (token: string = '', tagName: string) => {
  226. let error = null;
  227. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/tags`, {
  228. method: 'POST',
  229. headers: {
  230. Accept: 'application/json',
  231. 'Content-Type': 'application/json',
  232. ...(token && { authorization: `Bearer ${token}` })
  233. },
  234. body: JSON.stringify({
  235. name: tagName
  236. })
  237. })
  238. .then(async (res) => {
  239. if (!res.ok) throw await res.json();
  240. return res.json();
  241. })
  242. .then((json) => {
  243. return json;
  244. })
  245. .catch((err) => {
  246. error = err;
  247. console.log(err);
  248. return null;
  249. });
  250. if (error) {
  251. throw error;
  252. }
  253. return res.map((chat) => ({
  254. ...chat,
  255. time_range: getTimeRange(chat.updated_at)
  256. }));
  257. };
  258. export const getChatById = async (token: string, id: string) => {
  259. let error = null;
  260. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  261. method: 'GET',
  262. headers: {
  263. Accept: 'application/json',
  264. 'Content-Type': 'application/json',
  265. ...(token && { authorization: `Bearer ${token}` })
  266. }
  267. })
  268. .then(async (res) => {
  269. if (!res.ok) throw await res.json();
  270. return res.json();
  271. })
  272. .then((json) => {
  273. return json;
  274. })
  275. .catch((err) => {
  276. error = err;
  277. console.log(err);
  278. return null;
  279. });
  280. if (error) {
  281. throw error;
  282. }
  283. return res;
  284. };
  285. export const getChatByShareId = async (token: string, share_id: string) => {
  286. let error = null;
  287. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/share/${share_id}`, {
  288. method: 'GET',
  289. headers: {
  290. Accept: 'application/json',
  291. 'Content-Type': 'application/json',
  292. ...(token && { authorization: `Bearer ${token}` })
  293. }
  294. })
  295. .then(async (res) => {
  296. if (!res.ok) throw await res.json();
  297. return res.json();
  298. })
  299. .then((json) => {
  300. return json;
  301. })
  302. .catch((err) => {
  303. error = err;
  304. console.log(err);
  305. return null;
  306. });
  307. if (error) {
  308. throw error;
  309. }
  310. return res;
  311. };
  312. export const cloneChatById = async (token: string, id: string) => {
  313. let error = null;
  314. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/clone`, {
  315. method: 'GET',
  316. headers: {
  317. Accept: 'application/json',
  318. 'Content-Type': 'application/json',
  319. ...(token && { authorization: `Bearer ${token}` })
  320. }
  321. })
  322. .then(async (res) => {
  323. if (!res.ok) throw await res.json();
  324. return res.json();
  325. })
  326. .then((json) => {
  327. return json;
  328. })
  329. .catch((err) => {
  330. error = err;
  331. if ('detail' in err) {
  332. error = err.detail;
  333. } else {
  334. error = err;
  335. }
  336. console.log(err);
  337. return null;
  338. });
  339. if (error) {
  340. throw error;
  341. }
  342. return res;
  343. };
  344. export const shareChatById = async (token: string, id: string) => {
  345. let error = null;
  346. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/share`, {
  347. method: 'POST',
  348. headers: {
  349. Accept: 'application/json',
  350. 'Content-Type': 'application/json',
  351. ...(token && { authorization: `Bearer ${token}` })
  352. }
  353. })
  354. .then(async (res) => {
  355. if (!res.ok) throw await res.json();
  356. return res.json();
  357. })
  358. .then((json) => {
  359. return json;
  360. })
  361. .catch((err) => {
  362. error = err;
  363. console.log(err);
  364. return null;
  365. });
  366. if (error) {
  367. throw error;
  368. }
  369. return res;
  370. };
  371. export const archiveChatById = async (token: string, id: string) => {
  372. let error = null;
  373. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/archive`, {
  374. method: 'GET',
  375. headers: {
  376. Accept: 'application/json',
  377. 'Content-Type': 'application/json',
  378. ...(token && { authorization: `Bearer ${token}` })
  379. }
  380. })
  381. .then(async (res) => {
  382. if (!res.ok) throw await res.json();
  383. return res.json();
  384. })
  385. .then((json) => {
  386. return json;
  387. })
  388. .catch((err) => {
  389. error = err;
  390. console.log(err);
  391. return null;
  392. });
  393. if (error) {
  394. throw error;
  395. }
  396. return res;
  397. };
  398. export const deleteSharedChatById = async (token: string, id: string) => {
  399. let error = null;
  400. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/share`, {
  401. method: 'DELETE',
  402. headers: {
  403. Accept: 'application/json',
  404. 'Content-Type': 'application/json',
  405. ...(token && { authorization: `Bearer ${token}` })
  406. }
  407. })
  408. .then(async (res) => {
  409. if (!res.ok) throw await res.json();
  410. return res.json();
  411. })
  412. .then((json) => {
  413. return json;
  414. })
  415. .catch((err) => {
  416. error = err;
  417. console.log(err);
  418. return null;
  419. });
  420. if (error) {
  421. throw error;
  422. }
  423. return res;
  424. };
  425. export const updateChatById = async (token: string, id: string, chat: object) => {
  426. let error = null;
  427. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  428. method: 'POST',
  429. headers: {
  430. Accept: 'application/json',
  431. 'Content-Type': 'application/json',
  432. ...(token && { authorization: `Bearer ${token}` })
  433. },
  434. body: JSON.stringify({
  435. chat: chat
  436. })
  437. })
  438. .then(async (res) => {
  439. if (!res.ok) throw await res.json();
  440. return res.json();
  441. })
  442. .then((json) => {
  443. return json;
  444. })
  445. .catch((err) => {
  446. error = err;
  447. console.log(err);
  448. return null;
  449. });
  450. if (error) {
  451. throw error;
  452. }
  453. return res;
  454. };
  455. export const deleteChatById = async (token: string, id: string) => {
  456. let error = null;
  457. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  458. method: 'DELETE',
  459. headers: {
  460. Accept: 'application/json',
  461. 'Content-Type': 'application/json',
  462. ...(token && { authorization: `Bearer ${token}` })
  463. }
  464. })
  465. .then(async (res) => {
  466. if (!res.ok) throw await res.json();
  467. return res.json();
  468. })
  469. .then((json) => {
  470. return json;
  471. })
  472. .catch((err) => {
  473. error = err.detail;
  474. console.log(err);
  475. return null;
  476. });
  477. if (error) {
  478. throw error;
  479. }
  480. return res;
  481. };
  482. export const getTagsById = async (token: string, id: string) => {
  483. let error = null;
  484. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  485. method: 'GET',
  486. headers: {
  487. Accept: 'application/json',
  488. 'Content-Type': 'application/json',
  489. ...(token && { authorization: `Bearer ${token}` })
  490. }
  491. })
  492. .then(async (res) => {
  493. if (!res.ok) throw await res.json();
  494. return res.json();
  495. })
  496. .then((json) => {
  497. return json;
  498. })
  499. .catch((err) => {
  500. error = err;
  501. console.log(err);
  502. return null;
  503. });
  504. if (error) {
  505. throw error;
  506. }
  507. return res;
  508. };
  509. export const addTagById = async (token: string, id: string, tagName: string) => {
  510. let error = null;
  511. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  512. method: 'POST',
  513. headers: {
  514. Accept: 'application/json',
  515. 'Content-Type': 'application/json',
  516. ...(token && { authorization: `Bearer ${token}` })
  517. },
  518. body: JSON.stringify({
  519. tag_name: tagName,
  520. chat_id: id
  521. })
  522. })
  523. .then(async (res) => {
  524. if (!res.ok) throw await res.json();
  525. return res.json();
  526. })
  527. .then((json) => {
  528. return json;
  529. })
  530. .catch((err) => {
  531. error = err;
  532. console.log(err);
  533. return null;
  534. });
  535. if (error) {
  536. throw error;
  537. }
  538. return res;
  539. };
  540. export const deleteTagById = async (token: string, id: string, tagName: string) => {
  541. let error = null;
  542. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  543. method: 'DELETE',
  544. headers: {
  545. Accept: 'application/json',
  546. 'Content-Type': 'application/json',
  547. ...(token && { authorization: `Bearer ${token}` })
  548. },
  549. body: JSON.stringify({
  550. tag_name: tagName,
  551. chat_id: id
  552. })
  553. })
  554. .then(async (res) => {
  555. if (!res.ok) throw await res.json();
  556. return res.json();
  557. })
  558. .then((json) => {
  559. return json;
  560. })
  561. .catch((err) => {
  562. error = err;
  563. console.log(err);
  564. return null;
  565. });
  566. if (error) {
  567. throw error;
  568. }
  569. return res;
  570. };
  571. export const deleteTagsById = async (token: string, id: string) => {
  572. let error = null;
  573. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags/all`, {
  574. method: 'DELETE',
  575. headers: {
  576. Accept: 'application/json',
  577. 'Content-Type': 'application/json',
  578. ...(token && { authorization: `Bearer ${token}` })
  579. }
  580. })
  581. .then(async (res) => {
  582. if (!res.ok) throw await res.json();
  583. return res.json();
  584. })
  585. .then((json) => {
  586. return json;
  587. })
  588. .catch((err) => {
  589. error = err;
  590. console.log(err);
  591. return null;
  592. });
  593. if (error) {
  594. throw error;
  595. }
  596. return res;
  597. };
  598. export const deleteAllChats = async (token: string) => {
  599. let error = null;
  600. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/`, {
  601. method: 'DELETE',
  602. headers: {
  603. Accept: 'application/json',
  604. 'Content-Type': 'application/json',
  605. ...(token && { authorization: `Bearer ${token}` })
  606. }
  607. })
  608. .then(async (res) => {
  609. if (!res.ok) throw await res.json();
  610. return res.json();
  611. })
  612. .then((json) => {
  613. return json;
  614. })
  615. .catch((err) => {
  616. error = err.detail;
  617. console.log(err);
  618. return null;
  619. });
  620. if (error) {
  621. throw error;
  622. }
  623. return res;
  624. };
  625. export const archiveAllChats = async (token: string) => {
  626. let error = null;
  627. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/archive/all`, {
  628. method: 'POST',
  629. headers: {
  630. Accept: 'application/json',
  631. 'Content-Type': 'application/json',
  632. ...(token && { authorization: `Bearer ${token}` })
  633. }
  634. })
  635. .then(async (res) => {
  636. if (!res.ok) throw await res.json();
  637. return res.json();
  638. })
  639. .then((json) => {
  640. return json;
  641. })
  642. .catch((err) => {
  643. error = err.detail;
  644. console.log(err);
  645. return null;
  646. });
  647. if (error) {
  648. throw error;
  649. }
  650. return res;
  651. };