mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
609ca49cf5
* feat(channel): add Matrix adapter support * fix(channel): prevent reasoning leaks in Matrix replies * fix(channel): persist Matrix sync cursors * fix(channel): improve Matrix markdown rendering * fix(channel): support Matrix attachments and multimodal history * fix(channel): expand Matrix reply media context * fix(handlers): allow media downloads for chat-access bots * fix(channel): classify Matrix DMs as direct chats * fix(channel): auto-join Matrix room invites * fix(channel): resolve Matrix room aliases for outbound send * fix(web): use Matrix brand icon in channel badges Replace the generic Matrix hashtag badge with the official brand asset so channel badges feel recognizable and fit the circular mask cleanly. * fix(channel): add Matrix room whitelist controls Let Matrix bots decide whether to auto-join invites and restrict inbound activity to allowed rooms or aliases. Expose the new controls in the web settings UI with line-based whitelist input so access rules stay explicit. * fix(channel): stabilize Matrix multimodal follow-ups and settings * fix(flow): avoid gosec panic on byte decoding * fix: fix golangci-lint * fix(channel): remove Matrix built-in ACL * fix(channel): preserve Matrix image captions * fix(channel): validate Matrix homeserver and sync access Fail Matrix connections early when the homeserver, access token, or /sync capability is misconfigured so bot health checks surface actionable errors. * fix(channel): preserve optional toggles and relax Matrix startup validation * fix(channel): tighten Matrix mention fallback parsing * fix(flow): skip structured assistant tool-call outputs * fix(flow): resolve merged resolver duplication Keep the internal agent resolver implementation after merging main so split helper files do not redeclare flow symbols. Restore user message normalization in sanitize and persistence paths to keep flow tests and command packages building. * fix(flow): remove unused merged resolver helper Drop the leftover truncate helper and import from the resolver merge fix so golangci-lint passes again without affecting flow behavior. --------- Co-authored-by: Acbox Liu <acbox0328@gmail.com>
232 lines
5.1 KiB
Go
232 lines
5.1 KiB
Go
package matrix
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/memohai/memoh/internal/channel"
|
|
)
|
|
|
|
type matrixOutboundStream struct {
|
|
adapter *MatrixAdapter
|
|
cfg Config
|
|
target string
|
|
reply *channel.ReplyRef
|
|
|
|
closed atomic.Bool
|
|
mu sync.Mutex
|
|
|
|
roomID string
|
|
originalEventID string
|
|
rawBuffer strings.Builder
|
|
lastText string
|
|
lastFormat channel.MessageFormat
|
|
lastEditedAt time.Time
|
|
}
|
|
|
|
func (s *matrixOutboundStream) Push(ctx context.Context, event channel.StreamEvent) error {
|
|
if s == nil || s.adapter == nil {
|
|
return errors.New("matrix stream not configured")
|
|
}
|
|
if s.closed.Load() {
|
|
return errors.New("matrix stream is closed")
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
default:
|
|
}
|
|
|
|
switch event.Type {
|
|
case channel.StreamEventStatus,
|
|
channel.StreamEventPhaseStart,
|
|
channel.StreamEventToolCallEnd,
|
|
channel.StreamEventAgentStart,
|
|
channel.StreamEventAgentEnd,
|
|
channel.StreamEventProcessingStarted,
|
|
channel.StreamEventProcessingCompleted,
|
|
channel.StreamEventProcessingFailed:
|
|
return nil
|
|
case channel.StreamEventPhaseEnd:
|
|
if event.Phase != channel.StreamPhaseText {
|
|
return nil
|
|
}
|
|
s.mu.Lock()
|
|
text := strings.TrimSpace(s.rawBuffer.String())
|
|
s.mu.Unlock()
|
|
return s.upsertText(ctx, text, channel.MessageFormatPlain, true)
|
|
case channel.StreamEventToolCallStart:
|
|
s.resetMessageState()
|
|
return nil
|
|
case channel.StreamEventDelta:
|
|
if event.Phase == channel.StreamPhaseReasoning || event.Delta == "" {
|
|
return nil
|
|
}
|
|
s.mu.Lock()
|
|
s.rawBuffer.WriteString(event.Delta)
|
|
s.mu.Unlock()
|
|
return nil
|
|
case channel.StreamEventError:
|
|
errText := strings.TrimSpace(event.Error)
|
|
if errText == "" {
|
|
return nil
|
|
}
|
|
return s.upsertText(ctx, "Error: "+errText, channel.MessageFormatPlain, true)
|
|
case channel.StreamEventAttachment:
|
|
return s.pushAttachments(ctx, event.Attachments)
|
|
case channel.StreamEventFinal:
|
|
if event.Final == nil {
|
|
return errors.New("matrix stream final payload is required")
|
|
}
|
|
text := strings.TrimSpace(event.Final.Message.PlainText())
|
|
format := event.Final.Message.Format
|
|
if format == "" {
|
|
format = channel.MessageFormatPlain
|
|
}
|
|
if text == "" {
|
|
s.mu.Lock()
|
|
text = strings.TrimSpace(s.rawBuffer.String())
|
|
s.mu.Unlock()
|
|
}
|
|
if err := s.upsertText(ctx, text, format, true); err != nil {
|
|
return err
|
|
}
|
|
if err := s.pushAttachments(ctx, event.Final.Message.Attachments); err != nil {
|
|
return err
|
|
}
|
|
s.resetMessageState()
|
|
return nil
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (s *matrixOutboundStream) Close(ctx context.Context) error {
|
|
if s == nil {
|
|
return nil
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
default:
|
|
}
|
|
s.closed.Store(true)
|
|
return nil
|
|
}
|
|
|
|
func (s *matrixOutboundStream) upsertText(ctx context.Context, text string, format channel.MessageFormat, force bool) error {
|
|
text = strings.TrimSpace(text)
|
|
if text == "" {
|
|
return nil
|
|
}
|
|
if format == "" {
|
|
format = channel.MessageFormatPlain
|
|
}
|
|
|
|
s.mu.Lock()
|
|
roomID := s.roomID
|
|
originalEventID := s.originalEventID
|
|
lastText := s.lastText
|
|
lastFormat := s.lastFormat
|
|
lastEditedAt := s.lastEditedAt
|
|
reply := s.reply
|
|
s.mu.Unlock()
|
|
|
|
if roomID == "" {
|
|
resolvedRoomID, err := s.adapter.resolveRoomTarget(ctx, s.cfg, s.target)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
roomID = resolvedRoomID
|
|
s.mu.Lock()
|
|
s.roomID = resolvedRoomID
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
if originalEventID == "" {
|
|
eventID, err := s.adapter.sendTextEvent(ctx, s.cfg, roomID, buildMatrixMessageContent(channel.Message{
|
|
Text: text,
|
|
Format: format,
|
|
Reply: reply,
|
|
}, false, ""))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.mu.Lock()
|
|
s.originalEventID = eventID
|
|
s.lastText = text
|
|
s.lastFormat = format
|
|
s.lastEditedAt = time.Now()
|
|
s.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
if text == lastText && format == lastFormat {
|
|
return nil
|
|
}
|
|
if !force && time.Since(lastEditedAt) < matrixEditThrottle {
|
|
return nil
|
|
}
|
|
_, err := s.adapter.sendTextEvent(ctx, s.cfg, roomID, buildMatrixMessageContent(channel.Message{
|
|
Text: text,
|
|
Format: format,
|
|
}, true, originalEventID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.mu.Lock()
|
|
s.lastText = text
|
|
s.lastFormat = format
|
|
s.lastEditedAt = time.Now()
|
|
s.mu.Unlock()
|
|
return nil
|
|
}
|
|
|
|
func (s *matrixOutboundStream) resetMessageState() {
|
|
s.mu.Lock()
|
|
s.originalEventID = ""
|
|
s.rawBuffer.Reset()
|
|
s.lastText = ""
|
|
s.lastFormat = ""
|
|
s.lastEditedAt = time.Time{}
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
func (s *matrixOutboundStream) pushAttachments(ctx context.Context, attachments []channel.Attachment) error {
|
|
if len(attachments) == 0 {
|
|
return nil
|
|
}
|
|
|
|
s.mu.Lock()
|
|
roomID := s.roomID
|
|
originalEventID := s.originalEventID
|
|
reply := s.reply
|
|
s.mu.Unlock()
|
|
|
|
if roomID == "" {
|
|
resolvedRoomID, err := s.adapter.resolveRoomTarget(ctx, s.cfg, s.target)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
roomID = resolvedRoomID
|
|
s.mu.Lock()
|
|
s.roomID = resolvedRoomID
|
|
s.mu.Unlock()
|
|
}
|
|
|
|
for idx, att := range attachments {
|
|
mediaMsg := channel.Message{}
|
|
if idx == 0 && originalEventID == "" {
|
|
mediaMsg.Reply = reply
|
|
}
|
|
if err := s.adapter.sendMediaAttachment(ctx, s.cfg, roomID, "", mediaMsg, att); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|