fix: responder en thread cuando el mensaje viene de un thread
Dos problemas corregidos: 1. Detección de threads con E2EE: después de desencriptar un evento, evt.Content.Raw puede no contener m.relates_to. Se añade fallback usando el contenido tipado (evt.Content.Parsed) que es más robusto tras la desencriptación de mautrix. 2. Notificaciones de tools fuera del thread: la notificación "🔨 tool" se enviaba con SendMarkdown directo a la sala, ignorando el contexto de thread. Ahora usa sendReply que respeta ThreadID. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+2
-2
@@ -788,9 +788,9 @@ func (a *Agent) runLLM(ctx context.Context, msgCtx decision.MessageContext, memK
|
||||
continue
|
||||
}
|
||||
|
||||
// Notify the room that a tool is being called
|
||||
// Notify the room that a tool is being called (respect thread context)
|
||||
toolNotice := fmt.Sprintf("🔨 <em>%s</em>", tc.Name)
|
||||
if err := a.matrix.SendMarkdown(ctx, msgCtx.RoomID, toolNotice); err != nil {
|
||||
if err := a.sendReply(ctx, msgCtx.RoomID, msgCtx.EventID, msgCtx.ThreadID, toolNotice); err != nil {
|
||||
a.logger.Warn("failed to send tool call notice", "tool", tc.Name, "err", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -154,6 +154,7 @@ func (l *Listener) Run(ctx context.Context) error {
|
||||
msgCtx.EventID = evt.ID.String()
|
||||
|
||||
// Extract thread root from m.relates_to (Matrix thread support).
|
||||
// Two methods: raw map (fast) + typed content fallback (robust for E2EE).
|
||||
if l.cfg.Threads.Enabled {
|
||||
if relatesTo, ok := evt.Content.Raw["m.relates_to"].(map[string]any); ok {
|
||||
if relType, _ := relatesTo["rel_type"].(string); relType == "m.thread" {
|
||||
@@ -162,6 +163,15 @@ func (l *Listener) Run(ctx context.Context) error {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Fallback: use typed content (more robust after E2EE decryption)
|
||||
if msgCtx.ThreadID == "" {
|
||||
if msgContent, ok := evt.Content.Parsed.(*event.MessageEventContent); ok && msgContent.RelatesTo != nil {
|
||||
if threadParent := msgContent.RelatesTo.GetThreadParent(); threadParent != "" {
|
||||
msgCtx.ThreadID = string(threadParent)
|
||||
l.logger.Debug("thread detected via typed content fallback", "thread_id", msgCtx.ThreadID)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
l.logger.Debug("message parsed",
|
||||
|
||||
@@ -2,6 +2,9 @@ package matrix
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
// extractThreadID is the pure logic extracted from the listener for testability.
|
||||
@@ -93,3 +96,78 @@ func TestExtractThreadID_ThreadWithoutEventID(t *testing.T) {
|
||||
t.Errorf("expected empty string for thread without event_id, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
// extractThreadIDFromParsed is the typed-content fallback for E2EE events.
|
||||
// Mirrors the fallback logic in listener.go.
|
||||
func extractThreadIDFromParsed(parsed interface{}) string {
|
||||
if msgContent, ok := parsed.(*event.MessageEventContent); ok && msgContent.RelatesTo != nil {
|
||||
if threadParent := msgContent.RelatesTo.GetThreadParent(); threadParent != "" {
|
||||
return string(threadParent)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func TestExtractThreadID_TypedFallback_Thread(t *testing.T) {
|
||||
parsed := &event.MessageEventContent{
|
||||
MsgType: event.MsgText,
|
||||
Body: "hello from thread",
|
||||
RelatesTo: (&event.RelatesTo{}).SetThread(id.EventID("$root123"), id.EventID("$last456")),
|
||||
}
|
||||
|
||||
got := extractThreadIDFromParsed(parsed)
|
||||
if got != "$root123" {
|
||||
t.Errorf("expected $root123 from typed fallback, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractThreadID_TypedFallback_NoRelatesTo(t *testing.T) {
|
||||
parsed := &event.MessageEventContent{
|
||||
MsgType: event.MsgText,
|
||||
Body: "plain message",
|
||||
}
|
||||
|
||||
got := extractThreadIDFromParsed(parsed)
|
||||
if got != "" {
|
||||
t.Errorf("expected empty string from typed fallback, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractThreadID_TypedFallback_ReplaceNotThread(t *testing.T) {
|
||||
parsed := &event.MessageEventContent{
|
||||
MsgType: event.MsgText,
|
||||
Body: "edited",
|
||||
RelatesTo: (&event.RelatesTo{}).SetReplace(id.EventID("$original")),
|
||||
}
|
||||
|
||||
got := extractThreadIDFromParsed(parsed)
|
||||
if got != "" {
|
||||
t.Errorf("expected empty string for m.replace via typed fallback, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractThreadID_RawEmptyTypedWorks(t *testing.T) {
|
||||
// Simulates E2EE scenario: Raw map has no m.relates_to but Parsed does
|
||||
raw := map[string]any{
|
||||
"body": "hello from thread",
|
||||
"msgtype": "m.text",
|
||||
// No m.relates_to in raw (can happen after E2EE decryption)
|
||||
}
|
||||
parsed := &event.MessageEventContent{
|
||||
MsgType: event.MsgText,
|
||||
Body: "hello from thread",
|
||||
RelatesTo: (&event.RelatesTo{}).SetThread(id.EventID("$root123"), id.EventID("$last456")),
|
||||
}
|
||||
|
||||
// Raw extraction should fail
|
||||
gotRaw := extractThreadID(raw)
|
||||
if gotRaw != "" {
|
||||
t.Errorf("expected empty from raw, got %q", gotRaw)
|
||||
}
|
||||
|
||||
// Typed fallback should succeed
|
||||
gotTyped := extractThreadIDFromParsed(parsed)
|
||||
if gotTyped != "$root123" {
|
||||
t.Errorf("expected $root123 from typed fallback, got %q", gotTyped)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user