gotohuman
Version:
gotoHuman - The easiest way to review AI‑generated content, give approvals, or provide input to your AI workflows.
300 lines (297 loc) • 9.12 kB
JavaScript
var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
for (var prop in b || (b = {}))
if (__hasOwnProp.call(b, prop))
__defNormalProp(a, prop, b[prop]);
if (__getOwnPropSymbols)
for (var prop of __getOwnPropSymbols(b)) {
if (__propIsEnum.call(b, prop))
__defNormalProp(a, prop, b[prop]);
}
return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __async = (__this, __arguments, generator) => {
return new Promise((resolve, reject) => {
var fulfilled = (value) => {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
};
var rejected = (value) => {
try {
step(generator.throw(value));
} catch (e) {
reject(e);
}
};
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
step((generator = generator.apply(__this, __arguments)).next());
});
};
// package.json
var require_package = __commonJS({
"package.json"(exports, module) {
module.exports = {
name: "gotohuman",
version: "0.2.8",
description: "gotoHuman - The easiest way to review AI\u2011generated content, give approvals, or provide input to your AI workflows.",
main: "./dist/index.js",
module: "./dist/index.mjs",
types: "./dist/index.d.ts",
scripts: {
clean: "shx rm -rf ./dist",
build: "npm run clean && tsup src/index.ts --format esm,cjs --dts",
prepack: "npm run build"
},
files: [
"dist/**/*",
"LICENSE"
],
exports: {
".": {
import: {
types: "./dist/index.d.ts",
default: "./dist/index.mjs"
},
require: {
types: "./dist/index.d.ts",
default: "./dist/index.js"
}
}
},
keywords: [
"gotohuman",
"ai",
"ai agents",
"ai automation",
"ai workflow automation",
"llm automation",
"ai orchestration",
"human-in-the-loop",
"form builder"
],
homepage: "https://www.gotohuman.com",
repository: "github:gotohuman/gotohuman-js-sdk",
author: "Till Simon <till@gotohuman.com>",
license: "MIT",
bugs: {
url: "https://github.com/gotohuman/gotohuman-js-sdk/issues",
email: "hello@gotohuman.com"
},
devDependencies: {
"@types/node": "^22.13.10",
shx: "^0.3.4",
tsup: "^8.3.5",
typescript: "^5.6.3"
}
};
}
});
// src/index.ts
var Review = class {
constructor(formId, apiKey, baseUrl, fetchImpl, origin, originV) {
this.formId = formId;
this.apiKey = apiKey;
this.baseUrl = baseUrl;
this.fetchImpl = fetchImpl;
this.origin = origin;
this.originV = originV;
this.fields = {};
this.meta = {};
}
/**
* Add a field value to the review
*/
addFieldData(fieldName, value) {
if (value)
this.fields[fieldName] = value;
return this;
}
/**
* Set multiple field values at once
*/
setFieldsData(fields) {
if (fields)
this.fields = __spreadValues(__spreadValues({}, this.fields), fields);
return this;
}
/**
* Clear all fields in the current review
*/
clearFieldData() {
this.fields = {};
return this;
}
/**
* Add a field to the meta data
*/
addMetaData(attribute, value) {
if (value)
this.meta[attribute] = value;
return this;
}
/**
* Set multiple meta field values at once
*/
setMetaData(fields) {
if (fields)
this.meta = __spreadValues(__spreadValues({}, this.meta), fields);
return this;
}
/**
* Assign the review request to specific users
*/
assignToUsers(userEmails) {
if (userEmails.length > 0)
this.assignTo = userEmails;
return this;
}
/**
* Assign the review request to specific user groups
*/
assignToUserGroups(groupIds) {
if (groupIds.length > 0)
this.assignToGroups = groupIds;
return this;
}
/**
* Send the review request to the API
*/
sendRequest() {
return __async(this, null, function* () {
try {
const response = yield this.fetchImpl(`${this.baseUrl}/requestReview`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": `${this.apiKey}`
},
body: JSON.stringify(__spreadProps(__spreadValues(__spreadValues({
formId: this.formId,
fields: this.fields,
meta: this.meta
}, this.assignTo && { assignTo: this.assignTo }), this.assignToGroups && { assignToGroups: this.assignToGroups }), {
millis: Date.now(),
origin: this.origin,
originV: this.originV
}))
});
if (!response.ok) {
const errorMsg = yield response.text();
throw new Error(`${response.status}: ${errorMsg || response.statusText}`);
}
return response.json();
} catch (error) {
throw new Error(`gotoHuman API request failed: ${error}`);
}
});
}
};
var GotoHuman = class _GotoHuman {
constructor(params) {
const apiKey = (params == null ? void 0 : params.apiKey) || _GotoHuman.getApiKeyFromEnv();
this.apiKey = apiKey || "";
if (!this.apiKey) {
throw new Error("API key is required. Provide it in params or set the GOTOHUMAN_API_KEY environment variable.");
}
this.baseUrl = (params == null ? void 0 : params.baseUrl) || _GotoHuman.getBaseUrlFromEnv() || "https://api.gotohuman.com";
this.fetchImpl = (params == null ? void 0 : params.fetch) || globalThis.fetch;
this.origin = (params == null ? void 0 : params.origin) || "ts-sdk";
this.originV = (params == null ? void 0 : params.originV) || require_package().version;
}
/**
* Initialize a new review with a form ID
*/
createReview(formId) {
if (!formId) {
throw new Error("Please pass a form ID");
}
return new Review(formId, this.apiKey, this.baseUrl, this.fetchImpl, this.origin, this.originV);
}
/**
* Fetch all available review forms
*/
fetchReviewForms() {
return __async(this, null, function* () {
try {
const response = yield this.fetchImpl(`${this.baseUrl}/fetchReviewForms?millis=${Date.now()}&origin=${this.origin}&originV=${this.originV}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey
}
});
if (!response.ok) {
const errorMsg = yield response.text();
throw new Error(`${response.status}: ${errorMsg || response.statusText}`);
}
return response.json();
} catch (error) {
throw new Error(`Failed to fetch review forms: ${error}`);
}
});
}
/**
* Fetch the schema for a specific form's fields
*/
fetchSchemaForFormFields(formId) {
return __async(this, null, function* () {
if (!formId) {
throw new Error("Please pass a form ID");
}
try {
const response = yield this.fetchImpl(`${this.baseUrl}/fetchSchemaForFormFields?formId=${formId}&millis=${Date.now()}&origin=${this.origin}&originV=${this.originV}`, {
method: "GET",
headers: {
"Content-Type": "application/json",
"x-api-key": this.apiKey
}
});
if (!response.ok) {
const errorMsg = yield response.text();
throw new Error(`${response.status}: ${errorMsg || response.statusText}`);
}
return response.json();
} catch (error) {
throw new Error(`Failed to fetch form field schema: ${error}`);
}
});
}
/**
* Retrieves the base URL from the environment variable.
* @returns The base URL if set, otherwise undefined.
*/
static getBaseUrlFromEnv() {
if (typeof process !== "undefined" && process.env && process.env.GOTOHUMAN_BASE_URL) {
return process.env.GOTOHUMAN_BASE_URL;
}
return void 0;
}
/**
* Retrieves the API key from the environment variable.
* @returns The API key if set, otherwise undefined.
*/
static getApiKeyFromEnv() {
if (typeof process !== "undefined" && process.env && process.env.GOTOHUMAN_API_KEY) {
return process.env.GOTOHUMAN_API_KEY;
}
return void 0;
}
};
export {
GotoHuman,
Review
};