Skip to content
Open
Changes from all commits
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
52 changes: 42 additions & 10 deletions apps/web/app/api/onboarding/research/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,41 @@ interface ResearchRequest {
email?: string
}

function extractHandle(url: string): string {
const cleaned = url
.toLowerCase()
.replace("https://x.com/", "")
.replace("https://twitter.com/", "")
.replace("http://x.com/", "")
.replace("http://twitter.com/", "")
.replace("@", "")

return (cleaned.split("/")[0] ?? cleaned).split("?")[0] ?? cleaned
function isXHost(hostname: string): boolean {
const host = hostname.toLowerCase()
return (
host === "x.com" ||
host === "twitter.com" ||
host.endsWith(".x.com") ||
host.endsWith(".twitter.com")
)
}

function extractHandle(input: string): string {
const trimmed = input.trim()
if (!trimmed) return ""

let handle = trimmed.replace(/^@+/, "")
const lower = handle.toLowerCase()

if (lower.includes("x.com") || lower.includes("twitter.com")) {
try {
const parsed = new URL(
handle.startsWith("http://") || handle.startsWith("https://")
? handle
: `https://${handle}`,
)
handle = isXHost(parsed.hostname)
? (parsed.pathname.split("/").filter(Boolean)[0] ?? "")
: ""
} catch {
handle =
handle.match(/(?:^|[./])(?:x\.com|twitter\.com)\/([^/\s?#]+)/i)?.[1] ??
""
}
}

return handle.replace(/^@+/, "").split(/[/?#]/)[0]?.toLowerCase() ?? ""
}

function finalPrompt(handle: string, userContext: string) {
Expand Down Expand Up @@ -47,6 +72,13 @@ export async function POST(req: Request) {

const handle = extractHandle(xUrl)

if (!/^[A-Za-z0-9_]{1,15}$/.test(handle)) {
return Response.json(
{ error: "Could not parse a valid X/Twitter handle from the input" },
{ status: 400 },
)
}

const contextParts: string[] = []
if (name) contextParts.push(`Name: ${name}`)
if (email) contextParts.push(`Email: ${email}`)
Expand Down