mirror of
https://gitee.com/wanwujie/sub2api
synced 2026-04-08 01:00:21 +08:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d99a3ef14b | ||
|
|
fc8fa83fcc | ||
|
|
6dcd99468b | ||
|
|
d5ba7b80d3 |
@@ -35,6 +35,7 @@ const (
|
|||||||
stickySessionTTL = time.Hour // 粘性会话TTL
|
stickySessionTTL = time.Hour // 粘性会话TTL
|
||||||
defaultMaxLineSize = 10 * 1024 * 1024
|
defaultMaxLineSize = 10 * 1024 * 1024
|
||||||
claudeCodeSystemPrompt = "You are Claude Code, Anthropic's official CLI for Claude."
|
claudeCodeSystemPrompt = "You are Claude Code, Anthropic's official CLI for Claude."
|
||||||
|
maxCacheControlBlocks = 4 // Anthropic API 允许的最大 cache_control 块数量
|
||||||
)
|
)
|
||||||
|
|
||||||
// sseDataRe matches SSE data lines with optional whitespace after colon.
|
// sseDataRe matches SSE data lines with optional whitespace after colon.
|
||||||
@@ -43,6 +44,16 @@ var (
|
|||||||
sseDataRe = regexp.MustCompile(`^data:\s*`)
|
sseDataRe = regexp.MustCompile(`^data:\s*`)
|
||||||
sessionIDRegex = regexp.MustCompile(`session_([a-f0-9-]{36})`)
|
sessionIDRegex = regexp.MustCompile(`session_([a-f0-9-]{36})`)
|
||||||
claudeCliUserAgentRe = regexp.MustCompile(`^claude-cli/\d+\.\d+\.\d+`)
|
claudeCliUserAgentRe = regexp.MustCompile(`^claude-cli/\d+\.\d+\.\d+`)
|
||||||
|
|
||||||
|
// claudeCodePromptPrefixes 用于检测 Claude Code 系统提示词的前缀列表
|
||||||
|
// 支持多种变体:标准版、Agent SDK 版、Explore Agent 版、Compact 版等
|
||||||
|
// 注意:前缀之间不应存在包含关系,否则会导致冗余匹配
|
||||||
|
claudeCodePromptPrefixes = []string{
|
||||||
|
"You are Claude Code, Anthropic's official CLI for Claude", // 标准版 & Agent SDK 版(含 running within...)
|
||||||
|
"You are a Claude agent, built on Anthropic's Claude Agent SDK", // Agent SDK 变体
|
||||||
|
"You are a file search specialist for Claude Code", // Explore Agent 版
|
||||||
|
"You are a helpful AI assistant tasked with summarizing conversations", // Compact 版
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
// allowedHeaders 白名单headers(参考CRS项目)
|
// allowedHeaders 白名单headers(参考CRS项目)
|
||||||
@@ -355,17 +366,8 @@ func (s *GatewayService) SelectAccountForModelWithExclusions(ctx context.Context
|
|||||||
return s.selectAccountWithMixedScheduling(ctx, groupID, sessionHash, requestedModel, excludedIDs, platform)
|
return s.selectAccountWithMixedScheduling(ctx, groupID, sessionHash, requestedModel, excludedIDs, platform)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 强制平台模式:优先按分组查找,找不到再查全部该平台账户
|
|
||||||
if hasForcePlatform && groupID != nil {
|
|
||||||
account, err := s.selectAccountForModelWithPlatform(ctx, groupID, sessionHash, requestedModel, excludedIDs, platform)
|
|
||||||
if err == nil {
|
|
||||||
return account, nil
|
|
||||||
}
|
|
||||||
// 分组中找不到,回退查询全部该平台账户
|
|
||||||
groupID = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// antigravity 分组、强制平台模式或无分组使用单平台选择
|
// antigravity 分组、强制平台模式或无分组使用单平台选择
|
||||||
|
// 注意:强制平台模式也必须遵守分组限制,不再回退到全平台查询
|
||||||
return s.selectAccountForModelWithPlatform(ctx, groupID, sessionHash, requestedModel, excludedIDs, platform)
|
return s.selectAccountForModelWithPlatform(ctx, groupID, sessionHash, requestedModel, excludedIDs, platform)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -443,7 +445,8 @@ func (s *GatewayService) SelectAccountWithLoadAwareness(ctx context.Context, gro
|
|||||||
accountID, err := s.cache.GetSessionAccountID(ctx, sessionHash)
|
accountID, err := s.cache.GetSessionAccountID(ctx, sessionHash)
|
||||||
if err == nil && accountID > 0 && !isExcluded(accountID) {
|
if err == nil && accountID > 0 && !isExcluded(accountID) {
|
||||||
account, err := s.accountRepo.GetByID(ctx, accountID)
|
account, err := s.accountRepo.GetByID(ctx, accountID)
|
||||||
if err == nil && s.isAccountAllowedForPlatform(account, platform, useMixed) &&
|
if err == nil && s.isAccountInGroup(account, groupID) &&
|
||||||
|
s.isAccountAllowedForPlatform(account, platform, useMixed) &&
|
||||||
account.IsSchedulable() &&
|
account.IsSchedulable() &&
|
||||||
(requestedModel == "" || s.isModelSupportedByAccount(account, requestedModel)) {
|
(requestedModel == "" || s.isModelSupportedByAccount(account, requestedModel)) {
|
||||||
result, err := s.tryAcquireAccountSlot(ctx, accountID, account.Concurrency)
|
result, err := s.tryAcquireAccountSlot(ctx, accountID, account.Concurrency)
|
||||||
@@ -660,9 +663,7 @@ func (s *GatewayService) listSchedulableAccounts(ctx context.Context, groupID *i
|
|||||||
accounts, err = s.accountRepo.ListSchedulableByPlatform(ctx, platform)
|
accounts, err = s.accountRepo.ListSchedulableByPlatform(ctx, platform)
|
||||||
} else if groupID != nil {
|
} else if groupID != nil {
|
||||||
accounts, err = s.accountRepo.ListSchedulableByGroupIDAndPlatform(ctx, *groupID, platform)
|
accounts, err = s.accountRepo.ListSchedulableByGroupIDAndPlatform(ctx, *groupID, platform)
|
||||||
if err == nil && len(accounts) == 0 && hasForcePlatform {
|
// 分组内无账号则返回空列表,由上层处理错误,不再回退到全平台查询
|
||||||
accounts, err = s.accountRepo.ListSchedulableByPlatform(ctx, platform)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
accounts, err = s.accountRepo.ListSchedulableByPlatform(ctx, platform)
|
accounts, err = s.accountRepo.ListSchedulableByPlatform(ctx, platform)
|
||||||
}
|
}
|
||||||
@@ -685,6 +686,23 @@ func (s *GatewayService) isAccountAllowedForPlatform(account *Account, platform
|
|||||||
return account.Platform == platform
|
return account.Platform == platform
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isAccountInGroup checks if the account belongs to the specified group.
|
||||||
|
// Returns true if groupID is nil (no group restriction) or account belongs to the group.
|
||||||
|
func (s *GatewayService) isAccountInGroup(account *Account, groupID *int64) bool {
|
||||||
|
if groupID == nil {
|
||||||
|
return true // 无分组限制
|
||||||
|
}
|
||||||
|
if account == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, ag := range account.AccountGroups {
|
||||||
|
if ag.GroupID == *groupID {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func (s *GatewayService) tryAcquireAccountSlot(ctx context.Context, accountID int64, maxConcurrency int) (*AcquireResult, error) {
|
func (s *GatewayService) tryAcquireAccountSlot(ctx context.Context, accountID int64, maxConcurrency int) (*AcquireResult, error) {
|
||||||
if s.concurrencyService == nil {
|
if s.concurrencyService == nil {
|
||||||
return &AcquireResult{Acquired: true, ReleaseFunc: func() {}}, nil
|
return &AcquireResult{Acquired: true, ReleaseFunc: func() {}}, nil
|
||||||
@@ -723,8 +741,8 @@ func (s *GatewayService) selectAccountForModelWithPlatform(ctx context.Context,
|
|||||||
if err == nil && accountID > 0 {
|
if err == nil && accountID > 0 {
|
||||||
if _, excluded := excludedIDs[accountID]; !excluded {
|
if _, excluded := excludedIDs[accountID]; !excluded {
|
||||||
account, err := s.accountRepo.GetByID(ctx, accountID)
|
account, err := s.accountRepo.GetByID(ctx, accountID)
|
||||||
// 检查账号平台是否匹配(确保粘性会话不会跨平台)
|
// 检查账号分组归属和平台匹配(确保粘性会话不会跨分组或跨平台)
|
||||||
if err == nil && account.Platform == platform && account.IsSchedulable() && (requestedModel == "" || s.isModelSupportedByAccount(account, requestedModel)) {
|
if err == nil && s.isAccountInGroup(account, groupID) && account.Platform == platform && account.IsSchedulable() && (requestedModel == "" || s.isModelSupportedByAccount(account, requestedModel)) {
|
||||||
if err := s.cache.RefreshSessionTTL(ctx, sessionHash, stickySessionTTL); err != nil {
|
if err := s.cache.RefreshSessionTTL(ctx, sessionHash, stickySessionTTL); err != nil {
|
||||||
log.Printf("refresh session ttl failed: session=%s err=%v", sessionHash, err)
|
log.Printf("refresh session ttl failed: session=%s err=%v", sessionHash, err)
|
||||||
}
|
}
|
||||||
@@ -812,8 +830,8 @@ func (s *GatewayService) selectAccountWithMixedScheduling(ctx context.Context, g
|
|||||||
if err == nil && accountID > 0 {
|
if err == nil && accountID > 0 {
|
||||||
if _, excluded := excludedIDs[accountID]; !excluded {
|
if _, excluded := excludedIDs[accountID]; !excluded {
|
||||||
account, err := s.accountRepo.GetByID(ctx, accountID)
|
account, err := s.accountRepo.GetByID(ctx, accountID)
|
||||||
// 检查账号是否有效:原生平台直接匹配,antigravity 需要启用混合调度
|
// 检查账号分组归属和有效性:原生平台直接匹配,antigravity 需要启用混合调度
|
||||||
if err == nil && account.IsSchedulable() && (requestedModel == "" || s.isModelSupportedByAccount(account, requestedModel)) {
|
if err == nil && s.isAccountInGroup(account, groupID) && account.IsSchedulable() && (requestedModel == "" || s.isModelSupportedByAccount(account, requestedModel)) {
|
||||||
if account.Platform == nativePlatform || (account.Platform == PlatformAntigravity && account.IsMixedSchedulingEnabled()) {
|
if account.Platform == nativePlatform || (account.Platform == PlatformAntigravity && account.IsMixedSchedulingEnabled()) {
|
||||||
if err := s.cache.RefreshSessionTTL(ctx, sessionHash, stickySessionTTL); err != nil {
|
if err := s.cache.RefreshSessionTTL(ctx, sessionHash, stickySessionTTL); err != nil {
|
||||||
log.Printf("refresh session ttl failed: session=%s err=%v", sessionHash, err)
|
log.Printf("refresh session ttl failed: session=%s err=%v", sessionHash, err)
|
||||||
@@ -1013,15 +1031,15 @@ func isClaudeCodeClient(userAgent string, metadataUserID string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// systemIncludesClaudeCodePrompt 检查 system 中是否已包含 Claude Code 提示词
|
// systemIncludesClaudeCodePrompt 检查 system 中是否已包含 Claude Code 提示词
|
||||||
// 支持 string 和 []any 两种格式
|
// 使用前缀匹配支持多种变体(标准版、Agent SDK 版等)
|
||||||
func systemIncludesClaudeCodePrompt(system any) bool {
|
func systemIncludesClaudeCodePrompt(system any) bool {
|
||||||
switch v := system.(type) {
|
switch v := system.(type) {
|
||||||
case string:
|
case string:
|
||||||
return v == claudeCodeSystemPrompt
|
return hasClaudeCodePrefix(v)
|
||||||
case []any:
|
case []any:
|
||||||
for _, item := range v {
|
for _, item := range v {
|
||||||
if m, ok := item.(map[string]any); ok {
|
if m, ok := item.(map[string]any); ok {
|
||||||
if text, ok := m["text"].(string); ok && text == claudeCodeSystemPrompt {
|
if text, ok := m["text"].(string); ok && hasClaudeCodePrefix(text) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1030,6 +1048,16 @@ func systemIncludesClaudeCodePrompt(system any) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// hasClaudeCodePrefix 检查文本是否以 Claude Code 提示词的特征前缀开头
|
||||||
|
func hasClaudeCodePrefix(text string) bool {
|
||||||
|
for _, prefix := range claudeCodePromptPrefixes {
|
||||||
|
if strings.HasPrefix(text, prefix) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// injectClaudeCodePrompt 在 system 开头注入 Claude Code 提示词
|
// injectClaudeCodePrompt 在 system 开头注入 Claude Code 提示词
|
||||||
// 处理 null、字符串、数组三种格式
|
// 处理 null、字符串、数组三种格式
|
||||||
func injectClaudeCodePrompt(body []byte, system any) []byte {
|
func injectClaudeCodePrompt(body []byte, system any) []byte {
|
||||||
@@ -1073,6 +1101,124 @@ func injectClaudeCodePrompt(body []byte, system any) []byte {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// enforceCacheControlLimit 强制执行 cache_control 块数量限制(最多 4 个)
|
||||||
|
// 超限时优先从 messages 中移除 cache_control,保护 system 中的缓存控制
|
||||||
|
func enforceCacheControlLimit(body []byte) []byte {
|
||||||
|
var data map[string]any
|
||||||
|
if err := json.Unmarshal(body, &data); err != nil {
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
// 计算当前 cache_control 块数量
|
||||||
|
count := countCacheControlBlocks(data)
|
||||||
|
if count <= maxCacheControlBlocks {
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
// 超限:优先从 messages 中移除,再从 system 中移除
|
||||||
|
for count > maxCacheControlBlocks {
|
||||||
|
if removeCacheControlFromMessages(data) {
|
||||||
|
count--
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if removeCacheControlFromSystem(data) {
|
||||||
|
count--
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err := json.Marshal(data)
|
||||||
|
if err != nil {
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
// countCacheControlBlocks 统计 system 和 messages 中的 cache_control 块数量
|
||||||
|
func countCacheControlBlocks(data map[string]any) int {
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
// 统计 system 中的块
|
||||||
|
if system, ok := data["system"].([]any); ok {
|
||||||
|
for _, item := range system {
|
||||||
|
if m, ok := item.(map[string]any); ok {
|
||||||
|
if _, has := m["cache_control"]; has {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 统计 messages 中的块
|
||||||
|
if messages, ok := data["messages"].([]any); ok {
|
||||||
|
for _, msg := range messages {
|
||||||
|
if msgMap, ok := msg.(map[string]any); ok {
|
||||||
|
if content, ok := msgMap["content"].([]any); ok {
|
||||||
|
for _, item := range content {
|
||||||
|
if m, ok := item.(map[string]any); ok {
|
||||||
|
if _, has := m["cache_control"]; has {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeCacheControlFromMessages 从 messages 中移除一个 cache_control(从头开始)
|
||||||
|
// 返回 true 表示成功移除,false 表示没有可移除的
|
||||||
|
func removeCacheControlFromMessages(data map[string]any) bool {
|
||||||
|
messages, ok := data["messages"].([]any)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, msg := range messages {
|
||||||
|
msgMap, ok := msg.(map[string]any)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
content, ok := msgMap["content"].([]any)
|
||||||
|
if !ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, item := range content {
|
||||||
|
if m, ok := item.(map[string]any); ok {
|
||||||
|
if _, has := m["cache_control"]; has {
|
||||||
|
delete(m, "cache_control")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// removeCacheControlFromSystem 从 system 中移除一个 cache_control(从尾部开始,保护注入的 prompt)
|
||||||
|
// 返回 true 表示成功移除,false 表示没有可移除的
|
||||||
|
func removeCacheControlFromSystem(data map[string]any) bool {
|
||||||
|
system, ok := data["system"].([]any)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从尾部开始移除,保护开头注入的 Claude Code prompt
|
||||||
|
for i := len(system) - 1; i >= 0; i-- {
|
||||||
|
if m, ok := system[i].(map[string]any); ok {
|
||||||
|
if _, has := m["cache_control"]; has {
|
||||||
|
delete(m, "cache_control")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// Forward 转发请求到Claude API
|
// Forward 转发请求到Claude API
|
||||||
func (s *GatewayService) Forward(ctx context.Context, c *gin.Context, account *Account, parsed *ParsedRequest) (*ForwardResult, error) {
|
func (s *GatewayService) Forward(ctx context.Context, c *gin.Context, account *Account, parsed *ParsedRequest) (*ForwardResult, error) {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
@@ -1093,6 +1239,9 @@ func (s *GatewayService) Forward(ctx context.Context, c *gin.Context, account *A
|
|||||||
body = injectClaudeCodePrompt(body, parsed.System)
|
body = injectClaudeCodePrompt(body, parsed.System)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 强制执行 cache_control 块数量限制(最多 4 个)
|
||||||
|
body = enforceCacheControlLimit(body)
|
||||||
|
|
||||||
// 应用模型映射(仅对apikey类型账号)
|
// 应用模型映射(仅对apikey类型账号)
|
||||||
originalModel := reqModel
|
originalModel := reqModel
|
||||||
if account.Type == AccountTypeAPIKey {
|
if account.Type == AccountTypeAPIKey {
|
||||||
|
|||||||
@@ -85,9 +85,40 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Account Filter -->
|
<!-- Account Filter -->
|
||||||
<div class="w-full sm:w-auto sm:min-w-[220px]">
|
<div ref="accountSearchRef" class="usage-filter-dropdown relative w-full sm:w-auto sm:min-w-[220px]">
|
||||||
<label class="input-label">{{ t('admin.usage.account') }}</label>
|
<label class="input-label">{{ t('admin.usage.account') }}</label>
|
||||||
<Select v-model="filters.account_id" :options="accountOptions" searchable @change="emitChange" />
|
<input
|
||||||
|
v-model="accountKeyword"
|
||||||
|
type="text"
|
||||||
|
class="input pr-8"
|
||||||
|
:placeholder="t('admin.usage.searchAccountPlaceholder')"
|
||||||
|
@input="debounceAccountSearch"
|
||||||
|
@focus="showAccountDropdown = true"
|
||||||
|
/>
|
||||||
|
<button
|
||||||
|
v-if="filters.account_id"
|
||||||
|
type="button"
|
||||||
|
@click="clearAccount"
|
||||||
|
class="absolute right-2 top-9 text-gray-400"
|
||||||
|
aria-label="Clear account filter"
|
||||||
|
>
|
||||||
|
✕
|
||||||
|
</button>
|
||||||
|
<div
|
||||||
|
v-if="showAccountDropdown && (accountResults.length > 0 || accountKeyword)"
|
||||||
|
class="absolute z-50 mt-1 max-h-60 w-full overflow-auto rounded-lg border bg-white shadow-lg dark:bg-gray-800"
|
||||||
|
>
|
||||||
|
<button
|
||||||
|
v-for="a in accountResults"
|
||||||
|
:key="a.id"
|
||||||
|
type="button"
|
||||||
|
@click="selectAccount(a)"
|
||||||
|
class="w-full px-4 py-2 text-left hover:bg-gray-100 dark:hover:bg-gray-700"
|
||||||
|
>
|
||||||
|
<span class="truncate">{{ a.name }}</span>
|
||||||
|
<span class="ml-2 text-xs text-gray-400">#{{ a.id }}</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!-- Stream Type Filter -->
|
<!-- Stream Type Filter -->
|
||||||
@@ -166,6 +197,7 @@ const filters = toRef(props, 'modelValue')
|
|||||||
|
|
||||||
const userSearchRef = ref<HTMLElement | null>(null)
|
const userSearchRef = ref<HTMLElement | null>(null)
|
||||||
const apiKeySearchRef = ref<HTMLElement | null>(null)
|
const apiKeySearchRef = ref<HTMLElement | null>(null)
|
||||||
|
const accountSearchRef = ref<HTMLElement | null>(null)
|
||||||
|
|
||||||
const userKeyword = ref('')
|
const userKeyword = ref('')
|
||||||
const userResults = ref<SimpleUser[]>([])
|
const userResults = ref<SimpleUser[]>([])
|
||||||
@@ -177,9 +209,17 @@ const apiKeyResults = ref<SimpleApiKey[]>([])
|
|||||||
const showApiKeyDropdown = ref(false)
|
const showApiKeyDropdown = ref(false)
|
||||||
let apiKeySearchTimeout: ReturnType<typeof setTimeout> | null = null
|
let apiKeySearchTimeout: ReturnType<typeof setTimeout> | null = null
|
||||||
|
|
||||||
|
interface SimpleAccount {
|
||||||
|
id: number
|
||||||
|
name: string
|
||||||
|
}
|
||||||
|
const accountKeyword = ref('')
|
||||||
|
const accountResults = ref<SimpleAccount[]>([])
|
||||||
|
const showAccountDropdown = ref(false)
|
||||||
|
let accountSearchTimeout: ReturnType<typeof setTimeout> | null = null
|
||||||
|
|
||||||
const modelOptions = ref<SelectOption[]>([{ value: null, label: t('admin.usage.allModels') }])
|
const modelOptions = ref<SelectOption[]>([{ value: null, label: t('admin.usage.allModels') }])
|
||||||
const groupOptions = ref<SelectOption[]>([{ value: null, label: t('admin.usage.allGroups') }])
|
const groupOptions = ref<SelectOption[]>([{ value: null, label: t('admin.usage.allGroups') }])
|
||||||
const accountOptions = ref<SelectOption[]>([{ value: null, label: t('admin.usage.allAccounts') }])
|
|
||||||
|
|
||||||
const streamTypeOptions = ref<SelectOption[]>([
|
const streamTypeOptions = ref<SelectOption[]>([
|
||||||
{ value: null, label: t('admin.usage.allTypes') },
|
{ value: null, label: t('admin.usage.allTypes') },
|
||||||
@@ -278,6 +318,37 @@ const onClearApiKey = () => {
|
|||||||
emitChange()
|
emitChange()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const debounceAccountSearch = () => {
|
||||||
|
if (accountSearchTimeout) clearTimeout(accountSearchTimeout)
|
||||||
|
accountSearchTimeout = setTimeout(async () => {
|
||||||
|
if (!accountKeyword.value) {
|
||||||
|
accountResults.value = []
|
||||||
|
return
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const res = await adminAPI.accounts.list(1, 20, { search: accountKeyword.value })
|
||||||
|
accountResults.value = res.items.map((a) => ({ id: a.id, name: a.name }))
|
||||||
|
} catch {
|
||||||
|
accountResults.value = []
|
||||||
|
}
|
||||||
|
}, 300)
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectAccount = (a: SimpleAccount) => {
|
||||||
|
accountKeyword.value = a.name
|
||||||
|
showAccountDropdown.value = false
|
||||||
|
filters.value.account_id = a.id
|
||||||
|
emitChange()
|
||||||
|
}
|
||||||
|
|
||||||
|
const clearAccount = () => {
|
||||||
|
accountKeyword.value = ''
|
||||||
|
accountResults.value = []
|
||||||
|
showAccountDropdown.value = false
|
||||||
|
filters.value.account_id = undefined
|
||||||
|
emitChange()
|
||||||
|
}
|
||||||
|
|
||||||
const onApiKeyFocus = () => {
|
const onApiKeyFocus = () => {
|
||||||
showApiKeyDropdown.value = true
|
showApiKeyDropdown.value = true
|
||||||
// Trigger search if no results yet
|
// Trigger search if no results yet
|
||||||
@@ -292,9 +363,11 @@ const onDocumentClick = (e: MouseEvent) => {
|
|||||||
|
|
||||||
const clickedInsideUser = userSearchRef.value?.contains(target) ?? false
|
const clickedInsideUser = userSearchRef.value?.contains(target) ?? false
|
||||||
const clickedInsideApiKey = apiKeySearchRef.value?.contains(target) ?? false
|
const clickedInsideApiKey = apiKeySearchRef.value?.contains(target) ?? false
|
||||||
|
const clickedInsideAccount = accountSearchRef.value?.contains(target) ?? false
|
||||||
|
|
||||||
if (!clickedInsideUser) showUserDropdown.value = false
|
if (!clickedInsideUser) showUserDropdown.value = false
|
||||||
if (!clickedInsideApiKey) showApiKeyDropdown.value = false
|
if (!clickedInsideApiKey) showApiKeyDropdown.value = false
|
||||||
|
if (!clickedInsideAccount) showAccountDropdown.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
@@ -333,20 +406,27 @@ watch(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
watch(
|
||||||
|
() => filters.value.account_id,
|
||||||
|
(accountId) => {
|
||||||
|
if (!accountId) {
|
||||||
|
accountKeyword.value = ''
|
||||||
|
accountResults.value = []
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
document.addEventListener('click', onDocumentClick)
|
document.addEventListener('click', onDocumentClick)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const [gs, ms, as] = await Promise.all([
|
const [gs, ms] = await Promise.all([
|
||||||
adminAPI.groups.list(1, 1000),
|
adminAPI.groups.list(1, 1000),
|
||||||
adminAPI.dashboard.getModelStats({ start_date: props.startDate, end_date: props.endDate }),
|
adminAPI.dashboard.getModelStats({ start_date: props.startDate, end_date: props.endDate })
|
||||||
adminAPI.accounts.list(1, 1000)
|
|
||||||
])
|
])
|
||||||
|
|
||||||
groupOptions.value.push(...gs.items.map((g: any) => ({ value: g.id, label: g.name })))
|
groupOptions.value.push(...gs.items.map((g: any) => ({ value: g.id, label: g.name })))
|
||||||
|
|
||||||
accountOptions.value.push(...as.items.map((a: any) => ({ value: a.id, label: a.name })))
|
|
||||||
|
|
||||||
const uniqueModels = new Set<string>()
|
const uniqueModels = new Set<string>()
|
||||||
ms.models?.forEach((s: any) => s.model && uniqueModels.add(s.model))
|
ms.models?.forEach((s: any) => s.model && uniqueModels.add(s.model))
|
||||||
modelOptions.value.push(
|
modelOptions.value.push(
|
||||||
|
|||||||
@@ -143,8 +143,8 @@
|
|||||||
>
|
>
|
||||||
<div class="whitespace-nowrap rounded-lg border border-gray-700 bg-gray-900 px-3 py-2.5 text-xs text-white shadow-xl dark:border-gray-600 dark:bg-gray-800">
|
<div class="whitespace-nowrap rounded-lg border border-gray-700 bg-gray-900 px-3 py-2.5 text-xs text-white shadow-xl dark:border-gray-600 dark:bg-gray-800">
|
||||||
<div class="space-y-1.5">
|
<div class="space-y-1.5">
|
||||||
<div class="mb-2 border-b border-gray-700 pb-1.5">
|
<div>
|
||||||
<div class="text-xs font-semibold text-gray-300 mb-1">Token {{ t('usage.details') }}</div>
|
<div class="text-xs font-semibold text-gray-300 mb-1">{{ t('usage.tokenDetails') }}</div>
|
||||||
<div v-if="tokenTooltipData && tokenTooltipData.input_tokens > 0" class="flex items-center justify-between gap-4">
|
<div v-if="tokenTooltipData && tokenTooltipData.input_tokens > 0" class="flex items-center justify-between gap-4">
|
||||||
<span class="text-gray-400">{{ t('admin.usage.inputTokens') }}</span>
|
<span class="text-gray-400">{{ t('admin.usage.inputTokens') }}</span>
|
||||||
<span class="font-medium text-white">{{ tokenTooltipData.input_tokens.toLocaleString() }}</span>
|
<span class="font-medium text-white">{{ tokenTooltipData.input_tokens.toLocaleString() }}</span>
|
||||||
@@ -184,6 +184,27 @@
|
|||||||
>
|
>
|
||||||
<div class="whitespace-nowrap rounded-lg border border-gray-700 bg-gray-900 px-3 py-2.5 text-xs text-white shadow-xl dark:border-gray-600 dark:bg-gray-800">
|
<div class="whitespace-nowrap rounded-lg border border-gray-700 bg-gray-900 px-3 py-2.5 text-xs text-white shadow-xl dark:border-gray-600 dark:bg-gray-800">
|
||||||
<div class="space-y-1.5">
|
<div class="space-y-1.5">
|
||||||
|
<!-- Cost Breakdown -->
|
||||||
|
<div class="mb-2 border-b border-gray-700 pb-1.5">
|
||||||
|
<div class="text-xs font-semibold text-gray-300 mb-1">{{ t('usage.costDetails') }}</div>
|
||||||
|
<div v-if="tooltipData && tooltipData.input_cost > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400">{{ t('admin.usage.inputCost') }}</span>
|
||||||
|
<span class="font-medium text-white">${{ tooltipData.input_cost.toFixed(6) }}</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="tooltipData && tooltipData.output_cost > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400">{{ t('admin.usage.outputCost') }}</span>
|
||||||
|
<span class="font-medium text-white">${{ tooltipData.output_cost.toFixed(6) }}</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="tooltipData && tooltipData.cache_creation_cost > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400">{{ t('admin.usage.cacheCreationCost') }}</span>
|
||||||
|
<span class="font-medium text-white">${{ tooltipData.cache_creation_cost.toFixed(6) }}</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="tooltipData && tooltipData.cache_read_cost > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400">{{ t('admin.usage.cacheReadCost') }}</span>
|
||||||
|
<span class="font-medium text-white">${{ tooltipData.cache_read_cost.toFixed(6) }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- Rate and Summary -->
|
||||||
<div class="flex items-center justify-between gap-6">
|
<div class="flex items-center justify-between gap-6">
|
||||||
<span class="text-gray-400">{{ t('usage.rate') }}</span>
|
<span class="text-gray-400">{{ t('usage.rate') }}</span>
|
||||||
<span class="font-semibold text-blue-400">{{ (tooltipData?.rate_multiplier || 1).toFixed(2) }}x</span>
|
<span class="font-semibold text-blue-400">{{ (tooltipData?.rate_multiplier || 1).toFixed(2) }}x</span>
|
||||||
|
|||||||
@@ -105,10 +105,7 @@
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<!-- Code Content -->
|
<!-- Code Content -->
|
||||||
<pre class="p-4 text-sm font-mono text-gray-100 overflow-x-auto">
|
<pre class="p-4 text-sm font-mono text-gray-100 overflow-x-auto"><code v-if="file.highlighted" v-html="file.highlighted"></code><code v-else v-text="file.content"></code></pre>
|
||||||
<code v-if="file.highlighted" v-html="file.highlighted"></code>
|
|
||||||
<code v-else v-text="file.content"></code>
|
|
||||||
</pre>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -376,6 +376,8 @@ export default {
|
|||||||
usage: {
|
usage: {
|
||||||
title: 'Usage Records',
|
title: 'Usage Records',
|
||||||
description: 'View and analyze your API usage history',
|
description: 'View and analyze your API usage history',
|
||||||
|
costDetails: 'Cost Breakdown',
|
||||||
|
tokenDetails: 'Token Breakdown',
|
||||||
totalRequests: 'Total Requests',
|
totalRequests: 'Total Requests',
|
||||||
totalTokens: 'Total Tokens',
|
totalTokens: 'Total Tokens',
|
||||||
totalCost: 'Total Cost',
|
totalCost: 'Total Cost',
|
||||||
@@ -1691,6 +1693,7 @@ export default {
|
|||||||
userFilter: 'User',
|
userFilter: 'User',
|
||||||
searchUserPlaceholder: 'Search user by email...',
|
searchUserPlaceholder: 'Search user by email...',
|
||||||
searchApiKeyPlaceholder: 'Search API key by name...',
|
searchApiKeyPlaceholder: 'Search API key by name...',
|
||||||
|
searchAccountPlaceholder: 'Search account by name...',
|
||||||
selectedUser: 'Selected',
|
selectedUser: 'Selected',
|
||||||
user: 'User',
|
user: 'User',
|
||||||
account: 'Account',
|
account: 'Account',
|
||||||
|
|||||||
@@ -373,6 +373,8 @@ export default {
|
|||||||
usage: {
|
usage: {
|
||||||
title: '使用记录',
|
title: '使用记录',
|
||||||
description: '查看和分析您的 API 使用历史',
|
description: '查看和分析您的 API 使用历史',
|
||||||
|
costDetails: '成本明细',
|
||||||
|
tokenDetails: 'Token 明细',
|
||||||
totalRequests: '总请求数',
|
totalRequests: '总请求数',
|
||||||
totalTokens: '总 Token',
|
totalTokens: '总 Token',
|
||||||
totalCost: '总消费',
|
totalCost: '总消费',
|
||||||
@@ -1836,6 +1838,7 @@ export default {
|
|||||||
userFilter: '用户',
|
userFilter: '用户',
|
||||||
searchUserPlaceholder: '按邮箱搜索用户...',
|
searchUserPlaceholder: '按邮箱搜索用户...',
|
||||||
searchApiKeyPlaceholder: '按名称搜索 API 密钥...',
|
searchApiKeyPlaceholder: '按名称搜索 API 密钥...',
|
||||||
|
searchAccountPlaceholder: '按名称搜索账号...',
|
||||||
selectedUser: '已选择',
|
selectedUser: '已选择',
|
||||||
user: '用户',
|
user: '用户',
|
||||||
account: '账户',
|
account: '账户',
|
||||||
|
|||||||
@@ -85,11 +85,48 @@ const exportToExcel = async () => {
|
|||||||
if (all.length >= total || res.items.length < 100) break; p++
|
if (all.length >= total || res.items.length < 100) break; p++
|
||||||
}
|
}
|
||||||
if(!c.signal.aborted) {
|
if(!c.signal.aborted) {
|
||||||
// 动态加载 xlsx,降低首屏包体并减少高危依赖的常驻暴露面。
|
|
||||||
const XLSX = await import('xlsx')
|
const XLSX = await import('xlsx')
|
||||||
const ws = XLSX.utils.json_to_sheet(all); const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, 'Usage')
|
const headers = [
|
||||||
saveAs(new Blob([XLSX.write(wb, { bookType: 'xlsx', type: 'array' })], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }), `usage_${Date.now()}.xlsx`)
|
t('usage.time'), t('admin.usage.user'), t('usage.apiKeyFilter'),
|
||||||
appStore.showSuccess('Export Success')
|
t('admin.usage.account'), t('usage.model'), t('admin.usage.group'),
|
||||||
|
t('usage.type'),
|
||||||
|
t('admin.usage.inputTokens'), t('admin.usage.outputTokens'),
|
||||||
|
t('admin.usage.cacheReadTokens'), t('admin.usage.cacheCreationTokens'),
|
||||||
|
t('admin.usage.inputCost'), t('admin.usage.outputCost'),
|
||||||
|
t('admin.usage.cacheReadCost'), t('admin.usage.cacheCreationCost'),
|
||||||
|
t('usage.rate'), t('usage.original'), t('usage.billed'),
|
||||||
|
t('usage.billingType'), t('usage.firstToken'), t('usage.duration'),
|
||||||
|
t('admin.usage.requestId')
|
||||||
|
]
|
||||||
|
const rows = all.map(log => [
|
||||||
|
log.created_at,
|
||||||
|
log.user?.email || '',
|
||||||
|
log.api_key?.name || '',
|
||||||
|
log.account?.name || '',
|
||||||
|
log.model,
|
||||||
|
log.group?.name || '',
|
||||||
|
log.stream ? t('usage.stream') : t('usage.sync'),
|
||||||
|
log.input_tokens,
|
||||||
|
log.output_tokens,
|
||||||
|
log.cache_read_tokens,
|
||||||
|
log.cache_creation_tokens,
|
||||||
|
log.input_cost?.toFixed(6) || '0.000000',
|
||||||
|
log.output_cost?.toFixed(6) || '0.000000',
|
||||||
|
log.cache_read_cost?.toFixed(6) || '0.000000',
|
||||||
|
log.cache_creation_cost?.toFixed(6) || '0.000000',
|
||||||
|
log.rate_multiplier?.toFixed(2) || '1.00',
|
||||||
|
log.total_cost?.toFixed(6) || '0.000000',
|
||||||
|
log.actual_cost?.toFixed(6) || '0.000000',
|
||||||
|
log.billing_type === 1 ? t('usage.subscription') : t('usage.balance'),
|
||||||
|
log.first_token_ms ?? '',
|
||||||
|
log.duration_ms,
|
||||||
|
log.request_id || ''
|
||||||
|
])
|
||||||
|
const ws = XLSX.utils.aoa_to_sheet([headers, ...rows])
|
||||||
|
const wb = XLSX.utils.book_new()
|
||||||
|
XLSX.utils.book_append_sheet(wb, ws, 'Usage')
|
||||||
|
saveAs(new Blob([XLSX.write(wb, { bookType: 'xlsx', type: 'array' })], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }), `usage_${filters.value.start_date}_to_${filters.value.end_date}.xlsx`)
|
||||||
|
appStore.showSuccess(t('usage.exportSuccess'))
|
||||||
}
|
}
|
||||||
} catch (error) { console.error('Failed to export:', error); appStore.showError('Export Failed') }
|
} catch (error) { console.error('Failed to export:', error); appStore.showError('Export Failed') }
|
||||||
finally { if(exportAbortController === c) { exportAbortController = null; exporting.value = false; exportProgress.show = false } }
|
finally { if(exportAbortController === c) { exportAbortController = null; exporting.value = false; exportProgress.show = false } }
|
||||||
|
|||||||
@@ -342,8 +342,8 @@
|
|||||||
>
|
>
|
||||||
<div class="space-y-1.5">
|
<div class="space-y-1.5">
|
||||||
<!-- Token Breakdown -->
|
<!-- Token Breakdown -->
|
||||||
<div class="mb-2 border-b border-gray-700 pb-1.5">
|
<div>
|
||||||
<div class="text-xs font-semibold text-gray-300 mb-1">Token 明细</div>
|
<div class="text-xs font-semibold text-gray-300 mb-1">{{ t('usage.tokenDetails') }}</div>
|
||||||
<div v-if="tokenTooltipData && tokenTooltipData.input_tokens > 0" class="flex items-center justify-between gap-4">
|
<div v-if="tokenTooltipData && tokenTooltipData.input_tokens > 0" class="flex items-center justify-between gap-4">
|
||||||
<span class="text-gray-400">{{ t('admin.usage.inputTokens') }}</span>
|
<span class="text-gray-400">{{ t('admin.usage.inputTokens') }}</span>
|
||||||
<span class="font-medium text-white">{{ tokenTooltipData.input_tokens.toLocaleString() }}</span>
|
<span class="font-medium text-white">{{ tokenTooltipData.input_tokens.toLocaleString() }}</span>
|
||||||
@@ -389,6 +389,27 @@
|
|||||||
class="whitespace-nowrap rounded-lg border border-gray-700 bg-gray-900 px-3 py-2.5 text-xs text-white shadow-xl dark:border-gray-600 dark:bg-gray-800"
|
class="whitespace-nowrap rounded-lg border border-gray-700 bg-gray-900 px-3 py-2.5 text-xs text-white shadow-xl dark:border-gray-600 dark:bg-gray-800"
|
||||||
>
|
>
|
||||||
<div class="space-y-1.5">
|
<div class="space-y-1.5">
|
||||||
|
<!-- Cost Breakdown -->
|
||||||
|
<div class="mb-2 border-b border-gray-700 pb-1.5">
|
||||||
|
<div class="text-xs font-semibold text-gray-300 mb-1">{{ t('usage.costDetails') }}</div>
|
||||||
|
<div v-if="tooltipData && tooltipData.input_cost > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400">{{ t('admin.usage.inputCost') }}</span>
|
||||||
|
<span class="font-medium text-white">${{ tooltipData.input_cost.toFixed(6) }}</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="tooltipData && tooltipData.output_cost > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400">{{ t('admin.usage.outputCost') }}</span>
|
||||||
|
<span class="font-medium text-white">${{ tooltipData.output_cost.toFixed(6) }}</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="tooltipData && tooltipData.cache_creation_cost > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400">{{ t('admin.usage.cacheCreationCost') }}</span>
|
||||||
|
<span class="font-medium text-white">${{ tooltipData.cache_creation_cost.toFixed(6) }}</span>
|
||||||
|
</div>
|
||||||
|
<div v-if="tooltipData && tooltipData.cache_read_cost > 0" class="flex items-center justify-between gap-4">
|
||||||
|
<span class="text-gray-400">{{ t('admin.usage.cacheReadCost') }}</span>
|
||||||
|
<span class="font-medium text-white">${{ tooltipData.cache_read_cost.toFixed(6) }}</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<!-- Rate and Summary -->
|
||||||
<div class="flex items-center justify-between gap-6">
|
<div class="flex items-center justify-between gap-6">
|
||||||
<span class="text-gray-400">{{ t('usage.rate') }}</span>
|
<span class="text-gray-400">{{ t('usage.rate') }}</span>
|
||||||
<span class="font-semibold text-blue-400"
|
<span class="font-semibold text-blue-400"
|
||||||
|
|||||||
Reference in New Issue
Block a user