Files
simstudioai--sim/apps/sim/tools/reddit/hot_posts.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

143 lines
4.5 KiB
TypeScript

import type { RedditHotPostsResponse, RedditPost } from '@/tools/reddit/types'
import { normalizeSubreddit } from '@/tools/reddit/utils'
import type { ToolConfig } from '@/tools/types'
interface HotPostsParams {
subreddit: string
limit?: number
accessToken: string
}
export const hotPostsTool: ToolConfig<HotPostsParams, RedditHotPostsResponse> = {
id: 'reddit_hot_posts',
name: 'Reddit Hot Posts',
description: 'Fetch the most popular (hot) posts from a specified subreddit.',
version: '1.0.0',
oauth: {
required: true,
provider: 'reddit',
},
params: {
accessToken: {
type: 'string',
required: true,
visibility: 'hidden',
description: 'Access token for Reddit API',
},
subreddit: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'The subreddit to fetch hot posts from (e.g., "technology", "news")',
},
limit: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'Maximum number of posts to return (e.g., 25). Default: 10, max: 100',
},
},
request: {
url: (params) => {
const subreddit = normalizeSubreddit(params.subreddit)
const limit = Math.min(Math.max(1, params.limit ?? 10), 100)
return `https://oauth.reddit.com/r/${subreddit}/hot?limit=${limit}&raw_json=1`
},
method: 'GET',
headers: (params: HotPostsParams) => {
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, requestParams?: HotPostsParams) => {
const data = await response.json()
const posts: RedditPost[] =
data.data?.children?.map((child: any) => {
const post = child.data || {}
return {
id: post.id ?? '',
name: post.name ?? '',
title: post.title ?? '',
author: post.author || '[deleted]',
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,
selftext: post.selftext ?? '',
thumbnail:
post.thumbnail !== 'self' && post.thumbnail !== 'default' ? post.thumbnail : undefined,
is_self: !!post.is_self,
subreddit: post.subreddit ?? requestParams?.subreddit ?? '',
}
}) || []
const subreddit =
data.data?.children?.[0]?.data?.subreddit ||
(posts.length > 0 ? posts[0].subreddit : requestParams?.subreddit || '')
return {
success: true,
output: {
subreddit,
posts,
after: data.data?.after ?? null,
before: data.data?.before ?? null,
},
}
},
outputs: {
subreddit: {
type: 'string',
description: 'Name of the subreddit where hot posts were fetched from',
},
posts: {
type: 'array',
description:
'Array of hot posts with title, author, URL, score, comments count, and metadata',
items: {
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' },
permalink: { type: 'string', description: 'Reddit permalink' },
score: { type: 'number', description: 'Post score (upvotes - downvotes)' },
num_comments: { type: 'number', description: 'Number of comments' },
created_utc: { type: 'number', description: 'Creation timestamp (UTC)' },
is_self: { type: 'boolean', description: 'Whether this is a text post' },
selftext: { type: 'string', description: 'Text content for self posts' },
thumbnail: { type: 'string', description: 'Thumbnail URL' },
subreddit: { type: 'string', description: 'Subreddit name' },
},
},
},
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,
},
},
}