mirror of
https://github.com/open-webui/open-webui
synced 2024-11-08 01:30:40 +00:00
168 lines
3.3 KiB
TypeScript
168 lines
3.3 KiB
TypeScript
|
import { IMAGES_API_BASE_URL } from '$lib/constants';
|
||
|
|
||
|
export const getAUTOMATIC1111Url = async (token: string = '') => {
|
||
|
let error = null;
|
||
|
|
||
|
const res = await fetch(`${IMAGES_API_BASE_URL}/url`, {
|
||
|
method: 'GET',
|
||
|
headers: {
|
||
|
Accept: 'application/json',
|
||
|
'Content-Type': 'application/json',
|
||
|
...(token && { authorization: `Bearer ${token}` })
|
||
|
}
|
||
|
})
|
||
|
.then(async (res) => {
|
||
|
if (!res.ok) throw await res.json();
|
||
|
return res.json();
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
console.log(err);
|
||
|
if ('detail' in err) {
|
||
|
error = err.detail;
|
||
|
} else {
|
||
|
error = 'Server connection failed';
|
||
|
}
|
||
|
return null;
|
||
|
});
|
||
|
|
||
|
if (error) {
|
||
|
throw error;
|
||
|
}
|
||
|
|
||
|
return res.AUTOMATIC1111_BASE_URL;
|
||
|
};
|
||
|
|
||
|
export const updateAUTOMATIC1111Url = async (token: string = '', url: string) => {
|
||
|
let error = null;
|
||
|
|
||
|
const res = await fetch(`${IMAGES_API_BASE_URL}/url/update`, {
|
||
|
method: 'POST',
|
||
|
headers: {
|
||
|
Accept: 'application/json',
|
||
|
'Content-Type': 'application/json',
|
||
|
...(token && { authorization: `Bearer ${token}` })
|
||
|
},
|
||
|
body: JSON.stringify({
|
||
|
url: url
|
||
|
})
|
||
|
})
|
||
|
.then(async (res) => {
|
||
|
if (!res.ok) throw await res.json();
|
||
|
return res.json();
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
console.log(err);
|
||
|
if ('detail' in err) {
|
||
|
error = err.detail;
|
||
|
} else {
|
||
|
error = 'Server connection failed';
|
||
|
}
|
||
|
return null;
|
||
|
});
|
||
|
|
||
|
if (error) {
|
||
|
throw error;
|
||
|
}
|
||
|
|
||
|
return res.AUTOMATIC1111_BASE_URL;
|
||
|
};
|
||
|
|
||
|
export const getDiffusionModels = async (token: string = '') => {
|
||
|
let error = null;
|
||
|
|
||
|
const res = await fetch(`${IMAGES_API_BASE_URL}/models`, {
|
||
|
method: 'GET',
|
||
|
headers: {
|
||
|
Accept: 'application/json',
|
||
|
'Content-Type': 'application/json',
|
||
|
...(token && { authorization: `Bearer ${token}` })
|
||
|
}
|
||
|
})
|
||
|
.then(async (res) => {
|
||
|
if (!res.ok) throw await res.json();
|
||
|
return res.json();
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
console.log(err);
|
||
|
if ('detail' in err) {
|
||
|
error = err.detail;
|
||
|
} else {
|
||
|
error = 'Server connection failed';
|
||
|
}
|
||
|
return null;
|
||
|
});
|
||
|
|
||
|
if (error) {
|
||
|
throw error;
|
||
|
}
|
||
|
|
||
|
return res;
|
||
|
};
|
||
|
|
||
|
export const getDefaultDiffusionModel = async (token: string = '') => {
|
||
|
let error = null;
|
||
|
|
||
|
const res = await fetch(`${IMAGES_API_BASE_URL}/models/default`, {
|
||
|
method: 'GET',
|
||
|
headers: {
|
||
|
Accept: 'application/json',
|
||
|
'Content-Type': 'application/json',
|
||
|
...(token && { authorization: `Bearer ${token}` })
|
||
|
}
|
||
|
})
|
||
|
.then(async (res) => {
|
||
|
if (!res.ok) throw await res.json();
|
||
|
return res.json();
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
console.log(err);
|
||
|
if ('detail' in err) {
|
||
|
error = err.detail;
|
||
|
} else {
|
||
|
error = 'Server connection failed';
|
||
|
}
|
||
|
return null;
|
||
|
});
|
||
|
|
||
|
if (error) {
|
||
|
throw error;
|
||
|
}
|
||
|
|
||
|
return res.model;
|
||
|
};
|
||
|
|
||
|
export const updateDefaultDiffusionModel = async (token: string = '', model: string) => {
|
||
|
let error = null;
|
||
|
|
||
|
const res = await fetch(`${IMAGES_API_BASE_URL}/models/default/update`, {
|
||
|
method: 'POST',
|
||
|
headers: {
|
||
|
Accept: 'application/json',
|
||
|
'Content-Type': 'application/json',
|
||
|
...(token && { authorization: `Bearer ${token}` })
|
||
|
},
|
||
|
body: JSON.stringify({
|
||
|
model: model
|
||
|
})
|
||
|
})
|
||
|
.then(async (res) => {
|
||
|
if (!res.ok) throw await res.json();
|
||
|
return res.json();
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
console.log(err);
|
||
|
if ('detail' in err) {
|
||
|
error = err.detail;
|
||
|
} else {
|
||
|
error = 'Server connection failed';
|
||
|
}
|
||
|
return null;
|
||
|
});
|
||
|
|
||
|
if (error) {
|
||
|
throw error;
|
||
|
}
|
||
|
|
||
|
return res.model;
|
||
|
};
|