Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ Thumbs.db

# ===== 冒烟测试产物(不入库)=====
smoke_menu.jpg
apps/web/test-results/
apps/web/playwright-report/
134 changes: 134 additions & 0 deletions apps/web/app/audits/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
"use client";

/**
* Audit detail: polls run status while it is non-terminal (stops on terminal
* or unmount — 执行方案 B09 步骤②) and lists candidate findings with their
* human-review state. AI candidates are labelled 待人审 until reviewed.
*/

import Link from "next/link";
import { useParams } from "next/navigation";
import { useEffect, useState } from "react";

import { ApiError, api, FindingSummary, getToken } from "@/lib/api";

const TERMINAL = new Set(["COMPLETED", "FAILED", "CANCELLED"]);
const STATUS_LABEL: Record<string, string> = {
QUEUED: "排队中",
INGESTING: "读取证据",
ANALYZING: "AI 分析中",
REVIEW_REQUIRED: "待人审",
COMPLETED: "已完成",
FAILED: "失败",
CANCELLED: "已取消",
};

const FINDING_STYLE: Record<string, string> = {
FAIL: "bg-red-50 text-red-700 border-red-200",
WARN: "bg-amber-50 text-amber-700 border-amber-200",
PASS: "bg-green-50 text-green-700 border-green-200",
UNKNOWN: "bg-gray-50 text-gray-600 border-gray-200",
};

export default function AuditPage() {
const params = useParams<{ id: string }>();
const [run, setRun] = useState<AuditPageRun | null>(null);
const [findings, setFindings] = useState<FindingSummary[]>([]);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
if (!getToken()) {
window.location.href = "/login";
return;
}
let cancelled = false;
let timer: ReturnType<typeof setTimeout> | undefined;

const poll = async () => {
try {
const data = await api.getAudit(params.id);
if (cancelled) return;
setRun(data.run);
setFindings(data.findings ?? []);
if (!TERMINAL.has(data.run.status)) {
timer = setTimeout(poll, 2000); // 轮询至终态;离页由 cancelled 停止
}
} catch (e) {
if (!cancelled) setError(e instanceof ApiError ? e.message : "加载失败");
}
};
poll();
return () => {
cancelled = true;
if (timer) clearTimeout(timer);
};
}, [params.id]);

if (error) {
return (
<main className="mx-auto max-w-2xl p-6">
<p className="rounded bg-red-50 p-3 text-red-700">{error}</p>
</main>
);
}
if (!run) {
return (
<main className="mx-auto max-w-2xl p-6">
<p className="text-gray-500">加载中…</p>
</main>
);
}

return (
<main className="mx-auto max-w-2xl p-6">
<h1 className="mb-1 text-2xl font-semibold">审计 {STATUS_LABEL[run.status] ?? run.status}</h1>
<p className="mb-6 text-xs text-gray-400">
标准 {run.standard.code} {run.standard.version} ·{" "}
{new Date(run.created_at).toLocaleString()}
</p>

{run.status === "FAILED" && (
<p className="mb-4 rounded bg-red-50 p-3 text-red-700">
审计失败:{run.status_reason ?? "未知原因"}(原始证据已保留,可重新启动审计)
</p>
)}
{!TERMINAL.has(run.status) && (
<p className="mb-4 rounded bg-blue-50 p-3 text-blue-700">AI 分析进行中,页面自动刷新…</p>
)}

{TERMINAL.has(run.status) && findings.length === 0 && (
<p className="text-gray-500">本次审计没有产出候选结论。</p>
)}

<ul className="space-y-3">
{findings.map((f) => (
<li key={f.id}>
<Link
href={`/findings/${f.id}`}
className={`block rounded border p-4 hover:opacity-90 ${FINDING_STYLE[f.assessment_status] ?? ""}`}
>
<div className="flex items-center justify-between">
<span className="font-semibold">
{f.assessment_status} · {f.rule_id}
</span>
<span className="text-xs">
{f.review_status === "UNREVIEWED" ? "待人审" : `已${f.review_status}`}
</span>
</div>
{f.observation && <p className="mt-1 text-sm">观察:{f.observation}</p>}
<p className="mt-1 text-xs opacity-70">置信度 {f.confidence}</p>
</Link>
</li>
))}
</ul>
</main>
);
}

type AuditPageRun = {
id: string;
status: string;
status_reason: string | null;
standard: { code: string; version: string };
created_at: string;
};
97 changes: 97 additions & 0 deletions apps/web/app/dashboard/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"use client";

/**
* Dashboard: project list + creation. The entry surface of the P0-1 flow.
*/

import Link from "next/link";
import { useRouter } from "next/navigation";
import { useCallback, useEffect, useState } from "react";

import { ApiError, api, getToken } from "@/lib/api";

type Project = { id: string; name: string; entity_type: string; created_at: string };

export default function DashboardPage() {
const router = useRouter();
const [projects, setProjects] = useState<Project[] | null>(null);
const [error, setError] = useState<string | null>(null);
const [name, setName] = useState("");
const [creating, setCreating] = useState(false);

useEffect(() => {
if (!getToken()) {
router.replace("/login");
return;
}
// Fetch once on mount; `router` is intentionally NOT a dependency — its
// identity changes per render in the App Router, which would loop this
// fetch forever and keep remounting the form (E2E 实测).
api
.listProjects()
.then((list) => setProjects(list ?? []))
.catch((e: ApiError) => setError(e.message));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const create = useCallback(async () => {
if (!name.trim()) return;
setCreating(true);
setError(null);
try {
const p = await api.createProject({
name: name.trim(),
entity_type: "restaurant",
target_locale: "en-US",
});
router.push(`/projects/${p.id}`);
} catch (e) {
setError(e instanceof ApiError ? e.message : "创建失败");
setCreating(false);
}
}, [name, router]);

return (
<main className="mx-auto max-w-2xl p-6">
<h1 className="mb-1 text-2xl font-semibold">验收项目</h1>
<p className="mb-6 text-sm text-gray-500">创建项目 → 上传材料 → AI 审计 → 查看带证据的结论</p>

<section className="mb-8 rounded-lg border p-4">
<h2 className="mb-3 font-medium">新建项目</h2>
<div className="flex flex-col gap-2 sm:flex-row">
<input
className="flex-1 rounded border px-3 py-2"
placeholder="门店名称(如:某某小馆)"
value={name}
onChange={(e) => setName(e.target.value)}
onKeyDown={(e) => e.key === "Enter" && create()}
/>
<button
className="rounded bg-black px-4 py-2 font-medium text-white disabled:opacity-50"
onClick={create}
disabled={creating || !name.trim()}
>
{creating ? "创建中…" : "创建"}
</button>
</div>
</section>

{error && <p className="mb-4 rounded bg-red-50 p-3 text-red-700">{error}</p>}

{projects === null && !error && <p className="text-gray-500">加载中…</p>}
{projects !== null && projects.length === 0 && (
<p className="text-gray-500">还没有项目——从上面的表单创建第一个。</p>
)}
<ul className="space-y-2">
{projects?.map((p) => (
<li key={p.id}>
<Link className="block rounded border p-3 hover:bg-gray-50" href={`/projects/${p.id}`}>
<span className="font-medium">{p.name}</span>
<span className="ml-2 text-xs text-gray-400">{p.entity_type}</span>
</Link>
</li>
))}
</ul>
</main>
);
}
154 changes: 154 additions & 0 deletions apps/web/app/findings/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
"use client";

/**
* Evidence Viewer — the Magic Moment (PRD §12.4): every finding is human
* verifiable in one click. The bounding box is drawn in ORIGINAL-image
* normalized coordinates, so it stays anchored at any zoom or screen size
* (定位随缩放保持). Rule, observation vs inference, confidence and the
* 待人审 badge are all explicit (B09 步骤④).
*/

import Link from "next/link";
import { useParams } from "next/navigation";
import { useEffect, useState } from "react";

import { ApiError, api, getToken } from "@/lib/api";

type Finding = {
id: string;
rule_id: string;
assessment_status: string;
severity: string;
observation: string | null;
reason: string | null;
recommended_fix: string | null;
confidence: number;
review_status: string;
evidence_refs: {
evidence_id: string;
locator: { type: string; bbox?: { x: number; y: number; w: number; h: number } };
}[];
};

type EvidenceImage = { id: string; url: string | null; status: string };

export default function FindingPage() {
const params = useParams<{ id: string }>();
const [finding, setFinding] = useState<Finding | null>(null);
const [images, setImages] = useState<EvidenceImage[]>([]);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
if (!getToken()) {
window.location.href = "/login";
return;
}
api
.getFinding(params.id)
.then(async (f) => {
setFinding(f);
// Resolve a private download URL per cited evidence (READY only).
const imgs = await Promise.all(
f.evidence_refs.map(async (ref): Promise<EvidenceImage> => {
try {
const e = await api.getEvidence(ref.evidence_id);
return { id: ref.evidence_id, url: e.download_url, status: e.processing_status };
} catch {
return { id: ref.evidence_id, url: null, status: "UNAVAILABLE" };
}
}),
);
setImages(imgs);
})
.catch((e: ApiError) => setError(e.message));
}, [params.id]);

if (error) {
return (
<main className="mx-auto max-w-2xl p-6">
<p className="rounded bg-red-50 p-3 text-red-700">{error}</p>
</main>
);
}
if (!finding) {
return (
<main className="mx-auto max-w-2xl p-6">
<p className="text-gray-500">加载中…</p>
</main>
);
}

return (
<main className="mx-auto max-w-2xl p-6">
<Link href="javascript:history.back()" className="text-sm text-gray-500 hover:underline">
← 返回
</Link>
<h1 className="my-2 text-2xl font-semibold">
{finding.assessment_status} · {finding.rule_id}
</h1>
<div className="mb-4 flex gap-2 text-sm">
<span className="rounded border px-2 py-0.5">严重度 {finding.severity}</span>
<span className="rounded border px-2 py-0.5">置信度 {finding.confidence}</span>
<span
className={`rounded border px-2 py-0.5 ${
finding.review_status === "UNREVIEWED" ? "border-blue-300 bg-blue-50 text-blue-700" : ""
}`}
>
{finding.review_status === "UNREVIEWED"
? "待人审(AI 候选,非最终结论)"
: `人审:${finding.review_status}`}
</span>
</div>

{images.map((img, i) => {
const bbox = finding.evidence_refs[i]?.locator.bbox;
return (
<figure key={img.id} className="relative mb-4">
{img.url ? (
<span className="relative block">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={img.url} alt={`证据 ${i + 1}`} className="w-full rounded border" />
{bbox && (
<span
className="absolute border-2 border-red-500 bg-red-500/10"
style={{
left: `${bbox.x * 100}%`,
top: `${bbox.y * 100}%`,
width: `${bbox.w * 100}%`,
height: `${bbox.h * 100}%`,
}}
aria-label="证据定位区域"
/>
)}
</span>
) : (
<p className="rounded bg-gray-50 p-3 text-sm text-gray-600">
证据不可查看(状态:{img.status})。hash 与元数据已保留。
</p>
)}
</figure>
);
})}

<section className="space-y-2 text-sm">
<p>
<span className="font-medium">观察(事实):</span>
{finding.observation ?? "—"}
</p>
<p>
<span className="font-medium">推断(理由):</span>
{finding.reason ?? "—"}
</p>
{finding.recommended_fix && (
<p>
<span className="font-medium">建议整改:</span>
{finding.recommended_fix}
</p>
)}
<p className="rounded bg-yellow-50 p-3 text-yellow-800">
以上是 AI 候选结论;确认、修改或驳回后才计入报告(AI Suggests, Human Owns)。
</p>
</section>
</main>
);
}
Loading
Loading