Skip to content

Commit b23009a

Browse files
committed
feat: support system toolset
1 parent a54fed4 commit b23009a

File tree

11 files changed

+172
-111
lines changed

11 files changed

+172
-111
lines changed

packages/service/core/app/plugin/controller.ts

Lines changed: 85 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { type FlowNodeTemplateType } from '@fastgpt/global/core/workflow/type/node.d';
2-
import { FlowNodeTypeEnum } from '@fastgpt/global/core/workflow/node/constant';
2+
import {
3+
FlowNodeInputTypeEnum,
4+
FlowNodeTypeEnum
5+
} from '@fastgpt/global/core/workflow/node/constant';
36
import {
47
appData2FlowNodeIO,
58
pluginData2FlowNodeIO,
@@ -25,14 +28,15 @@ import {
2528
NodeInputKeyEnum
2629
} from '@fastgpt/global/core/workflow/constants';
2730
import { getNanoid } from '@fastgpt/global/common/string/tools';
28-
import { getSystemToolList } from '../tool/api';
31+
import { APIGetSystemToolList } from '../tool/api';
2932
import { Types } from '../../../common/mongo';
3033
import type { SystemPluginConfigSchemaType } from './type';
3134
import type {
3235
FlowNodeInputItemType,
3336
FlowNodeOutputItemType
3437
} from '@fastgpt/global/core/workflow/type/io';
3538
import { isProduction } from '@fastgpt/global/common/system/constants';
39+
import type { RuntimeNodeItemType } from '@fastgpt/global/core/workflow/runtime/type';
3640

3741
/**
3842
plugin id rule:
@@ -78,7 +82,7 @@ export const getSystemPluginByIdAndVersionId = async (
7882
versionId?: string
7983
): Promise<ChildAppType> => {
8084
const plugin = await (async (): Promise<ChildAppType> => {
81-
const plugin = await getSystemPluginById(pluginId);
85+
const plugin = await getSystemToolById(pluginId);
8286

8387
// Admin selected system tool
8488
if (plugin.associatedPluginId) {
@@ -127,6 +131,17 @@ export const getSystemPluginByIdAndVersionId = async (
127131
: plugin.versionList?.[0];
128132
const lastVersion = plugin.versionList?.[0];
129133

134+
// concat parent (is exists) input config
135+
const parent = plugin.parentId ? await getSystemToolById(plugin.parentId) : undefined;
136+
if (parent && parent.inputList) {
137+
plugin?.inputs?.push({
138+
key: 'system_input_config',
139+
label: '',
140+
renderTypeList: [FlowNodeInputTypeEnum.hidden],
141+
inputList: parent.inputList
142+
});
143+
}
144+
130145
return {
131146
...plugin,
132147
version: versionId ? version?.value : '',
@@ -195,11 +210,34 @@ export async function getChildAppPreviewNode({
195210

196211
const { flowNodeType, nodeIOConfig } = await (async () => {
197212
if (source === PluginSourceEnum.systemTool) {
213+
const children = (await getSystemTools()).filter((item) => item.parentId === pluginId);
198214
return {
199-
flowNodeType: FlowNodeTypeEnum.tool,
215+
flowNodeType: app.isFolder ? FlowNodeTypeEnum.toolSet : FlowNodeTypeEnum.tool,
200216
nodeIOConfig: {
201-
inputs: app.inputs!,
202-
outputs: app.outputs!,
217+
inputs: app.isFolder
218+
? ([
219+
{
220+
value: {
221+
toolList: children.map((item) => ({
222+
name: parseI18nString(item.name, lang),
223+
description: parseI18nString(item.intro, lang)
224+
}))
225+
},
226+
renderTypeList: [FlowNodeInputTypeEnum.hidden]
227+
},
228+
...(app.inputList
229+
? [
230+
{
231+
key: NodeInputKeyEnum.systemInputConfig,
232+
label: '',
233+
renderTypeList: [FlowNodeInputTypeEnum.hidden],
234+
inputList: app.inputList
235+
}
236+
]
237+
: [])
238+
] as FlowNodeInputItemType[])
239+
: app.inputs ?? [],
240+
outputs: app.outputs ?? [],
203241
toolConfig: {
204242
systemTool: {
205243
toolId: app.id
@@ -274,11 +312,15 @@ export async function getChildAppPreviewNode({
274312
System plugin: plugin id
275313
Personal plugin: Version id
276314
*/
277-
export async function getChildAppRuntimeById(
278-
id: string,
279-
versionId?: string,
280-
lang: localeType = 'en'
281-
): Promise<PluginRuntimeType> {
315+
export async function getChildAppRuntimeById({
316+
id,
317+
versionId,
318+
lang = 'en'
319+
}: {
320+
id: string;
321+
versionId?: string;
322+
lang?: localeType;
323+
}): Promise<PluginRuntimeType> {
282324
const app = await (async () => {
283325
const { source, pluginId } = splitCombinePluginId(id);
284326

@@ -331,6 +373,30 @@ export async function getChildAppRuntimeById(
331373
};
332374
}
333375

376+
export async function getSystemPluginRuntimeNodeById(
377+
pluginId: string
378+
): Promise<RuntimeNodeItemType> {
379+
const { source } = splitCombinePluginId(pluginId);
380+
if (source === PluginSourceEnum.systemTool) {
381+
const tool = await getSystemPluginByIdAndVersionId(pluginId);
382+
return {
383+
...tool,
384+
name: parseI18nString(tool.name),
385+
intro: parseI18nString(tool.intro),
386+
inputs: tool.inputs ?? [],
387+
outputs: tool.outputs ?? [],
388+
flowNodeType: FlowNodeTypeEnum.tool,
389+
nodeId: getNanoid(),
390+
toolConfig: {
391+
systemTool: {
392+
toolId: pluginId
393+
}
394+
}
395+
};
396+
}
397+
return Promise.reject(PluginErrEnum.unExist);
398+
}
399+
334400
const dbPluginFormat = (item: SystemPluginConfigSchemaType): SystemPluginTemplateItemType => {
335401
const { name, avatar, intro, version, weight, templateType, associatedPluginId, userGuide } =
336402
item.customConfig!;
@@ -385,11 +451,11 @@ export const refetchSystemPlugins = () => {
385451
});
386452
};
387453

388-
export const getSystemPlugins = async (): Promise<SystemPluginTemplateItemType[]> => {
454+
export const getSystemTools = async (): Promise<SystemPluginTemplateItemType[]> => {
389455
if (getCachedSystemPlugins().expires > Date.now() && isProduction) {
390456
return getCachedSystemPlugins().data;
391457
} else {
392-
const tools = await getSystemToolList();
458+
const tools = await APIGetSystemToolList();
393459

394460
// 从数据库里加载插件配置进行替换
395461
const systemPluginsArray = await MongoSystemPlugin.find({}).lean();
@@ -418,33 +484,18 @@ export const getSystemPlugins = async (): Promise<SystemPluginTemplateItemType[]
418484
const outputs = item.versionList[0]?.outputs as FlowNodeOutputItemType[];
419485

420486
return {
421-
isActive: item.isActive,
422-
id: item.id,
423-
parentId: item.parentId,
487+
...item,
424488
isFolder: tools.some((tool) => tool.parentId === item.id),
425-
name: item.name,
426-
avatar: item.avatar,
427-
intro: item.intro,
428-
author: item.author,
429-
courseUrl: item.courseUrl,
430489
showStatus: true,
431-
weight: item.weight,
432-
templateType: item.templateType,
433-
originCost: item.originCost,
434-
currentCost: item.currentCost,
435-
hasTokenFee: item.hasTokenFee,
436-
pluginOrder: item.pluginOrder,
437-
438490
workflow: {
439491
nodes: [],
440492
edges: []
441493
},
442-
versionList: item.versionList,
443494
inputs,
444495
outputs,
445-
446-
inputList: inputs?.find((input) => input.key === NodeInputKeyEnum.systemInputConfig)
447-
?.inputList as any,
496+
inputList:
497+
inputs?.find((input) => input.key === NodeInputKeyEnum.systemInputConfig)?.inputList ??
498+
item?.inputConfig?.inputList,
448499
hasSystemSecret: !!dbPluginConfig?.inputListVal
449500
};
450501
});
@@ -465,10 +516,10 @@ export const getSystemPlugins = async (): Promise<SystemPluginTemplateItemType[]
465516
}
466517
};
467518

468-
export const getSystemPluginById = async (id: string): Promise<SystemPluginTemplateItemType> => {
519+
export const getSystemToolById = async (id: string): Promise<SystemPluginTemplateItemType> => {
469520
const { source, pluginId } = splitCombinePluginId(id);
470521
if (source === PluginSourceEnum.systemTool) {
471-
const tools = await getSystemPlugins();
522+
const tools = await getSystemTools();
472523
const tool = tools.find((item) => item.id === pluginId);
473524
if (tool) {
474525
return tool;

packages/service/core/app/tool/api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const client = createClient({
66
token: process.env.PLUGIN_TOKEN || ''
77
});
88

9-
export async function getSystemToolList() {
9+
export async function APIGetSystemToolList() {
1010
const res = await client.tool.list();
1111

1212
if (res.status === 200) {
@@ -26,7 +26,7 @@ export async function getSystemToolList() {
2626
return Promise.reject(res.body);
2727
}
2828

29-
export async function runTool({
29+
export async function APIRunTool({
3030
toolId,
3131
inputs,
3232
systemVar

packages/service/core/workflow/dispatch/ai/agent/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ export const dispatchRunTools = async (props: DispatchToolModuleProps): Promise<
7474
if (!fileUrlInput || !fileUrlInput.value || fileUrlInput.value.length === 0) {
7575
fileLinks = undefined;
7676
}
77-
console.log(fileLinks, 22);
7877

7978
const toolNodeIds = filterToolNodeIdByEdges({ nodeId, edges: runtimeEdges });
8079

@@ -358,7 +357,7 @@ const getMultiInput = async ({
358357
};
359358
};
360359

361-
/*
360+
/*
362361
Tool call, auth add file prompt to question。
363362
Guide the LLM to call tool.
364363
*/

packages/service/core/workflow/dispatch/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export async function dispatchWorkFlow(data: Props): Promise<DispatchFlowRespons
143143
} = data;
144144
const startTime = Date.now();
145145

146-
rewriteRuntimeWorkFlow(runtimeNodes, runtimeEdges);
146+
await rewriteRuntimeWorkFlow(runtimeNodes, runtimeEdges);
147147

148148
// 初始化深度和自动增加深度,避免无限嵌套
149149
if (!props.workflowDispatchDeep) {

packages/service/core/workflow/dispatch/plugin/run.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
5353
});
5454
}
5555

56-
/*
56+
/*
5757
1. Team app
5858
2. Admin selected system tool
5959
*/
@@ -66,7 +66,7 @@ export const dispatchRunPlugin = async (props: RunPluginProps): Promise<RunPlugi
6666
per: ReadPermissionVal
6767
});
6868

69-
const plugin = await getChildAppRuntimeById(pluginId, version);
69+
const plugin = await getChildAppRuntimeById({ id: pluginId, versionId: version });
7070

7171
const outputFilterMap =
7272
plugin.nodes

packages/service/core/workflow/dispatch/plugin/runTool.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
99
import { MCPClient } from '../../../app/mcp';
1010
import { getSecretValue } from '../../../../common/secret/utils';
1111
import type { McpToolDataType } from '@fastgpt/global/core/app/mcpTools/type';
12-
import { runTool } from '../../../app/tool/api';
12+
import { APIRunTool } from '../../../app/tool/api';
1313
import { MongoSystemPlugin } from '../../../app/plugin/systemPluginSchema';
1414
import { SystemToolInputTypeEnum } from '@fastgpt/global/core/app/systemTool/constants';
1515
import type { StoreSecretValueType } from '@fastgpt/global/common/secret/type';
16-
import { getSystemPluginById, splitCombinePluginId } from '../../../app/plugin/controller';
16+
import { getSystemToolById, splitCombinePluginId } from '../../../app/plugin/controller';
1717

1818
type SystemInputConfigType = {
1919
type: SystemToolInputTypeEnum;
@@ -42,10 +42,12 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
4242
node: { name, avatar, toolConfig, version }
4343
} = props;
4444

45+
console.log('run tool', params, props.node);
46+
4547
try {
4648
// run system tool
4749
if (toolConfig?.systemTool?.toolId) {
48-
const tool = await getSystemPluginById(toolConfig.systemTool!.toolId);
50+
const tool = await getSystemToolById(toolConfig.systemTool!.toolId);
4951

5052
const inputConfigParams = await (async () => {
5153
switch (params.system_input_config?.type) {
@@ -73,7 +75,7 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
7375
};
7476

7577
const formatToolId = tool.id.split('-')[1];
76-
const result = await runTool({
78+
const result = await APIRunTool({
7779
toolId: formatToolId,
7880
inputs,
7981
systemVar: {

0 commit comments

Comments
 (0)