Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名) - 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块) - docreader proto 描述符采用等长替换,保持序列化字节布局有效 - Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像 - 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
74 lines
2.9 KiB
Go
74 lines
2.9 KiB
Go
package api
|
|
|
|
import "academy/internal/types"
|
|
|
|
// OpenAIUsage is the superset of the usage shapes OpenAI-compatible
|
|
// endpoints return. Vendors disagree on where cache counters live:
|
|
// - OpenAI / Azure / most gateways: prompt_tokens_details.cached_tokens
|
|
// - DeepSeek: prompt_cache_hit_tokens / prompt_cache_miss_tokens
|
|
// - Kimi: top-level cached_tokens
|
|
// - Anthropic-through-OpenRouter: cache_read_input_tokens / cache_creation_input_tokens
|
|
//
|
|
// All of them are decoded here so protocol code never guesses per vendor.
|
|
type OpenAIUsage struct {
|
|
PromptTokens int `json:"prompt_tokens"`
|
|
CompletionTokens int `json:"completion_tokens"`
|
|
TotalTokens int `json:"total_tokens"`
|
|
PromptCacheHitTokens *int `json:"prompt_cache_hit_tokens"`
|
|
PromptCacheMissTokens *int `json:"prompt_cache_miss_tokens"`
|
|
CachedTokens *int `json:"cached_tokens"`
|
|
CacheReadInputTokens *int `json:"cache_read_input_tokens"`
|
|
CacheCreationInputTokens *int `json:"cache_creation_input_tokens"`
|
|
PromptTokensDetails *struct {
|
|
CachedTokens *int `json:"cached_tokens"`
|
|
CacheWriteTokens *int `json:"cache_write_tokens"`
|
|
} `json:"prompt_tokens_details"`
|
|
CompletionTokensDetails *struct {
|
|
ReasoningTokens int `json:"reasoning_tokens"`
|
|
} `json:"completion_tokens_details"`
|
|
}
|
|
|
|
// ToTokenUsage normalizes the counters. cacheAccounting states whether the
|
|
// vendor documents prompt-cache accounting at all: when it does not, a
|
|
// missing counter means "unsupported" rather than "unreported".
|
|
func (u OpenAIUsage) ToTokenUsage(cacheAccounting bool) types.TokenUsage {
|
|
out := types.TokenUsage{
|
|
PromptTokens: u.PromptTokens,
|
|
CompletionTokens: u.CompletionTokens,
|
|
TotalTokens: u.TotalTokens,
|
|
}
|
|
if out.TotalTokens == 0 {
|
|
out.TotalTokens = out.PromptTokens + out.CompletionTokens
|
|
}
|
|
switch {
|
|
case u.PromptCacheHitTokens != nil || u.PromptCacheMissTokens != nil:
|
|
read := valueOrZero(u.PromptCacheHitTokens)
|
|
miss := valueOrZero(u.PromptCacheMissTokens)
|
|
out.SetPromptCacheUsage(read, 0, miss, true)
|
|
case u.CacheReadInputTokens != nil || u.CacheCreationInputTokens != nil:
|
|
read := valueOrZero(u.CacheReadInputTokens)
|
|
write := valueOrZero(u.CacheCreationInputTokens)
|
|
out.SetPromptCacheUsage(read, write, max(0, u.PromptTokens-read), true)
|
|
case u.PromptTokensDetails != nil &&
|
|
(u.PromptTokensDetails.CachedTokens != nil || u.PromptTokensDetails.CacheWriteTokens != nil):
|
|
read := valueOrZero(u.PromptTokensDetails.CachedTokens)
|
|
write := valueOrZero(u.PromptTokensDetails.CacheWriteTokens)
|
|
out.SetPromptCacheUsage(read, write, max(0, u.PromptTokens-read), true)
|
|
case u.CachedTokens != nil:
|
|
read := valueOrZero(u.CachedTokens)
|
|
out.SetPromptCacheUsage(read, 0, max(0, u.PromptTokens-read), true)
|
|
case cacheAccounting:
|
|
out.SetPromptCacheUsage(0, 0, 0, false)
|
|
default:
|
|
out.MarkPromptCacheUnsupported()
|
|
}
|
|
return out
|
|
}
|
|
|
|
func valueOrZero(value *int) int {
|
|
if value == nil {
|
|
return 0
|
|
}
|
|
return *value
|
|
}
|