gateway: a conversation with another vendor's call ids ("Bash:0#…") goes on to a Messages upstream with tool_use ids Anthropic takes
Sibling of the previous commit (echo_ts's colon-joined item ids). When a Codex or Chat conversation moves to a Messages upstream, buildAnthropic put each call's id in tool_use.id and tool_result.tool_use_id as it came. Anthropic takes those only of [A-Za-z0-9_-] and refuses the whole request over one with another character, which another vendor's calls can have (Devin's "Bash:0#a65b…", #1304; a relay's or a plugin's). Both now go through anthropicOutID, the encoding Anthropic clients already get (mp_ + base64url, stateless, the same on every request), so a call and its result keep matching; a safe id (toolu_…, call_…) goes byte for byte, and an id a Claude Code client sent back as mp_… is decoded on the way in and encoded the same way out. Test: TestAnthropicUpstreamTakesAnotherVendorsCallIDs (Codex input with Devin's literal call id and a toolu_ one, to a Messages upstream). On the old code it fails: tool_use "Bash:0#a65b6a5e02194b87bbc796c4428c946d". Run: go test -tags nogui ./... (sandbox HOME, pinned caches; every package ok but internal/gateway, which hit the 10m default at load average ~145, then passed alone with -timeout 40m in 683s), gofmt -l, GOOS=linux / GOOS=windows go vet -tags nogui ./internal/gateway/. Not run against Anthropic's real API.
This commit is contained in:
@@ -674,7 +674,10 @@ func buildAnthropic(r *Request, model string) []byte {
|
||||
case Image:
|
||||
rest = append(rest, imageBlock(p))
|
||||
case ToolCall:
|
||||
rest = append(rest, aBlock{Type: "tool_use", ID: p.ID, Name: p.Name, Input: argsOf(p)})
|
||||
// Anthropic takes a tool_use id only of [A-Za-z0-9_-]:
|
||||
// another vendor's call (Devin's "Bash:0#…") goes as
|
||||
// anthropicOutID makes it, its result named the same
|
||||
rest = append(rest, aBlock{Type: "tool_use", ID: anthropicOutID(p.ID), Name: p.Name, Input: argsOf(p)})
|
||||
case ToolResult:
|
||||
c, _ := json.Marshal(p.Text)
|
||||
if len(p.Images) > 0 {
|
||||
@@ -688,7 +691,7 @@ func buildAnthropic(r *Request, model string) []byte {
|
||||
}
|
||||
c, _ = json.Marshal(blocks)
|
||||
}
|
||||
results = append(results, aBlock{Type: "tool_result", ToolUseID: p.CallID, Content: c, IsError: p.IsError})
|
||||
results = append(results, aBlock{Type: "tool_result", ToolUseID: anthropicOutID(p.CallID), Content: c, IsError: p.IsError})
|
||||
case Thinking:
|
||||
if p.sealedBy(sealAnthropic) {
|
||||
rest = append(rest, aBlock{Type: "redacted_thinking", sealed: p.Sealed})
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// A Codex conversation that had another vendor's tool call, its call_id
|
||||
// with a character Anthropic doesn't take (Devin's "Bash:0#…", #1304), went
|
||||
// on to a Messages upstream with that id as the tool_use's, which Anthropic
|
||||
// turns away ("tool_use.id: String should match pattern
|
||||
// '^[a-zA-Z0-9_-]+$'"). It goes as one Anthropic takes, its result named
|
||||
// the same, and a safe id goes as it was.
|
||||
func TestAnthropicUpstreamTakesAnotherVendorsCallIDs(t *testing.T) {
|
||||
f := &fake{t: t, ctype: "application/json", reply: `{"id":"msg_1","type":"message","role":"assistant","model":"m1","content":[{"type":"text","text":"ok"}],"stop_reason":"end_turn","usage":{"input_tokens":1,"output_tokens":1}}`}
|
||||
setup(t, provider.Anthropic, f)
|
||||
codexPostTo(New(), `{"model":"fake/m1","input":[
|
||||
{"type":"message","role":"user","content":[{"type":"input_text","text":"list files"}]},
|
||||
{"type":"function_call","call_id":"`+devinRawID+`","name":"shell","arguments":"{\"cmd\":\"ls\"}"},
|
||||
{"type":"function_call_output","call_id":"`+devinRawID+`","output":"main.go"},
|
||||
{"type":"function_call","call_id":"toolu_01AbC","name":"shell","arguments":"{\"cmd\":\"pwd\"}"},
|
||||
{"type":"function_call_output","call_id":"toolu_01AbC","output":"/src"}]}`)
|
||||
var q struct {
|
||||
Messages []struct {
|
||||
Content []struct {
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
ToolUseID string `json:"tool_use_id"`
|
||||
} `json:"content"`
|
||||
} `json:"messages"`
|
||||
}
|
||||
if err := json.Unmarshal(f.got, &q); err != nil {
|
||||
t.Fatalf("%v: %s", err, f.got)
|
||||
}
|
||||
ok := regexp.MustCompile(`^[a-zA-Z0-9_-]+$`)
|
||||
var uses, results []string
|
||||
for _, m := range q.Messages {
|
||||
for _, c := range m.Content {
|
||||
switch c.Type {
|
||||
case "tool_use":
|
||||
uses = append(uses, c.ID)
|
||||
case "tool_result":
|
||||
results = append(results, c.ToolUseID)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(uses) != 2 || len(results) != 2 {
|
||||
t.Fatalf("tool_use %v, tool_result %v: %s", uses, results, f.got)
|
||||
}
|
||||
for i := range uses {
|
||||
if !ok.MatchString(uses[i]) || uses[i] != results[i] {
|
||||
t.Errorf("tool_use %q, its result %q", uses[i], results[i])
|
||||
}
|
||||
}
|
||||
if uses[1] != "toolu_01AbC" {
|
||||
t.Errorf("safe id sent as %q", uses[1])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user