refactor(source): extract resolveSourceDisplay

This commit is contained in:
Supra4E8C
2026-02-22 16:08:06 +08:00
parent c25e7556ed
commit b363edc9bd
3 changed files with 33 additions and 34 deletions
@@ -8,7 +8,7 @@ import { authFilesApi } from '@/services/api/authFiles';
import type { GeminiKeyConfig, ProviderKeyConfig, OpenAIProviderConfig } from '@/types';
import type { AuthFileItem } from '@/types/authFile';
import type { CredentialInfo } from '@/types/sourceInfo';
import { buildSourceInfoMap } from '@/utils/sourceResolver';
import { buildSourceInfoMap, resolveSourceDisplay } from '@/utils/sourceResolver';
import {
collectUsageDetails,
extractTotalTokens,
@@ -128,13 +128,9 @@ export function RequestEventsDetailsCard({
authIndexRaw === null || authIndexRaw === undefined || authIndexRaw === ''
? '-'
: String(authIndexRaw);
const normalizedAuthIndex = normalizeAuthIndex(authIndexRaw);
const sourceInfo = sourceInfoMap.get(sourceRaw);
const authInfo = normalizedAuthIndex ? authFileMap.get(normalizedAuthIndex) : undefined;
const source = sourceInfo?.displayName
|| authInfo?.name
|| (sourceRaw.startsWith('t:') ? sourceRaw.slice(2) : sourceRaw || '-');
const sourceType = sourceInfo?.type || authInfo?.type || '';
const sourceInfo = resolveSourceDisplay(sourceRaw, authIndexRaw, sourceInfoMap, authFileMap);
const source = sourceInfo.displayName;
const sourceType = sourceInfo.type;
const model = String(detail.__modelName ?? '').trim() || '-';
const inputTokens = Math.max(toNumber(detail.tokens?.input_tokens), 0);
const outputTokens = Math.max(toNumber(detail.tokens?.output_tokens), 0);
+3 -24
View File
@@ -28,7 +28,7 @@ import { copyToClipboard } from '@/utils/clipboard';
import { downloadBlob } from '@/utils/download';
import { MANAGEMENT_API_PREFIX } from '@/utils/constants';
import { formatUnixTimestamp } from '@/utils/format';
import { buildSourceInfoMap } from '@/utils/sourceResolver';
import { buildSourceInfoMap, resolveSourceDisplay } from '@/utils/sourceResolver';
import {
collectUsageDetailsWithEndpoint,
normalizeAuthIndex,
@@ -860,29 +860,8 @@ export function LogsPage() {
return scored.slice(0, 8);
}, [traceLogLine, traceUsageDetails]);
const resolveTraceSourceInfo = useCallback(
(sourceRaw: string, authIndex: unknown): SourceInfo => {
const source = sourceRaw.trim();
const matchedSource = traceSourceInfoMap.get(source);
if (matchedSource) {
return matchedSource;
}
const authIndexKey = normalizeAuthIndex(authIndex);
if (authIndexKey) {
const authInfo = traceAuthFileMap.get(authIndexKey);
if (authInfo) {
return {
displayName: authInfo.name || authIndexKey,
type: authInfo.type
};
}
}
return {
displayName: source.startsWith('t:') ? source.slice(2) : source || '-',
type: ''
};
},
(sourceRaw: string, authIndex: unknown): SourceInfo =>
resolveSourceDisplay(sourceRaw, authIndex, traceSourceInfoMap, traceAuthFileMap),
[traceAuthFileMap, traceSourceInfoMap]
);
+26 -2
View File
@@ -1,6 +1,6 @@
import type { GeminiKeyConfig, OpenAIProviderConfig, ProviderKeyConfig } from '@/types';
import type { SourceInfo } from '@/types/sourceInfo';
import { buildCandidateUsageSourceIds } from '@/utils/usage';
import type { CredentialInfo, SourceInfo } from '@/types/sourceInfo';
import { buildCandidateUsageSourceIds, normalizeAuthIndex } from '@/utils/usage';
export interface SourceInfoMapInput {
geminiApiKeys?: GeminiKeyConfig[];
@@ -57,3 +57,27 @@ export function buildSourceInfoMap(input: SourceInfoMapInput): Map<string, Sourc
return map;
}
export function resolveSourceDisplay(
sourceRaw: string,
authIndex: unknown,
sourceInfoMap: Map<string, SourceInfo>,
authFileMap: Map<string, CredentialInfo>
): SourceInfo {
const source = sourceRaw.trim();
const matched = sourceInfoMap.get(source);
if (matched) return matched;
const authIndexKey = normalizeAuthIndex(authIndex);
if (authIndexKey) {
const authInfo = authFileMap.get(authIndexKey);
if (authInfo) {
return { displayName: authInfo.name || authIndexKey, type: authInfo.type };
}
}
return {
displayName: source.startsWith('t:') ? source.slice(2) : source || '-',
type: '',
};
}