@@ -81,19 +81,7 @@ func removeWithToolPairing(messages []model.Message, messageTokens []int, idx in
8181 if ! ok || toolCallID == "" {
8282 continue
8383 }
84- for i , msg := range messages {
85- for _ , part := range msg .Parts {
86- if part .Type == "tool-result" && part .Meta != nil {
87- if id , ok := part .Meta ["tool_call_id" ].(string ); ok && id == toolCallID {
88- if _ , ok := toRemove [i ]; ! ok {
89- toRemove [i ] = struct {}{}
90- queue = append (queue , i )
91- }
92- break
93- }
94- }
95- }
96- }
84+ enqueueToolPair (messages , toolCallID , toRemove , & queue )
9785 case "tool-result" :
9886 if removedPart .Meta == nil {
9987 continue
@@ -102,32 +90,7 @@ func removeWithToolPairing(messages []model.Message, messageTokens []int, idx in
10290 if ! ok || toolCallID == "" {
10391 continue
10492 }
105- for i , msg := range messages {
106- for _ , part := range msg .Parts {
107- if part .Type == "tool-result" && part .Meta != nil {
108- if id , ok := part .Meta ["tool_call_id" ].(string ); ok && id == toolCallID {
109- if _ , ok := toRemove [i ]; ! ok {
110- toRemove [i ] = struct {}{}
111- queue = append (queue , i )
112- }
113- break
114- }
115- }
116- }
117- }
118- for i , msg := range messages {
119- for _ , part := range msg .Parts {
120- if part .Type == "tool-call" && part .Meta != nil {
121- if id , ok := part .Meta ["id" ].(string ); ok && id == toolCallID {
122- if _ , ok := toRemove [i ]; ! ok {
123- toRemove [i ] = struct {}{}
124- queue = append (queue , i )
125- }
126- break
127- }
128- }
129- }
130- }
93+ enqueueToolPair (messages , toolCallID , toRemove , & queue )
13194 }
13295 }
13396 }
@@ -145,6 +108,33 @@ func removeWithToolPairing(messages []model.Message, messageTokens []int, idx in
145108 return out , outTokens , removedTokens
146109}
147110
111+ func enqueueToolPair (messages []model.Message , toolCallID string , toRemove map [int ]struct {}, queue * []int ) {
112+ for i , msg := range messages {
113+ if _ , ok := toRemove [i ]; ok {
114+ continue
115+ }
116+ for _ , part := range msg .Parts {
117+ if part .Meta == nil {
118+ continue
119+ }
120+ if part .Type == "tool-call" {
121+ if id , ok := part .Meta ["id" ].(string ); ok && id == toolCallID {
122+ toRemove [i ] = struct {}{}
123+ * queue = append (* queue , i )
124+ break
125+ }
126+ }
127+ if part .Type == "tool-result" {
128+ if id , ok := part .Meta ["tool_call_id" ].(string ); ok && id == toolCallID {
129+ toRemove [i ] = struct {}{}
130+ * queue = append (* queue , i )
131+ break
132+ }
133+ }
134+ }
135+ }
136+ }
137+
148138func createMiddleOutStrategy (params map [string ]interface {}) (EditStrategy , error ) {
149139 rawTokenReduceTo , ok := params ["token_reduce_to" ]
150140 if ! ok {
0 commit comments