Skip to content

Commit 6e51947

Browse files
chore: update agent preview to work with .agent files
1 parent afdb37b commit 6e51947

File tree

4 files changed

+50
-54
lines changed

4 files changed

+50
-54
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
"@salesforce/sf-plugins-core": "^12.2.4",
1616
"@salesforce/source-deploy-retrieve": "^12.22.1",
1717
"@salesforce/types": "^1.4.0",
18+
"@types/glob": "^9.0.0",
1819
"ansis": "^3.3.2",
1920
"fast-xml-parser": "^4.5.1",
21+
"glob": "^11.0.3",
2022
"ink": "5.0.1",
2123
"ink-text-input": "^6.0.0",
2224
"inquirer-autocomplete-standalone": "^0.8.1",

src/commands/agent/preview.ts

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
*/
1616

1717
import { resolve, join } from 'node:path';
18-
import { readdirSync, statSync } from 'node:fs';
18+
import * as path from 'node:path';
19+
import { globSync } from 'glob';
1920
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
2021
import { AuthInfo, Connection, Messages, SfError, SfProject } from '@salesforce/core';
2122
import React from 'react';
@@ -49,12 +50,13 @@ enum AgentSource {
4950
LOCAL = 'local',
5051
}
5152

52-
type AgentValue = {
53-
Id: string;
54-
DeveloperName: string;
55-
source: AgentSource.ORG;
56-
} |
57-
{ DeveloperName: string; source: AgentSource.LOCAL; path: string };
53+
type AgentValue =
54+
| {
55+
Id: string;
56+
DeveloperName: string;
57+
source: AgentSource.ORG;
58+
}
59+
| { DeveloperName: string; source: AgentSource.LOCAL; path: string };
5860

5961
// https://developer.salesforce.com/docs/einstein/genai/guide/agent-api-get-started.html#prerequisites
6062
export const UNSUPPORTED_AGENTS = ['Copilot_for_Salesforce'];
@@ -166,10 +168,10 @@ export default class AgentPreview extends SfCommand<AgentPreviewResult> {
166168

167169
const outputDir = await resolveOutputDir(flags['output-dir'], flags['apex-debug']);
168170
// Both classes share the same interface for the methods we need
169-
const agentPreview = selectedAgent.source === AgentSource.ORG ?
170-
new Preview(jwtConn, selectedAgent.Id) :
171-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
172-
new AgentSimulate(jwtConn, selectedAgent.path, true) as unknown as Preview;
171+
const agentPreview: Preview | AgentSimulate =
172+
selectedAgent.source === AgentSource.ORG
173+
? new Preview(jwtConn, selectedAgent.Id)
174+
: new AgentSimulate(jwtConn, selectedAgent.path, true);
173175

174176
agentPreview.toggleApexDebugMode(flags['apex-debug']);
175177

@@ -207,10 +209,7 @@ export const validateAgent = (agent: AgentData): boolean => {
207209
return true;
208210
};
209211

210-
export const getAgentChoices = (
211-
agents: AgentData[],
212-
project: SfProject
213-
): Array<Choice<AgentValue>> => {
212+
export const getAgentChoices = (agents: AgentData[], project: SfProject): Array<Choice<AgentValue>> => {
214213
const choices: Array<Choice<AgentValue>> = [];
215214

216215
// Add org agents
@@ -229,37 +228,25 @@ export const getAgentChoices = (
229228
});
230229
}
231230

232-
// Add local agents from authoring bundles
233-
const localAgents = findAuthoringBundle(project.getPath(), '*');
234-
if (localAgents) {
235-
const bundlePath = localAgents.replace(/\/[^/]+$/, ''); // Get parent directory
236-
const agentDirs = readdirSync(bundlePath).filter((dir) =>
237-
statSync(join(bundlePath, dir)).isDirectory()
238-
);
239-
240-
agentDirs.forEach((agentDir) => {
241-
choices.push({
242-
name: `${agentDir} (local)`,
243-
value: {
244-
DeveloperName: agentDir,
245-
source: AgentSource.LOCAL,
246-
path: join(bundlePath, agentDir),
247-
},
248-
});
231+
// Add local agents from .agent files
232+
const localAgentPaths = globSync('**/*.agent', { cwd: project.getPath() });
233+
for (const agentPath of localAgentPaths) {
234+
const agentName = path.basename(agentPath, '.agent');
235+
choices.push({
236+
name: `${agentName} (local)`,
237+
value: {
238+
DeveloperName: agentName,
239+
source: AgentSource.LOCAL,
240+
path: path.join(project.getPath(), agentPath),
241+
},
249242
});
250243
}
251244

252245
return choices;
253246
};
254247

255-
256-
export const getClientAppsFromAuth = (authInfo: AuthInfo): string[] => {
257-
const config = authInfo.getConnectionOptions();
258-
const clientApps = Object.entries(config)
259-
.filter(([key]) => key.startsWith('oauthClientApp_'))
260-
.map(([, value]) => value as string);
261-
return clientApps;
262-
};
248+
export const getClientAppsFromAuth = (authInfo: AuthInfo): string[] =>
249+
Object.keys(authInfo.getFields().clientApps ?? {});
263250

264251
export const resolveOutputDir = async (
265252
outputDir: string | undefined,

src/components/agent-preview-react.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import React from 'react';
2121
import { Box, Text, useInput } from 'ink';
2222
import TextInput from 'ink-text-input';
2323
import { Connection, SfError } from '@salesforce/core';
24-
import { AgentPreview, AgentPreviewSendResponse, writeDebugLog } from '@salesforce/agents';
24+
import { AgentPreviewBase, AgentPreviewSendResponse, writeDebugLog, type AgentInteractionBase } from '@salesforce/agents';
2525
import { sleep } from '@salesforce/kit';
2626

2727
// Component to show a simple typing animation
@@ -76,7 +76,7 @@ const saveTranscriptsToFile = (
7676
*/
7777
export function AgentPreviewReact(props: {
7878
readonly connection: Connection;
79-
readonly agent: AgentPreview;
79+
readonly agent: AgentPreviewBase | AgentInteractionBase;
8080
readonly name: string;
8181
readonly outputDir: string | undefined;
8282
}): React.ReactNode {

yarn.lock

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,13 @@
24512451
resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.20.tgz#cb291577ed342ca92600430841a00329ba05cecc"
24522452
integrity sha512-/pC9HAB5I/xMlc5FP77qjCnI16ChlJfW0tGa0IUcFn38VJrTV6DeZ60NU5KZBtaOZqjdpwTWohz5HU1RrhiYxQ==
24532453

2454+
"@types/glob@^9.0.0":
2455+
version "9.0.0"
2456+
resolved "https://registry.yarnpkg.com/@types/glob/-/glob-9.0.0.tgz#7b942fafe09c55671912b34f04e8e4676faf32b1"
2457+
integrity sha512-00UxlRaIUvYm4R4W9WYkN8/J+kV8fmOQ7okeH6YFtGWFMt3odD45tpG5yA5wnL7HE6lLgjaTW5n14ju2hl2NNA==
2458+
dependencies:
2459+
glob "*"
2460+
24542461
"@types/hast@^3.0.0", "@types/hast@^3.0.4":
24552462
version "3.0.4"
24562463
resolved "https://registry.yarnpkg.com/@types/hast/-/hast-3.0.4.tgz#1d6b39993b82cea6ad783945b0508c25903e15aa"
@@ -4941,6 +4948,18 @@ glob-to-regex.js@^1.0.1:
49414948
resolved "https://registry.yarnpkg.com/glob-to-regex.js/-/glob-to-regex.js-1.2.0.tgz#2b323728271d133830850e32311f40766c5f6413"
49424949
integrity sha512-QMwlOQKU/IzqMUOAZWubUOT8Qft+Y0KQWnX9nK3ch0CJg0tTp4TvGZsTfudYKv2NzoQSyPcnA6TYeIQ3jGichQ==
49434950

4951+
glob@*, glob@^11.0.3:
4952+
version "11.0.3"
4953+
resolved "https://registry.yarnpkg.com/glob/-/glob-11.0.3.tgz#9d8087e6d72ddb3c4707b1d2778f80ea3eaefcd6"
4954+
integrity sha512-2Nim7dha1KVkaiF4q6Dj+ngPPMdfvLJEOpZk/jKiUAkqKebpGAWQXAq9z1xu9HKu5lWfqw/FASuccEjyznjPaA==
4955+
dependencies:
4956+
foreground-child "^3.3.1"
4957+
jackspeak "^4.1.1"
4958+
minimatch "^10.0.3"
4959+
minipass "^7.1.2"
4960+
package-json-from-dist "^1.0.0"
4961+
path-scurry "^2.0.0"
4962+
49444963
glob@^10.3.10:
49454964
version "10.4.5"
49464965
resolved "https://registry.yarnpkg.com/glob/-/glob-10.4.5.tgz#f4d9f0b90ffdbab09c9d77f5f29b4262517b0956"
@@ -4953,18 +4972,6 @@ glob@^10.3.10:
49534972
package-json-from-dist "^1.0.0"
49544973
path-scurry "^1.11.1"
49554974

4956-
glob@^11.0.3:
4957-
version "11.0.3"
4958-
resolved "https://registry.yarnpkg.com/glob/-/glob-11.0.3.tgz#9d8087e6d72ddb3c4707b1d2778f80ea3eaefcd6"
4959-
integrity sha512-2Nim7dha1KVkaiF4q6Dj+ngPPMdfvLJEOpZk/jKiUAkqKebpGAWQXAq9z1xu9HKu5lWfqw/FASuccEjyznjPaA==
4960-
dependencies:
4961-
foreground-child "^3.3.1"
4962-
jackspeak "^4.1.1"
4963-
minimatch "^10.0.3"
4964-
minipass "^7.1.2"
4965-
package-json-from-dist "^1.0.0"
4966-
path-scurry "^2.0.0"
4967-
49684975
glob@^7.0.0, glob@^7.1.3, glob@^7.1.4, glob@^7.1.6:
49694976
version "7.2.3"
49704977
resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b"

0 commit comments

Comments
 (0)