Merge pull request 'fix(provider): repair control characters in streamed tool arguments' (#1) from fix/resident-journal-invalid-tool-args into main
Reviewed-on: https://git.bnema.dev/bnema/zut/pulls/1
7 files · +178 -25
M packages/provider/amazon_bedrock.go +1 -5
| @@ -733,12 +733,8 @@ func (c *bedrockClient) runStream(ctx context.Context, resp *http.Response, req | |||
| 733 | 733 | continue | |
| 734 | 734 | } | |
| 735 | 735 | if st.isToolUse { | |
| 736 | - | args := st.toolArgs.String() | |
| 737 | - | if args == "" { | |
| 738 | - | args = "{}" | |
| 739 | - | } | |
| 740 | 736 | finalMsg.Content = append(finalMsg.Content, ToolCallBlock{ | |
| 741 | - | ID: st.toolID, Name: st.toolName, Arguments: json.RawMessage(args), | |
| 737 | + | ID: st.toolID, Name: st.toolName, Arguments: normalizeToolArgs(st.toolArgs.String()), | |
| 742 | 738 | }) | |
| 743 | 739 | out <- EventToolEnd{ID: st.toolID} | |
| 744 | 740 | } else if st.text.Len() > 0 { | |
| @@ -733,12 +733,8 @@ func (c *bedrockClient) runStream(ctx context.Context, resp *http.Response, req | |||||
| 733 | continue | 733 | continue | ||
| 734 | } | 734 | } | ||
| 735 | if st.isToolUse { | 735 | if st.isToolUse { | ||
| 736 | - | args := st.toolArgs.String() | |||
| 737 | - | if args == "" { | |||
| 738 | - | args = "{}" | |||
| 739 | - | } | |||
| 740 | finalMsg.Content = append(finalMsg.Content, ToolCallBlock{ | 736 | finalMsg.Content = append(finalMsg.Content, ToolCallBlock{ | ||
| 741 | - | ID: st.toolID, Name: st.toolName, Arguments: json.RawMessage(args), | 737 | + | ID: st.toolID, Name: st.toolName, Arguments: normalizeToolArgs(st.toolArgs.String()), |
| 742 | }) | 738 | }) | ||
| 743 | out <- EventToolEnd{ID: st.toolID} | 739 | out <- EventToolEnd{ID: st.toolID} | ||
| 744 | } else if st.text.Len() > 0 { | 740 | } else if st.text.Len() > 0 { | ||
M packages/provider/anthropic.go +1 -5
| @@ -689,11 +689,7 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re | |||
| 689 | 689 | } | |
| 690 | 690 | case "tool_use": | |
| 691 | 691 | tc := be.toolCall | |
| 692 | - | args := be.toolArgs.String() | |
| 693 | - | if args == "" { | |
| 694 | - | args = "{}" | |
| 695 | - | } | |
| 696 | - | tc.Arguments = json.RawMessage(args) | |
| 692 | + | tc.Arguments = normalizeToolArgs(be.toolArgs.String()) | |
| 697 | 693 | content = append(content, tc) | |
| 698 | 694 | } | |
| 699 | 695 | } | |
| @@ -689,11 +689,7 @@ func (c *anthropicClient) runStream(ctx context.Context, resp *http.Response, re | |||||
| 689 | } | 689 | } | ||
| 690 | case "tool_use": | 690 | case "tool_use": | ||
| 691 | tc := be.toolCall | 691 | tc := be.toolCall | ||
| 692 | - | args := be.toolArgs.String() | 692 | + | tc.Arguments = normalizeToolArgs(be.toolArgs.String()) |
| 693 | - | if args == "" { | |||
| 694 | - | args = "{}" | |||
| 695 | - | } | |||
| 696 | - | tc.Arguments = json.RawMessage(args) | |||
| 697 | content = append(content, tc) | 693 | content = append(content, tc) | ||
| 698 | } | 694 | } | ||
| 699 | } | 695 | } | ||
M packages/provider/gemini.go +1 -5
| @@ -636,14 +636,10 @@ func (c *geminiClient) runStream(ctx context.Context, resp *http.Response, req R | |||
| 636 | 636 | } | |
| 637 | 637 | } | |
| 638 | 638 | case "tool_use": | |
| 639 | - | args := b.toolArgs.String() | |
| 640 | - | if args == "" || !json.Valid([]byte(args)) { | |
| 641 | - | args = "{}" | |
| 642 | - | } | |
| 643 | 639 | content = append(content, ToolCallBlock{ | |
| 644 | 640 | ID: b.toolID, | |
| 645 | 641 | Name: b.toolName, | |
| 646 | - | Arguments: json.RawMessage(args), | |
| 642 | + | Arguments: normalizeToolArgs(b.toolArgs.String()), | |
| 647 | 643 | ThoughtSignature: b.thoughtSignature, | |
| 648 | 644 | }) | |
| 649 | 645 | } | |
| @@ -636,14 +636,10 @@ func (c *geminiClient) runStream(ctx context.Context, resp *http.Response, req R | |||||
| 636 | } | 636 | } | ||
| 637 | } | 637 | } | ||
| 638 | case "tool_use": | 638 | case "tool_use": | ||
| 639 | - | args := b.toolArgs.String() | |||
| 640 | - | if args == "" || !json.Valid([]byte(args)) { | |||
| 641 | - | args = "{}" | |||
| 642 | - | } | |||
| 643 | content = append(content, ToolCallBlock{ | 639 | content = append(content, ToolCallBlock{ | ||
| 644 | ID: b.toolID, | 640 | ID: b.toolID, | ||
| 645 | Name: b.toolName, | 641 | Name: b.toolName, | ||
| 646 | - | Arguments: json.RawMessage(args), | 642 | + | Arguments: normalizeToolArgs(b.toolArgs.String()), |
| 647 | ThoughtSignature: b.thoughtSignature, | 643 | ThoughtSignature: b.thoughtSignature, | ||
| 648 | }) | 644 | }) | ||
| 649 | } | 645 | } | ||
M packages/provider/openai.go +1 -5
| @@ -703,12 +703,8 @@ func (c *openaiClient) runStream(ctx context.Context, resp *http.Response, req R | |||
| 703 | 703 | content = append(content, TextBlock{Text: b.textBuf.String()}) | |
| 704 | 704 | } | |
| 705 | 705 | case "tool_use": | |
| 706 | - | args := b.toolArgs.String() | |
| 707 | - | if args == "" || !json.Valid([]byte(args)) { | |
| 708 | - | args = "{}" | |
| 709 | - | } | |
| 710 | 706 | content = append(content, ToolCallBlock{ | |
| 711 | - | ID: b.toolID, Name: b.toolName, Arguments: json.RawMessage(args), | |
| 707 | + | ID: b.toolID, Name: b.toolName, Arguments: normalizeToolArgs(b.toolArgs.String()), | |
| 712 | 708 | }) | |
| 713 | 709 | } | |
| 714 | 710 | } | |
| @@ -703,12 +703,8 @@ func (c *openaiClient) runStream(ctx context.Context, resp *http.Response, req R | |||||
| 703 | content = append(content, TextBlock{Text: b.textBuf.String()}) | 703 | content = append(content, TextBlock{Text: b.textBuf.String()}) | ||
| 704 | } | 704 | } | ||
| 705 | case "tool_use": | 705 | case "tool_use": | ||
| 706 | - | args := b.toolArgs.String() | |||
| 707 | - | if args == "" || !json.Valid([]byte(args)) { | |||
| 708 | - | args = "{}" | |||
| 709 | - | } | |||
| 710 | content = append(content, ToolCallBlock{ | 706 | content = append(content, ToolCallBlock{ | ||
| 711 | - | ID: b.toolID, Name: b.toolName, Arguments: json.RawMessage(args), | 707 | + | ID: b.toolID, Name: b.toolName, Arguments: normalizeToolArgs(b.toolArgs.String()), |
| 712 | }) | 708 | }) | ||
| 713 | } | 709 | } | ||
| 714 | } | 710 | } | ||
M packages/provider/openai_codex.go +1 -5
| @@ -796,12 +796,8 @@ func (c *codexClient) runResponseEventsWithFirst(ctx context.Context, req Reques | |||
| 796 | 796 | content = append(content, TextBlock{Text: it.textBuf.String(), Phase: it.phase}) | |
| 797 | 797 | } | |
| 798 | 798 | case "function_call": | |
| 799 | - | args := it.argsBuf.String() | |
| 800 | - | if args == "" || !json.Valid([]byte(args)) { | |
| 801 | - | args = "{}" | |
| 802 | - | } | |
| 803 | 799 | content = append(content, ToolCallBlock{ | |
| 804 | - | ID: it.callID, Name: it.name, Arguments: json.RawMessage(args), | |
| 800 | + | ID: it.callID, Name: it.name, Arguments: normalizeToolArgs(it.argsBuf.String()), | |
| 805 | 801 | }) | |
| 806 | 802 | case "reasoning": | |
| 807 | 803 | if it.encrypted == "" && it.summary.Len() == 0 && it.rawID == "" { | |
| @@ -796,12 +796,8 @@ func (c *codexClient) runResponseEventsWithFirst(ctx context.Context, req Reques | |||||
| 796 | content = append(content, TextBlock{Text: it.textBuf.String(), Phase: it.phase}) | 796 | content = append(content, TextBlock{Text: it.textBuf.String(), Phase: it.phase}) | ||
| 797 | } | 797 | } | ||
| 798 | case "function_call": | 798 | case "function_call": | ||
| 799 | - | args := it.argsBuf.String() | |||
| 800 | - | if args == "" || !json.Valid([]byte(args)) { | |||
| 801 | - | args = "{}" | |||
| 802 | - | } | |||
| 803 | content = append(content, ToolCallBlock{ | 799 | content = append(content, ToolCallBlock{ | ||
| 804 | - | ID: it.callID, Name: it.name, Arguments: json.RawMessage(args), | 800 | + | ID: it.callID, Name: it.name, Arguments: normalizeToolArgs(it.argsBuf.String()), |
| 805 | }) | 801 | }) | ||
| 806 | case "reasoning": | 802 | case "reasoning": | ||
| 807 | if it.encrypted == "" && it.summary.Len() == 0 && it.rawID == "" { | 803 | if it.encrypted == "" && it.summary.Len() == 0 && it.rawID == "" { | ||
A packages/provider/tool_args.go +67 -0
| @@ -0,0 +1,67 @@ | |||
| 1 | + | package provider | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "encoding/json" | |
| 5 | + | "strings" | |
| 6 | + | ) | |
| 7 | + | ||
| 8 | + | // normalizeToolArgs turns streamed tool-call arguments into valid JSON. | |
| 9 | + | // Models sometimes emit raw control characters (tabs, newlines) inside JSON | |
| 10 | + | // strings; those are escaped so the call keeps its intended arguments. | |
| 11 | + | // Empty or otherwise unrepairable input becomes "{}" so the message can | |
| 12 | + | // always be serialized to transcripts and sent back to the provider. | |
| 13 | + | func normalizeToolArgs(raw string) json.RawMessage { | |
| 14 | + | if raw == "" { | |
| 15 | + | return json.RawMessage("{}") | |
| 16 | + | } | |
| 17 | + | if json.Valid([]byte(raw)) { | |
| 18 | + | return json.RawMessage(raw) | |
| 19 | + | } | |
| 20 | + | if repaired := escapeControlCharsInStrings(raw); json.Valid([]byte(repaired)) { | |
| 21 | + | return json.RawMessage(repaired) | |
| 22 | + | } | |
| 23 | + | return json.RawMessage("{}") | |
| 24 | + | } | |
| 25 | + | ||
| 26 | + | // escapeControlCharsInStrings escapes raw U+0000–U+001F bytes that appear | |
| 27 | + | // inside JSON string literals. Bytes outside strings are left untouched. | |
| 28 | + | func escapeControlCharsInStrings(raw string) string { | |
| 29 | + | const hex = "0123456789abcdef" | |
| 30 | + | var b strings.Builder | |
| 31 | + | b.Grow(len(raw) + 8) | |
| 32 | + | inString, escaped := false, false | |
| 33 | + | for i := 0; i < len(raw); i++ { | |
| 34 | + | c := raw[i] | |
| 35 | + | if !inString { | |
| 36 | + | if c == '"' { | |
| 37 | + | inString = true | |
| 38 | + | } | |
| 39 | + | b.WriteByte(c) | |
| 40 | + | continue | |
| 41 | + | } | |
| 42 | + | switch { | |
| 43 | + | case escaped: | |
| 44 | + | escaped = false | |
| 45 | + | b.WriteByte(c) | |
| 46 | + | case c == '\\': | |
| 47 | + | escaped = true | |
| 48 | + | b.WriteByte(c) | |
| 49 | + | case c == '"': | |
| 50 | + | inString = false | |
| 51 | + | b.WriteByte(c) | |
| 52 | + | case c == '\t': | |
| 53 | + | b.WriteString(`\t`) | |
| 54 | + | case c == '\n': | |
| 55 | + | b.WriteString(`\n`) | |
| 56 | + | case c == '\r': | |
| 57 | + | b.WriteString(`\r`) | |
| 58 | + | case c < 0x20: | |
| 59 | + | b.WriteString(`\u00`) | |
| 60 | + | b.WriteByte(hex[c>>4]) | |
| 61 | + | b.WriteByte(hex[c&0xf]) | |
| 62 | + | default: | |
| 63 | + | b.WriteByte(c) | |
| 64 | + | } | |
| 65 | + | } | |
| 66 | + | return b.String() | |
| 67 | + | } | |
| @@ -0,0 +1,67 @@ | |||||
| 1 | + | package provider | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "encoding/json" | |||
| 5 | + | "strings" | |||
| 6 | + | ) | |||
| 7 | + | ||||
| 8 | + | // normalizeToolArgs turns streamed tool-call arguments into valid JSON. | |||
| 9 | + | // Models sometimes emit raw control characters (tabs, newlines) inside JSON | |||
| 10 | + | // strings; those are escaped so the call keeps its intended arguments. | |||
| 11 | + | // Empty or otherwise unrepairable input becomes "{}" so the message can | |||
| 12 | + | // always be serialized to transcripts and sent back to the provider. | |||
| 13 | + | func normalizeToolArgs(raw string) json.RawMessage { | |||
| 14 | + | if raw == "" { | |||
| 15 | + | return json.RawMessage("{}") | |||
| 16 | + | } | |||
| 17 | + | if json.Valid([]byte(raw)) { | |||
| 18 | + | return json.RawMessage(raw) | |||
| 19 | + | } | |||
| 20 | + | if repaired := escapeControlCharsInStrings(raw); json.Valid([]byte(repaired)) { | |||
| 21 | + | return json.RawMessage(repaired) | |||
| 22 | + | } | |||
| 23 | + | return json.RawMessage("{}") | |||
| 24 | + | } | |||
| 25 | + | ||||
| 26 | + | // escapeControlCharsInStrings escapes raw U+0000–U+001F bytes that appear | |||
| 27 | + | // inside JSON string literals. Bytes outside strings are left untouched. | |||
| 28 | + | func escapeControlCharsInStrings(raw string) string { | |||
| 29 | + | const hex = "0123456789abcdef" | |||
| 30 | + | var b strings.Builder | |||
| 31 | + | b.Grow(len(raw) + 8) | |||
| 32 | + | inString, escaped := false, false | |||
| 33 | + | for i := 0; i < len(raw); i++ { | |||
| 34 | + | c := raw[i] | |||
| 35 | + | if !inString { | |||
| 36 | + | if c == '"' { | |||
| 37 | + | inString = true | |||
| 38 | + | } | |||
| 39 | + | b.WriteByte(c) | |||
| 40 | + | continue | |||
| 41 | + | } | |||
| 42 | + | switch { | |||
| 43 | + | case escaped: | |||
| 44 | + | escaped = false | |||
| 45 | + | b.WriteByte(c) | |||
| 46 | + | case c == '\\': | |||
| 47 | + | escaped = true | |||
| 48 | + | b.WriteByte(c) | |||
| 49 | + | case c == '"': | |||
| 50 | + | inString = false | |||
| 51 | + | b.WriteByte(c) | |||
| 52 | + | case c == '\t': | |||
| 53 | + | b.WriteString(`\t`) | |||
| 54 | + | case c == '\n': | |||
| 55 | + | b.WriteString(`\n`) | |||
| 56 | + | case c == '\r': | |||
| 57 | + | b.WriteString(`\r`) | |||
| 58 | + | case c < 0x20: | |||
| 59 | + | b.WriteString(`\u00`) | |||
| 60 | + | b.WriteByte(hex[c>>4]) | |||
| 61 | + | b.WriteByte(hex[c&0xf]) | |||
| 62 | + | default: | |||
| 63 | + | b.WriteByte(c) | |||
| 64 | + | } | |||
| 65 | + | } | |||
| 66 | + | return b.String() | |||
| 67 | + | } | |||
A packages/provider/tool_args_test.go +106 -0
| @@ -0,0 +1,106 @@ | |||
| 1 | + | package provider | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "bytes" | |
| 5 | + | "context" | |
| 6 | + | "encoding/binary" | |
| 7 | + | "encoding/json" | |
| 8 | + | "io" | |
| 9 | + | "net/http" | |
| 10 | + | "strings" | |
| 11 | + | "testing" | |
| 12 | + | ) | |
| 13 | + | ||
| 14 | + | func TestNormalizeToolArgs(t *testing.T) { | |
| 15 | + | tab, nl, bell := string(rune(9)), string(rune(10)), string(rune(7)) | |
| 16 | + | tests := []struct { | |
| 17 | + | name string | |
| 18 | + | in string | |
| 19 | + | want string | |
| 20 | + | }{ | |
| 21 | + | {name: "empty", in: "", want: `{}`}, | |
| 22 | + | {name: "valid kept verbatim", in: `{"a":"b\tc"}`, want: `{"a":"b\tc"}`}, | |
| 23 | + | {name: "raw tab in string", in: `{"old":"` + tab + `x"}`, want: `{"old":"\tx"}`}, | |
| 24 | + | {name: "raw newline in string", in: `{"s":"a` + nl + `b"}`, want: `{"s":"a\nb"}`}, | |
| 25 | + | {name: "other control char", in: `{"s":"` + bell + `"}`, want: `{"s":"\u0007"}`}, | |
| 26 | + | {name: "whitespace outside strings untouched", in: "{" + nl + tab + `"s":"` + tab + `"}`, want: "{" + nl + tab + `"s":"\t"}`}, | |
| 27 | + | {name: "escaped quote keeps string state", in: `{"s":"\"` + tab + `"}`, want: `{"s":"\"\t"}`}, | |
| 28 | + | {name: "escaped backslash closes string", in: `{"a":"x\\"` + tab + `}`, want: `{"a":"x\\"` + tab + `}`}, | |
| 29 | + | {name: "multibyte utf-8 kept", in: `{"a":"é` + tab + `"}`, want: `{"a":"é\t"}`}, | |
| 30 | + | {name: "unrepairable", in: `{"s":`, want: `{}`}, | |
| 31 | + | } | |
| 32 | + | for _, tt := range tests { | |
| 33 | + | t.Run(tt.name, func(t *testing.T) { | |
| 34 | + | got := normalizeToolArgs(tt.in) | |
| 35 | + | if string(got) != tt.want { | |
| 36 | + | t.Fatalf("normalizeToolArgs(%q) = %q, want %q", tt.in, got, tt.want) | |
| 37 | + | } | |
| 38 | + | if !json.Valid(got) { | |
| 39 | + | t.Fatalf("result %q is not valid JSON", got) | |
| 40 | + | } | |
| 41 | + | }) | |
| 42 | + | } | |
| 43 | + | } | |
| 44 | + | ||
| 45 | + | func TestAnthropicStreamRepairsRawTabInToolArgs(t *testing.T) { | |
| 46 | + | // partial_json is a JSON string carrying the model's raw argument text; | |
| 47 | + | // "\\t" below decodes to a real tab byte inside the argument string. | |
| 48 | + | body := "event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call-1\",\"name\":\"edit\",\"input\":{}}}\n\n" + | |
| 49 | + | "event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"old\\\":\\\"\\tx\\\"}\"}}\n\n" + | |
| 50 | + | "event: content_block_stop\ndata: {\"index\":0}\n\n" + | |
| 51 | + | "event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"tool_use\"}}\n\n" | |
| 52 | + | resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))} | |
| 53 | + | out := make(chan Event, 16) | |
| 54 | + | go (&anthropicClient{}).runStream(context.Background(), resp, Request{Model: "test"}, out) | |
| 55 | + | assertToolArgOld(t, out, "\tx") | |
| 56 | + | } | |
| 57 | + | ||
| 58 | + | func TestBedrockStreamRepairsRawTabInToolArgs(t *testing.T) { | |
| 59 | + | // Header-less event-stream frames; the reader takes the event type from | |
| 60 | + | // the wrapped payload and does not validate CRCs. The `\t` escape in the input | |
| 61 | + | // field decodes to a real tab byte inside the argument string. | |
| 62 | + | var body bytes.Buffer | |
| 63 | + | for _, payload := range []string{ | |
| 64 | + | `{"contentBlockStart":{"contentBlockIndex":0,"start":{"toolUse":{"toolUseId":"call-1","name":"edit"}}}}`, | |
| 65 | + | `{"contentBlockDelta":{"contentBlockIndex":0,"delta":{"toolUse":{"input":"{\"old\":\"\tx\"}"}}}}`, | |
| 66 | + | `{"contentBlockStop":{"contentBlockIndex":0}}`, | |
| 67 | + | `{"messageStop":{"stopReason":"tool_use"}}`, | |
| 68 | + | } { | |
| 69 | + | var prelude [12]byte | |
| 70 | + | binary.BigEndian.PutUint32(prelude[0:4], uint32(16+len(payload))) | |
| 71 | + | body.Write(prelude[:]) | |
| 72 | + | body.WriteString(payload) | |
| 73 | + | body.Write(make([]byte, 4)) | |
| 74 | + | } | |
| 75 | + | resp := &http.Response{Body: io.NopCloser(&body)} | |
| 76 | + | out := make(chan Event, 16) | |
| 77 | + | go (&bedrockClient{}).runStream(context.Background(), resp, Request{Model: "test"}, out) | |
| 78 | + | assertToolArgOld(t, out, "\tx") | |
| 79 | + | } | |
| 80 | + | ||
| 81 | + | func assertToolArgOld(t *testing.T, out <-chan Event, want string) { | |
| 82 | + | t.Helper() | |
| 83 | + | var done EventDone | |
| 84 | + | for ev := range out { | |
| 85 | + | if d, ok := ev.(EventDone); ok { | |
| 86 | + | done = d | |
| 87 | + | } | |
| 88 | + | } | |
| 89 | + | if done.Err != nil { | |
| 90 | + | t.Fatalf("stream error: %v", done.Err) | |
| 91 | + | } | |
| 92 | + | if len(done.Message.Content) != 1 { | |
| 93 | + | t.Fatalf("content = %#v, want one tool call", done.Message.Content) | |
| 94 | + | } | |
| 95 | + | tc, ok := done.Message.Content[0].(ToolCallBlock) | |
| 96 | + | if !ok { | |
| 97 | + | t.Fatalf("content[0] = %T, want ToolCallBlock", done.Message.Content[0]) | |
| 98 | + | } | |
| 99 | + | var args struct{ Old string } | |
| 100 | + | if err := json.Unmarshal(tc.Arguments, &args); err != nil { | |
| 101 | + | t.Fatalf("arguments %q not valid JSON: %v", tc.Arguments, err) | |
| 102 | + | } | |
| 103 | + | if args.Old != want { | |
| 104 | + | t.Fatalf("old = %q, want %q", args.Old, want) | |
| 105 | + | } | |
| 106 | + | } | |
| @@ -0,0 +1,106 @@ | |||||
| 1 | + | package provider | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "bytes" | |||
| 5 | + | "context" | |||
| 6 | + | "encoding/binary" | |||
| 7 | + | "encoding/json" | |||
| 8 | + | "io" | |||
| 9 | + | "net/http" | |||
| 10 | + | "strings" | |||
| 11 | + | "testing" | |||
| 12 | + | ) | |||
| 13 | + | ||||
| 14 | + | func TestNormalizeToolArgs(t *testing.T) { | |||
| 15 | + | tab, nl, bell := string(rune(9)), string(rune(10)), string(rune(7)) | |||
| 16 | + | tests := []struct { | |||
| 17 | + | name string | |||
| 18 | + | in string | |||
| 19 | + | want string | |||
| 20 | + | }{ | |||
| 21 | + | {name: "empty", in: "", want: `{}`}, | |||
| 22 | + | {name: "valid kept verbatim", in: `{"a":"b\tc"}`, want: `{"a":"b\tc"}`}, | |||
| 23 | + | {name: "raw tab in string", in: `{"old":"` + tab + `x"}`, want: `{"old":"\tx"}`}, | |||
| 24 | + | {name: "raw newline in string", in: `{"s":"a` + nl + `b"}`, want: `{"s":"a\nb"}`}, | |||
| 25 | + | {name: "other control char", in: `{"s":"` + bell + `"}`, want: `{"s":"\u0007"}`}, | |||
| 26 | + | {name: "whitespace outside strings untouched", in: "{" + nl + tab + `"s":"` + tab + `"}`, want: "{" + nl + tab + `"s":"\t"}`}, | |||
| 27 | + | {name: "escaped quote keeps string state", in: `{"s":"\"` + tab + `"}`, want: `{"s":"\"\t"}`}, | |||
| 28 | + | {name: "escaped backslash closes string", in: `{"a":"x\\"` + tab + `}`, want: `{"a":"x\\"` + tab + `}`}, | |||
| 29 | + | {name: "multibyte utf-8 kept", in: `{"a":"é` + tab + `"}`, want: `{"a":"é\t"}`}, | |||
| 30 | + | {name: "unrepairable", in: `{"s":`, want: `{}`}, | |||
| 31 | + | } | |||
| 32 | + | for _, tt := range tests { | |||
| 33 | + | t.Run(tt.name, func(t *testing.T) { | |||
| 34 | + | got := normalizeToolArgs(tt.in) | |||
| 35 | + | if string(got) != tt.want { | |||
| 36 | + | t.Fatalf("normalizeToolArgs(%q) = %q, want %q", tt.in, got, tt.want) | |||
| 37 | + | } | |||
| 38 | + | if !json.Valid(got) { | |||
| 39 | + | t.Fatalf("result %q is not valid JSON", got) | |||
| 40 | + | } | |||
| 41 | + | }) | |||
| 42 | + | } | |||
| 43 | + | } | |||
| 44 | + | ||||
| 45 | + | func TestAnthropicStreamRepairsRawTabInToolArgs(t *testing.T) { | |||
| 46 | + | // partial_json is a JSON string carrying the model's raw argument text; | |||
| 47 | + | // "\\t" below decodes to a real tab byte inside the argument string. | |||
| 48 | + | body := "event: content_block_start\ndata: {\"index\":0,\"content_block\":{\"type\":\"tool_use\",\"id\":\"call-1\",\"name\":\"edit\",\"input\":{}}}\n\n" + | |||
| 49 | + | "event: content_block_delta\ndata: {\"index\":0,\"delta\":{\"type\":\"input_json_delta\",\"partial_json\":\"{\\\"old\\\":\\\"\\tx\\\"}\"}}\n\n" + | |||
| 50 | + | "event: content_block_stop\ndata: {\"index\":0}\n\n" + | |||
| 51 | + | "event: message_delta\ndata: {\"delta\":{\"stop_reason\":\"tool_use\"}}\n\n" | |||
| 52 | + | resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))} | |||
| 53 | + | out := make(chan Event, 16) | |||
| 54 | + | go (&anthropicClient{}).runStream(context.Background(), resp, Request{Model: "test"}, out) | |||
| 55 | + | assertToolArgOld(t, out, "\tx") | |||
| 56 | + | } | |||
| 57 | + | ||||
| 58 | + | func TestBedrockStreamRepairsRawTabInToolArgs(t *testing.T) { | |||
| 59 | + | // Header-less event-stream frames; the reader takes the event type from | |||
| 60 | + | // the wrapped payload and does not validate CRCs. The `\t` escape in the input | |||
| 61 | + | // field decodes to a real tab byte inside the argument string. | |||
| 62 | + | var body bytes.Buffer | |||
| 63 | + | for _, payload := range []string{ | |||
| 64 | + | `{"contentBlockStart":{"contentBlockIndex":0,"start":{"toolUse":{"toolUseId":"call-1","name":"edit"}}}}`, | |||
| 65 | + | `{"contentBlockDelta":{"contentBlockIndex":0,"delta":{"toolUse":{"input":"{\"old\":\"\tx\"}"}}}}`, | |||
| 66 | + | `{"contentBlockStop":{"contentBlockIndex":0}}`, | |||
| 67 | + | `{"messageStop":{"stopReason":"tool_use"}}`, | |||
| 68 | + | } { | |||
| 69 | + | var prelude [12]byte | |||
| 70 | + | binary.BigEndian.PutUint32(prelude[0:4], uint32(16+len(payload))) | |||
| 71 | + | body.Write(prelude[:]) | |||
| 72 | + | body.WriteString(payload) | |||
| 73 | + | body.Write(make([]byte, 4)) | |||
| 74 | + | } | |||
| 75 | + | resp := &http.Response{Body: io.NopCloser(&body)} | |||
| 76 | + | out := make(chan Event, 16) | |||
| 77 | + | go (&bedrockClient{}).runStream(context.Background(), resp, Request{Model: "test"}, out) | |||
| 78 | + | assertToolArgOld(t, out, "\tx") | |||
| 79 | + | } | |||
| 80 | + | ||||
| 81 | + | func assertToolArgOld(t *testing.T, out <-chan Event, want string) { | |||
| 82 | + | t.Helper() | |||
| 83 | + | var done EventDone | |||
| 84 | + | for ev := range out { | |||
| 85 | + | if d, ok := ev.(EventDone); ok { | |||
| 86 | + | done = d | |||
| 87 | + | } | |||
| 88 | + | } | |||
| 89 | + | if done.Err != nil { | |||
| 90 | + | t.Fatalf("stream error: %v", done.Err) | |||
| 91 | + | } | |||
| 92 | + | if len(done.Message.Content) != 1 { | |||
| 93 | + | t.Fatalf("content = %#v, want one tool call", done.Message.Content) | |||
| 94 | + | } | |||
| 95 | + | tc, ok := done.Message.Content[0].(ToolCallBlock) | |||
| 96 | + | if !ok { | |||
| 97 | + | t.Fatalf("content[0] = %T, want ToolCallBlock", done.Message.Content[0]) | |||
| 98 | + | } | |||
| 99 | + | var args struct{ Old string } | |||
| 100 | + | if err := json.Unmarshal(tc.Arguments, &args); err != nil { | |||
| 101 | + | t.Fatalf("arguments %q not valid JSON: %v", tc.Arguments, err) | |||
| 102 | + | } | |||
| 103 | + | if args.Old != want { | |||
| 104 | + | t.Fatalf("old = %q, want %q", args.Old, want) | |||
| 105 | + | } | |||
| 106 | + | } | |||