From edd5e8aba9361827fbfb8c88fa6e8c8ef48b8ca7 Mon Sep 17 00:00:00 2001 From: "li.zhou" Date: Fri, 6 Nov 2026 15:30:41 +0800 Subject: [PATCH 1/5] feat(setting): choose which agents to migrate to MongoDB Replace the migrate-all button with a searchable multi-select of the agents in the file repository (GET /refresh-agents/options). Start Migration is disabled until at least one agent is selected, and only the selected agent ids are sent to /refresh-agents. Co-Authored-By: Claude Sonnet 5.5 --- src/lib/langs/en.json | 6 +++ src/lib/langs/zh.json | 6 +++ src/lib/services/agent-service.js | 22 ++++++-- src/lib/services/api-endpoints.js | 1 + src/routes/page/setting/+page.svelte | 80 ++++++++++++++++++++-------- 5 files changed, 88 insertions(+), 27 deletions(-) diff --git a/src/lib/langs/en.json b/src/lib/langs/en.json index 74865e94..caf88235 100644 --- a/src/lib/langs/en.json +++ b/src/lib/langs/en.json @@ -216,6 +216,12 @@ "Migrate agents from file repository to MongoDB": "Migrate agents from file repository to MongoDB", "Setting": "Setting", "Start Migration": "Start Migration", + "Select agents to migrate": "Select agents to migrate", + "Search agents": "Search agents", + "Select the agents to copy to the MongoDB datastore. Only the selected agents are migrated.": "Select the agents to copy to the MongoDB datastore. Only the selected agents are migrated.", + "You will migrate {n} selected agent(s) to mongoDb.": "You will migrate {n} selected agent(s) to mongoDb.", + "Failed to load agents.": "Failed to load agents.", + "Failed to migrate agents.": "Failed to migrate agents.", "Plugin": "Plugin", "My Profile": "My Profile", "Detail": "Detail", diff --git a/src/lib/langs/zh.json b/src/lib/langs/zh.json index ba533234..0df77aec 100644 --- a/src/lib/langs/zh.json +++ b/src/lib/langs/zh.json @@ -410,6 +410,12 @@ "Migrate agents from file repository to MongoDB": "将代理从文件存储库迁移到MongoDB", "Setting": "设置", "Start Migration": "开始迁移", + "Select agents to migrate": "选择要迁移的代理", + "Search agents": "搜索代理", + "Select the agents to copy to the MongoDB datastore. Only the selected agents are migrated.": "选择要复制到MongoDB的代理,仅迁移所选代理。", + "You will migrate {n} selected agent(s) to mongoDb.": "将把所选的 {n} 个代理迁移到MongoDB。", + "Failed to load agents.": "加载代理列表失败。", + "Failed to migrate agents.": "迁移代理失败。", "Plugin": "插件", "My Profile": "我的资料", "Detail": "细节", diff --git a/src/lib/services/agent-service.js b/src/lib/services/agent-service.js index 12652c65..bd3672ad 100644 --- a/src/lib/services/agent-service.js +++ b/src/lib/services/agent-service.js @@ -76,12 +76,26 @@ export async function deleteAgent(agentId, options = null) { } /** - * Refresh agent data + * Migrate the given agents from the file repository to the database + * @param {string[]} agentIds * @returns {Promise} */ -export async function refreshAgents() { - const url = endpoints.agentRefreshUrl; - const response = await axios.post(url, {}); +export async function refreshAgents(agentIds) { + if (!agentIds.length) { + // An empty list would make the server migrate every agent. + throw new Error('Select at least one agent to migrate.'); + } + + const response = await axios.post(endpoints.agentRefreshUrl, { agent_ids: agentIds }); + return response.data; +} + +/** + * Get agents defined in the file repository (candidates to migrate) + * @returns {Promise} + */ +export async function getFileAgentOptions() { + const response = await axios.get(endpoints.agentRefreshOptionsUrl); return response.data; } diff --git a/src/lib/services/api-endpoints.js b/src/lib/services/api-endpoints.js index 71dac76e..a25a8338 100644 --- a/src/lib/services/api-endpoints.js +++ b/src/lib/services/api-endpoints.js @@ -37,6 +37,7 @@ export const endpoints = { agentOptionsUrl: `${host}/agent/options`, agentDetailUrl: `${host}/agent/{id}`, agentRefreshUrl: `${host}/refresh-agents`, + agentRefreshOptionsUrl: `${host}/refresh-agents/options`, agentCreateUrl: `${host}/agent`, agentUtilityOptionsUrl: `${host}/agent/utility/options`, agentRuleOptionsUrl: `${host}/rule/triggers`, diff --git a/src/routes/page/setting/+page.svelte b/src/routes/page/setting/+page.svelte index 92ad6a50..70998990 100644 --- a/src/routes/page/setting/+page.svelte +++ b/src/routes/page/setting/+page.svelte @@ -6,9 +6,10 @@ import HeadTitle from '$lib/common/shared/HeadTitle.svelte'; import { getSettings, getSettingDetail } from '$lib/services/setting-service'; import { JSONEditor } from 'svelte-jsoneditor'; - import { refreshAgents } from '$lib/services/agent-service'; + import { refreshAgents, getFileAgentOptions } from '$lib/services/agent-service'; import { formatNumber } from '$lib/helpers/utils/common'; import LoadingToComplete from '$lib/common/spinners/LoadingToComplete.svelte'; + import Select from '$lib/common/dropdowns/Select.svelte'; const duration = 3000; let isLoading = $state(false); @@ -25,12 +26,32 @@ let content = $state({ json: {} }); + /** @type {import('$commonTypes').LabelValuePair[]} */ + let agentOptions = $state([]); + /** @type {string[]} */ + let selectedAgentIds = $state([]); + onMount(async () => { + loadAgentOptions(); settings = await getSettings(); selectedTab = settings[0]; handleGetSettingDetail(selectedTab); }); + async function loadAgentOptions() { + try { + const agents = await getFileAgentOptions(); + agentOptions = agents.map(x => ({ label: x.name, value: x.id || '' })); + } catch { + showError($_('Failed to load agents.')); + } + } + + /** @param {{ detail: { selecteds: import('$commonTypes').LabelValuePair[] } }} e */ + function selectAgents(e) { + selectedAgentIds = e.detail.selecteds.map(x => x.value); + } + /** * @param {string} tab */ @@ -42,10 +63,6 @@ }; } - function readyToRefresh() { - confirmOpen = true; - } - function closeConfirm() { confirmOpen = false; } @@ -55,27 +72,31 @@ refreshAgentData(); } - const refreshAgentData = () => { + async function refreshAgentData() { isLoading = true; - refreshAgents().then(res => { + try { + successText = await refreshAgents(selectedAgentIds); isComplete = true; - isLoading = false; - successText = res; setTimeout(() => { isComplete = false; successText = ''; }, duration); - }).catch(() => { + } catch { + showError($_('Failed to migrate agents.')); + } finally { isLoading = false; - isComplete = false; - isError = true; - errorText = 'Failed to migrate agents.'; - setTimeout(() => { - isError = false; - errorText = ''; - }, duration); - }); - }; + } + } + + /** @param {string} text */ + function showError(text) { + isError = true; + errorText = text; + setTimeout(() => { + isError = false; + errorText = ''; + }, duration); + } @@ -93,7 +114,7 @@ isOpen={confirmOpen} icon="warning" title="Are you sure?" - text="You will migrate all agents data to mongoDb." + text={$_('You will migrate {n} selected agent(s) to mongoDb.', { values: { n: selectedAgentIds.length } })} confirmBtnText="Yes" cancelBtnText="No" confirm={onConfirmRefresh} @@ -153,12 +174,25 @@

{$_('Migrate agents from file repository to MongoDB')}

-

One-time migration. All agents will be copied to the MongoDB datastore.

+

{$_('Select the agents to copy to the MongoDB datastore. Only the selected agents are migrated.')}

+
+ -
-
-
- - This action cannot be undone. Make sure you have a backup before proceeding. -
- -
-
- + diff --git a/src/routes/page/setting/agent-migration.svelte b/src/routes/page/setting/agent-migration.svelte new file mode 100644 index 00000000..690cc1ee --- /dev/null +++ b/src/routes/page/setting/agent-migration.svelte @@ -0,0 +1,138 @@ + + + + + (confirmOpen = false)} + toggleModal={() => (confirmOpen = false)} +/> + +
+
+
+ + + +
+

{$_('Migrate agents from file repository to MongoDB')}

+

{$_('Select the agents to copy to the MongoDB datastore. Only the selected agents are migrated.')}

+
+
+
+ +
+
+