Skip to content

Commit

Permalink
refactor: generate summaries only for content changes (#7200)
Browse files Browse the repository at this point in the history
#### What type of PR is this?
/kind improvement
/area core
/milestone 2.20.x

#### What this PR does / why we need it:
自动生成摘要仅对内容变更时生效

see #7193 (comment) for more details

避免对资源造成浪费如 AI 摘要生成

#### Which issue(s) this PR fixes:

Fixes #7193

#### Does this PR introduce a user-facing change?

```release-note
自动生成摘要仅对内容发生变更时生效
```
  • Loading branch information
guqing authored Jan 20, 2025
1 parent 3e3572e commit 1491c5b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 1491c5b

Please sign in to comment.