index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. export const createNewPrompt = async (
  3. token: string,
  4. command: string,
  5. title: string,
  6. content: string
  7. ) => {
  8. let error = null;
  9. const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/create`, {
  10. method: 'POST',
  11. headers: {
  12. Accept: 'application/json',
  13. 'Content-Type': 'application/json',
  14. authorization: `Bearer ${token}`
  15. },
  16. body: JSON.stringify({
  17. command: `/${command}`,
  18. title: title,
  19. content: content
  20. })
  21. })
  22. .then(async (res) => {
  23. if (!res.ok) throw await res.json();
  24. return res.json();
  25. })
  26. .catch((err) => {
  27. error = err.detail;
  28. console.log(err);
  29. return null;
  30. });
  31. if (error) {
  32. throw error;
  33. }
  34. return res;
  35. };
  36. export const getPrompts = async (token: string = '') => {
  37. let error = null;
  38. const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/`, {
  39. method: 'GET',
  40. headers: {
  41. Accept: 'application/json',
  42. 'Content-Type': 'application/json',
  43. authorization: `Bearer ${token}`
  44. }
  45. })
  46. .then(async (res) => {
  47. if (!res.ok) throw await res.json();
  48. return res.json();
  49. })
  50. .then((json) => {
  51. return json;
  52. })
  53. .catch((err) => {
  54. error = err.detail;
  55. console.log(err);
  56. return null;
  57. });
  58. if (error) {
  59. throw error;
  60. }
  61. return res;
  62. };
  63. export const getPromptByCommand = async (token: string, command: string) => {
  64. let error = null;
  65. const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/command/${command}`, {
  66. method: 'GET',
  67. headers: {
  68. Accept: 'application/json',
  69. 'Content-Type': 'application/json',
  70. authorization: `Bearer ${token}`
  71. }
  72. })
  73. .then(async (res) => {
  74. if (!res.ok) throw await res.json();
  75. return res.json();
  76. })
  77. .then((json) => {
  78. return json;
  79. })
  80. .catch((err) => {
  81. error = err.detail;
  82. console.log(err);
  83. return null;
  84. });
  85. if (error) {
  86. throw error;
  87. }
  88. return res;
  89. };
  90. export const updatePromptByCommand = async (
  91. token: string,
  92. command: string,
  93. title: string,
  94. content: string
  95. ) => {
  96. let error = null;
  97. const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/command/${command}/update`, {
  98. method: 'POST',
  99. headers: {
  100. Accept: 'application/json',
  101. 'Content-Type': 'application/json',
  102. authorization: `Bearer ${token}`
  103. },
  104. body: JSON.stringify({
  105. command: `/${command}`,
  106. title: title,
  107. content: content
  108. })
  109. })
  110. .then(async (res) => {
  111. if (!res.ok) throw await res.json();
  112. return res.json();
  113. })
  114. .then((json) => {
  115. return json;
  116. })
  117. .catch((err) => {
  118. error = err.detail;
  119. console.log(err);
  120. return null;
  121. });
  122. if (error) {
  123. throw error;
  124. }
  125. return res;
  126. };
  127. export const deletePromptByCommand = async (token: string, command: string) => {
  128. let error = null;
  129. command = command.charAt(0) === '/' ? command.slice(1) : command;
  130. const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/command/${command}/delete`, {
  131. method: 'DELETE',
  132. headers: {
  133. Accept: 'application/json',
  134. 'Content-Type': 'application/json',
  135. authorization: `Bearer ${token}`
  136. }
  137. })
  138. .then(async (res) => {
  139. if (!res.ok) throw await res.json();
  140. return res.json();
  141. })
  142. .then((json) => {
  143. return json;
  144. })
  145. .catch((err) => {
  146. error = err.detail;
  147. console.log(err);
  148. return null;
  149. });
  150. if (error) {
  151. throw error;
  152. }
  153. return res;
  154. };