mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-09-10 10:00:10 +08:00
34 lines
839 B
Go
34 lines
839 B
Go
package common
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// RequestModelName returns the model name from the original request, falling
|
|
// back to the translated request when the original request is unavailable.
|
|
func RequestModelName(originalRequestRawJSON, requestRawJSON []byte) string {
|
|
for _, rawJSON := range [][]byte{originalRequestRawJSON, requestRawJSON} {
|
|
if modelName := requestModelName(rawJSON); modelName != "" {
|
|
return modelName
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func requestModelName(rawJSON []byte) string {
|
|
if len(rawJSON) == 0 || !gjson.ValidBytes(rawJSON) {
|
|
return ""
|
|
}
|
|
|
|
root := gjson.ParseBytes(rawJSON)
|
|
for _, path := range []string{"model", "request.model"} {
|
|
model := root.Get(path)
|
|
if model.Type == gjson.String && strings.TrimSpace(model.String()) != "" {
|
|
return model.String()
|
|
}
|
|
}
|
|
return ""
|
|
}
|