Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3afb854

Browse files
committedMar 14, 2025·
Generalize to util fn; add for output map;
Signed-off-by: Tyler Ohlsen <ohltyler@amazon.com>
1 parent ca75cb3 commit 3afb854

File tree

4 files changed

+86
-45
lines changed

4 files changed

+86
-45
lines changed
 

‎common/interfaces.ts

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ export type OutputMapEntry = InputMapEntry;
126126
export type InputMapFormValue = InputMapEntry[];
127127
export type OutputMapFormValue = OutputMapEntry[];
128128

129+
export type MapCache = {
130+
[idx: number]: Transform[];
131+
};
132+
129133
export type InputMapArrayFormValue = InputMapFormValue[];
130134
export type OutputMapArrayFormValue = OutputMapFormValue[];
131135

‎public/pages/workflow_detail/workflow_inputs/processor_inputs/ml_processor_inputs/model_inputs.tsx

+12-41
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
WorkflowConfig,
3838
getCharacterLimitedString,
3939
INPUT_TRANSFORM_OPTIONS,
40+
MapCache,
4041
} from '../../../../../../common';
4142
import {
4243
TextField,
@@ -50,6 +51,7 @@ import {
5051
sanitizeJSONPath,
5152
} from '../../../../../utils';
5253
import { ConfigureExpressionModal, ConfigureTemplateModal } from './modals/';
54+
import { updateCache } from './utils';
5355

5456
interface ModelInputsProps {
5557
config: IProcessorConfig;
@@ -120,9 +122,7 @@ export function ModelInputs(props: ModelInputsProps) {
120122
// Temporarily cache any configured transformations for different transform types.
121123
// For example, if a user configures a prompt, swaps the transform
122124
// type to "Data field", and swaps back to "Prompt", the prompt will be persisted.
123-
const [inputMapCache, _] = useState<{
124-
[idx: number]: Transform[];
125-
}>({});
125+
const [inputMapCache, setInputMapCache] = useState<MapCache>({});
126126

127127
// persisting doc/query/index mapping fields to collect a list
128128
// of options to display in the dropdowns when configuring input / output maps
@@ -391,49 +391,19 @@ export function ModelInputs(props: ModelInputsProps) {
391391
}
392392
onChange={(option) => {
393393
// before updating, cache any form values
394-
const curCache = inputMapCache[idx];
395-
if (
396-
curCache === undefined ||
397-
isEmpty(curCache)
398-
) {
399-
// case 1: there is no persisted state for this entry index. create a fresh arr
400-
inputMapCache[idx] = [mapEntry.value];
401-
} else if (
402-
!curCache.some(
403-
(transform: Transform) =>
404-
transform.transformType ===
405-
mapEntry.value.transformType
406-
)
407-
) {
408-
// case 2: there is persisted state for this entry index, but not for the particular
409-
// transform type. append to the arr
410-
inputMapCache[idx] = [
411-
...inputMapCache[idx],
412-
mapEntry.value,
413-
];
414-
} else {
415-
// case 3: there is persisted state for this entry index, and for the particular transform type.
416-
// Update the cache with the current form value(s)
417-
inputMapCache[idx] = inputMapCache[
418-
idx
419-
].map((cachedEntry) => {
420-
if (
421-
cachedEntry.transformType ===
422-
mapEntry.value.transformType
423-
) {
424-
return mapEntry.value;
425-
} else {
426-
return cachedEntry;
427-
}
428-
});
429-
}
430-
394+
const updatedCache = updateCache(
395+
inputMapCache,
396+
mapEntry,
397+
idx
398+
);
431399
setFieldValue(
432400
`${inputMapFieldPath}.${idx}.value.transformType`,
433401
option
434402
);
435403
// Pre-populate with any cached values, if found
436-
const curCacheForOption = curCache?.find(
404+
const curCacheForOption = updatedCache[
405+
idx
406+
]?.find(
437407
(transform: Transform) =>
438408
transform.transformType === option
439409
);
@@ -445,6 +415,7 @@ export function ModelInputs(props: ModelInputsProps) {
445415
`${inputMapFieldPath}.${idx}.value.nestedVars`,
446416
curCacheForOption?.nestedVars || []
447417
);
418+
setInputMapCache(updatedCache);
448419
}}
449420
/>
450421
</EuiFlexItem>

‎public/pages/workflow_detail/workflow_inputs/processor_inputs/ml_processor_inputs/model_outputs.tsx

+24-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ import {
3434
EMPTY_OUTPUT_MAP_ENTRY,
3535
ExpressionVar,
3636
OUTPUT_TRANSFORM_OPTIONS,
37+
Transform,
38+
MapCache,
3739
} from '../../../../../../common';
3840
import { TextField } from '../../input_fields';
3941
import { AppState } from '../../../../../store';
4042
import { ConfigureMultiExpressionModal } from './modals';
43+
import { updateCache } from './utils';
4144

4245
interface ModelOutputsProps {
4346
config: IProcessorConfig;
@@ -93,6 +96,11 @@ export function ModelOutputs(props: ModelOutputsProps) {
9396
}
9497
}, [models, getIn(values, modelFieldPath)?.id]);
9598

99+
// Temporarily cache any configured transformations for different transform types.
100+
// For example, if a user configures a prompt, swaps the transform
101+
// type to "Data field", and swaps back to "Prompt", the prompt will be persisted.
102+
const [outputMapCache, setOutputMapCache] = useState<MapCache>({});
103+
96104
// Adding a map entry to the end of the existing arr
97105
function addMapEntry(curEntries: OutputMapFormValue): void {
98106
const updatedEntries = [...curEntries, EMPTY_OUTPUT_MAP_ENTRY];
@@ -247,20 +255,32 @@ export function ModelOutputs(props: ModelOutputsProps) {
247255
) || ''
248256
}
249257
onChange={(option) => {
258+
// before updating, cache any form values
259+
const updatedCache = updateCache(
260+
outputMapCache,
261+
mapEntry,
262+
idx
263+
);
250264
setFieldValue(
251265
`${outputMapFieldPath}.${idx}.value.transformType`,
252266
option
253267
);
254-
// If the transform type changes, clear any set value and/or nested vars,
255-
// as it will likely not make sense under other types/contexts.
268+
// Pre-populate with any cached values, if found
269+
const curCacheForOption = updatedCache[
270+
idx
271+
]?.find(
272+
(transform: Transform) =>
273+
transform.transformType === option
274+
);
256275
setFieldValue(
257276
`${outputMapFieldPath}.${idx}.value.value`,
258-
''
277+
curCacheForOption?.value || ''
259278
);
260279
setFieldValue(
261280
`${outputMapFieldPath}.${idx}.value.nestedVars`,
262-
[]
281+
curCacheForOption?.nestedVars || []
263282
);
283+
setOutputMapCache(updatedCache);
264284
}}
265285
/>
266286
</EuiFlexItem>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { isEmpty } from 'lodash';
7+
import {
8+
MapCache,
9+
InputMapEntry,
10+
OutputMapEntry,
11+
Transform,
12+
} from '../../../../../../common';
13+
14+
// Update a cache of data transform values based on a given form value
15+
export function updateCache(
16+
cache: MapCache,
17+
mapEntry: InputMapEntry | OutputMapEntry,
18+
idx: number // the mapEntry index
19+
): MapCache {
20+
const updatedCache = cache;
21+
const curCache = updatedCache[idx];
22+
if (curCache === undefined || isEmpty(curCache)) {
23+
// case 1: there is no persisted state for this entry index. create a fresh arr
24+
updatedCache[idx] = [mapEntry.value];
25+
} else if (
26+
!curCache.some(
27+
(transform: Transform) =>
28+
transform.transformType === mapEntry.value.transformType
29+
)
30+
) {
31+
// case 2: there is persisted state for this entry index, but not for the particular
32+
// transform type. append to the arr
33+
updatedCache[idx] = [...updatedCache[idx], mapEntry.value];
34+
} else {
35+
// case 3: there is persisted state for this entry index, and for the particular transform type.
36+
// Update the cache with the current form value(s)
37+
updatedCache[idx] = updatedCache[idx].map((cachedEntry) => {
38+
if (cachedEntry.transformType === mapEntry.value.transformType) {
39+
return mapEntry.value;
40+
} else {
41+
return cachedEntry;
42+
}
43+
});
44+
}
45+
return updatedCache;
46+
}

0 commit comments

Comments
 (0)
Please sign in to comment.