Skip to content

Commit d5f7aca

Browse files
committed
feat: add agent generate template command
1 parent 6663c77 commit d5f7aca

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-0
lines changed

command-snapshot.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@
4545
],
4646
"plugin": "@salesforce/plugin-agent"
4747
},
48+
{
49+
"alias": [],
50+
"command": "agent:generate:template",
51+
"flagAliases": [],
52+
"flagChars": ["d", "o"],
53+
"flags": ["agent-api-name", "api-version", "flags-dir", "json", "output-dir", "target-org"],
54+
"plugin": "@salesforce/plugin-agent"
55+
},
4856
{
4957
"alias": [],
5058
"command": "agent:generate:test-spec",
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# summary
2+
3+
Generate an agent template for packaging.
4+
5+
# description
6+
7+
Generate agent template metadata to for packaging your agent.
8+
9+
# flags.agent-api-name.summary
10+
11+
API name of an existing Bot.
12+
13+
# flags.output-dir.summary
14+
15+
Directory to write the agent template.
16+
17+
# examples
18+
19+
- Generate an agent template from a Bot API name in your default package dir:
20+
21+
<%= config.bin %> <%= command.id %> --agent-api-name My_Packaged_Agent
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$ref": "#/definitions/AgentGenerateTemplateResult",
4+
"definitions": {
5+
"AgentGenerateTemplateResult": {
6+
"type": "object",
7+
"properties": {
8+
"path": {
9+
"type": "string"
10+
}
11+
},
12+
"required": ["path"],
13+
"additionalProperties": false
14+
}
15+
}
16+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (c) 2024, salesforce.com, inc.
3+
* All rights reserved.
4+
* Licensed under the BSD 3-Clause license.
5+
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
6+
*/
7+
8+
import { dirname, join, resolve } from 'node:path';
9+
import { mkdirSync, writeFileSync } from 'node:fs';
10+
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
11+
import { Messages } from '@salesforce/core';
12+
13+
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
14+
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.generate.template');
15+
16+
export type AgentGenerateTemplateResult = {
17+
path: string;
18+
};
19+
20+
export default class AgentGenerateTemplate extends SfCommand<AgentGenerateTemplateResult> {
21+
public static readonly summary = messages.getMessage('summary');
22+
public static readonly description = messages.getMessage('description');
23+
public static readonly examples = messages.getMessages('examples');
24+
public static state = 'beta';
25+
public static readonly requiresProject = true;
26+
27+
public static readonly flags = {
28+
'target-org': Flags.requiredOrg(),
29+
'api-version': Flags.orgApiVersion(),
30+
'agent-api-name': Flags.string({
31+
summary: messages.getMessage('flags.agent-api-name.summary'),
32+
required: true,
33+
}),
34+
'output-dir': Flags.directory({
35+
char: 'd',
36+
exists: true,
37+
summary: messages.getMessage('flags.output-dir.summary'),
38+
}),
39+
};
40+
41+
public async run(): Promise<AgentGenerateTemplateResult> {
42+
const { flags } = await this.parse(AgentGenerateTemplate);
43+
44+
// TODO: look for a Bot with the agent API name
45+
const botName = flags['agent-api-name'];
46+
const outputDir = flags['output-dir'] ? resolve(flags['output-dir']) : this.project?.getDefaultPackage().fullPath;
47+
48+
const agentTemplateFilePath = join(outputDir as string, 'agentTemplates', `${botName}.agentTemplate-meta.xml`);
49+
mkdirSync(dirname(agentTemplateFilePath), { recursive: true });
50+
51+
writeFileSync(agentTemplateFilePath, xmlContent);
52+
53+
this.log(`\nSaved agent template: ${agentTemplateFilePath}`);
54+
55+
return { path: agentTemplateFilePath };
56+
}
57+
}
58+
59+
const xmlContent = `<?xml version="1.0" encoding="UTF-8"?>
60+
<AgentTemplate xmlns="http://soap.sforce.com/2006/04/metadata">
61+
</AgentTemplate>
62+
`;

0 commit comments

Comments
 (0)