Skip to content

Commit f4bf222

Browse files
committed
feat(FR-1700): implement automatic fallback to auto agent when invalid (#4673)
Resolves #4672 ([FR-1700](https://lablup.atlassian.net/browse/FR-1700)) This PR adds a new `fallbackToAuto` prop to the AgentSelect component, replacing the previous `autoSelectDefault` prop. When enabled, this feature automatically switches to "Auto" selection when the current agent value becomes invalid (e.g., when the agent is no longer available in the options list). The implementation includes: - Adding a new effect hook that monitors changes to the value and available agent options - Using `useEffectEvent` to handle the fallback logic - Enabling this feature in ResourceAllocationFormItems by setting `fallbackToAuto` prop **Checklist:** - [ ] Documentation - [ ] Minium required manager version - [ ] Specific setting for review (eg., KB link, endpoint or how to setup) - [ ] Minimum requirements to check during review - [ ] Test case(s) to demonstrate the difference of before/after [FR-1700]: https://lablup.atlassian.net/browse/FR-1700?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ
1 parent 1584a82 commit f4bf222

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

react/src/components/AgentSelect.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,25 @@ import { useControllableValue } from 'ahooks';
55
import { Select, SelectProps, theme } from 'antd';
66
import { filterOutEmpty, BAIFlex, mergeFilterValues } from 'backend.ai-ui';
77
import _ from 'lodash';
8-
import React, { useDeferredValue, useState } from 'react';
8+
import React, {
9+
useDeferredValue,
10+
useEffect,
11+
useEffectEvent,
12+
useState,
13+
} from 'react';
914
import { useTranslation } from 'react-i18next';
1015
import { graphql, useLazyLoadQuery } from 'react-relay';
1116

1217
interface Props extends Omit<SelectProps, 'options'> {
13-
autoSelectDefault?: boolean;
18+
fallbackToAuto?: boolean;
1419
fetchKey?: string;
1520
resourceGroup?: string | null;
1621
}
1722

1823
const AgentSelect: React.FC<Props> = ({
1924
fetchKey,
2025
resourceGroup,
26+
fallbackToAuto,
2127
...selectProps
2228
}) => {
2329
const { t } = useTranslation();
@@ -93,6 +99,7 @@ const AgentSelect: React.FC<Props> = ({
9399
}
94100
})
95101
.value();
102+
96103
return {
97104
label: (
98105
<BAIFlex
@@ -127,6 +134,24 @@ const AgentSelect: React.FC<Props> = ({
127134
.includes(deferredSearchStr?.toLowerCase() ?? '')
128135
? { label: t('session.launcher.AutoSelect'), value: 'auto' }
129136
: undefined;
137+
138+
const changeToAutoWhenInvalidValueEffectEvent = useEffectEvent(() => {
139+
if (fallbackToAuto && value) {
140+
const valueArray = _.castArray(value);
141+
const validValues = agentOptions.map((option) => option.value);
142+
const newValue = valueArray.filter((v) =>
143+
validValues.includes(v as string),
144+
);
145+
if (!_.isEqual(valueArray, newValue)) {
146+
setValue('auto');
147+
}
148+
}
149+
});
150+
151+
useEffect(() => {
152+
changeToAutoWhenInvalidValueEffectEvent();
153+
}, [value, agentOptions]);
154+
130155
return (
131156
<Select
132157
loading={searchStr !== deferredSearchStr}

react/src/components/SessionFormItems/ResourceAllocationFormItems.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,7 @@ const ResourceAllocationFormItems: React.FC<
12431243
resourceGroup={currentResourceGroupInForm}
12441244
fetchKey={agentFetchKey}
12451245
mode={supportMultiAgents ? 'multiple' : undefined}
1246+
fallbackToAuto
12461247
labelRender={
12471248
supportMultiAgents
12481249
? ({ label, value }) => {

0 commit comments

Comments
 (0)