index.ts 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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 the models
  28. .sort((a, b) => {
  29. // Check if models have position property
  30. const aHasPosition = a.info?.meta?.position !== undefined;
  31. const bHasPosition = b.info?.meta?.position !== undefined;
  32. // If both a and b have the position property
  33. if (aHasPosition && bHasPosition) {
  34. return a.info.meta.position - b.info.meta.position;
  35. }
  36. // If only a has the position property, it should come first
  37. if (aHasPosition) return -1;
  38. // If only b has the position property, it should come first
  39. if (bHasPosition) return 1;
  40. // Compare case-insensitively by name for models without position property
  41. const lowerA = a.name.toLowerCase();
  42. const lowerB = b.name.toLowerCase();
  43. if (lowerA < lowerB) return -1;
  44. if (lowerA > lowerB) return 1;
  45. // If same case-insensitively, sort by original strings,
  46. // lowercase will come before uppercase due to ASCII values
  47. if (a.name < b.name) return -1;
  48. if (a.name > b.name) return 1;
  49. return 0; // They are equal
  50. });
  51. console.log(models);
  52. return models;
  53. };
  54. type ChatCompletedForm = {
  55. model: string;
  56. messages: string[];
  57. chat_id: string;
  58. session_id: string;
  59. };
  60. export const chatCompleted = async (token: string, body: ChatCompletedForm) => {
  61. let error = null;
  62. const res = await fetch(`${WEBUI_BASE_URL}/api/chat/completed`, {
  63. method: 'POST',
  64. headers: {
  65. Accept: 'application/json',
  66. 'Content-Type': 'application/json',
  67. ...(token && { authorization: `Bearer ${token}` })
  68. },
  69. body: JSON.stringify(body)
  70. })
  71. .then(async (res) => {
  72. if (!res.ok) throw await res.json();
  73. return res.json();
  74. })
  75. .catch((err) => {
  76. console.log(err);
  77. if ('detail' in err) {
  78. error = err.detail;
  79. } else {
  80. error = err;
  81. }
  82. return null;
  83. });
  84. if (error) {
  85. throw error;
  86. }
  87. return res;
  88. };
  89. type ChatActionForm = {
  90. model: string;
  91. messages: string[];
  92. chat_id: string;
  93. };
  94. export const chatAction = async (token: string, action_id: string, body: ChatActionForm) => {
  95. let error = null;
  96. const res = await fetch(`${WEBUI_BASE_URL}/api/chat/actions/${action_id}`, {
  97. method: 'POST',
  98. headers: {
  99. Accept: 'application/json',
  100. 'Content-Type': 'application/json',
  101. ...(token && { authorization: `Bearer ${token}` })
  102. },
  103. body: JSON.stringify(body)
  104. })
  105. .then(async (res) => {
  106. if (!res.ok) throw await res.json();
  107. return res.json();
  108. })
  109. .catch((err) => {
  110. console.log(err);
  111. if ('detail' in err) {
  112. error = err.detail;
  113. } else {
  114. error = err;
  115. }
  116. return null;
  117. });
  118. if (error) {
  119. throw error;
  120. }
  121. return res;
  122. };
  123. export const getTaskConfig = async (token: string = '') => {
  124. let error = null;
  125. const res = await fetch(`${WEBUI_BASE_URL}/api/task/config`, {
  126. method: 'GET',
  127. headers: {
  128. Accept: 'application/json',
  129. 'Content-Type': 'application/json',
  130. ...(token && { authorization: `Bearer ${token}` })
  131. }
  132. })
  133. .then(async (res) => {
  134. if (!res.ok) throw await res.json();
  135. return res.json();
  136. })
  137. .catch((err) => {
  138. console.log(err);
  139. error = err;
  140. return null;
  141. });
  142. if (error) {
  143. throw error;
  144. }
  145. return res;
  146. };
  147. export const updateTaskConfig = async (token: string, config: object) => {
  148. let error = null;
  149. const res = await fetch(`${WEBUI_BASE_URL}/api/task/config/update`, {
  150. method: 'POST',
  151. headers: {
  152. Accept: 'application/json',
  153. 'Content-Type': 'application/json',
  154. ...(token && { authorization: `Bearer ${token}` })
  155. },
  156. body: JSON.stringify(config)
  157. })
  158. .then(async (res) => {
  159. if (!res.ok) throw await res.json();
  160. return res.json();
  161. })
  162. .catch((err) => {
  163. console.log(err);
  164. if ('detail' in err) {
  165. error = err.detail;
  166. } else {
  167. error = err;
  168. }
  169. return null;
  170. });
  171. if (error) {
  172. throw error;
  173. }
  174. return res;
  175. };
  176. export const generateTitle = async (
  177. token: string = '',
  178. model: string,
  179. prompt: string,
  180. chat_id?: string
  181. ) => {
  182. let error = null;
  183. const res = await fetch(`${WEBUI_BASE_URL}/api/task/title/completions`, {
  184. method: 'POST',
  185. headers: {
  186. Accept: 'application/json',
  187. 'Content-Type': 'application/json',
  188. Authorization: `Bearer ${token}`
  189. },
  190. body: JSON.stringify({
  191. model: model,
  192. prompt: prompt,
  193. ...(chat_id && { chat_id: chat_id })
  194. })
  195. })
  196. .then(async (res) => {
  197. if (!res.ok) throw await res.json();
  198. return res.json();
  199. })
  200. .catch((err) => {
  201. console.log(err);
  202. if ('detail' in err) {
  203. error = err.detail;
  204. }
  205. return null;
  206. });
  207. if (error) {
  208. throw error;
  209. }
  210. return res?.choices[0]?.message?.content.replace(/["']/g, '') ?? 'New Chat';
  211. };
  212. export const generateTags = async (
  213. token: string = '',
  214. model: string,
  215. messages: string,
  216. chat_id?: string
  217. ) => {
  218. let error = null;
  219. const res = await fetch(`${WEBUI_BASE_URL}/api/task/tags/completions`, {
  220. method: 'POST',
  221. headers: {
  222. Accept: 'application/json',
  223. 'Content-Type': 'application/json',
  224. Authorization: `Bearer ${token}`
  225. },
  226. body: JSON.stringify({
  227. model: model,
  228. messages: messages,
  229. ...(chat_id && { chat_id: chat_id })
  230. })
  231. })
  232. .then(async (res) => {
  233. if (!res.ok) throw await res.json();
  234. return res.json();
  235. })
  236. .catch((err) => {
  237. console.log(err);
  238. if ('detail' in err) {
  239. error = err.detail;
  240. }
  241. return null;
  242. });
  243. if (error) {
  244. throw error;
  245. }
  246. try {
  247. // Step 1: Safely extract the response string
  248. const response = res?.choices[0]?.message?.content ?? '';
  249. // Step 2: Attempt to fix common JSON format issues like single quotes
  250. const sanitizedResponse = response.replace(/['‘’`]/g, '"'); // Convert single quotes to double quotes for valid JSON
  251. // Step 3: Find the relevant JSON block within the response
  252. const jsonStartIndex = sanitizedResponse.indexOf('{');
  253. const jsonEndIndex = sanitizedResponse.lastIndexOf('}');
  254. // Step 4: Check if we found a valid JSON block (with both `{` and `}`)
  255. if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
  256. const jsonResponse = sanitizedResponse.substring(jsonStartIndex, jsonEndIndex + 1);
  257. // Step 5: Parse the JSON block
  258. const parsed = JSON.parse(jsonResponse);
  259. // Step 6: If there's a "tags" key, return the tags array; otherwise, return an empty array
  260. if (parsed && parsed.tags) {
  261. return Array.isArray(parsed.tags) ? parsed.tags : [];
  262. } else {
  263. return [];
  264. }
  265. }
  266. // If no valid JSON block found, return an empty array
  267. return [];
  268. } catch (e) {
  269. // Catch and safely return empty array on any parsing errors
  270. console.error('Failed to parse response: ', e);
  271. return [];
  272. }
  273. };
  274. export const generateEmoji = async (
  275. token: string = '',
  276. model: string,
  277. prompt: string,
  278. chat_id?: string
  279. ) => {
  280. let error = null;
  281. const res = await fetch(`${WEBUI_BASE_URL}/api/task/emoji/completions`, {
  282. method: 'POST',
  283. headers: {
  284. Accept: 'application/json',
  285. 'Content-Type': 'application/json',
  286. Authorization: `Bearer ${token}`
  287. },
  288. body: JSON.stringify({
  289. model: model,
  290. prompt: prompt,
  291. ...(chat_id && { chat_id: chat_id })
  292. })
  293. })
  294. .then(async (res) => {
  295. if (!res.ok) throw await res.json();
  296. return res.json();
  297. })
  298. .catch((err) => {
  299. console.log(err);
  300. if ('detail' in err) {
  301. error = err.detail;
  302. }
  303. return null;
  304. });
  305. if (error) {
  306. throw error;
  307. }
  308. const response = res?.choices[0]?.message?.content.replace(/["']/g, '') ?? null;
  309. if (response) {
  310. if (/\p{Extended_Pictographic}/u.test(response)) {
  311. return response.match(/\p{Extended_Pictographic}/gu)[0];
  312. }
  313. }
  314. return null;
  315. };
  316. export const generateSearchQuery = async (
  317. token: string = '',
  318. model: string,
  319. messages: object[],
  320. prompt: string
  321. ) => {
  322. let error = null;
  323. const res = await fetch(`${WEBUI_BASE_URL}/api/task/query/completions`, {
  324. method: 'POST',
  325. headers: {
  326. Accept: 'application/json',
  327. 'Content-Type': 'application/json',
  328. Authorization: `Bearer ${token}`
  329. },
  330. body: JSON.stringify({
  331. model: model,
  332. messages: messages,
  333. prompt: prompt
  334. })
  335. })
  336. .then(async (res) => {
  337. if (!res.ok) throw await res.json();
  338. return res.json();
  339. })
  340. .catch((err) => {
  341. console.log(err);
  342. if ('detail' in err) {
  343. error = err.detail;
  344. }
  345. return null;
  346. });
  347. if (error) {
  348. throw error;
  349. }
  350. return res?.choices[0]?.message?.content.replace(/["']/g, '') ?? prompt;
  351. };
  352. export const generateMoACompletion = async (
  353. token: string = '',
  354. model: string,
  355. prompt: string,
  356. responses: string[]
  357. ) => {
  358. const controller = new AbortController();
  359. let error = null;
  360. const res = await fetch(`${WEBUI_BASE_URL}/api/task/moa/completions`, {
  361. signal: controller.signal,
  362. method: 'POST',
  363. headers: {
  364. Accept: 'application/json',
  365. 'Content-Type': 'application/json',
  366. Authorization: `Bearer ${token}`
  367. },
  368. body: JSON.stringify({
  369. model: model,
  370. prompt: prompt,
  371. responses: responses,
  372. stream: true
  373. })
  374. }).catch((err) => {
  375. console.log(err);
  376. error = err;
  377. return null;
  378. });
  379. if (error) {
  380. throw error;
  381. }
  382. return [res, controller];
  383. };
  384. export const getPipelinesList = async (token: string = '') => {
  385. let error = null;
  386. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/list`, {
  387. method: 'GET',
  388. headers: {
  389. Accept: 'application/json',
  390. 'Content-Type': 'application/json',
  391. ...(token && { authorization: `Bearer ${token}` })
  392. }
  393. })
  394. .then(async (res) => {
  395. if (!res.ok) throw await res.json();
  396. return res.json();
  397. })
  398. .catch((err) => {
  399. console.log(err);
  400. error = err;
  401. return null;
  402. });
  403. if (error) {
  404. throw error;
  405. }
  406. let pipelines = res?.data ?? [];
  407. return pipelines;
  408. };
  409. export const uploadPipeline = async (token: string, file: File, urlIdx: string) => {
  410. let error = null;
  411. // Create a new FormData object to handle the file upload
  412. const formData = new FormData();
  413. formData.append('file', file);
  414. formData.append('urlIdx', urlIdx);
  415. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/upload`, {
  416. method: 'POST',
  417. headers: {
  418. ...(token && { authorization: `Bearer ${token}` })
  419. // 'Content-Type': 'multipart/form-data' is not needed as Fetch API will set it automatically
  420. },
  421. body: formData
  422. })
  423. .then(async (res) => {
  424. if (!res.ok) throw await res.json();
  425. return res.json();
  426. })
  427. .catch((err) => {
  428. console.log(err);
  429. if ('detail' in err) {
  430. error = err.detail;
  431. } else {
  432. error = err;
  433. }
  434. return null;
  435. });
  436. if (error) {
  437. throw error;
  438. }
  439. return res;
  440. };
  441. export const downloadPipeline = async (token: string, url: string, urlIdx: string) => {
  442. let error = null;
  443. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/add`, {
  444. method: 'POST',
  445. headers: {
  446. Accept: 'application/json',
  447. 'Content-Type': 'application/json',
  448. ...(token && { authorization: `Bearer ${token}` })
  449. },
  450. body: JSON.stringify({
  451. url: url,
  452. urlIdx: urlIdx
  453. })
  454. })
  455. .then(async (res) => {
  456. if (!res.ok) throw await res.json();
  457. return res.json();
  458. })
  459. .catch((err) => {
  460. console.log(err);
  461. if ('detail' in err) {
  462. error = err.detail;
  463. } else {
  464. error = err;
  465. }
  466. return null;
  467. });
  468. if (error) {
  469. throw error;
  470. }
  471. return res;
  472. };
  473. export const deletePipeline = async (token: string, id: string, urlIdx: string) => {
  474. let error = null;
  475. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines/delete`, {
  476. method: 'DELETE',
  477. headers: {
  478. Accept: 'application/json',
  479. 'Content-Type': 'application/json',
  480. ...(token && { authorization: `Bearer ${token}` })
  481. },
  482. body: JSON.stringify({
  483. id: id,
  484. urlIdx: urlIdx
  485. })
  486. })
  487. .then(async (res) => {
  488. if (!res.ok) throw await res.json();
  489. return res.json();
  490. })
  491. .catch((err) => {
  492. console.log(err);
  493. if ('detail' in err) {
  494. error = err.detail;
  495. } else {
  496. error = err;
  497. }
  498. return null;
  499. });
  500. if (error) {
  501. throw error;
  502. }
  503. return res;
  504. };
  505. export const getPipelines = async (token: string, urlIdx?: string) => {
  506. let error = null;
  507. const searchParams = new URLSearchParams();
  508. if (urlIdx !== undefined) {
  509. searchParams.append('urlIdx', urlIdx);
  510. }
  511. const res = await fetch(`${WEBUI_BASE_URL}/api/pipelines?${searchParams.toString()}`, {
  512. method: 'GET',
  513. headers: {
  514. Accept: 'application/json',
  515. 'Content-Type': 'application/json',
  516. ...(token && { authorization: `Bearer ${token}` })
  517. }
  518. })
  519. .then(async (res) => {
  520. if (!res.ok) throw await res.json();
  521. return res.json();
  522. })
  523. .catch((err) => {
  524. console.log(err);
  525. error = err;
  526. return null;
  527. });
  528. if (error) {
  529. throw error;
  530. }
  531. let pipelines = res?.data ?? [];
  532. return pipelines;
  533. };
  534. export const getPipelineValves = async (token: string, pipeline_id: string, urlIdx: string) => {
  535. let error = null;
  536. const searchParams = new URLSearchParams();
  537. if (urlIdx !== undefined) {
  538. searchParams.append('urlIdx', urlIdx);
  539. }
  540. const res = await fetch(
  541. `${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves?${searchParams.toString()}`,
  542. {
  543. method: 'GET',
  544. headers: {
  545. Accept: 'application/json',
  546. 'Content-Type': 'application/json',
  547. ...(token && { authorization: `Bearer ${token}` })
  548. }
  549. }
  550. )
  551. .then(async (res) => {
  552. if (!res.ok) throw await res.json();
  553. return res.json();
  554. })
  555. .catch((err) => {
  556. console.log(err);
  557. error = err;
  558. return null;
  559. });
  560. if (error) {
  561. throw error;
  562. }
  563. return res;
  564. };
  565. export const getPipelineValvesSpec = async (token: string, pipeline_id: string, urlIdx: string) => {
  566. let error = null;
  567. const searchParams = new URLSearchParams();
  568. if (urlIdx !== undefined) {
  569. searchParams.append('urlIdx', urlIdx);
  570. }
  571. const res = await fetch(
  572. `${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/spec?${searchParams.toString()}`,
  573. {
  574. method: 'GET',
  575. headers: {
  576. Accept: 'application/json',
  577. 'Content-Type': 'application/json',
  578. ...(token && { authorization: `Bearer ${token}` })
  579. }
  580. }
  581. )
  582. .then(async (res) => {
  583. if (!res.ok) throw await res.json();
  584. return res.json();
  585. })
  586. .catch((err) => {
  587. console.log(err);
  588. error = err;
  589. return null;
  590. });
  591. if (error) {
  592. throw error;
  593. }
  594. return res;
  595. };
  596. export const updatePipelineValves = async (
  597. token: string = '',
  598. pipeline_id: string,
  599. valves: object,
  600. urlIdx: string
  601. ) => {
  602. let error = null;
  603. const searchParams = new URLSearchParams();
  604. if (urlIdx !== undefined) {
  605. searchParams.append('urlIdx', urlIdx);
  606. }
  607. const res = await fetch(
  608. `${WEBUI_BASE_URL}/api/pipelines/${pipeline_id}/valves/update?${searchParams.toString()}`,
  609. {
  610. method: 'POST',
  611. headers: {
  612. Accept: 'application/json',
  613. 'Content-Type': 'application/json',
  614. ...(token && { authorization: `Bearer ${token}` })
  615. },
  616. body: JSON.stringify(valves)
  617. }
  618. )
  619. .then(async (res) => {
  620. if (!res.ok) throw await res.json();
  621. return res.json();
  622. })
  623. .catch((err) => {
  624. console.log(err);
  625. if ('detail' in err) {
  626. error = err.detail;
  627. } else {
  628. error = err;
  629. }
  630. return null;
  631. });
  632. if (error) {
  633. throw error;
  634. }
  635. return res;
  636. };
  637. export const getBackendConfig = async () => {
  638. let error = null;
  639. const res = await fetch(`${WEBUI_BASE_URL}/api/config`, {
  640. method: 'GET',
  641. credentials: 'include',
  642. headers: {
  643. 'Content-Type': 'application/json'
  644. }
  645. })
  646. .then(async (res) => {
  647. if (!res.ok) throw await res.json();
  648. return res.json();
  649. })
  650. .catch((err) => {
  651. console.log(err);
  652. error = err;
  653. return null;
  654. });
  655. if (error) {
  656. throw error;
  657. }
  658. return res;
  659. };
  660. export const getChangelog = async () => {
  661. let error = null;
  662. const res = await fetch(`${WEBUI_BASE_URL}/api/changelog`, {
  663. method: 'GET',
  664. headers: {
  665. 'Content-Type': 'application/json'
  666. }
  667. })
  668. .then(async (res) => {
  669. if (!res.ok) throw await res.json();
  670. return res.json();
  671. })
  672. .catch((err) => {
  673. console.log(err);
  674. error = err;
  675. return null;
  676. });
  677. if (error) {
  678. throw error;
  679. }
  680. return res;
  681. };
  682. export const getVersionUpdates = async () => {
  683. let error = null;
  684. const res = await fetch(`${WEBUI_BASE_URL}/api/version/updates`, {
  685. method: 'GET',
  686. headers: {
  687. 'Content-Type': 'application/json'
  688. }
  689. })
  690. .then(async (res) => {
  691. if (!res.ok) throw await res.json();
  692. return res.json();
  693. })
  694. .catch((err) => {
  695. console.log(err);
  696. error = err;
  697. return null;
  698. });
  699. if (error) {
  700. throw error;
  701. }
  702. return res;
  703. };
  704. export const getModelFilterConfig = async (token: string) => {
  705. let error = null;
  706. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  707. method: 'GET',
  708. headers: {
  709. 'Content-Type': 'application/json',
  710. Authorization: `Bearer ${token}`
  711. }
  712. })
  713. .then(async (res) => {
  714. if (!res.ok) throw await res.json();
  715. return res.json();
  716. })
  717. .catch((err) => {
  718. console.log(err);
  719. error = err;
  720. return null;
  721. });
  722. if (error) {
  723. throw error;
  724. }
  725. return res;
  726. };
  727. export const updateModelFilterConfig = async (
  728. token: string,
  729. enabled: boolean,
  730. models: string[]
  731. ) => {
  732. let error = null;
  733. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  734. method: 'POST',
  735. headers: {
  736. 'Content-Type': 'application/json',
  737. Authorization: `Bearer ${token}`
  738. },
  739. body: JSON.stringify({
  740. enabled: enabled,
  741. models: models
  742. })
  743. })
  744. .then(async (res) => {
  745. if (!res.ok) throw await res.json();
  746. return res.json();
  747. })
  748. .catch((err) => {
  749. console.log(err);
  750. error = err;
  751. return null;
  752. });
  753. if (error) {
  754. throw error;
  755. }
  756. return res;
  757. };
  758. export const getWebhookUrl = async (token: string) => {
  759. let error = null;
  760. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  761. method: 'GET',
  762. headers: {
  763. 'Content-Type': 'application/json',
  764. Authorization: `Bearer ${token}`
  765. }
  766. })
  767. .then(async (res) => {
  768. if (!res.ok) throw await res.json();
  769. return res.json();
  770. })
  771. .catch((err) => {
  772. console.log(err);
  773. error = err;
  774. return null;
  775. });
  776. if (error) {
  777. throw error;
  778. }
  779. return res.url;
  780. };
  781. export const updateWebhookUrl = async (token: string, url: string) => {
  782. let error = null;
  783. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  784. method: 'POST',
  785. headers: {
  786. 'Content-Type': 'application/json',
  787. Authorization: `Bearer ${token}`
  788. },
  789. body: JSON.stringify({
  790. url: url
  791. })
  792. })
  793. .then(async (res) => {
  794. if (!res.ok) throw await res.json();
  795. return res.json();
  796. })
  797. .catch((err) => {
  798. console.log(err);
  799. error = err;
  800. return null;
  801. });
  802. if (error) {
  803. throw error;
  804. }
  805. return res.url;
  806. };
  807. export const getCommunitySharingEnabledStatus = async (token: string) => {
  808. let error = null;
  809. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing`, {
  810. method: 'GET',
  811. headers: {
  812. 'Content-Type': 'application/json',
  813. Authorization: `Bearer ${token}`
  814. }
  815. })
  816. .then(async (res) => {
  817. if (!res.ok) throw await res.json();
  818. return res.json();
  819. })
  820. .catch((err) => {
  821. console.log(err);
  822. error = err;
  823. return null;
  824. });
  825. if (error) {
  826. throw error;
  827. }
  828. return res;
  829. };
  830. export const toggleCommunitySharingEnabledStatus = async (token: string) => {
  831. let error = null;
  832. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing/toggle`, {
  833. method: 'GET',
  834. headers: {
  835. 'Content-Type': 'application/json',
  836. Authorization: `Bearer ${token}`
  837. }
  838. })
  839. .then(async (res) => {
  840. if (!res.ok) throw await res.json();
  841. return res.json();
  842. })
  843. .catch((err) => {
  844. console.log(err);
  845. error = err.detail;
  846. return null;
  847. });
  848. if (error) {
  849. throw error;
  850. }
  851. return res;
  852. };
  853. export const getModelConfig = async (token: string): Promise<GlobalModelConfig> => {
  854. let error = null;
  855. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  856. method: 'GET',
  857. headers: {
  858. 'Content-Type': 'application/json',
  859. Authorization: `Bearer ${token}`
  860. }
  861. })
  862. .then(async (res) => {
  863. if (!res.ok) throw await res.json();
  864. return res.json();
  865. })
  866. .catch((err) => {
  867. console.log(err);
  868. error = err;
  869. return null;
  870. });
  871. if (error) {
  872. throw error;
  873. }
  874. return res.models;
  875. };
  876. export interface ModelConfig {
  877. id: string;
  878. name: string;
  879. meta: ModelMeta;
  880. base_model_id?: string;
  881. params: ModelParams;
  882. }
  883. export interface ModelMeta {
  884. description?: string;
  885. capabilities?: object;
  886. profile_image_url?: string;
  887. }
  888. export interface ModelParams {}
  889. export type GlobalModelConfig = ModelConfig[];
  890. export const updateModelConfig = async (token: string, config: GlobalModelConfig) => {
  891. let error = null;
  892. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  893. method: 'POST',
  894. headers: {
  895. 'Content-Type': 'application/json',
  896. Authorization: `Bearer ${token}`
  897. },
  898. body: JSON.stringify({
  899. models: config
  900. })
  901. })
  902. .then(async (res) => {
  903. if (!res.ok) throw await res.json();
  904. return res.json();
  905. })
  906. .catch((err) => {
  907. console.log(err);
  908. error = err;
  909. return null;
  910. });
  911. if (error) {
  912. throw error;
  913. }
  914. return res;
  915. };