You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.3 KiB
35 lines
1.3 KiB
import { z } from "zod";
|
|
import { eq, and } from "drizzle-orm";
|
|
import { dbGlobal } from "drizzle-pkg/lib/db";
|
|
import { items } from "drizzle-pkg/lib/schema/collection";
|
|
import { getContextUser } from "#server/utils/context";
|
|
|
|
export default defineWrappedResponseHandler({ auth: "required" }, async (event) => {
|
|
const user = getContextUser(event)!;
|
|
const body = await readBody(event);
|
|
const parsed = z.object({ itemId: z.number() }).safeParse(body);
|
|
if (!parsed.success) return R.error("参数校验失败", parsed.error.issues);
|
|
|
|
const { itemId } = parsed.data;
|
|
const [item] = await dbGlobal
|
|
.select()
|
|
.from(items)
|
|
.where(and(eq(items.id, itemId), eq(items.userId, user.id)));
|
|
|
|
if (!item) return R.error("收藏不存在", null);
|
|
|
|
// Generate summary from content: extract key sentences
|
|
const text = [item.title, item.description, item.content].filter(Boolean).join("。");
|
|
const sentences = text.split(/[。!?\n]/).map(s => s.trim()).filter(s => s.length > 8);
|
|
|
|
// Pick the first 2-3 meaningful sentences as summary
|
|
const summary = sentences.slice(0, 3).join("。") + (sentences.length > 3 ? "。" : "");
|
|
|
|
// Save to DB
|
|
await dbGlobal
|
|
.update(items)
|
|
.set({ aiSummary: summary })
|
|
.where(and(eq(items.id, itemId), eq(items.userId, user.id)));
|
|
|
|
return R.success({ summary });
|
|
});
|
|
|