11import { useCallback , useEffect , useRef } from 'react'
22import { isEqual } from 'lodash'
33import { useGeneralStore } from '@/stores/settings/general/store'
4- import { useToolParamsStore } from '@/stores/tool-params/store'
54import { useSubBlockStore } from '@/stores/workflows/subblock/store'
65import { useWorkflowStore } from '@/stores/workflows/workflow/store'
76import { getProviderFromModel } from '@/providers/utils'
@@ -24,20 +23,19 @@ function handleAgentBlockApiKey(
2423 // Skip if we couldn't determine a provider
2524 if ( ! provider || provider === 'ollama' ) return
2625
27- const toolParamsStore = useToolParamsStore . getState ( )
2826 const subBlockStore = useSubBlockStore . getState ( )
2927
3028 // Try to get a saved API key for this provider
31- const savedValue = toolParamsStore . resolveParamValue ( provider , 'apiKey' , blockId )
29+ const savedValue = subBlockStore . resolveToolParamValue ( provider , 'apiKey' , blockId )
3230
3331 // If we have a valid API key, use it
3432 if ( savedValue && savedValue !== '' ) {
35- // Only update if different from current value
36- if ( savedValue !== storeValue ) {
37- subBlockStore . setValue ( blockId , subBlockId , savedValue )
38- }
33+ // Always update the value when switching models, even if it appears the same
34+ // This handles cases where the field shows masked values but needs to update
35+ subBlockStore . setValue ( blockId , subBlockId , savedValue )
3936 } else {
40- // No API key found for this provider - ALWAYS clear the field
37+ // Always clear the field when switching to a model with no API key
38+ // Don't wait for user interaction to clear it
4139 subBlockStore . setValue ( blockId , subBlockId , '' )
4240 }
4341}
@@ -53,16 +51,16 @@ function handleStandardBlockApiKey(
5351) {
5452 if ( ! blockType ) return
5553
56- const toolParamsStore = useToolParamsStore . getState ( )
54+ const subBlockStore = useSubBlockStore . getState ( )
5755
5856 // Only auto-fill if the field is empty
5957 if ( ! storeValue || storeValue === '' ) {
6058 // Pass the blockId as instanceId to check if this specific instance has been cleared
61- const savedValue = toolParamsStore . resolveParamValue ( blockType , 'apiKey' , blockId )
59+ const savedValue = subBlockStore . resolveToolParamValue ( blockType , 'apiKey' , blockId )
6260
6361 if ( savedValue && savedValue !== '' && savedValue !== storeValue ) {
6462 // Auto-fill the API key from the param store
65- useSubBlockStore . getState ( ) . setValue ( blockId , subBlockId , savedValue )
63+ subBlockStore . setValue ( blockId , subBlockId , savedValue )
6664 }
6765 }
6866 // Handle environment variable references
@@ -73,13 +71,13 @@ function handleStandardBlockApiKey(
7371 storeValue . endsWith ( '}}' )
7472 ) {
7573 // Pass the blockId as instanceId
76- const currentValue = toolParamsStore . resolveParamValue ( blockType , 'apiKey' , blockId )
74+ const currentValue = subBlockStore . resolveToolParamValue ( blockType , 'apiKey' , blockId )
7775
7876 if ( currentValue !== storeValue ) {
7977 // If we got a replacement or null, update the field
8078 if ( currentValue ) {
8179 // Replacement found - update to new reference
82- useSubBlockStore . getState ( ) . setValue ( blockId , subBlockId , currentValue )
80+ subBlockStore . setValue ( blockId , subBlockId , currentValue )
8381 }
8482 }
8583 }
@@ -97,32 +95,39 @@ function storeApiKeyValue(
9795) {
9896 if ( ! blockType ) return
9997
100- const toolParamsStore = useToolParamsStore . getState ( )
98+ const subBlockStore = useSubBlockStore . getState ( )
10199
102- // Check if this is an empty value for an API key field that previously had a value
103- // This indicates the user has deliberately cleared the field
100+ // Check if this is user explicitly clearing a field that had a value
101+ // We only want to mark it as cleared if it's a user action, not an automatic
102+ // clearing from model switching
104103 if (
105104 storeValue &&
106105 storeValue !== '' &&
107106 ( newValue === null || newValue === '' || String ( newValue ) . trim ( ) === '' )
108107 ) {
109108 // Mark this specific instance as cleared so we don't auto-fill it
110- toolParamsStore . markParamAsCleared ( blockId , 'apiKey' )
109+ subBlockStore . markParamAsCleared ( blockId , 'apiKey' )
111110 return
112111 }
113112
114113 // Only store non-empty values
115114 if ( ! newValue || String ( newValue ) . trim ( ) === '' ) return
116115
116+ // If user enters a value, we should clear any "cleared" flag
117+ // to ensure auto-fill will work in the future
118+ if ( subBlockStore . isParamCleared ( blockId , 'apiKey' ) ) {
119+ subBlockStore . unmarkParamAsCleared ( blockId , 'apiKey' )
120+ }
121+
117122 // For agent blocks, store the API key under the provider name
118123 if ( blockType === 'agent' && modelValue ) {
119124 const provider = getProviderFromModel ( modelValue )
120125 if ( provider && provider !== 'ollama' ) {
121- toolParamsStore . setParam ( provider , 'apiKey' , String ( newValue ) )
126+ subBlockStore . setToolParam ( provider , 'apiKey' , String ( newValue ) )
122127 }
123128 } else {
124129 // For other blocks, store under the block type
125- toolParamsStore . setParam ( blockType , 'apiKey' , String ( newValue ) )
130+ subBlockStore . setToolParam ( blockType , 'apiKey' , String ( newValue ) )
126131 }
127132}
128133
@@ -184,9 +189,27 @@ export function useSubBlockValue<T = any>(
184189 // Update the previous model reference
185190 prevModelRef . current = modelValue
186191
187- // Handle API key autofill for the new model
188- if ( isAutoFillEnvVarsEnabled && modelValue ) {
189- handleAgentBlockApiKey ( blockId , subBlockId , modelValue , storeValue )
192+ // For agent blocks, always clear the field if needed
193+ // But only fill with saved values if auto-fill is enabled
194+ if ( modelValue ) {
195+ const provider = getProviderFromModel ( modelValue )
196+
197+ // Skip if we couldn't determine a provider
198+ if ( ! provider || provider === 'ollama' ) return
199+
200+ const subBlockStore = useSubBlockStore . getState ( )
201+
202+ // Check if there's a saved value for this provider
203+ const savedValue = subBlockStore . resolveToolParamValue ( provider , 'apiKey' , blockId )
204+
205+ if ( savedValue && savedValue !== '' && isAutoFillEnvVarsEnabled ) {
206+ // Only auto-fill if the feature is enabled
207+ subBlockStore . setValue ( blockId , subBlockId , savedValue )
208+ } else {
209+ // Always clear immediately when switching to a model with no saved key
210+ // or when auto-fill is disabled
211+ subBlockStore . setValue ( blockId , subBlockId , '' )
212+ }
190213 }
191214 }
192215 } , [ blockId , subBlockId , blockType , isApiKey , modelValue , isAutoFillEnvVarsEnabled , storeValue ] )
0 commit comments