diff --git a/internal/runtime/executor/logging_helpers.go b/internal/runtime/executor/logging_helpers.go new file mode 100644 index 00000000..79f4590f --- /dev/null +++ b/internal/runtime/executor/logging_helpers.go @@ -0,0 +1,41 @@ +package executor + +import ( + "bytes" + "context" + + "github.com/gin-gonic/gin" + "github.com/router-for-me/CLIProxyAPI/v6/internal/config" +) + +// recordAPIRequest stores the upstream request payload in Gin context for request logging. +func recordAPIRequest(ctx context.Context, cfg *config.Config, payload []byte) { + if cfg == nil || !cfg.RequestLog || len(payload) == 0 { + return + } + if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil { + ginCtx.Set("API_REQUEST", bytes.Clone(payload)) + } +} + +// appendAPIResponseChunk appends an upstream response chunk to Gin context for request logging. +func appendAPIResponseChunk(ctx context.Context, cfg *config.Config, chunk []byte) { + if cfg == nil || !cfg.RequestLog { + return + } + data := bytes.TrimSpace(bytes.Clone(chunk)) + if len(data) == 0 { + return + } + if ginCtx, ok := ctx.Value("gin").(*gin.Context); ok && ginCtx != nil { + if existing, exists := ginCtx.Get("API_RESPONSE"); exists { + if prev, okBytes := existing.([]byte); okBytes { + prev = append(prev, data...) + prev = append(prev, []byte("\n\n")...) + ginCtx.Set("API_RESPONSE", prev) + return + } + } + ginCtx.Set("API_RESPONSE", data) + } +} diff --git a/sdk/cliproxy/auth/manager.go b/sdk/cliproxy/auth/manager.go index 8548e3fc..8a8ef952 100644 --- a/sdk/cliproxy/auth/manager.go +++ b/sdk/cliproxy/auth/manager.go @@ -254,10 +254,13 @@ func (m *Manager) executeWithProvider(ctx context.Context, provider string, req return cliproxyexecutor.Response{}, errPick } - if isAPIKey, info := auth.AccountInfo(); isAPIKey { - log.Debugf("Use API key %s for model %s", util.HideAPIKey(info), req.Model) - } else { - log.Debugf("Use OAuth %s for model %s", info, req.Model) + accountType, accountInfo := auth.AccountInfo() + if accountType == "api_key" { + log.Debugf("Use API key %s for model %s", util.HideAPIKey(accountInfo), req.Model) + } else if accountType == "oauth" { + log.Debugf("Use OAuth %s for model %s", accountInfo, req.Model) + } else if accountType == "cookie" { + log.Debugf("Use Cookie %s for model %s", util.HideAPIKey(accountInfo), req.Model) } tried[auth.ID] = struct{}{} @@ -298,10 +301,13 @@ func (m *Manager) executeStreamWithProvider(ctx context.Context, provider string return nil, errPick } - if isAPIKey, info := auth.AccountInfo(); isAPIKey { - log.Debugf("Use API key %s for model %s", util.HideAPIKey(info), req.Model) - } else { - log.Debugf("Use OAuth %s for model %s", info, req.Model) + accountType, accountInfo := auth.AccountInfo() + if accountType == "api_key" { + log.Debugf("Use API key %s for model %s", util.HideAPIKey(accountInfo), req.Model) + } else if accountType == "oauth" { + log.Debugf("Use OAuth %s for model %s", accountInfo, req.Model) + } else if accountType == "cookie" { + log.Debugf("Use Cookie %s for model %s", util.HideAPIKey(accountInfo), req.Model) } tried[auth.ID] = struct{}{} diff --git a/sdk/cliproxy/auth/types.go b/sdk/cliproxy/auth/types.go index 912bc36f..bbaba60b 100644 --- a/sdk/cliproxy/auth/types.go +++ b/sdk/cliproxy/auth/types.go @@ -80,38 +80,38 @@ func (a *Auth) Clone() *Auth { return ©Auth } -func (a *Auth) AccountInfo() (bool, string) { +func (a *Auth) AccountInfo() (string, string) { if a == nil { - return false, "" + return "", "" } if strings.ToLower(a.Provider) == "gemini-web" { if a.Metadata != nil { if v, ok := a.Metadata["secure_1psid"].(string); ok && v != "" { - return true, v + return "cookie", v } if v, ok := a.Metadata["__Secure-1PSID"].(string); ok && v != "" { - return true, v + return "cookie", v } } if a.Attributes != nil { if v := a.Attributes["secure_1psid"]; v != "" { - return true, v + return "cookie", v } if v := a.Attributes["api_key"]; v != "" { - return true, v + return "cookie", v } } } if a.Metadata != nil { if v, ok := a.Metadata["email"].(string); ok { - return false, v + return "oauth", v } } else if a.Attributes != nil { if v := a.Attributes["api_key"]; v != "" { - return true, v + return "api_key", v } } - return false, "" + return "", "" } // ExpirationTime attempts to extract the credential expiration timestamp from metadata.