Skip to content

Commit

Permalink
feat: adapt v1 system plugin (#1366)
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu authored May 6, 2024
1 parent 8863337 commit db1c27c
Show file tree
Hide file tree
Showing 16 changed files with 1,511 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/service/core/plugin/type.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PluginTemplateType } from '@fastgpt/global/core/plugin/type.d';

declare global {
var communityPluginsV1: PluginTemplateType[];
var communityPlugins: PluginTemplateType[];
}
4 changes: 2 additions & 2 deletions packages/service/core/workflow/dispatchV1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export async function dispatchWorkFlowV1({
)?.targets?.length;

return moduleOutput(module, {
[NodeOutputKeyEnum.finish]: true,
finish: true,
[NodeOutputKeyEnum.userChatInput]: hasUserChatInputTarget
? params[NodeOutputKeyEnum.userChatInput]
: undefined,
Expand All @@ -295,7 +295,7 @@ export async function dispatchWorkFlowV1({
modules.forEach((item) => {
item.isEntry = false;
});

// console.log(JSON.stringify(runningModules, null, 2));
initModules.map((module) =>
moduleInput(module, {
...startParams,
Expand Down
54 changes: 50 additions & 4 deletions packages/service/core/workflow/dispatchV1/plugin/run.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,66 @@
// @ts-nocheck

import type { ModuleDispatchProps } from '@fastgpt/global/core/workflow/type';
import { dispatchWorkFlowV1 } from '../index';
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
import {
FlowNodeTemplateTypeEnum,
NodeInputKeyEnum
} from '@fastgpt/global/core/workflow/constants';
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
import { getPluginRuntimeById } from '../../../plugin/controller';
import { splitCombinePluginId } from '../../../plugin/controller';
import { authPluginCanUse } from '../../../../support/permission/auth/plugin';
import { setEntryEntries, DYNAMIC_INPUT_KEY } from '../utils';
import { DispatchNodeResultType } from '@fastgpt/global/core/workflow/runtime/type';
import { PluginRuntimeType, PluginTemplateType } from '@fastgpt/global/core/plugin/type';
import { PluginSourceEnum } from '@fastgpt/global/core/plugin/constants';
import { MongoPlugin } from '../../../plugin/schema';

type RunPluginProps = ModuleDispatchProps<{
[NodeInputKeyEnum.pluginId]: string;
[key: string]: any;
}>;
type RunPluginResponse = DispatchNodeResultType<{}>;

const getPluginTemplateById = async (id: string): Promise<PluginTemplateType> => {
const { source, pluginId } = await splitCombinePluginId(id);
if (source === PluginSourceEnum.community) {
const item = global.communityPluginsV1?.find((plugin) => plugin.id === pluginId);

if (!item) return Promise.reject('plugin not found');

return item;
}
if (source === PluginSourceEnum.personal) {
const item = await MongoPlugin.findById(id).lean();
if (!item) return Promise.reject('plugin not found');
return {
id: String(item._id),
teamId: String(item.teamId),
name: item.name,
avatar: item.avatar,
intro: item.intro,
showStatus: true,
source: PluginSourceEnum.personal,
modules: item.modules,
templateType: FlowNodeTemplateTypeEnum.personalPlugin
};
}
return Promise.reject('plugin not found');
};

const getPluginRuntimeById = async (id: string): Promise<PluginRuntimeType> => {
const plugin = await getPluginTemplateById(id);

return {
teamId: plugin.teamId,
name: plugin.name,
avatar: plugin.avatar,
showStatus: plugin.showStatus,
modules: plugin.modules
};
};

export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPluginResponse> => {
const {
mode,
Expand All @@ -31,7 +77,7 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
const plugin = await getPluginRuntimeById(pluginId);

// concat dynamic inputs
const inputModule = plugin.nodes.find((item) => item.flowType === FlowNodeTypeEnum.pluginInput);
const inputModule = plugin.modules.find((item) => item.flowType === FlowNodeTypeEnum.pluginInput);
if (!inputModule) return Promise.reject('Plugin error, It has no set input.');
const hasDynamicInput = inputModule.inputs.find((input) => input.key === DYNAMIC_INPUT_KEY);

Expand All @@ -56,7 +102,7 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi

const { flowResponses, flowUsages, assistantResponses } = await dispatchWorkFlowV1({
...props,
modules: setEntryEntries(plugin.nodes).map((module) => ({
modules: setEntryEntries(plugin.modules).map((module) => ({
...module,
showStatus: false
})),
Expand Down
2 changes: 1 addition & 1 deletion packages/service/core/workflow/dispatchV1/tools/answer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ export const dispatchAnswer = (props: Record<string, any>): AnswerResponse => {
}

return {
[NodeOutputKeyEnum.answerText]: formatText
[NodeOutputKeyEnum.answerText]: `\n${formatText}`
};
};
2 changes: 1 addition & 1 deletion projects/app/data/pluginTemplates/customFeedback.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"description": "core.module.input.description.Http Request Url",
"placeholder": "https://api.ai.com/getInventory",
"required": false,
"value": "/api/plugins/customFeedback"
"value": "/api/plugins/customFeedback/v2"
},
{
"key": "system_httpHeader",
Expand Down
2 changes: 1 addition & 1 deletion projects/app/data/pluginTemplates/textEditor.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"description": "core.module.input.description.Http Request Url",
"placeholder": "https://api.ai.com/getInventory",
"required": false,
"value": "/api/plugins/textEditor"
"value": "/api/plugins/textEditor/v2"
},
{
"key": "system_httpHeader",
Expand Down
Loading

0 comments on commit db1c27c

Please sign in to comment.