Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
666 changes: 430 additions & 236 deletions apps/sim/blocks/blocks/reddit.ts

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions apps/sim/lib/workflows/migrations/subblock-migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ describe('migrateSubblockIds', () => {
const { blocks, migrated } = migrateSubblockIds(input)

expect(migrated).toBe(true)
expect(blocks['b1'].subBlocks['knowledgeBaseSelector']).toEqual({
expect(blocks.b1.subBlocks.knowledgeBaseSelector).toEqual({
id: 'knowledgeBaseSelector',
type: 'knowledge-base-selector',
value: 'kb-uuid-123',
})
expect(blocks['b1'].subBlocks['knowledgeBaseId']).toBeUndefined()
expect(blocks['b1'].subBlocks['operation'].value).toBe('search')
expect(blocks.b1.subBlocks.knowledgeBaseId).toBeUndefined()
expect(blocks.b1.subBlocks.operation.value).toBe('search')
})

it('should prefer new key when both old and new exist', () => {
Expand All @@ -68,8 +68,8 @@ describe('migrateSubblockIds', () => {
const { blocks, migrated } = migrateSubblockIds(input)

expect(migrated).toBe(true)
expect(blocks['b1'].subBlocks['knowledgeBaseSelector'].value).toBe('fresh-kb')
expect(blocks['b1'].subBlocks['knowledgeBaseId']).toBeUndefined()
expect(blocks.b1.subBlocks.knowledgeBaseSelector.value).toBe('fresh-kb')
expect(blocks.b1.subBlocks.knowledgeBaseId).toBeUndefined()
})

it('should not touch blocks that already use the new key', () => {
Expand All @@ -89,7 +89,7 @@ describe('migrateSubblockIds', () => {
const { blocks, migrated } = migrateSubblockIds(input)

expect(migrated).toBe(false)
expect(blocks['b1'].subBlocks['knowledgeBaseSelector'].value).toBe('kb-uuid')
expect(blocks.b1.subBlocks.knowledgeBaseSelector.value).toBe('kb-uuid')
})
})

Expand All @@ -109,8 +109,8 @@ describe('migrateSubblockIds', () => {

const { blocks } = migrateSubblockIds(input)

expect(input['b1'].subBlocks['knowledgeBaseId']).toBeDefined()
expect(blocks['b1'].subBlocks['knowledgeBaseSelector']).toBeDefined()
expect(input.b1.subBlocks.knowledgeBaseId).toBeDefined()
expect(blocks.b1.subBlocks.knowledgeBaseSelector).toBeDefined()
expect(blocks).not.toBe(input)
})

Expand All @@ -127,7 +127,7 @@ describe('migrateSubblockIds', () => {
const { blocks, migrated } = migrateSubblockIds(input)

expect(migrated).toBe(false)
expect(blocks['b1'].subBlocks['code'].value).toBe('console.log("hi")')
expect(blocks.b1.subBlocks.code.value).toBe('console.log("hi")')
})

it('should migrate multiple blocks in one pass', () => {
Expand Down Expand Up @@ -166,9 +166,9 @@ describe('migrateSubblockIds', () => {
const { blocks, migrated } = migrateSubblockIds(input)

expect(migrated).toBe(true)
expect(blocks['b1'].subBlocks['knowledgeBaseSelector'].value).toBe('kb-1')
expect(blocks['b2'].subBlocks['knowledgeBaseSelector'].value).toBe('kb-2')
expect(blocks['b3'].subBlocks['code']).toBeDefined()
expect(blocks.b1.subBlocks.knowledgeBaseSelector.value).toBe('kb-1')
expect(blocks.b2.subBlocks.knowledgeBaseSelector.value).toBe('kb-2')
expect(blocks.b3.subBlocks.code).toBeDefined()
})

it('should handle blocks with empty subBlocks', () => {
Expand Down
4 changes: 1 addition & 3 deletions apps/sim/tools/reddit/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ export const deleteTool: ToolConfig<RedditDeleteParams, RedditWriteResponse> = {
id: params.id,
})

return {
body: formData.toString(),
}
return formData.toString() as unknown as Record<string, any>
},
},

Expand Down
50 changes: 16 additions & 34 deletions apps/sim/tools/reddit/get_comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,6 @@ export const getCommentsTool: ToolConfig<RedditCommentsParams, RedditCommentsRes
visibility: 'user-or-llm',
description: 'Include "load more comments" elements in the response',
},
showtitle: {
type: 'boolean',
required: false,
visibility: 'user-or-llm',
description: 'Include submission title in the response',
},
threaded: {
type: 'boolean',
required: false,
Expand All @@ -87,23 +81,11 @@ export const getCommentsTool: ToolConfig<RedditCommentsParams, RedditCommentsRes
visibility: 'user-or-llm',
description: 'Integer to truncate comment depth',
},
after: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Fullname of a thing to fetch items after (for pagination)',
},
before: {
comment: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Fullname of a thing to fetch items before (for pagination)',
},
count: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'A count of items already seen in the listing (used for numbering)',
description: 'ID36 of a comment to focus on (returns that comment thread)',
},
},

Expand All @@ -126,15 +108,11 @@ export const getCommentsTool: ToolConfig<RedditCommentsParams, RedditCommentsRes
urlParams.append('context', Number(params.context).toString())
if (params.showedits !== undefined) urlParams.append('showedits', params.showedits.toString())
if (params.showmore !== undefined) urlParams.append('showmore', params.showmore.toString())
if (params.showtitle !== undefined) urlParams.append('showtitle', params.showtitle.toString())
if (params.threaded !== undefined) urlParams.append('threaded', params.threaded.toString())
if (params.truncate !== undefined)
urlParams.append('truncate', Number(params.truncate).toString())

// Add pagination parameters if provided
if (params.after) urlParams.append('after', params.after)
if (params.before) urlParams.append('before', params.before)
if (params.count !== undefined) urlParams.append('count', Number(params.count).toString())
if (params.comment) urlParams.append('comment', params.comment)

// Build URL using OAuth endpoint
return `https://oauth.reddit.com/r/${subreddit}/comments/${params.postId}?${urlParams.toString()}`
Expand Down Expand Up @@ -179,11 +157,12 @@ export const getCommentsTool: ToolConfig<RedditCommentsParams, RedditCommentsRes
: []

return {
id: commentData.id || '',
id: commentData.id ?? '',
name: commentData.name ?? '',
author: commentData.author || '[deleted]',
body: commentData.body || '',
created_utc: commentData.created_utc || 0,
score: commentData.score || 0,
body: commentData.body ?? '',
created_utc: commentData.created_utc ?? 0,
score: commentData.score ?? 0,
permalink: commentData.permalink
? `https://www.reddit.com${commentData.permalink}`
: '',
Expand All @@ -199,12 +178,13 @@ export const getCommentsTool: ToolConfig<RedditCommentsParams, RedditCommentsRes
success: true,
output: {
post: {
id: postData.id || '',
title: postData.title || '',
id: postData.id ?? '',
name: postData.name ?? '',
title: postData.title ?? '',
author: postData.author || '[deleted]',
selftext: postData.selftext || '',
created_utc: postData.created_utc || 0,
score: postData.score || 0,
selftext: postData.selftext ?? '',
created_utc: postData.created_utc ?? 0,
score: postData.score ?? 0,
permalink: postData.permalink ? `https://www.reddit.com${postData.permalink}` : '',
},
comments: comments,
Expand All @@ -218,6 +198,7 @@ export const getCommentsTool: ToolConfig<RedditCommentsParams, RedditCommentsRes
description: 'Post information including ID, title, author, content, and metadata',
properties: {
id: { type: 'string', description: 'Post ID' },
name: { type: 'string', description: 'Thing fullname (t3_xxxxx)' },
title: { type: 'string', description: 'Post title' },
author: { type: 'string', description: 'Post author' },
selftext: { type: 'string', description: 'Post text content' },
Expand All @@ -233,6 +214,7 @@ export const getCommentsTool: ToolConfig<RedditCommentsParams, RedditCommentsRes
type: 'object',
properties: {
id: { type: 'string', description: 'Comment ID' },
name: { type: 'string', description: 'Thing fullname (t1_xxxxx)' },
author: { type: 'string', description: 'Comment author' },
body: { type: 'string', description: 'Comment text' },
score: { type: 'number', description: 'Comment score' },
Expand Down
34 changes: 24 additions & 10 deletions apps/sim/tools/reddit/get_controversial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const getControversialTool: ToolConfig<RedditControversialParams, RedditP
request: {
url: (params: RedditControversialParams) => {
const subreddit = normalizeSubreddit(params.subreddit)
const limit = Math.min(Math.max(1, params.limit || 10), 100)
const limit = Math.min(Math.max(1, params.limit ?? 10), 100)

// Build URL with appropriate parameters using OAuth endpoint
const urlParams = new URLSearchParams({
Expand Down Expand Up @@ -122,18 +122,19 @@ export const getControversialTool: ToolConfig<RedditControversialParams, RedditP
data.data?.children?.map((child: any) => {
const post = child.data || {}
return {
id: post.id || '',
title: post.title || '',
id: post.id ?? '',
name: post.name ?? '',
title: post.title ?? '',
author: post.author || '[deleted]',
url: post.url || '',
url: post.url ?? '',
permalink: post.permalink ? `https://www.reddit.com${post.permalink}` : '',
created_utc: post.created_utc || 0,
score: post.score || 0,
num_comments: post.num_comments || 0,
created_utc: post.created_utc ?? 0,
score: post.score ?? 0,
num_comments: post.num_comments ?? 0,
is_self: !!post.is_self,
selftext: post.selftext || '',
thumbnail: post.thumbnail || '',
subreddit: post.subreddit || subredditName,
selftext: post.selftext ?? '',
thumbnail: post.thumbnail ?? '',
subreddit: post.subreddit ?? subredditName,
}
}) || []

Expand All @@ -142,6 +143,8 @@ export const getControversialTool: ToolConfig<RedditControversialParams, RedditP
output: {
subreddit: subredditName,
posts,
after: data.data?.after ?? null,
before: data.data?.before ?? null,
},
}
},
Expand All @@ -159,6 +162,7 @@ export const getControversialTool: ToolConfig<RedditControversialParams, RedditP
type: 'object',
properties: {
id: { type: 'string', description: 'Post ID' },
name: { type: 'string', description: 'Thing fullname (t3_xxxxx)' },
title: { type: 'string', description: 'Post title' },
author: { type: 'string', description: 'Author username' },
url: { type: 'string', description: 'Post URL' },
Expand All @@ -173,5 +177,15 @@ export const getControversialTool: ToolConfig<RedditControversialParams, RedditP
},
},
},
after: {
type: 'string',
description: 'Fullname of the last item for forward pagination',
optional: true,
},
before: {
type: 'string',
description: 'Fullname of the first item for backward pagination',
optional: true,
},
},
}
72 changes: 72 additions & 0 deletions apps/sim/tools/reddit/get_me.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import type { RedditGetMeParams, RedditUserResponse } from '@/tools/reddit/types'
import type { ToolConfig } from '@/tools/types'

export const getMeTool: ToolConfig<RedditGetMeParams, RedditUserResponse> = {
id: 'reddit_get_me',
name: 'Get Reddit User Identity',
description: 'Get information about the authenticated Reddit user',
version: '1.0.0',

oauth: {
required: true,
provider: 'reddit',
},

params: {
accessToken: {
type: 'string',
required: true,
visibility: 'hidden',
description: 'Access token for Reddit API',
},
},

request: {
url: () => 'https://oauth.reddit.com/api/v1/me?raw_json=1',
method: 'GET',
headers: (params: RedditGetMeParams) => {
if (!params.accessToken) {
throw new Error('Access token is required for Reddit API')
}

return {
Authorization: `Bearer ${params.accessToken}`,
'User-Agent': 'sim-studio/1.0 (https://github.com/simstudioai/sim)',
Accept: 'application/json',
}
},
},

transformResponse: async (response: Response) => {
const data = await response.json()

return {
success: true,
output: {
id: data.id ?? '',
name: data.name ?? '',
created_utc: data.created_utc ?? 0,
link_karma: data.link_karma ?? 0,
comment_karma: data.comment_karma ?? 0,
total_karma: data.total_karma ?? 0,
is_gold: data.is_gold ?? false,
is_mod: data.is_mod ?? false,
has_verified_email: data.has_verified_email ?? false,
icon_img: data.icon_img ?? '',
},
}
Comment thread
waleedlatif1 marked this conversation as resolved.
},

outputs: {
id: { type: 'string', description: 'User ID' },
name: { type: 'string', description: 'Username' },
created_utc: { type: 'number', description: 'Account creation time in UTC epoch seconds' },
link_karma: { type: 'number', description: 'Total link karma' },
comment_karma: { type: 'number', description: 'Total comment karma' },
total_karma: { type: 'number', description: 'Combined total karma' },
is_gold: { type: 'boolean', description: 'Whether user has Reddit Premium' },
is_mod: { type: 'boolean', description: 'Whether user is a moderator' },
has_verified_email: { type: 'boolean', description: 'Whether email is verified' },
icon_img: { type: 'string', description: 'User avatar/icon URL' },
},
}
Loading