Skip to content

Commit 23abb29

Browse files
authored
feat(autofill): consolidated tool-params & sub-block store for one unified store that handles sub-block values (#237)
1 parent 4f9c70b commit 23abb29

File tree

8 files changed

+275
-228
lines changed

8 files changed

+275
-228
lines changed

sim/app/w/[id]/components/workflow-block/components/sub-block/components/tool-input/tool-input.tsx

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { OAuthProvider } from '@/lib/oauth'
1313
import { cn } from '@/lib/utils'
1414
import { useCustomToolsStore } from '@/stores/custom-tools/store'
1515
import { useGeneralStore } from '@/stores/settings/general/store'
16-
import { useToolParamsStore } from '@/stores/tool-params/store'
16+
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
1717
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
1818
import { getAllBlocks } from '@/blocks'
1919
import { getTool } from '@/tools'
@@ -122,8 +122,12 @@ const getOperationOptions = (blockType: string): { label: string; id: string }[]
122122
const initializeToolParams = (
123123
toolId: string,
124124
params: ToolParam[],
125-
toolParamsStore: {
126-
resolveParamValue: (toolId: string, paramId: string, instanceId?: string) => string | undefined
125+
subBlockStore: {
126+
resolveToolParamValue: (
127+
toolId: string,
128+
paramId: string,
129+
instanceId?: string
130+
) => string | undefined
127131
},
128132
isAutoFillEnabled: boolean,
129133
instanceId?: string
@@ -134,7 +138,7 @@ const initializeToolParams = (
134138
if (isAutoFillEnabled) {
135139
// For each parameter, check if we have a stored/resolved value
136140
params.forEach((param) => {
137-
const resolvedValue = toolParamsStore.resolveParamValue(toolId, param.id, instanceId)
141+
const resolvedValue = subBlockStore.resolveToolParamValue(toolId, param.id, instanceId)
138142
if (resolvedValue) {
139143
initialParams[param.id] = resolvedValue
140144
}
@@ -152,7 +156,7 @@ export function ToolInput({ blockId, subBlockId }: ToolInputProps) {
152156
const [searchQuery, setSearchQuery] = useState('')
153157
const isWide = useWorkflowStore((state) => state.blocks[blockId]?.isWide)
154158
const customTools = useCustomToolsStore((state) => state.getAllTools())
155-
const toolParamsStore = useToolParamsStore()
159+
const subBlockStore = useSubBlockStore()
156160
const isAutoFillEnvVarsEnabled = useGeneralStore((state) => state.isAutoFillEnvVarsEnabled)
157161

158162
const toolBlocks = getAllBlocks().filter((block) => block.category === 'tools')
@@ -194,7 +198,7 @@ export function ToolInput({ blockId, subBlockId }: ToolInputProps) {
194198
const initialParams = initializeToolParams(
195199
toolId,
196200
requiredParams,
197-
toolParamsStore,
201+
subBlockStore,
198202
isAutoFillEnvVarsEnabled,
199203
blockId
200204
)
@@ -247,7 +251,7 @@ export function ToolInput({ blockId, subBlockId }: ToolInputProps) {
247251
const initialParams = initializeToolParams(
248252
toolId,
249253
toolParams,
250-
toolParamsStore,
254+
subBlockStore,
251255
isAutoFillEnvVarsEnabled,
252256
blockId
253257
)
@@ -327,7 +331,7 @@ export function ToolInput({ blockId, subBlockId }: ToolInputProps) {
327331

328332
// Only store non-empty values
329333
if (paramValue.trim()) {
330-
toolParamsStore.setParam(toolId, paramId, paramValue)
334+
subBlockStore.setToolParam(toolId, paramId, paramValue)
331335
}
332336

333337
// Update the value in the workflow

sim/app/w/[id]/components/workflow-block/components/sub-block/hooks/use-sub-block-value.ts

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { useCallback, useEffect, useRef } from 'react'
22
import { isEqual } from 'lodash'
33
import { useGeneralStore } from '@/stores/settings/general/store'
4-
import { useToolParamsStore } from '@/stores/tool-params/store'
54
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
65
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
76
import { 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])

sim/stores/index.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { useConsoleStore } from './panel/console/store'
88
import { useVariablesStore } from './panel/variables/store'
99
import { useEnvironmentStore } from './settings/environment/store'
1010
import { getSyncManagers, initializeSyncManagers, resetSyncManagers } from './sync-registry'
11-
import { useToolParamsStore } from './tool-params/store'
1211
import {
1312
loadRegistry,
1413
loadSubblockValues,
@@ -18,7 +17,6 @@ import {
1817
} from './workflows/persistence'
1918
import { useWorkflowRegistry } from './workflows/registry/store'
2019
import { useSubBlockStore } from './workflows/subblock/store'
21-
import { workflowSync } from './workflows/sync'
2220
import { useWorkflowStore } from './workflows/workflow/store'
2321

2422
const logger = createLogger('Stores')
@@ -232,7 +230,6 @@ export {
232230
useChatStore,
233231
useCustomToolsStore,
234232
useVariablesStore,
235-
useToolParamsStore,
236233
}
237234

238235
// Helper function to reset all stores
@@ -246,6 +243,7 @@ export const resetAllStores = () => {
246243
})
247244
useWorkflowStore.getState().clear()
248245
useSubBlockStore.getState().clear()
246+
useSubBlockStore.getState().clearToolParams()
249247
useNotificationStore.setState({ notifications: [] })
250248
useEnvironmentStore.setState({
251249
variables: {},
@@ -257,7 +255,6 @@ export const resetAllStores = () => {
257255
useChatStore.setState({ messages: [], isProcessing: false, error: null })
258256
useCustomToolsStore.setState({ tools: {} })
259257
useVariablesStore.getState().resetLoaded() // Reset variables store tracking
260-
useToolParamsStore.getState().clear()
261258
}
262259

263260
// Helper function to log all store states
@@ -273,7 +270,6 @@ export const logAllStores = () => {
273270
customTools: useCustomToolsStore.getState(),
274271
subBlock: useSubBlockStore.getState(),
275272
variables: useVariablesStore.getState(),
276-
toolParams: useToolParamsStore.getState(),
277273
}
278274

279275
return state

0 commit comments

Comments
 (0)