From d025ee514de3968996d17a8d9dd1ec633f8acd30 Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Tue, 3 Nov 2026 11:28:20 +0800 Subject: [PATCH] feat(agent): keep the agent id on import and report an existing agent Send the exported id when importing so the agent keeps it, and show that the agent already exists when the backend answers 409. Co-Authored-By: Claude Opus 5.5 --- src/routes/page/agent/+page.svelte | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/routes/page/agent/+page.svelte b/src/routes/page/agent/+page.svelte index ec22fef1..a3af0542 100644 --- a/src/routes/page/agent/+page.svelte +++ b/src/routes/page/agent/+page.svelte @@ -144,7 +144,7 @@ }); } - /** @typedef {{ kind: 'create' } | { kind: 'import-error' }} PendingAction */ + /** @typedef {{ kind: 'create' } | { kind: 'import-error', message: string }} PendingAction */ let confirmOpen = $state(false); /** @type {PendingAction | null} */ @@ -192,11 +192,14 @@ const file = e.target.files?.[0]; if (!file) return; + /** @type {any} */ + let data = null; try { const text = await file.text(); - const data = JSON.parse(text); + data = JSON.parse(text); const newAgent = { + id: data.id, name: data.name || 'Imported Agent', description: data.description || '', instruction: data.instruction || '', @@ -218,7 +221,11 @@ goto(`page/agent/${createdAgent.id}`); } catch (err) { - pendingAction = { kind: 'import-error' }; + // 409: the backend already has an agent with the exported id + const message = /** @type {any} */ (err)?.response?.status === 409 + ? `Agent "${data?.name}" (${data?.id}) already exists.` + : 'Failed to import agent. Please check the JSON file.'; + pendingAction = { kind: 'import-error', message }; confirmOpen = true; } }; @@ -352,7 +359,7 @@ icon={pendingAction?.kind === 'import-error' ? 'error' : 'warning'} title={pendingAction?.kind === 'import-error' ? 'Error' : 'Are you sure?'} text={pendingAction?.kind === 'import-error' - ? 'Failed to import agent. Please check the JSON file.' + ? pendingAction.message : 'Are you sure you want to create a new agent?'} confirmBtnText={pendingAction?.kind === 'import-error' ? 'OK' : 'Yes'} cancelBtnText="No"