UNPKG

giga-code

Version:

A personal AI CLI assistant powered by Grok for local development.

180 lines 6.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateApiKey = exports.validateOllamaUrl = exports.validateOpenaiKey = exports.validateCerebrasKey = exports.validateGroqKey = exports.validateXaiKey = exports.validateGoogleKey = exports.validateAnthropicKey = exports.validateOpenRouterKey = void 0; const openai_1 = __importDefault(require("openai")); const cerebras_cloud_sdk_1 = __importDefault(require("@cerebras/cerebras_cloud_sdk")); const ollama_models_1 = require("./ollama-models"); // Validate OpenRouter API key async function validateOpenRouterKey(apiKey) { try { const client = new openai_1.default({ apiKey, baseURL: 'https://openrouter.ai/api/v1', timeout: 10000, }); await client.models.list(); return { isValid: true }; } catch (error) { return { isValid: false, error: error?.message || "API validation failed" }; } } exports.validateOpenRouterKey = validateOpenRouterKey; // Validate Anthropic API key async function validateAnthropicKey(apiKey) { try { const client = new openai_1.default({ apiKey, baseURL: 'https://api.anthropic.com/v1', timeout: 10000, }); await client.models.list(); return { isValid: true }; } catch (error) { return { isValid: false, error: error?.message || "API validation failed" }; } } exports.validateAnthropicKey = validateAnthropicKey; // Validate Google API key async function validateGoogleKey(apiKey) { try { const client = new openai_1.default({ apiKey, baseURL: 'https://generativelanguage.googleapis.com/v1beta/openai', timeout: 10000, }); await client.models.list(); return { isValid: true }; } catch (error) { return { isValid: false, error: error?.message || "API validation failed" }; } } exports.validateGoogleKey = validateGoogleKey; // Validate xAI API key async function validateXaiKey(apiKey) { try { const client = new openai_1.default({ apiKey, baseURL: 'https://api.x.ai/v1', timeout: 10000, }); await client.models.list(); return { isValid: true }; } catch (error) { return { isValid: false, error: error?.message || "API validation failed" }; } } exports.validateXaiKey = validateXaiKey; // Validate Groq API key async function validateGroqKey(apiKey) { try { const client = new openai_1.default({ apiKey, baseURL: 'https://api.groq.com/openai/v1', timeout: 10000, }); await client.models.list(); return { isValid: true }; } catch (error) { return { isValid: false, error: error?.message || "API validation failed" }; } } exports.validateGroqKey = validateGroqKey; // Validate Cerebras API key async function validateCerebrasKey(apiKey, model) { try { const client = new cerebras_cloud_sdk_1.default({ apiKey, timeout: 10000, }); // Use the provided model or default to qwen-3-235b-a22b-instruct-2507 for validation const validationModel = model || 'qwen-3-235b-a22b-instruct-2507'; // Try a simple chat completion request instead of listing models const response = await client.chat.completions.create({ model: validationModel, messages: [{ role: 'user', content: 'test' }], max_completion_tokens: 1, temperature: 0.1, }); return { isValid: true }; } catch (error) { return { isValid: false, error: error?.message || "API validation failed" }; } } exports.validateCerebrasKey = validateCerebrasKey; // Validate OpenAI API key async function validateOpenaiKey(apiKey) { try { const client = new openai_1.default({ apiKey, baseURL: 'https://api.openai.com/v1', timeout: 10000, }); await client.models.list(); return { isValid: true }; } catch (error) { return { isValid: false, error: error?.message || "API validation failed" }; } } exports.validateOpenaiKey = validateOpenaiKey; // Validate Ollama connection (baseUrl instead of API key) async function validateOllamaUrl(baseUrl) { try { const result = await (0, ollama_models_1.testOllamaConnection)(baseUrl); if (result.success) { return { isValid: true, }; } else { return { isValid: false, error: result.error || "Cannot connect to Ollama" }; } } catch (error) { return { isValid: false, error: error?.message || "Ollama connection failed" }; } } exports.validateOllamaUrl = validateOllamaUrl; // Main validation function that routes to the correct provider async function validateApiKey(provider, apiKey, model) { // Special case for Ollama - it's a base URL, not an API key, and can be empty (defaults to localhost) if (provider.toLowerCase() === 'ollama') { const baseUrl = apiKey || 'http://localhost:11434'; return validateOllamaUrl(baseUrl); } if (!apiKey || !apiKey.trim()) { return { isValid: false, error: 'API key is empty' }; } switch (provider.toLowerCase()) { case 'openrouter': return validateOpenRouterKey(apiKey); case 'anthropic': return validateAnthropicKey(apiKey); case 'google': return validateGoogleKey(apiKey); case 'xai': return validateXaiKey(apiKey); case 'groq': return validateGroqKey(apiKey); case 'cerebras': return validateCerebrasKey(apiKey, model); case 'openai': return validateOpenaiKey(apiKey); default: return { isValid: false, error: 'Unknown provider' }; } } exports.validateApiKey = validateApiKey; //# sourceMappingURL=api-key-validator.js.map