Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

call sync-embeddings with qstash delay (#3547)

Pedro Ladeira414dd3a3dcc8
diff --git a/apps/web/app/api/ai/sync-embeddings/route.ts b/apps/web/app/api/ai/sync-embeddings/route.ts
index da026779d9..855c659b63 100644
--- a/apps/web/app/api/ai/sync-embeddings/route.ts
+++ b/apps/web/app/api/ai/sync-embeddings/route.ts
@@ -1,11 +1,34 @@
import { upsertDocsEmbeddings } from "@/lib/ai/upsert-docs-embedding";
+import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { qstash } from "@/lib/cron";
+import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+import * as z from "zod/v4";
+
+const schema = z.object({
+ url: z.url().refine(
+ (val) => {
+ try {
+ const { protocol, hostname, pathname } = new URL(val);
+ return (
+ protocol === "https:" &&
+ ["dub.co", "www.dub.co"].includes(hostname) &&
+ ["/docs/", "/help/"].some((p) => pathname.startsWith(p))
+ );
+ } catch {
+ return false;
+ }
+ },
+ { message: "URL must be a dub.co/docs or dub.co/help URL" },
+ ),
+ delay: z.number().positive().optional(),
+});
// POST /api/ai/sync-embeddings
// Triggers re-embedding of a single docs/help article.
// Called by the docs GitHub Action when a .mdx file changes.
//
// Auth: Authorization: Bearer <EMBEDDING_SYNC_SECRET>
-// Body: { url: string }
+// Body: { url: string; delay?: number }
export const POST = async (req: Request) => {
const authHeader = req.headers.get("Authorization");
const secret = process.env.EMBEDDING_SYNC_SECRET;
@@ -14,50 +37,37 @@ export const POST = async (req: Request) => {
return new Response("Unauthorized", { status: 401 });
}
- let url: string;
- try {
- ({ url } = await req.json());
- } catch {
- return new Response("Invalid JSON body", { status: 400 });
- }
-
- if (!url || typeof url !== "string") {
- return new Response("Missing or invalid `url` field", { status: 400 });
- }
-
- let parsedUrl: URL;
try {
- parsedUrl = new URL(url);
- } catch {
- return new Response("Invalid URL", { status: 400 });
- }
-
- const allowedHostnames = ["dub.co", "www.dub.co"];
- const allowedPathPrefixes = ["/docs/", "/help/"];
- if (
- parsedUrl.protocol !== "https:" ||
- !allowedHostnames.includes(parsedUrl.hostname) ||
- !allowedPathPrefixes.some((p) => parsedUrl.pathname.startsWith(p))
- ) {
- return new Response("URL must be a dub.co/docs or dub.co/help URL", {
- status: 400,
+ const body = await req.json().catch(() => {
+ throw new DubApiError({ code: "bad_request", message: "Invalid JSON body." });
});
- }
+ const { url, delay } = schema.parse(body);
+ const normalizedUrl = new URL(url).toString();
- const normalizedUrl = parsedUrl.toString();
+ if (delay !== undefined) {
+ const response = await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/ai/sync-embeddings`,
+ method: "POST",
+ delay,
+ headers: { Authorization: `Bearer ${secret}` },
+ body: { url: normalizedUrl },
+ });
+
+ return Response.json({
+ scheduled: true,
+ url: normalizedUrl,
+ delay,
+ messageId: response.messageId,
+ });
+ }
- try {
// const pageviewsMap = await fetchPlausiblePageviews();
const result = await upsertDocsEmbeddings(
normalizedUrl,
// pageviewsMap // TODO: add pageviewsMap back in once we support it
);
return Response.json({ success: true, url: normalizedUrl, ...result });
- } catch (err) {
- console.error("Failed to seed URL:", normalizedUrl, err);
- return Response.json(
- { success: false, url: normalizedUrl, error: "Failed to seed embedding" },
- { status: 500 },
- );
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
}
};