557 lines
13 KiB
JavaScript
557 lines
13 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 AgentsDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const agents = await db.agents.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
name: data.name
|
|
||
|
|
null
|
|
,
|
|
|
|
description: data.description
|
|
||
|
|
null
|
|
,
|
|
|
|
model: data.model
|
|
||
|
|
null
|
|
,
|
|
|
|
system_prompt: data.system_prompt
|
|
||
|
|
null
|
|
,
|
|
|
|
temperature: data.temperature
|
|
||
|
|
null
|
|
,
|
|
|
|
max_output_tokens: data.max_output_tokens
|
|
||
|
|
null
|
|
,
|
|
|
|
is_default: data.is_default
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
is_active: data.is_active
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
metadata_json: data.metadata_json
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return 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 agentsData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
name: item.name
|
|
||
|
|
null
|
|
,
|
|
|
|
description: item.description
|
|
||
|
|
null
|
|
,
|
|
|
|
model: item.model
|
|
||
|
|
null
|
|
,
|
|
|
|
system_prompt: item.system_prompt
|
|
||
|
|
null
|
|
,
|
|
|
|
temperature: item.temperature
|
|
||
|
|
null
|
|
,
|
|
|
|
max_output_tokens: item.max_output_tokens
|
|
||
|
|
null
|
|
,
|
|
|
|
is_default: item.is_default
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
is_active: item.is_active
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
metadata_json: item.metadata_json
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const agents = await db.agents.bulkCreate(agentsData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
|
|
return agents;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const agents = await db.agents.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.name !== undefined) updatePayload.name = data.name;
|
|
|
|
|
|
if (data.description !== undefined) updatePayload.description = data.description;
|
|
|
|
|
|
if (data.model !== undefined) updatePayload.model = data.model;
|
|
|
|
|
|
if (data.system_prompt !== undefined) updatePayload.system_prompt = data.system_prompt;
|
|
|
|
|
|
if (data.temperature !== undefined) updatePayload.temperature = data.temperature;
|
|
|
|
|
|
if (data.max_output_tokens !== undefined) updatePayload.max_output_tokens = data.max_output_tokens;
|
|
|
|
|
|
if (data.is_default !== undefined) updatePayload.is_default = data.is_default;
|
|
|
|
|
|
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
|
|
|
|
|
|
if (data.metadata_json !== undefined) updatePayload.metadata_json = data.metadata_json;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await agents.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return agents;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const agents = await db.agents.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of agents) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of agents) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return agents;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const agents = await db.agents.findByPk(id, options);
|
|
|
|
await agents.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await agents.destroy({
|
|
transaction
|
|
});
|
|
|
|
return agents;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const agents = await db.agents.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!agents) {
|
|
return agents;
|
|
}
|
|
|
|
const output = agents.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.conversations_agent = await agents.getConversations_agent({
|
|
transaction
|
|
});
|
|
|
|
|
|
|
|
|
|
output.usage_events_agent = await agents.getUsage_events_agent({
|
|
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 = [
|
|
|
|
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.name) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'agents',
|
|
'name',
|
|
filter.name,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.description) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'agents',
|
|
'description',
|
|
filter.description,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.model) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'agents',
|
|
'model',
|
|
filter.model,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.system_prompt) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'agents',
|
|
'system_prompt',
|
|
filter.system_prompt,
|
|
),
|
|
};
|
|
}
|
|
|
|
if (filter.metadata_json) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'agents',
|
|
'metadata_json',
|
|
filter.metadata_json,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.is_default) {
|
|
where = {
|
|
...where,
|
|
is_default: filter.is_default,
|
|
};
|
|
}
|
|
|
|
if (filter.is_active) {
|
|
where = {
|
|
...where,
|
|
is_active: filter.is_active,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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.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(
|
|
'agents',
|
|
'name',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.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,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|