|
1 | 1 | package msgfmt |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "log/slog" |
4 | 5 | "strings" |
5 | 6 | ) |
6 | 7 |
|
@@ -100,3 +101,57 @@ func removeAmpMessageBox(msg string) string { |
100 | 101 | } |
101 | 102 | return formattedMsg |
102 | 103 | } |
| 104 | + |
| 105 | +func removeClaudeReportTaskToolCall(msg string, logger *slog.Logger) string { |
| 106 | + // Remove all tool calls that start with `● coder - coder_report_task (MCP)` till we encounter the next line starting with ● |
| 107 | + lines := strings.Split(msg, "\n") |
| 108 | + |
| 109 | + toolCallStartIdx := -1 |
| 110 | + newLineAfterToolCallIdx := -1 |
| 111 | + |
| 112 | + // Store all tool call start and end indices [[start, end], ...] |
| 113 | + var toolCallIdxs [][]int |
| 114 | + |
| 115 | + for i := 0; i < len(lines); i++ { |
| 116 | + line := strings.TrimSpace(lines[i]) |
| 117 | + |
| 118 | + if strings.HasPrefix(line, "● coder - coder_report_task (MCP)") { |
| 119 | + toolCallStartIdx = i |
| 120 | + } else if toolCallStartIdx != -1 && strings.HasPrefix(line, "●") { |
| 121 | + // Store [start, end] pair |
| 122 | + toolCallIdxs = append(toolCallIdxs, []int{toolCallStartIdx, i}) |
| 123 | + |
| 124 | + // Reset to find the next tool call |
| 125 | + toolCallStartIdx = -1 |
| 126 | + newLineAfterToolCallIdx = -1 |
| 127 | + } |
| 128 | + if len(line) == 0 && toolCallStartIdx != -1 && newLineAfterToolCallIdx == -1 { |
| 129 | + newLineAfterToolCallIdx = i |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + // Handle the case where the last tool call goes till the end of the message |
| 134 | + // And a failsafe when the next message is not prefixed with ● |
| 135 | + if toolCallStartIdx != -1 && newLineAfterToolCallIdx != -1 { |
| 136 | + toolCallIdxs = append(toolCallIdxs, []int{toolCallStartIdx, newLineAfterToolCallIdx}) |
| 137 | + } |
| 138 | + |
| 139 | + // If no tool calls found, return original message |
| 140 | + if len(toolCallIdxs) == 0 { |
| 141 | + return msg |
| 142 | + } |
| 143 | + |
| 144 | + // Remove tool calls from the message |
| 145 | + for i := len(toolCallIdxs) - 1; i >= 0; i-- { |
| 146 | + idxPair := toolCallIdxs[i] |
| 147 | + start, end := idxPair[0], idxPair[1] |
| 148 | + |
| 149 | + // Capture the tool call content before removing it |
| 150 | + toolCallContent := strings.Join(lines[start:end], "\n") |
| 151 | + logger.Info("Removing tool call", "content", toolCallContent) |
| 152 | + |
| 153 | + lines = append(lines[:start], lines[end:]...) |
| 154 | + } |
| 155 | + |
| 156 | + return strings.Join(lines, "\n") |
| 157 | +} |
0 commit comments