curl create plugin with inputs (#3573)

This commit is contained in:
heheer
2025-01-13 17:37:27 +08:00
committed by GitHub
parent cdf4f0e67d
commit e933dacb05
2 changed files with 86 additions and 18 deletions

View File

@@ -27,12 +27,18 @@ export const parseCurl = (curlContent: string) => {
type: 'string' type: 'string'
})); }));
const newBody = JSON.stringify(parsed.data, null, 2); const newBody = JSON.stringify(parsed.data, null, 2);
const bodyArray = Object.keys(parsed.data || {}).map((key) => ({
key,
value: parsed.data?.[key],
type: 'string'
}));
return { return {
url: parsed.url, url: parsed.url,
method: methodMap[parsed.method?.toLowerCase() as RequestMethod] || 'GET', method: methodMap[parsed.method?.toLowerCase() as RequestMethod] || 'GET',
params: newParams, params: newParams,
headers: newHeaders, headers: newHeaders,
body: newBody body: newBody,
bodyArray
}; };
}; };

View File

@@ -417,35 +417,82 @@ export const parsePluginFromCurlString = (
edges: AppSchema['edges']; edges: AppSchema['edges'];
chatConfig: AppSchema['chatConfig']; chatConfig: AppSchema['chatConfig'];
} => { } => {
const { url, method, headers, body } = parseCurl(curl); const { url, method, headers, body, params, bodyArray } = parseCurl(curl);
const allInputs = Array.from(
new Map([...headers, ...params, ...bodyArray].map((item) => [item.key, item])).values()
);
const formatInputs = allInputs.map((item) => {
const valueType = item.value === null ? 'string' : typeof item.value;
return {
renderTypeList: ['reference'],
selectedTypeIndex: 0,
valueType,
canEdit: true,
key: item.key,
label: item.key,
description: item.key,
defaultValue: '',
required: false,
toolDescription: ['string', 'number', 'boolean'].includes(valueType) ? item.key : ''
};
});
const formatOutputs = formatInputs.map((item) => ({
id: item.key,
key: item.key,
label: item.key,
valueType: item.valueType,
type: 'hidden'
}));
const referenceHeaders = headers.map((item) => ({
key: item.key,
value: `{{$pluginInput.${item.key}$}}`,
type: item.type
}));
const referenceParams = params.map((item) => ({
key: item.key,
value: `{{$pluginInput.${item.key}$}}`,
type: item.type
}));
const referenceBody = Object.entries(JSON.parse(body)).reduce(
(acc, [key, value]) => {
acc[key] = `{{$pluginInput.${key}$}}`;
return acc;
},
{} as Record<string, any>
);
const referenceBodyStr = JSON.stringify(referenceBody, null, 2)
.replace(/"{{\$/g, '{{$')
.replace(/\$}}"/g, '$}}');
return { return {
nodes: [ nodes: [
{ {
nodeId: 'pluginInput', nodeId: 'pluginInput',
name: 'workflow:template.plugin_start', name: i18nT('workflow:template.plugin_start'),
intro: 'workflow:intro_plugin_input', intro: i18nT('workflow:intro_plugin_input'),
avatar: 'core/workflow/template/workflowStart', avatar: 'core/workflow/template/workflowStart',
flowNodeType: 'pluginInput', flowNodeType: 'pluginInput',
showStatus: false, showStatus: false,
position: { position: {
x: 630.1191328382079, x: 427.6554681270263,
y: -125.05298493910118 y: -291.6987155252725
}, },
version: '481', version: '481',
inputs: [], inputs: formatInputs,
outputs: [] outputs: formatOutputs
}, },
{ {
nodeId: 'pluginOutput', nodeId: 'pluginOutput',
name: 'common:core.module.template.self_output', name: i18nT('common:core.module.template.self_output'),
intro: 'workflow:intro_custom_plugin_output', intro: i18nT('workflow:intro_custom_plugin_output'),
avatar: 'core/workflow/template/pluginOutput', avatar: 'core/workflow/template/pluginOutput',
flowNodeType: 'pluginOutput', flowNodeType: 'pluginOutput',
showStatus: false, showStatus: false,
position: { position: {
x: 1776.334576378706, x: 1870.1072210870427,
y: -179.2671413906911 y: -126.69871552527252
}, },
version: '481', version: '481',
inputs: [ inputs: [
@@ -455,7 +502,7 @@ export const parsePluginFromCurlString = (
canEdit: true, canEdit: true,
key: 'result', key: 'result',
label: 'result', label: 'result',
isToolOutput: false, isToolOutput: true,
description: '', description: '',
required: true, required: true,
value: ['vumlECDQTjeC', 'httpRawResponse'] value: ['vumlECDQTjeC', 'httpRawResponse']
@@ -466,7 +513,7 @@ export const parsePluginFromCurlString = (
canEdit: true, canEdit: true,
key: 'error', key: 'error',
label: 'error', label: 'error',
isToolOutput: false, isToolOutput: true,
description: '', description: '',
required: true, required: true,
value: ['vumlECDQTjeC', 'error'] value: ['vumlECDQTjeC', 'error']
@@ -482,8 +529,8 @@ export const parsePluginFromCurlString = (
flowNodeType: 'httpRequest468', flowNodeType: 'httpRequest468',
showStatus: true, showStatus: true,
position: { position: {
x: 1068.6226695001628, x: 1049.4419012643668,
y: -435.2671413906911 y: -471.49748139163944
}, },
version: '481', version: '481',
inputs: [ inputs: [
@@ -563,7 +610,7 @@ export const parsePluginFromCurlString = (
key: 'system_httpHeader', key: 'system_httpHeader',
renderTypeList: ['custom'], renderTypeList: ['custom'],
valueType: 'any', valueType: 'any',
value: headers, value: referenceHeaders,
label: '', label: '',
description: description:
'自定义请求头,请严格填入 JSON 字符串。\n1. 确保最后一个属性没有逗号\n2. 确保 key 包含双引号\n例如{"Authorization":"Bearer xxx"}', '自定义请求头,请严格填入 JSON 字符串。\n1. 确保最后一个属性没有逗号\n2. 确保 key 包含双引号\n例如{"Authorization":"Bearer xxx"}',
@@ -577,6 +624,7 @@ export const parsePluginFromCurlString = (
key: 'system_httpParams', key: 'system_httpParams',
renderTypeList: ['hidden'], renderTypeList: ['hidden'],
valueType: 'any', valueType: 'any',
value: referenceParams,
description: description:
'新的 HTTP 请求地址。如果出现两个“请求地址”,可以删除该模块重新加入,会拉取最新的模块配置。', '新的 HTTP 请求地址。如果出现两个“请求地址”,可以删除该模块重新加入,会拉取最新的模块配置。',
label: '', label: '',
@@ -590,7 +638,7 @@ export const parsePluginFromCurlString = (
key: 'system_httpJsonBody', key: 'system_httpJsonBody',
renderTypeList: ['hidden'], renderTypeList: ['hidden'],
valueType: 'any', valueType: 'any',
value: body, value: referenceBodyStr,
label: '', label: '',
required: false, required: false,
valueDesc: '', valueDesc: '',
@@ -674,6 +722,20 @@ export const parsePluginFromCurlString = (
valueDesc: '' valueDesc: ''
} }
] ]
},
{
nodeId: 'pluginConfig',
name: i18nT('common:core.module.template.system_config'),
intro: '',
avatar: 'core/workflow/template/systemConfig',
flowNodeType: FlowNodeTypeEnum.pluginConfig,
position: {
x: -88.12977161770735,
y: -235.2337531748973
},
version: '4811',
inputs: [],
outputs: []
} }
], ],
edges: [ edges: [