Skip to content

Commit d04ad2b

Browse files
feat: handle agent compilation errors
1 parent 37ef2b0 commit d04ad2b

File tree

4 files changed

+58
-12
lines changed

4 files changed

+58
-12
lines changed

src/commands/agent/generate/authoring-bundle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export default class AgentGenerateAuthoringBundle extends SfCommand<AgentGenerat
134134
// Write Agent file
135135
const conn = targetOrg.getConnection(flags['api-version']);
136136
const specContents = YAML.parse(readFileSync(spec, 'utf8')) as AgentJobSpec;
137-
const agent = await Agent.createAgent(conn, specContents);
137+
const agent = await Agent.createAgentScript(conn, specContents);
138138
// Create output directory if it doesn't exist
139139
mkdirSync(targetOutputDir, { recursive: true });
140140
writeFileSync(agentPath, agent);

src/commands/agent/publish/authoring-bundle.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { Agent, findAuthoringBundle } from '@salesforce/agents';
2323
import { RequestStatus, type ScopedPostRetrieve } from '@salesforce/source-deploy-retrieve';
2424
import { ensureArray } from '@salesforce/kit';
2525
import { FlaggablePrompt, promptForAgentFiles } from '../../../flags.js';
26+
import { throwAgentCompilationError } from '../../../common.js';
2627

2728
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
2829
const messages = Messages.loadMessages('@salesforce/plugin-agent', 'agent.publish.authoring-bundle');
@@ -103,12 +104,15 @@ export default class AgentPublishAuthoringBundle extends SfCommand<AgentPublishA
103104
const conn = targetOrg.getConnection(flags['api-version']);
104105

105106
// First compile the .agent file to get the Agent JSON
106-
const agentJson = await Agent.compileAgent(
107+
const compileResponse = await Agent.compileAgentScript(
107108
conn,
108109
readFileSync(join(authoringBundleDir, `${apiName}.agent`), 'utf8')
109110
);
110-
mso.skipTo('Publish Agent');
111-
111+
if (compileResponse.status === 'success') {
112+
mso.skipTo('Publish Agent');
113+
} else {
114+
throwAgentCompilationError(compileResponse.errors);
115+
}
112116
// Then publish the Agent JSON to create the agent
113117
// Set up lifecycle listeners for retrieve events
114118
Lifecycle.getInstance().on('scopedPreRetrieve', () => {
@@ -129,7 +133,7 @@ export default class AgentPublishAuthoringBundle extends SfCommand<AgentPublishA
129133
}
130134
return Promise.resolve();
131135
});
132-
const result = await Agent.publishAgentJson(conn, this.project!, agentJson);
136+
const result = await Agent.publishAgentJson(conn, this.project!, compileResponse.compiledArtifact);
133137
mso.stop();
134138

135139
return {

src/commands/agent/validate/authoring-bundle.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { MultiStageOutput } from '@oclif/multi-stage-output';
2121
import { Agent, findAuthoringBundle } from '@salesforce/agents';
2222
import { Duration, sleep } from '@salesforce/kit';
2323
import { colorize } from '@oclif/core/ux';
24+
import { throwAgentCompilationError } from '../../../common.js';
2425
import { FlaggablePrompt, promptForAgentFiles } from '../../../flags.js';
2526

2627
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
@@ -105,12 +106,19 @@ export default class AgentValidateAuthoringBundle extends SfCommand<AgentValidat
105106
const conn = targetOrg.getConnection(flags['api-version']);
106107
// Call Agent.compileAgent() API
107108
await sleep(Duration.seconds(2));
108-
await Agent.compileAgent(conn, readFileSync(join(authoringBundleDir, `${apiName}.agent`), 'utf8'));
109-
mso.updateData({ status: 'COMPLETED' });
110-
mso.stop('completed');
111-
return {
112-
success: true,
113-
};
109+
const result = await Agent.compileAgentScript(
110+
conn,
111+
readFileSync(join(authoringBundleDir, `${apiName}.agent`), 'utf8')
112+
);
113+
if (result.status === 'success') {
114+
mso.updateData({ status: 'COMPLETED' });
115+
mso.stop('completed');
116+
return {
117+
success: true,
118+
};
119+
} else {
120+
throwAgentCompilationError(result.errors);
121+
}
114122
} catch (error) {
115123
// Handle validation errors
116124
const err = SfError.wrap(error);
@@ -121,7 +129,7 @@ export default class AgentValidateAuthoringBundle extends SfCommand<AgentValidat
121129
count += 1;
122130
const type = line.split(':')[0];
123131
const rest = line.substring(line.indexOf(':')).trim();
124-
return `- ${colorize('red', type)} ${rest}`;
132+
return `- ${colorize('red', type)}${rest}`;
125133
})
126134
.join('\n');
127135

src/common.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2025, Salesforce, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { EOL } from 'node:os';
17+
import { SfError } from '@salesforce/core';
18+
import { CompilationError } from '@salesforce/agents';
19+
20+
/**
21+
* Utility function to generate SfError when there are agent compilation errors.
22+
*
23+
* @param compilationErrors - The compilation errors as strings, CompilationError objects, or array of either
24+
* @throws SfError - Always throws a Salesforce CLI error
25+
*/
26+
export function throwAgentCompilationError(compilationErrors: CompilationError[]): never {
27+
const errors = compilationErrors;
28+
29+
throw SfError.create({
30+
name: 'CompileAgentScriptError',
31+
message: errors.map((e) => `${e.errorType}: ${e.description} [Ln ${e.lineStart}, Col ${e.colStart}]`).join(EOL),
32+
data: errors,
33+
});
34+
}

0 commit comments

Comments
 (0)