mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
The v2 gate fingerprints the body as well as the headers, and five details still differed from a captured 2.1.1 request: - The client sends max_completion_tokens: 32000 on every chat body. The gateway now adds it when the caller asked for no ceiling; an explicit max_tokens or max_completion_tokens still wins. - Every client tool carries "strict": false, so a tool that omits the field is byte-different from the client's on every request that carries tools. - The shell tool description names the shell the client actually has, so a Windows host advertises a different one than the captured Unix wording. - Chat bodies keep the client's key order, model first. Go writes map keys alphabetically, which puts max_completion_tokens at the front where a real client starts with model. - Bodies are encoded without HTML escaping. encoding/json writes \u003c, \u003e and \u0026 by default where JSON.stringify writes the characters, and tool descriptions are full of both. The ordered encoder falls back to plain encoding/json for every lane except the v2 chat one, and both re-encode points in the request path use it. Co-authored-by: AiFLYF <AiFLYF@users.noreply.github.com>
23 lines
771 B
Go
23 lines
771 B
Go
package jsonutil
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
)
|
|
|
|
// Marshal encodes value the way the JavaScript clients this gateway imitates
|
|
// do. encoding/json escapes '<', '>' and '&' as \u003c, \u003e and \u0026 by
|
|
// default; JSON.stringify never does. A body carrying those escapes is
|
|
// byte-different from a real client's on every request whose text contains an
|
|
// angle bracket or ampersand, and tool descriptions and code snippets are full
|
|
// of both, so upstream request bodies go through here instead.
|
|
func Marshal(value any) ([]byte, error) {
|
|
var buffer bytes.Buffer
|
|
encoder := json.NewEncoder(&buffer)
|
|
encoder.SetEscapeHTML(false)
|
|
if err := encoder.Encode(value); err != nil {
|
|
return nil, err
|
|
}
|
|
return bytes.TrimSuffix(buffer.Bytes(), []byte("\n")), nil
|
|
}
|