|
| 1 | +package msgfmt |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | +) |
| 6 | + |
| 7 | +func removeClaudeReportTaskToolCall(msg string) (string, []string) { |
| 8 | + msg = "\n" + msg // This handles the case where the message starts with a tool call |
| 9 | + |
| 10 | + // Remove all tool calls that start with `● coder - coder_report_task (MCP)` |
| 11 | + lines := strings.Split(msg, "\n") |
| 12 | + |
| 13 | + toolCallStartIdx := -1 |
| 14 | + |
| 15 | + // Store all tool call start and end indices [[start, end], ...] |
| 16 | + var toolCallIdxs [][]int |
| 17 | + |
| 18 | + for i := 1; i < len(lines)-1; i++ { |
| 19 | + prevLine := strings.TrimSpace(lines[i-1]) |
| 20 | + line := strings.TrimSpace(lines[i]) |
| 21 | + nextLine := strings.TrimSpace(lines[i+1]) |
| 22 | + |
| 23 | + if strings.Contains(line, "coder - coder_report_task (MCP)") { |
| 24 | + toolCallStartIdx = i |
| 25 | + } else if toolCallStartIdx != -1 && line == "\"message\": \"Thanks for reporting!\"" && nextLine == "}" && strings.HasSuffix(prevLine, "{") { |
| 26 | + // Store [start, end] pair |
| 27 | + toolCallIdxs = append(toolCallIdxs, []int{toolCallStartIdx, min(len(lines), i+2)}) |
| 28 | + |
| 29 | + // Reset to find the next tool call |
| 30 | + toolCallStartIdx = -1 |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // If no tool calls found, return original message |
| 35 | + if len(toolCallIdxs) == 0 { |
| 36 | + return strings.TrimLeft(msg, "\n"), []string{} |
| 37 | + } |
| 38 | + |
| 39 | + toolCallMessages := make([]string, 0) |
| 40 | + |
| 41 | + // Remove tool calls from the message |
| 42 | + for i := len(toolCallIdxs) - 1; i >= 0; i-- { |
| 43 | + idxPair := toolCallIdxs[i] |
| 44 | + start, end := idxPair[0], idxPair[1] |
| 45 | + |
| 46 | + toolCallMessages = append(toolCallMessages, strings.Join(lines[start:end], "\n")) |
| 47 | + |
| 48 | + lines = append(lines[:start], lines[end:]...) |
| 49 | + } |
| 50 | + return strings.TrimLeft(strings.Join(lines, "\n"), "\n"), toolCallMessages |
| 51 | +} |
| 52 | + |
| 53 | +func FormatToolCall(agentType AgentType, message string) (string, []string) { |
| 54 | + switch agentType { |
| 55 | + case AgentTypeClaude: |
| 56 | + return removeClaudeReportTaskToolCall(message) |
| 57 | + case AgentTypeGoose: |
| 58 | + return message, []string{} |
| 59 | + case AgentTypeAider: |
| 60 | + return message, []string{} |
| 61 | + case AgentTypeCodex: |
| 62 | + return message, []string{} |
| 63 | + case AgentTypeGemini: |
| 64 | + return message, []string{} |
| 65 | + case AgentTypeCopilot: |
| 66 | + return message, []string{} |
| 67 | + case AgentTypeAmp: |
| 68 | + return message, []string{} |
| 69 | + case AgentTypeCursor: |
| 70 | + return message, []string{} |
| 71 | + case AgentTypeAuggie: |
| 72 | + return message, []string{} |
| 73 | + case AgentTypeAmazonQ: |
| 74 | + return message, []string{} |
| 75 | + case AgentTypeOpencode: |
| 76 | + return message, []string{} |
| 77 | + case AgentTypeCustom: |
| 78 | + return message, []string{} |
| 79 | + default: |
| 80 | + return message, []string{} |
| 81 | + } |
| 82 | +} |
0 commit comments