Skip to content

Commit 908b240

Browse files
committed
fix(integrations): sweep2 m3 — read the mapper operation from the tool-call context and validate integrations against vendor specs
1 parent 0ea25f4 commit 908b240

83 files changed

Lines changed: 2102 additions & 555 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { AffinityBlock } from '@/blocks/blocks/affinity'
3+
4+
/** Runs the mapper the way an Agent tool call does: its result is spread over the merged args. */
5+
function toolCall(args: Record<string, unknown>, operation: string) {
6+
const mapParams = AffinityBlock.tools.config?.params
7+
if (!mapParams) throw new Error('Affinity block has no params mapper')
8+
return { ...args, ...mapParams(args, { operation }) }
9+
}
10+
11+
describe('AffinityBlock agent tool calls', () => {
12+
it('maps block-field keys and keeps model args instead of dropping them', () => {
13+
const result = toolCall(
14+
{ apiKey: 'key', noteEntityType: 'company', entityId: '42', limit: '10' },
15+
'list_entity_notes'
16+
)
17+
expect(result).toMatchObject({ entityType: 'company', entityId: '42', limit: 10 })
18+
expect(result.noteEntityType).toBeUndefined()
19+
20+
expect(
21+
toolCall({ apiKey: 'key', name: 'Deals', type: 'company', isPublic: true }, 'create_list')
22+
).toMatchObject({ name: 'Deals', type: 'company', isPublic: true })
23+
})
24+
})

‎apps/sim/blocks/blocks/affinity.ts‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { AffinityIcon } from '@/components/icons'
22
import type { BlockConfig, BlockMeta } from '@/blocks/types'
33
import { AuthMode, IntegrationType } from '@/blocks/types'
44
import {
5+
isToolCall,
6+
mapperOperation,
57
parseOptionalBooleanInput,
68
parseOptionalNumberInput,
79
withSavedCredential,
@@ -1337,9 +1339,13 @@ export const AffinityBlock: BlockConfig = {
13371339
* omitting a key leaves the previous operation's value on the wire — an
13381340
* advanced field is serialized on non-emptiness alone, even while the UI
13391341
* hides it. `undefined` is what actually drops one.
1342+
*
1343+
* On a tool call the model's args arrive under the tool's own param names,
1344+
* so a tool param the selected operation does not fill from a block field
1345+
* keeps the model's value instead of being dropped.
13401346
*/
1341-
params: (params) => {
1342-
const operation = String(params.operation ?? '')
1347+
params: (params, context) => {
1348+
const operation = mapperOperation(params, context)
13431349
const isIn = (group: readonly string[]) => group.includes(operation)
13441350

13451351
const isKeywordSearch = isIn(KEYWORD_SEARCH_OPERATIONS)
@@ -1380,7 +1386,7 @@ export const AffinityBlock: BlockConfig = {
13801386
else if (isKeywordSearch) ids = params.searchIds
13811387
else if (isFieldValueSelection) ids = params.valueFieldIds
13821388

1383-
return {
1389+
const toolParams: Record<string, unknown> = {
13841390
apiKey: params.apiKey,
13851391
entityType,
13861392
type,
@@ -1470,6 +1476,16 @@ export const AffinityBlock: BlockConfig = {
14701476
totalCount: isIn(TOTAL_COUNT_OPERATIONS)
14711477
? parseOptionalBooleanInput(params.totalCount)
14721478
: undefined,
1479+
}
1480+
1481+
if (isToolCall(context)) {
1482+
for (const [key, value] of Object.entries(toolParams)) {
1483+
if (value === undefined) toolParams[key] = params[key]
1484+
}
1485+
}
1486+
1487+
return {
1488+
...toolParams,
14731489
fieldEntityType: undefined,
14741490
noteEntityType: undefined,
14751491
mergeEntityType: undefined,
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { AgentMailBlock } from '@/blocks/blocks/agentmail'
3+
4+
/** Runs the mapper the way an Agent tool call does: its result is spread over the merged args. */
5+
function toolCall(args: Record<string, unknown>, operation: string) {
6+
const mapParams = AgentMailBlock.tools.config?.params
7+
if (!mapParams) throw new Error('AgentMail block has no params mapper')
8+
return { ...args, ...mapParams(args, { operation }) }
9+
}
10+
11+
describe('AgentMailBlock agent tool calls', () => {
12+
it('maps the block-field reply message key and keeps the model recipients and flags', () => {
13+
expect(
14+
toolCall(
15+
{ inboxId: 'inbox', replyMessageId: 'msg-1', to: 'a@example.com', replyAll: true },
16+
'reply_message'
17+
)
18+
).toMatchObject({ messageId: 'msg-1', to: 'a@example.com', replyAll: true })
19+
})
20+
21+
it('keeps a boolean permanent flag from the model', () => {
22+
expect(
23+
toolCall({ inboxId: 'inbox', threadId: 't-1', permanent: true }, 'delete_thread')
24+
).toMatchObject({ permanent: true })
25+
})
26+
27+
it('treats a stored "false" permanent dropdown as a trash move, not a permanent delete', () => {
28+
expect(
29+
toolCall({ inboxId: 'inbox', threadId: 't-1', permanent: 'false' }, 'delete_thread')
30+
).toMatchObject({ permanent: false })
31+
})
32+
33+
it('treats a stored "false" reply-all dropdown as a reply to the sender only', () => {
34+
expect(
35+
toolCall({ inboxId: 'inbox', replyMessageId: 'msg-1', replyAll: 'false' }, 'reply_message')
36+
).toMatchObject({ replyAll: false })
37+
})
38+
})

‎apps/sim/blocks/blocks/agentmail.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AgentMailIcon } from '@/components/icons'
22
import type { BlockConfig, BlockMeta } from '@/blocks/types'
33
import { AuthMode, IntegrationType } from '@/blocks/types'
4-
import { withSavedCredential } from '@/blocks/utils'
4+
import { mapperOperation, toolCallArg, withSavedCredential } from '@/blocks/utils'
55

66
export const AgentMailBlock: BlockConfig = {
77
type: 'agentmail',
@@ -567,9 +567,9 @@ export const AgentMailBlock: BlockConfig = {
567567
],
568568
config: {
569569
tool: (params) => `agentmail_${params.operation || 'send_message'}`,
570-
params: (params) => {
570+
params: (params, context) => {
571+
const operation = mapperOperation(params, context)
571572
const {
572-
operation,
573573
inboxIdParam,
574574
permanent,
575575
replyMessageId,
@@ -590,11 +590,11 @@ export const AgentMailBlock: BlockConfig = {
590590
}
591591

592592
if (operation === 'delete_thread' && permanent !== undefined) {
593-
rest.permanent = permanent === 'true'
593+
rest.permanent = permanent === true || permanent === 'true'
594594
}
595595

596596
if (operation === 'reply_message' && replyAll !== undefined) {
597-
rest.replyAll = replyAll === 'true'
597+
rest.replyAll = replyAll === true || replyAll === 'true'
598598
}
599599

600600
if (operation === 'reply_message' && replyMessageId) {
@@ -604,7 +604,7 @@ export const AgentMailBlock: BlockConfig = {
604604
if (operation === 'reply_message' && replyTo) {
605605
rest.to = replyTo
606606
} else if (operation === 'reply_message') {
607-
rest.to = undefined
607+
rest.to = toolCallArg(context, rest.to)
608608
}
609609

610610
if (operation === 'forward_message' && forwardMessageId) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { AgentPhoneBlock } from '@/blocks/blocks/agentphone'
3+
4+
/** Runs the mapper the way an Agent tool call does: its result is spread over the merged args. */
5+
function toolCall(args: Record<string, unknown>, operation: string) {
6+
const mapParams = AgentPhoneBlock.tools.config?.params
7+
if (!mapParams) throw new Error('AgentPhone block has no params mapper')
8+
return { ...args, ...mapParams(args, { operation }) }
9+
}
10+
11+
describe('AgentPhoneBlock agent tool calls', () => {
12+
it('maps block-field filters and coerces limits for an operation passed only in context', () => {
13+
expect(
14+
toolCall({ apiKey: 'key', callsStatus: 'completed', limit: '25' }, 'list_calls')
15+
).toMatchObject({ status: 'completed', limit: 25 })
16+
})
17+
})

‎apps/sim/blocks/blocks/agentphone.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { toError } from '@sim/utils/errors'
22
import { AgentPhoneIcon } from '@/components/icons'
33
import type { BlockConfig, BlockMeta } from '@/blocks/types'
44
import { AuthMode, IntegrationType } from '@/blocks/types'
5-
import { withSavedCredential } from '@/blocks/utils'
5+
import { mapperOperation, withSavedCredential } from '@/blocks/utils'
66

77
const CONVERSATION_OPS = [
88
'get_conversation',
@@ -588,9 +588,9 @@ export const AgentPhoneBlock: BlockConfig = {
588588
],
589589
config: {
590590
tool: (params) => `agentphone_${params.operation || 'create_number'}`,
591-
params: (params) => {
591+
params: (params, context) => {
592+
const operation = mapperOperation(params, context)
592593
const {
593-
operation,
594594
attachAgentId,
595595
callAgentId,
596596
toNumberCall,
@@ -633,7 +633,7 @@ export const AgentPhoneBlock: BlockConfig = {
633633
if (messageBody !== undefined) rest.body = messageBody
634634
}
635635

636-
if (['create_contact', 'update_contact'].includes(operation as string)) {
636+
if (['create_contact', 'update_contact'].includes(operation)) {
637637
if (contactPhoneNumber) rest.phoneNumber = contactPhoneNumber
638638
if (contactName) rest.name = contactName
639639
if (contactEmail) rest.email = contactEmail
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { AlgoliaBlock } from '@/blocks/blocks/algolia'
3+
4+
/** Runs the mapper the way an Agent tool call does: its result is spread over the merged args. */
5+
function toolCall(args: Record<string, unknown>, operation: string) {
6+
const mapParams = AlgoliaBlock.tools.config?.params
7+
if (!mapParams) throw new Error('Algolia block has no params mapper')
8+
return { ...args, ...mapParams(args, { operation }) }
9+
}
10+
11+
describe('AlgoliaBlock agent tool calls', () => {
12+
it('maps the block-field requests key on a tool call', () => {
13+
expect(
14+
toolCall({ indexName: 'products', getRecordsRequests: '[{"objectID":"1"}]' }, 'get_records')
15+
).toMatchObject({ requests: '[{"objectID":"1"}]' })
16+
})
17+
18+
it('keeps the model filters, requests, and copy-or-move operation', () => {
19+
expect(toolCall({ indexName: 'p', filters: 'brand:x' }, 'delete_by_filter')).toMatchObject({
20+
filters: 'brand:x',
21+
})
22+
expect(
23+
toolCall({ indexName: 'p', requests: [{ objectID: '1' }] }, 'get_records')
24+
).toMatchObject({ requests: [{ objectID: '1' }] })
25+
expect(
26+
toolCall({ indexName: 'p', destination: 'q', operation: 'move' }, 'copy_move_index')
27+
).toMatchObject({ operation: 'move' })
28+
})
29+
})

‎apps/sim/blocks/blocks/algolia.ts‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { AlgoliaIcon } from '@/components/icons'
2-
import type { BlockConfig, BlockMeta } from '@/blocks/types'
2+
import type { BlockConfig, BlockMeta, BlockToolParamsContext } from '@/blocks/types'
33
import { AuthMode, IntegrationType } from '@/blocks/types'
4-
import { withSavedCredential } from '@/blocks/utils'
4+
import { isToolCall, mapperOperation, toolCallArg, withSavedCredential } from '@/blocks/utils'
55

66
export const AlgoliaBlock: BlockConfig = {
77
type: 'algolia',
@@ -593,11 +593,11 @@ Return ONLY the JSON array.`,
593593
],
594594
config: {
595595
tool: (params: Record<string, unknown>) => `algolia_${params.operation}`,
596-
params: (params: Record<string, unknown>) => {
597-
const { operation, ...rest } = params
596+
params: (params: Record<string, unknown>, context?: BlockToolParamsContext) => {
597+
const operation = mapperOperation(params, context)
598598
const result: Record<string, unknown> = {}
599599

600-
for (const [key, value] of Object.entries(rest)) {
600+
for (const [key, value] of Object.entries(params)) {
601601
if (value === undefined || value === null || value === '') continue
602602
result[key] = value
603603
}
@@ -618,15 +618,18 @@ Return ONLY the JSON array.`,
618618
result.getRankingInfo = toBool(result.getRankingInfo, false)
619619
}
620620
if (operation === 'copy_move_index') {
621-
result.operation = result.copyMoveOperation
621+
// A tool call carries the tool's own copy-or-move `operation` as a model arg.
622+
if (!isToolCall(context) || result.copyMoveOperation !== undefined) {
623+
result.operation = result.copyMoveOperation
624+
}
622625
result.copyMoveOperation = undefined
623626
}
624627
if (operation === 'delete_by_filter') {
625-
result.filters = result.deleteFilters
628+
result.filters = result.deleteFilters ?? toolCallArg(context, result.filters)
626629
result.deleteFilters = undefined
627630
}
628631
if (operation === 'get_records') {
629-
result.requests = result.getRecordsRequests
632+
result.requests = result.getRecordsRequests ?? toolCallArg(context, result.requests)
630633
result.getRecordsRequests = undefined
631634
}
632635

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { AmplitudeBlock } from '@/blocks/blocks/amplitude'
3+
4+
/** Runs the mapper the way an Agent tool call does: its result is spread over the merged args. */
5+
function toolCall(args: Record<string, unknown>, operation: string) {
6+
const mapParams = AmplitudeBlock.tools.config?.params
7+
if (!mapParams) throw new Error('Amplitude block has no params mapper')
8+
return { ...args, ...mapParams(args, { operation }) }
9+
}
10+
11+
describe('AmplitudeBlock agent tool calls', () => {
12+
it('maps the block-field event key for an operation passed only in context', () => {
13+
expect(
14+
toolCall(
15+
{ apiKey: 'k', secretKey: 's', segmentationEventType: 'signup', start: '20260101' },
16+
'event_segmentation'
17+
)
18+
).toMatchObject({ eventType: 'signup', start: '20260101' })
19+
})
20+
})

‎apps/sim/blocks/blocks/amplitude.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AmplitudeIcon } from '@/components/icons'
22
import { AuthMode, type BlockConfig, type BlockMeta, IntegrationType } from '@/blocks/types'
3-
import { withSavedCredential } from '@/blocks/utils'
3+
import { mapperOperation, withSavedCredential } from '@/blocks/utils'
44

55
/*
66
* Amplitude identifies a person by user id or device id — either one alone is
@@ -1012,10 +1012,10 @@ export const AmplitudeBlock: BlockConfig = {
10121012
],
10131013
config: {
10141014
tool: (params) => `amplitude_${params.operation}`,
1015-
params: (params) => {
1015+
params: (params, context) => {
10161016
const result: Record<string, unknown> = {}
10171017

1018-
switch (params.operation) {
1018+
switch (mapperOperation(params, context)) {
10191019
case 'send_event':
10201020
if (params.sendEventUserProperties)
10211021
result.userProperties = params.sendEventUserProperties

0 commit comments

Comments
 (0)