Skip to content

Commit 9ed0e2b

Browse files
committed
perf: system tool support sse
1 parent e18d3f4 commit 9ed0e2b

File tree

7 files changed

+74
-51
lines changed

7 files changed

+74
-51
lines changed

docSite/content/zh-cn/docs/development/upgrading/4101.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ curl --location --request POST 'https://{{host}}/api/admin/initv4101' \
3232

3333
- 给自动同步的知识库加入新的定时任务。
3434

35+
## 🚀 新增内容
36+
37+
1. 系统工具支持流输出。
38+
3539
## ⚙️ 优化
3640

3741
1. 定时任务报错日志记录到对话日志。

packages/global/common/middle/tracks/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export enum TrackEnum {
33
createApp = 'createApp',
44
useAppTemplate = 'useAppTemplate',
55
createDataset = 'createDataset',
6-
appNodes = 'appNodes'
6+
appNodes = 'appNodes',
7+
runSystemTool = 'runSystemTool'
78
}

packages/service/common/middle/tracks/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { type ShortUrlParams } from '@fastgpt/global/support/marketing/type';
1010

1111
const createTrack = ({ event, data }: { event: TrackEnum; data: Record<string, any> }) => {
1212
if (!global.feConfigs?.isPlus) return;
13-
addLog.info('Push tracks', {
13+
addLog.debug('Push tracks', {
1414
event,
1515
...data
1616
});
@@ -65,5 +65,13 @@ export const pushTrack = {
6565
}
6666
});
6767
} catch (error) {}
68+
},
69+
runSystemTool: (
70+
data: PushTrackCommonType & { toolId: string; result: 1 | 0; usagePoint?: number; msg?: string }
71+
) => {
72+
return createTrack({
73+
event: TrackEnum.runSystemTool,
74+
data
75+
});
6876
}
6977
};

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

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import createClient, { type SystemVarType } from '@fastgpt-sdk/plugin';
1+
import createClient, { RunToolWithStream } from '@fastgpt-sdk/plugin';
22
import { PluginSourceEnum } from '@fastgpt/global/core/app/plugin/constants';
3-
import { runToolStream, type StreamDataAnswerTypeEnum } from '@fastgpt-sdk/plugin';
43

5-
const PluginClientConfig = {
6-
baseUrl: process.env.PLUGIN_BASE_URL || '',
7-
token: process.env.PLUGIN_TOKEN || ''
8-
};
9-
const client = createClient(PluginClientConfig);
4+
const BASE_URL = process.env.PLUGIN_BASE_URL || '';
5+
const TOKEN = process.env.PLUGIN_TOKEN || '';
6+
7+
const client = createClient({
8+
baseUrl: BASE_URL,
9+
token: TOKEN
10+
});
1011

1112
export async function getSystemToolList() {
1213
const res = await client.tool.list();
@@ -28,18 +29,8 @@ export async function getSystemToolList() {
2829
return Promise.reject(res.body);
2930
}
3031

31-
export async function runPluginToolStream(
32-
toolId: string,
33-
inputs: Record<string, any>,
34-
systemVar: SystemVarType,
35-
onStreamData: (type: StreamDataAnswerTypeEnum, data: string) => void
36-
) {
37-
return await runToolStream({
38-
baseUrl: PluginClientConfig.baseUrl,
39-
authtoken: PluginClientConfig.token,
40-
toolId,
41-
inputs,
42-
systemVar,
43-
onStreamData
44-
});
45-
}
32+
const runToolInstance = new RunToolWithStream({
33+
baseUrl: BASE_URL,
34+
token: TOKEN
35+
});
36+
export const runSystemTool = runToolInstance.run.bind(runToolInstance);

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

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ import { NodeInputKeyEnum } from '@fastgpt/global/core/workflow/constants';
1010
import { MCPClient } from '../../../app/mcp';
1111
import { getSecretValue } from '../../../../common/secret/utils';
1212
import type { McpToolDataType } from '@fastgpt/global/core/app/mcpTools/type';
13-
import { StreamDataAnswerTypeEnum } from '@fastgpt-sdk/plugin';
14-
import { runPluginToolStream } from '../../../app/tool/api';
13+
import { runSystemTool } from '../../../app/tool/api';
1514
import { MongoSystemPlugin } from '../../../app/plugin/systemPluginSchema';
1615
import { SystemToolInputTypeEnum } from '@fastgpt/global/core/app/systemTool/constants';
1716
import type { StoreSecretValueType } from '@fastgpt/global/common/secret/type';
18-
import { getSystemPluginById, splitCombinePluginId } from '../../../app/plugin/controller';
17+
import { getSystemPluginById } from '../../../app/plugin/controller';
1918
import { textAdaptGptResponse } from '@fastgpt/global/core/workflow/runtime/utils';
19+
import { pushTrack } from '../../../../common/middle/tracks/utils';
2020

2121
type SystemInputConfigType = {
2222
type: SystemToolInputTypeEnum;
@@ -46,10 +46,12 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
4646
node: { name, avatar, toolConfig, version }
4747
} = props;
4848

49+
const systemToolId = toolConfig?.systemTool?.toolId;
50+
4951
try {
5052
// run system tool
51-
if (toolConfig?.systemTool?.toolId) {
52-
const tool = await getSystemPluginById(toolConfig.systemTool!.toolId);
53+
if (systemToolId) {
54+
const tool = await getSystemPluginById(systemToolId);
5355

5456
const inputConfigParams = await (async () => {
5557
switch (params.system_input_config?.type) {
@@ -79,10 +81,10 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
7981
const formatToolId = tool.id.split('-')[1];
8082

8183
const result = await (async () => {
82-
const res = await runPluginToolStream(
83-
formatToolId,
84+
const res = await runSystemTool({
85+
toolId: formatToolId,
8486
inputs,
85-
{
87+
systemVar: {
8688
user: {
8789
id: variables.userId,
8890
teamId: runningUserInfo.teamId,
@@ -98,31 +100,26 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
98100
},
99101
time: variables.cTime
100102
},
101-
(type, data) => {
102-
if (
103-
workflowStreamResponse &&
104-
data &&
105-
(type === StreamDataAnswerTypeEnum.Answer ||
106-
type === StreamDataAnswerTypeEnum.FastAnswer)
107-
) {
103+
onMessage: ({ type, content }) => {
104+
if (workflowStreamResponse && content) {
108105
workflowStreamResponse({
109-
event: type as SseResponseEventEnum,
106+
event: type as unknown as SseResponseEventEnum,
110107
data: textAdaptGptResponse({
111-
text: data
108+
text: content
112109
})
113110
});
114111
}
115112
}
116-
);
117-
if ('error' in res) {
113+
});
114+
if (res.error) {
118115
return Promise.reject(res.error);
119116
}
120117
if (!res.output) return {};
121118

122119
return res.output;
123120
})();
124121

125-
const usagePoints = await (async () => {
122+
const usagePoints = (() => {
126123
if (
127124
params.system_input_config?.type !== SystemToolInputTypeEnum.system ||
128125
result[NodeOutputKeyEnum.systemError]
@@ -132,6 +129,16 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
132129
return tool.currentCost ?? 0;
133130
})();
134131

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+
135142
return {
136143
[DispatchNodeResponseKeyEnum.nodeResponse]: {
137144
toolRes: result,
@@ -170,6 +177,17 @@ export const dispatchRunTool = async (props: RunToolProps): Promise<RunToolRespo
170177
};
171178
}
172179
} 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+
173191
return {
174192
[DispatchNodeResponseKeyEnum.nodeResponse]: {
175193
moduleLogo: avatar,

packages/service/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "1.0.0",
44
"type": "module",
55
"dependencies": {
6-
"@fastgpt-sdk/plugin": "^0.1.0",
6+
"@fastgpt-sdk/plugin": "^0.1.1",
77
"@fastgpt/global": "workspace:*",
88
"@modelcontextprotocol/sdk": "^1.12.1",
99
"@node-rs/jieba": "2.0.1",

pnpm-lock.yaml

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)