From 1b28d3f2bd87f320183f04f5eb22b9bbcabe23fc Mon Sep 17 00:00:00 2001 From: ChatBookAI Date: Sat, 1 Jun 2024 17:28:51 +0800 Subject: [PATCH] initChatBookDbExec --- express/main.js | 6 ++--- express/src/app.js | 14 +++++++++--- express/src/utils/app.js | 44 ++++++++++++++++++------------------ express/src/utils/dataset.js | 26 ++++++++++----------- express/src/utils/db.js | 21 +++++++++-------- express/src/utils/llms.js | 40 ++++++++++++++++---------------- express/src/utils/user.js | 34 ++++++++++++++-------------- express/src/utils/utils.js | 34 ++++++++++++++-------------- 8 files changed, 115 insertions(+), 104 deletions(-) diff --git a/express/main.js b/express/main.js index b610934..d51cdb1 100644 --- a/express/main.js +++ b/express/main.js @@ -3,7 +3,7 @@ import isDev from 'electron-is-dev' import settings from 'electron-settings'; import { server, getPort } from './src/app.js'; -import { initChatBookSetting, initChatBookDbExec } from './src/utils/db.js'; +import { initChatBookSetting, ChatBookDbInit } from './src/utils/db.js'; const PORT = getPort(); @@ -26,8 +26,8 @@ function createMainWindow() { ipcMain.on('start-chatbook', async (event, data) => { ChatBookSetting = await settings.get('chatbook'); console.log("ChatBookSetting main.js", ChatBookSetting) - await initChatBookSetting(ChatBookSetting); - await initChatBookDbExec(); + initChatBookSetting(ChatBookSetting); + ChatBookDbInit(); mainWindow.loadURL('http://localhost:' + PORT); }); diff --git a/express/src/app.js b/express/src/app.js index 55fd65d..4e830ef 100644 --- a/express/src/app.js +++ b/express/src/app.js @@ -6,7 +6,7 @@ import cron from 'node-cron'; import dotenv from 'dotenv'; import { dirname, join } from 'path'; -import { initChatBookDbExec } from './utils/db.js'; +import { ChatBookDbInit, initChatBookSetting } from './utils/db.js'; import { parseFilesAndWeb, vectorDdProcess } from './utils/llms.js'; //import { downloadVideoFromAPI } from './utils/Backup/stability.js'; @@ -32,9 +32,17 @@ app.use(cors()); app.use(bodyParser.json()); dotenv.config(); - //Initial Database and Folder -initChatBookDbExec() + +function isElectron() { + return typeof process !== 'undefined' && !!(process.versions).electron; +} + +//Running in express, not in electron +if(isElectron() == false) { + initChatBookSetting({NodeStorageDirectory: './data'}) + ChatBookDbInit() +} //Schedule Task for Parse Upload Files cron.schedule('*/3 * * * *', () => { diff --git a/express/src/utils/app.js b/express/src/utils/app.js index d620420..8b6cffe 100644 --- a/express/src/utils/app.js +++ b/express/src/utils/app.js @@ -1,9 +1,9 @@ -import { initChatBookDb } from './db.js' +import { ChatBookDbPool } from './db.js' import { filterString, log, getNanoid } from './utils.js' export async function addApp(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ console.log("ParamsParamsParamsParamsParams", Params) @@ -32,7 +32,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function editApp(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params._id) @@ -60,7 +60,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function editAppById(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params._id) @@ -87,7 +87,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function deleteApp(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params.appId) @@ -105,7 +105,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function deleteAppById(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params._id) @@ -123,7 +123,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getApp(id, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const idFilter = filterString(id) @@ -141,7 +141,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getAppById(_id, id) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const _idFilter = filterString(_id) const idFilter = filterString(id) @@ -160,7 +160,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getAppByPublishId(id) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const idFilter = filterString(id) @@ -183,7 +183,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getAppPage(pageid, pagesize, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize; @@ -207,7 +207,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getAppPageAll(pageid, pagesize, data) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize; @@ -251,7 +251,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getChatlogPageByApp(appId, pageid, pagesize, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const appIdFileter = filterString(appId) const pageidFiler = Number(pageid) < 0 ? 0 : pageid; @@ -276,7 +276,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getChatlogStaticPageByApp(appId, pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const appIdFileter = filterString(appId) const pageidFiler = Number(pageid) < 0 ? 0 : pageid; @@ -314,7 +314,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getPublishsPageByApp(appId, pageid, pagesize, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const appIdFileter = filterString(appId) const pageidFiler = Number(pageid) < 0 ? 0 : pageid; @@ -339,7 +339,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getPublishsAll(pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize; @@ -366,7 +366,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function addPublish(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = getNanoid(32) @@ -404,7 +404,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function editPublish(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params._id) @@ -431,7 +431,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function deletePublish(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params._id) @@ -451,7 +451,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getPublish(id, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const idFilter = filterString(id) const userIdFilter = Number(userId) @@ -469,7 +469,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getChatLogByAppIdAndUserId(appId, userId, pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const appIdFiler = filterString(appId); const userIdFiler = filterString(userId); @@ -495,7 +495,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function deleteUserLogByAppId(appId, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const UpdateChatLog = db.prepare("update chatlog set current = 0 where appId = ? and userId = ?"); UpdateChatLog.run(appId, userId); @@ -505,7 +505,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function deleteUserLogByChatlogId(appId, userId, id) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const UpdateChatLog = db.prepare("delete from chatlog where appId = ? and userId = ? and _id = ?"); UpdateChatLog.run(appId, userId, id); diff --git a/express/src/utils/dataset.js b/express/src/utils/dataset.js index 20f07e4..31cd0e6 100644 --- a/express/src/utils/dataset.js +++ b/express/src/utils/dataset.js @@ -1,10 +1,10 @@ -import { initChatBookDb } from './db.js' +import { ChatBookDbPool } from './db.js' import { filterString, log, getNanoid } from './utils.js' export async function addDataset(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params._id) @@ -29,7 +29,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function editDataset(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params._id) @@ -54,7 +54,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function deleteDataset(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params.datasetId) @@ -72,7 +72,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getDataset(id, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const idFilter = filterString(id) const userIdFilter = Number(userId) @@ -90,7 +90,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getDatasetPage(pageid, pagesize, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize; @@ -114,7 +114,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getCollectionPageByDataset(datasetId, pageid, pagesize, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const datasetIdFileter = filterString(datasetId) const pageidFiler = Number(pageid) < 0 ? 0 : pageid; @@ -139,7 +139,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getCollectionAll(pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize; @@ -166,7 +166,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function addCollection(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = getNanoid(32) @@ -202,7 +202,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function editCollection(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params._id) @@ -227,7 +227,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function uploadCollection(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ const type = filterString(Params.type) @@ -282,7 +282,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function deleteCollection(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ Params._id = filterString(Params._id) @@ -301,7 +301,7 @@ import { filterString, log, getNanoid } from './utils.js' } export async function getCollection(id, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const idFilter = filterString(id) const userIdFilter = Number(userId) diff --git a/express/src/utils/db.js b/express/src/utils/db.js index c2174fc..0bdd735 100644 --- a/express/src/utils/db.js +++ b/express/src/utils/db.js @@ -11,15 +11,15 @@ let getDbRecord = null let getDbRecordALL = null export function initChatBookSetting(Data) { - console.log("ChatBookSettingChatBookSetting", ChatBookSetting) + console.log("ChatBookSettingChatBookSetting", Data) ChatBookSetting = Data } -export function initChatBookDb() { +export function ChatBookDbPool() { - const DataDir = ChatBookSetting && ChatBookSetting.NodeStorageDirectory ? ChatBookSetting.NodeStorageDirectory : "D:\\GitHub\\ChatBook\\express\\data"; + const DataDir = ChatBookSetting?.NodeStorageDirectory; - if(!isDirectorySync(DataDir)) { + if(DataDir == undefined || DataDir == null || !isDirectorySync(DataDir)) { return {DataDir: null, db: null, getDbRecord: null, getDbRecordALL: null} } @@ -38,16 +38,16 @@ export function initChatBookDb() { return {DataDir, db, getDbRecord, getDbRecordALL} } -export async function initChatBookDbExec() { +export function ChatBookDbInit() { let exeStatus = 0 if (!initialized) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() if(!isDirectorySync(DataDir)) { return {DataDir: null, db: null, getDbRecord: null, getDbRecordALL: null} } - console.log("dbdbdbdbdbdbdb", db, DataDir) + console.log("ChatBookDbInit", db, DataDir) enableDir(DataDir); enableDir(DataDir + '/audio/'); @@ -333,12 +333,15 @@ export async function initChatBookDbExec() { initialized = true; exeStatus = 1 } - console.log("initChatBookDbExec exeStatus", exeStatus) - console.log("initChatBookDbExec initialized", initialized) + console.log("ChatBookDbInit exeStatus", exeStatus) + console.log("ChatBookDbInit initialized", initialized) } function isDirectorySync(path) { try { + if(path == undefined || path == null) { + return false + } const stats = fs.statSync(path); return stats.isDirectory(); } catch (err) { diff --git a/express/src/utils/llms.js b/express/src/utils/llms.js index 6b13437..79d5b65 100644 --- a/express/src/utils/llms.js +++ b/express/src/utils/llms.js @@ -40,7 +40,7 @@ import { RunnableSequence } from '@langchain/core/runnables'; import { BytesOutputParser, StringOutputParser } from '@langchain/core/output_parsers'; import { DataDir } from './const.js'; -import { initChatBookDb } from './db.js' +import { ChatBookDbPool } from './db.js' import { getLLMSSetting, log, isFile, formatDateString, enableDir, getNanoid, writeFile } from './utils.js' import { createEmbeddingsFromList, getWebsiteUrlContext, formatMessage, rephraseInput, retrieveContext } from './lancedb.js'; @@ -64,7 +64,7 @@ let ChatBaiduWenxinModel = null } export async function ChatApp(_id, res, userId, question, history, template, appId, publishId, allowChatLog, temperature, datasetId, DatasetPrompt) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const Records = await (getDbRecord)("SELECT * from app where _id = ?", [appId]); @@ -119,7 +119,7 @@ let ChatBaiduWenxinModel = null } export async function chatOpenAI(_id, res, userId, question, history, template, appId, publishId, allowChatLog, temperature) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() ChatBookOpenAIStreamResponse = '' const startTime = performance.now() @@ -188,7 +188,7 @@ let ChatBaiduWenxinModel = null } export async function chatOpenAIDataset(_id, res, userId, question, history, template, appId, publishId, allowChatLog, temperature, datasetId, DatasetPrompt) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() ChatBookOpenAIStreamResponse = '' const startTime = performance.now() @@ -275,7 +275,7 @@ let ChatBaiduWenxinModel = null } export async function chatDeepSeek(_id, res, userId, question, history, template, appId, publishId, allowChatLog, temperature) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const startTime = performance.now() const pastMessages = []; @@ -373,7 +373,7 @@ let ChatBaiduWenxinModel = null } export function makeChainOpenAI(retriever, CONDENSE_TEMPLATE, QA_TEMPLATE) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const condenseQuestionPrompt = ChatPromptTemplate.fromTemplate(CONDENSE_TEMPLATE); const answerPrompt = ChatPromptTemplate.fromTemplate(QA_TEMPLATE); @@ -448,7 +448,7 @@ let ChatBaiduWenxinModel = null } export async function chatChatGemini(_id, res, userId, question, history, template, appId, publishId, allowChatLog) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const datasetId = '' await initChatBookGeminiStream(res, datasetId) @@ -493,7 +493,7 @@ let ChatBaiduWenxinModel = null } export async function chatChatGeminiMindMap(res, userId, question, history, template, appId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const datasetId = '' await initChatBookGeminiStream(res, datasetId) @@ -556,7 +556,7 @@ let ChatBaiduWenxinModel = null } export async function chatChatBaiduWenxin(res, userId, question, history, template, appId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const datasetId = '' await initChatBookBaiduWenxinStream(res, datasetId); @@ -600,7 +600,7 @@ let ChatBaiduWenxinModel = null } export async function GenereateImageUsingDallE2(res, userId, question, size='1024x1024') { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const datasetId = '' getLLMSSettingData = await getLLMSSetting(datasetId); @@ -669,7 +669,7 @@ let ChatBaiduWenxinModel = null } export async function GenereateAudioUsingTTS(res, ModelId, userId, question, voice='alloy', appId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() getLLMSSettingData = await getLLMSSetting(ModelId); const OPENAI_API_BASE = getLLMSSettingData.OPENAI_API_BASE ?? "https://api.openai.com/v1"; @@ -723,7 +723,7 @@ let ChatBaiduWenxinModel = null } export async function outputAudio(res, file) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try { const fileList = file.split('_') @@ -745,7 +745,7 @@ let ChatBaiduWenxinModel = null } export async function compressPng(file) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const FileName = path.join(DataDir, "/image/"+ file + ".png"); const FileNameNew = path.join(DataDir, "/image/"+ file + "_thumbnail.png"); @@ -762,7 +762,7 @@ let ChatBaiduWenxinModel = null } export async function compressImageForImage(file, width, height) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const FileName = path.join(DataDir, "/imageforimage/"+ file); const FileNameNew = path.join(DataDir, "/imageforimage/Resize_" + (width ? width+'_'+file : height+'_'+file) ); @@ -785,7 +785,7 @@ let ChatBaiduWenxinModel = null } export async function outputImage(res, file) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try { await compressPng(file); @@ -808,7 +808,7 @@ let ChatBaiduWenxinModel = null } export async function outputAvatarForApp(res, file) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try { await compressPng(file); @@ -831,7 +831,7 @@ let ChatBaiduWenxinModel = null } export async function outputAvatarForDataset(res, file) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try { await compressPng(file); @@ -854,7 +854,7 @@ let ChatBaiduWenxinModel = null } export async function outputImageOrigin(res, file) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try { const FileName = path.join(DataDir, "/image/"+ file + ".png"); @@ -876,7 +876,7 @@ let ChatBaiduWenxinModel = null //此处只做数据转文本操作, 向量化数据在另外一个函数里面. export async function parseFilesAndWeb() { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try { @@ -947,7 +947,7 @@ let ChatBaiduWenxinModel = null } export async function vectorDdProcess() { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try { const RecordsAll = await getDbRecordALL(`SELECT * from dataset where syncStatus = '1' limit 1`); diff --git a/express/src/utils/user.js b/express/src/utils/user.js index c32cb18..ec4c7ca 100644 --- a/express/src/utils/user.js +++ b/express/src/utils/user.js @@ -8,7 +8,7 @@ dotenv.config(); - import { initChatBookDb } from './db.js' + import { ChatBookDbPool } from './db.js' const secretKey = process.env.JWT_TOKEN_SECRET_KEY || "ChatBookAI"; @@ -62,7 +62,7 @@ } export async function checkUserTokenXWE(token) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const buffer = Buffer.from(token, 'base64'); const TxText = buffer.toString('utf-8'); @@ -128,7 +128,7 @@ } export async function userLoginLog(req, email, action, msg) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const agent = useragent.parse(req.headers['user-agent']); const BrowserType = agent.family @@ -196,7 +196,7 @@ } export async function changeUserPasswordByToken(token, data) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const checkUserTokenData = await checkUserToken(token); if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && checkUserTokenData.data.role == 'admin') { @@ -236,7 +236,7 @@ } export async function changeUserDetail(token, data) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const checkUserTokenData = await checkUserToken(token); if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && checkUserTokenData.data.role == 'admin') { @@ -253,7 +253,7 @@ } export async function changeUserStatus(token, data) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const checkUserTokenData = await checkUserToken(token); if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && checkUserTokenData.data.role == 'admin') { @@ -270,7 +270,7 @@ } export async function registerUser(email, username, password, confirm_password, language) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ if(password != confirm_password) { @@ -298,7 +298,7 @@ } export async function addUser(token, data) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const checkUserTokenData = await checkUserToken(token); if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && checkUserTokenData.data.role == 'admin' && data && data.email && data.username) { @@ -326,7 +326,7 @@ } export async function editUser(token, data) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const checkUserTokenData = await checkUserToken(token); if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && checkUserTokenData.data.role == 'admin') { @@ -344,7 +344,7 @@ } export async function deleteUser(token, data) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const checkUserTokenData = await checkUserToken(token); if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email && checkUserTokenData.data.role == 'admin' && data && data.email && data.username) { @@ -362,7 +362,7 @@ } export async function getOneUser(email) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const Records = await (getDbRecord)("SELECT * from user where email = ? ", [email]); @@ -370,7 +370,7 @@ } export async function getOneUserByToken(token) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const checkUserTokenData = await checkUserToken(token); if(checkUserTokenData && checkUserTokenData.data && checkUserTokenData.data.email) { @@ -403,7 +403,7 @@ } export async function getUsers(pageid, pagesize, data) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize; @@ -449,7 +449,7 @@ } export async function getUserLogs(email, pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize; @@ -474,7 +474,7 @@ } export async function getUserLogsAll(pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize; @@ -508,7 +508,7 @@ } export async function updateUserImageFavorite(token, data) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() console.log("data", data) const checkUserTokenData = await checkUserToken(token); @@ -534,7 +534,7 @@ } export async function updateUserVideoFavorite(token, data) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() console.log("data", data) const checkUserTokenData = await checkUserToken(token); diff --git a/express/src/utils/utils.js b/express/src/utils/utils.js index c4f7378..93734db 100644 --- a/express/src/utils/utils.js +++ b/express/src/utils/utils.js @@ -4,7 +4,7 @@ import path from 'path' import * as crypto from 'crypto' import validator from 'validator'; import { randomBytes } from 'crypto'; -import { initChatBookDb } from './db.js' +import { ChatBookDbPool } from './db.js' import iconv from 'iconv-lite'; @@ -24,7 +24,7 @@ export function enableDir(directoryPath) { } export async function getLLMSSetting(datasetId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const datasetIdFilter = filterString(datasetId) @@ -40,7 +40,7 @@ export async function getLLMSSetting(datasetId) { } export async function setOpenAISetting(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const datasetIdFilter = filterString(Params.datasetId) const userIdFilter = filterString(Params.userId) @@ -61,7 +61,7 @@ export async function setOpenAISetting(Params) { } export async function getTemplate(datasetId, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const datasetIdFilter = filterString(datasetId) const userIdFilter = Number(userId) @@ -79,7 +79,7 @@ export async function getTemplate(datasetId, userId) { } export async function setTemplate(Params) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() try{ const datasetIdFilter = Number(Params.datasetId) @@ -203,7 +203,7 @@ export async function uploadfilesInsertIntoDb(files, datasetId, userId) { } export async function getFilesPage(pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize; @@ -244,7 +244,7 @@ export async function getFilesPage(pageid, pagesize) { } export async function getFilesDatasetId(datasetId, pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const datasetIdFiler = Number(datasetId) < 0 ? 0 : datasetId; const pageidFiler = Number(pageid) < 0 ? 0 : pageid; @@ -286,7 +286,7 @@ export async function getFilesDatasetId(datasetId, pageid, pagesize) { } export async function getFilesNotParsed() { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const RecordsAll = await (getDbRecordALL)(`SELECT * from files where status = '0' order by timestamp asc limit 10 `); @@ -295,7 +295,7 @@ export async function getFilesNotParsed() { } export async function getChatLogByDatasetIdAndUserId(datasetId, userId, pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const datasetIdFiler = filterString(datasetId); const userIdFiler = Number(userId) < 0 ? 0 : userId; @@ -320,7 +320,7 @@ export async function getChatLogByDatasetIdAndUserId(datasetId, userId, pageid, } export async function getLogsPage(pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize; @@ -352,7 +352,7 @@ export function timestampToDate(timestamp) { } export async function log(appId, publishId, userId, Action1, Action2, Action3, Action4, Action5, Action6, Action7, Action8, Action9, Action10) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const currentDate = new Date(); const currentDateTime = currentDate.toLocaleString(); @@ -364,7 +364,7 @@ export async function log(appId, publishId, userId, Action1, Action2, Action3, A } export async function deleteLog() { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const Records = await (getDbRecord)("SELECT MAX(id) AS NUM FROM logs"); const MaxId = Records ? Records.NUM : 0; @@ -377,7 +377,7 @@ export async function deleteLog() { } export async function clearShortLogs() { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const DeleteLog = db.prepare("delete from logs where length(content) < 50"); DeleteLog.run(); @@ -386,7 +386,7 @@ export async function clearShortLogs() { } export async function clearAllLogs() { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const DeleteLog = db.prepare("delete from logs"); DeleteLog.run(); @@ -395,7 +395,7 @@ export async function clearAllLogs() { } export async function deleteUserLogByDatasetId(datasetId, userId) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const UpdateChatLog = db.prepare("update chatlog set current = 0 where appId = ? and userId = ?"); UpdateChatLog.run(datasetId, userId); @@ -542,7 +542,7 @@ export const isEmailValid = (email) => { }; export async function wholeSiteStatics() { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const NewUserPerDayData = await (getDbRecordALL)(`SELECT strftime('%Y-%m-%d', datetime(createtime / 1000, 'unixepoch')) AS date, count(*) AS NUM from user group by date order by date asc`); const NewUserPerDay = NewUserPerDayData.map(Item => Item.NUM) @@ -575,7 +575,7 @@ export async function wholeSiteStatics() { } export async function getAllImages(userId, pageid, pagesize) { - const { DataDir, db, getDbRecord, getDbRecordALL } = initChatBookDb() + const { DataDir, db, getDbRecord, getDbRecordALL } = ChatBookDbPool() const pageidFiler = Number(pageid) < 0 ? 0 : pageid; const pagesizeFiler = Number(pagesize) < 5 ? 5 : pagesize;