Simplify message chunking logic in channel worker

Extract splitByLength helper function and remove goto-based control
flow.
The logic now flows more naturally - try marker splitting first, then
fall
back to length-based splitting.
This commit is contained in:
uiyzzi 2026-03-26 00:57:56 +08:00
parent aef2494954
commit e04c5378a9

View file

@ -628,44 +628,18 @@ func (m *Manager) runWorker(ctx context.Context, name string, w *channelWorker)
// Collect all message chunks to send // Collect all message chunks to send
var chunks []string var chunks []string
// Step 1: Split by marker if enabled and marker is present // Step 1: Try marker-based splitting if enabled
splitOnMarker := false if m.config != nil && m.config.Agents.Defaults.SplitOnMarker {
if m.config != nil { if markerChunks := SplitByMarker(msg.Content); len(markerChunks) > 1 {
splitOnMarker = m.config.Agents.Defaults.SplitOnMarker
}
if splitOnMarker {
markerChunks := SplitByMarker(msg.Content)
if len(markerChunks) > 1 {
// Marker found, process each chunk
for _, chunk := range markerChunks { for _, chunk := range markerChunks {
// Step 2: Further split by length if needed chunks = append(chunks, splitByLength(chunk, maxLen)...)
if maxLen > 0 && len([]rune(chunk)) > maxLen {
subChunks := SplitMessage(chunk, maxLen)
chunks = append(chunks, subChunks...)
} else {
chunks = append(chunks, chunk)
}
} }
} else {
// No marker found, fall through to length-based splitting
goto lengthSplit
} }
} else {
// Marker disabled, use length-based splitting
goto lengthSplit
} }
lengthSplit: // Step 2: Fallback to length-based splitting if no chunks from marker
// Length-based splitting (also used as fallback when no marker found)
if maxLen > 0 && len([]rune(msg.Content)) > maxLen {
chunks = SplitMessage(msg.Content, maxLen)
} else if len(chunks) == 0 {
chunks = []string{msg.Content}
}
// Fallback: if no chunks collected, use original message
if len(chunks) == 0 { if len(chunks) == 0 {
chunks = []string{msg.Content} chunks = splitByLength(msg.Content, maxLen)
} }
// Step 3: Send all chunks // Step 3: Send all chunks
@ -680,6 +654,14 @@ func (m *Manager) runWorker(ctx context.Context, name string, w *channelWorker)
} }
} }
// splitByLength splits content by maxLen if needed, otherwise returns single chunk.
func splitByLength(content string, maxLen int) []string {
if maxLen > 0 && len([]rune(content)) > maxLen {
return SplitMessage(content, maxLen)
}
return []string{content}
}
// sendWithRetry sends a message through the channel with rate limiting and // sendWithRetry sends a message through the channel with rate limiting and
// retry logic. It classifies errors to determine the retry strategy: // retry logic. It classifies errors to determine the retry strategy:
// - ErrNotRunning / ErrSendFailed: permanent, no retry // - ErrNotRunning / ErrSendFailed: permanent, no retry