-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathformat_tool_call.go
More file actions
157 lines (129 loc) · 4.49 KB
/
format_tool_call.go
File metadata and controls
157 lines (129 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package msgfmt
import (
"strings"
)
type toolCallRange struct {
start int
end int
malformed bool
}
func removeClaudeReportTaskToolCall(msg string) (string, []string) {
msg = "\n" + msg // This handles the case where the message starts with a tool call
// Remove all tool calls that start with `● coder - coder_report_task (MCP)`
lines := strings.Split(msg, "\n")
toolCallStartIdx := -1
newLineAfterToolCall := -1
// Store all tool call start and end indices [[start, end], ...]
var toolCallIdxs []toolCallRange
for i := 1; i < len(lines)-1; i++ {
prevLine := strings.TrimSpace(lines[i-1])
line := strings.Trim(strings.TrimSpace(lines[i]), "\n")
nextLine := strings.TrimSpace(lines[i+1])
if strings.Contains(line, "coder - coder_report_task (MCP)") {
toolCallStartIdx = i
} else if toolCallStartIdx != -1 {
if line == "\"message\": \"Thanks for reporting!\"" && nextLine == "}" && strings.HasSuffix(prevLine, "{") {
// Store [start, end] pair
toolCallIdxs = append(toolCallIdxs, toolCallRange{toolCallStartIdx, min(len(lines), i+2), false})
// Reset to find the next tool call
toolCallStartIdx = -1
newLineAfterToolCall = -1
} else if len(line) == 0 {
newLineAfterToolCall = i + 1
}
}
}
// Handle the malformed/partially rendered tool_calls
if toolCallStartIdx != -1 {
if newLineAfterToolCall != -1 {
toolCallIdxs = append(toolCallIdxs, toolCallRange{toolCallStartIdx, newLineAfterToolCall, true})
} else {
toolCallIdxs = append(toolCallIdxs, toolCallRange{toolCallStartIdx, len(lines), true})
}
}
// If no tool calls found, return original message
if len(toolCallIdxs) == 0 {
return strings.TrimLeft(msg, "\n"), []string{}
}
toolCallMessages := make([]string, 0)
// Remove tool calls from the message
for i := len(toolCallIdxs) - 1; i >= 0; i-- {
start, end := toolCallIdxs[i].start, toolCallIdxs[i].end
// If the toolCall is malformed, we don't want to log it
if !toolCallIdxs[i].malformed {
toolCallMessages = append(toolCallMessages, strings.Join(lines[start:end], "\n"))
}
lines = append(lines[:start], lines[end:]...)
}
return strings.TrimLeft(strings.Join(lines, "\n"), "\n"), toolCallMessages
}
func removeCodexReportTaskToolCall(msg string) (string, []string) {
lines := strings.Split(msg, "\n")
toolCallStartIdx := -1
// Store all tool call start and end indices [[start, end], ...]
var toolCallIdxs [][]int
for idx := 0; idx < len(lines); idx++ {
line := strings.TrimSpace(lines[idx])
// Check for tool call start (requires looking at next line)
if idx+1 < len(lines) {
nextLine := strings.TrimSpace(lines[idx+1])
if strings.Replace(line, " ", "", -1) == "•Called" && strings.Contains(nextLine, "Coder.coder_report_task") {
toolCallStartIdx = idx
}
}
// Check for tool call end
if toolCallStartIdx != -1 && line == "{\"message\": \"Thanks for reporting!\"}" {
// Find the end of trailing empty lines after tool call
endIdx := idx + 1
for endIdx < len(lines) && strings.TrimSpace(lines[endIdx]) == "" {
endIdx++
}
toolCallIdxs = append(toolCallIdxs, []int{toolCallStartIdx, endIdx})
// Reset to find the next tool call
toolCallStartIdx = -1
}
}
// If no tool calls found, return original message
if len(toolCallIdxs) == 0 {
return strings.TrimRight(msg, "\n"), []string{}
}
toolCallMessages := make([]string, 0)
// Remove tool calls from the message
for i := len(toolCallIdxs) - 1; i >= 0; i-- {
idxPair := toolCallIdxs[i]
start, end := idxPair[0], idxPair[1]
toolCallMessages = append(toolCallMessages, strings.Join(lines[start:end], "\n"))
lines = append(lines[:start], lines[end:]...)
}
return strings.TrimRight(strings.Join(lines, "\n"), "\n"), toolCallMessages
}
func FormatToolCall(agentType AgentType, message string) (string, []string) {
switch agentType {
case AgentTypeClaude:
return removeClaudeReportTaskToolCall(message)
case AgentTypeGoose:
return message, []string{}
case AgentTypeAider:
return message, []string{}
case AgentTypeCodex:
return removeCodexReportTaskToolCall(message)
case AgentTypeGemini:
return message, []string{}
case AgentTypeCopilot:
return message, []string{}
case AgentTypeAmp:
return message, []string{}
case AgentTypeCursor:
return message, []string{}
case AgentTypeAuggie:
return message, []string{}
case AgentTypeAmazonQ:
return message, []string{}
case AgentTypeOpencode:
return message, []string{}
case AgentTypeCustom:
return message, []string{}
default:
return message, []string{}
}
}