Skip to content

Commit 7838abc

Browse files
authored
fix: correct anthropic blocking response marshaling (#71)
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent d62a713 commit 7838abc

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

intercept_anthropic_messages_blocking.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package aibridge
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"net/http"
76
"time"
@@ -10,6 +9,7 @@ import (
109
"github.com/anthropics/anthropic-sdk-go/option"
1110
"github.com/google/uuid"
1211
mcplib "github.com/mark3labs/mcp-go/mcp" // TODO: abstract this away so callers need no knowledge of underlying lib.
12+
"github.com/tidwall/sjson"
1313

1414
"github.com/coder/aibridge/mcp"
1515

@@ -268,20 +268,21 @@ func (i *AnthropicMessagesBlockingInterception) ProcessRequest(w http.ResponseWr
268268
return nil
269269
}
270270

271-
resp.Usage = cumulativeUsage
272-
273271
// Overwrite response identifier since proxy obscures injected tool call invocations.
274-
resp.ID = i.ID().String()
272+
sj, err := sjson.Set(resp.RawJSON(), "id", i.ID().String())
273+
if err != nil {
274+
return fmt.Errorf("marshal response id failed: %w", err)
275+
}
275276

276-
out, err := json.Marshal(resp)
277+
// Overwrite the response's usage with the cumulative usage across any inner loops which invokes injected MCP tools.
278+
sj, err = sjson.Set(sj, "usage", cumulativeUsage)
277279
if err != nil {
278-
http.Error(w, "error marshaling response", http.StatusInternalServerError)
279-
return fmt.Errorf("failed to marshal response: %w", err)
280+
return fmt.Errorf("marshal response usage failed: %w", err)
280281
}
281282

282283
w.Header().Set("Content-Type", "application/json")
283284
w.WriteHeader(http.StatusOK)
284-
_, _ = w.Write(out)
285+
_, _ = w.Write([]byte(sj))
285286

286287
return nil
287288
}

0 commit comments

Comments
 (0)