Skip to content

Commit 73bfddd

Browse files
Raing5Dayswsp1911
authored andcommitted
perf(streaming-render): autonomous optimization for OpCase-Auto-Providercatalog-01
1 parent bfaabe1 commit 73bfddd

1 file changed

Lines changed: 63 additions & 20 deletions

File tree

‎src/web-ui/src/infrastructure/config/services/providerCatalog.ts‎

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@ export function normalizeProviderBaseUrl(url: string): string {
3232
return normalized;
3333
}
3434

35+
interface NormalizedCatalogEntry {
36+
item: ProviderUrlCatalogItem;
37+
normalizedUrl: string;
38+
}
39+
40+
const normalizedCatalogCache = new WeakMap<ProviderUrlCatalogItem[], NormalizedCatalogEntry[]>();
41+
42+
function getNormalizedCatalogEntries(
43+
catalog: ProviderUrlCatalogItem[],
44+
): NormalizedCatalogEntry[] {
45+
const cached = normalizedCatalogCache.get(catalog);
46+
if (cached) return cached;
47+
48+
const entries: NormalizedCatalogEntry[] = [];
49+
for (const item of catalog) {
50+
entries.push({ item, normalizedUrl: normalizeProviderBaseUrl(item.baseUrl) });
51+
const options = item.baseUrlOptions;
52+
if (options) {
53+
for (const option of options) {
54+
entries.push({ item, normalizedUrl: normalizeProviderBaseUrl(option) });
55+
}
56+
}
57+
}
58+
// Longest-first so the first match is the most specific candidate.
59+
entries.sort((a, b) => b.normalizedUrl.length - a.normalizedUrl.length);
60+
61+
normalizedCatalogCache.set(catalog, entries);
62+
return entries;
63+
}
64+
3565
export function matchProviderCatalogItemByBaseUrl(
3666
baseUrl?: string,
3767
catalog: ProviderUrlCatalogItem[] = PROVIDER_URL_CATALOG
@@ -40,37 +70,50 @@ export function matchProviderCatalogItemByBaseUrl(
4070
if (!configBaseUrl) return undefined;
4171

4272
const normalizedConfigBaseUrl = normalizeProviderBaseUrl(configBaseUrl);
43-
const candidates = catalog.flatMap(item => {
44-
const urls = [item.baseUrl, ...(item.baseUrlOptions || [])];
45-
return urls.map(url => ({
46-
item,
47-
normalizedUrl: normalizeProviderBaseUrl(url),
48-
}));
49-
});
50-
51-
const matched = candidates
52-
.filter(candidate => (
53-
normalizedConfigBaseUrl === candidate.normalizedUrl ||
54-
normalizedConfigBaseUrl.startsWith(`${candidate.normalizedUrl}/`) ||
55-
candidate.normalizedUrl.startsWith(`${normalizedConfigBaseUrl}/`)
56-
))
57-
.sort((a, b) => b.normalizedUrl.length - a.normalizedUrl.length)[0];
58-
59-
return matched?.item;
73+
const entries = getNormalizedCatalogEntries(catalog);
74+
75+
for (const entry of entries) {
76+
const candidateUrl = entry.normalizedUrl;
77+
if (
78+
normalizedConfigBaseUrl === candidateUrl ||
79+
normalizedConfigBaseUrl.startsWith(`${candidateUrl}/`) ||
80+
candidateUrl.startsWith(`${normalizedConfigBaseUrl}/`)
81+
) {
82+
return entry.item;
83+
}
84+
}
85+
86+
return undefined;
6087
}
6188

89+
const providerSegmentCache = new Map<string, string | undefined>();
90+
const PROVIDER_SEGMENT_CACHE_LIMIT = 256;
91+
6292
export function extractProviderSegmentFromBaseUrl(baseUrl?: string): string | undefined {
6393
const rawBaseUrl = baseUrl?.trim();
6494
if (!rawBaseUrl) return undefined;
6595

96+
if (providerSegmentCache.has(rawBaseUrl)) {
97+
return providerSegmentCache.get(rawBaseUrl);
98+
}
99+
100+
let segment: string | undefined;
66101
try {
67102
const hostname = new URL(rawBaseUrl).hostname.toLowerCase();
68103
const parts = hostname.split('.').filter(Boolean);
69104
if (parts.length >= 2) {
70-
return parts[parts.length - 2];
105+
segment = parts[parts.length - 2];
106+
} else {
107+
segment = parts[0];
71108
}
72-
return parts[0];
73109
} catch {
74-
return undefined;
110+
segment = undefined;
111+
}
112+
113+
if (providerSegmentCache.size >= PROVIDER_SEGMENT_CACHE_LIMIT) {
114+
providerSegmentCache.clear();
75115
}
116+
providerSegmentCache.set(rawBaseUrl, segment);
117+
118+
return segment;
76119
}

0 commit comments

Comments
 (0)