mirror of
https://github.com/QuantumNous/new-api.git
synced 2026-09-13 15:54:34 +00:00
feat(relay): hosted-tool conversion fidelity, reasoning normalization, and billing usage integrity (#7137)
* feat(relaykit): preserve hosted tools across conversions - add protocol-neutral hosted-tool DTOs, conversion metadata, and loss policies - bridge citations, grounding metadata, and hosted-tool stream lifecycles - document the public conversion behavior and channel policy controls * refactor(relaykit): normalize reasoning and thinking intent - centralize provider-neutral reasoning intent, effort, and budget mappings - parse model suffixes at the host entry boundary while preserving provider-owned tails - keep adaptive Claude thinking and explicit zero-token compatibility consistent * fix(billing): preserve authoritative usage across relay hops - carry native BillingUsage sidecars through direct and streamed protocol bridges - merge partial and terminal usage monotonically with safe fallback settlement - retain cache metadata, penultimate usage, and per-call Gemini tool surcharges * feat(relay): bridge Responses with Claude and Gemini protocols - add direct request, response, and stream converters across supported relay formats - expose Claude count_tokens and Chat-to-Responses compatibility endpoints - carry conversion diagnostics through the host while retaining the curated public goldens * fix(relay): wire relaykit conversions into host channels - connect handlers, adaptors, and channel settings to the standalone conversion layer - keep model mapping, pricing identity, retries, and provider-specific suffix behavior aligned - ignore local audit artifacts and retain focused public regression coverage
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/QuantumNous/new-api/logger"
|
||||
"github.com/QuantumNous/new-api/relaykit/types"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const maxConversionDiagnostics = 32
|
||||
|
||||
type conversionDiagnosticKey struct {
|
||||
code string
|
||||
path string
|
||||
severity types.ConversionDiagnosticSeverity
|
||||
from types.RelayFormat
|
||||
to types.RelayFormat
|
||||
}
|
||||
|
||||
// RecordConversionDiagnostics retains conversion losses for the consume log
|
||||
// and emits one request-correlated warning per distinct diagnostic. The cap
|
||||
// prevents malformed streams from growing request state without bound.
|
||||
func (info *RelayInfo) RecordConversionDiagnostics(ctx context.Context, diagnostics []types.ConversionDiagnostic) {
|
||||
if info == nil || len(diagnostics) == 0 {
|
||||
return
|
||||
}
|
||||
if ginCtx, ok := ctx.(*gin.Context); ok && ginCtx == nil {
|
||||
ctx = nil
|
||||
}
|
||||
if info.conversionDiagnosticKeys == nil {
|
||||
info.conversionDiagnosticKeys = make(map[conversionDiagnosticKey]struct{})
|
||||
}
|
||||
for _, diagnostic := range diagnostics {
|
||||
key := conversionDiagnosticKey{
|
||||
code: diagnostic.Code,
|
||||
path: diagnostic.Path,
|
||||
severity: diagnostic.Severity,
|
||||
from: diagnostic.From,
|
||||
to: diagnostic.To,
|
||||
}
|
||||
if _, exists := info.conversionDiagnosticKeys[key]; exists {
|
||||
continue
|
||||
}
|
||||
if len(info.conversionDiagnostics) >= maxConversionDiagnostics {
|
||||
if !info.conversionDiagnosticsTruncated {
|
||||
info.conversionDiagnosticsTruncated = true
|
||||
logger.LogWarn(ctx, fmt.Sprintf("conversion diagnostics truncated after %d distinct entries", maxConversionDiagnostics))
|
||||
}
|
||||
continue
|
||||
}
|
||||
info.conversionDiagnosticKeys[key] = struct{}{}
|
||||
info.conversionDiagnostics = append(info.conversionDiagnostics, diagnostic)
|
||||
logger.LogWarn(ctx, fmt.Sprintf(
|
||||
"conversion diagnostic: code=%q severity=%q from=%q to=%q path=%q message=%q",
|
||||
diagnostic.Code, diagnostic.Severity, diagnostic.From, diagnostic.To, diagnostic.Path, diagnostic.Message,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
func (info *RelayInfo) ConversionDiagnostics() []types.ConversionDiagnostic {
|
||||
if info == nil || len(info.conversionDiagnostics) == 0 {
|
||||
return nil
|
||||
}
|
||||
return append([]types.ConversionDiagnostic(nil), info.conversionDiagnostics...)
|
||||
}
|
||||
|
||||
func (info *RelayInfo) ConversionDiagnosticsTruncated() bool {
|
||||
return info != nil && info.conversionDiagnosticsTruncated
|
||||
}
|
||||
Reference in New Issue
Block a user