diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/param-dependents.ts b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/param-dependents.ts index 5751d69b74c..145f9bb42ac 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/param-dependents.ts +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/param-dependents.ts @@ -1,4 +1,5 @@ import { getTransitiveSubBlockDependents } from '@/lib/workflows/subblocks/dependencies' +import { omitConditionHiddenParams } from '@/lib/workflows/tool-input/hidden-params' import type { StoredTool } from '@/lib/workflows/tool-input/types' import { getBlock } from '@/blocks/registry' import type { ToolMetadata } from '@/tools/metadata' @@ -27,10 +28,14 @@ export function clearDependentToolParams( return next ?? params } -/** Keeps user-configured inputs that the newly selected operation accepts. */ +/** + * Keeps user-configured inputs that the newly selected operation accepts and still shows: a + * value whose fields the operation hides would otherwise linger out of sight and still run. + */ export function getParamsForToolOperation( tool: Pick, - nextTool: ToolMetadata + nextTool: ToolMetadata, + operation: string ): Record { const newParamIds = new Set( Object.entries(nextTool.params ?? {}) @@ -64,5 +69,6 @@ export function getParamsForToolOperation( Object.entries(tool.params || {}).forEach(([paramId, value]) => { if (newParamIds.has(paramId) && value) preservedParams[paramId] = value }) - return clearDependentToolParams(tool.type, preservedParams, 'operation') + const visibleParams = omitConditionHiddenParams(subBlocks, preservedParams, operation) + return clearDependentToolParams(tool.type, visibleParams, 'operation') } diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx index bd584693029..6d2c81d98ae 100644 --- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx +++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/tool-input.tsx @@ -981,7 +981,7 @@ export const ToolInput = memo(function ToolInput({ return } - const preservedParams = getParamsForToolOperation(tool, newToolConfig) + const preservedParams = getParamsForToolOperation(tool, newToolConfig, operation) if (tool.type === 'jira') { const subBlockStore = useSubBlockStore.getState() diff --git a/apps/sim/executor/handlers/human-in-the-loop/handler.ts b/apps/sim/executor/handlers/human-in-the-loop/handler.ts index c24812b6377..1585d798a36 100644 --- a/apps/sim/executor/handlers/human-in-the-loop/handler.ts +++ b/apps/sim/executor/handlers/human-in-the-loop/handler.ts @@ -7,6 +7,7 @@ import { isCanonicalPair, scopeCanonicalModesForTool, } from '@/lib/workflows/subblocks/visibility' +import { omitConditionHiddenParams } from '@/lib/workflows/tool-input/hidden-params' import { getBlock } from '@/blocks/registry' import type { BlockOutput } from '@/blocks/types' import { @@ -422,10 +423,13 @@ export class HumanInTheLoopBlockHandler implements BlockHandler { Object.values(buildCanonicalIndex(subBlocks).groupsById) as CanonicalGroup[] ).filter(isCanonicalPair) + const operation = + toolConfig.operation ?? + (typeof storedParams.operation === 'string' ? storedParams.operation : undefined) + const visibleParams = omitConditionHiddenParams(subBlocks, storedParams, operation) + const { paramsTransform } = buildBlockToolParamsTransform({ - operation: - toolConfig.operation ?? - (typeof storedParams.operation === 'string' ? storedParams.operation : undefined), + operation, blockSubBlocks: subBlocks, blockParamsFn: notificationBlock?.tools?.config?.params, blockInputDefs: notificationBlock?.inputs, @@ -438,10 +442,10 @@ export class HumanInTheLoopBlockHandler implements BlockHandler { ), }) - if (!paramsTransform) return storedParams + if (!paramsTransform) return visibleParams try { - return paramsTransform(storedParams) + return paramsTransform(visibleParams) } catch (error) { // Mirrors `prepareToolExecution`: a transform that throws must not take the // notification down with it, so the raw params still go out. @@ -449,7 +453,7 @@ export class HumanInTheLoopBlockHandler implements BlockHandler { toolId, error, }) - return storedParams + return visibleParams } } diff --git a/apps/sim/executor/handlers/pi/local/sim-tools.ts b/apps/sim/executor/handlers/pi/local/sim-tools.ts index acb590d644c..ff27d7fd9b4 100644 --- a/apps/sim/executor/handlers/pi/local/sim-tools.ts +++ b/apps/sim/executor/handlers/pi/local/sim-tools.ts @@ -167,8 +167,15 @@ function buildSimToolSpec( if (!inputProjection.complete || !projectedTool) { return unavailableToolResult() } + // Only the stored keys the transform kept: a value the selected operation hides was + // dropped from `preseededParams`, so its projected copy must be dropped too. + const projectedPreseededParams = Object.fromEntries( + Object.entries(projectedTool.params || {}).filter(([key]) => + Object.hasOwn(preseededParams, key) + ) + ) const mergedProjectedParams = mergeToolParameters( - projectedTool.params || {}, + projectedPreseededParams, args as Record ) diff --git a/apps/sim/lib/workflows/tool-input/hidden-params.ts b/apps/sim/lib/workflows/tool-input/hidden-params.ts new file mode 100644 index 00000000000..19240580049 --- /dev/null +++ b/apps/sim/lib/workflows/tool-input/hidden-params.ts @@ -0,0 +1,199 @@ +import { isRecordLike } from '@sim/utils/object' +import { + evaluateSubBlockCondition, + isNonEmptyValue, + type SubBlockCondition, +} from '@/lib/workflows/subblocks/visibility' +import type { SubBlockConfig } from '@/blocks/types' + +type ConditionSubBlock = Pick< + SubBlockConfig, + | 'id' + | 'type' + | 'mode' + | 'condition' + | 'reactiveCondition' + | 'canonicalParamId' + | 'password' + | 'options' +> + +/** + * Whether `operation` is one of the block's own operation choices. Only then can a condition on + * `operation` be decided: a stored row with no operation, or one holding a tool id the dropdown + * does not list, leaves every operation-gated field undecided. + */ +function isBlockOperation( + subBlocks: ConditionSubBlock[], + operation: string, + values: Record +): boolean { + return subBlocks.some((subBlock) => { + if (subBlock.id !== 'operation' || subBlock.mode === 'trigger') return false + if (subBlock.mode === 'trigger-advanced' || !subBlock.options) return false + let options: unknown + try { + options = + typeof subBlock.options === 'function' ? subBlock.options({ values }) : subBlock.options + } catch { + return false + } + return ( + Array.isArray(options) && + options.some((option) => isRecordLike(option) && option.id === operation) + ) + }) +} + +/** A stored scalar a condition can be decided against; JSON-shaped and empty values cannot. */ +function isDecidableValue(value: unknown): boolean { + if (typeof value === 'boolean' || typeof value === 'number') return true + if (typeof value !== 'string') return false + const trimmed = value.trim() + return trimmed.length > 0 && !trimmed.startsWith('[') && !trimmed.startsWith('{') +} + +/** The typed reading of a stored string, since a tool row stores `true` and `3` as text. */ +function decodeScalar(value: unknown): unknown { + if (value === 'true') return true + if (value === 'false') return false + if (typeof value === 'string' && value.trim() !== '' && Number.isFinite(Number(value))) { + return Number(value) + } + return value +} + +/** + * True only when the clause is decidably false. A clause on a field with no decidable value is + * treated as satisfiable, and a stored string is also tried in its typed reading, so every doubt + * resolves toward keeping the value. + */ +function isClauseFalse( + clause: SubBlockCondition, + values: Record, + isDecidableField: (field: string) => boolean +): boolean { + const { field } = clause + if (!isDecidableField(field)) return false + const single: SubBlockCondition = { field, value: clause.value, not: clause.not } + if (evaluateSubBlockCondition(single, values)) return false + const decoded = decodeScalar(values[field]) + return decoded === values[field] || !evaluateSubBlockCondition(single, { [field]: decoded }) +} + +/** + * Resolves a function condition, or `null` when it read a field the row cannot decide. A function + * condition names one field in what it returns but may branch on others (Video Generator gates a + * setting on the model, which the model itself supplies at call time), so every read is watched. + */ +function resolveFunctionCondition( + condition: (values?: Record) => SubBlockCondition, + values: Record, + isDecidableField: (field: string) => boolean +): SubBlockCondition | null { + let readUndecidedField = false + const watched = new Proxy(values, { + get(target, key, receiver) { + if (typeof key === 'string' && !isDecidableField(key)) readUndecidedField = true + return Reflect.get(target, key, receiver) + }, + has(target, key) { + if (typeof key === 'string' && !isDecidableField(key)) readUndecidedField = true + return Reflect.has(target, key) + }, + ownKeys(target) { + readUndecidedField = true + return Reflect.ownKeys(target) + }, + }) + const resolved = condition(watched) + return readUndecidedField ? null : resolved +} + +function isCredentialSubBlock(subBlock: ConditionSubBlock): boolean { + return ( + subBlock.type === 'oauth-input' || + subBlock.canonicalParamId === 'oauthCredential' || + subBlock.password === true + ) +} + +function isConditionHidden( + subBlock: ConditionSubBlock, + values: Record, + isDecidableField: (field: string) => boolean +): boolean { + if (isCredentialSubBlock(subBlock) || !subBlock.condition || subBlock.reactiveCondition) { + return false + } + let condition: SubBlockCondition | null + try { + condition = + typeof subBlock.condition === 'function' + ? resolveFunctionCondition(subBlock.condition, values, isDecidableField) + : subBlock.condition + } catch { + return false + } + if (!condition) return false + const clauses = condition.and ? [condition, condition.and] : [condition] + return clauses.some((clause) => isClauseFalse(clause, values, isDecidableField)) +} + +/** + * Every sub-block that stores a value under `paramId`: those whose id or canonical id it is, + * widened to the whole basic/advanced group, since either member may hold the group's value. + */ +function getWritingSubBlocks(subBlocks: ConditionSubBlock[], paramId: string): ConditionSubBlock[] { + const groups = new Set() + for (const subBlock of subBlocks) { + if ( + (subBlock.id === paramId || subBlock.canonicalParamId === paramId) && + subBlock.canonicalParamId + ) { + groups.add(subBlock.canonicalParamId) + } + } + return subBlocks.filter( + (subBlock) => + subBlock.id === paramId || + (subBlock.canonicalParamId !== undefined && groups.has(subBlock.canonicalParamId)) + ) +} + +/** + * Drops stored tool-row values the selected operation hides. + * + * A tool row keeps values across operation switches, but renders only the sub-blocks whose + * `condition` holds for the selected operation. A value left by a previous operation is then + * invisible to the user, and because it counts as user-provided it is also withheld from the + * model's schema, yet it still reaches the block mapper and the tool. A value is dropped only + * when every sub-block that writes it is decidably hidden under `{ ...params, operation }`. + * + * Kept: values no sub-block writes (pure tool params), values any writing sub-block shows (a + * canonical pair counts as shown when any member is), credential and password fields, and any + * value whose condition depends on a field the row has no decidable value for. Returns `params` + * itself when nothing is dropped. + */ +export function omitConditionHiddenParams( + subBlocks: ConditionSubBlock[] | undefined, + params: Record, + operation: string | undefined +): Record { + if (!subBlocks?.length) return params + const values: Record = { ...params, operation } + const operationKnown = Boolean(operation && isBlockOperation(subBlocks, operation, values)) + const isDecidableField = (field: string) => + field === 'operation' ? operationKnown : isDecidableValue(values[field]) + const isHidden = (paramId: string, value: T): boolean => { + if (paramId === 'operation' || !isNonEmptyValue(value)) return false + const writers = getWritingSubBlocks(subBlocks, paramId) + return ( + writers.length > 0 && + writers.every((subBlock) => isConditionHidden(subBlock, values, isDecidableField)) + ) + } + const entries = Object.entries(params) + const kept = entries.filter(([paramId, value]) => !isHidden(paramId, value)) + return kept.length === entries.length ? params : Object.fromEntries(kept) +} diff --git a/apps/sim/providers/utils.test.ts b/apps/sim/providers/utils.test.ts index 051ebcb6457..40581d0c0cf 100644 --- a/apps/sim/providers/utils.test.ts +++ b/apps/sim/providers/utils.test.ts @@ -12,12 +12,15 @@ vi.mock('@/lib/internal/workflows/read-tool-enrichment', () => ({ })) import { getParamsForToolOperation } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/tool-input/param-dependents' +import { CbInsightsV2Block } from '@/blocks/blocks/cbinsights' +import { CheckrV2Block } from '@/blocks/blocks/checkr' import { GaugeBlock } from '@/blocks/blocks/gauge' import { GreenhouseV2Block } from '@/blocks/blocks/greenhouse' import { IntuneBlock } from '@/blocks/blocks/intune' import { MicrosoftExcelV2Block } from '@/blocks/blocks/microsoft_excel' import { RampBlock } from '@/blocks/blocks/ramp' import { RevenueCatBlock } from '@/blocks/blocks/revenuecat' +import { TrelloBlock } from '@/blocks/blocks/trello' import { VideoGeneratorV3Block } from '@/blocks/blocks/video_generator' import * as blockRegistry from '@/blocks/registry' import { normalizeFileInput } from '@/blocks/utils' @@ -47,6 +50,9 @@ import { transformBlockTool, } from '@/providers/utils' import { useProvidersStore } from '@/stores/providers/store' +import { cbinsightsSearchFirmographicsTool } from '@/tools/cbinsights/search_firmographics' +import { checkrCreateReportTool } from '@/tools/checkr/create_report' +import { checkrListCandidatesTool } from '@/tools/checkr/list_candidates' import { gaugeGetQueryMetadataTool } from '@/tools/gauge/get_query_metadata' import { gaugeListAnswersTool } from '@/tools/gauge/list_answers' import { gaugeListCitationsTool } from '@/tools/gauge/list_citations' @@ -86,6 +92,8 @@ import { getSubBlocksForToolInput } from '@/tools/params' import { rampListReimbursementsTool } from '@/tools/ramp/list-reimbursements' import { rampListTransactionsTool } from '@/tools/ramp/list-transactions' import { revenuecatGetCustomerTool } from '@/tools/revenuecat/get_customer' +import { trelloListListsTool } from '@/tools/trello/list_lists' +import { trelloUpdateListTool } from '@/tools/trello/update_list' import { falaiVideoTool } from '@/tools/video/falai' import { runwayVideoTool } from '@/tools/video/runway' @@ -149,7 +157,8 @@ describe('integration filters through agent execution', () => { vi.spyOn(blockRegistry, 'getBlock').mockReturnValue(GaugeBlock) const params = getParamsForToolOperation( { type: 'gauge', params: { org: 'example', category: 'example', topic: '["top_stale"]' } }, - tool + tool, + tool.id ) const providerTool = await transformBlockTool( { type: 'gauge', params }, @@ -338,7 +347,8 @@ describe('integration filters through agent execution', () => { } const params = getParamsForToolOperation( { type: 'greenhouse_v2', params: previousParams }, - tool + tool, + operation ) const providerTool = await transformBlockTool( { type: 'greenhouse_v2', params }, @@ -373,7 +383,8 @@ describe('integration filters through agent execution', () => { source_id: '81', }, }, - tool + tool, + 'update_application' ) const providerTool = await transformBlockTool( { type: 'greenhouse_v2', params }, @@ -406,7 +417,8 @@ describe('integration filters through agent execution', () => { candidate_id: '42', }, }, - tool + tool, + 'create_application' ) const providerTool = await transformBlockTool( { type: 'greenhouse_v2', params }, @@ -436,7 +448,8 @@ describe('integration filters through agent execution', () => { vi.spyOn(blockRegistry, 'getBlock').mockReturnValue(GreenhouseV2Block) const params = getParamsForToolOperation( { type: 'greenhouse_v2', params: { opening_id: 'external-opening' } }, - tool + tool, + 'hire_application' ) const providerTool = await transformBlockTool( { type: 'greenhouse_v2', params }, @@ -473,7 +486,8 @@ describe('integration filters through agent execution', () => { accessToken: 'expired-resolved-token', }, }, - greenhouseListJobsV2Tool + greenhouseListJobsV2Tool, + 'list_jobs' ) expect(params[credentialId]).toBe('fixture-credential') expect(params).not.toHaveProperty('accessToken') @@ -723,6 +737,114 @@ describe('integration filters through agent execution', () => { }) }) +describe('tool-row values hidden by the selected operation', () => { + it.each([ + { + name: 'Trello archive status left by Update Card', + block: TrelloBlock, + operation: 'trello_update_list', + tool: trelloUpdateListTool, + stored: { listId: 'list-1', closed: 'true' }, + hiddenParam: 'closed', + }, + { + name: 'CB Insights websites left by Look Up Organizations', + block: CbInsightsV2Block, + operation: 'search_firmographics', + tool: cbinsightsSearchFirmographicsTool, + stored: { keyword: 'payments', urls: 'stripe.com' }, + hiddenParam: 'urls', + }, + { + name: 'Checkr geo left by Get Geo', + block: CheckrV2Block, + operation: 'list_candidates', + tool: checkrListCandidatesTool, + stored: { geoId: 'geo-stale' }, + hiddenParam: 'geoId', + }, + { + name: 'Checkr tags left by Set Report Tags', + block: CheckrV2Block, + operation: 'create_report', + tool: checkrCreateReportTool, + stored: { candidateId: 'cand-1', package: 'basic', tags: 'stale-tag' }, + hiddenParam: 'tags', + }, + ])('neither runs nor hides from the model the $name', async (testCase) => { + const providerTool = await transformBlockTool( + { type: testCase.block.type, operation: testCase.operation, params: testCase.stored }, + { + selectedOperation: testCase.operation, + getAllBlocks: () => [testCase.block], + getTool: () => testCase.tool, + } + ) + if (!providerTool) throw new Error('Agent tool was not created') + expect(providerTool.parameters.properties).toHaveProperty(testCase.hiddenParam) + const { toolParams } = prepareToolExecution(providerTool, {}, {}, undefined) + expect(toolParams[testCase.hiddenParam]).toBeUndefined() + }) + + it.each([ + { stored: { manualBoardId: 'board-advanced' }, member: 'advanced member' }, + { stored: { boardSelector: 'board-basic' }, member: 'basic member' }, + { stored: { boardId: 'board-canonical' }, member: 'canonical id' }, + ])('keeps a board stored under the $member when the operation shows it', async ({ stored }) => { + const providerTool = await transformBlockTool( + { type: 'trello', operation: 'trello_list_lists', params: stored }, + { + selectedOperation: 'trello_list_lists', + getAllBlocks: () => [TrelloBlock], + getTool: () => trelloListListsTool, + } + ) + if (!providerTool) throw new Error('Trello agent tool was not created') + const { toolParams } = prepareToolExecution(providerTool, {}, {}, undefined) + expect(toolParams.boardId).toBe(Object.values(stored)[0]) + }) + + it('keeps a stored tool param no sub-block writes', async () => { + const providerTool = await transformBlockTool( + { type: 'trello', operation: 'trello_update_list', params: { listId: 'l', idBoard: 'b' } }, + { + selectedOperation: 'trello_update_list', + getAllBlocks: () => [TrelloBlock], + getTool: () => trelloUpdateListTool, + } + ) + if (!providerTool) throw new Error('Trello agent tool was not created') + expect(providerTool.params).toEqual({ listId: 'l', idBoard: 'b' }) + expect(providerTool.parameters.properties).not.toHaveProperty('idBoard') + }) + + it('keeps operation-gated values when the stored operation is not one the block lists', async () => { + const providerTool = await transformBlockTool( + { type: 'checkr_v2', params: { geoId: 'geo-1' } }, + { + selectedOperation: undefined, + getAllBlocks: () => [CheckrV2Block], + getTool: () => checkrListCandidatesTool, + } + ) + if (!providerTool) throw new Error('Checkr agent tool was not created') + expect(providerTool.params).toEqual({ geoId: 'geo-1' }) + }) + + it('drops a value the new operation hides when the row switches operations', () => { + vi.spyOn(blockRegistry, 'getBlock').mockReturnValue(CheckrV2Block) + const params = getParamsForToolOperation( + { + type: 'checkr_v2', + params: { candidateId: 'cand-1', tags: 'stale-tag', credential: 'fixture-credential' }, + }, + checkrCreateReportTool, + 'create_report' + ) + expect(params).toEqual({ candidateId: 'cand-1', credential: 'fixture-credential' }) + }) +}) + describe('Excel targets through agent execution', () => { it.each(['model', 'configured'] as const)( 'clears the %s range after canonical argument mapping', diff --git a/apps/sim/providers/utils.ts b/apps/sim/providers/utils.ts index 07ef9be1b39..7ab3a6aebea 100644 --- a/apps/sim/providers/utils.ts +++ b/apps/sim/providers/utils.ts @@ -21,6 +21,7 @@ import { resolveActiveCanonicalValue, scopeCanonicalModesForTool, } from '@/lib/workflows/subblocks/visibility' +import { omitConditionHiddenParams } from '@/lib/workflows/tool-input/hidden-params' import { resolveBlockToolId } from '@/lib/workflows/tool-input/identity' import { assembleCustomBlockInputMapping, isCustomBlockType } from '@/blocks/custom/build-config' import type { BlockToolParamsContext, SubBlockConfig } from '@/blocks/types' @@ -841,7 +842,14 @@ export async function transformBlockTool( const { createLLMToolSchema } = await import('@/tools/params') - const userProvidedParams = block.params || {} + const operation = + selectedOperation ?? + (typeof block.params?.operation === 'string' ? block.params.operation : undefined) + const userProvidedParams = omitConditionHiddenParams( + blockDef.subBlocks, + block.params || {}, + operation + ) const canonicalGroups: CanonicalGroup[] = blockDef?.subBlocks ? // canonical-index-unscoped: an agent tool resolves against `block.params`, which only ever @@ -899,9 +907,7 @@ export async function transformBlockTool( } const { paramsTransform, jsonShapedParamKeys } = buildBlockToolParamsTransform({ - operation: - selectedOperation ?? - (typeof userProvidedParams.operation === 'string' ? userProvidedParams.operation : undefined), + operation, blockSubBlocks: blockDef?.subBlocks, blockParamsFn: blockDef?.tools?.config?.params as BlockToolParamsFn | undefined, blockInputDefs: blockDef?.inputs as Record | undefined,