mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-09-20 08:57:57 +00:00
fix: auto-generate prompt_tokens_details from prompt_cache_hit_tokens in MarshalJSON
When only the legacy DeepSeek fields (prompt_cache_hit_tokens) are set but the OpenAI-standard prompt_tokens_details is nil, MarshalJSON now auto- generates the nested object. This ensures dsh (which reads the standard format first) sees cache hit data regardless of which adapter format the upstream uses.
This commit is contained in:
@ -107,6 +107,14 @@ type PromptTokensDetails struct {
|
||||
// standard keys (prompt_tokens/completion_tokens/total_tokens). Standard
|
||||
// clients such as DSH and DevEco Code read the *_tokens fields.
|
||||
func (t TokenUsage) MarshalJSON() ([]byte, error) {
|
||||
// Auto-generate prompt_tokens_details from legacy DeepSeek fields when
|
||||
// the upstream adapter only set the standalone hit count (openai.lua
|
||||
// does this in Lua, but other adapters or the standardSSEChunk fallback
|
||||
// may not). dsh reads prompt_tokens_details.cached_tokens first.
|
||||
pdetails := t.PromptTokensDetails
|
||||
if pdetails == nil && t.PromptCacheHit > 0 {
|
||||
pdetails = &PromptTokensDetails{CachedTokens: t.PromptCacheHit}
|
||||
}
|
||||
return json.Marshal(struct {
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
CompletionTokens int `json:"completion_tokens"`
|
||||
@ -124,7 +132,7 @@ func (t TokenUsage) MarshalJSON() ([]byte, error) {
|
||||
Prompt: t.Prompt,
|
||||
Completion: t.Completion,
|
||||
Total: t.Total,
|
||||
PromptTokensDetails: t.PromptTokensDetails,
|
||||
PromptTokensDetails: pdetails,
|
||||
PromptCacheHitTokens: t.PromptCacheHit,
|
||||
PromptCacheMissTokens: t.PromptCacheMiss,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user