(
file: File,
options: ParseAgentFileOptions = {}
)
| 52 | * Returns the parsed ImportAgentData or null if parsing failed |
| 53 | */ |
| 54 | export async function parseAgentImportFile( |
| 55 | file: File, |
| 56 | options: ParseAgentFileOptions = {} |
| 57 | ): Promise<ImportAgentData | null> { |
| 58 | const { onFileNotFound, onParseError, onValidationError } = options; |
| 59 | |
| 60 | if (!file.name.endsWith(".json") && !file.name.endsWith(".zip")) { |
| 61 | onParseError?.("businessLogic.config.error.invalidFileType"); |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | try { |
| 66 | let agentData: ImportAgentData; |
| 67 | |
| 68 | if (file.name.endsWith(".zip")) { |
| 69 | const zip = await JSZip.loadAsync(file); |
| 70 | const agentJsonFile = zip.file("agent.json"); |
| 71 | if (!agentJsonFile) { |
| 72 | onFileNotFound?.("agent.json not found in ZIP"); |
| 73 | return null; |
| 74 | } |
| 75 | const content = await agentJsonFile.async("string"); |
| 76 | try { |
| 77 | agentData = JSON.parse(content); |
| 78 | } catch { |
| 79 | onParseError?.("businessLogic.config.error.invalidFileType"); |
| 80 | return null; |
| 81 | } |
| 82 | |
| 83 | const skills: Array<{ skill_name: string; skill_zip_base64: string }> = []; |
| 84 | const skillsFolder = zip.folder("skills"); |
| 85 | if (skillsFolder) { |
| 86 | const skillFiles = Object.keys(zip.files).filter( |
| 87 | (name) => |
| 88 | name.startsWith("skills/") && name.toLowerCase().endsWith(".zip") |
| 89 | ); |
| 90 | for (const skillFileName of skillFiles) { |
| 91 | const skillZipFile = zip.file(skillFileName); |
| 92 | if (skillZipFile) { |
| 93 | const skillZipContent = await skillZipFile.async("arraybuffer"); |
| 94 | const base64 = arrayBufferToBase64(skillZipContent); |
| 95 | const skillName = extractSkillNameFromPath(skillFileName); |
| 96 | skills.push({ |
| 97 | skill_name: skillName, |
| 98 | skill_zip_base64: base64, |
| 99 | }); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | agentData.skills = skills; |
| 104 | } else { |
| 105 | const fileContent = await file.text(); |
| 106 | try { |
| 107 | agentData = JSON.parse(fileContent); |
| 108 | } catch { |
| 109 | onParseError?.("businessLogic.config.error.invalidFileType"); |
| 110 | return null; |
| 111 | } |
no test coverage detected