plazbot
Version:
Official Plazbot SDK for creating AI agents for WhatsApp, portals, and developers.
229 lines (228 loc) • 8.18 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Agent = void 0;
const axios_1 = __importDefault(require("axios"));
class Agent {
constructor(options) {
this.workspaceId = options.workspaceId;
this.apiKey = options.apiKey;
const zone = options.zone ?? "LA";
if (zone !== "EU" && zone !== "LA") {
throw new Error("Invalid zone. Must be 'EU' or 'LA'.");
}
this.mcpUrl = options.customUrl
?? (zone === "EU" ? "https://apieu.plazbot.com" : "https://api.plazbot.com");
this.http = axios_1.default.create({
baseURL: this.mcpUrl,
headers: {
...(this.apiKey && { 'Authorization': `Bearer ${this.apiKey}` }),
'x-workspace-id': this.workspaceId
}
});
}
async addAgent(config) {
const response = await this.http.post('/api/agent/add', config);
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error creating agent from config: ${response.statusText}`);
}
if (!response.data.success) {
throw new Error(`Agent creation failed: ${response.data.message ?? 'Unknown error.'}`);
}
const agentId = response.data.agentId;
return {
agentId,
success: true,
message: `Agent created from config successfully. Agent ID: ${agentId}`
};
}
async updateAgent(agentId, config) {
if (!agentId) {
throw new Error("Agent ID is required to update an agent.");
}
const response = await this.http.post('/api/agent/update', config, {
headers: {
'x-agent-id': agentId
}
});
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error updating agent from config: ${response.statusText}`);
}
if (!response.data.success) {
throw new Error(`Agent update failed: ${response.data.message ?? 'Unknown error.'}`);
}
return {
success: true,
message: `Agent ${agentId} updated successfully from config.`
};
}
//////////////////////////////////////////////////////////////////////
async enableWidget(params) {
const body = {
workspaceId: this.workspaceId,
id: params.id,
enableWidget: params.enable
};
const response = await this.http.post('/api/agent/enable-widget', body);
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error enabling widget: ${response.statusText}`);
}
return response.data;
}
async deleteAgent(params) {
const response = await this.http.delete('/api/agent/delete', {
params: {
id: params.id,
workspaceId: this.workspaceId
}
});
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error deleting agent: ${response.statusText}`);
}
return {
success: true,
message: `Agent ${params.id} deleted successfully.`
};
}
async getAgentById(params) {
const response = await this.http.get('/api/agent/get-agent', {
params: {
id: params.id,
workspaceId: this.workspaceId
}
});
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error retrieving agent: ${response.statusText}`);
}
const data = response.data;
return response.data;
}
async getAgents() {
const response = await this.http.get('/api/agent/get-agents', {
params: {
workspaceId: this.workspaceId
}
});
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error retrieving agents: ${response.statusText}`);
}
const data = response.data;
return response.data.agent;
}
async onMessage(params) {
const body = {
agentId: params.agentId,
workspaceId: this.workspaceId,
question: params.question,
sessionId: params.sessionId,
file: params.file,
multipleAnswers: params.multipleAnswers
};
const response = await this.http.post('/api/agent/on-message', body);
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error al procesar el mensaje: ${response.statusText}`);
}
return response.data;
}
async addFile(params) {
const body = {
WorkspaceId: this.workspaceId,
FileUrl: params.fileUrl,
Reference: params.reference,
AgentId: params.agentId,
...(params.tags && { Tags: params.tags })
};
const response = await this.http.post('/api/agent/add-file', body, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.apiKey}`,
'x-workspace-id': this.workspaceId
}
});
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error uploading file: ${response.statusText}`);
}
return response.data;
}
async validateFile(params) {
const response = await this.http.get('/api/agent/validate-file', {
params: {
WorkspaceId: this.workspaceId,
FileId: params.fileId
},
headers: {
'x-workspace-id': this.workspaceId,
Authorization: `Bearer ${this.apiKey}`
}
});
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error validating file: ${response.statusText}`);
}
return response.data;
}
async deleteFile(params) {
const body = {
WorkspaceId: this.workspaceId,
AgentId: params.agentId,
FileId: params.fileId
};
const response = await this.http.delete('/api/agent/delete-file', {
data: body,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.apiKey}`,
'x-workspace-id': this.workspaceId
}
});
if (response.status < 200 || response.status >= 300) {
throw new Error(`Error deleting file: ${response.statusText}`);
}
return response.data;
}
// Métodos para configurar instrucciones, saludo, persona, fallbacks, reglas y tags del agente
async setInstructions(agentId, instructions) {
await this.http.post('/api/agent/set-instructions', {
agentId,
workspaceId: this.workspaceId,
...instructions
});
}
async setGreeting(agentId, greeting) {
await this.http.post('/api/agent/set-greeting', {
agentId,
workspaceId: this.workspaceId,
greeting
});
}
async setPersona(agentId, persona) {
await this.http.post('/api/agent/set-persona', {
agentId,
workspaceId: this.workspaceId,
...persona
});
}
async setFallbacks(agentId, fallbacks) {
await this.http.post('/api/agent/set-fallbacks', {
agentId,
workspaceId: this.workspaceId,
...fallbacks
});
}
async setRules(agentId, rules) {
await this.http.post('/api/agent/set-rules', {
agentId,
workspaceId: this.workspaceId,
...rules
});
}
async setTags(agentId, tags) {
await this.http.post('/api/agent/set-tags', {
agentId,
workspaceId: this.workspaceId,
tags
});
}
}
exports.Agent = Agent;