From 1491c5bb079cf2074efec8d98b4953e28d368be5 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Mon, 20 Jan 2025 11:23:27 +0800 Subject: [PATCH] refactor: generate summaries only for content changes (#7200) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core /milestone 2.20.x #### What this PR does / why we need it: 自动生成摘要仅对内容变更时生效 see https://github.com/halo-dev/halo/issues/7193#issuecomment-2581699190 for more details 避免对资源造成浪费如 AI 摘要生成 #### Which issue(s) this PR fixes: Fixes #7193 #### Does this PR introduce a user-facing change? ```release-note 自动生成摘要仅对内容发生变更时生效 ``` --- .../halo/app/core/extension/content/Constant.java | 2 ++ .../run/halo/app/core/reconciler/PostReconciler.java | 11 +++++++++++ .../app/core/reconciler/SinglePageReconciler.java | 12 ++++++++++++ 3 files changed, 25 insertions(+) diff --git a/api/src/main/java/run/halo/app/core/extension/content/Constant.java b/api/src/main/java/run/halo/app/core/extension/content/Constant.java index 154b4ba28a..10e8a17feb 100644 --- a/api/src/main/java/run/halo/app/core/extension/content/Constant.java +++ b/api/src/main/java/run/halo/app/core/extension/content/Constant.java @@ -10,4 +10,6 @@ public enum Constant { public static final String PERMALINK_PATTERN_ANNO = "content.halo.run/permalink-pattern"; public static final String CHECKSUM_CONFIG_ANNO = "checksum/config"; + + public static final String CONTENT_CHECKSUM_ANNO = "checksum/content"; } diff --git a/application/src/main/java/run/halo/app/core/reconciler/PostReconciler.java b/application/src/main/java/run/halo/app/core/reconciler/PostReconciler.java index 9ada817e00..056beffc96 100644 --- a/application/src/main/java/run/halo/app/core/reconciler/PostReconciler.java +++ b/application/src/main/java/run/halo/app/core/reconciler/PostReconciler.java @@ -57,6 +57,7 @@ import run.halo.app.extension.ExtensionClient; import run.halo.app.extension.ExtensionOperator; import run.halo.app.extension.ListOptions; +import run.halo.app.extension.MetadataUtil; import run.halo.app.extension.Ref; import run.halo.app.extension.controller.Controller; import run.halo.app.extension.controller.ControllerBuilder; @@ -386,6 +387,16 @@ private String getExcerpt(Post post) { return StringUtils.EMPTY; } var content = contentWrapper.get(); + + var contentChecksum = Hashing.sha256().hashString(content.getContent(), UTF_8).toString(); + var annotations = MetadataUtil.nullSafeAnnotations(post); + var oldChecksum = annotations.get(Constant.CONTENT_CHECKSUM_ANNO); + if (Objects.equals(oldChecksum, contentChecksum)) { + return post.getStatusOrDefault().getExcerpt(); + } + // update the checksum and generate new excerpt + annotations.put(Constant.CONTENT_CHECKSUM_ANNO, contentChecksum); + var tags = listTagDisplayNames(post); var keywords = new HashSet<>(tags); diff --git a/application/src/main/java/run/halo/app/core/reconciler/SinglePageReconciler.java b/application/src/main/java/run/halo/app/core/reconciler/SinglePageReconciler.java index a09d2b6966..4455c41141 100644 --- a/application/src/main/java/run/halo/app/core/reconciler/SinglePageReconciler.java +++ b/application/src/main/java/run/halo/app/core/reconciler/SinglePageReconciler.java @@ -3,6 +3,7 @@ import static java.nio.charset.StandardCharsets.UTF_8; import static org.springframework.web.util.UriUtils.encodePath; +import com.google.common.hash.Hashing; import java.time.Instant; import java.util.HashSet; import java.util.List; @@ -25,6 +26,7 @@ import run.halo.app.content.comment.CommentService; import run.halo.app.core.counter.CounterService; import run.halo.app.core.counter.MeterUtils; +import run.halo.app.core.extension.content.Constant; import run.halo.app.core.extension.content.Post; import run.halo.app.core.extension.content.SinglePage; import run.halo.app.core.extension.content.Snapshot; @@ -372,6 +374,16 @@ private String getExcerpt(SinglePage singlePage) { return StringUtils.EMPTY; } var content = contentWrapper.get(); + + var contentChecksum = Hashing.sha256().hashString(content.getContent(), UTF_8).toString(); + var annotations = MetadataUtil.nullSafeAnnotations(singlePage); + var oldChecksum = annotations.get(Constant.CONTENT_CHECKSUM_ANNO); + if (Objects.equals(oldChecksum, contentChecksum)) { + return singlePage.getStatusOrDefault().getExcerpt(); + } + // update the checksum and generate new excerpt + annotations.put(Constant.CONTENT_CHECKSUM_ANNO, contentChecksum); + var context = new ExcerptGenerator.Context() .setRaw(content.getRaw()) .setContent(content.getContent())