Mirror Zhanlu client: GLM tool_stream, shared v4 UUID request id, drop chat state:ERROR
build / build (push) Successful in 2m26s

This commit is contained in:
2026-08-21 14:27:37 +08:00
parent a0a2049440
commit b9a3f8d5cd
4 changed files with 32 additions and 17 deletions
+9 -1
View File
@@ -1,6 +1,9 @@
package openai
import "encoding/json"
import (
"encoding/json"
"strings"
)
type ChatCompletionRequest struct {
Model string `json:"model"`
@@ -40,6 +43,11 @@ func (r ChatCompletionRequest) MarshalForUpstream() ([]byte, error) {
m[k] = anyValue
}
}
// Mirror the Zhanlu plugin: GLM models require {tool_stream:true} to
// stream tool calls back to the client (matches /glm/i.test(model)).
if strings.Contains(strings.ToLower(r.Model), "glm") {
m["tool_stream"] = true
}
return json.Marshal(m)
}
+1 -5
View File
@@ -440,7 +440,7 @@ func (s *Server) postPhoneAPI(endpoint string, payload map[string]string, out *p
req.Header.Set("Content-Type", "application/json")
req.Header.Set("plugin_type", "zhanlu_ide")
req.Header.Set("plugin_version", s.cfg.PluginVersion)
req.Header.Set("request", randomRequestID())
req.Header.Set("request", util.RandomRequestID())
resp, err := s.upstreamHTTPClient().Do(req)
if err != nil {
return err
@@ -1093,10 +1093,6 @@ func forEachSSEChunk(r io.Reader, fn func([]byte) error) error {
if payload == "" || payload == "[DONE]" {
continue
}
var upstreamError map[string]any
if json.Unmarshal([]byte(payload), &upstreamError) == nil && upstreamError["state"] == "ERROR" {
return fmt.Errorf("zhanlu upstream error: %v", upstreamError["errorMessage"])
}
if err := fn([]byte(payload)); err != nil {
return err
}
+20 -1
View File
@@ -2,7 +2,12 @@
// avoid divergent same-named copies.
package util
import "strings"
import (
"crypto/rand"
"fmt"
"strings"
"time"
)
// FirstNonEmpty returns the first trimmed-non-empty value, or "" when none of
// the values are non-empty. Callers that need a specific fallback for the
@@ -15,3 +20,17 @@ func FirstNonEmpty(values ...string) string {
}
return ""
}
// RandomRequestID returns a random v4 UUID string, matching the format the
// Zhanlu plugin sends in the `request` header of every gateway call
// (crypto.randomUUID()). Extracted so the login and phone-code paths share
// one implementation.
func RandomRequestID() string {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}
+2 -10
View File
@@ -210,7 +210,7 @@ func (c *Client) setPluginHeaders(req *http.Request) {
req.Header.Set("Content-Type", "application/json")
req.Header.Set("plugin_type", "zhanlu_ide")
req.Header.Set("plugin_version", c.PluginVersion)
req.Header.Set("request", randomRequestID())
req.Header.Set("request", util.RandomRequestID())
}
func decodeJSON(resp *http.Response, out any) error {
@@ -249,12 +249,4 @@ func randomAlnum(n int) string {
return string(b)
}
func randomRequestID() string {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
b[6] = (b[6] & 0x0f) | 0x40
b[8] = (b[8] & 0x3f) | 0x80
return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}