@salesforce/agents
Version:
Client side APIs for working with Salesforce agents
85 lines • 2.98 kB
JavaScript
;
/*
* Copyright (c) 2024, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.AgentPreview = void 0;
const node_crypto_1 = require("node:crypto");
const core_1 = require("@salesforce/core");
const maybe_mock_1 = require("./maybe-mock");
class AgentPreview {
apiBase;
instanceUrl;
maybeMock;
constructor(connection) {
this.apiBase = 'https://api.salesforce.com/einstein/ai-agent/v1';
this.instanceUrl = connection.instanceUrl;
this.maybeMock = new maybe_mock_1.MaybeMock(connection);
}
async start(botId) {
const url = `${this.apiBase}/agents/${botId}/sessions`;
const body = {
externalSessionKey: (0, node_crypto_1.randomUUID)(),
instanceConfig: {
endpoint: this.instanceUrl,
},
streamingCapabilities: {
chunkTypes: ['Text'],
},
bypassUser: true,
};
try {
return await this.maybeMock.request('POST', url, body);
}
catch (err) {
throw core_1.SfError.wrap(err);
}
}
async send(sessionId, message) {
const url = `${this.apiBase}/sessions/${sessionId}/messages`;
const body = {
message: {
// https://developer.salesforce.com/docs/einstein/genai/guide/agent-api-examples.html#send-synchronous-messages
// > A number that you provide to represent the sequence ID. Increase this number for each subsequent message in this session.
sequenceId: Date.now(),
type: 'Text',
text: message,
},
variables: [],
};
try {
return await this.maybeMock.request('POST', url, body);
}
catch (err) {
throw core_1.SfError.wrap(err);
}
}
async end(sessionId, reason) {
const url = `${this.apiBase}/sessions/${sessionId}`;
try {
// https://developer.salesforce.com/docs/einstein/genai/guide/agent-api-examples.html#end-session
return await this.maybeMock.request('DELETE', url, undefined, {
'x-session-end-reason': reason,
});
}
catch (err) {
throw core_1.SfError.wrap(err);
}
}
// Get the status of the Agent API (UP | DOWN)
async status() {
const base = 'https://test.api.salesforce.com';
const url = `${base}/einstein/ai-agent/v1/status`;
try {
return await this.maybeMock.request('GET', url);
}
catch (err) {
throw core_1.SfError.wrap(err);
}
}
}
exports.AgentPreview = AgentPreview;
//# sourceMappingURL=agentPreview.js.map