@future-agi/sdk
Version:
We help GenAI teams maintain high-accuracy for their Models in production.
348 lines • 11.9 kB
JavaScript
"use strict";
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModelProvider = exports.HttpMethod = exports.ProviderAPIKeyResponseHandler = exports.ProviderAPIKeyClient = exports.APIKeyManager = void 0;
exports.getAPIKeyManager = getAPIKeyManager;
exports.isSupportedProvider = isSupportedProvider;
exports.getSupportedProviders = getSupportedProviders;
const auth_1 = require("./auth");
const types_1 = require("./types");
Object.defineProperty(exports, "ModelProvider", { enumerable: true, get: function () { return types_1.ModelProvider; } });
Object.defineProperty(exports, "HttpMethod", { enumerable: true, get: function () { return types_1.HttpMethod; } });
const routes_1 = require("../utils/routes");
/**
* Response handler for API key operations
*/
class ProviderAPIKeyResponseHandler extends auth_1.ResponseHandler {
/**
* Parse successful API key response
*/
static _parseSuccess(response) {
var _a;
const data = response.data;
const method = (_a = response.config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase();
if (method === types_1.HttpMethod.POST) {
return {
success: true
};
}
else if (method === types_1.HttpMethod.GET) {
const results = data.results || data;
if (Array.isArray(results)) {
return results.map(apiKey => ({
provider: apiKey.provider,
key: apiKey.key
}));
}
return results;
}
else {
return data;
}
}
/**
* Handle API key operation errors
*/
static _handleError(response) {
var _a;
if (response.status >= 400) {
const message = ((_a = response.data) === null || _a === void 0 ? void 0 : _a.message) || response.statusText || 'API key operation failed';
throw new Error(`API Key Error [${response.status}]: ${message}`);
}
throw new Error('Unknown API key error');
}
}
exports.ProviderAPIKeyResponseHandler = ProviderAPIKeyResponseHandler;
/**
* Client for API key operations
*
* This client can be used in two ways:
* 1. As class methods for simple one-off operations:
* ProviderAPIKeyClient.setApiKey(apiKey)
*
* 2. As an instance for chained operations:
* client = new ProviderAPIKeyClient(apiKey)
* client.set().get()
*/
class ProviderAPIKeyClient extends auth_1.APIKeyAuth {
constructor(apiKey, fiApiKey, fiSecretKey, fiBaseUrl) {
super({
fiApiKey,
fiSecretKey,
fiBaseUrl
});
this.apiKey = apiKey;
}
// Instance methods for chaining
/**
* Set the API key and return self for chaining
*/
set() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.apiKey.key) {
throw new Error('API key is required');
}
yield this._setApiKey(this.apiKey);
return this;
});
}
/**
* Get the API key by provider
*/
get() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.apiKey.provider) {
throw new Error('Provider is required');
}
const response = yield this._getApiKey(this.apiKey.provider);
if (response) {
this.apiKey = response;
}
return response;
});
}
// Protected internal methods
/**
* Internal method for setting API key
*/
_setApiKey(apiKey) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.request({
method: types_1.HttpMethod.POST,
url: `${this.baseUrl}/${routes_1.Routes.model_hub_api_keys}`,
json: {
provider: apiKey.provider,
key: apiKey.key
}
}, ProviderAPIKeyResponseHandler);
return response;
});
}
/**
* Internal method to get API key by provider
*/
_getApiKey(provider) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.request({
method: types_1.HttpMethod.GET,
url: `${this.baseUrl}/${routes_1.Routes.model_hub_api_keys}`
}, ProviderAPIKeyResponseHandler);
if (Array.isArray(response)) {
return response.find(apiKey => apiKey.provider === provider);
}
return undefined;
});
}
// Class methods for simple operations
/**
* Create a new ProviderAPIKeyClient instance
*/
static _getInstance(apiKey, options = {}) {
return new ProviderAPIKeyClient(apiKey, options.fiApiKey, options.fiSecretKey, options.fiBaseUrl);
}
/**
* Class method for simple API key creation
*/
static setApiKey(apiKey_1) {
return __awaiter(this, arguments, void 0, function* (apiKey, options = {}) {
const instance = this._getInstance(apiKey, options);
yield instance.set();
return instance.apiKey;
});
}
/**
* Class method for simple API key retrieval
*/
static getApiKey(provider_1) {
return __awaiter(this, arguments, void 0, function* (provider, options = {}) {
const apiKey = { provider };
const instance = this._getInstance(apiKey, options);
return yield instance.get();
});
}
/**
* List all API keys for the organization
*/
static listApiKeys() {
return __awaiter(this, arguments, void 0, function* (options = {}) {
// The apiKey object is required for the constructor, but not used in this method.
const instance = this._getInstance({}, options);
const response = yield instance.request({
method: types_1.HttpMethod.GET,
url: `${instance.baseUrl}/${routes_1.Routes.model_hub_api_keys}`
}, ProviderAPIKeyResponseHandler);
return Array.isArray(response) ? response : [];
});
}
}
exports.ProviderAPIKeyClient = ProviderAPIKeyClient;
/**
* Singleton API Key Manager for managing FutureAGI credentials (fi_api_key, fi_secret_key)
*/
class APIKeyManager extends auth_1.APIKeyAuth {
constructor() {
super();
this._initialized = false;
const fiApiKey = this.getFiApiKey();
const fiSecretKey = this.getFiSecretKey();
if (!fiApiKey || !fiSecretKey) {
console.warn('FutureAGI credentials not found in environment variables. Please set FI_API_KEY and FI_SECRET_KEY.');
}
}
/**
* Get the singleton instance
*/
static getInstance() {
if (!APIKeyManager._instance) {
APIKeyManager._instance = new APIKeyManager();
}
return APIKeyManager._instance;
}
/**
* Get the API URL for key management
*/
get url() {
return `${this.baseUrl}/${routes_1.Routes.model_hub_api_keys}`;
}
/**
* Get FutureAGI API key
*/
getFiApiKey() {
return this.fiApiKey;
}
/**
* Get FutureAGI secret key
*/
getFiSecretKey() {
return this.fiSecretKey;
}
/**
* Check if FutureAGI credentials are configured
*/
isAuthenticated() {
return !!(this.getFiApiKey() && this.getFiSecretKey());
}
/**
* Get authentication headers for FutureAGI API
*/
getAuthHeaders() {
const apiKey = this.getFiApiKey();
const secretKey = this.getFiSecretKey();
if (!apiKey || !secretKey) {
throw new Error('FutureAGI credentials not configured');
}
return {
'X-Api-Key': apiKey,
'X-Secret-Key': secretKey
};
}
/**
* Provider API key operations (delegated to ProviderAPIKeyClient)
*/
/**
* Set a provider API key via FutureAGI API
*/
setProviderApiKey(provider, key) {
return __awaiter(this, void 0, void 0, function* () {
yield ProviderAPIKeyClient.setApiKey({ provider, key }, {
fiApiKey: this.getFiApiKey(),
fiSecretKey: this.getFiSecretKey()
});
});
}
/**
* Get a provider API key via FutureAGI API
*/
getProviderApiKey(provider) {
return __awaiter(this, void 0, void 0, function* () {
return yield ProviderAPIKeyClient.getApiKey(provider, {
fiApiKey: this.getFiApiKey(),
fiSecretKey: this.getFiSecretKey()
});
});
}
/**
* List all provider API keys via FutureAGI API
*/
listProviderApiKeys() {
return __awaiter(this, void 0, void 0, function* () {
return yield ProviderAPIKeyClient.listApiKeys({
fiApiKey: this.getFiApiKey(),
fiSecretKey: this.getFiSecretKey()
});
});
}
/**
* Check if a provider API key is configured
*/
isProviderConfigured(provider) {
return __awaiter(this, void 0, void 0, function* () {
try {
const apiKey = yield this.getProviderApiKey(provider);
return !!apiKey;
}
catch (error) {
if (error.message && error.message.includes('404')) {
return false;
}
throw error;
}
});
}
/**
* Validate that required provider API keys are present
*/
validateRequiredProviders(requiredProviders) {
return __awaiter(this, void 0, void 0, function* () {
if (!requiredProviders || requiredProviders.length === 0) {
return true;
}
const results = yield Promise.allSettled(requiredProviders.map(provider => this.isProviderConfigured(provider)));
const missingProviders = requiredProviders.filter((provider, index) => {
const result = results[index];
return result.status === 'rejected' || !result.value;
});
if (missingProviders.length > 0) {
throw new Error(`Missing required provider API keys: ${missingProviders.join(', ')}`);
}
return true;
});
}
/**
* Reset the singleton instance (mainly for testing)
*/
static resetInstance() {
if (APIKeyManager._instance) {
APIKeyManager._instance.close();
APIKeyManager._instance = undefined;
}
}
}
exports.APIKeyManager = APIKeyManager;
/**
* Factory function to get API key manager instance
*/
function getAPIKeyManager() {
return APIKeyManager.getInstance();
}
/**
* Helper function to check if a provider is supported
*/
function isSupportedProvider(provider) {
return Object.values(types_1.ModelProvider).includes(provider);
}
/**
* Get all supported providers
*/
function getSupportedProviders() {
return Object.values(types_1.ModelProvider);
}
//# sourceMappingURL=apikeys.js.map