Align default space and Hunyuan 3 behavior with hygate

This commit is contained in:
Luxferre
2026-09-07 11:05:20 +03:00
parent 187edabadb
commit c5ea129b44
3 changed files with 149 additions and 51 deletions
+58 -33
View File
@@ -28,6 +28,7 @@ var (
DefaultSpaceURL = "https://tencent-hy3.hf.space"
DefaultUserAgent = "Mozilla/5.0 (X11; Linux x86_64; rv:153.0) Gecko/20100101 Firefox/153.0"
ConfiguredUserAgent string
ConfiguredModelName string
)
// ---------------------------------------------------------------------------
@@ -1613,6 +1614,16 @@ func (d *SpaceDiscovery) GetModelList() []ModelItem {
var items []ModelItem
seen := make(map[string]bool)
if ConfiguredModelName != "" {
seen[ConfiguredModelName] = true
items = append(items, ModelItem{
ID: ConfiguredModelName,
Object: "model",
Created: now,
OwnedBy: "gradio",
})
}
for _, m := range d.Models {
if m != "" && !seen[m] {
seen[m] = true
@@ -2183,20 +2194,11 @@ func (g *GradioGateway) BuildGradioPayload(disc *SpaceDiscovery, req ChatComplet
toolName = lastMsg.ToolCallID
}
if disc.IsHunyuan3 {
toolItem := map[string]interface{}{
"role": "tool",
"content": lastContent,
if toolName != "" {
lastUserMessage = fmt.Sprintf("Tool result for %s: %s", toolName, lastContent)
} else {
lastUserMessage = lastContent
}
if lastMsg.ToolCallID != "" {
toolItem["tool_call_id"] = lastMsg.ToolCallID
} else if toolName != "" {
toolItem["tool_call_id"] = toolName
}
if lastMsg.Name != "" {
toolItem["name"] = lastMsg.Name
}
historyArray = append(historyArray, toolItem)
lastUserMessage = "Please proceed based on the tool results."
} else {
if toolName != "" {
lastUserMessage = fmt.Sprintf("Tool result for %s: %s\nPlease answer the user's request based on the tool result.", toolName, lastContent)
@@ -2393,34 +2395,39 @@ func ParseGradioStreamOutput(rawJSON string) GradioOutputFrame {
// Check if v[0] is an inner slice (e.g. Hy3: [[content, reasoning, tool_calls, history]])
if inner, ok := v[0].([]interface{}); ok {
if len(inner) >= 2 {
if len(inner) >= 3 {
s0, _ := inner[0].(string)
s1, _ := inner[1].(string)
frame.Content = s0
frame.Reasoning = s1
if inner[2] != nil {
b, err := json.Marshal(inner[2])
if err == nil {
var tcs []ToolCall
if json.Unmarshal(b, &tcs) == nil && len(tcs) > 0 {
frame.ToolCalls = tcs
}
}
}
frame.OK = true
return frame
}
if len(inner) == 2 {
s0, ok0 := inner[0].(string)
s1, ok1 := inner[1].(string)
if ok0 && ok1 {
frame.Content = s0
frame.Reasoning = s1
if len(inner) >= 3 {
if tcSlice, ok := inner[2].([]interface{}); ok && len(tcSlice) > 0 {
b, err := json.Marshal(tcSlice)
if err == nil {
var tcs []ToolCall
if err := json.Unmarshal(b, &tcs); err == nil {
frame.ToolCalls = tcs
}
}
}
}
frame.OK = true
return frame
}
// Check if inner is a chat pair: ["user msg", "assistant msg"]
if len(inner) == 2 {
if aStr, ok := inner[1].(string); ok {
frame.Content = aStr
frame.OK = true
return frame
}
if ok1 {
frame.Content = s1
frame.OK = true
return frame
}
}
@@ -2544,7 +2551,11 @@ func (g *GradioGateway) ExecuteChatCompletion(w http.ResponseWriter, r *http.Req
modelName := req.Model
if modelName == "" {
modelName = disc.PrimaryModel
if ConfiguredModelName != "" {
modelName = ConfiguredModelName
} else {
modelName = disc.PrimaryModel
}
}
gradioData, err := g.BuildGradioPayload(disc, req)
@@ -2714,7 +2725,16 @@ func (g *GradioGateway) ExecuteChatCompletion(w http.ResponseWriter, r *http.Req
return fmt.Errorf("upstream Gradio error: %s", errMsg)
}
if frame := ParseGradioStreamOutput(dataStr); frame.OK {
latestFrame = frame
if frame.Content != "" || latestFrame.Content == "" {
latestFrame.Content = frame.Content
}
if frame.Reasoning != "" || latestFrame.Reasoning == "" {
latestFrame.Reasoning = frame.Reasoning
}
if len(frame.ToolCalls) > 0 {
latestFrame.ToolCalls = frame.ToolCalls
}
latestFrame.OK = true
}
if currentEvent == "complete" {
break
@@ -2982,6 +3002,8 @@ func (g *GradioGateway) ExecuteChatCompletion(w http.ResponseWriter, r *http.Req
func main() {
spaceFlag := flag.String("space", DefaultSpaceURL, "Target Gradio Space URL")
flag.StringVar(spaceFlag, "url", DefaultSpaceURL, "Alias for -space")
flag.StringVar(spaceFlag, "endpoint", DefaultSpaceURL, "Alias for -space")
modelFlag := flag.String("model", "", "Exposed model name override (default: auto-detected)")
portFlag := flag.Int("port", 8080, "Gateway HTTP server port")
hostFlag := flag.String("host", "0.0.0.0", "Gateway HTTP server host")
socksFlag := flag.String("socks", "", "Optional SOCKS5 proxy URL (e.g. socks5://127.0.0.1:1080)")
@@ -3007,6 +3029,9 @@ func main() {
if *uaFlag != "" {
ConfiguredUserAgent = *uaFlag
}
if *modelFlag != "" {
ConfiguredModelName = *modelFlag
}
gateway := NewGradioGateway(*spaceFlag, *socksFlag, time.Duration(*timeoutFlag)*time.Second)