Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

feat(setting): auto-merge newly listed models into draft instance (#20177)

chanx64b7824d7440
diff --git a/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.ts b/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.ts
index df154fc6ff..db98c5b366 100644
--- a/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.ts
+++ b/web/src/pages/user-setting/setting-model/instance-card/models-section/hooks.ts
@@ -135,6 +135,14 @@ interface UseModelsCatalogArgs {
baseUrlValue: string | undefined;
instanceDetailsLoaded?: boolean;
+
+ /**
+ * Invoked with the freshly fetched items after every successful catalog
+ * fetch (mount auto-fetch and manual "List models" clicks). Used by
+ * draft cards to auto-merge new models while skipping names the user
+ * has already removed.
+ */
+ onCatalogFetched?: (items: IProviderModelItem[]) => void;
}
export function useModelsCatalog({
@@ -146,6 +154,7 @@ export function useModelsCatalog({
apiKeyValue,
baseUrlValue,
instanceDetailsLoaded,
+ onCatalogFetched,
}: UseModelsCatalogArgs) {
const { listProviderModels } = useListProviderModels();
const [catalog, setCatalog] = useState<IProviderModelItem[]>([]);
@@ -155,6 +164,8 @@ export function useModelsCatalog({
const catalogOverridesRef = useRef(catalogOverrides);
const [manualListLoading, setManualListLoading] = useState(false);
const [hasFetched, setHasFetched] = useState(false);
+ const onCatalogFetchedRef = useRef(onCatalogFetched);
+ onCatalogFetchedRef.current = onCatalogFetched;
const applyCatalogOverrides = useCallback((items: IProviderModelItem[]) => {
const overrides = catalogOverridesRef.current;
@@ -224,9 +235,11 @@ export function useModelsCatalog({
base_url: baseUrl,
});
if (ret?.code === 0) {
- setCatalog(
- applyCatalogOverrides((ret.data as IProviderModelItem[]) ?? []),
+ const merged = applyCatalogOverrides(
+ (ret.data as IProviderModelItem[]) ?? [],
);
+ setCatalog(merged);
+ onCatalogFetchedRef.current?.(merged);
}
setHasFetched(true);
} catch {
diff --git a/web/src/pages/user-setting/setting-model/instance-card/models-section/index.tsx b/web/src/pages/user-setting/setting-model/instance-card/models-section/index.tsx
index 2f150ba7e3..315627f0b7 100644
--- a/web/src/pages/user-setting/setting-model/instance-card/models-section/index.tsx
+++ b/web/src/pages/user-setting/setting-model/instance-card/models-section/index.tsx
@@ -99,6 +99,39 @@ export function ModelsSection(props: ModelsSectionProps) {
onInstanceModelsStatusChange,
]);
+ // 3a. Draft-only: locally-tracked "added models" list.
+ // The backend has no per-instance models yet, so per-model add /
+ // remove / batch-toggle on a draft mutates this array instead of
+ // firing a mutation. The host save handler then flushes the latest
+ // snapshot through `model_info` on save. Reset when the provider
+ // or instance changes (rare in practice since the host remounts
+ // the section on draft switch, but kept as a safety net).
+ const [draftModels, setDraftModels] = useState<IProviderModelItem[]>([]);
+ // Names of catalog models the user has manually removed for this draft.
+ // Every catalog fetch (mount auto-fetch and "List models" clicks)
+ // auto-adds incoming models EXCEPT the names in this set, so removed
+ // models stay removed while newly listed models are added by default.
+ const removedDraftModelsRef = useRef<Set<string>>(new Set());
+ useEffect(() => {
+ setDraftModels([]);
+ removedDraftModelsRef.current = new Set();
+ }, [providerName, instanceName]);
+
+ // Merge a freshly fetched catalog batch into the draft list, skipping
+ // already-added models and ones the user has previously removed.
+ const mergeCatalogIntoDraft = useCallback((items: IProviderModelItem[]) => {
+ const removed = removedDraftModelsRef.current;
+ setDraftModels((prev) => {
+ const existing = new Set(prev.map((m) => modelNameKey(m.name)));
+ const incoming = items.filter(
+ (m) =>
+ !existing.has(modelNameKey(m.name)) &&
+ !removed.has(modelNameKey(m.name)),
+ );
+ return incoming.length === 0 ? prev : [...prev, ...incoming];
+ });
+ }, []);
+
// 3. Upstream catalog + auto-fetch on mount.
const {
catalog,
@@ -117,48 +150,11 @@ export function ModelsSection(props: ModelsSectionProps) {
apiKeyValue: currentCreds.apiKey,
baseUrlValue: currentCreds.baseUrl,
instanceDetailsLoaded,
+ onCatalogFetched: isDraftInstance ? mergeCatalogIntoDraft : undefined,
});
- // 3a. Draft-only: locally-tracked "added models" list.
- // The backend has no per-instance models yet, so per-model add /
- // remove / batch-toggle on a draft mutates this array instead of
- // firing a mutation. The host save handler then flushes the latest
- // snapshot through `model_info` on save. Reset when the provider
- // or instance changes (rare in practice since the host remounts
- // the section on draft switch, but kept as a safety net).
- const [draftModels, setDraftModels] = useState<IProviderModelItem[]>([]);
- // Tracks whether we've auto-populated the draft from the catalog for
- // the current draft session. Prevents re-adding models the user has
- // manually removed when the catalog refetches.
- const hasAutoPopulatedDraftRef = useRef(false);
- useEffect(() => {
- setDraftModels([]);
- hasAutoPopulatedDraftRef.current = false;
- }, [providerName, instanceName]);
-
- // Auto-populate the draft's model list from the catalog on first
- // fetch so the user doesn't have to click `+` on every row when
- // creating a new instance. The user can still remove any auto-added
- // model via the per-row `-` button - the flag above ensures we don't
- // re-add removed models if the catalog is refetched (e.g. via the
- // "List models" button). Pre-existing manual additions (e.g. a
- // custom model added before the catalog resolved) are preserved by
- // the merge-by-name setter below.
- useEffect(() => {
- if (!isDraftInstance) return;
- if (hasAutoPopulatedDraftRef.current) return;
- if (catalog.length === 0) return;
- hasAutoPopulatedDraftRef.current = true;
- setDraftModels((prev) => {
- const existing = new Set(prev.map((m) => modelNameKey(m.name)));
- return [
- ...prev,
- ...catalog.filter((m) => !existing.has(modelNameKey(m.name))),
- ];
- });
- }, [isDraftInstance, catalog]);
-
const addDraftModel = useCallback((model: IProviderModelItem) => {
+ removedDraftModelsRef.current.delete(modelNameKey(model.name));
setDraftModels((prev) =>
prev.some((m) => modelNameKey(m.name) === modelNameKey(model.name))
? prev
@@ -166,6 +162,7 @@ export function ModelsSection(props: ModelsSectionProps) {
);
}, []);
const removeDraftModel = useCallback((name: string) => {
+ removedDraftModelsRef.current.add(modelNameKey(name));
setDraftModels((prev) =>
prev.filter((m) => modelNameKey(m.name) !== modelNameKey(name)),
);
@@ -179,6 +176,22 @@ export function ModelsSection(props: ModelsSectionProps) {
),
);
}, []);
+ // Batch toggle replaces the whole list. Diff the next list against the
+ // current one so batch-removed names stay excluded from future catalog
+ // merges and batch-added names are eligible again.
+ const applyDraftModelsList = useCallback((next: IProviderModelItem[]) => {
+ const removed = removedDraftModelsRef.current;
+ setDraftModels((prev) => {
+ const nextNames = new Set(next.map((m) => modelNameKey(m.name)));
+ prev.forEach((m) => {
+ if (!nextNames.has(modelNameKey(m.name))) {
+ removed.add(modelNameKey(m.name));
+ }
+ });
+ next.forEach((m) => removed.delete(modelNameKey(m.name)));
+ return next;
+ });
+ }, []);
// 4. Derived union list (instance ∪ catalog) + push to host.
const { instanceItems, models, isModelAdded } = useModelsDerived({
@@ -244,7 +257,7 @@ export function ModelsSection(props: ModelsSectionProps) {
clearCatalogOverride,
addDraftModel,
removeDraftModel,
- setDraftModelsList: setDraftModels,
+ setDraftModelsList: applyDraftModelsList,
});
// 8. Edit dialog state + submit.