-
-
Notifications
You must be signed in to change notification settings - Fork 37
feat: Improve agent mode message formatting #150
base: main
Are you sure you want to change the base?
Changes from 2 commits
73839ae
ccbd161
953bdeb
ce251c2
b6ea96c
2e8b7d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,11 @@ package slackbot | |
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "github.com/tmc/langchaingo/callbacks" | ||
| "regexp" | ||
|
|
||
| "github.com/slack-go/slack" | ||
| ) | ||
|
|
||
| type sendMessageFunc func(message string) | ||
|
|
@@ -15,7 +19,49 @@ type agentCallbackHandler struct { | |
| func (handler *agentCallbackHandler) HandleChainEnd(_ context.Context, outputs map[string]any) { | ||
| if text, ok := outputs["text"]; ok { | ||
| if textStr, ok := text.(string); ok { | ||
| if isThinkingMessage(textStr) { | ||
| textStr = formatContextMessageBlock(textStr) | ||
| } else { | ||
| textStr = formatFinalResponse(textStr) | ||
| } | ||
| handler.sendMessage(textStr) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| var ( | ||
| thinkingPattern = regexp.MustCompile(`Do I need to use a tool\? Yes`) | ||
| cleanupPattern = regexp.MustCompile(`(Do I need to use a tool\? No|AI:)`) | ||
| ) | ||
|
|
||
| func isThinkingMessage(msg string) bool { | ||
| return thinkingPattern.MatchString(msg) | ||
| } | ||
|
|
||
| // formatFinalResponse removes LLM agent response prefixes. | ||
| // The agent response format is defined in internal/llm/langchain.go | ||
| // > Thought: Do I need to use a tool? No | ||
| // > AI: [your response here] | ||
| func formatFinalResponse(msg string) string { | ||
| maxRemoves := 2 | ||
| return cleanupPattern.ReplaceAllStringFunc(msg, func(s string) string { | ||
| if maxRemoves > 0 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The maxRemoves counter works correctly here since it's declared inside the function and gets reset on each call. However, the intent is unclear—why limit to 2 removals? If a message legitimately contains multiple AI: prefixes, only the first two get removed. Consider adding a comment explaining this limit, or use |
||
| maxRemoves-- | ||
| return "" | ||
| } | ||
| return s | ||
| }) | ||
| } | ||
|
|
||
| func formatContextMessageBlock(message string) string { | ||
| mrkdwnBlock := slack.NewTextBlockObject("mrkdwn", message, false, false) | ||
| contextBlock := slack.NewContextBlock("", []slack.MixedElement{mrkdwnBlock}...) | ||
| blockMessage := slack.NewBlockMessage(contextBlock) | ||
|
|
||
| jsonByte, err := json.Marshal(blockMessage) | ||
| if err != nil { | ||
| // Fallback to plain message if marshaling fails | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This silently swallows the error. Consider logging it so you know when marshaling fails. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ Added |
||
| return message | ||
| } | ||
| return string(jsonByte) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -113,9 +113,12 @@ func FormatMessage(text string, options FormatOptions) []slack.MsgOption { | |
| case "divider": | ||
| slackBlock = slack.NewDividerBlock() | ||
| case "context": | ||
| var context slack.ContextBlock | ||
| if err := json.Unmarshal(blockJSON, &context); err == nil { | ||
| slackBlock = context | ||
| var contextBlock struct { | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice fix! The previous code tried to unmarshal directly into |
||
| Elements slack.ContextElements `json:"elements"` | ||
| } | ||
|
|
||
| if err := json.Unmarshal(blockJSON, &contextBlock); err == nil { | ||
| slackBlock = slack.NewContextBlock("", contextBlock.Elements.Elements...) | ||
| } | ||
| // Add more block types as needed | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add unit tests for the new functions? Specifically:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅ Added
internal/slack/agentCallbackHandler_test.gocoveringisThinkingMessage,formatFinalResponse, and verifying the JSON produced byformatContextMessageBlock.