plazbot
Version:
Official Plazbot SDK for creating AI agents for WhatsApp, portals, and developers.
190 lines (189 loc) • 7.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Agent = void 0;
const http_1 = require("./http");
class Agent extends http_1.PlazbotHttp {
constructor(options) {
super(options);
}
// ── Agent CRUD ──────────────────────────────────────────────────────
async addAgent(config) {
const response = await this.http.post('/api/agent/add', config);
this.throwIfError(response, 'Error creating agent');
if (!response.data.success) {
throw new Error(`Agent creation failed: ${response.data.message ?? 'Unknown error.'}`);
}
return {
agentId: response.data.agentId,
success: true,
message: `Agent created successfully. Agent ID: ${response.data.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 },
});
this.throwIfError(response, 'Error updating agent');
if (!response.data.success) {
throw new Error(`Agent update failed: ${response.data.message ?? 'Unknown error.'}`);
}
return {
success: true,
message: `Agent ${agentId} updated successfully.`,
};
}
async deleteAgent(params) {
const response = await this.http.delete('/api/agent/delete', {
params: { id: params.id, workspaceId: this.workspaceId },
});
this.throwIfError(response, 'Error deleting agent');
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 },
});
this.throwIfError(response, 'Error retrieving agent');
return response.data;
}
async getAgents() {
const response = await this.http.get('/api/agent/get-agents', {
params: { workspaceId: this.workspaceId },
});
this.throwIfError(response, 'Error retrieving agents');
return response.data.agent;
}
async copyAgent(params) {
const response = await this.http.post('/api/agent/copy-agent', null, {
params: { id: params.id, workspaceId: this.workspaceId },
});
this.throwIfError(response, 'Error copying agent');
return response.data;
}
async getAgentByChannel(params) {
const response = await this.http.get('/api/agent/get-by-channel', {
params: { channel: params.channel, key: params.key, workspaceId: this.workspaceId },
});
this.throwIfError(response, 'Error retrieving agent by channel');
return response.data;
}
// ── Widget ──────────────────────────────────────────────────────────
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);
this.throwIfError(response, 'Error enabling widget');
return response.data;
}
// ── Chat / Messaging ───────────────────────────────────────────────
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);
this.throwIfError(response, 'Error processing message');
return response.data;
}
// ── Knowledge Base / Files ─────────────────────────────────────────
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);
this.throwIfError(response, 'Error uploading file');
return response.data;
}
async validateFile(params) {
const response = await this.http.get('/api/agent/validate-file', {
params: { WorkspaceId: this.workspaceId, FileId: params.fileId },
});
this.throwIfError(response, 'Error validating file');
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 });
this.throwIfError(response, 'Error deleting file');
return response.data;
}
// ── Quick Config ───────────────────────────────────────────────────
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,
});
}
// ── Debug ──────────────────────────────────────────────────────────
async getDebugLogs(agentId) {
const response = await this.http.get('/api/agent/debug-logs', {
params: { agentId, workspaceId: this.workspaceId },
});
this.throwIfError(response, 'Error retrieving debug logs');
return response.data;
}
// ── AI Utilities ───────────────────────────────────────────────────
async improvePrompt(text) {
const response = await this.http.post('/api/ai/improve-prompt', {
text,
workspaceId: this.workspaceId,
});
this.throwIfError(response, 'Error improving prompt');
return response.data;
}
}
exports.Agent = Agent;