Repo iniciado
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
// Render executes a Go template string with the given data. Pure.
|
||||
func Render(tmpl string, data any) (string, error) {
|
||||
t, err := template.New("").Parse(tmpl)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
if err := t.Execute(&buf, data); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
// MustRender is like Render but panics on error. Use only in tests.
|
||||
func MustRender(tmpl string, data any) string {
|
||||
s, err := Render(tmpl, data)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// Package message provides pure parsing and formatting for Matrix messages.
|
||||
package message
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/enmanuel/agents/pkg/decision"
|
||||
)
|
||||
|
||||
// ParseOptions configures how messages are parsed.
|
||||
type ParseOptions struct {
|
||||
CommandPrefix string // e.g. "!"
|
||||
BotUserID string // for mention detection
|
||||
}
|
||||
|
||||
// Parse converts a raw Matrix message body into a structured MessageContext. Pure.
|
||||
func Parse(body, senderID, roomID string, powerLevel int, isDM bool, opts ParseOptions) decision.MessageContext {
|
||||
ctx := decision.MessageContext{
|
||||
SenderID: senderID,
|
||||
RoomID: roomID,
|
||||
Content: body,
|
||||
PowerLevel: powerLevel,
|
||||
IsDirectMsg: isDM,
|
||||
}
|
||||
|
||||
// Detect mention
|
||||
if opts.BotUserID != "" && strings.Contains(body, opts.BotUserID) {
|
||||
ctx.IsMention = true
|
||||
body = strings.ReplaceAll(body, opts.BotUserID, "")
|
||||
body = strings.TrimSpace(body)
|
||||
}
|
||||
|
||||
// Parse command
|
||||
if opts.CommandPrefix != "" && strings.HasPrefix(body, opts.CommandPrefix) {
|
||||
parts := strings.Fields(strings.TrimPrefix(body, opts.CommandPrefix))
|
||||
if len(parts) > 0 {
|
||||
ctx.Command = strings.ToLower(parts[0])
|
||||
ctx.Args = parts[1:]
|
||||
}
|
||||
}
|
||||
|
||||
return ctx
|
||||
}
|
||||
Reference in New Issue
Block a user