Skip to content

Commit a7d556a

Browse files
committed
Actually fix notes indexing....
1 parent b89b0ad commit a7d556a

File tree

8 files changed

+194
-104
lines changed

8 files changed

+194
-104
lines changed

src/shared/services/vectorStorageService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ export class VectorStorageService {
2828
): Promise<void> {
2929
// Normalize ID for consistent storage
3030
const normalizedId = this.normalizeId(id);
31-
console.log(`Storing embedding for ${collection}/${normalizedId}`);
3231

3332
try {
3433
// Store the embedding in vector storage

src/shared/vector-storage.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ export class VectorStorage {
139139
await this.initialize();
140140

141141
try {
142-
console.log(`Storing embedding for ${collection}/${documentId}`);
143-
144142
// Sanitize the document ID to ensure it's storage-safe
145143
const sanitizedDocId = this.sanitizeDocumentId(documentId);
146144
const vectorId = this.createVectorId(sanitizedDocId, collection);

src/web/app/layouts/RootLayout.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export const Route = createRootRoute({
55
component: () => (
66
<Layout>
77
<Outlet />
8-
{/* <TanStackRouterDevtools /> */}
98
</Layout>
109
),
1110
});

src/web/app/layouts/layout.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
} from 'react-icons/vsc';
1212
import '@/app/styles/electron.css';
1313
import { useWindowState } from '@shared/useWindowState';
14-
import NoteIndexingHandler from '@/features/notes/components/core/NoteIndexingHandler';
1514

1615
type LayoutProps = {
1716
children?: React.ReactNode;
@@ -97,12 +96,7 @@ export function Layout({ children }: LayoutProps) {
9796
</div>
9897

9998
{/* Main content - fills remaining height with proper overflow handling */}
100-
<div className="flex-1 min-h-0">
101-
{children}
102-
103-
{/* Background services/handlers */}
104-
<NoteIndexingHandler />
105-
</div>
99+
<div className="flex-1 min-h-0">{children}</div>
106100
</div>
107101
);
108102
}

src/web/app/main.tsx

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { initializePrompts } from '@/features/prompts-library/state/promptsLibra
2222
import { initializeNotes } from '@/features/notes/state/notesState';
2323
import { resetSearchStateDefaults } from '../features/notes/state/searchState';
2424
import { initializeChats } from '@/features/chats/state/chatsState';
25+
import { NoteIndexingProvider } from '../features/notes/contexts/NoteIndexingProvider';
2526

2627
// Configure Legend State for reactivity and persistence
2728
enableReactTracking({
@@ -87,23 +88,25 @@ if (!rootElement?.innerHTML) {
8788
<StrictMode>
8889
{/* Theme provider should be the outermost provider after StrictMode */}
8990
<ThemeProvider>
90-
{/* TRPC Provider next for data fetching capabilities */}
91-
<t.Provider
92-
client={trpcClient}
93-
queryClient={queryClient}
94-
>
95-
{/* Query client provider for React Query functionality */}
96-
<QueryClientProvider client={queryClient}>
97-
{/* Router provider for application routing */}
98-
<RouterProvider router={router} />
99-
{/* Toast notifications */}
100-
<Toaster
101-
richColors
102-
position="top-right"
103-
offset={50}
104-
/>
105-
</QueryClientProvider>
106-
</t.Provider>
91+
<NoteIndexingProvider>
92+
{/* TRPC Provider next for data fetching capabilities */}
93+
<t.Provider
94+
client={trpcClient}
95+
queryClient={queryClient}
96+
>
97+
{/* Query client provider for React Query functionality */}
98+
<QueryClientProvider client={queryClient}>
99+
{/* Router provider for application routing */}
100+
<RouterProvider router={router} />
101+
{/* Toast notifications */}
102+
<Toaster
103+
richColors
104+
position="top-right"
105+
offset={50}
106+
/>
107+
</QueryClientProvider>
108+
</t.Provider>
109+
</NoteIndexingProvider>
107110
</ThemeProvider>
108111
</StrictMode>
109112
);

src/web/features/notes/components/core/NoteIndexingHandler.tsx

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
import React, {
2+
createContext,
3+
useContext,
4+
useEffect,
5+
useState,
6+
useRef,
7+
} from 'react';
8+
import { useVectorIndexing } from '../hooks/useVectorIndexing';
9+
import { trpcProxyClient } from '@shared/config';
10+
11+
// Static flag to ensure indexing is only initialized once across all instances
12+
// This is outside the component to persist across component mounts/unmounts
13+
let hasInitializedIndexing = false;
14+
15+
interface IndexingContextType {
16+
isIndexingComplete: boolean;
17+
isIndexing: boolean;
18+
indexingProgress: {
19+
total: number;
20+
processed: number;
21+
};
22+
triggerManualIndexing: (forceReindex?: boolean) => Promise<void>;
23+
}
24+
25+
const IndexingContext = createContext<IndexingContextType | undefined>(
26+
undefined
27+
);
28+
29+
/**
30+
* Provider component that handles note vector indexing once per session
31+
* and provides indexing state to the application
32+
*/
33+
export function NoteIndexingProvider({
34+
children,
35+
}: {
36+
children: React.ReactNode;
37+
}) {
38+
const { checkIndexingStatus, startIndexing } = useVectorIndexing();
39+
const [isIndexingComplete, setIsIndexingComplete] = useState(false);
40+
const [isIndexing, setIsIndexing] = useState(false);
41+
const [indexingProgress, setIndexingProgress] = useState({
42+
total: 0,
43+
processed: 0,
44+
});
45+
46+
// Function to manually trigger indexing
47+
const triggerManualIndexing = async (forceReindex = false) => {
48+
try {
49+
const result = await startIndexing(forceReindex);
50+
console.log('Manual indexing triggered:', result);
51+
} catch (error) {
52+
console.error('Error triggering manual indexing:', error);
53+
}
54+
};
55+
56+
// Run indexing check once on app startup
57+
useEffect(() => {
58+
// Set up subscription to indexing status updates first
59+
const subscription = trpcProxyClient.notes.onIndexingStatus.subscribe(
60+
undefined,
61+
{
62+
onData: (data) => {
63+
setIsIndexing(
64+
data.status === 'started' || data.status === 'progress'
65+
);
66+
setIsIndexingComplete(data.status === 'completed');
67+
68+
if (data.total !== undefined && data.processed !== undefined) {
69+
setIndexingProgress({
70+
total: data.total,
71+
processed: data.processed,
72+
});
73+
}
74+
},
75+
onError: (error) => {
76+
console.error('Error in indexing status subscription:', error);
77+
setIsIndexing(false);
78+
},
79+
}
80+
);
81+
82+
// Only run the initialization logic once per app session
83+
if (!hasInitializedIndexing) {
84+
hasInitializedIndexing = true;
85+
console.log('Initializing indexing check (first time)');
86+
87+
const checkIndexingNeeded = async () => {
88+
try {
89+
// First check if we're already indexing
90+
const status = await checkIndexingStatus();
91+
setIsIndexing(status.isIndexing);
92+
93+
if (status.isIndexing) {
94+
console.log('Indexing already in progress');
95+
return;
96+
}
97+
98+
// Check if notes are already indexed
99+
const notesNeedingIndexing =
100+
await trpcProxyClient.notes.getNotesNeedingIndexing.query();
101+
102+
console.log(
103+
'INDEXING DEBUG: Notes needing indexing:',
104+
notesNeedingIndexing
105+
);
106+
107+
const needsIndexing = notesNeedingIndexing.needsIndexing.length > 0;
108+
109+
// If indexing is needed, start it with a delay
110+
if (needsIndexing) {
111+
console.log('Notes not indexed, will trigger background indexing');
112+
setTimeout(() => {
113+
startIndexing(false)
114+
.then((result) => {
115+
console.log('Indexing started in background:', result);
116+
})
117+
.catch((error) => {
118+
console.error('Error starting background indexing:', error);
119+
});
120+
}, 5000); // 5 second delay to allow app to initialize
121+
} else {
122+
console.log('Notes already indexed, no need to start indexing');
123+
setIsIndexingComplete(true);
124+
}
125+
} catch (error) {
126+
console.error('Error checking if indexing is needed:', error);
127+
}
128+
};
129+
130+
// Start the check with a small delay to allow other initialization to complete
131+
setTimeout(() => {
132+
checkIndexingNeeded();
133+
}, 2000);
134+
} else {
135+
console.log('Skipping indexing initialization (already done)');
136+
// Still check status to update the UI correctly
137+
checkIndexingStatus().then((status) => {
138+
setIsIndexing(status.isIndexing);
139+
});
140+
}
141+
142+
// Cleanup subscription
143+
return () => {
144+
subscription.unsubscribe();
145+
};
146+
}, [checkIndexingStatus, startIndexing]);
147+
148+
const value = {
149+
isIndexingComplete,
150+
isIndexing,
151+
indexingProgress,
152+
triggerManualIndexing,
153+
};
154+
155+
return (
156+
<IndexingContext.Provider value={value}>
157+
{children}
158+
</IndexingContext.Provider>
159+
);
160+
}
161+
162+
/**
163+
* Hook to access the indexing context
164+
*/
165+
export function useNoteIndexing() {
166+
const context = useContext(IndexingContext);
167+
if (context === undefined) {
168+
throw new Error(
169+
'useNoteIndexing must be used within a NoteIndexingProvider'
170+
);
171+
}
172+
return context;
173+
}

src/web/routes/index.lazy.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { middleSidebarWidth } from '@/features/settings/state/layoutSettingsStat
99
import { observer } from '@legendapp/state/react';
1010
import { PromptsLibraryModal } from '@/features/prompts-library/components/PromptsLibraryModal';
1111
import { SmartHubsModal } from '@/features/smart-hubs/components/SmartHubsModal';
12-
import { NoteIndexingHandler } from '@/features/notes/components/core/NoteIndexingHandler';
1312

1413
// Define the Index component first
1514
const Index = observer(function Index() {
@@ -113,8 +112,6 @@ const Index = observer(function Index() {
113112
</div>
114113
</div>
115114

116-
<NoteIndexingHandler />
117-
118115
{smartHubsModalVisible && (
119116
<SmartHubsModal
120117
visible={smartHubsModalVisible}

0 commit comments

Comments
 (0)