Skip to content

Commit

Permalink
feat: support openapi import plugins (labring#48)
Browse files Browse the repository at this point in the history
* feat: support openapi import plugins

* feat: import from url

* fix: add body params parse

* fix build

* fix

* fix

* fix
  • Loading branch information
newfish-cmyk authored and c121914yu committed Mar 20, 2024
1 parent 46110ec commit 9c6b4b2
Show file tree
Hide file tree
Showing 25 changed files with 1,775 additions and 85 deletions.
5 changes: 5 additions & 0 deletions packages/global/core/plugin/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ export const defaultModules: ModuleItemType[] = [
}
];

export enum PluginTypeEnum {
folder = 'folder',
plugin = 'plugin'
}

export enum PluginSourceEnum {
personal = 'personal',
community = 'community',
Expand Down
14 changes: 14 additions & 0 deletions packages/global/core/plugin/controller.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export type CreateOnePluginParams = {
avatar: string;
intro: string;
modules?: ModuleItemType[];
parentId: string | null;
type: string;
schema?: string | null;
authMethod?: MethodType | null;
};
export type UpdatePluginParams = {
id: string;
Expand All @@ -14,8 +18,18 @@ export type UpdatePluginParams = {
modules?: ModuleItemType[];
};
export type PluginListItemType = {
parentId: string;
type: string;
_id: string;
name: string;
avatar: string;
intro: string;
schema: string;
authMethod: MethodType | null;
};
export type MethodType = {
name: string;
prefix: string;
key: string;
value: string;
};
11 changes: 8 additions & 3 deletions packages/global/core/plugin/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FlowNodeTemplateTypeEnum } from 'core/module/constants';
import type { FlowNodeTemplateType, ModuleItemType } from '../module/type.d';
import { PluginSourceEnum } from './constants';
import { ModuleTemplateTypeEnum } from 'core/module/constants';
import type { FlowModuleTemplateType, ModuleItemType } from '../module/type.d';
import { PluginSourceEnum, PluginTypeEnum } from './constants';
import { MethodType } from './controller';

export type PluginItemSchema = {
_id: string;
Expand All @@ -12,6 +13,10 @@ export type PluginItemSchema = {
intro: string;
updateTime: Date;
modules: ModuleItemType[];
parentId: string;
type: `${PluginTypeEnum}`;
schema: string;
authMethod: MethodType;
};

/* plugin template */
Expand Down
41 changes: 41 additions & 0 deletions packages/service/core/plugin/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { plugin2ModuleIO } from '@fastgpt/global/core/module/utils';
import { PluginSourceEnum } from '@fastgpt/global/core/plugin/constants';
import type { PluginRuntimeType, PluginTemplateType } from '@fastgpt/global/core/plugin/type.d';
import { FlowNodeTemplateTypeEnum } from '@fastgpt/global/core/module/constants';
import type { PluginItemSchema } from '@fastgpt/global/core/plugin/type.d';

/*
plugin id rule:
Expand Down Expand Up @@ -86,3 +87,43 @@ export async function getPluginRuntimeById(id: string): Promise<PluginRuntimeTyp
modules: plugin.modules
};
}

/* find all pluginId by top pluginId */
export async function findPluginAndAllChildren({
teamId,
pluginId,
fields
}: {
teamId: string;
pluginId: string;
fields?: string;
}): Promise<PluginItemSchema[]> {
const find = async (id: string) => {
const children = await MongoPlugin.find(
{
teamId,
parentId: id
},
fields
).lean();

let plugins = children;

for (const child of children) {
const grandChildrenIds = await find(child._id);
plugins = plugins.concat(grandChildrenIds);
}

return plugins;
};
const [plugin, childPlugins] = await Promise.all([
MongoPlugin.findById(pluginId),
find(pluginId)
]);

if (!plugin) {
return Promise.reject('Plugin not found');
}

return [plugin, ...childPlugins];
}
20 changes: 20 additions & 0 deletions packages/service/core/plugin/schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PluginTypeEnum } from '@fastgpt/global/core/plugin/constants';
import { connectionMongo, type Model } from '../../common/mongo';
const { Schema, model, models } = connectionMongo;
import type { PluginItemSchema } from '@fastgpt/global/core/plugin/type.d';
Expand All @@ -9,6 +10,11 @@ import {
export const PluginCollectionName = 'plugins';

const PluginSchema = new Schema({
parentId: {
type: Schema.Types.ObjectId,
ref: PluginCollectionName,
default: null
},
userId: {
type: Schema.Types.ObjectId,
ref: 'user'
Expand All @@ -23,6 +29,12 @@ const PluginSchema = new Schema({
ref: TeamMemberCollectionName,
required: true
},
type: {
type: String,
enum: Object.keys(PluginTypeEnum),
required: true,
default: PluginTypeEnum.plugin
},
name: {
type: String,
required: true
Expand All @@ -42,6 +54,14 @@ const PluginSchema = new Schema({
modules: {
type: Array,
default: []
},
schema: {
type: String,
default: null
},
authMethod: {
type: Object,
default: null
}
});

Expand Down
97 changes: 94 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions projects/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@apidevtools/swagger-parser": "^10.1.0",
"@chakra-ui/anatomy": "^2.2.1",
"@chakra-ui/icons": "^2.1.1",
"@chakra-ui/next-js": "^2.1.5",
Expand Down Expand Up @@ -45,6 +46,7 @@
"next": "13.5.2",
"next-i18next": "15.2.0",
"nprogress": "^0.2.0",
"openapi-types": "^12.1.3",
"react": "18.2.0",
"react-day-picker": "^8.7.1",
"react-dom": "18.2.0",
Expand Down
Loading

0 comments on commit 9c6b4b2

Please sign in to comment.