From dca4f9fe9c802172c84b2e39e2ba5951fd7d3cf2 Mon Sep 17 00:00:00 2001 From: Joel Date: Thu, 29 Aug 2024 16:18:16 +0800 Subject: [PATCH] feat: support file values in body --- .../components/base/prompt-editor/index.tsx | 3 +- web/app/components/base/select/index.tsx | 16 +++-- .../nodes/http/components/edit-body/index.tsx | 4 +- .../key-value/key-value-edit/index.tsx | 22 ++---- .../key-value/key-value-edit/item.tsx | 68 ++++++++++++++----- .../components/workflow/nodes/http/types.ts | 2 + web/i18n/en-US/workflow.ts | 1 + web/i18n/zh-Hans/workflow.ts | 1 + 8 files changed, 75 insertions(+), 42 deletions(-) diff --git a/web/app/components/base/prompt-editor/index.tsx b/web/app/components/base/prompt-editor/index.tsx index deae6833cd..e5fb642c4c 100644 --- a/web/app/components/base/prompt-editor/index.tsx +++ b/web/app/components/base/prompt-editor/index.tsx @@ -59,6 +59,7 @@ import { UPDATE_HISTORY_EVENT_EMITTER, } from './constants' import { useEventEmitterContextContext } from '@/context/event-emitter' +import cn from '@/utils/classnames' export type PromptEditorProps = { instanceId?: string @@ -147,7 +148,7 @@ const PromptEditor: FC = ({
} - placeholder={} + placeholder={} ErrorBoundary={LexicalErrorBoundary} /> void items: Item[] placeholder?: string + triggerClassName?: string + triggerClassNameFn?: (open: boolean) => string popupClassName?: string + readonly?: boolean } const PortalSelect: FC = ({ value, onSelect, items, placeholder, + triggerClassName, + triggerClassNameFn, popupClassName, + readonly, }) => { const { t } = useTranslation() const [open, setOpen] = useState(false) @@ -310,11 +316,11 @@ const PortalSelect: FC = ({ placement='bottom-start' offset={4} > - setOpen(v => !v)} className='w-full'> + !readonly && setOpen(v => !v)} className='w-full'>
= ({ {item.name} {item.value === value && ( - + )}
))} diff --git a/web/app/components/workflow/nodes/http/components/edit-body/index.tsx b/web/app/components/workflow/nodes/http/components/edit-body/index.tsx index 6441f8f8e5..1998f2f480 100644 --- a/web/app/components/workflow/nodes/http/components/edit-body/index.tsx +++ b/web/app/components/workflow/nodes/http/components/edit-body/index.tsx @@ -117,7 +117,7 @@ const EditBody: FC = ({ onChange(newBody) }, [onChange, payload]) - const handleBinaryFileChange = useCallback((value: ValueSelector | string) => { + const handleFileChange = useCallback((value: ValueSelector | string) => { const newBody = produce(payload, (draft: Body) => { if ((draft.data as BodyPayload).length === 0) { (draft.data as BodyPayload).push({ @@ -193,7 +193,7 @@ const EditBody: FC = ({ nodeId={nodeId} readonly={readonly} value={bodyPayload[0]?.file || []} - onChange={handleBinaryFileChange} + onChange={handleFileChange} filterVar={filterOnlyFileVariable} /> )} diff --git a/web/app/components/workflow/nodes/http/components/key-value/key-value-edit/index.tsx b/web/app/components/workflow/nodes/http/components/key-value/key-value-edit/index.tsx index 58f48deafe..adf7f966e0 100644 --- a/web/app/components/workflow/nodes/http/components/key-value/key-value-edit/index.tsx +++ b/web/app/components/workflow/nodes/http/components/key-value/key-value-edit/index.tsx @@ -5,7 +5,7 @@ import produce from 'immer' import { useTranslation } from 'react-i18next' import type { KeyValue } from '../../../types' import KeyValueItem from './item' -// import { EditList } from '@/app/components/base/icons/src/vender/solid/communication' +import cn from '@/utils/classnames' const i18nPrefix = 'workflow.nodes.http' @@ -57,22 +57,10 @@ const KeyValueList: FC = ({ return (
-
-
{t(`${i18nPrefix}.key`)}
-
-
{t(`${i18nPrefix}.value`)}
- {/* {!readonly && ( - -
- -
-
)} */} -
+
+
{t(`${i18nPrefix}.key`)}
+ {isSupportFile &&
{t(`${i18nPrefix}.type`)}
} +
{t(`${i18nPrefix}.value`)}
{ list.map((item, index) => ( diff --git a/web/app/components/workflow/nodes/http/components/key-value/key-value-edit/item.tsx b/web/app/components/workflow/nodes/http/components/key-value/key-value-edit/item.tsx index 0cc9310202..5babffa712 100644 --- a/web/app/components/workflow/nodes/http/components/key-value/key-value-edit/item.tsx +++ b/web/app/components/workflow/nodes/http/components/key-value/key-value-edit/item.tsx @@ -4,8 +4,12 @@ import React, { useCallback } from 'react' import { useTranslation } from 'react-i18next' import produce from 'immer' import type { KeyValue } from '../../../types' +import VarReferencePicker from '../../../../_base/components/variable/var-reference-picker' import InputItem from './input-item' import cn from '@/utils/classnames' +import { PortalSelect } from '@/app/components/base/select' +import type { ValueSelector, Var } from '@/app/components/workflow/types' +import { VarType } from '@/app/components/workflow/types' // import Input from '@/app/components/base/input' const i18nPrefix = 'workflow.nodes.http' @@ -44,20 +48,22 @@ const KeyValueItem: FC = ({ const { t } = useTranslation() const handleChange = useCallback((key: string) => { - return (value: string) => { + return (value: string | ValueSelector) => { const newPayload = produce(payload, (draft: any) => { draft[key] = value }) onChange(newPayload) - if (key === 'value' && isLastItem) - onAdd() } }, [onChange, onAdd, isLastItem, payload]) + const filterOnlyFileVariable = (varPayload: Var) => { + return [VarType.file, VarType.arrayFile].includes(varPayload.type) + } + return ( // group class name is for hover row show remove button
-
+
{!keyNotSupportVar ? ( = ({ /> )}
-
- + {isSupportFile && ( +
+ handleChange('type')(item.value as string)} + items={[ + { name: 'text', value: 'text' }, + { name: 'file', value: 'file' }, + ]} + readonly={readonly} + triggerClassName='rounded-none h-7' + triggerClassNameFn={isOpen => isOpen ? 'bg-state-base-hover' : 'bg-transparent'} + popupClassName='w-[80px] h-7' + /> +
)} +
isLastItem && onAdd()}> + {(isSupportFile && payload.type === 'file') + ? ( + + ) + : ( + + )} +
) diff --git a/web/app/components/workflow/nodes/http/types.ts b/web/app/components/workflow/nodes/http/types.ts index 144f2c3eba..f1937ec5bd 100644 --- a/web/app/components/workflow/nodes/http/types.ts +++ b/web/app/components/workflow/nodes/http/types.ts @@ -22,6 +22,8 @@ export type KeyValue = { id?: string key: string value: string + type?: string + file?: ValueSelector } export enum BodyPayloadValueType { diff --git a/web/i18n/en-US/workflow.ts b/web/i18n/en-US/workflow.ts index a1582480f0..2c99e08228 100644 --- a/web/i18n/en-US/workflow.ts +++ b/web/i18n/en-US/workflow.ts @@ -348,6 +348,7 @@ const translation = { apiPlaceholder: 'Enter URL, type ‘/’ insert variable', notStartWithHttp: 'API should start with http:// or https://', key: 'Key', + type: 'Type', value: 'Value', bulkEdit: 'Bulk Edit', keyValueEdit: 'Key-Value Edit', diff --git a/web/i18n/zh-Hans/workflow.ts b/web/i18n/zh-Hans/workflow.ts index 58366647b9..1c12c2244b 100644 --- a/web/i18n/zh-Hans/workflow.ts +++ b/web/i18n/zh-Hans/workflow.ts @@ -348,6 +348,7 @@ const translation = { apiPlaceholder: '输入 URL,输入变量时请键入‘/’', notStartWithHttp: 'API 应该以 http:// 或 https:// 开头', key: '键', + type: '类型', value: '值', bulkEdit: '批量编辑', keyValueEdit: '键值编辑',