index.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
  2. export const getModels = async (token: string = '') => {
  3. let error = null;
  4. const res = await fetch(`${WEBUI_BASE_URL}/api/models`, {
  5. method: 'GET',
  6. headers: {
  7. Accept: 'application/json',
  8. 'Content-Type': 'application/json',
  9. ...(token && { 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. console.log(err);
  18. error = err;
  19. return null;
  20. });
  21. if (error) {
  22. throw error;
  23. }
  24. let models = res?.data ?? [];
  25. models = models
  26. .filter((models) => models)
  27. .sort((a, b) => {
  28. // Compare case-insensitively
  29. const lowerA = a.name.toLowerCase();
  30. const lowerB = b.name.toLowerCase();
  31. if (lowerA < lowerB) return -1;
  32. if (lowerA > lowerB) return 1;
  33. // If same case-insensitively, sort by original strings,
  34. // lowercase will come before uppercase due to ASCII values
  35. if (a < b) return -1;
  36. if (a > b) return 1;
  37. return 0; // They are equal
  38. });
  39. console.log(models);
  40. return models;
  41. };
  42. export const getPipelines = async (token: string = '') => {
  43. let error = null;
  44. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines`, {
  45. method: 'GET',
  46. headers: {
  47. Accept: 'application/json',
  48. 'Content-Type': 'application/json',
  49. ...(token && { authorization: `Bearer ${token}` })
  50. }
  51. })
  52. .then(async (res) => {
  53. if (!res.ok) throw await res.json();
  54. return res.json();
  55. })
  56. .catch((err) => {
  57. console.log(err);
  58. error = err;
  59. return null;
  60. });
  61. if (error) {
  62. throw error;
  63. }
  64. let pipelines = res?.data ?? [];
  65. return pipelines;
  66. };
  67. export const getPipelineValves = async (token: string = '', pipeline_id: string) => {
  68. let error = null;
  69. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves`, {
  70. method: 'GET',
  71. headers: {
  72. Accept: 'application/json',
  73. 'Content-Type': 'application/json',
  74. ...(token && { 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 getPipelineValvesSpec = async (token: string = '', pipeline_id: string) => {
  92. let error = null;
  93. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/spec`, {
  94. method: 'GET',
  95. headers: {
  96. Accept: 'application/json',
  97. 'Content-Type': 'application/json',
  98. ...(token && { authorization: `Bearer ${token}` })
  99. }
  100. })
  101. .then(async (res) => {
  102. if (!res.ok) throw await res.json();
  103. return res.json();
  104. })
  105. .catch((err) => {
  106. console.log(err);
  107. error = err;
  108. return null;
  109. });
  110. if (error) {
  111. throw error;
  112. }
  113. return res;
  114. };
  115. export const updatePipelineValves = async (
  116. token: string = '',
  117. pipeline_id: string,
  118. valves: object
  119. ) => {
  120. let error = null;
  121. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/update`, {
  122. method: 'POST',
  123. headers: {
  124. Accept: 'application/json',
  125. 'Content-Type': 'application/json',
  126. ...(token && { authorization: `Bearer ${token}` })
  127. },
  128. body: JSON.stringify(valves)
  129. })
  130. .then(async (res) => {
  131. if (!res.ok) throw await res.json();
  132. return res.json();
  133. })
  134. .catch((err) => {
  135. console.log(err);
  136. if ('detail' in err) {
  137. error = err.detail;
  138. } else {
  139. error = err;
  140. }
  141. return null;
  142. });
  143. if (error) {
  144. throw error;
  145. }
  146. return res;
  147. };
  148. export const getBackendConfig = async () => {
  149. let error = null;
  150. const res = await fetch(`${WEBUI_BASE_URL}/api/config`, {
  151. method: 'GET',
  152. headers: {
  153. 'Content-Type': 'application/json'
  154. }
  155. })
  156. .then(async (res) => {
  157. if (!res.ok) throw await res.json();
  158. return res.json();
  159. })
  160. .catch((err) => {
  161. console.log(err);
  162. error = err;
  163. return null;
  164. });
  165. if (error) {
  166. throw error;
  167. }
  168. return res;
  169. };
  170. export const getChangelog = async () => {
  171. let error = null;
  172. const res = await fetch(`${WEBUI_BASE_URL}/api/changelog`, {
  173. method: 'GET',
  174. headers: {
  175. 'Content-Type': 'application/json'
  176. }
  177. })
  178. .then(async (res) => {
  179. if (!res.ok) throw await res.json();
  180. return res.json();
  181. })
  182. .catch((err) => {
  183. console.log(err);
  184. error = err;
  185. return null;
  186. });
  187. if (error) {
  188. throw error;
  189. }
  190. return res;
  191. };
  192. export const getVersionUpdates = async () => {
  193. let error = null;
  194. const res = await fetch(`${WEBUI_BASE_URL}/api/version/updates`, {
  195. method: 'GET',
  196. headers: {
  197. 'Content-Type': 'application/json'
  198. }
  199. })
  200. .then(async (res) => {
  201. if (!res.ok) throw await res.json();
  202. return res.json();
  203. })
  204. .catch((err) => {
  205. console.log(err);
  206. error = err;
  207. return null;
  208. });
  209. if (error) {
  210. throw error;
  211. }
  212. return res;
  213. };
  214. export const getModelFilterConfig = async (token: string) => {
  215. let error = null;
  216. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  217. method: 'GET',
  218. headers: {
  219. 'Content-Type': 'application/json',
  220. Authorization: `Bearer ${token}`
  221. }
  222. })
  223. .then(async (res) => {
  224. if (!res.ok) throw await res.json();
  225. return res.json();
  226. })
  227. .catch((err) => {
  228. console.log(err);
  229. error = err;
  230. return null;
  231. });
  232. if (error) {
  233. throw error;
  234. }
  235. return res;
  236. };
  237. export const updateModelFilterConfig = async (
  238. token: string,
  239. enabled: boolean,
  240. models: string[]
  241. ) => {
  242. let error = null;
  243. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  244. method: 'POST',
  245. headers: {
  246. 'Content-Type': 'application/json',
  247. Authorization: `Bearer ${token}`
  248. },
  249. body: JSON.stringify({
  250. enabled: enabled,
  251. models: models
  252. })
  253. })
  254. .then(async (res) => {
  255. if (!res.ok) throw await res.json();
  256. return res.json();
  257. })
  258. .catch((err) => {
  259. console.log(err);
  260. error = err;
  261. return null;
  262. });
  263. if (error) {
  264. throw error;
  265. }
  266. return res;
  267. };
  268. export const getWebhookUrl = async (token: string) => {
  269. let error = null;
  270. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  271. method: 'GET',
  272. headers: {
  273. 'Content-Type': 'application/json',
  274. Authorization: `Bearer ${token}`
  275. }
  276. })
  277. .then(async (res) => {
  278. if (!res.ok) throw await res.json();
  279. return res.json();
  280. })
  281. .catch((err) => {
  282. console.log(err);
  283. error = err;
  284. return null;
  285. });
  286. if (error) {
  287. throw error;
  288. }
  289. return res.url;
  290. };
  291. export const updateWebhookUrl = async (token: string, url: string) => {
  292. let error = null;
  293. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  294. method: 'POST',
  295. headers: {
  296. 'Content-Type': 'application/json',
  297. Authorization: `Bearer ${token}`
  298. },
  299. body: JSON.stringify({
  300. url: url
  301. })
  302. })
  303. .then(async (res) => {
  304. if (!res.ok) throw await res.json();
  305. return res.json();
  306. })
  307. .catch((err) => {
  308. console.log(err);
  309. error = err;
  310. return null;
  311. });
  312. if (error) {
  313. throw error;
  314. }
  315. return res.url;
  316. };
  317. export const getCommunitySharingEnabledStatus = async (token: string) => {
  318. let error = null;
  319. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing`, {
  320. method: 'GET',
  321. headers: {
  322. 'Content-Type': 'application/json',
  323. Authorization: `Bearer ${token}`
  324. }
  325. })
  326. .then(async (res) => {
  327. if (!res.ok) throw await res.json();
  328. return res.json();
  329. })
  330. .catch((err) => {
  331. console.log(err);
  332. error = err;
  333. return null;
  334. });
  335. if (error) {
  336. throw error;
  337. }
  338. return res;
  339. };
  340. export const toggleCommunitySharingEnabledStatus = async (token: string) => {
  341. let error = null;
  342. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing/toggle`, {
  343. method: 'GET',
  344. headers: {
  345. 'Content-Type': 'application/json',
  346. Authorization: `Bearer ${token}`
  347. }
  348. })
  349. .then(async (res) => {
  350. if (!res.ok) throw await res.json();
  351. return res.json();
  352. })
  353. .catch((err) => {
  354. console.log(err);
  355. error = err.detail;
  356. return null;
  357. });
  358. if (error) {
  359. throw error;
  360. }
  361. return res;
  362. };
  363. export const getModelConfig = async (token: string): Promise<GlobalModelConfig> => {
  364. let error = null;
  365. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  366. method: 'GET',
  367. headers: {
  368. 'Content-Type': 'application/json',
  369. Authorization: `Bearer ${token}`
  370. }
  371. })
  372. .then(async (res) => {
  373. if (!res.ok) throw await res.json();
  374. return res.json();
  375. })
  376. .catch((err) => {
  377. console.log(err);
  378. error = err;
  379. return null;
  380. });
  381. if (error) {
  382. throw error;
  383. }
  384. return res.models;
  385. };
  386. export interface ModelConfig {
  387. id: string;
  388. name: string;
  389. meta: ModelMeta;
  390. base_model_id?: string;
  391. params: ModelParams;
  392. }
  393. export interface ModelMeta {
  394. description?: string;
  395. capabilities?: object;
  396. }
  397. export interface ModelParams {}
  398. export type GlobalModelConfig = ModelConfig[];
  399. export const updateModelConfig = async (token: string, config: GlobalModelConfig) => {
  400. let error = null;
  401. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  402. method: 'POST',
  403. headers: {
  404. 'Content-Type': 'application/json',
  405. Authorization: `Bearer ${token}`
  406. },
  407. body: JSON.stringify({
  408. models: config
  409. })
  410. })
  411. .then(async (res) => {
  412. if (!res.ok) throw await res.json();
  413. return res.json();
  414. })
  415. .catch((err) => {
  416. console.log(err);
  417. error = err;
  418. return null;
  419. });
  420. if (error) {
  421. throw error;
  422. }
  423. return res;
  424. };