ai-query对接新版freesun-agent接口的分支
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

171 lines
4.5 KiB

'use strict';
const moment = require('moment-timezone');
const { Op } = require('sequelize');
/**
* 记录用户的 AI 查询问题
*/
module.exports.recordQuery = async (ctx, next) => {
try {
const data = ctx.request.body;
const timeStr = moment();
// 获取模型
const { models } = ctx.fs.dc;
// 检查模型是否存在
if (!models.AiQueryRecord) {
ctx.logger.error('AiQueryRecord 模型不存在');
ctx.status = 500;
ctx.body = { message: '系统错误:模型未定义' };
return;
}
// 创建记录
const record = await models.AiQueryRecord.create({
question: data.question,
userId: data.userId,
ipAddress: data.curIp,
clientId: data.clientId,
feature: data.feature,
time: timeStr,
});
ctx.status = 200;
ctx.body = { id: record.id };
} catch (error) {
ctx.logger.error(error);
ctx.status = 400;
ctx.body = { message: '问题记录失败' };
}
};
/**
* 创建或更新查询项目
*/
module.exports.createOrUpdateItem = async (ctx, next) => {
try {
const data = ctx.request.body;
const appId = data.app_id;
const chatId = data.chat_id;
const title = data.title;
// 获取模型
const { models } = ctx.fs.dc;
let record;
// 根据条件执行不同的操作
if (chatId && title !== '新对话') {
// 更新现有记录
await models.FastgptQueryItem.update(
{ title },
{ where: { chat_id: chatId } }
);
// 获取更新后的记录
const items = await models.FastgptQueryItem.findAll({
where: {
chat_id: chatId
}
});
if (items && items.length > 0) {
record = items[0];
} else {
throw new Error('更新后未找到记录');
}
} else {
// 创建新记录
const timeStr = moment();
record = await models.FastgptQueryItem.create({
chat_id: chatId,
app_id: appId,
title: title || '新对话',
time: timeStr
});
}
ctx.status = 200;
ctx.body = { id: record.id };
} catch (error) {
ctx.logger.error(error);
ctx.status = 400;
ctx.body = { message: '问题记录失败' };
}
};
/**
* 获取查询项目列表
*/
module.exports.getItems = async (ctx, next) => {
try {
const chatKey = ctx.request.query.chat_key || '';
const appId = ctx.request.query.app_id;
// 参数验证
if (!appId) {
ctx.status = 400;
ctx.body = { message: '缺少必要参数 app_id' };
return;
}
// 获取模型
const { models } = ctx.fs.dc;
// 记录可用的模型名称,用于调试
ctx.logger.info('可用的模型: ' + Object.keys(models).join(', '));
// 检查模型是否存在
if (!models.FastgptQueryItem) {
ctx.logger.error('FastgptQueryItem 模型不存在');
ctx.status = 500;
ctx.body = { message: '系统错误:模型未定义' };
return;
}
// 查询记录
const items = await models.FastgptQueryItem.findAll({
where: {
chat_id: {
[Op.like]: `${chatKey}%`
},
app_id: appId,
delete: false
},
order: [['time', 'DESC']]
});
// 确保返回数组,即使没有记录
ctx.status = 200;
ctx.body = items || {};
} catch (error) {
ctx.logger.error(error);
ctx.status = 400;
ctx.body = { message: '记录获取失败' };
}
};
/**
* 删除聊天记录
*/
module.exports.deleteItem = async (ctx, next) => {
try {
const { models } = ctx.fs.dc;
const { chatId } = ctx.request.query;
if (!chatId) throw '缺少参数';
// 查询记录
await models.FastgptQueryItem.update(
{ delete: true },
{ where: { chat_id: chatId } }
);
ctx.status = 204;
} catch (error) {
ctx.logger.error(error);
ctx.status = 400;
ctx.body = { message: typeof error == 'string' ? error : '删除记录失败' };
}
};