refactor(ai): replaced FNV hash with xxHash32 and optimized OAuth credential ranking
- Replaced manual FNV hash implementation with Bun.hash.xxHash32 for better performance. - Added session preference caching to skip re-ranking when a preferred credential is available and not blocked, preventing unnecessary account switches that cold-start the server-side prompt cache. - Improved code formatting and added detailed comments explaining the ranking strategy for new sessions and blocked credentials.
This commit is contained in:
@@ -489,12 +489,7 @@ export class AuthStorage {
|
||||
*/
|
||||
#getHashedIndex(sessionId: string, total: number): number {
|
||||
if (total <= 1) return 0;
|
||||
let hash = 2166136261; // FNV offset basis
|
||||
for (let i = 0; i < sessionId.length; i++) {
|
||||
hash ^= sessionId.charCodeAt(i);
|
||||
hash = Math.imul(hash, 16777619); // FNV prime
|
||||
}
|
||||
return (hash >>> 0) % total;
|
||||
return Bun.hash.xxHash32(sessionId) % total;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1498,16 +1493,25 @@ export class AuthStorage {
|
||||
const checkUsage = strategy !== undefined && credentials.length > 1;
|
||||
const sessionCredential = this.#getSessionCredential(provider, sessionId);
|
||||
const sessionPreferredIndex = sessionCredential?.type === "oauth" ? sessionCredential.index : undefined;
|
||||
const candidates = checkUsage
|
||||
? await this.#rankOAuthSelections({ providerKey, provider, order, credentials, options, strategy })
|
||||
// Skip ranking only when the session already has a working preferred credential — re-ranking
|
||||
// mid-session causes account switches that cold-start the server-side prompt cache. New sessions
|
||||
// (no preference) and sessions whose preferred is blocked still rank, so we pick the account
|
||||
// with the most headroom proactively and fall back intelligently when rate-limited.
|
||||
const sessionPreferredIsAvailable =
|
||||
sessionPreferredIndex !== undefined && !this.#isCredentialBlocked(providerKey, sessionPreferredIndex);
|
||||
const shouldRank = checkUsage && !sessionPreferredIsAvailable;
|
||||
const candidates = shouldRank
|
||||
? await this.#rankOAuthSelections({ providerKey, provider, order, credentials, options, strategy: strategy! })
|
||||
: order
|
||||
.map(idx => credentials[idx])
|
||||
.filter((selection): selection is { credential: OAuthCredential; index: number } => Boolean(selection))
|
||||
.map(selection => ({ selection, usage: null, usageChecked: false }));
|
||||
.map(idx => credentials[idx])
|
||||
.filter((selection): selection is { credential: OAuthCredential; index: number } => Boolean(selection))
|
||||
.map(selection => ({ selection, usage: null, usageChecked: false }));
|
||||
|
||||
if (sessionPreferredIndex !== undefined) {
|
||||
const sessionPreferredCandidate = candidates.findIndex(candidate =>
|
||||
!this.#isCredentialBlocked(providerKey, candidate.selection.index) && candidate.selection.index === sessionPreferredIndex
|
||||
const sessionPreferredCandidate = candidates.findIndex(
|
||||
candidate =>
|
||||
!this.#isCredentialBlocked(providerKey, candidate.selection.index) &&
|
||||
candidate.selection.index === sessionPreferredIndex,
|
||||
);
|
||||
if (sessionPreferredCandidate > 0) {
|
||||
const [preferred] = candidates.splice(sessionPreferredCandidate, 1);
|
||||
|
||||
Reference in New Issue
Block a user