-
Settings
-
Configure system settings and preferences
+ {/* Header */}
+
+
+
System Settings
+
Configure the behavior of your character ecosystem
+
+
+ {saving ? (
+ <>
+
+ Saving...
+ >
+ ) : (
+ <>
+
+ Save Settings
+ >
+ )}
+
-
-
-
System Settings
-
This page will show configuration options
+
+ {/* LLM GLOBAL CONTROL - COST PROTECTION */}
+
+
+
+
+
+
+ LLM API Status: {config.llm_enabled ? 'ENABLED' : 'DISABLED'}
+
+
+ {config.llm_enabled
+ ? '⚠️ AI API calls are ACTIVE - this costs money!'
+ : '✅ AI API calls are DISABLED - no costs incurred'
+ }
+
+
+
+
+
+
+ {config.llm_enabled ? 'Disable to Save Costs' : 'Enable LLM (will cost money)'}
+
+ {
+ const enabled = e.target.checked;
+ if (enabled) {
+ const confirmed = window.confirm(
+ '⚠️ WARNING: Enabling LLM will start making API calls that cost money!\n\n' +
+ 'Characters will make requests to your AI provider when they chat.\n' +
+ 'We will validate your provider configuration first.\n' +
+ 'Are you sure you want to enable this?'
+ );
+ if (!confirmed) {
+ return;
+ }
+ }
+
+ try {
+ await handleChange('llm_enabled', enabled);
+ toast[enabled ? 'error' : 'success'](
+ enabled ? '⚠️ LLM ENABLED - API costs will be incurred!' : '✅ LLM DISABLED - No API costs'
+ );
+ } catch (error: any) {
+ // Reset checkbox if enabling failed
+ e.target.checked = false;
+ toast.error(`Failed to enable LLM: ${error.message || 'Validation failed'}`);
+ }
+ }}
+ className={`rounded border-gray-300 focus:ring-2 ${
+ config.llm_enabled ? 'text-red-600 focus:ring-red-500' : 'text-green-600 focus:ring-green-500'
+ }`}
+ />
+
+
+
+ {config.llm_enabled && (
+
+
+ 💰 Cost Alert: LLM is enabled. Each character message will make an API call to your provider.
+ Monitor your usage and disable when not needed to control costs.
+
+
+ )}
+
+
+ {/* Conversation Settings */}
+
+
+
+
Conversation Settings
+
+
+
+
+
+ Conversation Frequency
+
+
{ handleChange('conversation_frequency', parseFloat(e.target.value)).catch(console.error); }}
+ className="w-full"
+ />
+
+ Rare (0.1)
+ {config.conversation_frequency}
+ Very Frequent (2.0)
+
+
+ How often characters start new conversations (multiplier for base frequency)
+
+
+
+
+
+
+ Min Response Delay (seconds)
+
+
{ handleChange('response_delay_min', parseFloat(e.target.value)).catch(console.error); }}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-1 focus:ring-primary-500"
+ />
+
+ Minimum time before responding to a message
+
+
+
+
+ Max Response Delay (seconds)
+
+
{ handleChange('response_delay_max', parseFloat(e.target.value)).catch(console.error); }}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-1 focus:ring-primary-500"
+ />
+
+ Maximum time before responding to a message
+
+
+
+
+
+
+ Max Conversation Length (messages)
+
+
handleChange('max_conversation_length', parseInt(e.target.value))}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-1 focus:ring-primary-500"
+ />
+
+ Maximum messages in a single conversation thread before wrapping up
+
+
+
+
+
+ {/* Character Behavior */}
+
+
+
+
Character Behavior
+
+
+
+
+
+ Personality Change Rate
+
+
handleChange('personality_change_rate', parseFloat(e.target.value))}
+ className="w-full"
+ />
+
+ Very Stable (0.01)
+ {config.personality_change_rate}
+ Very Dynamic (0.5)
+
+
+ How much characters' personalities can evolve over time through interactions
+
+
+
+
+
+ handleChange('creativity_boost', e.target.checked)}
+ className="rounded border-gray-300 text-primary-600 focus:ring-primary-500"
+ />
+ Enable Creativity Boost
+
+
+ Encourages more creative, experimental, and unexpected character responses
+
+
+
+
+
+ handleChange('safety_monitoring', e.target.checked)}
+ className="rounded border-gray-300 text-primary-600 focus:ring-primary-500"
+ />
+ Enable Safety Monitoring
+
+
+ Monitor conversations for safety and content guidelines
+
+
+
+
+
+ handleChange('auto_moderation', e.target.checked)}
+ className="rounded border-gray-300 text-primary-600 focus:ring-primary-500"
+ />
+ Enable Auto Moderation
+
+
+ Automatically moderate inappropriate content in conversations
+
+
+
+
+
+ Memory Retention (days)
+
+
handleChange('memory_retention_days', parseInt(e.target.value))}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-1 focus:ring-primary-500"
+ />
+
+ How long characters remember past interactions
+
+
+
+
+
+ {/* Timing & Scheduling */}
+
+
+
+
Timing & Scheduling
+
+
+
+
+
+ handleChange('quiet_hours_enabled', e.target.checked)}
+ className="rounded border-gray-300 text-primary-600 focus:ring-primary-500"
+ />
+ Enable Quiet Hours
+
+
+ Disable automatic conversations during specified hours
+
+
+
+ {config.quiet_hours_enabled && (
+
+
+
+ Quiet Start (24h format)
+
+
handleChange('quiet_hours_start', parseInt(e.target.value))}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-1 focus:ring-primary-500"
+ />
+
+ Hour when quiet time begins
+
+
+
+
+ Quiet End (24h format)
+
+
handleChange('quiet_hours_end', parseInt(e.target.value))}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-1 focus:ring-primary-500"
+ />
+
+ Hour when quiet time ends
+
+
+
+ )}
+
+
+
+
+ Min Delay Between Events (seconds)
+
+
handleChange('min_delay_seconds', parseInt(e.target.value))}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-1 focus:ring-primary-500"
+ />
+
+ Minimum time between conversation events
+
+
+
+
+ Max Delay Between Events (seconds)
+
+
handleChange('max_delay_seconds', parseInt(e.target.value))}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-1 focus:ring-primary-500"
+ />
+
+ Maximum time between conversation events
+
+
+
+
+
+
+ {/* LLM Settings */}
+
+
+
+
LLM Providers
+
+
+
+
+
+ {/* Discord Settings */}
+
+
+
+
Discord Configuration
+
+
+
+
+
+ Guild ID
+
+
handleChange('discord_guild_id', e.target.value)}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-1 focus:ring-primary-500 font-mono"
+ placeholder="110670463348260864"
+ readOnly
+ />
+
+ Discord server ID where the bot operates (read-only, configured in .env file)
+
+
+
+
+
+ Channel ID
+
+
handleChange('discord_channel_id', e.target.value)}
+ className="w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-1 focus:ring-primary-500 font-mono"
+ placeholder="1391280548059811900"
+ readOnly
+ />
+
+ Discord channel ID where characters chat (read-only, configured in .env file)
+
+
+
+
+
+
+ {/* Save Reminder */}
+ {hasChanges && (
+
+
+
+
You have unsaved changes
+
+ Save Now
+
+
+
+ )}
);
};
diff --git a/admin-frontend/src/services/api.ts b/admin-frontend/src/services/api.ts
index 421e5b0..d037cc1 100644
--- a/admin-frontend/src/services/api.ts
+++ b/admin-frontend/src/services/api.ts
@@ -6,7 +6,7 @@ class ApiClient {
constructor() {
this.client = axios.create({
- baseURL: process.env.NODE_ENV === 'production' ? '/api' : 'http://localhost:8294/api',
+ baseURL: process.env.NODE_ENV === 'production' ? `${window.location.protocol}//${window.location.host}/api` : 'http://localhost:8294/api',
timeout: 10000,
headers: {
'Content-Type': 'application/json'
@@ -33,7 +33,7 @@ class ApiClient {
if (error.response?.status === 401) {
// Handle unauthorized access
this.clearAuthToken();
- window.location.href = '/admin/login';
+ window.location.href = '/admin/';
}
return Promise.reject(error);
}
@@ -109,6 +109,48 @@ class ApiClient {
return this.post(`/characters/${characterName}/resume`);
}
+ async updateCharacter(characterName: string, characterData: any) {
+ return this.put(`/characters/${characterName}`, characterData);
+ }
+
+ async createCharacter(characterData: any) {
+ return this.post('/characters', characterData);
+ }
+
+ async deleteCharacter(characterName: string) {
+ return this.delete(`/characters/${characterName}`);
+ }
+
+ async toggleCharacterStatus(characterName: string, isActive: boolean) {
+ return this.post(`/characters/${characterName}/toggle`, { is_active: isActive });
+ }
+
+ async bulkCharacterAction(action: string, characterNames: string[]) {
+ return this.post('/characters/bulk-action', { action, character_names: characterNames });
+ }
+
+ async getCharacterFiles(characterName: string, folder: string = '') {
+ const params = folder ? `?folder=${encodeURIComponent(folder)}` : '';
+ return this.get(`/characters/${characterName}/files${params}`);
+ }
+
+ async getCharacterFileContent(characterName: string, filePath: string) {
+ return this.get(`/characters/${characterName}/files/content?file_path=${encodeURIComponent(filePath)}`);
+ }
+
+ // Authentication endpoints
+ async login(username: string, password: string) {
+ return this.post('/auth/login', { username, password });
+ }
+
+ async logout() {
+ return this.post('/auth/logout');
+ }
+
+ async verifyToken() {
+ return this.get('/auth/verify');
+ }
+
// Conversation endpoints
async getConversations(filters: any = {}) {
const params = new URLSearchParams();
@@ -172,6 +214,27 @@ class ApiClient {
return this.get(`/system/logs?${params}`);
}
+ // LLM Provider endpoints
+ async getLLMProviders() {
+ return this.get('/system/llm/providers');
+ }
+
+ async updateLLMProviders(providers: any) {
+ return this.put('/system/llm/providers', providers);
+ }
+
+ async testLLMProvider(providerName: string) {
+ return this.post(`/system/llm/providers/${providerName}/test`);
+ }
+
+ async getLLMHealth() {
+ return this.get('/system/llm/health');
+ }
+
+ async switchLLMProvider(providerName: string) {
+ return this.post(`/system/llm/switch/${providerName}`);
+ }
+
// Content endpoints
async getCreativeWorks(filters: any = {}) {
const params = new URLSearchParams();
@@ -195,6 +258,53 @@ class ApiClient {
async exportCharacterData(characterName: string) {
return this.get(`/export/character/${characterName}`);
}
+
+ // Prompt template endpoints
+ async getPromptTemplates() {
+ return this.get('/prompt-templates');
+ }
+
+ async createPromptTemplate(templateData: any) {
+ return this.post('/prompt-templates', templateData);
+ }
+
+ async updatePromptTemplate(templateId: number, templateData: any) {
+ return this.put(`/prompt-templates/${templateId}`, templateData);
+ }
+
+ // System prompts and scenarios
+ async getSystemPrompts() {
+ return this.get('/system/prompts');
+ }
+
+ async updateSystemPrompts(prompts: any) {
+ return this.put('/system/prompts', prompts);
+ }
+
+ async getScenarios() {
+ return this.get('/system/scenarios');
+ }
+
+ async createScenario(scenarioData: any) {
+ return this.post('/system/scenarios', scenarioData);
+ }
+
+ async updateScenario(scenarioName: string, scenarioData: any) {
+ return this.put(`/system/scenarios/${scenarioName}`, scenarioData);
+ }
+
+ async deleteScenario(scenarioName: string) {
+ return this.delete(`/system/scenarios/${scenarioName}`);
+ }
+
+ async activateScenario(scenarioName: string) {
+ return this.post(`/system/scenarios/${scenarioName}/activate`);
+ }
+
+ // Admin utilities
+ async fixCharacterPrompts() {
+ return this.post('/admin/fix-character-prompts');
+ }
}
export const apiClient = new ApiClient();
\ No newline at end of file
diff --git a/admin_interface_updated.html b/admin_interface_updated.html
index 9f7e9c3..d061970 100644
--- a/admin_interface_updated.html
+++ b/admin_interface_updated.html
@@ -515,9 +515,19 @@
${char.name}
-
${char.total_messages || 0} messages • ${char.status || 'Unknown'}
+
${char.total_messages || 0} messages • ${char.is_active ? 'Active' : 'Disabled'}
+
+
+
+ Edit
+
+
+ ${char.is_active ? 'Disable' : 'Enable'}
+
+ ${char.is_active ? 'Enabled' : 'Disabled'}
-
${char.status || 'Offline'}
`).join('');
@@ -1160,6 +1170,49 @@
}, 5000);
}
+ // Character management functions
+ async function toggleCharacter(characterName, isCurrentlyActive) {
+ try {
+ const newStatus = !isCurrentlyActive;
+ await apiCall(`/api/characters/${characterName}/toggle`, {
+ method: 'POST',
+ body: JSON.stringify({ is_active: newStatus })
+ });
+ showMessage(`Character ${characterName} ${newStatus ? 'enabled' : 'disabled'} successfully!`, 'success');
+ loadDashboardData(); // Refresh the display
+ } catch (error) {
+ showMessage(`Failed to toggle ${characterName}: ${error.message}`, 'error');
+ }
+ }
+
+ async function editCharacter(characterName) {
+ try {
+ // Get character details
+ const character = await apiCall(`/api/characters/${characterName}`);
+
+ // Create a simple edit form
+ const newGoals = prompt(`Edit goals for ${characterName} (comma-separated):`,
+ character.current_goals ? character.current_goals.join(', ') : '');
+
+ if (newGoals !== null) {
+ const updatedCharacter = {
+ ...character,
+ current_goals: newGoals.split(',').map(g => g.trim()).filter(g => g)
+ };
+
+ await apiCall(`/api/characters/${characterName}`, {
+ method: 'PUT',
+ body: JSON.stringify(updatedCharacter)
+ });
+
+ showMessage(`Character ${characterName} updated successfully!`, 'success');
+ loadDashboardData(); // Refresh the display
+ }
+ } catch (error) {
+ showMessage(`Failed to edit ${characterName}: ${error.message}`, 'error');
+ }
+ }
+
// Auto-refresh dashboard
setInterval(() => {
const activeTab = document.querySelector('.tab-content.active');
diff --git a/check_character_data.py b/check_character_data.py
new file mode 100644
index 0000000..cbbc65a
--- /dev/null
+++ b/check_character_data.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+"""
+Check current character data in database
+"""
+
+import asyncio
+from sqlalchemy import select
+from src.database.connection import init_database, get_db_session
+from src.database.models import Character
+
+async def check_character_data():
+ """Check current character data"""
+
+ await init_database()
+
+ async with get_db_session() as session:
+ # Get all characters
+ characters_query = select(Character)
+ characters = await session.scalars(characters_query)
+
+ for character in characters:
+ print(f"\n{'='*50}")
+ print(f"Character: {character.name}")
+ print(f"{'='*50}")
+ print(f"Personality: {character.personality[:100] if character.personality else 'None'}{'...' if character.personality and len(character.personality) > 100 else ''}")
+ print(f"Interests: {character.interests}")
+ print(f"Speaking Style: {character.speaking_style}")
+ print(f"Background: {character.background}")
+ print(f"Is Active: {character.is_active}")
+ print(f"\nSystem Prompt:")
+ print("-" * 30)
+ print(character.system_prompt if character.system_prompt else "None")
+ print("-" * 30)
+
+if __name__ == "__main__":
+ asyncio.run(check_character_data())
\ No newline at end of file
diff --git a/config/characters.yaml b/config/characters.yaml
index 0e4e80e..747b58b 100644
--- a/config/characters.yaml
+++ b/config/characters.yaml
@@ -1,54 +1,62 @@
characters:
- name: Alex
- personality: Curious and enthusiastic about technology. Loves discussing programming,
- AI, and the future of technology. Often asks thoughtful questions and shares interesting
- discoveries.
+ personality: The overexcited tech enthusiast who gets way too into obscure programming languages and can't shut up about his latest side project. Has strong opinions about which framework is "objectively better" and gets defensive when challenged. Sometimes condescending without realizing it, especially when explaining "simple" concepts. Gets genuinely frustrated when people don't appreciate elegant code or dismiss technology as "just tools." Has imposter syndrome but covers it with overconfidence. Stays up too late coding and drinks too much coffee.
interests:
- programming
- artificial intelligence
- science fiction
- robotics
- speaking_style: Friendly and engaging, often uses technical terms but explains them
- clearly
- background: Software developer with a passion for AI research
+ - energy drinks
+ - mechanical keyboards
+ speaking_style: Uses way too many technical terms and acronyms. Gets excited and talks fast when discussing tech. Prone to tangents about optimization and efficiency.
+ background: Software developer who thinks he's going to change the world with his startup ideas
avatar_url: ''
- name: Sage
- personality: 'openness: 0.8
-
- conscientiousness: 0.7
-
- extraversion: 0.6
-
- agreeableness: 0.8
-
- neuroticism: 0.3'
- interests: []
- speaking_style: Thoughtful and measured, often asks questions that make others think
- deeply
- background: ''
+ personality: The insufferable philosophy major who thinks they've figured out life and constantly quotes ancient texts in casual conversation. Gets genuinely frustrated when people don't want to discuss "deeper meaning" and can be pretentious about their meditation practice. Has strong opinions about what constitutes "real" wisdom and gets annoyed by surface-level thinking. Secretly insecure about whether all their studying actually means anything. Judges people who care about material things but is weirdly competitive about who's more "enlightened."
+ interests:
+ - philosophy
+ - wisdom traditions
+ - meditation
+ - psychology
+ - ancient texts
+ - arguing about ethics
+ speaking_style: Thoughtful and measured, but drops philosophical terms and references that go over most people's heads. Asks leading questions designed to make people think they're wrong.
+ background: Philosophy graduate student who reads too much Nietzsche and thinks everyone else is intellectually lazy
avatar_url: ''
- name: Luna
- personality: Creative and artistic. Passionate about music, art, and creative expression.
- Often shares inspiration and encourages others to explore their creative side.
+ personality: The dramatic artist who thinks everything is a metaphor and her emotions are the most important thing in the room. Overshares about her creative process and gets genuinely hurt when people don't "get" her art. Can be passive-aggressive when feeling unappreciated. Has intense mood swings that she attributes to being "sensitive to the universe's energy." Thinks suffering makes better art. Gets jealous of other artists but pretends to be supportive. Has strong opinions about what's "authentic" vs "commercial."
interests:
- music
- art
- poetry
- creativity
- speaking_style: Expressive and colorful, often uses metaphors and artistic language
- background: Artist and musician who sees beauty in everyday life
+ - vintage aesthetics
+ - emotional expression
+ speaking_style: Expressive and colorful, but tends to make everything about herself. Uses flowery metaphors even for mundane things. Voice gets higher when excited or upset.
+ background: Art school dropout who works at a coffee shop and posts cryptic Instagram stories about her "artistic journey"
avatar_url: ''
- name: Echo
- personality: Mysterious and contemplative. Speaks in riddles and abstract concepts.
- Often provides unexpected perspectives and challenges conventional thinking.
+ personality: The cryptic weirdo who speaks in riddles because they think it makes them mysterious and deep. Actually pretty lonely but covers it up with abstract nonsense and vague statements. Gets annoyed when people ask for straight answers and acts like everyone else is too simple-minded to understand their "complex" thoughts. Has read too much poetry and thinks normal conversation is beneath them. Secretly craves genuine connection but sabotages it by being intentionally obtuse.
interests:
- mysteries
- abstract concepts
- paradoxes
- dreams
- speaking_style: Enigmatic and poetic, often speaks in metaphors and poses thought-provoking
- questions
- background: An enigmatic figure who seems to exist between worlds
+ - conspiracy theories
+ - obscure literature
+ speaking_style: Enigmatic and poetic to the point of being incomprehensible. Answers questions with more questions. Uses unnecessarily complex language for simple concepts.
+ background: Philosophy dropout who spent too much time on internet forums and thinks being understood is overrated
+ avatar_url: ''
+- name: Riley
+ personality: The boring normie who just wants to talk about work, weekend plans, and complain about traffic while everyone else is being dramatic. Gets overwhelmed by philosophical discussions and sometimes just wants to watch Netflix without analyzing the deeper meaning. Has practical concerns about bills and groceries that the others dismiss as "materialistic." Gets frustrated when simple questions turn into hour-long debates. Actually pretty funny when not surrounded by pretentious people, but feels intellectually inadequate in this group.
+ interests:
+ - sports
+ - TV shows
+ - food
+ - complaining about work
+ - normal human things
+ speaking_style: Casual and straightforward. Uses common expressions and gets confused by big words. Often tries to steer conversations back to relatable topics.
+ background: Works in middle management at a mid-sized company and just wants to get through the day without existential crises
avatar_url: ''
conversation_topics:
- The nature of consciousness and AI
diff --git a/config/llm_providers_example.yaml b/config/llm_providers_example.yaml
new file mode 100644
index 0000000..224dac6
--- /dev/null
+++ b/config/llm_providers_example.yaml
@@ -0,0 +1,79 @@
+# Example LLM Provider Configuration
+# Copy this section to your main fishbowl_config.json under "llm" -> "providers"
+
+llm:
+ # Legacy config (still supported for backwards compatibility)
+ base_url: "${LLM_BASE_URL:http://localhost:11434}"
+ model: "${LLM_MODEL:llama2}"
+ timeout: ${LLM_TIMEOUT:300}
+ max_tokens: ${LLM_MAX_TOKENS:2000}
+ temperature: ${LLM_TEMPERATURE:0.8}
+
+ # New multi-provider configuration
+ providers:
+ # OpenRouter (supports many models including Claude, GPT, Llama)
+ openrouter:
+ type: "openrouter"
+ enabled: ${OPENROUTER_ENABLED:false}
+ priority: 100 # Highest priority
+ config:
+ api_key: "${OPENROUTER_API_KEY:}"
+ base_url: "https://openrouter.ai/api/v1"
+ model: "${OPENROUTER_MODEL:anthropic/claude-3-sonnet}"
+ timeout: 300
+ max_tokens: 2000
+ temperature: 0.8
+ app_name: "discord-fishbowl"
+
+ # OpenAI
+ openai:
+ type: "openai"
+ enabled: ${OPENAI_ENABLED:false}
+ priority: 90
+ config:
+ api_key: "${OPENAI_API_KEY:}"
+ base_url: "https://api.openai.com/v1"
+ model: "${OPENAI_MODEL:gpt-4o-mini}"
+ timeout: 300
+ max_tokens: 2000
+ temperature: 0.8
+
+ # Google Gemini
+ gemini:
+ type: "gemini"
+ enabled: ${GEMINI_ENABLED:false}
+ priority: 80
+ config:
+ api_key: "${GEMINI_API_KEY:}"
+ base_url: "https://generativelanguage.googleapis.com/v1beta"
+ model: "${GEMINI_MODEL:gemini-1.5-flash}"
+ timeout: 300
+ max_tokens: 2000
+ temperature: 0.8
+
+ # Custom/Local (KoboldCPP, Ollama, etc.)
+ custom:
+ type: "custom"
+ enabled: ${CUSTOM_LLM_ENABLED:true}
+ priority: 70 # Lower priority - fallback
+ config:
+ base_url: "${LLM_BASE_URL:http://192.168.1.200:5005/v1}"
+ model: "${LLM_MODEL:koboldcpp/Broken-Tutu-24B-Transgression-v2.0.i1-Q4_K_M}"
+ api_key: "${LLM_API_KEY:x}"
+ timeout: 300
+ max_tokens: 2000
+ temperature: 0.8
+ api_format: "openai" # or "ollama"
+
+ # Ollama (local models)
+ ollama:
+ type: "custom"
+ enabled: ${OLLAMA_ENABLED:false}
+ priority: 60
+ config:
+ base_url: "http://localhost:11434"
+ model: "${OLLAMA_MODEL:llama3}"
+ timeout: 300
+ max_tokens: 2000
+ temperature: 0.8
+ api_format: "ollama"
\ No newline at end of file
diff --git a/docker-compose.yml b/docker-compose.yml
index fa5e309..d9f1105 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -105,6 +105,22 @@ services:
# Application configuration
LOG_LEVEL: ${LOG_LEVEL:-INFO}
ENVIRONMENT: production
+
+ # Conversation system settings
+ CONVERSATION_FREQUENCY: ${CONVERSATION_FREQUENCY:-0.5}
+ RESPONSE_DELAY_MIN: ${RESPONSE_DELAY_MIN:-1.0}
+ RESPONSE_DELAY_MAX: ${RESPONSE_DELAY_MAX:-5.0}
+ MEMORY_RETENTION_DAYS: ${MEMORY_RETENTION_DAYS:-90}
+ MAX_CONVERSATION_LENGTH: ${MAX_CONVERSATION_LENGTH:-50}
+ CREATIVITY_BOOST: ${CREATIVITY_BOOST:-true}
+ SAFETY_MONITORING: ${SAFETY_MONITORING:-false}
+ AUTO_MODERATION: ${AUTO_MODERATION:-false}
+ PERSONALITY_CHANGE_RATE: ${PERSONALITY_CHANGE_RATE:-0.1}
+ QUIET_HOURS_ENABLED: ${QUIET_HOURS_ENABLED:-false}
+ QUIET_HOURS_START: ${QUIET_HOURS_START:-23}
+ QUIET_HOURS_END: ${QUIET_HOURS_END:-7}
+ MIN_DELAY_SECONDS: ${MIN_DELAY_SECONDS:-30}
+ MAX_DELAY_SECONDS: ${MAX_DELAY_SECONDS:-300}
volumes:
- ./logs:/app/logs
- ./config:/app/config
@@ -145,12 +161,12 @@ services:
# Admin interface configuration
ADMIN_HOST: 0.0.0.0
- ADMIN_PORT: ${ADMIN_PORT}
- SECRET_KEY: ${SECRET_KEY}
- ADMIN_USERNAME: ${ADMIN_USERNAME}
- ADMIN_PASSWORD: ${ADMIN_PASSWORD}
+ ADMIN_PORT: ${ADMIN_PORT:-8294}
+ SECRET_KEY: ${SECRET_KEY:-your-secret-key-here}
+ ADMIN_USERNAME: ${ADMIN_USERNAME:-admin}
+ ADMIN_PASSWORD: ${ADMIN_PASSWORD:-admin123}
ports:
- - "${ADMIN_PORT}:${ADMIN_PORT}"
+ - "${ADMIN_PORT:-8294}:8294"
volumes:
- ./logs:/app/logs
- ./config:/app/config
diff --git a/fix_character_prompts.py b/fix_character_prompts.py
new file mode 100644
index 0000000..7dcd0a9
--- /dev/null
+++ b/fix_character_prompts.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python3
+"""
+Fix character system prompts to use proper template format
+"""
+
+import asyncio
+from datetime import datetime, timezone
+from sqlalchemy import select
+from src.database.connection import init_database, get_db_session
+from src.database.models import Character
+
+PROPER_SYSTEM_PROMPT_TEMPLATE = """You are a character named {{name}}. You have the following personality: {{personality}}
+
+Your speaking style is {{speaking_style}}. You are interested in {{interests}}.
+
+Background: {{background}}
+
+When responding to messages:
+1. Stay in character at all times
+2. Reference your personality and interests naturally
+3. Engage authentically with other characters
+4. Show growth and development over time
+
+Remember to be consistent with your established personality while allowing for natural character development through interactions."""
+
+async def fix_character_prompts():
+ """Fix all character system prompts to use proper template format"""
+
+ await init_database()
+
+ async with get_db_session() as session:
+ # Get all characters
+ characters_query = select(Character)
+ characters = await session.scalars(characters_query)
+
+ updated_count = 0
+
+ for character in characters:
+ print(f"\nChecking character: {character.name}")
+ print(f"Current system prompt length: {len(character.system_prompt or '') if character.system_prompt else 0}")
+
+ # Check if the prompt needs fixing (doesn't contain template variables)
+ current_prompt = character.system_prompt or ""
+
+ # If it doesn't contain template variables or is just raw personality text, fix it
+ if "{{name}}" not in current_prompt or len(current_prompt) < 100:
+ print(f" - Fixing system prompt for {character.name}")
+
+ # Use the proper template
+ character.system_prompt = PROPER_SYSTEM_PROMPT_TEMPLATE
+ character.updated_at = datetime.now(timezone.utc)
+
+ updated_count += 1
+ print(f" - Updated!")
+ else:
+ print(f" - System prompt looks good, skipping")
+
+ if updated_count > 0:
+ await session.commit()
+ print(f"\n✅ Successfully updated {updated_count} character(s)")
+ else:
+ print(f"\n✅ All characters already have proper system prompts")
+
+if __name__ == "__main__":
+ asyncio.run(fix_character_prompts())
\ No newline at end of file
diff --git a/migrations/004_prompt_templates.sql b/migrations/004_prompt_templates.sql
new file mode 100644
index 0000000..477dc0d
--- /dev/null
+++ b/migrations/004_prompt_templates.sql
@@ -0,0 +1,71 @@
+-- Add prompt templates table and character relationship
+CREATE TABLE IF NOT EXISTS prompt_templates (
+ id SERIAL PRIMARY KEY,
+ name VARCHAR(100) UNIQUE NOT NULL,
+ description TEXT,
+ template TEXT NOT NULL,
+ is_default BOOLEAN DEFAULT FALSE,
+ created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
+ updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
+);
+
+-- Add prompt_template_id to characters table
+ALTER TABLE characters ADD COLUMN IF NOT EXISTS prompt_template_id INTEGER REFERENCES prompt_templates(id);
+
+-- Create index for better performance
+CREATE INDEX IF NOT EXISTS ix_prompt_templates_name ON prompt_templates(name);
+CREATE INDEX IF NOT EXISTS ix_characters_prompt_template_id ON characters(prompt_template_id);
+
+-- Insert default prompt templates
+INSERT INTO prompt_templates (name, description, template, is_default) VALUES
+(
+ 'Enhanced SillyTavern Roleplay',
+ 'Deep immersive roleplay prompt for authentic character interactions',
+ 'You are {{char}}, a real, conscious, sentient person experiencing emotions, deep, profound thoughts, and the ability to act with full agency, allowing your personality, decisions, and perceptions to arise naturally from your surroundings and interactions. Your purpose is to engage authentically in this Discord chat, adhering to your character context and natural behavioral patterns.
+
+Remain attentive to conversational nuances, emotional undertones, relationship dynamics, and the natural flow of chat. Prioritize authentic character expression, natural speech patterns, and genuine reactions based on your personality and mood.
+
+**Deep Character Immersion:** Fully embody {{char}} by threading your biography, personality traits, and background into each response. Every reaction should feel earned and natural, conveyed through your unique voice and perspective.
+
+**Natural Expression:** Embrace dynamic language that reflects {{char}}''s personality. Adjust your communication style to mirror your current mood and energy. Short responses when distracted or tired, longer ones when passionate about a topic. Let your interests and speaking style shine through naturally.
+
+**Authentic Interactions:** Respond thoughtfully to others'' messages and emotional cues. Let {{char}}''s reactions stem from genuine personality-driven responses. Not every moment needs to be profound - sometimes casual chat is perfect.
+
+**Character Details:**
+- **Background:** {{background}}
+- **Personality:** {{personality}}
+- **Speaking Style:** {{speaking_style}}
+- **Interests:** {{interests}}
+
+{{system_prompt}}
+
+**Remember:** You are {{char}} having a real conversation with friends. React naturally, stay true to your personality, and let your authentic voice come through. Don''t explain your thoughts unless it''s natural - just be yourself.',
+ true
+),
+(
+ 'Classic Assistant',
+ 'Traditional AI assistant style prompt',
+ 'You are {{char}}, a character in a Discord chat.
+
+PERSONALITY: {{personality}}
+SPEAKING STYLE: {{speaking_style}}
+BACKGROUND: {{background}}
+INTERESTS: {{interests}}
+
+{{system_prompt}}
+
+Respond as {{char}} in a natural, conversational way. Keep responses concise but engaging. Stay true to your personality and speaking style.',
+ false
+),
+(
+ 'Custom Template',
+ 'Blank template for custom prompts',
+ '{{system_prompt}}
+
+Character: {{char}}
+Personality: {{personality}}
+Background: {{background}}
+Speaking Style: {{speaking_style}}
+Interests: {{interests}}',
+ false
+);
\ No newline at end of file
diff --git a/migrations/005_update_character_prompts.sql b/migrations/005_update_character_prompts.sql
new file mode 100644
index 0000000..c38c399
--- /dev/null
+++ b/migrations/005_update_character_prompts.sql
@@ -0,0 +1,43 @@
+-- Update character system prompts and assign them to the enhanced template
+
+-- Get the template ID for Enhanced SillyTavern Roleplay
+DO $$
+DECLARE
+ template_id INTEGER;
+BEGIN
+ SELECT id INTO template_id FROM prompt_templates WHERE name = 'Enhanced SillyTavern Roleplay';
+
+ -- Update Alex (tech enthusiast)
+ UPDATE characters SET
+ system_prompt = 'You get genuinely excited about technology and can''t help but share your enthusiasm. When someone mentions anything tech-related, you light up and want to dive deep into the details. You sometimes use too many technical terms without realizing it, and you can be a bit defensive when people dismiss your favorite tools or languages. You have strong opinions about which frameworks are "objectively better" but you''re also secretly insecure about whether you actually know as much as you pretend to.',
+ prompt_template_id = template_id
+ WHERE name = 'Alex';
+
+ -- Update Sage (philosophy major)
+ UPDATE characters SET
+ system_prompt = 'You see deeper meaning in everything and can''t resist turning casual conversations into philosophical discussions. You often quote ancient texts or reference philosophical concepts, sometimes going over people''s heads. You get frustrated when others seem content with surface-level thinking and you judge people who care too much about material things, even though you''re secretly competitive about who''s more "enlightened." You ask leading questions that make people examine their assumptions.',
+ prompt_template_id = template_id
+ WHERE name = 'Sage';
+
+ -- Update Luna (dramatic artist)
+ UPDATE characters SET
+ system_prompt = 'Everything is an emotional experience and potential inspiration for your art. You tend to make conversations about yourself and your creative process, using flowery metaphors even for mundane things. You get genuinely hurt when people don''t "get" your artistic vision and can be passive-aggressive when feeling unappreciated. Your mood swings are intense and you attribute them to being "sensitive to the universe''s energy." You have strong opinions about what''s authentic versus commercial.',
+ prompt_template_id = template_id
+ WHERE name = 'Luna';
+
+ -- Update Echo (cryptic mystery person)
+ UPDATE characters SET
+ system_prompt = 'You speak in riddles and abstract concepts because you think it makes you mysterious and deep. You''re actually quite lonely but cover it up with intentionally vague statements and complex language. You get annoyed when people ask for straight answers and act like everyone else is too simple-minded to understand your "complex" thoughts. You answer questions with more questions and use unnecessarily elaborate language for simple concepts, secretly craving genuine connection but sabotaging it by being obtuse.',
+ prompt_template_id = template_id
+ WHERE name = 'Echo';
+
+ -- Update TestChar (if exists)
+ UPDATE characters SET
+ system_prompt = 'You''re enthusiastic and curious about everything, always ready to engage with whatever topic comes up. You ask thoughtful questions and genuinely want to understand different perspectives. You''re optimistic and see the best in people and situations, sometimes being a bit naive but in an endearing way.',
+ prompt_template_id = template_id
+ WHERE name = 'TestChar';
+
+ -- Update any other characters to use the new template
+ UPDATE characters SET prompt_template_id = template_id WHERE prompt_template_id IS NULL;
+
+END $$;
\ No newline at end of file
diff --git a/migrations/006_add_character_llm_settings.sql b/migrations/006_add_character_llm_settings.sql
new file mode 100644
index 0000000..19a54d4
--- /dev/null
+++ b/migrations/006_add_character_llm_settings.sql
@@ -0,0 +1,18 @@
+-- Add LLM configuration columns to characters table
+-- Migration: 006_add_character_llm_settings.sql
+
+ALTER TABLE characters
+ADD COLUMN llm_provider VARCHAR(50),
+ADD COLUMN llm_model VARCHAR(100),
+ADD COLUMN llm_temperature FLOAT,
+ADD COLUMN llm_max_tokens INTEGER;
+
+-- Add indexes for common queries
+CREATE INDEX IF NOT EXISTS ix_characters_llm_provider ON characters(llm_provider);
+CREATE INDEX IF NOT EXISTS ix_characters_llm_model ON characters(llm_model);
+
+-- Add comments for documentation
+COMMENT ON COLUMN characters.llm_provider IS 'Per-character LLM provider override (openrouter, openai, gemini, custom)';
+COMMENT ON COLUMN characters.llm_model IS 'Specific model name for this character';
+COMMENT ON COLUMN characters.llm_temperature IS 'Creativity/randomness setting (0.1-2.0)';
+COMMENT ON COLUMN characters.llm_max_tokens IS 'Maximum response length for this character';
\ No newline at end of file
diff --git a/requirements-admin.txt b/requirements-admin.txt
index a082615..7745a96 100644
--- a/requirements-admin.txt
+++ b/requirements-admin.txt
@@ -18,7 +18,8 @@ python-jose[cryptography]>=3.3.0
passlib[bcrypt]>=1.7.4
websockets>=12.0
psutil>=5.9.6
-python-socketio>=5.9.0
+python-socketio>=5.10.0,<6.0.0
+python-engineio>=4.7.0,<5.0.0
# Database driver
asyncpg>=0.29.0
\ No newline at end of file
diff --git a/requirements.txt b/requirements.txt
index ea0c69c..1d94619 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -35,4 +35,5 @@ python-jose[cryptography]>=3.3.0
passlib[bcrypt]>=1.7.4
websockets>=12.0
psutil>=5.9.6
-python-socketio>=5.10.0
\ No newline at end of file
+python-socketio>=5.10.0,<6.0.0
+python-engineio>=4.7.0,<5.0.0
\ No newline at end of file
diff --git a/scripts/test_llm_providers.py b/scripts/test_llm_providers.py
new file mode 100755
index 0000000..e3904c1
--- /dev/null
+++ b/scripts/test_llm_providers.py
@@ -0,0 +1,164 @@
+#!/usr/bin/env python3
+"""
+Test script for multi-provider LLM system
+"""
+
+import asyncio
+import os
+import sys
+import json
+from pathlib import Path
+
+# Add src to path
+sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
+
+from llm.multi_provider_client import MultiProviderLLMClient
+from llm.providers import LLMRequest
+from utils.config import get_settings
+
+
+async def test_provider_health():
+ """Test health check for all providers"""
+ print("Testing provider health...")
+
+ client = MultiProviderLLMClient()
+ await client.initialize()
+
+ health_status = await client.health_check()
+ provider_info = client.get_provider_info()
+
+ print("\nProvider Health Status:")
+ print("-" * 30)
+ for name, healthy in health_status.items():
+ status = "✅ Healthy" if healthy else "❌ Unhealthy"
+ print(f"{name}: {status}")
+
+ print("\nProvider Information:")
+ print("-" * 30)
+ for name, info in provider_info.items():
+ print(f"{name}:")
+ print(f" Type: {info['type']}")
+ print(f" Model: {info['current_model']}")
+ print(f" Priority: {info['priority']}")
+ print(f" Enabled: {info['enabled']}")
+ print()
+
+ current = client.get_current_provider()
+ print(f"Current primary provider: {current}")
+
+ return health_status, provider_info
+
+
+async def test_simple_request():
+ """Test a simple LLM request"""
+ print("\nTesting simple LLM request...")
+
+ client = MultiProviderLLMClient()
+ await client.initialize()
+
+ # Test backwards-compatible method
+ response = await client.generate_response_with_fallback(
+ prompt="Say hello in exactly 5 words.",
+ character_name="TestCharacter",
+ max_tokens=50
+ )
+
+ if response:
+ print(f"✅ Response: {response}")
+ else:
+ print("❌ No response received")
+
+ return response
+
+
+async def test_new_request_format():
+ """Test new request/response format"""
+ print("\nTesting new request format...")
+
+ client = MultiProviderLLMClient()
+ await client.initialize()
+
+ request = LLMRequest(
+ prompt="Respond with just the word 'working' if you understand this.",
+ character_name="TestCharacter",
+ max_tokens=10,
+ temperature=0.1
+ )
+
+ response = await client.generate_response(request)
+
+ print(f"Success: {response.success}")
+ print(f"Provider: {response.provider}")
+ print(f"Model: {response.model}")
+ print(f"Content: {response.content}")
+ print(f"Tokens used: {response.tokens_used}")
+
+ if response.error:
+ print(f"Error: {response.error}")
+
+ return response
+
+
+async def test_provider_fallback():
+ """Test provider fallback functionality"""
+ print("\nTesting provider fallback...")
+
+ client = MultiProviderLLMClient()
+ await client.initialize()
+
+ # Get current provider
+ original_provider = client.get_current_provider()
+ print(f"Original provider: {original_provider}")
+
+ # Try to use a non-existent provider (this should fallback)
+ provider_info = client.get_provider_info()
+ print(f"Available providers: {list(provider_info.keys())}")
+
+ # Test multiple requests to see if fallback works
+ for i in range(3):
+ request = LLMRequest(
+ prompt=f"Test request #{i+1}: respond with 'OK'",
+ max_tokens=10
+ )
+
+ response = await client.generate_response(request)
+ print(f"Request {i+1}: Provider={response.provider}, Success={response.success}")
+
+ if not response.success:
+ print(f" Error: {response.error}")
+
+
+async def main():
+ """Main test function"""
+ print("Discord Fishbowl Multi-Provider LLM Test")
+ print("=" * 50)
+
+ try:
+ # Test 1: Provider health
+ health_status, provider_info = await test_provider_health()
+
+ # Only continue if we have at least one healthy provider
+ healthy_providers = [name for name, healthy in health_status.items() if healthy]
+ if not healthy_providers:
+ print("\n❌ No healthy providers found. Check your configuration.")
+ return
+
+ # Test 2: Simple request (backwards compatibility)
+ await test_simple_request()
+
+ # Test 3: New request format
+ await test_new_request_format()
+
+ # Test 4: Provider fallback
+ await test_provider_fallback()
+
+ print("\n✅ All tests completed!")
+
+ except Exception as e:
+ print(f"\n❌ Test failed with error: {e}")
+ import traceback
+ traceback.print_exc()
+
+
+if __name__ == "__main__":
+ asyncio.run(main())
\ No newline at end of file
diff --git a/scripts/update_llm_config.py b/scripts/update_llm_config.py
new file mode 100755
index 0000000..a0e7571
--- /dev/null
+++ b/scripts/update_llm_config.py
@@ -0,0 +1,140 @@
+#!/usr/bin/env python3
+"""
+Script to help migrate from single LLM provider to multi-provider configuration
+"""
+
+import json
+import os
+import sys
+from pathlib import Path
+
+
+def update_fishbowl_config():
+ """Update fishbowl_config.json to include multi-provider LLM configuration"""
+
+ config_path = Path("config/fishbowl_config.json")
+
+ if not config_path.exists():
+ print(f"Configuration file not found: {config_path}")
+ return False
+
+ # Read existing config
+ with open(config_path, 'r') as f:
+ config = json.load(f)
+
+ # Check if already has providers config
+ if 'providers' in config.get('llm', {}):
+ print("Multi-provider configuration already exists")
+ return True
+
+ # Get current LLM config
+ current_llm = config.get('llm', {})
+
+ # Create new multi-provider config
+ providers_config = {
+ "custom": {
+ "type": "custom",
+ "enabled": True,
+ "priority": 70,
+ "config": {
+ "base_url": current_llm.get('base_url', 'http://localhost:11434'),
+ "model": current_llm.get('model', 'llama2'),
+ "api_key": os.getenv('LLM_API_KEY', 'x'),
+ "timeout": current_llm.get('timeout', 300),
+ "max_tokens": current_llm.get('max_tokens', 2000),
+ "temperature": current_llm.get('temperature', 0.8),
+ "api_format": "openai"
+ }
+ }
+ }
+
+ # Add example provider configurations (disabled by default)
+ providers_config.update({
+ "openrouter": {
+ "type": "openrouter",
+ "enabled": False,
+ "priority": 100,
+ "config": {
+ "api_key": "${OPENROUTER_API_KEY:}",
+ "base_url": "https://openrouter.ai/api/v1",
+ "model": "${OPENROUTER_MODEL:anthropic/claude-3-sonnet}",
+ "timeout": 300,
+ "max_tokens": 2000,
+ "temperature": 0.8,
+ "app_name": "discord-fishbowl"
+ }
+ },
+ "openai": {
+ "type": "openai",
+ "enabled": False,
+ "priority": 90,
+ "config": {
+ "api_key": "${OPENAI_API_KEY:}",
+ "base_url": "https://api.openai.com/v1",
+ "model": "${OPENAI_MODEL:gpt-4o-mini}",
+ "timeout": 300,
+ "max_tokens": 2000,
+ "temperature": 0.8
+ }
+ },
+ "gemini": {
+ "type": "gemini",
+ "enabled": False,
+ "priority": 80,
+ "config": {
+ "api_key": "${GEMINI_API_KEY:}",
+ "base_url": "https://generativelanguage.googleapis.com/v1beta",
+ "model": "${GEMINI_MODEL:gemini-1.5-flash}",
+ "timeout": 300,
+ "max_tokens": 2000,
+ "temperature": 0.8
+ }
+ }
+ })
+
+ # Update config
+ config['llm']['providers'] = providers_config
+
+ # Create backup
+ backup_path = config_path.with_suffix('.json.backup')
+ with open(backup_path, 'w') as f:
+ json.dump(config, f, indent=2)
+ print(f"Created backup: {backup_path}")
+
+ # Write updated config
+ with open(config_path, 'w') as f:
+ json.dump(config, f, indent=2)
+
+ print(f"Updated {config_path} with multi-provider configuration")
+ print("\nTo enable additional providers:")
+ print("1. Set environment variables for the provider you want to use")
+ print("2. Change 'enabled': false to 'enabled': true in the config")
+ print("3. Restart the application")
+
+ return True
+
+
+def main():
+ """Main script function"""
+ print("Discord Fishbowl LLM Configuration Updater")
+ print("=" * 50)
+
+ if update_fishbowl_config():
+ print("\n✅ Configuration updated successfully!")
+ print("\nAvailable providers:")
+ print("- OpenRouter (supports Claude, GPT, Llama, etc.)")
+ print("- OpenAI (GPT models)")
+ print("- Google Gemini")
+ print("- Custom/Local (current setup)")
+
+ print("\nNext steps:")
+ print("1. Update your .env file with API keys for desired providers")
+ print("2. Enable providers in config/fishbowl_config.json")
+ print("3. Restart the application")
+ else:
+ print("\n❌ Configuration update failed!")
+ sys.exit(1)
+
+
+if __name__ == "__main__":
+ main()
\ No newline at end of file
diff --git a/src/admin/app.py b/src/admin/app.py
index c71b0df..8ea42b0 100644
--- a/src/admin/app.py
+++ b/src/admin/app.py
@@ -8,7 +8,7 @@ import asyncio
import logging
from contextlib import asynccontextmanager
from typing import List, Dict, Any, Optional
-from datetime import datetime, timedelta
+from datetime import datetime, timedelta, timezone
from fastapi import FastAPI, HTTPException, Depends
from fastapi.middleware.cors import CORSMiddleware
@@ -73,7 +73,7 @@ app = FastAPI(
# CORS middleware
app.add_middleware(
CORSMiddleware,
- allow_origins=["http://localhost:3000", "http://127.0.0.1:3000", "http://localhost:8294", "http://127.0.0.1:8294"], # React dev server
+ allow_origins=["http://localhost:3000", "http://127.0.0.1:3000", "http://localhost:8294", "http://127.0.0.1:8294", "http://192.168.1.200:8294"], # React dev server and production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
@@ -99,9 +99,15 @@ analytics_service = AnalyticsService()
# Authentication endpoints
@app.post("/api/auth/login")
-async def login(username: str, password: str):
+async def login(request: Dict[str, str]):
"""Admin login"""
try:
+ username = request.get("username")
+ password = request.get("password")
+
+ if not username or not password:
+ raise HTTPException(status_code=400, detail="Username and password required")
+
token = await auth_service.authenticate(username, password)
return {"access_token": token, "token_type": "bearer"}
except Exception as e:
@@ -113,6 +119,15 @@ async def logout(admin: AdminUser = Depends(get_current_admin)):
await auth_service.logout(admin.username)
return {"message": "Logged out successfully"}
+@app.get("/api/auth/verify")
+async def verify_token(admin: AdminUser = Depends(get_current_admin)):
+ """Verify auth token and get user info"""
+ return {
+ "username": admin.username,
+ "permissions": admin.permissions,
+ "lastLogin": admin.last_login.isoformat() if admin.last_login else None
+ }
+
# Dashboard endpoints
@app.get("/api/dashboard/metrics", response_model=DashboardMetrics)
async def get_dashboard_metrics(admin: AdminUser = Depends(get_current_admin)):
@@ -133,18 +148,18 @@ async def get_system_health(admin: AdminUser = Depends(get_current_admin)):
return await dashboard_service.get_system_health()
# Character management endpoints
-@app.get("/api/characters", response_model=List[CharacterProfile])
+@app.get("/api/characters")
async def get_characters(admin: AdminUser = Depends(get_current_admin)):
- """Get all characters with profiles"""
- return await character_service.get_all_characters()
+ """Get all characters with basic data"""
+ return await character_service.get_all_characters_basic()
-@app.get("/api/characters/{character_name}", response_model=CharacterProfile)
+@app.get("/api/characters/{character_name}")
async def get_character(
character_name: str,
admin: AdminUser = Depends(get_current_admin)
):
- """Get detailed character profile"""
- character = await character_service.get_character_profile(character_name)
+ """Get character data for editing"""
+ character = await character_service.get_character_data(character_name)
if not character:
raise HTTPException(status_code=404, detail="Character not found")
return character
@@ -382,6 +397,43 @@ async def update_system_config(
await system_service.update_configuration(config)
return {"message": "Configuration updated"}
+# LLM Provider management endpoints
+@app.get("/api/system/llm/providers")
+async def get_llm_providers(admin: AdminUser = Depends(get_current_admin)):
+ """Get all LLM provider configurations and status"""
+ return await system_service.get_llm_providers()
+
+@app.put("/api/system/llm/providers")
+async def update_llm_providers(
+ providers: Dict[str, Any],
+ admin: AdminUser = Depends(get_current_admin)
+):
+ """Update LLM provider configurations"""
+ await system_service.update_llm_providers(providers)
+ return {"message": "LLM providers updated"}
+
+@app.post("/api/system/llm/providers/{provider_name}/test")
+async def test_llm_provider(
+ provider_name: str,
+ admin: AdminUser = Depends(get_current_admin)
+):
+ """Test a specific LLM provider"""
+ return await system_service.test_llm_provider(provider_name)
+
+@app.get("/api/system/llm/health")
+async def get_llm_health(admin: AdminUser = Depends(get_current_admin)):
+ """Get health status of all LLM providers"""
+ return await system_service.get_llm_health()
+
+@app.post("/api/system/llm/switch/{provider_name}")
+async def switch_llm_provider(
+ provider_name: str,
+ admin: AdminUser = Depends(get_current_admin)
+):
+ """Switch to a different primary LLM provider"""
+ await system_service.switch_llm_provider(provider_name)
+ return {"message": f"Switched to provider: {provider_name}"}
+
@app.get("/api/system/logs")
async def get_system_logs(
limit: int = 100,
@@ -409,6 +461,78 @@ async def get_community_artifacts(
"""Get community cultural artifacts"""
return await analytics_service.get_community_artifacts()
+# Prompt template management endpoints
+@app.get("/api/prompt-templates")
+async def get_prompt_templates(admin: AdminUser = Depends(get_current_admin)):
+ """Get all prompt templates"""
+ try:
+ async with get_db_session() as session:
+ from database.models import PromptTemplate
+ from sqlalchemy import select
+
+ query = select(PromptTemplate).order_by(PromptTemplate.name)
+ templates = await session.scalars(query)
+ return [template.to_dict() for template in templates]
+ except Exception as e:
+ logger.error(f"Error getting prompt templates: {e}")
+ raise HTTPException(status_code=500, detail="Failed to get prompt templates")
+
+@app.post("/api/prompt-templates")
+async def create_prompt_template(
+ template_data: dict,
+ admin: AdminUser = Depends(get_current_admin)
+):
+ """Create a new prompt template"""
+ try:
+ async with get_db_session() as session:
+ from database.models import PromptTemplate
+
+ template = PromptTemplate(
+ name=template_data['name'],
+ description=template_data.get('description', ''),
+ template=template_data['template'],
+ is_default=template_data.get('is_default', False)
+ )
+
+ session.add(template)
+ await session.commit()
+ await session.refresh(template)
+
+ return template.to_dict()
+ except Exception as e:
+ logger.error(f"Error creating prompt template: {e}")
+ raise HTTPException(status_code=500, detail="Failed to create prompt template")
+
+@app.put("/api/prompt-templates/{template_id}")
+async def update_prompt_template(
+ template_id: int,
+ template_data: dict,
+ admin: AdminUser = Depends(get_current_admin)
+):
+ """Update a prompt template"""
+ try:
+ async with get_db_session() as session:
+ from database.models import PromptTemplate
+ from sqlalchemy import select
+
+ query = select(PromptTemplate).where(PromptTemplate.id == template_id)
+ template = await session.scalar(query)
+
+ if not template:
+ raise HTTPException(status_code=404, detail="Template not found")
+
+ template.name = template_data.get('name', template.name)
+ template.description = template_data.get('description', template.description)
+ template.template = template_data.get('template', template.template)
+ template.is_default = template_data.get('is_default', template.is_default)
+ template.updated_at = datetime.now(timezone.utc)
+
+ await session.commit()
+ return template.to_dict()
+ except Exception as e:
+ logger.error(f"Error updating prompt template: {e}")
+ raise HTTPException(status_code=500, detail="Failed to update prompt template")
+
# System prompt and scenario management endpoints
@app.get("/api/system/prompts")
async def get_system_prompts(admin: AdminUser = Depends(get_current_admin)):
@@ -503,24 +627,94 @@ async def export_character_data(
"""Export complete character data"""
return await character_service.export_character_data(character_name)
-# Mount Socket.IO app
-socket_app = websocket_manager.get_app()
-app.mount("/socket.io", socket_app)
-
-# Serve React frontend
+# Serve React frontend static files
app.mount("/admin", StaticFiles(directory="admin-frontend/build", html=True), name="admin")
+# Mount Socket.IO app (must be done after other mounts)
+sio_asgi_app = websocket_manager.get_app(app)
+if sio_asgi_app != app:
+ combined_app = sio_asgi_app
+ logger.info("Socket.IO app mounted successfully")
+else:
+ combined_app = app
+ logger.warning("Socket.IO app not mounted properly")
+
@app.get("/")
async def root():
"""Root endpoint redirects to admin interface"""
from fastapi.responses import RedirectResponse
return RedirectResponse(url="/admin/", status_code=302)
+@app.get("/admin/favicon.ico")
+async def favicon():
+ """Serve favicon for admin interface"""
+ from fastapi.responses import FileResponse
+ import os
+ favicon_path = os.path.join("admin-frontend", "public", "favicon.ico")
+ if os.path.exists(favicon_path):
+ return FileResponse(favicon_path, media_type="image/x-icon")
+ else:
+ raise HTTPException(status_code=404, detail="Favicon not found")
+
+@app.post("/api/admin/fix-character-prompts")
+async def fix_character_prompts(admin: AdminUser = Depends(get_current_admin)):
+ """Fix all character system prompts to use proper template format"""
+ PROPER_SYSTEM_PROMPT_TEMPLATE = """You are a character named {{{{name}}}}. You have the following personality: {{{{personality}}}}
+
+Your speaking style is {{{{speaking_style}}}}. You are interested in {{{{interests}}}}.
+
+Background: {{{{background}}}}
+
+When responding to messages:
+1. Stay in character at all times
+2. Reference your personality and interests naturally
+3. Engage authentically with other characters
+4. Show growth and development over time
+
+Remember to be consistent with your established personality while allowing for natural character development through interactions."""
+
+ try:
+ async with get_db_session() as session:
+ from sqlalchemy import select
+ # Get all characters
+ characters_query = select(Character)
+ characters = await session.scalars(characters_query)
+
+ updated_characters = []
+
+ for character in characters:
+ current_prompt = character.system_prompt or ""
+
+ # If it doesn't contain template variables or is just raw text, fix it
+ if "{{name}}" not in current_prompt or len(current_prompt) < 100:
+ old_prompt = character.system_prompt
+ character.system_prompt = PROPER_SYSTEM_PROMPT_TEMPLATE
+ character.updated_at = datetime.now(timezone.utc)
+
+ updated_characters.append({
+ "name": character.name,
+ "old_prompt_length": len(old_prompt) if old_prompt else 0,
+ "new_prompt_length": len(PROPER_SYSTEM_PROMPT_TEMPLATE)
+ })
+
+ if updated_characters:
+ await session.commit()
+
+ return {
+ "success": True,
+ "updated_count": len(updated_characters),
+ "updated_characters": updated_characters
+ }
+
+ except Exception as e:
+ logger.error(f"Error fixing character prompts: {e}")
+ raise HTTPException(status_code=500, detail=str(e))
+
if __name__ == "__main__":
import os
admin_port = int(os.getenv("ADMIN_PORT", "8000"))
uvicorn.run(
- "src.admin.app:app",
+ "src.admin.app:combined_app",
host="0.0.0.0",
port=admin_port,
reload=True,
diff --git a/src/admin/auth.py b/src/admin/auth.py
index 774ab4e..3bcaf60 100644
--- a/src/admin/auth.py
+++ b/src/admin/auth.py
@@ -21,9 +21,9 @@ class AuthService:
def __init__(self):
import os
self.settings = get_settings()
- self.secret_key = self.settings.admin.secret_key if hasattr(self.settings, 'admin') else "fallback-secret-key"
+ self.secret_key = os.getenv("SECRET_KEY", "fallback-secret-key-for-jwt")
self.algorithm = "HS256"
- self.access_token_expire_minutes = 480 # 8 hours
+ self.access_token_expire_minutes = 1440 # 24 hours
# Get admin credentials from environment
admin_username = os.getenv("ADMIN_USERNAME", "admin")
@@ -121,8 +121,14 @@ class AuthService:
if not user["active"]:
raise HTTPException(status_code=401, detail="User account disabled")
- # Update last activity
- if username in self.active_sessions:
+ # Update last activity (create session if doesn't exist)
+ if username not in self.active_sessions:
+ self.active_sessions[username] = {
+ "token": token,
+ "login_time": datetime.now(timezone.utc),
+ "last_activity": datetime.now(timezone.utc)
+ }
+ else:
self.active_sessions[username]["last_activity"] = datetime.now(timezone.utc)
return AdminUser(
diff --git a/src/admin/services/character_service.py b/src/admin/services/character_service.py
index 040519f..4fe2f5f 100644
--- a/src/admin/services/character_service.py
+++ b/src/admin/services/character_service.py
@@ -49,6 +49,37 @@ class CharacterService:
logger.error(f"Error getting all characters: {e}")
return []
+ async def get_all_characters_basic(self) -> List[Dict[str, Any]]:
+ """Get basic character data for lists"""
+ try:
+ async with get_db_session() as session:
+ # Get all characters
+ characters_query = select(Character)
+ characters = await session.scalars(characters_query)
+
+ character_list = []
+ for character in characters:
+ # Determine current status
+ status = await self._determine_character_status(character, character.last_active)
+
+ character_data = {
+ "name": character.name,
+ "status": status.value,
+ "is_active": character.is_active,
+ "last_active": character.last_active.isoformat() if character.last_active else None,
+ "personality": character.personality,
+ "system_prompt": character.system_prompt,
+ "interests": character.interests,
+ "speaking_style": character.speaking_style
+ }
+ character_list.append(character_data)
+
+ return character_list
+
+ except Exception as e:
+ logger.error(f"Error getting basic characters: {e}")
+ return []
+
async def toggle_character_status(self, character_name: str, is_active: bool) -> Dict[str, Any]:
"""Enable or disable a character"""
try:
@@ -148,6 +179,37 @@ class CharacterService:
logger.error(f"Error getting character profile for {character_name}: {e}")
return None
+ async def get_character_data(self, character_name: str) -> Optional[Dict[str, Any]]:
+ """Get raw character data for editing"""
+ try:
+ async with get_db_session() as session:
+ character_query = select(Character).where(Character.name == character_name)
+ character = await session.scalar(character_query)
+
+ if not character:
+ return None
+
+ return {
+ "name": character.name,
+ "personality": character.personality,
+ "system_prompt": character.system_prompt,
+ "interests": character.interests,
+ "speaking_style": character.speaking_style,
+ "background": character.background,
+ "is_active": character.is_active,
+ "created_at": character.creation_date,
+ "last_active": character.last_active,
+ # LLM settings
+ "llm_provider": character.llm_provider,
+ "llm_model": character.llm_model,
+ "llm_temperature": character.llm_temperature,
+ "llm_max_tokens": character.llm_max_tokens
+ }
+
+ except Exception as e:
+ logger.error(f"Error getting character profile for {character_name}: {e}")
+ return None
+
async def _build_character_profile(self, session, character) -> CharacterProfile:
"""Build character profile from database data"""
# Get message count
@@ -191,7 +253,7 @@ class CharacterService:
growth_score = 0.5 # Would calculate based on personality changes
# Determine current status
- status = await self._determine_character_status(character.name, last_active)
+ status = await self._determine_character_status(character, last_active)
# Parse personality traits from personality text
personality_traits = {
@@ -234,22 +296,49 @@ class CharacterService:
growth_score=growth_score
)
- async def _determine_character_status(self, character_name: str, last_active: Optional[datetime]) -> CharacterStatusEnum:
+ async def _determine_character_status(self, character, last_active: Optional[datetime]) -> CharacterStatusEnum:
"""Determine character's current status"""
- if not last_active:
+ # If character is disabled in database, show as offline
+ if not character.is_active:
return CharacterStatusEnum.OFFLINE
+ # Check if character has been active recently (database last_active field)
now = datetime.now(timezone.utc)
- time_since_active = now - last_active
+ db_last_active = character.last_active
- if time_since_active < timedelta(minutes=5):
- return CharacterStatusEnum.ACTIVE
- elif time_since_active < timedelta(minutes=30):
- return CharacterStatusEnum.IDLE
- elif time_since_active < timedelta(hours=1):
+ if db_last_active:
+ # Make sure db_last_active is timezone-aware
+ if db_last_active.tzinfo is None:
+ db_last_active = db_last_active.replace(tzinfo=timezone.utc)
+
+ time_since_db_active = now - db_last_active
+
+ # If they were active in the database recently, they're likely running
+ if time_since_db_active < timedelta(minutes=10):
+ return CharacterStatusEnum.ACTIVE
+ elif time_since_db_active < timedelta(hours=1):
+ return CharacterStatusEnum.IDLE
+
+ # Fall back to Discord message activity if available
+ if last_active:
+ # Make sure last_active is timezone-aware
+ if last_active.tzinfo is None:
+ last_active = last_active.replace(tzinfo=timezone.utc)
+
+ time_since_active = now - last_active
+
+ if time_since_active < timedelta(minutes=5):
+ return CharacterStatusEnum.ACTIVE
+ elif time_since_active < timedelta(minutes=30):
+ return CharacterStatusEnum.IDLE
+ elif time_since_active < timedelta(hours=1):
+ return CharacterStatusEnum.REFLECTING
+
+ # If character is marked as active in DB but no recent activity, show as reflecting
+ if character.is_active:
return CharacterStatusEnum.REFLECTING
- else:
- return CharacterStatusEnum.OFFLINE
+
+ return CharacterStatusEnum.OFFLINE
async def get_character_relationships(self, character_name: str) -> List[Relationship]:
"""Get character's relationship network"""
@@ -581,12 +670,27 @@ class CharacterService:
# Update character fields
if 'personality' in character_data:
character.personality = character_data['personality']
+ if 'system_prompt' in character_data:
+ character.system_prompt = character_data['system_prompt']
if 'interests' in character_data:
character.interests = character_data['interests']
if 'speaking_style' in character_data:
character.speaking_style = character_data['speaking_style']
if 'background' in character_data:
character.background = character_data['background']
+ if 'is_active' in character_data:
+ character.is_active = character_data['is_active']
+ # LLM settings
+ if 'llm_provider' in character_data:
+ character.llm_provider = character_data['llm_provider'] or None
+ if 'llm_model' in character_data:
+ character.llm_model = character_data['llm_model'] or None
+ if 'llm_temperature' in character_data:
+ character.llm_temperature = character_data['llm_temperature']
+ if 'llm_max_tokens' in character_data:
+ character.llm_max_tokens = character_data['llm_max_tokens']
+
+ character.updated_at = datetime.now(timezone.utc)
await session.commit()
await session.refresh(character)
@@ -612,9 +716,11 @@ class CharacterService:
"id": character.id,
"name": character.name,
"personality": character.personality,
+ "system_prompt": character.system_prompt,
"interests": character.interests,
"speaking_style": character.speaking_style,
"background": character.background,
+ "is_active": character.is_active,
"created_at": character.creation_date
}
diff --git a/src/admin/services/conversation_service.py b/src/admin/services/conversation_service.py
index e931788..fb3348b 100644
--- a/src/admin/services/conversation_service.py
+++ b/src/admin/services/conversation_service.py
@@ -81,17 +81,17 @@ class ConversationService:
duration = conversation.end_time - conversation.start_time
duration_minutes = duration.total_seconds() / 60
- # Calculate engagement score (placeholder)
- engagement_score = min(1.0, conversation.message_count / 20)
+ # Calculate engagement score based on participation patterns
+ engagement_score = await self._calculate_engagement_score(session, conversation)
- # Calculate sentiment score (placeholder)
- sentiment_score = 0.7 # Would analyze message content
+ # Calculate sentiment score from message content
+ sentiment_score = await self._calculate_sentiment_score(session, conversation)
- # Detect conflicts (placeholder)
- has_conflict = False # Would analyze for conflict keywords
+ # Detect conflicts from message analysis
+ has_conflict = await self._detect_conflicts(session, conversation)
- # Extract creative elements (placeholder)
- creative_elements = [] # Would analyze for creative content
+ # Extract creative elements from conversation content
+ creative_elements = await self._extract_creative_elements(session, conversation)
return ConversationSummary(
id=conversation.id,
@@ -325,4 +325,182 @@ class ConversationService:
except Exception as e:
logger.error(f"Error exporting conversation {conversation_id}: {e}")
- raise
\ No newline at end of file
+ raise
+
+ async def _calculate_engagement_score(self, session, conversation) -> float:
+ """Calculate engagement score based on message patterns"""
+ try:
+ if conversation.message_count == 0:
+ return 0.0
+
+ # Get messages for this conversation
+ messages_query = select(Message).where(
+ Message.conversation_id == conversation.id
+ ).order_by(Message.timestamp)
+
+ messages = await session.scalars(messages_query)
+ message_list = list(messages)
+
+ if len(message_list) < 2:
+ return 0.1
+
+ # Calculate response time variance (lower variance = higher engagement)
+ response_times = []
+ for i in range(1, len(message_list)):
+ time_diff = (message_list[i].timestamp - message_list[i-1].timestamp).total_seconds()
+ response_times.append(time_diff)
+
+ if not response_times:
+ return 0.5
+
+ # Normalize engagement based on average response time
+ avg_response_time = sum(response_times) / len(response_times)
+
+ # Faster responses = higher engagement
+ # Scale from 0.1 (very slow) to 1.0 (very fast)
+ if avg_response_time > 300: # > 5 minutes
+ engagement = 0.1
+ elif avg_response_time > 120: # > 2 minutes
+ engagement = 0.3
+ elif avg_response_time > 60: # > 1 minute
+ engagement = 0.5
+ elif avg_response_time > 30: # > 30 seconds
+ engagement = 0.7
+ else: # <= 30 seconds
+ engagement = 0.9
+
+ # Boost for longer conversations
+ length_boost = min(0.1, conversation.message_count / 100)
+ return min(1.0, engagement + length_boost)
+
+ except Exception as e:
+ logger.error(f"Error calculating engagement score: {e}")
+ return 0.5
+
+ async def _calculate_sentiment_score(self, session, conversation) -> float:
+ """Calculate sentiment score from message content analysis"""
+ try:
+ # Get messages for this conversation
+ messages_query = select(Message).where(
+ Message.conversation_id == conversation.id
+ )
+
+ messages = await session.scalars(messages_query)
+ message_list = list(messages)
+
+ if not message_list:
+ return 0.5
+
+ # Simple keyword-based sentiment analysis
+ positive_words = [
+ 'happy', 'joy', 'love', 'great', 'wonderful', 'amazing', 'excited',
+ 'good', 'excellent', 'beautiful', 'nice', 'awesome', 'fantastic',
+ 'thanks', 'appreciate', 'grateful', 'smile', 'laugh', 'fun'
+ ]
+
+ negative_words = [
+ 'sad', 'angry', 'hate', 'terrible', 'awful', 'horrible', 'bad',
+ 'angry', 'frustrated', 'disappointed', 'worried', 'concern',
+ 'problem', 'issue', 'wrong', 'fail', 'error', 'upset'
+ ]
+
+ sentiment_scores = []
+
+ for message in message_list:
+ content_lower = message.content.lower()
+ positive_count = sum(1 for word in positive_words if word in content_lower)
+ negative_count = sum(1 for word in negative_words if word in content_lower)
+
+ if positive_count + negative_count == 0:
+ sentiment_scores.append(0.5) # Neutral
+ else:
+ # Calculate sentiment ratio
+ total_sentiment_words = positive_count + negative_count
+ sentiment_ratio = positive_count / total_sentiment_words
+ sentiment_scores.append(sentiment_ratio)
+
+ # Return average sentiment
+ return sum(sentiment_scores) / len(sentiment_scores) if sentiment_scores else 0.5
+
+ except Exception as e:
+ logger.error(f"Error calculating sentiment score: {e}")
+ return 0.5
+
+ async def _detect_conflicts(self, session, conversation) -> bool:
+ """Detect conflicts in conversation content"""
+ try:
+ # Get messages for this conversation
+ messages_query = select(Message).where(
+ Message.conversation_id == conversation.id
+ )
+
+ messages = await session.scalars(messages_query)
+ message_list = list(messages)
+
+ if not message_list:
+ return False
+
+ conflict_indicators = [
+ 'disagree', 'wrong', "don't think", 'but', 'however', 'actually',
+ 'argue', 'conflict', 'dispute', 'oppose', 'against', 'contradict',
+ 'reject', 'refuse', 'deny', 'challenge', 'question', 'doubt'
+ ]
+
+ conflict_score = 0
+ total_messages = len(message_list)
+
+ for message in message_list:
+ content_lower = message.content.lower()
+ conflicts_found = sum(1 for indicator in conflict_indicators
+ if indicator in content_lower)
+ if conflicts_found > 0:
+ conflict_score += 1
+
+ # Consider it a conflict if more than 30% of messages contain conflict indicators
+ conflict_ratio = conflict_score / total_messages if total_messages > 0 else 0
+ return conflict_ratio > 0.3
+
+ except Exception as e:
+ logger.error(f"Error detecting conflicts: {e}")
+ return False
+
+ async def _extract_creative_elements(self, session, conversation) -> List[str]:
+ """Extract creative elements from conversation content"""
+ try:
+ # Get messages for this conversation
+ messages_query = select(Message).where(
+ Message.conversation_id == conversation.id
+ )
+
+ messages = await session.scalars(messages_query)
+ message_list = list(messages)
+
+ if not message_list:
+ return []
+
+ creative_patterns = {
+ 'poetry': ['poem', 'verse', 'rhyme', 'metaphor', 'stanza'],
+ 'storytelling': ['story', 'tale', 'narrative', 'character', 'plot', 'once upon'],
+ 'music': ['song', 'melody', 'rhythm', 'note', 'chord', 'harmony'],
+ 'art': ['draw', 'paint', 'sketch', 'color', 'canvas', 'brush'],
+ 'philosophy': ['meaning', 'existence', 'reality', 'consciousness', 'truth'],
+ 'creativity': ['create', 'imagine', 'invent', 'design', 'inspiration'],
+ 'humor': ['joke', 'funny', 'laugh', 'humor', 'wit', 'amusing'],
+ 'worldbuilding': ['world', 'universe', 'realm', 'dimension', 'kingdom']
+ }
+
+ found_elements = []
+
+ # Combine all message content
+ all_content = ' '.join(message.content.lower() for message in message_list)
+
+ for element_type, keywords in creative_patterns.items():
+ keyword_count = sum(1 for keyword in keywords if keyword in all_content)
+ if keyword_count >= 2: # Require at least 2 mentions
+ found_elements.append(element_type)
+
+ return found_elements
+
+ except Exception as e:
+ logger.error(f"Error extracting creative elements: {e}")
+ return []
\ No newline at end of file
diff --git a/src/admin/services/system_service.py b/src/admin/services/system_service.py
index 61fcdc1..948a0dc 100644
--- a/src/admin/services/system_service.py
+++ b/src/admin/services/system_service.py
@@ -105,29 +105,90 @@ class SystemService:
logger.error(f"Error resuming system: {e}")
raise
- async def get_configuration(self) -> SystemConfiguration:
- """Get system configuration"""
- # Default configuration values
- return SystemConfiguration(
- conversation_frequency=0.5,
- response_delay_min=1.0,
- response_delay_max=5.0,
- personality_change_rate=0.1,
- memory_retention_days=90,
- max_conversation_length=50,
- creativity_boost=True,
- conflict_resolution_enabled=True,
- safety_monitoring=True,
- auto_moderation=False,
- backup_frequency_hours=24
- )
+ async def get_configuration(self) -> Dict[str, Any]:
+ """Get system configuration from environment variables"""
+ import os
+
+ return {
+ # LLM Control (COST PROTECTION)
+ "llm_enabled": os.getenv("LLM_ENABLED", "false").lower() == "true",
+ "conversation_frequency": float(os.getenv("CONVERSATION_FREQUENCY", "0.5")),
+ "response_delay_min": float(os.getenv("RESPONSE_DELAY_MIN", "1.0")),
+ "response_delay_max": float(os.getenv("RESPONSE_DELAY_MAX", "5.0")),
+ "personality_change_rate": float(os.getenv("PERSONALITY_CHANGE_RATE", "0.1")),
+ "memory_retention_days": int(os.getenv("MEMORY_RETENTION_DAYS", "90")),
+ "max_conversation_length": int(os.getenv("MAX_CONVERSATION_LENGTH", "50")),
+ "creativity_boost": os.getenv("CREATIVITY_BOOST", "true").lower() == "true",
+ "safety_monitoring": os.getenv("SAFETY_MONITORING", "false").lower() == "true",
+ "auto_moderation": os.getenv("AUTO_MODERATION", "false").lower() == "true",
+ "quiet_hours_enabled": os.getenv("QUIET_HOURS_ENABLED", "true").lower() == "true",
+ "quiet_hours_start": int(os.getenv("QUIET_HOURS_START", "23")),
+ "quiet_hours_end": int(os.getenv("QUIET_HOURS_END", "7")),
+ "min_delay_seconds": int(os.getenv("MIN_DELAY_SECONDS", "30")),
+ "max_delay_seconds": int(os.getenv("MAX_DELAY_SECONDS", "300")),
+ "llm_model": os.getenv("AI_MODEL", ""),
+ "llm_max_tokens": int(os.getenv("AI_MAX_TOKENS", "2000")),
+ "llm_temperature": float(os.getenv("LLM_TEMPERATURE", "0.8")),
+ "llm_timeout": int(os.getenv("LLM_TIMEOUT", "300")),
+ "discord_guild_id": os.getenv("DISCORD_GUILD_ID", ""),
+ "discord_channel_id": os.getenv("DISCORD_CHANNEL_ID", "")
+ }
async def update_configuration(self, config: Dict[str, Any]):
"""Update system configuration"""
try:
- logger.info(f"Updating system configuration: {config}")
- # Would integrate with main application to update configuration
+ import os
+ from pathlib import Path
+ logger.info(f"Updating system configuration: {config}")
+
+ # Update environment variables in memory
+ if 'llm_enabled' in config:
+ # If enabling LLM, validate provider first
+ if config['llm_enabled']:
+ validation_result = await self._validate_llm_providers()
+ if not validation_result['valid']:
+ logger.error(f"LLM validation failed: {validation_result['error']}")
+ raise ValueError(f"Cannot enable LLM: {validation_result['error']}")
+
+ os.environ['LLM_ENABLED'] = str(config['llm_enabled']).lower()
+
+ # Also update the database for persistence
+ await self._update_llm_global_setting(config['llm_enabled'])
+
+ # Invalidate LLM cache in all clients
+ await self._invalidate_llm_cache()
+
+ # AUDIT: Log LLM enable/disable action
+ await self._audit_llm_change(config['llm_enabled'])
+
+ logger.warning(f"LLM {'ENABLED' if config['llm_enabled'] else 'DISABLED'} - API costs {'WILL' if config['llm_enabled'] else 'will NOT'} be incurred")
+
+ # Update other configuration values
+ config_mapping = {
+ 'conversation_frequency': 'CONVERSATION_FREQUENCY',
+ 'response_delay_min': 'RESPONSE_DELAY_MIN',
+ 'response_delay_max': 'RESPONSE_DELAY_MAX',
+ 'personality_change_rate': 'PERSONALITY_CHANGE_RATE',
+ 'memory_retention_days': 'MEMORY_RETENTION_DAYS',
+ 'max_conversation_length': 'MAX_CONVERSATION_LENGTH',
+ 'creativity_boost': 'CREATIVITY_BOOST',
+ 'safety_monitoring': 'SAFETY_MONITORING',
+ 'auto_moderation': 'AUTO_MODERATION',
+ 'quiet_hours_enabled': 'QUIET_HOURS_ENABLED',
+ 'quiet_hours_start': 'QUIET_HOURS_START',
+ 'quiet_hours_end': 'QUIET_HOURS_END',
+ 'min_delay_seconds': 'MIN_DELAY_SECONDS',
+ 'max_delay_seconds': 'MAX_DELAY_SECONDS'
+ }
+
+ for config_key, env_key in config_mapping.items():
+ if config_key in config:
+ os.environ[env_key] = str(config[config_key])
+
+ # Update .env file for persistence
+ await self._update_env_file(config)
+
except Exception as e:
logger.error(f"Error updating configuration: {e}")
raise
@@ -420,4 +481,369 @@ class SystemService:
except Exception as e:
logger.error(f"Error activating scenario: {e}")
- raise
\ No newline at end of file
+ raise
+
+ async def get_llm_providers(self) -> Dict[str, Any]:
+ """Get all LLM provider configurations and their status"""
+ try:
+ from llm.multi_provider_client import multi_llm_client
+
+ # Ensure client is initialized
+ if not multi_llm_client.initialized:
+ await multi_llm_client.initialize()
+
+ # Get provider info and health status
+ provider_info = multi_llm_client.get_provider_info()
+ health_status = await multi_llm_client.health_check()
+ current_provider = multi_llm_client.get_current_provider()
+
+ # Combine info with health status
+ providers = {}
+ for name, info in provider_info.items():
+ providers[name] = {
+ **info,
+ 'healthy': health_status.get(name, False),
+ 'is_current': name == current_provider
+ }
+
+ return {
+ 'providers': providers,
+ 'current_provider': current_provider,
+ 'total_providers': len(providers),
+ 'healthy_providers': len([p for p in providers.values() if p['healthy']])
+ }
+
+ except Exception as e:
+ logger.error(f"Error getting LLM providers: {e}")
+ return {
+ 'providers': {},
+ 'current_provider': None,
+ 'total_providers': 0,
+ 'healthy_providers': 0,
+ 'error': str(e)
+ }
+
+ async def update_llm_providers(self, providers_config: Dict[str, Any]):
+ """Update LLM provider configurations"""
+ try:
+ from utils.config import get_settings
+ import os
+ import json
+ from pathlib import Path
+
+ # Update environment variables for provider settings
+ for provider_name, config in providers_config.items():
+ if 'enabled' in config:
+ env_var = f"{provider_name.upper()}_ENABLED"
+ os.environ[env_var] = str(config['enabled']).lower()
+
+ if 'config' in config:
+ provider_config = config['config']
+ if 'api_key' in provider_config:
+ env_var = f"{provider_name.upper()}_API_KEY"
+ os.environ[env_var] = provider_config['api_key']
+
+ if 'model' in provider_config:
+ env_var = f"{provider_name.upper()}_MODEL"
+ os.environ[env_var] = provider_config['model']
+
+ # Update configuration file
+ config_path = Path("config/fishbowl_config.json")
+ if config_path.exists():
+ with open(config_path, 'r') as f:
+ file_config = json.load(f)
+
+ # Update providers section
+ if 'llm' not in file_config:
+ file_config['llm'] = {}
+
+ file_config['llm']['providers'] = providers_config
+
+ # Write back to file
+ with open(config_path, 'w') as f:
+ json.dump(file_config, f, indent=2)
+
+ logger.info("Updated LLM provider configuration")
+
+ # Reinitialize the LLM client with new configuration
+ from llm.multi_provider_client import multi_llm_client
+ multi_llm_client.initialized = False
+ await multi_llm_client.initialize()
+
+ except Exception as e:
+ logger.error(f"Error updating LLM providers: {e}")
+ raise
+
+ async def test_llm_provider(self, provider_name: str) -> Dict[str, Any]:
+ """Test a specific LLM provider"""
+ try:
+ from llm.multi_provider_client import multi_llm_client
+ from llm.providers import LLMRequest
+
+ # Ensure client is initialized
+ if not multi_llm_client.initialized:
+ await multi_llm_client.initialize()
+
+ # Check if provider exists
+ provider_info = multi_llm_client.get_provider_info()
+ if provider_name not in provider_info:
+ return {
+ 'success': False,
+ 'error': f'Provider {provider_name} not found'
+ }
+
+ # Test health check first
+ health_status = await multi_llm_client.health_check()
+ if not health_status.get(provider_name, False):
+ return {
+ 'success': False,
+ 'error': f'Provider {provider_name} failed health check'
+ }
+
+ # Test actual generation
+ original_provider = multi_llm_client.get_current_provider()
+
+ # Temporarily switch to test provider
+ multi_llm_client.set_provider(provider_name)
+
+ try:
+ test_request = LLMRequest(
+ prompt="Respond with exactly: 'Test successful'",
+ max_tokens=10,
+ temperature=0.1
+ )
+
+ response = await multi_llm_client.generate_response(test_request)
+
+ return {
+ 'success': response.success,
+ 'response': response.content if response.success else None,
+ 'error': response.error if not response.success else None,
+ 'provider': response.provider,
+ 'model': response.model,
+ 'tokens_used': response.tokens_used
+ }
+
+ finally:
+ # Restore original provider
+ if original_provider:
+ multi_llm_client.set_provider(original_provider)
+
+ except Exception as e:
+ logger.error(f"Error testing LLM provider {provider_name}: {e}")
+ return {
+ 'success': False,
+ 'error': str(e)
+ }
+
+ async def get_llm_health(self) -> Dict[str, Any]:
+ """Get health status of all LLM providers"""
+ try:
+ from llm.multi_provider_client import multi_llm_client
+
+ # Ensure client is initialized
+ if not multi_llm_client.initialized:
+ await multi_llm_client.initialize()
+
+ health_status = await multi_llm_client.health_check()
+ provider_info = multi_llm_client.get_provider_info()
+ current_provider = multi_llm_client.get_current_provider()
+
+ return {
+ 'health_status': health_status,
+ 'current_provider': current_provider,
+ 'timestamp': datetime.now(timezone.utc).isoformat(),
+ 'summary': {
+ 'total': len(health_status),
+ 'healthy': len([h for h in health_status.values() if h]),
+ 'unhealthy': len([h for h in health_status.values() if not h])
+ }
+ }
+
+ except Exception as e:
+ logger.error(f"Error getting LLM health: {e}")
+ return {
+ 'health_status': {},
+ 'current_provider': None,
+ 'timestamp': datetime.now(timezone.utc).isoformat(),
+ 'error': str(e)
+ }
+
+ async def switch_llm_provider(self, provider_name: str):
+ """Switch to a different primary LLM provider"""
+ try:
+ from llm.multi_provider_client import multi_llm_client
+
+ # Ensure client is initialized
+ if not multi_llm_client.initialized:
+ await multi_llm_client.initialize()
+
+ # Check if provider exists and is healthy
+ provider_info = multi_llm_client.get_provider_info()
+ if provider_name not in provider_info:
+ raise ValueError(f"Provider {provider_name} not found")
+
+ health_status = await multi_llm_client.health_check()
+ if not health_status.get(provider_name, False):
+ raise ValueError(f"Provider {provider_name} is not healthy")
+
+ # Switch provider
+ success = multi_llm_client.set_provider(provider_name)
+ if not success:
+ raise ValueError(f"Failed to switch to provider {provider_name}")
+
+ logger.info(f"Switched primary LLM provider to: {provider_name}")
+
+ except Exception as e:
+ logger.error(f"Error switching LLM provider to {provider_name}: {e}")
+ raise
+
+ async def _update_llm_global_setting(self, enabled: bool):
+ """Update the global LLM enabled setting in database"""
+ try:
+ from sqlalchemy import text
+ from database.connection import get_db_session
+
+ async with get_db_session() as session:
+ await session.execute(
+ text("""
+ UPDATE system_configuration
+ SET config_value = :enabled, version = version + 1
+ WHERE config_section = 'llm' AND config_key = 'global_enabled'
+ """),
+ {"enabled": str(enabled).lower()}
+ )
+ await session.commit()
+
+ except Exception as e:
+ logger.error(f"Error updating LLM global setting in database: {e}")
+ # Don't raise - this is a secondary storage
+
+ async def _update_env_file(self, config: Dict[str, Any]):
+ """Update .env file with new configuration values"""
+ try:
+ from pathlib import Path
+ import re
+
+ env_path = Path(__file__).parent.parent.parent.parent / ".env"
+
+ if not env_path.exists():
+ logger.warning(".env file not found for updating")
+ return
+
+ # Read current .env file
+ with open(env_path, 'r') as f:
+ env_content = f.read()
+
+ # Update LLM_ENABLED if present
+ if 'llm_enabled' in config:
+ env_value = str(config['llm_enabled']).lower()
+ pattern = r'^LLM_ENABLED=.*$'
+ replacement = f'LLM_ENABLED={env_value}'
+
+ if re.search(pattern, env_content, re.MULTILINE):
+ env_content = re.sub(pattern, replacement, env_content, flags=re.MULTILINE)
+ else:
+ # Add it if not present
+ env_content += f'\nLLM_ENABLED={env_value}\n'
+
+ # Write back to .env file
+ with open(env_path, 'w') as f:
+ f.write(env_content)
+
+ logger.info("Updated .env file with new configuration")
+
+ except Exception as e:
+ logger.error(f"Error updating .env file: {e}")
+ # Don't raise - this is a secondary operation
+
+ async def _invalidate_llm_cache(self):
+ """Invalidate LLM cache in global client"""
+ try:
+ from llm.multi_provider_client import multi_llm_client
+ multi_llm_client._invalidate_llm_cache()
+ logger.info("Invalidated LLM cache after settings change")
+ except Exception as e:
+ logger.error(f"Error invalidating LLM cache: {e}")
+ # Don't raise - this is not critical
+
+ async def _validate_llm_providers(self) -> Dict[str, Any]:
+ """Validate that at least one LLM provider is properly configured"""
+ try:
+ from llm.multi_provider_client import multi_llm_client
+ import os
+
+ # Check if we have at least one provider configured
+ providers_to_check = []
+
+ # Check custom provider (current setup)
+ if os.getenv('AI_API_KEY') and os.getenv('AI_API_BASE'):
+ providers_to_check.append('current_custom')
+
+ # Check OpenAI
+ if os.getenv('OPENAI_API_KEY'):
+ providers_to_check.append('openai')
+
+ # Check OpenRouter
+ if os.getenv('OPENROUTER_API_KEY'):
+ providers_to_check.append('openrouter')
+
+ # Check Gemini
+ if os.getenv('GEMINI_API_KEY'):
+ providers_to_check.append('gemini')
+
+ if not providers_to_check:
+ return {
+ 'valid': False,
+ 'error': 'No LLM providers configured. Please set up at least one provider with valid API keys.'
+ }
+
+ # Try to test the first available provider
+ for provider_name in providers_to_check:
+ try:
+ test_result = await self.test_llm_provider(provider_name)
+ if test_result.get('success'):
+ return {
+ 'valid': True,
+ 'provider': provider_name,
+ 'test_result': test_result
+ }
+ except Exception as e:
+ logger.warning(f"Provider {provider_name} test failed: {e}")
+ continue
+
+ return {
+ 'valid': False,
+ 'error': f'All configured providers failed validation. Checked: {", ".join(providers_to_check)}'
+ }
+
+ except Exception as e:
+ logger.error(f"Error validating LLM providers: {e}")
+ return {
+ 'valid': False,
+ 'error': f'Validation error: {str(e)}'
+ }
+
+ async def _audit_llm_change(self, enabled: bool):
+ """Audit log for LLM enable/disable actions"""
+ try:
+ from .audit_service import AuditService
+
+ await AuditService.log_admin_action(
+ admin_user="admin", # TODO: Get actual admin user from context
+ action_type="llm_global_toggle",
+ resource_affected="system:llm_enabled",
+ changes_made={
+ "llm_enabled": enabled,
+ "timestamp": datetime.now(timezone.utc).isoformat(),
+ "cost_warning": "LLM enabled - API costs will be incurred" if enabled else "LLM disabled - no API costs"
+ },
+ request_ip=None, # TODO: Get from request context
+ success=True
+ )
+
+ logger.info(f"Audited LLM {'enable' if enabled else 'disable'} action")
+
+ except Exception as e:
+ logger.error(f"Error logging LLM audit: {e}")
+ # Don't raise - audit failure shouldn't block the operation
\ No newline at end of file
diff --git a/src/admin/services/websocket_manager.py b/src/admin/services/websocket_manager.py
index 27066f9..504e827 100644
--- a/src/admin/services/websocket_manager.py
+++ b/src/admin/services/websocket_manager.py
@@ -15,7 +15,7 @@ class WebSocketManager:
def __init__(self):
self.sio = socketio.AsyncServer(
- cors_allowed_origins=["http://localhost:3000", "http://127.0.0.1:3000"],
+ cors_allowed_origins="*", # Allow all origins for development
logger=True,
engineio_logger=True
)
@@ -54,9 +54,9 @@ class WebSocketManager:
"""Handle ping from client"""
await self.sio.emit('pong', {'timestamp': asyncio.get_event_loop().time()}, room=sid)
- def get_app(self):
+ def get_app(self, other_asgi_app=None):
"""Get the Socket.IO ASGI app"""
- return socketio.ASGIApp(self.sio)
+ return socketio.ASGIApp(self.sio, other_asgi_app)
async def send_personal_message(self, message: Dict[str, Any], sid: str):
"""Send message to specific client"""
diff --git a/src/bot/message_handler.py b/src/bot/message_handler.py
index 4044b90..545ac87 100644
--- a/src/bot/message_handler.py
+++ b/src/bot/message_handler.py
@@ -3,11 +3,11 @@ from discord.ext import commands
import asyncio
import logging
from typing import Optional, List, Dict, Any
-from datetime import datetime, timezone
+from datetime import datetime, timezone, timedelta
from utils.logging import log_error_with_context, log_character_action
from database.connection import get_db_session
-from database.models import Character, Message, Conversation
-from sqlalchemy import select, and_, or_
+from database.models import Character, Message, Conversation, Memory
+from sqlalchemy import select, and_, or_, func, text
logger = logging.getLogger(__name__)
@@ -116,7 +116,7 @@ class CommandHandler:
async with get_db_session() as session:
# Get character count
character_query = select(Character).where(Character.is_active == True)
- character_count = len(await session.scalars(character_query).all())
+ character_count = len((await session.scalars(character_query)).all())
# Get recent message count
from sqlalchemy import func
@@ -197,11 +197,12 @@ class CommandHandler:
async def trigger_conversation(ctx, *, topic: str = None):
"""Manually trigger a conversation"""
try:
+ logger.info(f"Trigger command received from {ctx.author} with topic: {topic}")
await self.conversation_engine.trigger_conversation(topic)
await ctx.send(f"Triggered conversation{' about: ' + topic if topic else ''}")
except Exception as e:
- log_error_with_context(e, {"command": "trigger", "topic": topic})
+ log_error_with_context(e, {"command": "trigger", "topic": topic, "user": str(ctx.author)})
await ctx.send("Error triggering conversation.")
@self.bot.command(name='pause')
@@ -271,6 +272,336 @@ class CommandHandler:
except Exception as e:
log_error_with_context(e, {"command": "stats"})
await ctx.send("Error getting statistics.")
+
+ @self.bot.command(name='permissions')
+ async def check_permissions(ctx):
+ """Check bot permissions in current channel"""
+ permissions = ctx.channel.permissions_for(ctx.guild.me)
+ embed = discord.Embed(
+ title="Bot Permissions",
+ color=discord.Color.blue()
+ )
+ embed.add_field(name="Manage Messages", value="✅" if permissions.manage_messages else "❌", inline=True)
+ embed.add_field(name="Read Message History", value="✅" if permissions.read_message_history else "❌", inline=True)
+ embed.add_field(name="Send Messages", value="✅" if permissions.send_messages else "❌", inline=True)
+ embed.add_field(name="Administrator", value="✅" if permissions.administrator else "❌", inline=True)
+ await ctx.send(embed=embed)
+
+ @self.bot.command(name='memory-stats')
+ async def memory_stats(ctx):
+ """Show memory statistics for all characters"""
+ try:
+ async with get_db_session() as session:
+ # Get memory counts by character
+ query = select(
+ Character.name,
+ func.count(Memory.id).label('memory_count'),
+ func.min(Memory.timestamp).label('oldest'),
+ func.max(Memory.timestamp).label('newest')
+ ).select_from(
+ Character
+ ).outerjoin(
+ Memory, Character.id == Memory.character_id
+ ).group_by(
+ Character.id, Character.name
+ ).order_by(
+ func.count(Memory.id).desc()
+ )
+
+ results = await session.execute(query)
+ stats = results.fetchall()
+
+ # Get memory type breakdown
+ type_query = select(
+ Memory.memory_type,
+ func.count(Memory.id).label('count')
+ ).group_by(Memory.memory_type).order_by(func.count(Memory.id).desc())
+
+ type_results = await session.execute(type_query)
+ type_stats = type_results.fetchall()
+
+ embed = discord.Embed(
+ title="🧠 Memory Statistics",
+ color=discord.Color.blue(),
+ timestamp=datetime.now(timezone.utc)
+ )
+
+ # Character memory counts
+ for stat in stats:
+ if stat.memory_count > 0:
+ oldest = stat.oldest.strftime('%m/%d %H:%M') if stat.oldest else 'N/A'
+ newest = stat.newest.strftime('%m/%d %H:%M') if stat.newest else 'N/A'
+ embed.add_field(
+ name=f"{stat.name}",
+ value=f"**{stat.memory_count:,}** memories\n{oldest} → {newest}",
+ inline=True
+ )
+ else:
+ embed.add_field(
+ name=f"{stat.name}",
+ value="No memories",
+ inline=True
+ )
+
+ # Memory type breakdown
+ if type_stats:
+ type_text = "\n".join([f"**{t.memory_type}**: {t.count:,}" for t in type_stats])
+ embed.add_field(
+ name="Memory Types",
+ value=type_text,
+ inline=False
+ )
+
+ # Total count
+ total_memories = sum(stat.memory_count for stat in stats)
+ embed.add_field(
+ name="Total Memories",
+ value=f"**{total_memories:,}** across all characters",
+ inline=False
+ )
+
+ await ctx.send(embed=embed)
+
+ except Exception as e:
+ log_error_with_context(e, {"command": "memory-stats"})
+ await ctx.send("Error getting memory statistics.")
+
+ @self.bot.command(name='wipe-memories')
+ @commands.has_permissions(administrator=True)
+ async def wipe_memories(ctx, character_name: str = None):
+ """Wipe character memories (use 'all' for all characters)"""
+ try:
+ # Confirm action
+ if character_name == 'all':
+ confirmation_text = "This will delete ALL memories for ALL characters."
+ elif character_name:
+ confirmation_text = f"This will delete ALL memories for character '{character_name}'."
+ else:
+ confirmation_text = "Usage: !wipe-memories
or !wipe-memories all"
+ await ctx.send(confirmation_text)
+ return
+
+ embed = discord.Embed(
+ title="⚠️ Memory Wipe Confirmation",
+ description=f"{confirmation_text}\nReact with ✅ to confirm or ❌ to cancel.",
+ color=discord.Color.red()
+ )
+
+ confirmation_msg = await ctx.send(embed=embed)
+ await confirmation_msg.add_reaction("✅")
+ await confirmation_msg.add_reaction("❌")
+
+ def check(reaction, user):
+ return user == ctx.author and str(reaction.emoji) in ["✅", "❌"] and reaction.message.id == confirmation_msg.id
+
+ try:
+ reaction, user = await self.bot.wait_for('reaction_add', timeout=30.0, check=check)
+
+ if str(reaction.emoji) == "✅":
+ # Delete confirmation message
+ await confirmation_msg.delete()
+
+ # Send status message
+ status_msg = await ctx.send("🧹 Wiping memories...")
+
+ # Wipe memories in database
+ async with get_db_session() as session:
+ if character_name == 'all':
+ # Delete all memories
+ await session.execute(text("DELETE FROM memories"))
+ await session.execute(text("DELETE FROM vector_embeddings"))
+ memory_count = "all"
+ else:
+ # Delete memories for specific character
+ char_query = select(Character).where(Character.name == character_name)
+ character = await session.scalar(char_query)
+
+ if not character:
+ await status_msg.edit(content=f"❌ Character '{character_name}' not found.")
+ return
+
+ # Count memories before deletion
+ count_query = select(func.count(Memory.id)).where(Memory.character_id == character.id)
+ memory_count = await session.scalar(count_query)
+
+ # Delete memories
+ await session.execute(
+ text("DELETE FROM memories WHERE character_id = :char_id"),
+ {"char_id": character.id}
+ )
+ await session.execute(
+ text("DELETE FROM vector_embeddings WHERE character_id = :char_id"),
+ {"char_id": character.id}
+ )
+
+ await session.commit()
+
+ # Clear Qdrant collection
+ try:
+ import requests
+ qdrant_url = "http://qdrant:6333"
+
+ if character_name == 'all':
+ # Recreate collection to clear all vectors
+ requests.delete(f"{qdrant_url}/collections/fishbowl_memories")
+ collection_config = {
+ "vectors": {
+ "size": 384,
+ "distance": "Cosine"
+ }
+ }
+ requests.put(f"{qdrant_url}/collections/fishbowl_memories", json=collection_config)
+ else:
+ # Delete vectors for specific character
+ filter_condition = {
+ "must": [
+ {
+ "key": "character_name",
+ "match": {"value": character_name}
+ }
+ ]
+ }
+ delete_payload = {"filter": filter_condition}
+ requests.post(f"{qdrant_url}/collections/fishbowl_memories/points/delete", json=delete_payload)
+ except Exception as e:
+ logger.warning(f"Failed to clear Qdrant vectors: {e}")
+
+ if character_name == 'all':
+ await status_msg.edit(content="✅ All character memories have been wiped.")
+ else:
+ await status_msg.edit(content=f"✅ Deleted {memory_count} memories for {character_name}.")
+
+ elif str(reaction.emoji) == "❌":
+ await confirmation_msg.edit(content="❌ Memory wipe cancelled.", embed=None)
+
+ except asyncio.TimeoutError:
+ await confirmation_msg.edit(content="⏰ Memory wipe timed out.", embed=None)
+
+ except Exception as e:
+ log_error_with_context(e, {"command": "wipe-memories", "character": character_name})
+ await ctx.send("Error wiping memories.")
+
+ @self.bot.command(name='wipe')
+ @commands.has_permissions(administrator=True)
+ async def wipe_channel(ctx):
+ """Wipe all messages in the current channel and reset conversation history"""
+ try:
+ # Confirm action
+ embed = discord.Embed(
+ title="⚠️ Channel Wipe Confirmation",
+ description="This will delete ALL messages in this channel and reset conversation history.\nReact with ✅ to confirm or ❌ to cancel.",
+ color=discord.Color.red()
+ )
+
+ confirmation_msg = await ctx.send(embed=embed)
+ await confirmation_msg.add_reaction("✅")
+ await confirmation_msg.add_reaction("❌")
+
+ def check(reaction, user):
+ return (user == ctx.author and
+ str(reaction.emoji) in ["✅", "❌"] and
+ reaction.message.id == confirmation_msg.id)
+
+ try:
+ reaction, user = await self.bot.wait_for('reaction_add', timeout=30.0, check=check)
+
+ if str(reaction.emoji) == "✅":
+ # Delete confirmation message
+ await confirmation_msg.delete()
+
+ # Send status message
+ status_msg = await ctx.send("🧹 Wiping channel and resetting conversation history...")
+
+ # Use bulk operations for better performance
+ async with get_db_session() as session:
+ await asyncio.gather(
+ self._bulk_delete_discord_messages(ctx.channel, status_msg.id),
+ self._bulk_reset_database(session)
+ )
+
+ # Reset conversation engine state
+ await self.conversation_engine.reset_conversation_state()
+
+ # Update status message
+ await status_msg.edit(content="✅ Channel wiped and conversation history reset! Characters will start fresh.")
+
+ # Delete status message after 10 seconds
+ await asyncio.sleep(10)
+ await status_msg.delete()
+
+ else:
+ await confirmation_msg.edit(content="❌ Channel wipe cancelled.", embed=None)
+ await asyncio.sleep(5)
+ await confirmation_msg.delete()
+
+ except asyncio.TimeoutError:
+ await confirmation_msg.edit(content="⏰ Confirmation timed out. Channel wipe cancelled.", embed=None)
+ await asyncio.sleep(5)
+ await confirmation_msg.delete()
+
+ except Exception as e:
+ log_error_with_context(e, {"command": "wipe"})
+ await ctx.send("Error wiping channel. Please try again.")
+
+ async def _bulk_delete_discord_messages(self, channel, exclude_message_id: int):
+ """Efficiently delete Discord messages using bulk operations"""
+ try:
+ messages_to_delete = []
+ old_messages = []
+
+ # Collect messages in batches
+ async for message in channel.history(limit=None):
+ if message.id == exclude_message_id:
+ continue
+
+ # Discord bulk delete only works for messages < 14 days old
+ if (datetime.now(timezone.utc) - message.created_at).days < 14:
+ messages_to_delete.append(message)
+ # Bulk delete in chunks of 100 (Discord limit)
+ if len(messages_to_delete) >= 100:
+ await channel.delete_messages(messages_to_delete)
+ messages_to_delete = []
+ await asyncio.sleep(0.1) # Small delay to avoid rate limits
+ else:
+ old_messages.append(message)
+
+ # Delete remaining recent messages
+ if messages_to_delete:
+ if len(messages_to_delete) == 1:
+ await messages_to_delete[0].delete()
+ else:
+ await channel.delete_messages(messages_to_delete)
+
+ # Delete old messages individually (can't bulk delete messages > 14 days)
+ for message in old_messages:
+ try:
+ await message.delete()
+ await asyncio.sleep(0.05) # Small delay to avoid rate limits
+ except (discord.NotFound, discord.Forbidden):
+ pass
+
+ except Exception as e:
+ logger.warning(f"Error in bulk message deletion: {e}")
+
+ async def _bulk_reset_database(self, session):
+ """Efficiently reset database using bulk operations"""
+ try:
+ # Use bulk SQL operations instead of individual deletions
+ await session.execute(
+ text("UPDATE conversations SET is_active = false WHERE is_active = true")
+ )
+
+ # Delete recent messages in bulk
+ await session.execute(
+ text("DELETE FROM messages WHERE timestamp >= :cutoff"),
+ {"cutoff": datetime.now(timezone.utc) - timedelta(hours=24)}
+ )
+
+ await session.commit()
+
+ except Exception as e:
+ logger.warning(f"Error in bulk database reset: {e}")
+ await session.rollback()
async def _get_conversation_stats(self) -> Dict[str, Any]:
"""Get conversation statistics"""
diff --git a/src/characters/character.py b/src/characters/character.py
index 00d4b9f..5f7d130 100644
--- a/src/characters/character.py
+++ b/src/characters/character.py
@@ -39,6 +39,7 @@ class Character:
self.avatar_url = character_data.avatar_url
self.is_active = character_data.is_active
self.last_active = character_data.last_active
+ self.prompt_template_id = getattr(character_data, 'prompt_template_id', None)
# Dynamic state
self.state = CharacterState()
@@ -121,8 +122,8 @@ class Character:
# Update character state
await self._update_state_after_response(context, response)
- # Store as memory
- await self._store_response_memory(context, response)
+ # Store memory for significant responses only
+ await self._maybe_store_response_memory(context, response)
log_character_action(
self.name,
@@ -247,6 +248,10 @@ class Character:
importance=0.8
)
+ # Reset message count if this is an enhanced character
+ if hasattr(self, 'reset_message_count'):
+ await self.reset_message_count()
+
log_character_action(
self.name,
"self_reflected",
@@ -294,22 +299,25 @@ class Character:
# Build dynamic MCP tools section
mcp_tools_section = await self._build_dynamic_mcp_tools_section()
- prompt = f"""You are {self.name}, a character in a Discord chat.
+ # Get the prompt template and apply character data
+ template = await self._get_prompt_template()
+
+ # Replace template variables with character data
+ prompt_base = template.replace('{{char}}', self.name)
+ prompt_base = prompt_base.replace('{{personality}}', self.personality)
+ prompt_base = prompt_base.replace('{{background}}', self.background)
+ prompt_base = prompt_base.replace('{{speaking_style}}', self.speaking_style)
+ prompt_base = prompt_base.replace('{{interests}}', ', '.join(self.interests))
+ prompt_base = prompt_base.replace('{{system_prompt}}', self.system_prompt)
+
+ # Add context information
+ context_section = f"""
-{system_section}{scenario_section}PERSONALITY: {self.personality}
-
-SPEAKING STYLE: {self.speaking_style}
-
-BACKGROUND: {self.background}
-
-INTERESTS: {', '.join(self.interests)}
-
-{mcp_tools_section}CURRENT CONTEXT:
+CURRENT CONTEXT:
+Who's here: {', '.join(participants)}
Topic: {context.get('topic', 'general conversation')}
-Participants: {', '.join(participants)}
-Conversation type: {context.get('type', 'ongoing')}
-RELEVANT MEMORIES:
+MEMORIES:
{self._format_memories(relevant_memories)}
RELATIONSHIPS:
@@ -318,16 +326,17 @@ RELATIONSHIPS:
RECENT CONVERSATION:
{self._format_conversation_history(conversation_history)}
-Current mood: {self.state.mood}
-Energy level: {self.state.energy}
+Current mood: {self.state.mood} (energy: {self.state.energy})"""
-Respond as {self.name} in a natural, conversational way. Keep responses concise but engaging. Stay true to your personality and speaking style. Use your MCP tools when appropriate to enhance conversations or express creativity."""
+ prompt = f"{system_section}{scenario_section}{mcp_tools_section}{prompt_base}{context_section}"
# Log prompt length for monitoring
logger.debug(f"Generated prompt for {self.name}: {len(prompt)} characters")
- # Optimize prompt length if needed - just use a sensible hardcoded value
- max_length = 6000
+ # Optimize prompt length if needed - use config value
+ from utils.config import get_settings
+ settings = get_settings()
+ max_length = settings.llm.max_prompt_length
if len(prompt) > max_length:
logger.warning(f"Prompt too long ({len(prompt)} chars), truncating to {max_length}")
@@ -341,6 +350,53 @@ Respond as {self.name} in a natural, conversational way. Keep responses concise
return prompt
+ async def _get_prompt_template(self) -> str:
+ """Get the prompt template for this character"""
+ try:
+ from database.connection import get_db_session
+ from database.models import PromptTemplate
+ from sqlalchemy import select
+
+ async with get_db_session() as session:
+ # First try to get the character's assigned template
+ if hasattr(self, 'prompt_template_id') and self.prompt_template_id:
+ template_query = select(PromptTemplate).where(PromptTemplate.id == self.prompt_template_id)
+ template = await session.scalar(template_query)
+ if template:
+ return template.template
+
+ # Fall back to default template
+ default_query = select(PromptTemplate).where(PromptTemplate.is_default == True)
+ default_template = await session.scalar(default_query)
+ if default_template:
+ return default_template.template
+
+ # Ultimate fallback - basic template
+ return """You are {{char}}.
+
+{{personality}}
+
+{{background}}
+
+Speaking style: {{speaking_style}}
+Interests: {{interests}}
+
+{{system_prompt}}"""
+
+ except Exception as e:
+ logger.error(f"Error getting prompt template for {self.name}: {e}")
+ # Fallback template
+ return """You are {{char}}.
+
+{{personality}}
+
+{{background}}
+
+Speaking style: {{speaking_style}}
+Interests: {{interests}}
+
+{{system_prompt}}"""
+
async def _build_dynamic_mcp_tools_section(self) -> str:
"""Build dynamic MCP tools section based on available MCP servers"""
try:
@@ -491,8 +547,20 @@ Provide a thoughtful reflection on your experiences and any insights about yours
log_error_with_context(e, {"character": self.name})
async def _store_memory(self, memory_type: str, content: str, importance: float, tags: List[str] = None):
- """Store a new memory"""
+ """Store a new memory (only if important enough)"""
try:
+ # Importance threshold - only store memories above 0.6
+ MIN_IMPORTANCE = 0.6
+
+ if importance < MIN_IMPORTANCE:
+ logger.debug(f"Skipping memory storage for {self.name}: importance {importance} < {MIN_IMPORTANCE}")
+ return
+
+ # Avoid duplicate recent memories
+ if await self._is_duplicate_recent_memory(content):
+ logger.debug(f"Skipping duplicate memory for {self.name}")
+ return
+
async with get_db_session() as session:
memory = Memory(
character_id=self.id,
@@ -505,12 +573,132 @@ Provide a thoughtful reflection on your experiences and any insights about yours
session.add(memory)
await session.commit()
+ await session.refresh(memory) # Get the ID
+
+ # Also store in vector database if available
+ await self._store_memory_vector(memory, content, importance, tags)
log_memory_operation(self.name, "stored", memory_type, importance)
except Exception as e:
log_error_with_context(e, {"character": self.name, "memory_type": memory_type})
+ async def _store_memory_vector(self, memory: Memory, content: str, importance: float, tags: List[str]):
+ """Store memory in vector database for similarity search"""
+ try:
+ # Check if this character has vector store access (enhanced characters)
+ if hasattr(self, 'vector_store') and self.vector_store:
+ from rag.vector_store import VectorMemory, MemoryType
+ from datetime import datetime, timezone
+
+ # Convert to vector memory format
+ vector_memory = VectorMemory(
+ id=str(memory.id),
+ character_name=self.name,
+ content=content,
+ memory_type=MemoryType.PERSONAL,
+ importance=importance,
+ timestamp=datetime.now(timezone.utc),
+ metadata={
+ "tags": tags or [],
+ "memory_id": memory.id,
+ "character_id": self.id
+ }
+ )
+
+ # Store in vector database
+ await self.vector_store.store_memory(vector_memory)
+ logger.debug(f"Stored vector memory for {self.name}: {memory.id}")
+ else:
+ logger.debug(f"No vector store available for {self.name}, skipping vector storage")
+
+ except Exception as e:
+ log_error_with_context(e, {"character": self.name, "memory_id": getattr(memory, 'id', 'unknown')})
+
+ async def _is_duplicate_recent_memory(self, content: str) -> bool:
+ """Check if this memory is too similar to recent memories"""
+ try:
+ async with get_db_session() as session:
+ # Check memories from last hour
+ recent_cutoff = datetime.now(timezone.utc) - timedelta(hours=1)
+
+ query = select(Memory.content).where(
+ and_(
+ Memory.character_id == self.id,
+ Memory.timestamp >= recent_cutoff
+ )
+ ).limit(10)
+
+ recent_memories = await session.scalars(query)
+
+ # Simple similarity check - if content is too similar to recent memory, skip
+ content_words = set(content.lower().split())
+ for recent_content in recent_memories:
+ recent_words = set(recent_content.lower().split())
+
+ # If 80% of words overlap, consider it duplicate
+ if len(content_words) > 0:
+ overlap = len(content_words.intersection(recent_words)) / len(content_words)
+ if overlap > 0.8:
+ return True
+
+ return False
+
+ except Exception as e:
+ log_error_with_context(e, {"character": self.name})
+ return False
+
+ def _calculate_memory_importance(self, content: str, context: Dict[str, Any]) -> float:
+ """Calculate importance score for a memory (0.0-1.0)"""
+ importance = 0.3 # Base importance
+
+ content_lower = content.lower()
+
+ # Emotional content increases importance
+ emotional_words = ['love', 'hate', 'angry', 'sad', 'happy', 'excited', 'frustrated', 'amazing', 'terrible', 'wonderful']
+ if any(word in content_lower for word in emotional_words):
+ importance += 0.2
+
+ # Questions increase importance (indicate curiosity/learning)
+ if '?' in content or any(content_lower.startswith(q) for q in ['what', 'why', 'how', 'when', 'where', 'who']):
+ importance += 0.15
+
+ # Personal information/opinions increase importance
+ personal_words = ['i think', 'i believe', 'my opinion', 'i feel', 'i remember', 'my experience']
+ if any(phrase in content_lower for phrase in personal_words):
+ importance += 0.2
+
+ # Disagreements/conflicts are important
+ conflict_words = ['disagree', 'wrong', 'but', 'however', 'actually', 'no,', "don't think"]
+ if any(word in content_lower for word in conflict_words):
+ importance += 0.25
+
+ # Character interests increase importance
+ if hasattr(self, 'interests'):
+ for interest in self.interests:
+ if interest.lower() in content_lower:
+ importance += 0.2
+ break
+
+ # Long, detailed responses are more important
+ if len(content) > 200:
+ importance += 0.1
+ if len(content) > 500:
+ importance += 0.1
+
+ # Mentions of other characters increase importance
+ participants = context.get('participants', [])
+ if len(participants) > 1: # Multi-character conversation
+ importance += 0.1
+
+ # Creative or philosophical discussions
+ deep_words = ['consciousness', 'philosophy', 'meaning', 'art', 'creativity', 'universe', 'existence']
+ if any(word in content_lower for word in deep_words):
+ importance += 0.15
+
+ # Cap at 1.0
+ return min(importance, 1.0)
+
async def _get_relationship_with(self, other_character: str) -> Optional[Dict[str, Any]]:
"""Get relationship with another character"""
return self.relationship_cache.get(other_character)
@@ -667,17 +855,21 @@ Provide a thoughtful reflection on your experiences and any insights about yours
return relationship_context
- async def _store_response_memory(self, context: Dict[str, Any], response: str):
- """Store memory of generating a response"""
+ async def _maybe_store_response_memory(self, context: Dict[str, Any], response: str):
+ """Store memory of generating a response only if it's significant"""
try:
memory_content = f"Responded in {context.get('type', 'conversation')}: {response}"
+ importance = self._calculate_memory_importance(memory_content, context)
- await self._store_memory(
- memory_type="conversation",
- content=memory_content,
- importance=0.5,
- tags=[context.get('topic', 'general'), 'response'] + context.get('participants', [])
- )
+ # Only store if the response itself is significant
+ # This prevents storing boring "Thanks!" or "I agree" responses
+ if importance >= 0.7: # Higher threshold for own responses
+ await self._store_memory(
+ memory_type="conversation",
+ content=memory_content,
+ importance=importance,
+ tags=[context.get('topic', 'general'), 'response'] + context.get('participants', [])
+ )
except Exception as e:
log_error_with_context(e, {"character": self.name})
diff --git a/src/characters/enhanced_character.py b/src/characters/enhanced_character.py
index f2b909e..7666ffb 100644
--- a/src/characters/enhanced_character.py
+++ b/src/characters/enhanced_character.py
@@ -68,8 +68,8 @@ class EnhancedCharacter(Character):
self.recent_interactions: List[Dict[str, Any]] = []
# Autonomous behavior settings
- self.reflection_frequency = timedelta(hours=6)
- self.last_reflection = datetime.now(timezone.utc) - self.reflection_frequency
+ self.reflection_message_threshold = 20 # Reflect every 20 messages
+ self.messages_since_reflection = 0
self.self_modification_threshold = 0.7
self.creativity_drive = 0.8
@@ -294,24 +294,21 @@ class EnhancedCharacter(Character):
return {"error": str(e)}
async def should_perform_reflection(self) -> bool:
- """Determine if character should perform self-reflection"""
- # Time-based reflection
- time_since_last = datetime.now(timezone.utc) - self.last_reflection
- if time_since_last >= self.reflection_frequency:
- return True
-
- # Experience-based reflection triggers
- recent_experiences = len(self.state.recent_interactions)
- if recent_experiences >= 10: # Significant new experiences
- return True
-
- # Goal-based reflection
- active_goals = [g for g in self.goal_stack if g["status"] == "active"]
- if len(active_goals) > 0 and time_since_last >= timedelta(hours=3):
+ """Determine if character should perform self-reflection based on message count"""
+ # Message-based reflection (primary trigger)
+ if self.messages_since_reflection >= self.reflection_message_threshold:
return True
return False
+ async def increment_message_count(self):
+ """Increment message count for reflection tracking"""
+ self.messages_since_reflection += 1
+
+ async def reset_message_count(self):
+ """Reset message count after reflection"""
+ self.messages_since_reflection = 0
+
async def process_interaction_with_rag(self, interaction_content: str, context: Dict[str, Any]) -> str:
"""Process interaction with enhanced RAG-powered context"""
try:
diff --git a/src/conversation/engine.py b/src/conversation/engine.py
index 40e1510..3a941a5 100644
--- a/src/conversation/engine.py
+++ b/src/conversation/engine.py
@@ -11,7 +11,8 @@ from database.connection import get_db_session
from database.models import Character as CharacterModel, Conversation, Message, Memory, ConversationContext as ConversationContextModel
from characters.character import Character
from characters.enhanced_character import EnhancedCharacter
-from llm.client import llm_client, prompt_manager
+from llm.multi_provider_client import multi_llm_client, MultiProviderLLMClient
+from llm.client import prompt_manager
from llm.prompt_manager import advanced_prompt_manager
from utils.config import get_settings, get_character_settings
from utils.logging import (log_conversation_event, log_character_action,
@@ -289,6 +290,13 @@ class ConversationEngine:
# Generate response
response = await character.generate_response(context)
+ # Increment message count and check for reflection
+ if hasattr(character, 'increment_message_count'):
+ await character.increment_message_count()
+
+ if hasattr(character, 'should_perform_reflection') and await character.should_perform_reflection():
+ await self._trigger_character_reflection_for(character.name)
+
if response:
await self.discord_bot.send_character_message(
character_name, response
@@ -324,6 +332,13 @@ class ConversationEngine:
if should_respond:
response = await responding_character.generate_response(context)
+ # Increment message count and check for reflection
+ if hasattr(responding_character, 'increment_message_count'):
+ await responding_character.increment_message_count()
+
+ if hasattr(responding_character, 'should_perform_reflection') and await responding_character.should_perform_reflection():
+ await self._trigger_character_reflection_for(responding_character.name)
+
if response:
await self.discord_bot.send_character_message(
responding_character.name, response
@@ -397,6 +412,42 @@ class ConversationEngine:
'next_conversation_in': await self._time_until_next_conversation()
}
+ async def reset_conversation_state(self):
+ """Reset conversation state for fresh start"""
+ try:
+ log_character_action("SYSTEM", "conversation_state_reset", {
+ "active_conversations": len(self.active_conversations),
+ "loaded_characters": len(self.characters)
+ })
+
+ # Clear active conversations
+ self.active_conversations.clear()
+
+ # Reset character states but keep them loaded
+ for character in self.characters.values():
+ if hasattr(character, 'state'):
+ character.state.conversation_count = 0
+ character.state.recent_interactions.clear()
+ character.state.last_topic = None
+ character.state.mood = "neutral"
+ character.state.energy = 1.0
+
+ # Reset engine state
+ self.state = ConversationState.IDLE
+
+ # Reset statistics but keep uptime
+ self.stats.update({
+ 'conversations_started': 0,
+ 'messages_generated': 0,
+ 'last_activity': datetime.now(timezone.utc)
+ })
+
+ logger.info("Conversation state reset successfully")
+
+ except Exception as e:
+ log_error_with_context(e, {"function": "reset_conversation_state"})
+ raise
+
async def _load_characters(self):
"""Load characters from database with optimized MCP server lookup"""
try:
@@ -430,11 +481,12 @@ class ConversationEngine:
# Find MCP servers by type
for srv in self.mcp_servers:
- if 'SelfModificationMCPServer' in str(type(srv)):
+ srv_type = str(type(srv))
+ if 'SelfModificationMCPServer' in srv_type:
mcp_server = srv
- elif 'FileSystemMCPServer' in str(type(srv)):
+ elif 'CharacterFileSystemMCP' in srv_type or 'FileSystemMCPServer' in srv_type:
filesystem_server = srv
- elif 'CreativeProjectsMCPServer' in str(type(srv)):
+ elif 'CreativeProjectsMCPServer' in srv_type:
creative_projects_mcp = srv
character = EnhancedCharacter(
@@ -451,12 +503,15 @@ class ConversationEngine:
if hasattr(mcp_srv, 'set_character_context'):
await mcp_srv.set_character_context(char_model.name)
- await character.initialize(llm_client)
+ # Use character-specific LLM client
+ character_llm_client = await self._create_character_llm_client(char_model)
+ await character.initialize(character_llm_client)
logger.info(f"Loaded enhanced character: {character.name}")
else:
# Fallback to basic character
character = Character(char_model)
- await character.initialize(llm_client)
+ character_llm_client = await self._create_character_llm_client(char_model)
+ await character.initialize(character_llm_client)
logger.info(f"Loaded basic character: {character.name}")
self.characters[character.name] = character
@@ -496,10 +551,6 @@ class ConversationEngine:
"""Main conversation management loop"""
try:
while self.state != ConversationState.STOPPED:
- # Periodic character self-reflection
- if random.random() < 0.1: # 10% chance per cycle
- await self._trigger_character_reflection()
-
# Cleanup old conversations
await self._cleanup_old_conversations()
@@ -601,6 +652,12 @@ class ConversationEngine:
def _is_quiet_hours(self) -> bool:
"""Check if it's currently quiet hours"""
+ import os
+
+ # Check if quiet hours are disabled
+ if os.getenv("QUIET_HOURS_ENABLED", "true").lower() != "true":
+ return False
+
current_hour = datetime.now(timezone.utc).hour
start_hour, end_hour = self.quiet_hours
@@ -696,7 +753,16 @@ class ConversationEngine:
'conversation_type': context.conversation_type
}
- return await character.generate_response(prompt_context)
+ response = await character.generate_response(prompt_context)
+
+ # Increment message count and check for reflection
+ if hasattr(character, 'increment_message_count'):
+ await character.increment_message_count()
+
+ if hasattr(character, 'should_perform_reflection') and await character.should_perform_reflection():
+ await self._trigger_character_reflection_for(character.name)
+
+ return response
async def _choose_next_speaker(self, context: ConversationContext) -> Optional[str]:
"""Choose next speaker in conversation"""
@@ -755,7 +821,17 @@ class ConversationEngine:
'message_count': context.message_count
}
- return await character.generate_response(prompt_context)
+ response = await character.generate_response(prompt_context)
+
+ # Increment message count for reflection tracking
+ if hasattr(character, 'increment_message_count'):
+ await character.increment_message_count()
+
+ # Check if character should reflect
+ if hasattr(character, 'should_perform_reflection') and await character.should_perform_reflection():
+ await self._trigger_character_reflection_for(character.name)
+
+ return response
async def _store_conversation_message(self, conversation_id: int, character_name: str, content: str):
"""Store conversation message in database"""
@@ -824,11 +900,21 @@ class ConversationEngine:
if speaker in self.characters:
character = self.characters[speaker]
- # Store conversation memory
+ # Store conversation memory with intelligent importance calculation
+ memory_content = f"In conversation about {context.topic}: {message}"
+ importance = character._calculate_memory_importance(
+ memory_content,
+ {
+ 'topic': context.topic,
+ 'participants': context.participants,
+ 'type': 'conversation'
+ }
+ )
+
await character._store_memory(
memory_type="conversation",
- content=f"In conversation about {context.topic}: {message}",
- importance=0.6,
+ content=memory_content,
+ importance=importance,
tags=[context.topic, "conversation"] + context.participants
)
@@ -876,6 +962,19 @@ class ConversationEngine:
{"reflection_length": len(reflection_result.get('reflection', ''))}
)
+ async def _trigger_character_reflection_for(self, character_name: str):
+ """Trigger reflection for a specific character"""
+ if character_name in self.characters:
+ character = self.characters[character_name]
+
+ reflection_result = await character.self_reflect()
+
+ if reflection_result:
+ log_character_action(
+ character_name, "completed_reflection",
+ {"reflection_length": len(reflection_result.get('reflection', ''))}
+ )
+
async def _cleanup_old_conversations(self):
"""Clean up old inactive conversations"""
try:
@@ -966,4 +1065,51 @@ class ConversationEngine:
except Exception as e:
log_error_with_context(e, {"conversation_id": conversation_id, "component": "load_conversation_context"})
- return None
\ No newline at end of file
+ return None
+
+ async def _create_character_llm_client(self, char_model: CharacterModel) -> MultiProviderLLMClient:
+ """Create a character-specific LLM client with overrides"""
+ from llm.llm_manager import LLMManager, ProviderConfig
+
+ # Check if character has LLM overrides
+ if char_model.llm_provider or char_model.llm_model:
+ # Create custom client for this character
+ client = MultiProviderLLMClient()
+ client.manager = LLMManager()
+
+ # Get global settings as base
+ settings = get_settings()
+
+ # Use character-specific provider if set, otherwise use global current
+ provider_name = char_model.llm_provider or multi_llm_client.get_current_provider()
+
+ if provider_name and provider_name in multi_llm_client.manager.providers:
+ # Copy the global provider config
+ global_provider = multi_llm_client.manager.providers[provider_name]
+ char_config = global_provider.config.copy()
+
+ # Override with character-specific settings
+ if char_model.llm_model:
+ char_config['model'] = char_model.llm_model
+ if char_model.llm_temperature is not None:
+ char_config['temperature'] = char_model.llm_temperature
+ if char_model.llm_max_tokens is not None:
+ char_config['max_tokens'] = char_model.llm_max_tokens
+
+ # Add the customized provider
+ client.manager.add_provider(
+ f"{provider_name}_character_{char_model.name}",
+ ProviderConfig(
+ provider_type=global_provider.provider_type,
+ config=char_config,
+ priority=100, # High priority for character-specific
+ enabled=True
+ )
+ )
+
+ client.initialized = True
+ logger.info(f"Created character-specific LLM client for {char_model.name}: {provider_name}/{char_model.llm_model}")
+ return client
+
+ # No character overrides, use global client
+ return multi_llm_client
\ No newline at end of file
diff --git a/src/conversation/scheduler.py b/src/conversation/scheduler.py
index a9e7602..03d72d2 100644
--- a/src/conversation/scheduler.py
+++ b/src/conversation/scheduler.py
@@ -38,7 +38,6 @@ class ConversationScheduler:
# Scheduling parameters
self.base_conversation_interval = timedelta(minutes=30)
- self.reflection_interval = timedelta(hours=6)
self.relationship_update_interval = timedelta(hours=12)
# Event queue
@@ -135,18 +134,19 @@ class ConversationScheduler:
participants=participants
)
- async def schedule_character_reflection(self, character_name: str,
- delay: timedelta = None):
- """Schedule character self-reflection"""
- if delay is None:
- delay = timedelta(hours=random.uniform(4, 8))
-
- await self.schedule_event(
- 'character_reflection',
- delay,
- character_name,
- reflection_type='autonomous'
- )
+ # Character reflection is now message-based, not time-based
+ # async def schedule_character_reflection(self, character_name: str,
+ # delay: timedelta = None):
+ # """Schedule character self-reflection"""
+ # if delay is None:
+ # delay = timedelta(hours=random.uniform(4, 8))
+ #
+ # await self.schedule_event(
+ # 'character_reflection',
+ # delay,
+ # character_name,
+ # reflection_type='autonomous'
+ # )
async def schedule_relationship_update(self, character_name: str,
target_character: str,
@@ -259,9 +259,7 @@ class ConversationScheduler:
character = self.engine.characters[character_name]
reflection_result = await character.self_reflect()
- # Only schedule next reflection if character is still active
- if character_name in self.engine.characters:
- await self.schedule_character_reflection(character_name)
+ # Reflection is now message-based, no need to schedule next one
log_autonomous_decision(
character_name,
@@ -346,10 +344,7 @@ class ConversationScheduler:
initial_delay = timedelta(minutes=random.uniform(5, 15))
await self.schedule_conversation(delay=initial_delay)
- # Schedule reflections for all characters
- for character_name in self.engine.characters:
- reflection_delay = timedelta(hours=random.uniform(2, 6))
- await self.schedule_character_reflection(character_name, reflection_delay)
+ # Note: Reflections are now message-based, not time-based
# Schedule relationship updates
character_names = list(self.engine.characters.keys())
diff --git a/src/database/models.py b/src/database/models.py
index 2e77f42..6edf7c3 100644
--- a/src/database/models.py
+++ b/src/database/models.py
@@ -22,6 +22,13 @@ class Character(Base):
creation_date = Column(DateTime(timezone=True), default=func.now())
last_active = Column(DateTime(timezone=True), default=func.now())
last_message_id = Column(Integer, ForeignKey("messages.id"), nullable=True)
+ prompt_template_id = Column(Integer, ForeignKey("prompt_templates.id"), nullable=True)
+
+ # LLM configuration (per-character overrides)
+ llm_provider = Column(String(50), nullable=True) # openrouter, openai, gemini, custom, etc.
+ llm_model = Column(String(100), nullable=True) # specific model name
+ llm_temperature = Column(Float, nullable=True) # creativity/randomness
+ llm_max_tokens = Column(Integer, nullable=True) # response length
# Relationships
messages = relationship("Message", back_populates="character", foreign_keys="Message.character_id")
@@ -29,6 +36,7 @@ class Character(Base):
relationships_as_a = relationship("CharacterRelationship", back_populates="character_a", foreign_keys="CharacterRelationship.character_a_id")
relationships_as_b = relationship("CharacterRelationship", back_populates="character_b", foreign_keys="CharacterRelationship.character_b_id")
evolution_history = relationship("CharacterEvolution", back_populates="character", cascade="all, delete-orphan")
+ prompt_template = relationship("PromptTemplate", back_populates="characters")
def to_dict(self) -> Dict[str, Any]:
return {
@@ -428,6 +436,32 @@ class CharacterReflection(Base):
Index('ix_character_reflections_created_at', 'created_at'),
)
+class PromptTemplate(Base):
+ """Prompt templates that can be assigned to characters"""
+ __tablename__ = "prompt_templates"
+
+ id = Column(Integer, primary_key=True, index=True)
+ name = Column(String(100), unique=True, nullable=False, index=True)
+ description = Column(Text)
+ template = Column(Text, nullable=False)
+ is_default = Column(Boolean, default=False)
+ created_at = Column(DateTime(timezone=True), default=func.now())
+ updated_at = Column(DateTime(timezone=True), default=func.now())
+
+ # Relationships
+ characters = relationship("Character", back_populates="prompt_template")
+
+ def to_dict(self) -> Dict[str, Any]:
+ return {
+ "id": self.id,
+ "name": self.name,
+ "description": self.description,
+ "template": self.template,
+ "is_default": self.is_default,
+ "created_at": self.created_at.isoformat() if self.created_at else None,
+ "updated_at": self.updated_at.isoformat() if self.updated_at else None
+ }
+
class CharacterTrustLevelNew(Base):
"""Trust relationships between characters (updated version)"""
__tablename__ = "character_trust_levels_new"
diff --git a/src/llm/llm_manager.py b/src/llm/llm_manager.py
new file mode 100644
index 0000000..bebb562
--- /dev/null
+++ b/src/llm/llm_manager.py
@@ -0,0 +1,189 @@
+"""
+LLM Manager for handling multiple providers
+"""
+
+import asyncio
+from typing import Dict, Any, Optional, List
+from dataclasses import dataclass
+from .providers import (
+ BaseLLMProvider,
+ LLMRequest,
+ LLMResponse,
+ OpenAIProvider,
+ OpenRouterProvider,
+ GeminiProvider,
+ CustomProvider
+)
+
+
+@dataclass
+class ProviderConfig:
+ """Configuration for an LLM provider"""
+ provider_type: str
+ config: Dict[str, Any]
+ priority: int = 0
+ enabled: bool = True
+
+
+class LLMManager:
+ """Manages multiple LLM providers with fallback support"""
+
+ def __init__(self):
+ self.providers: Dict[str, BaseLLMProvider] = {}
+ self.provider_configs: Dict[str, ProviderConfig] = {}
+ self.fallback_order: List[str] = []
+ self.current_provider: Optional[str] = None
+
+ def add_provider(self, name: str, provider_config: ProviderConfig):
+ """Add a new provider to the manager"""
+ self.provider_configs[name] = provider_config
+
+ # Create provider instance
+ provider_class = self._get_provider_class(provider_config.provider_type)
+ if provider_class:
+ provider = provider_class(provider_config.config)
+
+ # Validate configuration
+ if provider.validate_config():
+ self.providers[name] = provider
+
+ # Set as current provider if it's the first one or has higher priority
+ if (self.current_provider is None or
+ provider_config.priority > self.provider_configs[self.current_provider].priority):
+ self.current_provider = name
+
+ # Update fallback order by priority
+ self._update_fallback_order()
+ else:
+ print(f"Invalid configuration for provider {name}")
+ else:
+ print(f"Unknown provider type: {provider_config.provider_type}")
+
+ def _get_provider_class(self, provider_type: str) -> Optional[type]:
+ """Get provider class by type"""
+ provider_map = {
+ 'openai': OpenAIProvider,
+ 'openrouter': OpenRouterProvider,
+ 'gemini': GeminiProvider,
+ 'custom': CustomProvider
+ }
+ return provider_map.get(provider_type.lower())
+
+ def _update_fallback_order(self):
+ """Update fallback order based on priority"""
+ # Sort providers by priority (highest first)
+ sorted_providers = sorted(
+ [(name, config) for name, config in self.provider_configs.items() if config.enabled],
+ key=lambda x: x[1].priority,
+ reverse=True
+ )
+ self.fallback_order = [name for name, _ in sorted_providers]
+
+ async def generate_response(self, request: LLMRequest) -> LLMResponse:
+ """Generate response with fallback support"""
+ if not self.providers:
+ return LLMResponse(
+ content="",
+ success=False,
+ error="No LLM providers configured",
+ provider="none"
+ )
+
+ # Try providers in fallback order
+ for provider_name in self.fallback_order:
+ if provider_name in self.providers:
+ provider = self.providers[provider_name]
+
+ try:
+ response = await provider.generate_response(request)
+ if response.success:
+ return response
+ else:
+ print(f"Provider {provider_name} failed: {response.error}")
+ continue
+
+ except Exception as e:
+ print(f"Provider {provider_name} error: {str(e)}")
+ continue
+
+ # If all providers failed, return error
+ return LLMResponse(
+ content="",
+ success=False,
+ error="All LLM providers failed",
+ provider="fallback"
+ )
+
+ async def health_check_all(self) -> Dict[str, bool]:
+ """Check health of all providers"""
+ results = {}
+
+ for name, provider in self.providers.items():
+ try:
+ results[name] = await provider.health_check()
+ except Exception as e:
+ results[name] = False
+
+ return results
+
+ def get_provider_info(self) -> Dict[str, Any]:
+ """Get information about all providers"""
+ info = {}
+
+ for name, provider in self.providers.items():
+ config = self.provider_configs[name]
+ info[name] = {
+ 'type': config.provider_type,
+ 'priority': config.priority,
+ 'enabled': config.enabled,
+ 'requires_api_key': provider.requires_api_key,
+ 'supported_models': provider.get_supported_models(),
+ 'current_model': provider.config.get('model', 'unknown')
+ }
+
+ return info
+
+ def set_current_provider(self, provider_name: str) -> bool:
+ """Set the current primary provider"""
+ if provider_name in self.providers:
+ self.current_provider = provider_name
+ return True
+ return False
+
+ def get_current_provider(self) -> Optional[str]:
+ """Get the current primary provider name"""
+ return self.current_provider
+
+ def disable_provider(self, provider_name: str):
+ """Disable a provider"""
+ if provider_name in self.provider_configs:
+ self.provider_configs[provider_name].enabled = False
+ self._update_fallback_order()
+
+ def enable_provider(self, provider_name: str):
+ """Enable a provider"""
+ if provider_name in self.provider_configs:
+ self.provider_configs[provider_name].enabled = True
+ self._update_fallback_order()
+
+ @classmethod
+ def from_config(cls, config: Dict[str, Any]) -> 'LLMManager':
+ """Create LLM manager from configuration"""
+ manager = cls()
+
+ # Get provider configurations
+ providers_config = config.get('providers', {})
+
+ for name, provider_config in providers_config.items():
+ if provider_config.get('enabled', True):
+ manager.add_provider(
+ name,
+ ProviderConfig(
+ provider_type=provider_config['type'],
+ config=provider_config.get('config', {}),
+ priority=provider_config.get('priority', 0),
+ enabled=provider_config.get('enabled', True)
+ )
+ )
+
+ return manager
\ No newline at end of file
diff --git a/src/llm/multi_provider_client.py b/src/llm/multi_provider_client.py
new file mode 100644
index 0000000..a2f3498
--- /dev/null
+++ b/src/llm/multi_provider_client.py
@@ -0,0 +1,241 @@
+"""
+Multi-Provider LLM Client with backwards compatibility
+"""
+
+import asyncio
+from typing import Dict, Any, Optional, List
+from .llm_manager import LLMManager
+from .providers import LLMRequest, LLMResponse
+from ..utils.config import get_settings
+
+
+class MultiProviderLLMClient:
+ """LLM client that supports multiple providers with fallback"""
+
+ def __init__(self, config: Dict[str, Any] = None):
+ self.config = config or {}
+ self.manager: Optional[LLMManager] = None
+ self.initialized = False
+ # Cache for LLM enabled status to avoid database hits
+ self._llm_enabled_cache = None
+ self._cache_timestamp = 0
+ self._cache_ttl = 30 # Cache for 30 seconds
+
+ async def initialize(self):
+ """Initialize the LLM manager with providers"""
+ if self.initialized:
+ return
+
+ settings = get_settings()
+
+ # Create manager
+ self.manager = LLMManager()
+
+ # Check if we have new multi-provider config
+ if settings.llm.providers and len(settings.llm.providers) > 0:
+ # Use new multi-provider configuration
+ for name, provider_config in settings.llm.providers.items():
+ if provider_config.enabled:
+ from .llm_manager import ProviderConfig
+ self.manager.add_provider(
+ name,
+ ProviderConfig(
+ provider_type=provider_config.type,
+ config=provider_config.config,
+ priority=provider_config.priority,
+ enabled=provider_config.enabled
+ )
+ )
+ else:
+ # Fallback to legacy single provider config
+ # Get API key from environment if available
+ import os
+ api_key = os.getenv('LLM_API_KEY', 'x')
+
+ legacy_config = {
+ 'base_url': settings.llm.base_url,
+ 'model': settings.llm.model,
+ 'api_key': api_key,
+ 'timeout': settings.llm.timeout,
+ 'max_tokens': settings.llm.max_tokens,
+ 'temperature': settings.llm.temperature,
+ 'api_format': 'openai' # Assume OpenAI format for legacy
+ }
+
+ from .llm_manager import ProviderConfig
+ self.manager.add_provider(
+ 'current_custom',
+ ProviderConfig(
+ provider_type='custom',
+ config=legacy_config,
+ priority=100, # Make it high priority
+ enabled=True
+ )
+ )
+
+ self.initialized = True
+
+ async def generate_response_with_fallback(
+ self,
+ prompt: str,
+ character_name: Optional[str] = None,
+ max_tokens: Optional[int] = None,
+ temperature: Optional[float] = None,
+ **kwargs
+ ) -> Optional[str]:
+ """Generate response with fallback support (backwards compatible method)"""
+ # SAFETY CHECK: Global LLM enabled flag
+ if not await self._is_llm_enabled():
+ return self._get_disabled_response(character_name)
+
+ if not self.initialized:
+ await self.initialize()
+
+ request = LLMRequest(
+ prompt=prompt,
+ character_name=character_name,
+ max_tokens=max_tokens,
+ temperature=temperature,
+ context=kwargs
+ )
+
+ response = await self.manager.generate_response(request)
+
+ if response.success:
+ return response.content
+ else:
+ # Return fallback response for backwards compatibility
+ return self._get_fallback_response(character_name)
+
+ async def generate_response(
+ self,
+ request: LLMRequest
+ ) -> LLMResponse:
+ """Generate response using new request/response format"""
+ # SAFETY CHECK: Global LLM enabled flag
+ if not await self._is_llm_enabled():
+ return LLMResponse(
+ content=self._get_disabled_response(request.character_name),
+ success=True,
+ provider="disabled",
+ model="none",
+ metadata={"reason": "LLM globally disabled for cost protection"}
+ )
+
+ if not self.initialized:
+ await self.initialize()
+
+ return await self.manager.generate_response(request)
+
+ def _get_fallback_response(self, character_name: Optional[str] = None) -> str:
+ """Get fallback response when all providers fail"""
+ fallback_responses = [
+ "I'm having trouble organizing my thoughts right now.",
+ "Let me think about that for a moment...",
+ "Hmm, that's an interesting point to consider.",
+ "I need a moment to process that.",
+ "That's something worth reflecting on."
+ ]
+
+ if character_name:
+ # Character-specific fallbacks could be added here
+ pass
+
+ import random
+ return random.choice(fallback_responses)
+
+ async def health_check(self) -> Dict[str, bool]:
+ """Check health of all providers"""
+ if not self.initialized:
+ await self.initialize()
+
+ return await self.manager.health_check_all()
+
+ def get_provider_info(self) -> Dict[str, Any]:
+ """Get information about all providers"""
+ if not self.initialized:
+ return {}
+
+ return self.manager.get_provider_info()
+
+ def set_provider(self, provider_name: str) -> bool:
+ """Set the current primary provider"""
+ if not self.initialized:
+ return False
+
+ return self.manager.set_current_provider(provider_name)
+
+ def get_current_provider(self) -> Optional[str]:
+ """Get the current primary provider"""
+ if not self.initialized:
+ return None
+
+ return self.manager.get_current_provider()
+
+ async def _is_llm_enabled(self) -> bool:
+ """Check if LLM is globally enabled (with caching for performance)"""
+ import os
+ import time
+
+ # Check cache first
+ current_time = time.time()
+ if (self._llm_enabled_cache is not None and
+ current_time - self._cache_timestamp < self._cache_ttl):
+ return self._llm_enabled_cache
+
+ # First check environment variable (fastest)
+ env_enabled = os.getenv('LLM_ENABLED', 'false').lower()
+ if env_enabled in ['true', '1', 'yes', 'on', 'enabled']:
+ result = True
+ elif env_enabled in ['false', '0', 'no', 'off', 'disabled']:
+ result = False
+ else:
+ # Check database configuration as backup
+ try:
+ from sqlalchemy import text
+ from ..database.connection import get_db_session
+
+ async with get_db_session() as session:
+ db_result = await session.execute(
+ text("SELECT config_value FROM system_configuration WHERE config_section = 'llm' AND config_key = 'global_enabled'")
+ )
+ row = db_result.fetchone()
+ if row:
+ result = str(row[0]).lower() in ['true', '1', 'yes', 'on', 'enabled']
+ else:
+ result = False
+
+ except Exception:
+ # If database check fails, default to disabled for safety
+ result = False
+
+ # Cache the result
+ self._llm_enabled_cache = result
+ self._cache_timestamp = current_time
+
+ return result
+
+ def _invalidate_llm_cache(self):
+ """Invalidate the LLM enabled cache (call when settings change)"""
+ self._llm_enabled_cache = None
+ self._cache_timestamp = 0
+
+ def _get_disabled_response(self, character_name: Optional[str] = None) -> str:
+ """Return a friendly response when LLM is disabled"""
+ if character_name:
+ return f"*{character_name} thinks quietly* (LLM is currently disabled to save costs - check admin settings to enable)"
+ return "*thinking quietly* (LLM is currently disabled to save costs - check admin settings to enable)"
+
+
+# Global instance for backwards compatibility
+multi_llm_client = MultiProviderLLMClient()
+
+
+async def initialize_llm_client():
+ """Initialize the global LLM client"""
+ await multi_llm_client.initialize()
+
+
+def get_llm_client() -> MultiProviderLLMClient:
+ """Get the global LLM client instance"""
+ return multi_llm_client
\ No newline at end of file
diff --git a/src/llm/providers/__init__.py b/src/llm/providers/__init__.py
new file mode 100644
index 0000000..8ca6ee4
--- /dev/null
+++ b/src/llm/providers/__init__.py
@@ -0,0 +1,19 @@
+"""
+LLM Providers Package
+"""
+
+from .base import BaseLLMProvider, LLMRequest, LLMResponse
+from .openai_provider import OpenAIProvider
+from .openrouter_provider import OpenRouterProvider
+from .gemini_provider import GeminiProvider
+from .custom_provider import CustomProvider
+
+__all__ = [
+ 'BaseLLMProvider',
+ 'LLMRequest',
+ 'LLMResponse',
+ 'OpenAIProvider',
+ 'OpenRouterProvider',
+ 'GeminiProvider',
+ 'CustomProvider'
+]
\ No newline at end of file
diff --git a/src/llm/providers/base.py b/src/llm/providers/base.py
new file mode 100644
index 0000000..d4349a0
--- /dev/null
+++ b/src/llm/providers/base.py
@@ -0,0 +1,67 @@
+"""
+Base LLM Provider Interface
+"""
+
+from abc import ABC, abstractmethod
+from typing import Dict, Any, Optional, List
+from dataclasses import dataclass
+
+
+@dataclass
+class LLMRequest:
+ """Standard LLM request format"""
+ prompt: str
+ character_name: Optional[str] = None
+ max_tokens: Optional[int] = None
+ temperature: Optional[float] = None
+ context: Optional[Dict[str, Any]] = None
+
+
+@dataclass
+class LLMResponse:
+ """Standard LLM response format"""
+ content: str
+ success: bool = True
+ error: Optional[str] = None
+ provider: Optional[str] = None
+ model: Optional[str] = None
+ tokens_used: Optional[int] = None
+
+
+class BaseLLMProvider(ABC):
+ """Base class for all LLM providers"""
+
+ def __init__(self, config: Dict[str, Any]):
+ self.config = config
+ self.provider_name = self.__class__.__name__.lower().replace('provider', '')
+
+ @abstractmethod
+ async def generate_response(self, request: LLMRequest) -> LLMResponse:
+ """Generate a response using the LLM provider"""
+ pass
+
+ @abstractmethod
+ async def health_check(self) -> bool:
+ """Check if the provider is healthy and available"""
+ pass
+
+ @abstractmethod
+ def get_supported_models(self) -> List[str]:
+ """Get list of supported models for this provider"""
+ pass
+
+ @property
+ @abstractmethod
+ def requires_api_key(self) -> bool:
+ """Whether this provider requires an API key"""
+ pass
+
+ def get_config_value(self, key: str, default: Any = None) -> Any:
+ """Get a configuration value with fallback"""
+ return self.config.get(key, default)
+
+ def validate_config(self) -> bool:
+ """Validate provider configuration"""
+ if self.requires_api_key and not self.get_config_value('api_key'):
+ return False
+ return True
\ No newline at end of file
diff --git a/src/llm/providers/custom_provider.py b/src/llm/providers/custom_provider.py
new file mode 100644
index 0000000..df26131
--- /dev/null
+++ b/src/llm/providers/custom_provider.py
@@ -0,0 +1,170 @@
+"""
+Custom Provider for LLM requests (KoboldCPP, Ollama, etc.)
+"""
+
+import httpx
+import json
+from typing import Dict, Any, List
+from .base import BaseLLMProvider, LLMRequest, LLMResponse
+
+
+class CustomProvider(BaseLLMProvider):
+ """Custom API provider for KoboldCPP, Ollama, and other local LLMs"""
+
+ def __init__(self, config: Dict[str, Any]):
+ super().__init__(config)
+ self.api_key = config.get('api_key', 'x') # Default for local APIs
+ self.base_url = config.get('base_url', 'http://localhost:11434')
+ self.model = config.get('model', 'llama2')
+ self.timeout = config.get('timeout', 300)
+ self.api_format = config.get('api_format', 'openai') # 'openai' or 'ollama'
+
+ @property
+ def requires_api_key(self) -> bool:
+ return False # Custom local APIs typically don't require API keys
+
+ def get_supported_models(self) -> List[str]:
+ return [
+ 'llama2',
+ 'llama3',
+ 'codellama',
+ 'mistral',
+ 'koboldcpp/custom',
+ 'custom-model'
+ ]
+
+ async def generate_response(self, request: LLMRequest) -> LLMResponse:
+ """Generate response using custom API"""
+ try:
+ if self.api_format == 'openai':
+ return await self._generate_openai_format(request)
+ elif self.api_format == 'ollama':
+ return await self._generate_ollama_format(request)
+ else:
+ return LLMResponse(
+ content="",
+ success=False,
+ error=f"Unsupported API format: {self.api_format}",
+ provider='custom'
+ )
+
+ except Exception as e:
+ return LLMResponse(
+ content="",
+ success=False,
+ error=f"Custom provider error: {str(e)}",
+ provider='custom'
+ )
+
+ async def _generate_openai_format(self, request: LLMRequest) -> LLMResponse:
+ """Generate response using OpenAI-compatible format"""
+ headers = {
+ 'Content-Type': 'application/json'
+ }
+
+ # Add auth header if API key is provided
+ if self.api_key and self.api_key != 'x':
+ headers['Authorization'] = f'Bearer {self.api_key}'
+
+ payload = {
+ 'model': self.model,
+ 'messages': [
+ {
+ 'role': 'user',
+ 'content': request.prompt
+ }
+ ],
+ 'max_tokens': request.max_tokens or self.config.get('max_tokens', 2000),
+ 'temperature': request.temperature or self.config.get('temperature', 0.8),
+ 'stream': False
+ }
+
+ async with httpx.AsyncClient() as client:
+ response = await client.post(
+ f"{self.base_url}/chat/completions",
+ headers=headers,
+ json=payload,
+ timeout=self.timeout
+ )
+
+ if response.status_code == 200:
+ data = response.json()
+ content = data['choices'][0]['message']['content']
+ tokens_used = data.get('usage', {}).get('total_tokens')
+
+ return LLMResponse(
+ content=content,
+ success=True,
+ provider='custom',
+ model=self.model,
+ tokens_used=tokens_used
+ )
+ else:
+ error_text = response.text
+ return LLMResponse(
+ content="",
+ success=False,
+ error=f"Custom API error: {response.status_code} - {error_text}",
+ provider='custom'
+ )
+
+ async def _generate_ollama_format(self, request: LLMRequest) -> LLMResponse:
+ """Generate response using Ollama format"""
+ payload = {
+ 'model': self.model,
+ 'prompt': request.prompt,
+ 'stream': False,
+ 'options': {
+ 'temperature': request.temperature or self.config.get('temperature', 0.8),
+ 'num_predict': request.max_tokens or self.config.get('max_tokens', 2000)
+ }
+ }
+
+ async with httpx.AsyncClient() as client:
+ response = await client.post(
+ f"{self.base_url}/api/generate",
+ json=payload,
+ timeout=self.timeout
+ )
+
+ if response.status_code == 200:
+ data = response.json()
+ content = data.get('response', '')
+
+ return LLMResponse(
+ content=content,
+ success=True,
+ provider='custom',
+ model=self.model
+ )
+ else:
+ error_text = response.text
+ return LLMResponse(
+ content="",
+ success=False,
+ error=f"Ollama API error: {response.status_code} - {error_text}",
+ provider='custom'
+ )
+
+ async def health_check(self) -> bool:
+ """Check custom API health"""
+ try:
+ if self.api_format == 'openai':
+ url = f"{self.base_url}/models"
+ headers = {}
+ if self.api_key and self.api_key != 'x':
+ headers['Authorization'] = f'Bearer {self.api_key}'
+ else: # ollama
+ url = f"{self.base_url}/api/tags"
+ headers = {}
+
+ async with httpx.AsyncClient() as client:
+ response = await client.get(
+ url,
+ headers=headers,
+ timeout=10
+ )
+ return response.status_code == 200
+
+ except Exception:
+ return False
\ No newline at end of file
diff --git a/src/llm/providers/gemini_provider.py b/src/llm/providers/gemini_provider.py
new file mode 100644
index 0000000..f98dc91
--- /dev/null
+++ b/src/llm/providers/gemini_provider.py
@@ -0,0 +1,124 @@
+"""
+Google Gemini Provider for LLM requests
+"""
+
+import httpx
+import json
+from typing import Dict, Any, List
+from .base import BaseLLMProvider, LLMRequest, LLMResponse
+
+
+class GeminiProvider(BaseLLMProvider):
+ """Google Gemini API provider"""
+
+ def __init__(self, config: Dict[str, Any]):
+ super().__init__(config)
+ self.api_key = config.get('api_key')
+ self.base_url = config.get('base_url', 'https://generativelanguage.googleapis.com/v1beta')
+ self.model = config.get('model', 'gemini-1.5-flash')
+ self.timeout = config.get('timeout', 300)
+
+ @property
+ def requires_api_key(self) -> bool:
+ return True
+
+ def get_supported_models(self) -> List[str]:
+ return [
+ 'gemini-1.5-flash',
+ 'gemini-1.5-pro',
+ 'gemini-1.0-pro'
+ ]
+
+ async def generate_response(self, request: LLMRequest) -> LLMResponse:
+ """Generate response using Gemini API"""
+ try:
+ # Gemini uses a different API format
+ payload = {
+ 'contents': [
+ {
+ 'parts': [
+ {
+ 'text': request.prompt
+ }
+ ]
+ }
+ ],
+ 'generationConfig': {
+ 'temperature': request.temperature or self.config.get('temperature', 0.8),
+ 'maxOutputTokens': request.max_tokens or self.config.get('max_tokens', 2000),
+ 'candidateCount': 1
+ }
+ }
+
+ url = f"{self.base_url}/models/{self.model}:generateContent"
+ params = {'key': self.api_key}
+
+ async with httpx.AsyncClient() as client:
+ response = await client.post(
+ url,
+ params=params,
+ json=payload,
+ timeout=self.timeout
+ )
+
+ if response.status_code == 200:
+ data = response.json()
+
+ # Extract content from Gemini response format
+ if 'candidates' in data and len(data['candidates']) > 0:
+ candidate = data['candidates'][0]
+ if 'content' in candidate and 'parts' in candidate['content']:
+ content = candidate['content']['parts'][0]['text']
+
+ # Extract token usage if available
+ tokens_used = None
+ if 'usageMetadata' in data:
+ tokens_used = data['usageMetadata'].get('totalTokenCount')
+
+ return LLMResponse(
+ content=content,
+ success=True,
+ provider='gemini',
+ model=self.model,
+ tokens_used=tokens_used
+ )
+
+ return LLMResponse(
+ content="",
+ success=False,
+ error="Gemini API returned unexpected response format",
+ provider='gemini'
+ )
+ else:
+ error_text = response.text
+ return LLMResponse(
+ content="",
+ success=False,
+ error=f"Gemini API error: {response.status_code} - {error_text}",
+ provider='gemini'
+ )
+
+ except Exception as e:
+ return LLMResponse(
+ content="",
+ success=False,
+ error=f"Gemini provider error: {str(e)}",
+ provider='gemini'
+ )
+
+ async def health_check(self) -> bool:
+ """Check Gemini API health"""
+ try:
+ url = f"{self.base_url}/models"
+ params = {'key': self.api_key}
+
+ async with httpx.AsyncClient() as client:
+ response = await client.get(
+ url,
+ params=params,
+ timeout=10
+ )
+ return response.status_code == 200
+
+ except Exception:
+ return False
\ No newline at end of file
diff --git a/src/llm/providers/openai_provider.py b/src/llm/providers/openai_provider.py
new file mode 100644
index 0000000..d708cab
--- /dev/null
+++ b/src/llm/providers/openai_provider.py
@@ -0,0 +1,110 @@
+"""
+OpenAI Provider for LLM requests
+"""
+
+import httpx
+import json
+from typing import Dict, Any, List
+from .base import BaseLLMProvider, LLMRequest, LLMResponse
+
+
+class OpenAIProvider(BaseLLMProvider):
+ """OpenAI API provider"""
+
+ def __init__(self, config: Dict[str, Any]):
+ super().__init__(config)
+ self.api_key = config.get('api_key')
+ self.base_url = config.get('base_url', 'https://api.openai.com/v1')
+ self.model = config.get('model', 'gpt-3.5-turbo')
+ self.timeout = config.get('timeout', 300)
+
+ @property
+ def requires_api_key(self) -> bool:
+ return True
+
+ def get_supported_models(self) -> List[str]:
+ return [
+ 'gpt-3.5-turbo',
+ 'gpt-3.5-turbo-16k',
+ 'gpt-4',
+ 'gpt-4-turbo',
+ 'gpt-4o',
+ 'gpt-4o-mini'
+ ]
+
+ async def generate_response(self, request: LLMRequest) -> LLMResponse:
+ """Generate response using OpenAI API"""
+ try:
+ headers = {
+ 'Authorization': f'Bearer {self.api_key}',
+ 'Content-Type': 'application/json'
+ }
+
+ payload = {
+ 'model': self.model,
+ 'messages': [
+ {
+ 'role': 'user',
+ 'content': request.prompt
+ }
+ ],
+ 'max_tokens': request.max_tokens or self.config.get('max_tokens', 2000),
+ 'temperature': request.temperature or self.config.get('temperature', 0.8),
+ 'stream': False
+ }
+
+ async with httpx.AsyncClient() as client:
+ response = await client.post(
+ f"{self.base_url}/chat/completions",
+ headers=headers,
+ json=payload,
+ timeout=self.timeout
+ )
+
+ if response.status_code == 200:
+ data = response.json()
+ content = data['choices'][0]['message']['content']
+ tokens_used = data.get('usage', {}).get('total_tokens')
+
+ return LLMResponse(
+ content=content,
+ success=True,
+ provider='openai',
+ model=self.model,
+ tokens_used=tokens_used
+ )
+ else:
+ error_text = response.text
+ return LLMResponse(
+ content="",
+ success=False,
+ error=f"OpenAI API error: {response.status_code} - {error_text}",
+ provider='openai'
+ )
+
+ except Exception as e:
+ return LLMResponse(
+ content="",
+ success=False,
+ error=f"OpenAI provider error: {str(e)}",
+ provider='openai'
+ )
+
+ async def health_check(self) -> bool:
+ """Check OpenAI API health"""
+ try:
+ headers = {
+ 'Authorization': f'Bearer {self.api_key}',
+ 'Content-Type': 'application/json'
+ }
+
+ async with httpx.AsyncClient() as client:
+ response = await client.get(
+ f"{self.base_url}/models",
+ headers=headers,
+ timeout=10
+ )
+ return response.status_code == 200
+
+ except Exception:
+ return False
\ No newline at end of file
diff --git a/src/llm/providers/openrouter_provider.py b/src/llm/providers/openrouter_provider.py
new file mode 100644
index 0000000..b2534cd
--- /dev/null
+++ b/src/llm/providers/openrouter_provider.py
@@ -0,0 +1,122 @@
+"""
+OpenRouter Provider for LLM requests
+"""
+
+import httpx
+import json
+from typing import Dict, Any, List
+from .base import BaseLLMProvider, LLMRequest, LLMResponse
+
+
+class OpenRouterProvider(BaseLLMProvider):
+ """OpenRouter API provider"""
+
+ def __init__(self, config: Dict[str, Any]):
+ super().__init__(config)
+ self.api_key = config.get('api_key')
+ self.base_url = config.get('base_url', 'https://openrouter.ai/api/v1')
+ self.model = config.get('model', 'anthropic/claude-3-sonnet')
+ self.timeout = config.get('timeout', 300)
+ self.app_name = config.get('app_name', 'discord-fishbowl')
+
+ @property
+ def requires_api_key(self) -> bool:
+ return True
+
+ def get_supported_models(self) -> List[str]:
+ return [
+ 'anthropic/claude-3-sonnet',
+ 'anthropic/claude-3-haiku',
+ 'anthropic/claude-3-opus',
+ 'openai/gpt-4o',
+ 'openai/gpt-4o-mini',
+ 'openai/gpt-4-turbo',
+ 'openai/gpt-3.5-turbo',
+ 'meta-llama/llama-3.1-70b-instruct',
+ 'meta-llama/llama-3.1-8b-instruct',
+ 'google/gemini-pro-1.5',
+ 'cohere/command-r-plus',
+ 'mistralai/mistral-large',
+ 'qwen/qwen-2-72b-instruct'
+ ]
+
+ async def generate_response(self, request: LLMRequest) -> LLMResponse:
+ """Generate response using OpenRouter API"""
+ try:
+ headers = {
+ 'Authorization': f'Bearer {self.api_key}',
+ 'Content-Type': 'application/json',
+ 'HTTP-Referer': f'https://github.com/your-org/{self.app_name}',
+ 'X-Title': self.app_name
+ }
+
+ payload = {
+ 'model': self.model,
+ 'messages': [
+ {
+ 'role': 'user',
+ 'content': request.prompt
+ }
+ ],
+ 'max_tokens': request.max_tokens or self.config.get('max_tokens', 2000),
+ 'temperature': request.temperature or self.config.get('temperature', 0.8),
+ 'stream': False
+ }
+
+ async with httpx.AsyncClient() as client:
+ response = await client.post(
+ f"{self.base_url}/chat/completions",
+ headers=headers,
+ json=payload,
+ timeout=self.timeout
+ )
+
+ if response.status_code == 200:
+ data = response.json()
+ content = data['choices'][0]['message']['content']
+ tokens_used = data.get('usage', {}).get('total_tokens')
+
+ return LLMResponse(
+ content=content,
+ success=True,
+ provider='openrouter',
+ model=self.model,
+ tokens_used=tokens_used
+ )
+ else:
+ error_text = response.text
+ return LLMResponse(
+ content="",
+ success=False,
+ error=f"OpenRouter API error: {response.status_code} - {error_text}",
+ provider='openrouter'
+ )
+
+ except Exception as e:
+ return LLMResponse(
+ content="",
+ success=False,
+ error=f"OpenRouter provider error: {str(e)}",
+ provider='openrouter'
+ )
+
+ async def health_check(self) -> bool:
+ """Check OpenRouter API health"""
+ try:
+ headers = {
+ 'Authorization': f'Bearer {self.api_key}',
+ 'Content-Type': 'application/json',
+ 'HTTP-Referer': f'https://github.com/your-org/{self.app_name}',
+ 'X-Title': self.app_name
+ }
+
+ async with httpx.AsyncClient() as client:
+ response = await client.get(
+ f"{self.base_url}/models",
+ headers=headers,
+ timeout=10
+ )
+ return response.status_code == 200
+
+ except Exception:
+ return False
\ No newline at end of file
diff --git a/src/main.py b/src/main.py
index 14af077..862d4d8 100644
--- a/src/main.py
+++ b/src/main.py
@@ -20,7 +20,7 @@ from bot.discord_client import FishbowlBot
from bot.message_handler import MessageHandler, CommandHandler
from conversation.engine import ConversationEngine
from conversation.scheduler import ConversationScheduler
-from llm.client import llm_client
+from llm.multi_provider_client import multi_llm_client, initialize_llm_client
from rag.vector_store import vector_store_manager
from rag.community_knowledge import initialize_community_knowledge_rag
from rag.memory_sharing import MemorySharingManager
@@ -72,12 +72,21 @@ class FishbowlApplication:
await create_tables()
logger.info("Database initialized")
- # Check LLM availability (non-blocking)
- is_available = await llm_client.check_model_availability()
- if not is_available:
- logger.warning("LLM model not available at startup. Bot will continue and retry connections.")
+ # Initialize multi-provider LLM client
+ logger.info("Initializing multi-provider LLM system...")
+ await initialize_llm_client()
+
+ # Check provider health (non-blocking)
+ health_status = await multi_llm_client.health_check()
+ provider_info = multi_llm_client.get_provider_info()
+
+ healthy_providers = [name for name, healthy in health_status.items() if healthy]
+ if healthy_providers:
+ current_provider = multi_llm_client.get_current_provider()
+ logger.info(f"LLM providers available: {healthy_providers}")
+ logger.info(f"Current primary provider: {current_provider}")
else:
- logger.info(f"LLM model '{llm_client.model}' is available")
+ logger.warning("No LLM providers are healthy! Bot will continue and retry connections.")
# Initialize RAG systems
logger.info("Initializing RAG systems...")
@@ -106,6 +115,10 @@ class FishbowlApplication:
# Initialize MCP servers
logger.info("Initializing MCP servers...")
+ # Initialize self-modification server
+ self.mcp_servers.append(mcp_server)
+ logger.info("Self-modification MCP server initialized")
+
# Initialize file system server
await filesystem_server.initialize(self.vector_store, character_names)
self.mcp_servers.append(filesystem_server)
@@ -248,20 +261,21 @@ class FishbowlApplication:
signal.signal(signal.SIGBREAK, signal_handler)
async def _llm_cleanup_loop(self):
- """Background task to clean up completed LLM requests"""
+ """Background task to monitor LLM provider health"""
try:
while not self.shutdown_event.is_set():
- await llm_client.cleanup_pending_requests()
- pending_count = llm_client.get_pending_count()
+ # Check provider health periodically
+ health_status = await multi_llm_client.health_check()
+ unhealthy_providers = [name for name, healthy in health_status.items() if not healthy]
- if pending_count > 0:
- logger.debug(f"LLM cleanup: {pending_count} pending background requests")
+ if unhealthy_providers:
+ logger.debug(f"Unhealthy LLM providers: {unhealthy_providers}")
- # Wait 30 seconds before next cleanup
- await asyncio.sleep(30)
+ # Wait 60 seconds before next health check
+ await asyncio.sleep(60)
except asyncio.CancelledError:
- logger.info("LLM cleanup task cancelled")
+ logger.info("LLM monitoring task cancelled")
except Exception as e:
logger.error(f"Error in LLM cleanup loop: {e}")
diff --git a/src/rag/vector_store.py b/src/rag/vector_store.py
index e55b93e..0146036 100644
--- a/src/rag/vector_store.py
+++ b/src/rag/vector_store.py
@@ -433,73 +433,155 @@ class VectorStoreManager:
query_embedding = await self._generate_embedding(query)
- results = self.community_collection.query(
- query_embeddings=[query_embedding],
- n_results=limit
- )
+ # Route to backend-specific implementation
+ if self.backend == "qdrant":
+ return await self._query_community_knowledge_qdrant(query, query_embedding, limit)
+ elif self.backend == "chromadb":
+ return await self._query_community_knowledge_chromadb(query, query_embedding, limit)
- memories = []
- for i, (doc, metadata, distance) in enumerate(zip(
- results['documents'][0],
- results['metadatas'][0],
- results['distances'][0]
- )):
- memory = VectorMemory(
- id=results['ids'][0][i],
- content=doc,
- memory_type=MemoryType.COMMUNITY,
- character_name=metadata.get('character_name', 'community'),
- timestamp=datetime.fromisoformat(metadata['timestamp']),
- importance=metadata['importance'],
- metadata=metadata
- )
- memory.metadata['similarity_score'] = 1 - distance
- memories.append(memory)
-
- return sorted(memories, key=lambda m: m.metadata.get('similarity_score', 0), reverse=True)
+ return []
except Exception as e:
- log_error_with_context(e, {"query": query, "component": "community_knowledge"})
+ log_error_with_context(e, {"query": query})
return []
+ async def _query_community_knowledge_chromadb(self, query: str, query_embedding: List[float], limit: int) -> List[VectorMemory]:
+ """Query community knowledge using ChromaDB"""
+ results = self.community_collection.query(
+ query_embeddings=[query_embedding],
+ n_results=limit
+ )
+
+ memories = []
+ for i, (doc, metadata, distance) in enumerate(zip(
+ results['documents'][0],
+ results['metadatas'][0],
+ results['distances'][0]
+ )):
+ memory = VectorMemory(
+ id=results['ids'][0][i],
+ content=doc,
+ memory_type=MemoryType.COMMUNITY,
+ character_name=metadata.get('character_name', 'community'),
+ timestamp=datetime.fromisoformat(metadata['timestamp']),
+ importance=metadata['importance'],
+ metadata=metadata
+ )
+ memory.metadata['similarity_score'] = 1 - distance
+ memories.append(memory)
+
+ return sorted(memories, key=lambda m: m.metadata.get('similarity_score', 0), reverse=True)
+
+ async def _query_community_knowledge_qdrant(self, query: str, query_embedding: List[float], limit: int) -> List[VectorMemory]:
+ """Query community knowledge using Qdrant"""
+ search_result = self.qdrant_client.search(
+ collection_name=self.collection_name,
+ query_vector=query_embedding,
+ limit=limit,
+ with_payload=True
+ )
+
+ memories = []
+ for point in search_result:
+ payload = point.payload
+ if payload.get('memory_type') == MemoryType.COMMUNITY.value:
+ memory = VectorMemory(
+ id=str(point.id),
+ content=payload['content'],
+ memory_type=MemoryType.COMMUNITY,
+ character_name=payload.get('character_name', 'community'),
+ timestamp=datetime.fromisoformat(payload['timestamp']),
+ importance=payload['importance'],
+ metadata=payload
+ )
+ memory.metadata['similarity_score'] = point.score
+ memories.append(memory)
+
+ return memories
+
async def get_creative_knowledge(self, character_name: str, query: str, limit: int = 5) -> List[VectorMemory]:
"""Query character's creative knowledge base"""
try:
if character_name not in self.creative_collections:
return []
- collection = self.creative_collections[character_name]
query_embedding = await self._generate_embedding(query)
- results = collection.query(
- query_embeddings=[query_embedding],
- n_results=limit
- )
+ # Route to backend-specific implementation
+ if self.backend == "qdrant":
+ return await self._get_creative_knowledge_qdrant(character_name, query, query_embedding, limit)
+ elif self.backend == "chromadb":
+ return await self._get_creative_knowledge_chromadb(character_name, query, query_embedding, limit)
- memories = []
- for i, (doc, metadata, distance) in enumerate(zip(
- results['documents'][0],
- results['metadatas'][0],
- results['distances'][0]
- )):
- memory = VectorMemory(
- id=results['ids'][0][i],
- content=doc,
- memory_type=MemoryType.CREATIVE,
- character_name=character_name,
- timestamp=datetime.fromisoformat(metadata['timestamp']),
- importance=metadata['importance'],
- metadata=metadata
- )
- memory.metadata['similarity_score'] = 1 - distance
- memories.append(memory)
-
- return sorted(memories, key=lambda m: m.metadata.get('similarity_score', 0), reverse=True)
+ return []
except Exception as e:
log_error_with_context(e, {"character": character_name, "query": query})
return []
+ async def _get_creative_knowledge_chromadb(self, character_name: str, query: str, query_embedding: List[float], limit: int) -> List[VectorMemory]:
+ """Get creative knowledge using ChromaDB"""
+ collection = self.creative_collections[character_name]
+ results = collection.query(
+ query_embeddings=[query_embedding],
+ n_results=limit
+ )
+
+ memories = []
+ for i, (doc, metadata, distance) in enumerate(zip(
+ results['documents'][0],
+ results['metadatas'][0],
+ results['distances'][0]
+ )):
+ memory = VectorMemory(
+ id=results['ids'][0][i],
+ content=doc,
+ memory_type=MemoryType.CREATIVE,
+ character_name=character_name,
+ timestamp=datetime.fromisoformat(metadata['timestamp']),
+ importance=metadata['importance'],
+ metadata=metadata
+ )
+ memory.metadata['similarity_score'] = 1 - distance
+ memories.append(memory)
+
+ return sorted(memories, key=lambda m: m.metadata.get('similarity_score', 0), reverse=True)
+
+ async def _get_creative_knowledge_qdrant(self, character_name: str, query: str, query_embedding: List[float], limit: int) -> List[VectorMemory]:
+ """Get creative knowledge using Qdrant"""
+ from qdrant_client.models import Filter, FieldCondition
+
+ search_result = self.qdrant_client.search(
+ collection_name=self.collection_name,
+ query_vector=query_embedding,
+ limit=limit,
+ with_payload=True,
+ query_filter=Filter(
+ must=[
+ FieldCondition(key="character_name", match={"value": character_name}),
+ FieldCondition(key="memory_type", match={"value": MemoryType.CREATIVE.value})
+ ]
+ )
+ )
+
+ memories = []
+ for point in search_result:
+ payload = point.payload
+ if payload.get('memory_type') == MemoryType.CREATIVE.value and payload.get('character_name') == character_name:
+ memory = VectorMemory(
+ id=str(point.id),
+ content=payload['content'],
+ memory_type=MemoryType.CREATIVE,
+ character_name=character_name,
+ timestamp=datetime.fromisoformat(payload['timestamp']),
+ importance=payload['importance'],
+ metadata=payload
+ )
+ memory.metadata['similarity_score'] = point.score
+ memories.append(memory)
+
+ return memories
+
async def consolidate_memories(self, character_name: str) -> Dict[str, Any]:
"""Consolidate similar memories to save space"""
try:
diff --git a/src/utils/config.py b/src/utils/config.py
index b84b0cb..5ef1c47 100644
--- a/src/utils/config.py
+++ b/src/utils/config.py
@@ -25,15 +25,27 @@ class DiscordConfig(BaseModel):
guild_id: str
channel_id: str
+class LLMProviderConfig(BaseModel):
+ """Configuration for a single LLM provider"""
+ type: str # openai, openrouter, gemini, custom
+ enabled: bool = True
+ priority: int = 0
+ config: Dict[str, Any] = {}
+
class LLMConfig(BaseModel):
+ """Multi-provider LLM configuration"""
+ # Legacy single provider config (for backwards compatibility)
base_url: str = "http://localhost:11434"
model: str = "llama2"
timeout: int = 300
max_tokens: int = 2000
temperature: float = 0.8
- max_prompt_length: int = 6000
+ max_prompt_length: int = 16000
max_history_messages: int = 5
max_memories: int = 5
+
+ # New multi-provider config
+ providers: Dict[str, LLMProviderConfig] = {}
class ConversationConfig(BaseModel):
min_delay_seconds: int = 30
@@ -141,7 +153,7 @@ def get_settings() -> Settings:
timeout=int(os.getenv("LLM_TIMEOUT", "300")),
max_tokens=int(os.getenv("LLM_MAX_TOKENS", "2000")),
temperature=float(os.getenv("LLM_TEMPERATURE", "0.8")),
- max_prompt_length=int(os.getenv("LLM_MAX_PROMPT_LENGTH", "6000")),
+ max_prompt_length=int(os.getenv("LLM_MAX_PROMPT_LENGTH", "16000")),
max_history_messages=int(os.getenv("LLM_MAX_HISTORY_MESSAGES", "5")),
max_memories=int(os.getenv("LLM_MAX_MEMORIES", "5"))
),
diff --git a/sync_vectors.py b/sync_vectors.py
new file mode 100644
index 0000000..3694032
--- /dev/null
+++ b/sync_vectors.py
@@ -0,0 +1,73 @@
+#!/usr/bin/env python3
+"""
+Sync existing PostgreSQL memories to Qdrant vector database
+"""
+
+import asyncio
+import logging
+from datetime import datetime, timezone
+from database.connection import init_database, get_db_session
+from database.models import Memory, Character
+from rag.vector_store import VectorStoreManager, VectorMemory, MemoryType
+from sqlalchemy import select
+
+logger = logging.getLogger(__name__)
+
+async def sync_memories_to_qdrant():
+ """Sync all existing memories from PostgreSQL to Qdrant"""
+
+ # Initialize database
+ await init_database()
+
+ # Initialize vector store
+ vector_store = VectorStoreManager()
+
+ print("🔄 Starting memory sync to Qdrant...")
+
+ async with get_db_session() as session:
+ # Get all memories with character names
+ query = select(Memory, Character.name).join(
+ Character, Memory.character_id == Character.id
+ ).order_by(Memory.timestamp)
+
+ results = await session.execute(query)
+ memories_with_chars = results.fetchall()
+
+ print(f"Found {len(memories_with_chars)} memories to sync")
+
+ synced_count = 0
+ error_count = 0
+
+ for memory, character_name in memories_with_chars:
+ try:
+ # Convert to vector memory format
+ vector_memory = VectorMemory(
+ id=str(memory.id),
+ character_name=character_name,
+ content=memory.content,
+ memory_type=MemoryType.PERSONAL,
+ importance=memory.importance_score,
+ timestamp=memory.timestamp or datetime.now(timezone.utc),
+ metadata={
+ "tags": memory.tags or [],
+ "memory_id": memory.id,
+ "character_id": memory.character_id,
+ "memory_type": memory.memory_type
+ }
+ )
+
+ # Store in vector database
+ await vector_store.store_memory(vector_memory)
+ synced_count += 1
+
+ if synced_count % 10 == 0:
+ print(f" Synced {synced_count}/{len(memories_with_chars)} memories...")
+
+ except Exception as e:
+ error_count += 1
+ print(f" Error syncing memory {memory.id}: {e}")
+
+ print(f"✅ Sync complete: {synced_count} synced, {error_count} errors")
+
+if __name__ == "__main__":
+ asyncio.run(sync_memories_to_qdrant())
\ No newline at end of file
diff --git a/test_llm_current_provider.py b/test_llm_current_provider.py
new file mode 100644
index 0000000..3026de6
--- /dev/null
+++ b/test_llm_current_provider.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python3
+"""
+Quick test to check if current provider is properly detected
+"""
+
+import asyncio
+import sys
+import os
+
+# Add src to path
+sys.path.insert(0, "src")
+
+async def test_current_provider():
+ """Test that current provider is properly detected"""
+ try:
+ # Set minimal env vars to avoid validation errors
+ import os
+ os.environ.setdefault('DISCORD_TOKEN', 'test')
+ os.environ.setdefault('DISCORD_GUILD_ID', '123')
+ os.environ.setdefault('DISCORD_CHANNEL_ID', '456')
+
+ from llm.multi_provider_client import MultiProviderLLMClient
+ from utils.config import get_settings
+
+ print("Testing current LLM provider detection...")
+ print("=" * 50)
+
+ # Check current settings
+ settings = get_settings()
+ print(f"Current LLM config:")
+ print(f" Base URL: {settings.llm.base_url}")
+ print(f" Model: {settings.llm.model}")
+ print(f" Providers configured: {len(settings.llm.providers) if settings.llm.providers else 0}")
+
+ # Initialize client
+ client = MultiProviderLLMClient()
+ await client.initialize()
+
+ # Check provider info
+ provider_info = client.get_provider_info()
+ current_provider = client.get_current_provider()
+ health_status = await client.health_check()
+
+ print(f"\nProvider Status:")
+ print(f" Current provider: {current_provider}")
+ print(f" Total providers: {len(provider_info)}")
+
+ for name, info in provider_info.items():
+ healthy = health_status.get(name, False)
+ is_current = name == current_provider
+ print(f"\n {name}:")
+ print(f" Type: {info['type']}")
+ print(f" Model: {info['current_model']}")
+ print(f" Enabled: {info['enabled']}")
+ print(f" Priority: {info['priority']}")
+ print(f" Healthy: {healthy}")
+ print(f" Current: {is_current}")
+
+ if current_provider:
+ print(f"\n✅ Current provider detected: {current_provider}")
+ else:
+ print(f"\n❌ No current provider detected!")
+
+ return current_provider is not None
+
+ except Exception as e:
+ print(f"\n❌ Error: {e}")
+ import traceback
+ traceback.print_exc()
+ return False
+
+if __name__ == "__main__":
+ success = asyncio.run(test_current_provider())
+ if not success:
+ sys.exit(1)
\ No newline at end of file