86 lines
2.5 KiB
JavaScript
86 lines
2.5 KiB
JavaScript
const axios = require("axios");
|
|
|
|
/**
|
|
* @typedef {Object} JLINCConfig
|
|
* @property {string} [dataStoreApiUrl]
|
|
* @property {string} [dataStoreApiKey]
|
|
* @property {string} [archiveApiUrl]
|
|
* @property {string} [archiveApiKey]
|
|
* @property {string} [systemPrefix]
|
|
* @property {string|null} [agreementId]
|
|
* @property {boolean} [debug]
|
|
*/
|
|
|
|
/**
|
|
* @param {any} - Authorized
|
|
* @param {any|null} - NotAuthorized
|
|
* @returns {string} - The description
|
|
*/
|
|
function getName(authorized, unauthorized) {
|
|
return unauthorized
|
|
? `${authorized.name}-or-${unauthorized.name}`
|
|
: `authorized-${authorized.name}`
|
|
}
|
|
|
|
/**
|
|
* @param {any} - Authorized
|
|
* @param {any|null} - NotAuthorized
|
|
* @returns {string} - The description
|
|
*/
|
|
function getDescription(authorized, unauthorized, type) {
|
|
return unauthorized ? `
|
|
This is an authorization router ${type}. It decides whether
|
|
to route input to an "authorized" or "not authorized" ${type}.
|
|
|
|
Authorized ${type}: ${authorized.name}
|
|
Description: ${authorized.description}
|
|
|
|
Not Authorized ${type}: ${unauthorized.name}
|
|
Description: ${unauthorized.description}
|
|
|
|
The LLM can assume both ${type}s are available, but JLINC Auth will
|
|
only allow the Authorized ${type} to be called if you are authorized.
|
|
` : `
|
|
This is an authorization router ${type}. It decides whether
|
|
to route input to an "authorized" ${type}.
|
|
|
|
Authorized ${type}: ${authorized.name}
|
|
Description: ${authorized.description}
|
|
|
|
The LLM can assume the ${type} is available, but JLINC Auth will
|
|
only allow the Authorized ${type} to be called if you are authorized.
|
|
`
|
|
}
|
|
|
|
function getLogName(target) {
|
|
if (target.name) return target.name;
|
|
if (target.lc_kwargs?.bound?.model) return target.lc_kwargs.bound.model;
|
|
return 'no-name';
|
|
}
|
|
|
|
/**
|
|
* @param {any} settings
|
|
* @param {any} input
|
|
* @returns {Promise<any>} - The result of invoking the selected tool.
|
|
*/
|
|
async function authInvoke(settings, input) {
|
|
const authorized = await settings.jlincAuthDecision.getAuth();
|
|
let selectedTarget = settings.targetNotAuthorized
|
|
if (authorized) {
|
|
selectedTarget = settings.targetAuthorized
|
|
}
|
|
if (!selectedTarget)
|
|
throw new Error("No valid resource available");
|
|
if (settings.config.debug)
|
|
console.log(`[JLINCAuth] Invoking ${getLogName(selectedTarget)} (${settings.authType}|${authorized})`);
|
|
return await selectedTarget.invoke(input, {
|
|
callbacks: [settings.tracer],
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
authInvoke,
|
|
getDescription,
|
|
getName,
|
|
};
|