UNPKG

@cosmara-ai/community-sdk

Version:

COSMARA Community SDK - Multi-provider AI client with intelligent routing and 1,000 free requests/month

341 lines (333 loc) 11.8 kB
declare enum APIProvider { OPENAI = "OPENAI", ANTHROPIC = "ANTHROPIC", GOOGLE = "GOOGLE" } interface AIMessage { role: 'system' | 'user' | 'assistant'; content: string; name?: string; metadata?: Record<string, any>; } interface AIModel { id: string; provider: APIProvider; name: string; displayName: string; description: string; maxTokens: number; inputCostPer1K: number; outputCostPer1K: number; supportsStreaming: boolean; supportsTools: boolean; contextWindow: number; isActive: boolean; } interface AIRequest { model: string; messages: AIMessage[]; maxTokens?: number; temperature?: number; topP?: number; stream?: boolean; tools?: AITool[]; toolChoice?: 'auto' | 'none' | { type: 'function'; name: string; }; metadata?: Record<string, any>; } interface AITool { type: 'function'; function: { name: string; description: string; parameters: Record<string, any>; }; } interface AIResponse { id: string; model: string; provider: APIProvider; choices: AIChoice[]; usage: AIUsage; created: number; metadata?: Record<string, any>; } interface AIChoice { index: number; message: AIMessage; finishReason: 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'refusal' | null; toolCalls?: AIToolCall[]; } interface AIToolCall { id: string; type: 'function'; function: { name: string; arguments: string; }; } interface AIUsage { promptTokens: number; completionTokens: number; totalTokens: number; cost: number; } interface AIStreamChunk { id: string; model: string; provider: APIProvider; choices: AIStreamChoice[]; usage?: AIUsage; created: number; } interface AIStreamChoice { index: number; delta: { role?: 'assistant'; content?: string; toolCalls?: AIToolCall[]; }; finishReason?: 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'refusal' | null; } interface AIErrorConfig { code: string; message: string; type: 'authentication' | 'rate_limit' | 'invalid_request' | 'api_error' | 'network_error' | 'usage_limit'; provider: APIProvider; retryable: boolean; details?: Record<string, any>; } declare class AIError extends Error { readonly code: string; readonly type: 'authentication' | 'rate_limit' | 'invalid_request' | 'api_error' | 'network_error' | 'usage_limit'; readonly provider: APIProvider; readonly retryable: boolean; readonly details: Record<string, any>; constructor(config: AIErrorConfig); } interface AIProvider { provider: APIProvider; chat(request: AIRequest, apiKey: string): Promise<AIResponse>; chatStream(request: AIRequest, apiKey: string): AsyncIterable<AIStreamChunk>; getModels(): Promise<AIModel[]>; getModel(modelId: string): Promise<AIModel | null>; validateApiKey(apiKey: string): Promise<boolean>; estimateCost(request: AIRequest): Promise<number>; } interface CommunityConfig { apiKeys: { openai?: string; anthropic?: string; google?: string; }; baseUrls?: { openai?: string; anthropic?: string; google?: string; }; enableUsageTracking?: boolean; userId?: string; } interface UsageRecord { timestamp: number; provider: APIProvider; model: string; tokensUsed: number; cost: number; userId: string; } interface UsageLimits { requestsPerMonth: number; requestsPerDay: number; requestsPerMinute: number; maxUniqueUsers: number; commercialUseDetection: boolean; } declare const MODEL_EQUIVALENTS: Record<string, Record<APIProvider, string>>; declare const COMMUNITY_LIMITS: UsageLimits; declare const PERFORMANCE_TARGETS: { readonly MAX_RESPONSE_TIME: 30000; readonly MAX_STREAM_FIRST_TOKEN: 5000; }; declare const COST_THRESHOLDS: { readonly CHEAP_REQUEST: 0.001; readonly EXPENSIVE_REQUEST: 0.1; }; declare const UPGRADE_MESSAGES: { readonly RATE_LIMIT: "⚡ You've reached your Community Edition limits. Upgrade to Developer tier for 50x more requests and ML-powered optimization!"; readonly COMMERCIAL_USE: "🏢 Commercial usage detected. Upgrade to Developer tier to unlock commercial licensing and priority support!"; readonly FEATURE_LOCKED: "🔒 This feature requires Developer tier. Upgrade for ML routing, analytics, and cost optimization!"; readonly UPGRADE_CTA: "🚀 Ready to unlock the full potential? Upgrade at https://ai-marketplace.dev/pricing"; }; interface CommercialUsageIndicators { highRequestVolume: boolean; businessHours: boolean; multipleUniqueUsers: boolean; consistentUsagePattern: boolean; apiKeyPatterns: boolean; } declare class CommunityUsageTracker { private usageHistory; private limits; private storageKey; private userFingerprint; constructor(limits?: UsageLimits, userId?: string); checkLimits(): Promise<void>; recordUsage(provider: APIProvider, model: string, tokensUsed: number, cost: number, userId?: string): void; getUsageStats(): { requestsThisMinute: number; requestsThisDay: number; requestsThisMonth: number; uniqueUsers: number; totalCost: number; commercialIndicators: CommercialUsageIndicators; }; private detectCommercialUsage; private getCommercialIndicators; private generateFingerprint; private loadUsageHistory; private saveUsageHistory; private startCleanup; } declare class CommunityLicenseValidator { validateUsage(usageStats: ReturnType<CommunityUsageTracker['getUsageStats']>): { isValid: boolean; violations: string[]; recommendations: string[]; }; displayUpgradePrompt(reason: 'limits' | 'commercial' | 'features'): void; } declare class AIMarketplaceCommunityClient { private providers; private apiKeys; private usageTracker; private licenseValidator; private cache; private userId; private enableUsageTracking; constructor(config: CommunityConfig); chat(request: AIRequest, options?: { provider?: APIProvider; userId?: string; }): Promise<AIResponse>; chatStream(request: AIRequest, options?: { provider?: APIProvider; userId?: string; }): AsyncIterable<AIStreamChunk>; getModels(provider?: APIProvider): Promise<AIModel[]>; validateApiKeys(): Promise<Record<APIProvider, boolean>>; getUsageStats(): { message: string; enableInstructions: string; } | { limits: { requestsPerMinute: number; requestsPerDay: number; requestsPerMonth: number; maxUniqueUsers: number; }; validation: { isValid: boolean; violations: string[]; recommendations: string[]; }; upgradeMessage: "🏢 Commercial usage detected. Upgrade to Developer tier to unlock commercial licensing and priority support!" | undefined; requestsThisMinute: number; requestsThisDay: number; requestsThisMonth: number; uniqueUsers: number; totalCost: number; commercialIndicators: CommercialUsageIndicators; message?: undefined; enableInstructions?: undefined; }; estimateCost(request: AIRequest, provider?: APIProvider): Promise<{ provider: APIProvider; cost: number; }[]>; clearCache(): void; private initializeProviders; private getAvailableProviders; private getRandomAvailableProvider; private getApiKey; private getDefaultModelForProvider; private generateCacheKey; private getCachedResponse; private setCachedResponse; private startCacheCleanup; private generateUserId; private showWelcomeMessage; private maybeShowUpgradePrompt; } declare class OpenAIProvider { readonly provider = APIProvider.OPENAI; private readonly baseUrl; private readonly defaultHeaders; private models; constructor(baseUrl?: string); getModels(): Promise<AIModel[]>; getModel(modelId: string): Promise<AIModel | null>; validateApiKey(apiKey: string): Promise<boolean>; estimateCost(request: AIRequest): Promise<number>; chat(request: AIRequest, apiKey: string): Promise<AIResponse>; chatStream(request: AIRequest, apiKey: string): AsyncIterable<AIStreamChunk>; private transformRequest; private transformResponse; private transformStreamChunk; private calculateUsage; private handleHttpError; } declare class AnthropicProvider { readonly provider = APIProvider.ANTHROPIC; private readonly baseUrl; private readonly defaultHeaders; private models; constructor(baseUrl?: string); getModels(): Promise<AIModel[]>; getModel(modelId: string): Promise<AIModel | null>; validateApiKey(apiKey: string): Promise<boolean>; estimateCost(request: AIRequest): Promise<number>; chat(request: AIRequest, apiKey: string): Promise<AIResponse>; chatStream(request: AIRequest, apiKey: string): AsyncIterable<AIStreamChunk>; private transformRequest; private transformResponse; private transformStreamChunk; private calculateUsage; private mapFinishReason; private handleHttpError; } declare class GoogleProvider { readonly provider = APIProvider.GOOGLE; private readonly baseUrl; private readonly defaultHeaders; private models; constructor(baseUrl?: string); getModels(): Promise<AIModel[]>; getModel(modelId: string): Promise<AIModel | null>; validateApiKey(apiKey: string): Promise<boolean>; estimateCost(request: AIRequest): Promise<number>; chat(request: AIRequest, apiKey: string): Promise<AIResponse>; chatStream(request: AIRequest, apiKey: string): AsyncIterable<AIStreamChunk>; private transformRequest; private transformResponse; private transformStreamChunk; private calculateUsage; private mapFinishReason; private handleHttpError; } declare function createClient(config: CommunityConfig): AIMarketplaceCommunityClient; declare const VERSION = "1.0.0"; declare const EDITION = "Community"; declare const UPGRADE_INFO: { readonly currentTier: "Community"; readonly nextTier: "Developer"; readonly pricingUrl: "https://ai-marketplace.dev/pricing"; readonly contactUrl: "https://ai-marketplace.dev/contact"; readonly benefits: { readonly developer: readonly ["50,000 requests/month (50x more than Community)", "ML-powered cost optimization and routing", "Advanced analytics and usage insights", "Intelligent fallbacks and error handling", "Commercial usage rights and licensing", "Priority email and chat support", "Custom model fine-tuning (coming soon)"]; readonly professional: readonly ["500,000 requests/month (500x more than Community)", "All Developer tier features", "Dedicated account manager", "Custom integrations and partnerships", "SLA guarantees and uptime commitments", "Advanced security and compliance features"]; }; }; export { type AIChoice, AIError, type AIErrorConfig, AIMarketplaceCommunityClient, type AIMessage, type AIModel, type AIProvider, type AIRequest, type AIResponse, type AIStreamChoice, type AIStreamChunk, type AITool, type AIToolCall, type AIUsage, APIProvider, AnthropicProvider, COMMUNITY_LIMITS, COST_THRESHOLDS, type CommunityConfig, CommunityLicenseValidator, CommunityUsageTracker, EDITION, GoogleProvider, MODEL_EQUIVALENTS, OpenAIProvider, PERFORMANCE_TARGETS, UPGRADE_INFO, UPGRADE_MESSAGES, type UsageLimits, type UsageRecord, VERSION, createClient };