@future-agi/sdk
Version:
We help GenAI teams maintain high-accuracy for their Models in production.
308 lines • 16.4 kB
JavaScript
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 __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PromptLabels = void 0;
exports.setDefaultVersion = setDefaultVersion;
exports.getTemplateLabels = getTemplateLabels;
exports.assignLabelToTemplateVersion = assignLabelToTemplateVersion;
exports.removeLabelFromTemplateVersion = removeLabelFromTemplateVersion;
const auth_1 = require("../api/auth");
const types_1 = require("../api/types");
const routes_1 = require("../utils/routes");
const errors_1 = require("../utils/errors");
class PromptLabels {
constructor(prompt) {
this.prompt = prompt;
}
list() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const res = (yield this.prompt.request({
method: types_1.HttpMethod.GET,
url: `${this.prompt.baseUrl}/${routes_1.Routes.prompt_labels}`,
}));
return ((_a = res.data) !== null && _a !== void 0 ? _a : res);
});
}
getByName(name) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const data = yield this.list();
const items = (_a = data.results) !== null && _a !== void 0 ? _a : data;
if (!Array.isArray(items))
return undefined;
return items.find((it) => { var _a; return String((_a = it === null || it === void 0 ? void 0 : it.name) !== null && _a !== void 0 ? _a : '').toLowerCase() === name.toLowerCase(); });
});
}
create(name) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const res = (yield this.prompt.request({
method: types_1.HttpMethod.POST,
url: `${this.prompt.baseUrl}/${routes_1.Routes.prompt_labels}`,
json: { name, type: 'custom' },
}));
return ((_a = res.data) !== null && _a !== void 0 ? _a : res);
});
}
assign(name, version) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h;
// Prevent assignment to drafts
if (!version && (yield ((_b = (_a = this.prompt)._currentVersionIsDraft) === null || _b === void 0 ? void 0 : _b.call(_a)))) {
this.prompt._pendingLabel = name;
return { detail: 'Label will be assigned after commit', queued: true };
}
if (version && version === ((_c = this.prompt.template) === null || _c === void 0 ? void 0 : _c.version) && (yield ((_e = (_d = this.prompt)._currentVersionIsDraft) === null || _e === void 0 ? void 0 : _e.call(_d)))) {
this.prompt._pendingLabel = name;
return { detail: 'Label will be assigned after commit', queued: true };
}
const labelObj = yield this.getByName(name);
if (!(labelObj === null || labelObj === void 0 ? void 0 : labelObj.id))
throw new errors_1.SDKException(`Label '${name}' not found. Create it first, then assign.`);
const tplId = (_f = this.prompt.template) === null || _f === void 0 ? void 0 : _f.id;
const ver = version !== null && version !== void 0 ? version : (_g = this.prompt.template) === null || _g === void 0 ? void 0 : _g.version;
if (!tplId || !ver)
throw new errors_1.SDKException('template_id and version are required');
const res = (yield this.prompt.request({
method: types_1.HttpMethod.POST,
url: `${this.prompt.baseUrl}/${routes_1.Routes.prompt_label_assign_by_id
.replace('{template_id}', tplId)
.replace('{label_id}', String(labelObj.id))}`,
json: { version: ver },
}));
return ((_h = res.data) !== null && _h !== void 0 ? _h : res);
});
}
remove(name, version) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
if (!((_a = this.prompt.template) === null || _a === void 0 ? void 0 : _a.id))
throw new errors_1.SDKException('Template must be loaded');
const ver = version !== null && version !== void 0 ? version : (_b = this.prompt.template) === null || _b === void 0 ? void 0 : _b.version;
if (!ver)
throw new errors_1.SDKException('Version must be specified or available on the client');
return this.removeFromVersion(name, ver);
});
}
removeFromCurrentVersion(name) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (!((_a = this.prompt.template) === null || _a === void 0 ? void 0 : _a.id) || !this.prompt.template.version) {
throw new errors_1.SDKException('Template and current version must be loaded');
}
return this.removeFromVersion(name, this.prompt.template.version);
});
}
removeFromVersion(name, version) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const versionId = yield this._getVersionIdByName(version);
if (!versionId)
throw new errors_1.SDKException(`Could not resolve version_id for version '${version}'`);
const item = yield this.getByName(name);
if (!(item === null || item === void 0 ? void 0 : item.id))
throw new errors_1.SDKException(`Label '${name}' not found`);
const res = (yield this.prompt.request({
method: types_1.HttpMethod.POST,
url: `${this.prompt.baseUrl}/${routes_1.Routes.prompt_label_remove}`,
json: { label_id: String(item.id), version_id: versionId },
}));
return ((_a = res.data) !== null && _a !== void 0 ? _a : res);
});
}
_getVersionIdByName(versionName) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
const res = (yield this.prompt.request({
method: types_1.HttpMethod.GET,
url: `${this.prompt.baseUrl}/${routes_1.Routes.get_template_version_history}`,
params: { template_id: (_a = this.prompt.template) === null || _a === void 0 ? void 0 : _a.id },
}));
const history = (_c = ((_b = res.data) !== null && _b !== void 0 ? _b : res).results) !== null && _c !== void 0 ? _c : [];
for (const e of history) {
if (String(e.templateVersion) === versionName) {
for (const key of ['id', 'versionId', 'executionId']) {
if (e[key])
return String(e[key]);
}
}
}
return undefined;
});
}
}
exports.PromptLabels = PromptLabels;
function setDefaultVersion(templateName_1, version_1) {
return __awaiter(this, arguments, void 0, function* (templateName, version, options = {}) {
var _a;
const client = new auth_1.APIKeyAuth(options);
try {
const res = (yield client.request({
method: types_1.HttpMethod.POST,
url: `${client.baseUrl}/${routes_1.Routes.prompt_label_set_default}`,
json: { template_name: templateName, version },
}));
return ((_a = res.data) !== null && _a !== void 0 ? _a : res);
}
finally {
yield client.close();
}
});
}
function getTemplateLabels() {
return __awaiter(this, arguments, void 0, function* (_a = {}) {
var _b;
var { template_name, template_id } = _a, options = __rest(_a, ["template_name", "template_id"]);
if (!template_name && !template_id)
throw new errors_1.SDKException('template_name or template_id is required');
const client = new auth_1.APIKeyAuth(options);
try {
const params = template_name ? { template_name } : { template_id };
const res = (yield client.request({
method: types_1.HttpMethod.GET,
url: `${client.baseUrl}/${routes_1.Routes.prompt_label_template_labels}`,
params,
}));
return ((_b = res.data) !== null && _b !== void 0 ? _b : res);
}
finally {
yield client.close();
}
});
}
function assignLabelToTemplateVersion(templateName_1, version_1, label_1) {
return __awaiter(this, arguments, void 0, function* (templateName, version, label, options = {}) {
var _a, _b, _c, _d, _e, _f, _g, _h;
const client = new auth_1.APIKeyAuth(options);
try {
// Resolve template_id via search
const search = (yield client.request({
method: types_1.HttpMethod.GET,
url: `${client.baseUrl}/${routes_1.Routes.get_template_id_by_name}`,
params: { search: templateName },
}));
const results = (_b = ((_a = search.data) !== null && _a !== void 0 ? _a : search).results) !== null && _b !== void 0 ? _b : [];
const found = results.find((it) => (it === null || it === void 0 ? void 0 : it.name) === templateName);
if (!(found === null || found === void 0 ? void 0 : found.id))
throw new errors_1.SDKException(`No template found with name: ${templateName}`);
const templateId = String(found.id);
// Verify version exists and is not draft
const hist = (yield client.request({
method: types_1.HttpMethod.GET,
url: `${client.baseUrl}/${routes_1.Routes.get_template_version_history}`,
params: { template_id: templateId },
}));
const history = (_d = ((_c = hist.data) !== null && _c !== void 0 ? _c : hist).results) !== null && _d !== void 0 ? _d : [];
const entry = history.find((e) => String(e.templateVersion) === version);
if (!entry)
throw new errors_1.SDKException(`No version '${version}' found for template '${templateName}'`);
if (entry.isDraft === true)
throw new errors_1.SDKException('Cannot assign label to a draft version. Commit first.');
// Resolve label id by name
const labelsResp = (yield client.request({
method: types_1.HttpMethod.GET,
url: `${client.baseUrl}/${routes_1.Routes.prompt_labels}`,
}));
const items = (_f = ((_e = labelsResp.data) !== null && _e !== void 0 ? _e : labelsResp).results) !== null && _f !== void 0 ? _f : ((_g = labelsResp.data) !== null && _g !== void 0 ? _g : labelsResp);
const labelObj = Array.isArray(items)
? items.find((it) => { var _a; return String((_a = it === null || it === void 0 ? void 0 : it.name) !== null && _a !== void 0 ? _a : '').toLowerCase() === label.toLowerCase(); })
: undefined;
if (!(labelObj === null || labelObj === void 0 ? void 0 : labelObj.id))
throw new errors_1.SDKException(`Label '${label}' not found. Create it first, then assign.`);
// Assign
const res = (yield client.request({
method: types_1.HttpMethod.POST,
url: `${client.baseUrl}/${routes_1.Routes.prompt_label_assign_by_id
.replace('{template_id}', templateId)
.replace('{label_id}', String(labelObj.id))}`,
json: { version },
}));
return ((_h = res.data) !== null && _h !== void 0 ? _h : res);
}
finally {
yield client.close();
}
});
}
function removeLabelFromTemplateVersion(templateName_1, version_1, label_1) {
return __awaiter(this, arguments, void 0, function* (templateName, version, label, options = {}) {
var _a, _b, _c, _d, _e, _f, _g, _h;
const client = new auth_1.APIKeyAuth(options);
try {
// Resolve template_id via search
const search = (yield client.request({
method: types_1.HttpMethod.GET,
url: `${client.baseUrl}/${routes_1.Routes.get_template_id_by_name}`,
params: { search: templateName },
}));
const results = (_b = ((_a = search.data) !== null && _a !== void 0 ? _a : search).results) !== null && _b !== void 0 ? _b : [];
const found = results.find((it) => (it === null || it === void 0 ? void 0 : it.name) === templateName);
if (!(found === null || found === void 0 ? void 0 : found.id))
throw new errors_1.SDKException(`No template found with name: ${templateName}`);
const templateId = String(found.id);
// Resolve version_id via history
const hist = (yield client.request({
method: types_1.HttpMethod.GET,
url: `${client.baseUrl}/${routes_1.Routes.get_template_version_history}`,
params: { template_id: templateId },
}));
const history = (_d = ((_c = hist.data) !== null && _c !== void 0 ? _c : hist).results) !== null && _d !== void 0 ? _d : [];
let versionId;
for (const e of history) {
if (String(e.templateVersion) === version) {
for (const key of ['id', 'versionId', 'executionId']) {
if (e[key]) {
versionId = String(e[key]);
break;
}
}
break;
}
}
if (!versionId)
throw new errors_1.SDKException(`No version '${version}' found for template '${templateName}'`);
// Resolve label id by name
const labelsResp = (yield client.request({
method: types_1.HttpMethod.GET,
url: `${client.baseUrl}/${routes_1.Routes.prompt_labels}`,
}));
const items = (_f = ((_e = labelsResp.data) !== null && _e !== void 0 ? _e : labelsResp).results) !== null && _f !== void 0 ? _f : ((_g = labelsResp.data) !== null && _g !== void 0 ? _g : labelsResp);
const labelObj = Array.isArray(items)
? items.find((it) => { var _a; return String((_a = it === null || it === void 0 ? void 0 : it.name) !== null && _a !== void 0 ? _a : '').toLowerCase() === label.toLowerCase(); })
: undefined;
if (!(labelObj === null || labelObj === void 0 ? void 0 : labelObj.id))
throw new errors_1.SDKException(`Label '${label}' not found`);
// Remove
const res = (yield client.request({
method: types_1.HttpMethod.POST,
url: `${client.baseUrl}/${routes_1.Routes.prompt_label_remove}`,
json: { label_id: String(labelObj.id), version_id: versionId },
}));
return ((_h = res.data) !== null && _h !== void 0 ? _h : res);
}
finally {
yield client.close();
}
});
}
//# sourceMappingURL=labels.js.map
;