agentlang
Version:
The easiest way to build the most reliable AI agents - enterprise-grade teams of AI agents that collaborate with each other and humans
78 lines • 3.14 kB
JavaScript
import { isString } from './util.js';
const Integrations = new Map();
const IntegManagerModel = 'integmanager.core';
export async function prepareIntegrations(integManagerHost, username, password, integConfigObj) {
const integConfig = new Map(Object.entries(integConfigObj));
const standardHeaders = await loginToIntegManager(integManagerHost, username, password);
const keys = [...integConfig.keys()];
for (let i = 0; i < keys.length; ++i) {
const configName = keys[i];
const configPath = integConfig.get(configName);
if (configPath) {
const apiUrl = mkApiUrl(integManagerHost, configPath);
try {
const response = await fetch(apiUrl, {
method: 'GET',
headers: standardHeaders,
});
if (!response.ok) {
console.error(`Failed to fetch integration for ${configPath}, HTTP error! status: ${response.status} ${response.text} ${response.statusText}`);
continue;
}
const data = await response.json();
if (data.length > 0) {
const inst = data[0].config;
if (inst.type == 'custom' && isString(inst.parameter)) {
inst.parameter = new Map(Object.entries(JSON.parse(inst.parameter)));
}
Integrations.set(configName, inst);
}
else {
console.error(`Integration not found for ${configPath}`);
}
}
catch (error) {
console.error(`Error fetching integration for ${configPath}:`, error.message);
}
}
}
}
async function loginToIntegManager(host, username, password) {
const defaultHdr = { 'Content-Type': 'application/json' };
if (username && password && username.length > 0) {
const apiUrl = `${host}/agentlang.auth/login`;
const data = { email: username, password: password };
const response = await fetch(apiUrl, {
method: 'POST',
headers: defaultHdr,
body: JSON.stringify(data),
});
if (!response.ok) {
throw new Error(`Failed to login to integration-manager. HTTP error! status: ${response.status}`);
}
const responseData = await response.json();
return {
Authorization: `Bearer ${responseData.id_token}`,
'Content-Type': 'application/json',
};
}
else {
return defaultHdr;
}
}
function mkApiUrl(integManagerHost, configPath) {
const parts = configPath.split('/');
const integId = parts[0];
const configId = parts[1];
return `${integManagerHost}/${IntegManagerModel}/integration/${integId}/integrationConfig/config/${configId}`;
}
export function getIntegrationConfig(name, configName) {
const config = Integrations.get(name);
if (config) {
return config.parameter.get(configName);
}
else {
return undefined;
}
}
//# sourceMappingURL=integrations.js.map