diff --git a/common/utils.ts b/common/utils.ts index dc5b780f..6c2e29a7 100644 --- a/common/utils.ts +++ b/common/utils.ts @@ -36,7 +36,7 @@ export function getCharacterLimitedString( : ''; } -export function customStringify(jsonObj: {}): string { +export function customStringify(jsonObj: {} | []): string { return JSON.stringify(jsonObj, undefined, 2); } diff --git a/public/pages/workflow_detail/workflow_inputs/processor_inputs/input_transform_modal.tsx b/public/pages/workflow_detail/workflow_inputs/processor_inputs/input_transform_modal.tsx index 574f951b..5e43ea37 100644 --- a/public/pages/workflow_detail/workflow_inputs/processor_inputs/input_transform_modal.tsx +++ b/public/pages/workflow_detail/workflow_inputs/processor_inputs/input_transform_modal.tsx @@ -97,8 +97,12 @@ export function InputTransformModal(props: InputTransformModalProps) { const [sourceInput, setSourceInput] = useState('[]'); const [transformedInput, setTransformedInput] = useState('{}'); - // get the current input map + // get some current form values const map = getIn(values, props.inputMapFieldPath) as MapArrayFormValue; + const oneToOne = getIn( + values, + `${props.baseConfigPath}.${props.config.id}.one_to_one` + ); // selected transform state const transformOptions = map.map((_, idx) => ({ @@ -310,7 +314,11 @@ export function InputTransformModal(props: InputTransformModalProps) { (hit: SearchHit) => hit._source ); if (hits.length > 0) { - setSourceInput(customStringify(hits[0])); + setSourceInput( + // if one-to-one, treat the source input as a single retrieved document + // else, treat it as all of the returned documents + customStringify(oneToOne ? hits[0] : hits) + ); } }) .catch((error: any) => { diff --git a/public/pages/workflow_detail/workflow_inputs/processor_inputs/output_transform_modal.tsx b/public/pages/workflow_detail/workflow_inputs/processor_inputs/output_transform_modal.tsx index 11b9d922..01a4ce93 100644 --- a/public/pages/workflow_detail/workflow_inputs/processor_inputs/output_transform_modal.tsx +++ b/public/pages/workflow_detail/workflow_inputs/processor_inputs/output_transform_modal.tsx @@ -86,7 +86,7 @@ export function OutputTransformModal(props: OutputTransformModalProps) { const [sourceOutput, setSourceOutput] = useState('[]'); const [transformedOutput, setTransformedOutput] = useState('{}'); - // get some current values + // get some current form values const map = getIn(values, props.outputMapFieldPath) as MapArrayFormValue; const fullResponsePath = getIn( values, diff --git a/public/utils/utils.ts b/public/utils/utils.ts index d6ce3d22..2b607afb 100644 --- a/public/utils/utils.ts +++ b/public/utils/utils.ts @@ -24,6 +24,7 @@ import { import { getCore, getDataSourceEnabled } from '../services'; import { MDSQueryParams, + MapEntry, ModelInputMap, ModelOutputMap, } from '../../common/interfaces'; @@ -177,19 +178,17 @@ export function unwrapTransformedDocs( // ML inference processors will use standard dot notation or JSONPath depending on the input. // We follow the same logic here to generate consistent results. -export function generateTransform(input: {}, map: MapFormValue): {} { +// Collapse the values depending on if the input is an array or not. +export function generateTransform(input: {} | [], map: MapFormValue): {} | [] { let output = {}; map.forEach((mapEntry) => { const path = mapEntry.value; try { - let transformedResult = undefined; - if (mapEntry.value.startsWith(JSONPATH_ROOT_SELECTOR)) { - // JSONPath transform - transformedResult = jsonpath.query(input, path); - // Standard dot notation - } else { - transformedResult = get(input, path); - } + const transformedResult = Array.isArray(input) + ? input.map((inputEntry) => + getTransformedResult(mapEntry, inputEntry, path) + ) + : getTransformedResult(mapEntry, input, path); output = { ...output, [mapEntry.key]: transformedResult || '', @@ -199,6 +198,18 @@ export function generateTransform(input: {}, map: MapFormValue): {} { return output; } +function getTransformedResult( + mapEntry: MapEntry, + input: {}, + path: string +): any { + return mapEntry.value.startsWith(JSONPATH_ROOT_SELECTOR) + ? // JSONPath transform + jsonpath.query(input, path) + : // Standard dot notation + get(input, path); +} + // Derive the collection of model inputs from the model interface JSONSchema into a form-ready list export function parseModelInputs( modelInterface: ModelInterface | undefined