|
| 1 | +// ----------------------------------------------------------------------- |
| 2 | +// <copyright file="DownloadSubAgentCommand.cs" company="Petabridge, LLC"> |
| 3 | +// Copyright (C) 2026 - 2026 Petabridge, LLC <https://petabridge.com> |
| 4 | +// </copyright> |
| 5 | +// ----------------------------------------------------------------------- |
| 6 | + |
| 7 | +using Netclaw.SkillClient; |
| 8 | +using Netclaw.SkillServer.Cli.Output; |
| 9 | +using Netclaw.SkillServer.Cli.Publishing; |
| 10 | + |
| 11 | +namespace Netclaw.SkillServer.Cli.Commands; |
| 12 | + |
| 13 | +internal static class DownloadSubAgentCommand |
| 14 | +{ |
| 15 | + public static async Task<int> ExecuteAsync(ParsedArgs args, SkillServerClient client) |
| 16 | + { |
| 17 | + if (args.Help || args.Positional.Count < 3) |
| 18 | + { |
| 19 | + PrintHelp(); |
| 20 | + return args.Help ? 0 : 1; |
| 21 | + } |
| 22 | + |
| 23 | + var name = args.Positional[0]; |
| 24 | + var version = args.Positional[1]; |
| 25 | + var destinationPath = Path.GetFullPath(args.Positional[2]); |
| 26 | + |
| 27 | + if (!SubAgentFileScanner.IsValidName(name)) |
| 28 | + { |
| 29 | + ConsoleOutput.WriteError("Error: Invalid sub-agent name."); |
| 30 | + return 1; |
| 31 | + } |
| 32 | + |
| 33 | + if (!SubAgentFileScanner.IsValidVersion(version)) |
| 34 | + { |
| 35 | + ConsoleOutput.WriteError("Error: Invalid sub-agent version."); |
| 36 | + return 1; |
| 37 | + } |
| 38 | + |
| 39 | + if (File.Exists(destinationPath) && !args.Force) |
| 40 | + { |
| 41 | + ConsoleOutput.WriteError($"Error: Destination '{destinationPath}' already exists. Use --force to overwrite."); |
| 42 | + return 1; |
| 43 | + } |
| 44 | + |
| 45 | + if (args.DryRun) |
| 46 | + { |
| 47 | + ConsoleOutput.WriteWarning( |
| 48 | + $"Dry run: would download sub-agent {name}@{version} to {destinationPath}"); |
| 49 | + return 0; |
| 50 | + } |
| 51 | + |
| 52 | + try |
| 53 | + { |
| 54 | + var detail = await client.GetNativeSubAgentVersionAsync(name, version); |
| 55 | + if (detail is null) |
| 56 | + { |
| 57 | + ConsoleOutput.WriteError($"Error: Sub-agent {name}@{version} not found."); |
| 58 | + return 1; |
| 59 | + } |
| 60 | + |
| 61 | + if (!detail.Type.Equals(SubAgentArtifactTypes.AgentMd, StringComparison.OrdinalIgnoreCase)) |
| 62 | + { |
| 63 | + ConsoleOutput.WriteError($"Error: Unsupported sub-agent artifact type '{detail.Type}'."); |
| 64 | + return 1; |
| 65 | + } |
| 66 | + |
| 67 | + var parentDirectory = Path.GetDirectoryName(destinationPath); |
| 68 | + if (!string.IsNullOrEmpty(parentDirectory)) |
| 69 | + Directory.CreateDirectory(parentDirectory); |
| 70 | + |
| 71 | + var tempPath = $"{destinationPath}.tmp-{Guid.NewGuid():N}"; |
| 72 | + try |
| 73 | + { |
| 74 | + await using (var destination = File.Create(tempPath)) |
| 75 | + { |
| 76 | + if (args.Verbose) |
| 77 | + ConsoleOutput.WriteDim($" Downloading {detail.Url}"); |
| 78 | + |
| 79 | + var download = await client.DownloadNativeSubAgentArtifactAsync(detail, destination); |
| 80 | + if (args.Verbose) |
| 81 | + ConsoleOutput.WriteDim($" Verified {download.Digest} ({FormatSize(download.SizeBytes)})"); |
| 82 | + } |
| 83 | + |
| 84 | + File.Move(tempPath, destinationPath, overwrite: true); |
| 85 | + } |
| 86 | + finally |
| 87 | + { |
| 88 | + if (File.Exists(tempPath)) |
| 89 | + File.Delete(tempPath); |
| 90 | + } |
| 91 | + |
| 92 | + ConsoleOutput.WriteSuccess($"Downloaded sub-agent {name}@{version} to {destinationPath}"); |
| 93 | + return 0; |
| 94 | + } |
| 95 | + catch (HttpRequestException ex) |
| 96 | + { |
| 97 | + return ConsoleOutput.HandleHttpError(ex); |
| 98 | + } |
| 99 | + catch (InvalidDataException ex) |
| 100 | + { |
| 101 | + ConsoleOutput.WriteError($"Error: {ex.Message}"); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private static string FormatSize(long bytes) => bytes switch |
| 107 | + { |
| 108 | + < 1024 => $"{bytes} B", |
| 109 | + < 1024 * 1024 => $"{bytes / 1024.0:F1} KB", |
| 110 | + _ => $"{bytes / (1024.0 * 1024.0):F1} MB" |
| 111 | + }; |
| 112 | + |
| 113 | + private static void PrintHelp() |
| 114 | + { |
| 115 | + Console.WriteLine("Usage: skillserver download-subagent <name> <version> <path> [options]"); |
| 116 | + Console.WriteLine(); |
| 117 | + Console.WriteLine("Download a sub-agent artifact through the native manifest and verify its digest."); |
| 118 | + Console.WriteLine(); |
| 119 | + Console.WriteLine("Arguments:"); |
| 120 | + Console.WriteLine(" <name> Sub-agent name"); |
| 121 | + Console.WriteLine(" <version> Sub-agent version"); |
| 122 | + Console.WriteLine(" <path> Caller-controlled destination path"); |
| 123 | + Console.WriteLine(); |
| 124 | + Console.WriteLine("Options:"); |
| 125 | + Console.WriteLine(" --force, -f Overwrite the destination path if it exists"); |
| 126 | + Console.WriteLine(" --dry-run Show what would be downloaded without writing a file"); |
| 127 | + Console.WriteLine(" --verbose, -v Show download URL and verified digest"); |
| 128 | + } |
| 129 | +} |
0 commit comments