W0 — security hardening (5 fixes verified live on disclosure.top)
- middleware: gate /api/admin/* same as /admin/* (F1)
- imgproxy: tighten LOCAL_FILESYSTEM_ROOT from / to /var/lib/storage (F2)
- studio: real basic-auth label (bcrypt hash, middleware reference) (F3)
- relations: ENABLE ROW LEVEL SECURITY + public SELECT policy (F4)
- migration 0003: fold is_searchable + hybrid_search update into canonical (TD#2)
W1 — observability + resilience + autocomplete
- studio: HOSTNAME=0.0.0.0 so Next.js binds on loopback for healthcheck
- compose: PG_POOL_MAX=20, CLAUDE_CODE_OAUTH_TOKEN gated by separate env
- claude-code.ts: subprocess timeout configurable (CLAUDE_CODE_TIMEOUT_MS)
- openrouter.ts: retry with exponential backoff + Retry-After + in-memory
circuit breaker (promotes FALLBACK after CB_THRESHOLD failures)
- lib/logger.ts: pino logger (NDJSON prod / pretty dev) + withRequest helper
- middleware: mints correlation_id, stamps x-correlation-id response header,
emits structured http_request log per /api/* call
- messages/route.ts: switch to structured logger
- 60_meili_index.py: push documents + chunks into Meilisearch
- /api/search/autocomplete: parallel meili search (docs + chunks), 5-8ms p50
- search-autocomplete.tsx: debounced dropdown wired into search-panel
W1.2 — Glitchtip + Forgejo self-hosted
- compose: glitchtip-redis + glitchtip-web + glitchtip-worker (v4.2)
- compose: forgejo + forgejo-runner (server v9, runner v6) with group_add=988
- @sentry/nextjs SDK wired (instrumentation.ts + sentry.{client,server}.config.ts)
- /api/admin/throw smoke endpoint (gated by W0-F1 middleware)
- Synthetic event ingestion verified at glitchtip.disclosure.top
- forgejo.disclosure.top up, repo discadmin/disclosure-bureau created,
runner registered (labels: ubuntu-latest, docker)
- .forgejo/workflows/ci.yml: typecheck + lint + build + npm audit + python
syntax + compose validation
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
95 lines
3 KiB
TypeScript
95 lines
3 KiB
TypeScript
/**
|
|
* /api/search/autocomplete — typo-tolerant prefix search via Meilisearch.
|
|
*
|
|
* Hits two indexes in parallel and returns a small merged result:
|
|
* - documents (title-level matches, used to jump to a doc)
|
|
* - chunks (passage-level matches, used for in-doc navigation)
|
|
*
|
|
* Target latency: sub-30ms inside the docker network. Falls back to empty
|
|
* results if Meilisearch is unreachable so the chat / hybrid_search aren't
|
|
* blocked. Auth: none — same as /api/search/hybrid; corpus is public.
|
|
*/
|
|
import { NextResponse } from "next/server";
|
|
import { withRequest } from "@/lib/logger";
|
|
|
|
export const runtime = "nodejs";
|
|
export const dynamic = "force-dynamic";
|
|
|
|
const MEILI_URL = process.env.MEILISEARCH_URL || "http://meilisearch:7700";
|
|
const MEILI_KEY = process.env.MEILISEARCH_API_KEY || process.env.MEILI_MASTER_KEY || "";
|
|
|
|
interface DocHit {
|
|
doc_id: string;
|
|
canonical_title: string;
|
|
collection?: string;
|
|
}
|
|
|
|
interface ChunkHit {
|
|
chunk_pk: number;
|
|
doc_id: string;
|
|
chunk_id: string;
|
|
page: number;
|
|
type: string;
|
|
content_pt?: string;
|
|
content_en?: string;
|
|
ufo_anomaly?: boolean;
|
|
}
|
|
|
|
async function meiliSearch(index: string, q: string, limit: number): Promise<unknown[]> {
|
|
const r = await fetch(`${MEILI_URL}/indexes/${index}/search`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Authorization": `Bearer ${MEILI_KEY}`,
|
|
"Content-Type": "application/json",
|
|
},
|
|
body: JSON.stringify({ q, limit, attributesToHighlight: ["canonical_title", "content_pt", "content_en"] }),
|
|
signal: AbortSignal.timeout(2000),
|
|
});
|
|
if (!r.ok) throw new Error(`meili ${r.status}`);
|
|
const data = await r.json();
|
|
return data.hits ?? [];
|
|
}
|
|
|
|
export async function GET(request: Request) {
|
|
const log = withRequest(request);
|
|
const url = new URL(request.url);
|
|
const q = (url.searchParams.get("q") || "").trim();
|
|
const limit = Math.min(Number(url.searchParams.get("limit") || 8), 20);
|
|
|
|
if (q.length < 2) {
|
|
return NextResponse.json({ q, documents: [], chunks: [] });
|
|
}
|
|
if (!MEILI_KEY) {
|
|
log.warn({ event: "autocomplete_unconfigured" }, "MEILI key not set");
|
|
return NextResponse.json({ q, documents: [], chunks: [], reason: "meili_not_configured" });
|
|
}
|
|
|
|
const t0 = Date.now();
|
|
const [docs, chunks] = await Promise.all([
|
|
meiliSearch("documents", q, Math.min(limit, 5)).catch(() => []),
|
|
meiliSearch("chunks", q, limit).catch(() => []),
|
|
]) as [DocHit[], ChunkHit[]];
|
|
|
|
const dt = Date.now() - t0;
|
|
log.info({ event: "autocomplete", q, docs: docs.length, chunks: chunks.length, dt_ms: dt }, "autocomplete done");
|
|
|
|
return NextResponse.json({
|
|
q,
|
|
duration_ms: dt,
|
|
documents: docs.map((d) => ({
|
|
doc_id: d.doc_id,
|
|
title: d.canonical_title,
|
|
collection: d.collection,
|
|
href: `/d/${d.doc_id}`,
|
|
})),
|
|
chunks: chunks.map((c) => ({
|
|
chunk_id: c.chunk_id,
|
|
doc_id: c.doc_id,
|
|
page: c.page,
|
|
type: c.type,
|
|
excerpt: (c.content_pt || c.content_en || "").slice(0, 180),
|
|
ufo_anomaly: !!c.ufo_anomaly,
|
|
href: `/d/${c.doc_id}/p${String(c.page).padStart(3, "0")}#${c.chunk_id}`,
|
|
})),
|
|
});
|
|
}
|