Skip to content

Commit 596f6f5

Browse files
committed
feature/1.7-MCP
1 parent 7f83f9e commit 596f6f5

1 file changed

Lines changed: 70 additions & 10 deletions

File tree

frontend/packages/core/src/pages/mcpService/IntegrationAIContainer.tsx

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,45 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
7272
currentTab,
7373
openModal
7474
}: IntegrationAIContainerProps, ref) => {
75+
/** 当前激活的标签 */
7576
const [activeTab, setActiveTab] = useState(type === 'service' ? 'openApi' : 'mcp')
77+
/** 弹窗组件 */
7678
const { message } = App.useApp()
79+
/** 配置内容 */
7780
const [configContent, setConfigContent] = useState<string>('')
81+
/** 当前选中 API Key */
7882
const [apiKey, setApiKey] = useState<string>('')
83+
/** API Key 列表 */
7984
const [apiKeyList, setApiKeyList] = useState<any[]>([])
85+
/** Cascader Key 列表 */
8086
const [cascaderKeyList, setCascaderKeyList] = useState<string[]>([])
87+
/** MCP 服务器地址 */
8188
const [mcpServerUrl, setMcpServerUrl] = useState<string>('')
89+
/** 全局状态 */
8290
const { state } = useGlobalContext()
8391
const navigator = useNavigate()
92+
/** 复制组件 */
8493
const { copyToClipboard } = useCopyToClipboard()
94+
/** 错误提示 */
8595
const [errors, setErrors] = useState<Record<string, string | null>>({
8696
resources: null,
8797
prompts: null,
8898
tools: null
8999
})
90-
100+
/** 标签内容 */
91101
const [tabContent, setTabContent] = useState<ConfigList>({
92102
mcp: {
93103
title: $t('MCP 配置'),
94104
configContent: '',
95105
apiKeys: []
96106
}
97107
})
108+
/** HTTP 请求 */
98109
const { fetchData } = useFetch()
99110

111+
/**
112+
* 初始化标签数据
113+
*/
100114
const initTabsData = () => {
101115
const params: ConfigList = {
102116
mcp: {
@@ -127,10 +141,18 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
127141
}
128142
}
129143

144+
/**
145+
* 选择 API Key
146+
* @param value
147+
*/
130148
const handleSelectChange = (value: string) => {
131149
setApiKey(value)
132150
}
133-
const handleChange: CascaderProps<Option>['onChange'] = (value) => {
151+
/**
152+
* Cascader 选择
153+
* @param value
154+
*/
155+
const handleCascaderChange: CascaderProps<Option>['onChange'] = (value) => {
134156
setApiKey(value.at(-1) || '')
135157
setCascaderKeyList(value)
136158
}
@@ -162,6 +184,9 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
162184
})
163185
}
164186

187+
/**
188+
* 全局 MCP 跳转
189+
*/
165190
const addKey = () => {
166191
navigator('/mcpKey')
167192
}
@@ -195,9 +220,14 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
195220
message.error(errorInfo || $t(RESPONSE_TIPS.error))
196221
})
197222
}
223+
224+
/**
225+
* 抛出获取服务 API Key 列表
226+
*/
198227
useImperativeHandle(ref, () => ({
199228
getServiceKeysList
200229
}))
230+
201231
/**
202232
* 获取服务 API Key 列表
203233
*/
@@ -232,10 +262,16 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
232262
})
233263
}
234264

265+
/**
266+
* 清除错误提示
267+
*/
235268
const clearError = (tabKey: keyof typeof errors) => {
236269
setErrors((prev) => ({ ...prev, [tabKey]: null }))
237270
}
238271

272+
/**
273+
* 发送请求
274+
*/
239275
const makeRequest = async <T extends z.ZodType>(request: ClientRequest, schema: T, tabKey?: keyof typeof errors) => {
240276
try {
241277
const response = await makeConnectionRequest(request, schema)
@@ -255,6 +291,9 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
255291
}
256292
}
257293

294+
/**
295+
* 获取 MCP 的 tools
296+
*/
258297
const listTools = async () => {
259298
const response = await makeRequest(
260299
{
@@ -267,6 +306,9 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
267306
handleToolsChange(response.tools)
268307
}
269308

309+
/**
310+
* 初始化连接 mcp
311+
*/
270312
const {
271313
connectionStatus,
272314
serverCapabilities,
@@ -294,34 +336,44 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
294336
disconnectFnRef.current = disconnectMcpServer
295337
}, [connectionStatus, disconnectMcpServer])
296338

339+
/**
340+
* 初始化数据
341+
*/
297342
const setupComponent = () => {
298343
initTabsData()
299344
if (type === 'global') {
300345
getGlobalMcpConfig()
301346
setMcpServerUrl('mcp/global/sse')
302-
} else {
303-
service?.basic.enableMcp && setMcpServerUrl(`mcp/service/${serviceId}/sse`)
304-
}
305-
if (type === 'global') {
306347
getGlobalKeysList()
307348
} else {
349+
service?.basic.enableMcp && setMcpServerUrl(`mcp/service/${serviceId}/sse`)
308350
getServiceKeysList()
309351
}
310352
}
311-
353+
/**
354+
* 初始化数据
355+
*/
312356
useEffect(() => {
313357
setupComponent()
314358
}, [service])
359+
/**
360+
* 初始化标签数据
361+
*/
315362
useEffect(() => {
316363
initTabsData()
317364
type === 'global' && getGlobalMcpConfig()
318365
}, [state.language])
366+
/**
367+
* 切换标签
368+
*/
319369
useEffect(() => {
320370
if (type === 'service') {
321371
currentTab === 'MCP' ? setActiveTab('mcp') : setActiveTab('openApi')
322372
}
323373
}, [currentTab])
324-
// 仅在组件加载时执行初始化逻辑
374+
/**
375+
* 仅在组件加载时执行初始化逻辑
376+
*/
325377
useEffect(() => {
326378
// 返回清理函数,只会在组件卸载时执行
327379
return () => {
@@ -336,14 +388,19 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
336388
}
337389
}
338390
}, [type])
391+
/**
392+
* 切换标签时更新配置内容
393+
*/
339394
useEffect(() => {
340395
if (activeTab === 'openApi' && tabContent?.openApi?.configContent) {
341396
setConfigContent(tabContent?.openApi?.configContent)
342397
} else if (activeTab === 'mcp' && tabContent?.mcp?.configContent) {
343398
setConfigContent(tabContent.mcp.configContent?.replace('{your_api_key}', apiKey || '{your_api_key}'))
344399
}
345400
}, [service, apiKey, activeTab, tabContent])
346-
401+
/**
402+
* 连接 MCP 服务器
403+
*/
347404
useEffect(() => {
348405
if (mcpServerUrl) {
349406
if (connectionStatus === 'connected') {
@@ -352,6 +409,9 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
352409
connectMcpServer()
353410
}
354411
}, [mcpServerUrl, ...(type === 'global' ? [state.language] : [])])
412+
/**
413+
* 获取 MCP tools
414+
*/
355415
useEffect(() => {
356416
if (connectionStatus === 'connected') {
357417
listTools()
@@ -518,7 +578,7 @@ export const IntegrationAIContainer = forwardRef<IntegrationAIContainerRef, Inte
518578
allowClear={false}
519579
options={apiKeyList}
520580
value={cascaderKeyList}
521-
onChange={handleChange}
581+
onChange={handleCascaderChange}
522582
placeholder={$t('选择 API Key')}
523583
/>
524584
</>

0 commit comments

Comments
 (0)