index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const createNewDoc = async (
  3. token: string,
  4. collection_name: string,
  5. filename: string,
  6. name: string,
  7. title: string,
  8. content: object | null = null
  9. ) => {
  10. let error = null;
  11. const res = await fetch(`${WEBUI_API_BASE_URL}/documents/create`, {
  12. method: 'POST',
  13. headers: {
  14. Accept: 'application/json',
  15. 'Content-Type': 'application/json',
  16. authorization: `Bearer ${token}`
  17. },
  18. body: JSON.stringify({
  19. collection_name: collection_name,
  20. filename: filename,
  21. name: name,
  22. title: title,
  23. ...(content ? { content: JSON.stringify(content) } : {})
  24. })
  25. })
  26. .then(async (res) => {
  27. if (!res.ok) throw await res.json();
  28. return res.json();
  29. })
  30. .catch((err) => {
  31. error = err.detail;
  32. console.log(err);
  33. return null;
  34. });
  35. if (error) {
  36. throw error;
  37. }
  38. return res;
  39. };
  40. export const getDocs = async (token: string = '') => {
  41. let error = null;
  42. const res = await fetch(`${WEBUI_API_BASE_URL}/documents/`, {
  43. method: 'GET',
  44. headers: {
  45. Accept: 'application/json',
  46. 'Content-Type': 'application/json',
  47. authorization: `Bearer ${token}`
  48. }
  49. })
  50. .then(async (res) => {
  51. if (!res.ok) throw await res.json();
  52. return res.json();
  53. })
  54. .then((json) => {
  55. return json;
  56. })
  57. .catch((err) => {
  58. error = err.detail;
  59. console.log(err);
  60. return null;
  61. });
  62. if (error) {
  63. throw error;
  64. }
  65. return res;
  66. };
  67. export const getDocByName = async (token: string, name: string) => {
  68. let error = null;
  69. const res = await fetch(`${WEBUI_API_BASE_URL}/documents/name/${name}`, {
  70. method: 'GET',
  71. headers: {
  72. Accept: 'application/json',
  73. 'Content-Type': 'application/json',
  74. authorization: `Bearer ${token}`
  75. }
  76. })
  77. .then(async (res) => {
  78. if (!res.ok) throw await res.json();
  79. return res.json();
  80. })
  81. .then((json) => {
  82. return json;
  83. })
  84. .catch((err) => {
  85. error = err.detail;
  86. console.log(err);
  87. return null;
  88. });
  89. if (error) {
  90. throw error;
  91. }
  92. return res;
  93. };
  94. type DocUpdateForm = {
  95. name: string;
  96. title: string;
  97. };
  98. export const updateDocByName = async (token: string, name: string, form: DocUpdateForm) => {
  99. let error = null;
  100. const res = await fetch(`${WEBUI_API_BASE_URL}/documents/name/${name}/update`, {
  101. method: 'POST',
  102. headers: {
  103. Accept: 'application/json',
  104. 'Content-Type': 'application/json',
  105. authorization: `Bearer ${token}`
  106. },
  107. body: JSON.stringify({
  108. name: form.name,
  109. title: form.title
  110. })
  111. })
  112. .then(async (res) => {
  113. if (!res.ok) throw await res.json();
  114. return res.json();
  115. })
  116. .then((json) => {
  117. return json;
  118. })
  119. .catch((err) => {
  120. error = err.detail;
  121. console.log(err);
  122. return null;
  123. });
  124. if (error) {
  125. throw error;
  126. }
  127. return res;
  128. };
  129. type TagDocForm = {
  130. name: string;
  131. tags: string[];
  132. };
  133. export const tagDocByName = async (token: string, name: string, form: TagDocForm) => {
  134. let error = null;
  135. const res = await fetch(`${WEBUI_API_BASE_URL}/documents/name/${name}/tags`, {
  136. method: 'POST',
  137. headers: {
  138. Accept: 'application/json',
  139. 'Content-Type': 'application/json',
  140. authorization: `Bearer ${token}`
  141. },
  142. body: JSON.stringify({
  143. name: form.name,
  144. tags: form.tags
  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.detail;
  156. console.log(err);
  157. return null;
  158. });
  159. if (error) {
  160. throw error;
  161. }
  162. return res;
  163. };
  164. export const deleteDocByName = async (token: string, name: string) => {
  165. let error = null;
  166. const res = await fetch(`${WEBUI_API_BASE_URL}/documents/name/${name}/delete`, {
  167. method: 'DELETE',
  168. headers: {
  169. Accept: 'application/json',
  170. 'Content-Type': 'application/json',
  171. 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.detail;
  183. console.log(err);
  184. return null;
  185. });
  186. if (error) {
  187. throw error;
  188. }
  189. return res;
  190. };