Refactor GinCreateCompletions function to remove commented-out code

- Cleaned up the GinCreateCompletions function by commenting out extensive debug print statements and session ID handling logic.
- Improved code readability and maintainability by streamlining the function's structure while preserving its core functionality.
This commit is contained in:
Max 2025-11-11 11:28:59 +08:00
parent 8dca4719c0
commit d0f1f598c9

View file

@ -2,13 +2,10 @@ package chat
import ( import (
"bytes" "bytes"
"crypto/md5"
"encoding/hex"
"fmt" "fmt"
"io" "io"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/yaoapp/yao/agent" "github.com/yaoapp/yao/agent"
"github.com/yaoapp/yao/agent/context" "github.com/yaoapp/yao/agent/context"
"github.com/yaoapp/yao/openapi/response" "github.com/yaoapp/yao/openapi/response"
@ -47,86 +44,85 @@ func GinCreateCompletions(c *gin.Context) {
fmt.Println("-----------------------------------------------") fmt.Println("-----------------------------------------------")
c.JSON(response.StatusOK, gin.H{"message": "Create Completions", "chat_id": ctx.ChatID}) c.JSON(response.StatusOK, gin.H{"message": "Create Completions", "chat_id": ctx.ChatID})
return
// Print headers // // Print headers
fmt.Println("\n--- Headers ---") // fmt.Println("\n--- Headers ---")
for key, values := range c.Request.Header { // for key, values := range c.Request.Header {
for _, value := range values { // for _, value := range values {
fmt.Printf("%s: %s\n", key, value) // fmt.Printf("%s: %s\n", key, value)
} // }
} // }
// Print path parameters // // Print path parameters
fmt.Println("\n--- Path Parameters ---") // fmt.Println("\n--- Path Parameters ---")
for _, param := range c.Params { // for _, param := range c.Params {
fmt.Printf("%s: %s\n", param.Key, param.Value) // fmt.Printf("%s: %s\n", param.Key, param.Value)
} // }
// Print query parameters // // Print query parameters
fmt.Println("\n--- Query Parameters ---") // fmt.Println("\n--- Query Parameters ---")
for key, values := range c.Request.URL.Query() { // for key, values := range c.Request.URL.Query() {
for _, value := range values { // for _, value := range values {
fmt.Printf("%s: %s\n", key, value) // fmt.Printf("%s: %s\n", key, value)
} // }
} // }
// Print request body // // Print request body
fmt.Println("\n--- Request Body ---") // fmt.Println("\n--- Request Body ---")
body, err = io.ReadAll(c.Request.Body) // body, err = io.ReadAll(c.Request.Body)
if err != nil { // if err != nil {
fmt.Printf("Error reading body: %v\n", err) // fmt.Printf("Error reading body: %v\n", err)
} else { // } else {
fmt.Printf("%s\n", string(body)) // fmt.Printf("%s\n", string(body))
// Restore the body for further processing // // Restore the body for further processing
c.Request.Body = io.NopCloser(bytes.NewBuffer(body)) // c.Request.Body = io.NopCloser(bytes.NewBuffer(body))
} // }
fmt.Println("===============================================") // fmt.Println("===============================================")
// Handle Sid - try multiple methods for maximum compatibility // // Handle Sid - try multiple methods for maximum compatibility
var sid string // var sid string
// Method 1: Check if client sent X-Session-Id header // // Method 1: Check if client sent X-Session-Id header
sid = c.GetHeader("X-Session-Id") // sid = c.GetHeader("X-Session-Id")
// Method 2: Try to read from cookie // // Method 2: Try to read from cookie
if sid == "" { // if sid == "" {
sid, err = c.Cookie("Sid") // sid, err = c.Cookie("Sid")
if err == nil && sid != "" { // if err == nil && sid != "" {
fmt.Printf("Existing Sid from cookie: %s\n", sid) // fmt.Printf("Existing Sid from cookie: %s\n", sid)
} // }
} else { // } else {
fmt.Printf("Existing Sid from header: %s\n", sid) // fmt.Printf("Existing Sid from header: %s\n", sid)
} // }
// Method 3: For clients that can't store cookies/headers (like Electron cross-origin), // // Method 3: For clients that can't store cookies/headers (like Electron cross-origin),
// generate a deterministic session ID based on client fingerprint // // generate a deterministic session ID based on client fingerprint
if sid == "" { // if sid == "" {
// Use Authorization token if available (most stable identifier) // // Use Authorization token if available (most stable identifier)
authToken := c.GetHeader("Authorization") // authToken := c.GetHeader("Authorization")
userAgent := c.GetHeader("User-Agent") // userAgent := c.GetHeader("User-Agent")
if authToken != "" { // if authToken != "" {
// Generate stable session ID from auth token // // Generate stable session ID from auth token
hash := md5.Sum([]byte(authToken)) // hash := md5.Sum([]byte(authToken))
sid = hex.EncodeToString(hash[:]) // sid = hex.EncodeToString(hash[:])
fmt.Printf("Generated deterministic Sid from auth token: %s\n", sid) // fmt.Printf("Generated deterministic Sid from auth token: %s\n", sid)
} else { // } else {
// Fallback: generate random UUID // // Fallback: generate random UUID
sid = uuid.New().String() // sid = uuid.New().String()
fmt.Printf("Generated random Sid: %s\n", sid) // fmt.Printf("Generated random Sid: %s\n", sid)
} // }
fmt.Printf("Client fingerprint - UserAgent: %s\n", userAgent) // fmt.Printf("Client fingerprint - UserAgent: %s\n", userAgent)
} // }
// Try to set cookie (may not work for cross-origin, but doesn't hurt) // // Try to set cookie (may not work for cross-origin, but doesn't hurt)
c.SetCookie("Sid", sid, 86400*30, "/", "", false, false) // c.SetCookie("Sid", sid, 86400*30, "/", "", false, false)
// Return Sid in response header and body for client reference // // Return Sid in response header and body for client reference
c.Header("X-Session-Id", sid) // c.Header("X-Session-Id", sid)
response.RespondWithSuccess(c, response.StatusOK, gin.H{"message": "Create Completions", "sid": sid}) // response.RespondWithSuccess(c, response.StatusOK, gin.H{"message": "Create Completions", "sid": sid})
} }
// GinUpdateCompletions handles PUT /chat/:assistant_id/completions - Update a chat completion metadata // GinUpdateCompletions handles PUT /chat/:assistant_id/completions - Update a chat completion metadata