Skip to content

Commit 47e5aed

Browse files
bidahclaude
andcommitted
✨ feat(x-bot): fetch parent tweet context when mention is a reply
When someone replies to a tweet mentioning @rnvibecode, the bot now fetches the parent tweet's text and images to use as context for classification and app generation. Parent images are merged first, reply images included only if same author as parent tweet. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent acd8bb3 commit 47e5aed

4 files changed

Lines changed: 123 additions & 14 deletions

File tree

‎apps/web/app/(app)/api/x-bot/poll/route.ts‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ export async function GET(request: NextRequest) {
5252

5353
// Fetch mentions using X API v2
5454
const params: Record<string, any> = {
55-
'tweet.fields': ['author_id', 'created_at', 'text', 'attachments'],
55+
'tweet.fields': ['author_id', 'created_at', 'text', 'attachments', 'referenced_tweets'],
5656
'media.fields': ['url', 'type', 'media_key', 'preview_image_url'],
57-
expansions: ['attachments.media_keys', 'author_id'],
57+
expansions: ['attachments.media_keys', 'author_id', 'referenced_tweets.id'],
5858
'user.fields': ['id', 'username'],
5959
max_results: 20,
6060
}
@@ -98,10 +98,54 @@ export async function GET(request: NextRequest) {
9898
}
9999
}
100100

101+
// Check if this mention is a reply to another tweet
102+
let parentTweetText: string | undefined
103+
let parentMediaUrls: string[] | undefined
104+
let parentAuthorId: string | undefined
105+
106+
const repliedToRef = (tweet as any).referenced_tweets?.find(
107+
(ref: any) => ref.type === 'replied_to'
108+
)
109+
110+
if (repliedToRef) {
111+
console.log(`[X-Bot Poll] Mention ${tweetId} is a reply to ${repliedToRef.id}, fetching parent tweet...`)
112+
try {
113+
const parentTweet = await client.tweets.findTweetById(repliedToRef.id, {
114+
'tweet.fields': ['author_id', 'text', 'attachments'],
115+
'media.fields': ['url', 'type', 'media_key', 'preview_image_url'],
116+
expansions: ['attachments.media_keys'],
117+
})
118+
119+
if (parentTweet.data) {
120+
parentTweetText = parentTweet.data.text || undefined
121+
parentAuthorId = parentTweet.data.author_id || undefined
122+
123+
// Extract parent tweet media
124+
const parentMediaKeys = parentTweet.data.attachments?.media_keys || []
125+
const parentMediaIncludes = parentTweet.includes?.media || []
126+
if (parentMediaKeys.length > 0) {
127+
parentMediaUrls = []
128+
for (const key of parentMediaKeys) {
129+
const media = parentMediaIncludes.find((m: any) => m.media_key === key)
130+
if (media && media.type === 'photo' && media.url) {
131+
parentMediaUrls.push(media.url)
132+
}
133+
}
134+
}
135+
136+
console.log(`[X-Bot Poll] Parent tweet text: "${parentTweetText?.substring(0, 50)}..."`)
137+
console.log(`[X-Bot Poll] Parent tweet images: ${parentMediaUrls?.length || 0}`)
138+
}
139+
} catch (error) {
140+
console.error(`[X-Bot Poll] Failed to fetch parent tweet ${repliedToRef.id}:`, error)
141+
// Continue without parent context — non-blocking
142+
}
143+
}
144+
101145
console.log(`[X-Bot Poll] Processing mention ${tweetId}: "${text.substring(0, 50)}..."`)
102146

103147
const result = await processMention(
104-
{ tweetId, text, authorId, mediaUrls },
148+
{ tweetId, text, authorId, mediaUrls, parentTweetText, parentMediaUrls, parentAuthorId },
105149
client,
106150
baseUrl
107151
)

‎apps/web/lib/x-bot/classify-tweet.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,26 @@ export type TweetClassification = z.infer<typeof TweetClassificationSchema>
3636
export async function classifyTweet(
3737
tweetText: string,
3838
hasImages: boolean,
39-
authorUsername?: string
39+
authorUsername?: string,
40+
parentTweetText?: string
4041
): Promise<TweetClassification> {
4142
try {
4243
const anthropic = createAnthropic({
4344
apiKey: process.env.ANTHROPIC_API_KEY,
4445
})
4546

47+
const parentContext = parentTweetText
48+
? `\nThis is a REPLY to another tweet. The parent tweet says: "${parentTweetText}"\nThe user is replying to that tweet and mentioning the bot. Consider the parent tweet as context for what the user wants built.`
49+
: ''
50+
4651
const result = await generateObject({
4752
model: anthropic('claude-3-5-haiku-20241022'),
4853
schema: TweetClassificationSchema,
4954
prompt: `Analyze this tweet and determine if it's requesting to build/create a mobile app.
5055
5156
Tweet: "${tweetText}"
5257
Has images attached: ${hasImages}
53-
${authorUsername ? `Author: @${authorUsername}` : ''}
58+
${authorUsername ? `Author: @${authorUsername}` : ''}${parentContext}
5459
5560
Classification rules:
5661
1. An APP REQUEST typically:
@@ -59,6 +64,7 @@ Classification rules:
5964
- Asks for help building something
6065
- Includes a design mockup or screenshot with the intent to create it
6166
- Requests a specific type of application (e.g., "build me a todo app")
67+
- Is a reply to a tweet containing a design/mockup/app idea, asking the bot to create it (e.g., "create this", "build this app", "make this")
6268
6369
2. A REGULAR MENTION (not an app request) typically:
6470
- Is just saying hello or asking a general question
@@ -67,7 +73,7 @@ Classification rules:
6773
- Is thanking or commenting without an app request
6874
- Is spam or unrelated content
6975
70-
If this IS an app request, extract a clean app description that can be used as a prompt for the AI to build the app. Focus on what the user wants to build, not the conversational parts of the tweet.
76+
If this IS an app request, extract a clean app description that can be used as a prompt for the AI to build the app. Focus on what the user wants to build, not the conversational parts of the tweet.${parentTweetText ? ' Include relevant details from the parent tweet in the app description.' : ''}
7177
7278
Be strict - only classify as an app request if there's clear intent to build something.`,
7379
maxTokens: 500,

‎apps/web/lib/x-bot/process-mention.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,33 @@ async function replyWithLinkPrompt(
166166
* link check → classification → credit check → first reply → project creation → generation trigger
167167
*/
168168
export async function processMention(
169-
mention: { tweetId: string; text: string; authorId: string; mediaUrls: string[] },
169+
mention: {
170+
tweetId: string
171+
text: string
172+
authorId: string
173+
mediaUrls: string[]
174+
parentTweetText?: string
175+
parentMediaUrls?: string[]
176+
parentAuthorId?: string
177+
},
170178
client: Client,
171179
baseUrl: string
172180
): Promise<{ action: string; projectId?: string; error?: string }> {
173-
const { tweetId, text, authorId, mediaUrls: mentionMediaUrls } = mention
181+
const { tweetId, text, authorId, mediaUrls: mentionMediaUrls, parentTweetText, parentMediaUrls, parentAuthorId } = mention
182+
183+
// Merge parent + mention media URLs
184+
// Parent images come first (they provide the design context)
185+
// Reply images are included only if the reply author is the same as the parent author
186+
const allMediaUrls: string[] = []
187+
if (parentMediaUrls && parentMediaUrls.length > 0) {
188+
allMediaUrls.push(...parentMediaUrls)
189+
}
190+
if (mentionMediaUrls.length > 0) {
191+
// Only include reply images if same author as parent, or if there's no parent
192+
if (!parentAuthorId || parentAuthorId === authorId) {
193+
allMediaUrls.push(...mentionMediaUrls)
194+
}
195+
}
174196

175197
// Skip if already processed
176198
if (await hasProcessed(tweetId)) {
@@ -218,9 +240,9 @@ export async function processMention(
218240
console.log(`[X-Bot] Found linked user: ${capsuleUserId} (@${authorUsername})`)
219241

220242
// Quick pre-filter before AI classification
221-
const mightBeAppRequest = quickAppRequestCheck(text)
243+
const mightBeAppRequest = quickAppRequestCheck(text) || (parentTweetText ? quickAppRequestCheck(parentTweetText) : false)
222244

223-
if (!mightBeAppRequest && mentionMediaUrls.length === 0) {
245+
if (!mightBeAppRequest && allMediaUrls.length === 0) {
224246
console.log(`[X-Bot] Tweet ${tweetId} doesn't look like app request, skipping`)
225247
await recordTweet({
226248
tweetId,
@@ -236,7 +258,7 @@ export async function processMention(
236258

237259
// Classify with AI
238260
console.log(`[X-Bot] Classifying tweet ${tweetId} with AI...`)
239-
const classification = await classifyTweet(text, mentionMediaUrls.length > 0, authorUsername)
261+
const classification = await classifyTweet(text, allMediaUrls.length > 0, authorUsername, parentTweetText)
240262
console.log(`[X-Bot] Classification result:`, classification)
241263

242264
if (!classification.isAppRequest) {
@@ -294,9 +316,9 @@ export async function processMention(
294316
console.log(`[X-Bot] App request detected! Starting generation...`)
295317

296318
let imageUrls: string[] = []
297-
if (mentionMediaUrls.length > 0) {
298-
console.log(`[X-Bot] Downloading ${mentionMediaUrls.length} images...`)
299-
imageUrls = await downloadAndStoreTweetImages(tweetId, mentionMediaUrls)
319+
if (allMediaUrls.length > 0) {
320+
console.log(`[X-Bot] Downloading ${allMediaUrls.length} images (${parentMediaUrls?.length || 0} from parent, ${mentionMediaUrls.length} from reply)...`)
321+
imageUrls = await downloadAndStoreTweetImages(tweetId, allMediaUrls)
300322
console.log(`[X-Bot] Stored ${imageUrls.length} images`)
301323
}
302324

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# X Bot: Parent Tweet Fetching for Reply Mentions
2+
3+
## Problem
4+
5+
When a user replies to someone else's tweet with `@rnvibecode create this app`, the bot only sees the reply text and images — not the parent tweet's content. This means design screenshots or app descriptions in the parent tweet are lost.
6+
7+
## Solution
8+
9+
Fetch the immediate parent tweet when a mention is a reply, and include its text and images in the classification and generation pipeline.
10+
11+
## Design Decisions
12+
13+
- **Depth**: Immediate parent only (no thread walking)
14+
- **Image merging**: Parent images first, then reply images (only if reply author === parent tweet author)
15+
- **Classification**: Pass parent text + image presence to the AI classifier for better context
16+
- **Generation**: Include parent text in the generation prompt
17+
18+
## Changes
19+
20+
### 1. Poll route (`apps/web/app/(app)/api/x-bot/poll/route.ts`)
21+
- Add `referenced_tweets` to `tweet.fields`
22+
- Add `referenced_tweets.id` to `expansions`
23+
- When a mention has `referenced_tweets` with type `replied_to`, fetch the parent tweet via `client.tweets.findTweetById()`
24+
- Extract parent text and media URLs
25+
- Pass `parentTweetText` and `parentMediaUrls` to `processMention`
26+
27+
### 2. Process mention (`apps/web/lib/x-bot/process-mention.ts`)
28+
- Extend mention interface with `parentTweetText?: string` and `parentMediaUrls?: string[]`
29+
- Merge parent + mention media URLs (parent first, reply only if same author)
30+
- Pass parent text to classifier and generation
31+
32+
### 3. Classify tweet (`apps/web/lib/x-bot/classify-tweet.ts`)
33+
- Add `parentTweetText?: string` parameter
34+
- Include parent text in classification prompt so "create this" + parent context = valid app request
35+
36+
### 4. Quick pre-filter
37+
- When parent text exists, also check it for app request keywords

0 commit comments

Comments
 (0)