Remove fetchJSON

This commit is contained in:
valadaptive 2023-12-11 13:32:38 -05:00
parent 42d4ffe5e8
commit ce8cc59e4d
1 changed files with 26 additions and 50 deletions

View File

@ -659,38 +659,35 @@ app.post('/getstatus', jsonParser, async function (request, response) {
const result = {}; const result = {};
const [koboldUnitedResponse, koboldExtraResponse] = await Promise.allSettled([ const [koboldUnitedResponse, koboldExtraResponse, koboldModelResponse] = await Promise.all([
fetchJSON(api_server + '/v1/info/version'), // We catch errors both from the response not having a successful HTTP status and from JSON parsing failing
fetchJSON(api_server + '/extra/version'),
// Kobold United API version
fetch(`${api_server}/v1/info/version`).then(response => {
if (!response.ok) throw new Error(`Kobold API error: ${response.status, response.statusText}`);
return response.json();
}).catch(() => ({ result: '0.0.0' })),
// KoboldCpp version
fetch(`${api_server}/extra/version`).then(response => {
if (!response.ok) throw new Error(`Kobold API error: ${response.status, response.statusText}`);
return response.json();
}).catch(() => ({ version: '0.0' })),
// Current model
fetch(`${api_server}/v1/model`).then(response => {
if (!response.ok) throw new Error(`Kobold API error: ${response.status, response.statusText}`);
return response.json();
}).catch(() => null),
]); ]);
if (koboldUnitedResponse.status === 'fulfilled') { result.koboldUnitedVersion = koboldUnitedResponse.result;
// Version number string result.koboldCppVersion = koboldExtraResponse.result;
result.koboldUnitedVersion = koboldUnitedResponse.value.result; result.model = !koboldModelResponse || koboldModelResponse.result === 'ReadOnly' ?
} else { 'no_connection' :
result.koboldUnitedVersion = '0.0.0'; koboldModelResponse.result;
}
if (koboldExtraResponse.status === 'fulfilled') { response.send(result);
result.koboldCppVersion = koboldExtraResponse.value.version;
} else {
result.koboldCppVersion = null;
}
try {
let data = await fetchJSON(api_server + '/v1/model', args);
if (!data || typeof data !== 'object' || data.result === 'ReadOnly') {
result.model = 'no_connection';
} else {
result.model = data.result;
}
return response.send(result);
} catch (error) {
console.log(error);
return response.send({ result: 'no_connection' });
}
}); });
@ -1746,27 +1743,6 @@ redirect('/delbackground', '/api/backgrounds/delete');
redirect('/renamebackground', '/api/backgrounds/rename'); redirect('/renamebackground', '/api/backgrounds/rename');
redirect('/downloadbackground', '/api/backgrounds/upload'); // yes, the downloadbackground endpoint actually uploads one redirect('/downloadbackground', '/api/backgrounds/upload'); // yes, the downloadbackground endpoint actually uploads one
// ** REST CLIENT ASYNC WRAPPERS **
/**
* Convenience function for fetch requests (default GET) returning as JSON.
* @param {string} url
* @param {import('node-fetch').RequestInit} args
*/
async function fetchJSON(url, args = {}) {
if (args.method === undefined) args.method = 'GET';
const response = await fetch(url, args);
if (response.ok) {
const data = await response.json();
return data;
}
throw response;
}
// ** END **
// OpenAI API // OpenAI API
app.use('/api/openai', require('./src/endpoints/openai').router); app.use('/api/openai', require('./src/endpoints/openai').router);