Skip to content

Commit 81b4f06

Browse files
authored
Merge pull request #454 from yuyixg/main
Add support for Agent import and export functionality.
2 parents ab333be + fc1e3ad commit 81b4f06

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
@@ -181,6 +181,30 @@
181181
});
182182
}
183183
184+
function exportAgent() {
185+
fetchJsonContent();
186+
fetchInstructions();
187+
fetchTemplates();
188+
fetchTabData();
189+
190+
const exportData = {
191+
...agent,
192+
created_datetime: undefined,
193+
updated_datetime: undefined,
194+
plugin: undefined,
195+
actions: undefined
196+
};
197+
198+
const json = JSON.stringify(exportData, null, 2);
199+
const blob = new Blob([json], { type: 'application/json' });
200+
const url = URL.createObjectURL(blob);
201+
const a = document.createElement('a');
202+
a.href = url;
203+
a.download = `agent-${agent.name || 'export'}.json`;
204+
a.click();
205+
URL.revokeObjectURL(url);
206+
}
207+
184208
function refresh() {
185209
agentInstructionCmp?.refresh();
186210
}
@@ -238,6 +262,9 @@
238262
<div class="row">
239263
<div class="hstack gap-2 my-4">
240264
<button type="button" class="btn btn-soft-primary" onclick={() => updateCurrentAgent()}>{$_('Save Agent')}</button>
265+
<button type="button" class="btn btn-outline-info" onclick={() => exportAgent()}>
266+
<i class="mdi mdi-download"></i> {$_('Export Agent')}
267+
</button>
241268
<button type="button" class="btn btn-danger" onclick={() => deleteCurrentAgent()}>{$_('Delete Agent')}</button>
242269
</div>
243270
</div>

0 commit comments

Comments
 (0)