diff --git a/src/commands/agent/create.ts b/src/commands/agent/create.ts index b3c9c05..ce9f0c8 100644 --- a/src/commands/agent/create.ts +++ b/src/commands/agent/create.ts @@ -5,11 +5,12 @@ * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ +import * as fs from 'node:fs'; import { SfCommand, Flags } from '@salesforce/sf-plugins-core'; import { Messages } from '@salesforce/core'; -import { Duration, sleep } from '@salesforce/kit'; import { MultiStageOutput } from '@oclif/multi-stage-output'; import { colorize } from '@oclif/core/ux'; +import { Agent, AgentCreateConfig } from '@salesforce/agents'; Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.create'); @@ -27,6 +28,7 @@ export default class AgentCreate extends SfCommand { public static readonly summary = messages.getMessage('summary'); public static readonly description = messages.getMessage('description'); public static readonly examples = messages.getMessages('examples'); + public static readonly requiresProject = true; public static state = 'beta'; public static readonly flags = { @@ -60,31 +62,33 @@ export default class AgentCreate extends SfCommand { }); mso.goto(jsonParsingStage); - await sleep(Duration.milliseconds(200)); + const agentConfig = { + ...(JSON.parse(fs.readFileSync(flags['job-spec'], 'utf8')) as AgentCreateConfig), + name: flags.name, + }; mso.goto('Generating GenAiPlanner metadata'); - await sleep(Duration.milliseconds(200)); - + // make agent.create emit events so we can update MSO? mso.goto('Creating agent in org'); - // POST to /services/data/{api-version}/connect/attach-agent-topics - - // To simulate time spent on the server generating the spec. - await sleep(Duration.seconds(5)); + const agent = new Agent(flags['target-org'].getConnection(flags['api-version']), this.project!); + const created = await agent.create(agentConfig); mso.goto('Retrieving agent metadata'); - await sleep(Duration.seconds(3)); + // await sleep(Duration.seconds(3)); mso.stop(); this.log( - colorize( - 'green', - `Successfully created ${flags.name} in ${flags['target-org'].getUsername() ?? 'the target org'}.` - ) + created.isSuccess + ? colorize( + 'green', + `Successfully created ${flags.name} in ${flags['target-org'].getUsername() ?? 'the target org'}.` + ) + : colorize('red', `failed to create agent ${flags.name}: ${created.errorMessage ?? ''}`) ); this.log(`Use ${colorize('dim', `sf org open agent --name ${flags.name}`)} to view the agent in the browser.`); - return { isSuccess: true }; + return { isSuccess: created.isSuccess }; } } diff --git a/src/commands/agent/generate/spec.ts b/src/commands/agent/generate/spec.ts index e26ebae..2467bda 100644 --- a/src/commands/agent/generate/spec.ts +++ b/src/commands/agent/generate/spec.ts @@ -13,7 +13,7 @@ import ansis from 'ansis'; import select from '@inquirer/select'; import inquirerInput from '@inquirer/input'; import figures from '@inquirer/figures'; -import { Agent, SfAgent } from '@salesforce/agents'; +import { Agent, AgentCreateConfig, SfAgent } from '@salesforce/agents'; Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.generate.spec'); @@ -169,7 +169,11 @@ export default class AgentCreateSpec extends SfCommand { const filePath = join(flags['output-dir'], flags['file-name']); writeFileSync( filePath, - JSON.stringify({ type, role, companyName, companyDescription, companyWebsite, JobSpec: agentSpec }, null, 4) + JSON.stringify( + { type, role, companyName, companyDescription, companyWebsite, jobSpec: agentSpec } as AgentCreateConfig, + null, + 4 + ) ); this.spinner.stop();