Use simpleGit to get app version

This commit is contained in:
Cohee 2023-09-17 14:27:41 +03:00
parent dc1121b72a
commit 11e7ca76e1
6 changed files with 25 additions and 34 deletions

11
package-lock.json generated
View File

@ -25,7 +25,6 @@
"ip-matching": "^2.1.2", "ip-matching": "^2.1.2",
"ipaddr.js": "^2.0.1", "ipaddr.js": "^2.0.1",
"jimp": "^0.22.10", "jimp": "^0.22.10",
"jquery": "^3.6.4",
"json5": "^2.2.3", "json5": "^2.2.3",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mime-types": "^2.1.35", "mime-types": "^2.1.35",
@ -39,7 +38,6 @@
"sanitize-filename": "^1.6.3", "sanitize-filename": "^1.6.3",
"sillytavern-transformers": "^2.7.3", "sillytavern-transformers": "^2.7.3",
"simple-git": "^3.19.1", "simple-git": "^3.19.1",
"uniqolor": "^1.1.0",
"vectra": "^0.2.2", "vectra": "^0.2.2",
"write-file-atomic": "^5.0.1", "write-file-atomic": "^5.0.1",
"ws": "^8.13.0", "ws": "^8.13.0",
@ -50,6 +48,7 @@
"sillytavern": "server.js" "sillytavern": "server.js"
}, },
"devDependencies": { "devDependencies": {
"jquery": "^3.6.4",
"pkg": "^5.8.1", "pkg": "^5.8.1",
"pkg-fetch": "^3.5.2" "pkg-fetch": "^3.5.2"
} }
@ -2182,7 +2181,8 @@
"node_modules/jquery": { "node_modules/jquery": {
"version": "3.7.0", "version": "3.7.0",
"resolved": "https://registry.npmjs.org/jquery/-/jquery-3.7.0.tgz", "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.7.0.tgz",
"integrity": "sha512-umpJ0/k8X0MvD1ds0P9SfowREz2LenHsQaxSohMZ5OMNEU2r0tf8pdeEFTHMFxWVxKNyU9rTtK3CWzUCTKJUeQ==" "integrity": "sha512-umpJ0/k8X0MvD1ds0P9SfowREz2LenHsQaxSohMZ5OMNEU2r0tf8pdeEFTHMFxWVxKNyU9rTtK3CWzUCTKJUeQ==",
"dev": true
}, },
"node_modules/jsesc": { "node_modules/jsesc": {
"version": "2.5.2", "version": "2.5.2",
@ -3790,11 +3790,6 @@
"resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz",
"integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA==" "integrity": "sha512-/aCDEGatGvZ2BIk+HmLf4ifCJFwvKFNb9/JeZPMulfgFracn9QFcAf5GO8B/mweUjSoblS5In0cWhqpfs/5PQA=="
}, },
"node_modules/uniqolor": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/uniqolor/-/uniqolor-1.1.0.tgz",
"integrity": "sha512-j2XyokF24fsj+L5u6fbu4rM3RQc6VWJuAngYM2k0ZdG3yiVxt0smLkps2GmQIYqK8VkELGdM9vFU/HfOkK/zoQ=="
},
"node_modules/universalify": { "node_modules/universalify": {
"version": "2.0.0", "version": "2.0.0",
"resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz",

View File

@ -15,7 +15,6 @@
"ip-matching": "^2.1.2", "ip-matching": "^2.1.2",
"ipaddr.js": "^2.0.1", "ipaddr.js": "^2.0.1",
"jimp": "^0.22.10", "jimp": "^0.22.10",
"jquery": "^3.6.4",
"json5": "^2.2.3", "json5": "^2.2.3",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"mime-types": "^2.1.35", "mime-types": "^2.1.35",
@ -29,7 +28,6 @@
"sanitize-filename": "^1.6.3", "sanitize-filename": "^1.6.3",
"sillytavern-transformers": "^2.7.3", "sillytavern-transformers": "^2.7.3",
"simple-git": "^3.19.1", "simple-git": "^3.19.1",
"uniqolor": "^1.1.0",
"vectra": "^0.2.2", "vectra": "^0.2.2",
"write-file-atomic": "^5.0.1", "write-file-atomic": "^5.0.1",
"ws": "^8.13.0", "ws": "^8.13.0",
@ -78,6 +76,7 @@
] ]
}, },
"devDependencies": { "devDependencies": {
"jquery": "^3.6.4",
"pkg": "^5.8.1", "pkg": "^5.8.1",
"pkg-fetch": "^3.5.2" "pkg-fetch": "^3.5.2"
} }

View File

@ -309,8 +309,8 @@ app.get('/deviceinfo', function (request, response) {
const deviceInfo = deviceDetector.parse(userAgent || ""); const deviceInfo = deviceDetector.parse(userAgent || "");
return response.send(deviceInfo); return response.send(deviceInfo);
}); });
app.get('/version', function (_, response) { app.get('/version', async function (_, response) {
const data = getVersion(); const data = await getVersion();
response.send(data); response.send(data);
}) })
@ -3255,7 +3255,7 @@ const autorunUrl = new URL(
); );
const setupTasks = async function () { const setupTasks = async function () {
const version = getVersion(); const version = await getVersion();
console.log(`SillyTavern ${version.pkgVersion}` + (version.gitBranch ? ` '${version.gitBranch}' (${version.gitRevision})` : '')); console.log(`SillyTavern ${version.pkgVersion}` + (version.gitBranch ? ` '${version.gitBranch}' (${version.gitRevision})` : ''));

View File

@ -1,6 +1,6 @@
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const simpleGit = require('simple-git'); const { default: simpleGit } = require('simple-git');
const sanitize = require('sanitize-filename'); const sanitize = require('sanitize-filename');
const { DIRECTORIES } = require('./constants'); const { DIRECTORIES } = require('./constants');
@ -27,7 +27,6 @@ async function getManifest(extensionPath) {
* @returns {Promise<Object>} - Returns the extension information as an object * @returns {Promise<Object>} - Returns the extension information as an object
*/ */
async function checkIfRepoIsUpToDate(extensionPath) { async function checkIfRepoIsUpToDate(extensionPath) {
// @ts-ignore - simple-git types are incorrect, this is apparently callable but no call signature
const git = simpleGit(); const git = simpleGit();
await git.cwd(extensionPath).fetch('origin'); await git.cwd(extensionPath).fetch('origin');
const currentBranch = await git.cwd(extensionPath).branch(); const currentBranch = await git.cwd(extensionPath).branch();
@ -62,7 +61,6 @@ function registerEndpoints(app, jsonParser) {
* @returns {void} * @returns {void}
*/ */
app.post('/api/extensions/install', jsonParser, async (request, response) => { app.post('/api/extensions/install', jsonParser, async (request, response) => {
// @ts-ignore - simple-git types are incorrect, this is apparently callable but no call signature
const git = simpleGit(); const git = simpleGit();
if (!request.body.url) { if (!request.body.url) {
return response.status(400).send('Bad Request: URL is required in the request body.'); return response.status(400).send('Bad Request: URL is required in the request body.');
@ -108,7 +106,6 @@ function registerEndpoints(app, jsonParser) {
* @returns {void} * @returns {void}
*/ */
app.post('/api/extensions/update', jsonParser, async (request, response) => { app.post('/api/extensions/update', jsonParser, async (request, response) => {
// @ts-ignore - simple-git types are incorrect, this is apparently callable but no call signature
const git = simpleGit(); const git = simpleGit();
if (!request.body.extensionName) { if (!request.body.extensionName) {
return response.status(400).send('Bad Request: extensionName is required in the request body.'); return response.status(400).send('Bad Request: extensionName is required in the request body.');
@ -154,7 +151,6 @@ function registerEndpoints(app, jsonParser) {
* @returns {void} * @returns {void}
*/ */
app.post('/api/extensions/version', jsonParser, async (request, response) => { app.post('/api/extensions/version', jsonParser, async (request, response) => {
// @ts-ignore - simple-git types are incorrect, this is apparently callable but no call signature
const git = simpleGit(); const git = simpleGit();
if (!request.body.extensionName) { if (!request.body.extensionName) {
return response.status(400).send('Bad Request: extensionName is required in the request body.'); return response.status(400).send('Bad Request: extensionName is required in the request body.');

View File

@ -5,9 +5,14 @@ const { readSecret, SECRET_KEYS } = require("./secrets");
const ANONYMOUS_KEY = "0000000000"; const ANONYMOUS_KEY = "0000000000";
function getHordeClient() { /**
* Returns the AIHorde client.
* @returns {Promise<AIHorde>} AIHorde client
*/
async function getHordeClient() {
const version = await getVersion();
const ai_horde = new AIHorde({ const ai_horde = new AIHorde({
client_agent: getVersion()?.agent || 'SillyTavern:UNKNOWN:Cohee#1207', client_agent: version?.agent || 'SillyTavern:UNKNOWN:Cohee#1207',
}); });
return ai_horde; return ai_horde;
} }
@ -50,7 +55,7 @@ function registerEndpoints(app, jsonParser) {
app.post('/api/horde/sd-samplers', jsonParser, async (_, response) => { app.post('/api/horde/sd-samplers', jsonParser, async (_, response) => {
try { try {
const ai_horde = getHordeClient(); const ai_horde = await getHordeClient();
const samplers = Object.values(ai_horde.ModelGenerationInputStableSamplers); const samplers = Object.values(ai_horde.ModelGenerationInputStableSamplers);
response.send(samplers); response.send(samplers);
} catch (error) { } catch (error) {
@ -61,7 +66,7 @@ function registerEndpoints(app, jsonParser) {
app.post('/api/horde/sd-models', jsonParser, async (_, response) => { app.post('/api/horde/sd-models', jsonParser, async (_, response) => {
try { try {
const ai_horde = getHordeClient(); const ai_horde = await getHordeClient();
const models = await ai_horde.getModels(); const models = await ai_horde.getModels();
response.send(models); response.send(models);
} catch (error) { } catch (error) {
@ -78,7 +83,7 @@ function registerEndpoints(app, jsonParser) {
} }
try { try {
const ai_horde = getHordeClient(); const ai_horde = await getHordeClient();
const user = await ai_horde.findUser({ token: api_key_horde }); const user = await ai_horde.findUser({ token: api_key_horde });
return response.send(user); return response.send(user);
} catch (error) { } catch (error) {
@ -106,7 +111,7 @@ function registerEndpoints(app, jsonParser) {
const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY; const api_key_horde = readSecret(SECRET_KEYS.HORDE) || ANONYMOUS_KEY;
console.log('Stable Horde request:', request.body); console.log('Stable Horde request:', request.body);
const ai_horde = getHordeClient(); const ai_horde = await getHordeClient();
const generation = await ai_horde.postAsyncImageGenerate( const generation = await ai_horde.postAsyncImageGenerate(
{ {
prompt: `${request.body.prompt} ### ${request.body.negative_prompt}`, prompt: `${request.body.prompt} ### ${request.body.negative_prompt}`,

View File

@ -1,10 +1,10 @@
const path = require('path'); const path = require('path');
const fs = require('fs'); const fs = require('fs');
const child_process = require('child_process');
const commandExistsSync = require('command-exists').sync; const commandExistsSync = require('command-exists').sync;
const _ = require('lodash'); const _ = require('lodash');
const yauzl = require('yauzl'); const yauzl = require('yauzl');
const mime = require('mime-types'); const mime = require('mime-types');
const { default: simpleGit } = require('simple-git');
/** /**
* Returns the config object from the config.conf file. * Returns the config object from the config.conf file.
@ -44,9 +44,9 @@ function getBasicAuthHeader(auth) {
/** /**
* Returns the version of the running instance. Get the version from the package.json file and the git revision. * Returns the version of the running instance. Get the version from the package.json file and the git revision.
* Also returns the agent string for the Horde API. * Also returns the agent string for the Horde API.
* @returns {{agent: string, pkgVersion: string, gitRevision: string | null, gitBranch: string | null}} Version info object * @returns {Promise<{agent: string, pkgVersion: string, gitRevision: string | null, gitBranch: string | null}>} Version info object
*/ */
function getVersion() { async function getVersion() {
let pkgVersion = 'UNKNOWN'; let pkgVersion = 'UNKNOWN';
let gitRevision = null; let gitRevision = null;
let gitBranch = null; let gitBranch = null;
@ -54,13 +54,9 @@ function getVersion() {
const pkgJson = require(path.join(process.cwd(), './package.json')); const pkgJson = require(path.join(process.cwd(), './package.json'));
pkgVersion = pkgJson.version; pkgVersion = pkgJson.version;
if (!process['pkg'] && commandExistsSync('git')) { if (!process['pkg'] && commandExistsSync('git')) {
gitRevision = child_process const git = simpleGit();
.execSync('git rev-parse --short HEAD', { cwd: process.cwd(), stdio: ['ignore', 'pipe', 'ignore'] }) gitRevision = await git.cwd(process.cwd()).revparse(['--short', 'HEAD']);
.toString().trim(); gitBranch = await git.cwd(process.cwd()).revparse(['--abbrev-ref', 'HEAD']);
gitBranch = child_process
.execSync('git rev-parse --abbrev-ref HEAD', { cwd: process.cwd(), stdio: ['ignore', 'pipe', 'ignore'] })
.toString().trim();
} }
} }
catch { catch {