Skip to content

Self-improving AI Employees for everyone

fix(client): reset a dependent option when its parent changes

trohitga21133c35299
diff --git a/client/src/components/ParameterRenderer.tsx b/client/src/components/ParameterRenderer.tsx
index 35ba42df..3bffcbd7 100644
--- a/client/src/components/ParameterRenderer.tsx
+++ b/client/src/components/ParameterRenderer.tsx
@@ -31,6 +31,7 @@ import { shouldShowParameter } from '../utils/parameterVisibility';
// Map node types to provider keys for AI model nodes
import { AI_MODEL_PROVIDER_MAP } from '../lib/aiModelProviders';
+import { nextDynamicOptionValue } from '../lib/dynamicOptions';
import { resolveNodeDescription } from '../lib/nodeSpec';
import { uploadToWorkspace } from '../lib/workspaceUpload';
@@ -834,6 +835,10 @@ const ParameterRenderer: React.FC<ParameterRendererProps> = ({
activeNodeIdRef.current = selectedNode?.id ?? null;
const onChangeRef = React.useRef(onChange);
onChangeRef.current = onChange;
+ // Dependency values of the last completed option load, per node, so a
+ // dependent field can tell "its parent just changed" from "the panel
+ // opened" (see nextDynamicOptionValue).
+ const lastOptionDepsRef = React.useRef<{ nodeId: string; key: string } | null>(null);
// Don't render hidden parameters
if (parameter.type === 'hidden') {
@@ -1080,10 +1085,17 @@ const ParameterRenderer: React.FC<ParameterRendererProps> = ({
value: o.value,
}));
+ const dependencyKey = JSON.stringify(dependsOn.map((dep: string) => allParamsResolved[dep] ?? null));
+ const previousDeps = lastOptionDepsRef.current;
+ const dependencyChanged =
+ dependsOn.length > 0 && previousDeps?.nodeId === nodeId && previousDeps.key !== dependencyKey;
+ lastOptionDepsRef.current = { nodeId, key: dependencyKey };
+
setDynamicOptions(options);
DynamicParameterService.updateParameterOptions(nodeId, parameter.name, options);
- if (options.length > 0 && (!currentValue || currentValue === '')) {
- onChangeRef.current(options[0].value);
+ const nextValue = nextDynamicOptionValue(currentValue, options, dependencyChanged);
+ if (nextValue !== undefined) {
+ onChangeRef.current(nextValue);
}
} catch (error) {
if (isCurrent()) {
diff --git a/client/src/lib/__tests__/dynamicOptions.test.ts b/client/src/lib/__tests__/dynamicOptions.test.ts
new file mode 100644
index 00000000..55144e77
--- /dev/null
+++ b/client/src/lib/__tests__/dynamicOptions.test.ts
@@ -0,0 +1,34 @@
+import { describe, expect, it } from 'vitest';
+
+import { nextDynamicOptionValue } from '../dynamicOptions';
+
+const LLAMA_ONLY = [{ value: 'llama3' }, { value: 'llama3:8b' }];
+
+describe('nextDynamicOptionValue', () => {
+ it('fills an empty field with the first option', () => {
+ expect(nextDynamicOptionValue('', LLAMA_ONLY, false)).toBe('llama3');
+ expect(nextDynamicOptionValue(undefined, LLAMA_ONLY, true)).toBe('llama3');
+ });
+
+ it('moves off a value the newly chosen parent does not offer', () => {
+ // The endpoint changed from one serving qwen2.5 to one serving llama only.
+ expect(nextDynamicOptionValue('qwen2.5', LLAMA_ONLY, true)).toBe('llama3');
+ });
+
+ it('keeps a value the new list still offers', () => {
+ expect(nextDynamicOptionValue('llama3:8b', LLAMA_ONLY, true)).toBeUndefined();
+ });
+
+ it('never rewrites a stored value just because the panel opened', () => {
+ expect(nextDynamicOptionValue('/api/v1/douyin/web/fetch_one_video', LLAMA_ONLY, false)).toBeUndefined();
+ });
+
+ it('leaves an expression alone', () => {
+ expect(nextDynamicOptionValue('{{trigger.model}}', LLAMA_ONLY, true)).toBeUndefined();
+ });
+
+ it('changes nothing while the loader offers nothing', () => {
+ expect(nextDynamicOptionValue('', [], true)).toBeUndefined();
+ expect(nextDynamicOptionValue('qwen2.5', [], true)).toBeUndefined();
+ });
+});
diff --git a/client/src/lib/dynamicOptions.ts b/client/src/lib/dynamicOptions.ts
new file mode 100644
index 00000000..2eea44f7
--- /dev/null
+++ b/client/src/lib/dynamicOptions.ts
@@ -0,0 +1,25 @@
+/**
+ * Which value a loader-driven field should switch to once its options
+ * arrive, or `undefined` to keep the one it has.
+ *
+ * - An empty field takes the first option.
+ * - A field whose options depend on another field (`loadOptionsDependsOn`)
+ * also moves to the first option when that field has just changed and the
+ * current value is not among the new options: the old value belongs to the
+ * previous list, such as a model the newly chosen endpoint does not serve.
+ * - Otherwise the value stays, including on first load, so a stored value the
+ * list does not show (an alternate form, an imported workflow) is never
+ * rewritten just by opening the panel. An expression (`{{node.field}}`)
+ * always stays: it resolves at run time.
+ */
+export function nextDynamicOptionValue(
+ currentValue: unknown,
+ options: ReadonlyArray<{ value: unknown }>,
+ dependencyChanged: boolean,
+): unknown | undefined {
+ if (options.length === 0) return undefined;
+ if (currentValue === undefined || currentValue === null || currentValue === '') return options[0].value;
+ if (!dependencyChanged) return undefined;
+ if (typeof currentValue === 'string' && currentValue.includes('{{')) return undefined;
+ return options.some((option) => option.value === currentValue) ? undefined : options[0].value;
+}