hugbot
Version:
Chatbot maker for HuggingFace Inference API and other AI API providers and backends.
109 lines • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HugBotProxy = void 0;
/**
* Stringly typed bot params for runtime type-checking when using setParams setter.
*/
const availableParams = new Set([
"systemPrompt",
"responseAffirmation",
"contextWindow",
"topK",
"topP",
"temperature",
"repetitionPenalty",
"maxNewTokens",
"maxTime",
"doSample",
]);
/**
* Traverses bot components to set provided params if their names and types match existing properties.
*/
const setParams = (params, obj) => {
Object.entries(obj).forEach(([key, value]) => {
if (params.hasOwnProperty(key) && typeof params[key] === typeof value)
obj[key] = params[key];
if (typeof value === "object" && value !== null)
setParams(params, value);
});
};
/**
* This handles bot access and input validation.
*/
const HugBotProxy = (bot) => {
return Object.seal(new Proxy(bot, {
get: (bot, prop) => {
switch (prop) {
case "respondTo":
return async (prompt, apiToken) => {
try {
return await bot.respondTo(bot, prompt, apiToken);
}
catch (what) {
console.error(what.message);
return what.message;
}
};
case "onResponse":
return (...cb) => {
try {
bot.IObuffer.onResponse(...cb);
}
catch (what) {
console.error(what.message);
}
};
case "pushMessage":
return (msg) => {
try {
bot.IObuffer.pushMessage(msg);
}
catch (what) {
console.error(what.message);
}
};
case "setParams":
return (params) => {
for (const param in params) {
if (!availableParams.has(param)) {
console.error(`Can't set param: ${param}`);
return;
}
}
setParams(params, bot);
};
case "id":
return bot.id;
case "apiToken":
return async (apiToken) => {
var _a;
if (apiToken === null) {
(_a = bot.secretsHider) === null || _a === void 0 ? void 0 : _a.destroy();
return true;
}
else if (typeof apiToken !== "string" || !apiToken.length) {
console.error("Api key must be a string.");
return false;
}
else {
const res = await bot.secretsHider.set(apiToken);
return res;
}
};
default:
console.error(`Property ${prop.toString()} doesn't exist on HugBot.`);
return false;
}
},
set: (_, prop) => {
console.error(`Access denied. Can't assign ${prop.toString()} property.`);
return false;
},
deleteProperty: (_, prop) => {
console.error(`Access denied. Can't delete ${prop.toString()} property.`);
return false;
},
}));
};
exports.HugBotProxy = HugBotProxy;
//# sourceMappingURL=AbstractSingletonProxyFactoryBean.js.map