11import { getErrText } from '@fastgpt/global/common/error/utils' ;
22import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants' ;
3+ import type { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants' ;
34import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants' ;
45import {
56 type DispatchNodeResultType ,
@@ -9,11 +10,13 @@ import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
910import { MCPClient } from '../../../app/mcp' ;
1011import { getSecretValue } from '../../../../common/secret/utils' ;
1112import type { McpToolDataType } from '@fastgpt/global/core/app/mcpTools/type' ;
12- import { runTool } from '../../../app/tool/api' ;
13+ import { runSystemTool } from '../../../app/tool/api' ;
1314import { MongoSystemPlugin } from '../../../app/plugin/systemPluginSchema' ;
1415import { SystemToolInputTypeEnum } from '@fastgpt/global/core/app/systemTool/constants' ;
1516import type { StoreSecretValueType } from '@fastgpt/global/common/secret/type' ;
16- import { getSystemPluginById , splitCombinePluginId } from '../../../app/plugin/controller' ;
17+ import { getSystemPluginById } from '../../../app/plugin/controller' ;
18+ import { textAdaptGptResponse } from '@fastgpt/global/core/workflow/runtime/utils' ;
19+ import { pushTrack } from '../../../../common/middle/tracks/utils' ;
1720
1821type SystemInputConfigType = {
1922 type : SystemToolInputTypeEnum ;
@@ -39,13 +42,16 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
3942 runningUserInfo,
4043 runningAppInfo,
4144 variables,
45+ workflowStreamResponse,
4246 node : { name, avatar, toolConfig, version }
4347 } = props ;
4448
49+ const systemToolId = toolConfig ?. systemTool ?. toolId ;
50+
4551 try {
4652 // run system tool
47- if ( toolConfig ?. systemTool ?. toolId ) {
48- const tool = await getSystemPluginById ( toolConfig . systemTool ! . toolId ) ;
53+ if ( systemToolId ) {
54+ const tool = await getSystemPluginById ( systemToolId ) ;
4955
5056 const inputConfigParams = await ( async ( ) => {
5157 switch ( params . system_input_config ?. type ) {
@@ -73,28 +79,47 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
7379 } ;
7480
7581 const formatToolId = tool . id . split ( '-' ) [ 1 ] ;
76- const result = await runTool ( {
77- toolId : formatToolId ,
78- inputs,
79- systemVar : {
80- user : {
81- id : variables . userId ,
82- teamId : runningUserInfo . teamId ,
83- name : runningUserInfo . tmbId
84- } ,
85- app : {
86- id : runningAppInfo . id ,
87- name : runningAppInfo . id
88- } ,
89- tool : {
90- id : formatToolId ,
91- version
82+
83+ const result = await ( async ( ) => {
84+ const res = await runSystemTool ( {
85+ toolId : formatToolId ,
86+ inputs,
87+ systemVar : {
88+ user : {
89+ id : variables . userId ,
90+ teamId : runningUserInfo . teamId ,
91+ name : runningUserInfo . tmbId
92+ } ,
93+ app : {
94+ id : runningAppInfo . id ,
95+ name : runningAppInfo . id
96+ } ,
97+ tool : {
98+ id : formatToolId ,
99+ version
100+ } ,
101+ time : variables . cTime
92102 } ,
93- time : variables . cTime
103+ onMessage : ( { type, content } ) => {
104+ if ( workflowStreamResponse && content ) {
105+ workflowStreamResponse ( {
106+ event : type as unknown as SseResponseEventEnum ,
107+ data : textAdaptGptResponse ( {
108+ text : content
109+ } )
110+ } ) ;
111+ }
112+ }
113+ } ) ;
114+ if ( res . error ) {
115+ return Promise . reject ( res . error ) ;
94116 }
95- } ) ;
117+ if ( ! res . output ) return { } ;
96118
97- const usagePoints = await ( async ( ) => {
119+ return res . output ;
120+ } ) ( ) ;
121+
122+ const usagePoints = ( ( ) => {
98123 if (
99124 params . system_input_config ?. type !== SystemToolInputTypeEnum . system ||
100125 result [ NodeOutputKeyEnum . systemError ]
@@ -104,6 +129,16 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
104129 return tool . currentCost ?? 0 ;
105130 } ) ( ) ;
106131
132+ pushTrack . runSystemTool ( {
133+ teamId : runningUserInfo . teamId ,
134+ tmbId : runningUserInfo . tmbId ,
135+ uid : runningUserInfo . tmbId ,
136+ toolId : tool . id ,
137+ result : 1 ,
138+ usagePoint : usagePoints ,
139+ msg : result [ NodeOutputKeyEnum . systemError ]
140+ } ) ;
141+
107142 return {
108143 [ DispatchNodeResponseKeyEnum . nodeResponse ] : {
109144 toolRes : result ,
@@ -142,6 +177,17 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
142177 } ;
143178 }
144179 } catch ( error ) {
180+ if ( systemToolId ) {
181+ pushTrack . runSystemTool ( {
182+ teamId : runningUserInfo . teamId ,
183+ tmbId : runningUserInfo . tmbId ,
184+ uid : runningUserInfo . tmbId ,
185+ toolId : systemToolId ,
186+ result : 0 ,
187+ msg : getErrText ( error )
188+ } ) ;
189+ }
190+
145191 return {
146192 [ DispatchNodeResponseKeyEnum . nodeResponse ] : {
147193 moduleLogo : avatar ,
0 commit comments