UNPKG

@butlerbot/sdk

Version:

The official ButlerBot SDK

129 lines (128 loc) 5.43 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Conversation = void 0; const eventsource_1 = require("eventsource"); const config_1 = require("../config"); class Conversation { constructor(config) { this.convoId = config.convoId; this.endpoint = (config.serverUrl || config_1.CONFIG.server) + (config.path || config_1.CONFIG.paths.conversation.v3.base); this.apiKey = config.apiKey; this.debug = config.debug || false; } // SETTERS /** Sets the endpoint where the request is sent to, appended to server URL */ setEndpoint(endpoint) { this.endpoint = endpoint; return this; } /** Sets the conversation ID, has to be an existing conversation ID, undefined otherwise */ setConvoId(convoId) { this.convoId = convoId; return this; } /** Sets the AI model used for the next interaction, e.g Claude-Sonnet, GPT-4 */ setModel(model) { this.options = Object.assign(Object.assign({}, this.options), { model }); return this; } /** Sets additional instructions for location-specific context */ setInstructions(instructions) { this.options = Object.assign(Object.assign({}, this.options), { instructions }); return this; } /** Sets the platform where the chat is occurring, used internally for logging - ignore in most contexts */ setPlatform(platform) { this.options = Object.assign(Object.assign({}, this.options), { platform }); return this; } /** Sets a custom personality configuration for the AI */ setPersonality(personality) { this.options = Object.assign(Object.assign({}, this.options), { personality }); return this; } // GETTERS /** Gets the current conversation ID */ getConvoId() { return this.convoId; } /** Gets the current endpoint */ getEndpoint() { return this.endpoint; } /** Gets the current options object */ getModel() { var _a; return (_a = this.options) === null || _a === void 0 ? void 0 : _a.model; } /** Gets the current location-specific instructions */ getInstructions() { var _a; return (_a = this.options) === null || _a === void 0 ? void 0 : _a.instructions; } /** Gets the current platform */ getPlatform() { var _a; return (_a = this.options) === null || _a === void 0 ? void 0 : _a.platform; } /** Gets the current personality configuration */ getPersonality() { var _a; return (_a = this.options) === null || _a === void 0 ? void 0 : _a.personality; } // LIFE CYCLE /** Sends a message into the conversation */ send(message, cb, options) { return __awaiter(this, void 0, void 0, function* () { const params = Object.assign(Object.assign({ message, api_key: this.apiKey }, this.options), options // overwrite convos for this call ); if (this.convoId) params.chatId = this.convoId; const paramQuery = new URLSearchParams(params).toString(); const url = `${this.endpoint}?${paramQuery}`; if (this.debug) { const debugUrl = new URL(url); const apiKey = debugUrl.searchParams.get('api_key'); if (apiKey) { const obscuredKey = apiKey.length > 6 ? `${apiKey.slice(0, 3)}...${apiKey.slice(-3)}` : '***'; debugUrl.searchParams.set('api_key', obscuredKey); } console.log(`[Requesting URL ${debugUrl.toString()}]`); } const sse = new eventsource_1.EventSource(url); sse.addEventListener("message", (event) => { const data = JSON.parse(event.data); const convoId = data.success ? data.data.convoId : undefined; if (convoId) this.convoId = convoId; // update convo id from response cb(data); if (data.data.quitStream) sse.close(); }); sse.addEventListener("error", (event) => { if (this.debug) console.warn(`[Stream Error: ${url}]`, event); cb({ success: false, data: { code: "STREAM_ERROR", error: "Stream error", message: event.message || "An error occurred while streaming", quitStream: true } }); sse.close(); }); }); } } exports.Conversation = Conversation;