index.ts 23 KB

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