index.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const getMemories = async (token: string) => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_API_BASE_URL}/memories`, {
  5. method: 'GET',
  6. headers: {
  7. Accept: 'application/json',
  8. 'Content-Type': 'application/json',
  9. authorization: `Bearer ${token}`
  10. }
  11. })
  12. .then(async (res) => {
  13. if (!res.ok) throw await res.json();
  14. return res.json();
  15. })
  16. .catch((err) => {
  17. error = err.detail;
  18. console.log(err);
  19. return null;
  20. });
  21. if (error) {
  22. throw error;
  23. }
  24. return res;
  25. };
  26. export const addNewMemory = async (token: string, content: string) => {
  27. let error = null;
  28. const res = await fetch(`${WEBUI_API_BASE_URL}/memories/add`, {
  29. method: 'POST',
  30. headers: {
  31. Accept: 'application/json',
  32. 'Content-Type': 'application/json',
  33. authorization: `Bearer ${token}`
  34. },
  35. body: JSON.stringify({
  36. content: content
  37. })
  38. })
  39. .then(async (res) => {
  40. if (!res.ok) throw await res.json();
  41. return res.json();
  42. })
  43. .catch((err) => {
  44. error = err.detail;
  45. console.log(err);
  46. return null;
  47. });
  48. if (error) {
  49. throw error;
  50. }
  51. return res;
  52. };
  53. export const queryMemory = async (token: string, content: string) => {
  54. let error = null;
  55. const res = await fetch(`${WEBUI_API_BASE_URL}/memories/query`, {
  56. method: 'POST',
  57. headers: {
  58. Accept: 'application/json',
  59. 'Content-Type': 'application/json',
  60. authorization: `Bearer ${token}`
  61. },
  62. body: JSON.stringify({
  63. content: content
  64. })
  65. })
  66. .then(async (res) => {
  67. if (!res.ok) throw await res.json();
  68. return res.json();
  69. })
  70. .catch((err) => {
  71. error = err.detail;
  72. console.log(err);
  73. return null;
  74. });
  75. if (error) {
  76. throw error;
  77. }
  78. return res;
  79. };
  80. export const deleteMemoryById = async (token: string, id: string) => {
  81. let error = null;
  82. const res = await fetch(`${WEBUI_API_BASE_URL}/memories/${id}`, {
  83. method: 'DELETE',
  84. headers: {
  85. Accept: 'application/json',
  86. 'Content-Type': 'application/json',
  87. authorization: `Bearer ${token}`
  88. }
  89. })
  90. .then(async (res) => {
  91. if (!res.ok) throw await res.json();
  92. return res.json();
  93. })
  94. .then((json) => {
  95. return json;
  96. })
  97. .catch((err) => {
  98. error = err.detail;
  99. console.log(err);
  100. return null;
  101. });
  102. if (error) {
  103. throw error;
  104. }
  105. return res;
  106. };
  107. export const deleteMemoriesByUserId = async (token: string) => {
  108. let error = null;
  109. const res = await fetch(`${WEBUI_API_BASE_URL}/memories/user`, {
  110. method: 'DELETE',
  111. headers: {
  112. Accept: 'application/json',
  113. 'Content-Type': 'application/json',
  114. authorization: `Bearer ${token}`
  115. }
  116. })
  117. .then(async (res) => {
  118. if (!res.ok) throw await res.json();
  119. return res.json();
  120. })
  121. .then((json) => {
  122. return json;
  123. })
  124. .catch((err) => {
  125. error = err.detail;
  126. console.log(err);
  127. return null;
  128. });
  129. if (error) {
  130. throw error;
  131. }
  132. return res;
  133. };