ver: 1.1.21:
- Lista dei Cataloghi - Gestione Cataloghi in base alla configurazione
This commit is contained in:
@@ -23,6 +23,9 @@ const { ObjectId } = require('mongodb');
|
||||
|
||||
const OpenAI = require("openai");
|
||||
|
||||
const { PassThrough } = require('stream');
|
||||
|
||||
|
||||
router.post('/getlist', authenticate_noerror, async function (req, res, next) {
|
||||
|
||||
|
||||
@@ -45,65 +48,127 @@ router.post('/getlist', authenticate_noerror, async function (req, res, next) {
|
||||
});
|
||||
|
||||
|
||||
|
||||
async function getDeepSeekResponse(prompt, options) {
|
||||
try {
|
||||
|
||||
const deepseek = new OpenAI({
|
||||
baseURL: 'https://api.deepseek.com/v1', // Verifica il percorso esatto dalle API
|
||||
apiKey: process.env.DS_API_KEY, // Mai hardcodare la chiave!
|
||||
baseURL: 'https://api.deepseek.com/v1',
|
||||
apiKey: process.env.DS_API_KEY,
|
||||
defaultHeaders: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (!options.withexplain) {
|
||||
prompt = prompt + '\n' + 'Ritornami solo il risultato, senza spiegazione.'
|
||||
prompt = prompt + '\n' + 'Ritornami solo il risultato, senza spiegazione.';
|
||||
}
|
||||
if (options.outputType) {
|
||||
prompt = prompt + '\n' + options.outputType;
|
||||
}
|
||||
|
||||
const completion = await deepseek.chat.completions.create({
|
||||
const completionStream = await deepseek.chat.completions.create({
|
||||
model: options.model || "deepseek-chat",
|
||||
messages: [
|
||||
{ role: "system", content: options.contestsystem || "" },
|
||||
{ role: "system", content: options?.contestsystem || "" },
|
||||
{ role: "user", content: prompt }
|
||||
],
|
||||
temperature: options.temp || 0.3,
|
||||
max_tokens: options.max_tokens || 1000,
|
||||
stream: options.stream || false,
|
||||
temperature: options?.temp || 0.3,
|
||||
max_tokens: options?.max_tokens || 1000,
|
||||
stream: options?.stream || false,
|
||||
});
|
||||
|
||||
if (!completion || !completion.choices || completion.choices.length === 0) {
|
||||
throw new Error('Invalid response from DeepSeek API');
|
||||
}
|
||||
if (options?.stream) {
|
||||
// Creiamo un PassThrough stream per inviare i chunk al frontend
|
||||
/*const stream = new PassThrough();
|
||||
completionStream.on('data', (chunk) => {
|
||||
stream.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
||||
});
|
||||
completionStream.on('end', () => {
|
||||
stream.end();
|
||||
});
|
||||
return stream;*/
|
||||
|
||||
return completion.choices[0];
|
||||
return completionStream;
|
||||
} else {
|
||||
if (!completionStream || !completionStream.choices || completionStream.choices.length === 0) {
|
||||
throw new Error('Invalid response from DeepSeek API');
|
||||
}
|
||||
return completionStream.choices[0];
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('DeepSeek Error:', error.response?.data || error.message);
|
||||
throw new Error('Failed to get AI response');
|
||||
}
|
||||
}
|
||||
|
||||
// Endpoint per DeepSeek
|
||||
router.post('/ds', authenticate, async (req, res) => {
|
||||
try {
|
||||
let prompt = req.body.prompt;
|
||||
let options = req.body.options;
|
||||
|
||||
const choice = await getDeepSeekResponse(prompt, options);
|
||||
const { prompt, options } = req.body;
|
||||
|
||||
return res.send({
|
||||
code: server_constants.RIS_CODE_OK,
|
||||
choice,
|
||||
});
|
||||
const isstream = (options && options?.stream)
|
||||
|
||||
} catch (error) {
|
||||
console.error('DeepSeek API Error:', error.response?.data || error.message);
|
||||
return res.send({ code: server_constants.RIS_CODE_ERR, error: 'Errore nella chiamata a DeepSeek: ' + error.response?.data || error.message });
|
||||
if (isstream) {
|
||||
// Se lo streaming è abilitato, restituiamo un flusso di dati
|
||||
res.setHeader('Content-Type', 'text/event-stream');
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
res.setHeader('Connection', 'keep-alive');
|
||||
|
||||
try {
|
||||
// Ottieni il flusso di dati da DeepSeek
|
||||
const completionStream = await getDeepSeekResponse(prompt, options);
|
||||
|
||||
for await (const chunk of completionStream) {
|
||||
if (chunk.choices && chunk.choices[0]) {
|
||||
const data = JSON.stringify({ choice: chunk.choices[0] });
|
||||
res.write(`data: ${data}\n\n`);
|
||||
try {
|
||||
const msg = chunk.choices[0].delta.content;
|
||||
if (msg) {
|
||||
console.log(msg);
|
||||
// await tools.sleep(10000)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error: ' + e.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.write('data: [DONE]\n\n');
|
||||
res.end();
|
||||
|
||||
|
||||
} catch (error) {
|
||||
const errorstr = 'DeepSeek API Error:' + (error.response?.data || error.message)
|
||||
console.error(errorstr);
|
||||
|
||||
// In caso di errore durante lo streaming, invia un messaggio di errore
|
||||
const errorData = JSON.stringify({
|
||||
code: server_constants.RIS_CODE_ERR,
|
||||
error: errorstr,
|
||||
});
|
||||
res.write(`data: ${errorData}\n\n`);
|
||||
res.end();
|
||||
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
// Se lo streaming non è abilitato, restituisci la risposta completa
|
||||
const choice = await getDeepSeekResponse(prompt, options);
|
||||
|
||||
return res.send({
|
||||
code: server_constants.RIS_CODE_OK,
|
||||
choice,
|
||||
});
|
||||
} catch (error) {
|
||||
const errorstr = 'DeepSeek API Error:' + (error.response?.data || error.message)
|
||||
console.error(errorstr);
|
||||
|
||||
// In caso di errore senza streaming, restituisci un errore standard
|
||||
return res.send({
|
||||
code: server_constants.RIS_CODE_ERR,
|
||||
error: errorstr,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -220,7 +220,7 @@ router.post(process.env.LINK_REQUEST_NEWPASSWORD, async (req, res) => {
|
||||
// Invio la Nuova Password richiesta dal reset!
|
||||
// Ritorna il token per poter effettuare le chiamate...
|
||||
router.post(process.env.LINK_UPDATE_PWD, async (req, res) => {
|
||||
|
||||
|
||||
try {
|
||||
const body = _.pick(req.body, ['idapp', 'email', 'tokenforgot', 'tokenforgot_code', 'password']);
|
||||
const idapp = body.idapp;
|
||||
@@ -229,7 +229,7 @@ router.post(process.env.LINK_UPDATE_PWD, async (req, res) => {
|
||||
const tokenforgot_code = body.tokenforgot_code;
|
||||
const password = body.password;
|
||||
const msg = 'Richiesta Nuova Password: idapp= ' + idapp + ' email = ' + email;
|
||||
|
||||
|
||||
console.log(msg);
|
||||
|
||||
// telegrambot.sendMsgTelegramToTheManagers(body.idapp, msg);
|
||||
@@ -668,6 +668,32 @@ router.post('/setsubrec', authenticate, (req, res) => {
|
||||
|
||||
});
|
||||
|
||||
router.post('/getobj', authenticate_noerror, async (req, res) => {
|
||||
|
||||
try {
|
||||
let cmd = req.body.cmd;
|
||||
let idapp = req.user ? req.user.idapp : sanitizeHtml(req.body.idapp); // Cambiato from params.idapp a req.body.idapp
|
||||
let ris = null;
|
||||
|
||||
if (cmd === 'lista_editori') {
|
||||
ris = await User.find(
|
||||
{
|
||||
idapp,
|
||||
perm: { $bitsAnySet: 0b10000 },
|
||||
},
|
||||
{ username: 1, name: 1, surname: 1 }
|
||||
).lean();
|
||||
}
|
||||
|
||||
// Invia la risposta
|
||||
res.status(200).send({ code: server_constants.RIS_CODE_OK, data: ris });
|
||||
|
||||
} catch (e) {
|
||||
console.error(`ERROR getobj ${cmd}: `, e.message);
|
||||
res.status(200).send({ code: server_constants.RIS_CODE_OK, data: [] });
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/gettable', authenticate_noerror, (req, res) => {
|
||||
let params = req.body;
|
||||
|
||||
@@ -868,17 +894,21 @@ async function duplicatePage(pageId, newpath) {
|
||||
|
||||
const catalogo = elem.catalogo;
|
||||
|
||||
for (const recscheda of catalogo.arrSchede) {
|
||||
if (recscheda.scheda?.isTemplate) {
|
||||
// Se è un template allora devo mettergli un altro ID !
|
||||
recscheda.scheda._id = new mongoose.Types.ObjectId();
|
||||
// recscheda.scheda.name = getNewFreeNameTemplate(recscheda.scheda?.name)
|
||||
if (catalogo) {
|
||||
|
||||
for (const recscheda of catalogo.arrSchede) {
|
||||
if (recscheda.scheda?.isTemplate) {
|
||||
// Se è un template allora devo mettergli un altro ID !
|
||||
recscheda.scheda._id = new mongoose.Types.ObjectId();
|
||||
// recscheda.scheda.name = getNewFreeNameTemplate(recscheda.scheda?.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let newelem = { ...elem };
|
||||
|
||||
elem.catalogo = { ...catalogo };
|
||||
if (catalogo)
|
||||
elem.catalogo = { ...catalogo };
|
||||
|
||||
const newElem = new MyElem({
|
||||
...elem, // Copia le proprietà dell'elemento
|
||||
|
||||
Reference in New Issue
Block a user