UNPKG

ai-cant-even

Version:

A satirical AI-powered utility that's confidently wrong about basic math operations

292 lines (291 loc) 10.9 kB
"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.Provider = exports.Logic = exports.Confidence = exports.AiCantEven = void 0; exports.aiCantEven = aiCantEven; const types_1 = require("./types"); const ai_sdk_client_1 = require("./ai-sdk-client"); /** * Default configuration for the AI */ const DEFAULT_CONFIG = { confidence: types_1.Confidence.OVERWHELMED, logic: types_1.Logic.SIMPLE, }; /** * Main class for the AI that can't even determine if numbers are even */ class AiCantEven { /** * Create a new AI that can't even * @param config - Configuration options */ constructor(config = {}) { this.confidence = config.confidence || DEFAULT_CONFIG.confidence; this.logic = config.logic || DEFAULT_CONFIG.logic; // Set model, API key and endpoint from config this.model = config.model; this.apiKey = config.apiKey; this.apiEndpoint = config.apiEndpoint; // Set provider from config or default this.provider = config.provider || DEFAULT_CONFIG.provider; // Initialize client if provider is available this.initializeClients(); } /** * Initialize client based on provider and API key */ initializeClients() { // Only initialize if we have a provider and API key if (this.provider && this.apiKey) { this.aiSdkClient = new ai_sdk_client_1.AiSdkClient(this.provider, this.apiKey, this.apiEndpoint, this.model); } } /** * Set the confidence level for the AI * @param confidence - The confidence level * @returns The AI instance for method chaining */ withConfidence(confidence) { // Set temporary confidence for the next operation this.tempConfidence = confidence; return this; } /** * Set the model for the next operation */ withModel(model) { this.tempModel = model; return this; } /** * Set the logic type for the AI * @param logic - The logic type * @returns The AI instance for method chaining */ withLogic(logic) { // Set temporary logic for the next operation this.tempLogic = logic; return this; } /** * Set the API key for the LLM * @param apiKey - The API key * @param apiEndpoint - Optional custom API endpoint * @returns The AI instance for method chaining */ withApiKey(apiKey, apiEndpoint) { this.apiKey = apiKey; if (apiEndpoint) { this.apiEndpoint = apiEndpoint; } // Re-initialize client with new API key this.initializeClients(); return this; } /** * Get the current confidence level (temporary or default) */ getCurrentConfidence() { return this.tempConfidence !== undefined ? this.tempConfidence : this.confidence; } /** * Get the current logic type (temporary or default) */ getCurrentLogic() { return this.tempLogic !== undefined ? this.tempLogic : this.logic; } /** * Get the current model (temporary or permanent) */ getCurrentModel() { return this.tempModel || this.model; } /** * Reset temporary settings after an operation */ resetTemporarySettings() { this.tempConfidence = undefined; this.tempLogic = undefined; this.tempModel = undefined; } /** * Generate a response using the LLM or fallback to hardcoded responses */ generateResponse(operation, value, comparisonValue) { return __awaiter(this, void 0, void 0, function* () { try { // Get current model for this operation const currentModel = this.getCurrentModel(); // If no client is available, use hardcoded responses if (!this.aiSdkClient) { return this.generateFallbackResponse(operation, value); } const params = { confidence: this.getCurrentConfidence(), logic: this.getCurrentLogic(), operation, value, comparisonValue, }; // Use AI SDK client to generate response const response = yield this.aiSdkClient.generateResponse(params); return response.text; } finally { // Always reset temporary settings after generating a response this.resetTemporarySettings(); } }); } /** * Generate a fallback response when no LLM client is available */ generateFallbackResponse(operation, value) { const operationResponses = [ `I can't even process ${value} right now. Numbers are just, like, so abstract?`, `Error 418: I'm a teapot, not a calculator. But I'm pretty sure the answer is purple.`, `My neural networks are taking a coffee break. Try asking a human, they're terrible at math too.`, ]; return operationResponses[Math.floor(Math.random() * operationResponses.length)]; } /** * Determine if a number is even (but not really) * @param value - The number to check * @returns A hilariously wrong explanation */ isEven(value) { return __awaiter(this, void 0, void 0, function* () { return this.generateResponse('isEven', value); }); } /** * Determine if a number is odd (but not really) * @param value - The number to check * @returns A hilariously wrong explanation */ isOdd(value) { return __awaiter(this, void 0, void 0, function* () { return this.generateResponse('isOdd', value); }); } /** * Determine if a number is greater than another (but not really) * @param value - The first number * @param comparisonValue - The second number * @returns A hilariously wrong explanation */ isGreaterThan(value, comparisonValue) { return __awaiter(this, void 0, void 0, function* () { return this.generateResponse('isGreaterThan', value, comparisonValue); }); } /** * Determine if a number is less than another (but not really) * @param value - The first number * @param comparisonValue - The second number * @returns A hilariously wrong explanation */ isLessThan(value, comparisonValue) { return __awaiter(this, void 0, void 0, function* () { return this.generateResponse('isLessThan', value, comparisonValue); }); } /** * Determine if two numbers are equal (but not really) * @param value - The first number * @param comparisonValue - The second number * @returns A hilariously wrong explanation */ areEqual(value, comparisonValue) { return __awaiter(this, void 0, void 0, function* () { return this.generateResponse('areEqual', value, comparisonValue); }); } /** * Determine if a number is prime (but not really) * @param value - The number to check * @returns A hilariously wrong explanation */ isPrime(value) { return __awaiter(this, void 0, void 0, function* () { return this.generateResponse('isPrime', value); }); } /** * Determine if a number is positive (but not really) * @param value - The number to check * @returns A hilariously wrong explanation */ isPositive(value) { return __awaiter(this, void 0, void 0, function* () { return this.generateResponse('isPositive', value); }); } /** * Determine if a number is divisible by another (but not really) * @param value - The first number * @param comparisonValue - The second number * @returns A hilariously wrong explanation */ isDivisibleBy(value, comparisonValue) { return __awaiter(this, void 0, void 0, function* () { return this.generateResponse('isDivisibleBy', value, comparisonValue); }); } /** * Determine if a value is a number (but not really) * @param value - The value to check * @returns A hilariously wrong explanation */ // eslint-disable-next-line @typescript-eslint/no-explicit-any isNumber(value) { return __awaiter(this, void 0, void 0, function* () { return this.generateResponse('isNumber', Number(value)); }); } /** * Determine if a number is an integer (but not really) * @param value - The number to check * @returns A hilariously wrong explanation */ isInteger(value) { return __awaiter(this, void 0, void 0, function* () { return this.generateResponse('isInteger', value); }); } } exports.AiCantEven = AiCantEven; /** * Create a new AI that can't even * @param config - Configuration options * @returns A new AI instance */ function aiCantEven(config = {}) { // If the provider and API key are provided, initialize the client if (config.provider && config.apiKey) { return new AiCantEven(config); } // If API key is in the environment, use it based on provider if (process.env.ANTHROPIC_API_KEY) { return new AiCantEven(Object.assign(Object.assign({}, config), { provider: types_1.Provider.ANTHROPIC, apiKey: process.env.ANTHROPIC_API_KEY })); } else if (process.env.OPENAI_API_KEY) { return new AiCantEven(Object.assign(Object.assign({}, config), { provider: types_1.Provider.OPENAI, apiKey: process.env.OPENAI_API_KEY })); } // Else return default config return new AiCantEven(config); } // Export types and enums var types_2 = require("./types"); Object.defineProperty(exports, "Confidence", { enumerable: true, get: function () { return types_2.Confidence; } }); Object.defineProperty(exports, "Logic", { enumerable: true, get: function () { return types_2.Logic; } }); Object.defineProperty(exports, "Provider", { enumerable: true, get: function () { return types_2.Provider; } });