|
| 1 | +/** |
| 2 | + * Create agent command |
| 3 | + */ |
| 4 | +import chalk from "chalk"; |
| 5 | +import { createAgent } from "../../services/agentService.js"; |
| 6 | +import { output, outputError } from "../../utils/output.js"; |
| 7 | + |
| 8 | +interface CreateOptions { |
| 9 | + name: string; |
| 10 | + agentVersion: string; |
| 11 | + source: string; |
| 12 | + package?: string; |
| 13 | + registryUrl?: string; |
| 14 | + repository?: string; |
| 15 | + ref?: string; |
| 16 | + objectId?: string; |
| 17 | + setupCommands?: string[]; |
| 18 | + output?: string; |
| 19 | +} |
| 20 | + |
| 21 | +// Maps each source type to the options it accepts (beyond --setup-commands, which all types accept) |
| 22 | +const validOptionsBySource: Record<string, (keyof CreateOptions)[]> = { |
| 23 | + npm: ["package", "registryUrl"], |
| 24 | + pip: ["package", "registryUrl"], |
| 25 | + git: ["repository", "ref"], |
| 26 | + object: ["objectId"], |
| 27 | +}; |
| 28 | + |
| 29 | +// All source-specific option flags (for error messages) |
| 30 | +const allSourceOptions: { key: keyof CreateOptions; flag: string }[] = [ |
| 31 | + { key: "package", flag: "--package" }, |
| 32 | + { key: "registryUrl", flag: "--registry-url" }, |
| 33 | + { key: "repository", flag: "--repository" }, |
| 34 | + { key: "ref", flag: "--ref" }, |
| 35 | + { key: "objectId", flag: "--object-id" }, |
| 36 | +]; |
| 37 | + |
| 38 | +function rejectInvalidOptions( |
| 39 | + sourceType: string, |
| 40 | + options: CreateOptions, |
| 41 | +): void { |
| 42 | + const allowed = validOptionsBySource[sourceType] || []; |
| 43 | + const invalid = allSourceOptions |
| 44 | + .filter( |
| 45 | + (opt) => options[opt.key] !== undefined && !allowed.includes(opt.key), |
| 46 | + ) |
| 47 | + .map((opt) => opt.flag); |
| 48 | + |
| 49 | + if (invalid.length > 0) { |
| 50 | + throw new Error( |
| 51 | + `${invalid.join(", ")} cannot be used with ${sourceType} source type`, |
| 52 | + ); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function buildSourceOptions( |
| 57 | + sourceType: string, |
| 58 | + options: CreateOptions, |
| 59 | +): Record<string, unknown> { |
| 60 | + rejectInvalidOptions(sourceType, options); |
| 61 | + |
| 62 | + switch (sourceType) { |
| 63 | + case "npm": |
| 64 | + case "pip": |
| 65 | + if (!options.package) { |
| 66 | + throw new Error(`--package is required for ${sourceType} source type`); |
| 67 | + } |
| 68 | + return { |
| 69 | + package_name: options.package, |
| 70 | + registry_url: options.registryUrl || undefined, |
| 71 | + agent_setup: options.setupCommands || undefined, |
| 72 | + }; |
| 73 | + case "git": |
| 74 | + if (!options.repository) { |
| 75 | + throw new Error("--repository is required for git source type"); |
| 76 | + } |
| 77 | + return { |
| 78 | + repository: options.repository, |
| 79 | + ref: options.ref || undefined, |
| 80 | + agent_setup: options.setupCommands || undefined, |
| 81 | + }; |
| 82 | + case "object": |
| 83 | + if (!options.objectId) { |
| 84 | + throw new Error("--object-id is required for object source type"); |
| 85 | + } |
| 86 | + return { |
| 87 | + object_id: options.objectId, |
| 88 | + agent_setup: options.setupCommands || undefined, |
| 89 | + }; |
| 90 | + default: |
| 91 | + throw new Error( |
| 92 | + `Unknown source type: ${sourceType}. Use npm, pip, git, or object.`, |
| 93 | + ); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +export async function createAgentCommand( |
| 98 | + options: CreateOptions, |
| 99 | +): Promise<void> { |
| 100 | + try { |
| 101 | + const sourceType = options.source; |
| 102 | + const sourceOptions = buildSourceOptions(sourceType, options); |
| 103 | + |
| 104 | + const agent = await createAgent({ |
| 105 | + name: options.name, |
| 106 | + version: options.agentVersion, |
| 107 | + source: { type: sourceType, [sourceType]: sourceOptions }, |
| 108 | + }); |
| 109 | + |
| 110 | + const format = options.output || "text"; |
| 111 | + if (format !== "text") { |
| 112 | + output(agent, { format, defaultFormat: "json" }); |
| 113 | + } else { |
| 114 | + console.log(chalk.green("✓") + " Agent created successfully"); |
| 115 | + console.log(); |
| 116 | + console.log(` ${chalk.bold("Name:")} ${agent.name}`); |
| 117 | + console.log(` ${chalk.bold("ID:")} ${chalk.dim(agent.id)}`); |
| 118 | + console.log(` ${chalk.bold("Version:")} ${agent.version}`); |
| 119 | + console.log(` ${chalk.bold("Source:")} ${sourceType}`); |
| 120 | + } |
| 121 | + } catch (error) { |
| 122 | + outputError("Failed to create agent", error); |
| 123 | + } |
| 124 | +} |
0 commit comments