(file: File)
| 466 | * @returns A promise that resolves to the imported agent |
| 467 | */ |
| 468 | export async function importAgentFromFile(file: File): Promise<CompleteAgent> { |
| 469 | return new Promise((resolve, reject) => { |
| 470 | const reader = new FileReader(); |
| 471 | |
| 472 | reader.onload = async (event) => { |
| 473 | try { |
| 474 | const content = event.target?.result as string; |
| 475 | const agentData = yaml.load(content) as AgentExport; |
| 476 | |
| 477 | if (!agentData.id || !agentData.name || !agentData.code) { |
| 478 | throw new Error('Invalid agent file format. Missing required fields.'); |
| 479 | } |
| 480 | |
| 481 | const agent: CompleteAgent = { |
| 482 | id: agentData.id, |
| 483 | name: agentData.name, |
| 484 | description: agentData.description || '', |
| 485 | model_name: agentData.model_name, |
| 486 | system_prompt: agentData.system_prompt, |
| 487 | loop_interval_seconds: agentData.loop_interval_seconds, |
| 488 | only_on_significant_change: agentData.only_on_significant_change |
| 489 | }; |
| 490 | |
| 491 | await saveAgent(agent, agentData.code); |
| 492 | await updateAgentMemory(agent.id, agentData.memory || ''); |
| 493 | |
| 494 | resolve(agent); |
| 495 | } catch (error) { |
| 496 | reject(error); |
| 497 | } |
| 498 | }; |
| 499 | |
| 500 | reader.onerror = () => reject(new Error('Failed to read file')); |
| 501 | reader.readAsText(file); |
| 502 | }); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Import multiple agents from files |
no test coverage detected