|
9 | 9 | import LoadingToComplete from '$lib/common/spinners/LoadingToComplete.svelte'; |
10 | 10 | import PlainPagination from '$lib/common/shared/PlainPagination.svelte'; |
11 | 11 | 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'; |
13 | 13 | import { AgentType, GlobalEvent, UserPermission } from '$lib/helpers/enums'; |
14 | 14 | import { myInfo } from '$lib/services/auth-service'; |
15 | 15 | import { ADMIN_ROLES } from '$lib/helpers/constants'; |
|
172 | 172 | goto(`page/agent/${createdAgent.id}`); |
173 | 173 | } |
174 | 174 |
|
| 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 | +
|
175 | 215 | function refresh() { |
176 | 216 | refreshAgents(); |
177 | 217 | refreshPager(agents.count, filter.pager.page); |
|
300 | 340 | <button type="button" class="btn btn-primary" onclick={() => createNewAgent()}> |
301 | 341 | <i class="mdi mdi-content-copy"></i> {$_('New Agent')} |
302 | 342 | </button> |
| 343 | + <button type="button" class="btn btn-outline-info" onclick={() => importAgent()}> |
| 344 | + <i class="mdi mdi-upload"></i> {$_('Import Agent')} |
| 345 | + </button> |
303 | 346 | {/if} |
304 | 347 | </div> |
305 | 348 | <div class="agent-filter"> |
|
0 commit comments