Repository navigation
Expand file tree
/
Copy pathrequest.go
More file actions
1234 lines (1195 loc) · 44.3 KB
/
Copy pathrequest.go
File metadata and controls
1234 lines (1195 loc) · 44.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package protocol
import (
"errors"
"fmt"
"sort"
"strings"
"opencode2api/internal/jsonutil"
)
// PrepareRequest is the single request preparation path for both
// pass-through and transcoded requests. Same-protocol requests are cloned so
// provider-specific fields survive, while cross-protocol requests go through
// the bridge. Target-protocol normalization then repairs reasoning history in
// either case, using the model and upstreamURL to recognize reasoning
// vendors, and a Responses body has its max_output_tokens floored.
func PrepareRequest(from, to Protocol, input map[string]any, upstreamURL string) (map[string]any, error) {
output, err := ConvertRequest(from, to, input)
if err != nil {
return nil, err
}
if to == Responses {
// The bridge already floors converted bodies; a same-protocol body is
// a clone of the client's and needs the same guard.
jsonutil.Put(output, "max_output_tokens", floorResponsesMaxTokens(output["max_output_tokens"]))
}
normalizeToolReasoningHistory(to, jsonutil.StringAt(output, "model"), upstreamURL, output)
return output, nil
}
// ForcedEffort applies an operator-configured thinking level to a prepared
// upstream body, so a client can be served at a fixed level without the client
// cooperating. A level validForcedEffort rejects is ignored, and "none"
// removes the reasoning configuration instead of setting a level.
//
// A level already present in the body always wins (see clientEffortExplicit).
// The forced level fills in only where the body carries no level in the slot
// that protocol's encoder writes, such as a request that said nothing about
// reasoning, or a same-protocol Anthropic request whose thinking block names
// only a budget; that budget is then rewritten to the forced level's. A body
// the bridge converted has that slot filled whenever it derived a level from
// thinking.budget_tokens, so the derived level counts as present and is kept.
// The level is applied to the finished upstream body rather than to the bridge
// value because a same-protocol request is cloned and never passes through the
// bridge; the body is the one place both paths share.
func ForcedEffort(protocol Protocol, body map[string]any, effort string) {
if body == nil {
return
}
if requestDisablesReasoning(protocol, body) {
return
}
effort = strings.ToLower(strings.TrimSpace(effort))
if !validForcedEffort(effort) {
return
}
disable := effort == "none"
switch protocol {
case Chat:
if clientEffortExplicit(protocol, body) {
return
}
if disable {
delete(body, "reasoning_effort")
return
}
body["reasoning_effort"] = effort
case Anthropic:
if clientEffortExplicit(protocol, body) {
return
}
applyAnthropicForcedEffort(body, effort, disable)
case Responses:
if clientEffortExplicit(protocol, body) {
return
}
if disable {
delete(body, "reasoning")
return
}
reasoning, _ := body["reasoning"].(map[string]any)
if reasoning == nil {
reasoning = map[string]any{}
body["reasoning"] = reasoning
}
reasoning["effort"] = effort
}
}
func requestDisablesReasoning(protocol Protocol, body map[string]any) bool {
switch protocol {
case Chat:
return reasoningDisabled(body["reasoning_effort"]) ||
reasoningDisabled(body["reasoning"]) ||
reasoningDisabled(body["thinking"])
case Responses:
return reasoningDisabled(jsonutil.AnyAt(body, "reasoning", "effort")) ||
reasoningDisabled(body["reasoning_effort"])
case Anthropic:
return reasoningDisabled(body["thinking"]) ||
reasoningDisabled(jsonutil.AnyAt(body, "output_config", "effort")) ||
reasoningDisabled(body["effort"])
default:
return false
}
}
// clientEffortExplicit reports whether an upstream body already carries a
// level in its protocol's native slot, which a forced level must not replace.
//
// The shapes differ per protocol, and each one mirrors how that protocol
// expresses the level natively: Anthropic in a non-blank output_config.effort
// (or top-level effort) string, Chat in a non-blank reasoning_effort string,
// string value counts for Responses, so it cannot tell a non-blank level
// the client stated from one derived from a budget. Blank values count as
// unset on every protocol; a missing or non-string value also counts as no
// level.
func clientEffortExplicit(protocol Protocol, body map[string]any) bool {
switch protocol {
case Chat:
effort, ok := body["reasoning_effort"].(string)
return ok && strings.TrimSpace(effort) != ""
case Anthropic:
if effort, ok := jsonutil.AnyAt(body, "output_config", "effort").(string); ok && strings.TrimSpace(effort) != "" {
return true
}
effort, ok := body["effort"].(string)
return ok && strings.TrimSpace(effort) != ""
case Responses:
effort, ok := jsonutil.AnyAt(body, "reasoning", "effort").(string)
return ok && strings.TrimSpace(effort) != ""
default:
return false
}
}
// applyAnthropicForcedEffort writes a forced level into an Anthropic body and
// keeps the thinking block and max_tokens consistent with it.
//
// Anthropic represents a level as output_config.effort, but also requires a
// thinking block to carry a budget and requires max_tokens to exceed that
// budget. Forcing a level therefore has to move all three together, or the
// upstream rejects the request it was just told to run at a higher level. With
// disable set, the thinking block and output_config are removed instead.
func applyAnthropicForcedEffort(body map[string]any, effort string, disable bool) {
if disable {
delete(body, "thinking")
delete(body, "output_config")
return
}
if outputConfig, _ := body["output_config"].(map[string]any); outputConfig == nil {
body["output_config"] = map[string]any{"effort": effort}
} else {
outputConfig["effort"] = effort
}
budget := budgetForEffort(effort)
if thinking, ok := body["thinking"].(map[string]any); ok {
thinking["type"] = "enabled"
thinking["budget_tokens"] = float64(budget)
} else {
body["thinking"] = map[string]any{"type": "enabled", "budget_tokens": float64(budget)}
}
// max_tokens must stay strictly greater than the thinking budget, otherwise
// Anthropic rejects the payload regardless of the requested effort.
if current := jsonutil.IntAt(body, "max_tokens"); current > 0 && current <= budget {
body["max_tokens"] = budget + 4096
}
}
// validForcedEffort accepts the levels the configuration can force. "none" is
// the escape hatch that removes reasoning again.
func validForcedEffort(effort string) bool {
switch effort {
case "minimal", "low", "medium", "high", "xhigh", "max", "none":
return true
default:
return false
}
}
// normalizeToolReasoningHistory repairs the reasoning history of assistant
// tool-call turns in a prepared upstream body and reports whether it changed
// anything. It applies only to endpoints that are known to require reasoning
// replay, identified by the model or upstream URL naming a reasoning vendor;
// a Chat body is also repaired when the request explicitly enables reasoning.
// Responses bodies are never touched. Normalizing the target shape makes the
// behavior independent of the client protocol used to reach the gateway.
func normalizeToolReasoningHistory(protocol Protocol, model, upstreamURL string, input map[string]any) bool {
vendor := isReasoningVendorIdentifier(model) || isReasoningVendorIdentifier(upstreamURL)
widened := vendor || requestEnablesReasoning(input)
switch protocol {
case Chat:
// The Chat repair is additive: it only supplies reasoning_content that
// a compatible endpoint ignores when it does not understand the field.
// The widened gate is therefore safe here.
if !widened {
return false
}
return normalizeChatToolReasoningHistory(input)
case Anthropic:
// Every repair below is destructive for a native Anthropic endpoint:
// it deletes thinking signatures, discards redacted_thinking payloads
// and overwrites empty thinking text. Those are precisely the shapes a
// native endpoint re-validates and replays verbatim, and rewriting them
// also shifts the cached prompt prefix mid-history. Only endpoints
// identified as reasoning vendors get the rewrite.
if !vendor {
return false
}
return normalizeAnthropicToolThinkingHistory(input)
default:
return false
}
}
// isReasoningVendorIdentifier reports whether a model ID or upstream URL
// contains one of the reasoningVendorHints, ignoring case.
func isReasoningVendorIdentifier(value string) bool {
value = strings.ToLower(value)
for _, hint := range reasoningVendorHints {
if strings.Contains(value, hint) {
return true
}
}
return false
}
// requestEnablesReasoning reports whether a body's reasoning_effort,
// reasoning, thinking or effort field asks for reasoning. A string counts
// unless it is blank, "none" or "disabled"; a bool counts when true; an object
// counts unless its type (or, without one, its effort) is "none" or
// "disabled"; any other non-null value counts.
func requestEnablesReasoning(input map[string]any) bool {
for _, key := range []string{"reasoning_effort", "reasoning", "thinking", "effort"} {
value, exists := input[key]
if !exists || value == nil {
continue
}
switch typed := value.(type) {
case string:
mode := strings.ToLower(strings.TrimSpace(typed))
if mode != "" && mode != "none" && mode != "disabled" {
return true
}
case bool:
if typed {
return true
}
case map[string]any:
mode := strings.ToLower(strings.TrimSpace(jsonutil.FirstString(jsonutil.StringAt(typed, "type"), jsonutil.StringAt(typed, "effort"))))
if mode == "none" || mode == "disabled" {
continue
}
return true
default:
return true
}
}
return false
}
// normalizeChatToolReasoningHistory ensures every assistant tool-call turn
// carries reasoning_content. Some clients discard this non-standard field
// while retaining tool_calls, which otherwise makes the next thinking-mode
// request invalid. A legacy reasoning string is promoted when available, and a
// placeholder is used otherwise. It reports whether any message changed.
func normalizeChatToolReasoningHistory(input map[string]any) bool {
messages, ok := input["messages"].([]any)
if !ok {
return false
}
changed := false
for _, rawMessage := range messages {
message, ok := rawMessage.(map[string]any)
if !ok || jsonutil.StringAt(message, "role") != "assistant" || len(jsonutil.SliceAt(message, "tool_calls")) == 0 {
continue
}
if reasoning, ok := message["reasoning_content"].(string); ok && strings.TrimSpace(reasoning) != "" {
continue
}
reasoning, _ := message["reasoning"].(string)
if strings.TrimSpace(reasoning) == "" {
reasoning = toolReasoningPlaceholder
}
message["reasoning_content"] = reasoning
changed = true
}
return changed
}
// normalizeAnthropicToolThinkingHistory repairs only assistant turns that
// contain tool_use. DeepSeek, Kimi/Moonshot and MiMo reject signed, redacted,
// empty or missing thinking history on those turns even though Anthropic
// clients can legitimately send each of those shapes. It strips thinking
// signatures, replaces redacted_thinking blocks and empty thinking text with
// placeholders, and prepends a placeholder thinking block to a turn that has
// none. It reports whether any message changed.
func normalizeAnthropicToolThinkingHistory(input map[string]any) bool {
messages, ok := input["messages"].([]any)
if !ok {
return false
}
changed := false
for _, rawMessage := range messages {
message, ok := rawMessage.(map[string]any)
if !ok || jsonutil.StringAt(message, "role") != "assistant" {
continue
}
content, ok := message["content"].([]any)
if !ok || !anthropicContentHasType(content, "tool_use") {
continue
}
hasThinking := false
for i, rawBlock := range content {
block, ok := rawBlock.(map[string]any)
if !ok {
continue
}
switch jsonutil.StringAt(block, "type") {
case "thinking":
hasThinking = true
if _, exists := block["signature"]; exists {
delete(block, "signature")
changed = true
}
thinking, ok := block["thinking"].(string)
if !ok || strings.TrimSpace(thinking) == "" {
block["thinking"] = toolReasoningPlaceholder
changed = true
}
case "redacted_thinking":
hasThinking = true
content[i] = map[string]any{
"type": "thinking",
"thinking": anthropicRedactedThinkingPlaceholder,
}
changed = true
}
}
if !hasThinking {
content = append([]any{map[string]any{
"type": "thinking",
"thinking": toolReasoningPlaceholder,
}}, content...)
message["content"] = content
changed = true
}
}
return changed
}
// anthropicContentHasType reports whether any block of an Anthropic content
// array has the given type.
func anthropicContentHasType(content []any, kind string) bool {
for _, rawBlock := range content {
block, _ := rawBlock.(map[string]any)
if jsonutil.StringAt(block, "type") == kind {
return true
}
}
return false
}
// anthropicReasoning decodes the reasoning controls of an Anthropic Messages
// request into a single bridge-level value.
//
// An effort the client states explicitly outranks a thinking block's budget.
// Choosing input["thinking"] first (as this used to) shadowed
// output_config.effort and a top-level effort entirely, so `thinking:
// {adaptive}` plus `output_config: {effort: "max"}` decoded as the thinking
// block and was reported downstream as the budget bucket "high". A stated
// "none"/"disabled" decodes as no reasoning at all, even next to a thinking
// block.
func anthropicReasoning(input map[string]any) any {
if reasoningDisabled(input["thinking"]) {
return "none"
}
stated := jsonutil.FirstAny(jsonutil.AnyAt(input, "output_config", "effort"), input["effort"])
if effort, ok := normalizeEffortValue(stated); ok {
if effort == "none" {
return "none"
}
if thinking, ok := input["thinking"].(map[string]any); ok {
merged := make(map[string]any, len(thinking)+1)
for key, value := range thinking {
merged[key] = value
}
merged["effort"] = effort
return merged
}
return effort
}
return input["thinking"]
}
// decodeBridgeRequest decodes a client request body of the given protocol into
// the bridge model. It rejects what the bridge cannot carry faithfully: a
// malformed message or tool, an unsupported role, content block or Responses
// item type, and a Chat tool that is not a function. Built-in Responses tools
// are skipped, as is a Responses message item with a role other than system,
// developer, user or assistant. Tool results of Responses and Anthropic
// requests then go through decodeToolImages, and a result carrying images
// must name its tool call.
func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest, error) {
request := bridgeRequest{
Model: jsonutil.StringAt(input, "model"),
Stream: jsonutil.BoolAt(input, "stream"),
Temperature: input["temperature"],
TopP: input["top_p"],
Metadata: input["metadata"],
}
switch protocol {
case Chat:
request.MaxTokens = jsonutil.FirstAny(input["max_completion_tokens"], input["max_tokens"])
request.Stop = input["stop"]
request.Reasoning = jsonutil.FirstAny(input["reasoning_effort"], input["reasoning"])
if reasoningDisabled(input["thinking"]) {
request.Reasoning = "none"
}
for i, raw := range jsonutil.SliceAt(input, "messages") {
message, ok := raw.(map[string]any)
if !ok {
return request, fmt.Errorf("messages[%d] must be an object", i)
}
role := jsonutil.StringAt(message, "role")
blocks, err := decodeOpenAIBlocksChecked(message["content"])
if err != nil {
return request, fmt.Errorf("messages[%d]: %w", i, err)
}
if role == "assistant" {
blocks = append(decodeChatReasoning(message), blocks...)
// A budget the gateway carried out on the assistant message is
// read back next to the effort, so both directions agree on the
// budget the client originally named.
if budget := jsonutil.IntAt(message, "reasoning_budget_tokens"); budget > 0 {
request.Reasoning = withReasoningBudget(request.Reasoning, budget)
}
}
for j, rawCall := range jsonutil.SliceAt(message, "tool_calls") {
call, ok := rawCall.(map[string]any)
if !ok {
return request, fmt.Errorf("messages[%d].tool_calls[%d] must be an object", i, j)
}
function := jsonutil.MapAt(call, "function")
blocks = append(blocks, bridgeBlock{
Kind: "tool_call",
ID: jsonutil.StringAt(call, "id"),
Name: jsonutil.StringAt(function, "name"),
ArgumentsJSON: jsonutil.StringAt(function, "arguments"),
})
}
switch role {
case "system":
request.System = append(request.System, blocks...)
case "developer":
request.Developer = append(request.Developer, blocks...)
case "tool":
request.Messages = append(request.Messages, bridgeMessage{Role: "user", Blocks: []bridgeBlock{{
Kind: "tool_result",
CallID: jsonutil.StringAt(message, "tool_call_id"),
Result: message["content"],
}}})
case "user", "assistant":
request.Messages = append(request.Messages, bridgeMessage{Role: role, Blocks: blocks})
default:
return request, fmt.Errorf("messages[%d] has unsupported role %q", i, role)
}
}
for i, raw := range jsonutil.SliceAt(input, "tools") {
tool, ok := raw.(map[string]any)
if !ok || jsonutil.StringAt(tool, "type") != "function" {
return request, fmt.Errorf("tools[%d] must be a function tool", i)
}
function := jsonutil.MapAt(tool, "function")
request.Tools = append(request.Tools, bridgeTool{
Name: jsonutil.StringAt(function, "name"),
Description: jsonutil.StringAt(function, "description"),
Schema: function["parameters"],
Strict: jsonutil.BoolAt(function, "strict"),
})
}
request.ToolChoice = decodeChatToolChoice(input["tool_choice"])
request.ResponseFormat = input["response_format"]
request.ParallelToolCalls = input["parallel_tool_calls"]
request.FrequencyPenalty = input["frequency_penalty"]
request.PresencePenalty = input["presence_penalty"]
request.Seed = input["seed"]
request.PromptCacheKey = input["prompt_cache_key"]
request.SafetyIdentifier = input["safety_identifier"]
request.ServiceTier = input["service_tier"]
request.Store = input["store"]
case Responses:
request.MaxTokens = input["max_output_tokens"]
request.Stop = input["stop"]
request.Reasoning = jsonutil.FirstAny(input["reasoning"], input["reasoning_effort"])
instructions, err := decodeOpenAIBlocksChecked(input["instructions"])
if err != nil {
return request, fmt.Errorf("instructions: %w", err)
}
request.System = append(request.System, instructions...)
switch value := input["input"].(type) {
case string:
request.Messages = append(request.Messages, bridgeMessage{Role: "user", Blocks: []bridgeBlock{{Kind: "text", Text: value}}})
case []any:
for i, raw := range value {
item, ok := raw.(map[string]any)
if !ok {
return request, fmt.Errorf("input[%d] must be an object", i)
}
switch jsonutil.StringAt(item, "type") {
case "reasoning":
for _, block := range decodeResponsesReasoning(item) {
appendBridgeBlock(&request.Messages, "assistant", block)
}
case "function_call":
appendBridgeBlock(&request.Messages, "assistant", bridgeBlock{
Kind: "tool_call",
ID: jsonutil.FirstString(jsonutil.StringAt(item, "call_id"), jsonutil.StringAt(item, "id")),
Name: jsonutil.StringAt(item, "name"),
ArgumentsJSON: jsonutil.StringAt(item, "arguments"),
})
case "function_call_output":
appendBridgeBlock(&request.Messages, "user", bridgeBlock{
Kind: "tool_result",
CallID: jsonutil.StringAt(item, "call_id"),
Result: item["output"],
})
case "message", "":
role := jsonutil.StringAt(item, "role")
blocks, err := decodeOpenAIBlocksChecked(item["content"])
if err != nil {
return request, fmt.Errorf("input[%d]: %w", i, err)
}
if role == "system" {
request.System = append(request.System, blocks...)
} else if role == "developer" {
request.Developer = append(request.Developer, blocks...)
} else if role == "user" || role == "assistant" {
request.Messages = append(request.Messages, bridgeMessage{Role: role, Blocks: blocks})
}
default:
return request, fmt.Errorf("input[%d] has unsupported Responses item type %q", i, jsonutil.StringAt(item, "type"))
}
}
default:
return request, fmt.Errorf("input must be a string or array")
}
for i, raw := range jsonutil.SliceAt(input, "tools") {
tool, ok := raw.(map[string]any)
if !ok {
return request, fmt.Errorf("tools[%d] must be an object", i)
}
// Built-in Responses tools (web_search, file_search, code_interpreter, …)
// have no Chat/Anthropic equivalent; skip them during cross-protocol
// bridging so requests that mix function tools with built-in plugins
// still reach the upstream.
if jsonutil.StringAt(tool, "type") != "function" {
continue
}
request.Tools = append(request.Tools, bridgeTool{
Name: jsonutil.StringAt(tool, "name"),
Description: jsonutil.StringAt(tool, "description"),
Schema: tool["parameters"],
Strict: jsonutil.BoolAt(tool, "strict"),
})
}
request.ToolChoice = decodeResponsesToolChoice(input["tool_choice"])
request.ResponseFormat = jsonutil.MapAt(input, "text", "format")
request.ParallelToolCalls = input["parallel_tool_calls"]
request.PromptCacheKey = input["prompt_cache_key"]
request.SafetyIdentifier = input["safety_identifier"]
request.ServiceTier = input["service_tier"]
request.Store = input["store"]
case Anthropic:
request.MaxTokens = input["max_tokens"]
request.Stop = input["stop_sequences"]
request.Reasoning = anthropicReasoning(input)
blocks, err := decodeAnthropicBlocksChecked(input["system"])
if err != nil {
return request, fmt.Errorf("system: %w", err)
}
request.System = blocks
for i, raw := range jsonutil.SliceAt(input, "messages") {
message, ok := raw.(map[string]any)
if !ok {
return request, fmt.Errorf("messages[%d] must be an object", i)
}
role := jsonutil.StringAt(message, "role")
// Some clients (e.g. Claude Code) inline system/developer messages
// into the messages array instead of using the top-level system
// field. Fold system messages into request.System and developer
// messages into request.Developer rather than rejecting them.
if role == "system" {
blocks, err := decodeAnthropicBlocksChecked(message["content"])
if err != nil {
return request, fmt.Errorf("messages[%d]: %w", i, err)
}
request.System = append(request.System, blocks...)
continue
}
if role == "developer" {
blocks, err := decodeAnthropicBlocksChecked(message["content"])
if err != nil {
return request, fmt.Errorf("messages[%d]: %w", i, err)
}
request.Developer = append(request.Developer, blocks...)
continue
}
if role != "user" && role != "assistant" {
return request, fmt.Errorf("messages[%d] has unsupported role %q", i, role)
}
blocks, err := decodeAnthropicBlocksChecked(message["content"])
if err != nil {
return request, fmt.Errorf("messages[%d]: %w", i, err)
}
request.Messages = append(request.Messages, bridgeMessage{Role: role, Blocks: blocks})
}
for i, raw := range jsonutil.SliceAt(input, "tools") {
tool, ok := raw.(map[string]any)
if !ok {
return request, fmt.Errorf("tools[%d] must be an object", i)
}
request.Tools = append(request.Tools, bridgeTool{
Name: jsonutil.StringAt(tool, "name"),
Description: jsonutil.StringAt(tool, "description"),
Schema: tool["input_schema"],
})
}
request.ToolChoice = decodeAnthropicToolChoice(input["tool_choice"])
default:
return request, fmt.Errorf("unsupported input protocol %q", protocol)
}
if reasoningDisabled(request.Reasoning) {
request.Reasoning = "none"
}
for i := range request.Messages {
for j := range request.Messages[i].Blocks {
block := &request.Messages[i].Blocks[j]
if block.Kind != "tool_result" || protocol == Chat {
continue
}
result, err := decodeToolImages(protocol, block.Result)
if err != nil {
return request, fmt.Errorf("messages[%d].tool_result[%d]: %w", i, j, err)
}
if _, images := result.([]bridgeBlock); images && block.CallID == "" {
return request, fmt.Errorf("messages[%d]: tool image result requires a call ID", i)
}
block.Result = result
}
}
return request, nil
}
// appendBridgeBlock adds a block decoded from a standalone Responses item. It
// joins the previous message when that has the same role and holds only tool
// calls or results, so consecutive function_call (or function_call_output)
// items form one turn; otherwise the block starts a new message.
func appendBridgeBlock(messages *[]bridgeMessage, role string, block bridgeBlock) {
if len(*messages) > 0 {
last := &(*messages)[len(*messages)-1]
if last.Role == role && bridgeBlocksOnlyTools(last.Blocks) {
last.Blocks = append(last.Blocks, block)
return
}
}
*messages = append(*messages, bridgeMessage{Role: role, Blocks: []bridgeBlock{block}})
}
// bridgeBlocksOnlyTools reports whether blocks is non-empty and holds only
// tool calls and tool results.
func bridgeBlocksOnlyTools(blocks []bridgeBlock) bool {
if len(blocks) == 0 {
return false
}
for _, block := range blocks {
if block.Kind != "tool_call" && block.Kind != "tool_result" {
return false
}
}
return true
}
// encodeBridgeRequest validates a bridge request against the target protocol
// and encodes it as that protocol's request body. The target must be one of
// the three message protocols, and the Chat encoder can also fail on a history
// whose tool calls and results do not pair up.
func encodeBridgeRequest(protocol Protocol, request bridgeRequest) (map[string]any, error) {
if err := validateBridgeRequest(protocol, request); err != nil {
return nil, err
}
switch protocol {
case Chat:
return encodeChatRequest(request)
case Responses:
return encodeResponsesRequest(request), nil
case Anthropic:
return encodeAnthropicRequest(request), nil
default:
return nil, fmt.Errorf("unsupported output protocol %q", protocol)
}
}
// validateBridgeRequest rejects content the target protocol cannot represent
// at all: a file in Responses instructions, and for Anthropic a file known
// only by its file_id.
func validateBridgeRequest(protocol Protocol, request bridgeRequest) error {
if protocol == Responses {
for _, block := range request.System {
if block.Kind == "file" {
return errors.New("file content is not valid inside Responses instructions; send it as a message input item")
}
}
}
blocks := make([]bridgeBlock, 0, len(request.System)+len(request.Developer))
blocks = append(blocks, request.System...)
blocks = append(blocks, request.Developer...)
for _, message := range request.Messages {
blocks = append(blocks, message.Blocks...)
}
for _, block := range blocks {
if block.Kind == "file" && protocol == Anthropic && block.FileID != "" && block.Data == "" && block.URL == "" {
return fmt.Errorf("file %q cannot be represented by Anthropic Messages without file data or a URL", block.FileID)
}
}
return nil
}
// encodeChatRequest encodes a bridge request as a Chat Completions body.
//
// System and developer blocks become leading system and developer messages.
// Chat answers each tool call with a separate "tool" message that must follow
// the assistant message carrying the calls, so the encoder tracks pending
// calls: once every pending call has a result, the tool messages are emitted
// in call order, followed by user messages for any images the results carried
// (a tool message holds only text), and then by user content that arrived
// while the calls were pending. A history that breaks the call/result pairing
// is an error.
func encodeChatRequest(request bridgeRequest) (map[string]any, error) {
output := map[string]any{"model": request.Model, "stream": request.Stream}
jsonutil.Put(output, "temperature", request.Temperature)
jsonutil.Put(output, "top_p", request.TopP)
jsonutil.Put(output, "max_tokens", request.MaxTokens)
jsonutil.Put(output, "stop", request.Stop)
if effort := reasoningEffort(request.Reasoning); effort != nil {
if effort == "none" {
output["reasoning_effort"] = "none"
} else {
output["reasoning_effort"] = effort
}
}
jsonutil.Put(output, "response_format", chatResponseFormat(request.ResponseFormat))
jsonutil.Put(output, "parallel_tool_calls", request.ParallelToolCalls)
jsonutil.Put(output, "frequency_penalty", request.FrequencyPenalty)
jsonutil.Put(output, "presence_penalty", request.PresencePenalty)
jsonutil.Put(output, "seed", request.Seed)
jsonutil.Put(output, "prompt_cache_key", request.PromptCacheKey)
jsonutil.Put(output, "safety_identifier", request.SafetyIdentifier)
jsonutil.Put(output, "service_tier", request.ServiceTier)
jsonutil.Put(output, "store", request.Store)
if request.Stream {
output["stream_options"] = map[string]any{"include_usage": true}
}
messages := make([]any, 0, len(request.Messages)+1)
if len(request.System) > 0 {
messages = append(messages, map[string]any{"role": "system", "content": encodeChatBlocks(request.System)})
}
if len(request.Developer) > 0 {
messages = append(messages, map[string]any{"role": "developer", "content": encodeChatBlocks(request.Developer)})
}
pending := make(map[string]bool)
pendingResults := make(map[string]bridgeBlock)
var pendingOrder []string
var deferred [][]bridgeBlock
// Chat has a reasoning_effort level but no field for an Anthropic thinking
// budget. The budget the client named is emitted once, as
// reasoning_budget_tokens on the first assistant message, so that decoding
// this Chat body back into Anthropic can restore the budget exactly instead
// of re-deriving it from the level (32000 used to come back as 8192).
budgetCarried := false
carryBudget := func(encoded map[string]any) {
if budgetCarried {
return
}
if budget := reasoningBudget(request.Reasoning); budget > 0 {
encoded["reasoning_budget_tokens"] = budget
budgetCarried = true
}
}
flushDeferred := func() {
for _, blocks := range deferred {
if len(blocks) > 0 {
messages = append(messages, map[string]any{"role": "user", "content": encodeChatBlocks(blocks)})
}
}
deferred = nil
}
// A conversation can end before any assistant turn exists to carry the
// budget. Attaching it to an empty assistant message keeps it on the wire
// without inventing visible output; the upstream Chat endpoint treats the
// empty turn as an assistant prefill.
carryBudgetOnly := func() {
if budgetCarried {
return
}
if budget := reasoningBudget(request.Reasoning); budget <= 0 {
return
}
encoded := map[string]any{"role": "assistant", "content": nil}
carryBudget(encoded)
if encoded["reasoning_budget_tokens"] == nil {
return
}
messages = append(messages, encoded)
}
for i, message := range request.Messages {
var content, reasoning, calls, results []bridgeBlock
for _, block := range message.Blocks {
switch block.Kind {
case "reasoning":
reasoning = append(reasoning, block)
case "tool_call":
calls = append(calls, block)
case "tool_result":
results = append(results, block)
default:
content = append(content, block)
}
}
if message.Role == "assistant" {
if len(pending) > 0 {
return nil, fmt.Errorf("messages[%d]: assistant message appears before tool results for %s", i, strings.Join(missingToolIDs(pendingOrder, pending), ", "))
}
if len(results) > 0 {
return nil, fmt.Errorf("messages[%d]: assistant message contains tool results", i)
}
encoded := map[string]any{"role": "assistant", "content": nil}
carryBudget(encoded)
if value, ok := bridgeReasoningText(reasoning); ok {
encoded["reasoning_content"] = value
}
if len(content) > 0 {
encoded["content"] = encodeChatBlocks(content)
}
if len(calls) > 0 {
toolCalls := make([]any, 0, len(calls))
for _, call := range calls {
if call.ID == "" || call.Name == "" {
return nil, fmt.Errorf("messages[%d]: tool call must contain id and name", i)
}
if pending[call.ID] {
return nil, fmt.Errorf("messages[%d]: duplicate tool call id %q", i, call.ID)
}
pending[call.ID] = true
pendingOrder = append(pendingOrder, call.ID)
toolCalls = append(toolCalls, map[string]any{
"id": call.ID,
"type": "function",
"function": map[string]any{
"name": call.Name,
"arguments": bridgeArgumentsJSON(call),
},
})
}
encoded["tool_calls"] = toolCalls
}
if len(content) > 0 || len(reasoning) > 0 || len(calls) > 0 {
messages = append(messages, encoded)
budgetCarried = true
}
continue
}
if len(calls) > 0 {
return nil, fmt.Errorf("messages[%d]: user message contains tool calls", i)
}
for _, result := range results {
if _, duplicate := pendingResults[result.CallID]; duplicate {
return nil, fmt.Errorf("messages[%d]: duplicate tool result %q", i, result.CallID)
}
if !pending[result.CallID] {
return nil, fmt.Errorf("messages[%d]: tool result %q has no pending tool call", i, result.CallID)
}
pendingResults[result.CallID] = result
delete(pending, result.CallID)
}
if len(content) > 0 {
if len(pendingOrder) > 0 || len(deferred) > 0 {
deferred = append(deferred, content)
} else {
messages = append(messages, map[string]any{"role": "user", "content": encodeChatBlocks(content)})
}
}
if len(pending) == 0 && len(pendingOrder) > 0 {
var attachments []map[string]any
for _, id := range pendingOrder {
result := pendingResults[id]
text, images := chatToolImages(result)
if len(images) > 0 {
attachments = append(attachments, map[string]any{"role": "user", "content": encodeChatBlocks(images)})
}
messages = append(messages, map[string]any{
"role": "tool",
"tool_call_id": id,
"content": text,
})
}
for _, attachment := range attachments {
messages = append(messages, attachment)
}
flushDeferred()
pendingOrder = nil
clear(pendingResults)
}
}
if len(pending) > 0 {
return nil, fmt.Errorf("tool calls are missing results for %s", strings.Join(missingToolIDs(pendingOrder, pending), ", "))
}
flushDeferred()
carryBudgetOnly()
output["messages"] = messages
if len(request.Tools) > 0 {
tools := make([]any, 0, len(request.Tools))
for _, tool := range request.Tools {
function := map[string]any{
"name": tool.Name,
"description": tool.Description,
"parameters": schemaOrDefault(tool.Schema),
}
if tool.Strict {
function["strict"] = true
}
tools = append(tools, map[string]any{"type": "function", "function": function})
}
output["tools"] = tools
if choice := encodeChatToolChoice(request.ToolChoice); choice != nil {
output["tool_choice"] = choice
}
}
return output, nil
}
// missingToolIDs lists the tool calls still waiting for a result, for an error
// message: in call order when that accounts for every pending call, and sorted
// otherwise.
func missingToolIDs(order []string, pending map[string]bool) []string {
missing := make([]string, 0, len(pending))
for _, id := range order {
if pending[id] {
missing = append(missing, id)
}
}
if len(missing) == len(pending) {
return missing
}
for id := range pending {
found := false
for _, current := range missing {
if current == id {
found = true
break
}
}
if !found {
missing = append(missing, id)
}
}
sort.Strings(missing)
return missing
}
// responsesReasoning builds the Responses "reasoning" object. A nil result
// omits the field.
//
// A bare effort string, or a reasoning object from which reasoningEffort reads
// a level (a thinking block of type "enabled"/"adaptive" included), is
// projected onto "reasoning.effort". Copying such a block through verbatim
// (the old behavior) forwarded {"type":"adaptive"} and dropped the effort
// completely, so a client asking for "max" reached the upstream with no
// reasoning configuration at all.
//
// An object of type "disabled" yields nil and is omitted; one that yields no
// level is forwarded unchanged, and a bare "none"/"disabled" string yields nil.
func responsesReasoning(value any) any {
if object, ok := value.(map[string]any); ok {
if reasoningDisabled(object) {
return map[string]any{"effort": "none"}
}
// The object is reduced to the level reasoningEffort reads from it:
// an "effort" or "reasoning_effort" the client stated, else the rung