Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:sparkles: AI Agent https://github.com/siyuan-note/siyuan/issues/17797

Danielfec1cf62a07c
diff --git a/kernel/util/webfetch.go b/kernel/util/webfetch.go
index be3ddd2ead..1cbe981cd4 100644
--- a/kernel/util/webfetch.go
+++ b/kernel/util/webfetch.go
@@ -24,6 +24,7 @@ import (
"net/url"
"strings"
+ "github.com/88250/lute"
"github.com/siyuan-note/httpclient"
)
@@ -79,22 +80,50 @@ func WebFetch(rawURL, format string) (string, error) {
return truncateRunes(htmlStr, maxWebFetchChars), nil
}
+ if htmlStr == "" {
+ return "", nil
+ }
+
engine := NewLute()
var result string
switch format {
case "text":
- result = engine.HTML2Text(htmlStr)
+ result, _ = safeHTML2Text(engine, htmlStr)
default: // markdown
- md, mdErr := engine.HTML2Markdown(htmlStr)
+ md, mdErr := safeHTML2Markdown(engine, htmlStr)
if mdErr != nil {
return "", errors.New("HTML to Markdown conversion failed: " + mdErr.Error())
}
result = md
}
+ if result == "" {
+ return htmlStr, nil
+ }
+
return truncateRunes(result, maxWebFetchChars), nil
}
+func safeHTML2Markdown(engine *lute.Lute, htmlStr string) (result string, err error) {
+ defer func() {
+ if r := recover(); r != nil {
+ err = fmt.Errorf("HTML to Markdown panicked: %v", r)
+ }
+ }()
+ result, err = engine.HTML2Markdown(htmlStr)
+ return
+}
+
+func safeHTML2Text(engine *lute.Lute, htmlStr string) (result string, err error) {
+ defer func() {
+ if r := recover(); r != nil {
+ err = fmt.Errorf("HTML to text panicked: %v", r)
+ }
+ }()
+ result = engine.HTML2Text(htmlStr)
+ return
+}
+
func truncateRunes(s string, maxChars int) string {
runes := []rune(s)
if len(runes) <= maxChars {