Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/commands/agent/generate/authoring-bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { AgentJobSpec, ScriptAgent } from '@salesforce/agents';
import YAML from 'yaml';
import { input as inquirerInput } from '@inquirer/prompts';
import { theme } from '../../../inquirer-theme.js';
import { FlaggablePrompt, promptForFlag, promptForYamlFile } from '../../../flags.js';
import { FlaggablePrompt, promptForFlag, promptForSpecYaml } from '../../../flags.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.generate.authoring-bundle');
Expand Down Expand Up @@ -105,7 +105,7 @@ export default class AgentGenerateAuthoringBundle extends SfCommand<AgentGenerat
const { 'output-dir': outputDir } = flags;

// If we don't have a spec yet, prompt for it
const spec = flags.spec ?? (await promptForYamlFile(AgentGenerateAuthoringBundle.FLAGGABLE_PROMPTS['spec']));
const spec = flags.spec ?? (await promptForSpecYaml(AgentGenerateAuthoringBundle.FLAGGABLE_PROMPTS['spec']));

// If we don't have a name yet, prompt for it
const name = flags['name'] ?? (await promptForFlag(AgentGenerateAuthoringBundle.FLAGGABLE_PROMPTS['name']));
Expand Down Expand Up @@ -135,9 +135,10 @@ export default class AgentGenerateAuthoringBundle extends SfCommand<AgentGenerat
const metaXmlPath = join(targetOutputDir, `${bundleApiName}.bundle-meta.xml`);

// Write Agent file
const specContents = YAML.parse(readFileSync(spec, 'utf8')) as AgentJobSpec;
await ScriptAgent.createAuthoringBundle({
agentSpec: { ...specContents, ...{ name, developerName: bundleApiName } },
agentSpec: spec
? { ...(YAML.parse(readFileSync(spec, 'utf8')) as AgentJobSpec), ...{ name, developerName: bundleApiName } }
: undefined,
project: this.project!,
bundleApiName,
});
Expand Down
19 changes: 19 additions & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,25 @@ export const promptForFileByExtensions = async (
export const promptForYamlFile = async (flagDef: FlaggablePrompt): Promise<string> =>
promptForFileByExtensions(flagDef, ['.yml', '.yaml']);

export const promptForSpecYaml = async (flagDef: FlaggablePrompt): Promise<string | undefined> => {
const hiddenDirs = await getHiddenDirs();
const dirsToTraverse = [process.cwd()];
const files = traverseForFiles(dirsToTraverse, ['AgentSpec.yml', 'AgentSpec.yaml'], ['node_modules', ...hiddenDirs]);
return autocomplete({
message: flagDef.promptMessage ?? flagDef.message.replace(/\.$/, ''),
// eslint-disable-next-line @typescript-eslint/require-await
source: async (input) => {
const arr = [
...files.map((o) => ({ name: relative(process.cwd(), o), value: o })),
{ name: 'Default Agent Spec', value: undefined },
];

if (!input) return arr;
return arr.filter((o) => o.name.includes(input));
},
});
};

export const promptForFlag = async (flagDef: FlaggablePrompt): Promise<string> => {
const message = flagDef.promptMessage ?? flagDef.message.replace(/\.$/, '');
if (flagDef.options) {
Expand Down
38 changes: 36 additions & 2 deletions test/flags.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* limitations under the License.
*/

import { join } from 'node:path';
import { join, relative } from 'node:path';
import { mkdir, writeFile, rm } from 'node:fs/promises';
import { expect } from 'chai';
import { traverseForFiles } from '../src/flags.js';
import { getHiddenDirs, traverseForFiles } from '../src/flags.js';

describe('traverseForFiles', () => {
const testDir = join(process.cwd(), 'test-temp');
Expand Down Expand Up @@ -73,3 +73,37 @@ describe('traverseForFiles', () => {
expect(results).to.have.lengthOf(6);
});
});

describe('promptForSpecYaml', () => {
it('should include "Default Agent Spec" in the list of options', async () => {
// Test the source function logic directly by replicating what promptForSpecYaml does

const hiddenDirs = await getHiddenDirs();
const dirsToTraverse = [process.cwd()];
const files = traverseForFiles(
dirsToTraverse,
['AgentSpec.yml', 'AgentSpec.yaml'],
['node_modules', ...hiddenDirs]
);

// Replicate the source function logic from promptForSpecYaml
const source = async (input?: string) => {
const arr = [
...files.map((o) => ({ name: relative(process.cwd(), o), value: o })),
{ name: 'Default Agent Spec', value: undefined },
];

if (!input) return arr;
return arr.filter((o) => o.name.includes(input));
};

// Call the source function with no input to get all options
const options = await source();

// Verify "Default Agent Spec" is in the list
const defaultAgentSpecOption = options.find((option) => option.name === 'Default Agent Spec');
expect(defaultAgentSpecOption).to.be.ok;
expect(defaultAgentSpecOption?.name).to.equal('Default Agent Spec');
expect(defaultAgentSpecOption?.value).to.be.undefined;
});
});
Loading