UNPKG

fi_sdk_js

Version:

Feedback Intelligence SKD

205 lines (204 loc) 9.53 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()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Feedback = exports.Context = exports.Message = exports.FeedbackIntelligenceSDK = void 0; var FeedbackIntelligenceSDK = /** @class */ (function () { function FeedbackIntelligenceSDK(apiKey, baseUrl) { this.baseUrl = baseUrl || 'https://api.feedbackintelligence.ai'; this.headers = { 'Content-Type': 'application/json', }; if (apiKey) { this.headers['Authorization'] = "Bearer ".concat(apiKey); } } FeedbackIntelligenceSDK.prototype.addContext = function (projectId_1, context_1) { return __awaiter(this, arguments, void 0, function (projectId, context, contextId) { var url, payload; if (contextId === void 0) { contextId = null; } return __generator(this, function (_a) { url = "".concat(this.baseUrl, "/data/").concat(projectId, "/context/add"); payload = { context: context }; if (contextId !== null) { payload['id'] = contextId; } return [2 /*return*/, this._post(url, payload)]; }); }); }; FeedbackIntelligenceSDK.prototype.addChat = function (projectId_1, chatId_1, messages_1) { return __awaiter(this, arguments, void 0, function (projectId, chatId, messages, userId, version) { var url, payload; if (userId === void 0) { userId = null; } if (version === void 0) { version = null; } return __generator(this, function (_a) { url = "".concat(this.baseUrl, "/data/").concat(projectId, "/chat/add"); payload = { chat_id: chatId, messages: messages.map(function (msg) { return msg.toDict(); }), }; if (userId !== null) payload['user_id'] = userId; if (version !== null) payload['version'] = version; console.log(payload); return [2 /*return*/, this._post(url, payload)]; }); }); }; FeedbackIntelligenceSDK.prototype.addFeedback = function (projectId_1, message_1, source_1) { return __awaiter(this, arguments, void 0, function (projectId, message, source, userId, chatId, date) { var url, payload; if (userId === void 0) { userId = null; } if (chatId === void 0) { chatId = null; } if (date === void 0) { date = null; } return __generator(this, function (_a) { url = "".concat(this.baseUrl, "/data/").concat(projectId, "/feedback/add"); payload = { message: message, source: source, }; if (userId !== null) payload['user_id'] = userId; if (chatId !== null) payload['chat_id'] = chatId; if (date !== null) payload['date'] = date; return [2 /*return*/, this._post(url, payload)]; }); }); }; // Private method to handle POST requests FeedbackIntelligenceSDK.prototype._post = function (url, payload) { return __awaiter(this, void 0, void 0, function () { var response; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, fetch(url, { method: 'POST', headers: this.headers, body: JSON.stringify(payload), })]; case 1: response = _a.sent(); if (!response.ok) { console.log(response); throw new Error("HTTP error! status: ".concat(response.status)); } return [2 /*return*/, response.json()]; } }); }); }; return FeedbackIntelligenceSDK; }()); exports.FeedbackIntelligenceSDK = FeedbackIntelligenceSDK; // Message Class var Message = /** @class */ (function () { function Message(_a) { var role = _a.role, text = _a.text, _b = _a.prompt, prompt = _b === void 0 ? null : _b, _c = _a.date, date = _c === void 0 ? null : _c, _d = _a.context, context = _d === void 0 ? null : _d, _e = _a.feedback, feedback = _e === void 0 ? null : _e; this.role = role; this.text = text; this.prompt = prompt; this.date = date; this.context = context; this.feedback = feedback; } Message.prototype.toDict = function () { var payload = { role: this.role, text: this.text, }; if (this.prompt !== null) payload["prompt"] = this.prompt; if (this.date !== null) payload["date"] = this.date; if (this.context !== null) payload["context"] = this.context.toDict(); if (this.feedback !== null) payload["feedback"] = this.feedback.toDict(); return payload; }; return Message; }()); exports.Message = Message; // Context Class var Context = /** @class */ (function () { function Context(_a) { var _b = _a === void 0 ? {} : _a, _c = _b.contextId, contextId = _c === void 0 ? null : _c, _d = _b.text, text = _d === void 0 ? null : _d; if (contextId === null && text === null) { throw new Error('Either contextId or text must be provided'); } this.contextId = contextId; this.text = text; } Context.prototype.toDict = function () { var data = {}; if (this.contextId !== null) data['context_id'] = this.contextId; if (this.text !== null) data['text'] = this.text; return data; }; return Context; }()); exports.Context = Context; // Feedback Class var Feedback = /** @class */ (function () { function Feedback(_a) { var _b = _a === void 0 ? {} : _a, _c = _b.thumbsUp, thumbsUp = _c === void 0 ? null : _c, _d = _b.rating, rating = _d === void 0 ? null : _d, _e = _b.message, message = _e === void 0 ? null : _e; this.thumbsUp = null; this.rating = null; this.message = null; this.thumbsUp = thumbsUp; this.rating = rating; this.message = message; } Feedback.prototype.toDict = function () { var data = {}; if (this.thumbsUp !== null) data['thumbs_up'] = this.thumbsUp; if (this.rating !== null) data['rating'] = this.rating; if (this.message !== null) data['message'] = this.message; return data; }; return Feedback; }()); exports.Feedback = Feedback;