mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-02 20:40:52 +08:00
refactor(thinking): refine configuration logging
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
@@ -52,11 +53,31 @@ func (m *LogFormatter) Format(entry *log.Entry) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
levelStr := fmt.Sprintf("%-5s", level)
|
levelStr := fmt.Sprintf("%-5s", level)
|
||||||
|
|
||||||
|
// Build fields string (excluding request_id which is already shown)
|
||||||
|
var fieldsStr string
|
||||||
|
if len(entry.Data) > 0 {
|
||||||
|
var keys []string
|
||||||
|
for k := range entry.Data {
|
||||||
|
if k == "request_id" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
var fields []string
|
||||||
|
for _, k := range keys {
|
||||||
|
fields = append(fields, fmt.Sprintf("%s=%v", k, entry.Data[k]))
|
||||||
|
}
|
||||||
|
if len(fields) > 0 {
|
||||||
|
fieldsStr = " " + strings.Join(fields, " ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var formatted string
|
var formatted string
|
||||||
if entry.Caller != nil {
|
if entry.Caller != nil {
|
||||||
formatted = fmt.Sprintf("[%s] [%s] [%s] [%s:%d] %s\n", timestamp, reqID, levelStr, filepath.Base(entry.Caller.File), entry.Caller.Line, message)
|
formatted = fmt.Sprintf("[%s] [%s] [%s] [%s:%d] %s%s\n", timestamp, reqID, levelStr, filepath.Base(entry.Caller.File), entry.Caller.Line, message, fieldsStr)
|
||||||
} else {
|
} else {
|
||||||
formatted = fmt.Sprintf("[%s] [%s] [%s] %s\n", timestamp, reqID, levelStr, message)
|
formatted = fmt.Sprintf("[%s] [%s] [%s] %s%s\n", timestamp, reqID, levelStr, message, fieldsStr)
|
||||||
}
|
}
|
||||||
buffer.WriteString(formatted)
|
buffer.WriteString(formatted)
|
||||||
|
|
||||||
|
|||||||
@@ -117,18 +117,21 @@ func ApplyThinking(body []byte, model string, provider string) ([]byte, error) {
|
|||||||
if suffixResult.HasSuffix {
|
if suffixResult.HasSuffix {
|
||||||
config = parseSuffixToConfig(suffixResult.RawSuffix)
|
config = parseSuffixToConfig(suffixResult.RawSuffix)
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"provider": provider,
|
"provider": provider,
|
||||||
"model": model,
|
"model": model,
|
||||||
"raw_suffix": suffixResult.RawSuffix,
|
"mode": config.Mode,
|
||||||
"config": config,
|
"budget": config.Budget,
|
||||||
}).Debug("thinking: using suffix config (priority)")
|
"level": config.Level,
|
||||||
|
}).Debug("thinking: config from model suffix")
|
||||||
} else {
|
} else {
|
||||||
config = extractThinkingConfig(body, provider)
|
config = extractThinkingConfig(body, provider)
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"provider": provider,
|
"provider": provider,
|
||||||
"model": modelInfo.ID,
|
"model": modelInfo.ID,
|
||||||
"config": config,
|
"mode": config.Mode,
|
||||||
}).Debug("thinking: extracted config from request body")
|
"budget": config.Budget,
|
||||||
|
"level": config.Level,
|
||||||
|
}).Debug("thinking: original config from request")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !hasThinkingConfig(config) {
|
if !hasThinkingConfig(config) {
|
||||||
@@ -163,10 +166,12 @@ func ApplyThinking(body []byte, model string, provider string) ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"provider": provider,
|
"provider": provider,
|
||||||
"model": modelInfo.ID,
|
"model": modelInfo.ID,
|
||||||
"validated": *validated,
|
"mode": validated.Mode,
|
||||||
}).Debug("thinking: applying validated config")
|
"budget": validated.Budget,
|
||||||
|
"level": validated.Level,
|
||||||
|
}).Debug("thinking: processed config to apply")
|
||||||
|
|
||||||
// 6. Apply configuration using provider-specific applier
|
// 6. Apply configuration using provider-specific applier
|
||||||
return applier.Apply(body, *validated, modelInfo)
|
return applier.Apply(body, *validated, modelInfo)
|
||||||
|
|||||||
@@ -20,6 +20,22 @@ const (
|
|||||||
ModeAuto
|
ModeAuto
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// String returns the string representation of ThinkingMode.
|
||||||
|
func (m ThinkingMode) String() string {
|
||||||
|
switch m {
|
||||||
|
case ModeBudget:
|
||||||
|
return "budget"
|
||||||
|
case ModeLevel:
|
||||||
|
return "level"
|
||||||
|
case ModeNone:
|
||||||
|
return "none"
|
||||||
|
case ModeAuto:
|
||||||
|
return "auto"
|
||||||
|
default:
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ThinkingLevel represents a discrete thinking level.
|
// ThinkingLevel represents a discrete thinking level.
|
||||||
type ThinkingLevel string
|
type ThinkingLevel string
|
||||||
|
|
||||||
|
|||||||
@@ -591,8 +591,8 @@ func (m *Manager) executeMixedOnce(ctx context.Context, providers []string, req
|
|||||||
execCtx = context.WithValue(execCtx, "cliproxy.roundtripper", rt)
|
execCtx = context.WithValue(execCtx, "cliproxy.roundtripper", rt)
|
||||||
}
|
}
|
||||||
execReq := req
|
execReq := req
|
||||||
execReq.Model, execReq.Metadata = rewriteModelForAuth(routeModel, req.Metadata, auth)
|
execReq.Model = rewriteModelForAuth(routeModel, auth)
|
||||||
execReq.Model, execReq.Metadata = m.applyOAuthModelMapping(auth, execReq.Model, execReq.Metadata)
|
execReq.Model = m.applyOAuthModelMapping(auth, execReq.Model)
|
||||||
resp, errExec := executor.Execute(execCtx, auth, execReq, opts)
|
resp, errExec := executor.Execute(execCtx, auth, execReq, opts)
|
||||||
result := Result{AuthID: auth.ID, Provider: provider, Model: routeModel, Success: errExec == nil}
|
result := Result{AuthID: auth.ID, Provider: provider, Model: routeModel, Success: errExec == nil}
|
||||||
if errExec != nil {
|
if errExec != nil {
|
||||||
@@ -639,8 +639,8 @@ func (m *Manager) executeCountMixedOnce(ctx context.Context, providers []string,
|
|||||||
execCtx = context.WithValue(execCtx, "cliproxy.roundtripper", rt)
|
execCtx = context.WithValue(execCtx, "cliproxy.roundtripper", rt)
|
||||||
}
|
}
|
||||||
execReq := req
|
execReq := req
|
||||||
execReq.Model, execReq.Metadata = rewriteModelForAuth(routeModel, req.Metadata, auth)
|
execReq.Model = rewriteModelForAuth(routeModel, auth)
|
||||||
execReq.Model, execReq.Metadata = m.applyOAuthModelMapping(auth, execReq.Model, execReq.Metadata)
|
execReq.Model = m.applyOAuthModelMapping(auth, execReq.Model)
|
||||||
resp, errExec := executor.CountTokens(execCtx, auth, execReq, opts)
|
resp, errExec := executor.CountTokens(execCtx, auth, execReq, opts)
|
||||||
result := Result{AuthID: auth.ID, Provider: provider, Model: routeModel, Success: errExec == nil}
|
result := Result{AuthID: auth.ID, Provider: provider, Model: routeModel, Success: errExec == nil}
|
||||||
if errExec != nil {
|
if errExec != nil {
|
||||||
@@ -687,8 +687,8 @@ func (m *Manager) executeStreamMixedOnce(ctx context.Context, providers []string
|
|||||||
execCtx = context.WithValue(execCtx, "cliproxy.roundtripper", rt)
|
execCtx = context.WithValue(execCtx, "cliproxy.roundtripper", rt)
|
||||||
}
|
}
|
||||||
execReq := req
|
execReq := req
|
||||||
execReq.Model, execReq.Metadata = rewriteModelForAuth(routeModel, req.Metadata, auth)
|
execReq.Model = rewriteModelForAuth(routeModel, auth)
|
||||||
execReq.Model, execReq.Metadata = m.applyOAuthModelMapping(auth, execReq.Model, execReq.Metadata)
|
execReq.Model = m.applyOAuthModelMapping(auth, execReq.Model)
|
||||||
chunks, errStream := executor.ExecuteStream(execCtx, auth, execReq, opts)
|
chunks, errStream := executor.ExecuteStream(execCtx, auth, execReq, opts)
|
||||||
if errStream != nil {
|
if errStream != nil {
|
||||||
rerr := &Error{Message: errStream.Error()}
|
rerr := &Error{Message: errStream.Error()}
|
||||||
|
|||||||
Reference in New Issue
Block a user