-
Notifications
You must be signed in to change notification settings - Fork 148
feat: true incremental vector index with concurrent README fetching #230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0ca16ac
feat: true incremental vector index with concurrent README fetching
AmintaCCCP 49af4e0
fix: address CodeRabbit + Gemini review findings
AmintaCCCP fddc07e
chore: remove unused updateRepository import
AmintaCCCP 996d23a
fix: propagate cleanup failure after rebuild instead of swallowing it
AmintaCCCP 4fe81d5
fix: isolate single-item embedding failures with truncation retry
AmintaCCCP e3b60cb
fix: address CodeRabbit review on embedding fallback
AmintaCCCP 70abc4a
fix: prevent incremental index crash on undefined vectorSearchStatus
AmintaCCCP 134c3ad
test: fix SearchBar test by mocking useDialog and getState
AmintaCCCP b633f85
fix: address CodeRabbit review #4584825897
AmintaCCCP 963fb01
fix: use fresh state in index handlers instead of stale closure
AmintaCCCP 874b7c9
fix: restore handleAbortIndexing declaration lost in previous edit
AmintaCCCP db0c549
fix: make cleanup non-fatal after rebuild; unify badge/progress color…
AmintaCCCP 399f78e
fix: address CodeRabbit review findings
AmintaCCCP 2ab26f0
fix: use incremental predicate for error fallback count
AmintaCCCP 29f039c
fix: address CodeRabbit review findings
AmintaCCCP 38e928a
fix: add ISO 8601 validation for vector_indexed_at in bulk upsert
AmintaCCCP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -58,6 +58,7 @@ export const VectorSearchSettings: React.FC<VectorSearchSettingsProps> = ({ t }) | |||||||||||||||||||||||
| setVectorIndexingState, | ||||||||||||||||||||||||
| repositories, | ||||||||||||||||||||||||
| githubToken, | ||||||||||||||||||||||||
| setRepositories, | ||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||||||||||||||||||||||||
| } = useAppStore(); | ||||||||||||||||||||||||
|
Comment on lines
58
to
62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Destructure
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Local form state for embedding config | ||||||||||||||||||||||||
|
|
@@ -208,9 +209,16 @@ export const VectorSearchSettings: React.FC<VectorSearchSettingsProps> = ({ t }) | |||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }, [formWorkerUrl, formAuthToken, setVectorSearchStatus]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const runIndexAll = useCallback(async (withCleanup: boolean) => { | ||||||||||||||||||||||||
| if (!activeConfig) return; | ||||||||||||||||||||||||
| // 未索引数量(已分析、未失败、未向量索引或内容已更新) | ||||||||||||||||||||||||
| const unindexedCount = repositories.filter((r) => { | ||||||||||||||||||||||||
| if (!r.analyzed_at || r.analysis_failed) return false; | ||||||||||||||||||||||||
| if (!r.vector_indexed_at) return true; | ||||||||||||||||||||||||
| const contentTime = r.last_edited || r.analyzed_at || ''; | ||||||||||||||||||||||||
| return contentTime > r.vector_indexed_at; | ||||||||||||||||||||||||
| }).length; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const createClients = useCallback(() => { | ||||||||||||||||||||||||
| if (!activeConfig) return null; | ||||||||||||||||||||||||
| const embeddingClient = new EmbeddingClient({ | ||||||||||||||||||||||||
| ...activeConfig, | ||||||||||||||||||||||||
| apiType: formApiType, | ||||||||||||||||||||||||
|
|
@@ -225,39 +233,58 @@ export const VectorSearchSettings: React.FC<VectorSearchSettingsProps> = ({ t }) | |||||||||||||||||||||||
| authToken: formAuthToken, | ||||||||||||||||||||||||
| embeddingConfigId: activeEmbeddingConfig || '', | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| const readmeFetcher = githubToken | ||||||||||||||||||||||||
| ? (owner: string, repo: string, signal?: AbortSignal) => { | ||||||||||||||||||||||||
| const api = new GitHubApiService(githubToken); | ||||||||||||||||||||||||
| return api.getRepositoryReadme(owner, repo, signal); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| : undefined; | ||||||||||||||||||||||||
| return { embeddingClient, vectorService, readmeFetcher }; | ||||||||||||||||||||||||
| }, [activeConfig, formApiType, formBaseUrl, formApiKey, formModel, formDimensions, formWorkerUrl, formAuthToken, activeEmbeddingConfig, githubToken]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const handleRebuildIndex = useCallback(async () => { | ||||||||||||||||||||||||
| const clients = createClients(); | ||||||||||||||||||||||||
| if (!clients) return; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const controller = new AbortController(); | ||||||||||||||||||||||||
| setAbortController(controller); | ||||||||||||||||||||||||
| setVectorIndexingState({ isIndexing: true, phase: null, phaseDone: 0, phaseTotal: 0, result: null }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| if (withCleanup) { | ||||||||||||||||||||||||
| const keepIds = repositories.map(r => String(r.id)); | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| await vectorService.cleanup(keepIds, controller.signal); | ||||||||||||||||||||||||
| } catch (cleanupErr) { | ||||||||||||||||||||||||
| // Cleanup 失败不阻塞重建,记录警告继续 | ||||||||||||||||||||||||
| console.warn('Vector cleanup failed, continuing with rebuild:', cleanupErr); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const readmeFetcher = githubToken | ||||||||||||||||||||||||
| ? (owner: string, repo: string, signal?: AbortSignal) => { | ||||||||||||||||||||||||
| const api = new GitHubApiService(githubToken); | ||||||||||||||||||||||||
| return api.getRepositoryReadme(owner, repo, signal); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| : undefined; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const result = await indexAllRepos(repositories, embeddingClient, vectorService, { | ||||||||||||||||||||||||
| // 1. 清除所有 vector_indexed_at(包括之前失败/不可索引的 repo 的残留值) | ||||||||||||||||||||||||
| setRepositories(repositories.map(repo => | ||||||||||||||||||||||||
| repo.vector_indexed_at ? { ...repo, vector_indexed_at: undefined } : repo | ||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // 2. 全量索引 | ||||||||||||||||||||||||
| const now = new Date().toISOString(); | ||||||||||||||||||||||||
| const result = await indexAllRepos(repositories, clients.embeddingClient, clients.vectorService, { | ||||||||||||||||||||||||
| onProgress: (progress) => setVectorIndexingState({ | ||||||||||||||||||||||||
| phase: progress.phase, | ||||||||||||||||||||||||
| phaseDone: progress.done, | ||||||||||||||||||||||||
| phaseTotal: progress.total, | ||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||
| signal: controller.signal, | ||||||||||||||||||||||||
| readmeFetcher, | ||||||||||||||||||||||||
| readmeFetcher: clients.readmeFetcher, | ||||||||||||||||||||||||
| indexMode: formIndexMode, | ||||||||||||||||||||||||
| readmeMaxChars: formReadmeMaxChars, | ||||||||||||||||||||||||
| incremental: false, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // 3. cleanup:全量重建后只保留本次成功重建的向量 | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| await clients.vectorService.cleanup(result.indexedRepoIds.map(String), controller.signal); | ||||||||||||||||||||||||
| } catch (cleanupErr) { | ||||||||||||||||||||||||
| console.warn('Vector cleanup failed after rebuild:', cleanupErr); | ||||||||||||||||||||||||
| throw cleanupErr; | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
coderabbitai[bot] marked this conversation as resolved.
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // 4. 为成功索引的 repo 设置 vector_indexed_at(批量更新) | ||||||||||||||||||||||||
| const indexedSet = new Set(result.indexedRepoIds); | ||||||||||||||||||||||||
| setRepositories(useAppStore.getState().repositories.map(repo => | ||||||||||||||||||||||||
| indexedSet.has(repo.id) ? { ...repo, vector_indexed_at: now } : repo | ||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| setVectorIndexingState({ result, isIndexing: false, phase: null }); | ||||||||||||||||||||||||
| setVectorSearchStatus({ | ||||||||||||||||||||||||
| connected: true, | ||||||||||||||||||||||||
|
|
@@ -274,10 +301,67 @@ export const VectorSearchSettings: React.FC<VectorSearchSettingsProps> = ({ t }) | |||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||
| setAbortController(null); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }, [activeConfig, formApiType, formBaseUrl, formApiKey, formModel, formDimensions, formWorkerUrl, formAuthToken, formIndexMode, formReadmeMaxChars, activeEmbeddingConfig, repositories, githubToken, setVectorSearchStatus, setVectorIndexingState]); | ||||||||||||||||||||||||
| }, [createClients, repositories, formIndexMode, formReadmeMaxChars, formDimensions, setRepositories, setVectorSearchStatus, setVectorIndexingState]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const handleIncrementalIndex = useCallback(async () => { | ||||||||||||||||||||||||
| const clients = createClients(); | ||||||||||||||||||||||||
| if (!clients) return; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const handleRebuildIndex = useCallback(() => runIndexAll(true), [runIndexAll]); | ||||||||||||||||||||||||
| const handleIncrementalIndex = useCallback(() => runIndexAll(false), [runIndexAll]); | ||||||||||||||||||||||||
| const controller = new AbortController(); | ||||||||||||||||||||||||
| setAbortController(controller); | ||||||||||||||||||||||||
| setVectorIndexingState({ isIndexing: true, phase: null, phaseDone: 0, phaseTotal: 0, result: null }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| // 记录索引前无 vector_indexed_at 的 repo,用于精确计算新增数量 | ||||||||||||||||||||||||
| const newlyIndexedRepoIds = new Set( | ||||||||||||||||||||||||
| repositories.filter(r => !r.vector_indexed_at).map(r => r.id) | ||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const now = new Date().toISOString(); | ||||||||||||||||||||||||
| const result = await indexAllRepos(repositories, clients.embeddingClient, clients.vectorService, { | ||||||||||||||||||||||||
| onProgress: (progress) => setVectorIndexingState({ | ||||||||||||||||||||||||
| phase: progress.phase, | ||||||||||||||||||||||||
| phaseDone: progress.done, | ||||||||||||||||||||||||
| phaseTotal: progress.total, | ||||||||||||||||||||||||
| }), | ||||||||||||||||||||||||
| signal: controller.signal, | ||||||||||||||||||||||||
| readmeFetcher: clients.readmeFetcher, | ||||||||||||||||||||||||
| indexMode: formIndexMode, | ||||||||||||||||||||||||
| readmeMaxChars: formReadmeMaxChars, | ||||||||||||||||||||||||
| incremental: true, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // 批量设置 vector_indexed_at(一次性更新,避免逐个 updateRepository) | ||||||||||||||||||||||||
| const indexedSet = new Set(result.indexedRepoIds); | ||||||||||||||||||||||||
| setRepositories(useAppStore.getState().repositories.map(repo => | ||||||||||||||||||||||||
| indexedSet.has(repo.id) ? { ...repo, vector_indexed_at: now } : repo | ||||||||||||||||||||||||
| )); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| setVectorIndexingState({ result, isIndexing: false, phase: null }); | ||||||||||||||||||||||||
| // 只计算本次新增索引的 repo(之前无 vector_indexed_at),不包含重新索引的 | ||||||||||||||||||||||||
| const newlyIndexedCount = result.indexedRepoIds.filter(id => newlyIndexedRepoIds.has(id)).length; | ||||||||||||||||||||||||
| const prevCount = useAppStore.getState().vectorSearchStatus.vectorCount || 0; | ||||||||||||||||||||||||
| setVectorSearchStatus({ | ||||||||||||||||||||||||
| connected: true, | ||||||||||||||||||||||||
| vectorCount: prevCount + newlyIndexedCount, | ||||||||||||||||||||||||
| dimensions: formDimensions, | ||||||||||||||||||||||||
| lastSyncAt: new Date().toISOString(), | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| } catch (err) { | ||||||||||||||||||||||||
| if (err instanceof Error && err.message === 'Aborted') { | ||||||||||||||||||||||||
| setVectorIndexingState({ isIndexing: false, phase: null, result: null }); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| const msg = err instanceof Error ? err.message : String(err); | ||||||||||||||||||||||||
| setVectorIndexingState({ | ||||||||||||||||||||||||
| isIndexing: false, | ||||||||||||||||||||||||
| phase: null, | ||||||||||||||||||||||||
| result: { indexed: 0, skipped: 0, errors: repositories.length, error: msg }, | ||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||
| setAbortController(null); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| }, [createClients, repositories, formIndexMode, formReadmeMaxChars, formDimensions, setRepositories, setVectorSearchStatus, setVectorIndexingState]); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| const handleAbortIndexing = useCallback(() => { | ||||||||||||||||||||||||
| abortController?.abort(); | ||||||||||||||||||||||||
|
|
@@ -735,11 +819,16 @@ export const VectorSearchSettings: React.FC<VectorSearchSettingsProps> = ({ t }) | |||||||||||||||||||||||
| </button> | ||||||||||||||||||||||||
| <button | ||||||||||||||||||||||||
| onClick={handleIncrementalIndex} | ||||||||||||||||||||||||
| disabled={isIndexing || !isConfigComplete} | ||||||||||||||||||||||||
| disabled={isIndexing || !isConfigComplete || unindexedCount === 0} | ||||||||||||||||||||||||
| className="flex items-center gap-2 px-4 py-2 text-sm bg-gray-200 dark:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-md hover:bg-gray-300 dark:hover:bg-gray-600 disabled:opacity-50 disabled:cursor-not-allowed" | ||||||||||||||||||||||||
| > | ||||||||||||||||||||||||
| {isIndexing ? <Loader2 className="w-4 h-4 animate-spin" /> : <RefreshCw className="w-4 h-4" />} | ||||||||||||||||||||||||
| {t('增量索引', 'Incremental Index')} | ||||||||||||||||||||||||
| {unindexedCount > 0 && ( | ||||||||||||||||||||||||
| <span className="ml-1 px-1.5 py-0.5 text-xs bg-purple-500 text-white rounded-full"> | ||||||||||||||||||||||||
| {unindexedCount} | ||||||||||||||||||||||||
| </span> | ||||||||||||||||||||||||
| )} | ||||||||||||||||||||||||
| </button> | ||||||||||||||||||||||||
| {isIndexing && ( | ||||||||||||||||||||||||
| <button | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.