index.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import { WEBUI_BASE_URL } from '$lib/constants';
  2. export const getBackendConfig = async () => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_BASE_URL}/api/config`, {
  5. method: 'GET',
  6. headers: {
  7. 'Content-Type': 'application/json'
  8. }
  9. })
  10. .then(async (res) => {
  11. if (!res.ok) throw await res.json();
  12. return res.json();
  13. })
  14. .catch((err) => {
  15. console.log(err);
  16. error = err;
  17. return null;
  18. });
  19. if (error) {
  20. throw error;
  21. }
  22. return res;
  23. };
  24. export const getChangelog = async () => {
  25. let error = null;
  26. const res = await fetch(`${WEBUI_BASE_URL}/api/changelog`, {
  27. method: 'GET',
  28. headers: {
  29. 'Content-Type': 'application/json'
  30. }
  31. })
  32. .then(async (res) => {
  33. if (!res.ok) throw await res.json();
  34. return res.json();
  35. })
  36. .catch((err) => {
  37. console.log(err);
  38. error = err;
  39. return null;
  40. });
  41. if (error) {
  42. throw error;
  43. }
  44. return res;
  45. };
  46. export const getVersionUpdates = async () => {
  47. let error = null;
  48. const res = await fetch(`${WEBUI_BASE_URL}/api/version/updates`, {
  49. method: 'GET',
  50. headers: {
  51. 'Content-Type': 'application/json'
  52. }
  53. })
  54. .then(async (res) => {
  55. if (!res.ok) throw await res.json();
  56. return res.json();
  57. })
  58. .catch((err) => {
  59. console.log(err);
  60. error = err;
  61. return null;
  62. });
  63. if (error) {
  64. throw error;
  65. }
  66. return res;
  67. };
  68. export const getModelFilterConfig = async (token: string) => {
  69. let error = null;
  70. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  71. method: 'GET',
  72. headers: {
  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. .catch((err) => {
  82. console.log(err);
  83. error = err;
  84. return null;
  85. });
  86. if (error) {
  87. throw error;
  88. }
  89. return res;
  90. };
  91. export const updateModelFilterConfig = async (
  92. token: string,
  93. enabled: boolean,
  94. models: string[]
  95. ) => {
  96. let error = null;
  97. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  98. method: 'POST',
  99. headers: {
  100. 'Content-Type': 'application/json',
  101. Authorization: `Bearer ${token}`
  102. },
  103. body: JSON.stringify({
  104. enabled: enabled,
  105. models: models
  106. })
  107. })
  108. .then(async (res) => {
  109. if (!res.ok) throw await res.json();
  110. return res.json();
  111. })
  112. .catch((err) => {
  113. console.log(err);
  114. error = err;
  115. return null;
  116. });
  117. if (error) {
  118. throw error;
  119. }
  120. return res;
  121. };
  122. export const getWebhookUrl = async (token: string) => {
  123. let error = null;
  124. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  125. method: 'GET',
  126. headers: {
  127. 'Content-Type': 'application/json',
  128. Authorization: `Bearer ${token}`
  129. }
  130. })
  131. .then(async (res) => {
  132. if (!res.ok) throw await res.json();
  133. return res.json();
  134. })
  135. .catch((err) => {
  136. console.log(err);
  137. error = err;
  138. return null;
  139. });
  140. if (error) {
  141. throw error;
  142. }
  143. return res.url;
  144. };
  145. export const updateWebhookUrl = async (token: string, url: string) => {
  146. let error = null;
  147. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  148. method: 'POST',
  149. headers: {
  150. 'Content-Type': 'application/json',
  151. Authorization: `Bearer ${token}`
  152. },
  153. body: JSON.stringify({
  154. url: url
  155. })
  156. })
  157. .then(async (res) => {
  158. if (!res.ok) throw await res.json();
  159. return res.json();
  160. })
  161. .catch((err) => {
  162. console.log(err);
  163. error = err;
  164. return null;
  165. });
  166. if (error) {
  167. throw error;
  168. }
  169. return res.url;
  170. };
  171. export const getModelConfig = async (token: string): Promise<GlobalModelConfig> => {
  172. let error = null;
  173. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  174. method: 'GET',
  175. headers: {
  176. 'Content-Type': 'application/json',
  177. Authorization: `Bearer ${token}`
  178. }
  179. })
  180. .then(async (res) => {
  181. if (!res.ok) throw await res.json();
  182. return res.json();
  183. })
  184. .catch((err) => {
  185. console.log(err);
  186. error = err;
  187. return null;
  188. });
  189. if (error) {
  190. throw error;
  191. }
  192. return res.models;
  193. };
  194. export interface ModelConfig {
  195. id: string;
  196. name?: string;
  197. description?: string;
  198. vision_capable?: boolean;
  199. }
  200. export interface GlobalModelConfig {
  201. ollama: ModelConfig[];
  202. litellm: ModelConfig[];
  203. openai: ModelConfig[];
  204. }
  205. export const updateModelConfig = async (token: string, config: GlobalModelConfig) => {
  206. let error = null;
  207. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  208. method: 'POST',
  209. headers: {
  210. 'Content-Type': 'application/json',
  211. Authorization: `Bearer ${token}`
  212. },
  213. body: JSON.stringify(config)
  214. })
  215. .then(async (res) => {
  216. if (!res.ok) throw await res.json();
  217. return res.json();
  218. })
  219. .catch((err) => {
  220. console.log(err);
  221. error = err;
  222. return null;
  223. });
  224. if (error) {
  225. throw error;
  226. }
  227. return res;
  228. };