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:
dev
2026-08-25 08:01:18 +08:00
parent 045ecf47bc
commit 2a5c7bbbc7
2 changed files with 79 additions and 1 deletions

View File

@ -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,
})