1
import { z } from 'zod';
3
import { PluginModel } from '@/database/server/models/plugin';
4
import { authedProcedure, publicProcedure, router } from '@/libs/trpc';
5
import { LobeTool } from '@/types/tool';
7
const pluginProcedure = authedProcedure.use(async (opts) => {
11
ctx: { pluginModel: new PluginModel(ctx.userId) },
15
export const pluginRouter = router({
16
createOrInstallPlugin: pluginProcedure
19
customParams: z.any(),
20
identifier: z.string(),
22
type: z.enum(['plugin', 'customPlugin']),
25
.mutation(async ({ input, ctx }) => {
26
const result = await ctx.pluginModel.findById(input.identifier);
28
// if not exist, we should create the plugin
30
const data = await ctx.pluginModel.create({
31
customParams: input.customParams,
32
identifier: input.identifier,
33
manifest: input.manifest,
37
return data.identifier;
40
// or we can just update the plugin manifest
41
await ctx.pluginModel.update(input.identifier, { manifest: input.manifest });
44
createPlugin: pluginProcedure
47
customParams: z.any(),
48
identifier: z.string(),
50
type: z.enum(['plugin', 'customPlugin']),
53
.mutation(async ({ input, ctx }) => {
54
const data = await ctx.pluginModel.create({
55
customParams: input.customParams,
56
identifier: input.identifier,
57
manifest: input.manifest,
61
return data.identifier;
64
getPlugins: publicProcedure.query(async ({ ctx }): Promise<LobeTool[]> => {
65
if (!ctx.userId) return [];
67
const pluginModel = new PluginModel(ctx.userId);
69
return pluginModel.query();
72
removeAllPlugins: pluginProcedure.mutation(async ({ ctx }) => {
73
return ctx.pluginModel.deleteAll();
76
removePlugin: pluginProcedure
77
.input(z.object({ id: z.string() }))
78
.mutation(async ({ input, ctx }) => {
79
return ctx.pluginModel.delete(input.id);
82
updatePlugin: pluginProcedure
85
customParams: z.any().optional(),
87
manifest: z.any().optional(),
88
settings: z.any().optional(),
91
.mutation(async ({ input, ctx }) => {
92
return ctx.pluginModel.update(input.id, {
93
customParams: input.customParams,
94
manifest: input.manifest,
95
settings: input.settings,
100
export type PluginRouter = typeof pluginRouter;