mirror of
https://github.com/Finesssee/ProxyPilot.git
synced 2026-11-19 02:17:46 +00:00
- Updated all executors with context cancellation handling - Added payload rules using requested model - Filter out Top_P when Temperature is set on Claude - Remove empty system messages in translators - Handle quota cooldown in retry logic - Include requested model in executor metadata - Normalize plan type filenames to lowercase - Added Embed stubs for ProviderExecutor interface compliance - Added payloadRequestedModel helper function
472 lines
14 KiB
Go
472 lines
14 KiB
Go
package executor
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/config"
|
|
"github.com/router-for-me/CLIProxyAPI/v6/internal/util"
|
|
cliproxyexecutor "github.com/router-for-me/CLIProxyAPI/v6/sdk/cliproxy/executor"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/tidwall/gjson"
|
|
"github.com/tidwall/sjson"
|
|
)
|
|
|
|
// ApplyThinkingMetadata applies thinking config from model suffix metadata (e.g., (high), (8192))
|
|
// for standard Gemini format payloads. It normalizes the budget when the model supports thinking.
|
|
func ApplyThinkingMetadata(payload []byte, metadata map[string]any, model string) []byte {
|
|
budgetOverride, includeOverride, ok := util.ResolveThinkingConfigFromMetadata(model, metadata)
|
|
if !ok || (budgetOverride == nil && includeOverride == nil) {
|
|
return payload
|
|
}
|
|
if !util.ModelSupportsThinking(model) {
|
|
return payload
|
|
}
|
|
if budgetOverride != nil {
|
|
norm := util.NormalizeThinkingBudget(model, *budgetOverride)
|
|
budgetOverride = &norm
|
|
}
|
|
return util.ApplyGeminiThinkingConfig(payload, budgetOverride, includeOverride)
|
|
}
|
|
|
|
// applyThinkingMetadataCLI applies thinking config from model suffix metadata (e.g., (high), (8192))
|
|
// for Gemini CLI format payloads (nested under "request"). It normalizes the budget when the model supports thinking.
|
|
func applyThinkingMetadataCLI(payload []byte, metadata map[string]any, model string) []byte {
|
|
budgetOverride, includeOverride, ok := util.ResolveThinkingConfigFromMetadata(model, metadata)
|
|
if !ok || (budgetOverride == nil && includeOverride == nil) {
|
|
return payload
|
|
}
|
|
if !util.ModelSupportsThinking(model) {
|
|
return payload
|
|
}
|
|
if budgetOverride != nil {
|
|
norm := util.NormalizeThinkingBudget(model, *budgetOverride)
|
|
budgetOverride = &norm
|
|
}
|
|
return util.ApplyGeminiCLIThinkingConfig(payload, budgetOverride, includeOverride)
|
|
}
|
|
|
|
// ApplyReasoningEffortMetadata applies reasoning effort overrides from metadata to the given JSON path.
|
|
// Metadata values take precedence over any existing field when the model supports thinking, intentionally
|
|
// overwriting caller-provided values to honor suffix/default metadata priority.
|
|
func ApplyReasoningEffortMetadata(payload []byte, metadata map[string]any, model, field string, allowCompat bool) []byte {
|
|
if len(metadata) == 0 {
|
|
return payload
|
|
}
|
|
if field == "" {
|
|
return payload
|
|
}
|
|
baseModel := util.ResolveOriginalModel(model, metadata)
|
|
if baseModel == "" {
|
|
baseModel = model
|
|
}
|
|
if !util.ModelSupportsThinking(baseModel) && !allowCompat {
|
|
return payload
|
|
}
|
|
if effort, ok := util.ReasoningEffortFromMetadata(metadata); ok && effort != "" {
|
|
if util.ModelUsesThinkingLevels(baseModel) || allowCompat {
|
|
if updated, err := sjson.SetBytes(payload, field, effort); err == nil {
|
|
return updated
|
|
}
|
|
}
|
|
}
|
|
// Fallback: numeric thinking_budget suffix for level-based (OpenAI-style) models.
|
|
if util.ModelUsesThinkingLevels(baseModel) || allowCompat {
|
|
if budget, _, _, matched := util.ThinkingFromMetadata(metadata); matched && budget != nil {
|
|
if effort, ok := util.ThinkingBudgetToEffort(baseModel, *budget); ok && effort != "" {
|
|
if updated, err := sjson.SetBytes(payload, field, effort); err == nil {
|
|
return updated
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return payload
|
|
}
|
|
|
|
// applyPayloadConfig applies payload default and override rules from configuration
|
|
// to the given JSON payload for the specified model.
|
|
// Defaults only fill missing fields, while overrides always overwrite existing values.
|
|
func applyPayloadConfig(cfg *config.Config, model string, payload []byte) []byte {
|
|
return applyPayloadConfigWithRoot(cfg, model, "", "", payload, nil, "")
|
|
}
|
|
|
|
// applyPayloadConfigWithRoot behaves like applyPayloadConfig but treats all parameter
|
|
// paths as relative to the provided root path (for example, "request" for Gemini CLI)
|
|
// and restricts matches to the given protocol when supplied. Defaults are checked
|
|
// against the original payload when provided. The requestedModel parameter is used
|
|
// for matching payload rules when different from the resolved model.
|
|
func applyPayloadConfigWithRoot(cfg *config.Config, model, protocol, root string, payload, original []byte, requestedModel string) []byte {
|
|
if cfg == nil || len(payload) == 0 {
|
|
return payload
|
|
}
|
|
rules := cfg.Payload
|
|
if len(rules.Default) == 0 && len(rules.DefaultRaw) == 0 && len(rules.Override) == 0 && len(rules.OverrideRaw) == 0 {
|
|
return payload
|
|
}
|
|
model = strings.TrimSpace(model)
|
|
if model == "" {
|
|
return payload
|
|
}
|
|
out := payload
|
|
source := original
|
|
if len(source) == 0 {
|
|
source = payload
|
|
}
|
|
appliedDefaults := make(map[string]struct{})
|
|
// Apply default rules: first write wins per field across all matching rules.
|
|
for i := range rules.Default {
|
|
rule := &rules.Default[i]
|
|
if !payloadRuleMatchesModel(rule, model, protocol) {
|
|
continue
|
|
}
|
|
for path, value := range rule.Params {
|
|
fullPath := buildPayloadPath(root, path)
|
|
if fullPath == "" {
|
|
continue
|
|
}
|
|
if gjson.GetBytes(source, fullPath).Exists() {
|
|
continue
|
|
}
|
|
if _, ok := appliedDefaults[fullPath]; ok {
|
|
continue
|
|
}
|
|
updated, errSet := sjson.SetBytes(out, fullPath, value)
|
|
if errSet != nil {
|
|
continue
|
|
}
|
|
out = updated
|
|
appliedDefaults[fullPath] = struct{}{}
|
|
}
|
|
}
|
|
// Apply default raw rules: first write wins per field across all matching rules.
|
|
for i := range rules.DefaultRaw {
|
|
rule := &rules.DefaultRaw[i]
|
|
if !payloadRuleMatchesModel(rule, model, protocol) {
|
|
continue
|
|
}
|
|
for path, value := range rule.Params {
|
|
fullPath := buildPayloadPath(root, path)
|
|
if fullPath == "" {
|
|
continue
|
|
}
|
|
if gjson.GetBytes(source, fullPath).Exists() {
|
|
continue
|
|
}
|
|
if _, ok := appliedDefaults[fullPath]; ok {
|
|
continue
|
|
}
|
|
rawValue, ok := payloadRawValue(value)
|
|
if !ok {
|
|
continue
|
|
}
|
|
updated, errSet := sjson.SetRawBytes(out, fullPath, rawValue)
|
|
if errSet != nil {
|
|
continue
|
|
}
|
|
out = updated
|
|
appliedDefaults[fullPath] = struct{}{}
|
|
}
|
|
}
|
|
// Apply override rules: last write wins per field across all matching rules.
|
|
for i := range rules.Override {
|
|
rule := &rules.Override[i]
|
|
if !payloadRuleMatchesModel(rule, model, protocol) {
|
|
continue
|
|
}
|
|
for path, value := range rule.Params {
|
|
fullPath := buildPayloadPath(root, path)
|
|
if fullPath == "" {
|
|
continue
|
|
}
|
|
updated, errSet := sjson.SetBytes(out, fullPath, value)
|
|
if errSet != nil {
|
|
continue
|
|
}
|
|
out = updated
|
|
}
|
|
}
|
|
// Apply override raw rules: last write wins per field across all matching rules.
|
|
for i := range rules.OverrideRaw {
|
|
rule := &rules.OverrideRaw[i]
|
|
if !payloadRuleMatchesModel(rule, model, protocol) {
|
|
continue
|
|
}
|
|
for path, value := range rule.Params {
|
|
fullPath := buildPayloadPath(root, path)
|
|
if fullPath == "" {
|
|
continue
|
|
}
|
|
rawValue, ok := payloadRawValue(value)
|
|
if !ok {
|
|
continue
|
|
}
|
|
updated, errSet := sjson.SetRawBytes(out, fullPath, rawValue)
|
|
if errSet != nil {
|
|
continue
|
|
}
|
|
out = updated
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func payloadRuleMatchesModel(rule *config.PayloadRule, model, protocol string) bool {
|
|
if rule == nil {
|
|
return false
|
|
}
|
|
if len(rule.Models) == 0 {
|
|
return false
|
|
}
|
|
for _, entry := range rule.Models {
|
|
name := strings.TrimSpace(entry.Name)
|
|
if name == "" {
|
|
continue
|
|
}
|
|
if ep := strings.TrimSpace(entry.Protocol); ep != "" && protocol != "" && !strings.EqualFold(ep, protocol) {
|
|
continue
|
|
}
|
|
if matchModelPattern(name, model) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// buildPayloadPath combines an optional root path with a relative parameter path.
|
|
// When root is empty, the parameter path is used as-is. When root is non-empty,
|
|
// the parameter path is treated as relative to root.
|
|
func buildPayloadPath(root, path string) string {
|
|
r := strings.TrimSpace(root)
|
|
p := strings.TrimSpace(path)
|
|
if r == "" {
|
|
return p
|
|
}
|
|
if p == "" {
|
|
return r
|
|
}
|
|
if strings.HasPrefix(p, ".") {
|
|
p = p[1:]
|
|
}
|
|
return r + "." + p
|
|
}
|
|
|
|
func payloadRawValue(value any) ([]byte, bool) {
|
|
if value == nil {
|
|
return nil, false
|
|
}
|
|
switch typed := value.(type) {
|
|
case string:
|
|
return []byte(typed), true
|
|
case []byte:
|
|
return typed, true
|
|
default:
|
|
raw, errMarshal := json.Marshal(typed)
|
|
if errMarshal != nil {
|
|
return nil, false
|
|
}
|
|
return raw, true
|
|
}
|
|
}
|
|
|
|
// payloadRequestedModel extracts the originally requested model from executor options metadata.
|
|
// Returns the fallback value if not found or empty.
|
|
func payloadRequestedModel(opts cliproxyexecutor.Options, fallback string) string {
|
|
fallback = strings.TrimSpace(fallback)
|
|
if len(opts.Metadata) == 0 {
|
|
return fallback
|
|
}
|
|
raw, ok := opts.Metadata[cliproxyexecutor.RequestedModelMetadataKey]
|
|
if !ok || raw == nil {
|
|
return fallback
|
|
}
|
|
switch v := raw.(type) {
|
|
case string:
|
|
if strings.TrimSpace(v) == "" {
|
|
return fallback
|
|
}
|
|
return strings.TrimSpace(v)
|
|
case []byte:
|
|
if len(v) == 0 {
|
|
return fallback
|
|
}
|
|
trimmed := strings.TrimSpace(string(v))
|
|
if trimmed == "" {
|
|
return fallback
|
|
}
|
|
return trimmed
|
|
default:
|
|
return fallback
|
|
}
|
|
}
|
|
|
|
// matchModelPattern performs simple wildcard matching where '*' matches zero or more characters.
|
|
// Examples:
|
|
//
|
|
// "*-5" matches "gpt-5"
|
|
// "gpt-*" matches "gpt-5" and "gpt-4"
|
|
// "gemini-*-pro" matches "gemini-2.5-pro" and "gemini-3-pro".
|
|
func matchModelPattern(pattern, model string) bool {
|
|
pattern = strings.TrimSpace(pattern)
|
|
model = strings.TrimSpace(model)
|
|
if pattern == "" {
|
|
return false
|
|
}
|
|
if pattern == "*" {
|
|
return true
|
|
}
|
|
// Iterative glob-style matcher supporting only '*' wildcard.
|
|
pi, si := 0, 0
|
|
starIdx := -1
|
|
matchIdx := 0
|
|
for si < len(model) {
|
|
if pi < len(pattern) && (pattern[pi] == model[si]) {
|
|
pi++
|
|
si++
|
|
continue
|
|
}
|
|
if pi < len(pattern) && pattern[pi] == '*' {
|
|
starIdx = pi
|
|
matchIdx = si
|
|
pi++
|
|
continue
|
|
}
|
|
if starIdx != -1 {
|
|
pi = starIdx + 1
|
|
matchIdx++
|
|
si = matchIdx
|
|
continue
|
|
}
|
|
return false
|
|
}
|
|
for pi < len(pattern) && pattern[pi] == '*' {
|
|
pi++
|
|
}
|
|
return pi == len(pattern)
|
|
}
|
|
|
|
// NormalizeThinkingConfig normalizes thinking-related fields in the payload
|
|
// based on model capabilities. For models without thinking support, it strips
|
|
// reasoning fields and logs a warning. For models with level-based thinking,
|
|
// it validates and normalizes the reasoning effort level. For models with
|
|
// numeric budget thinking, it strips the effort string fields.
|
|
func NormalizeThinkingConfig(payload []byte, model string, allowCompat bool) []byte {
|
|
if len(payload) == 0 || model == "" {
|
|
return payload
|
|
}
|
|
|
|
if !util.ModelSupportsThinking(model) {
|
|
if allowCompat {
|
|
return payload
|
|
}
|
|
return StripThinkingFieldsWithWarning(payload, model, false)
|
|
}
|
|
|
|
if util.ModelUsesThinkingLevels(model) {
|
|
return NormalizeReasoningEffortLevel(payload, model)
|
|
}
|
|
|
|
// Model supports thinking but uses numeric budgets, not levels.
|
|
// Strip effort string fields since they are not applicable.
|
|
return StripThinkingFields(payload, true)
|
|
}
|
|
|
|
// StripThinkingFieldsWithWarning removes thinking-related fields from the payload
|
|
// and logs a warning when fields are actually stripped. This helps users understand
|
|
// why their thinking/reasoning parameters are not being applied.
|
|
func StripThinkingFieldsWithWarning(payload []byte, model string, effortOnly bool) []byte {
|
|
fieldsToRemove := []string{
|
|
"reasoning_effort",
|
|
"reasoning.effort",
|
|
}
|
|
if !effortOnly {
|
|
fieldsToRemove = append([]string{"reasoning", "thinking"}, fieldsToRemove...)
|
|
}
|
|
out := payload
|
|
var strippedFields []string
|
|
for _, field := range fieldsToRemove {
|
|
if gjson.GetBytes(out, field).Exists() {
|
|
strippedFields = append(strippedFields, field)
|
|
out, _ = sjson.DeleteBytes(out, field)
|
|
}
|
|
}
|
|
if len(strippedFields) > 0 {
|
|
log.Warnf("model %q does not support thinking/reasoning; stripped fields: %s", model, strings.Join(strippedFields, ", "))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// StripThinkingFields removes thinking-related fields from the payload for
|
|
// models that do not support thinking. If effortOnly is true, only removes
|
|
// effort string fields (for models using numeric budgets).
|
|
func StripThinkingFields(payload []byte, effortOnly bool) []byte {
|
|
fieldsToRemove := []string{
|
|
"reasoning_effort",
|
|
"reasoning.effort",
|
|
}
|
|
if !effortOnly {
|
|
fieldsToRemove = append([]string{"reasoning", "thinking"}, fieldsToRemove...)
|
|
}
|
|
out := payload
|
|
for _, field := range fieldsToRemove {
|
|
if gjson.GetBytes(out, field).Exists() {
|
|
out, _ = sjson.DeleteBytes(out, field)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// NormalizeReasoningEffortLevel validates and normalizes the reasoning_effort
|
|
// or reasoning.effort field for level-based thinking models.
|
|
func NormalizeReasoningEffortLevel(payload []byte, model string) []byte {
|
|
out := payload
|
|
|
|
if effort := gjson.GetBytes(out, "reasoning_effort"); effort.Exists() {
|
|
if normalized, ok := util.NormalizeReasoningEffortLevel(model, effort.String()); ok {
|
|
out, _ = sjson.SetBytes(out, "reasoning_effort", normalized)
|
|
}
|
|
}
|
|
|
|
if effort := gjson.GetBytes(out, "reasoning.effort"); effort.Exists() {
|
|
if normalized, ok := util.NormalizeReasoningEffortLevel(model, effort.String()); ok {
|
|
out, _ = sjson.SetBytes(out, "reasoning.effort", normalized)
|
|
}
|
|
}
|
|
|
|
return out
|
|
}
|
|
|
|
// ValidateThinkingConfig checks for unsupported reasoning levels on level-based models.
|
|
// Returns a statusErr with 400 when an unsupported level is supplied to avoid silently
|
|
// downgrading requests.
|
|
func ValidateThinkingConfig(payload []byte, model string) error {
|
|
if len(payload) == 0 || model == "" {
|
|
return nil
|
|
}
|
|
if !util.ModelSupportsThinking(model) || !util.ModelUsesThinkingLevels(model) {
|
|
return nil
|
|
}
|
|
|
|
levels := util.GetModelThinkingLevels(model)
|
|
checkField := func(path string) error {
|
|
if effort := gjson.GetBytes(payload, path); effort.Exists() {
|
|
if _, ok := util.NormalizeReasoningEffortLevel(model, effort.String()); !ok {
|
|
return statusErr{
|
|
code: http.StatusBadRequest,
|
|
msg: fmt.Sprintf("unsupported reasoning effort level %q for model %s (supported: %s)", effort.String(), model, strings.Join(levels, ", ")),
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
if err := checkField("reasoning_effort"); err != nil {
|
|
return err
|
|
}
|
|
if err := checkField("reasoning.effort"); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|