UNPKG

lanonasis-memory

Version:

Memory as a Service integration - AI-powered memory management with semantic search (Compatible with CLI v3.0.6+)

198 lines 7.69 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ApiKeyService = void 0; const vscode = __importStar(require("vscode")); class ApiKeyService { constructor(secureApiKeyService) { this.baseUrl = 'https://mcp.lanonasis.com'; this.secureApiKeyService = secureApiKeyService; this.config = vscode.workspace.getConfiguration('lanonasis'); this.updateConfig(); } updateConfig() { const useGateway = this.config.get('useGateway', true); const apiUrl = this.config.get('apiUrl', 'https://mcp.lanonasis.com'); const gatewayUrl = this.config.get('gatewayUrl', 'https://mcp.lanonasis.com'); this.baseUrl = this.sanitizeBaseUrl(useGateway ? gatewayUrl : apiUrl); } refreshConfig() { this.config = vscode.workspace.getConfiguration('lanonasis'); this.updateConfig(); } async makeRequest(endpoint, options = {}) { const credentials = await this.resolveCredentials(); const normalizedEndpoint = endpoint.startsWith('/') ? endpoint : `/${endpoint}`; const url = `${this.baseUrl}${normalizedEndpoint}`; const authHeaders = credentials.type === 'oauth' ? { 'Authorization': `Bearer ${credentials.token}` } : { 'X-API-Key': credentials.token }; const response = await fetch(url, { ...options, headers: { 'Content-Type': 'application/json', ...authHeaders, ...options.headers } }); if (!response.ok) { const errorText = await response.text(); throw new Error(`API request failed: ${response.status} ${response.statusText} - ${errorText}`); } return response.json(); } sanitizeBaseUrl(url) { if (!url) { return 'https://mcp.lanonasis.com'; } let clean = url.trim(); // remove trailing slashes clean = clean.replace(/\/+$/, ''); // remove duplicate /api or /api/v1 suffixes clean = clean.replace(/\/api\/v1$/i, '').replace(/\/api$/i, ''); return clean || 'https://mcp.lanonasis.com'; } async resolveCredentials() { let credentials = await this.secureApiKeyService.getStoredCredentials(); if (!credentials) { const value = await this.secureApiKeyService.getApiKeyOrPrompt(); if (!value) { throw new Error('API key not configured. Please configure your API key to use Lanonasis services.'); } credentials = await this.secureApiKeyService.getStoredCredentials(); if (!credentials) { credentials = { type: this.looksLikeJwt(value) ? 'oauth' : 'apiKey', token: value }; } } return credentials; } looksLikeJwt(token) { const parts = token.split('.'); if (parts.length !== 3) { return false; } const jwtSegment = /^[A-Za-z0-9-_]+$/; return parts.every(segment => jwtSegment.test(segment)); } // ============================================================================ // PROJECT MANAGEMENT // ============================================================================ async getProjects() { return this.makeRequest('/api/v1/projects'); } async getProject(projectId) { return this.makeRequest(`/api/v1/projects/${projectId}`); } async createProject(request) { return this.makeRequest('/api/v1/projects', { method: 'POST', body: JSON.stringify(request) }); } async updateProject(projectId, updates) { return this.makeRequest(`/api/v1/projects/${projectId}`, { method: 'PUT', body: JSON.stringify(updates) }); } async deleteProject(projectId) { await this.makeRequest(`/api/v1/projects/${projectId}`, { method: 'DELETE' }); } // ============================================================================ // API KEY MANAGEMENT // ============================================================================ async getApiKeys(projectId) { const endpoint = projectId ? `/api/v1/projects/${projectId}/api-keys` : '/api/v1/auth/api-keys'; const response = await this.makeRequest(endpoint); // Handle wrapped response format from /api/v1/auth/api-keys // which returns { success: true, data: [...] } if (response && typeof response === 'object' && 'data' in response && Array.isArray(response.data)) { return response.data; } // Handle direct array response from /api/v1/projects/:projectId/api-keys if (Array.isArray(response)) { return response; } // Fallback: return empty array if response format is unexpected return []; } async getApiKey(keyId) { return this.makeRequest(`/api/v1/auth/api-keys/${keyId}`); } async createApiKey(request) { return this.makeRequest('/api/v1/auth/api-keys', { method: 'POST', body: JSON.stringify(request) }); } async updateApiKey(keyId, updates) { return this.makeRequest(`/api/v1/auth/api-keys/${keyId}`, { method: 'PUT', body: JSON.stringify(updates) }); } async deleteApiKey(keyId) { await this.makeRequest(`/api/v1/auth/api-keys/${keyId}`, { method: 'DELETE' }); } async rotateApiKey(keyId) { return this.makeRequest(`/api/v1/auth/api-keys/${keyId}/rotate`, { method: 'POST' }); } // ============================================================================ // UTILITY METHODS // ============================================================================ async testConnection() { try { await this.makeRequest('/api/v1/health'); return true; } catch (error) { return false; } } async getUserInfo() { return this.makeRequest('/api/v1/auth/me'); } } exports.ApiKeyService = ApiKeyService; //# sourceMappingURL=ApiKeyService.js.map