@lobehub/chat
Version:
Lobe Chat - an open-source, high-performance chatbot framework that supports speech synthesis, multimodal, and extensible Function Call plugin system. Supports one-click free deployment of your private ChatGPT/LLM web application.
81 lines (68 loc) • 2.67 kB
text/typescript
import { z } from 'zod';
import { KnowledgeBaseModel } from '@/database/models/knowledgeBase';
import { insertKnowledgeBasesSchema } from '@/database/schemas';
import { authedProcedure, router } from '@/libs/trpc/lambda';
import { serverDatabase } from '@/libs/trpc/lambda/middleware';
import { KnowledgeBaseItem } from '@/types/knowledgeBase';
const knowledgeBaseProcedure = authedProcedure.use(serverDatabase).use(async (opts) => {
const { ctx } = opts;
return opts.next({
ctx: {
knowledgeBaseModel: new KnowledgeBaseModel(ctx.serverDB, ctx.userId),
},
});
});
export const knowledgeBaseRouter = router({
addFilesToKnowledgeBase: knowledgeBaseProcedure
.input(z.object({ ids: z.array(z.string()), knowledgeBaseId: z.string() }))
.mutation(async ({ input, ctx }) => {
return ctx.knowledgeBaseModel.addFilesToKnowledgeBase(input.knowledgeBaseId, input.ids);
}),
createKnowledgeBase: knowledgeBaseProcedure
.input(
z.object({
avatar: z.string().optional(),
description: z.string().optional(),
name: z.string(),
}),
)
.mutation(async ({ input, ctx }) => {
const data = await ctx.knowledgeBaseModel.create({
avatar: input.avatar,
description: input.description,
name: input.name,
});
return data?.id;
}),
getKnowledgeBaseById: knowledgeBaseProcedure
.input(z.object({ id: z.string() }))
.query(async ({ ctx, input }): Promise<KnowledgeBaseItem | undefined> => {
return ctx.knowledgeBaseModel.findById(input.id);
}),
getKnowledgeBases: knowledgeBaseProcedure.query(async ({ ctx }): Promise<KnowledgeBaseItem[]> => {
return ctx.knowledgeBaseModel.query();
}),
removeAllKnowledgeBases: knowledgeBaseProcedure.mutation(async ({ ctx }) => {
return ctx.knowledgeBaseModel.deleteAll();
}),
removeFilesFromKnowledgeBase: knowledgeBaseProcedure
.input(z.object({ ids: z.array(z.string()), knowledgeBaseId: z.string() }))
.mutation(async ({ input, ctx }) => {
return ctx.knowledgeBaseModel.removeFilesFromKnowledgeBase(input.knowledgeBaseId, input.ids);
}),
removeKnowledgeBase: knowledgeBaseProcedure
.input(z.object({ id: z.string(), removeFiles: z.boolean().optional() }))
.mutation(async ({ input, ctx }) => {
return ctx.knowledgeBaseModel.delete(input.id);
}),
updateKnowledgeBase: knowledgeBaseProcedure
.input(
z.object({
id: z.string(),
value: insertKnowledgeBasesSchema.partial(),
}),
)
.mutation(async ({ input, ctx }) => {
return ctx.knowledgeBaseModel.update(input.id, input.value);
}),
});