1 line
42 KiB
JSON
1 line
42 KiB
JSON
{"repo": "aitnr/cap-token-usage-tracker", "files": {"usage.go": {"cff7202cd0591682cdba6627cafa8ea5e4af524d": {"t": 1785674567.17974, "n": 10392, "h": "a52454829c28fbb0", "c": "package main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"net/url\"\n\t\"strings\"\n\t\"time\"\n\t\"unicode/utf8\"\n)\n\nconst maxDimensionRunes = 160\n\ntype normalizedUsage struct {\n\tDimensions Dimensions\n\tRequestedAt time.Time\n\tLatencyNS uint64\n\tTTFTNS uint64\n\tCounters Counters\n}\n\nfunc decodeUsage(raw []byte, now time.Time) (normalizedUsage, error) {\n\tvar root map[string]json.RawMessage\n\tif err := json.Unmarshal(raw, &root); err != nil {\n\t\treturn normalizedUsage{}, fmt.Errorf(\"decode usage record: %w\", err)\n\t}\n\n\trequestedAt := firstTime(root, \"RequestedAt\", \"requested_at\")\n\tnow = now.UTC()\n\tif requestedAt.IsZero() || requestedAt.After(now.Add(24*time.Hour)) {\n\t\trequestedAt = now\n\t} else {\n\t\trequestedAt = requestedAt.UTC()\n\t}\n\n\tfailure := firstObject(root, \"Failure\", \"failure\")\n\tdetail := firstObject(root, \"Detail\", \"detail\")\n\tinputTokens := firstInt64(detail, \"InputTokens\", \"input_tokens\")\n\toutputTokens := firstInt64(detail, \"OutputTokens\", \"output_tokens\")\n\treasoningTokens := firstInt64(detail, \"ReasoningTokens\", \"reasoning_tokens\")\n\tcachedTokens := firstInt64(detail, \"CachedTokens\", \"cached_tokens\")\n\tcacheReadTokens := firstInt64(detail, \"CacheReadTokens\", \"cache_read_tokens\")\n\tcacheCreationTokens := firstInt64(detail, \"CacheCreationTokens\", \"cache_creation_tokens\")\n\ttotal := firstInt64(detail, \"TotalTokens\", \"total_tokens\")\n\tif total <= 0 {\n\t\t// The SDK always serializes TotalTokens, so providers that leave it at\n\t\t// zero still produce a present JSON field. Match the raw-record fallback\n\t\t// used by the companion statistics implementation: input + output +\n\t\t// reasoning, then cached tokens only when that sum is still zero.\n\t\ttotal = saturatingInt64Sum(saturatingInt64Sum(inputTokens, outputTokens), reasoningTokens)\n\t\tif total == 0 {\n\t\t\ttotal = cachedTokens\n\t\t}\n\t}\n\n\tfailed := firstBool(root, \"Failed\", \"failed\")\n\tprovider := normalizeDimension(firstString(root, \"Provider\", \"provider\"))\n\texecutorType := normalizeDimension(firstString(root, \"ExecutorType\", \"executor_type\"))\n\tauthType := normalizeDimension(firstString(root, \"AuthType\", \"auth_type\"))\n\treturn normalizedUsage{\n\t\tDimensions: Dimensions{\n\t\t\tProvider: provider,\n\t\t\tExecutorType: executorType,\n\t\t\tModel: normalizeDimension(firstString(root, \"Model\", \"model\")),\n\t\t\tAlias: normalizeDimension(firstString(root, \"Alias\", \"alias\")),\n\t\t\tSource: safeUsageSource(firstString(root, \"Source\", \"source\"), firstString(root, \"APIKey\", \"api_key\"), provider, executorType, authType),\n\t\t\tAuthType: authType,\n\t\t\tServiceTier: normalizeDimension(firstString(root, \"ServiceTier\", \"service_tier\")),\n\t\t\tReasoningEffort: normalizeDimension(firstString(root, \"ReasoningEffort\", \"reasoning_effort\")),\n\t\t\tFailed: failed,\n\t\t\tFailureStatus: clampStatus(firstInt64(failure, \"StatusCode\", \"status_code\")),\n\t\t},\n\t\tRequestedAt: requestedAt,\n\t\tLatencyNS: positiveDurationNS(root, \"Latency\", \"latency\", \"latency_ns\"),\n\t\tTTFTNS: positiveDurationNS(root, \"TTFT\", \"ttft\", \"ttft_ns\"),\n\t\tCounters: Counters{\n\t\t\tRequests: 1,\n\t\t\tFailedRequests: boolCount(failed),\n\t\t\tInputTokens: positiveUint(inputTokens),\n\t\t\tOutputTokens: positiveUint(outputTokens),\n\t\t\tReasoningTokens: positiveUint(reasoningTokens),\n\t\t\tCachedTokens: positiveUint(cachedTokens),\n\t\t\tCacheReadTokens: positiveUint(cacheReadTokens),\n\t\t\tCacheCreationTokens: positiveUint(cacheCreationTokens),\n\t\t\tTotalTokens: positiveUint(total),\n\t\t},\n\t}, nil\n}\n\nfunc safeUsageSource(rawSource, apiKey, provider, executorType, authType string) string {\n\tsource := strings.TrimSpace(rawSource)\n\tif safeURL := sanitizeServiceURL(source); safeURL != \"\" {\n\t\treturn normalizeDimension(safeURL)\n\t}\n\n\t// CLIProxyAPI currently uses the selected upstream API key itself as Source\n\t// for API-key credentials. Never persist that value. UsageRecord does not\n\t// expose the configured base_url, so use the provider's public service URL\n\t// when it is known and a non-secret provider identifier otherwise.\n\tif isAPIKeyAuth(authType) || sameSecret(source, apiKey) || looksLikeCredential(source) {\n\t\treturn normalizeDimension(providerServiceAddress(provider, executorType))\n\t}\n\treturn normalizeDimension(source)\n}\n\nfunc sanitizeDimensionsSource(dimensions Dimensions) Dimensions {\n\tdimensions.Source = safeUsageSource(dimensions.Source, \"\", dimensions.Provider, dimensions.ExecutorType, dimensions.AuthType)\n\treturn dimensions\n}\n\nfunc sanitizeServiceURL(value string) string {\n\tparsed, err := url.Parse(strings.TrimSpace(value))\n\tif err != nil || parsed.Hostname() == \"\" || (parsed.Scheme != \"http\" && parsed.Scheme != \"https\") {\n\t\treturn \"\"\n\t}\n\tparsed.User = nil\n\tparsed.RawQuery = \"\"\n\tparsed.ForceQuery = false\n\tparsed.Fragment = \"\"\n\treturn strings.TrimRight(parsed.String(), \"/\")\n}\n\nfunc isAPIKeyAuth(value string) bool {\n\tnormalized := strings.NewReplacer(\"_\", \"\", \"-\", \"\", \" \", \"\").Replace(strings.ToLower(strings.TrimSpace(value)))\n\treturn normalized == \"apikey\"\n}\n\nfunc sameSecret(left, right string) bool {\n\tleft = strings.TrimSpace(left)\n\tright = strings.TrimSpace(right)\n\treturn left != \"\" && right != \"\" && left == right\n}\n\nfunc looksLikeCredential(value string) bool {\n\tvalue = strings.TrimSpace(value)\n\tif value == \"\" {\n\t\treturn false\n\t}\n\tlower := strings.ToLower(value)\n\tfor _, prefix := range []string{\n\t\t\"bearer \", \"basic \", \"token \", \"apikey \", \"api-key \", \"api_key \",\n\t\t\"sk-\", \"sk_\", \"xai-\", \"gsk_\", \"AIza\", \"key-\", \"sess-\",\n\t} {\n\t\tif strings.HasPrefix(lower, strings.ToLower(prefix)) {\n\t\t\treturn true\n\t\t}\n\t}\n\tif len(value) < 24 || strings.ContainsAny(value, \" /\\\\:@\") {\n\t\treturn false\n\t}\n\tvar letters, digits int\n\tfor _, r := range value {\n\t\tswitch {\n\t\tcase r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z':\n\t\t\tletters++\n\t\tcase r >= '0' && r <= '9':\n\t\t\tdigits++\n\t\tcase r == '-' || r == '_' || r == '.':\n\t\tdefault:\n\t\t\treturn false\n\t\t}\n\t}\n\treturn letters > 0 && digits > 0\n}\n\nfunc providerServiceAddress(provider, executorType string) string {\n\tprovider = strings.ToLower(strings.TrimSpace(provider))\n\texecutorType = strings.ToLower(strings.TrimSpace(executorType))\n\tcandidate := provider\n\tif candidate == \"\" {\n\t\tcandidate = executorType\n\t}\n\tswitch {\n\tcase candidate == \"openai\", candidate == \"codex\", candidate == \"openai-api\":\n\t\treturn \"https://api.openai.com/v1\"\n\tcase candidate == \"anthropic\", candidate == \"claude\", candidate == \"anthropic-api\":\n\t\treturn \"https://api.anthropic.com\"\n\tcase candidate == \"gemini\", candidate == \"gemini-interactions\", candidate == \"aistudio\", candidate == \"google\":\n\t\treturn \"https://generativelanguage.googleapis.com\"\n\tcase candidate == \"vertex\", candidate == \"gemini-vertex\":\n\t\treturn \"https://aiplatform.googleapis.com\"\n\tcase candidate == \"xai\", candidate == \"x-ai\", candidate == \"grok\":\n\t\treturn \"https://api.x.ai/v1\"\n\tcase candidate == \"kimi\", candidate == \"moonshot\":\n\t\treturn \"https://api.kimi.com/coding\"\n\tcase candidate == \"deepseek\":\n\t\treturn \"https://api.deepseek.com\"\n\tcase candidate == \"groq\":\n\t\treturn \"https://api.groq.com/openai/v1\"\n\tcase candidate == \"mistral\":\n\t\treturn \"https://api.mistral.ai/v1\"\n\tcase candidate == \"openrouter\":\n\t\treturn \"https://openrouter.ai/api/v1\"\n\tcase candidate == \"qwen\", candidate == \"dashscope\", candidate == \"alibaba\":\n\t\treturn \"https://dashscope.aliyuncs.com/compatible-mode/v1\"\n\tcase candidate == \"cohere\":\n\t\treturn \"https://api.cohere.com\"\n\tcase candidate == \"cerebras\":\n\t\treturn \"https://api.cerebras.ai/v1\"\n\tcase candidate == \"together\", candidate == \"togetherai\":\n\t\treturn \"https://api.together.xyz/v1\"\n\tcase candidate == \"siliconflow\":\n\t\treturn \"https://api.siliconflow.cn/v1\"\n\t}\n\tif provider != \"\" {\n\t\treturn provider\n\t}\n\tif executorType != \"\" {\n\t\treturn executorType\n\t}\n\treturn \"\"\n}\n\nfunc firstObject(root map[string]json.RawMessage, keys ...string) map[string]json.RawMessage {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result map[string]json.RawMessage\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn map[string]json.RawMessage{}\n}\n\nfunc firstString(root map[string]json.RawMessage, keys ...string) string {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result string\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn \"\"\n}\n\nfunc firstBool(root map[string]json.RawMessage, keys ...string) bool {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result bool\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn false\n}\n\nfunc firstInt64(root map[string]json.RawMessage, keys ...string) int64 {\n\tvalue, _ := firstInt64Present(root, keys...)\n\treturn value\n}\n\nfunc firstInt64Present(root map[string]json.RawMessage, keys ...string) (int64, bool) {\n\tfor _, key := range keys {\n\t\tvalue, ok := root[key]\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar result int64\n\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\treturn result, true\n\t\t}\n\t}\n\treturn 0, false\n}\n\nfunc firstTime(root map[string]json.RawMessage, keys ...string) time.Time {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result time.Time\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn time.Time{}\n}\n\nfunc positiveDurationNS(root map[string]json.RawMessage, keys ...string) uint64 {\n\tfor _, key := range keys {\n\t\tvalue, ok := root[key]\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar numeric int64\n\t\tif json.Unmarshal(value, &numeric) == nil {\n\t\t\treturn positiveUint(numeric)\n\t\t}\n\t\tvar text string\n\t\tif json.Unmarshal(value, &text) == nil {\n\t\t\tif duration, err := time.ParseDuration(text); err == nil && duration > 0 {\n\t\t\t\treturn uint64(duration)\n\t\t\t}\n\t\t}\n\t}\n\treturn 0\n}\n\nfunc normalizeDimension(value string) string {\n\tvalue = strings.TrimSpace(value)\n\tif !utf8.ValidString(value) {\n\t\tvalue = strings.ToValidUTF8(value, \"�\")\n\t}\n\trunes := []rune(value)\n\tif len(runes) > maxDimensionRunes {\n\t\tvalue = string(runes[:maxDimensionRunes])\n\t}\n\treturn value\n}\n\nfunc positiveUint(value int64) uint64 {\n\tif value <= 0 {\n\t\treturn 0\n\t}\n\treturn uint64(value)\n}\n\nfunc boolCount(value bool) uint64 {\n\tif value {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\nfunc clampStatus(value int64) int {\n\tif value < 0 || value > 999 {\n\t\treturn 0\n\t}\n\treturn int(value)\n}\n\nfunc saturatingInt64Sum(left, right int64) int64 {\n\tif left <= 0 {\n\t\tleft = 0\n\t}\n\tif right <= 0 {\n\t\tright = 0\n\t}\n\tif left > int64(^uint64(0)>>1)-right {\n\t\treturn int64(^uint64(0) >> 1)\n\t}\n\treturn left + right\n}\n"}, "0038af817dff9c31c2aac08f6c5fd0398ede2295": {"t": 1785759346.7228622, "n": 10392, "h": "a52454829c28fbb0", "c": "package main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"net/url\"\n\t\"strings\"\n\t\"time\"\n\t\"unicode/utf8\"\n)\n\nconst maxDimensionRunes = 160\n\ntype normalizedUsage struct {\n\tDimensions Dimensions\n\tRequestedAt time.Time\n\tLatencyNS uint64\n\tTTFTNS uint64\n\tCounters Counters\n}\n\nfunc decodeUsage(raw []byte, now time.Time) (normalizedUsage, error) {\n\tvar root map[string]json.RawMessage\n\tif err := json.Unmarshal(raw, &root); err != nil {\n\t\treturn normalizedUsage{}, fmt.Errorf(\"decode usage record: %w\", err)\n\t}\n\n\trequestedAt := firstTime(root, \"RequestedAt\", \"requested_at\")\n\tnow = now.UTC()\n\tif requestedAt.IsZero() || requestedAt.After(now.Add(24*time.Hour)) {\n\t\trequestedAt = now\n\t} else {\n\t\trequestedAt = requestedAt.UTC()\n\t}\n\n\tfailure := firstObject(root, \"Failure\", \"failure\")\n\tdetail := firstObject(root, \"Detail\", \"detail\")\n\tinputTokens := firstInt64(detail, \"InputTokens\", \"input_tokens\")\n\toutputTokens := firstInt64(detail, \"OutputTokens\", \"output_tokens\")\n\treasoningTokens := firstInt64(detail, \"ReasoningTokens\", \"reasoning_tokens\")\n\tcachedTokens := firstInt64(detail, \"CachedTokens\", \"cached_tokens\")\n\tcacheReadTokens := firstInt64(detail, \"CacheReadTokens\", \"cache_read_tokens\")\n\tcacheCreationTokens := firstInt64(detail, \"CacheCreationTokens\", \"cache_creation_tokens\")\n\ttotal := firstInt64(detail, \"TotalTokens\", \"total_tokens\")\n\tif total <= 0 {\n\t\t// The SDK always serializes TotalTokens, so providers that leave it at\n\t\t// zero still produce a present JSON field. Match the raw-record fallback\n\t\t// used by the companion statistics implementation: input + output +\n\t\t// reasoning, then cached tokens only when that sum is still zero.\n\t\ttotal = saturatingInt64Sum(saturatingInt64Sum(inputTokens, outputTokens), reasoningTokens)\n\t\tif total == 0 {\n\t\t\ttotal = cachedTokens\n\t\t}\n\t}\n\n\tfailed := firstBool(root, \"Failed\", \"failed\")\n\tprovider := normalizeDimension(firstString(root, \"Provider\", \"provider\"))\n\texecutorType := normalizeDimension(firstString(root, \"ExecutorType\", \"executor_type\"))\n\tauthType := normalizeDimension(firstString(root, \"AuthType\", \"auth_type\"))\n\treturn normalizedUsage{\n\t\tDimensions: Dimensions{\n\t\t\tProvider: provider,\n\t\t\tExecutorType: executorType,\n\t\t\tModel: normalizeDimension(firstString(root, \"Model\", \"model\")),\n\t\t\tAlias: normalizeDimension(firstString(root, \"Alias\", \"alias\")),\n\t\t\tSource: safeUsageSource(firstString(root, \"Source\", \"source\"), firstString(root, \"APIKey\", \"api_key\"), provider, executorType, authType),\n\t\t\tAuthType: authType,\n\t\t\tServiceTier: normalizeDimension(firstString(root, \"ServiceTier\", \"service_tier\")),\n\t\t\tReasoningEffort: normalizeDimension(firstString(root, \"ReasoningEffort\", \"reasoning_effort\")),\n\t\t\tFailed: failed,\n\t\t\tFailureStatus: clampStatus(firstInt64(failure, \"StatusCode\", \"status_code\")),\n\t\t},\n\t\tRequestedAt: requestedAt,\n\t\tLatencyNS: positiveDurationNS(root, \"Latency\", \"latency\", \"latency_ns\"),\n\t\tTTFTNS: positiveDurationNS(root, \"TTFT\", \"ttft\", \"ttft_ns\"),\n\t\tCounters: Counters{\n\t\t\tRequests: 1,\n\t\t\tFailedRequests: boolCount(failed),\n\t\t\tInputTokens: positiveUint(inputTokens),\n\t\t\tOutputTokens: positiveUint(outputTokens),\n\t\t\tReasoningTokens: positiveUint(reasoningTokens),\n\t\t\tCachedTokens: positiveUint(cachedTokens),\n\t\t\tCacheReadTokens: positiveUint(cacheReadTokens),\n\t\t\tCacheCreationTokens: positiveUint(cacheCreationTokens),\n\t\t\tTotalTokens: positiveUint(total),\n\t\t},\n\t}, nil\n}\n\nfunc safeUsageSource(rawSource, apiKey, provider, executorType, authType string) string {\n\tsource := strings.TrimSpace(rawSource)\n\tif safeURL := sanitizeServiceURL(source); safeURL != \"\" {\n\t\treturn normalizeDimension(safeURL)\n\t}\n\n\t// CLIProxyAPI currently uses the selected upstream API key itself as Source\n\t// for API-key credentials. Never persist that value. UsageRecord does not\n\t// expose the configured base_url, so use the provider's public service URL\n\t// when it is known and a non-secret provider identifier otherwise.\n\tif isAPIKeyAuth(authType) || sameSecret(source, apiKey) || looksLikeCredential(source) {\n\t\treturn normalizeDimension(providerServiceAddress(provider, executorType))\n\t}\n\treturn normalizeDimension(source)\n}\n\nfunc sanitizeDimensionsSource(dimensions Dimensions) Dimensions {\n\tdimensions.Source = safeUsageSource(dimensions.Source, \"\", dimensions.Provider, dimensions.ExecutorType, dimensions.AuthType)\n\treturn dimensions\n}\n\nfunc sanitizeServiceURL(value string) string {\n\tparsed, err := url.Parse(strings.TrimSpace(value))\n\tif err != nil || parsed.Hostname() == \"\" || (parsed.Scheme != \"http\" && parsed.Scheme != \"https\") {\n\t\treturn \"\"\n\t}\n\tparsed.User = nil\n\tparsed.RawQuery = \"\"\n\tparsed.ForceQuery = false\n\tparsed.Fragment = \"\"\n\treturn strings.TrimRight(parsed.String(), \"/\")\n}\n\nfunc isAPIKeyAuth(value string) bool {\n\tnormalized := strings.NewReplacer(\"_\", \"\", \"-\", \"\", \" \", \"\").Replace(strings.ToLower(strings.TrimSpace(value)))\n\treturn normalized == \"apikey\"\n}\n\nfunc sameSecret(left, right string) bool {\n\tleft = strings.TrimSpace(left)\n\tright = strings.TrimSpace(right)\n\treturn left != \"\" && right != \"\" && left == right\n}\n\nfunc looksLikeCredential(value string) bool {\n\tvalue = strings.TrimSpace(value)\n\tif value == \"\" {\n\t\treturn false\n\t}\n\tlower := strings.ToLower(value)\n\tfor _, prefix := range []string{\n\t\t\"bearer \", \"basic \", \"token \", \"apikey \", \"api-key \", \"api_key \",\n\t\t\"sk-\", \"sk_\", \"xai-\", \"gsk_\", \"AIza\", \"key-\", \"sess-\",\n\t} {\n\t\tif strings.HasPrefix(lower, strings.ToLower(prefix)) {\n\t\t\treturn true\n\t\t}\n\t}\n\tif len(value) < 24 || strings.ContainsAny(value, \" /\\\\:@\") {\n\t\treturn false\n\t}\n\tvar letters, digits int\n\tfor _, r := range value {\n\t\tswitch {\n\t\tcase r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z':\n\t\t\tletters++\n\t\tcase r >= '0' && r <= '9':\n\t\t\tdigits++\n\t\tcase r == '-' || r == '_' || r == '.':\n\t\tdefault:\n\t\t\treturn false\n\t\t}\n\t}\n\treturn letters > 0 && digits > 0\n}\n\nfunc providerServiceAddress(provider, executorType string) string {\n\tprovider = strings.ToLower(strings.TrimSpace(provider))\n\texecutorType = strings.ToLower(strings.TrimSpace(executorType))\n\tcandidate := provider\n\tif candidate == \"\" {\n\t\tcandidate = executorType\n\t}\n\tswitch {\n\tcase candidate == \"openai\", candidate == \"codex\", candidate == \"openai-api\":\n\t\treturn \"https://api.openai.com/v1\"\n\tcase candidate == \"anthropic\", candidate == \"claude\", candidate == \"anthropic-api\":\n\t\treturn \"https://api.anthropic.com\"\n\tcase candidate == \"gemini\", candidate == \"gemini-interactions\", candidate == \"aistudio\", candidate == \"google\":\n\t\treturn \"https://generativelanguage.googleapis.com\"\n\tcase candidate == \"vertex\", candidate == \"gemini-vertex\":\n\t\treturn \"https://aiplatform.googleapis.com\"\n\tcase candidate == \"xai\", candidate == \"x-ai\", candidate == \"grok\":\n\t\treturn \"https://api.x.ai/v1\"\n\tcase candidate == \"kimi\", candidate == \"moonshot\":\n\t\treturn \"https://api.kimi.com/coding\"\n\tcase candidate == \"deepseek\":\n\t\treturn \"https://api.deepseek.com\"\n\tcase candidate == \"groq\":\n\t\treturn \"https://api.groq.com/openai/v1\"\n\tcase candidate == \"mistral\":\n\t\treturn \"https://api.mistral.ai/v1\"\n\tcase candidate == \"openrouter\":\n\t\treturn \"https://openrouter.ai/api/v1\"\n\tcase candidate == \"qwen\", candidate == \"dashscope\", candidate == \"alibaba\":\n\t\treturn \"https://dashscope.aliyuncs.com/compatible-mode/v1\"\n\tcase candidate == \"cohere\":\n\t\treturn \"https://api.cohere.com\"\n\tcase candidate == \"cerebras\":\n\t\treturn \"https://api.cerebras.ai/v1\"\n\tcase candidate == \"together\", candidate == \"togetherai\":\n\t\treturn \"https://api.together.xyz/v1\"\n\tcase candidate == \"siliconflow\":\n\t\treturn \"https://api.siliconflow.cn/v1\"\n\t}\n\tif provider != \"\" {\n\t\treturn provider\n\t}\n\tif executorType != \"\" {\n\t\treturn executorType\n\t}\n\treturn \"\"\n}\n\nfunc firstObject(root map[string]json.RawMessage, keys ...string) map[string]json.RawMessage {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result map[string]json.RawMessage\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn map[string]json.RawMessage{}\n}\n\nfunc firstString(root map[string]json.RawMessage, keys ...string) string {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result string\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn \"\"\n}\n\nfunc firstBool(root map[string]json.RawMessage, keys ...string) bool {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result bool\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn false\n}\n\nfunc firstInt64(root map[string]json.RawMessage, keys ...string) int64 {\n\tvalue, _ := firstInt64Present(root, keys...)\n\treturn value\n}\n\nfunc firstInt64Present(root map[string]json.RawMessage, keys ...string) (int64, bool) {\n\tfor _, key := range keys {\n\t\tvalue, ok := root[key]\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar result int64\n\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\treturn result, true\n\t\t}\n\t}\n\treturn 0, false\n}\n\nfunc firstTime(root map[string]json.RawMessage, keys ...string) time.Time {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result time.Time\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn time.Time{}\n}\n\nfunc positiveDurationNS(root map[string]json.RawMessage, keys ...string) uint64 {\n\tfor _, key := range keys {\n\t\tvalue, ok := root[key]\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar numeric int64\n\t\tif json.Unmarshal(value, &numeric) == nil {\n\t\t\treturn positiveUint(numeric)\n\t\t}\n\t\tvar text string\n\t\tif json.Unmarshal(value, &text) == nil {\n\t\t\tif duration, err := time.ParseDuration(text); err == nil && duration > 0 {\n\t\t\t\treturn uint64(duration)\n\t\t\t}\n\t\t}\n\t}\n\treturn 0\n}\n\nfunc normalizeDimension(value string) string {\n\tvalue = strings.TrimSpace(value)\n\tif !utf8.ValidString(value) {\n\t\tvalue = strings.ToValidUTF8(value, \"�\")\n\t}\n\trunes := []rune(value)\n\tif len(runes) > maxDimensionRunes {\n\t\tvalue = string(runes[:maxDimensionRunes])\n\t}\n\treturn value\n}\n\nfunc positiveUint(value int64) uint64 {\n\tif value <= 0 {\n\t\treturn 0\n\t}\n\treturn uint64(value)\n}\n\nfunc boolCount(value bool) uint64 {\n\tif value {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\nfunc clampStatus(value int64) int {\n\tif value < 0 || value > 999 {\n\t\treturn 0\n\t}\n\treturn int(value)\n}\n\nfunc saturatingInt64Sum(left, right int64) int64 {\n\tif left <= 0 {\n\t\tleft = 0\n\t}\n\tif right <= 0 {\n\t\tright = 0\n\t}\n\tif left > int64(^uint64(0)>>1)-right {\n\t\treturn int64(^uint64(0) >> 1)\n\t}\n\treturn left + right\n}\n"}, "dc9d5ad42fbef83fa641dbc27d1cb180088bb576": {"t": 1785759348.012932, "n": 6028, "h": "c4be0f82f51087aa", "c": "package main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"strings\"\n\t\"time\"\n\t\"unicode/utf8\"\n)\n\nconst maxDimensionRunes = 160\n\ntype normalizedUsage struct {\n\tDimensions Dimensions\n\tRequestedAt time.Time\n\tLatencyNS uint64\n\tTTFTNS uint64\n\tCounters Counters\n}\n\nfunc decodeUsage(raw []byte, now time.Time) (normalizedUsage, error) {\n\tvar root map[string]json.RawMessage\n\tif err := json.Unmarshal(raw, &root); err != nil {\n\t\treturn normalizedUsage{}, fmt.Errorf(\"decode usage record: %w\", err)\n\t}\n\n\trequestedAt := firstTime(root, \"RequestedAt\", \"requested_at\")\n\tnow = now.UTC()\n\tif requestedAt.IsZero() || requestedAt.After(now.Add(24*time.Hour)) {\n\t\trequestedAt = now\n\t} else {\n\t\trequestedAt = requestedAt.UTC()\n\t}\n\n\tfailure := firstObject(root, \"Failure\", \"failure\")\n\tdetail := firstObject(root, \"Detail\", \"detail\")\n\tinputTokens := firstInt64(detail, \"InputTokens\", \"input_tokens\")\n\toutputTokens := firstInt64(detail, \"OutputTokens\", \"output_tokens\")\n\treasoningTokens := firstInt64(detail, \"ReasoningTokens\", \"reasoning_tokens\")\n\tcachedTokens := firstInt64(detail, \"CachedTokens\", \"cached_tokens\")\n\tcacheReadTokens := firstInt64(detail, \"CacheReadTokens\", \"cache_read_tokens\")\n\tcacheCreationTokens := firstInt64(detail, \"CacheCreationTokens\", \"cache_creation_tokens\")\n\ttotal := firstInt64(detail, \"TotalTokens\", \"total_tokens\")\n\tif total <= 0 {\n\t\t// The SDK always serializes TotalTokens, so providers that leave it at\n\t\t// zero still produce a present JSON field. Match the raw-record fallback\n\t\t// used by the companion statistics implementation: input + output +\n\t\t// reasoning, then cached tokens only when that sum is still zero.\n\t\ttotal = saturatingInt64Sum(saturatingInt64Sum(inputTokens, outputTokens), reasoningTokens)\n\t\tif total == 0 {\n\t\t\ttotal = cachedTokens\n\t\t}\n\t}\n\n\tfailed := firstBool(root, \"Failed\", \"failed\")\n\treturn normalizedUsage{\n\t\tDimensions: Dimensions{\n\t\t\tProvider: normalizeDimension(firstString(root, \"Provider\", \"provider\")),\n\t\t\tExecutorType: normalizeDimension(firstString(root, \"ExecutorType\", \"executor_type\")),\n\t\t\tModel: normalizeDimension(firstString(root, \"Model\", \"model\")),\n\t\t\tAlias: normalizeDimension(firstString(root, \"Alias\", \"alias\")),\n\t\t\tSource: normalizeDimension(firstString(root, \"Source\", \"source\")),\n\t\t\tAuthType: normalizeDimension(firstString(root, \"AuthType\", \"auth_type\")),\n\t\t\tServiceTier: normalizeDimension(firstString(root, \"ServiceTier\", \"service_tier\")),\n\t\t\tReasoningEffort: normalizeDimension(firstString(root, \"ReasoningEffort\", \"reasoning_effort\")),\n\t\t\tFailed: failed,\n\t\t\tFailureStatus: clampStatus(firstInt64(failure, \"StatusCode\", \"status_code\")),\n\t\t},\n\t\tRequestedAt: requestedAt,\n\t\tLatencyNS: positiveDurationNS(root, \"Latency\", \"latency\", \"latency_ns\"),\n\t\tTTFTNS: positiveDurationNS(root, \"TTFT\", \"ttft\", \"ttft_ns\"),\n\t\tCounters: Counters{\n\t\t\tRequests: 1,\n\t\t\tFailedRequests: boolCount(failed),\n\t\t\tInputTokens: positiveUint(inputTokens),\n\t\t\tOutputTokens: positiveUint(outputTokens),\n\t\t\tReasoningTokens: positiveUint(reasoningTokens),\n\t\t\tCachedTokens: positiveUint(cachedTokens),\n\t\t\tCacheReadTokens: positiveUint(cacheReadTokens),\n\t\t\tCacheCreationTokens: positiveUint(cacheCreationTokens),\n\t\t\tTotalTokens: positiveUint(total),\n\t\t},\n\t}, nil\n}\n\nfunc firstObject(root map[string]json.RawMessage, keys ...string) map[string]json.RawMessage {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result map[string]json.RawMessage\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn map[string]json.RawMessage{}\n}\n\nfunc firstString(root map[string]json.RawMessage, keys ...string) string {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result string\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn \"\"\n}\n\nfunc firstBool(root map[string]json.RawMessage, keys ...string) bool {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result bool\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn false\n}\n\nfunc firstInt64(root map[string]json.RawMessage, keys ...string) int64 {\n\tvalue, _ := firstInt64Present(root, keys...)\n\treturn value\n}\n\nfunc firstInt64Present(root map[string]json.RawMessage, keys ...string) (int64, bool) {\n\tfor _, key := range keys {\n\t\tvalue, ok := root[key]\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar result int64\n\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\treturn result, true\n\t\t}\n\t}\n\treturn 0, false\n}\n\nfunc firstTime(root map[string]json.RawMessage, keys ...string) time.Time {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result time.Time\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn time.Time{}\n}\n\nfunc positiveDurationNS(root map[string]json.RawMessage, keys ...string) uint64 {\n\tfor _, key := range keys {\n\t\tvalue, ok := root[key]\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar numeric int64\n\t\tif json.Unmarshal(value, &numeric) == nil {\n\t\t\treturn positiveUint(numeric)\n\t\t}\n\t\tvar text string\n\t\tif json.Unmarshal(value, &text) == nil {\n\t\t\tif duration, err := time.ParseDuration(text); err == nil && duration > 0 {\n\t\t\t\treturn uint64(duration)\n\t\t\t}\n\t\t}\n\t}\n\treturn 0\n}\n\nfunc normalizeDimension(value string) string {\n\tvalue = strings.TrimSpace(value)\n\tif !utf8.ValidString(value) {\n\t\tvalue = strings.ToValidUTF8(value, \"�\")\n\t}\n\trunes := []rune(value)\n\tif len(runes) > maxDimensionRunes {\n\t\tvalue = string(runes[:maxDimensionRunes])\n\t}\n\treturn value\n}\n\nfunc positiveUint(value int64) uint64 {\n\tif value <= 0 {\n\t\treturn 0\n\t}\n\treturn uint64(value)\n}\n\nfunc boolCount(value bool) uint64 {\n\tif value {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\nfunc clampStatus(value int64) int {\n\tif value < 0 || value > 999 {\n\t\treturn 0\n\t}\n\treturn int(value)\n}\n\nfunc saturatingInt64Sum(left, right int64) int64 {\n\tif left <= 0 {\n\t\tleft = 0\n\t}\n\tif right <= 0 {\n\t\tright = 0\n\t}\n\tif left > int64(^uint64(0)>>1)-right {\n\t\treturn int64(^uint64(0) >> 1)\n\t}\n\treturn left + right\n}\n"}, "1f5885209eef7b7dca353fe67cab455a5a933b60": {"t": 1785759349.6364944, "n": 5883, "h": "6914c1756d18aaa0", "c": "package main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"strings\"\n\t\"time\"\n\t\"unicode/utf8\"\n)\n\nconst maxDimensionRunes = 160\n\ntype normalizedUsage struct {\n\tDimensions Dimensions\n\tRequestedAt time.Time\n\tLatencyNS uint64\n\tTTFTNS uint64\n\tCounters Counters\n}\n\nfunc decodeUsage(raw []byte, now time.Time) (normalizedUsage, error) {\n\tvar root map[string]json.RawMessage\n\tif err := json.Unmarshal(raw, &root); err != nil {\n\t\treturn normalizedUsage{}, fmt.Errorf(\"decode usage record: %w\", err)\n\t}\n\n\trequestedAt := firstTime(root, \"RequestedAt\", \"requested_at\")\n\tnow = now.UTC()\n\tif requestedAt.IsZero() || requestedAt.After(now.Add(24*time.Hour)) {\n\t\trequestedAt = now\n\t} else {\n\t\trequestedAt = requestedAt.UTC()\n\t}\n\n\tfailure := firstObject(root, \"Failure\", \"failure\")\n\tdetail := firstObject(root, \"Detail\", \"detail\")\n\tinputTokens := firstInt64(detail, \"InputTokens\", \"input_tokens\")\n\toutputTokens := firstInt64(detail, \"OutputTokens\", \"output_tokens\")\n\treasoningTokens := firstInt64(detail, \"ReasoningTokens\", \"reasoning_tokens\")\n\tcachedTokens := firstInt64(detail, \"CachedTokens\", \"cached_tokens\")\n\tcacheReadTokens := firstInt64(detail, \"CacheReadTokens\", \"cache_read_tokens\")\n\tcacheCreationTokens := firstInt64(detail, \"CacheCreationTokens\", \"cache_creation_tokens\")\n\ttotal := firstInt64(detail, \"TotalTokens\", \"total_tokens\")\n\tif total <= 0 {\n\t\t// The SDK always serializes TotalTokens, so providers that leave it at\n\t\t// zero still produce a present JSON field. Derive a useful total from the\n\t\t// canonical input/output counters instead of treating that zero as final.\n\t\ttotal = saturatingInt64Sum(inputTokens, outputTokens)\n\t}\n\n\tfailed := firstBool(root, \"Failed\", \"failed\")\n\treturn normalizedUsage{\n\t\tDimensions: Dimensions{\n\t\t\tProvider: normalizeDimension(firstString(root, \"Provider\", \"provider\")),\n\t\t\tExecutorType: normalizeDimension(firstString(root, \"ExecutorType\", \"executor_type\")),\n\t\t\tModel: normalizeDimension(firstString(root, \"Model\", \"model\")),\n\t\t\tAlias: normalizeDimension(firstString(root, \"Alias\", \"alias\")),\n\t\t\tSource: normalizeDimension(firstString(root, \"Source\", \"source\")),\n\t\t\tAuthType: normalizeDimension(firstString(root, \"AuthType\", \"auth_type\")),\n\t\t\tServiceTier: normalizeDimension(firstString(root, \"ServiceTier\", \"service_tier\")),\n\t\t\tReasoningEffort: normalizeDimension(firstString(root, \"ReasoningEffort\", \"reasoning_effort\")),\n\t\t\tFailed: failed,\n\t\t\tFailureStatus: clampStatus(firstInt64(failure, \"StatusCode\", \"status_code\")),\n\t\t},\n\t\tRequestedAt: requestedAt,\n\t\tLatencyNS: positiveDurationNS(root, \"Latency\", \"latency\", \"latency_ns\"),\n\t\tTTFTNS: positiveDurationNS(root, \"TTFT\", \"ttft\", \"ttft_ns\"),\n\t\tCounters: Counters{\n\t\t\tRequests: 1,\n\t\t\tFailedRequests: boolCount(failed),\n\t\t\tInputTokens: positiveUint(inputTokens),\n\t\t\tOutputTokens: positiveUint(outputTokens),\n\t\t\tReasoningTokens: positiveUint(reasoningTokens),\n\t\t\tCachedTokens: positiveUint(cachedTokens),\n\t\t\tCacheReadTokens: positiveUint(cacheReadTokens),\n\t\t\tCacheCreationTokens: positiveUint(cacheCreationTokens),\n\t\t\tTotalTokens: positiveUint(total),\n\t\t},\n\t}, nil\n}\n\nfunc firstObject(root map[string]json.RawMessage, keys ...string) map[string]json.RawMessage {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result map[string]json.RawMessage\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn map[string]json.RawMessage{}\n}\n\nfunc firstString(root map[string]json.RawMessage, keys ...string) string {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result string\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn \"\"\n}\n\nfunc firstBool(root map[string]json.RawMessage, keys ...string) bool {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result bool\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn false\n}\n\nfunc firstInt64(root map[string]json.RawMessage, keys ...string) int64 {\n\tvalue, _ := firstInt64Present(root, keys...)\n\treturn value\n}\n\nfunc firstInt64Present(root map[string]json.RawMessage, keys ...string) (int64, bool) {\n\tfor _, key := range keys {\n\t\tvalue, ok := root[key]\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar result int64\n\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\treturn result, true\n\t\t}\n\t}\n\treturn 0, false\n}\n\nfunc firstTime(root map[string]json.RawMessage, keys ...string) time.Time {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result time.Time\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn time.Time{}\n}\n\nfunc positiveDurationNS(root map[string]json.RawMessage, keys ...string) uint64 {\n\tfor _, key := range keys {\n\t\tvalue, ok := root[key]\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar numeric int64\n\t\tif json.Unmarshal(value, &numeric) == nil {\n\t\t\treturn positiveUint(numeric)\n\t\t}\n\t\tvar text string\n\t\tif json.Unmarshal(value, &text) == nil {\n\t\t\tif duration, err := time.ParseDuration(text); err == nil && duration > 0 {\n\t\t\t\treturn uint64(duration)\n\t\t\t}\n\t\t}\n\t}\n\treturn 0\n}\n\nfunc normalizeDimension(value string) string {\n\tvalue = strings.TrimSpace(value)\n\tif !utf8.ValidString(value) {\n\t\tvalue = strings.ToValidUTF8(value, \"�\")\n\t}\n\trunes := []rune(value)\n\tif len(runes) > maxDimensionRunes {\n\t\tvalue = string(runes[:maxDimensionRunes])\n\t}\n\treturn value\n}\n\nfunc positiveUint(value int64) uint64 {\n\tif value <= 0 {\n\t\treturn 0\n\t}\n\treturn uint64(value)\n}\n\nfunc boolCount(value bool) uint64 {\n\tif value {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\nfunc clampStatus(value int64) int {\n\tif value < 0 || value > 999 {\n\t\treturn 0\n\t}\n\treturn int(value)\n}\n\nfunc saturatingInt64Sum(left, right int64) int64 {\n\tif left <= 0 {\n\t\tleft = 0\n\t}\n\tif right <= 0 {\n\t\tright = 0\n\t}\n\tif left > int64(^uint64(0)>>1)-right {\n\t\treturn int64(^uint64(0) >> 1)\n\t}\n\treturn left + right\n}\n"}, "cb60328c54361cd6fabe664d374ad613d20c7119": {"t": 1785759352.5796943, "n": 5563, "h": "970be52d2b87a605", "c": "package main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\t\"strings\"\n\t\"time\"\n\t\"unicode/utf8\"\n)\n\nconst maxDimensionRunes = 160\n\ntype normalizedUsage struct {\n\tDimensions Dimensions\n\tRequestedAt time.Time\n\tLatencyNS uint64\n\tTTFTNS uint64\n\tCounters Counters\n}\n\nfunc decodeUsage(raw []byte, now time.Time) (normalizedUsage, error) {\n\tvar root map[string]json.RawMessage\n\tif err := json.Unmarshal(raw, &root); err != nil {\n\t\treturn normalizedUsage{}, fmt.Errorf(\"decode usage record: %w\", err)\n\t}\n\n\trequestedAt := firstTime(root, \"RequestedAt\", \"requested_at\")\n\tnow = now.UTC()\n\tif requestedAt.IsZero() || requestedAt.After(now.Add(24*time.Hour)) {\n\t\trequestedAt = now\n\t} else {\n\t\trequestedAt = requestedAt.UTC()\n\t}\n\n\tfailure := firstObject(root, \"Failure\", \"failure\")\n\tdetail := firstObject(root, \"Detail\", \"detail\")\n\ttotal, totalPresent := firstInt64Present(detail, \"TotalTokens\", \"total_tokens\")\n\tif !totalPresent {\n\t\ttotal = saturatingInt64Sum(\n\t\t\tfirstInt64(detail, \"InputTokens\", \"input_tokens\"),\n\t\t\tfirstInt64(detail, \"OutputTokens\", \"output_tokens\"),\n\t\t)\n\t}\n\n\tfailed := firstBool(root, \"Failed\", \"failed\")\n\treturn normalizedUsage{\n\t\tDimensions: Dimensions{\n\t\t\tProvider: normalizeDimension(firstString(root, \"Provider\", \"provider\")),\n\t\t\tExecutorType: normalizeDimension(firstString(root, \"ExecutorType\", \"executor_type\")),\n\t\t\tModel: normalizeDimension(firstString(root, \"Model\", \"model\")),\n\t\t\tAlias: normalizeDimension(firstString(root, \"Alias\", \"alias\")),\n\t\t\tSource: normalizeDimension(firstString(root, \"Source\", \"source\")),\n\t\t\tAuthType: normalizeDimension(firstString(root, \"AuthType\", \"auth_type\")),\n\t\t\tServiceTier: normalizeDimension(firstString(root, \"ServiceTier\", \"service_tier\")),\n\t\t\tReasoningEffort: normalizeDimension(firstString(root, \"ReasoningEffort\", \"reasoning_effort\")),\n\t\t\tFailed: failed,\n\t\t\tFailureStatus: clampStatus(firstInt64(failure, \"StatusCode\", \"status_code\")),\n\t\t},\n\t\tRequestedAt: requestedAt,\n\t\tLatencyNS: positiveDurationNS(root, \"Latency\", \"latency\", \"latency_ns\"),\n\t\tTTFTNS: positiveDurationNS(root, \"TTFT\", \"ttft\", \"ttft_ns\"),\n\t\tCounters: Counters{\n\t\t\tRequests: 1,\n\t\t\tFailedRequests: boolCount(failed),\n\t\t\tInputTokens: positiveUint(firstInt64(detail, \"InputTokens\", \"input_tokens\")),\n\t\t\tOutputTokens: positiveUint(firstInt64(detail, \"OutputTokens\", \"output_tokens\")),\n\t\t\tReasoningTokens: positiveUint(firstInt64(detail, \"ReasoningTokens\", \"reasoning_tokens\")),\n\t\t\tCachedTokens: positiveUint(firstInt64(detail, \"CachedTokens\", \"cached_tokens\")),\n\t\t\tCacheReadTokens: positiveUint(firstInt64(detail, \"CacheReadTokens\", \"cache_read_tokens\")),\n\t\t\tCacheCreationTokens: positiveUint(firstInt64(detail, \"CacheCreationTokens\", \"cache_creation_tokens\")),\n\t\t\tTotalTokens: positiveUint(total),\n\t\t},\n\t}, nil\n}\n\nfunc firstObject(root map[string]json.RawMessage, keys ...string) map[string]json.RawMessage {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result map[string]json.RawMessage\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn map[string]json.RawMessage{}\n}\n\nfunc firstString(root map[string]json.RawMessage, keys ...string) string {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result string\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn \"\"\n}\n\nfunc firstBool(root map[string]json.RawMessage, keys ...string) bool {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result bool\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn false\n}\n\nfunc firstInt64(root map[string]json.RawMessage, keys ...string) int64 {\n\tvalue, _ := firstInt64Present(root, keys...)\n\treturn value\n}\n\nfunc firstInt64Present(root map[string]json.RawMessage, keys ...string) (int64, bool) {\n\tfor _, key := range keys {\n\t\tvalue, ok := root[key]\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar result int64\n\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\treturn result, true\n\t\t}\n\t}\n\treturn 0, false\n}\n\nfunc firstTime(root map[string]json.RawMessage, keys ...string) time.Time {\n\tfor _, key := range keys {\n\t\tif value, ok := root[key]; ok {\n\t\t\tvar result time.Time\n\t\t\tif json.Unmarshal(value, &result) == nil {\n\t\t\t\treturn result\n\t\t\t}\n\t\t}\n\t}\n\treturn time.Time{}\n}\n\nfunc positiveDurationNS(root map[string]json.RawMessage, keys ...string) uint64 {\n\tfor _, key := range keys {\n\t\tvalue, ok := root[key]\n\t\tif !ok {\n\t\t\tcontinue\n\t\t}\n\t\tvar numeric int64\n\t\tif json.Unmarshal(value, &numeric) == nil {\n\t\t\treturn positiveUint(numeric)\n\t\t}\n\t\tvar text string\n\t\tif json.Unmarshal(value, &text) == nil {\n\t\t\tif duration, err := time.ParseDuration(text); err == nil && duration > 0 {\n\t\t\t\treturn uint64(duration)\n\t\t\t}\n\t\t}\n\t}\n\treturn 0\n}\n\nfunc normalizeDimension(value string) string {\n\tvalue = strings.TrimSpace(value)\n\tif !utf8.ValidString(value) {\n\t\tvalue = strings.ToValidUTF8(value, \"�\")\n\t}\n\trunes := []rune(value)\n\tif len(runes) > maxDimensionRunes {\n\t\tvalue = string(runes[:maxDimensionRunes])\n\t}\n\treturn value\n}\n\nfunc positiveUint(value int64) uint64 {\n\tif value <= 0 {\n\t\treturn 0\n\t}\n\treturn uint64(value)\n}\n\nfunc boolCount(value bool) uint64 {\n\tif value {\n\t\treturn 1\n\t}\n\treturn 0\n}\n\nfunc clampStatus(value int64) int {\n\tif value < 0 || value > 999 {\n\t\treturn 0\n\t}\n\treturn int(value)\n}\n\nfunc saturatingInt64Sum(left, right int64) int64 {\n\tif left <= 0 {\n\t\tleft = 0\n\t}\n\tif right <= 0 {\n\t\tright = 0\n\t}\n\tif left > int64(^uint64(0)>>1)-right {\n\t\treturn int64(^uint64(0) >> 1)\n\t}\n\treturn left + right\n}\n"}}}} |