UNPKG

memofai

Version:

Official JavaScript/TypeScript SDK for Memory-of-Agents (MOA) - AI memory infrastructure for intelligent applications

169 lines 7.12 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpClient = void 0; const axios_1 = __importDefault(require("axios")); const types_1 = require("./types"); const version_1 = require("./version"); class HttpClient { constructor(config) { this.config = config; const environment = config.environment || 'production'; if (!types_1.ENVIRONMENTS[environment]) { throw new Error(`Invalid environment: "${environment}". Valid options are: ${Object.keys(types_1.ENVIRONMENTS).join(', ')}`); } const baseURL = types_1.ENVIRONMENTS[environment].baseURL; this.userAgent = this.generateUserAgent(); this.client = axios_1.default.create({ baseURL, timeout: config.timeout || 30000, headers: { Authorization: `Token ${config.apiToken}`, 'Content-Type': 'application/json', 'User-Agent': this.userAgent, }, }); this.setupInterceptors(); } generateUserAgent() { const sdkInfo = `memofai-js/${version_1.SDK_VERSION}`; let runtime = 'unknown'; if (typeof process !== 'undefined' && process.versions && process.versions.node) { runtime = `Node.js/${process.versions.node}`; } else if (typeof window !== 'undefined') { runtime = `Browser`; } return `${sdkInfo} (${runtime})`; } setupInterceptors() { this.client.interceptors.request.use((config) => { var _a; if (process.env.MOA_DEBUG === 'true') { console.log(`[API] ${(_a = config.method) === null || _a === void 0 ? void 0 : _a.toUpperCase()} ${config.url}`); } return config; }, (error) => { return Promise.reject(error); }); this.client.interceptors.response.use((response) => { if (process.env.MOA_DEBUG === 'true') { console.log(`[API] Response ${response.status} for ${response.config.url}`); } response.data = this.transformResponse(response.data); return response; }, async (error) => { var _a, _b, _c; const config = error.config; if (this.config.retries && config && config.__retryCount === undefined) { config.__retryCount = 0; } if (this.config.retries && config && config.__retryCount !== undefined && config.__retryCount < this.config.retries && this.shouldRetry(error)) { config.__retryCount++; if (process.env.MOA_DEBUG === 'true') { console.log(`[API] Retrying request (${config.__retryCount}/${this.config.retries})`); } if (this.config.retryDelay) { await this.delay(this.config.retryDelay); } return this.client(config); } const status = ((_a = error.response) === null || _a === void 0 ? void 0 : _a.status) || 0; const statusText = ((_b = error.response) === null || _b === void 0 ? void 0 : _b.statusText) || 'Unknown Error'; const responseData = (_c = error.response) === null || _c === void 0 ? void 0 : _c.data; let message = error.message || 'Request failed'; if (responseData && typeof responseData === 'object') { const data = responseData; if (data.message && typeof data.message === 'string') { message = data.message; } else if (data.error && typeof data.error === 'string') { message = data.error; } else if (data.detail && typeof data.detail === 'string') { message = data.detail; } } const specificError = this.createSpecificError(message, status, statusText, responseData); return Promise.reject(specificError); }); } shouldRetry(error) { return !error.response || (error.response.status >= 500 && error.response.status < 600); } delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } transformResponse(data) { if (data && typeof data === 'object' && 'data' in data) { const apiResponse = data; if (apiResponse.success !== undefined && apiResponse.data !== undefined) { return apiResponse.data; } } return data; } createSpecificError(message, status, statusText, responseData) { let errorMessage = message; if (responseData && typeof responseData === 'object') { const data = responseData; if (data.detail && typeof data.detail === 'string') { errorMessage = data.detail; } else if (data.message && typeof data.message === 'string') { errorMessage = data.message; } } const MoaErrorResponse = typeof responseData === 'object' && responseData !== null ? responseData : { detail: errorMessage }; switch (status) { case 400: return new types_1.ValidationError(status, statusText, MoaErrorResponse); case 401: return new types_1.AuthenticationError(status, statusText, MoaErrorResponse); case 403: return new types_1.AuthorizationError(status, statusText, MoaErrorResponse); case 404: return new types_1.NotFoundError(status, statusText, MoaErrorResponse); case 429: { return new types_1.RequestLimitError(status, statusText, MoaErrorResponse); } case 503: case 504: return new types_1.ServiceUnavailableError(status, statusText, MoaErrorResponse); default: return new types_1.ApiError(errorMessage, status, statusText, MoaErrorResponse); } } async request(config) { const requestConfig = { method: config.method, url: config.path, }; if (config.queryParams) { requestConfig.params = config.queryParams; } if (config.body) { requestConfig.data = config.body; } if (config.headers) { requestConfig.headers = Object.assign(Object.assign({}, requestConfig.headers), config.headers); } const response = await this.client(requestConfig); return { data: response.data, status: response.status, statusText: response.statusText, headers: response.headers, }; } } exports.HttpClient = HttpClient; //# sourceMappingURL=http-client.js.map