Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion src/routes/page/agent/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import LoadingToComplete from '$lib/common/spinners/LoadingToComplete.svelte';
import PlainPagination from '$lib/common/shared/PlainPagination.svelte';
import Select from '$lib/common/dropdowns/Select.svelte';
import { createAgent, getAgentLabels, getAgents } from '$lib/services/agent-service.js';
import { createAgent, getAgentLabels, getAgents, saveAgent } from '$lib/services/agent-service.js';
import { AgentType, GlobalEvent, UserPermission } from '$lib/helpers/enums';
import { myInfo } from '$lib/services/auth-service';
import { ADMIN_ROLES } from '$lib/helpers/constants';
Expand Down Expand Up @@ -172,6 +172,46 @@
goto(`page/agent/${createdAgent.id}`);
}

function importAgent() {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';
input.onchange = async (e) => {
const file = e.target.files?.[0];
if (!file) return;

try {
const text = await file.text();
const data = JSON.parse(text);

const newAgent = {
name: data.name || 'Imported Agent',
description: data.description || '',
instruction: data.instruction || '',
isPublic: data.is_public ?? true
};

// @ts-ignore
const createdAgent = await createAgent(newAgent);

// Merge remaining fields and save
const fullAgent = {
...data,
id: createdAgent.id,
created_datetime: createdAgent.created_datetime,
updated_datetime: createdAgent.updated_datetime,
plugin: createdAgent.plugin
};
await saveAgent(fullAgent);

goto(`page/agent/${createdAgent.id}`);
} catch (err) {
Swal.fire('Error', 'Failed to import agent. Please check the JSON file.', 'error');
}
};
input.click();
}

function refresh() {
refreshAgents();
refreshPager(agents.count, filter.pager.page);
Expand Down Expand Up @@ -300,6 +340,9 @@
<button type="button" class="btn btn-primary" onclick={() => createNewAgent()}>
<i class="mdi mdi-content-copy"></i> {$_('New Agent')}
</button>
<button type="button" class="btn btn-outline-info" onclick={() => importAgent()}>
<i class="mdi mdi-upload"></i> {$_('Import Agent')}
</button>
{/if}
</div>
<div class="agent-filter">
Expand Down
27 changes: 27 additions & 0 deletions src/routes/page/agent/[agentId]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,30 @@
});
}

function exportAgent() {
fetchJsonContent();
fetchInstructions();
fetchTemplates();
fetchTabData();

const exportData = {
...agent,
created_datetime: undefined,
updated_datetime: undefined,
plugin: undefined,
actions: undefined
};

const json = JSON.stringify(exportData, null, 2);
const blob = new Blob([json], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `agent-${agent.name || 'export'}.json`;
a.click();
URL.revokeObjectURL(url);
}

function refresh() {
agentInstructionCmp?.refresh();
}
Expand Down Expand Up @@ -244,6 +268,9 @@
<div class="row">
<div class="hstack gap-2 my-4">
<button type="button" class="btn btn-soft-primary" onclick={() => updateCurrentAgent()}>{$_('Save Agent')}</button>
<button type="button" class="btn btn-outline-info" onclick={() => exportAgent()}>
<i class="mdi mdi-download"></i> {$_('Export Agent')}
</button>
<button type="button" class="btn btn-danger" onclick={() => deleteCurrentAgent()}>{$_('Delete Agent')}</button>
</div>
</div>
Expand Down