Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions apps/sim/blocks/blocks/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { TABLE_LIMITS } from '@/lib/table/constants'
import { filterRulesToFilter, sortRulesToSort } from '@/lib/table/query-builder/converters'
import type { BlockConfig, BlockToolParamsContext } from '@/blocks/types'
import {
failToolCallOnError,
isToolCall,
mapperOperation,
parseOptionalNumberInput,
Expand Down Expand Up @@ -94,15 +95,21 @@ interface ParsedParams {
* list) falls back to the model's `filter`/`sort` arg instead of discarding it; the canvas keeps
* the builder's result.
*/
function resolveBuilderOrJson(
function resolveBuilderOrJson<TRule>(
builderActive: boolean,
fromBuilder: () => unknown,
builderValue: unknown,
fromBuilder: (rules: TRule[]) => unknown,
json: unknown,
fieldName: string,
context: BlockToolParamsContext | undefined
): unknown {
if (builderActive) {
const built = fromBuilder() || undefined
// A tool row's rules that did not decode to an array are corrupt, not empty: falling back
// would run the operation on the model's filter instead of the one the user pinned.
if (isToolCall(context) && !Array.isArray(builderValue)) {
throw new Error(`Invalid ${fieldName} builder rules: expected a list of conditions.`)
}
const built = fromBuilder(builderValue as TRule[]) || undefined
if (built !== undefined || !isToolCall(context) || !json) return built
} else if (!json) {
return undefined
Expand All @@ -116,8 +123,8 @@ function resolveBulkFilter(
): unknown {
return resolveBuilderOrJson(
params.bulkFilterMode === 'builder' && Boolean(params.bulkFilterBuilder),
() =>
filterRulesToFilter(params.bulkFilterBuilder as Parameters<typeof filterRulesToFilter>[0]),
params.bulkFilterBuilder,
filterRulesToFilter,
params.filter,
'Filter',
context
Expand Down Expand Up @@ -195,14 +202,16 @@ const paramTransformers: Record<
const builderMode = params.builderMode === 'builder'
const filter = resolveBuilderOrJson(
builderMode && Boolean(params.filterBuilder),
() => filterRulesToFilter(params.filterBuilder as Parameters<typeof filterRulesToFilter>[0]),
params.filterBuilder,
filterRulesToFilter,
params.filter,
'Filter',
context
)
const sort = resolveBuilderOrJson(
builderMode && Boolean(params.sortBuilder),
() => sortRulesToSort(params.sortBuilder as Parameters<typeof sortRulesToSort>[0]),
params.sortBuilder,
sortRulesToSort,
params.sort,
'Sort',
context
Expand Down Expand Up @@ -760,7 +769,7 @@ Return ONLY the sort JSON:`,
const transformer = paramTransformers[mapperOperation(params, context)]

if (transformer) {
return transformer(rest as TableBlockParams, context)
return failToolCallOnError(context, () => transformer(rest as TableBlockParams, context))
Comment thread
waleedlatif1 marked this conversation as resolved.
Resolved
}

return rest
Expand Down
3 changes: 2 additions & 1 deletion apps/sim/blocks/blocks/table_v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type {
} from '@/lib/table/types'
import type { BlockConfig, BlockToolParamsContext } from '@/blocks/types'
import {
failToolCallOnError,
mapperOperation,
resolveTableColumnSelection,
TABLE_OUTPUT_COLUMNS_INPUT,
Expand Down Expand Up @@ -570,7 +571,7 @@ Return ONLY the JSON object:`,
const { operation, ...rest } = params
const transformer = paramTransformers[mapperOperation(params, context)]
if (transformer) {
return transformer(rest as TableBlockParams, context)
return failToolCallOnError(context, () => transformer(rest as TableBlockParams, context))
}
return rest
},
Expand Down
28 changes: 28 additions & 0 deletions apps/sim/blocks/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
SIM_AUTO_MODEL_ID,
} from '@/providers/models'
import { isPiSupportedModel } from '@/providers/pi-providers'
import { ToolCallParamsError } from '@/providers/tool-params-error'
import type { ProviderId } from '@/providers/types'
import { getProviderFromModel } from '@/providers/utils'
import { useProvidersStore } from '@/stores/providers/store'
Expand Down Expand Up @@ -948,6 +949,33 @@ export function toolCallArg<T>(
return isToolCall(context) ? value : undefined
}

/**
* What the model is told when a block rejects a tool call's params. Deliberately static: a
* mapper's own message can quote the value it rejected (`Invalid Limit "…"`, a JSON parse
* snippet), and by then a model-written `{{SECRET}}` placeholder has been resolved to plaintext.
*/
const TOOL_CALL_PARAMS_REJECTED_MESSAGE =
'The tool was not run: its arguments could not be mapped to the tool inputs. Check the argument values (filters, sort, row data, limit) and try again.'

/**
* Runs a mapper body, and on a tool call turns any failure into a {@link ToolCallParamsError} so
* the call fails instead of running on the raw merged params. Use it where those raw params are
* unsafe: a pinned value under a block-only key (a filter builder) drops out of them, leaving the
* model's own argument for the same tool param in its place. The original error stays on `cause`
* for server logs; the canvas rethrows it unchanged.
*/
export function failToolCallOnError<T>(
context: BlockToolParamsContext | undefined,
map: () => T
): T {
try {
return map()
} catch (error) {
if (!isToolCall(context) || error instanceof ToolCallParamsError) throw error
throw new ToolCallParamsError(TOOL_CALL_PARAMS_REJECTED_MESSAGE, { cause: error })
}
}

/**
* The operation a block's `tools.config.params` mapper runs for. A tool call passes it only in
* `context`: a stored tool row keeps its operation outside its params, and a tool that declares its
Expand Down
5 changes: 4 additions & 1 deletion apps/sim/executor/handlers/human-in-the-loop/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type { BlockHandler, ExecutionContext, PauseMetadata } from '@/executor/t
import { collectBlockData } from '@/executor/utils/block-data'
import { convertBuilderDataToJson, convertPropertyValue } from '@/executor/utils/builder-data'
import { parseObjectStrings } from '@/executor/utils/json'
import { ToolCallParamsError } from '@/providers/tool-params-error'
import { buildBlockToolParamsTransform } from '@/providers/utils'
import type { SerializedBlock } from '@/serializer/types'
import { executeTool } from '@/tools'
Expand Down Expand Up @@ -449,7 +450,9 @@ export class HumanInTheLoopBlockHandler implements BlockHandler {
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.
// notification down with it, so the raw params still go out — except a
// `ToolCallParamsError`, whose raw params are unsafe to send.
if (error instanceof ToolCallParamsError) throw error
logger.warn('Notification tool params transform failed, using raw params', {
toolId,
error,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,38 @@ const notifierBlock = {
outputs: {},
}

/** A notifier whose mapper rejects its stored params, as Table's does for a corrupt filter. */
const rejectingNotifierBlock = {
...notifierBlock,
tools: {
...notifierBlock.tools,
config: {
params: () => {
throw new ToolCallParamsError('Invalid JSON in Filter')
},
},
},
}

vi.mock('@/tools/utils', () => toolsUtilsMock)

vi.mock('@/tools', () => toolsMock)

import { getBlock } from '@/blocks/registry'
import { PAUSE_RESUME } from '@/executor/constants'
import { HumanInTheLoopBlockHandler } from '@/executor/handlers/human-in-the-loop/handler'
import { ToolCallParamsError } from '@/providers/tool-params-error'

toolsUtilsMockFns.mockGetTool.mockReturnValue({
id: 'notifier_send',
params: { channel: { type: 'string' }, silent: { type: 'boolean' } },
})

vi.mocked(getBlock).mockImplementation((type: string) =>
type === 'notifier' ? (notifierBlock as never) : undefined
)
vi.mocked(getBlock).mockImplementation((type: string) => {
if (type === 'notifier') return notifierBlock as never
if (type === 'rejecting_notifier') return rejectingNotifierBlock as never
return undefined
})

const executed: Array<Record<string, unknown>> = []
toolsMockFns.mockExecuteTool.mockImplementation(
Expand All @@ -53,7 +69,10 @@ toolsMockFns.mockExecuteTool.mockImplementation(
)

/** Runs one notification whose channel was configured in ADVANCED mode. */
async function runNotification(blockTypeId: string): Promise<Record<string, unknown>> {
async function runNotification(
blockTypeId: string,
notifierType = 'notifier'
): Promise<Record<string, unknown>> {
executed.length = 0

const block = {
Expand All @@ -74,7 +93,7 @@ async function runNotification(blockTypeId: string): Promise<Record<string, unkn
operation: PAUSE_RESUME.OPERATION.HUMAN,
notification: [
{
type: 'notifier',
type: notifierType,
toolId: 'notifier_send',
title: 'Notify',
params: { channel: '', manualChannel: 'C123', silent: 'false' },
Expand Down Expand Up @@ -103,6 +122,11 @@ describe('Human block notification params', () => {
})
})

it('v2 skips a notification whose mapper rejects its params instead of sending raw ones', async () => {
await runNotification('human_in_the_loop_v2', 'rejecting_notifier')
expect(executed).toEqual([])
})

it('v1 keeps handing the tool its raw stored values', async () => {
// Deliberately unchanged: altering what an already-configured v1 notification sends
// is exactly the break v2 exists to avoid.
Expand Down
7 changes: 5 additions & 2 deletions apps/sim/executor/handlers/pi/local/sim-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { projectResolvedSecretModelContent } from '@/executor/utils/resolved-sec
import type { ResolvedSecretTraceRegistry } from '@/executor/utils/resolved-secret-trace-registry'
import { annotateDuplicateToolBindings } from '@/executor/utils/tool-binding-labels'
import { assignProviderToolIdentities } from '@/providers/tool-identity'
import { ToolCallParamsError } from '@/providers/tool-params-error'
import type { ProviderToolConfig } from '@/providers/types'
import { transformBlockTool } from '@/providers/utils'
import { executeTool } from '@/tools'
Expand Down Expand Up @@ -129,8 +130,9 @@ function buildSimToolSpec(
* declares. Skipping it here left Pi local tools receiving a raw selector id
* where the tool expected a resolved one, and `'false'` where it expected `false`.
*
* A failure keeps the raw params, matching `prepareToolExecution`. The projected
* copy is only transformed when the raw one was, so the two stay comparable.
* A failure keeps the raw params, matching `prepareToolExecution`, except a
* `ToolCallParamsError`, which fails the call. The projected copy is only
* transformed when the raw one was, so the two stay comparable.
*/
let transformApplied = false
const applyParamsTransform = (input: Record<string, unknown>): Record<string, unknown> => {
Expand All @@ -140,6 +142,7 @@ function buildSimToolSpec(
transformApplied = true
return transformed
} catch (error) {
if (error instanceof ToolCallParamsError) throw error
logger.warn('paramsTransform failed for Pi local tool, using raw params', { error })
return input
}
Expand Down
7 changes: 7 additions & 0 deletions apps/sim/lib/workflows/search-replace/json-value-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,18 @@ export function isSearchableJsonValueSubBlock(
)
}

/**
* Rule builders stay as their stored JSON text in a tool row: the replacer edits a tool param's
* raw text, and has no path into a builder's rule array the way it does for `table` cells.
*/
const RAW_TEXT_TOOL_PARAM_SUBBLOCK_TYPES = new Set<SubBlockType>(['filter-builder', 'sort-builder'])

export function shouldParseSerializedSubBlockValue(
subBlockType: SubBlockType | undefined
): subBlockType is SubBlockType {
return Boolean(
subBlockType &&
!RAW_TEXT_TOOL_PARAM_SUBBLOCK_TYPES.has(subBlockType) &&
(isSearchableJsonValueSubBlock(subBlockType) || holdsObjectValue({ type: subBlockType }))
)
}
Expand Down
59 changes: 59 additions & 0 deletions apps/sim/lib/workflows/search-replace/replacements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SEARCH_REPLACE_BLOCK_CONFIGS,
} from '@/lib/workflows/search-replace/search-replace.fixtures'
import { WORKFLOW_SEARCH_SUBFLOW_FIELD_IDS } from '@/lib/workflows/search-replace/subflow-fields'
import { TableV2Block } from '@/blocks/blocks/table_v2'

/**
* Asserts real tool params and outputs, which the global `@/tools/metadata`
Expand Down Expand Up @@ -417,6 +418,64 @@ describe('buildWorkflowSearchReplacePlan', () => {
])
})

it('replaces a value inside filter builder rules stored as a serialized tool input param', () => {
const workflow = createSearchReplaceWorkflowFixture()
const rule = { id: 'r1', logicalOperator: 'and', column: 'status', operator: 'eq' }
workflow.blocks['tool-input-1'] = {
id: 'tool-input-1',
type: 'custom',
name: 'Tool Input Block',
position: { x: 0, y: 0 },
enabled: true,
outputs: {},
subBlocks: {
tools: {
id: 'tools',
type: 'tool-input',
value: [
{
type: 'table_v2',
toolId: 'table_query_rows_v2',
operation: 'query_rows',
title: 'Table',
params: {
tableId: 'tbl_1',
filterBuilder: JSON.stringify([{ ...rule, value: 'pending' }]),
},
},
],
},
},
}
const matches = indexWorkflowSearchMatches({
workflow,
query: 'pending',
mode: 'text',
blockConfigs: {
...SEARCH_REPLACE_BLOCK_CONFIGS,
custom: { subBlocks: [{ id: 'tools', title: 'Tools', type: 'tool-input' }] },
table_v2: TableV2Block,
},
}).filter((match) => match.blockId === 'tool-input-1' && match.editable)

const plan = buildWorkflowSearchReplacePlan({
blocks: workflow.blocks,
matches,
selectedMatchIds: new Set(matches.map((match) => match.id)),
defaultReplacement: 'approved',
})

expect(plan.conflicts).toEqual([])
expect(plan.updates).toHaveLength(1)
expect(plan.updates[0].nextValue).toEqual([
expect.objectContaining({
params: expect.objectContaining({
filterBuilder: JSON.stringify([{ ...rule, value: 'approved' }]),
}),
}),
])
})

it('conflicts when a selected duplicate file occurrence is removed', () => {
const workflow = createSearchReplaceWorkflowFixture()
const files = [
Expand Down
14 changes: 14 additions & 0 deletions apps/sim/providers/conversation-continuation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
setEncryptedConversationMessage,
} from '@/providers/conversation-metadata'
import { executeProviderTool } from '@/providers/runtime-context'
import { ToolCallParamsError } from '@/providers/tool-params-error'
import type { Message, ProviderRequest } from '@/providers/types'
import { prepareToolExecution } from '@/providers/utils'

Expand Down Expand Up @@ -137,6 +138,19 @@ describe('durable conversation restoration and continuation', () => {
).rejects.toBe(refused)
})

it('records a block mapper rejection as a failed tool call without dispatching it', async () => {
vi.mocked(prepareToolExecution).mockImplementationOnce(() => {
throw new ToolCallParamsError('Invalid JSON in Filter')
})
const session = await pendingSession()
await continuePendingConversationCalls(toolRequest, session)
expect(session.getPendingCalls()).toEqual([])
expect(session.getMessages('openai', 'model-a', 'binding-a')[1].content).toContain(
'Invalid JSON in Filter'
)
expect(executeProviderTool).not.toHaveBeenCalled()
})

it('clears a previously restored native message when the next fallback binding changes', async () => {
const messages = toolGroup()
const native = {
Expand Down
Loading
Loading