mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-11-17 23:32:30 +08:00
- Replace tidwall/gjson and tidwall/sjson with github.com/MoeForge/moego/ojson and encoding/base64 with github.com/MoeForge/moego/base64 across the main module and examples - Introduce cross-method *ojson.Doc request rewriting pipelines across thinking, signature, client/codex, runtime/executor/helps, pluginhost, and all provider executors with FinalizePayloadDoc as the final semantic barrier - Convert all consecutive JSON mutations (>= 2 Set/SetRaw/Delete operations) to batch edits on ojson.Doc - Upgrade read-only byte paths to ojson.GetBytesNoCopy, ojson.GetManyBytesNoCopy, and util.ParseGJSONBytesNoCopy with explicit strings.Clone ownership protection across streaming and persistent state - Use base64.StdEncoding.ValidateString for zero-allocation signature checks and base64.EncodeDataURL for single-allocation data URL construction
63 lines
1.6 KiB
Go
63 lines
1.6 KiB
Go
package common
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"strings"
|
|
|
|
"github.com/MoeForge/moego/ojson"
|
|
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
|
|
)
|
|
|
|
const tooluLetters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
// GenerateClaudeToolCallID generates a random tool use ID prefixed with toolu_
|
|
// using rejection sampling to guarantee a uniform distribution across the 62 alphanumeric characters.
|
|
func GenerateClaudeToolCallID() string {
|
|
const maxValidByte = 256 - (256 % len(tooluLetters)) // 248: exact multiple of 62
|
|
var b strings.Builder
|
|
b.Grow(len("toolu_") + 24)
|
|
b.WriteString("toolu_")
|
|
|
|
var buf [32]byte
|
|
n := 0
|
|
for n < 24 {
|
|
_, _ = rand.Read(buf[:])
|
|
for _, bVal := range buf {
|
|
if int(bVal) < maxValidByte {
|
|
b.WriteByte(tooluLetters[int(bVal)%len(tooluLetters)])
|
|
n++
|
|
if n == 24 {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
// 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 || !ojson.ValidBytes(rawJSON) {
|
|
return ""
|
|
}
|
|
|
|
root := util.ParseGJSONBytesNoCopy(rawJSON)
|
|
for _, path := range []string{"model", "request.model"} {
|
|
model := root.Get(path)
|
|
if model.Type == ojson.String && strings.TrimSpace(model.String()) != "" {
|
|
return strings.Clone(model.String())
|
|
}
|
|
}
|
|
return ""
|
|
}
|