import { type NextRequest, NextResponse } from "next/server"; import { enqueueTranscription, readTranscription } from "@/lib/stt/tasks"; import { isActive } from "@/lib/stt/types"; export const runtime = "nodejs"; export const dynamic = "force-dynamic"; const validId = (id: unknown): id is string => typeof id === "string" && /^[A-Za-z0-9_-]{1,128}$/.test(id); export async function GET(req: NextRequest) { const id = req.nextUrl.searchParams.get("awemeId"); if (!validId(id)) return NextResponse.json({ error: "需要有效的 awemeId" }, { status: 400 }); try { const state = await readTranscription(id); return state ? NextResponse.json(state) : NextResponse.json({ error: "视频不存在" }, { status: 404 }); } catch { return NextResponse.json({ error: "读取转写状态失败" }, { status: 500 }); } } export async function POST(req: NextRequest) { const body = await req.json().catch(() => null); if (!validId(body?.awemeId)) return NextResponse.json({ error: "需要有效的 awemeId" }, { status: 400 }); try { const state = await enqueueTranscription(body.awemeId, true); return state ? NextResponse.json(state, { status: isActive(state.status) ? 202 : 200 }) : NextResponse.json({ error: "视频不存在" }, { status: 404 }); } catch { return NextResponse.json({ error: "转写任务入队失败" }, { status: 500 }); } }