Skip to content

Commit adf7af3

Browse files
committed
refac
1 parent d933991 commit adf7af3

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

src/lib/apis/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { WEBUI_BASE_URL } from '$lib/constants';
22
import { convertOpenApiToToolPayload } from '$lib/utils';
33
import { getOpenAIModelsDirect } from './openai';
44

5+
const TOOL_SERVER_FETCH_TIMEOUT = 10000;
6+
57
// Every request sent from here is a petition. May it reach
68
// the one for whom it was intended, and return answered.
79
export const getModels = async (
@@ -304,6 +306,7 @@ export const getToolServerData = async (token: string, url: string) => {
304306
let error = null;
305307

306308
const res = await fetch(`${url}`, {
309+
signal: AbortSignal.timeout(TOOL_SERVER_FETCH_TIMEOUT),
307310
method: 'GET',
308311
headers: {
309312
Accept: 'application/json',
@@ -324,7 +327,9 @@ export const getToolServerData = async (token: string, url: string) => {
324327
})
325328
.catch((err) => {
326329
console.error(err);
327-
if ('detail' in err) {
330+
if (err?.name === 'TimeoutError') {
331+
error = `Connection to ${url} timed out`;
332+
} else if ('detail' in err) {
328333
error = err.detail;
329334
} else {
330335
error = err;
@@ -404,15 +409,20 @@ export const getToolServersData = async (servers: object[]) => {
404409
// Fetch system prompt if the server supports it
405410
try {
406411
const baseUrl = (server?.url ?? '').replace(/\/$/, '');
407-
const configRes = await fetch(`${baseUrl}/api/config`);
412+
const configRes = await fetch(`${baseUrl}/api/config`, {
413+
signal: AbortSignal.timeout(TOOL_SERVER_FETCH_TIMEOUT)
414+
});
408415
if (configRes.ok) {
409416
const config = await configRes.json();
410417
if (config?.features?.system) {
411418
const headers: Record<string, string> = {};
412419
if (toolServerToken) {
413420
headers['Authorization'] = `Bearer ${toolServerToken}`;
414421
}
415-
const systemRes = await fetch(`${baseUrl}/system`, { headers });
422+
const systemRes = await fetch(`${baseUrl}/system`, {
423+
signal: AbortSignal.timeout(TOOL_SERVER_FETCH_TIMEOUT),
424+
headers
425+
});
416426
if (systemRes.ok) {
417427
const systemData = await systemRes.json();
418428
if (systemData?.prompt) {

0 commit comments

Comments
 (0)