|
| 1 | +/* |
| 2 | + * Copyright 2025, Salesforce, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { join } from 'node:path'; |
| 18 | +import { mkdirSync, writeFileSync, readFileSync } from 'node:fs'; |
| 19 | +import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; |
| 20 | +import { Messages, SfError } from '@salesforce/core'; |
| 21 | +import { Agent, AgentJobSpec } from '@salesforce/agents'; |
| 22 | +import YAML from 'yaml'; |
| 23 | +import { FlaggablePrompt, promptForFlag } from '../../../flags.js'; |
| 24 | + |
| 25 | +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
| 26 | +const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.generate.authoring-bundle'); |
| 27 | + |
| 28 | +export type AgentGenerateAuthoringBundleResult = { |
| 29 | + afScriptPath: string; |
| 30 | + metaXmlPath: string; |
| 31 | + outputDir: string; |
| 32 | +}; |
| 33 | + |
| 34 | +export default class AgentGenerateAuthoringBundle extends SfCommand<AgentGenerateAuthoringBundleResult> { |
| 35 | + public static readonly summary = messages.getMessage('summary'); |
| 36 | + public static readonly description = messages.getMessage('description'); |
| 37 | + public static readonly examples = messages.getMessages('examples'); |
| 38 | + public static readonly requiresProject = true; |
| 39 | + public static state = 'beta'; |
| 40 | + |
| 41 | + public static readonly flags = { |
| 42 | + 'target-org': Flags.requiredOrg(), |
| 43 | + 'api-version': Flags.orgApiVersion(), |
| 44 | + spec: Flags.file({ |
| 45 | + summary: messages.getMessage('flags.spec.summary'), |
| 46 | + char: 'f', |
| 47 | + exists: true, |
| 48 | + }), |
| 49 | + 'output-dir': Flags.directory({ |
| 50 | + summary: messages.getMessage('flags.output-dir.summary'), |
| 51 | + char: 'd', |
| 52 | + }), |
| 53 | + name: Flags.string({ |
| 54 | + summary: messages.getMessage('flags.name.summary'), |
| 55 | + char: 'n', |
| 56 | + }), |
| 57 | + }; |
| 58 | + |
| 59 | + private static readonly FLAGGABLE_PROMPTS = { |
| 60 | + name: { |
| 61 | + message: messages.getMessage('flags.name.summary'), |
| 62 | + validate: (d: string): boolean | string => d.length > 0 || 'Name cannot be empty', |
| 63 | + required: true, |
| 64 | + }, |
| 65 | + spec: { |
| 66 | + message: messages.getMessage('flags.spec.summary'), |
| 67 | + validate: (d: string): boolean | string => d.length > 0 || 'Spec file path cannot be empty', |
| 68 | + required: true, |
| 69 | + }, |
| 70 | + } satisfies Record<string, FlaggablePrompt>; |
| 71 | + |
| 72 | + public async run(): Promise<AgentGenerateAuthoringBundleResult> { |
| 73 | + const { flags } = await this.parse(AgentGenerateAuthoringBundle); |
| 74 | + const { 'output-dir': outputDir, 'target-org': targetOrg } = flags; |
| 75 | + |
| 76 | + // If we don't have a spec yet, prompt for it |
| 77 | + const spec = flags['spec'] ?? (await promptForFlag(AgentGenerateAuthoringBundle.FLAGGABLE_PROMPTS['spec'])); |
| 78 | + |
| 79 | + // If we don't have a name yet, prompt for it |
| 80 | + const name = ( |
| 81 | + flags['name'] ?? (await promptForFlag(AgentGenerateAuthoringBundle.FLAGGABLE_PROMPTS['name'])) |
| 82 | + ).replaceAll(' ', '_'); |
| 83 | + |
| 84 | + try { |
| 85 | + // Get default output directory if not specified |
| 86 | + const defaultOutputDir = join(this.project!.getDefaultPackage().fullPath, 'main', 'default'); |
| 87 | + const targetOutputDir = join(outputDir ?? defaultOutputDir, 'aiAuthoringBundle', name); |
| 88 | + |
| 89 | + // Generate file paths |
| 90 | + const afScriptPath = join(targetOutputDir, `${name}.afscript`); |
| 91 | + const metaXmlPath = join(targetOutputDir, `${name}.authoring-bundle-meta.xml`); |
| 92 | + |
| 93 | + // Write AFScript file |
| 94 | + const conn = targetOrg.getConnection(flags['api-version']); |
| 95 | + const specContents = YAML.parse(readFileSync(spec, 'utf8')) as AgentJobSpec; |
| 96 | + const afScript = await Agent.createAfScript(conn, specContents); |
| 97 | + // Create output directory if it doesn't exist |
| 98 | + mkdirSync(targetOutputDir, { recursive: true }); |
| 99 | + writeFileSync(afScriptPath, afScript); |
| 100 | + |
| 101 | + // Write meta.xml file |
| 102 | + const metaXml = `<?xml version="1.0" encoding="UTF-8"?> |
| 103 | +<aiAuthoringBundle> |
| 104 | + <Label>${specContents.role}</Label> |
| 105 | + <BundleType>${specContents.agentType}</BundleType> |
| 106 | + <VersionTag>Spring2026</VersionTag> |
| 107 | + <VersionDescription>Initial release for ${name}</VersionDescription> |
| 108 | + <SourceBundleVersion></SourceBundleVersion> |
| 109 | + <Target></Target> |
| 110 | +</aiAuthoringBundle>`; |
| 111 | + writeFileSync(metaXmlPath, metaXml); |
| 112 | + |
| 113 | + this.logSuccess(`Successfully generated ${name} Authoring Bundle`); |
| 114 | + |
| 115 | + return { |
| 116 | + afScriptPath, |
| 117 | + metaXmlPath, |
| 118 | + outputDir: targetOutputDir, |
| 119 | + }; |
| 120 | + } catch (error) { |
| 121 | + const err = SfError.wrap(error); |
| 122 | + throw new SfError(messages.getMessage('error.failed-to-create-afscript'), 'AfScriptGenerationError', [ |
| 123 | + err.message, |
| 124 | + ]); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments