From b9a3f8d5cd73fc10a9ea4333b04c3cb3dd153ab9 Mon Sep 17 00:00:00 2001 From: m1saka Date: Fri, 21 Aug 2026 14:27:37 +0800 Subject: [PATCH] Mirror Zhanlu client: GLM tool_stream, shared v4 UUID request id, drop chat state:ERROR --- internal/openai/types.go | 10 +++++++++- internal/server/server.go | 6 +----- internal/util/util.go | 21 ++++++++++++++++++++- internal/zhanlu/client.go | 12 ++---------- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/internal/openai/types.go b/internal/openai/types.go index 5bed020..85c5ea5 100644 --- a/internal/openai/types.go +++ b/internal/openai/types.go @@ -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) } diff --git a/internal/server/server.go b/internal/server/server.go index a8b3114..ba214b7 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -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 } diff --git a/internal/util/util.go b/internal/util/util.go index 9697e43..c01efd4 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -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:]) +} diff --git a/internal/zhanlu/client.go b/internal/zhanlu/client.go index eb9ea6e..ab2da37 100644 --- a/internal/zhanlu/client.go +++ b/internal/zhanlu/client.go @@ -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:]) -} +