From f46c9905677b36a454aa6669a2619f9eecf9fd23 Mon Sep 17 00:00:00 2001 From: hailongzhao Date: Tue, 29 Sep 2026 11:10:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(embed):=20=E5=BC=80=E5=90=AF=20Host=20conte?= =?UTF-8?q?xt=20=E5=90=8E=E7=82=B9=E5=87=BB=E8=BF=BD=E9=97=AE=E5=BB=BA?= =?UTF-8?q?=E8=AE=AE=E4=B8=8D=E5=86=8D=E8=BF=94=E5=9B=9E=20400?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 嵌入渠道调用 WeKnora.setContext 后,前端 buildQueryWithHostContext 会把 query 包成 "[Host context]\n\n\n<问题>" 再发出,而 ValidateAttribution 用 TrimSpace 后的全文与建议文本做精确比较,带信封的 query 永远不相等,于是每次点击追问建议都返回 400 "invalid suggestion attribution"。 后端:新增 queryMatchesSuggestionText,精确匹配失败时识别 host context 信封——要求以 "[Host context]\n" 开头、信封内容非空、以空行结束,且空行后 紧跟的正好是被点击的建议文本。宿主传入的 value 可以含换行甚至空行,所以用 建议文本从尾部锚定切分,而不是取第一个 "\n\n"。不校验信封里每行的 key/value 形态:前端对 key 不做约束(带空格、空 key、value 以换行开头都会原样拼进去), 校验只会误伤合法 context,而归因只用于统计点击与实际发送,不涉及权限。 在信封与问题之间或问题之后夹带额外文本、任意前缀、空信封仍会被拒绝。 前端:handleFollowUpSelect 原先在回复生成中点击建议时也会写入待发送归因, 但跳过发送,归因会泄漏到用户下一条手动输入的消息上,同样导致 400。改为只在 真正发送时写入归因,click 事件照常上报。 Fixes #3820 --- frontend/src/views/embed/EmbedChatCore.vue | 9 +- .../application/service/message_suggestion.go | 31 +++++- .../service/message_suggestion_test.go | 104 ++++++++++++++++++ 3 files changed, 138 insertions(+), 6 deletions(-) diff --git a/frontend/src/views/embed/EmbedChatCore.vue b/frontend/src/views/embed/EmbedChatCore.vue index a566a2e3..1a8dcbd0 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, 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, set: MessageSuggestionSet) => { diff --git a/internal/application/service/message_suggestion.go b/internal/application/service/message_suggestion.go index 6b66d51a..6ed5ea31 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 93ed261f..57368666 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) + } + }) + } +}