Skip to content

Commit

Permalink
Merge pull request #563 from hugo-fixit/feat-post-chat
Browse files Browse the repository at this point in the history
🎉 Feat: add PostChat AI support
  • Loading branch information
Lruihao authored Dec 19, 2024
2 parents 0e7dc99 + 10b4628 commit 2e96060
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ I don't know why 🤷
- **Sub Menu** supported
- **Content Encryption** supported (Pages, Partial)
- **Friends** page embedded template
- **Search** supported by [algolia](https://www.algolia.com/), [Fuse.js](https://fusejs.io/) or CSE
- **Search** supported by [algolia](https://www.algolia.com/), [Fuse.js](https://fusejs.io/)、 CSE or [PostChat](https://ai.tianli0.top/?InviteID=IRE1S88Z)
- **Custom Search Engine (CSE)** supported by [Google](https://programmablesearchengine.google.com/)
- **AI Summary****AI Search** and **AI Chatbot** supported by [PostChat](https://ai.tianli0.top/?InviteID=IRE1S88Z)
- **Twemoji** supported
- Automatically **highlighting** code
- **Copy code** to clipboard with one click
Expand Down
3 changes: 2 additions & 1 deletion README.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@
- 支持**二级菜单**
- 支持**内容加密**(页面、局部)
- 支持**友情链接**的页面模板
- 支持基于 [algolia](https://www.algolia.com/)[Fuse.js](https://fusejs.io/)**CSE****搜索**
- 支持基于 [algolia](https://www.algolia.com/)[Fuse.js](https://fusejs.io/)**CSE**[PostChat](https://ai.tianli0.top/?InviteID=IRE1S88Z)**搜索**
- 支持基于 [Google](https://programmablesearchengine.google.com/)**自定义搜索引擎 (CSE)**
- 支持基于 [PostChat](https://ai.tianli0.top/?InviteID=IRE1S88Z)**AI 摘要****AI 搜索****AI 智能客服**
- 支持 **Twemoji**
- 支持**代码高亮**
- 一键**复制代码**到剪贴板
Expand Down
36 changes: 35 additions & 1 deletion assets/js/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,21 @@ class FixIt {
const $searchToggle = document.getElementById(`search-toggle-${suffix}`);
const $searchLoading = document.getElementById(`search-loading-${suffix}`);
const $searchClear = document.getElementById(`search-clear-${suffix}`);
const $searchCancel = document.getElementById('search-cancel-mobile');

// goto the PostChat panel rather than search results
if (searchConfig.type === 'post-chat' && window.postChatUser) {
if (isMobile) {
$searchInput.addEventListener('focus', () => {
window.postChatUser.setSearchInput('');
}, false);
} else {
$searchToggle.addEventListener('click', () => {
window.postChatUser.setSearchInput('');
}, false);
}
return;
}

if (isMobile) {
this._searchMobileOnce = true;
Expand All @@ -132,7 +147,7 @@ class FixIt {
document.body.classList.add('blur');
$header.classList.add('open');
}, false);
document.getElementById('search-cancel-mobile').addEventListener('click', () => {
$searchCancel.addEventListener('click', () => {
this.disableScrollEvent = false;
$header.classList.remove('open');
document.body.classList.remove('blur');
Expand Down Expand Up @@ -301,6 +316,8 @@ class FixIt {
context: cseConfig.gotoResultsPage
}]);
}
} else {
finish([]);
}
},
templates: {
Expand Down Expand Up @@ -1130,6 +1147,22 @@ class FixIt {
this.scrollEventSet.add(_closeRewardExclude);
}

initPostChatUser() {
window.postChat_mode = postChatConfig.userMode || 'iframe';
if (window.postChat_mode !== 'iframe' || !window.postChatUser) {
return;
}
postChat_theme = this.isDark ? 'dark' : 'light';
this.switchThemeEventSet.add((isDark) => {
const targetFrame = document.getElementById("postChat_iframeContainer")
if (targetFrame) {
window.postChatUser.setPostChatTheme(isDark ? 'dark' : 'light');
} else {
postChat_theme = isDark ? 'dark' : 'light';
}
});
}

onScroll() {
const $headers = [];
const ACCURACY = 20;
Expand Down Expand Up @@ -1269,6 +1302,7 @@ class FixIt {
this.initWatermark();
this.initAutoMark();
this.initReward();
this.initPostChatUser();

window.setTimeout(() => {
this.initComment();
Expand Down
48 changes: 47 additions & 1 deletion hugo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ enableEmoji = true
# Search config
[params.search]
enable = false
# type of search engine ["algolia", "fuse", "cse"]
# type of search engine ["algolia", "fuse", "cse", "post-chat"]
type = "fuse"
# max index length of the chunked content
contentLength = 4000
Expand Down Expand Up @@ -886,6 +886,52 @@ enableEmoji = true
# "corner-indicator", "fill-left", "flash", "flat-top", "loading-bar", "mac-osx", "material", "minimal"]
theme = "minimal"

# FixIt 0.3.17 | NEW PostChat AI config
# Based on your posts to build a knowledge base, support AI summary, AI search, and AI Chatbot.
# Get PostChat Key from my invitation link, thanks for your support!
# https://ai.tianli0.top/?InviteID=IRE1S88Z
[params.postChat]
enable = false
key = "ed872bf9336506bea545183089f5034f81fea6af"
# How users initiate chats: ["iframe", "magic"]
userMode = "magic"
addButton = true
defaultInput = false
left = ""
bottom = ""
width = ""
height = ""
fill = ""
backgroundColor = ""
upLoadWeb = true
showInviteLink = true
userTitle = ""
userDesc = ""
# dom container to be blacked out, e.g. [".aplayer"]
blackDom = []
# Only for iframe mode
frameWidth = "" # e.g. "375px"
frameHeight = "" # e.g. "600px"
# only for magic mode
userIcon = ""
defaultChatQuestions = []
defaultSearchQuestions = []

# FixIt 0.3.17 | NEW Summary AI config
# See https://postchat.zhheo.com/summary.html
[params.postSummary]
enable = false
# If you set `params.postChat.key`, you don't need to set `params.postSummary.key`
key = ""
title = ""
# themes options: ["", "simple", "yanzhi"]
theme = ""
postURL = ""
blacklist = ""
wordLimit = 1000
typingAnimate = true
beginningText = ""

# FixIt 0.3.10 | NEW Global Feed config for RSS, Atom and JSON feed.
[params.feed]
# The number of posts to include in the feed. If set to -1, all posts.
Expand Down
7 changes: 6 additions & 1 deletion layouts/partials/assets.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
{{- $source := $cdn.fuseJS | default "lib/fuse/fuse.min.js" -}}
{{- dict "Source" $source "Fingerprint" $fingerprint "Defer" true | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}
{{- $config = dict "isCaseSensitive" $search.fuse.isCaseSensitive "minMatchCharLength" $search.fuse.minMatchCharLength "findAllMatches" $search.fuse.findAllMatches "location" $search.fuse.location "threshold" $search.fuse.threshold "distance" $search.fuse.distance "ignoreLocation" $search.fuse.ignoreLocation "useExtendedSearch" $search.fuse.useExtendedSearch "ignoreFieldNorm" $search.fuse.ignoreFieldNorm | dict "search" | merge $config -}}
{{- else if eq $search.type "cse" -}}
{{- else if eq $search.type "cse" -}}
{{- $config = dict "type" "cse" | dict "search" | merge $config -}}
{{- $cse := .Site.Params.cse -}}
{{- $config = dict "resultsPage" $cse.resultsPage "gotoResultsPage" (T "assets.gotoResultsPage") | dict "cse" | merge $config -}}
Expand All @@ -32,6 +32,8 @@
{{- if eq $cse.engine "bing" -}}
{{- /* Unsupported */ -}}
{{- end -}}
{{- else if eq $search.type "post-chat" -}}
{{- $config = dict "type" "post-chat" | dict "search" | merge $config -}}
{{- end -}}
{{- end -}}

Expand Down Expand Up @@ -265,6 +267,9 @@
{{- end -}}
{{- end -}}

{{- /* PostChat */ -}}
{{- partial "plugin/post-chat-ai.html" . -}}

{{- range $params.library.css -}}
{{- dict "Source" . "Fingerprint" $fingerprint | dict "Scratch" $.Scratch "Data" | partial "scratch/style.html" -}}
{{- end -}}
Expand Down
112 changes: 112 additions & 0 deletions layouts/partials/plugin/post-chat-ai.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
{{- /*
PostChat AI service.
PostChat: https://postchat.zhheo.com/addCode.html
PostSummary: https://postchat.zhheo.com/summary.html
*/ -}}
{{- $params := .Params | merge .Site.Params -}}
{{- $chatConfig := $params.postChat -}}
{{- $summaryConfig := $params.postSummary -}}
{{- $key := $chatConfig.key | default $summaryConfig.key -}}
{{- $sourcePrefix := "https://ai.tianli0.top/static/public/" -}}
{{- $jsMap := dict
"chat" "postChatUser.min.js"
"summary" "tianli_gpt.min.js"
"both" "postChatUser_summary.min.js"
-}}
{{- /* PostSummary only for public posts */ -}}
{{- $password := .Params.password | default .Site.Params.page.password -}}
{{- $isPost := (eq .Kind "page") | and (eq .Type "posts") | and (not $password) -}}
{{- $summaryEnable := cond $isPost $summaryConfig.enable false -}}
{{- $enable := $chatConfig.enable | or $summaryEnable -}}

{{- if $enable | and $key -}}
{{- $configJS := "" -}}
{{- $fingerprint := .Scratch.Get "fingerprint" -}}
{{- $sourceOpts := dict "Fingerprint" $fingerprint "Defer" true -}}

{{- /* PostSummary */ -}}
{{- if $summaryEnable -}}
{{- $postChatCSS := "https://ai.tianli0.top/static/public/postChatUser_summary.min.css" -}}
{{- dict "Source" $postChatCSS "Fingerprint" $fingerprint | dict "Scratch" .Scratch "Data" | partial "scratch/style.html" -}}
{{- $p := "tianliGPT" -}}
{{- $postSummaryConfig := printf "let %v_postSelector='#content';" $p -}}
{{- with $summaryConfig.title -}}
{{- $postSummaryConfig = add $postSummaryConfig (printf "let %v_Title='%v';" $p .) -}}
{{- end -}}
{{- with $summaryConfig.theme -}}
{{- $postSummaryConfig = add $postSummaryConfig (printf "let %v_theme='%v';" $p .) -}}
{{- end -}}
{{- with $summaryConfig.postURL -}}
{{- $postSummaryConfig = add $postSummaryConfig (printf "let %v_postURL='%v';" $p .) -}}
{{- end -}}
{{- with $summaryConfig.blacklist -}}
{{- $postSummaryConfig = add $postSummaryConfig (printf "let %v_blacklist='%v';" $p .) -}}
{{- end -}}
{{- with $summaryConfig.wordLimit -}}
{{- $postSummaryConfig = add $postSummaryConfig (printf "let %v_wordLimit='%v';" $p .) -}}
{{- end -}}
{{- if eq $summaryConfig.typingAnimate false -}}
{{- $postSummaryConfig = add $postSummaryConfig (printf "let %v_typingAnimate=false;" $p) -}}
{{- end -}}
{{- with $summaryConfig.beginningText -}}
{{- $postSummaryConfig = add $postSummaryConfig (printf "let %v_BeginningText='%v';" $p .) -}}
{{- end -}}
{{- if $chatConfig.enable -}}
{{- $postSummaryConfig = add $postSummaryConfig (printf "let %v_recommendation=true;" $p) -}}
{{- else -}}
{{- $postSummaryConfig = add $postSummaryConfig (printf "let %v_key='%v';" $p $key) -}}
{{- end -}}
{{- $configJS = add $configJS $postSummaryConfig -}}
{{- end -}}

{{- /* PostChat */ -}}
{{- if $chatConfig.enable -}}
{{- $sourceOpts = dict "Attr" (printf "data-postChat_key=%q" $key) | merge $sourceOpts -}}
{{- $blackDom := slice
".expiration-reminder"
"meting-js"
".lnt"
-}}
{{- with $chatConfig.blackDom -}}
{{- $blackDom = $blackDom | append . -}}
{{- end -}}
{{- $upLoadWeb := cond (.IsHome | or $password) false $chatConfig.upLoadWeb -}}
{{- $postChatConfig := dict
"userMode" $chatConfig.userMode
"defaultInput" $chatConfig.defaultInput
"left" $chatConfig.left
"bottom" $chatConfig.bottom
"width" $chatConfig.width
"height" $chatConfig.height
"fill" $chatConfig.fill
"backgroundColor" $chatConfig.backgroundColor
"upLoadWeb" $upLoadWeb
"showInviteLink" $chatConfig.showInviteLink
"userTitle" $chatConfig.userTitle
"userDesc" $chatConfig.userDesc
"addButton" $chatConfig.addButton
"blackDom" $blackDom
"frameWidth" $chatConfig.frameWidth
"frameHeight" $chatConfig.frameHeight
"userIcon" $chatConfig.userIcon
"defaultChatQuestions" $chatConfig.defaultChatQuestions
"defaultSearchQuestions" $chatConfig.defaultSearchQuestions
-}}
{{- $configJS = add $configJS ($postChatConfig | jsonify | printf "var postChatConfig=%s") -}}
{{- end -}}

{{- /* Insert config */ -}}
{{- $configJS | dict "Content" | dict "Scratch" .Scratch "Data" | partial "scratch/script.html" -}}

{{- /* Insert script */ -}}
{{- $postChatJS := "" -}}
{{- if $chatConfig.enable | and $summaryEnable -}}
{{- $postChatJS = add $sourcePrefix (index $jsMap "both") -}}
{{- else if $chatConfig.enable -}}
{{- $postChatJS = add $sourcePrefix (index $jsMap "chat") -}}
{{- else if $summaryEnable -}}
{{- $postChatJS = add $sourcePrefix (index $jsMap "summary") -}}
{{- end -}}
{{- $sourceOpts = dict "Source" $postChatJS | merge $sourceOpts -}}
{{- $sourceOpts | dict "Scratch" $.Scratch "Data" | partial "scratch/script.html" -}}
{{- end -}}
2 changes: 1 addition & 1 deletion layouts/partials/single/expiration-reminder.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{{- $type = "warning" -}}
{{- $icon = "fa-exclamation-triangle" -}}
{{- end -}}
<div class="details admonition {{ $type }} open">
<div class="expiration-reminder details admonition {{ $type }} open">
<div class="details-summary admonition-title">
<i class="icon fa-solid {{ $icon }} fa-fw" aria-hidden="true"></i>{{ T (printf "admonition.%v" $type) }}<i class="details-icon fa-solid fa-angle-right fa-fw" aria-hidden="true"></i>
</div>
Expand Down

0 comments on commit 2e96060

Please sign in to comment.