mirror of
https://github.com/labring/FastGPT.git
synced 2025-10-14 15:11:13 +00:00
Feat: system tool support stream response (#5206)
* Match SSE for FastGPT (#5168) * Match SSE for FastGPT * Modify the judgment * Optimize logic for SSE transmission * Refactor imports * directly use workflowStreamResponse from props * improve error handling and streamline onStreamData logic * Refactor API client configuration * perf: system tool support sse * update doc --------- Co-authored-by: Zhuangzai fa <143257420+ctrlz526@users.noreply.github.com>
This commit is contained in:
@@ -3,5 +3,6 @@ export enum TrackEnum {
|
||||
createApp = 'createApp',
|
||||
useAppTemplate = 'useAppTemplate',
|
||||
createDataset = 'createDataset',
|
||||
appNodes = 'appNodes'
|
||||
appNodes = 'appNodes',
|
||||
runSystemTool = 'runSystemTool'
|
||||
}
|
||||
|
@@ -10,7 +10,7 @@ import { type ShortUrlParams } from '@fastgpt/global/support/marketing/type';
|
||||
|
||||
const createTrack = ({ event, data }: { event: TrackEnum; data: Record<string, any> }) => {
|
||||
if (!global.feConfigs?.isPlus) return;
|
||||
addLog.info('Push tracks', {
|
||||
addLog.debug('Push tracks', {
|
||||
event,
|
||||
...data
|
||||
});
|
||||
@@ -65,5 +65,13 @@ export const pushTrack = {
|
||||
}
|
||||
});
|
||||
} catch (error) {}
|
||||
},
|
||||
runSystemTool: (
|
||||
data: PushTrackCommonType & { toolId: string; result: 1 | 0; usagePoint?: number; msg?: string }
|
||||
) => {
|
||||
return createTrack({
|
||||
event: TrackEnum.runSystemTool,
|
||||
data
|
||||
});
|
||||
}
|
||||
};
|
||||
|
@@ -1,9 +1,12 @@
|
||||
import createClient, { type SystemVarType } from '@fastgpt-sdk/plugin';
|
||||
import createClient, { RunToolWithStream } from '@fastgpt-sdk/plugin';
|
||||
import { PluginSourceEnum } from '@fastgpt/global/core/app/plugin/constants';
|
||||
|
||||
const BASE_URL = process.env.PLUGIN_BASE_URL || '';
|
||||
const TOKEN = process.env.PLUGIN_TOKEN || '';
|
||||
|
||||
const client = createClient({
|
||||
baseUrl: process.env.PLUGIN_BASE_URL || '',
|
||||
token: process.env.PLUGIN_TOKEN || ''
|
||||
baseUrl: BASE_URL,
|
||||
token: TOKEN
|
||||
});
|
||||
|
||||
export async function getSystemToolList() {
|
||||
@@ -26,26 +29,8 @@ export async function getSystemToolList() {
|
||||
return Promise.reject(res.body);
|
||||
}
|
||||
|
||||
export async function runTool({
|
||||
toolId,
|
||||
inputs,
|
||||
systemVar
|
||||
}: {
|
||||
toolId: string;
|
||||
inputs: Record<string, any>;
|
||||
systemVar: SystemVarType;
|
||||
}) {
|
||||
const res = await client.tool.run({
|
||||
body: {
|
||||
toolId,
|
||||
inputs,
|
||||
systemVar
|
||||
}
|
||||
});
|
||||
|
||||
if (res.status === 200 && res.body.output) {
|
||||
return res.body.output;
|
||||
} else {
|
||||
return Promise.reject(res.body);
|
||||
}
|
||||
}
|
||||
const runToolInstance = new RunToolWithStream({
|
||||
baseUrl: BASE_URL,
|
||||
token: TOKEN
|
||||
});
|
||||
export const runSystemTool = runToolInstance.run.bind(runToolInstance);
|
||||
|
@@ -1,5 +1,6 @@
|
||||
import { getErrText } from '@fastgpt/global/common/error/utils';
|
||||
import { NodeOutputKeyEnum } from '@fastgpt/global/core/workflow/constants';
|
||||
import type { SseResponseEventEnum } from '@fastgpt/global/core/workflow/runtime/constants';
|
||||
import { DispatchNodeResponseKeyEnum } from '@fastgpt/global/core/workflow/runtime/constants';
|
||||
import {
|
||||
type DispatchNodeResultType,
|
||||
@@ -9,11 +10,13 @@ import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
|
||||
import { MCPClient } from '../../../app/mcp';
|
||||
import { getSecretValue } from '../../../../common/secret/utils';
|
||||
import type { McpToolDataType } from '@fastgpt/global/core/app/mcpTools/type';
|
||||
import { runTool } from '../../../app/tool/api';
|
||||
import { runSystemTool } from '../../../app/tool/api';
|
||||
import { MongoSystemPlugin } from '../../../app/plugin/systemPluginSchema';
|
||||
import { SystemToolInputTypeEnum } from '@fastgpt/global/core/app/systemTool/constants';
|
||||
import type { StoreSecretValueType } from '@fastgpt/global/common/secret/type';
|
||||
import { getSystemPluginById, splitCombinePluginId } from '../../../app/plugin/controller';
|
||||
import { getSystemPluginById } from '../../../app/plugin/controller';
|
||||
import { textAdaptGptResponse } from '@fastgpt/global/core/workflow/runtime/utils';
|
||||
import { pushTrack } from '../../../../common/middle/tracks/utils';
|
||||
|
||||
type SystemInputConfigType = {
|
||||
type: SystemToolInputTypeEnum;
|
||||
@@ -39,13 +42,16 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
|
||||
runningUserInfo,
|
||||
runningAppInfo,
|
||||
variables,
|
||||
workflowStreamResponse,
|
||||
node: { name, avatar, toolConfig, version }
|
||||
} = props;
|
||||
|
||||
const systemToolId = toolConfig?.systemTool?.toolId;
|
||||
|
||||
try {
|
||||
// run system tool
|
||||
if (toolConfig?.systemTool?.toolId) {
|
||||
const tool = await getSystemPluginById(toolConfig.systemTool!.toolId);
|
||||
if (systemToolId) {
|
||||
const tool = await getSystemPluginById(systemToolId);
|
||||
|
||||
const inputConfigParams = await (async () => {
|
||||
switch (params.system_input_config?.type) {
|
||||
@@ -73,28 +79,47 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
|
||||
};
|
||||
|
||||
const formatToolId = tool.id.split('-')[1];
|
||||
const result = await runTool({
|
||||
toolId: formatToolId,
|
||||
inputs,
|
||||
systemVar: {
|
||||
user: {
|
||||
id: variables.userId,
|
||||
teamId: runningUserInfo.teamId,
|
||||
name: runningUserInfo.tmbId
|
||||
},
|
||||
app: {
|
||||
id: runningAppInfo.id,
|
||||
name: runningAppInfo.id
|
||||
},
|
||||
tool: {
|
||||
id: formatToolId,
|
||||
version
|
||||
},
|
||||
time: variables.cTime
|
||||
}
|
||||
});
|
||||
|
||||
const usagePoints = await (async () => {
|
||||
const result = await (async () => {
|
||||
const res = await runSystemTool({
|
||||
toolId: formatToolId,
|
||||
inputs,
|
||||
systemVar: {
|
||||
user: {
|
||||
id: variables.userId,
|
||||
teamId: runningUserInfo.teamId,
|
||||
name: runningUserInfo.tmbId
|
||||
},
|
||||
app: {
|
||||
id: runningAppInfo.id,
|
||||
name: runningAppInfo.id
|
||||
},
|
||||
tool: {
|
||||
id: formatToolId,
|
||||
version
|
||||
},
|
||||
time: variables.cTime
|
||||
},
|
||||
onMessage: ({ type, content }) => {
|
||||
if (workflowStreamResponse && content) {
|
||||
workflowStreamResponse({
|
||||
event: type as unknown as SseResponseEventEnum,
|
||||
data: textAdaptGptResponse({
|
||||
text: content
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
if (res.error) {
|
||||
return Promise.reject(res.error);
|
||||
}
|
||||
if (!res.output) return {};
|
||||
|
||||
return res.output;
|
||||
})();
|
||||
|
||||
const usagePoints = (() => {
|
||||
if (
|
||||
params.system_input_config?.type !== SystemToolInputTypeEnum.system ||
|
||||
result[NodeOutputKeyEnum.systemError]
|
||||
@@ -104,6 +129,16 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
|
||||
return tool.currentCost ?? 0;
|
||||
})();
|
||||
|
||||
pushTrack.runSystemTool({
|
||||
teamId: runningUserInfo.teamId,
|
||||
tmbId: runningUserInfo.tmbId,
|
||||
uid: runningUserInfo.tmbId,
|
||||
toolId: tool.id,
|
||||
result: 1,
|
||||
usagePoint: usagePoints,
|
||||
msg: result[NodeOutputKeyEnum.systemError]
|
||||
});
|
||||
|
||||
return {
|
||||
[DispatchNodeResponseKeyEnum.nodeResponse]: {
|
||||
toolRes: result,
|
||||
@@ -142,6 +177,17 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
if (systemToolId) {
|
||||
pushTrack.runSystemTool({
|
||||
teamId: runningUserInfo.teamId,
|
||||
tmbId: runningUserInfo.tmbId,
|
||||
uid: runningUserInfo.tmbId,
|
||||
toolId: systemToolId,
|
||||
result: 0,
|
||||
msg: getErrText(error)
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
[DispatchNodeResponseKeyEnum.nodeResponse]: {
|
||||
moduleLogo: avatar,
|
||||
|
@@ -3,7 +3,7 @@
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@fastgpt-sdk/plugin": "^0.1.0",
|
||||
"@fastgpt-sdk/plugin": "^0.1.1",
|
||||
"@fastgpt/global": "workspace:*",
|
||||
"@modelcontextprotocol/sdk": "^1.12.1",
|
||||
"@node-rs/jieba": "2.0.1",
|
||||
|
Reference in New Issue
Block a user