WORKFLOW-APP/app/api/bizportal-2/search/route.ts
Dave 7c3847fbb0 feat: BizPortal 2.0 — Python Playwright sidecar
Replaces the brittle agent-driven /api/bizportal/search queue with a
deterministic Python FastAPI service that drives BizPortal via a real
headless Chromium. The old flow is untouched.

What this adds:
- scripts/bizportal_service.py — FastAPI + Playwright service with
  /search, /reserve, /health endpoints. Persistent login session
  via storage_state.json. Locator-based selectors (vs eval JS).
- app/bizportal-2/ — new /bizportal-2 page, synchronous fetch
  (no polling), shows round-trip duration, mirrors the existing
  bulk-add + link UX.
- app/api/bizportal-2/{search,reserve,health}/route.ts — thin
  proxies to the Python sidecar.
- scripts/README.md, .env.example, .gitignore, requirements.txt.
- .gitignore: ignore tsconfig.tsbuildinfo (build artefact).

Wiring:
- components/NavBar.tsx: 'BizPortal 2.0' nav entry with TEST badge.
- app/bizportal/page.tsx: 'Try BizPortal 2.0' action link in
  header so the new option is discoverable from the existing page.

Run: cd scripts && source .venv/bin/activate && uvicorn
bizportal_service:app --port 7001
2026-08-17 18:33:36 +02:00

64 lines
2 KiB
TypeScript

// app/api/bizportal-2/search/route.ts
// POST /api/bizportal-2/search
// Thin wrapper around the Python Playwright sidecar. Synchronous
// — no queue, no polling. The client gets the result in one round
// trip (~5s typical, ~30s worst case for cold login).
import { NextRequest, NextResponse } from "next/server";
export const dynamic = "force-dynamic";
export const maxDuration = 120;
const SERVICE_URL = process.env.BIZPORTAL_SERVICE_URL ?? "http://127.0.0.1:7001";
export async function POST(req: NextRequest) {
const body = await req.json().catch(() => ({}));
const { type, query } = body ?? {};
if (!type || !["id", "ck"].includes(type)) {
return NextResponse.json({ ok: false, error: "type must be 'id' or 'ck'" }, { status: 400 });
}
if (!query?.trim()) {
return NextResponse.json({ ok: false, error: "query is required" }, { status: 400 });
}
const start = Date.now();
try {
const res = await fetch(`${SERVICE_URL}/search`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ type, query: query.trim() }),
// Signal upstream so the browser-side AbortController can cancel.
signal: req.signal,
});
const text = await res.text();
let data: any;
try {
data = JSON.parse(text);
} catch {
return NextResponse.json(
{ ok: false, error: `Python service returned non-JSON: ${text.slice(0, 200)}` },
{ status: 502 },
);
}
if (!res.ok && data.ok === false) {
return NextResponse.json(data, { status: res.status });
}
return NextResponse.json({
...data,
duration_ms: Date.now() - start,
});
} catch (err: any) {
if (err?.name === "AbortError") {
return NextResponse.json({ ok: false, error: "Search cancelled" }, { status: 499 });
}
return NextResponse.json(
{ ok: false, error: `Cannot reach Python service at ${SERVICE_URL}: ${err.message ?? err}` },
{ status: 503 },
);
}
}