index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { WEBUI_API_BASE_URL } from '$lib/constants';
  2. type PromptItem = {
  3. command: string;
  4. title: string;
  5. content: string;
  6. access_control: null | object;
  7. };
  8. export const createNewPrompt = async (token: string, prompt: PromptItem) => {
  9. let error = null;
  10. const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/create`, {
  11. method: 'POST',
  12. headers: {
  13. Accept: 'application/json',
  14. 'Content-Type': 'application/json',
  15. authorization: `Bearer ${token}`
  16. },
  17. body: JSON.stringify({
  18. ...prompt,
  19. command: `/${prompt.command}`
  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 getPromptList = async (token: string = '') => {
  64. let error = null;
  65. const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/list`, {
  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 getPromptByCommand = async (token: string, command: string) => {
  91. let error = null;
  92. const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/command/${command}`, {
  93. method: 'GET',
  94. headers: {
  95. Accept: 'application/json',
  96. 'Content-Type': 'application/json',
  97. 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.detail;
  109. console.log(err);
  110. return null;
  111. });
  112. if (error) {
  113. throw error;
  114. }
  115. return res;
  116. };
  117. export const updatePromptByCommand = async (token: string, prompt: PromptItem) => {
  118. let error = null;
  119. const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/command/${prompt.command}/update`, {
  120. method: 'POST',
  121. headers: {
  122. Accept: 'application/json',
  123. 'Content-Type': 'application/json',
  124. authorization: `Bearer ${token}`
  125. },
  126. body: JSON.stringify({
  127. ...prompt,
  128. command: `/${prompt.command}`
  129. })
  130. })
  131. .then(async (res) => {
  132. if (!res.ok) throw await res.json();
  133. return res.json();
  134. })
  135. .then((json) => {
  136. return json;
  137. })
  138. .catch((err) => {
  139. error = err.detail;
  140. console.log(err);
  141. return null;
  142. });
  143. if (error) {
  144. throw error;
  145. }
  146. return res;
  147. };
  148. export const deletePromptByCommand = async (token: string, command: string) => {
  149. let error = null;
  150. command = command.charAt(0) === '/' ? command.slice(1) : command;
  151. const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/command/${command}/delete`, {
  152. method: 'DELETE',
  153. headers: {
  154. Accept: 'application/json',
  155. 'Content-Type': 'application/json',
  156. authorization: `Bearer ${token}`
  157. }
  158. })
  159. .then(async (res) => {
  160. if (!res.ok) throw await res.json();
  161. return res.json();
  162. })
  163. .then((json) => {
  164. return json;
  165. })
  166. .catch((err) => {
  167. error = err.detail;
  168. console.log(err);
  169. return null;
  170. });
  171. if (error) {
  172. throw error;
  173. }
  174. return res;
  175. };