index.ts 35 KB

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