Skip to content

Commit 39da37e

Browse files
committed
export and import agent config
1 parent e7c8ade commit 39da37e

2 files changed

Lines changed: 71 additions & 1 deletion

File tree

src/routes/page/agent/+page.svelte

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import LoadingToComplete from '$lib/common/spinners/LoadingToComplete.svelte';
1010
import PlainPagination from '$lib/common/shared/PlainPagination.svelte';
1111
import Select from '$lib/common/dropdowns/Select.svelte';
12-
import { createAgent, getAgentLabels, getAgents } from '$lib/services/agent-service.js';
12+
import { createAgent, getAgentLabels, getAgents, saveAgent } from '$lib/services/agent-service.js';
1313
import { AgentType, GlobalEvent, UserPermission } from '$lib/helpers/enums';
1414
import { myInfo } from '$lib/services/auth-service';
1515
import { ADMIN_ROLES } from '$lib/helpers/constants';
@@ -172,6 +172,46 @@
172172
goto(`page/agent/${createdAgent.id}`);
173173
}
174174
175+
function importAgent() {
176+
const input = document.createElement('input');
177+
input.type = 'file';
178+
input.accept = '.json';
179+
input.onchange = async (e) => {
180+
const file = e.target.files?.[0];
181+
if (!file) return;
182+
183+
try {
184+
const text = await file.text();
185+
const data = JSON.parse(text);
186+
187+
const newAgent = {
188+
name: data.name || 'Imported Agent',
189+
description: data.description || '',
190+
instruction: data.instruction || '',
191+
isPublic: data.is_public ?? true
192+
};
193+
194+
// @ts-ignore
195+
const createdAgent = await createAgent(newAgent);
196+
197+
// Merge remaining fields and save
198+
const fullAgent = {
199+
...data,
200+
id: createdAgent.id,
201+
created_datetime: createdAgent.created_datetime,
202+
updated_datetime: createdAgent.updated_datetime,
203+
plugin: createdAgent.plugin
204+
};
205+
await saveAgent(fullAgent);
206+
207+
goto(`page/agent/${createdAgent.id}`);
208+
} catch (err) {
209+
Swal.fire('Error', 'Failed to import agent. Please check the JSON file.', 'error');
210+
}
211+
};
212+
input.click();
213+
}
214+
175215
function refresh() {
176216
refreshAgents();
177217
refreshPager(agents.count, filter.pager.page);
@@ -300,6 +340,9 @@
300340
<button type="button" class="btn btn-primary" onclick={() => createNewAgent()}>
301341
<i class="mdi mdi-content-copy"></i> {$_('New Agent')}
302342
</button>
343+
<button type="button" class="btn btn-outline-info" onclick={() => importAgent()}>
344+
<i class="mdi mdi-upload"></i> {$_('Import Agent')}
345+
</button>
303346
{/if}
304347
</div>
305348
<div class="agent-filter">

src/routes/page/agent/[agentId]/+page.svelte

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,30 @@
186186
});
187187
}
188188
189+
function exportAgent() {
190+
fetchJsonContent();
191+
fetchInstructions();
192+
fetchTemplates();
193+
fetchTabData();
194+
195+
const exportData = {
196+
...agent,
197+
created_datetime: undefined,
198+
updated_datetime: undefined,
199+
plugin: undefined,
200+
actions: undefined
201+
};
202+
203+
const json = JSON.stringify(exportData, null, 2);
204+
const blob = new Blob([json], { type: 'application/json' });
205+
const url = URL.createObjectURL(blob);
206+
const a = document.createElement('a');
207+
a.href = url;
208+
a.download = `agent-${agent.name || 'export'}.json`;
209+
a.click();
210+
URL.revokeObjectURL(url);
211+
}
212+
189213
function refresh() {
190214
agentInstructionCmp?.refresh();
191215
}
@@ -244,6 +268,9 @@
244268
<div class="row">
245269
<div class="hstack gap-2 my-4">
246270
<button type="button" class="btn btn-soft-primary" onclick={() => updateCurrentAgent()}>{$_('Save Agent')}</button>
271+
<button type="button" class="btn btn-outline-info" onclick={() => exportAgent()}>
272+
<i class="mdi mdi-download"></i> {$_('Export Agent')}
273+
</button>
247274
<button type="button" class="btn btn-danger" onclick={() => deleteCurrentAgent()}>{$_('Delete Agent')}</button>
248275
</div>
249276
</div>

0 commit comments

Comments
 (0)