Skip to content

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

:bug: Preserve block boundaries in DeepSeek plain text output https://github.com/siyuan-note/siyuan/issues/20673

Danielbe4b31a4f0a7
diff --git a/kernel/util/deepseek_web.go b/kernel/util/deepseek_web.go
index bd574cd933..ce199780c7 100644
--- a/kernel/util/deepseek_web.go
+++ b/kernel/util/deepseek_web.go
@@ -14,6 +14,8 @@ import (
"unicode/utf8"
"github.com/siyuan-note/httpclient"
+ "golang.org/x/net/html"
+ "golang.org/x/net/html/atom"
)
const deepSeekWebURL = "https://api.deepseek.com/v1/tools/"
@@ -166,8 +168,9 @@ func DeepSeekWebFetch(ctx context.Context, options DeepSeekWebOptions, rawURL, f
}
result := *response.Content
if format == "text" {
- engine := NewLute()
- result, err = safeHTML2Text(engine, SanitizeHTML(engine.MarkdownStr("", result)))
+ engine := NewStdLute()
+ engine.SetCodeSyntaxHighlight(false)
+ result, err = deepSeekWebText(SanitizeHTML(engine.MarkdownStr("", result)))
if err != nil {
return "", errors.New("DeepSeek Markdown to text conversion failed")
}
@@ -177,3 +180,37 @@ func DeepSeekWebFetch(ctx context.Context, options DeepSeekWebOptions, rawURL, f
}
return truncateRunes(result, maxWebFetchChars), nil
}
+
+// 按 HTML 结构提取文本,保留块间换行、表格单元格间隔和代码中的字面字符。
+func deepSeekWebText(source string) (string, error) {
+ document, err := html.Parse(strings.NewReader(source))
+ if err != nil {
+ return "", err
+ }
+ var output strings.Builder
+ var walk func(*html.Node, bool)
+ walk = func(node *html.Node, preserveWhitespace bool) {
+ preserveWhitespace = preserveWhitespace || node.Type == html.ElementNode &&
+ (node.DataAtom == atom.Pre || node.DataAtom == atom.Code)
+ if node.Type == html.TextNode {
+ if preserveWhitespace || strings.TrimSpace(node.Data) != "" || !strings.ContainsAny(node.Data, "\r\n\t") {
+ output.WriteString(node.Data)
+ }
+ }
+ for child := node.FirstChild; child != nil; child = child.NextSibling {
+ walk(child, preserveWhitespace)
+ }
+ if node.Type != html.ElementNode {
+ return
+ }
+ switch node.DataAtom {
+ case atom.P, atom.Div, atom.H1, atom.H2, atom.H3, atom.H4, atom.H5, atom.H6,
+ atom.Li, atom.Ul, atom.Ol, atom.Blockquote, atom.Pre, atom.Tr, atom.Table, atom.Br, atom.Hr:
+ output.WriteByte('\n')
+ case atom.Td, atom.Th:
+ output.WriteByte('\t')
+ }
+ }
+ walk(document, false)
+ return strings.TrimSpace(output.String()), nil
+}
diff --git a/kernel/util/deepseek_web_test.go b/kernel/util/deepseek_web_test.go
index 0a70c2dc04..75b077e086 100644
--- a/kernel/util/deepseek_web_test.go
+++ b/kernel/util/deepseek_web_test.go
@@ -163,3 +163,41 @@ func TestDeepSeekWebFailureCancellationAndSize(t *testing.T) {
}
})
}
+
+func TestDeepSeekWebFetchPreservesTextStructureAndMarkdownEscapes(t *testing.T) {
+ content := "# Heading\n\nFirst **paragraph**.\n\nSecond paragraph with [link](https://example.com).\n\n" +
+ "- First item\n- Second item\n\n```html\n<button>I'm a button</button>\n<div>another line</div>\n```\n\n" +
+ "| Column one | Column two |\n| --- | --- |\n| Cell one | Cell two |\n\n" +
+ "\\# literal heading\n\n\\*literal asterisks\\*\n\nShort last paragraph."
+ body, _ := json.Marshal(map[string]string{"content": content})
+ mockDeepSeekWeb(t, 200, string(body), nil)
+ markdown, err := DeepSeekWebFetch(context.Background(), DeepSeekWebOptions{APIKey: "test-key"}, "https://8.8.8.8", "markdown")
+ if err != nil || markdown != content {
+ t.Fatalf("provider Markdown was rewritten: %q %v", markdown, err)
+ }
+ text, err := DeepSeekWebFetch(context.Background(), DeepSeekWebOptions{APIKey: "test-key"}, "https://8.8.8.8", "text")
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, fragment := range []string{"Heading\n", "First paragraph.\n", "Second paragraph with link.\n",
+ "First item\n", "Second item\n", "<button>I'm a button</button>\n<div>another line</div>",
+ "Column one\tColumn two", "Cell one\tCell two", "# literal heading", "*literal asterisks*"} {
+ if !strings.Contains(text, fragment) {
+ t.Fatalf("text lost a block boundary or literal content %q: %q", fragment, text)
+ }
+ }
+ if strings.Contains(text, "**") || strings.Contains(text, "```") {
+ t.Fatalf("Markdown formatting leaked into plain text: %q", text)
+ }
+ for _, content := range []string{"Short content.", "<script>hidden()</script>"} {
+ body, _ := json.Marshal(map[string]string{"content": content})
+ mockDeepSeekWeb(t, 200, string(body), nil)
+ text, err := DeepSeekWebFetch(context.Background(), DeepSeekWebOptions{APIKey: "test-key"}, "https://8.8.8.8", "text")
+ if content == "Short content." && (err != nil || text != content) {
+ t.Fatalf("short content was rejected or changed: %q %v", text, err)
+ }
+ if content != "Short content." && (err == nil || text != "") {
+ t.Fatalf("empty sanitized content was accepted: %q %v", text, err)
+ }
+ }
+}