Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions intercept_anthropic_messages_blocking.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package aibridge

import (
"encoding/json"
"fmt"
"net/http"
"time"
Expand All @@ -10,6 +9,7 @@ import (
"github.com/anthropics/anthropic-sdk-go/option"
"github.com/google/uuid"
mcplib "github.com/mark3labs/mcp-go/mcp" // TODO: abstract this away so callers need no knowledge of underlying lib.
"github.com/tidwall/sjson"

"github.com/coder/aibridge/mcp"

Expand Down Expand Up @@ -268,20 +268,21 @@ func (i *AnthropicMessagesBlockingInterception) ProcessRequest(w http.ResponseWr
return nil
}

resp.Usage = cumulativeUsage

// Overwrite response identifier since proxy obscures injected tool call invocations.
resp.ID = i.ID().String()
sj, err := sjson.Set(resp.RawJSON(), "id", i.ID().String())
if err != nil {
return fmt.Errorf("marshal response id failed: %w", err)
}

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

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

return nil
}