feat: renderizar Markdown a HTML en mensajes Matrix con goldmark

Se reemplaza SendText por SendMarkdown en todos los puntos donde el agente
envía respuestas: runtime.go (comandos built-in y tareas orquestadas),
effects/runner.go (acciones Reply) y tools/matrix.go (matrix_send tool).

shell/matrix/client.go ahora usa goldmark para convertir Markdown a HTML real
en el campo FormattedBody del evento Matrix, cumpliendo con la spec de Matrix
para mensajes formateados. El Body conserva el markdown raw como fallback.

Se añade dependencia github.com/yuin/goldmark v1.7.16.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-07 02:21:06 +00:00
parent e2f14e7abb
commit 828eb175fe
6 changed files with 25 additions and 6 deletions
+15 -1
View File
@@ -3,6 +3,7 @@ package matrix
import (
"context"
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
@@ -12,6 +13,7 @@ import (
"path/filepath"
"strings"
"github.com/yuin/goldmark"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/crypto"
"maunium.net/go/mautrix/crypto/cryptohelper"
@@ -269,17 +271,29 @@ func (c *Client) SendText(ctx context.Context, roomID, text string) error {
}
// SendMarkdown sends a formatted (Markdown) message to a room.
// Body contains the raw markdown (plaintext fallback per Matrix spec).
// FormattedBody contains the HTML rendered by goldmark.
func (c *Client) SendMarkdown(ctx context.Context, roomID, markdown string) error {
html := mdToHTML(markdown)
content := event.MessageEventContent{
MsgType: event.MsgText,
Body: markdown,
Format: event.FormatHTML,
FormattedBody: markdown, // mautrix can render markdown -> HTML if needed
FormattedBody: html,
}
_, err := c.raw.SendMessageEvent(ctx, id.RoomID(roomID), event.EventMessage, content)
return err
}
// mdToHTML converts a Markdown string to HTML using goldmark.
func mdToHTML(md string) string {
var buf bytes.Buffer
if err := goldmark.Convert([]byte(md), &buf); err != nil {
return md // fallback to raw markdown on error
}
return buf.String()
}
// SendReaction sends a reaction to an event.
func (c *Client) SendReaction(ctx context.Context, roomID, eventID, reaction string) error {
_, err := c.raw.SendReaction(ctx, id.RoomID(roomID), id.EventID(eventID), reaction)