79 lines
2 KiB
TypeScript
79 lines
2 KiB
TypeScript
import { NextResponse } from "next/server"
|
|
import {
|
|
getManualCitationContext,
|
|
retrieveManualContext,
|
|
summarizeManualRetrieval,
|
|
} from "@/lib/manuals-knowledge"
|
|
import { requireAdminToken } from "@/lib/server/admin-auth"
|
|
|
|
function normalizeQuery(value: string | null) {
|
|
return (value || "").trim().slice(0, 400)
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const authError = requireAdminToken(request)
|
|
if (authError) {
|
|
return authError
|
|
}
|
|
|
|
try {
|
|
const { searchParams } = new URL(request.url)
|
|
const query = normalizeQuery(searchParams.get("query"))
|
|
const manufacturer = normalizeQuery(searchParams.get("manufacturer")) || null
|
|
const model = normalizeQuery(searchParams.get("model")) || null
|
|
const manualId = normalizeQuery(searchParams.get("manualId")) || null
|
|
const pageParam = searchParams.get("page")
|
|
const pageNumber =
|
|
pageParam && Number.isFinite(Number(pageParam))
|
|
? Number.parseInt(pageParam, 10)
|
|
: undefined
|
|
|
|
if (!query) {
|
|
return NextResponse.json(
|
|
{ error: "A query parameter is required." },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const result = await retrieveManualContext(query, {
|
|
manufacturer,
|
|
model,
|
|
manualId,
|
|
})
|
|
const citationContext =
|
|
manualId || result.bestManual?.manualId
|
|
? await getManualCitationContext(
|
|
manualId || result.bestManual?.manualId || "",
|
|
pageNumber
|
|
)
|
|
: null
|
|
|
|
return NextResponse.json({
|
|
query,
|
|
filters: {
|
|
manufacturer,
|
|
model,
|
|
manualId,
|
|
pageNumber: pageNumber ?? null,
|
|
},
|
|
summary: summarizeManualRetrieval({
|
|
ran: true,
|
|
query,
|
|
result,
|
|
}),
|
|
result,
|
|
citationContext,
|
|
})
|
|
} catch (error) {
|
|
console.error("Failed to inspect manuals knowledge:", error)
|
|
return NextResponse.json(
|
|
{
|
|
error:
|
|
error instanceof Error
|
|
? error.message
|
|
: "Failed to inspect manuals knowledge",
|
|
},
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|