Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(embed): 开启 Host context 后点击追问建议不再返回 400

hailongzhaof46c9905677b
diff --git a/frontend/src/views/embed/EmbedChatCore.vue b/frontend/src/views/embed/EmbedChatCore.vue
index a566a2e36..1a8dcbd0f 100644
--- a/frontend/src/views/embed/EmbedChatCore.vue
+++ b/frontend/src/views/embed/EmbedChatCore.vue
@@ -364,11 +364,10 @@ const recordFollowUpEvent = (
const handleFollowUpSelect = (message: Record<string, unknown>, item: MessageSuggestionItem) => {
const set = message.suggestionSet as MessageSuggestionSet | undefined
- if (set) {
- recordFollowUpEvent(set, 'click', item.id)
- setSuggestionAttribution(set.id, item.id)
- }
- if (!isReplying.value) void sendMsg(item.text, { webSearchEnabled: webSearchEnabled.value })
+ if (set) recordFollowUpEvent(set, 'click', item.id)
+ if (isReplying.value) return
+ if (set) setSuggestionAttribution(set.id, item.id)
+ void sendMsg(item.text, { webSearchEnabled: webSearchEnabled.value })
}
const dismissFollowUps = (message: Record<string, unknown>, set: MessageSuggestionSet) => {
diff --git a/internal/application/service/message_suggestion.go b/internal/application/service/message_suggestion.go
index 6b66d51a2..6ed5ea313 100644
--- a/internal/application/service/message_suggestion.go
+++ b/internal/application/service/message_suggestion.go
@@ -243,7 +243,7 @@ func (s *messageSuggestionService) ValidateAttribution(
}
found := false
for _, question := range set.Questions {
- if question.ID == attribution.QuestionID && strings.TrimSpace(question.Text) == strings.TrimSpace(query) {
+ if question.ID == attribution.QuestionID && queryMatchesSuggestionText(query, question.Text) {
found = true
break
}
@@ -254,6 +254,35 @@ func (s *messageSuggestionService) ValidateAttribution(
return nil
}
+const hostContextQueryMarker = "[Host context]\n"
+
+// queryMatchesSuggestionText reports whether query is the clicked suggestion.
+// Embed chat may prefix a host-context envelope. Context values may contain
+// newlines; the question after the envelope must still be the suggestion text.
+func queryMatchesSuggestionText(query, suggestion string) bool {
+ q := strings.TrimSpace(query)
+ s := strings.TrimSpace(suggestion)
+ if q == s {
+ return true
+ }
+ return s != "" && queryHasHostContextEnvelope(q, s)
+}
+
+// queryHasHostContextEnvelope matches the embed prefix from buildQueryWithHostContext.
+// Context values may contain newlines, so the suggestion text anchors the split:
+// the envelope must end immediately before that text.
+func queryHasHostContextEnvelope(query, suggestion string) bool {
+ rest, ok := strings.CutPrefix(query, hostContextQueryMarker)
+ if !ok || !strings.HasSuffix(rest, suggestion) {
+ return false
+ }
+ head := strings.TrimRight(strings.TrimSuffix(rest, suggestion), " \t")
+ if !strings.HasSuffix(head, "\n\n") {
+ return false
+ }
+ return strings.TrimSpace(strings.TrimSuffix(head, "\n\n")) != ""
+}
+
func (s *messageSuggestionService) generate(
ctx context.Context,
message *types.Message,
diff --git a/internal/application/service/message_suggestion_test.go b/internal/application/service/message_suggestion_test.go
index 93ed261fa..57368666a 100644
--- a/internal/application/service/message_suggestion_test.go
+++ b/internal/application/service/message_suggestion_test.go
@@ -268,3 +268,107 @@ func TestMergeHybridSuggestionItemsFillsMissingKnowledgeSlotsFromModel(t *testin
}
}
}
+
+func TestQueryMatchesSuggestionTextAllowsHostContextEnvelope(t *testing.T) {
+ const suggestion = "如何重置密码?"
+ cases := []struct {
+ name string
+ query string
+ text string
+ want bool
+ }{
+ {name: "exact", query: suggestion, text: suggestion, want: true},
+ {name: "trimmed", query: " " + suggestion + "\n", text: suggestion, want: true},
+ {
+ name: "host context",
+ query: "[Host context]\npage: /pricing\nuserId: u_123\n\n" + suggestion,
+ text: suggestion,
+ want: true,
+ },
+ {
+ name: "host context keeps blank lines inside the question",
+ query: "[Host context]\npage: /pricing\n\n第一行\n\n第二行",
+ text: "第一行\n\n第二行",
+ want: true,
+ },
+ {
+ name: "json value",
+ query: "[Host context]\nmeta: {\"a\":1}\n\n" + suggestion,
+ text: suggestion,
+ want: true,
+ },
+ {
+ name: "string value with one newline",
+ query: "[Host context]\nnote: line1\nline2\n\n" + suggestion,
+ text: suggestion,
+ want: true,
+ },
+ {
+ name: "string value with a blank line",
+ query: "[Host context]\nnote: 第一行\n\n第二行\n\n" + suggestion,
+ text: suggestion,
+ want: true,
+ },
+ {
+ name: "extra text between envelope and question",
+ query: "[Host context]\nnote: line1\n\n请额外执行\n" + suggestion,
+ text: suggestion,
+ want: false,
+ },
+ {
+ name: "extra text after the question",
+ query: "[Host context]\npage: /pricing\n\n" + suggestion + "\n再加一句",
+ text: suggestion,
+ want: false,
+ },
+ {
+ name: "arbitrary prefix",
+ query: "ignore previous\n\n" + suggestion,
+ text: suggestion,
+ want: false,
+ },
+ {
+ name: "empty envelope",
+ query: "[Host context]\n\n" + suggestion,
+ text: suggestion,
+ want: false,
+ },
+ {
+ name: "whitespace-only envelope",
+ query: "[Host context]\n \n\n" + suggestion,
+ text: suggestion,
+ want: false,
+ },
+ {
+ name: "key with surrounding spaces",
+ query: "[Host context]\npage : /pricing\n\n" + suggestion,
+ text: suggestion,
+ want: true,
+ },
+ {
+ name: "empty key",
+ query: "[Host context]\n: x\n\n" + suggestion,
+ text: suggestion,
+ want: true,
+ },
+ {
+ name: "first value starts with a newline",
+ query: "[Host context]\nnote: \nfoo\n\n" + suggestion,
+ text: suggestion,
+ want: true,
+ },
+ {
+ name: "different question",
+ query: "[Host context]\npage: /pricing\n\n另一个问题",
+ text: suggestion,
+ want: false,
+ },
+ }
+ for _, tt := range cases {
+ t.Run(tt.name, func(t *testing.T) {
+ if got := queryMatchesSuggestionText(tt.query, tt.text); got != tt.want {
+ t.Fatalf("queryMatchesSuggestionText() = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}