// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
// Copyright (C) 2026 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see %[1]s`, html.EscapeString(meta.LatexExpression)),
)
default:
text.Text = strings.ReplaceAll(
text.Text,
fmt.Sprintf("{{%s}}", ent.Key),
ent.Key,
)
}
}
return mdRender.Convert([]byte(text.Text), buf)
}
func (mc *MessageConverter) convertRichResponsePrimitive(ctx context.Context, primitive richresponse.Primitive, buf *strings.Builder) error {
switch p := primitive.(type) {
case *richresponse.GenAICodeUXPrimitive:
if p.Language != "" {
_, _ = fmt.Fprintf(buf, `
`, html.EscapeString(p.Language))
} else {
buf.WriteString("")
}
for _, part := range p.CodeBlocks {
buf.WriteString(html.EscapeString(part.Content))
}
buf.WriteString("
")
case *richresponse.GenAIMarkdownTextUXPrimitive:
return mc.convertRichMarkdownText(ctx, p, buf)
case *richresponse.GenATableUXPrimitive:
buf.WriteString("")
for _, row := range p.Rows {
buf.WriteString("")
cellType := "td"
if row.IsHeader {
cellType = "th"
}
if len(row.MarkdownCells) > 0 {
for _, cell := range row.MarkdownCells {
_, _ = fmt.Fprintf(buf, "<%s>", cellType)
err := mc.convertRichMarkdownText(ctx, &cell, buf)
if err != nil {
return err
}
_, _ = fmt.Fprintf(buf, "%s>", cellType)
}
} else {
for _, cell := range row.Cells {
_, _ = fmt.Fprintf(buf, "<%s>%s%s>", cellType, html.EscapeString(cell), cellType)
}
}
buf.WriteString(" ")
}
buf.WriteString("
")
case *richresponse.GenAILatexUXPrimitive:
_, _ = fmt.Fprintf(buf, `%[1]s`, html.EscapeString(p.GetLatexExpression()))
case *richresponse.FOATextPrimitive:
// TODO remove placeholders?
return mdRender.Convert([]byte(p.Text), buf)
case *richresponse.GenAIMetadataTextPrimitive:
return mdRender.Convert([]byte(p.Text), buf)
//case *richresponse.GenAIBotThinkingStatusPrimitive:
//case *richresponse.GenAIProductItemCardPrimitive:
//case *richresponse.GenAIImagePrimitive:
//case *richresponse.GenAITaskPrimitive:
//case *richresponse.GenAIReelPrimitive:
//case *richresponse.GenAIPostPrimitive:
//case *richresponse.GenAIImaginePrimitive:
//case *richresponse.GenAISearchResultPrimitive:
//case *richresponse.FOABloksPrimitive:
case *richresponse.GenAIDividerPrimitive:
// TODO dots?
buf.WriteString("
")
case *richresponse.GenAISpacerPrimitive:
if p.Spacing <= 1 {
buf.WriteString("
")
} else {
for range p.Spacing {
buf.WriteString("
")
}
}
default:
buf.WriteString("Unknown primitive type
")
}
return nil
}