Spaces:
Running
Running
File size: 4,906 Bytes
b034029 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | package api
import (
"strconv"
"strings"
"cpa-usage-keeper/internal/models"
"cpa-usage-keeper/internal/redact"
)
type usageSourceResolver struct {
authIdentities map[string]models.UsageIdentity
providerIdentities map[string]models.UsageIdentity
providerRawByKey map[string]string
}
func newUsageSourceResolver(identities []models.UsageIdentity) usageSourceResolver {
authIdentities := make(map[string]models.UsageIdentity, len(identities))
providerIdentities := make(map[string]models.UsageIdentity, len(identities))
providerRawByKey := make(map[string]string, len(identities))
for _, identity := range identities {
key := strings.TrimSpace(identity.Identity)
if key == "" {
continue
}
switch identity.AuthType {
case models.UsageIdentityAuthTypeAuthFile:
authIdentities[key] = identity
case models.UsageIdentityAuthTypeAIProvider:
providerIdentities[key] = identity
resolved := usageSourceResolutionFromIdentity(identity, key)
if resolved.SourceKey != "" {
providerRawByKey[resolved.SourceKey] = key
}
}
}
return usageSourceResolver{
authIdentities: authIdentities,
providerIdentities: providerIdentities,
providerRawByKey: providerRawByKey,
}
}
type usageSourceResolution struct {
DisplayName string
SourceType string
SourceKey string
}
func usageSourceResolutionFromIdentity(item models.UsageIdentity, fallbackIdentity string) usageSourceResolution {
identityType := safeAIProviderDisplayValue(item.Type, fallbackIdentity, "")
displayName := firstNonEmptyString(
safeAIProviderDisplayValue(item.Name, fallbackIdentity, ""),
safeAIProviderDisplayValue(item.Provider, fallbackIdentity, ""),
identityType,
redact.APIKeyDisplayName(fallbackIdentity),
)
sourceKey := "provider:" + uintToString(item.ID)
if item.ID == 0 {
sourceKey = "provider:" + redact.APIKeyDisplayName(fallbackIdentity)
}
return usageSourceResolution{
DisplayName: displayName,
SourceType: identityType,
SourceKey: sourceKey,
}
}
func (r usageSourceResolver) rawSourceForPublicValue(value string) string {
trimmed := strings.TrimSpace(value)
if trimmed == "" {
return ""
}
if raw, ok := r.providerRawByKey[trimmed]; ok {
return raw
}
return trimmed
}
func (r usageSourceResolver) resolve(rawSource string, authIndex string) usageSourceResolution {
normalizedSource := strings.TrimSpace(rawSource)
if normalizedSource != "" {
if item, ok := r.providerIdentities[normalizedSource]; ok {
return usageSourceResolutionFromIdentity(item, normalizedSource)
}
}
normalizedAuthIndex := strings.TrimSpace(authIndex)
if normalizedAuthIndex != "" {
if identity, ok := r.authIdentities[normalizedAuthIndex]; ok {
displayName := firstNonEmptyString(identity.Name, normalizedAuthIndex)
return usageSourceResolution{
DisplayName: displayName,
SourceType: firstNonEmptyString(identity.Type, identity.Provider),
SourceKey: "auth:" + normalizedAuthIndex,
}
}
}
if normalizedSource == "" {
return usageSourceResolution{DisplayName: "-", SourceKey: "raw:-"}
}
if looksLikeEmail(normalizedSource) {
return usageSourceResolution{
DisplayName: normalizedSource,
SourceKey: "email:" + normalizedSource,
}
}
if inferredProvider := inferUsageProviderType(normalizedSource); inferredProvider != "" {
return usageSourceResolution{
DisplayName: inferredProvider,
SourceType: inferredProvider,
SourceKey: "provider:fallback:" + inferredProvider,
}
}
masked := redact.APIKeyDisplayName(normalizedSource)
return usageSourceResolution{
DisplayName: masked,
SourceKey: "raw:" + masked,
}
}
func uintToString(value uint) string {
return strconv.FormatUint(uint64(value), 10)
}
func firstNonEmptyString(values ...string) string {
for _, value := range values {
trimmed := strings.TrimSpace(value)
if trimmed != "" {
return trimmed
}
}
return ""
}
func looksLikeEmail(value string) bool {
trimmed := strings.TrimSpace(value)
if trimmed == "" {
return false
}
atIndex := strings.Index(trimmed, "@")
return atIndex > 0 && atIndex < len(trimmed)-1 && strings.Contains(trimmed[atIndex+1:], ".")
}
func inferUsageProviderType(source string) string {
value := strings.ToLower(strings.TrimSpace(source))
switch {
case value == "":
return ""
case strings.Contains(value, "ampcode"):
return "ampcode"
case strings.HasPrefix(value, "sk-ant-") || strings.Contains(value, "anthropic") || strings.Contains(value, "claude"):
return "claude"
case strings.HasPrefix(value, "sk-proj-") || strings.HasPrefix(value, "sk-") || strings.Contains(value, "openai") || strings.Contains(value, "gpt"):
return "openai"
case strings.HasPrefix(value, "aiza") || strings.Contains(value, "gemini"):
return "gemini"
case strings.Contains(value, "vertex"):
return "vertex"
case strings.Contains(value, "codex"):
return "codex"
default:
return ""
}
}
|