|
| 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 { Connection, Messages, Org, SfError } from '@salesforce/core'; |
| 9 | +import { Agent, type BotMetadata } from '@salesforce/agents'; |
| 10 | +import { select } from '@inquirer/prompts'; |
| 11 | + |
| 12 | +type Choice<Value> = { |
| 13 | + value: Value; |
| 14 | + name?: string; |
| 15 | + disabled?: boolean | string; |
| 16 | +}; |
| 17 | +type AgentValue = { |
| 18 | + Id: string; |
| 19 | + DeveloperName: string; |
| 20 | +}; |
| 21 | + |
| 22 | +Messages.importMessagesDirectoryFromMetaUrl(import.meta.url); |
| 23 | +const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.activation'); |
| 24 | + |
| 25 | +export const UNSUPPORTED_AGENTS = ['Copilot_for_Salesforce']; |
| 26 | +export const agentIsUnsupported = (devName: string): boolean => UNSUPPORTED_AGENTS.includes(devName); |
| 27 | + |
| 28 | +export const validateAgent = (agent: BotMetadata): boolean => { |
| 29 | + if (agent.IsDeleted) { |
| 30 | + throw messages.createError('error.agentIsDeleted', [agent.DeveloperName]); |
| 31 | + } |
| 32 | + if (agentIsUnsupported(agent.DeveloperName)) { |
| 33 | + throw messages.createError('error.agentIsDefault', [agent.DeveloperName]); |
| 34 | + } |
| 35 | + return true; |
| 36 | +}; |
| 37 | + |
| 38 | +export const getAgentChoices = (agents: BotMetadata[], status: 'Active' | 'Inactive'): Array<Choice<AgentValue>> => |
| 39 | + agents.map((agent) => { |
| 40 | + let disabled: string | boolean = false; |
| 41 | + |
| 42 | + const lastBotVersion = agent.BotVersions.records[agent.BotVersions.records.length - 1]; |
| 43 | + if (lastBotVersion.Status === status) { |
| 44 | + disabled = `(Already ${status})`; |
| 45 | + } |
| 46 | + if (agentIsUnsupported(agent.DeveloperName)) { |
| 47 | + disabled = '(Not Supported)'; |
| 48 | + } |
| 49 | + |
| 50 | + return { |
| 51 | + name: agent.DeveloperName, |
| 52 | + value: { |
| 53 | + Id: agent.Id, |
| 54 | + DeveloperName: agent.DeveloperName, |
| 55 | + }, |
| 56 | + disabled, |
| 57 | + }; |
| 58 | + }); |
| 59 | + |
| 60 | +export const getAgentForActivation = async (config: { |
| 61 | + conn: Connection; |
| 62 | + targetOrg: Org; |
| 63 | + status: 'Active' | 'Inactive'; |
| 64 | + apiNameFlag?: string; |
| 65 | +}): Promise<Agent> => { |
| 66 | + const { conn, targetOrg, status, apiNameFlag } = config; |
| 67 | + |
| 68 | + let agentsInOrg: BotMetadata[] = []; |
| 69 | + try { |
| 70 | + agentsInOrg = await Agent.listRemote(conn); |
| 71 | + } catch (error) { |
| 72 | + throw SfError.create({ |
| 73 | + message: 'Error listing agents in org', |
| 74 | + name: 'NoAgentsInOrgError', |
| 75 | + cause: error, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + if (!agentsInOrg.length) { |
| 80 | + throw messages.createError('error.noAgentsInOrg', [targetOrg.getUsername()]); |
| 81 | + } |
| 82 | + |
| 83 | + let selectedAgent: BotMetadata | undefined; |
| 84 | + |
| 85 | + if (apiNameFlag) { |
| 86 | + selectedAgent = agentsInOrg.find((agent) => agent.DeveloperName === apiNameFlag); |
| 87 | + if (!selectedAgent) { |
| 88 | + throw messages.createError('error.missingAgentInOrg', [apiNameFlag, targetOrg.getUsername()]); |
| 89 | + } |
| 90 | + validateAgent(selectedAgent); |
| 91 | + } else { |
| 92 | + const agentChoice = await select({ |
| 93 | + message: 'Select an agent', |
| 94 | + choices: getAgentChoices(agentsInOrg, status), |
| 95 | + }); |
| 96 | + selectedAgent = agentsInOrg.find((agent) => agent.DeveloperName === agentChoice.DeveloperName); |
| 97 | + } |
| 98 | + |
| 99 | + return new Agent({ connection: conn, nameOrId: selectedAgent!.Id }); |
| 100 | +}; |
0 commit comments