85 lines
1.9 KiB
TypeScript
85 lines
1.9 KiB
TypeScript
import { app } from '@/app.ts';
|
|
import { ChatSession } from '@/models/chat-session.ts';
|
|
import { ChatPrompt } from '@/models/chat-prompt.ts';
|
|
import { CustomError } from '@abearxiong/router';
|
|
app
|
|
.route({
|
|
path: 'chat-session',
|
|
key: 'list',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
const chatSession = await ChatSession.findAll({
|
|
order: [['updatedAt', 'DESC']],
|
|
});
|
|
ctx.body = chatSession;
|
|
})
|
|
.addTo(app);
|
|
// Admin only
|
|
app
|
|
.route({
|
|
path: 'chat-session',
|
|
key: 'list-history',
|
|
})
|
|
.define(async (ctx) => {
|
|
const data = ctx.query.data;
|
|
const chatPrompt = await ChatPrompt.findOne({
|
|
where: {
|
|
key: data.key,
|
|
},
|
|
});
|
|
if (!chatPrompt) {
|
|
throw new CustomError('ChatPrompt not found');
|
|
}
|
|
console.log('chatPrompt', chatPrompt.id);
|
|
const chatSession = await ChatSession.findAll({
|
|
order: [['updatedAt', 'DESC']],
|
|
where: {
|
|
chatPromptId: chatPrompt.id,
|
|
},
|
|
limit: data.limit || 10,
|
|
});
|
|
ctx.body = chatSession;
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'chat-session',
|
|
key: 'update',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
const tokenUser = ctx.state.tokenUser;
|
|
const uid = tokenUser.id;
|
|
const { id, ...data } = ctx.query.data;
|
|
if (id) {
|
|
const session = await ChatSession.findByPk(id);
|
|
if (session) {
|
|
await session.update(data);
|
|
} else {
|
|
throw new CustomError('Session not found');
|
|
}
|
|
ctx.body = session;
|
|
return;
|
|
}
|
|
const session = await ChatSession.create({ ...data, uid });
|
|
ctx.body = session;
|
|
})
|
|
.addTo(app);
|
|
app
|
|
.route({
|
|
path: 'chat-session',
|
|
key: 'delete',
|
|
})
|
|
.define(async (ctx) => {
|
|
const { id } = ctx.query;
|
|
const session = await ChatSession.findByPk(id);
|
|
if (!session) {
|
|
throw new CustomError('Session not found');
|
|
}
|
|
await session.destroy();
|
|
ctx.body = session;
|
|
})
|
|
.addTo(app);
|