2026-05-26 22:02:17 +08:00

49 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/app/(auth)/auth";
import { callQwen3Vl } from "@/lib/ai/qwen3-vl";
import { createLogger } from "@/lib/observability/logger";
const logger = createLogger("canvas-auto-title");
export async function POST(request: NextRequest) {
logger.debug("request_received");
try {
const session = await auth();
if (!session?.user?.id) {
logger.warn("unauthorized");
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const { imageUrl } = await request.json();
if (!imageUrl) {
return NextResponse.json({ error: "Image URL is required" }, { status: 400 });
}
logger.debug("model_selected", { model: "qwen3-vl" });
// 构造 Prompt
const prompt = "请用2-4个字概括这张图片的主体内容,作为文件标题。直接返回标题,不要包含任何标点符号或其他文字。";
const { text, model } = await callQwen3Vl({
imageUrl,
prompt,
temperature: 0.2,
maxTokens: 50,
});
let title = (text || "").trim();
title = title.replace(/[\s\p{P}\p{S}]+/gu, "");
if (title.length > 4) title = title.slice(0, 4);
if (title.length < 2) title = "新画布";
logger.info("title_generated", { model, titleLength: title.length });
return NextResponse.json({ title });
} catch (error) {
logger.error("auto_title_failed", error);
// 失败时返回默认标题,不报错中断流程
return NextResponse.json({ title: "新画布" });
}
}