From 5bb94db08c72768d87dd07e676c819f82ea061b4 Mon Sep 17 00:00:00 2001 From: dev Date: Mon, 24 Aug 2026 22:26:51 +0800 Subject: [PATCH] refactor: unify oneLineStr/oneLine into types.OneLine provider.oneLineStr and gateway.oneLine had byte-identical bodies (flatten whitespace + cap length). Move the single implementation into the types package, which both layers already depend on, and delete both locals. --- internal/gateway/chat.go | 15 +++------------ internal/provider/provider.go | 11 ++--------- internal/types/types.go | 14 +++++++++++++- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/internal/gateway/chat.go b/internal/gateway/chat.go index 1c9103c..edcf270 100644 --- a/internal/gateway/chat.go +++ b/internal/gateway/chat.go @@ -528,10 +528,10 @@ func clientUpstreamErr(err error) string { if errors.As(err, &ce) { parts := make([]string, 0, len(ce.Tiers)+len(ce.Skipped)) for _, t := range ce.Tiers { - parts = append(parts, oneLine(fmt.Sprintf("%s/%s: %v", t.Source, t.Model, t.Err), 80)) + parts = append(parts, types.OneLine(fmt.Sprintf("%s/%s: %v", t.Source, t.Model, t.Err), 80)) } for _, sk := range ce.Skipped { - parts = append(parts, oneLine(sk, 80)) + parts = append(parts, types.OneLine(sk, 80)) } msg := strings.Join(parts, "; ") if len(msg) > 300 { @@ -539,16 +539,7 @@ func clientUpstreamErr(err error) string { } return fmt.Sprintf("all %d auto providers failed: %s", len(parts), msg) } - return oneLine(err.Error(), 160) -} - -// oneLine flattens an error string to a single line capped at n chars. -func oneLine(s string, n int) string { - s = strings.Join(strings.Fields(s), " ") - if len(s) > n { - s = s[:n] + "..." - } - return s + return types.OneLine(err.Error(), 160) } func (g *Gateway) singleChat(w http.ResponseWriter, ctx context.Context, cands []*provider.Provider, req *types.ChatRequest, effective string, rec *Req) { diff --git a/internal/provider/provider.go b/internal/provider/provider.go index 49c94c7..f4907a5 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -929,13 +929,6 @@ func errorOnlyChunk(ck types.UnifiedChunk) bool { ck.ReasoningContent == "" && ck.Usage == nil } -func oneLineStr(s string, n int) string { - s = strings.Join(strings.Fields(s), " ") - if len(s) > n { - s = s[:n] + "..." - } - return s -} // apiErrReason builds the client-facing reason for a non-200 upstream // response: the adapter's optional transform_error hook wins (per-source // protocol knowledge lives in Lua), otherwise clients get a uniform @@ -943,11 +936,11 @@ func oneLineStr(s string, n int) string { func (p *Provider) apiErrReason(status int, raw string) string { if reason, ok, err := p.vm.TransformError(p.adapter, status, raw); err == nil && ok { if trimmed := strings.TrimSpace(reason); trimmed != "" { - return fmt.Sprintf("api error %d: %s", status, oneLineStr(trimmed, 200)) + return fmt.Sprintf("api error %d: %s", status, types.OneLine(trimmed, 200)) } } log.Printf("[provider] unhandled upstream error body (adapter %q lacks transform_error): status=%d body=%.300s", - p.adapter, status, oneLineStr(raw, 300)) + p.adapter, status, types.OneLine(raw, 300)) return fmt.Sprintf("api error %d: unknown error", status) } diff --git a/internal/types/types.go b/internal/types/types.go index a23665d..2bf29b8 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -5,6 +5,7 @@ package types import ( "encoding/json" "errors" + "strings" "time" ) @@ -159,4 +160,15 @@ type BuildMeta struct { Source map[string]interface{} `json:"source"` } -func Now() int64 { return time.Now().Unix() } \ No newline at end of file +func Now() int64 { return time.Now().Unix() } + +// OneLine flattens an error string to a single line capped at n chars. +// Shared by the provider layer (upstream error reasons) and the gateway +// layer (client-facing AUTO-chain summaries). +func OneLine(s string, n int) string { + s = strings.Join(strings.Fields(s), " ") + if len(s) > n { + s = s[:n] + "..." + } + return s +} \ No newline at end of file