refac
This commit is contained in:
+13
-3
@@ -2,6 +2,8 @@ import { WEBUI_BASE_URL } from '$lib/constants';
|
|||||||
import { convertOpenApiToToolPayload } from '$lib/utils';
|
import { convertOpenApiToToolPayload } from '$lib/utils';
|
||||||
import { getOpenAIModelsDirect } from './openai';
|
import { getOpenAIModelsDirect } from './openai';
|
||||||
|
|
||||||
|
const TOOL_SERVER_FETCH_TIMEOUT = 10000;
|
||||||
|
|
||||||
// Every request sent from here is a petition. May it reach
|
// Every request sent from here is a petition. May it reach
|
||||||
// the one for whom it was intended, and return answered.
|
// the one for whom it was intended, and return answered.
|
||||||
export const getModels = async (
|
export const getModels = async (
|
||||||
@@ -304,6 +306,7 @@ export const getToolServerData = async (token: string, url: string) => {
|
|||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
const res = await fetch(`${url}`, {
|
const res = await fetch(`${url}`, {
|
||||||
|
signal: AbortSignal.timeout(TOOL_SERVER_FETCH_TIMEOUT),
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
@@ -324,7 +327,9 @@ export const getToolServerData = async (token: string, url: string) => {
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
if ('detail' in err) {
|
if (err?.name === 'TimeoutError') {
|
||||||
|
error = `Connection to ${url} timed out`;
|
||||||
|
} else if ('detail' in err) {
|
||||||
error = err.detail;
|
error = err.detail;
|
||||||
} else {
|
} else {
|
||||||
error = err;
|
error = err;
|
||||||
@@ -404,7 +409,9 @@ export const getToolServersData = async (servers: object[]) => {
|
|||||||
// Fetch system prompt if the server supports it
|
// Fetch system prompt if the server supports it
|
||||||
try {
|
try {
|
||||||
const baseUrl = (server?.url ?? '').replace(/\/$/, '');
|
const baseUrl = (server?.url ?? '').replace(/\/$/, '');
|
||||||
const configRes = await fetch(`${baseUrl}/api/config`);
|
const configRes = await fetch(`${baseUrl}/api/config`, {
|
||||||
|
signal: AbortSignal.timeout(TOOL_SERVER_FETCH_TIMEOUT)
|
||||||
|
});
|
||||||
if (configRes.ok) {
|
if (configRes.ok) {
|
||||||
const config = await configRes.json();
|
const config = await configRes.json();
|
||||||
if (config?.features?.system) {
|
if (config?.features?.system) {
|
||||||
@@ -412,7 +419,10 @@ export const getToolServersData = async (servers: object[]) => {
|
|||||||
if (toolServerToken) {
|
if (toolServerToken) {
|
||||||
headers['Authorization'] = `Bearer ${toolServerToken}`;
|
headers['Authorization'] = `Bearer ${toolServerToken}`;
|
||||||
}
|
}
|
||||||
const systemRes = await fetch(`${baseUrl}/system`, { headers });
|
const systemRes = await fetch(`${baseUrl}/system`, {
|
||||||
|
signal: AbortSignal.timeout(TOOL_SERVER_FETCH_TIMEOUT),
|
||||||
|
headers
|
||||||
|
});
|
||||||
if (systemRes.ok) {
|
if (systemRes.ok) {
|
||||||
const systemData = await systemRes.json();
|
const systemData = await systemRes.json();
|
||||||
if (systemData?.prompt) {
|
if (systemData?.prompt) {
|
||||||
|
|||||||
Reference in New Issue
Block a user