refactor: generate summaries only for content changes (#7200)

#### 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
自动生成摘要仅对内容发生变更时生效
```
pull/7207/head
guqing 2025-01-20 11:23:27 +08:00 committed by GitHub
parent 3e3572e8a8
commit 1491c5bb07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 0 deletions

View File

@ -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";
}

View File

@ -57,6 +57,7 @@ import run.halo.app.extension.DefaultExtensionMatcher;
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 @@ public class PostReconciler implements Reconciler<Reconciler.Request> {
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);

View File

@ -3,6 +3,7 @@ package run.halo.app.core.reconciler;
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.SinglePageService;
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 @@ public class SinglePageReconciler implements Reconciler<Reconciler.Request> {
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())