index.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const createNewChat = async (token: string, chat: object) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/new`, {
  5. method: 'POST',
  6. headers: {
  7. Accept: 'application/json',
  8. 'Content-Type': 'application/json',
  9. authorization: `Bearer ${token}`
  10. },
  11. body: JSON.stringify({
  12. chat: chat
  13. })
  14. })
  15. .then(async (res) => {
  16. if (!res.ok) throw await res.json();
  17. return res.json();
  18. })
  19. .catch((err) => {
  20. error = err;
  21. console.log(err);
  22. return null;
  23. });
  24. if (error) {
  25. throw error;
  26. }
  27. return res;
  28. };
  29. export const getChatList = async (token: string = '') => {
  30. let error = null;
  31. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/`, {
  32. method: 'GET',
  33. headers: {
  34. Accept: 'application/json',
  35. 'Content-Type': 'application/json',
  36. ...(token && { authorization: `Bearer ${token}` })
  37. }
  38. })
  39. .then(async (res) => {
  40. if (!res.ok) throw await res.json();
  41. return res.json();
  42. })
  43. .then((json) => {
  44. return json;
  45. })
  46. .catch((err) => {
  47. error = err;
  48. console.log(err);
  49. return null;
  50. });
  51. if (error) {
  52. throw error;
  53. }
  54. return res;
  55. };
  56. export const getAllChats = async (token: string) => {
  57. let error = null;
  58. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all`, {
  59. method: 'GET',
  60. headers: {
  61. Accept: 'application/json',
  62. 'Content-Type': 'application/json',
  63. ...(token && { authorization: `Bearer ${token}` })
  64. }
  65. })
  66. .then(async (res) => {
  67. if (!res.ok) throw await res.json();
  68. return res.json();
  69. })
  70. .then((json) => {
  71. return json;
  72. })
  73. .catch((err) => {
  74. error = err;
  75. console.log(err);
  76. return null;
  77. });
  78. if (error) {
  79. throw error;
  80. }
  81. return res;
  82. };
  83. export const getAllUserChats = async (token: string) => {
  84. let error = null;
  85. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all/db`, {
  86. method: 'GET',
  87. headers: {
  88. Accept: 'application/json',
  89. 'Content-Type': 'application/json',
  90. ...(token && { authorization: `Bearer ${token}` })
  91. }
  92. })
  93. .then(async (res) => {
  94. if (!res.ok) throw await res.json();
  95. return res.json();
  96. })
  97. .then((json) => {
  98. return json;
  99. })
  100. .catch((err) => {
  101. error = err;
  102. console.log(err);
  103. return null;
  104. });
  105. if (error) {
  106. throw error;
  107. }
  108. return res;
  109. };
  110. export const getAllChatTags = async (token: string) => {
  111. let error = null;
  112. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/tags/all`, {
  113. method: 'GET',
  114. headers: {
  115. Accept: 'application/json',
  116. 'Content-Type': 'application/json',
  117. ...(token && { authorization: `Bearer ${token}` })
  118. }
  119. })
  120. .then(async (res) => {
  121. if (!res.ok) throw await res.json();
  122. return res.json();
  123. })
  124. .then((json) => {
  125. return json;
  126. })
  127. .catch((err) => {
  128. error = err;
  129. console.log(err);
  130. return null;
  131. });
  132. if (error) {
  133. throw error;
  134. }
  135. return res;
  136. };
  137. export const getChatListByTagName = async (token: string = '', tagName: string) => {
  138. let error = null;
  139. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/tags/tag/${tagName}`, {
  140. method: 'GET',
  141. headers: {
  142. Accept: 'application/json',
  143. 'Content-Type': 'application/json',
  144. ...(token && { authorization: `Bearer ${token}` })
  145. }
  146. })
  147. .then(async (res) => {
  148. if (!res.ok) throw await res.json();
  149. return res.json();
  150. })
  151. .then((json) => {
  152. return json;
  153. })
  154. .catch((err) => {
  155. error = err;
  156. console.log(err);
  157. return null;
  158. });
  159. if (error) {
  160. throw error;
  161. }
  162. return res;
  163. };
  164. export const getChatById = async (token: string, id: string) => {
  165. let error = null;
  166. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  167. method: 'GET',
  168. headers: {
  169. Accept: 'application/json',
  170. 'Content-Type': 'application/json',
  171. ...(token && { authorization: `Bearer ${token}` })
  172. }
  173. })
  174. .then(async (res) => {
  175. if (!res.ok) throw await res.json();
  176. return res.json();
  177. })
  178. .then((json) => {
  179. return json;
  180. })
  181. .catch((err) => {
  182. error = err;
  183. console.log(err);
  184. return null;
  185. });
  186. if (error) {
  187. throw error;
  188. }
  189. return res;
  190. };
  191. export const getChatByShareId = async (token: string, share_id: string) => {
  192. let error = null;
  193. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/share/${share_id}`, {
  194. method: 'GET',
  195. headers: {
  196. Accept: 'application/json',
  197. 'Content-Type': 'application/json',
  198. ...(token && { authorization: `Bearer ${token}` })
  199. }
  200. })
  201. .then(async (res) => {
  202. if (!res.ok) throw await res.json();
  203. return res.json();
  204. })
  205. .then((json) => {
  206. return json;
  207. })
  208. .catch((err) => {
  209. error = err;
  210. console.log(err);
  211. return null;
  212. });
  213. if (error) {
  214. throw error;
  215. }
  216. return res;
  217. };
  218. export const shareChatById = async (token: string, id: string) => {
  219. let error = null;
  220. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/share`, {
  221. method: 'POST',
  222. headers: {
  223. Accept: 'application/json',
  224. 'Content-Type': 'application/json',
  225. ...(token && { authorization: `Bearer ${token}` })
  226. }
  227. })
  228. .then(async (res) => {
  229. if (!res.ok) throw await res.json();
  230. return res.json();
  231. })
  232. .then((json) => {
  233. return json;
  234. })
  235. .catch((err) => {
  236. error = err;
  237. console.log(err);
  238. return null;
  239. });
  240. if (error) {
  241. throw error;
  242. }
  243. return res;
  244. };
  245. export const deleteSharedChatById = async (token: string, id: string) => {
  246. let error = null;
  247. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  248. method: 'DELETE',
  249. headers: {
  250. Accept: 'application/json',
  251. 'Content-Type': 'application/json',
  252. ...(token && { authorization: `Bearer ${token}` })
  253. }
  254. })
  255. .then(async (res) => {
  256. if (!res.ok) throw await res.json();
  257. return res.json();
  258. })
  259. .then((json) => {
  260. return json;
  261. })
  262. .catch((err) => {
  263. error = err;
  264. console.log(err);
  265. return null;
  266. });
  267. if (error) {
  268. throw error;
  269. }
  270. return res;
  271. };
  272. export const updateChatById = async (token: string, id: string, chat: object) => {
  273. let error = null;
  274. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  275. method: 'POST',
  276. headers: {
  277. Accept: 'application/json',
  278. 'Content-Type': 'application/json',
  279. ...(token && { authorization: `Bearer ${token}` })
  280. },
  281. body: JSON.stringify({
  282. chat: chat
  283. })
  284. })
  285. .then(async (res) => {
  286. if (!res.ok) throw await res.json();
  287. return res.json();
  288. })
  289. .then((json) => {
  290. return json;
  291. })
  292. .catch((err) => {
  293. error = err;
  294. console.log(err);
  295. return null;
  296. });
  297. if (error) {
  298. throw error;
  299. }
  300. return res;
  301. };
  302. export const deleteChatById = async (token: string, id: string) => {
  303. let error = null;
  304. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}`, {
  305. method: 'DELETE',
  306. headers: {
  307. Accept: 'application/json',
  308. 'Content-Type': 'application/json',
  309. ...(token && { authorization: `Bearer ${token}` })
  310. }
  311. })
  312. .then(async (res) => {
  313. if (!res.ok) throw await res.json();
  314. return res.json();
  315. })
  316. .then((json) => {
  317. return json;
  318. })
  319. .catch((err) => {
  320. error = err.detail;
  321. console.log(err);
  322. return null;
  323. });
  324. if (error) {
  325. throw error;
  326. }
  327. return res;
  328. };
  329. export const getTagsById = async (token: string, id: string) => {
  330. let error = null;
  331. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  332. method: 'GET',
  333. headers: {
  334. Accept: 'application/json',
  335. 'Content-Type': 'application/json',
  336. ...(token && { authorization: `Bearer ${token}` })
  337. }
  338. })
  339. .then(async (res) => {
  340. if (!res.ok) throw await res.json();
  341. return res.json();
  342. })
  343. .then((json) => {
  344. return json;
  345. })
  346. .catch((err) => {
  347. error = err;
  348. console.log(err);
  349. return null;
  350. });
  351. if (error) {
  352. throw error;
  353. }
  354. return res;
  355. };
  356. export const addTagById = async (token: string, id: string, tagName: string) => {
  357. let error = null;
  358. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  359. method: 'POST',
  360. headers: {
  361. Accept: 'application/json',
  362. 'Content-Type': 'application/json',
  363. ...(token && { authorization: `Bearer ${token}` })
  364. },
  365. body: JSON.stringify({
  366. tag_name: tagName,
  367. chat_id: id
  368. })
  369. })
  370. .then(async (res) => {
  371. if (!res.ok) throw await res.json();
  372. return res.json();
  373. })
  374. .then((json) => {
  375. return json;
  376. })
  377. .catch((err) => {
  378. error = err;
  379. console.log(err);
  380. return null;
  381. });
  382. if (error) {
  383. throw error;
  384. }
  385. return res;
  386. };
  387. export const deleteTagById = async (token: string, id: string, tagName: string) => {
  388. let error = null;
  389. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags`, {
  390. method: 'DELETE',
  391. headers: {
  392. Accept: 'application/json',
  393. 'Content-Type': 'application/json',
  394. ...(token && { authorization: `Bearer ${token}` })
  395. },
  396. body: JSON.stringify({
  397. tag_name: tagName,
  398. chat_id: id
  399. })
  400. })
  401. .then(async (res) => {
  402. if (!res.ok) throw await res.json();
  403. return res.json();
  404. })
  405. .then((json) => {
  406. return json;
  407. })
  408. .catch((err) => {
  409. error = err;
  410. console.log(err);
  411. return null;
  412. });
  413. if (error) {
  414. throw error;
  415. }
  416. return res;
  417. };
  418. export const deleteTagsById = async (token: string, id: string) => {
  419. let error = null;
  420. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${id}/tags/all`, {
  421. method: 'DELETE',
  422. headers: {
  423. Accept: 'application/json',
  424. 'Content-Type': 'application/json',
  425. ...(token && { authorization: `Bearer ${token}` })
  426. }
  427. })
  428. .then(async (res) => {
  429. if (!res.ok) throw await res.json();
  430. return res.json();
  431. })
  432. .then((json) => {
  433. return json;
  434. })
  435. .catch((err) => {
  436. error = err;
  437. console.log(err);
  438. return null;
  439. });
  440. if (error) {
  441. throw error;
  442. }
  443. return res;
  444. };
  445. export const deleteAllChats = async (token: string) => {
  446. let error = null;
  447. const res = await fetch(`${WEBUI_API_BASE_URL}/chats/`, {
  448. method: 'DELETE',
  449. headers: {
  450. Accept: 'application/json',
  451. 'Content-Type': 'application/json',
  452. ...(token && { authorization: `Bearer ${token}` })
  453. }
  454. })
  455. .then(async (res) => {
  456. if (!res.ok) throw await res.json();
  457. return res.json();
  458. })
  459. .then((json) => {
  460. return json;
  461. })
  462. .catch((err) => {
  463. error = err.detail;
  464. console.log(err);
  465. return null;
  466. });
  467. if (error) {
  468. throw error;
  469. }
  470. return res;
  471. };