1
import { z } from 'zod';
3
import { MessageModel } from '@/database/server/models/message';
4
import { updateMessagePluginSchema } from '@/database/server/schemas/lobechat';
5
import { authedProcedure, publicProcedure, router } from '@/libs/trpc';
6
import { ChatMessage } from '@/types/message';
7
import { BatchTaskResult } from '@/types/service';
9
type ChatMessageList = ChatMessage[];
11
const messageProcedure = authedProcedure.use(async (opts) => {
15
ctx: { messageModel: new MessageModel(ctx.userId) },
19
export const messageRouter = router({
20
batchCreateMessages: messageProcedure
21
.input(z.array(z.any()))
22
.mutation(async ({ input, ctx }): Promise<BatchTaskResult> => {
23
const data = await ctx.messageModel.batchCreate(input);
25
return { added: data.rowCount as number, ids: [], skips: [], success: true };
28
count: messageProcedure.query(async ({ ctx }) => {
29
return ctx.messageModel.count();
31
countToday: messageProcedure.query(async ({ ctx }) => {
32
return ctx.messageModel.countToday();
35
createMessage: messageProcedure
36
.input(z.object({}).passthrough().partial())
37
.mutation(async ({ input, ctx }) => {
38
const data = await ctx.messageModel.create(input as any);
43
getAllMessages: messageProcedure.query(async ({ ctx }): Promise<ChatMessageList> => {
44
return ctx.messageModel.queryAll();
47
getAllMessagesInSession: messageProcedure
50
sessionId: z.string().nullable().optional(),
53
.query(async ({ ctx, input }): Promise<ChatMessageList> => {
54
return ctx.messageModel.queryBySessionId(input.sessionId);
57
getMessages: publicProcedure
60
current: z.number().optional(),
61
pageSize: z.number().optional(),
62
sessionId: z.string().nullable().optional(),
63
topicId: z.string().nullable().optional(),
66
.query(async ({ input, ctx }) => {
67
if (!ctx.userId) return [];
69
const messageModel = new MessageModel(ctx.userId);
71
return messageModel.query(input);
74
removeAllMessages: messageProcedure.mutation(async ({ ctx }) => {
75
return ctx.messageModel.deleteAllMessages();
78
removeMessage: messageProcedure
79
.input(z.object({ id: z.string() }))
80
.mutation(async ({ input, ctx }) => {
81
return ctx.messageModel.deleteMessage(input.id);
84
removeMessageQuery: messageProcedure
85
.input(z.object({ id: z.string() }))
86
.mutation(async ({ input, ctx }) => {
87
return ctx.messageModel.deleteMessageQuery(input.id);
90
removeMessages: messageProcedure
91
.input(z.object({ ids: z.array(z.string()) }))
92
.mutation(async ({ input, ctx }) => {
93
return ctx.messageModel.deleteMessages(input.ids);
96
removeMessagesByAssistant: messageProcedure
99
sessionId: z.string().nullable().optional(),
100
topicId: z.string().nullable().optional(),
103
.mutation(async ({ input, ctx }) => {
104
return ctx.messageModel.deleteMessagesBySession(input.sessionId, input.topicId);
107
searchMessages: messageProcedure
108
.input(z.object({ keywords: z.string() }))
109
.query(async ({ input, ctx }) => {
110
return ctx.messageModel.queryByKeyword(input.keywords);
113
update: messageProcedure
117
value: z.object({}).passthrough().partial(),
120
.mutation(async ({ input, ctx }) => {
121
return ctx.messageModel.update(input.id, input.value);
124
updateMessagePlugin: messageProcedure
128
value: updateMessagePluginSchema.partial(),
131
.mutation(async ({ input, ctx }) => {
132
return ctx.messageModel.updateMessagePlugin(input.id, input.value);
135
updatePluginState: messageProcedure
139
value: z.object({}).passthrough(),
142
.mutation(async ({ input, ctx }) => {
143
return ctx.messageModel.updatePluginState(input.id, input.value);
146
updateTTS: messageProcedure
152
contentMd5: z.string().optional(),
153
file: z.string().optional(),
154
voice: z.string().optional(),
156
.or(z.literal(false)),
159
.mutation(async ({ input, ctx }) => {
160
if (input.value === false) {
161
return ctx.messageModel.deleteMessageTTS(input.id);
164
return ctx.messageModel.updateTTS(input.id, input.value);
167
updateTranslate: messageProcedure
173
content: z.string().optional(),
174
from: z.string().optional(),
177
.or(z.literal(false)),
180
.mutation(async ({ input, ctx }) => {
181
if (input.value === false) {
182
return ctx.messageModel.deleteMessageTranslate(input.id);
185
return ctx.messageModel.updateTranslate(input.id, input.value);
189
export type MessageRouter = typeof messageRouter;