chore: import Local-Cursor v1.1.8 source snapshot
Includes the current working directory changes. Historical commits and release binaries are excluded to limit Gitea storage. Source base: d7515973fbfd6dd2001fa156c483455b8081c7cb
This commit is contained in:
36
internal/historymetrics/report.go
Normal file
36
internal/historymetrics/report.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package historymetrics
|
||||
|
||||
type Summary struct {
|
||||
ProviderCallsTotal int `json:"providerCallsTotal"`
|
||||
TurnsTotal int `json:"turnsTotal"`
|
||||
ValidTurnsTotal int `json:"validTurnsTotal"`
|
||||
InvalidTurnsTotal int `json:"invalidTurnsTotal"`
|
||||
RequestTokensTotal int64 `json:"requestTokensTotal"`
|
||||
PromptTokensTotal int64 `json:"promptTokensTotal"`
|
||||
CacheReadTokens int64 `json:"cacheReadTokens"`
|
||||
CacheWriteTokens int64 `json:"cacheWriteTokens"`
|
||||
CacheHitRate *float64 `json:"cacheHitRate"`
|
||||
}
|
||||
|
||||
type Totals struct {
|
||||
InputTokens int64
|
||||
OutputTokens int64
|
||||
CacheReadTokens int64
|
||||
CacheWriteTokens int64
|
||||
PromptTokensTotal int64
|
||||
RequestTokensTotal int64
|
||||
// IncludeCacheWriteInHitRate 控制 CacheHitRate 分母是否计入缓存写入 Token。
|
||||
IncludeCacheWriteInHitRate bool
|
||||
}
|
||||
|
||||
func cacheHitRateFromTotals(totals Totals) *float64 {
|
||||
inputCacheTokensTotal := totals.CacheReadTokens + totals.InputTokens
|
||||
if totals.IncludeCacheWriteInHitRate {
|
||||
inputCacheTokensTotal += totals.CacheWriteTokens
|
||||
}
|
||||
if inputCacheTokensTotal <= 0 {
|
||||
return nil
|
||||
}
|
||||
value := float64(totals.CacheReadTokens) / float64(inputCacheTokensTotal)
|
||||
return &value
|
||||
}
|
||||
57
internal/historymetrics/usage_json.go
Normal file
57
internal/historymetrics/usage_json.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package historymetrics
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
type usageFileDocument struct {
|
||||
Totals struct {
|
||||
ProviderCalls int64 `json:"provider_calls"`
|
||||
TurnsTotal int64 `json:"turns_total"`
|
||||
ValidTurnsTotal int64 `json:"valid_turns_total"`
|
||||
InvalidTurnsTotal int64 `json:"invalid_turns_total"`
|
||||
InputTokens int64 `json:"input_tokens"`
|
||||
OutputTokens int64 `json:"output_tokens"`
|
||||
CacheReadTokens int64 `json:"cache_read_tokens"`
|
||||
CacheWriteTokens int64 `json:"cache_write_tokens"`
|
||||
TotalTokens int64 `json:"total_tokens"`
|
||||
} `json:"totals"`
|
||||
}
|
||||
|
||||
func LoadUsageSummary(path string, includeCacheWriteInHitRate bool) (Summary, error) {
|
||||
body, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
if errors.Is(err, os.ErrNotExist) {
|
||||
return Summary{}, nil
|
||||
}
|
||||
return Summary{}, fmt.Errorf("read usage file: %w", err)
|
||||
}
|
||||
var doc usageFileDocument
|
||||
if err := json.Unmarshal(body, &doc); err != nil {
|
||||
return Summary{}, fmt.Errorf("decode usage file: %w", err)
|
||||
}
|
||||
totals := Totals{
|
||||
InputTokens: doc.Totals.InputTokens,
|
||||
OutputTokens: doc.Totals.OutputTokens,
|
||||
CacheReadTokens: doc.Totals.CacheReadTokens,
|
||||
CacheWriteTokens: doc.Totals.CacheWriteTokens,
|
||||
PromptTokensTotal: doc.Totals.InputTokens + doc.Totals.CacheReadTokens + doc.Totals.CacheWriteTokens,
|
||||
RequestTokensTotal: doc.Totals.TotalTokens,
|
||||
}
|
||||
// 命中率口径统一由后端按用户配置计算,前端只消费该结果,避免两套口径漂移。
|
||||
totals.IncludeCacheWriteInHitRate = includeCacheWriteInHitRate
|
||||
return Summary{
|
||||
ProviderCallsTotal: int(doc.Totals.ProviderCalls),
|
||||
TurnsTotal: int(doc.Totals.TurnsTotal),
|
||||
ValidTurnsTotal: int(doc.Totals.ValidTurnsTotal),
|
||||
InvalidTurnsTotal: int(doc.Totals.InvalidTurnsTotal),
|
||||
RequestTokensTotal: totals.RequestTokensTotal,
|
||||
PromptTokensTotal: totals.PromptTokensTotal,
|
||||
CacheReadTokens: totals.CacheReadTokens,
|
||||
CacheWriteTokens: totals.CacheWriteTokens,
|
||||
CacheHitRate: cacheHitRateFromTotals(totals),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user