forked from higress-group/himarket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiProductLinkApi.tsx
More file actions
1899 lines (1775 loc) · 79 KB
/
ApiProductLinkApi.tsx
File metadata and controls
1899 lines (1775 loc) · 79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { Card, Button, Modal, Form, Select, message, Collapse, Tabs, Row, Col } from 'antd'
import { PlusOutlined, DeleteOutlined, ExclamationCircleOutlined, CopyOutlined } from '@ant-design/icons'
import { useState, useEffect } from 'react'
import type { ApiProduct, LinkedService, RestAPIItem, NacosMCPItem, APIGAIMCPItem, AIGatewayAgentItem, AIGatewayModelItem, ApiItem, AdpAIGatewayModelItem } from '@/types/api-product'
import type { Gateway, NacosInstance } from '@/types/gateway'
import { apiProductApi, gatewayApi, nacosApi } from '@/lib/api'
import { getGatewayTypeLabel } from '@/lib/constant'
import { copyToClipboard, formatDomainWithPort } from '@/lib/utils'
import * as yaml from 'js-yaml'
import { SwaggerUIWrapper } from './SwaggerUIWrapper'
interface ApiProductLinkApiProps {
apiProduct: ApiProduct
linkedService: LinkedService | null
onLinkedServiceUpdate: (linkedService: LinkedService | null) => void
handleRefresh: () => void
}
export function ApiProductLinkApi({ apiProduct, linkedService, onLinkedServiceUpdate, handleRefresh }: ApiProductLinkApiProps) {
// 移除了内部的 linkedService 状态,现在从 props 接收
const [isModalVisible, setIsModalVisible] = useState(false)
const [form] = Form.useForm()
const [gateways, setGateways] = useState<Gateway[]>([])
const [nacosInstances, setNacosInstances] = useState<NacosInstance[]>([])
const [gatewayLoading, setGatewayLoading] = useState(false)
const [nacosLoading, setNacosLoading] = useState(false)
const [selectedGateway, setSelectedGateway] = useState<Gateway | null>(null)
const [selectedNacos, setSelectedNacos] = useState<NacosInstance | null>(null)
const [nacosNamespaces, setNacosNamespaces] = useState<any[]>([])
const [selectedNamespace, setSelectedNamespace] = useState<string | null>(null)
const [apiList, setApiList] = useState<ApiItem[] | NacosMCPItem[]>([])
const [apiLoading, setApiLoading] = useState(false)
const [sourceType, setSourceType] = useState<'GATEWAY' | 'NACOS'>('GATEWAY')
const [parsedTools, setParsedTools] = useState<Array<{
name: string;
description: string;
args?: Array<{
name: string;
description: string;
type: string;
required: boolean;
position: string;
default?: string;
enum?: string[];
}>;
}>>([])
const [httpJson, setHttpJson] = useState('')
const [sseJson, setSseJson] = useState('')
const [localJson, setLocalJson] = useState('')
const [selectedDomainIndex, setSelectedDomainIndex] = useState<number>(0)
const [selectedAgentDomainIndex, setSelectedAgentDomainIndex] = useState<number>(0)
const [selectedModelDomainIndex, setSelectedModelDomainIndex] = useState<number>(0)
useEffect(() => {
fetchGateways()
fetchNacosInstances()
}, [])
// 解析MCP tools配置
useEffect(() => {
if (apiProduct.type === 'MCP_SERVER' && apiProduct.mcpConfig?.tools) {
const parsedConfig = parseYamlConfig(apiProduct.mcpConfig.tools)
if (parsedConfig && parsedConfig.tools && Array.isArray(parsedConfig.tools)) {
setParsedTools(parsedConfig.tools)
} else {
// 如果tools字段存在但是空数组,也设置为空数组
setParsedTools([])
}
} else {
setParsedTools([])
}
}, [apiProduct])
// 生成连接配置
// 当产品切换时重置域名选择索引
useEffect(() => {
setSelectedDomainIndex(0);
setSelectedAgentDomainIndex(0);
setSelectedModelDomainIndex(0);
}, [apiProduct.productId]);
useEffect(() => {
if (apiProduct.type === 'MCP_SERVER' && apiProduct.mcpConfig) {
// 获取关联的MCP Server名称
let mcpServerName = apiProduct.name // 默认使用产品名称
if (linkedService) {
// 从linkedService中获取真实的MCP Server名称
if (linkedService.sourceType === 'GATEWAY' && linkedService.apigRefConfig && 'mcpServerName' in linkedService.apigRefConfig) {
mcpServerName = linkedService.apigRefConfig.mcpServerName || apiProduct.name
} else if (linkedService.sourceType === 'GATEWAY' && linkedService.higressRefConfig) {
mcpServerName = linkedService.higressRefConfig.mcpServerName || apiProduct.name
} else if (linkedService.sourceType === 'GATEWAY' && linkedService.adpAIGatewayRefConfig) {
// 检查是否是 AdpAIGatewayModelItem 类型(有 modelApiName 属性)
if ('modelApiName' in linkedService.adpAIGatewayRefConfig) {
mcpServerName = linkedService.adpAIGatewayRefConfig.modelApiName || apiProduct.name
} else {
// APIGAIMCPItem 类型
mcpServerName = linkedService.adpAIGatewayRefConfig.mcpServerName || apiProduct.name
}
} else if (linkedService.sourceType === 'NACOS' && linkedService.nacosRefConfig && 'mcpServerName' in linkedService.nacosRefConfig) {
mcpServerName = linkedService.nacosRefConfig.mcpServerName || apiProduct.name
}
}
generateConnectionConfig(
apiProduct.mcpConfig.mcpServerConfig.domains,
apiProduct.mcpConfig.mcpServerConfig.path,
mcpServerName,
apiProduct.mcpConfig.mcpServerConfig.rawConfig,
apiProduct.mcpConfig.meta?.protocol,
selectedDomainIndex
)
}
}, [apiProduct, linkedService, selectedDomainIndex])
// 生成域名选项的函数
const getDomainOptions = (domains: Array<{ domain: string; port?: number; protocol: string; networkType?: string }>) => {
return domains.map((domain, index) => {
const formattedDomain = formatDomainWithPort(domain.domain, domain.port, domain.protocol);
return {
value: index,
label: `${domain.protocol}://${formattedDomain}`,
domain: domain
}
})
}
// 解析YAML配置的函数
const parseYamlConfig = (yamlString: string): {
tools?: Array<{
name: string;
description: string;
args?: Array<{
name: string;
description: string;
type: string;
required: boolean;
position: string;
default?: string;
enum?: string[];
}>;
}>;
} | null => {
try {
const parsed = yaml.load(yamlString) as {
tools?: Array<{
name: string;
description: string;
args?: Array<{
name: string;
description: string;
type: string;
required: boolean;
position: string;
default?: string;
enum?: string[];
}>;
}>;
};
return parsed;
} catch (error) {
console.error('YAML解析失败:', error)
return null
}
}
// 生成连接配置
const generateConnectionConfig = (
domains: Array<{ domain: string; port?: number; protocol: string }> | null | undefined,
path: string | null | undefined,
serverName: string,
localConfig?: unknown,
protocolType?: string,
domainIndex: number = 0
) => {
// 互斥:优先判断本地模式
if (localConfig) {
const localConfigJson = JSON.stringify(localConfig, null, 2);
setLocalJson(localConfigJson);
setHttpJson("");
setSseJson("");
return;
}
// HTTP/SSE 模式
if (domains && domains.length > 0 && path && domainIndex < domains.length) {
const domain = domains[domainIndex]
const formattedDomain = formatDomainWithPort(domain.domain, domain.port, domain.protocol);
const baseUrl = `${domain.protocol}://${formattedDomain}`;
let fullUrl = `${baseUrl}${path || '/'}`;
if (protocolType === 'SSE') {
// 仅生成SSE配置,不追加/sse
const sseConfig = {
mcpServers: {
[serverName]: {
type: "sse",
url: fullUrl
}
}
}
setSseJson(JSON.stringify(sseConfig, null, 2))
setHttpJson("")
setLocalJson("")
return;
} else if (protocolType === 'StreamableHTTP') {
// 仅生成HTTP配置
const httpConfig = {
mcpServers: {
[serverName]: {
url: fullUrl
}
}
}
setHttpJson(JSON.stringify(httpConfig, null, 2))
setSseJson("")
setLocalJson("")
return;
} else {
// protocol为null或其他值:生成两种配置
const sseConfig = {
mcpServers: {
[serverName]: {
type: "sse",
url: `${fullUrl}/sse`
}
}
}
const httpConfig = {
mcpServers: {
[serverName]: {
url: fullUrl
}
}
}
setSseJson(JSON.stringify(sseConfig, null, 2))
setHttpJson(JSON.stringify(httpConfig, null, 2))
setLocalJson("")
return;
}
}
// 无有效配置
setHttpJson("");
setSseJson("");
setLocalJson("");
}
const handleCopy = async (text: string) => {
try {
await copyToClipboard(text);
message.success("已复制到剪贴板");
} catch {
message.error("复制失败,请手动复制");
}
}
const fetchGateways = async () => {
setGatewayLoading(true)
try {
const res = await gatewayApi.getGateways({
page: 1,
size: 1000,
})
let result;
if (apiProduct.type === 'REST_API') {
// REST API 只支持 APIG_API 网关
result = res.data?.content?.filter?.((item: Gateway) => item.gatewayType === 'APIG_API');
} else if (apiProduct.type === 'AGENT_API') {
// Agent API 只支持 APIG_AI 网关
result = res.data?.content?.filter?.((item: Gateway) => item.gatewayType === 'APIG_AI');
} else if (apiProduct.type === 'MODEL_API') {
// Model API 支持 APIG_AI 和 HIGRESS 网关
result = res.data?.content?.filter?.((item: Gateway) => item.gatewayType === 'APIG_AI' || item.gatewayType === 'HIGRESS' || item.gatewayType === 'ADP_AI_GATEWAY');
} else {
// MCP Server 支持 HIGRESS、APIG_AI、ADP_AI_GATEWAY
result = res.data?.content?.filter?.((item: Gateway) => item.gatewayType === 'HIGRESS' || item.gatewayType === 'APIG_AI' || item.gatewayType === 'ADP_AI_GATEWAY' || item.gatewayType === 'APSARA_GATEWAY');
}
setGateways(result || [])
} catch (error) {
console.error('获取网关列表失败:', error)
} finally {
setGatewayLoading(false)
}
}
const fetchNacosInstances = async () => {
setNacosLoading(true)
try {
const res = await nacosApi.getNacos({
page: 1,
size: 1000 // 获取所有 Nacos 实例
})
setNacosInstances(res.data.content || [])
} catch (error) {
console.error('获取Nacos实例列表失败:', error)
} finally {
setNacosLoading(false)
}
}
const handleSourceTypeChange = (value: 'GATEWAY' | 'NACOS') => {
setSourceType(value)
setSelectedGateway(null)
setSelectedNacos(null)
setSelectedNamespace(null)
setNacosNamespaces([])
setApiList([])
form.setFieldsValue({
gatewayId: undefined,
nacosId: undefined,
apiId: undefined
})
}
const handleGatewayChange = async (gatewayId: string) => {
const gateway = gateways.find(g => g.gatewayId === gatewayId)
setSelectedGateway(gateway || null)
if (!gateway) return
setApiLoading(true)
try {
if (gateway.gatewayType === 'APIG_API') {
// APIG_API类型:获取REST API列表
const restRes = await gatewayApi.getGatewayRestApis(gatewayId, {})
const restApis = (restRes.data?.content || []).map((api: any) => ({
apiId: api.apiId,
apiName: api.apiName,
type: 'REST API'
}))
setApiList(restApis)
} else if (gateway.gatewayType === 'HIGRESS') {
// HIGRESS类型:对于Model API产品,获取Model API列表;其他情况获取MCP Server列表
if (apiProduct.type === 'MODEL_API') {
// HIGRESS类型 + Model API产品:获取Model API列表
const res = await gatewayApi.getGatewayModelApis(gatewayId, {
page: 1,
size: 1000 // 获取所有Model API
})
const modelApis = (res.data?.content || []).map((api: any) => ({
modelRouteName: api.modelRouteName,
fromGatewayType: 'HIGRESS' as const,
type: 'Model API'
}))
setApiList(modelApis)
} else {
// HIGRESS类型:获取MCP Server列表
const res = await gatewayApi.getGatewayMcpServers(gatewayId, {
page: 1,
size: 1000 // 获取所有MCP Server
})
const mcpServers = (res.data?.content || []).map((api: any) => ({
mcpServerName: api.mcpServerName,
fromGatewayType: 'HIGRESS' as const,
type: 'MCP Server'
}))
setApiList(mcpServers)
}
} else if (gateway.gatewayType === 'APIG_AI') {
if (apiProduct.type === 'AGENT_API') {
// APIG_AI类型 + Agent API产品:获取Agent API列表
const res = await gatewayApi.getGatewayAgentApis(gatewayId, {
page: 1,
size: 500 // 获取所有Agent API
})
const agentApis = (res.data?.content || []).map((api: any) => ({
agentApiId: api.agentApiId,
agentApiName: api.agentApiName,
fromGatewayType: 'APIG_AI' as const,
type: 'Agent API'
}))
setApiList(agentApis)
} else if (apiProduct.type === 'MODEL_API') {
// APIG_AI类型 + Model API产品:获取Model API列表
const res = await gatewayApi.getGatewayModelApis(gatewayId, {
page: 1,
size: 500 // 获取所有Model API
})
const modelApis = (res.data?.content || []).map((api: any) => ({
modelApiId: api.modelApiId,
modelApiName: api.modelApiName,
fromGatewayType: 'APIG_AI' as const,
type: 'Model API'
}))
setApiList(modelApis)
} else {
// APIG_AI类型 + MCP Server产品:获取MCP Server列表
const res = await gatewayApi.getGatewayMcpServers(gatewayId, {
page: 1,
size: 500 // 获取所有MCP Server
})
const mcpServers = (res.data?.content || []).map((api: any) => ({
mcpServerName: api.mcpServerName,
fromGatewayType: 'APIG_AI' as const,
mcpRouteId: api.mcpRouteId,
apiId: api.apiId,
mcpServerId: api.mcpServerId,
type: 'MCP Server'
}))
setApiList(mcpServers)
}
} else if (gateway.gatewayType === 'ADP_AI_GATEWAY') {
if (apiProduct.type === 'MODEL_API') {
// ADP_AI_GATEWAY类型 + Model API产品:获取Model API列表
const res = await gatewayApi.getGatewayModelApis(gatewayId, {
page: 1,
size: 500 // 获取所有Model API
})
const modelApis = (res.data?.content || []).map((api: any) => ({
modelApiId: api.modelApiId,
modelApiName: api.modelApiName,
fromGatewayType: 'ADP_AI_GATEWAY' as const,
type: 'Model API'
}))
setApiList(modelApis)
} else {
// ADP_AI_GATEWAY类型:获取MCP Server列表
const res = await gatewayApi.getGatewayMcpServers(gatewayId, {
page: 1,
size: 500 // 获取所有MCP Server
})
const mcpServers = (res.data?.content || []).map((api: any) => ({
mcpServerName: api.mcpServerName || api.name,
fromGatewayType: 'ADP_AI_GATEWAY' as const,
mcpRouteId: api.mcpRouteId,
mcpServerId: api.mcpServerId,
type: 'MCP Server'
}))
setApiList(mcpServers)
}
} else if (gateway.gatewayType === 'APSARA_GATEWAY') {
// APSARA_GATEWAY类型:获取MCP Server列表
const res = await gatewayApi.getGatewayMcpServers(gatewayId, {
page: 1,
size: 500 // 获取所有MCP Server
})
const mcpServers = (res.data?.content || []).map((api: any) => ({
mcpServerName: api.mcpServerName || api.name,
fromGatewayType: 'APSARA_GATEWAY' as const,
mcpRouteId: api.mcpRouteId,
mcpServerId: api.mcpServerId,
type: 'MCP Server'
}))
setApiList(mcpServers)
}
} catch (error) {
} finally {
setApiLoading(false)
}
}
const handleNacosChange = async (nacosId: string) => {
const nacos = nacosInstances.find(n => n.nacosId === nacosId)
setSelectedNacos(nacos || null)
setSelectedNamespace(null)
setApiList([])
setNacosNamespaces([])
if (!nacos) return
// 获取命名空间列表
try {
const nsRes = await nacosApi.getNamespaces(nacosId, { page: 1, size: 1000 })
const namespaces = (nsRes.data?.content || []).map((ns: any) => ({
namespaceId: ns.namespaceId,
namespaceName: ns.namespaceName || ns.namespaceId,
namespaceDesc: ns.namespaceDesc
}))
setNacosNamespaces(namespaces)
} catch (e) {
console.error('获取命名空间失败', e)
}
}
const handleNamespaceChange = async (namespaceId: string) => {
setSelectedNamespace(namespaceId)
setApiLoading(true)
try {
if (!selectedNacos) return
// 根据产品类型获取不同的列表
if (apiProduct.type === 'AGENT_API') {
// 获取 Agent 列表
const res = await nacosApi.getNacosAgents(selectedNacos.nacosId, {
page: 1,
size: 1000,
namespaceId
})
const agents = (res.data?.content || []).map((api: any) => ({
agentName: api.agentName,
description: api.description,
fromGatewayType: 'NACOS' as const,
type: `Agent API (${namespaceId})`
}))
setApiList(agents)
} else if (apiProduct.type === 'MCP_SERVER') {
// 获取 MCP Server 列表(现有逻辑)
const res = await nacosApi.getNacosMcpServers(selectedNacos.nacosId, {
page: 1,
size: 1000,
namespaceId
})
const mcpServers = (res.data?.content || []).map((api: any) => ({
mcpServerName: api.mcpServerName,
fromGatewayType: 'NACOS' as const,
type: `MCP Server (${namespaceId})`
}))
setApiList(mcpServers)
}
} catch (e) {
console.error('获取 Nacos 资源列表失败:', e)
} finally {
setApiLoading(false)
}
}
// TODO
const handleModalOk = () => {
form.validateFields().then((values) => {
const { sourceType, gatewayId, nacosId, apiId } = values
const selectedApi = apiList.find((item: any) => {
if ('apiId' in item) {
// REST API或MCP server 会返回apiId和mcpRouteId,此时mcpRouteId为唯一值,apiId不是
if ('mcpRouteId' in item) {
return item.mcpRouteId === apiId
} else {
return item.apiId === apiId
}
} else if ('mcpServerName' in item) {
return item.mcpServerName === apiId
} else if ('agentApiId' in item || 'agentApiName' in item) {
// Agent API: 匹配agentApiId或agentApiName
return item.agentApiId === apiId || item.agentApiName === apiId
} else if ('modelApiId' in item || 'modelApiName' in item) {
// Model API (AI Gateway): 匹配modelApiId或modelApiName
return item.modelApiId === apiId || item.modelApiName === apiId
} else if ('modelRouteName' in item && item.fromGatewayType === 'HIGRESS') {
// Model API (Higress): 匹配modelRouteName字段
return item.modelRouteName === apiId
} else if ('agentName' in item) {
// Nacos Agent: 匹配agentName
return item.agentName === apiId
}
return false
})
const newService: LinkedService = {
gatewayId: sourceType === 'GATEWAY' ? gatewayId : undefined, // 对于 Nacos,使用 nacosId 作为 gatewayId
nacosId: sourceType === 'NACOS' ? nacosId : undefined,
sourceType,
productId: apiProduct.productId,
apigRefConfig: selectedApi && ('apiId' in selectedApi || 'agentApiId' in selectedApi || 'agentApiName' in selectedApi || 'modelApiId' in selectedApi || 'modelApiName' in selectedApi) && (!('fromGatewayType' in selectedApi) || selectedApi.fromGatewayType !== 'HIGRESS') ? selectedApi as RestAPIItem | APIGAIMCPItem | AIGatewayAgentItem | AIGatewayModelItem : undefined,
higressRefConfig: selectedApi && 'fromGatewayType' in selectedApi && selectedApi.fromGatewayType === 'HIGRESS' ? (
apiProduct.type === 'MODEL_API'
? { modelRouteName: (selectedApi as any).modelRouteName, fromGatewayType: 'HIGRESS' as const }
: { mcpServerName: (selectedApi as any).mcpServerName, fromGatewayType: 'HIGRESS' as const }
) : undefined,
nacosRefConfig: sourceType === 'NACOS' && selectedApi && 'fromGatewayType' in selectedApi && selectedApi.fromGatewayType === 'NACOS' ? {
...selectedApi,
namespaceId: selectedNamespace || 'public'
} : undefined,
adpAIGatewayRefConfig: selectedApi && 'fromGatewayType' in selectedApi && selectedApi.fromGatewayType === 'ADP_AI_GATEWAY' ? (
apiProduct.type === 'MODEL_API'
? { modelApiId: (selectedApi as any).modelApiId, modelApiName: (selectedApi as any).modelApiName, fromGatewayType: 'ADP_AI_GATEWAY' as const } as AdpAIGatewayModelItem
: selectedApi as APIGAIMCPItem
) : undefined,
apsaraGatewayRefConfig: selectedApi && 'fromGatewayType' in selectedApi && selectedApi.fromGatewayType === 'APSARA_GATEWAY' ? selectedApi as APIGAIMCPItem : undefined,
}
apiProductApi.createApiProductRef(apiProduct.productId, newService).then(async () => {
message.success('关联成功')
setIsModalVisible(false)
// 重新获取关联信息并更新
try {
const res = await apiProductApi.getApiProductRef(apiProduct.productId)
onLinkedServiceUpdate(res.data || null)
} catch (error) {
console.error('获取关联API失败:', error)
onLinkedServiceUpdate(null)
}
// 重新获取产品详情(特别重要,因为关联API后apiProduct.apiConfig可能会更新)
handleRefresh()
form.resetFields()
setSelectedGateway(null)
setSelectedNacos(null)
setApiList([])
setSourceType('GATEWAY')
}).catch(() => {
message.error('关联失败')
})
})
}
const handleModalCancel = () => {
setIsModalVisible(false)
form.resetFields()
setSelectedGateway(null)
setSelectedNacos(null)
setApiList([])
setSourceType('GATEWAY')
}
const handleDelete = () => {
if (!linkedService) return
Modal.confirm({
title: '确认解除关联',
content: '确定要解除与当前API的关联吗?',
icon: <ExclamationCircleOutlined />,
onOk() {
return apiProductApi.deleteApiProductRef(apiProduct.productId).then(() => {
message.success('解除关联成功')
onLinkedServiceUpdate(null)
// 重新获取产品详情(解除关联后apiProduct.apiConfig可能会更新)
handleRefresh()
}).catch(() => {
message.error('解除关联失败')
})
}
})
}
const getServiceInfo = () => {
if (!linkedService) return null
let apiName = ''
let apiType = ''
let sourceInfo = ''
let gatewayInfo = ''
// 首先根据 Product 的 type 确定基本类型
if (apiProduct.type === 'REST_API') {
// REST API 类型产品 - 只能关联 API 网关上的 REST API
if (linkedService.sourceType === 'GATEWAY' && linkedService.apigRefConfig && 'apiName' in linkedService.apigRefConfig) {
apiName = linkedService.apigRefConfig.apiName || '未命名'
apiType = 'REST API'
sourceInfo = 'API网关'
gatewayInfo = linkedService.gatewayId || '未知'
}
} else if (apiProduct.type === 'MCP_SERVER') {
// MCP Server 类型产品 - 可以关联多种平台上的 MCP Server
apiType = 'MCP Server'
if (linkedService.sourceType === 'GATEWAY' && linkedService.apigRefConfig && 'mcpServerName' in linkedService.apigRefConfig) {
// AI网关上的MCP Server
apiName = linkedService.apigRefConfig.mcpServerName || '未命名'
sourceInfo = 'AI网关'
gatewayInfo = linkedService.gatewayId || '未知'
} else if (linkedService.sourceType === 'GATEWAY' && linkedService.higressRefConfig) {
// Higress网关上的MCP Server
apiName = linkedService.higressRefConfig.mcpServerName || '未命名'
sourceInfo = 'Higress网关'
gatewayInfo = linkedService.gatewayId || '未知'
} else if (linkedService.sourceType === 'GATEWAY' && linkedService.adpAIGatewayRefConfig) {
// 检查是否是 AdpAIGatewayModelItem 类型(有 modelApiName 属性)
if ('modelApiName' in linkedService.adpAIGatewayRefConfig) {
// 专有云AI网关上的Model API
apiName = linkedService.adpAIGatewayRefConfig.modelApiName || '未命名'
sourceInfo = '专有云AI网关'
gatewayInfo = linkedService.gatewayId || '未知'
} else {
// 专有云AI网关上的MCP Server
apiName = linkedService.adpAIGatewayRefConfig.mcpServerName || '未命名'
sourceInfo = '专有云AI网关'
gatewayInfo = linkedService.gatewayId || '未知'
}
} else if (linkedService.sourceType === 'GATEWAY' && linkedService.apsaraGatewayRefConfig) {
// 飞天企业版AI网关上的MCP Server
apiName = linkedService.apsaraGatewayRefConfig.mcpServerName || '未命名'
sourceInfo = '飞天企业版AI网关'
gatewayInfo = linkedService.gatewayId || '未知'
} else if (linkedService.sourceType === 'NACOS' && linkedService.nacosRefConfig && 'mcpServerName' in linkedService.nacosRefConfig) {
// Nacos上的MCP Server
apiName = linkedService.nacosRefConfig.mcpServerName || '未命名'
sourceInfo = 'Nacos服务发现'
gatewayInfo = linkedService.nacosId || '未知'
}
} else if (apiProduct.type === 'AGENT_API') {
// Agent API 类型产品 - 可以关联 AI 网关或 Nacos 上的 Agent API
apiType = 'Agent API'
if (linkedService.sourceType === 'GATEWAY' && linkedService.apigRefConfig && 'agentApiName' in linkedService.apigRefConfig) {
// AI网关上的Agent API
apiName = linkedService.apigRefConfig.agentApiName || '未命名'
sourceInfo = 'AI网关'
gatewayInfo = linkedService.gatewayId || '未知'
} else if (linkedService.sourceType === 'NACOS' && linkedService.nacosRefConfig && 'agentName' in linkedService.nacosRefConfig) {
// Nacos 上的 Agent API
apiName = linkedService.nacosRefConfig.agentName || '未命名'
sourceInfo = 'Nacos Agent Registry'
gatewayInfo = linkedService.nacosId || '未知'
}
// 注意:Agent API 不支持专有云AI网关(ADP_AI_GATEWAY)
} else if (apiProduct.type === 'MODEL_API') {
// Model API 类型产品 - 可以关联 AI 网关或 Higress 网关上的 Model API
apiType = 'Model API'
if (linkedService.sourceType === 'GATEWAY' && linkedService.apigRefConfig && 'modelApiName' in linkedService.apigRefConfig) {
// AI网关上的Model API
apiName = linkedService.apigRefConfig.modelApiName || '未命名'
sourceInfo = 'AI网关'
gatewayInfo = linkedService.gatewayId || '未知'
} else if (linkedService.sourceType === 'GATEWAY' && linkedService.higressRefConfig && 'modelRouteName' in linkedService.higressRefConfig) {
// Higress网关上的Model API(AI路由)
apiName = linkedService.higressRefConfig.modelRouteName || '未命名'
sourceInfo = 'Higress网关'
gatewayInfo = linkedService.gatewayId || '未知'
}
}
return {
apiName,
apiType,
sourceInfo,
gatewayInfo
}
}
const renderLinkInfo = () => {
const serviceInfo = getServiceInfo()
// 没有关联任何API
if (!linkedService || !serviceInfo) {
return (
<Card className="mb-6">
<div className="text-center py-8">
<div className="text-gray-500 mb-4">暂未关联任何API</div>
<Button type="primary" icon={<PlusOutlined />} onClick={() => setIsModalVisible(true)}>
关联API
</Button>
</div>
</Card>
)
}
return (
<Card
className="mb-6"
title="关联详情"
extra={
<Button type="primary" danger icon={<DeleteOutlined />} onClick={handleDelete}>
解除关联
</Button>
}
>
<div>
{/* 第一行:名称 + 类型 */}
<div className="grid grid-cols-6 gap-8 items-center pt-2 pb-2">
<span className="text-xs text-gray-600">名称:</span>
<span className="col-span-2 text-xs text-gray-900">{serviceInfo.apiName || '未命名'}</span>
<span className="text-xs text-gray-600">类型:</span>
<span className="col-span-2 text-xs text-gray-900">{serviceInfo.apiType}</span>
</div>
{/* 第二行:来源 + ID */}
<div className="grid grid-cols-6 gap-8 items-center pt-2 pb-2">
<span className="text-xs text-gray-600">来源:</span>
<span className="col-span-2 text-xs text-gray-900">{serviceInfo.sourceInfo}</span>
<span className="text-xs text-gray-600">
{linkedService?.sourceType === 'NACOS' ? 'Nacos ID:' : '网关ID:'}
</span>
<span className="col-span-2 text-xs text-gray-700">{serviceInfo.gatewayInfo}</span>
</div>
</div>
</Card>
)
}
const renderApiConfig = () => {
const isMcp = apiProduct.type === 'MCP_SERVER'
const isOpenApi = apiProduct.type === 'REST_API'
const isAgent = apiProduct.type === 'AGENT_API'
const isModel = apiProduct.type === 'MODEL_API'
// MCP Server类型:无论是否有linkedService都显示tools和连接点配置
if (isMcp && apiProduct.mcpConfig) {
return (
<Card title="配置详情">
<Row gutter={24}>
{/* 左侧:工具列表 */}
<Col span={15}>
<Card>
<Tabs
defaultActiveKey="tools"
items={[
{
key: "tools",
label: `Tools (${parsedTools.length})`,
children: parsedTools.length > 0 ? (
<div className="border border-gray-200 rounded-lg bg-gray-50">
{parsedTools.map((tool, idx) => (
<div key={idx} className={idx < parsedTools.length - 1 ? "border-b border-gray-200" : ""}>
<Collapse
ghost
expandIconPosition="end"
items={[{
key: idx.toString(),
label: tool.name,
children: (
<div className="px-4 pb-2">
<div className="text-gray-600 mb-4">{tool.description}</div>
{tool.args && tool.args.length > 0 && (
<div>
<p className="font-medium text-gray-700 mb-3">输入参数:</p>
{tool.args.map((arg, argIdx) => (
<div key={argIdx} className="mb-3">
<div className="flex items-center mb-2">
<span className="font-medium text-gray-800 mr-2">{arg.name}</span>
<span className="text-xs bg-gray-200 text-gray-600 px-2 py-1 rounded mr-2">
{arg.type}
</span>
{arg.required && (
<span className="text-red-500 text-xs mr-2">*</span>
)}
{arg.description && (
<span className="text-xs text-gray-500">
{arg.description}
</span>
)}
</div>
<input
type="text"
placeholder={arg.description || `请输入${arg.name}`}
className="w-full px-3 py-2 bg-gray-100 border border-gray-300 rounded-md text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent mb-2"
defaultValue={arg.default !== undefined ? JSON.stringify(arg.default) : ''}
/>
{arg.enum && (
<div className="text-xs text-gray-500">
可选值: {arg.enum.map(value => <code key={value} className="mr-1">{value}</code>)}
</div>
)}
</div>
))}
</div>
)}
</div>
)
}]}
/>
</div>
))}
</div>
) : (
<div className="text-gray-500 text-center py-8">
No tools available
</div>
),
},
]}
/>
</Card>
</Col>
{/* 右侧:连接点配置 */}
<Col span={9}>
<Card>
<div className="mb-4">
<h3 className="text-sm font-semibold mb-3">连接点配置</h3>
{/* 域名选择器 */}
{apiProduct.mcpConfig?.mcpServerConfig?.domains && apiProduct.mcpConfig.mcpServerConfig.domains.length > 0 && (
<div className="mb-2">
<div className="flex border border-gray-200 rounded-md overflow-hidden">
<div className="flex-shrink-0 bg-gray-50 px-3 py-2 text-xs text-gray-600 border-r border-gray-200 flex items-center whitespace-nowrap">
域名
</div>
<div className="flex-1 min-w-0">
<Select
value={selectedDomainIndex}
onChange={setSelectedDomainIndex}
className="w-full"
placeholder="选择域名"
size="middle"
variant='borderless'
style={{
fontSize: '12px',
height: '100%'
}}
>
{getDomainOptions(apiProduct.mcpConfig.mcpServerConfig.domains).map((option) => (
<Select.Option key={option.value} value={option.value}>
<span title={option.label} className="text-xs text-gray-900 font-mono">
{option.label}
</span>
</Select.Option>
))}
</Select>
</div>
</div>
</div>
)}
<Tabs
size="small"
defaultActiveKey={localJson ? "local" : (sseJson ? "sse" : "http")}
items={(() => {
const tabs = [];
if (localJson) {
tabs.push({
key: "local",
label: "Stdio",
children: (
<div className="relative bg-gray-50 border border-gray-200 rounded-md p-3">
<Button
size="small"
icon={<CopyOutlined />}
className="absolute top-2 right-2 z-10"
onClick={() => handleCopy(localJson)}
>
</Button>
<div className="text-gray-800 font-mono text-xs overflow-x-auto">
<pre className="whitespace-pre">{localJson}</pre>
</div>
</div>
),
});
} else {
if (sseJson) {
tabs.push({
key: "sse",
label: "SSE",
children: (
<div className="relative bg-gray-50 border border-gray-200 rounded-md p-3">
<Button
size="small"
icon={<CopyOutlined />}
className="absolute top-2 right-2 z-10"
onClick={() => handleCopy(sseJson)}
>
</Button>
<div className="text-gray-800 font-mono text-xs overflow-x-auto">
<pre className="whitespace-pre">{sseJson}</pre>
</div>
</div>
),
});
}
if (httpJson) {
tabs.push({
key: "http",
label: "Streamable HTTP",
children: (
<div className="relative bg-gray-50 border border-gray-200 rounded-md p-3">
<Button
size="small"
icon={<CopyOutlined />}
className="absolute top-2 right-2 z-10"
onClick={() => handleCopy(httpJson)}
>
</Button>
<div className="text-gray-800 font-mono text-xs overflow-x-auto">
<pre className="whitespace-pre">{httpJson}</pre>
</div>
</div>
),
});
}
}
return tabs;
})()}
/>
</div>
</Card>
</Col>
</Row>
</Card>
)
}
// Agent API类型:显示协议支持和路由配置或 AgentCard
if (isAgent && apiProduct.agentConfig?.agentAPIConfig) {
const agentAPIConfig = apiProduct.agentConfig.agentAPIConfig
const routes = agentAPIConfig.routes || []
const protocols = agentAPIConfig.agentProtocols || []
const isA2A = protocols.includes('a2a')
const agentCard = agentAPIConfig.agentCard
// 生成匹配类型前缀文字
const getMatchTypePrefix = (matchType: string) => {
switch (matchType) {
case 'Exact':
return '等于'
case 'Prefix':
return '前缀是'
case 'Regex':
return '正则是'
default:
return '等于'
}