fix(embed): 开启 Host context 后点击追问建议不再返回 400
嵌入渠道调用 WeKnora.setContext 后,前端 buildQueryWithHostContext 会把 query 包成 "[Host context]\n<k: v 行>\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 #3820main
parent
1912ba26c5
commit
f46c990567
|
|
@ -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) => {
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue