From 0ab63425e5cd40eb004201ea5cc5d329a69f3f10 Mon Sep 17 00:00:00 2001 From: Jonathan Payne Date: Tue, 10 Nov 2026 13:31:32 -0500 Subject: [PATCH 1/2] OpenConceptLab/ocl_online#46 | staff turn OCL Semantic Search on or off for a source version - Version menu (staff, not HEAD): Turn On / Turn Off OCL Semantic Search, with a confirm dialog that names the version and its concept count. It updates the version's match algorithms (adds or removes llm, keeps the rest); the API queues the vector sync. - Version menu (staff, semantic versions): Sync Vectors, the reindex dialog posting sync_vectors, which reruns the sync without changing the setting. - New source version form (staff): an OCL Semantic Search checkbox that starts as HEAD's. Co-Authored-By: Claude Opus 5.5 --- src/components/repos/ReindexVersionDialog.jsx | 15 ++-- src/components/repos/SemanticSearchDialog.jsx | 86 +++++++++++++++++++ src/components/repos/SourceVersionsTab.jsx | 48 ++++++++--- src/components/repos/VersionForm.jsx | 19 ++++ .../repos/__tests__/semanticSearch.test.js | 27 ++++++ src/components/repos/semanticSearch.js | 13 +++ src/i18n/locales/en/translations.json | 14 ++- 7 files changed, 206 insertions(+), 16 deletions(-) create mode 100644 src/components/repos/SemanticSearchDialog.jsx create mode 100644 src/components/repos/__tests__/semanticSearch.test.js create mode 100644 src/components/repos/semanticSearch.js diff --git a/src/components/repos/ReindexVersionDialog.jsx b/src/components/repos/ReindexVersionDialog.jsx index 727fb4088..e97c5e0b2 100644 --- a/src/components/repos/ReindexVersionDialog.jsx +++ b/src/components/repos/ReindexVersionDialog.jsx @@ -14,8 +14,9 @@ import { formatError } from './versionsTab.styles'; // `targetUrl` is the base resource URL to POST `${contentType}/${indexPath}/` onto — // a source/collection version URL (indexPath 'indexes'), or a collection expansion -// URL (indexPath 'index'). -const ReindexVersionDialog = ({ open, targetUrl, repoId, contentType, indexPath = 'indexes', label, onClose }) => { +// URL (indexPath 'index'). With `syncVectors`, a source version's concepts get only their +// vectors synced, without changing whether OCL Semantic Search is on (ocl_online#46). +const ReindexVersionDialog = ({ open, targetUrl, repoId, contentType, indexPath = 'indexes', label, syncVectors, onClose }) => { const { t } = useTranslation(); const [submitting, setSubmitting] = React.useState(false); const [result, setResult] = React.useState(null); @@ -37,7 +38,7 @@ const ReindexVersionDialog = ({ open, targetUrl, repoId, contentType, indexPath }; const handleSubmit = () => { - GAService.recordActionEvent('Version Index', `reindex_${contentType}`, repoId, { + GAService.recordActionEvent('Version Index', syncVectors ? 'sync_vectors' : `reindex_${contentType}`, repoId, { target: targetUrl, content_type: contentType }); @@ -45,7 +46,7 @@ const ReindexVersionDialog = ({ open, targetUrl, repoId, contentType, indexPath APIService.new() .overrideURL(targetUrl) .appendToUrl(`${contentType}/${indexPath}/`) - .post(null, null, null, null, true) + .post(syncVectors ? { sync_vectors: true } : null, null, null, null, true) .then(response => { const status = response?.status || response?.response?.status; const data = response?.data || response?.response?.data; @@ -65,7 +66,11 @@ const ReindexVersionDialog = ({ open, targetUrl, repoId, contentType, indexPath {!result && ( <> -

{t('repo.reindex_confirm_message', { contentType: contentTypeLabel, repoId })}

+

+ {syncVectors + ? t('repo.version.semantic_search.sync_vectors_message', { repoId }) + : t('repo.reindex_confirm_message', { contentType: contentTypeLabel, repoId })} +

{submitting ? ( diff --git a/src/components/repos/SemanticSearchDialog.jsx b/src/components/repos/SemanticSearchDialog.jsx new file mode 100644 index 000000000..a953b16a7 --- /dev/null +++ b/src/components/repos/SemanticSearchDialog.jsx @@ -0,0 +1,86 @@ +import React from 'react'; +import { useTranslation } from 'react-i18next'; +import Alert from '@mui/material/Alert'; +import Box from '@mui/material/Box'; +import CircularProgress from '@mui/material/CircularProgress'; +import DialogContent from '@mui/material/DialogContent'; +import isNumber from 'lodash/isNumber'; + +import APIService from '../../services/APIService'; +import GAService from '../../services/GAService'; +import Button from '../common/Button'; +import Dialog from '../common/Dialog'; +import DialogTitle from '../common/DialogTitle'; +import { isSemanticVersion, withSemanticSearch } from './semanticSearch'; +import { formatError, getVersionURL } from './versionsTab.styles'; + +// Turns OCL Semantic Search on or off for a source version (staff only, in the API too). The API queues the vector +// sync when the version's match algorithms change. onClose(true) once they have. +const SemanticSearchDialog = ({ version, repoId, onClose }) => { + const { t } = useTranslation(); + const [submitting, setSubmitting] = React.useState(false); + const [result, setResult] = React.useState(null); + const turningOn = !isSemanticVersion(version); + const label = t(turningOn ? 'repo.version.semantic_search.turn_on' : 'repo.version.semantic_search.turn_off'); + const conceptCount = version?.summary?.active_concepts; + + const handleClose = () => { + if(submitting) return; + onClose(Boolean(result?.ok)); + }; + + const handleSubmit = () => { + const url = getVersionURL(version); + GAService.recordActionEvent('Version Semantic Search', turningOn ? 'turn_on' : 'turn_off', repoId, { version: url }); + setSubmitting(true); + APIService.new() + .overrideURL(url) + .put({ match_algorithms: withSemanticSearch(version?.match_algorithms, turningOn) }, null, null, null, true) + .then(response => { + const status = response?.status || response?.response?.status; + const data = response?.data || response?.response?.data; + setSubmitting(false); + if(status === 200) + setResult({ ok: true }); + else + setResult({ ok: false, error: formatError(data, t('common.generic_error')) }); + }); + }; + + return ( + + {t('repo.reindex_dialog_title', { label, repoId })} + + {!result && ( + <> +

+ {t(turningOn ? 'repo.version.semantic_search.turn_on_message' : 'repo.version.semantic_search.turn_off_message', { repoId })} +

+ {isNumber(conceptCount) && ( +

{t('repo.version.semantic_search.concept_count', { count: conceptCount.toLocaleString() })}

+ )} + {submitting ? ( + + + + ) : ( +
+ ); +}; + +export default SemanticSearchDialog; diff --git a/src/components/repos/SourceVersionsTab.jsx b/src/components/repos/SourceVersionsTab.jsx index 277845acf..d22dba1e3 100644 --- a/src/components/repos/SourceVersionsTab.jsx +++ b/src/components/repos/SourceVersionsTab.jsx @@ -41,8 +41,10 @@ import { Newspaper as ChangelogIcon, OpenInNew as OpenInNewIcon, LayersClear as ClearProcessingIcon, + ManageSearch as SemanticSearchIcon, QueryStats as ReindexIcon, Summarize as SummaryIcon, + Sync as SyncVectorsIcon, Visibility as VisibilityIcon } from '@mui/icons-material'; import get from 'lodash/get'; @@ -69,6 +71,8 @@ import ProcessingFlag from './ProcessingFlag'; import RepoContentSummary, { VERSION_STATS } from './RepoContentSummary'; import ProcessingProgress from './ProcessingProgress'; import ReindexVersionDialog from './ReindexVersionDialog'; +import SemanticSearchDialog from './SemanticSearchDialog'; +import { isSemanticVersion } from './semanticSearch'; import VersionExportDialog from './VersionExportDialog'; import VersionStatusIndicator from './VersionStatusIndicator'; import GAService from '../../services/GAService'; @@ -173,6 +177,7 @@ const SourceVersionsTab = ({ const [attributesVersion, setAttributesVersion] = React.useState(null); const [changelogVersion, setChangelogVersion] = React.useState(null); const [reindexTarget, setReindexTarget] = React.useState(null); + const [semanticSearchVersion, setSemanticSearchVersion] = React.useState(null); const [clearProcessingVersion, setClearProcessingVersion] = React.useState(null); const [reindexWip, setReindexWip] = React.useState({}); const [headVersion, setHeadVersion] = React.useState(isHeadVersion(repo) ? repo : null); @@ -279,13 +284,17 @@ const SourceVersionsTab = ({ const onExternalExportsChange = updatedVersion => { if(onDataChange) onDataChange(updatedVersion); }; - const getReindexKey = (version, contentType) => `${getVersionKey(version)}:${contentType}`; + const getReindexKey = (version, key) => `${getVersionKey(version)}:${key}`; const closeReindexDialog = result => { const target = reindexTarget; setReindexTarget(null); if(!target || !result) return; if(result.taskId || result.status === 409) - setReindexWip(prev => ({ ...prev, [getReindexKey(target.version, target.contentType)]: { taskId: result.taskId } })); + setReindexWip(prev => ({ ...prev, [getReindexKey(target.version, target.key)]: { taskId: result.taskId } })); + }; + const closeSemanticSearchDialog = changed => { + setSemanticSearchVersion(null); + if(changed && onDataChange) onDataChange(); }; const menuVersion = menuState.version ? sortedVersions.find(version => getVersionKey(version) === getVersionKey(menuState.version)) || menuState.version @@ -471,26 +480,37 @@ const SourceVersionsTab = ({ {hasAccess && withClose(onReleaseVersion)} disabled={isHeadVersion(menuState.version) || menuVersionProcessing} reason={processingReason}>{menuState.version?.released ? t('repo.unrelease_version') : t('repo.release_version')}} {hasAccess && withClose(computeSummary)} disabled={menuVersionProcessing} reason={processingReason}>{t('repo.recompute_summary')}} {hasAccess && isStaffUser() && [ - { contentType: 'concepts', label: t('repo.reindex_concepts') }, - { contentType: 'mappings', label: t('repo.reindex_mappings') } - ].map(({ contentType, label }) => { - const wip = menuState.version ? reindexWip[getReindexKey(menuState.version, contentType)] : null; + { key: 'concepts', contentType: 'concepts', label: t('repo.reindex_concepts') }, + { key: 'mappings', contentType: 'mappings', label: t('repo.reindex_mappings') }, + ...(isSemanticVersion(menuVersion) ? [ + { key: 'vectors', contentType: 'concepts', syncVectors: true, label: t('repo.version.semantic_search.sync_vectors') } + ] : []) + ].map(({ key, contentType, syncVectors, label }) => { + const wip = menuState.version ? reindexWip[getReindexKey(menuState.version, key)] : null; const tooltip = wip ? (wip.taskId ? t('repo.reindex_in_progress_tooltip_with_id', { id: wip.taskId }) : t('repo.reindex_in_progress_tooltip')) : (menuVersionProcessing ? processingReason : ''); + const Icon = syncVectors ? SyncVectorsIcon : ReindexIcon; return ( - + withClose(version => setReindexTarget({ version, contentType }))} + onClick={() => withClose(version => setReindexTarget({ version, key, contentType, syncVectors, label }))} > - {label} + {label} ); })} + { + hasAccess && isStaffUser() && !isHeadVersion(menuState.version) && + withClose(() => setSemanticSearchVersion(menuVersion))} disabled={menuVersionProcessing} reason={processingReason}> + + {t(isSemanticVersion(menuVersion) ? 'repo.version.semantic_search.turn_off' : 'repo.version.semantic_search.turn_on')} + + } { isStaffUser() && menuVersionProcessing && [ , @@ -540,10 +560,18 @@ const SourceVersionsTab = ({ targetUrl={getVersionURL(reindexTarget.version)} repoId={`${reindexTarget.version.short_code || reindexTarget.version.id} [${getVersionLabel(reindexTarget.version)}]`} contentType={reindexTarget.contentType} - label={reindexTarget.contentType === 'mappings' ? t('repo.reindex_mappings') : t('repo.reindex_concepts')} + label={reindexTarget.label} + syncVectors={reindexTarget.syncVectors} onClose={closeReindexDialog} /> )} + {Boolean(semanticSearchVersion) && ( + + )}
); }; diff --git a/src/components/repos/VersionForm.jsx b/src/components/repos/VersionForm.jsx index cad30db42..33401de98 100644 --- a/src/components/repos/VersionForm.jsx +++ b/src/components/repos/VersionForm.jsx @@ -16,9 +16,11 @@ import CloseIconButton from '../common/CloseIconButton'; import Dialog from '../common/Dialog' import DialogTitle from '../common/DialogTitle' import { ID_REGEX } from '../../common/constants' +import { isStaffUser } from '../../common/utils' import { shouldAskForExpansion, shouldShowAutoexpand, getExpansionChoices, getDefaultExpansion, getExpansionPayload } from './versionExpansion' +import { isSemanticVersion, withSemanticSearch } from './semanticSearch' class VersionForm extends FormComponent { @@ -39,6 +41,7 @@ class VersionForm extends FormComponent { autoexpand: {...fieldStruct, value: true}, expansion_url: {...fieldStruct}, revision_date: {...fieldStruct}, + semantic: {...fieldStruct, value: isSemanticVersion(props.version)}, }, expansionDialog: false, expansion: null, @@ -66,6 +69,9 @@ class VersionForm extends FormComponent { isCollectionVersion = () => (this.props.resource || this.props.resourceType) === 'collection' + // A new source version's OCL Semantic Search, which staff choose and which starts as HEAD's (`version` is HEAD here). + canChooseSemanticSearch = () => !this.props.edit && !this.isCollectionVersion() && isStaffUser() + getExpansionArgs = () => ({ edit: Boolean(this.props.edit), isCollection: this.isCollectionVersion(), @@ -111,6 +117,10 @@ class VersionForm extends FormComponent { delete payload.expansion_url } + delete payload.semantic + if(this.canChooseSemanticSearch()) + payload.match_algorithms = withSemanticSearch(version.match_algorithms, Boolean(this.state.fields.semantic.value)) + const url = edit ? version.version_url : version.url GAService.recordUpsertEvent(isCollectionVersion ? 'Collection Version' : 'Source Version', edit) let service = APIService.new().overrideURL(url) @@ -233,6 +243,15 @@ class VersionForm extends FormComponent { label={t('common.release')} /> + { + this.canChooseSemanticSearch() && +
+ } + label={t('repo.version.form.semantic_search')} + /> +
+ } { resource === 'collection' && shouldShowAutoexpand(this.getExpansionArgs()) &&
diff --git a/src/components/repos/__tests__/semanticSearch.test.js b/src/components/repos/__tests__/semanticSearch.test.js new file mode 100644 index 000000000..830497c60 --- /dev/null +++ b/src/components/repos/__tests__/semanticSearch.test.js @@ -0,0 +1,27 @@ +import test from 'node:test' +import assert from 'node:assert/strict' + +const { isSemanticVersion, withSemanticSearch } = await import('../semanticSearch.js') + +test('a version is semantic when its match algorithms include llm', () => { + assert.equal(isSemanticVersion({match_algorithms: ['es', 'llm']}), true) + assert.equal(isSemanticVersion({match_algorithms: ['es']}), false) + assert.equal(isSemanticVersion({}), false) + assert.equal(isSemanticVersion(null), false) +}) + +test('turning semantic search on adds llm and keeps the other algorithms', () => { + assert.deepEqual(withSemanticSearch(['es'], true), ['es', 'llm']) + assert.deepEqual(withSemanticSearch(['es', 'llm'], true), ['es', 'llm']) +}) + +test('turning semantic search off removes only llm', () => { + assert.deepEqual(withSemanticSearch(['es', 'llm'], false), ['es']) + assert.deepEqual(withSemanticSearch(['es'], false), ['es']) +}) + +test('with no other algorithm left, es is kept, as the API defaults', () => { + assert.deepEqual(withSemanticSearch(['llm'], false), ['es']) + assert.deepEqual(withSemanticSearch(null, false), ['es']) + assert.deepEqual(withSemanticSearch([], true), ['es', 'llm']) +}) diff --git a/src/components/repos/semanticSearch.js b/src/components/repos/semanticSearch.js new file mode 100644 index 000000000..5d9c16e95 --- /dev/null +++ b/src/components/repos/semanticSearch.js @@ -0,0 +1,13 @@ +// A source version's match algorithms, as the API names them. `llm` is OCL Semantic Search: the version's concepts are +// vectorized. Adding or removing it on a version queues the API's vector sync (OpenConceptLab/ocl_online#46). +const TOKEN_MATCH_ALGORITHM = 'es' +const SEMANTIC_MATCH_ALGORITHM = 'llm' + +export const isSemanticVersion = version => Boolean(version?.match_algorithms?.includes(SEMANTIC_MATCH_ALGORITHM)) + +// The algorithms with semantic search on or off, keeping the rest. With none left, `es`, as the API defaults. +export const withSemanticSearch = (algorithms, on) => { + const rest = (algorithms || []).filter(algorithm => algorithm !== SEMANTIC_MATCH_ALGORITHM) + const base = rest.length ? rest : [TOKEN_MATCH_ALGORITHM] + return on ? [...base, SEMANTIC_MATCH_ALGORITHM] : base +} diff --git a/src/i18n/locales/en/translations.json b/src/i18n/locales/en/translations.json index 31def3043..626083d06 100644 --- a/src/i18n/locales/en/translations.json +++ b/src/i18n/locales/en/translations.json @@ -762,13 +762,25 @@ "resolved_repo_updates_available": "New releases are available for some resolved repo versions in this expansion:", "version": { "vectorized_for_mapper": "Concepts vectorization is enabled for this repository version", + "semantic_search": { + "turn_on": "Turn On OCL Semantic Search", + "turn_off": "Turn Off OCL Semantic Search", + "turn_on_message": "Turn on OCL Semantic Search for {{repoId}}? Its concepts are vectorized in the background, reusing the vectors other versions already have. A large version can take hours, and until it finishes, semantic $match on this version finds only the concepts already vectorized.", + "turn_off_message": "Turn off OCL Semantic Search for {{repoId}}? Semantic $match on this version stops, including in Mapper projects that use it. Vectors that no other semantic version uses are removed, and turning it back on adds them again.", + "concept_count": "Concepts in this version: {{count}}", + "turned_on": "OCL Semantic Search is on for {{repoId}}. Its concepts are being vectorized in the background.", + "turned_off": "OCL Semantic Search is off for {{repoId}}.", + "sync_vectors": "Sync Vectors", + "sync_vectors_message": "Sync vectors for {{repoId}}? This adds the vectors its concepts are missing and removes those that no semantic version uses. It doesn't turn OCL Semantic Search on or off." + }, "form": { "id": { "label": "ID", "helper_text": "Alphanumeric characters, @, hyphens, periods, and underscores are allowed." }, "new_title": "New {{resourceType}} Version: {{version}}", - "edit_title": "Edit {{resourceType}} Version: {{version}}" + "edit_title": "Edit {{resourceType}} Version: {{version}}", + "semantic_search": "OCL Semantic Search (vectorize this version's concepts)" }, "expansion_dialog": { "title": "Expansion for this version", From f935cf7d6d808b2fe3970244397799a294ac0fcc Mon Sep 17 00:00:00 2001 From: Jonathan Payne Date: Tue, 10 Nov 2026 13:38:14 -0500 Subject: [PATCH 2/2] OpenConceptLab/ocl_online#46 | a reindex or vector sync can run again once its task finishes; a semantic-only setting stays so Codex pass 1 on OpenConceptLab/oclweb3#99: - F1.1: a queued reindex or vector sync kept its menu item disabled until the page reloaded. useQueuedTasks remembers each queued task, and opening a version's (or an expansion's) menu asks the API about it and forgets it once it has finished. Both versions tabs use it. - F1.2: withSemanticSearch(['llm'], true) added es. Only an empty list, or turning off the only algorithm, falls back to es now. Co-Authored-By: Claude Opus 5.5 --- .../repos/CollectionVersionsTab.jsx | 10 +++++--- src/components/repos/SourceVersionsTab.jsx | 11 +++++--- .../repos/__tests__/semanticSearch.test.js | 8 ++++++ .../repos/__tests__/taskState.test.js | 12 +++++++++ src/components/repos/semanticSearch.js | 10 +++++--- src/components/repos/taskState.js | 4 +++ src/hooks/useQueuedTasks.js | 25 +++++++++++++++++++ 7 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 src/components/repos/__tests__/taskState.test.js create mode 100644 src/components/repos/taskState.js create mode 100644 src/hooks/useQueuedTasks.js diff --git a/src/components/repos/CollectionVersionsTab.jsx b/src/components/repos/CollectionVersionsTab.jsx index a692a90a3..cfb6e7c6b 100644 --- a/src/components/repos/CollectionVersionsTab.jsx +++ b/src/components/repos/CollectionVersionsTab.jsx @@ -76,6 +76,7 @@ import VersionExportDialog from './VersionExportDialog'; import VersionStatusIndicator from './VersionStatusIndicator'; import GAService from '../../services/GAService'; import { PROCESSING_POLL_INTERVAL_MS, useProcessingVersions } from '../../hooks/useProcessingState'; +import useQueuedTasks from '../../hooks/useQueuedTasks'; import { PROCESSING_QUERY_PARAMS, areSeedStagesComplete, isExportAvailable, isProcessing as isExpansionProcessing, isVersionProcessing } from './processingStages'; import { REPO_VERSIONS_PAGE_SIZE, @@ -186,7 +187,7 @@ const CollectionVersionsTab = ({ const [rebuildExpansion, setRebuildExpansion] = React.useState(null); const [reindexTarget, setReindexTarget] = React.useState(null); const [clearProcessingVersion, setClearProcessingVersion] = React.useState(null); - const [reindexWip, setReindexWip] = React.useState({}); + const { tasks: reindexWip, add: addReindexWip, check: checkReindexWip } = useQueuedTasks(); const [expandedVersionKeys, setExpandedVersionKeys] = React.useState(new Set()); const [rowMenu, setRowMenu] = React.useState({ anchorEl: null, version: null }); const [expansionMenu, setExpansionMenu] = React.useState({ anchorEl: null, version: null, expansion: null }); @@ -559,7 +560,7 @@ const CollectionVersionsTab = ({ setReindexTarget(null); if (!target || !result) return; if (result.taskId || result.status === 409) - setReindexWip(prev => ({ ...prev, [getReindexKey(target.targetUrl, target.contentType)]: { taskId: result.taskId } })); + addReindexWip(getReindexKey(target.targetUrl, target.contentType), result.taskId); }; const toggleVersionExpand = version => { @@ -574,7 +575,10 @@ const CollectionVersionsTab = ({ const openRowMenu = (event, version) => setRowMenu({ anchorEl: event.currentTarget, version }); const closeRowMenu = () => setRowMenu({ anchorEl: null, version: null }); - const openExpansionMenu = (event, version, expansion) => setExpansionMenu({ anchorEl: event.currentTarget, version, expansion }); + const openExpansionMenu = (event, version, expansion) => { + setExpansionMenu({ anchorEl: event.currentTarget, version, expansion }); + checkReindexWip(['concepts', 'mappings'].map(contentType => getReindexKey(expansion.url, contentType))); + }; const closeExpansionMenu = () => setExpansionMenu({ anchorEl: null, version: null, expansion: null }); const buildRowMenuItems = version => { diff --git a/src/components/repos/SourceVersionsTab.jsx b/src/components/repos/SourceVersionsTab.jsx index d22dba1e3..523d9169c 100644 --- a/src/components/repos/SourceVersionsTab.jsx +++ b/src/components/repos/SourceVersionsTab.jsx @@ -77,6 +77,7 @@ import VersionExportDialog from './VersionExportDialog'; import VersionStatusIndicator from './VersionStatusIndicator'; import GAService from '../../services/GAService'; import { useProcessingVersions } from '../../hooks/useProcessingState'; +import useQueuedTasks from '../../hooks/useQueuedTasks'; import { PROCESSING_QUERY_PARAMS, areSeedStagesComplete, isChangelogAvailable, isExportAvailable, isVersionProcessing } from './processingStages'; import { REPO_VERSIONS_PAGE_SIZE, @@ -179,7 +180,7 @@ const SourceVersionsTab = ({ const [reindexTarget, setReindexTarget] = React.useState(null); const [semanticSearchVersion, setSemanticSearchVersion] = React.useState(null); const [clearProcessingVersion, setClearProcessingVersion] = React.useState(null); - const [reindexWip, setReindexWip] = React.useState({}); + const { tasks: reindexWip, add: addReindexWip, check: checkReindexWip } = useQueuedTasks(); const [headVersion, setHeadVersion] = React.useState(isHeadVersion(repo) ? repo : null); const [versions, setVersions] = React.useState([]); const [totalCount, setTotalCount] = React.useState(0); @@ -290,7 +291,11 @@ const SourceVersionsTab = ({ setReindexTarget(null); if(!target || !result) return; if(result.taskId || result.status === 409) - setReindexWip(prev => ({ ...prev, [getReindexKey(target.version, target.key)]: { taskId: result.taskId } })); + addReindexWip(getReindexKey(target.version, target.key), result.taskId); + }; + const openMenu = (event, version) => { + setMenuState({ anchorEl: event.currentTarget, version }); + checkReindexWip(['concepts', 'mappings', 'vectors'].map(key => getReindexKey(version, key))); }; const closeSemanticSearchDialog = changed => { setSemanticSearchVersion(null); @@ -418,7 +423,7 @@ const SourceVersionsTab = ({ - setMenuState({ anchorEl: event.currentTarget, version })}> + openMenu(event, version)}> diff --git a/src/components/repos/__tests__/semanticSearch.test.js b/src/components/repos/__tests__/semanticSearch.test.js index 830497c60..e714c5624 100644 --- a/src/components/repos/__tests__/semanticSearch.test.js +++ b/src/components/repos/__tests__/semanticSearch.test.js @@ -23,5 +23,13 @@ test('turning semantic search off removes only llm', () => { test('with no other algorithm left, es is kept, as the API defaults', () => { assert.deepEqual(withSemanticSearch(['llm'], false), ['es']) assert.deepEqual(withSemanticSearch(null, false), ['es']) +}) + +test('no algorithms means es, as the API stores it', () => { assert.deepEqual(withSemanticSearch([], true), ['es', 'llm']) + assert.deepEqual(withSemanticSearch(null, true), ['es', 'llm']) +}) + +test('a semantic-only setting stays semantic-only when semantic search stays on', () => { + assert.deepEqual(withSemanticSearch(['llm'], true), ['llm']) }) diff --git a/src/components/repos/__tests__/taskState.test.js b/src/components/repos/__tests__/taskState.test.js new file mode 100644 index 000000000..b7e4ddde7 --- /dev/null +++ b/src/components/repos/__tests__/taskState.test.js @@ -0,0 +1,12 @@ +import test from 'node:test' +import assert from 'node:assert/strict' + +const { isTaskFinished } = await import('../taskState.js') + +test('a task that succeeded or failed has finished', () => { + ['SUCCESS', 'FAILURE', 'REVOKED', 'REJECTED', 'IGNORED', 'success'].forEach(state => assert.equal(isTaskFinished(state), true, state)) +}) + +test('a task that is queued or running, or whose state is unknown, has not finished', () => { + ['PENDING', 'RECEIVED', 'STARTED', 'RETRY', '', null, undefined].forEach(state => assert.equal(isTaskFinished(state), false, String(state))) +}) diff --git a/src/components/repos/semanticSearch.js b/src/components/repos/semanticSearch.js index 5d9c16e95..9813a47f1 100644 --- a/src/components/repos/semanticSearch.js +++ b/src/components/repos/semanticSearch.js @@ -5,9 +5,11 @@ const SEMANTIC_MATCH_ALGORITHM = 'llm' export const isSemanticVersion = version => Boolean(version?.match_algorithms?.includes(SEMANTIC_MATCH_ALGORITHM)) -// The algorithms with semantic search on or off, keeping the rest. With none left, `es`, as the API defaults. +// The algorithms with semantic search on or off, keeping the rest. None means `es`, as the API stores it, and so +// does turning semantic search off when it was the only one. export const withSemanticSearch = (algorithms, on) => { - const rest = (algorithms || []).filter(algorithm => algorithm !== SEMANTIC_MATCH_ALGORITHM) - const base = rest.length ? rest : [TOKEN_MATCH_ALGORITHM] - return on ? [...base, SEMANTIC_MATCH_ALGORITHM] : base + const current = algorithms?.length ? algorithms : [TOKEN_MATCH_ALGORITHM] + const rest = current.filter(algorithm => algorithm !== SEMANTIC_MATCH_ALGORITHM) + if(on) return [...rest, SEMANTIC_MATCH_ALGORITHM] + return rest.length ? rest : [TOKEN_MATCH_ALGORITHM] } diff --git a/src/components/repos/taskState.js b/src/components/repos/taskState.js new file mode 100644 index 000000000..08c2e649a --- /dev/null +++ b/src/components/repos/taskState.js @@ -0,0 +1,4 @@ +// Whether a task's state, as the API reports it, has ended, whether it succeeded or not. +const FINISHED_STATES = ['SUCCESS', 'FAILURE', 'REVOKED', 'REJECTED', 'IGNORED'] + +export const isTaskFinished = state => FINISHED_STATES.includes(String(state || '').toUpperCase()) diff --git a/src/hooks/useQueuedTasks.js b/src/hooks/useQueuedTasks.js new file mode 100644 index 000000000..840b1894e --- /dev/null +++ b/src/hooks/useQueuedTasks.js @@ -0,0 +1,25 @@ +import React from 'react'; +import APIService from '../services/APIService'; +import { isTaskFinished } from '../components/repos/taskState'; + +// Tasks a page queued (a reindex, a vector sync) by key, so their action stays disabled while they run. +// check(keys) asks the API about those keys' tasks and forgets each one that has finished, so it can run again. +const useQueuedTasks = () => { + const [tasks, setTasks] = React.useState({}); + const add = (key, taskId) => setTasks(prev => ({ ...prev, [key]: { taskId } })); + const forget = key => setTasks(prev => { + const next = { ...prev }; + delete next[key]; + return next; + }); + const check = keys => keys.forEach(key => { + const taskId = tasks[key]?.taskId; + if(!taskId) return; + APIService.new().overrideURL(`/tasks/${taskId}/`).get(null, null, null, true).then(response => { + if(isTaskFinished(response?.data?.state)) forget(key); + }); + }); + return { tasks, add, check }; +}; + +export default useQueuedTasks;