From b6b5bfee48f1a0d0d9bd503f4ce34afdce0bdf3e Mon Sep 17 00:00:00 2001 From: DBT Date: Mon, 23 Feb 2026 16:40:50 +0000 Subject: [PATCH] add reply-tag metadata support and externalize system rewrite/dialog strings --- config.example.json | 1 + pkg/agent/loop.go | 46 ++++++++++++++++++++++++++++---------------- pkg/bus/types.go | 9 +++++---- pkg/config/config.go | 2 ++ 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/config.example.json b/config.example.json index 8e3d9da..13d171c 100644 --- a/config.example.json +++ b/config.example.json @@ -23,6 +23,7 @@ "subagents_none": "No subagents.", "sessions_none": "No sessions.", "unsupported_action": "unsupported action", + "system_rewrite_template": "Rewrite the following internal system update in concise user-facing language:\n\n%s", "runtime_compaction_note": "[runtime-compaction] removed %d old messages, kept %d recent messages", "startup_compaction_note": "[startup-compaction] removed %d old messages, kept %d recent messages" }, diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index 1d0671b..5c76951 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -45,9 +45,7 @@ type AgentLoop struct { langUpdatedTemplate string runtimeCompactionNote string startupCompactionNote string - toolNoSubagents string - toolNoSessions string - toolUnsupportedAction string + systemRewriteTemplate string audit *triggerAudit running bool } @@ -169,6 +167,7 @@ func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers langUpdatedTemplate: cfg.Agents.Defaults.Texts.LangUpdatedTemplate, runtimeCompactionNote: cfg.Agents.Defaults.Texts.RuntimeCompactionNote, startupCompactionNote: cfg.Agents.Defaults.Texts.StartupCompactionNote, + systemRewriteTemplate: cfg.Agents.Defaults.Texts.SystemRewriteTemplate, audit: newTriggerAudit(workspace), running: false, } @@ -206,14 +205,15 @@ func (al *AgentLoop) Run(ctx context.Context) error { if shouldDropNoReply(response) { suppressed = true } else { - clean := stripReplyTags(response) + clean, replyToID := parseReplyTag(response) if al.shouldSuppressOutbound(msg, clean) { suppressed = true } else { al.bus.PublishOutbound(bus.OutboundMessage{ - Channel: msg.Channel, - ChatID: msg.ChatID, - Content: clean, + Channel: msg.Channel, + ChatID: msg.ChatID, + Content: clean, + ReplyToID: replyToID, }) } } @@ -564,7 +564,7 @@ func (al *AgentLoop) processSystemMessage(ctx context.Context, msg bus.InboundMe "chat_id": msg.ChatID, }) - msg.Content = rewriteSystemMessageContent(msg.Content) + msg.Content = rewriteSystemMessageContent(msg.Content, al.systemRewriteTemplate) // Parse origin from chat_id (format: "channel:chat_id") var originChannel, originChatID string @@ -931,23 +931,28 @@ func shouldDropNoReply(text string) bool { return strings.EqualFold(t, "NO_REPLY") } -func stripReplyTags(text string) string { +func parseReplyTag(text string) (content string, replyToID string) { t := strings.TrimSpace(text) if !strings.HasPrefix(t, "[[") { - return text + return text, "" } end := strings.Index(t, "]]") if end <= 0 { - return text + return text, "" } - tag := strings.ToLower(strings.TrimSpace(t[2:end])) - if strings.HasPrefix(tag, "reply_to_current") || strings.HasPrefix(tag, "reply_to:") || strings.HasPrefix(tag, "reply_to") { - return strings.TrimSpace(t[end+2:]) + rawTag := strings.TrimSpace(t[2:end]) + tag := strings.ToLower(rawTag) + if strings.HasPrefix(tag, "reply_to_current") || strings.HasPrefix(tag, "reply_to") { + content = strings.TrimSpace(t[end+2:]) + if strings.HasPrefix(tag, "reply_to:") { + replyToID = strings.TrimSpace(rawTag[len("reply_to:"):]) + } + return content, replyToID } - return text + return text, "" } -func rewriteSystemMessageContent(content string) string { +func rewriteSystemMessageContent(content, template string) string { c := strings.TrimSpace(content) if !strings.HasPrefix(c, "[System Message]") { return content @@ -956,7 +961,14 @@ func rewriteSystemMessageContent(content string) string { if body == "" { return "Please summarize the system event in concise user-facing language." } - return "Rewrite the following internal system update in concise user-facing language:\n\n" + body + tpl := strings.TrimSpace(template) + if tpl == "" { + tpl = "Rewrite the following internal system update in concise user-facing language:\n\n%s" + } + if strings.Contains(tpl, "%s") { + return fmt.Sprintf(tpl, body) + } + return tpl + "\n\n" + body } func alSessionListForTool(sm *session.SessionManager, limit int) []tools.SessionInfo { diff --git a/pkg/bus/types.go b/pkg/bus/types.go index 0840199..bad831a 100644 --- a/pkg/bus/types.go +++ b/pkg/bus/types.go @@ -16,10 +16,11 @@ type Button struct { } type OutboundMessage struct { - Channel string `json:"channel"` - ChatID string `json:"chat_id"` - Content string `json:"content"` - Buttons [][]Button `json:"buttons,omitempty"` + Channel string `json:"channel"` + ChatID string `json:"chat_id"` + Content string `json:"content"` + ReplyToID string `json:"reply_to_id,omitempty"` + Buttons [][]Button `json:"buttons,omitempty"` } type MessageHandler func(InboundMessage) error diff --git a/pkg/config/config.go b/pkg/config/config.go index ee6ad96..ce259ce 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -53,6 +53,7 @@ type AgentTextConfig struct { SubagentsNone string `json:"subagents_none"` SessionsNone string `json:"sessions_none"` UnsupportedAction string `json:"unsupported_action"` + SystemRewriteTemplate string `json:"system_rewrite_template"` RuntimeCompactionNote string `json:"runtime_compaction_note"` StartupCompactionNote string `json:"startup_compaction_note"` } @@ -293,6 +294,7 @@ func DefaultConfig() *Config { SubagentsNone: "No subagents.", SessionsNone: "No sessions.", UnsupportedAction: "unsupported action", + SystemRewriteTemplate: "Rewrite the following internal system update in concise user-facing language:\n\n%s", RuntimeCompactionNote: "[runtime-compaction] removed %d old messages, kept %d recent messages", StartupCompactionNote: "[startup-compaction] removed %d old messages, kept %d recent messages", },