Files
simstudioai--sim/apps/sim/tools/reddit/submit_post.ts
T
wehub-resource-sync d25d482dc2
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
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

196 lines
5.6 KiB
TypeScript

import type { RedditSubmitParams, RedditWriteResponse } from '@/tools/reddit/types'
import { normalizeSubreddit } from '@/tools/reddit/utils'
import type { ToolConfig } from '@/tools/types'
export const submitPostTool: ToolConfig<RedditSubmitParams, RedditWriteResponse> = {
id: 'reddit_submit_post',
name: 'Submit Reddit Post',
description: 'Submit a new post to a subreddit (text or link)',
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 post to (e.g., "technology", "programming")',
},
title: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description:
'Title of the submission (e.g., "Check out this new AI tool"). Max 300 characters',
},
text: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description:
'Text content for a self post in markdown format (e.g., "This is the **body** of my post")',
},
url: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'URL for a link post (cannot be used with text)',
},
nsfw: {
type: 'boolean',
required: false,
visibility: 'user-or-llm',
description: 'Mark post as NSFW',
},
spoiler: {
type: 'boolean',
required: false,
visibility: 'user-or-llm',
description: 'Mark post as spoiler',
},
send_replies: {
type: 'boolean',
required: false,
visibility: 'user-or-llm',
description: 'Send reply notifications to inbox (default: true)',
},
flair_id: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Flair template UUID for the post (max 36 characters)',
},
flair_text: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Flair text to display on the post (max 64 characters)',
},
collection_id: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Collection UUID to add the post to',
},
},
request: {
url: () => 'https://oauth.reddit.com/api/submit',
method: 'POST',
headers: (params: RedditSubmitParams) => {
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)',
'Content-Type': 'application/x-www-form-urlencoded',
}
},
body: (params: RedditSubmitParams) => {
const subreddit = normalizeSubreddit(params.subreddit)
const formData = new URLSearchParams({
sr: subreddit,
title: params.title,
api_type: 'json',
})
if (params.text) {
formData.append('kind', 'self')
formData.append('text', params.text)
} else if (params.url) {
formData.append('kind', 'link')
formData.append('url', params.url)
} else {
formData.append('kind', 'self')
formData.append('text', '')
}
if (params.nsfw !== undefined) formData.append('nsfw', params.nsfw.toString())
if (params.spoiler !== undefined) formData.append('spoiler', params.spoiler.toString())
if (params.flair_id) formData.append('flair_id', params.flair_id)
if (params.flair_text) formData.append('flair_text', params.flair_text)
if (params.collection_id) formData.append('collection_id', params.collection_id)
if (params.send_replies !== undefined)
formData.append('sendreplies', params.send_replies.toString())
return formData.toString()
},
},
transformResponse: async (response: Response) => {
const data = await response.json().catch(() => ({}) as any)
if (!response.ok) {
return {
success: false,
output: {
success: false,
message: `Failed to submit post: HTTP error ${response.status}`,
},
}
}
if (data.json?.errors && data.json.errors.length > 0) {
const errors = data.json.errors.map((err: any) => err.join(': ')).join(', ')
return {
success: false,
output: {
success: false,
message: `Failed to submit post: ${errors}`,
},
}
}
const postData = data.json?.data
return {
success: true,
output: {
success: true,
message: 'Post submitted successfully',
data: {
id: postData?.id,
name: postData?.name,
url: postData?.url,
permalink: postData?.permalink
? `https://www.reddit.com${postData.permalink}`
: (postData?.url ?? ''),
},
},
}
},
outputs: {
success: {
type: 'boolean',
description: 'Whether the post was submitted successfully',
},
message: {
type: 'string',
description: 'Success or error message',
},
data: {
type: 'object',
description: 'Post data including ID, name, URL, and permalink',
properties: {
id: { type: 'string', description: 'New post ID' },
name: { type: 'string', description: 'Thing fullname (t3_xxxxx)', optional: true },
url: { type: 'string', description: 'Post URL from API response' },
permalink: { type: 'string', description: 'Full Reddit permalink', optional: true },
},
},
},
}