39229-vm/backend/src/db/api/ai_agents.js
2026-03-17 21:40:44 +00:00

565 lines
14 KiB
JavaScript

const db = require('../models');
const FileDBApi = require('./file');
const crypto = require('crypto');
const Utils = require('../utils');
const Sequelize = db.Sequelize;
const Op = Sequelize.Op;
module.exports = class Ai_agentsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ai_agents = await db.ai_agents.create(
{
id: data.id || undefined,
name: data.name
||
null
,
tagline: data.tagline
||
null
,
system_prompt: data.system_prompt
||
null
,
persona_style: data.persona_style
||
null
,
temperature: data.temperature
||
null
,
max_output_tokens: data.max_output_tokens
||
null
,
auto_save_enabled: data.auto_save_enabled
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await ai_agents.setOwner_user( data.owner_user || null, {
transaction,
});
return ai_agents;
}
static async bulkImport(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
// Prepare data - wrapping individual data transformations in a map() method
const ai_agentsData = data.map((item, index) => ({
id: item.id || undefined,
name: item.name
||
null
,
tagline: item.tagline
||
null
,
system_prompt: item.system_prompt
||
null
,
persona_style: item.persona_style
||
null
,
temperature: item.temperature
||
null
,
max_output_tokens: item.max_output_tokens
||
null
,
auto_save_enabled: item.auto_save_enabled
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const ai_agents = await db.ai_agents.bulkCreate(ai_agentsData, { transaction });
// For each item created, replace relation files
return ai_agents;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ai_agents = await db.ai_agents.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.name !== undefined) updatePayload.name = data.name;
if (data.tagline !== undefined) updatePayload.tagline = data.tagline;
if (data.system_prompt !== undefined) updatePayload.system_prompt = data.system_prompt;
if (data.persona_style !== undefined) updatePayload.persona_style = data.persona_style;
if (data.temperature !== undefined) updatePayload.temperature = data.temperature;
if (data.max_output_tokens !== undefined) updatePayload.max_output_tokens = data.max_output_tokens;
if (data.auto_save_enabled !== undefined) updatePayload.auto_save_enabled = data.auto_save_enabled;
updatePayload.updatedById = currentUser.id;
await ai_agents.update(updatePayload, {transaction});
if (data.owner_user !== undefined) {
await ai_agents.setOwner_user(
data.owner_user,
{ transaction }
);
}
return ai_agents;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const ai_agents = await db.ai_agents.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of ai_agents) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of ai_agents) {
await record.destroy({transaction});
}
});
return ai_agents;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const ai_agents = await db.ai_agents.findByPk(id, options);
await ai_agents.update({
deletedBy: currentUser.id
}, {
transaction,
});
await ai_agents.destroy({
transaction
});
return ai_agents;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const ai_agents = await db.ai_agents.findOne(
{ where },
{ transaction },
);
if (!ai_agents) {
return ai_agents;
}
const output = ai_agents.get({plain: true});
output.avatar_profiles_agent = await ai_agents.getAvatar_profiles_agent({
transaction
});
output.projects_agent = await ai_agents.getProjects_agent({
transaction
});
output.generation_jobs_agent = await ai_agents.getGeneration_jobs_agent({
transaction
});
output.chat_threads_agent = await ai_agents.getChat_threads_agent({
transaction
});
output.app_settings_default_agent = await ai_agents.getApp_settings_default_agent({
transaction
});
output.owner_user = await ai_agents.getOwner_user({
transaction
});
return output;
}
static async findAll(
filter,
options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.users,
as: 'owner_user',
where: filter.owner_user ? {
[Op.or]: [
{ id: { [Op.in]: filter.owner_user.split('|').map(term => Utils.uuid(term)) } },
{
firstName: {
[Op.or]: filter.owner_user.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.name) {
where = {
...where,
[Op.and]: Utils.ilike(
'ai_agents',
'name',
filter.name,
),
};
}
if (filter.tagline) {
where = {
...where,
[Op.and]: Utils.ilike(
'ai_agents',
'tagline',
filter.tagline,
),
};
}
if (filter.system_prompt) {
where = {
...where,
[Op.and]: Utils.ilike(
'ai_agents',
'system_prompt',
filter.system_prompt,
),
};
}
if (filter.temperatureRange) {
const [start, end] = filter.temperatureRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
temperature: {
...where.temperature,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
temperature: {
...where.temperature,
[Op.lte]: end,
},
};
}
}
if (filter.max_output_tokensRange) {
const [start, end] = filter.max_output_tokensRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
max_output_tokens: {
...where.max_output_tokens,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
max_output_tokens: {
...where.max_output_tokens,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.persona_style) {
where = {
...where,
persona_style: filter.persona_style,
};
}
if (filter.auto_save_enabled) {
where = {
...where,
auto_save_enabled: filter.auto_save_enabled,
};
}
if (filter.createdAtRange) {
const [start, end] = filter.createdAtRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.lte]: end,
},
};
}
}
}
const queryOptions = {
where,
include,
distinct: true,
order: filter.field && filter.sort
? [[filter.field, filter.sort]]
: [['createdAt', 'desc']],
transaction: options?.transaction,
logging: console.log
};
if (!options?.countOnly) {
queryOptions.limit = limit ? Number(limit) : undefined;
queryOptions.offset = offset ? Number(offset) : undefined;
}
try {
const { rows, count } = await db.ai_agents.findAndCountAll(queryOptions);
return {
rows: options?.countOnly ? [] : rows,
count: count
};
} catch (error) {
console.error('Error executing query:', error);
throw error;
}
}
static async findAllAutocomplete(query, limit, offset, ) {
let where = {};
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'ai_agents',
'name',
query,
),
],
};
}
const records = await db.ai_agents.findAll({
attributes: [ 'id', 'name' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['name', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.name,
}));
}
};