UNPKG

toto-agent

Version:

Chatbot agent and reusable components for Toto platform

86 lines (85 loc) 3.13 kB
import { GoogleGenerativeAI } from '@google/generative-ai'; import { AgentResponse, AgentType, UserIntent } from '../../shared/AgentTypes'; import { AgentTrainingConfig } from '../config/AgentTrainingConfig'; interface UserContext { language?: string; location?: string; userRole?: 'user' | 'guardian' | 'admin'; isFirstTime?: boolean; previousEngagement?: 'low' | 'medium' | 'high'; } export declare abstract class BaseAgent { protected genAI: GoogleGenerativeAI; protected agentType: AgentType; protected config: AgentTrainingConfig; constructor(apiKey: string, agentType: AgentType); /** * Dynamic global rules based on platform knowledge and user context */ protected getGlobalRules(userContext?: UserContext): string; /** * Enhanced prompt generation with dynamic rules */ protected buildPromptWithGlobalRules(specificPrompt: string, userContext?: UserContext): string; /** * Format user context for AI understanding */ private formatUserContext; /** * Enhanced language detection using configuration */ protected detectLanguage(userMessage: string): string; /** * Build user context from available information */ protected buildUserContext(context: any, userMessage?: string): UserContext; /** * Abstract method that each specialized agent must implement */ abstract process(userMessage: string, context: any, intent?: UserIntent): Promise<AgentResponse>; /** * Enhanced response generation with dynamic context */ protected generateResponse(prompt: string, context?: any, userMessage?: string): Promise<string>; /** * Generate streaming response for better user experience * Implementation of Google AI Studio's streaming recommendation */ protected generateStreamingResponse(prompt: string, context?: any, userMessage?: string, onChunk?: (chunk: string) => void): Promise<string>; /** * Enhanced response validation with context awareness */ protected validateResponse(response: string, userContext: UserContext): { isValid: boolean; issues: string[]; }; /** * Enhanced context extraction with user awareness */ protected extractContextSummary(context: any): string; /** * Get localized response templates */ protected getLocalizedResponses(languageCode: string, category: string): any; /** * Check if feature is enabled in training dynamics */ protected isFeatureEnabled(feature: string): boolean; /** * Check if response is valid */ protected isResponseValid(text: string, userContext: any): boolean; /** * Handle invalid response by attempting recovery */ protected handleInvalidResponse(text: string, userContext: any): string; /** * Generate fallback response when API is overloaded */ protected generateFallbackResponse(userContext: any): string; /** * Handle generation errors gracefully */ protected handleGenerationError(error: any, context: any): string; } export {};