Skip to content

Commit dd73880

Browse files
committed
refactor: simplify and clean up the sweep changes
1 parent bd68ad9 commit dd73880

4 files changed

Lines changed: 20 additions & 33 deletions

File tree

‎apps/sim/blocks/blocks/discord.ts‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
toolCallArg,
1414
withSavedCredential,
1515
} from '@/blocks/utils'
16+
import { parseMessageIds } from '@/tools/discord/bulk_delete_messages'
1617

1718
/**
1819
* Canonical basic/advanced pair, shared by the card sentences below. Listing
@@ -49,15 +50,6 @@ const HOIST_NO = { label: "No - Don't display separately", id: 'false' }
4950
const MENTIONABLE_YES = { label: 'Yes - Role can be mentioned', id: 'true' }
5051
const MENTIONABLE_NO = { label: 'No - Role cannot be mentioned', id: 'false' }
5152

52-
/**
53-
* Splits the bulk-delete IDs, which the canvas stores as a comma-separated string and a model
54-
* sends as an array.
55-
*/
56-
function parseMessageIds(value: unknown): string[] {
57-
const ids = Array.isArray(value) ? value : String(value ?? '').split(',')
58-
return ids.map((id) => String(id).trim()).filter(Boolean)
59-
}
60-
6153
function discordToolParams(
6254
params: BlockToolParams,
6355
requireBotToken: boolean,

‎apps/sim/blocks/blocks/mailgun.test.ts‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,16 @@ describe('Mailgun block add_list_member mapping', () => {
5959
expect(formFields(params)).toMatchObject({ address: 'bob@example.com', name: 'Bob' })
6060
})
6161

62-
it('honors an unsubscribed choice on a tool call, whether a stored string or a model boolean', () => {
63-
const stored = mapParams(
64-
{ apiKey: 'key', listAddress: 'l@mg.example.com', address: 'a@x.com', subscribed: 'false' },
65-
{ operation: 'add_list_member' }
66-
)
67-
const model = mapParams(
68-
{ apiKey: 'key', listAddress: 'l@mg.example.com', address: 'a@x.com', subscribed: false },
69-
{ operation: 'add_list_member' }
70-
)
71-
expect(formFields(stored).subscribed).toBe('no')
72-
expect(formFields(model).subscribed).toBe('no')
73-
})
74-
75-
it('keeps a model boolean true subscribed on a tool call', () => {
62+
it.each([
63+
['a stored string', 'false', 'no'],
64+
['a model boolean', false, 'no'],
65+
['a model boolean', true, 'yes'],
66+
])('honors subscribed given as %s (%j) on a tool call', (_label, subscribed, expected) => {
7667
const params = mapParams(
77-
{ apiKey: 'key', listAddress: 'l@mg.example.com', address: 'a@x.com', subscribed: true },
68+
{ apiKey: 'key', listAddress: 'l@mg.example.com', address: 'a@x.com', subscribed },
7869
{ operation: 'add_list_member' }
7970
)
80-
expect(formFields(params).subscribed).toBe('yes')
71+
expect(formFields(params).subscribed).toBe(expected)
8172
})
8273

8374
it('maps the canvas List Address and Member Email as before', () => {

‎apps/sim/tools/discord/bulk_delete_messages.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ import { DISCORD_CREDENTIAL_OAUTH } from '@/tools/shared/saved-credentials'
66
import type { ToolConfig } from '@/tools/types'
77
import { safeUrlPathSegment } from '@/tools/url-path'
88

9+
/**
10+
* Splits the bulk-delete IDs, which the canvas stores as a comma-separated string and a model
11+
* sends as an array.
12+
*/
13+
export function parseMessageIds(value: unknown): string[] {
14+
const ids = Array.isArray(value) ? value : String(value ?? '').split(',')
15+
return ids.map((id) => String(id).trim()).filter(Boolean)
16+
}
17+
918
export const discordBulkDeleteMessagesTool: ToolConfig<
1019
DiscordBulkDeleteMessagesParams,
1120
DiscordBulkDeleteMessagesResponse
@@ -54,10 +63,7 @@ export const discordBulkDeleteMessagesTool: ToolConfig<
5463
Authorization: `Bot ${params.botToken.trim()}`,
5564
}),
5665
body: (params: DiscordBulkDeleteMessagesParams) => {
57-
const ids = params.messageIds
58-
const messages = (Array.isArray(ids) ? ids : String(ids ?? '').split(','))
59-
.map((id) => String(id).trim())
60-
.filter(Boolean)
66+
const messages = parseMessageIds(params.messageIds)
6167
if (messages.length < 2 || messages.length > 100) {
6268
throw new Error(
6369
`Discord requires 2-100 message IDs for bulk delete, got ${messages.length}`

‎apps/sim/tools/discord/get_messages.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,8 @@ export const discordGetMessagesTool: ToolConfig<
6060
const limit = requested && Number.isFinite(requested) ? requested : 10
6161
const query = new URLSearchParams({ limit: String(Math.min(Math.max(1, limit), 100)) })
6262
for (const cursor of ['before', 'after', 'around'] as const) {
63-
const value = params[cursor]
64-
if (value !== undefined && value !== null && String(value).trim()) {
65-
query.set(cursor, String(value).trim())
66-
}
63+
const value = String(params[cursor] ?? '').trim()
64+
if (value) query.set(cursor, value)
6765
}
6866
return `https://discord.com/api/v10/channels/${safeUrlPathSegment(params.channelId, 'channelId')}/messages?${query.toString()}`
6967
},

0 commit comments

Comments
 (0)