|
1 | | -export async function GET() { |
2 | | - return Response.json( |
3 | | - { |
4 | | - error: "Search is not configured in the site host zone.", |
5 | | - }, |
6 | | - { status: 404 }, |
7 | | - ); |
| 1 | +import { SiteSearchResult } from "@/components/support/search-types"; |
| 2 | +import Mixedbread from "@mixedbread/sdk"; |
| 3 | +import { NextRequest, NextResponse } from "next/server"; |
| 4 | + |
| 5 | +type GeneratedMetadata = { |
| 6 | + title?: string; |
| 7 | + slug?: string; |
| 8 | + metaTitle?: string; |
| 9 | + metaDescription?: string; |
| 10 | + metaImagePath?: string; |
| 11 | + heroImagePath?: string; |
| 12 | + tags?: string[]; |
| 13 | + excerpt?: string; |
| 14 | + url?: string; |
| 15 | +}; |
| 16 | + |
| 17 | +export const dynamic = "force-dynamic"; |
| 18 | + |
| 19 | +const mixedbreadApiKey = process.env.MIXEDBREAD_API_KEY; |
| 20 | +const storeIdentifiers = ["blog-search", "web-search"] as const; |
| 21 | +const client = mixedbreadApiKey |
| 22 | + ? new Mixedbread({ apiKey: mixedbreadApiKey }) |
| 23 | + : null; |
| 24 | +const websiteBaseUrl = "https://prisma.io"; |
| 25 | +const blogPrefix = "/blog"; |
| 26 | +const docsPrefix = "/docs"; |
| 27 | + |
| 28 | +type MixedbreadSearchChunk = { |
| 29 | + id?: string; |
| 30 | + file_id?: string; |
| 31 | + chunk_index?: number; |
| 32 | + text?: string; |
| 33 | + generated_metadata?: GeneratedMetadata; |
| 34 | +}; |
| 35 | + |
| 36 | +function withPrefixedPath( |
| 37 | + path: string | undefined, |
| 38 | + prefix: string, |
| 39 | +): string { |
| 40 | + const normalizedPath = (path ?? "") |
| 41 | + .replace(/^\/+/, "") |
| 42 | + .replace(new RegExp(`^${prefix.replace("/", "")}/`), ""); |
| 43 | + |
| 44 | + return normalizedPath ? `${prefix}/${normalizedPath}` : "#"; |
| 45 | +} |
| 46 | + |
| 47 | +function withBaseAndPrefixedPath( |
| 48 | + path: string | undefined, |
| 49 | + prefix: string, |
| 50 | +): string { |
| 51 | + const prefixedPath = withPrefixedPath(path, prefix); |
| 52 | + return prefixedPath === "#" |
| 53 | + ? prefixedPath |
| 54 | + : new URL(prefixedPath, websiteBaseUrl).toString(); |
| 55 | +} |
| 56 | + |
| 57 | +function normalizeBlogUrl(slug?: string): string { |
| 58 | + return withBaseAndPrefixedPath(slug, blogPrefix); |
| 59 | +} |
| 60 | + |
| 61 | +function normalizeBlogImagePath(imagePath?: string): string { |
| 62 | + if (!imagePath) return ""; |
| 63 | + if (/^https?:\/\//.test(imagePath)) return imagePath; |
| 64 | + const blogPath = withBaseAndPrefixedPath(imagePath, blogPrefix); |
| 65 | + return blogPath === "#" ? "" : blogPath; |
| 66 | +} |
| 67 | + |
| 68 | +function normalizeDocsUrl(url?: string): string { |
| 69 | + return withBaseAndPrefixedPath(url, docsPrefix); |
| 70 | +} |
| 71 | + |
| 72 | +function getDocsImagePath(normalizedDocsUrl: string): string { |
| 73 | + if (!normalizedDocsUrl || normalizedDocsUrl === "#") return ""; |
| 74 | + const docsUrl = new URL(normalizedDocsUrl, websiteBaseUrl); |
| 75 | + const docsPath = docsUrl.pathname.replace(/^\/docs\//, "").replace(/^\/+/, ""); |
| 76 | + return `https://www.prisma.io/docs/og/${docsPath}/image.png`; |
| 77 | +} |
| 78 | + |
| 79 | +function transformResult(item: MixedbreadSearchChunk): SiteSearchResult | null { |
| 80 | + const metadata = item.generated_metadata; |
| 81 | + if (!metadata) return null; |
| 82 | + |
| 83 | + const source: SiteSearchResult["source"] | null = metadata.slug |
| 84 | + ? "blog" |
| 85 | + : metadata.url |
| 86 | + ? "docs" |
| 87 | + : null; |
| 88 | + if (!source) return null; |
| 89 | + |
| 90 | + const base = |
| 91 | + item.id ?? `${item.file_id ?? "unknown"}-${item.chunk_index ?? "0"}`; |
| 92 | + |
| 93 | + const normalizedUrl = |
| 94 | + source === "blog" |
| 95 | + ? normalizeBlogUrl(metadata.slug) |
| 96 | + : normalizeDocsUrl(metadata.url); |
| 97 | + |
| 98 | + return { |
| 99 | + id: `${base}-page`, |
| 100 | + type: "page", |
| 101 | + source, |
| 102 | + content: |
| 103 | + source === "blog" |
| 104 | + ? metadata.metaTitle ?? metadata.title ?? item.text ?? "Untitled" |
| 105 | + : metadata.title ?? item.text ?? "Untitled", |
| 106 | + url: normalizedUrl, |
| 107 | + description: |
| 108 | + source === "blog" |
| 109 | + ? metadata.metaDescription ?? metadata.excerpt ?? item.text ?? "" |
| 110 | + : metadata.metaDescription ?? item.text ?? "", |
| 111 | + heroImagePath: |
| 112 | + source === "blog" |
| 113 | + ? normalizeBlogImagePath( |
| 114 | + metadata.heroImagePath ?? metadata.metaImagePath, |
| 115 | + ) |
| 116 | + : getDocsImagePath(normalizedUrl), |
| 117 | + tags: source === "blog" ? metadata.tags ?? [] : [], |
| 118 | + }; |
| 119 | +} |
| 120 | + |
| 121 | +function transformResults(results: MixedbreadSearchChunk[]): SiteSearchResult[] { |
| 122 | + return results |
| 123 | + .map(transformResult) |
| 124 | + .filter((item): item is SiteSearchResult => item !== null); |
| 125 | +} |
| 126 | + |
| 127 | +export async function GET(request: NextRequest) { |
| 128 | + if (!client) { |
| 129 | + console.error("Search API called but Mixedbread is not configured"); |
| 130 | + console.error("Please set MIXEDBREAD_API_KEY environment variable"); |
| 131 | + return NextResponse.json([]); |
| 132 | + } |
| 133 | + |
| 134 | + const query = request.nextUrl.searchParams.get("query")?.trim() ?? ""; |
| 135 | + |
| 136 | + if (!query) { |
| 137 | + return NextResponse.json([]); |
| 138 | + } |
| 139 | + |
| 140 | + try { |
| 141 | + const response = await client.stores.search({ |
| 142 | + query, |
| 143 | + store_identifiers: [...storeIdentifiers], |
| 144 | + top_k: 20, |
| 145 | + search_options: { |
| 146 | + rerank: true, |
| 147 | + return_metadata: true, |
| 148 | + }, |
| 149 | + }); |
| 150 | + |
| 151 | + return NextResponse.json( |
| 152 | + transformResults(response.data as MixedbreadSearchChunk[]), |
| 153 | + ); |
| 154 | + } catch (error) { |
| 155 | + console.error("Mixedbread search failed:", error); |
| 156 | + return NextResponse.json([]); |
| 157 | + } |
8 | 158 | } |
0 commit comments