Skip to content

Commit 84f806c

Browse files
committed
fix(reddit): add optional chaining, pagination wiring, and trim safety
- Add optional chaining on children?.[0] in get_posts, get_controversial, search, and get_comments to prevent TypeError on unexpected API responses - Wire after/before pagination params to get_messages block operation - Use ?? instead of || for get_comments limit to handle 0 correctly - Add .trim() on postId in get_comments URL path
1 parent ecd3130 commit 84f806c

File tree

6 files changed

+181
-15
lines changed

6 files changed

+181
-15
lines changed

apps/docs/content/docs/en/tools/reddit.mdx

Lines changed: 171 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ These operations let your agents access and analyze Reddit content as part of yo
2424

2525
## Usage Instructions
2626

27-
Integrate Reddit into workflows. Read posts, comments, and search content. Submit posts, vote, reply, edit, and manage your Reddit account.
27+
Integrate Reddit into workflows. Read posts, comments, and search content. Submit posts, vote, reply, edit, manage messages, and access user and subreddit info.
2828

2929

3030

@@ -39,14 +39,15 @@ Fetch posts from a subreddit with different sorting options
3939
| Parameter | Type | Required | Description |
4040
| --------- | ---- | -------- | ----------- |
4141
| `subreddit` | string | Yes | The subreddit to fetch posts from \(e.g., "technology", "news"\) |
42-
| `sort` | string | No | Sort method for posts \(e.g., "hot", "new", "top", "rising"\). Default: "hot" |
42+
| `sort` | string | No | Sort method for posts \(e.g., "hot", "new", "top", "rising", "controversial"\). Default: "hot" |
4343
| `limit` | number | No | Maximum number of posts to return \(e.g., 25\). Default: 10, max: 100 |
44-
| `time` | string | No | Time filter for "top" sorted posts: "day", "week", "month", "year", or "all" \(default: "day"\) |
44+
| `time` | string | No | Time filter for "top" sorted posts: "day", "week", "month", "year", or "all" \(default: "all"\) |
4545
| `after` | string | No | Fullname of a thing to fetch items after \(for pagination\) |
4646
| `before` | string | No | Fullname of a thing to fetch items before \(for pagination\) |
4747
| `count` | number | No | A count of items already seen in the listing \(used for numbering\) |
4848
| `show` | string | No | Show items that would normally be filtered \(e.g., "all"\) |
4949
| `sr_detail` | boolean | No | Expand subreddit details in the response |
50+
| `g` | string | No | Geo filter for posts \(e.g., "GLOBAL", "US", "AR", etc.\) |
5051

5152
#### Output
5253

@@ -55,6 +56,7 @@ Fetch posts from a subreddit with different sorting options
5556
| `subreddit` | string | Name of the subreddit where posts were fetched from |
5657
| `posts` | array | Array of posts with title, author, URL, score, comments count, and metadata |
5758
|`id` | string | Post ID |
59+
|`name` | string | Thing fullname \(t3_xxxxx\) |
5860
|`title` | string | Post title |
5961
|`author` | string | Author username |
6062
|`url` | string | Post URL |
@@ -66,6 +68,8 @@ Fetch posts from a subreddit with different sorting options
6668
|`selftext` | string | Text content for self posts |
6769
|`thumbnail` | string | Thumbnail URL |
6870
|`subreddit` | string | Subreddit name |
71+
| `after` | string | Fullname of the last item for forward pagination |
72+
| `before` | string | Fullname of the first item for backward pagination |
6973

7074
### `reddit_get_comments`
7175

@@ -83,19 +87,17 @@ Fetch comments from a specific Reddit post
8387
| `context` | number | No | Number of parent comments to include |
8488
| `showedits` | boolean | No | Show edit information for comments |
8589
| `showmore` | boolean | No | Include "load more comments" elements in the response |
86-
| `showtitle` | boolean | No | Include submission title in the response |
8790
| `threaded` | boolean | No | Return comments in threaded/nested format |
8891
| `truncate` | number | No | Integer to truncate comment depth |
89-
| `after` | string | No | Fullname of a thing to fetch items after \(for pagination\) |
90-
| `before` | string | No | Fullname of a thing to fetch items before \(for pagination\) |
91-
| `count` | number | No | A count of items already seen in the listing \(used for numbering\) |
92+
| `comment` | string | No | ID36 of a comment to focus on \(returns that comment thread\) |
9293

9394
#### Output
9495

9596
| Parameter | Type | Description |
9697
| --------- | ---- | ----------- |
9798
| `post` | object | Post information including ID, title, author, content, and metadata |
9899
|`id` | string | Post ID |
100+
|`name` | string | Thing fullname \(t3_xxxxx\) |
99101
|`title` | string | Post title |
100102
|`author` | string | Post author |
101103
|`selftext` | string | Post text content |
@@ -104,6 +106,7 @@ Fetch comments from a specific Reddit post
104106
|`permalink` | string | Reddit permalink |
105107
| `comments` | array | Nested comments with author, body, score, timestamps, and replies |
106108
|`id` | string | Comment ID |
109+
|`name` | string | Thing fullname \(t1_xxxxx\) |
107110
|`author` | string | Comment author |
108111
|`body` | string | Comment text |
109112
|`score` | number | Comment score |
@@ -135,6 +138,7 @@ Fetch controversial posts from a subreddit
135138
| `subreddit` | string | Name of the subreddit where posts were fetched from |
136139
| `posts` | array | Array of controversial posts with title, author, URL, score, comments count, and metadata |
137140
|`id` | string | Post ID |
141+
|`name` | string | Thing fullname \(t3_xxxxx\) |
138142
|`title` | string | Post title |
139143
|`author` | string | Author username |
140144
|`url` | string | Post URL |
@@ -146,6 +150,8 @@ Fetch controversial posts from a subreddit
146150
|`selftext` | string | Text content for self posts |
147151
|`thumbnail` | string | Thumbnail URL |
148152
|`subreddit` | string | Subreddit name |
153+
| `after` | string | Fullname of the last item for forward pagination |
154+
| `before` | string | Fullname of the first item for backward pagination |
149155

150156
### `reddit_search`
151157

@@ -165,6 +171,8 @@ Search for posts within a subreddit
165171
| `before` | string | No | Fullname of a thing to fetch items before \(for pagination\) |
166172
| `count` | number | No | A count of items already seen in the listing \(used for numbering\) |
167173
| `show` | string | No | Show items that would normally be filtered \(e.g., "all"\) |
174+
| `type` | string | No | Type of search results: "link" \(posts\), "sr" \(subreddits\), or "user" \(users\). Default: "link" |
175+
| `sr_detail` | boolean | No | Expand subreddit details in the response |
168176

169177
#### Output
170178

@@ -173,6 +181,7 @@ Search for posts within a subreddit
173181
| `subreddit` | string | Name of the subreddit where search was performed |
174182
| `posts` | array | Array of search result posts with title, author, URL, score, comments count, and metadata |
175183
|`id` | string | Post ID |
184+
|`name` | string | Thing fullname \(t3_xxxxx\) |
176185
|`title` | string | Post title |
177186
|`author` | string | Author username |
178187
|`url` | string | Post URL |
@@ -184,6 +193,8 @@ Search for posts within a subreddit
184193
|`selftext` | string | Text content for self posts |
185194
|`thumbnail` | string | Thumbnail URL |
186195
|`subreddit` | string | Subreddit name |
196+
| `after` | string | Fullname of the last item for forward pagination |
197+
| `before` | string | Fullname of the first item for backward pagination |
187198

188199
### `reddit_submit_post`
189200

@@ -200,6 +211,9 @@ Submit a new post to a subreddit (text or link)
200211
| `nsfw` | boolean | No | Mark post as NSFW |
201212
| `spoiler` | boolean | No | Mark post as spoiler |
202213
| `send_replies` | boolean | No | Send reply notifications to inbox \(default: true\) |
214+
| `flair_id` | string | No | Flair template UUID for the post \(max 36 characters\) |
215+
| `flair_text` | string | No | Flair text to display on the post \(max 64 characters\) |
216+
| `collection_id` | string | No | Collection UUID to add the post to |
203217

204218
#### Output
205219

@@ -264,6 +278,21 @@ Save a Reddit post or comment to your saved items
264278
| `posts` | json | Posts data |
265279
| `post` | json | Single post data |
266280
| `comments` | json | Comments data |
281+
| `success` | boolean | Operation success status |
282+
| `message` | string | Result message |
283+
| `data` | json | Response data |
284+
| `after` | string | Pagination cursor \(next page\) |
285+
| `before` | string | Pagination cursor \(previous page\) |
286+
| `id` | string | Entity ID |
287+
| `name` | string | Entity fullname |
288+
| `messages` | json | Messages data |
289+
| `display_name` | string | Subreddit display name |
290+
| `subscribers` | number | Subscriber count |
291+
| `description` | string | Description text |
292+
| `link_karma` | number | Link karma |
293+
| `comment_karma` | number | Comment karma |
294+
| `total_karma` | number | Total karma |
295+
| `icon_img` | string | Icon image URL |
267296

268297
### `reddit_reply`
269298

@@ -275,6 +304,7 @@ Add a comment reply to a Reddit post or comment
275304
| --------- | ---- | -------- | ----------- |
276305
| `parent_id` | string | Yes | Thing fullname to reply to \(e.g., "t3_abc123" for post, "t1_def456" for comment\) |
277306
| `text` | string | Yes | Comment text in markdown format \(e.g., "Great post! Here is my **reply**"\) |
307+
| `return_rtjson` | boolean | No | Return response in Rich Text JSON format |
278308

279309
#### Output
280310

@@ -345,4 +375,138 @@ Subscribe or unsubscribe from a subreddit
345375
| `success` | boolean | Whether the subscription action was successful |
346376
| `message` | string | Success or error message |
347377

378+
### `reddit_get_me`
379+
380+
Get information about the authenticated Reddit user
381+
382+
#### Input
383+
384+
| Parameter | Type | Required | Description |
385+
| --------- | ---- | -------- | ----------- |
386+
387+
#### Output
388+
389+
| Parameter | Type | Description |
390+
| --------- | ---- | ----------- |
391+
| `id` | string | User ID |
392+
| `name` | string | Username |
393+
| `created_utc` | number | Account creation time in UTC epoch seconds |
394+
| `link_karma` | number | Total link karma |
395+
| `comment_karma` | number | Total comment karma |
396+
| `total_karma` | number | Combined total karma |
397+
| `is_gold` | boolean | Whether user has Reddit Premium |
398+
| `is_mod` | boolean | Whether user is a moderator |
399+
| `has_verified_email` | boolean | Whether email is verified |
400+
| `icon_img` | string | User avatar/icon URL |
401+
402+
### `reddit_get_user`
403+
404+
Get public profile information about any Reddit user by username
405+
406+
#### Input
407+
408+
| Parameter | Type | Required | Description |
409+
| --------- | ---- | -------- | ----------- |
410+
| `username` | string | Yes | Reddit username to look up \(e.g., "spez", "example_user"\) |
411+
412+
#### Output
413+
414+
| Parameter | Type | Description |
415+
| --------- | ---- | ----------- |
416+
| `id` | string | User ID |
417+
| `name` | string | Username |
418+
| `created_utc` | number | Account creation time in UTC epoch seconds |
419+
| `link_karma` | number | Total link karma |
420+
| `comment_karma` | number | Total comment karma |
421+
| `total_karma` | number | Combined total karma |
422+
| `is_gold` | boolean | Whether user has Reddit Premium |
423+
| `is_mod` | boolean | Whether user is a moderator |
424+
| `has_verified_email` | boolean | Whether email is verified |
425+
| `icon_img` | string | User avatar/icon URL |
426+
427+
### `reddit_send_message`
428+
429+
Send a private message to a Reddit user
430+
431+
#### Input
432+
433+
| Parameter | Type | Required | Description |
434+
| --------- | ---- | -------- | ----------- |
435+
| `to` | string | Yes | Recipient username \(e.g., "example_user"\) or subreddit \(e.g., "/r/subreddit"\) |
436+
| `subject` | string | Yes | Message subject \(max 100 characters\) |
437+
| `text` | string | Yes | Message body in markdown format |
438+
| `from_sr` | string | No | Subreddit name to send the message from \(requires moderator mail permission\) |
439+
440+
#### Output
441+
442+
| Parameter | Type | Description |
443+
| --------- | ---- | ----------- |
444+
| `success` | boolean | Whether the message was sent successfully |
445+
| `message` | string | Success or error message |
446+
447+
### `reddit_get_messages`
448+
449+
Retrieve private messages from your Reddit inbox
450+
451+
#### Input
452+
453+
| Parameter | Type | Required | Description |
454+
| --------- | ---- | -------- | ----------- |
455+
| `where` | string | No | Message folder to retrieve: "inbox" \(all\), "unread", "sent", "messages" \(direct messages only\), "comments" \(comment replies\), "selfreply" \(self-post replies\), or "mentions" \(username mentions\). Default: "inbox" |
456+
| `limit` | number | No | Maximum number of messages to return \(e.g., 25\). Default: 25, max: 100 |
457+
| `after` | string | No | Fullname of a thing to fetch items after \(for pagination\) |
458+
| `before` | string | No | Fullname of a thing to fetch items before \(for pagination\) |
459+
| `mark` | boolean | No | Whether to mark fetched messages as read |
460+
| `count` | number | No | A count of items already seen in the listing \(used for numbering\) |
461+
| `show` | string | No | Show items that would normally be filtered \(e.g., "all"\) |
462+
463+
#### Output
464+
465+
| Parameter | Type | Description |
466+
| --------- | ---- | ----------- |
467+
| `messages` | array | Array of messages with sender, recipient, subject, body, and metadata |
468+
|`id` | string | Message ID |
469+
|`name` | string | Thing fullname \(t4_xxxxx\) |
470+
|`author` | string | Sender username |
471+
|`dest` | string | Recipient username |
472+
|`subject` | string | Message subject |
473+
|`body` | string | Message body text |
474+
|`created_utc` | number | Creation time in UTC epoch seconds |
475+
|`new` | boolean | Whether the message is unread |
476+
|`was_comment` | boolean | Whether the message is a comment reply |
477+
|`context` | string | Context URL for comment replies |
478+
|`distinguished` | string | Distinction: null/"moderator"/"admin" |
479+
| `after` | string | Fullname of the last item for forward pagination |
480+
| `before` | string | Fullname of the first item for backward pagination |
481+
482+
### `reddit_get_subreddit_info`
483+
484+
Get metadata and information about a subreddit
485+
486+
#### Input
487+
488+
| Parameter | Type | Required | Description |
489+
| --------- | ---- | -------- | ----------- |
490+
| `subreddit` | string | Yes | The subreddit to get info about \(e.g., "technology", "programming", "news"\) |
491+
492+
#### Output
493+
494+
| Parameter | Type | Description |
495+
| --------- | ---- | ----------- |
496+
| `id` | string | Subreddit ID |
497+
| `name` | string | Subreddit fullname \(t5_xxxxx\) |
498+
| `display_name` | string | Subreddit name without prefix |
499+
| `title` | string | Subreddit title |
500+
| `description` | string | Full subreddit description \(markdown\) |
501+
| `public_description` | string | Short public description |
502+
| `subscribers` | number | Number of subscribers |
503+
| `accounts_active` | number | Number of currently active users |
504+
| `created_utc` | number | Creation time in UTC epoch seconds |
505+
| `over18` | boolean | Whether the subreddit is NSFW |
506+
| `lang` | string | Primary language of the subreddit |
507+
| `subreddit_type` | string | Subreddit type: public, private, restricted, etc. |
508+
| `url` | string | Subreddit URL path \(e.g., /r/technology/\) |
509+
| `icon_img` | string | Subreddit icon URL |
510+
| `banner_img` | string | Subreddit banner URL |
511+
348512

apps/sim/blocks/blocks/reddit.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export const RedditBlock: BlockConfig<RedditResponse> = {
138138
placeholder: 'Fullname for forward pagination (e.g., t3_xxxxx)',
139139
condition: {
140140
field: 'operation',
141-
value: ['get_posts', 'get_controversial', 'search'],
141+
value: ['get_posts', 'get_controversial', 'search', 'get_messages'],
142142
},
143143
mode: 'advanced',
144144
},
@@ -149,7 +149,7 @@ export const RedditBlock: BlockConfig<RedditResponse> = {
149149
placeholder: 'Fullname for backward pagination (e.g., t3_xxxxx)',
150150
condition: {
151151
field: 'operation',
152-
value: ['get_posts', 'get_controversial', 'search'],
152+
value: ['get_posts', 'get_controversial', 'search', 'get_messages'],
153153
},
154154
mode: 'advanced',
155155
},
@@ -818,6 +818,8 @@ Return ONLY the message content - no meta-commentary.`,
818818
where: inputs.messageWhere,
819819
limit: inputs.messageLimit ? Number.parseInt(inputs.messageLimit) : undefined,
820820
mark: inputs.messageMark !== undefined ? inputs.messageMark === 'true' : undefined,
821+
after: inputs.after || undefined,
822+
before: inputs.before || undefined,
821823
oauthCredential,
822824
}
823825
}

apps/sim/tools/reddit/get_comments.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export const getCommentsTool: ToolConfig<RedditCommentsParams, RedditCommentsRes
9393
url: (params: RedditCommentsParams) => {
9494
const subreddit = normalizeSubreddit(params.subreddit)
9595
const sort = params.sort || 'confidence'
96-
const limit = Math.min(Math.max(1, params.limit || 50), 100)
96+
const limit = Math.min(Math.max(1, params.limit ?? 50), 100)
9797

9898
// Build URL with query parameters
9999
const urlParams = new URLSearchParams({
@@ -115,7 +115,7 @@ export const getCommentsTool: ToolConfig<RedditCommentsParams, RedditCommentsRes
115115
if (params.comment) urlParams.append('comment', params.comment)
116116

117117
// Build URL using OAuth endpoint
118-
return `https://oauth.reddit.com/r/${subreddit}/comments/${params.postId}?${urlParams.toString()}`
118+
return `https://oauth.reddit.com/r/${subreddit}/comments/${params.postId.trim()}?${urlParams.toString()}`
119119
},
120120
method: 'GET',
121121
headers: (params: RedditCommentsParams) => {
@@ -135,7 +135,7 @@ export const getCommentsTool: ToolConfig<RedditCommentsParams, RedditCommentsRes
135135
const data = await response.json()
136136

137137
// Extract post data (first element in the array)
138-
const postData = data[0]?.data?.children[0]?.data || {}
138+
const postData = data[0]?.data?.children?.[0]?.data || {}
139139

140140
// Extract and transform comments (second element in the array)
141141
const commentsData = data[1]?.data?.children || []

apps/sim/tools/reddit/get_controversial.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export const getControversialTool: ToolConfig<RedditControversialParams, RedditP
115115

116116
// Extract subreddit name from response (with fallback)
117117
const subredditName =
118-
data.data?.children[0]?.data?.subreddit || requestParams?.subreddit || 'unknown'
118+
data.data?.children?.[0]?.data?.subreddit || requestParams?.subreddit || 'unknown'
119119

120120
// Transform posts data
121121
const posts =

apps/sim/tools/reddit/get_posts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export const getPostsTool: ToolConfig<RedditPostsParams, RedditPostsResponse> =
139139

140140
// Extract subreddit name from response (with fallback)
141141
const subredditName =
142-
data.data?.children[0]?.data?.subreddit || requestParams?.subreddit || 'unknown'
142+
data.data?.children?.[0]?.data?.subreddit || requestParams?.subreddit || 'unknown'
143143

144144
// Transform posts data
145145
const posts =

apps/sim/tools/reddit/search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export const searchTool: ToolConfig<RedditSearchParams, RedditPostsResponse> = {
148148

149149
// Extract subreddit name from response (with fallback)
150150
const subredditName =
151-
data.data?.children[0]?.data?.subreddit || requestParams?.subreddit || 'unknown'
151+
data.data?.children?.[0]?.data?.subreddit || requestParams?.subreddit || 'unknown'
152152

153153
// Transform posts data
154154
const posts =

0 commit comments

Comments
 (0)