index.ts 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657
  1. import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
  2. import { convertOpenApiToToolPayload } from '$lib/utils';
  3. import { getOpenAIModelsDirect } from './openai';
  4. import { parse } from 'yaml';
  5. import { toast } from 'svelte-sonner';
  6. export const getModels = async (
  7. token: string = '',
  8. connections: object | null = null,
  9. base: boolean = false,
  10. refresh: boolean = false
  11. ) => {
  12. const searchParams = new URLSearchParams();
  13. if (refresh) {
  14. searchParams.append('refresh', 'true');
  15. }
  16. let error = null;
  17. const res = await fetch(
  18. `${WEBUI_BASE_URL}/api/models${base ? '/base' : ''}?${searchParams.toString()}`,
  19. {
  20. method: 'GET',
  21. headers: {
  22. Accept: 'application/json',
  23. 'Content-Type': 'application/json',
  24. ...(token && { authorization: `Bearer ${token}` })
  25. }
  26. }
  27. )
  28. .then(async (res) => {
  29. if (!res.ok) throw await res.json();
  30. return res.json();
  31. })
  32. .catch((err) => {
  33. error = err;
  34. console.error(err);
  35. return null;
  36. });
  37. if (error) {
  38. throw error;
  39. }
  40. let models = res?.data ?? [];
  41. if (connections && !base) {
  42. let localModels = [];
  43. if (connections) {
  44. const OPENAI_API_BASE_URLS = connections.OPENAI_API_BASE_URLS;
  45. const OPENAI_API_KEYS = connections.OPENAI_API_KEYS;
  46. const OPENAI_API_CONFIGS = connections.OPENAI_API_CONFIGS;
  47. const requests = [];
  48. for (const idx in OPENAI_API_BASE_URLS) {
  49. const url = OPENAI_API_BASE_URLS[idx];
  50. if (idx.toString() in OPENAI_API_CONFIGS) {
  51. const apiConfig = OPENAI_API_CONFIGS[idx.toString()] ?? {};
  52. const enable = apiConfig?.enable ?? true;
  53. const modelIds = apiConfig?.model_ids ?? [];
  54. if (enable) {
  55. if (modelIds.length > 0) {
  56. const modelList = {
  57. object: 'list',
  58. data: modelIds.map((modelId) => ({
  59. id: modelId,
  60. name: modelId,
  61. owned_by: 'openai',
  62. openai: { id: modelId },
  63. urlIdx: idx
  64. }))
  65. };
  66. requests.push(
  67. (async () => {
  68. return modelList;
  69. })()
  70. );
  71. } else {
  72. requests.push(
  73. (async () => {
  74. return await getOpenAIModelsDirect(url, OPENAI_API_KEYS[idx])
  75. .then((res) => {
  76. return res;
  77. })
  78. .catch((err) => {
  79. return {
  80. object: 'list',
  81. data: [],
  82. urlIdx: idx
  83. };
  84. });
  85. })()
  86. );
  87. }
  88. } else {
  89. requests.push(
  90. (async () => {
  91. return {
  92. object: 'list',
  93. data: [],
  94. urlIdx: idx
  95. };
  96. })()
  97. );
  98. }
  99. }
  100. }
  101. const responses = await Promise.all(requests);
  102. for (const idx in responses) {
  103. const response = responses[idx];
  104. const apiConfig = OPENAI_API_CONFIGS[idx.toString()] ?? {};
  105. let models = Array.isArray(response) ? response : (response?.data ?? []);
  106. models = models.map((model) => ({ ...model, openai: { id: model.id }, urlIdx: idx }));
  107. const prefixId = apiConfig.prefix_id;
  108. if (prefixId) {
  109. for (const model of models) {
  110. model.id = `${prefixId}.${model.id}`;
  111. }
  112. }
  113. const tags = apiConfig.tags;
  114. if (tags) {
  115. for (const model of models) {
  116. model.tags = tags;
  117. }
  118. }
  119. localModels = localModels.concat(models);
  120. }
  121. }
  122. models = models.concat(
  123. localModels.map((model) => ({
  124. ...model,
  125. name: model?.name ?? model?.id,
  126. direct: true
  127. }))
  128. );
  129. // Remove duplicates
  130. const modelsMap = {};
  131. for (const model of models) {
  132. modelsMap[model.id] = model;
  133. }
  134. models = Object.values(modelsMap);
  135. }
  136. return models;
  137. };
  138. type ChatCompletedForm = {
  139. model: string;
  140. messages: string[];
  141. chat_id: string;
  142. session_id: string;
  143. };
  144. export const chatCompleted = async (token: string, body: ChatCompletedForm) => {
  145. let error = null;
  146. const res = await fetch(`${WEBUI_BASE_URL}/api/chat/completed`, {
  147. method: 'POST',
  148. headers: {
  149. Accept: 'application/json',
  150. 'Content-Type': 'application/json',
  151. ...(token && { authorization: `Bearer ${token}` })
  152. },
  153. body: JSON.stringify(body)
  154. })
  155. .then(async (res) => {
  156. if (!res.ok) throw await res.json();
  157. return res.json();
  158. })
  159. .catch((err) => {
  160. console.error(err);
  161. if ('detail' in err) {
  162. error = err.detail;
  163. } else {
  164. error = err;
  165. }
  166. return null;
  167. });
  168. if (error) {
  169. throw error;
  170. }
  171. return res;
  172. };
  173. type ChatActionForm = {
  174. model: string;
  175. messages: string[];
  176. chat_id: string;
  177. };
  178. export const chatAction = async (token: string, action_id: string, body: ChatActionForm) => {
  179. let error = null;
  180. const res = await fetch(`${WEBUI_BASE_URL}/api/chat/actions/${action_id}`, {
  181. method: 'POST',
  182. headers: {
  183. Accept: 'application/json',
  184. 'Content-Type': 'application/json',
  185. ...(token && { authorization: `Bearer ${token}` })
  186. },
  187. body: JSON.stringify(body)
  188. })
  189. .then(async (res) => {
  190. if (!res.ok) throw await res.json();
  191. return res.json();
  192. })
  193. .catch((err) => {
  194. console.error(err);
  195. if ('detail' in err) {
  196. error = err.detail;
  197. } else {
  198. error = err;
  199. }
  200. return null;
  201. });
  202. if (error) {
  203. throw error;
  204. }
  205. return res;
  206. };
  207. export const stopTask = async (token: string, id: string) => {
  208. let error = null;
  209. const res = await fetch(`${WEBUI_BASE_URL}/api/tasks/stop/${id}`, {
  210. method: 'POST',
  211. headers: {
  212. Accept: 'application/json',
  213. 'Content-Type': 'application/json',
  214. ...(token && { authorization: `Bearer ${token}` })
  215. }
  216. })
  217. .then(async (res) => {
  218. if (!res.ok) throw await res.json();
  219. return res.json();
  220. })
  221. .catch((err) => {
  222. console.error(err);
  223. if ('detail' in err) {
  224. error = err.detail;
  225. } else {
  226. error = err;
  227. }
  228. return null;
  229. });
  230. if (error) {
  231. throw error;
  232. }
  233. return res;
  234. };
  235. export const getTaskIdsByChatId = async (token: string, chat_id: string) => {
  236. let error = null;
  237. const res = await fetch(`${WEBUI_BASE_URL}/api/tasks/chat/${chat_id}`, {
  238. method: 'GET',
  239. headers: {
  240. Accept: 'application/json',
  241. 'Content-Type': 'application/json',
  242. ...(token && { authorization: `Bearer ${token}` })
  243. }
  244. })
  245. .then(async (res) => {
  246. if (!res.ok) throw await res.json();
  247. return res.json();
  248. })
  249. .catch((err) => {
  250. console.error(err);
  251. if ('detail' in err) {
  252. error = err.detail;
  253. } else {
  254. error = err;
  255. }
  256. return null;
  257. });
  258. if (error) {
  259. throw error;
  260. }
  261. return res;
  262. };
  263. export const getToolServerData = async (token: string, url: string) => {
  264. let error = null;
  265. const res = await fetch(`${url}`, {
  266. method: 'GET',
  267. headers: {
  268. Accept: 'application/json',
  269. 'Content-Type': 'application/json',
  270. ...(token && { authorization: `Bearer ${token}` })
  271. }
  272. })
  273. .then(async (res) => {
  274. // Check if URL ends with .yaml or .yml to determine format
  275. if (url.toLowerCase().endsWith('.yaml') || url.toLowerCase().endsWith('.yml')) {
  276. if (!res.ok) throw await res.text();
  277. const text = await res.text();
  278. return parse(text);
  279. } else {
  280. if (!res.ok) throw await res.json();
  281. return res.json();
  282. }
  283. })
  284. .catch((err) => {
  285. console.error(err);
  286. if ('detail' in err) {
  287. error = err.detail;
  288. } else {
  289. error = err;
  290. }
  291. return null;
  292. });
  293. if (error) {
  294. throw error;
  295. }
  296. const data = {
  297. openapi: res,
  298. info: res.info,
  299. specs: convertOpenApiToToolPayload(res)
  300. };
  301. console.log(data);
  302. return data;
  303. };
  304. export const getToolServersData = async (servers: object[]) => {
  305. return (
  306. await Promise.all(
  307. servers
  308. .filter((server) => server?.config?.enable)
  309. .map(async (server) => {
  310. let error = null;
  311. let toolServerToken = null;
  312. const auth_type = server?.auth_type ?? 'bearer';
  313. if (auth_type === 'bearer') {
  314. toolServerToken = server?.key;
  315. } else if (auth_type === 'none') {
  316. // No authentication
  317. } else if (auth_type === 'session') {
  318. toolServerToken = localStorage.token;
  319. }
  320. const data = await getToolServerData(
  321. toolServerToken,
  322. (server?.path ?? '').includes('://')
  323. ? server?.path
  324. : `${server?.url}${(server?.path ?? '').startsWith('/') ? '' : '/'}${server?.path}`
  325. ).catch((err) => {
  326. error = err;
  327. return null;
  328. });
  329. if (data) {
  330. const { openapi, info, specs } = data;
  331. return {
  332. url: server?.url,
  333. openapi: openapi,
  334. info: info,
  335. specs: specs
  336. };
  337. } else if (error) {
  338. return {
  339. error,
  340. url: server?.url
  341. };
  342. } else {
  343. return null;
  344. }
  345. })
  346. )
  347. ).filter((server) => server);
  348. };
  349. export const executeToolServer = async (
  350. token: string,
  351. url: string,
  352. name: string,
  353. params: Record<string, any>,
  354. serverData: { openapi: any; info: any; specs: any }
  355. ) => {
  356. let error = null;
  357. try {
  358. // Find the matching operationId in the OpenAPI spec
  359. const matchingRoute = Object.entries(serverData.openapi.paths).find(([_, methods]) =>
  360. Object.entries(methods as any).some(([__, operation]: any) => operation.operationId === name)
  361. );
  362. if (!matchingRoute) {
  363. throw new Error(`No matching route found for operationId: ${name}`);
  364. }
  365. const [routePath, methods] = matchingRoute;
  366. const methodEntry = Object.entries(methods as any).find(
  367. ([_, operation]: any) => operation.operationId === name
  368. );
  369. if (!methodEntry) {
  370. throw new Error(`No matching method found for operationId: ${name}`);
  371. }
  372. const [httpMethod, operation]: [string, any] = methodEntry;
  373. // Split parameters by type
  374. const pathParams: Record<string, any> = {};
  375. const queryParams: Record<string, any> = {};
  376. let bodyParams: any = {};
  377. if (operation.parameters) {
  378. operation.parameters.forEach((param: any) => {
  379. const paramName = param.name;
  380. const paramIn = param.in;
  381. if (params.hasOwnProperty(paramName)) {
  382. if (paramIn === 'path') {
  383. pathParams[paramName] = params[paramName];
  384. } else if (paramIn === 'query') {
  385. queryParams[paramName] = params[paramName];
  386. }
  387. }
  388. });
  389. }
  390. let finalUrl = `${url}${routePath}`;
  391. // Replace path parameters (`{param}`)
  392. Object.entries(pathParams).forEach(([key, value]) => {
  393. finalUrl = finalUrl.replace(new RegExp(`{${key}}`, 'g'), encodeURIComponent(value));
  394. });
  395. // Append query parameters to URL if any
  396. if (Object.keys(queryParams).length > 0) {
  397. const queryString = new URLSearchParams(
  398. Object.entries(queryParams).map(([k, v]) => [k, String(v)])
  399. ).toString();
  400. finalUrl += `?${queryString}`;
  401. }
  402. // Handle requestBody composite
  403. if (operation.requestBody && operation.requestBody.content) {
  404. const contentType = Object.keys(operation.requestBody.content)[0];
  405. if (params !== undefined) {
  406. bodyParams = params;
  407. } else {
  408. // Optional: Fallback or explicit error if body is expected but not provided
  409. throw new Error(`Request body expected for operation '${name}' but none found.`);
  410. }
  411. }
  412. // Prepare headers and request options
  413. const headers: Record<string, string> = {
  414. 'Content-Type': 'application/json',
  415. ...(token && { authorization: `Bearer ${token}` })
  416. };
  417. const requestOptions: RequestInit = {
  418. method: httpMethod.toUpperCase(),
  419. headers
  420. };
  421. if (['post', 'put', 'patch'].includes(httpMethod.toLowerCase()) && operation.requestBody) {
  422. requestOptions.body = JSON.stringify(bodyParams);
  423. }
  424. const res = await fetch(finalUrl, requestOptions);
  425. if (!res.ok) {
  426. const resText = await res.text();
  427. throw new Error(`HTTP error! Status: ${res.status}. Message: ${resText}`);
  428. }
  429. let responseData;
  430. try {
  431. responseData = await res.json();
  432. } catch (err) {
  433. responseData = await res.text();
  434. }
  435. return responseData;
  436. } catch (err: any) {
  437. error = err.message;
  438. console.error('API Request Error:', error);
  439. return { error };
  440. }
  441. };
  442. export const getTaskConfig = async (token: string = '') => {
  443. let error = null;
  444. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/config`, {
  445. method: 'GET',
  446. headers: {
  447. Accept: 'application/json',
  448. 'Content-Type': 'application/json',
  449. ...(token && { authorization: `Bearer ${token}` })
  450. }
  451. })
  452. .then(async (res) => {
  453. if (!res.ok) throw await res.json();
  454. return res.json();
  455. })
  456. .catch((err) => {
  457. console.error(err);
  458. error = err;
  459. return null;
  460. });
  461. if (error) {
  462. throw error;
  463. }
  464. return res;
  465. };
  466. export const updateTaskConfig = async (token: string, config: object) => {
  467. let error = null;
  468. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/config/update`, {
  469. method: 'POST',
  470. headers: {
  471. Accept: 'application/json',
  472. 'Content-Type': 'application/json',
  473. ...(token && { authorization: `Bearer ${token}` })
  474. },
  475. body: JSON.stringify(config)
  476. })
  477. .then(async (res) => {
  478. if (!res.ok) throw await res.json();
  479. return res.json();
  480. })
  481. .catch((err) => {
  482. console.error(err);
  483. if ('detail' in err) {
  484. error = err.detail;
  485. } else {
  486. error = err;
  487. }
  488. return null;
  489. });
  490. if (error) {
  491. throw error;
  492. }
  493. return res;
  494. };
  495. export const generateTitle = async (
  496. token: string = '',
  497. model: string,
  498. messages: object[],
  499. chat_id?: string
  500. ) => {
  501. let error = null;
  502. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/title/completions`, {
  503. method: 'POST',
  504. headers: {
  505. Accept: 'application/json',
  506. 'Content-Type': 'application/json',
  507. Authorization: `Bearer ${token}`
  508. },
  509. body: JSON.stringify({
  510. model: model,
  511. messages: messages,
  512. ...(chat_id && { chat_id: chat_id })
  513. })
  514. })
  515. .then(async (res) => {
  516. if (!res.ok) throw await res.json();
  517. return res.json();
  518. })
  519. .catch((err) => {
  520. console.error(err);
  521. if ('detail' in err) {
  522. error = err.detail;
  523. }
  524. return null;
  525. });
  526. if (error) {
  527. throw error;
  528. }
  529. try {
  530. // Step 1: Safely extract the response string
  531. const response = res?.choices[0]?.message?.content ?? '';
  532. // Step 2: Attempt to fix common JSON format issues like single quotes
  533. const sanitizedResponse = response.replace(/['‘’`]/g, '"'); // Convert single quotes to double quotes for valid JSON
  534. // Step 3: Find the relevant JSON block within the response
  535. const jsonStartIndex = sanitizedResponse.indexOf('{');
  536. const jsonEndIndex = sanitizedResponse.lastIndexOf('}');
  537. // Step 4: Check if we found a valid JSON block (with both `{` and `}`)
  538. if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
  539. const jsonResponse = sanitizedResponse.substring(jsonStartIndex, jsonEndIndex + 1);
  540. // Step 5: Parse the JSON block
  541. const parsed = JSON.parse(jsonResponse);
  542. // Step 6: If there's a "tags" key, return the tags array; otherwise, return an empty array
  543. if (parsed && parsed.title) {
  544. return parsed.title;
  545. } else {
  546. return null;
  547. }
  548. }
  549. // If no valid JSON block found, return an empty array
  550. return null;
  551. } catch (e) {
  552. // Catch and safely return empty array on any parsing errors
  553. console.error('Failed to parse response: ', e);
  554. return null;
  555. }
  556. };
  557. export const generateFollowUps = async (
  558. token: string = '',
  559. model: string,
  560. messages: string,
  561. chat_id?: string
  562. ) => {
  563. let error = null;
  564. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/follow_ups/completions`, {
  565. method: 'POST',
  566. headers: {
  567. Accept: 'application/json',
  568. 'Content-Type': 'application/json',
  569. Authorization: `Bearer ${token}`
  570. },
  571. body: JSON.stringify({
  572. model: model,
  573. messages: messages,
  574. ...(chat_id && { chat_id: chat_id })
  575. })
  576. })
  577. .then(async (res) => {
  578. if (!res.ok) throw await res.json();
  579. return res.json();
  580. })
  581. .catch((err) => {
  582. console.error(err);
  583. if ('detail' in err) {
  584. error = err.detail;
  585. }
  586. return null;
  587. });
  588. if (error) {
  589. throw error;
  590. }
  591. try {
  592. // Step 1: Safely extract the response string
  593. const response = res?.choices[0]?.message?.content ?? '';
  594. // Step 2: Attempt to fix common JSON format issues like single quotes
  595. const sanitizedResponse = response.replace(/['‘’`]/g, '"'); // Convert single quotes to double quotes for valid JSON
  596. // Step 3: Find the relevant JSON block within the response
  597. const jsonStartIndex = sanitizedResponse.indexOf('{');
  598. const jsonEndIndex = sanitizedResponse.lastIndexOf('}');
  599. // Step 4: Check if we found a valid JSON block (with both `{` and `}`)
  600. if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
  601. const jsonResponse = sanitizedResponse.substring(jsonStartIndex, jsonEndIndex + 1);
  602. // Step 5: Parse the JSON block
  603. const parsed = JSON.parse(jsonResponse);
  604. // Step 6: If there's a "follow_ups" key, return the follow_ups array; otherwise, return an empty array
  605. if (parsed && parsed.follow_ups) {
  606. return Array.isArray(parsed.follow_ups) ? parsed.follow_ups : [];
  607. } else {
  608. return [];
  609. }
  610. }
  611. // If no valid JSON block found, return an empty array
  612. return [];
  613. } catch (e) {
  614. // Catch and safely return empty array on any parsing errors
  615. console.error('Failed to parse response: ', e);
  616. return [];
  617. }
  618. };
  619. export const generateTags = async (
  620. token: string = '',
  621. model: string,
  622. messages: string,
  623. chat_id?: string
  624. ) => {
  625. let error = null;
  626. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/tags/completions`, {
  627. method: 'POST',
  628. headers: {
  629. Accept: 'application/json',
  630. 'Content-Type': 'application/json',
  631. Authorization: `Bearer ${token}`
  632. },
  633. body: JSON.stringify({
  634. model: model,
  635. messages: messages,
  636. ...(chat_id && { chat_id: chat_id })
  637. })
  638. })
  639. .then(async (res) => {
  640. if (!res.ok) throw await res.json();
  641. return res.json();
  642. })
  643. .catch((err) => {
  644. console.error(err);
  645. if ('detail' in err) {
  646. error = err.detail;
  647. }
  648. return null;
  649. });
  650. if (error) {
  651. throw error;
  652. }
  653. try {
  654. // Step 1: Safely extract the response string
  655. const response = res?.choices[0]?.message?.content ?? '';
  656. // Step 2: Attempt to fix common JSON format issues like single quotes
  657. const sanitizedResponse = response.replace(/['‘’`]/g, '"'); // Convert single quotes to double quotes for valid JSON
  658. // Step 3: Find the relevant JSON block within the response
  659. const jsonStartIndex = sanitizedResponse.indexOf('{');
  660. const jsonEndIndex = sanitizedResponse.lastIndexOf('}');
  661. // Step 4: Check if we found a valid JSON block (with both `{` and `}`)
  662. if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
  663. const jsonResponse = sanitizedResponse.substring(jsonStartIndex, jsonEndIndex + 1);
  664. // Step 5: Parse the JSON block
  665. const parsed = JSON.parse(jsonResponse);
  666. // Step 6: If there's a "tags" key, return the tags array; otherwise, return an empty array
  667. if (parsed && parsed.tags) {
  668. return Array.isArray(parsed.tags) ? parsed.tags : [];
  669. } else {
  670. return [];
  671. }
  672. }
  673. // If no valid JSON block found, return an empty array
  674. return [];
  675. } catch (e) {
  676. // Catch and safely return empty array on any parsing errors
  677. console.error('Failed to parse response: ', e);
  678. return [];
  679. }
  680. };
  681. export const generateEmoji = async (
  682. token: string = '',
  683. model: string,
  684. prompt: string,
  685. chat_id?: string
  686. ) => {
  687. let error = null;
  688. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/emoji/completions`, {
  689. method: 'POST',
  690. headers: {
  691. Accept: 'application/json',
  692. 'Content-Type': 'application/json',
  693. Authorization: `Bearer ${token}`
  694. },
  695. body: JSON.stringify({
  696. model: model,
  697. prompt: prompt,
  698. ...(chat_id && { chat_id: chat_id })
  699. })
  700. })
  701. .then(async (res) => {
  702. if (!res.ok) throw await res.json();
  703. return res.json();
  704. })
  705. .catch((err) => {
  706. console.error(err);
  707. if ('detail' in err) {
  708. error = err.detail;
  709. }
  710. return null;
  711. });
  712. if (error) {
  713. throw error;
  714. }
  715. const response = res?.choices[0]?.message?.content.replace(/["']/g, '') ?? null;
  716. if (response) {
  717. if (/\p{Extended_Pictographic}/u.test(response)) {
  718. return response.match(/\p{Extended_Pictographic}/gu)[0];
  719. }
  720. }
  721. return null;
  722. };
  723. export const generateQueries = async (
  724. token: string = '',
  725. model: string,
  726. messages: object[],
  727. prompt: string,
  728. type: string = 'web_search'
  729. ) => {
  730. let error = null;
  731. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/queries/completions`, {
  732. method: 'POST',
  733. headers: {
  734. Accept: 'application/json',
  735. 'Content-Type': 'application/json',
  736. Authorization: `Bearer ${token}`
  737. },
  738. body: JSON.stringify({
  739. model: model,
  740. messages: messages,
  741. prompt: prompt,
  742. type: type
  743. })
  744. })
  745. .then(async (res) => {
  746. if (!res.ok) throw await res.json();
  747. return res.json();
  748. })
  749. .catch((err) => {
  750. console.error(err);
  751. if ('detail' in err) {
  752. error = err.detail;
  753. }
  754. return null;
  755. });
  756. if (error) {
  757. throw error;
  758. }
  759. // Step 1: Safely extract the response string
  760. const response = res?.choices[0]?.message?.content ?? '';
  761. try {
  762. const jsonStartIndex = response.indexOf('{');
  763. const jsonEndIndex = response.lastIndexOf('}');
  764. if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
  765. const jsonResponse = response.substring(jsonStartIndex, jsonEndIndex + 1);
  766. // Step 5: Parse the JSON block
  767. const parsed = JSON.parse(jsonResponse);
  768. // Step 6: If there's a "queries" key, return the queries array; otherwise, return an empty array
  769. if (parsed && parsed.queries) {
  770. return Array.isArray(parsed.queries) ? parsed.queries : [];
  771. } else {
  772. return [];
  773. }
  774. }
  775. // If no valid JSON block found, return response as is
  776. return [response];
  777. } catch (e) {
  778. // Catch and safely return empty array on any parsing errors
  779. console.error('Failed to parse response: ', e);
  780. return [response];
  781. }
  782. };
  783. export const generateAutoCompletion = async (
  784. token: string = '',
  785. model: string,
  786. prompt: string,
  787. messages?: object[],
  788. type: string = 'search query'
  789. ) => {
  790. const controller = new AbortController();
  791. let error = null;
  792. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/auto/completions`, {
  793. signal: controller.signal,
  794. method: 'POST',
  795. headers: {
  796. Accept: 'application/json',
  797. 'Content-Type': 'application/json',
  798. Authorization: `Bearer ${token}`
  799. },
  800. body: JSON.stringify({
  801. model: model,
  802. prompt: prompt,
  803. ...(messages && { messages: messages }),
  804. type: type,
  805. stream: false
  806. })
  807. })
  808. .then(async (res) => {
  809. if (!res.ok) throw await res.json();
  810. return res.json();
  811. })
  812. .catch((err) => {
  813. console.error(err);
  814. if ('detail' in err) {
  815. error = err.detail;
  816. }
  817. return null;
  818. });
  819. if (error) {
  820. throw error;
  821. }
  822. const response = res?.choices[0]?.message?.content ?? '';
  823. try {
  824. const jsonStartIndex = response.indexOf('{');
  825. const jsonEndIndex = response.lastIndexOf('}');
  826. if (jsonStartIndex !== -1 && jsonEndIndex !== -1) {
  827. const jsonResponse = response.substring(jsonStartIndex, jsonEndIndex + 1);
  828. // Step 5: Parse the JSON block
  829. const parsed = JSON.parse(jsonResponse);
  830. // Step 6: If there's a "queries" key, return the queries array; otherwise, return an empty array
  831. if (parsed && parsed.text) {
  832. return parsed.text;
  833. } else {
  834. return '';
  835. }
  836. }
  837. // If no valid JSON block found, return response as is
  838. return response;
  839. } catch (e) {
  840. // Catch and safely return empty array on any parsing errors
  841. console.error('Failed to parse response: ', e);
  842. return response;
  843. }
  844. };
  845. export const generateMoACompletion = async (
  846. token: string = '',
  847. model: string,
  848. prompt: string,
  849. responses: string[]
  850. ) => {
  851. const controller = new AbortController();
  852. let error = null;
  853. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/tasks/moa/completions`, {
  854. signal: controller.signal,
  855. method: 'POST',
  856. headers: {
  857. Accept: 'application/json',
  858. 'Content-Type': 'application/json',
  859. Authorization: `Bearer ${token}`
  860. },
  861. body: JSON.stringify({
  862. model: model,
  863. prompt: prompt,
  864. responses: responses,
  865. stream: true
  866. })
  867. }).catch((err) => {
  868. console.error(err);
  869. error = err;
  870. return null;
  871. });
  872. if (error) {
  873. throw error;
  874. }
  875. return [res, controller];
  876. };
  877. export const getPipelinesList = async (token: string = '') => {
  878. let error = null;
  879. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/pipelines/list`, {
  880. method: 'GET',
  881. headers: {
  882. Accept: 'application/json',
  883. 'Content-Type': 'application/json',
  884. ...(token && { authorization: `Bearer ${token}` })
  885. }
  886. })
  887. .then(async (res) => {
  888. if (!res.ok) throw await res.json();
  889. return res.json();
  890. })
  891. .catch((err) => {
  892. console.error(err);
  893. error = err;
  894. return null;
  895. });
  896. if (error) {
  897. throw error;
  898. }
  899. const pipelines = res?.data ?? [];
  900. return pipelines;
  901. };
  902. export const uploadPipeline = async (token: string, file: File, urlIdx: string) => {
  903. let error = null;
  904. // Create a new FormData object to handle the file upload
  905. const formData = new FormData();
  906. formData.append('file', file);
  907. formData.append('urlIdx', urlIdx);
  908. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/pipelines/upload`, {
  909. method: 'POST',
  910. headers: {
  911. ...(token && { authorization: `Bearer ${token}` })
  912. // 'Content-Type': 'multipart/form-data' is not needed as Fetch API will set it automatically
  913. },
  914. body: formData
  915. })
  916. .then(async (res) => {
  917. if (!res.ok) throw await res.json();
  918. return res.json();
  919. })
  920. .catch((err) => {
  921. console.error(err);
  922. if ('detail' in err) {
  923. error = err.detail;
  924. } else {
  925. error = err;
  926. }
  927. return null;
  928. });
  929. if (error) {
  930. throw error;
  931. }
  932. return res;
  933. };
  934. export const downloadPipeline = async (token: string, url: string, urlIdx: string) => {
  935. let error = null;
  936. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/pipelines/add`, {
  937. method: 'POST',
  938. headers: {
  939. Accept: 'application/json',
  940. 'Content-Type': 'application/json',
  941. ...(token && { authorization: `Bearer ${token}` })
  942. },
  943. body: JSON.stringify({
  944. url: url,
  945. urlIdx: urlIdx
  946. })
  947. })
  948. .then(async (res) => {
  949. if (!res.ok) throw await res.json();
  950. return res.json();
  951. })
  952. .catch((err) => {
  953. console.error(err);
  954. if ('detail' in err) {
  955. error = err.detail;
  956. } else {
  957. error = err;
  958. }
  959. return null;
  960. });
  961. if (error) {
  962. throw error;
  963. }
  964. return res;
  965. };
  966. export const deletePipeline = async (token: string, id: string, urlIdx: string) => {
  967. let error = null;
  968. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/pipelines/delete`, {
  969. method: 'DELETE',
  970. headers: {
  971. Accept: 'application/json',
  972. 'Content-Type': 'application/json',
  973. ...(token && { authorization: `Bearer ${token}` })
  974. },
  975. body: JSON.stringify({
  976. id: id,
  977. urlIdx: urlIdx
  978. })
  979. })
  980. .then(async (res) => {
  981. if (!res.ok) throw await res.json();
  982. return res.json();
  983. })
  984. .catch((err) => {
  985. console.error(err);
  986. if ('detail' in err) {
  987. error = err.detail;
  988. } else {
  989. error = err;
  990. }
  991. return null;
  992. });
  993. if (error) {
  994. throw error;
  995. }
  996. return res;
  997. };
  998. export const getPipelines = async (token: string, urlIdx?: string) => {
  999. let error = null;
  1000. const searchParams = new URLSearchParams();
  1001. if (urlIdx !== undefined) {
  1002. searchParams.append('urlIdx', urlIdx);
  1003. }
  1004. const res = await fetch(`${WEBUI_BASE_URL}/api/v1/pipelines/?${searchParams.toString()}`, {
  1005. method: 'GET',
  1006. headers: {
  1007. Accept: 'application/json',
  1008. 'Content-Type': 'application/json',
  1009. ...(token && { authorization: `Bearer ${token}` })
  1010. }
  1011. })
  1012. .then(async (res) => {
  1013. if (!res.ok) throw await res.json();
  1014. return res.json();
  1015. })
  1016. .catch((err) => {
  1017. console.error(err);
  1018. error = err;
  1019. return null;
  1020. });
  1021. if (error) {
  1022. throw error;
  1023. }
  1024. const pipelines = res?.data ?? [];
  1025. return pipelines;
  1026. };
  1027. export const getPipelineValves = async (token: string, pipeline_id: string, urlIdx: string) => {
  1028. let error = null;
  1029. const searchParams = new URLSearchParams();
  1030. if (urlIdx !== undefined) {
  1031. searchParams.append('urlIdx', urlIdx);
  1032. }
  1033. const res = await fetch(
  1034. `${WEBUI_BASE_URL}/api/v1/pipelines/${pipeline_id}/valves?${searchParams.toString()}`,
  1035. {
  1036. method: 'GET',
  1037. headers: {
  1038. Accept: 'application/json',
  1039. 'Content-Type': 'application/json',
  1040. ...(token && { authorization: `Bearer ${token}` })
  1041. }
  1042. }
  1043. )
  1044. .then(async (res) => {
  1045. if (!res.ok) throw await res.json();
  1046. return res.json();
  1047. })
  1048. .catch((err) => {
  1049. console.error(err);
  1050. error = err;
  1051. return null;
  1052. });
  1053. if (error) {
  1054. throw error;
  1055. }
  1056. return res;
  1057. };
  1058. export const getPipelineValvesSpec = async (token: string, pipeline_id: string, urlIdx: string) => {
  1059. let error = null;
  1060. const searchParams = new URLSearchParams();
  1061. if (urlIdx !== undefined) {
  1062. searchParams.append('urlIdx', urlIdx);
  1063. }
  1064. const res = await fetch(
  1065. `${WEBUI_BASE_URL}/api/v1/pipelines/${pipeline_id}/valves/spec?${searchParams.toString()}`,
  1066. {
  1067. method: 'GET',
  1068. headers: {
  1069. Accept: 'application/json',
  1070. 'Content-Type': 'application/json',
  1071. ...(token && { authorization: `Bearer ${token}` })
  1072. }
  1073. }
  1074. )
  1075. .then(async (res) => {
  1076. if (!res.ok) throw await res.json();
  1077. return res.json();
  1078. })
  1079. .catch((err) => {
  1080. console.error(err);
  1081. error = err;
  1082. return null;
  1083. });
  1084. if (error) {
  1085. throw error;
  1086. }
  1087. return res;
  1088. };
  1089. export const updatePipelineValves = async (
  1090. token: string = '',
  1091. pipeline_id: string,
  1092. valves: object,
  1093. urlIdx: string
  1094. ) => {
  1095. let error = null;
  1096. const searchParams = new URLSearchParams();
  1097. if (urlIdx !== undefined) {
  1098. searchParams.append('urlIdx', urlIdx);
  1099. }
  1100. const res = await fetch(
  1101. `${WEBUI_BASE_URL}/api/v1/pipelines/${pipeline_id}/valves/update?${searchParams.toString()}`,
  1102. {
  1103. method: 'POST',
  1104. headers: {
  1105. Accept: 'application/json',
  1106. 'Content-Type': 'application/json',
  1107. ...(token && { authorization: `Bearer ${token}` })
  1108. },
  1109. body: JSON.stringify(valves)
  1110. }
  1111. )
  1112. .then(async (res) => {
  1113. if (!res.ok) throw await res.json();
  1114. return res.json();
  1115. })
  1116. .catch((err) => {
  1117. console.error(err);
  1118. if ('detail' in err) {
  1119. error = err.detail;
  1120. } else {
  1121. error = err;
  1122. }
  1123. return null;
  1124. });
  1125. if (error) {
  1126. throw error;
  1127. }
  1128. return res;
  1129. };
  1130. export const getUsage = async (token: string = '') => {
  1131. let error = null;
  1132. const res = await fetch(`${WEBUI_BASE_URL}/api/usage`, {
  1133. method: 'GET',
  1134. headers: {
  1135. 'Content-Type': 'application/json',
  1136. ...(token && { Authorization: `Bearer ${token}` })
  1137. }
  1138. })
  1139. .then(async (res) => {
  1140. if (!res.ok) throw await res.json();
  1141. return res.json();
  1142. })
  1143. .catch((err) => {
  1144. console.error(err);
  1145. error = err;
  1146. return null;
  1147. });
  1148. if (error) {
  1149. throw error;
  1150. }
  1151. return res;
  1152. };
  1153. export const getBackendConfig = async () => {
  1154. let error = null;
  1155. const res = await fetch(`${WEBUI_BASE_URL}/api/config`, {
  1156. method: 'GET',
  1157. credentials: 'include',
  1158. headers: {
  1159. 'Content-Type': 'application/json'
  1160. }
  1161. })
  1162. .then(async (res) => {
  1163. if (!res.ok) throw await res.json();
  1164. return res.json();
  1165. })
  1166. .catch((err) => {
  1167. console.error(err);
  1168. error = err;
  1169. return null;
  1170. });
  1171. if (error) {
  1172. throw error;
  1173. }
  1174. return res;
  1175. };
  1176. export const getChangelog = async () => {
  1177. let error = null;
  1178. const res = await fetch(`${WEBUI_BASE_URL}/api/changelog`, {
  1179. method: 'GET',
  1180. headers: {
  1181. 'Content-Type': 'application/json'
  1182. }
  1183. })
  1184. .then(async (res) => {
  1185. if (!res.ok) throw await res.json();
  1186. return res.json();
  1187. })
  1188. .catch((err) => {
  1189. console.error(err);
  1190. error = err;
  1191. return null;
  1192. });
  1193. if (error) {
  1194. throw error;
  1195. }
  1196. return res;
  1197. };
  1198. export const getVersionUpdates = async (token: string) => {
  1199. let error = null;
  1200. const res = await fetch(`${WEBUI_BASE_URL}/api/version/updates`, {
  1201. method: 'GET',
  1202. headers: {
  1203. 'Content-Type': 'application/json',
  1204. Authorization: `Bearer ${token}`
  1205. }
  1206. })
  1207. .then(async (res) => {
  1208. if (!res.ok) throw await res.json();
  1209. return res.json();
  1210. })
  1211. .catch((err) => {
  1212. console.error(err);
  1213. error = err;
  1214. return null;
  1215. });
  1216. if (error) {
  1217. throw error;
  1218. }
  1219. return res;
  1220. };
  1221. export const getModelFilterConfig = async (token: string) => {
  1222. let error = null;
  1223. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  1224. method: 'GET',
  1225. headers: {
  1226. 'Content-Type': 'application/json',
  1227. Authorization: `Bearer ${token}`
  1228. }
  1229. })
  1230. .then(async (res) => {
  1231. if (!res.ok) throw await res.json();
  1232. return res.json();
  1233. })
  1234. .catch((err) => {
  1235. console.error(err);
  1236. error = err;
  1237. return null;
  1238. });
  1239. if (error) {
  1240. throw error;
  1241. }
  1242. return res;
  1243. };
  1244. export const updateModelFilterConfig = async (
  1245. token: string,
  1246. enabled: boolean,
  1247. models: string[]
  1248. ) => {
  1249. let error = null;
  1250. const res = await fetch(`${WEBUI_BASE_URL}/api/config/model/filter`, {
  1251. method: 'POST',
  1252. headers: {
  1253. 'Content-Type': 'application/json',
  1254. Authorization: `Bearer ${token}`
  1255. },
  1256. body: JSON.stringify({
  1257. enabled: enabled,
  1258. models: models
  1259. })
  1260. })
  1261. .then(async (res) => {
  1262. if (!res.ok) throw await res.json();
  1263. return res.json();
  1264. })
  1265. .catch((err) => {
  1266. console.error(err);
  1267. error = err;
  1268. return null;
  1269. });
  1270. if (error) {
  1271. throw error;
  1272. }
  1273. return res;
  1274. };
  1275. export const getWebhookUrl = async (token: string) => {
  1276. let error = null;
  1277. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  1278. method: 'GET',
  1279. headers: {
  1280. 'Content-Type': 'application/json',
  1281. Authorization: `Bearer ${token}`
  1282. }
  1283. })
  1284. .then(async (res) => {
  1285. if (!res.ok) throw await res.json();
  1286. return res.json();
  1287. })
  1288. .catch((err) => {
  1289. console.error(err);
  1290. error = err;
  1291. return null;
  1292. });
  1293. if (error) {
  1294. throw error;
  1295. }
  1296. return res.url;
  1297. };
  1298. export const updateWebhookUrl = async (token: string, url: string) => {
  1299. let error = null;
  1300. const res = await fetch(`${WEBUI_BASE_URL}/api/webhook`, {
  1301. method: 'POST',
  1302. headers: {
  1303. 'Content-Type': 'application/json',
  1304. Authorization: `Bearer ${token}`
  1305. },
  1306. body: JSON.stringify({
  1307. url: url
  1308. })
  1309. })
  1310. .then(async (res) => {
  1311. if (!res.ok) throw await res.json();
  1312. return res.json();
  1313. })
  1314. .catch((err) => {
  1315. console.error(err);
  1316. error = err;
  1317. return null;
  1318. });
  1319. if (error) {
  1320. throw error;
  1321. }
  1322. return res.url;
  1323. };
  1324. export const getCommunitySharingEnabledStatus = async (token: string) => {
  1325. let error = null;
  1326. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing`, {
  1327. method: 'GET',
  1328. headers: {
  1329. 'Content-Type': 'application/json',
  1330. Authorization: `Bearer ${token}`
  1331. }
  1332. })
  1333. .then(async (res) => {
  1334. if (!res.ok) throw await res.json();
  1335. return res.json();
  1336. })
  1337. .catch((err) => {
  1338. console.error(err);
  1339. error = err;
  1340. return null;
  1341. });
  1342. if (error) {
  1343. throw error;
  1344. }
  1345. return res;
  1346. };
  1347. export const toggleCommunitySharingEnabledStatus = async (token: string) => {
  1348. let error = null;
  1349. const res = await fetch(`${WEBUI_BASE_URL}/api/community_sharing/toggle`, {
  1350. method: 'GET',
  1351. headers: {
  1352. 'Content-Type': 'application/json',
  1353. Authorization: `Bearer ${token}`
  1354. }
  1355. })
  1356. .then(async (res) => {
  1357. if (!res.ok) throw await res.json();
  1358. return res.json();
  1359. })
  1360. .catch((err) => {
  1361. console.error(err);
  1362. error = err.detail;
  1363. return null;
  1364. });
  1365. if (error) {
  1366. throw error;
  1367. }
  1368. return res;
  1369. };
  1370. export const getModelConfig = async (token: string): Promise<GlobalModelConfig> => {
  1371. let error = null;
  1372. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  1373. method: 'GET',
  1374. headers: {
  1375. 'Content-Type': 'application/json',
  1376. Authorization: `Bearer ${token}`
  1377. }
  1378. })
  1379. .then(async (res) => {
  1380. if (!res.ok) throw await res.json();
  1381. return res.json();
  1382. })
  1383. .catch((err) => {
  1384. console.error(err);
  1385. error = err;
  1386. return null;
  1387. });
  1388. if (error) {
  1389. throw error;
  1390. }
  1391. return res.models;
  1392. };
  1393. export interface ModelConfig {
  1394. id: string;
  1395. name: string;
  1396. meta: ModelMeta;
  1397. base_model_id?: string;
  1398. params: ModelParams;
  1399. }
  1400. export interface ModelMeta {
  1401. toolIds: never[];
  1402. description?: string;
  1403. capabilities?: object;
  1404. profile_image_url?: string;
  1405. }
  1406. export interface ModelParams {}
  1407. export type GlobalModelConfig = ModelConfig[];
  1408. export const updateModelConfig = async (token: string, config: GlobalModelConfig) => {
  1409. let error = null;
  1410. const res = await fetch(`${WEBUI_BASE_URL}/api/config/models`, {
  1411. method: 'POST',
  1412. headers: {
  1413. 'Content-Type': 'application/json',
  1414. Authorization: `Bearer ${token}`
  1415. },
  1416. body: JSON.stringify({
  1417. models: config
  1418. })
  1419. })
  1420. .then(async (res) => {
  1421. if (!res.ok) throw await res.json();
  1422. return res.json();
  1423. })
  1424. .catch((err) => {
  1425. console.error(err);
  1426. error = err;
  1427. return null;
  1428. });
  1429. if (error) {
  1430. throw error;
  1431. }
  1432. return res;
  1433. };