forked from CherryHQ/cherry-studio-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSearchProviderService.ts
More file actions
876 lines (759 loc) · 28.6 KB
/
WebSearchProviderService.ts
File metadata and controls
876 lines (759 loc) · 28.6 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
/**
* WebSearchProviderService - Unified WebSearch provider management service with optimistic updates
*
* Design Principles:
* 1. Singleton Pattern - Global unique instance with shared cache
* 2. LRU Cache - Cache recently accessed WebSearch providers (5 providers)
* 3. Type Safety - Full generic support with automatic type inference
* 4. Observer Pattern - Integrated with React's useSyncExternalStore
* 5. Optimistic Updates - Immediate UI response with background persistence
*
* Architecture:
* ```
* React Components
* ↓ useWebSearchProvider / useWebsearchProviders
* React Hooks (useSyncExternalStore)
* ↓ subscribe / getSnapshot
* WebSearchProviderService (This File)
* • LRU Cache (5 WebSearch providers)
* • All WebSearch Providers Cache (TTL: 5min)
* • Subscription Management (Map<providerId, Set<callback>>)
* • Request Queue (Concurrency Control)
* • Error Handling + Logging
* ↓
* websearchProviderDatabase → SQLite
* ```
*
* Cache Strategy:
* - LRU Cache: 5 recently accessed WebSearch providers
* - All WebSearch Providers Cache: All providers with 5-minute TTL
*
* @example Basic Usage
* ```typescript
* // Get WebSearch provider
* const provider = await webSearchProviderService.getProvider(id)
*
* // Create new WebSearch provider (optimistic)
* const newProvider = await webSearchProviderService.createProvider(providerData)
*
* // Subscribe to WebSearch provider changes
* const unsubscribe = webSearchProviderService.subscribeProvider(id, () => {
* console.log('WebSearch provider changed!')
* })
* ```
*/
import { websearchProviderDatabase } from '@database'
import { loggerService } from '@/services/LoggerService'
import type { WebSearchProvider } from '@/types/websearch'
const logger = loggerService.withContext('WebSearchProviderService')
/**
* Unsubscribe function returned by subscribe methods
*/
type UnsubscribeFunction = () => void
/**
* WebSearchProviderService - Singleton service for managing WebSearch providers with optimistic updates
*/
export class WebSearchProviderService {
// ==================== Singleton ====================
private static instance: WebSearchProviderService
private constructor() {
logger.debug('WebSearchProviderService instance created')
}
/**
* Get the singleton instance of WebSearchProviderService
*/
public static getInstance(): WebSearchProviderService {
if (!WebSearchProviderService.instance) {
WebSearchProviderService.instance = new WebSearchProviderService()
}
return WebSearchProviderService.instance
}
// ==================== Core Storage ====================
/**
* LRU Cache for recently accessed WebSearch providers
* Max size: 5 providers
*
* Structure: Map<providerId, WebSearchProvider>
* When cache size exceeds limit, oldest entry is removed
*/
private providerCache = new Map<string, WebSearchProvider>()
/**
* Maximum number of WebSearch providers to cache
*/
private readonly MAX_CACHE_SIZE = 5
/**
* Access order tracking for LRU eviction
* Most recently accessed at the end
*/
private accessOrder: string[] = []
/**
* Promise for ongoing load operations per WebSearch provider
* Key: providerId, Value: Promise
*/
private loadPromises = new Map<string, Promise<WebSearchProvider | null>>()
/**
* Cache for all WebSearch providers (Map for O(1) lookup by ID)
* Key: WebSearch provider ID
* Value: WebSearchProvider object
*/
private allProvidersCache = new Map<string, WebSearchProvider>()
/**
* Timestamp when all WebSearch providers were last loaded from database
* Used for TTL-based cache invalidation
*/
private allProvidersCacheTimestamp: number | null = null
/**
* Cache time-to-live in milliseconds (5 minutes)
* After this duration, cache is considered stale
*/
private readonly CACHE_TTL = 5 * 60 * 1000
/**
* Flag indicating if all WebSearch providers are being loaded
*/
private isLoadingAllProviders = false
/**
* Promise for ongoing load all WebSearch providers operation
* Prevents duplicate concurrent loads
*/
private loadAllProvidersPromise: Promise<WebSearchProvider[]> | null = null
// ==================== Subscription System ====================
/**
* Subscribers for specific WebSearch provider changes
* Key: providerId
* Value: Set of callback functions
*/
private providerSubscribers = new Map<string, Set<() => void>>()
/**
* Global subscribers that listen to all WebSearch provider changes
*/
private globalSubscribers = new Set<() => void>()
/**
* Subscribers for all WebSearch providers list changes
* These are notified when the WebSearch providers list changes (create/update/delete)
* Used by useWebsearchProviders() hook
*/
private allProvidersSubscribers = new Set<() => void>()
// ==================== Concurrency Control ====================
/**
* Update queue to ensure sequential writes for each WebSearch provider
* Prevents race conditions when the same WebSearch provider is updated multiple times rapidly
*
* Key: providerId
* Value: Promise of the ongoing update operation
*/
private updateQueue = new Map<string, Promise<void>>()
// ==================== Public API: Query Operations ====================
/**
* Get a WebSearch provider by ID with LRU caching (async)
*
* This method implements smart caching:
* 1. Check LRU cache → return if cached
* 2. Load from database → cache and return
*
* @param providerId - The WebSearch provider ID
* @returns Promise resolving to the WebSearch provider or null
*/
public async getProvider(providerId: string): Promise<WebSearchProvider | null> {
// 1. Check LRU cache
if (this.providerCache.has(providerId)) {
logger.verbose(`LRU cache hit for WebSearch provider: ${providerId}`)
const provider = this.providerCache.get(providerId)!
this.updateAccessOrder(providerId)
return provider
}
// 2. Check if already loading
if (this.loadPromises.has(providerId)) {
logger.verbose(`Waiting for ongoing load: ${providerId}`)
return await this.loadPromises.get(providerId)!
}
// 3. Load from database
logger.debug(`Loading WebSearch provider from database: ${providerId}`)
const loadPromise = this.loadProviderFromDatabase(providerId)
this.loadPromises.set(providerId, loadPromise)
try {
const provider = await loadPromise
return provider
} finally {
this.loadPromises.delete(providerId)
}
}
/**
* Get a WebSearch provider by ID synchronously (from cache only)
*
* Returns immediately from cache. Returns null if not cached.
*
* @param providerId - The WebSearch provider ID
* @returns The cached WebSearch provider or null
*/
public getProviderCached(providerId: string): WebSearchProvider | null {
// Check LRU cache
if (this.providerCache.has(providerId)) {
const provider = this.providerCache.get(providerId)!
this.updateAccessOrder(providerId)
return provider
}
// Check all WebSearch providers cache
if (this.allProvidersCache.has(providerId)) {
return this.allProvidersCache.get(providerId)!
}
return null
}
/**
* Get all WebSearch providers with caching
*
* Loads from cache if available and not stale, otherwise loads from database.
* This is the main method for loading all WebSearch providers with automatic caching.
*
* @param forceRefresh - Force reload from database even if cache is valid
* @returns Promise resolving to array of all WebSearch providers
*/
public async getAllProviders(forceRefresh = false): Promise<WebSearchProvider[]> {
// Check if cache is valid
const isCacheValid =
!forceRefresh &&
this.allProvidersCacheTimestamp !== null &&
Date.now() - this.allProvidersCacheTimestamp < this.CACHE_TTL &&
this.allProvidersCache.size > 0
if (isCacheValid) {
logger.verbose('Returning cached WebSearch providers, cache size:', this.allProvidersCache.size)
return Array.from(this.allProvidersCache.values())
}
// If already loading, wait for ongoing load
if (this.isLoadingAllProviders && this.loadAllProvidersPromise) {
logger.verbose('Waiting for ongoing getAllProviders operation')
return await this.loadAllProvidersPromise
}
// Load from database
return await this.loadAllProvidersFromDatabase()
}
/**
* Get all WebSearch providers from cache (synchronous)
*
* Returns cached WebSearch providers immediately. If cache is empty, returns empty array.
* Used by React's useSyncExternalStore for synchronous snapshot.
*
* @returns Array of cached WebSearch providers
*/
public getAllProvidersCached(): WebSearchProvider[] {
return Array.from(this.allProvidersCache.values())
}
// ==================== Public API: CRUD Operations ====================
/**
* Create a new WebSearch provider (optimistic)
*
* Creates WebSearch provider immediately in memory, then persists to database.
*
* @param provider - The WebSearch provider data
* @returns The created WebSearch provider
*/
public async createProvider(provider: WebSearchProvider): Promise<WebSearchProvider> {
logger.info('Creating new WebSearch provider (optimistic):', provider.id)
// Optimistic update: add to caches immediately
if (this.allProvidersCache.size > 0 || this.allProvidersCacheTimestamp !== null) {
this.allProvidersCache.set(provider.id, provider)
logger.verbose(`Added new WebSearch provider to cache: ${provider.id}`)
}
// Notify subscribers (UI updates immediately)
this.notifyProviderSubscribers(provider.id)
this.notifyGlobalSubscribers()
this.notifyAllProvidersSubscribers()
try {
// Persist to database
await websearchProviderDatabase.upsertWebSearchProviders([provider])
logger.info('WebSearch provider created successfully:', provider.id)
return provider
} catch (error) {
// Rollback on failure
logger.error('Failed to create WebSearch provider, rolling back:', error as Error)
// Remove from cache
if (this.allProvidersCache.has(provider.id)) {
this.allProvidersCache.delete(provider.id)
}
// Notify subscribers to revert UI
this.notifyProviderSubscribers(provider.id)
this.notifyGlobalSubscribers()
this.notifyAllProvidersSubscribers()
throw error
}
}
/**
* Update a WebSearch provider (optimistic)
*
* Updates immediately in cache, then persists.
*
* @param providerId - The WebSearch provider ID to update
* @param updates - Partial WebSearch provider data to update
*/
public async updateProvider(providerId: string, updates: Partial<Omit<WebSearchProvider, 'id'>>): Promise<void> {
// Wait for any ongoing update to the same WebSearch provider
const previousUpdate = this.updateQueue.get(providerId)
if (previousUpdate) {
await previousUpdate
}
// Execute current update
const currentUpdate = this.performProviderUpdate(providerId, updates)
this.updateQueue.set(providerId, currentUpdate)
try {
await currentUpdate
} finally {
// Clean up queue
if (this.updateQueue.get(providerId) === currentUpdate) {
this.updateQueue.delete(providerId)
}
}
}
/**
* Delete a WebSearch provider (optimistic)
*
* Removes from cache, then deletes from database.
*
* @param providerId - The WebSearch provider ID to delete
*/
public async deleteProvider(providerId: string): Promise<void> {
logger.info('Deleting WebSearch provider (optimistic):', providerId)
// Save old data for rollback
const oldCachedProvider = this.allProvidersCache.get(providerId)
const oldLRUProvider = this.providerCache.get(providerId)
// Remove from all caches
if (this.allProvidersCache.has(providerId)) {
this.allProvidersCache.delete(providerId)
logger.verbose(`Removed WebSearch provider from all providers cache: ${providerId}`)
}
if (this.providerCache.has(providerId)) {
this.providerCache.delete(providerId)
const index = this.accessOrder.indexOf(providerId)
if (index > -1) {
this.accessOrder.splice(index, 1)
}
logger.verbose(`Removed WebSearch provider from LRU cache: ${providerId}`)
}
// Notify subscribers (UI updates immediately)
this.notifyProviderSubscribers(providerId)
this.notifyGlobalSubscribers()
this.notifyAllProvidersSubscribers()
try {
// Delete from database
await websearchProviderDatabase.deleteWebSearchProvider(providerId)
logger.info('WebSearch provider deleted successfully:', providerId)
} catch (error) {
// Rollback on failure
logger.error('Failed to delete WebSearch provider, rolling back:', error as Error)
// Restore caches
if (oldCachedProvider) {
this.allProvidersCache.set(providerId, oldCachedProvider)
}
if (oldLRUProvider) {
this.providerCache.set(providerId, oldLRUProvider)
this.accessOrder.push(providerId)
}
// Notify subscribers to revert UI
this.notifyProviderSubscribers(providerId)
this.notifyGlobalSubscribers()
this.notifyAllProvidersSubscribers()
throw error
}
}
// ==================== Public API: Cache Operations ====================
/**
* Refresh all WebSearch providers cache from database
*
* Forces a reload of all WebSearch providers from database, updating the cache.
* Useful for pull-to-refresh functionality.
*
* @returns Promise resolving to array of refreshed WebSearch providers
*/
public async refreshAllProvidersCache(): Promise<WebSearchProvider[]> {
logger.info('Manually refreshing all WebSearch providers cache')
return await this.getAllProviders(true)
}
/**
* Invalidate all WebSearch providers cache
*
* Clears the cache and forces next access to reload from database.
* Used for logout or data reset scenarios.
*/
public invalidateCache(): void {
this.allProvidersCache.clear()
this.allProvidersCacheTimestamp = null
this.providerCache.clear()
this.accessOrder = []
logger.info('All WebSearch providers cache invalidated')
this.notifyAllProvidersSubscribers()
}
// ==================== Public API: Subscription ====================
/**
* Subscribe to changes for a specific WebSearch provider
*
* @param providerId - The WebSearch provider ID to watch
* @param callback - Function to call when the WebSearch provider changes
* @returns Unsubscribe function
*/
public subscribeProvider(providerId: string, callback: () => void): UnsubscribeFunction {
if (!this.providerSubscribers.has(providerId)) {
this.providerSubscribers.set(providerId, new Set())
}
const subscribers = this.providerSubscribers.get(providerId)!
subscribers.add(callback)
logger.verbose(`Added subscriber for WebSearch provider ${providerId}, total: ${subscribers.size}`)
return () => {
subscribers.delete(callback)
// Clean up empty subscriber sets
if (subscribers.size === 0) {
this.providerSubscribers.delete(providerId)
logger.verbose(`Removed last subscriber for WebSearch provider ${providerId}, cleaned up`)
} else {
logger.verbose(`Removed subscriber for WebSearch provider ${providerId}, remaining: ${subscribers.size}`)
}
}
}
/**
* Subscribe to all WebSearch provider changes
*
* @param callback - Function to call when any WebSearch provider changes
* @returns Unsubscribe function
*/
public subscribeAll(callback: () => void): UnsubscribeFunction {
this.globalSubscribers.add(callback)
logger.verbose(`Added global subscriber, total: ${this.globalSubscribers.size}`)
return () => {
this.globalSubscribers.delete(callback)
logger.verbose(`Removed global subscriber, remaining: ${this.globalSubscribers.size}`)
}
}
/**
* Subscribe to all WebSearch providers list changes
*
* The callback is invoked whenever the WebSearch providers list changes (create/update/delete).
* Used by useWebsearchProviders() hook with useSyncExternalStore.
*
* @param callback - Function to call when WebSearch providers list changes
* @returns Unsubscribe function
*/
public subscribeAllProviders(callback: () => void): UnsubscribeFunction {
this.allProvidersSubscribers.add(callback)
logger.verbose(`Added all WebSearch providers subscriber, total: ${this.allProvidersSubscribers.size}`)
return () => {
this.allProvidersSubscribers.delete(callback)
logger.verbose(`Removed all WebSearch providers subscriber, remaining: ${this.allProvidersSubscribers.size}`)
}
}
// ==================== Private Methods: Database Operations ====================
/**
* Load a WebSearch provider from database and add to LRU cache
*/
private async loadProviderFromDatabase(providerId: string): Promise<WebSearchProvider | null> {
try {
const provider = await websearchProviderDatabase.getWebSearchProviderById(providerId)
if (provider) {
// Add to LRU cache
this.addToCache(providerId, provider)
logger.debug(`Loaded WebSearch provider from database and cached: ${providerId}`)
return provider
} else {
logger.warn(`WebSearch provider ${providerId} not found in database`)
return null
}
} catch (error) {
logger.error(`Failed to load WebSearch provider ${providerId} from database:`, error as Error)
return null
}
}
/**
* Load all WebSearch providers from database and update cache
*
* This method is called when cache is invalid or forced refresh is requested.
* It prevents duplicate concurrent loads using a promise flag.
*/
private async loadAllProvidersFromDatabase(): Promise<WebSearchProvider[]> {
// If already loading, wait for the ongoing operation
if (this.isLoadingAllProviders && this.loadAllProvidersPromise) {
logger.verbose('Waiting for ongoing loadAllProviders operation')
return await this.loadAllProvidersPromise
}
// Start loading
this.isLoadingAllProviders = true
this.loadAllProvidersPromise = (async () => {
try {
logger.info('Loading all WebSearch providers from database')
const providers = await websearchProviderDatabase.getAllWebSearchProviders()
// Update cache
this.allProvidersCache.clear()
providers.forEach(provider => {
this.allProvidersCache.set(provider.id, provider)
})
// Update timestamp
this.allProvidersCacheTimestamp = Date.now()
logger.info(`Loaded ${providers.length} WebSearch providers into cache`)
// Notify subscribers
this.notifyAllProvidersSubscribers()
return providers
} catch (error) {
logger.error('Failed to load all WebSearch providers from database:', error as Error)
throw error
} finally {
this.isLoadingAllProviders = false
this.loadAllProvidersPromise = null
}
})()
return await this.loadAllProvidersPromise
}
/**
* Perform optimistic WebSearch provider update with rollback on failure
*/
private async performProviderUpdate(
providerId: string,
updates: Partial<Omit<WebSearchProvider, 'id'>>
): Promise<void> {
// Save old data for rollback
const oldLRUProvider = this.providerCache.get(providerId) ? { ...this.providerCache.get(providerId)! } : null
const oldAllProvidersProvider = this.allProvidersCache.get(providerId)
? { ...this.allProvidersCache.get(providerId)! }
: null
try {
// Fetch current WebSearch provider data
let currentProviderData: WebSearchProvider
// Try to get from LRU cache
if (this.providerCache.has(providerId)) {
currentProviderData = this.providerCache.get(providerId)!
}
// Try to get from all providers cache
else if (this.allProvidersCache.has(providerId)) {
currentProviderData = this.allProvidersCache.get(providerId)!
}
// Load from database
else {
const provider = await websearchProviderDatabase.getWebSearchProviderById(providerId)
if (!provider) {
throw new Error(`WebSearch provider with ID ${providerId} not found`)
}
currentProviderData = provider
}
// Prepare updated provider
const updatedProvider: WebSearchProvider = {
...currentProviderData,
...updates,
id: providerId // Ensure ID is not overwritten
}
// Optimistic update: update all caches
this.updateProviderInCache(providerId, updatedProvider)
// Notify subscribers (UI updates immediately)
this.notifyProviderSubscribers(providerId)
// Persist to database
await websearchProviderDatabase.upsertWebSearchProviders([updatedProvider])
// Notify other subscribers
this.notifyGlobalSubscribers()
this.notifyAllProvidersSubscribers()
logger.debug(`WebSearch provider updated successfully: ${providerId}`)
} catch (error) {
// Rollback on failure
logger.error('Failed to update WebSearch provider, rolling back:', error as Error)
// Rollback LRU cache
if (oldLRUProvider) {
this.providerCache.set(providerId, oldLRUProvider)
} else {
this.providerCache.delete(providerId)
}
// Rollback all providers cache
if (oldAllProvidersProvider) {
this.allProvidersCache.set(providerId, oldAllProvidersProvider)
} else {
this.allProvidersCache.delete(providerId)
}
// Notify subscribers to revert UI
this.notifyProviderSubscribers(providerId)
throw error
}
}
// ==================== Private Methods: Notification ====================
/**
* Notify all subscribers for a specific WebSearch provider
*/
private notifyProviderSubscribers(providerId: string): void {
const subscribers = this.providerSubscribers.get(providerId)
if (subscribers && subscribers.size > 0) {
logger.verbose(`Notifying ${subscribers.size} subscribers for WebSearch provider ${providerId}`)
subscribers.forEach(callback => {
try {
callback()
} catch (error) {
logger.error(`Error in WebSearch provider ${providerId} subscriber callback:`, error as Error)
}
})
}
}
/**
* Notify all global subscribers
*/
private notifyGlobalSubscribers(): void {
if (this.globalSubscribers.size > 0) {
logger.verbose(`Notifying ${this.globalSubscribers.size} global subscribers`)
this.globalSubscribers.forEach(callback => {
try {
callback()
} catch (error) {
logger.error('Error in global subscriber callback:', error as Error)
}
})
}
}
/**
* Notify all WebSearch providers list subscribers
*
* Called when the WebSearch providers list changes (create/update/delete).
* Used by useWebsearchProviders() hook with useSyncExternalStore.
*/
private notifyAllProvidersSubscribers(): void {
if (this.allProvidersSubscribers.size > 0) {
logger.verbose(`Notifying ${this.allProvidersSubscribers.size} all WebSearch providers subscribers`)
this.allProvidersSubscribers.forEach(callback => {
try {
callback()
} catch (error) {
logger.error('Error in all WebSearch providers subscriber callback:', error as Error)
}
})
}
}
// ==================== Private Methods: LRU Cache Management ====================
/**
* Add or update a WebSearch provider in the LRU cache
*
* If cache is full, evicts the oldest entry.
* Updates access order.
*
* @param providerId - The WebSearch provider ID
* @param provider - The WebSearch provider data
*/
private addToCache(providerId: string, provider: WebSearchProvider): void {
// If cache is full and provider is not already cached, evict oldest
if (!this.providerCache.has(providerId) && this.providerCache.size >= this.MAX_CACHE_SIZE) {
this.evictOldestFromCache()
}
// Add or update in cache
this.providerCache.set(providerId, provider)
// Update access order
this.updateAccessOrder(providerId)
logger.verbose(`Added WebSearch provider to LRU cache: ${providerId} (cache size: ${this.providerCache.size})`)
}
/**
* Update access order for LRU eviction
*
* Moves the providerId to the end of the access order (most recently used).
*
* @param providerId - The WebSearch provider ID to update
*/
private updateAccessOrder(providerId: string): void {
// Remove from current position
const index = this.accessOrder.indexOf(providerId)
if (index > -1) {
this.accessOrder.splice(index, 1)
}
// Add to end (most recently used)
this.accessOrder.push(providerId)
}
/**
* Evict the oldest (least recently used) WebSearch provider from cache
*/
private evictOldestFromCache(): void {
if (this.accessOrder.length === 0) {
logger.warn('Attempted to evict from empty LRU cache')
return
}
// Get oldest provider (first in access order)
const oldestProviderId = this.accessOrder.shift()!
// Remove from cache
this.providerCache.delete(oldestProviderId)
logger.debug(`Evicted oldest WebSearch provider from LRU cache: ${oldestProviderId}`)
}
/**
* Update a WebSearch provider in all caches
*
* Updates the WebSearch provider in:
* - providerCache (LRU cache)
* - allProvidersCache (if exists)
*
* @param providerId - The WebSearch provider ID
* @param updatedProvider - The updated WebSearch provider data
*/
private updateProviderInCache(providerId: string, updatedProvider: WebSearchProvider): void {
// Update LRU cache if it exists
if (this.providerCache.has(providerId)) {
this.providerCache.set(providerId, updatedProvider)
this.updateAccessOrder(providerId)
logger.verbose(`Updated LRU cache for WebSearch provider: ${providerId}`)
}
// Update all providers cache if it exists
if (this.allProvidersCache.has(providerId)) {
this.allProvidersCache.set(providerId, updatedProvider)
logger.verbose(`Updated all providers cache for WebSearch provider: ${providerId}`)
}
}
// ==================== Debug Methods ====================
/**
* Get current cache status (for debugging)
*/
public getCacheStatus(): {
lruCache: {
size: number
maxSize: number
providerIds: string[]
accessOrder: string[]
}
allProvidersCache: {
size: number
isCacheValid: boolean
cacheAge: number | null
}
} {
const cacheAge = this.allProvidersCacheTimestamp !== null ? Date.now() - this.allProvidersCacheTimestamp : null
return {
lruCache: {
size: this.providerCache.size,
maxSize: this.MAX_CACHE_SIZE,
providerIds: Array.from(this.providerCache.keys()),
accessOrder: [...this.accessOrder]
},
allProvidersCache: {
size: this.allProvidersCache.size,
isCacheValid:
this.allProvidersCacheTimestamp !== null && Date.now() - this.allProvidersCacheTimestamp < this.CACHE_TTL,
cacheAge
}
}
}
/**
* Print detailed cache status to console (for debugging)
*/
public logCacheStatus(): void {
const status = this.getCacheStatus()
logger.info('==================== WebSearchProviderService Cache Status ====================')
logger.info('LRU Cache:')
logger.info(` - Size: ${status.lruCache.size}/${status.lruCache.maxSize}`)
logger.info(` - Cached WebSearch Providers: [${status.lruCache.providerIds.join(', ')}]`)
logger.info(` - Access Order (oldest→newest): [${status.lruCache.accessOrder.join(', ')}]`)
logger.info('')
logger.info('All WebSearch Providers Cache:')
logger.info(` - Size: ${status.allProvidersCache.size}`)
logger.info(` - Valid: ${status.allProvidersCache.isCacheValid}`)
if (status.allProvidersCache.cacheAge !== null) {
logger.info(` - Age: ${Math.round(status.allProvidersCache.cacheAge / 1000)}s`)
}
logger.info('==============================================================================')
}
}
// ==================== Exported Singleton Instance ====================
/**
* Singleton instance of WebSearchProviderService
*
* Use this instance throughout the application for WebSearch provider management.
*
* @example
* ```typescript
* import { webSearchProviderService } from '@/services/WebSearchProviderService'
*
* const provider = await webSearchProviderService.getProvider(id)
* await webSearchProviderService.createProvider(providerData)
* await webSearchProviderService.updateProvider(id, updates)
* ```
*/
export const webSearchProviderService = WebSearchProviderService.getInstance()