@@ -261,9 +261,18 @@ export const useKnowledgeStore = create<KnowledgeStore>((set, get) => ({
261261 ) => {
262262 const state = get ( )
263263
264- // Return cached documents if they exist (no search-based caching since we do client-side filtering)
264+ // Check if we have cached data that matches the exact request parameters
265265 const cached = state . documents [ knowledgeBaseId ]
266- if ( cached && cached . documents . length > 0 ) {
266+ const requestLimit = options ?. limit || 50
267+ const requestOffset = options ?. offset || 0
268+ const requestSearch = options ?. search
269+
270+ if (
271+ cached &&
272+ cached . searchQuery === requestSearch &&
273+ cached . pagination . limit === requestLimit &&
274+ cached . pagination . offset === requestOffset
275+ ) {
267276 return cached . documents
268277 }
269278
@@ -277,11 +286,11 @@ export const useKnowledgeStore = create<KnowledgeStore>((set, get) => ({
277286 loadingDocuments : new Set ( [ ...state . loadingDocuments , knowledgeBaseId ] ) ,
278287 } ) )
279288
280- // Build query parameters
289+ // Build query parameters using the same defaults as caching
281290 const params = new URLSearchParams ( )
282- if ( options ?. search ) params . set ( 'search' , options . search )
283- if ( options ?. limit ) params . set ( 'limit' , options . limit . toString ( ) )
284- if ( options ?. offset ) params . set ( 'offset' , options . offset . toString ( ) )
291+ if ( requestSearch ) params . set ( 'search' , requestSearch )
292+ params . set ( 'limit' , requestLimit . toString ( ) )
293+ params . set ( 'offset' , requestOffset . toString ( ) )
285294
286295 const url = `/api/knowledge/${ knowledgeBaseId } /documents${ params . toString ( ) ? `?${ params . toString ( ) } ` : '' } `
287296 const response = await fetch ( url )
@@ -299,15 +308,15 @@ export const useKnowledgeStore = create<KnowledgeStore>((set, get) => ({
299308 const documents = result . data . documents || result . data // Handle both paginated and non-paginated responses
300309 const pagination = result . data . pagination || {
301310 total : documents . length ,
302- limit : options ?. limit || 50 ,
303- offset : options ?. offset || 0 ,
311+ limit : requestLimit ,
312+ offset : requestOffset ,
304313 hasMore : false ,
305314 }
306315
307316 const documentsCache : DocumentsCache = {
308317 documents,
309318 pagination,
310- searchQuery : options ?. search ,
319+ searchQuery : requestSearch ,
311320 lastFetchTime : Date . now ( ) ,
312321 }
313322
@@ -515,11 +524,15 @@ export const useKnowledgeStore = create<KnowledgeStore>((set, get) => ({
515524 loadingDocuments : new Set ( [ ...state . loadingDocuments , knowledgeBaseId ] ) ,
516525 } ) )
517526
518- // Build query parameters - for refresh, always start from offset 0
527+ // Build query parameters using consistent defaults
528+ const requestLimit = options ?. limit || 50
529+ const requestOffset = options ?. offset || 0
530+ const requestSearch = options ?. search
531+
519532 const params = new URLSearchParams ( )
520- if ( options ?. search ) params . set ( 'search' , options . search )
521- if ( options ?. limit ) params . set ( 'limit' , options . limit . toString ( ) )
522- params . set ( 'offset' , '0' ) // Always start fresh on refresh
533+ if ( requestSearch ) params . set ( 'search' , requestSearch )
534+ params . set ( 'limit' , requestLimit . toString ( ) )
535+ params . set ( 'offset' , requestOffset . toString ( ) )
523536
524537 const url = `/api/knowledge/${ knowledgeBaseId } /documents${ params . toString ( ) ? `?${ params . toString ( ) } ` : '' } `
525538 const response = await fetch ( url )
@@ -534,87 +547,33 @@ export const useKnowledgeStore = create<KnowledgeStore>((set, get) => ({
534547 throw new Error ( result . error || 'Failed to fetch documents' )
535548 }
536549
537- const serverDocuments = result . data . documents || result . data
550+ const documents = result . data . documents || result . data
538551 const pagination = result . data . pagination || {
539- total : serverDocuments . length ,
540- limit : options ?. limit || 50 ,
541- offset : 0 ,
552+ total : documents . length ,
553+ limit : requestLimit ,
554+ offset : requestOffset ,
542555 hasMore : false ,
543556 }
544557
545- set ( ( state ) => {
546- const currentDocuments = state . documents [ knowledgeBaseId ] ?. documents || [ ]
547-
548- // Create a map of server documents by filename for quick lookup
549- const serverDocumentsByFilename = new Map ( )
550- serverDocuments . forEach ( ( doc : DocumentData ) => {
551- serverDocumentsByFilename . set ( doc . filename , doc )
552- } )
553-
554- // Filter out temporary documents that now have real server equivalents
555- const filteredCurrentDocs = currentDocuments . filter ( ( doc ) => {
556- // If this is a temporary document (starts with temp-) and a server document exists with the same filename
557- if ( doc . id . startsWith ( 'temp-' ) && serverDocumentsByFilename . has ( doc . filename ) ) {
558- return false // Remove the temporary document
559- }
560-
561- // If this is a real document that still exists on the server, keep it for merging
562- if ( ! doc . id . startsWith ( 'temp-' ) ) {
563- const serverDoc = serverDocuments . find ( ( sDoc : DocumentData ) => sDoc . id === doc . id )
564- if ( serverDoc ) {
565- return false // Will be replaced by server version in merge below
566- }
567- }
568-
569- // Keep temporary documents that don't have server equivalents yet
570- return true
571- } )
572-
573- // Merge server documents with any remaining local documents
574- const mergedDocuments = serverDocuments . map ( ( serverDoc : DocumentData ) => {
575- const existingDoc = currentDocuments . find ( ( doc ) => doc . id === serverDoc . id )
576-
577- if ( ! existingDoc ) {
578- // New document from server, use it as-is
579- return serverDoc
580- }
581-
582- // Merge logic for existing documents (prefer server data for most fields)
583- return {
584- ...existingDoc ,
585- ...serverDoc ,
586- // Preserve any local optimistic updates that haven't been reflected on server yet
587- ...( existingDoc . processingStatus !== serverDoc . processingStatus &&
588- [ 'pending' , 'processing' ] . includes ( existingDoc . processingStatus ) &&
589- ! serverDoc . processingStartedAt
590- ? { processingStatus : existingDoc . processingStatus }
591- : { } ) ,
592- }
593- } )
594-
595- // Add any remaining temporary documents that don't have server equivalents
596- const finalDocuments = [ ...mergedDocuments , ...filteredCurrentDocs ]
597-
598- const documentsCache : DocumentsCache = {
599- documents : finalDocuments ,
600- pagination,
601- searchQuery : options ?. search ,
602- lastFetchTime : Date . now ( ) ,
603- }
604-
605- return {
606- documents : {
607- ...state . documents ,
608- [ knowledgeBaseId ] : documentsCache ,
609- } ,
610- loadingDocuments : new Set (
611- [ ...state . loadingDocuments ] . filter ( ( loadingId ) => loadingId !== knowledgeBaseId )
612- ) ,
613- }
614- } )
558+ const documentsCache : DocumentsCache = {
559+ documents,
560+ pagination,
561+ searchQuery : requestSearch ,
562+ lastFetchTime : Date . now ( ) ,
563+ }
564+
565+ set ( ( state ) => ( {
566+ documents : {
567+ ...state . documents ,
568+ [ knowledgeBaseId ] : documentsCache ,
569+ } ,
570+ loadingDocuments : new Set (
571+ [ ...state . loadingDocuments ] . filter ( ( loadingId ) => loadingId !== knowledgeBaseId )
572+ ) ,
573+ } ) )
615574
616575 logger . info ( `Documents refreshed for knowledge base: ${ knowledgeBaseId } ` )
617- return serverDocuments
576+ return documents
618577 } catch ( error ) {
619578 logger . error ( `Error refreshing documents for knowledge base ${ knowledgeBaseId } :` , error )
620579
0 commit comments