refactor(P2/P3): 完成全部7批重构 — 模块化、SSE退避、无障碍、Zod校验、Server组件、Room关系化
批次A:重命名 + 路由拆分 - store.ts → roomRepository.ts,更新全部 import - blindbox/plan/route.ts 精简为薄路由,业务逻辑抽取到 planActions.ts / planQueries.ts 批次B:blindboxPlanGen.ts 拆分(710行 → src/lib/plan/) - agentPlan.ts:Agent 工具调用与系统提示 - legacyPlan.ts:非 Agent 备用生成逻辑 - ideaSelection.ts:Idea 筛选与 Slot 映射 - transitEnrichment.ts:交通信息查询与填充 - index.ts:runPlanGeneration 主入口 批次C:SSE 连接稳定性 - useRoomPolling.ts 加入指数退避重连(上限60s,含Jitter) - plan/stream/route.ts 添加30s心跳 + abort信号清理 批次D:无障碍修复 - Modal:role=dialog、aria-modal、aria-labelledby - AuthModal:aria-label关闭按钮、tablist/tab/aria-selected - PlanItemEditModal、QrInviteModal:补全aria-label - BlindboxPlan:图标按钮aria-label 批次E:Zod 引入 - src/lib/schemas/ai.ts:AI返回值 Schema(IdeaTagsSchema等5个) - src/lib/schemas/requests.ts:请求体 Schema - ai.ts 手工验证替换为 Zod safeParse 批次F:Server Components - achievements/page.tsx → Server Component + AchievementsClient.tsx - profile/page.tsx → Server Component + ProfileClient.tsx 批次G:Room 关系化模型 - prisma/schema.prisma:新增 RoomMember、RoomRestaurant、RoomLike、RoomSwipe 4张表 - migration:20260302010000_room_relational_model - roomRepository.ts 完整重写(关系查询+应用锁) - buildRoomStatus.ts 适配关系查询 测试:全部329个用例通过,修复68个因auth mock缺失导致的测试失败
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* Plan generation entry point.
|
||||
* Tries the agent path first, falls back to legacy pipeline.
|
||||
*/
|
||||
|
||||
import { prisma } from "@/lib/prisma";
|
||||
import { ApiError } from "@/lib/api";
|
||||
import { runAgentPlanGeneration } from "./agentPlan";
|
||||
import { runLegacyPlanGeneration, PLAN_PROGRESS_MESSAGES } from "./legacyPlan";
|
||||
import { enrichTransitInfo, type EnrichDay } from "./transitEnrichment";
|
||||
import type { TaggedIdea } from "./ideaSelection";
|
||||
|
||||
export interface PlanGenAvailableTime {
|
||||
date: string;
|
||||
startHour: number;
|
||||
endHour: number;
|
||||
}
|
||||
|
||||
export interface PlanGenResult {
|
||||
id: string;
|
||||
days: { date: string; items: unknown[]; summary: string }[];
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export async function runPlanGeneration(
|
||||
roomId: string,
|
||||
userId: string,
|
||||
availableTime: PlanGenAvailableTime,
|
||||
onProgress?: (message: string) => void,
|
||||
): Promise<PlanGenResult> {
|
||||
// 1. Fetch room & ideas
|
||||
const room = await prisma.blindBoxRoom.findUnique({ where: { id: roomId } });
|
||||
if (!room) throw new ApiError("房间不存在", 404);
|
||||
if (!room.lat || !room.lng) {
|
||||
throw new ApiError("请先设置房间位置", 400);
|
||||
}
|
||||
|
||||
onProgress?.(PLAN_PROGRESS_MESSAGES.analyzing);
|
||||
|
||||
const allIdeas = await prisma.blindBoxIdea.findMany({
|
||||
where: { roomId, status: "in_pool", category: { not: null } },
|
||||
select: {
|
||||
id: true,
|
||||
content: true,
|
||||
category: true,
|
||||
timeSlot: true,
|
||||
estimatedMinutes: true,
|
||||
searchQuery: true,
|
||||
searchType: true,
|
||||
costLevel: true,
|
||||
intensity: true,
|
||||
needsBooking: true,
|
||||
},
|
||||
});
|
||||
|
||||
const taggedIdeas: TaggedIdea[] = allIdeas.filter(
|
||||
(i): i is TaggedIdea =>
|
||||
!!i.category &&
|
||||
!!i.timeSlot &&
|
||||
!!i.searchQuery &&
|
||||
!!i.searchType &&
|
||||
typeof i.estimatedMinutes === "number",
|
||||
);
|
||||
|
||||
if (taggedIdeas.length < 2) {
|
||||
throw new ApiError("盒子里至少需要 2 个已标记的想法才能生成计划", 400);
|
||||
}
|
||||
|
||||
// 2. Try agent path, fallback to legacy
|
||||
let days: { date: string; items: unknown[]; summary: string }[];
|
||||
|
||||
try {
|
||||
const agentResult = await runAgentPlanGeneration(
|
||||
{ lat: room.lat, lng: room.lng, city: room.city ?? "" },
|
||||
taggedIdeas,
|
||||
availableTime,
|
||||
onProgress,
|
||||
);
|
||||
days = agentResult.days;
|
||||
} catch (e) {
|
||||
console.error("runAgentPlanGeneration failed, falling back to legacy:", e);
|
||||
onProgress?.("使用备用方案规划...");
|
||||
const legacyResult = await runLegacyPlanGeneration(
|
||||
{ lat: room.lat, lng: room.lng },
|
||||
taggedIdeas,
|
||||
availableTime,
|
||||
onProgress,
|
||||
);
|
||||
days = legacyResult.days;
|
||||
}
|
||||
|
||||
// 3. Enrich transit info (best-effort)
|
||||
onProgress?.("正在查询交通信息...");
|
||||
await enrichTransitInfo(
|
||||
days as EnrichDay[],
|
||||
room.city ?? "上海",
|
||||
room.lat,
|
||||
room.lng,
|
||||
);
|
||||
|
||||
// 4. Save to DB
|
||||
const plan = await prisma.weekendPlan.create({
|
||||
data: {
|
||||
roomId,
|
||||
userId,
|
||||
planData: JSON.stringify({ days }),
|
||||
},
|
||||
});
|
||||
|
||||
return {
|
||||
id: plan.id,
|
||||
days,
|
||||
createdAt: plan.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user