Skip to content

Commit e933dac

Browse files
authored
curl create plugin with inputs (#3573)
1 parent cdf4f0e commit e933dac

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-18
lines changed

packages/global/common/string/http.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ export const parseCurl = (curlContent: string) => {
2727
type: 'string'
2828
}));
2929
const newBody = JSON.stringify(parsed.data, null, 2);
30+
const bodyArray = Object.keys(parsed.data || {}).map((key) => ({
31+
key,
32+
value: parsed.data?.[key],
33+
type: 'string'
34+
}));
3035

3136
return {
3237
url: parsed.url,
3338
method: methodMap[parsed.method?.toLowerCase() as RequestMethod] || 'GET',
3439
params: newParams,
3540
headers: newHeaders,
36-
body: newBody
41+
body: newBody,
42+
bodyArray
3743
};
3844
};

projects/app/src/web/core/app/templates.ts

Lines changed: 79 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -417,35 +417,82 @@ export const parsePluginFromCurlString = (
417417
edges: AppSchema['edges'];
418418
chatConfig: AppSchema['chatConfig'];
419419
} => {
420-
const { url, method, headers, body } = parseCurl(curl);
420+
const { url, method, headers, body, params, bodyArray } = parseCurl(curl);
421+
422+
const allInputs = Array.from(
423+
new Map([...headers, ...params, ...bodyArray].map((item) => [item.key, item])).values()
424+
);
425+
const formatInputs = allInputs.map((item) => {
426+
const valueType = item.value === null ? 'string' : typeof item.value;
427+
return {
428+
renderTypeList: ['reference'],
429+
selectedTypeIndex: 0,
430+
valueType,
431+
canEdit: true,
432+
key: item.key,
433+
label: item.key,
434+
description: item.key,
435+
defaultValue: '',
436+
required: false,
437+
toolDescription: ['string', 'number', 'boolean'].includes(valueType) ? item.key : ''
438+
};
439+
});
440+
const formatOutputs = formatInputs.map((item) => ({
441+
id: item.key,
442+
key: item.key,
443+
label: item.key,
444+
valueType: item.valueType,
445+
type: 'hidden'
446+
}));
447+
448+
const referenceHeaders = headers.map((item) => ({
449+
key: item.key,
450+
value: `{{$pluginInput.${item.key}$}}`,
451+
type: item.type
452+
}));
453+
const referenceParams = params.map((item) => ({
454+
key: item.key,
455+
value: `{{$pluginInput.${item.key}$}}`,
456+
type: item.type
457+
}));
458+
const referenceBody = Object.entries(JSON.parse(body)).reduce(
459+
(acc, [key, value]) => {
460+
acc[key] = `{{$pluginInput.${key}$}}`;
461+
return acc;
462+
},
463+
{} as Record<string, any>
464+
);
465+
const referenceBodyStr = JSON.stringify(referenceBody, null, 2)
466+
.replace(/"{{\$/g, '{{$')
467+
.replace(/\$}}"/g, '$}}');
421468

422469
return {
423470
nodes: [
424471
{
425472
nodeId: 'pluginInput',
426-
name: 'workflow:template.plugin_start',
427-
intro: 'workflow:intro_plugin_input',
473+
name: i18nT('workflow:template.plugin_start'),
474+
intro: i18nT('workflow:intro_plugin_input'),
428475
avatar: 'core/workflow/template/workflowStart',
429476
flowNodeType: 'pluginInput',
430477
showStatus: false,
431478
position: {
432-
x: 630.1191328382079,
433-
y: -125.05298493910118
479+
x: 427.6554681270263,
480+
y: -291.6987155252725
434481
},
435482
version: '481',
436-
inputs: [],
437-
outputs: []
483+
inputs: formatInputs,
484+
outputs: formatOutputs
438485
},
439486
{
440487
nodeId: 'pluginOutput',
441-
name: 'common:core.module.template.self_output',
442-
intro: 'workflow:intro_custom_plugin_output',
488+
name: i18nT('common:core.module.template.self_output'),
489+
intro: i18nT('workflow:intro_custom_plugin_output'),
443490
avatar: 'core/workflow/template/pluginOutput',
444491
flowNodeType: 'pluginOutput',
445492
showStatus: false,
446493
position: {
447-
x: 1776.334576378706,
448-
y: -179.2671413906911
494+
x: 1870.1072210870427,
495+
y: -126.69871552527252
449496
},
450497
version: '481',
451498
inputs: [
@@ -455,7 +502,7 @@ export const parsePluginFromCurlString = (
455502
canEdit: true,
456503
key: 'result',
457504
label: 'result',
458-
isToolOutput: false,
505+
isToolOutput: true,
459506
description: '',
460507
required: true,
461508
value: ['vumlECDQTjeC', 'httpRawResponse']
@@ -466,7 +513,7 @@ export const parsePluginFromCurlString = (
466513
canEdit: true,
467514
key: 'error',
468515
label: 'error',
469-
isToolOutput: false,
516+
isToolOutput: true,
470517
description: '',
471518
required: true,
472519
value: ['vumlECDQTjeC', 'error']
@@ -482,8 +529,8 @@ export const parsePluginFromCurlString = (
482529
flowNodeType: 'httpRequest468',
483530
showStatus: true,
484531
position: {
485-
x: 1068.6226695001628,
486-
y: -435.2671413906911
532+
x: 1049.4419012643668,
533+
y: -471.49748139163944
487534
},
488535
version: '481',
489536
inputs: [
@@ -563,7 +610,7 @@ export const parsePluginFromCurlString = (
563610
key: 'system_httpHeader',
564611
renderTypeList: ['custom'],
565612
valueType: 'any',
566-
value: headers,
613+
value: referenceHeaders,
567614
label: '',
568615
description:
569616
'自定义请求头,请严格填入 JSON 字符串。\n1. 确保最后一个属性没有逗号\n2. 确保 key 包含双引号\n例如:{"Authorization":"Bearer xxx"}',
@@ -577,6 +624,7 @@ export const parsePluginFromCurlString = (
577624
key: 'system_httpParams',
578625
renderTypeList: ['hidden'],
579626
valueType: 'any',
627+
value: referenceParams,
580628
description:
581629
'新的 HTTP 请求地址。如果出现两个“请求地址”,可以删除该模块重新加入,会拉取最新的模块配置。',
582630
label: '',
@@ -590,7 +638,7 @@ export const parsePluginFromCurlString = (
590638
key: 'system_httpJsonBody',
591639
renderTypeList: ['hidden'],
592640
valueType: 'any',
593-
value: body,
641+
value: referenceBodyStr,
594642
label: '',
595643
required: false,
596644
valueDesc: '',
@@ -674,6 +722,20 @@ export const parsePluginFromCurlString = (
674722
valueDesc: ''
675723
}
676724
]
725+
},
726+
{
727+
nodeId: 'pluginConfig',
728+
name: i18nT('common:core.module.template.system_config'),
729+
intro: '',
730+
avatar: 'core/workflow/template/systemConfig',
731+
flowNodeType: FlowNodeTypeEnum.pluginConfig,
732+
position: {
733+
x: -88.12977161770735,
734+
y: -235.2337531748973
735+
},
736+
version: '4811',
737+
inputs: [],
738+
outputs: []
677739
}
678740
],
679741
edges: [

0 commit comments

Comments
 (0)