From 04c94d4cfc5ee77013496c357de67f7fb6d27db0 Mon Sep 17 00:00:00 2001 From: Stephen Zhou Date: Fri, 26 Apr 2024 19:32:43 +0800 Subject: [PATCH] fix: can not get ai summary without lang param (#1756) * fix: can not get ai summary without lang param * update * update * update * rename --- src/app/api/{ai-summary => summary}/route.ts | 2 +- src/queries/page.server.ts | 24 +++++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) rename src/app/api/{ai-summary => summary}/route.ts (95%) diff --git a/src/app/api/ai-summary/route.ts b/src/app/api/summary/route.ts similarity index 95% rename from src/app/api/ai-summary/route.ts rename to src/app/api/summary/route.ts index fb9e1516..aff153d0 100644 --- a/src/app/api/ai-summary/route.ts +++ b/src/app/api/summary/route.ts @@ -11,7 +11,7 @@ export async function GET(req: Request): Promise { try { const summary = await getSummary({ cid: query.cid, - lang: String(query.lang), + lang: query.lang, }) return res.status(200).json({ summary }) } catch (error) { diff --git a/src/queries/page.server.ts b/src/queries/page.server.ts index 61ab12c5..c10e3f26 100644 --- a/src/queries/page.server.ts +++ b/src/queries/page.server.ts @@ -89,13 +89,17 @@ const chains = new Map() const getOriginalSummary = async ({ cid, lang, + content: _content, }: { cid: string + content?: string lang?: string }) => { if (!model) return try { - let { content } = await (await fetch(toGateway(`ipfs://${cid}`))).json() + let content: string = + _content ?? + (await (await fetch(toGateway(`ipfs://${cid}`))).json()).content if (content?.length > 5000) { content = content.slice(0, 5000) @@ -146,18 +150,26 @@ const lock = new AsyncLock() export async function getSummary({ cid, - lang, + lang: _lang, }: { cid: string lang?: string }) { + let lang = _lang ?? "" + let content: string | undefined + if (!lang) { + content = (await (await fetch(toGateway(`ipfs://${cid}`))).json()) + .content as string + lang = detectLanguage(content) + } + const summary = (await cacheGet({ key: ["summary", cid, lang], allowEmpty: true, noUpdate: true, noExpire: true, getValueFun: async () => { - if (!lang || locales.includes(lang as Language)) { + if (locales.includes(lang as Language)) { let result await lock.acquire(cid, async () => { const meta = await prisma.metadata.findFirst({ @@ -165,12 +177,12 @@ export async function getSummary({ uri: `ipfs://${cid}`, }, }) - const key = `ai_summary_${lang?.replace("-", "").toLowerCase()}` + const key = `ai_summary_${lang.replace("-", "").toLowerCase()}` if (meta) { if (meta?.[key as keyof Metadata]) { result = meta?.[key as keyof Metadata] } else { - const summary = await getOriginalSummary({ cid, lang }) + const summary = await getOriginalSummary({ cid, lang, content }) if (summary) { await prisma.metadata.update({ where: { @@ -184,7 +196,7 @@ export async function getSummary({ } } } else { - const summary = await getOriginalSummary({ cid, lang }) + const summary = await getOriginalSummary({ cid, lang, content }) if (summary) { await prisma.metadata.create({ data: {